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 doloop() function that our original transaction application used (see Method: SimpleTxn.doloop()) to fully support our replicated application.
In the following example code, code that we add to the original example is presented in bold.
To begin, we must implement a way to track whether our application is running as a master or a client. There are many ways to do this, but in this case what we will do is extend com.sleepycat.db.Environment to carry the information. We do this by creating the RepQuoteEnvironment class.
package db.repquote; import com.sleepycat.db.DatabaseException; import com.sleepycat.db.Environment; import com.sleepycat.db.EnvironmentConfig; public class RepQuoteEnvironment extends Environment { private boolean isMaster; public RepQuoteEnvironment(final java.io.File host, EnvironmentConfig config) throws DatabaseException, java.io.FileNotFoundException { super(host, config); isMaster = false; } boolean getIsMaster() { return isMaster; } public void setIsMaster(boolean isMaster) { this.isMaster = isMaster; } }
Next, we go to RepQuoteExample.java and we include the RepQuoteEnvironment class as well as the EventHandler class. We then cause our RepQuoteExample class to implement EventHandler. We also change our environment handle to be an instance of RepQuoteEnvironment instead of Environment.
Note that we also import the com.sleepycat.db.ReplicationHandleDeadException class. We will discuss what that exception is used for a little later in this example.
package db.repquote; import java.io.FileNotFoundException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.Thread; import java.lang.InterruptedException; import com.sleepycat.db.Cursor; import com.sleepycat.db.Database; import com.sleepycat.db.DatabaseConfig; import com.sleepycat.db.DatabaseEntry; import com.sleepycat.db.DatabaseException; import com.sleepycat.db.DatabaseType; import com.sleepycat.db.EnvironmentConfig; import com.sleepycat.db.EventHandler; import com.sleepycat.db.LockMode; import com.sleepycat.db.OperationStatus; import com.sleepycat.db.ReplicationHandleDeadException; import db.repquote.RepConfig; import db.repquote.RepQuoteEnvironment public class RepQuoteExample implements EventHandler { private RepConfig repConfig; private RepQuoteEnvironment dbenv;
That done, we can skip the main() method and our class constructor, because they do not change. Instead, we skip down to our init() method where we take care of opening our environment and setting the event handler.
To update our init() method, we only need to do a couple of things. First, we identify the current class as the event handler. Then, when we open our environment, we instantiate a RepQuoteEnvironment class instead of an Environment class.
public int init(RepConfig config) throws DatabaseException { int ret = 0; appConfig = config; EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setErrorStream(System.err); envConfig.setErrorPrefix(RepConfig.progname); envConfig.setReplicationManagerLocalSite(appConfig.getThisHost()); for (ReplicationHostAddress host = appConfig.getFirstOtherHost(); host != null; host = appConfig.getNextOtherHost()) { envConfig.replicationManagerAddRemoteSite(host); } if (appConfig.totalSites > 0) envConfig.setReplicationNumSites(appConfig.totalSites); envConfig.setReplicationPriority(appConfig.priority); envConfig.setReplicationManagerAckPolicy( ReplicationManagerAckPolicy.ALL); envConfig.setCacheSize(RepConfig.CACHESIZE); envConfig.setTxnNoSync(true); envConfig.setEventHandler(this); envConfig.setAllowCreate(true); envConfig.setRunRecovery(true); envConfig.setThreaded(true); envConfig.setInitializeReplication(true); envConfig.setInitializeLocking(true); envConfig.setInitializeLogging(true); envConfig.setInitializeCache(true); envConfig.setTransactional(true); envConfig.setVerboseReplication(appConfig.verbose); try { dbenv = new RepQuoteEnvironment(appConfig.getHome(), envConfig); } catch(FileNotFoundException e) { System.err.println("FileNotFound exception: " + e.toString()); System.err.println( "Ensure that the environment directory is pre-created."); ret = 1; } // start replication manager dbenv.replicationManagerStart(3, appConfig.startPolicy); return ret; }
That done, we need to implement our handleEvent() method. This method is required because we are now implementing com.sleepycat.db.EventHandler. We use this method to track whether we are operating as a master.
public int handleEvent(EventType event) { int ret = 0; if (event == EventType.REP_MASTER) dbenv.setIsMaster(true); else if (event == EventType.REP_CLIENT) dbenv.setIsMaster(false); else if (event == EventType.REP_NEW_MASTER) { // ignored for now. } else { System.err.println("Unknown event callback received.\n"); ret = 1; } return ret; }
That done, we need to update our doloop() method.
We begin by updating our DatabaseConfig instance to determine which options to use, depending on whether the application is running as a master.
public int doloop() throws DatabaseException { Database db = null; for (;;) { if (db == null) { DatabaseConfig dbconf = new DatabaseConfig(); // Set page size small so page allocation is cheap. dbconf.setPageSize(512); dbconf.setType(DatabaseType.BTREE); if (dbenv.getIsMaster()) { dbconf.setAllowCreate(true); } dbconf.setTransactional(true);
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.
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.
try { db = dbenv.openDatabase (null, RepConfig.progname, null, dbconf); } catch (java.io.FileNotFoundException e) { System.err.println("no stock database available yet."); if (db != null) { db.close(true); db = null; } try { Thread.sleep(RepConfig.SLEEPTIME); } catch (InterruptedException ie) {} continue; } }
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.
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); // listen for input, and add it to the database. System.out.print("QUOTESERVER"); if (!dbenv.getIsMaster()) System.out.print("(read-only)"); System.out.print("> "); System.out.flush(); String nextline = null; try { nextline = stdin.readLine(); } catch (IOException ioe) { System.err.println("Unable to get data from stdin"); break; } String[] words = nextline.split("\\s");
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 print_stocks() method (which has not required a modification since we first introduced it in Method: SimpleTxn.printStocks() ).
When we call printStocks(), 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.
// A blank line causes the DB to be dumped to stdout. if (words.length == 0 || (words.length == 1 && words[0].length() == 0)) { try { printStocks(db); } catch (DeadlockException de) { continue; // Dead replication handles are caused by an election // resulting in a previously committing read becoming // invalid. Close the db handle and reopen. } catch (ReplicationHandleDeadException rhde) { db.close(true); // close no sync. db = null; continue; } catch (DatabaseException e) { System.err.println("Got db exception reading " + "replication DB: " + e.toString()); break; } continue; } if (words.length == 1 && (words[0].compareToIgnoreCase("quit") == 0 || words[0].compareToIgnoreCase("exit") == 0)) { break; } else if (words.length != 2) { System.err.println("Format: TICKER VALUE"); continue; }
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.
if (!dbenv.getIsMaster()) { System.err.println("Can't update client."); continue; } DatabaseEntry key = new DatabaseEntry(words[0].getBytes()); DatabaseEntry data = new DatabaseEntry(words[1].getBytes()); db.put(null, key, data); } if (db != null) db.close(true); return 0; }
With that completed, we are all done updating our application for replication. The only remaining method, printStocks(), is unmodified from when we originally introduced it. For details on that function, see Method: SimpleTxn.printStocks() .
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.
To run a process, make sure the environment home exists and then start the process using the -h option to specify that directory. You must also use the -m option to identify the local host and port that this process will use to listen for replication messages, and the -o option to identify the other processes in the replication group. Finally, use the -p option to specify a priority. The process that you designate to have the highest priority will become the master.
> mkdir env1 > java db.repquote.RepQuoteExample -h env1 -n 2 -m localhost:8080 \ -o localhost:8081 -p 10 No stock database yet available. No stock database yet available.
Now, start another process. This time, change the environment home to something else, use the -m to at least change the port number the process is listening on, and use the -o option to identify the host and port of the other replication process:
> mkdir env2 > java db.repquote.RepQuoteExample -h env2 -n 2 -m localhost:8081 \ -o localhost:8080 -p 20
After a short pause, the second process should display the master prompt:
QUOTESERVER >
And the first process should display the read-only prompt:
QUOTESERVER (read-only)>
Now go to the master process and give it a couple of stocks and stock prices:
QUOTESERVER> FAKECO 9.87 QUOTESERVER> NOINC .23 QUOTESERVER>
Then, go to the replica and hit return at the prompt to see the new values:
QUOTESERVER (read-only)> Symbol Price ====== ===== FAKECO 9.87 NOINC .23 QUOTESERVER (read-only)>
Doing the same at the master results in the same thing:
QUOTESERVER> Symbol Price ====== ===== FAKECO 9.87 NOINC .23 QUOTESERVER>
You can change a stock by simply entering the stock value and new price at the master's prompt:
QUOTESERVER> FAKECO 10.01 QUOTESERVER>
Then, go to either the master or the replica to see the updated database:
QUOTESERVER> Symbol Price ====== ===== FAKECO 10.01 NOINC .23 QUOTESERVER>
And on the replica:
QUOTESERVER (read-only)> Symbol Price ====== ===== FAKECO 10.01 NOINC .23 QUOTESERVER (read-only)>
Finally, to quit the applications, simply type quit at both prompts:
QUOTESERVER (read-only)> quit >
And on the master as well:
QUOTESERVER> quit >