Debian Codegraph @

src/rep/mlease.html

raw symbols SCIP
1 <!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en">
2 <!--Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved.-->
3 <html>
4 <head>
5 <meta http-equiv="Content-Type"
6 content="text/html; charset=iso-8859-1">
7 <meta name="GENERATOR"
8 content="Mozilla/4.76 [en] (X11; U; FreeBSD 4.3-RELEASE i386) [Netscape]">
9 <title>Master Lease</title>
10 </head>
11 <body>
12 <center>
13 <h1>Master Leases for Berkeley DB</h1>
14 </center>
15 <center><i>Susan LoVerso</i> <br>
16 <i>[email protected]</i> <br>
17 <i>Rev 1.1</i><br>
18 <i>2007 Feb 2</i><br>
19 </center>
20 <p><br>
21 </p>
22 <h2>What are Master Leases?</h2>
23 A master lease is a mechanism whereby clients grant master-ship rights
24 to a site and that master, by holding lease rights can provide a&nbsp;
25 guarantee of durability to a replication group for a given period of
26 time.&nbsp; By granting a lease to a master,
27 a&nbsp; client will not participate in an election to elect a new
28 master until that granted master lease has expired.&nbsp; By holding a
29 collection of granted leases, a master will be able to supply
30 authoritative read requests to applications.&nbsp; By holding leases a
31 read operation on a master can guarantee several things to the
32 application:<br>
33 <ol>
34 <li>Authoritative reads: a guarantee that the data being read by the
35 application is durable and can never be rolled back.</li>
36 <li>Freshness: a guarantee that the data being read by the
37 application <b>at the master</b> is
38 not stale.</li>
39 <li>Master viability: a guarantee that a current master with valid
40 leases will not encounter a duplicate master situation.<br>
41 </li>
42 </ol>
43 <h2>Requirements</h2>
44 The requirements of DB to support this include:<br>
45 <ul>
46 <li>After turning them on, users can choose to ignore them in reads
47 or not.</li>
48 <li>We are providing read authority on the master only.&nbsp; A
49 read on a client is equivalent to a read while ignoring leases.</li>
50 <li>We guarantee that data committed on a master <b>that has been
51 read by an application on the
52 master</b> will not be rolled back.&nbsp; Data read on a client or
53 while ignoring leases <i>or data
54 successfully updated/committed but not read,</i>
55 may be rolled back.<br>
56 </li>
57 <li>A master will not return successfully from a read operation
58 unless it holds a
59 majority of leases unless leases are ignored.</li>
60 <li>Master leases will remove the possibility of a current/correct
61 master being "shot down" by DUPMASTER.&nbsp; <b>NOTE: Old/Expired
62 masters may discover a
63 later master and return DUPMASTER to the application however.</b><br>
64 </li>
65 <li>Any send callback failure must result in premature lease
66 expiration on the master.<br>
67 </li>
68 <li>Users who change the system clock during master leases void the
69 guarantee and may get undefined behavior.&nbsp; We assume time always
70 runs forward. <br>
71 </li>
72 <li>Clients are forbidden from participating in elections while they
73 have an outstanding lease granted to another site.</li>
74 <li>Clients are forbidden from accepting a new master while they have
75 an outstanding lease granted to another site.</li>
76 <li>Clients are forbidden from upgrading themselves to master while
77 they have an outstanding lease granted to another site.</li>
78 <li>When asked for a lease grant explicitly by the master, the client
79 cannot grant the lease to the master unless the LSN in the master's
80 request has been processed by this client.<br>
81 </li>
82 </ul>
83 The requirements of the
84 application using leases include:<br>
85 <ul>
86 <li>Users must implement (Base API users on their own, RepMgr users
87 via configuration) a majority (or larger) ACK policy. <br>
88 </li>
89 <li>The application must use the election mechanism to decide a master.
90 It may not simply declare a site master.</li>
91 <li>The send callback must return an error if the majority ACK policy
92 is not met for PERM records.</li>
93 <li>Users must set the number of sites in the group.</li>
94 <li>Using leases in a replication group is all-or-none.&nbsp;
95 Therefore, if a site knows it is using leases, it can assume other
96 sites are also.<br>
97 </li>
98 <li>All applications that care about read guarantees must forward or
99 perform all reads on the master.&nbsp; Reading on the client means a
100 read ignoring leases. </li>
101 </ul>
102 <p>There are some open questions
103 remaining.</p>
104 <ul>
105 <li>There is one major showstopper issue, see Crashing - Potential
106 problem near the end of the document.&nbsp; We need a better solution
107 than the one shown there (writing to disk every time a lease is
108 granted). Perhaps just documenting that durability means it must be
109 flushed to disk before success to avoid that situation?<br>
110 </li>
111 <li>What about db-&gt;join?&nbsp; Users can call join, but the calls
112 on the join cursor to get the data would be subject to leases and
113 therefore protected.&nbsp; Ok, this is not an open question.</li>
114 <li>What about other read-like operations?&nbsp; Clearly <i>
115 DB-&gt;get, DB-&gt;pget, DBC-&gt;get,
116 DBC-&gt;pget</i> need lease checks.&nbsp; However, other APIs use
117 keys.&nbsp; <i>DB-&gt;key_range</i>
118 provides an estimate only so it shouldn't need lease checks. <i>
119 DB-&gt;stat</i> provides exact counts
120 to <i>bt_nkeys</i> and <i>bt_ndata</i> fields.&nbsp; Are those
121 fields considered authoritative that providing those values implies a
122 durability guarantee and therefore <i>DB-&gt;stat</i>
123 should be subject to lease verification?&nbsp; <i>DBC-&gt;count</i>
124 provides a count for
125 the number of data items associated with a key.&nbsp; Is this
126 authoritative information? This is similar to stat - should it be
127 subject to lease verification?<br>
128 </li>
129 <li>Do we require master lease checks on write operations?&nbsp; I
130 think lease checks are not needed on write operations.&nbsp; It doesn't
131 add correctness and adds a lot of complexity (checking leases in put,
132 del, and cursors, then what about rename, remove, etc).<br>
133 </li>
134 <li>Do master leases give an iron-clad guarantee of never rolling
135 back a transaction? No, but it should mean that a committed transaction
136 can never be <b>read</b> on a master
137 unless the lease is valid.&nbsp; A committed transaction on a master
138 that has never been presented to the application may get rolled back.<br>
139 </li>
140 <li>Do we need to quarantine or prevent reads on an ex-master until
141 sync-up is done?&nbsp; No.&nbsp; A master that is simply downgraded to
142 client or crashes and reboots is now a client.&nbsp; Reading from that
143 client is the same as saying Ignore Leases.</li>
144 <li>What about adding and removing sites while leases are
145 active?&nbsp; This is SR 14778.&nbsp; A consistent <i>nsites</i> value
146 is required by master
147 leases.&nbsp; &nbsp; It isn't
148 clear to me what a master is
149 supposed to do if the value of nsites gets smaller while leases are
150 active.&nbsp; Perhaps it leaves its larger table intact and simply
151 checks for a smaller number of granted leases?<br>
152 </li>
153 <li>Can users turn leases off?&nbsp; No.&nbsp; There is no planned <i>turn
154 leases off</i> API.</li>
155 <li>Clock skew will be a percentage.&nbsp; However, the smallest, 1%,
156 is probably rather large for clock skew.&nbsp; Percentage was chosen
157 for simplicity and similarity to other APIs.&nbsp; What granularity is
158 appropriate here?</li>
159 </ul>
160 <h2>API Changes</h2>
161 The API changes that are visible
162 to the user are fairly minimal.&nbsp;
163 There are a few API calls they need to make to configure master leases
164 and then there is the API call to turn them on.&nbsp; There is also a
165 new flag to existing APIs to allow read operations to ignore leases and
166 return data that
167 may be non-durable potentially.<br>
168 <h3>Lease Timeout<br>
169 </h3>
170 There is a new timout the user
171 must configure for leases called <b>DB_REP_LEASE_TIMEOUT</b>.&nbsp;
172 This timeout will be new to
173 the <i>dbenv-&gt;rep_set_timeout</i> method. The <b>DB_REP_LEASE_TIMEOUT</b>
174 has no default and it is required that the user configure a timeout
175 before they turn on leases (obviously, this timeout need not be set of
176 leases will not be used).&nbsp; That timeout is the amount of time
177 the lease is valid on the master and how long it is granted
178 on the client.&nbsp; This timeout must be the same
179 value on all sites (like log file size).&nbsp; The timeout used when
180 refreshing leases is the <b>DB_REP_ACK_TIMEOUT</b>
181 for RepMgr application.&nbsp; For Base API applications, lease
182 refreshes will use the same mechanism as <b>PERM</b> messages and they
183 should
184 have no additional burden.&nbsp; This timeout is used for lease
185 refreshment and is the amount of time a reader will wait to refresh
186 leases before returning failure to the application from a read
187 operation.<br>
188 <br>
189 This timeout will be both stored
190 with its original value, and also
191 converted to a <i>db_timespec</i>
192 using the <b>DB_TIMEOUT_TO_TIMESPEC</b>
193 macro and have the clock skew accounted for and stored in the shared
194 rep structure:<br>
195 <pre>db_timeout_t lease_timeout;<br>db_timespec lease_duration;<br></pre>
196 NOTE:&nbsp; By sending the lease refresh during DB operations, we are
197 forcing/assuming that the operation's process has a replication
198 transport function set.&nbsp; That is obviously the case for write
199 operations, but would it be a burden for read processes (on a
200 master)?&nbsp; I think mostly not, but if we need leases for <i>
201 DB-&gt;stat</i> then we need to
202 document it as it is certainly possible for an application to have a
203 separate or dedicated <i>stat</i>
204 application or attempt to use <i>db_stat</i>
205 (which will not work if leases must be checked).<br>
206 <br>
207 Leases should be checked after the local operation so that we don't
208 have a window/boundary if we were to check leases first, get
209 descheduled, the lose our lease and then perform the operation.&nbsp;
210 Do the operation, then check leases before returning to the user.<br>
211 <h3>Using Leases</h3>
212 There is a new API that the user must call to tell the system to use
213 the lease mechanism.&nbsp; The method must be called before the
214 application calls <i>dbenv-&gt;rep_start</i>
215 or <i>dbenv-&gt;repmgr_start</i>.
216 This new
217 method is:<br>
218 <br>
219 <pre>&nbsp;&nbsp;&nbsp; dbenv-&gt;rep_set_lease(DB_ENV *dbenv, u_int32_t clock_scale_factor, u_int32_t flags)<br>
220 </pre>
221 The <i>clock_scale_factor</i>
222 parameter is interpreted as a percentage, greater than 100 (to transmit
223 a floating point number as an integer to the API) that represents the
224 maximum shkew between any two sites' clocks.&nbsp; That is, a <span
225 style="font-style: italic;">clock_scale_factor</span> of 150 suggests
226 that the greatest discrepancy between clocks is that one runs 50%
227 faster than the others.&nbsp; Both the
228 master and client sides
229 compensate for possible clock skew.&nbsp; The master uses the value to
230 compensate in case the replica has a slow clock and replicas compensate
231 in case they have a fast clock.&nbsp; This scaling factor will need to
232 be divided by 100 on all sites to truly represent the percentage for
233 adjustments made to time values.<br>
234 <br>
235 Assume the slowest replica's clock is a factor of <i>clock_scale_factor</i>
236 slower than the
237 fastest clock.&nbsp; Using that assumption, if the fastest clock goes
238 from time t1 to t2 in X
239 seconds, the slowest clock does it in (<i>clock_scale_factor</i> / 100)
240 * X seconds.<br>
241 <br>
242 The <i>flags</i> parameter is not
243 currently used.<br>
244 <br>
245 When the <i>dbenv-&gt;rep_set_lease</i>
246 method is called, we will set a configuration flag indicating that
247 leases are turned on:<br>
248 <b>#define REP_C_LEASE &lt;value&gt;</b>.&nbsp;
249 We will also record the <b>u_int32_t
250 clock_skew</b> value passed in.&nbsp; The <i>rep_set_lease</i> method
251 will not allow
252 calls after <i>rep_start.&nbsp; </i>If
253 multiple calls are made prior to calling <i>rep_start</i> then later
254 calls will
255 overwrite the earlier clock skew value.&nbsp; <br>
256 <br>
257 We need a new flag to prevent calling <i>rep_set_lease</i>
258 after <i>rep_start</i>.&nbsp; The
259 simplest solution would be to reject the call to
260 <i>rep_set_lease&nbsp;
261 </i>if<b>
262 REP_F_CLIENT</b>
263 or <b>REP_F_MASTER</b> is set.&nbsp;
264 However that does not work in the cases where a site cleanly closes its
265 environment and then opens without running recovery.&nbsp; The
266 replication state will still be set.&nbsp; The prevention will be
267 implemented as:<br>
268 <pre>#define REP_F_START_CALLED &lt;some bit value&gt;<br></pre>
269 In __rep_start, at the end:<br>
270 <pre>if (ret == 0 ) {<br> REP_SYSTEM_LOCK<br> F_SET(rep, REP_F_START_CALLED)<br> REP_SYSTEM_UNLOCK<br>}</pre>
271 In <i>__rep_env_refresh</i>, if we
272 are the last reference closing the env (we already check for that):<br>
273 <pre>F_CLR(rep, REP_F_START_CALLED);</pre>
274 In order to avoid run-time floating point operations
275 on <i>db_timespec</i> structures,
276 when a site is declared as a client or master in <i>rep_start</i> we
277 will pre-compute the
278 lease duration based on the integer-based clock skew and the
279 integer-based lease timeout.&nbsp; A master should set a replica's
280 lease expiration to the <b>start time of
281 the sent message +
282 (lease_timeout / clock_scale_factor)</b> in case the replica has a
283 slow clock.&nbsp; Replicas extend their leases to <b>received message
284 time + (lease_timeout *
285 clock_scale_factor)</b> in case this replica has a fast clock.&nbsp;
286 Therefore, the computation will be as follows if the site is becoming a
287 master:<br>
288 <pre>db_timeout_t tmp;<br>tmp = (db_timeout_t)((double)rep-&gt;lease_timeout / ((double)rep-&gt;clock_skew / (double)100));<br>rep-&gt;lease_duration = DB_TIMEOUT_TO_TIMESPEC(&amp;tmp);<br></pre>
289 Similarly, on a client the computation is:<br>
290 <pre>tmp = (db_timeout_t)((double)rep-&gt;lease_timeout * ((double)rep-&gt;clock_skew / (double)100));<br></pre>
291 When a site changes state, its lease duration will change based on
292 whether it is becoming a master or client and it will be recomputed
293 from the original values.&nbsp; Note that these computations, coupled
294 with the fact that the lease on the master is computed based on the
295 master's time that it sent the message means that leases on the master
296 are more conservatively computed than on the clients.<br>
297 <br>
298 The <i>dbenv-&gt;rep_set_lease</i>
299 method must be called after <i>dbenv-&gt;open</i>,
300 similar to <i>dbenv-&gt;rep_set_config</i>.&nbsp;
301 The reason is so that we can check that this is a replication
302 environment and we have access to the replication shared memory region.<br>
303 <h3>Read Operations<br>
304 </h3>
305 Authoritative read operations on the master with leases enabled will
306 abide by leases by default.&nbsp; We will provide a flag that allows an
307 operation on a master to ignore leases.&nbsp; <b>All read operations
308 on a client imply
309 ignoring leases.</b> If an application wants authoritative reads
310 they must forward the read requests to the master and it is the
311 application's responsibility to provide the forwarding.
312 The consensus was that forcing <span style="font-weight: bold;">DB_IGNORE_LEASE</span>
313 on client read operations (with leases enabled, obviously) was too
314 heavy handed.&nbsp; Read operations on the client will ignore leases,
315 but do no special flag checking.<br>
316 <br>
317 The flag will be called <b>DB_IGNORE_LEASE</b>
318 and it will be a flag that can be OR'd into the DB access method and
319 cursor operation values.&nbsp; It will be similar to the <b>DB_READ_UNCOMMITTED</b>
320 flag.
321 <br>
322 </b>The methods that will
323 adhere to leases are:<br>
324 <ul>
325 <li><i>Db-&gt;get</i></li>
326 <li><i>Db-&gt;pget</i></li>
327 <li><i>Dbc-&gt;get</i></li>
328 <li><i>Dbc-&gt;pget</i></li>
329 </ul>
330 The code that will check leases for a client reading would look
331 something
332 like this, if we decide to become heavy-handed:<br>
333 <pre>if (IS_REP_CLIENT(dbenv)) {<br> [get to rep structure]<br> if (FLD_ISSET(rep-&gt;config, REP_C_LEASE) &amp;&amp; !LF_ISSET(DB_IGNORE_LEASE)) {<br> db_err("Read operations must ignore leases or go to master");<br> ret = EINVAL;<br> goto err;<br> }<br>}<br></pre>
334 On the master, the new code to abide by leases is more complex.&nbsp;
335 After the call to perform the operation we will check the lease.&nbsp;
336 In that checking code, the master will see if it has a valid
337 lease.&nbsp; If so, then all is well.&nbsp; If not, it will try to
338 refresh the leases.&nbsp; If that refresh attempt results in leases,
339 all is well.&nbsp; If the refresh attempt does not get leases, then the
340 master cannot respond to the read as an authority and we return an
341 error.&nbsp; The new error is called <b>DB_REP_LEASE_EXPIRED</b>.&nbsp;
342 The location of the master lease check is down after the internal call
343 to read the data is successful:<br>
344 <pre>if (IS_REP_MASTER(dbenv) &amp;&amp; !LF_ISSET(DB_IGNORE_LEASE)) {<br> [get to rep structure]<br> if (FLD_ISSET(rep-&gt;config, REP_C_LEASE) &amp;&amp;<br> (ret = __rep_lease_check(dbenv)) != 0) {<br> /*<br> * We don't hold the lease.<br> */<br> goto err;<br> }<br>}<br></pre>
345 See below for the details of <i>__rep_lease_check</i>.<br>
346 <br>
347 Also note that if leases (or replication) are not configured, then <span
348 style="font-weight: bold;">DB_IGNORE_LEASE</span> is a no-op.&nbsp; It
349 is ignored (and won't error) if used when leases are not in
350 effect.&nbsp; The reason is so that we can generically set that flag in
351 utility programs like <span style="font-style: italic;">db_dump</span>
352 that walk the database with a cursor.&nbsp; Note that <span
353 style="font-style: italic;">db_dump</span> is the only utility that
354 reads with a cursor.<span style="font-style: italic;"><span
355 style="font-style: italic;"></span></span><br>
356 <h3><b>Nsites
357 and Elections</b></h3>
358 The call to <i>dbenv-&gt;rep_set_nsites</i>
359 must be performed before the call to <i>dbenv-&gt;rep_start</i>
360 or <i>dbenv-&gt;repmgr_start</i>.&nbsp;
361 This document assumes either that <b>SR
362 14778</b> gets resolved, or assumes that the value of <i>nsites</i> is
363 immutable.&nbsp; The
364 master and all clients need to know how many sites and leases are in
365 the group.&nbsp; Clients need to know for elections.&nbsp; The master
366 needs to know for the size of the lease table and to know what value a
367 majority of the group is. <b>[Until
368 14778 is resolved, the master lease work must assume <i>nsites</i> is
369 immutable and will
370 therefore enforce that this is called before <i>rep_start</i> using
371 the same mechanism
372 as <i>rep_set_lease</i>.]</b><br>
373 <br>
374 Elections and leases need to agree on the number of sites in the
375 group.&nbsp; Therefore, when leases are in effect on clients, all calls
376 to <i>dbenv-&gt;rep_elect</i> must
377 set the <i>nsites</i> parameter to
378 0.&nbsp; The <i>rep_elect</i> code
379 path will return <b>EINVAL</b> if <b>REP_C_LEASE</b> is set and <i>nsites</i>
380 is non-0.
381 <h2>Lease Management</h2>
382 <h3>Message Changes</h3>
383 In order for clients to grant leases to the master a new message type
384 must be added for that purpose.&nbsp; This will be the <b>REP_LEASE_GRANT</b>
385 message.&nbsp;
386 Granting leases will be a result of applying a <b>DB_REP_PERMANENT</b>
387 record and therefore we
388 do not need any additional message in order for a master to request a
389 lease grant.&nbsp; The <b>REP_LEASE_GRANT</b>
390 message will pass a structure as its message DBT:<br>
391 <pre>struct __rep_lease_grant {<br> db_timespec msg_time;<br>#ifdef DIAGNOSTIC<br> db_timespec expire_time;<br>#endif<br>} REP_GRANT_INFO;<br></pre>
392 In the <b>REP_LEASE_GRANT</b>
393 message, the client is actually giving the master several pieces of
394 information.&nbsp; We only need the echoed <i>msg_time</i> in this
395 structure because
396 everything else is already sent.&nbsp; The client is really sending the
397 master:<br>
398 <ul>
399 <li>Its EID (parameter to <span style="font-style: italic;">rep_send_message</span>
400 and <span style="font-style: italic;">rep_process_message</span>)<br>
401 </li>
402 <li>The PERM LSN this message acknowledged (sent in the control
403 message)</li>
404 <li>Unique identifier echoed back to master (<i>msg_time</i> sent in
405 message as above)</li>
406 </ul>
407 On the client, we always maintain the maximum PERM LSN already in <i>lp-&gt;max_perm_lsn</i>.&nbsp;
408 <h3>Local State Management</h3>
409 Each client must maintain a <i>db_timespec</i>
410 timestamp containing the expiration of its granted lease.&nbsp; This
411 field will be in the replication shared memory structure:<br>
412 <pre>db_timespec grant_expire;<br></pre>
413 This timestamp already takes into account the clock skew.&nbsp; All
414 new fields must be initialized when the region is created. Whenever we
415 grant our master lease and want to send the <b>REP_LEASE_GRANT</b>
416 message, this value
417 will be updated.&nbsp; It will be used in the following way:
418 <pre>db_timespec mytime;<br>DB_LSN perm_lsn;<br>DBT lease_dbt;<br>REP_GRANT_INFO gi;<br><br><br>timespecclear(&amp;mytime);<br>timespecclear(&amp;newgrant);<br>memset(&amp;lease_dbt, 0, sizeof(lease_dbt));<br>memset(&amp;gi, 0, sizeof(gi));<br>__os_gettime(dbenv, &amp;mytime);<br>timespecadd(&amp;mytime, &amp;rep-&gt;lease_duration);<br>MUTEX_LOCK(rep-&gt;clientdb_mutex);<br>perm_lsn = lp-&gt;max_perm_lsn;<br>MUTEX_UNLOCK(rep-&gt;clientdb_mutex);<br>REP_SYSTEM_LOCK(dbenv);<br>if (timespeccmp(mytime, rep-&gt;grant_expire, &gt;))<br> rep-&gt;grant_expire = mytime;<br>gi.msg_time = msg-&gt;msg_time;<br>#ifdef DIAGNOSTIC<br>gi.expire_time = rep-&gt;grant_expire;<br>#endif<br>lease_dbt.data = &amp;gi;<br>lease_dbt.size = sizeof(gi);<br>REP_SYSTEM_UNLOCK(dbenv);<br>__rep_send_message(dbenv, eid, REP_LEASE_GRANT, &amp;perm_lsn, &amp;lease_dbt, 0, 0);<br></pre>
419 This updating of the lease grant will occur in the <b>PERM</b> code
420 path when we have
421 successfully applied the permanent record.<br>
422 <h3>Maintaining Leases on the
423 Master/Rep_start</h3>
424 The master maintains a lease table that it checks when fulfilling a
425 read request that is subject to leases.&nbsp; This table is initialized
426 when a site calls<i>
427 dbenv-&gt;rep_start(DB_MASTER)</i> and the site is undergoing a role
428 change (i.e. a master making additional calls to <i>dbenv-&gt;rep_start(DB_MASTER)</i>
429 does
430 not affect an already existing table).<br>
431 <br>
432 When a non-master site becomes master, it must do two things related to
433 leases on a role change.&nbsp; First, a client cannot upgrade to master
434 while it has an outstanding lease granted to another site.&nbsp; If a
435 client attempts to do so, an error, <b>EINVAL</b>,
436 will be returned.&nbsp; The only way this should happen is if the
437 application simply declares a site master, instead of using
438 elections.&nbsp; Elections will already wait for leases to expire
439 before proceeding. (See below.)
440 <br>
441 <br>
442 Second, once we are proceeding with becoming a master, the site must
443 allocate the table it will use to maintain lease information.&nbsp;
444 This table will be sized based on <i>nsites</i>
445 and it will be an array of the following structure:<br>
446 <pre>struct {<br> int eid; /* EID of client site. */<br> db_timespec start_time; /* Unique time ID client echoes back on grants. */<br> db_timespec end_time; /* Master's lease expiration time. */<br> DB_LSN lease_lsn; /* Durable LSN this lease applies to. */<br> u_int32_t flags; /* Unused for now?? */<br>} REP_LEASE_ENTRY;<br></pre>
447 <h3>Granting Leases</h3>
448 It is the burden of the application to make sure that all sites in the
449 group
450 are using leases, or none are.&nbsp; Therefore, when a client processes
451 a <b>PERM</b>
452 log record that arrived from the master, it will grant its lease
453 automatically if that record is permanent (i.e. <b>DB_REP_ISPERM</b>
454 is being returned),
455 and leases are configured.&nbsp; A client will not send a
456 lease grant when it is processing log records (even <b>PERM</b>
457 ones) it receives from other clients that use client-to-client
458 synchronization.&nbsp; The reason is that the master requires a unique
459 time-of-msg ID (see below) that the client echoes back in its lease
460 grant and it will not have such an ID from another client.<br>
461 <br>
462 The master stores a time-of-msg ID in each message and the client
463 simply echoes it back to the master.&nbsp; In its lease table, it does
464 keep the base
465 time-of-msg for a valid lease.&nbsp; When <b>REP_LEASE_GRANT</b>
466 message comes in,
467 the master does a number of things:<br>
468 <ol>
469 <li>Pulls the echoed timespec from the client message, into <i>msg_time</i>.<br>
470 </li>
471 <li>Finds the entry in its lease table for the client's EID.&nbsp; It
472 walks the table searching for the ID.&nbsp; EIDs of <span
473 style="font-weight: bold;">DB_EID_INVALID</span> are
474 illegal.&nbsp; Either the master will find the entry, or it will find
475 an empty slot in the table (i.e. it is still populating the table with
476 leases).</li>
477 <li>If this is a previously unknown site lease, the master
478 initializes the entry by copying to the <i>eid</i>, <i>start_time, </i>and
479 <i>lease_lsn</i> fields.&nbsp; The master
480 also computes the <i>end_time</i>
481 based on the adjusted <i>rep-&gt;lease_duration</i>.</li>
482 <li>If this is a lease from a previously known site, the master must
483 perform <i>timespeccmp(&amp;msg_time,
484 &amp;table[i].start_time, &gt;)</i> and only update the <i>end_time</i>
485 of the lease when this is
486 a more recent message.&nbsp; If it is a more recent message, then we
487 should update
488 the <i>lease_lsn</i> to the LSN in
489 the message.</li>
490 <li>Since lease durations are computed taking the clock skew into
491 account, clients compute them based on the current time and the master
492 computes it based on original sending time, for diagnostic purposes
493 only, I also plan to send the client's expiration time.&nbsp; The
494 client errs on the side of computing a larger lease expiration time and
495 the master errs on the side of computing a smaller duration.&nbsp;
496 Since both are taking the clock skew
497 into account, the client's ending expiration time should never be
498 smaller than
499 the master's computed expiration time or their value for clock skew may
500 not be correct.<br>
501 </li>
502 </ol>
503 Any log records (new or resent) that originate from the master and
504 result in <b>DB_REP_ISPERM</b> get an
505 ack.<br>
506 <br>
507 <h3>Refreshing Leases</h3>
508 Leases get refreshed when a master receives a <b>REP_LEASE_GRANT</b>
509 message from a client. There are three pieces to lease
510 refreshment.&nbsp; <br>
511 <h4>Lazy Lease Refreshing on Read<br>
512 </h4>
513 If the master discovers that leases are
514 expired during the read operation, it attempts to refresh its
515 collection of lease grants.&nbsp; It does this by calling a new
516 function <i>__rep_lease_refresh</i>.&nbsp;
517 This function is very similar to the already-existing function <i>__rep_flush</i>.&nbsp;
518 Basically, to
519 refresh the lease, the master simply needs to resend the last PERM
520 record to the clients.&nbsp; The requirements state that when the
521 application send function returns successfully from sending a PERM
522 record, the majority of clients have that PERM LSN durable.&nbsp; We
523 will have a new public DB error return called <b>DB_REP_LEASE_EXPIRED</b>
524 that will be
525 returned back to the caller if the master cannot assert its
526 authority.&nbsp; The code will look something like this:<br>
527 <pre>/*<br> * Use lp-&gt;max_perm_lsn on the master (currently not used on the master)<br> * to keep track of the last PERM record written through the logging system.<br> * need to initialize lp-&gt;max_perm_lsn in rep_start on role_chg.<br> */<br>call __rep_send_message on the last PERM record the master wrote, with DB_REP_PERMANENT<br>if failure<br> expire leases<br> return lease expired error to caller<br>else /* success */<br> recheck lease table<br> /*<br> * We need to recheck the lease table because the client<br> * lease grant messages may not be processed yet, or got<br> * lost, or racing with the application's ACK messages or<br> * whatever. <br> */<br> if we have a majority of valid leases<br> return success<br> else<br> return lease expired error to caller <br></pre>
528 <h4>Ongoing Update Refreshment<br>
529 </h4>
530 Second is having the master indicate to
531 the client it needs to send a lease grant in response to the current
532 PERM log message.&nbsp; The problem is
533 that acknowledgements must contain a master-supplied message timestamp
534 that the client sends back to the master.&nbsp; We need to modify the
535 structure of the&nbsp; log record messages when leases are configured
536 so
537 that when a PERM message is sent, the master sends, and the client
538 expects, the message timestamp.&nbsp; There are three fairly
539 straightforward and different implementations to consider.<br>
540 <ol>
541 <li>Adding the timestamp to the <b>REP_CONTROL</b>
542 structure.&nbsp; If this option is chosen, then the code trivially
543 sends back the timestamp in the client's reply.&nbsp; There is no
544 special processing done by either side with the message contents.&nbsp;
545 So, on a PERM log record, the master will send a non-zero
546 timestamp.&nbsp; On a normal log record the timestamp will be zero or
547 some known invalid value.&nbsp; If the client sees a non-zero
548 timestamp, it sends a <b>REP_LEASE_GRANT</b>
549 with the <i>lp-&gt;max_perm_lsn</i>
550 after applying that log record.&nbsp; If it is zero, then the client
551 does nothing different.&nbsp; The advantage is ease of code.&nbsp; The
552 disadvantage is that for mixed version systems, the client is now
553 dealing with different sized control structures.&nbsp; We would have to
554 retain the old control structure so that during a mixed version group
555 the (upgraded) clients can use, expect and send old control structures
556 to the master.&nbsp; This is unfortunate, so let's consider additional
557 implementations that don't require modifying the control structure.<br>
558 </li>
559 <li>Adding a new <b>REPCTL_LEASE</b>
560 flag to the list of flags for the control structure, but do not change
561 the control structure fields.&nbsp; When a master wants to send a
562 message that needs a lease ack, it sets the flag.&nbsp; Additionally,
563 instead of simply sending a log record DBT as the <i>rec</i> parameter
564 for replication, we
565 would send a new structure that had the timestamp first and then the
566 record (similar to the bulk transfer buffer).&nbsp; The advantage of
567 this is that the control structure does not change.&nbsp; Disadvantages
568 include more special-cased code in the normal code path where we have
569 to check the flag.&nbsp; If the flag is set we have to extract the
570 timestamp value and massage the incoming data to pass on the real log
571 record to <i>rep_apply</i>.&nbsp; On
572 bulk transfer, we would just add the timestamp into the buffer.&nbsp;
573 On normal transfers, it would incur an additional data copy on the
574 master side.&nbsp; That is unfortunate.&nbsp; Additionally, if this
575 record needs to be stored in the temp db, we need some way to get it
576 back again later or <span style="font-style: italic;">rep_apply</span>
577 would have to extract the timestamp out when it processed the record
578 (either live or from the temp db).<br>
579 </li>
580 <li>Adding a different message type, such as <b>REP_LOG_ACK</b>.&nbsp;
581 Similarly to <b>REP_LOG_MORE</b> this message would be a
582 special-case version of a log record.&nbsp; We would extract out the
583 timestamp and then handle as a normal log record.&nbsp; This
584 implementation is rejected because it actually would require three new
585 message types: <b>REP_LOG_ACK,
586 REP_LOG_ACK_MORE, REP_BULK_LOG_ACK</b>.&nbsp; That is just too ugly
587 to contemplate.</li>
588 </ol>
589 <b>[Slight digression:</b> it occurs
590 to me while writing about #2 and #3 above, that our implementation of
591 all of the *_MORE messages could really be implemented with a <b>REPCTL_MORE</b>
592 flag instead of a
593 separate message type.&nbsp; We should clean that up and simplify the
594 messages but not part of master leases. Hmm, taking that thought
595 process further, we really could get rid of the <b>REP_BULK_*</b>
596 messages as well if we
597 added a <b>REPCTL_BULK</b>
598 flag.&nbsp; I think we should definitely do it for the *_MORE
599 messages.&nbsp; I am not sure we should do it for bulk because the
600 structure of the incoming data record is vastly different.]<br>
601 <br>
602 Of these options, I believe that modifying the control structure is the
603 best alternative.&nbsp; The handling of the old structure will be very
604 isolated to code dealing with old versions and is far less complicated
605 than injecting the timestamp into the log record DBT and doing a data
606 copy.&nbsp; Actually, I will likely combine #1 and the flag from #2
607 above.&nbsp; I will have the <b>REPCTL_LEASE</b>
608 flag that indicates a lease grant reply is expected and have the
609 timestamp in the control structure.&nbsp;
610 Also I will probably add in a spare field or two for future use in the <b>REP_CONTROL</b>
611 structure.<br>
612 <h4>Gap processing</h4>
613 No matter which implementation we choose for ongoing lease refreshment,
614 gap processing must be considered.&nbsp; The code above assumes the
615 timestamps will be placed on PERM records only.&nbsp; Normal log
616 records will not have a timestamp, nor a flag or anything else like
617 that.&nbsp; However, any log message can fill a gap on a client and
618 result in the processing of that normal log record to return <b>DB_REP_ISPERM</b>
619 because later records
620 were also processed.<br>
621 <br>
622 The current implementation should work fine in that case because when
623 we store the message in the client temp db we store both the control
624 DBT and the record DBT.&nbsp; Therefore, when a normal record fills a
625 gap, the later PERM record, when retrieved will look just like it did
626 when it arrived.&nbsp; The client will have access to the LSN, and the
627 timestamp, etc.&nbsp; However, it does mean that sending the <b>REP_LEASE_GRANT</b>
628 message must take
629 place down in <i>__rep_apply</i>
630 because that is the only place we have access to the contents of those
631 stored records with the timestamps.<br>
632 <br>
633 There are two logical choices to consider for granting the lease when
634 processing an update.&nbsp; As we process (either a live record or one
635 read from the temp db after filling a gap) a PERM message, we send the <b>REP_LEASE_GRANT</b>
636 message for each
637 PERM record we successfully apply.&nbsp; Or, second, we keep track of
638 the largest timestamp of all PERM records we've processed and at the
639 end of the function after we've applied all records, we send back a
640 single lease grant with the <i>max_perm_lsn</i>
641 and a new <i>max_lease_timestamp</i>
642 value to the master.&nbsp; The first is easier to implement, the second
643 results in possibly slightly fewer messages at the expense of more
644 bookkeeping on the client.<br>
645 <br>
646 A third, more complicated option would be to have the message timestamp
647 on all records, but grants are only sent on the PERM messages.&nbsp; A
648 reason to do this is that the later timestamp of a normal log record
649 would be used as the timestamp sent in the reply and the master would
650 get a more up to date timestamp value and a longer lease.&nbsp; <br>
651 <br>
652 If we change the <span style="font-weight: bold;">REP_CONTROL</span>
653 structure to include the timestamp, we potentially break or at least
654 need to revisit the gap processing algorithm.&nbsp; That code assumes
655 that the control and record elements for the same LSN look the same
656 each and every time.&nbsp; The code stores the <span
657 style="font-style: italic;">control</span> DBT as the key and the <span
658 style="font-style: italic;">rec</span> DBT as the data.&nbsp; We use a
659 specialized compare function to sort based on the LSN in the control
660 DBT.&nbsp; With master leases, the same record transmitted by a master
661 multiple times or client for the same LSN will be different because the
662 timestamp field will not be the same.&nbsp; Therefore, the client will
663 end up with duplicate entries in the temp database for the same
664 LSN.&nbsp; Both solutions (adding the timestamp to <span
665 style="font-weight: bold;">REP_CONTROL</span> and adding a <span
666 style="font-weight: bold;">REPCTL_LEASE</span> flag) can yield
667 duplicate entries.&nbsp; The flag would cause the same record from the
668 master and client to be different as well.<br>
669 <h4>Handling Incoming Lease Grants<br>
670 </h4>
671 The third piece of lease management is handling the incoming <b>REP_LEASE_GRANT</b>
672 message on the
673 master.&nbsp; When this message is received, the master must do the
674 following:<br>
675 <pre>REP_SYSTEM_LOCK<br>msg_timestamp = cntrl-&gt;timestamp;<br>client_lease = __rep_lease_entry(dbenv, client eid)<br>if (client_lease == NULL)<br> initial lease for this site, DB_ASSERT there is space in the table<br> add this to the table if there is space<br>} else <br> compare msg_timestamp with client_lease-&gt;start_time<br> if (msg_timestamp is more recent &amp;&amp; msg_lsn &gt;= lease LSN)<br> update entry in table<br>REP_SYSTEM_UNLOCK<br></pre>
676 <h3>Expiring Leases</h3>
677 Leases can expire in two ways.&nbsp; First they can expire naturally
678 due to the passage of time.&nbsp; When checking leases, if the current
679 time is later than the lease entry's <i>end_time</i>
680 then the lease is expired.&nbsp; Second, they can be forced with a
681 premature expiration when the application's transport function returns
682 an error.&nbsp; In the first case, there is nothing to do, in the
683 second case we need to manipulate the <i>end_time</i>
684 so that all future lease checks fail.&nbsp; Since the lease <i>start_time</i>
685 is guaranteed to not be in the future we will have a function <i>__rep_lease_expire</i>
686 that will:<br>
687 <pre>REP_SYSTEM_LOCK<br>for each entry in the lease table<br> entry-&gt;end_time = entry-&gt;start_time;<br>REP_SYSTEM_UNLOCK<br></pre>
688 Is there a potential race or problem with prematurely expiring
689 leases?&nbsp; Consider an application that enforces an ALL
690 acknowledgement policy for PERM records in its transport
691 callback.&nbsp; There are four clients and three send the PERM ack to
692 the application.&nbsp; The callback returns an error to the master DB
693 code.&nbsp; The DB code will now prematurely expire its leases.&nbsp;
694 However, at approximately the same time the three clients are also
695 sending their <span style="font-weight: bold;">REP_LEASE_GRANT</span>
696 messages to the master.&nbsp; There is a race between the master
697 processing those messages and the thread handling the callback failure
698 expiring the table.&nbsp; This is only an issue if the messages arrive
699 after the table has been expired.<br>
700 <br>
701 Let's assume all three clients send their grants after the master
702 expires the table.&nbsp; If we accept those grants and then a read
703 occurs the read will succeed since the master has a majority of leases
704 even though the callback failed earlier.&nbsp; Is that a problem?&nbsp;
705 The lease code is using a majority and the application policy is using
706 something other value.&nbsp; It feels like this should be okay since
707 the data is held by leases on a majority.&nbsp; Should we consider
708 having the lease checking threshold be the same as the permanent ack
709 policy?&nbsp; That is difficult because Base API users implement
710 whatever they want and DB does not know what it is.<br>
711 <h3>Checking Leases</h3>
712 When a read operation on the master completes, the last thing we need
713 to do is verify the master leases.&nbsp; We've already discussed
714 refreshing them when they are expired above.&nbsp; We need two things
715 for a lease to be valid.&nbsp; It must be within the timeframe of the
716 lease grant and the lease must be valid for the last PERM record
717 LSN.&nbsp; Here is the logic
718 for checking the validity of leases in <i>__rep_lease_check</i>:<br>
719 <pre>#define MAX_REFRESH_TRIES 3<br>DB_LSN lease_lsn;<br>REP_LEASE_ENTRY *entry;<br>u_int32_t min_leases, valid_leases;<br>db_timespec cur_time;<br>int ret, tries;<br><br> tries = 0;<br>retry:<br> ret = 0;<br> LOG_SYSTEM_LOCK<br> lease_lsn = lp-&gt;lsn<br> LOG_SYSTEM_UNLOCK<br> REP_SYSTEM_LOCK<br> min_leases = rep-&gt;nsites / 2;<br> __os_gettime(dbenv, &amp;cur_time);<br> for (entry = head of table, valid_leases = 0; entry != NULL &amp;&amp; valid_leases &lt; min_leases; entry++)<br> if (timespec_cmp(&amp;entry-&gt;end_time, &amp;cur_time) &gt;= 0 &amp;&amp; log_compare(&amp;entry-&gt;lsn, lease_lsn) == 0)<br> valid_leases++;<br> REP_SYSTEM_UNLOCK<br> if (valid_leases &lt; min_leases) {<br> ret =__rep_lease_refresh(dbenv, ...);<br> /*<br> * If we are successful, we need to recheck the leases because <br> * the lease grant messages may have raced with the PERM<br> * acknowledgement. Give those messages a chance to arrive.<br> */<br> if (ret == 0) {<br> if (tries &lt;= MAX_REFRESH_TRIES) {<br> /*<br> * If we were successful sending, but not successful in racing the<br> * message thread, yield the processor so that message<br> * threads may have a chance to run.<br> */<br> if (tries &gt; 0)<br> /* __os_sleep instead?? */<br> __os_yield()<br> tries++;<br> goto retry;<br> } else<br> ret = DB_RET_LEASE_EXPIRED;<br> }<br> }<br> return (ret);</pre>
720 If the master has enough valid leases it returns success.&nbsp; If it
721 does not have enough, it attempts to refresh them.&nbsp; This attempt
722 may fail if sending the PERM record does not receive sufficient
723 acks.&nbsp; If we do receive sufficient acknowledgements we may still
724 find that scheduling of message threads means the master hasn't yet
725 processed the incoming <b>REP_LEASE_GRANT</b>
726 messages yet.&nbsp; We will retry a couple times (possibly
727 parameterized) if the master discovers that situation.&nbsp; <br>
728 <h2>Elections</h2>
729 When a client grants a lease to a master, it gives up the right to
730 participate in an election until that grant expires.&nbsp; If we are
731 the master and <i>dbenv-&gt;rep_elect</i>
732 is called, it should return, no matter what, like it does today.&nbsp;
733 If we are a client and <i>rep_elect</i>
734 is called special processing takes place when leases are in
735 effect.&nbsp; First, the easy case is if the lease granted by this
736 client has already expired, then the client goes directly into the
737 election as normal.&nbsp; If a valid lease grant is outstanding to a
738 master, this site cannot participate in an election until that grant
739 expires.&nbsp; We have at least two options when a site calls the <i>dbenv-&gt;rep_elect</i>
740 API while
741 leases are in effect.<br>
742 <ol>
743 <li>The simplest coding solution for DB would be simply to refuse to
744 participate in the election if this site has a current lease granted to
745 a master.&nbsp; We would detect this situation and return EINVAL.&nbsp;
746 This is correct behavior and trivial to implement.&nbsp; The
747 disadvantage of this solution is that the application would then be
748 responsible for repeatedly attempting an election until the lease grant
749 expired.<br>
750 </li>
751 <li>The more satisfying solution is for DB to wait the remaining time
752 for the grant.&nbsp; If this client hears from the master during that
753 time the election does not take place and the call to <i>rep_elect</i>
754 returns with the
755 information for the current/old master.</li>
756 </ol>
757 <h3>Election Code Changes</h3>
758 The code changes to support leases in the election code are fairly
759 isolated.&nbsp; First if leases are configured, we must verify the <i>nsites</i>
760 parameter is set to 0.&nbsp;
761 Second, in <i>__rep_elect_init</i>
762 we must not overwrite the value of <i>rep-&gt;nsites</i>
763 for leases because it is controlled by the <i>dbenv-&gt;rep_set_nsites</i>
764 API.&nbsp;
765 These changes are small and easy to understand.<br>
766 <br>
767 The more complicated code will be the client code when it has an
768 outstanding lease granted.&nbsp; The client will wait for the current
769 lease grant to expire before proceeding with the election.&nbsp; The
770 client will only do so if it does not hear from the master for the
771 remainder of the lease grant time.&nbsp; If the client hears from the
772 master, it returns and does not begin participating in the
773 election.&nbsp; A new election phase, <b>REP_EPHASE0</b>
774 will exist so that the call to <i>__rep_wait</i>
775 can detect if a master responds.&nbsp; The client, while waiting for
776 the lease grant to expire, will send a <b>REP_MASTER_REQ</b>
777 message so that the master will respond with a <b>REP_NEWMASTER</b>
778 message and thus,
779 allow the client to know the master exists.&nbsp; However, it is also
780 desirable that if the master
781 replies to the client, the master wants the client to update its lease
782 grant.&nbsp; <br>
783 <br>
784 Recall that the <b>REP_NEWMASTER</b>
785 message does not result in a lease grant from the client.&nbsp; The
786 client responds when it processes a PERM record that has the <b>REPCTL_LEASE</b>
787 flag set in the message
788 with its lease grant up to the given LSN.&nbsp; Therefore, we want the
789 client's <b>REP_MASTER_REQ</b> to
790 yield both the discovery of the existing master and have the master
791 refresh its leases.&nbsp; The client will also use the <b>REPCTL_LEASE</b>
792 flag in its <b>REP_MASTER_REQ</b> message to the
793 master.&nbsp; This flag will serve as the indicator to the master that
794 it needs to deal with leases and both send the <b>REP_NEWMASTER</b>
795 message and refresh
796 the lease.<br>
797 The code will work as follows:<br>
798 <pre>if (leases_configured &amp;&amp; (my_grant_still_valid || lease_never_granted) {<br> if (lease_never_granted)<br> wait_time = lease_timeout<br> else<br> wait_time = grant_expiration - current_time<br> F_SET(REP_F_EPHASE0);<br> __rep_send_message(..., REP_MASTER_REQ, ... REPCTL_LEASE);<br> ret = __rep_wait(..., REP_F_EPHASE0);<br> if (we found a master)<br> return<br>} /* if we don't return, fall out and proceed with election */<br></pre>
799 On the master side, the code handling the <b>REP_MASTER_REQ</b> will
800 do:<br>
801 <pre>if (I am master) {<br> ...<br> __rep_send_message(REP_NEWMASTER...)<br> if (F_ISSET(rp, REPCTL_LEASE))<br> __rep_lease_refresh(...)<br>}<br></pre>
802 Other minor implementation details are that<i> __rep_elect_done</i>
803 must also clear
804 the <b>REP_F_EPHASE0</b> flag.&nbsp;
805 We also, obviously, need to define <b>REP_F_EPHASE0</b>
806 in the list of replication flags.&nbsp; Note that the client's call to <i>__rep_wait</i>
807 will return upon
808 receiving the <b>REP_NEWMASTER</b>
809 message.&nbsp; The client will independently refresh its lease when it
810 receives the log record from the master's call to refresh the lease.<br>
811 <br>
812 Again, similar to what I suggested above, the code could simply assume
813 global leases are configured, and instead of having the <b>REPCTL_LEASE</b>
814 flag at all, the master
815 assumes that it needs to refresh leases because it has them configured,
816 not because it is specified in the <b>REP_MASTER_REQ</b>
817 message it is processing. Right now I don't think every possible
818 <b>REP_MASTER_REQ</b> message should result in a lease grant request.<br>
819 <h4>Elections and Quiescient Systems</h4>
820 It is possible that a master is slow or the client is close to its
821 expiration time, or that the master is quiescient and all leases are
822 currently expired, but nothing much is going on anyway, yet some client
823 calls <i>__rep_elect</i> at that
824 time.&nbsp; In the code above, we will not send the <b>REP_MASTER_REQ</b>
825 because the lease is
826 not valid.&nbsp; The client will simply proceed directly to sending the
827 <b>REP_VOTE1</b> message, throwing all
828 other clients into an election.&nbsp; The master is still master and
829 should stay that way.&nbsp; Currently in response to a vote message, a
830 master will broadcast out a <b>REP_NEWMASTER</b>
831 to assert its mastership.&nbsp; That causes the election to
832 complete.&nbsp; However, if desired the master may want to proactively
833 refresh its leases.&nbsp; This situation indicates to me that the
834 master should choose to refresh leases based on configuration, not a
835 flag sent from the client.&nbsp; I believe anytime the master asserts
836 its mastership via sending a <b>REP_NEWMASTER</b>
837 message that I need to add code to proactively refresh leases at that
838 time.<br>
839 <h2>Other Implementation Details</h2>
840 <h3>Role Changes<br>
841 </h3>
842 When a site changes its role via a call to <i>rep_start</i> in either
843 direction, we
844 must take action when leases are configured.&nbsp; There are three
845 types of role changes that all need changes to deal with leases:<br>
846 <ol>
847 <li><i>A master downgrading to a
848 client.</i> When a master downgrades to a client, it can do so
849 immediately after it has proactively expired all existing leases it
850 holds.&nbsp; This situation is similar to an error from the send
851 callback, and it effectively cancels all outstanding leases held on
852 this site.&nbsp; Note that if this master expires its leases, it does
853 not have any effect on when the clients' lease grants expire on the
854 client side.&nbsp; The clients must still wait their full expected
855 grant time.<br>
856 </li>
857 <li><i>A client upgrading to master.</i>
858 If a client is upgrading to a master but it has an outstanding lease
859 granted to another site, the code will return an <b>EINVAL</b>
860 error.&nbsp; This situation
861 only arises if the application simply declares this site master.&nbsp;
862 If a site wins an election then the election itself should have waited
863 long enough for the granted lease to expire and this state should not
864 arise then.</li>
865 <li><i>A client finding a new master.</i>
866 When a client discovers a new and different master, via a <b>REP_NEWMASTER</b>
867 message then the
868 client cannot accept that new master until its current lease grant
869 expires.&nbsp; This situation should only occur when a site declares
870 itself master without an election and that site's lease grant expires
871 before this client's grant expires.&nbsp; However, it is <b>possible</b>
872 for this situation to arise
873 with elections also.&nbsp; If we have 5 sites holding an election and 4
874 of those sites have leases expire at about the same time T, and this
875 site's lease expires at time T+N and the election timeout is &lt; N,
876 then those 4 sites may hold an election and elect a master without this
877 site's participation.&nbsp; A client in this situation must call <i>__rep_wait</i>
878 with the time remaining
879 on its lease.&nbsp; If the lease is expired after waiting the remaining
880 time, then the client can accept this new master.&nbsp; If the lease
881 was refreshed during the waiting period then the client does not accept
882 this new master and returns.<br>
883 </li>
884 </ol>
885 <h3>DUPMASTER</h3>
886 A duplicate master situation can occur if an old master becomes
887 disconnected from the rest of the group, that group elects a new master
888 and then the partition is resolved.&nbsp; The requirement for master
889 leases is that this situation will not cause the newly elected,
890 rightful master to receive the <b>DB_REP_DUPMASTER</b>
891 return.&nbsp; It is okay for the old master to get that return
892 value.&nbsp; When a dual master situation exists, the following will
893 happen:<br>
894 <ul>
895 <li><i>On the current master and all
896 current clients</i> - If the current master receives an update
897 message or other conflicting message from the old master then that
898 message will be ignored because the generation number is out of date.</li>
899 <li><i>On the old master</i> - If
900 the old master receives an update message from the current master, or
901 any other message with a later generation from any site, the new
902 generation number will trigger this site to return <b>DB_REP_DUPMASTER</b>.&nbsp;
903 However,
904 instead of broadcasting out the <b>REP_DUPMASTER</b>
905 message to shoot down others as well, this site, if leases are
906 configured, will call <i>__rep_lease_check</i>
907 and if they are expired, return the error.&nbsp; It should be
908 impossible for us to receive a later generation message and still hold
909 a majority of master leases.&nbsp; Something is seriously wrong and we
910 will <b>DB_ASSERT</b> this situation
911 cannot happen.<br>
912 </li>
913 </ul>
914 <h3>Client to Client Synchronization</h3>
915 One question to ask is how lease grants interact with client-to-client
916 synchronization. The only answer is that they do not.&nbsp; A client
917 that is sending log records to another client cannot request the
918 receiving client refresh its lease with the master.&nbsp; That client
919 does not have a timestamp it can use for the master and clock skew
920 makes it meaningless between machines.&nbsp; Therefore, sites that use
921 client-to-client synchronization will likely see more lease refreshment
922 during the read path and leases will be refreshed during live updates
923 only.&nbsp; Of course, if a client supplies log records that fill a
924 gap, and the later log records stored came from the master in a live
925 update then the client will respond as per the discussion on Gap
926 Processing above.<br>
927 <h2>Interaction Matrix</h2>
928 If leases are granted (by a client) or held (by a master) what should
929 the following APIs and messages do?<br>
930 <br>
931 Other:<br>
932 log_archive: Leases do not affect log_archive.&nbsp; OK.<br>
933 dbenv-&gt;close: OK.<br>
934 crash during lease grant and restart: <b>Potential
935 problem here.&nbsp; See discussion below</b>.<br>
936 <br>
937 Rep Base API method:<br>
938 rep_elect: Already discussed above.&nbsp; Must wait for lease to expire.<br>
939 rep_flush: Master only, OK - this will be the basis for refreshing
940 leases.<br>
941 rep_get_*: Not affected by leases.<br>
942 rep_process_message: Generally OK.&nbsp; We'll discuss each message
943 below.<br>
944 rep_set_config: OK.<br>
945 rep_set_limit: OK<br>
946 rep_set_nsites: Must be called before <i>rep_start</i>
947 and <i>nsites</i> is immutable until
948 14778 is resolved.<br>
949 rep_set_priority: OK<br>
950 rep_set_timeout: OK.&nbsp; Used to set lease timeout.<br>
951 rep_set_transport: OK.<br>
952 rep_start(MASTER): Role changes are discussed above.&nbsp; Make sure
953 duplicate rep_start calls are no-ops for leases.<br>
954 rep_start(CLIENT): Role changes are discussed above.&nbsp; Make sure
955 duplicate calls are no-ops for leases.<br>
956 rep_stat: OK.<br>
957 rep_sync: Should not be able to happen.&nbsp; Client cannot accept new
958 master with outstanding lease grant.&nbsp; Add DB_ASSERT here.<br>
959 <br>
960 REP_ALIVE: OK.<br>
961 REP_ALIVE_REQ: OK.<br>
962 REP_ALL_REQ: OK.<br>
963 REP_BULK_LOG: OK.&nbsp; Clients check to send ACK.<br>
964 REP_BULK_PAGE: Should never process one with lease granted.&nbsp; Add
965 DB_ASSERT.<br>
966 REP_DUPMASTER: Should never happen, this is what leases are supposed to
967 prevent.&nbsp; See above.<br>
968 REP_LOG: OK.&nbsp; Clients check to send ACK.<br>
969 REP_LOG_MORE: OK.&nbsp; Clients check to send ACK.<br>
970 REP_LOG_REQ: OK.<br>
971 REP_MASTER_REQ: OK.<br>
972 REP_NEWCLIENT: OK.<br>
973 REP_NEWFILE: OK.&nbsp; Clients check to send ACK.<br>
974 REP_NEWMASTER: See above.<br>
975 REP_NEWSITE: OK.<br>
976 REP_PAGE: OK.&nbsp; Should never process one with lease granted.&nbsp;
977 Add DB_ASSERT.<br>
978 REP_PAGE_FAIL:&nbsp; OK.&nbsp; Should never process one with lease
979 granted.&nbsp; Add DB_ASSERT.<br>
980 REP_PAGE_MORE:&nbsp; OK.&nbsp; Should never process one with lease
981 granted.&nbsp; Add DB_ASSERT.<br>
982 REP_PAGE_REQ: OK.<br>
983 REP_REREQUEST: OK.<br>
984 REP_UPDATE: OK.&nbsp; Should never process one with lease
985 granted.&nbsp; Add DB_ASSERT.<br>
986 REP_UPDATE_REQ: OK.&nbsp; This is a master-only message.<br>
987 REP_VERIFY: OK.&nbsp; Should never process one with lease
988 granted.&nbsp; Add DB_ASSERT.<br>
989 REP_VERIFY_FAIL: OK.&nbsp; Should never process one with lease
990 granted.&nbsp; Add DB_ASSERT.<br>
991 REP_VERIFY_REQ: OK.<br>
992 REP_VOTE1: OK.&nbsp; See Election discussion above.&nbsp; It is
993 possible to receive one with a lease granted.&nbsp; Client cannot send
994 one with an outstanding lease however.<br>
995 REP_VOTE2: OK.&nbsp; See Election discussion above.&nbsp; It is
996 possible to receive one with a lease granted.<br>
997 <br>
998 If the following method or message processing is in progress and a
999 client wants to grant a lease, what should it do?&nbsp; Let's examine
1000 what this means.&nbsp; The client wanting to grant a lease simply means
1001 it is responding to the receipt of a <b>REP_LOG</b>
1002 (or its variants) message and applying a log record.&nbsp; Therefore,
1003 we need to consider a thread processing a log message racing with these
1004 other actions.<br>
1005 <br>
1006 Other:<br>
1007 log_archive: OK.&nbsp; <br>
1008 dbenv-&gt;close: User error.&nbsp; User should not be closing the env
1009 while other threads are using that handle.&nbsp; Should have no effect
1010 if a 2nd dbenv handle to same env is closed.<br>
1011 <br>
1012 Rep Base API method:<br>
1013 rep_elect: See Election discussion above.&nbsp; <i>rep_elect</i>
1014 should wait and may grant
1015 lease while election is in progress.<br>
1016 rep_flush: Should not be called on client.<br>
1017 rep_get_*: OK.<br>
1018 rep_process_message: Generally OK.&nbsp; See handling each message
1019 below.<br>
1020 rep_set_config: OK.<br>
1021 rep_set_limit: OK.<br>
1022 rep_set_nsites: Must be called before <i>rep_start</i>
1023 until 14778 is resolved.<br>
1024 rep_set_priority: OK.<br>
1025 rep_set_timeout: OK.<br>
1026 rep_set_transport: OK.<br>
1027 rep_start(MASTER): OK, can't happen - already protect racing <i>rep_start</i>
1028 and <i>rep_process_message</i>.<br>
1029 rep_start(CLIENT): OK, can't happen - already protect racing <i>rep_start</i>
1030 and <i>rep_process_message</i>.<br>
1031 rep_stat: OK.<br>
1032 rep_sync: Shouldn't happen because client cannot grant leases during
1033 sync-up.&nbsp; Incoming log message ignored.<br>
1034 <br>
1035 REP_ALIVE: OK.<br>
1036 REP_ALIVE_REQ: OK.<br>
1037 REP_ALL_REQ: OK.<br>
1038 REP_BULK_LOG: OK.<br>
1039 REP_BULK_PAGE: OK.&nbsp; Incoming log message ignored during internal
1040 init.<br>
1041 REP_DUPMASTER: Shouldn't happen.&nbsp; See DUPMASTER discussion above.<br>
1042 REP_LOG: OK.<br>
1043 REP_LOG_MORE: OK.<br>
1044 REP_LOG_REQ: OK.<br>
1045 REP_MASTER_REQ: OK.<br>
1046 REP_NEWCLIENT: OK.<br>
1047 REP_NEWFILE: OK.<br>
1048 REP_NEWMASTER: See above.&nbsp; If a client accepts a new master
1049 because its lease grant expired, then that master sends a message
1050 requesting the lease grant, this client will not process the log record
1051 if it is in sync-up recovery, or it may after the master switch is
1052 complete and the client doesn't need sync-up recovery.&nbsp; Basically,
1053 just uses existing log record processing/newmaster infrastructure.<br>
1054 REP_NEWSITE: OK.<br>
1055 REP_PAGE: OK.&nbsp; Receiving a log record during internal init PAGE
1056 phase should ignore log record.<br>
1057 REP_PAGE_FAIL: OK.<br>
1058 REP_PAGE_MORE: OK.<br>
1059 REP_PAGE_REQ: OK.<br>
1060 REP_REREQUEST: OK.<br>
1061 REP_UPDATE: OK.&nbsp; Receiving a log record during internal init
1062 should ignore log record.<br>
1063 REP_UPDATE_REQ: OK - master-only message.<br>
1064 REP_VERIFY: OK.&nbsp; Receiving a log record during verify phase
1065 ignores log record.<br>
1066 REP_VERIFY_FAIL: OK.<br>
1067 REP_VERIFY_REQ: OK.<br>
1068 REP_VOTE1: OK.&nbsp; This client is processing someone else's vote when
1069 the lease request comes in.&nbsp; That is fine.&nbsp; We protect our
1070 own election and lease interaction in <i>__rep_elect</i>.<br>
1071 REP_VOTE2: OK.<br>
1072 <h4>Crashing - Potential Problem<br>
1073 </h4>
1074 It appears there is one area where we could have a problem.&nbsp; I
1075 believe that crashes can cause us to break our guarantee on durability,
1076 authoritative reads and inability to elect duplicate masters.&nbsp;
1077 Consider this scenario:<br>
1078 <ol>
1079 <li>A master and 4 clients are all up and running.</li>
1080 <li>The master commits a txn and all 4 clients refresh their lease
1081 grants at time T.</li>
1082 <li>All 4 clients have the txn and log records in the cache.&nbsp;
1083 None are flushing to disk.</li>
1084 <li>All 4 clients have responded to the PERM messages as well as
1085 refreshed their lease with the master.</li>
1086 <li>All 4 clients hit the same application coding error and crash
1087 (machine/OS stays up).</li>
1088 <li>Master authoritatively reads data in txn from step 2.</li>
1089 <li>All 4 clients restart the application and run recovery, thus the
1090 txn from step 2 is lost on all clients because it isn't any logs.<span
1091 style="font-weight: bold;"></span><br>
1092 </li>
1093 <li>A network partition happens and the master is alone on its side.</li>
1094 <li>All 4 clients are on the other side and elect a new master.</li>
1095 <li>Partition resolves itself and we have duplicate masters, where
1096 the former master still holds all valid lease grants.<span
1097 style="font-weight: bold;"></span><br>
1098 </li>
1099 </ol>
1100 Therefore, we have broken both guarantees.&nbsp; In step 6 the data is
1101 really not durable and we've given it to the user.&nbsp; One can argue
1102 that if this is an issue the application better be syncing somewhere if
1103 they really want durability.&nbsp; However, worse than that is that we
1104 have a legitimate DUPMASTER situation in step 10 where both masters
1105 hold valid leases.&nbsp; The reason is that all lease knowledge is in
1106 the shared memory and that is lost when the app restarts and runs
1107 recovery.<br>
1108 <br>
1109 How can we solve this?&nbsp; The obvious solution is (ugh, yet another)
1110 durable BDB-owned file with some information in it, such as the current
1111 lease expiration time so that rebooting after a crash leaves the
1112 knowledge that the lease was granted.&nbsp; However, writing and
1113 syncing every lease grant on every client out to disk is far too
1114 expensive.<br>
1115 <br>
1116 A second possible solution is to have clients wait a full lease timeout
1117 before entering an election the first time. This solution solves the
1118 DUPMASTER issue, but not the non-authoritative read.&nbsp; This
1119 solution naturally falls out of elections and leases really.&nbsp; If a
1120 client has never granted a lease, it should be considered as having to
1121 wait a full lease timeout before entering an election.&nbsp;
1122 Applications already know that leases impact elections and this does
1123 not seem so bad as it is only on the first election.<br>
1124 <br>
1125 Is it sufficient to document that the authoritative read is only as
1126 authoritative as the durability guarantees they make on the sites that
1127 indicate it is permanent? Yes, I believe this is sufficient.&nbsp; If
1128 the application says it is permanent and it really isn't, then the
1129 application is at fault.&nbsp; Believing the application when it
1130 indicates with the PERM response that it is permanent avoids the
1131 authoritative problem.&nbsp; <br>
1132 <h2>Upgrade/Mixed Versions</h2>
1133 Clearly leases cannot be used with mixed version sites since masters
1134 running older releases will not have any knowledge of lease
1135 support.&nbsp; What considerations are needed in the lease code for
1136 mixed versions?<br>
1137 <br>
1138 First if the <b>REP_CONTROL</b>
1139 structure changes, we need to maintain and use an old version of the
1140 structure for talking to older clients and masters.&nbsp; The
1141 implementation of this would be similar to the way we manage for old <b>REP_VOTE_INFO</b>
1142 structures.&nbsp;
1143 Second any new messages need translation table entries added.&nbsp;
1144 Third, if we are assuming global leases then clearly any mixed versions
1145 cannot have leases configured, and leases cannot be used in mixed
1146 version groups.&nbsp; Maintaining two versions of the control structure
1147 is not necessary if we choose a different style of implementation and
1148 don't change the control structure.<br>
1149 <br>
1150 However, then how could an old application both run continuously,
1151 upgrade to the new release and take advantage of leases without taking
1152 down the entire application?&nbsp; I believe it is possible for clients
1153 to be configured for leases but be subject to the master regarding
1154 leases, yet the master code can assume that if it has leases
1155 configured, all client sites do as well.&nbsp; In several places above
1156 I suggested that a client could make a choice based on either a new <b>REPCTL_LEASE</b>
1157 flag or simply having
1158 leases turned on locally.&nbsp; If we choose to use the flag, then we
1159 can support leases with mixed versions.&nbsp; The upgraded clients can
1160 configure leases and they simply will not be granted until the old
1161 master is upgraded and send PERM message with the flag indicating it
1162 wants a lease grant.&nbsp; The client will not grant a lease until such
1163 time.&nbsp; The clients, while having the leases configured, will not
1164 grant a lease until told to do so and will simply have an expired
1165 lease.&nbsp; Then, when the old master finally upgrades, it too can
1166 configure leases and suddenly all sites are using them.&nbsp; I believe
1167 this should work just fine and I will need to make sure a client's
1168 granting of leases is only in response to the master asking for a
1169 grant.&nbsp; If the master never asks, then the client has them
1170 configured, but doesn't grant them.<br>
1171 <h2>Testing</h2>
1172 Clearly any user-facing API changes will need the equivalent reflection
1173 in the Tcl API for testing, under CONFIG_TEST.<br>
1174 <br>
1175 I am sure the list of tests will grow but off the top of my head:<br>
1176 Basic test: have N sites all configure leases, run some,&nbsp; read on
1177 master, etc.<br>
1178 Refresh test: Perform update on master, sleep until past expiration,
1179 read on master and make sure leases are refreshed/read successful<br>
1180 Error test: Test error conditions (reading on client with leases but no
1181 ignore flag, calling after rep_start, etc)<br>
1182 Read test: Test reading on both client and master both with and without
1183 the IGNORE flag.&nbsp; Test that data read with the ignore flag can be
1184 rolled back.<br>
1185 Dupmaster test: Force a DUPMASTER situation and verify that the newer
1186 master cannot get DUPMASTER error.<br>
1187 Election test: Call election while grant is outstanding and master
1188 exists.<br>
1189 Call election while grant is outstanding and master does not exist.<br>
1190 Call election after expiration on quiescient system with master
1191 existing.<br>
1192 Run with a group where some members have leases configured and other do
1193 not to make sure we get errors instead of dumping core.<br>
1194 <br>
1195 <small><br>
1196 </small>
1197 </body>
1198 </html>