exampledoloop.html   [plain text]


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Example Processing Loop</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
    <link rel="home" href="index.html" title="Getting Started with Replicated Berkeley DB Applications" />
    <link rel="up" href="fwrkmasterreplica.html" title="Chapter 4. Replica versus Master Processes" />
    <link rel="previous" href="processingloop.html" title="Processing Loop" />
    <link rel="next" href="addfeatures.html" title="Chapter 5. Additional Features" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Example Processing Loop</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="processingloop.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 4. Replica versus Master Processes</th>
          <td width="20%" align="right"> <a accesskey="n" href="addfeatures.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="exampledoloop"></a>Example Processing Loop</h2>
          </div>
        </div>
        <div></div>
      </div>
      <p>
                          In this section we take the example
                          processing loop that we presented in the
                          previous section and we flesh it out to
                          provide a more complete example. We do this
                          by updating the
                          <tt class="function">doloop()</tt>
                          function that our original transaction
                          application used 
                          <span>(see <a href="simpleprogramlisting.html#doloop_c">Function: doloop()</a>)</span>
                          
                          
                          to fully support our replicated application. 
                  </p>
      <p>
                          In the following example code, code that we
                          add to the original example is presented in 
                          <b class="userinput"><tt>bold</tt></b>.
                  </p>
      <p>
                          To begin, we include a new header file into
                          our application so that we can check for the 
                          <tt class="literal">ENOENT</tt> return value later
                          in our processing loop. We also define our 
                            <tt class="literal">APP_DATA</tt>
                          structure, and we define a
                          <tt class="literal">sleeptime</tt> value. 

                          <span>
                          Finally, we
                          add a new forward declaration for our event
                          callback.
                          </span>

                          
                  </p>
      <pre class="programlisting">/*
 * File: rep_mgr.c
 */

#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
<b class="userinput"><tt>#include &lt;errno.h&gt;</tt></b>
#ifndef _WIN32
#include &lt;unistd.h&gt;
#endif

#include &lt;db.h&gt;

#ifdef _WIN32
extern int getopt(int, char * const *, const char *);
#endif


#define CACHESIZE   (10 * 1024 * 1024)
#define DATABASE    "quote.db"
<b class="userinput"><tt>#define SLEEPTIME 3</tt></b>

const char *progname = "rep_mgr";

<b class="userinput"><tt>typedef struct {
    int is_master;
} APP_DATA;</tt></b>

int create_env(const char *, DB_ENV **);
int env_init(DB_ENV *, const char *);
int doloop (DB_ENV *);
int print_stocks(DBC *);
<b class="userinput"><tt>void *event_callback(DB_ENV *, u_int32_t, void *);</tt></b> </pre>
      <p>
        In our <tt class="function">main()</tt> function, most of what we
        have to add to it is some new variable declarations and
        initializations:
</p>
      <pre class="programlisting">int
main(int argc, char *argv[])
{
    extern char *optarg;
    DB_ENV *dbenv;
    const char *home;
    char ch, *host, *portstr;
    int ret, local_is_set, totalsites;
    u_int32_t port;
    <b class="userinput"><tt>/* Used to track whether this is a replica or a master */
    APP_DATA my_app_data;

    my_app_data.is_master = 0;  /* Assume that we start as a replica */</tt></b>
    dbenv = NULL;

    ret = local_is_set = totalsites = 0;
    home = NULL;</pre>
      <p>
                The rest of our <tt class="function">main()</tt> function
                is unchanged, except that we make our
                <tt class="literal">APP_DATA</tt> structure available through our
                environment handle's <tt class="methodname">app_private</tt>
                field:
        </p>
      <pre class="programlisting">
    if ((ret = create_env(progname, &amp;dbenv)) != 0)
            goto err;

    <b class="userinput"><tt>/* Make APP_DATA available through the environment handle */
    dbenv-&gt;app_private = &amp;my_app_data;</tt></b>

    /* Default priority is 100 */
    dbenv-&gt;rep_set_priority(dbenv, 100);

    /* Collect the command line options */
    while ((ch = getopt(argc, argv, "h:m:o:p:")) != EOF)
        switch (ch) {
        case 'h':
            home = optarg;
            break;
        /* Set the host and port used by this environment */
        case 'm':
            host = strtok(optarg, ":");
            if ((portstr = strtok(NULL, ":")) == NULL) {
                fprintf(stderr, "Bad host specification.\n");
                goto err;
            }
            port = (unsigned short)atoi(portstr);
            if (dbenv-&gt;repmgr_set_local_site(dbenv, host, port, 0) != 0) {
                fprintf(stderr,
                    "Could not set local address %s.\n", host);
                goto err;
            }
            local_is_set = 1;
            break;
        /* Identify another site in the replication group */
        case 'o':
            host = strtok(optarg, ":");
            if ((portstr = strtok(NULL, ":")) == NULL) {
                fprintf(stderr, "Bad host specification.\n");
                goto err;
            }
            port = (unsigned short)atoi(portstr);
            if (dbenv-&gt;repmgr_add_remote_site(dbenv, host, port, 0) != 0) {
                fprintf(stderr,
                    "Could not add site %s.\n", host);
                goto err;
            }
            break;
        /* Set the number of sites in this replication group */
        case 'n':
            totalsites = atoi(optarg);
            if ((ret = dbenv-&gt;rep_set_nsites(dbenv, totalsites)) != 0)
                dbenv-&gt;err(dbenv, ret, "set_nsites");
            break;
        /* Set this replica's election priority */
        case 'p':
            dbenv-&gt;rep_set_priority(dbenv, atoi(optarg));
            break; 
        case '?':
        default:
            usage();
        }

    /* Error check command line. */
    if (home == NULL || !local_is_set || !totalsites)
        usage();

    if ((ret = env_init(dbenv, home)) != 0)
            goto err;

    if ((ret = dbenv-&gt;repmgr_start(dbenv, 3, DB_REP_ELECTION)) != 0)
        goto err;

    /* Sleep to give ourselves time to find a master. */
    sleep(5);

    if ((ret = doloop(dbenv)) != 0) {
        dbenv-&gt;err(dbenv, ret, "Application failed");
        goto err;
    }

err: if (dbenv != NULL)
        (void)dbenv-&gt;close(dbenv, 0);

    return (ret);
} </pre>
      <p>
            Having updated our <tt class="function">main()</tt>, we must also
            update our <tt class="function">create_env()</tt> function to
            register our <tt class="function">event_callback</tt> callback.
            Notice that our <tt class="function">env_init()</tt> function, which is
            responsible for actually opening our environment handle, is
            unchanged:
    </p>
      <pre class="programlisting">int
create_env(char *progname, DB_ENV **dbenvp)
{
    DB_ENV *dbenv;
    int ret;

    if ((ret = db_env_create(&amp;dbenv, 0)) != 0) {
        fprintf(stderr, "can't create env handle: %s\n",
            db_strerror(ret));
        return (ret);
    }

    dbenv-&gt;set_errfile(dbenv, stderr);
    dbenv-&gt;set_errpfx(dbenv, progname);
    <b class="userinput"><tt>(void)dbenv-&gt;set_event_notify(dbenv, event_callback);</tt></b>

    *dbenvp = dbenv;
    return (0);
} 

int
env_init(DB_ENV *dbenv, const char *home)
{
    u_int32_t flags;
    int ret;

    (void)dbenv-&gt;set_cachesize(dbenv, 0, CACHESIZE, 0);
    (void)dbenv-&gt;set_flags(dbenv, DB_TXN_NOSYNC, 1);

    flags = DB_CREATE |
            DB_INIT_LOCK |
            DB_INIT_LOG |
            DB_INIT_MPOOL |
            DB_INIT_TXN |
            DB_INIT_REP |
            DB_THREAD |
            DB_RECOVER;
    if ((ret = dbenv-&gt;open(dbenv, home, flags, 0)) != 0)
        dbenv-&gt;err(dbenv, ret, "can't open environment");
    return (ret);
}</pre>
      <p>
        That done, we need to implement our
        <tt class="function">event_callback()</tt> callback. Note that what we use
        here is no different from the callback that we described in
        the previous section. However, for the sake of completeness we
        provide the implementation here again.
</p>
      <pre class="programlisting">
        <b class="userinput">
          <tt>/*
 * A callback used to determine whether the local environment is a replica
 * or a master. This is called by the replication framework
 * when the local replication environment changes state.
 */
void *
event_callback(DB_ENV *dbenv, u_int32_t which, void *info)
{
    APP_DATA *app = dbenv-&gt;app_private;

    info = NULL;                /* Currently unused. */

    switch (which) {
    case DB_EVENT_REP_MASTER:
        app-&gt;is_master = 1;
        break;

    case DB_EVENT_REP_CLIENT:
        app-&gt;is_master = 0;
        break;

    case DB_EVENT_REP_STARTUPDONE: /* fallthrough */
    case DB_EVENT_REP_NEWMASTER:
        /* Ignore. */
        break;

    default:
        dbenv-&gt;errx(dbenv, "ignoring event %d", which);
    }
}</tt>
        </b>
      </pre>
      <p>
        That done, we need to update our
        <tt class="function">doloop()</tt> 
        <span>function. This is the place where we most
        heavily modify our application.</span>

        
</p>
      <p>
        We begin by introducing <tt class="literal">APP_DATA</tt> to the
        function:
</p>
      <pre class="programlisting">/*
 * Provides the main data processing function for our application.
 * This function provides a command line prompt to which the user
 * can provide a ticker string and a stock price. Once a value is
 * entered to the application, the application writes the value to
 * the database and then displays the entire database.
 */
#define BUFSIZE 1024
int
doloop(DB_ENV *dbenv)
{
    DB *dbp;
    <b class="userinput"><tt>APP_DATA *app_data;</tt></b>
    DBT key, data;
    char buf[BUFSIZE], *rbuf;
    int ret;
    u_int32_t flags;

    dbp = NULL;
    ret = 0;
    memset(&amp;key, 0, sizeof(key));
    memset(&amp;data, 0, sizeof(data));
    <b class="userinput"><tt>app_data = dbenv-&gt;app_private;</tt></b></pre>
      <p>
        Next we begin to modify our main loop. 
        To start, upon entering the loop we create the database handle and configure it as
        normal. But we also have to decide what flags we will use for
        the open. Again, it depends on whether we are a replica or a
        master.
    </p>
      <pre class="programlisting">    for (;;) {
        if (dbp == NULL) {
            if ((ret = db_create(&amp;dbp, dbenv, 0)) != 0)
                return (ret);

            /* Set page size small so page allocation is cheap. */
            if ((ret = dbp-&gt;set_pagesize(dbp, 512)) != 0)
                goto err;

            <b class="userinput"><tt>flags = DB_AUTO_COMMIT;
            if (app_data-&gt;is_master)
                flags |= DB_CREATE;</tt></b> </pre>
      <p>
    When we open the database, we modify our error handling to
    account for the case where the database does not yet exist. This can
    happen if our code is running as a replica and the replication framework has not
    yet had a chance to create the databases for us. Recall that replicas never
    write to their own databases directly, and so they cannot
    create databases on their own.
</p>
      <p>
        If we detect that the database does not yet exist, we simply
        close the database handle, sleep for a short period of time
        and then continue processing. This gives the replication framework a chance to
        create the database so that our replica can continue
        operations.
</p>
      <pre class="programlisting">            if ((ret = dbp-&gt;open(dbp,
                NULL, DATABASE, NULL, DB_BTREE, flags, 0)) != 0) {
                <b class="userinput"><tt>if (ret == ENOENT) {
                    printf(
                      "No stock database yet available.\n");
                    if ((ret = dbp-&gt;close(dbp, 0)) != 0) {
                        dbenv-&gt;err(dbenv, ret,
                            "DB-&gt;close");
                        goto err;
                    }
                    dbp = NULL;
                    sleep(SLEEPTIME);
                    continue;
                }</tt></b>
                dbenv-&gt;err(dbenv, ret, "DB-&gt;open");
                goto err;
            }
        } </pre>
      <p>
        Next we modify our prompt, so that if the local process is running
        as a replica, we can tell from the shell that the prompt is for a
        read-only process.
    </p>
      <pre class="programlisting">        printf("QUOTESERVER<b class="userinput"><tt>%s</tt></b>&gt; ",
            <b class="userinput"><tt>app_data-&gt;is_master ? "" : " (read-only)");</tt></b>
        fflush(stdout); </pre>
      <p>
            When we collect data from the prompt, there is a case that says
            if no data is entered then show the entire stocks database.
            This display is performed by our
            <tt class="function">print_stocks()</tt> 
            <span>function</span> 
             
            (which has not
            required a modification since we first introduced it in 
            <a href="simpleprogramlisting.html#printstocks_c">
                            <span>Function: print_stocks()</span>
                            
                            
                    </a>).
    </p>
      <p>
            When we call 
               <span><tt class="function">print_stocks()</tt>, </span>
               
            we check for a dead replication handle. Dead
            replication handles happen whenever a replication election
            results in a previously committed transaction becoming
            invalid. This is an error scenario caused by a new master having a
            slightly older version of the data than the original
            master and so all replicas must modify their database(s) to
            reflect that of the new master. In this situation, some
            number of previously committed transactions may have to be
            unrolled. From the replica's perspective, the database
            handles should all be closed and then opened again.
    </p>
      <pre class="programlisting">        if (fgets(buf, sizeof(buf), stdin) == NULL)
            break;
        if (strtok(&amp;buf[0], " \t\n") == NULL) {
            switch ((ret = print_stocks(dbp))) {
            case 0:
                continue;
            <b class="userinput"><tt>case DB_REP_HANDLE_DEAD:
                (void)dbp-&gt;close(dbp, DB_NOSYNC);
                dbp = NULL;
                dbenv-&gt;errx(dbenv, "Got a dead replication handle");
                continue; </tt></b>
            default:
                dbp-&gt;err(dbp, ret, "Error traversing data");
                goto err;
            }
        }
        rbuf = strtok(NULL, " \t\n");
        if (rbuf == NULL || rbuf[0] == '\0') {
            if (strncmp(buf, "exit", 4) == 0 ||
                strncmp(buf, "quit", 4) == 0)
                break;
            dbenv-&gt;errx(dbenv, "Format: TICKER VALUE");
            continue;
        }</pre>
      <p>
            That done, we need to add a little error checking to our
            command prompt to make sure the user is not attempting to
            modify the database at a replica. Remember, replicas must never
            modify their local databases on their own. This guards against
            that happening due to user input at the prompt.
    </p>
      <pre class="programlisting">        <b class="userinput"><tt>if (!app_data-&gt;is_master) {
            dbenv-&gt;errx(dbenv, "Can't update at client");
            continue;
        }</tt></b>
        key.data = buf;
        key.size = (u_int32_t)strlen(buf);

        data.data = rbuf;
        data.size = (u_int32_t)strlen(rbuf);

        if ((ret = dbp-&gt;put(dbp,
            NULL, &amp;key, &amp;data, 0)) != 0) {
            dbp-&gt;err(dbp, ret, "DB-&gt;put");
            goto err;
        }
    }

err:    if (dbp != NULL)
        (void)dbp-&gt;close(dbp, DB_NOSYNC);

    return (ret);
} </pre>
      <p>
        With that completed, we are all done updating our application
        for replication. 

        The only remaining 
        <span>function, <tt class="function">print_stocks()</tt>,</span> 
         
         

        is unmodified from when we
        originally introduced it. For details on that function, see
        <a href="simpleprogramlisting.html#printstocks_c">
                            <span>Function: print_stocks()</span>
                            
                            
                    </a>.
</p>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="runningit"></a>Running It</h3>
            </div>
          </div>
          <div></div>
        </div>
        <p>
                To run our replicated application, we need to make
                sure each participating environment has its own unique
                home directory. We can do this by running
                each site on a separate networked machine, but that
                is not strictly necessary; multiple instances of this
                code can run on the same machine provided the
                environment home restriction is observed.
        </p>
        <p>
                To run a process, make sure the environment home
                exists and then start the process using the
                <tt class="literal">-h</tt> option to specify that
                directory. You must also use the <tt class="literal">-m</tt>
                option to identify the local host and port that this
                process will use to listen for replication messages, and
                the <tt class="literal">-o</tt> option to identify the other
                processes in the replication group. Finally, use the
                <tt class="literal">-p</tt> option to specify a priority.
                The process that you designate to have the highest priority will become
                the master.
        </p>
        <pre class="programlisting">&gt; mkdir env1
&gt; ./rep_mgr -h env1 -n 2 -m localhost:8080 -o localhost:8081 -p 10
No stock database yet available.
No stock database yet available.  </pre>
        <p>
        Now, start another process. This time, change the environment
        home to something else, use the <tt class="literal">-m</tt> to at
        least change the port number the process is listening on, and
        use the <tt class="literal">-o</tt> option to identify the host and
        port of the other replication process:
</p>
        <pre class="programlisting">&gt; mkdir env2
&gt; ./rep_mgr -h env2 -n 2 -m localhost:8081 -o localhost:8080 -p 20</pre>
        <p>
        After a short pause, the second process should display the master
        prompt:
</p>
        <pre class="programlisting">
QUOTESERVER &gt; </pre>
        <p>
        And the first process should 
        display the read-only prompt:
</p>
        <pre class="programlisting">
QUOTESERVER (read-only)&gt; </pre>
        <p>
        Now go to the master process and give it a couple of stocks and stock
        prices:
</p>
        <pre class="programlisting">QUOTESERVER&gt; FAKECO 9.87
QUOTESERVER&gt; NOINC .23
QUOTESERVER&gt; </pre>
        <p>
    Then, go to the replica and hit <b class="userinput"><tt>return</tt></b> at the prompt to
    see the new values:
</p>
        <pre class="programlisting">QUOTESERVER (read-only)&gt; 
        Symbol  Price
        ======  =====
        FAKECO  9.87
        NOINC    .23 
QUOTESERVER (read-only)&gt; </pre>
        <p>
        Doing the same at the master results in the same thing:
</p>
        <pre class="programlisting">QUOTESERVER&gt; 
        Symbol  Price
        ======  =====
        FAKECO  9.87
        NOINC    .23 
QUOTESERVER&gt; </pre>
        <p>
        You can change a stock by simply entering the stock value and
        new price at the master's prompt:
</p>
        <pre class="programlisting">QUOTESERVER&gt; FAKECO 10.01 
QUOTESERVER&gt; </pre>
        <p>
        Then, go to either the master or the replica to see the updated
        database:
</p>
        <pre class="programlisting">QUOTESERVER&gt; 
        Symbol  Price
        ======  =====
        FAKECO  10.01
        NOINC    .23 
QUOTESERVER&gt; </pre>
        <p>
        And on the replica:
</p>
        <pre class="programlisting">QUOTESERVER (read-only)&gt; 
        Symbol  Price
        ======  =====
        FAKECO  10.01
        NOINC    .23 
QUOTESERVER (read-only)&gt; </pre>
        <p>
        Finally, to quit the applications, simply type
        <tt class="literal">quit</tt> at both prompts:
</p>
        <pre class="programlisting">QUOTESERVER (read-only)&gt; quit
&gt; </pre>
        <p>
        And on the master as well:
</p>
        <pre class="programlisting">QUOTESERVER&gt; quit
&gt; </pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="processingloop.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="fwrkmasterreplica.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="addfeatures.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Processing Loop </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Chapter 5. Additional Features</td>
        </tr>
      </table>
    </div>
  </body>
</html>