| 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>Master Leases</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="rep.html" title="Chapter 12. Berkeley DB Replication" /> |
| 11 | <link rel="prev" href="rep_trans.html" title="Transactional guarantees" /> |
| 12 | <link rel="next" href="rep_ryw.html" title="Read your writes consistency" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Master Leases</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="rep_trans.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 12. |
| 26 | Berkeley DB Replication |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="rep_ryw.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="rep_lease"></a>Master Leases</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div class="toc"> |
| 42 | <dl> |
| 43 | <dt> |
| 44 | <span class="sect2"> |
| 45 | <a href="rep_lease.html#masterlease_change_groupsize">Changing Group Size</a> |
| 46 | </span> |
| 47 | </dt> |
| 48 | </dl> |
| 49 | </div> |
| 50 | <p> |
| 51 | Some applications have strict requirements about the consistency of |
| 52 | data read on a master site. Berkeley DB provides a mechanism called |
| 53 | master leases to provide such consistency. Without master leases, it |
| 54 | is sometimes possible for Berkeley DB to return old data to an |
| 55 | application when newer data is available due to unfortunate scheduling |
| 56 | as illustrated below: |
| 57 | </p> |
| 58 | <div class="orderedlist"> |
| 59 | <ol type="1"> |
| 60 | <li><span class="bold"><strong>Application on master site</strong></span>: Read data item |
| 61 | <span class="emphasis"><em>foo</em></span> via Berkeley DB <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> call. |
| 62 | </li> |
| 63 | <li><span class="bold"><strong>Application on master site</strong></span>: sleep, get descheduled, etc. |
| 64 | </li> |
| 65 | <li><span class="bold"><strong>System</strong></span>: Master changes role, becomes a client. |
| 66 | </li> |
| 67 | <li><span class="bold"><strong>System</strong></span>: New site is elected master. |
| 68 | </li> |
| 69 | <li><span class="bold"><strong>System</strong></span>: New master modifies data item |
| 70 | <span class="emphasis"><em>foo</em></span>. |
| 71 | </li> |
| 72 | <li><span class="bold"><strong>Application</strong></span>: Berkeley DB returns old data for |
| 73 | <span class="emphasis"><em>foo</em></span> to application. |
| 74 | </li> |
| 75 | </ol> |
| 76 | </div> |
| 77 | <p> |
| 78 | By using master leases, Berkeley DB can provide guarantees about the |
| 79 | consistency of data read on a master site. The master site can be |
| 80 | considered a recognized authority for the data and consequently can |
| 81 | provide authoritative reads. Clients grant master leases to a master |
| 82 | site. By doing so, clients acknowledge the right of that site to |
| 83 | retain the role of master for a period of time. During that period of |
| 84 | time, clients cannot elect a new master, become master, nor grant their |
| 85 | lease to another site. |
| 86 | </p> |
| 87 | <p> |
| 88 | By holding a collection of granted leases, a master site can guarantee |
| 89 | to the application that the data returned is the current, authoritative |
| 90 | value. As a master performs operations, it continually requests |
| 91 | updated grants from the clients. When a read operation is required, |
| 92 | the master guarantees that it holds a valid collection of lease grants |
| 93 | from clients before returning data to the application. By holding |
| 94 | leases, Berkeley DB provides several guarantees to the application: |
| 95 | </p> |
| 96 | <div class="orderedlist"> |
| 97 | <ol type="1"> |
| 98 | <li> |
| 99 | Authoritative reads: A guarantee that the data being read by the |
| 100 | application is the current value. |
| 101 | </li> |
| 102 | <li> |
| 103 | <p> |
| 104 | Durability from rollback: A guarantee that the data being |
| 105 | written or read by the application is permanent across a |
| 106 | majority of client sites and will never be rolled back. |
| 107 | </p> |
| 108 | <p> |
| 109 | The rollback guarantee also depends on the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> flag. The |
| 110 | guarantee is effective as long as there isn't total replication group |
| 111 | failure while clients have granted leases but are holding the updates |
| 112 | in their cache. The application must weigh the performance impact of |
| 113 | synchronous transactions against the risk of total replication group |
| 114 | failure. If clients grant a lease while holding updated data in cache, |
| 115 | and total failure occurs, then the data is no longer present on the |
| 116 | clients and rollback can occur if the master also crashes. |
| 117 | </p> |
| 118 | <p> |
| 119 | The guarantee that data will not be rolled back applies only to data |
| 120 | successfully committed on a master. Data read on a client, or read |
| 121 | while ignoring leases can be rolled back. |
| 122 | </p> |
| 123 | </li> |
| 124 | <li> |
| 125 | <p> |
| 126 | Freshness: A guarantee that the data being read by the |
| 127 | application on the <span class="emphasis"><em>master</em></span> is up-to-date |
| 128 | and has not been modified or removed during the read. |
| 129 | </p> |
| 130 | <p> |
| 131 | The read authority is only on the master. Read operations on a |
| 132 | client always ignore leases and consequently, these operations |
| 133 | can return stale data. |
| 134 | </p> |
| 135 | </li> |
| 136 | <li> |
| 137 | <p> |
| 138 | Master viability: A guarantee that a current master with valid |
| 139 | leases cannot encounter a duplicate master situation. |
| 140 | </p> |
| 141 | <p> |
| 142 | Leases remove the possibility of a duplicate master situation that |
| 143 | forces the current master to downgrade to a client. However, it is |
| 144 | still possible that old masters with expired leases can discover a |
| 145 | later master and return <a href="../api_reference/C/repmessage.html#repmsg_DB_REP_DUPMASTER" class="olink">DB_REP_DUPMASTER</a> to the application. |
| 146 | </p> |
| 147 | </li> |
| 148 | </ol> |
| 149 | </div> |
| 150 | <p> |
| 151 | There are several requirements of the application using leases: |
| 152 | </p> |
| 153 | <div class="orderedlist"> |
| 154 | <ol type="1"> |
| 155 | <li> |
| 156 | Replication Manager applications must configure a majority (or |
| 157 | larger) acknowledgement policy via the <a href="../api_reference/C/repmgrset_ack_policy.html" class="olink">DB_ENV->repmgr_set_ack_policy()</a> method. |
| 158 | Base API applications must implement and enforce such a policy on |
| 159 | their own. |
| 160 | </li> |
| 161 | <li> |
| 162 | Base API applications must return an error from the send callback |
| 163 | function when the majority acknowledgement policy is not met for |
| 164 | permanent records marked with <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a>. Note that the |
| 165 | Replication Manager automatically fulfills this requirement. |
| 166 | </li> |
| 167 | <li> |
| 168 | Base API applications must set the number of sites in the group |
| 169 | using the <a href="../api_reference/C/repnsites.html" class="olink">DB_ENV->rep_set_nsites()</a> method before starting replication and cannot |
| 170 | change it during operation. |
| 171 | </li> |
| 172 | <li> |
| 173 | Using leases in a replication group is all or none. Behavior is |
| 174 | undefined when some sites configure leases and others do not. Use |
| 175 | the <a href="../api_reference/C/repconfig.html" class="olink">DB_ENV->rep_set_config()</a> method to turn on leases. |
| 176 | </li> |
| 177 | <li> |
| 178 | The configured lease timeout value must be the same on all sites |
| 179 | in a replication group, set via the <a href="../api_reference/C/repset_timeout.html" class="olink">DB_ENV->rep_set_timeout()</a> method. |
| 180 | </li> |
| 181 | <li> |
| 182 | The configured clock_scale_factor value must be the same on all |
| 183 | sites in a replication group. This value defaults to no skew, but |
| 184 | can be set via the <a href="../api_reference/C/repclockskew.html" class="olink">DB_ENV->rep_set_clockskew()</a> method. |
| 185 | </li> |
| 186 | <li> |
| 187 | Applications that care about read guarantees must perform all read |
| 188 | operations on the master. Reading on a client does not guarantee |
| 189 | freshness. |
| 190 | </li> |
| 191 | <li> |
| 192 | The application must use elections to choose a master site. It |
| 193 | must never simply declare a master without having won an election |
| 194 | (as is allowed without Master Leases). |
| 195 | </li> |
| 196 | </ol> |
| 197 | </div> |
| 198 | <p> |
| 199 | Master leases are based on timeouts. Berkeley DB assumes that time |
| 200 | always runs forward. Users who change the system clock on either |
| 201 | client or master sites when leases are in use void all guarantees and |
| 202 | can get undefined behavior. See the <a href="../api_reference/C/repset_timeout.html" class="olink">DB_ENV->rep_set_timeout()</a> method for more |
| 203 | information. |
| 204 | </p> |
| 205 | <p> |
| 206 | Applications using master leases should be prepared to handle |
| 207 | <code class="literal">DB_REP_LEASE_EXPIRED</code> errors from read operations |
| 208 | on a master and from the <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN->commit()</a> method. |
| 209 | </p> |
| 210 | <p> |
| 211 | Read operations on a master that should not be subject to leases can |
| 212 | use the <a href="../api_reference/C/dbget.html#get_DB_IGNORE_LEASE" class="olink">DB_IGNORE_LEASE</a> flag to the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> method. Read operations |
| 213 | on a client always imply leases are ignored. |
| 214 | </p> |
| 215 | <p> |
| 216 | Master lease checks cannot succeed until a majority of sites have |
| 217 | completed client synchronization. Read operations on a master performed |
| 218 | before this condition is met can use the <a href="../api_reference/C/dbget.html#get_DB_IGNORE_LEASE" class="olink">DB_IGNORE_LEASE</a> flag to |
| 219 | avoid errors. |
| 220 | </p> |
| 221 | <p> |
| 222 | Clients are forbidden from participating in elections while they have |
| 223 | an outstanding lease granted to a master. Therefore, if the <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a> |
| 224 | method is called, then Berkeley DB will block, waiting until its lease |
| 225 | grant expires before participating in any election. While it waits, |
| 226 | the client attempts to contact the current master. If the client finds |
| 227 | a current master, then it returns from the <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a> method. When |
| 228 | leases are configured and the lease has never yet been granted (on |
| 229 | start-up), clients must wait a full lease timeout before participating |
| 230 | in an election. |
| 231 | </p> |
| 232 | <div class="sect2" lang="en" xml:lang="en"> |
| 233 | <div class="titlepage"> |
| 234 | <div> |
| 235 | <div> |
| 236 | <h3 class="title"><a id="masterlease_change_groupsize"></a>Changing Group Size</h3> |
| 237 | </div> |
| 238 | </div> |
| 239 | </div> |
| 240 | <p> |
| 241 | If you are using master leases and you change the size of your |
| 242 | replication group, there is a remote possibility that you can |
| 243 | lose some data previously thought to be durable. This is only |
| 244 | true for users of the Base API. |
| 245 | </p> |
| 246 | <p> |
| 247 | The problem can arise if you are removing sites from your |
| 248 | replication group. (You might be increasing the size of your |
| 249 | site overall, but if you remove all of the wrong sites you can |
| 250 | lose data.) |
| 251 | </p> |
| 252 | <p> |
| 253 | Suppose you have a replication group with five sites; A, B, C, |
| 254 | D and E; and you are using a quorum acknowledgement policy. Then: |
| 255 | </p> |
| 256 | <div class="orderedlist"> |
| 257 | <ol type="1"> |
| 258 | <li> |
| 259 | <p> |
| 260 | Master A replicates a transaction to replicas B and C. |
| 261 | Those sites acknowledge the write activity. |
| 262 | </p> |
| 263 | </li> |
| 264 | <li> |
| 265 | <p> |
| 266 | Sites D and E do not receive the transaction. However, |
| 267 | B and C have acknowledged the transaction, which means the |
| 268 | acknowledgement policy is met and so the transaction is |
| 269 | considered durable. |
| 270 | </p> |
| 271 | </li> |
| 272 | <li> |
| 273 | <p> |
| 274 | You shutdown sites B and C. Now only A has the transaction. |
| 275 | </p> |
| 276 | </li> |
| 277 | <li> |
| 278 | <p> |
| 279 | You increase the size of your replication group to 3 |
| 280 | using <a href="../api_reference/C/repnsites.html" class="olink">DB_ENV->rep_set_nsites()</a>. |
| 281 | </p> |
| 282 | </li> |
| 283 | <li> |
| 284 | <p> |
| 285 | You shutdown or otherwise lose site A. |
| 286 | </p> |
| 287 | </li> |
| 288 | <li> |
| 289 | <p> |
| 290 | Sites D and E hold an election. Because the size of the |
| 291 | replication group is 3, they have enough sites to |
| 292 | successfully hold an election. However, neither site |
| 293 | has the transaction in question. In this way, the |
| 294 | transaction can become lost. |
| 295 | </p> |
| 296 | </li> |
| 297 | </ol> |
| 298 | </div> |
| 299 | <p> |
| 300 | An alternative scenario exists where you do not change the size |
| 301 | of your replication group, or you actually increase the size of |
| 302 | your replication group, but in the process you happen to remove |
| 303 | the exact wrong sites: |
| 304 | </p> |
| 305 | <div class="orderedlist"> |
| 306 | <ol type="1"> |
| 307 | <li> |
| 308 | <p> |
| 309 | Master A replicates a transaction to replicas B and C. |
| 310 | Those sites acknowledge the write activity. |
| 311 | </p> |
| 312 | </li> |
| 313 | <li> |
| 314 | <p> |
| 315 | Sites D and E do not receive the transaction. However, |
| 316 | B and C have acknowledged the transaction, which means the |
| 317 | acknowledgement policy is met and so the transaction is |
| 318 | considered durable. |
| 319 | </p> |
| 320 | </li> |
| 321 | <li> |
| 322 | <p> |
| 323 | You shutdown sites B and C. Now only A has the transaction. |
| 324 | </p> |
| 325 | </li> |
| 326 | <li> |
| 327 | <p> |
| 328 | You add three new sites to your replication group: F, |
| 329 | G and H, increasing the size of your replication group |
| 330 | to 6 using <a href="../api_reference/C/repnsites.html" class="olink">DB_ENV->rep_set_nsites()</a>. |
| 331 | </p> |
| 332 | </li> |
| 333 | <li> |
| 334 | <p> |
| 335 | You shutdown or otherwise lose site A before F, G and H |
| 336 | can be fully populated with data. |
| 337 | </p> |
| 338 | </li> |
| 339 | <li> |
| 340 | <p> |
| 341 | Sites D, E, F, G and H hold an election. Because the size of the |
| 342 | replication group is 6, they have enough sites to |
| 343 | successfully hold an election. However, none of these sites |
| 344 | has the transaction in question. In this way, the |
| 345 | transaction can become lost. |
| 346 | </p> |
| 347 | </li> |
| 348 | </ol> |
| 349 | </div> |
| 350 | <p> |
| 351 | This scenario represents a race condition that would be highly |
| 352 | unlikely to be seen outside of a lab environment. To minimize |
| 353 | the chance of this race condition occurring to the absolute |
| 354 | minimum, do one or more of the following when using master |
| 355 | leases with the Base API: |
| 356 | </p> |
| 357 | <div class="orderedlist"> |
| 358 | <ol type="1"> |
| 359 | <li> |
| 360 | <p> |
| 361 | Require all sites to acknowledge transaction commits. |
| 362 | </p> |
| 363 | </li> |
| 364 | <li> |
| 365 | <p> |
| 366 | Never change the size of your replication group unless |
| 367 | all sites in the group are running and communicating |
| 368 | normally with one another. |
| 369 | </p> |
| 370 | </li> |
| 371 | <li> |
| 372 | <p> |
| 373 | Don't remove (or replace) a large percentage of your |
| 374 | sites from your replication group unless all sites in |
| 375 | the group are running and communicating normally with |
| 376 | one another. If you are going to remove a large |
| 377 | percentage of your sites from your replication group, |
| 378 | try removing just one site at a time, pausing in |
| 379 | between each removal to give the replication group a |
| 380 | chance to fully distribute all writes before removing |
| 381 | the next site. |
| 382 | </p> |
| 383 | </li> |
| 384 | </ol> |
| 385 | </div> |
| 386 | </div> |
| 387 | </div> |
| 388 | <div class="navfooter"> |
| 389 | <hr /> |
| 390 | <table width="100%" summary="Navigation footer"> |
| 391 | <tr> |
| 392 | <td width="40%" align="left"><a accesskey="p" href="rep_trans.html">Prev</a> </td> |
| 393 | <td width="20%" align="center"> |
| 394 | <a accesskey="u" href="rep.html">Up</a> |
| 395 | </td> |
| 396 | <td width="40%" align="right"> <a accesskey="n" href="rep_ryw.html">Next</a></td> |
| 397 | </tr> |
| 398 | <tr> |
| 399 | <td width="40%" align="left" valign="top">Transactional guarantees </td> |
| 400 | <td width="20%" align="center"> |
| 401 | <a accesskey="h" href="index.html">Home</a> |
| 402 | </td> |
| 403 | <td width="40%" align="right" valign="top"> Read your writes consistency</td> |
| 404 | </tr> |
| 405 | </table> |
| 406 | </div> |
| 407 | </body> |
| 408 | </html> |