\documentclass[xcolor=dvipsnames]{beamer}

\setbeamertemplate{navigation symbols}{}
% \setbeamertemplate{background}[grid][step=1cm]
\useoutertheme{infolines}
\usecolortheme[named=violet]{structure}
\setbeamertemplate{items}[circle]

\usepackage{tikz}
\usetikzlibrary{arrows,shapes,backgrounds}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{color}
\usepackage{hyperref}

\author[Brett Viren]{Brett Viren}
\institute[BNL]
{
  Physics Department
  
  \includegraphics[height=1.5cm]{dyb_logo}
  \includegraphics[height=1.5cm]{bnl-logo}
  \includegraphics[height=1.5cm]{dyw_logo}
}
\title[DBI]{Experience with Filling a DBI Table}
\date[Offline Workshop]{Offline Workshop, IHEP, 2011-07-11}

\newcommand{\fixme}[1]{\textbf{FIXME:} \textit{#1}}
\newcommand{\code}[1]{\textcolor{blue}{\texttt{#1}}}

\begin{document}

\lstset{%
  emphstyle=\color{red},
  keywordstyle=\color{black}\bfseries,
  basicstyle=\footnotesize,
  identifierstyle=\color{DarkOrchid}\ttfamily,
  commentstyle=\color{Brown}\ttfamily,
  stringstyle=\color{blue}\ttfamily,
  showstringspaces=false}

\begin{frame}
  \titlepage
\end{frame}
\begin{frame}
  \frametitle{Outline}
  \tableofcontents    
\end{frame}

\section{Overview}

\begin{frame}[fragile]
  \frametitle{Context}

  Starting point:
  \begin{itemize}
  \item \code{StaticCableSvc} uses text files, eg \code{feeCableMap.txt}.
  \item There are 3 schema for these text files (2 for FEE, 1 for FEC).
  \item These files have to unrelated maps: sensor-channel and
    sensor/channel-hardwareID.
  \item A ``Master'' text file pretends some features like DBI
  \item There is also a \code{DbiCableSvc} that works off the combined schema
  \item Many hard coding of ``\code{StaticCableSvc}'' name in user config code.
  \end{itemize}

  
\end{frame}

\begin{frame}[fragile]
  \frametitle{Overview of the Steps Taken}
  
  Broad steps
  \begin{itemize}
  \item Refactor schema to make two DBI table pairs (\code{CableMap}, \code{HardwareID})
  \item New ICableSvc implementation (\code{DybCableSvc}) to front
    these tables
  \item Convert and load existing text files into the DBI tables following SOP
  \item Make a global switch between ``static'' and ``dbi'' services in \code{nuwa.py}
  \item Change existing code to use generic service name
  \end{itemize}
  
  Details at: \url{http://dayabay.ihep.ac.cn/tracs/dybsvn/ticket/880}
\end{frame}


\section{Standard Operating Procedure}

\begin{frame}[fragile]
  \frametitle{Which Database}
  
  % Add a line to address comment from craig:
  % pg 8 - did you use tmp_offline_db or tmp_bviren_offline_db...?
  % Please change throughout to tmp_bviren_offline_db so that people
  % imitating your steps from your slides will realize that they should
  % be using their own temp db, rather than a shared, temp db.

  In NuWa, the main database named \code{offline\_db}.  \\
  In DBI SOP there is also one named \code{tmp\_offline\_db}. \\
  They connect to a real database through \code{$\sim$/.my.cnf}
  \begin{lstlisting}[language=sh]
[offline_db]
host            = home2.phy.bnl.gov
user            = reader
#host           = dayabaydb.lbl.gov
#user           = dayabay
password        = XXXXXX
database        = offline_db

[tmp_offline_db]
host            = home2.phy.bnl.gov
user            = bviren
password        = XXXXXX
database        = tmp_bviren_offline_db
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Standard Operating Procedure for DBI Filling}

  \begin{itemize}
  \item Rigorous procedures for updating the master DB.
  \item Highly controlled, carefully recorded, easily reproducible.
  \item Well documented in the Offline User Manual\footnote{At \url{http://dayabay.bnl.gov/oum/sop/} and other locations.}
    \begin{itemize}
    \item Definitions, DBI explanation, software rules, workflow, etc
    \end{itemize}
  \end{itemize}

  % address comment from craig:
  % Somewhere (pg 6?) I'd like you to at least say, and preferably
  % write, that using the *actual* DBI API is necessary (rather than
  % writing into Vld tables manually)... I know this seems obvious,
  % but somehow this mentality seems to persist in various corners.


\end{frame}

\begin{frame}[fragile]
  \frametitle{SOP Workflow}

  Expanded from the SOP workflow\footnote{\url{http://dayabay.bnl.gov/oum/sop/dbops/\#workflow-outline}}

  \begin{enumerate}
  \item Obtain quantities to be inserted into \code{offline\_db} tables.
  \item Validate quantities using expert understanding and test codes.
  \item Make a temporary (partial) copy of \code{offline\_db} to \code{tmp\_offline\_db}
  \item Checkout current \code{tmp\_offline\_db} CSV catalog from \code{dybaux} SVN
  \item Dump \code{tmp\_offline\_db} on top of CSV catalog.  
    \begin{itemize}
    \item Assure no changes!
    \end{itemize}
  \item Insert new quantities into \code{tmp\_offline\_db}
  \item Dump modified \code{tmp\_offline\_db} on top of CSV catalog.
    \begin{itemize}
    \item Assure only expected changes!
    \end{itemize}
  \item Commit to SVN and notify DB masters of the pending update.
  \end{enumerate}
  
\end{frame}

\begin{frame}[fragile]
  \frametitle{SOP Details: Step 3}
  \begin{enumerate}
  \item[3] Make a temporary (partial) copy of \code{offline\_db} to \code{tmp\_offline\_db}
  \end{enumerate}

  \begin{lstlisting}[language=sh,emph={dump,load}]
# Dump offline_db to SQL file
db.py offline_db dump offline_db.sql

# Load into tmp_offline_db - drops any preexisting
db.py tmp_offline_db load offline_db.sql
  \end{lstlisting}

  Depending on network and size of tables, both steps should take only
  a few seconds.  Future work (S.Blyth) may allow targeting specific
  table pairs.
  
  % Add a line to address comment from craig:
  % pg 8 - did you use tmp_offline_db or tmp_bviren_offline_db...?
  % Please change throughout to tmp_bviren_offline_db so that people
  % imitating your steps from your slides will realize that they should
  % be using their own temp db, rather than a shared, temp db.

\end{frame}

\begin{frame}[fragile]
  \frametitle{SOP Details: Steps 4, 5}
  \begin{enumerate}
  \item[4] Checkout current \code{tmp\_offline\_db} CSV catalog from \code{dybaux} SVN
  \item[5] Dump \code{tmp\_offline\_db} on top of CSV catalog.  
    \begin{itemize}
    \item Assure no changes!
    \end{itemize}
  \end{enumerate}

  \begin{lstlisting}[language=sh,emph={rdumpcat}]
# checkout CVS catalog
CSVCAT=http://dayabay.ihep.ac.cn/svn/dybaux/catalog
svn co --username=bv $CSVCAT/tmp_offline_db tmp_offline_db

# Dump tmp DB on top of CSV catalog
db.py tmp_offline_db rdumpcat tmp_offline_db/

# Check for no change
svn diff tmp_offline_db/
  \end{lstlisting}
%$
\end{frame}

\begin{frame}[fragile]
  \frametitle{SOP Details: Step 6}

  % Address comment from craig:
  % You don't mention the decision point, or final implementation for
  % the insertion date in the movement from static to DB... is it
  % worth describing that?


  \begin{enumerate}
  \item[6] Insert new quantities into \code{tmp\_offline\_db}
  \end{enumerate}

  For \code{CableMap}/\code{HardwareID} the commit dates of the
  \code{fe[ec]CableMap.txt} files and their contents are used as
  input and a control ``\code{commit.txt}'' file drives things.

  \begin{lstlisting}[language=sh]
pushd $TESTCABLEMAPROOT/run/
../share/load_static.py commits-r12820.txt [1|2]
  \end{lstlisting}
%$

  The \code{[1|2]} selects Data vs. Simulation for the context.  The
  \code{commits-r12820.txt} file looks like:

{\footnotesize
\begin{verbatim}
r12820 2011-06-30 11:11:45 -0400 feeCableMap.txt | bv | (Thu, 30 Jun 2011) | 2 lines
\end{verbatim}}
and is constructed via the output of \code{svn log} for that commit.

\end{frame}

\begin{frame}[fragile]
  \frametitle{SOP Details: Step 7}
  \begin{enumerate}
  \item[7] Dump modified \code{tmp\_offline\_db} on top of CSV catalog.
    \begin{itemize}
    \item Assure only expected changes!
    \end{itemize}
  \end{enumerate}
  \begin{lstlisting}[language=sh,emph={rdumpcat}]
# Dump on top of CSV catalog
db.py tmp_offline_db rdumpcat /data0/bv/offline/database/tmp_offline_db/

# check that correct files were touched
cd tmp_offline_db/
svn status                      
svn diff LOCALSEQNO/LOCALSEQNO.csv
  \end{lstlisting}
\end{frame}

\section{Automate the Filling Process}

\begin{frame}[fragile]
  \frametitle{Automation for CableMap/HardwareID}
  
  \begin{itemize}
  \item Package: \code{Database/TableTests/TestCableMap/}
    \begin{itemize}
    \item others should consider adding their own packages under
      \code{TableTests}
    \end{itemize}
  \item Driving script \code{share/dlfcrs.sh}
    \begin{itemize}
    \item ``dump, load, fill, checkout, rdumpcat, sync''
    \item ugly, developed while working out the SOP kinks
    \item requires re-editing to get different behavior
    \end{itemize}
  \end{itemize}
  
  \begin{lstlisting}[language=sh]
prime_db        # Step 3
prime_svn       # Steps 4 and 5
# load revision of given file for data(1) or sim(2) context
do_one [revision] [file] [1|2]
  \end{lstlisting}

  \begin{itemize}
  \item   If more than one revision is loaded in a single filling session the
    first two priming functions should be commented out.
  \item Each \code{do\_one} is run and then commented out and the SVN
    commit revision is noted to provide a record of what has been
    loaded.
  \end{itemize}
\end{frame}

\section{Service Mapping for Static $\longleftrightarrow$ Dbi}

\begin{frame}[fragile]
  \frametitle{Service Mapping}
  In \code{DybPython.Control.NuWa} (ie, \code{nuwa.py}) new utility method:
  \begin{lstlisting}[language=Python,emph={add_service_redirect,alias,name}]
def add_service_redirect(self,alias,name):
    """
    Make alias an alias for given service.  
    Should be called during configuration only
    """
  \end{lstlisting}
  If \code{DBCONF} is defined or \code{nuwa.py --dbconf=} option given switch
  between static and (some) DBI versions of the data service trio:
  \begin{itemize}
  \item \code{CableSvc} (\code{StaticCableSvc} vs. \code{DybCableSvc})
  \item \code{SimDataSvc} (\code{StaticSimDataSvc} only)
  \item \code{CalibDataSvc} (\code{StaticCalibDataSvc} only)
  \end{itemize}
  Not all have DBI versions yet, update the switch as they are developed.\\
  Will need to be extended for any novel services developed.
  
  % Address comment from craig
  % One more point. The implementation details on pg 13 are important
  % to emphasize, I think. Not only to alert people to the ability to
  % rewind to the old services with job options, but also to educate
  % folks about how to think about abstract interfaces to
  % services. This is a powerful architectural decision within Gaudi
  % and this is the first time we've exercised it in real usage. I
  % think it's worth highlighting as that aspect may escape some.


\end{frame}

\begin{frame}[fragile]
  \frametitle{Migrating existing code} 

  There exists many places that use explicitly the name
  ``\code{StaticCableMap}''
  \begin{itemize}
  \item Default property values
  \item Job modules
  \item Hard coded
  \end{itemize}
  Most have now been fixed to use the generic ``\code{CableMap}''.\\
  Did not touch code that used something besides the standard \code{feeCableMap.txt}\\
  Converted some, but not all, of \code{SimDataSvc} and \code{CalibDataSvc}.
\end{frame}

\begin{frame}[fragile]
  \frametitle{NB: Hard Coded Local Paths}

  During the migration I found a few cases of hard coding local path!
  Many were fixed by the migration but one should instead rely on the
  many environment variables that NuWa gives - at least one for each
  package!

  \begin{lstlisting}[language=Python]
    # bad
    CableMapFile = "/afs/ihep.ac.cn/soft/dayabay/NuWa/daily"+\
                   "/release/NuWa-trunk/dybgaudi/DataModel"+\
                   "/DataSvc/share/feeCableMap.txt"
    # better
    import os
    dsroot = os.environ.get("DATASVCROOT")
    CableMapFile = dsroot + "/share/feeCableMap.txt"

    # best
    # ... nothing here, since the nuwa.py now handles this
  \end{lstlisting}
    
\end{frame}  
  
\section{Current Status and Still To Do}

\begin{frame}[fragile]
  \frametitle{Current Status}

  \begin{itemize}
  \item Migration to a SOP-compliant DBI tables for
    CableMap/HardwareID done.
    \begin{itemize}
    \item Future updates probably best done by committing static table
      and repeating the \code{dlfcrs.sh} procedures.
    \end{itemize}
  \item New \code{DybCableSvc} added to new \code{DybSvc/DybMetaDataSvc/}.
    \begin{itemize}
    \item Recommend all SOP-compliant DBI services (at least among the
      trio of data services) be added to this package.
    \end{itemize}
  \item Single Static/DBI switch added to \code{nuwa.py}.
  \item Most C++ and Python code modified to use generic
    \code{CableSvc} name.
  \end{itemize}
  
\end{frame}

\begin{frame}[fragile]
  \frametitle{Still To Do}
  
  \begin{itemize}
  \item \code{SimDataSvc} will get the treatment next.
    \begin{itemize}
    \item I leave \code{CalibDataSvc} to the calib experts but am
      available to help.
    \end{itemize}
  \item Obsolescence of \code{DbiCableSvc} and \code{StaticCableSvc}
    \begin{itemize}
    \item Is there a reason to keep \code{StaticCableSvc} in the long run?
    \end{itemize}
  \item Move/remove \code{DataModel/DataSvc} and \code{Database/DbiDataSvc}
    \begin{itemize}
    \item Irrespective of the migration they better belong in \code{DybSvc/}
    \item I would like all new SOP-compliant services are put in
      \code{DybSvc/DybMetaDataSvc} so we can remove \code{DbiDataSvc}
    \item Remove \code{DataModel/DataSvc} if we no longer want the
      static counterparts.
    \end{itemize}
  \end{itemize}
  
\end{frame}

\end{document}