1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Chapter��4.��Replica versus Master Processes</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 9 <link rel="home" href="index.html" title="Getting Started with Replicated Berkeley DB Applications" /> 10 <link rel="up" href="index.html" title="Getting Started with Replicated Berkeley DB Applications" /> 11 <link rel="previous" href="heartbeats.html" title="Managing Heartbeats" /> 12 <link rel="next" href="processingloop.html" title="Processing Loop" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Chapter��4.��Replica versus Master Processes</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="heartbeats.html">Prev</a>��</td> 22 <th width="60%" align="center">��</th> 23 <td width="20%" align="right">��<a accesskey="n" href="processingloop.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="chapter" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title"><a id="fwrkmasterreplica"></a>Chapter��4.��Replica versus Master Processes</h2> 33 </div> 34 </div> 35 <div></div> 36 </div> 37 <div class="toc"> 38 <p> 39 <b>Table of Contents</b> 40 </p> 41 <dl> 42 <dt> 43 <span class="sect1"> 44 <a href="fwrkmasterreplica.html#determinestate">Determining State</a> 45 </span> 46 </dt> 47 <dt> 48 <span class="sect1"> 49 <a href="processingloop.html">Processing Loop</a> 50 </span> 51 </dt> 52 <dt> 53 <span class="sect1"> 54 <a href="exampledoloop.html">Example Processing Loop</a> 55 </span> 56 </dt> 57 <dd> 58 <dl> 59 <dt> 60 <span class="sect2"> 61 <a href="exampledoloop.html#runningit">Running It</a> 62 </span> 63 </dt> 64 </dl> 65 </dd> 66 </dl> 67 </div> 68 <p> 69 Every environment participating in a replicated application 70 must know whether it is a <span class="emphasis"><em>master</em></span> or 71 <span class="emphasis"><em>replica</em></span>. The reason for this is 72 because, simply, the master can modify the database while 73 replicas cannot. As a result, not only will you open 74 databases differently depended on whether the environment is 75 running as a master, but the environment will frequently 76 behave quit a bit differently depending on whether it 77 thinks it is operating as the read/write interface for 78 your database. 79 </p> 80 <p> 81 Moreover, an environment must also be capable of 82 gracefully switching between master and replica states. 83 This means that the environment must be able to detect when 84 it has switched states. 85 </p> 86 <p> 87 Not surprisingly, a large part of your application's code 88 will be tied up in knowing which state a given 89 environment is in and then in the logic of how to behave depending on 90 its state. 91 </p> 92 <p> 93 This chapter shows you how to determine your environment's 94 state, and it then shows you some sample code on how 95 an application might behave depending on whether it is a 96 master or a replica in a replicated application. 97 </p> 98 <div class="sect1" lang="en" xml:lang="en"> 99 <div class="titlepage"> 100 <div> 101 <div> 102 <h2 class="title" style="clear: both"><a id="determinestate"></a>Determining State</h2> 103 </div> 104 </div> 105 <div></div> 106 </div> 107 <p> 108 In order to determine whether your code is running 109 as a master or a replica, you must write your 110 application as an implementation of 111 <tt class="classname">com.sleepycat.db.EventHandler</tt>. 112 This class gives you a single method, 113 <tt class="methodname">handleEvent()</tt>, within 114 which you can detect and respond to various events 115 that occur in your DB code. 116 </p> 117 <p> 118 Note that <tt class="methodname">EventHandler</tt> 119 responds to a number of 120 <tt class="classname">com.sleepycat.db.EventType</tt> 121 events, only some of which are related to 122 replication. For replication, the events that we 123 care about are: 124 </p> 125 <div class="itemizedlist"> 126 <ul type="disc"> 127 <li> 128 <p> 129 130 <tt class="literal">EventType.REP_MASTER</tt> 131 </p> 132 <p> 133 The local environment is now a master. 134 </p> 135 </li> 136 <li> 137 <p> 138 139 <tt class="literal">EventType.REP_CLIENT</tt> 140 </p> 141 <p> 142 The local environment is now a replica. 143 </p> 144 </li> 145 <li> 146 <p> 147 148 <tt class="literal">EventType.REP_STARTUPDONE</tt> 149 </p> 150 <p> 151 The replica has completed startup 152 synchronization and is now 153 processing log records received 154 from the master. 155 </p> 156 </li> 157 <li> 158 <p> 159 160 <tt class="literal">EventType.REP_NEWMASTER</tt> 161 </p> 162 <p> 163 An election was held and a new 164 environment was made a master. However, 165 the current environment <span class="emphasis"><em>is 166 not</em></span> the master. This 167 event exists so that you can 168 cause your code to take some 169 unique action in the event that the 170 replication groups switches masters. 171 </p> 172 </li> 173 </ul> 174 </div> 175 <p> 176 Note that these events are raised whenever the 177 state is established. That is, when the current 178 environment becomes a client, and that includes 179 at application startup, the event is raised. 180 Also, when an election is held and a client is elected to be a 181 master, then the event occurs. 182 </p> 183 <p> 184 The <tt class="classname">EventHandler</tt> 185 implementation is fairly simple. First you detect 186 the event, and then you record the state change 187 in some data member maintained in a location that 188 is convenient to you. 189 </p> 190 <p> 191 For example: 192 </p> 193 <pre class="programlisting"> 194package db.repquote; 195 196// We make our main class an EventHandler implementation 197... 198import com.sleepycat.db.EventHandler; 199... 200 201public class MyReplicationClass implements EventHandler 202{ 203 204... 205 206// Somewhere we provide a data member that is used to track 207// whether we are a master server. This could be in our main 208// class, or it could be part of a supporting class. 209private boolean isMaster; 210 211... 212 213isMaster = false; 214 215... 216 217// In the code where we open our environment and start replication, 218// we must identify the class that is the event handler. In this 219// example, we are performing this from within the class that 220// implements com.sleepycat.db.EventHandler so we identify 221// "this" class as the event handler 222envConfig.setEventHandler(this); </pre> 223 <p> 224 That done, we still need to implement the <tt class="methodname">handleEvent</tt> 225 method. This implementation can be fairly trivial. 226</p> 227 <pre class="programlisting"> public int handleEvent(EventType event) 228 { 229 int ret = 0; 230 if (event == EventType.REP_MASTER) 231 dbenv.setIsMaster(true); 232 else if (event == EventType.REP_CLIENT) 233 dbenv.setIsMaster(false); 234 else if (event == EventType.REP_NEW_MASTER) { 235 // ignored for now. 236 } else { 237 System.err.println("Unknown event callback received.\n"); 238 ret = 1; 239 } 240 return ret; 241 } </pre> 242 <p> 243 Of course, this only gives us the current state of the environment. We 244 still need the code that determines what to do when the environment 245 changes state and how to behave depending on the state (described 246 in the next section). 247</p> 248 </div> 249 </div> 250 <div class="navfooter"> 251 <hr /> 252 <table width="100%" summary="Navigation footer"> 253 <tr> 254 <td width="40%" align="left"><a accesskey="p" href="heartbeats.html">Prev</a>��</td> 255 <td width="20%" align="center"> 256 <a accesskey="u" href="index.html">Up</a> 257 </td> 258 <td width="40%" align="right">��<a accesskey="n" href="processingloop.html">Next</a></td> 259 </tr> 260 <tr> 261 <td width="40%" align="left" valign="top">Managing Heartbeats��</td> 262 <td width="20%" align="center"> 263 <a accesskey="h" href="index.html">Home</a> 264 </td> 265 <td width="40%" align="right" valign="top">��Processing Loop</td> 266 </tr> 267 </table> 268 </div> 269 </body> 270</html> 271