| 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>Writing In-Memory Berkeley DB Applications</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="Writing In-Memory Berkeley DB Applications" /> |
| 10 |
</head> |
| 11 |
<body> |
| 12 |
<div xmlns="" class="navheader"> |
| 13 |
<div class="libver"> |
| 14 |
<p>(Library Version 11.2.5.3)</p> |
| 15 |
</div> |
| 16 |
<table width="100%" summary="Navigation header"> |
| 17 |
<tr> |
| 18 |
<th colspan="3" align="center">Writing In-Memory Berkeley DB Applications</th> |
| 19 |
</tr> |
| 20 |
</table> |
| 21 |
<hr /> |
| 22 |
</div> |
| 23 |
<div class="article" lang="en" xml:lang="en"> |
| 24 |
<div class="titlepage"> |
| 25 |
<div> |
| 26 |
<div> |
| 27 |
<h1 class="title"><a id="idp22816"></a>Writing In-Memory Berkeley DB Applications</h1> |
| 28 |
</div> |
| 29 |
<div> |
| 30 |
<div class="legalnotice"> |
| 31 |
<a id="idp41376"></a> |
| 32 |
<p class="legalnotice-title"> |
| 33 |
<b>Legal Notice</b> |
| 34 |
</p> |
| 35 |
<span> |
| 36 |
<p> |
| 37 |
This documentation is distributed under an open source license. |
| 38 |
You may review the terms of this license at: |
| 39 |
<a class="ulink" href="http://www.oracle.com/technetwork/database/berkeleydb/downloads/oslicense-093458.html" target="_top">http://www.oracle.com/technetwork/database/berkeleydb/downloads/oslicense-093458.html</a> |
| 40 |
|
| 41 |
|
| 42 |
</p> |
| 43 |
|
| 44 |
<p> |
| 45 |
Oracle, Berkeley DB, |
| 46 |
|
| 47 |
|
| 48 |
and |
| 49 |
Sleepycat are trademarks or registered trademarks of |
| 50 |
Oracle. All rights to these marks are reserved. |
| 51 |
No third-party use is permitted without the |
| 52 |
express prior written consent of Oracle. |
| 53 |
</p> |
| 54 |
|
| 55 |
<p> |
| 56 |
Other names may be trademarks of their respective owners. |
| 57 |
</p> |
| 58 |
<p> |
| 59 |
To obtain a copy of this document's original source code, please |
| 60 |
submit a request to the Oracle Technology Network forum at: |
| 61 |
<a class="ulink" href="http://forums.oracle.com/forums/forum.jspa?forumID=271" target="_top">http://forums.oracle.com/forums/forum.jspa?forumID=271</a> |
| 62 |
|
| 63 |
|
| 64 |
</p> |
| 65 |
</span> |
| 66 |
</div> |
| 67 |
</div> |
| 68 |
<div> |
| 69 |
<p class="pubdate">9/9/2013</p> |
| 70 |
</div> |
| 71 |
</div> |
| 72 |
<hr /> |
| 73 |
</div> |
| 74 |
<div class="toc"> |
| 75 |
<p> |
| 76 |
<b>Table of Contents</b> |
| 77 |
</p> |
| 78 |
<dl> |
| 79 |
<dt> |
| 80 |
<span class="sect1"> |
| 81 |
<a href="index.html#intro">Introduction</a> |
| 82 |
</span> |
| 83 |
</dt> |
| 84 |
<dt> |
| 85 |
<span class="sect1"> |
| 86 |
<a href="index.html#resources">Resources to be Managed</a> |
| 87 |
</span> |
| 88 |
</dt> |
| 89 |
<dt> |
| 90 |
<span class="sect1"> |
| 91 |
<a href="index.html#strategies">Strategies</a> |
| 92 |
</span> |
| 93 |
</dt> |
| 94 |
<dt> |
| 95 |
<span class="sect1"> |
| 96 |
<a href="index.html#dbfiles">Keeping the Database in Memory</a> |
| 97 |
</span> |
| 98 |
</dt> |
| 99 |
<dt> |
| 100 |
<span class="sect1"> |
| 101 |
<a href="index.html#env">Keeping Environments in Memory</a> |
| 102 |
</span> |
| 103 |
</dt> |
| 104 |
<dt> |
| 105 |
<span class="sect1"> |
| 106 |
<a href="index.html#cachesize">Sizing the Cache</a> |
| 107 |
</span> |
| 108 |
</dt> |
| 109 |
<dd> |
| 110 |
<dl> |
| 111 |
<dt> |
| 112 |
<span class="sect2"> |
| 113 |
<a href="index.html#dbcachesize-db">Specifying a Cache Size using the |
| 114 |
Database Handle</a> |
| 115 |
</span> |
| 116 |
</dt> |
| 117 |
<dt> |
| 118 |
<span class="sect2"> |
| 119 |
<a href="index.html#dbcachesize-env">Specifying a Cache Size using the |
| 120 |
Environment Handle</a> |
| 121 |
</span> |
| 122 |
</dt> |
| 123 |
</dl> |
| 124 |
</dd> |
| 125 |
<dt> |
| 126 |
<span class="sect1"> |
| 127 |
<a href="index.html#mpool-nofile">Keeping Temporary Overflow Pages in Memory</a> |
| 128 |
</span> |
| 129 |
</dt> |
| 130 |
<dt> |
| 131 |
<span class="sect1"> |
| 132 |
<a href="index.html#logs">Keeping Logs in Memory</a> |
| 133 |
</span> |
| 134 |
</dt> |
| 135 |
<dt> |
| 136 |
<span class="sect1"> |
| 137 |
<a href="index.html#in-mem-rep">In-Memory Replicated Applications</a> |
| 138 |
</span> |
| 139 |
</dt> |
| 140 |
<dt> |
| 141 |
<span class="sect1"> |
| 142 |
<a href="index.html#example_in-mem">Example In-Memory Application</a> |
| 143 |
</span> |
| 144 |
</dt> |
| 145 |
</dl> |
| 146 |
</div> |
| 147 |
<div class="sect1" lang="en" xml:lang="en"> |
| 148 |
<div class="titlepage"> |
| 149 |
<div> |
| 150 |
<div> |
| 151 |
<h2 class="title" style="clear: both"><a id="intro"></a>Introduction</h2> |
| 152 |
</div> |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
<p> |
| 156 |
This document describes how to write a DB application |
| 157 |
that keeps its data entirely in memory. That is, the |
| 158 |
application writes no data to disk. For this reason, |
| 159 |
in-memory only applications typically discard all |
| 160 |
data durability guarantees. |
| 161 |
</p> |
| 162 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 163 |
<h3 class="title">Note</h3> |
| 164 |
<p> |
| 165 |
This document assume familiarity with the |
| 166 |
<em class="citetitle">Getting Started with Berkeley DB</em> guide. If |
| 167 |
you are using environments or transactions, then |
| 168 |
you should also have an understanding of the |
| 169 |
concepts in <em class="citetitle">Berkeley DB Getting Started with Transaction Processing</em> |
| 170 |
guide. |
| 171 |
</p> |
| 172 |
</div> |
| 173 |
<p> |
| 174 |
There are several reasons why you might want to write an |
| 175 |
in-memory only DB application. For platforms on which a |
| 176 |
disk drive is available to back your data, an in-memory |
| 177 |
application might be desirable from a performance |
| 178 |
perspective. In this case, the data that your application |
| 179 |
manages might be generated during run-time and so is of no |
| 180 |
interest across application startups. |
| 181 |
</p> |
| 182 |
<p> |
| 183 |
Other platforms are disk-less. In this case, an in-memory only |
| 184 |
configuration is the only possible choice. Note that this |
| 185 |
document's primary focus is disk-less systems for which an |
| 186 |
on-disk filesystem is not available. |
| 187 |
</p> |
| 188 |
</div> |
| 189 |
<div class="sect1" lang="en" xml:lang="en"> |
| 190 |
<div class="titlepage"> |
| 191 |
<div> |
| 192 |
<div> |
| 193 |
<h2 class="title" style="clear: both"><a id="resources"></a>Resources to be Managed</h2> |
| 194 |
</div> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
<p> |
| 198 |
Before continuing, it is worthwhile to briefly |
| 199 |
describe the DB resources that must be managed |
| 200 |
if you are going to configure an in-memory only |
| 201 |
DB application. These are resources that are by |
| 202 |
default persisted on disk, or backed by a |
| 203 |
filesystem on disk. Some configuration is therefore |
| 204 |
required to keep these resources in-memory only. |
| 205 |
</p> |
| 206 |
<p> |
| 207 |
Note that you can configure only some of these |
| 208 |
resources to be held in-memory, and allow others to |
| 209 |
be backed by disk. This might be desireable for |
| 210 |
some applications that wish to improve application |
| 211 |
performance by, for example, eliminating disk I/O |
| 212 |
for some, but not all, of these resources. However, |
| 213 |
for the purpose of this document, we assume you |
| 214 |
want to configure all of these resources to be held |
| 215 |
in memory. |
| 216 |
</p> |
| 217 |
<p> |
| 218 |
Managing these resources for an in-memory |
| 219 |
application is described in detail later in this |
| 220 |
article. |
| 221 |
</p> |
| 222 |
<div class="itemizedlist"> |
| 223 |
<ul type="disc"> |
| 224 |
<li> |
| 225 |
<p> |
| 226 |
Database files |
| 227 |
</p> |
| 228 |
<p> |
| 229 |
Normally, DB stores your |
| 230 |
database data within on-disk files. |
| 231 |
For an entirely in-memory |
| 232 |
application, you are required to turn |
| 233 |
off this behavior. |
| 234 |
</p> |
| 235 |
</li> |
| 236 |
<li> |
| 237 |
<p> |
| 238 |
Environment region files |
| 239 |
</p> |
| 240 |
<p> |
| 241 |
DB environments manage region |
| 242 |
files for a variety of purposes. |
| 243 |
Normally these are backed by the |
| 244 |
filesystem, but by using the |
| 245 |
appropriate configuration option |
| 246 |
you can cause region files to |
| 247 |
reside in memory only. |
| 248 |
</p> |
| 249 |
</li> |
| 250 |
<li> |
| 251 |
<p> |
| 252 |
Database cache |
| 253 |
</p> |
| 254 |
<p> |
| 255 |
The DB cache must be configured |
| 256 |
large enough to hold all your data |
| 257 |
in memory. If you do not size your |
| 258 |
cache large enough, then DB |
| 259 |
will attempt to write pages to |
| 260 |
disk. In a disk-less system, this |
| 261 |
will result in an abnormal |
| 262 |
termination of your program. |
| 263 |
</p> |
| 264 |
</li> |
| 265 |
<li> |
| 266 |
<p> |
| 267 |
Logs |
| 268 |
</p> |
| 269 |
<p> |
| 270 |
DB logs describe the write activity |
| 271 |
that has occurred in your application. |
| 272 |
They are used for a number of purposes, |
| 273 |
such as recovery operations for |
| 274 |
applications that are seeking data |
| 275 |
durability guarantees. |
| 276 |
</p> |
| 277 |
<p> |
| 278 |
For in-memory applications that do not |
| 279 |
care about durability guarantees, logs |
| 280 |
are still required if you want |
| 281 |
transactional benefits other than |
| 282 |
durability (such as isolation |
| 283 |
and atomicity). This is because DB's |
| 284 |
transactional subsystem requires logs, |
| 285 |
even if you want to discard all data |
| 286 |
durability guarantees. |
| 287 |
</p> |
| 288 |
<p> |
| 289 |
If this describes your application, you |
| 290 |
must enable logs but configure them to |
| 291 |
reside only within memory. |
| 292 |
</p> |
| 293 |
</li> |
| 294 |
<li> |
| 295 |
<p> |
| 296 |
Temporary overflow pages |
| 297 |
</p> |
| 298 |
<p> |
| 299 |
You must disallow backing temporary |
| 300 |
database files with the |
| 301 |
filesystem. This is mostly a |
| 302 |
configuration option, but it is |
| 303 |
also dependent upon sizing your |
| 304 |
cache correctly. |
| 305 |
</p> |
| 306 |
</li> |
| 307 |
</ul> |
| 308 |
</div> |
| 309 |
<p> |
| 310 |
In addition to these, if you are writing a replicated application (see |
| 311 |
<em class="citetitle">Berkeley DB Getting Started with Replicated Applications</em> for an introduction to writing replicated |
| 312 |
applications), there is internal replication information that is normally kept |
| 313 |
on-disk. You can cause this information to be kept in-memory if you are |
| 314 |
willing to accept some limitations in how your replicated application operates. |
| 315 |
See <a class="xref" href="index.html#in-mem-rep" title="In-Memory Replicated Applications">In-Memory Replicated Applications</a> for more information. |
| 316 |
</p> |
| 317 |
</div> |
| 318 |
<div class="sect1" lang="en" xml:lang="en"> |
| 319 |
<div class="titlepage"> |
| 320 |
<div> |
| 321 |
<div> |
| 322 |
<h2 class="title" style="clear: both"><a id="strategies"></a>Strategies</h2> |
| 323 |
</div> |
| 324 |
</div> |
| 325 |
</div> |
| 326 |
<p> |
| 327 |
DB is an extremely flexible product that can be |
| 328 |
adapted to suit almost any data management |
| 329 |
requirements. This means that you can configure |
| 330 |
DB to operate entirely within memory, but still |
| 331 |
retain some data durability guarantees or even |
| 332 |
throw away all durability guarantees. |
| 333 |
</p> |
| 334 |
<p> |
| 335 |
Data durability guarantees describe how persistent |
| 336 |
your data is. That is, once you have made a change |
| 337 |
to the data stored in your database, how much of a |
| 338 |
guarantee do you require that that modification |
| 339 |
will persist (not be lost)? There are a great many |
| 340 |
options here. For the absolute best durability guarantee, you |
| 341 |
should fully transaction-protect your data and |
| 342 |
allow your data to be written to disk upon each |
| 343 |
transaction commit. Of course, this guarantee is |
| 344 |
not available for disk-less systems. |
| 345 |
</p> |
| 346 |
<p> |
| 347 |
At the opposite end of the spectrum, you can throw |
| 348 |
away all your data once your application is done |
| 349 |
with it (for example, at application shutdown). |
| 350 |
This is a good option if you are using DB only |
| 351 |
as a kind of caching mechanism. In this case, you |
| 352 |
obviously must either generate your data entirely |
| 353 |
during runtime, or obtain it from some remote |
| 354 |
location during application startup. |
| 355 |
</p> |
| 356 |
<p> |
| 357 |
There are also durability options that exist somewhere in |
| 358 |
between these two extremes. For example, disk-less |
| 359 |
systems are sometimes backed by some kind of flash |
| 360 |
memory (e.g. compact flash cards). These |
| 361 |
platforms may want to limit the number of writes |
| 362 |
applied to the backing media because it is capable |
| 363 |
of accepting only a limited number of writes before it must |
| 364 |
be replaced. For this reason, you might want to |
| 365 |
limit data writes to the flash media only during |
| 366 |
specific moments during your application's |
| 367 |
runtime; for example, only at application shutdown. |
| 368 |
</p> |
| 369 |
<p> |
| 370 |
Another way to improve data durability |
| 371 |
for in-memory configurations is to use |
| 372 |
DB replication to commit data to the network. |
| 373 |
This strategy takes advantage of the fact that |
| 374 |
running clients have in-memory copies of the data |
| 375 |
and can take over in the event of an outage at the |
| 376 |
master. The use of replication in this way |
| 377 |
increases durability for your data while providing |
| 378 |
the benefit of avoiding disk I/O on transaction |
| 379 |
commit. |
| 380 |
</p> |
| 381 |
<p> |
| 382 |
In-memory replicated applications are described in more |
| 383 |
detail in |
| 384 |
<a class="xref" href="index.html#in-mem-rep" title="In-Memory Replicated Applications">In-Memory Replicated Applications</a>. |
| 385 |
</p> |
| 386 |
<p> |
| 387 |
The point here is to be aware that a great many |
| 388 |
options are available to you when writing an |
| 389 |
in-memory only application. That said, the focus of |
| 390 |
this document is strictly disk-less systems; that |
| 391 |
is, systems that provide no means by which |
| 392 |
data can be written to persistent media. |
| 393 |
</p> |
| 394 |
</div> |
| 395 |
<div class="sect1" lang="en" xml:lang="en"> |
| 396 |
<div class="titlepage"> |
| 397 |
<div> |
| 398 |
<div> |
| 399 |
<h2 class="title" style="clear: both"><a id="dbfiles"></a>Keeping the Database in Memory</h2> |
| 400 |
</div> |
| 401 |
</div> |
| 402 |
</div> |
| 403 |
<p> |
| 404 |
Normally DB databases are backed by the |
| 405 |
filesystem. For in-memory applications, such as |
| 406 |
is required on disk-less systems, you can cause your |
| 407 |
database to only reside in-memory. That is, |
| 408 |
their contents are stored entirely within DB's |
| 409 |
cache. |
| 410 |
</p> |
| 411 |
<p> |
| 412 |
There are two requirements for keeping your |
| 413 |
database(s) in-memory. |
| 414 |
The first is to size your cache such that it is big |
| 415 |
enough to hold all your data in-memory. If your |
| 416 |
cache fills up, then DB will return |
| 417 |
<code class="literal">ENOMEM</code> on the next operation |
| 418 |
that requests additional pages in the cache. As with all errors while |
| 419 |
updating a database, the current transaction must be aborted. If the update |
| 420 |
was being done without a transaction, then the application must close its |
| 421 |
environment and database handles, reopen them, and then refresh the database |
| 422 |
from some backup data source. |
| 423 |
</p> |
| 424 |
<p> |
| 425 |
For information on setting the cache size, see |
| 426 |
<a class="xref" href="index.html#cachesize" title="Sizing the Cache">Sizing the Cache</a>. |
| 427 |
</p> |
| 428 |
<p> |
| 429 |
Beyond cache sizing, you also must tell DB not |
| 430 |
to back your database with an on-disk file. You do |
| 431 |
this by NOT providing a database file name |
| 432 |
when you open the database. Note that the database |
| 433 |
file name is different from the database name; you |
| 434 |
can name your in-memory databases even if you |
| 435 |
are not storing them in an on-disk file. |
| 436 |
</p> |
| 437 |
<p> |
| 438 |
For example: |
| 439 |
</p> |
| 440 |
<pre class="programlisting">#include "db.h" |
| 441 |
|
| 442 |
... |
| 443 |
|
| 444 |
int ret, ret_c; |
| 445 |
const char *db_name = "in_mem_db1"; |
| 446 |
u_int32_t db_flags; /* For open flags */ |
| 447 |
DB *dbp; /* Database handle */ |
| 448 |
|
| 449 |
... |
| 450 |
|
| 451 |
/* Initialize the DB handle */ |
| 452 |
ret = db_create(&dbp, NULL, 0); |
| 453 |
if (ret != 0) { |
| 454 |
fprintf(stderr, "Error creating database handle: %s\n", |
| 455 |
db_strerror(ret)); |
| 456 |
goto err; |
| 457 |
} |
| 458 |
|
| 459 |
db_flags = DB_CREATE; /* If it doesn't exist, create it */ |
| 460 |
|
| 461 |
/* |
| 462 |
* Open the database. Note that the file name is NULL. |
| 463 |
* This forces the database to be stored in the cache only. |
| 464 |
* Also note that the database has a name, even though its |
| 465 |
* file name is NULL. |
| 466 |
*/ |
| 467 |
ret = dbp->open(dbp, /* Pointer to the database */ |
| 468 |
NULL, /* Txn pointer */ |
| 469 |
<strong class="userinput"><code>NULL,</code></strong> /* File name is not specified |
| 470 |
* on purpose */ |
| 471 |
db_name, /* Logical db name. */ |
| 472 |
DB_BTREE, /* Database type (using btree) */ |
| 473 |
db_flags, /* Open flags */ |
| 474 |
0); /* File mode. Using defaults */ |
| 475 |
if (ret != 0) { |
| 476 |
dbp->err(dbp, ret, "Database open failed"); |
| 477 |
goto err; |
| 478 |
} |
| 479 |
|
| 480 |
err: |
| 481 |
/* Close the database */ |
| 482 |
if (dbp != NULL) { |
| 483 |
ret_c = dbp->close(dbp, 0); |
| 484 |
if (ret_c != 0) { |
| 485 |
fprintf(stderr, "%s database close failed.\n", |
| 486 |
db_strerror(ret_c)); |
| 487 |
ret = ret_c |
| 488 |
} |
| 489 |
} </pre> |
| 490 |
</div> |
| 491 |
<div class="sect1" lang="en" xml:lang="en"> |
| 492 |
<div class="titlepage"> |
| 493 |
<div> |
| 494 |
<div> |
| 495 |
<h2 class="title" style="clear: both"><a id="env"></a>Keeping Environments in Memory</h2> |
| 496 |
</div> |
| 497 |
</div> |
| 498 |
</div> |
| 499 |
<p> |
| 500 |
Like databases, DB environments are usually backed by the filesystem. In |
| 501 |
fact, a big part of what environments do is identify the location on disk |
| 502 |
where resources (such as log and database files) are kept. |
| 503 |
</p> |
| 504 |
<p> |
| 505 |
However, environments are also used for managing resources, such as |
| 506 |
obtaining new transactions, so they are useful even when building an |
| 507 |
in-memory application. Therefore, if you are going to use an environment for |
| 508 |
your in-memory DB application, you must configure it such that it does |
| 509 |
not want to use the filesystem. There are two things you need to do here. |
| 510 |
</p> |
| 511 |
<p> |
| 512 |
First, when you open your environment, do NOT identify a home directory. To |
| 513 |
accomplish this, you must: |
| 514 |
</p> |
| 515 |
<div class="itemizedlist"> |
| 516 |
<ul type="disc"> |
| 517 |
<li> |
| 518 |
<p> |
| 519 |
NOT provide a value for the <code class="literal">db_home</code> |
| 520 |
parameter on the |
| 521 |
<code class="methodname">DB_ENV->open()</code> |
| 522 |
method. |
| 523 |
</p> |
| 524 |
</li> |
| 525 |
<li> |
| 526 |
<p> |
| 527 |
NOT have a DB_HOME environment variable set. |
| 528 |
</p> |
| 529 |
</li> |
| 530 |
<li> |
| 531 |
<p> |
| 532 |
NOT call any of the methods that affect file naming |
| 533 |
(<code class="methodname">DB_ENV->set_data_dir()</code>, |
| 534 |
<code class="methodname">DB_ENV->set_lg_dir()</code>, or |
| 535 |
<code class="methodname">DB_ENV->set_tmp_dir()</code>). |
| 536 |
</p> |
| 537 |
</li> |
| 538 |
</ul> |
| 539 |
</div> |
| 540 |
<p> |
| 541 |
Beyond this, you must also ensure that regions are backed by heap memory |
| 542 |
instead of by the filesystem or system shared memory. You do this when you |
| 543 |
open your environment by specifying the <code class="literal">DB_PRIVATE</code> flag. |
| 544 |
Note that the use of <code class="literal">DB_PRIVATE</code> means that you can only |
| 545 |
have one open handle for your environment at a time. Consequently, your |
| 546 |
in-memory only application must be single-process, although it can be |
| 547 |
multi-threaded. |
| 548 |
</p> |
| 549 |
<p> |
| 550 |
For example: |
| 551 |
</p> |
| 552 |
<pre class="programlisting">#include "db.h" |
| 553 |
|
| 554 |
... |
| 555 |
|
| 556 |
int ret, ret_c; |
| 557 |
u_int32_t env_flags; /* For open flags */ |
| 558 |
DB_ENV *envp; /* Environment handle */ |
| 559 |
|
| 560 |
... |
| 561 |
|
| 562 |
/* Initialize the ENV handle */ |
| 563 |
ret = db_env_create(&envp, 0); |
| 564 |
if (ret != 0) { |
| 565 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 566 |
db_strerror(ret)); |
| 567 |
goto err; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
/* |
| 572 |
* Environment flags. These are for a non-threaded |
| 573 |
* in-memory application. |
| 574 |
*/ |
| 575 |
env_flags = |
| 576 |
DB_CREATE | /* Create the environment if it does not exist */ |
| 577 |
DB_INIT_LOCK | /* Initialize the locking subsystem */ |
| 578 |
DB_INIT_LOG | /* Initialize the logging subsystem */ |
| 579 |
DB_INIT_TXN | /* Initialize the transactional subsystem. This |
| 580 |
* also turns on logging. */ |
| 581 |
DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */ |
| 582 |
<strong class="userinput"><code>DB_PRIVATE | /* Region files are not backed by the |
| 583 |
* filesystem. Instead, they are backed by |
| 584 |
* heap memory. */</code></strong> |
| 585 |
|
| 586 |
/* |
| 587 |
* Now open the environment. Notice that we do not provide a location |
| 588 |
* for the environment's home directory. This is required for an |
| 589 |
* in-memory only application. |
| 590 |
*/ |
| 591 |
ret = envp->open(envp, <strong class="userinput"><code>NULL</code></strong>, env_flags, 0); |
| 592 |
if (ret != 0) { |
| 593 |
fprintf(stderr, "Error opening environment: %s\n", |
| 594 |
db_strerror(ret)); |
| 595 |
goto err; |
| 596 |
} |
| 597 |
|
| 598 |
err: |
| 599 |
/* Close the environment */ |
| 600 |
if (envp != NULL) { |
| 601 |
ret_c = envp->close(envp, 0); |
| 602 |
if (ret_c != 0) { |
| 603 |
fprintf(stderr, "environment close failed: %s\n", |
| 604 |
db_strerror(ret_c)); |
| 605 |
ret = ret_c |
| 606 |
} |
| 607 |
}</pre> |
| 608 |
</div> |
| 609 |
<div class="sect1" lang="en" xml:lang="en"> |
| 610 |
<div class="titlepage"> |
| 611 |
<div> |
| 612 |
<div> |
| 613 |
<h2 class="title" style="clear: both"><a id="cachesize"></a>Sizing the Cache</h2> |
| 614 |
</div> |
| 615 |
</div> |
| 616 |
</div> |
| 617 |
<div class="toc"> |
| 618 |
<dl> |
| 619 |
<dt> |
| 620 |
<span class="sect2"> |
| 621 |
<a href="index.html#dbcachesize-db">Specifying a Cache Size using the |
| 622 |
Database Handle</a> |
| 623 |
</span> |
| 624 |
</dt> |
| 625 |
<dt> |
| 626 |
<span class="sect2"> |
| 627 |
<a href="index.html#dbcachesize-env">Specifying a Cache Size using the |
| 628 |
Environment Handle</a> |
| 629 |
</span> |
| 630 |
</dt> |
| 631 |
</dl> |
| 632 |
</div> |
| 633 |
<p> |
| 634 |
One of the most important considerations for an |
| 635 |
in-memory application is to ensure that your |
| 636 |
database cache is large enough. In a normal |
| 637 |
application that is not in-memory, the cache |
| 638 |
provides a mechanism by which frequently-used |
| 639 |
data can be accessed without resorting to disk I/O. |
| 640 |
For an in-memory application, the cache is the only |
| 641 |
location your data can exist so it is critical that |
| 642 |
you make the cache large enough for your data set. |
| 643 |
</p> |
| 644 |
<p> |
| 645 |
You specify the size of your cache at application |
| 646 |
startup. Obviously you should not specify a size |
| 647 |
that is larger than available memory. Note that the |
| 648 |
size you specify for your cache is actually a |
| 649 |
<span class="emphasis"><em>maximum</em></span> size; DB will only |
| 650 |
use memory as required so if you specify a cache |
| 651 |
size of 1 GB but your data set is only ever 10 MB |
| 652 |
in size, then 10 MB is what DB will use. |
| 653 |
</p> |
| 654 |
<p> |
| 655 |
Note that if you specify a cache size less than |
| 656 |
500 MB, then the cache size is automatically |
| 657 |
increased by 25% to account for internal overhead |
| 658 |
purposes. |
| 659 |
</p> |
| 660 |
<p> |
| 661 |
There are two ways to specify a cache size, |
| 662 |
depending on whether you are using a database |
| 663 |
environment. |
| 664 |
</p> |
| 665 |
<div class="sect2" lang="en" xml:lang="en"> |
| 666 |
<div class="titlepage"> |
| 667 |
<div> |
| 668 |
<div> |
| 669 |
<h3 class="title"><a id="dbcachesize-db"></a>Specifying a Cache Size using the |
| 670 |
Database Handle</h3> |
| 671 |
</div> |
| 672 |
</div> |
| 673 |
</div> |
| 674 |
<p> |
| 675 |
To select a cache size using the database |
| 676 |
handle, use the |
| 677 |
<code class="methodname">DB->set_cachesize()</code> |
| 678 |
method. Note that you cannot use this |
| 679 |
method after the database has been opened. |
| 680 |
</p> |
| 681 |
<p> |
| 682 |
Also, if you are using a database environment, |
| 683 |
it is an error to use this method. See |
| 684 |
the next section for details on |
| 685 |
selecting your cache size. |
| 686 |
</p> |
| 687 |
<p> |
| 688 |
The following code fragment creates a |
| 689 |
database handle, sets the cache size to |
| 690 |
10 MB and then opens the database: |
| 691 |
</p> |
| 692 |
<pre class="programlisting">#include "db.h" |
| 693 |
|
| 694 |
... |
| 695 |
|
| 696 |
int ret, ret_c; |
| 697 |
const char *db_name = "in_mem_db1"; |
| 698 |
u_int32_t db_flags; /* For open flags */ |
| 699 |
DB *dbp; /* Database handle */ |
| 700 |
|
| 701 |
... |
| 702 |
|
| 703 |
/* Initialize the DB handle */ |
| 704 |
ret = db_create(&dbp, NULL, 0); |
| 705 |
if (ret != 0) { |
| 706 |
fprintf(stderr, "Error creating database handle: %s\n", |
| 707 |
db_strerror(ret)); |
| 708 |
goto err; |
| 709 |
} |
| 710 |
|
| 711 |
<strong class="userinput"><code>/*************************************************************/ |
| 712 |
/*************************************************************/ |
| 713 |
/************* Set the cache size here **********************/ |
| 714 |
/*************************************************************/ |
| 715 |
/*************************************************************/ |
| 716 |
|
| 717 |
ret = dbp->set_cachesize(dbp, |
| 718 |
0, /* 0 gigabytes */ |
| 719 |
10 * 1024 * 1024, /* 10 megabytes */ |
| 720 |
1); /* Create 1 cache. All memory will |
| 721 |
* be allocated contiguously. */ |
| 722 |
if (ret != 0) { |
| 723 |
dbp->err(dbp, ret, "Database open failed"); |
| 724 |
goto err; |
| 725 |
}</code></strong> |
| 726 |
|
| 727 |
|
| 728 |
db_flags = DB_CREATE; /* If it doesn't exist, create it */ |
| 729 |
ret = dbp->open(dbp, /* Pointer to the database */ |
| 730 |
NULL, /* Txn pointer */ |
| 731 |
NULL, /* File name is not specified on |
| 732 |
* purpose */ |
| 733 |
db_name, /* Logical db name */ |
| 734 |
DB_BTREE, /* Database type (using btree) */ |
| 735 |
db_flags, /* Open flags */ |
| 736 |
0); /* File mode. Using defaults */ |
| 737 |
if (ret != 0) { |
| 738 |
dbp->err(dbp, ret, "Database open failed"); |
| 739 |
goto err; |
| 740 |
} |
| 741 |
|
| 742 |
err: |
| 743 |
/* Close the database */ |
| 744 |
if (dbp != NULL) { |
| 745 |
ret_c = dbp->close(dbp, 0); |
| 746 |
if (ret_c != 0) { |
| 747 |
fprintf(stderr, "%s database close failed.\n", |
| 748 |
db_strerror(ret_c)); |
| 749 |
ret = ret_c |
| 750 |
} |
| 751 |
} </pre> |
| 752 |
</div> |
| 753 |
<div class="sect2" lang="en" xml:lang="en"> |
| 754 |
<div class="titlepage"> |
| 755 |
<div> |
| 756 |
<div> |
| 757 |
<h3 class="title"><a id="dbcachesize-env"></a>Specifying a Cache Size using the |
| 758 |
Environment Handle</h3> |
| 759 |
</div> |
| 760 |
</div> |
| 761 |
</div> |
| 762 |
<p> |
| 763 |
To select a cache size using the |
| 764 |
environment handle, use the |
| 765 |
<code class="methodname">ENV->set_cachesize()</code> |
| 766 |
method. Note that you cannot use this |
| 767 |
method after the environment has been opened. |
| 768 |
</p> |
| 769 |
<p> |
| 770 |
The following code fragment creates an |
| 771 |
environment handle, sets the cache size to |
| 772 |
10 MB and then opens the environment: Once |
| 773 |
opened, you can use the environment when |
| 774 |
you open your database(s). This means all |
| 775 |
your databases will use the same |
| 776 |
cache. |
| 777 |
</p> |
| 778 |
<pre class="programlisting">#include "db.h" |
| 779 |
|
| 780 |
... |
| 781 |
|
| 782 |
int ret, ret_c; |
| 783 |
u_int32_t env_flags; /* For open flags */ |
| 784 |
DB_ENV *envp; /* Environment handle */ |
| 785 |
|
| 786 |
... |
| 787 |
|
| 788 |
/* Initialize the ENV handle */ |
| 789 |
ret = db_env_create(&envp, 0); |
| 790 |
if (ret != 0) { |
| 791 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 792 |
db_strerror(ret)); |
| 793 |
goto err; |
| 794 |
} |
| 795 |
|
| 796 |
<strong class="userinput"><code>/*************************************************************/ |
| 797 |
/*************************************************************/ |
| 798 |
/************* Set the cache size here **********************/ |
| 799 |
/*************************************************************/ |
| 800 |
/*************************************************************/ |
| 801 |
|
| 802 |
ret = |
| 803 |
envp->set_cachesize(envp, |
| 804 |
0, /* 0 gigabytes */ |
| 805 |
10 * 1024 * 1024, /* 10 megabytes */ |
| 806 |
1); /* Create 1 cache. All memory will |
| 807 |
* be allocated contiguously. */ |
| 808 |
if (ret != 0) { |
| 809 |
envp->err(envp, ret, "Environment open failed"); |
| 810 |
goto err; |
| 811 |
}</code></strong> |
| 812 |
|
| 813 |
/* |
| 814 |
* Environment flags. These are for a non-threaded |
| 815 |
* in-memory application. |
| 816 |
*/ |
| 817 |
env_flags = |
| 818 |
DB_CREATE | /* Create the environment if it does not exist */ |
| 819 |
DB_INIT_LOCK | /* Initialize the locking subsystem */ |
| 820 |
DB_INIT_LOG | /* Initialize the logging subsystem */ |
| 821 |
DB_INIT_TXN | /* Initialize the transactional subsystem. This |
| 822 |
* also turns on logging. */ |
| 823 |
DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */ |
| 824 |
DB_PRIVATE | /* Region files are not backed by the filesystem. |
| 825 |
* Instead, they are backed by heap memory. */ |
| 826 |
|
| 827 |
/* |
| 828 |
* Now open the environment. Notice that we do not provide a location |
| 829 |
* for the environment's home directory. This is required for an |
| 830 |
* in-memory only application. |
| 831 |
*/ |
| 832 |
ret = envp->open(envp, NULL, env_flags, 0); |
| 833 |
if (ret != 0) { |
| 834 |
fprintf(stderr, "Error opening environment: %s\n", |
| 835 |
db_strerror(ret)); |
| 836 |
goto err; |
| 837 |
} |
| 838 |
|
| 839 |
err: |
| 840 |
/* Close the environment */ |
| 841 |
if (envp != NULL) { |
| 842 |
ret_c = envp->close(envp, 0); |
| 843 |
if (ret_c != 0) { |
| 844 |
fprintf(stderr, "environment close failed: %s\n", |
| 845 |
db_strerror(ret_c)); |
| 846 |
ret = ret_c |
| 847 |
} |
| 848 |
} </pre> |
| 849 |
</div> |
| 850 |
</div> |
| 851 |
<div class="sect1" lang="en" xml:lang="en"> |
| 852 |
<div class="titlepage"> |
| 853 |
<div> |
| 854 |
<div> |
| 855 |
<h2 class="title" style="clear: both"><a id="mpool-nofile"></a>Keeping Temporary Overflow Pages in Memory</h2> |
| 856 |
</div> |
| 857 |
</div> |
| 858 |
</div> |
| 859 |
<p> |
| 860 |
Normally, when a database is opened, a temporary |
| 861 |
file is opened on-disk to back the database. This |
| 862 |
file is used if the database grows so large that it |
| 863 |
fills the entire cache. At that time, database |
| 864 |
pages that do not fit into the in-memory cache file |
| 865 |
are written temporarily to this file. |
| 866 |
</p> |
| 867 |
<p> |
| 868 |
For disk-less systems, you should configure your |
| 869 |
databases so that this temporary file is not |
| 870 |
created. When you do this, any attempt to create |
| 871 |
new database pages once the cache is full will |
| 872 |
fail. |
| 873 |
</p> |
| 874 |
<p> |
| 875 |
You configure this option on a per-database handle |
| 876 |
basis. That means you must configure this for every |
| 877 |
in-memory database that your application uses. |
| 878 |
</p> |
| 879 |
<p> |
| 880 |
To set this option, obtain the |
| 881 |
<code class="literal">DB_MPOOLFILE</code> field from you |
| 882 |
<code class="literal">DB</code> and then configure |
| 883 |
<code class="literal">DB_MPOOL_NOFILE</code> using the |
| 884 |
<code class="methodname">DB_MPOOLFILE->set_flags()</code> |
| 885 |
method. |
| 886 |
</p> |
| 887 |
<p> |
| 888 |
For example: |
| 889 |
</p> |
| 890 |
<pre class="programlisting">#include "db.h" |
| 891 |
|
| 892 |
... |
| 893 |
|
| 894 |
int ret, ret_c; |
| 895 |
u_int32_t env_flags; /* For open flags */ |
| 896 |
DB_ENV *envp; /* Environment handle */ |
| 897 |
|
| 898 |
... |
| 899 |
|
| 900 |
/* |
| 901 |
* Configure the cache file. This can be done |
| 902 |
* at any point in the application's life once the |
| 903 |
* DB handle has been created. |
| 904 |
*/ |
| 905 |
mpf = dbp->get_mpf(dbp); |
| 906 |
ret = mpf->set_flags(mpf, DB_MPOOL_NOFILE, 1); |
| 907 |
|
| 908 |
if (ret != 0) { |
| 909 |
fprintf(stderr, |
| 910 |
"Attempt failed to configure for no backing of temp files: %s\n", |
| 911 |
db_strerror(ret)); |
| 912 |
goto err; |
| 913 |
} </pre> |
| 914 |
</div> |
| 915 |
<div class="sect1" lang="en" xml:lang="en"> |
| 916 |
<div class="titlepage"> |
| 917 |
<div> |
| 918 |
<div> |
| 919 |
<h2 class="title" style="clear: both"><a id="logs"></a>Keeping Logs in Memory</h2> |
| 920 |
</div> |
| 921 |
</div> |
| 922 |
</div> |
| 923 |
<p> |
| 924 |
DB logs describe the write activity that has occurred in |
| 925 |
your application. For a purely in-memory application, logs |
| 926 |
should be used only if you wish to transaction-protect your |
| 927 |
database writes as logs are required by the DB |
| 928 |
transactional subsystem. |
| 929 |
</p> |
| 930 |
<p> |
| 931 |
Note that transactions provide a number of guarantees. One |
| 932 |
of these is not interesting to a purely in-memory |
| 933 |
application (data durability). However, other transaction |
| 934 |
guarantees such as isolation and atomicity might be of |
| 935 |
interest to your application. |
| 936 |
</p> |
| 937 |
<p> |
| 938 |
If this is the case for your application, then you must |
| 939 |
configure your logs to be kept entirely in-memory. You |
| 940 |
do this by setting a configuration option that prevents |
| 941 |
DB from writing log data to disk. |
| 942 |
Do this by setting the <code class="literal">DB_LOG_IN_MEMORY</code> flag using the |
| 943 |
<code class="methodname">DB_ENV->log_set_config()</code> method. |
| 944 |
</p> |
| 945 |
<p> |
| 946 |
In addition, you must configure your log buffer size so that it is capable |
| 947 |
of holding all log information that can accumulate during your longest |
| 948 |
running transaction. That is, make sure the in-memory log buffer is large |
| 949 |
enough that no transaction will ever span the entire buffer. Also, avoid a |
| 950 |
state where the in-memory buffer is full and no space can be freed because a |
| 951 |
transaction that started the first log "file" is still active. |
| 952 |
</p> |
| 953 |
<p> |
| 954 |
How much log buffer space is required is a function of the number of |
| 955 |
transactions you have running concurrently, how long they last, and how much |
| 956 |
write activity occurs within them. When in-memory logging is configured, the |
| 957 |
default log buffer space is 1 MB. |
| 958 |
</p> |
| 959 |
<p> |
| 960 |
You set your log buffer space using the |
| 961 |
<code class="methodname">DB_ENV->set_lg_bsize()</code>. |
| 962 |
</p> |
| 963 |
<p> |
| 964 |
For example, the following code fragment configure in-memory log usage, and |
| 965 |
it configures the log buffer size to 10 MB: |
| 966 |
</p> |
| 967 |
<pre class="programlisting">#include "db.h" |
| 968 |
|
| 969 |
... |
| 970 |
|
| 971 |
int ret, ret_c; |
| 972 |
u_int32_t env_flags; /* For open flags */ |
| 973 |
DB_ENV *envp; /* Environment handle */ |
| 974 |
|
| 975 |
... |
| 976 |
|
| 977 |
/* Initialize the ENV handle */ |
| 978 |
ret = db_env_create(&envp, 0); |
| 979 |
if (ret != 0) { |
| 980 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 981 |
db_strerror(ret)); |
| 982 |
goto err; |
| 983 |
} |
| 984 |
|
| 985 |
/* |
| 986 |
* Environment flags. These are for a non-threaded |
| 987 |
* in-memory application. |
| 988 |
*/ |
| 989 |
env_flags = |
| 990 |
DB_CREATE | /* Create the environment if it does not exist */ |
| 991 |
DB_INIT_LOCK | /* Initialize the locking subsystem */ |
| 992 |
DB_INIT_LOG | /* Initialize the logging subsystem */ |
| 993 |
DB_INIT_TXN | /* Initialize the transactional subsystem. This |
| 994 |
* also turns on logging. */ |
| 995 |
DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */ |
| 996 |
DB_PRIVATE | /* Region files are not backed by the filesystem. |
| 997 |
* Instead, they are backed by heap memory. */ |
| 998 |
|
| 999 |
|
| 1000 |
<strong class="userinput"><code>/* Specify in-memory logging */ |
| 1001 |
ret = envp->log_set_config(envp, DB_LOG_IN_MEMORY, 1); |
| 1002 |
if (ret != 0) { |
| 1003 |
fprintf(stderr, "Error setting log subsystem to in-memory: %s\n", |
| 1004 |
db_strerror(ret)); |
| 1005 |
goto err; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* |
| 1009 |
* Specify the size of the in-memory log buffer. |
| 1010 |
*/ |
| 1011 |
ret = envp->set_lg_bsize(envp, 10 * 1024 * 1024); |
| 1012 |
if (ret != 0) { |
| 1013 |
fprintf(stderr, "Error increasing the log buffer size: %s\n", |
| 1014 |
db_strerror(ret)); |
| 1015 |
goto err; |
| 1016 |
}</code></strong> |
| 1017 |
|
| 1018 |
/* |
| 1019 |
* Now open the environment. Notice that we do not provide a location |
| 1020 |
* for the environment's home directory. This is required for an |
| 1021 |
* in-memory only application. |
| 1022 |
*/ |
| 1023 |
ret = envp->open(envp, NULL, env_flags, 0); |
| 1024 |
if (ret != 0) { |
| 1025 |
fprintf(stderr, "Error opening environment: %s\n", |
| 1026 |
db_strerror(ret)); |
| 1027 |
goto err; |
| 1028 |
} |
| 1029 |
|
| 1030 |
err: |
| 1031 |
/* Close the environment */ |
| 1032 |
if (envp != NULL) { |
| 1033 |
ret_c = envp->close(envp, 0); |
| 1034 |
if (ret_c != 0) { |
| 1035 |
fprintf(stderr, "environment close failed: %s\n", |
| 1036 |
db_strerror(ret_c)); |
| 1037 |
ret = ret_c |
| 1038 |
} |
| 1039 |
} </pre> |
| 1040 |
</div> |
| 1041 |
<div class="sect1" lang="en" xml:lang="en"> |
| 1042 |
<div class="titlepage"> |
| 1043 |
<div> |
| 1044 |
<div> |
| 1045 |
<h2 class="title" style="clear: both"><a id="in-mem-rep"></a>In-Memory Replicated Applications</h2> |
| 1046 |
</div> |
| 1047 |
</div> |
| 1048 |
</div> |
| 1049 |
<p> |
| 1050 |
If you are unfamiliar with writing DB replicated |
| 1051 |
applications, or if you are simply uninterested in this |
| 1052 |
topic, you can skip this section. For an introductory |
| 1053 |
description of DB replication, please see the |
| 1054 |
<em class="citetitle">Berkeley DB Getting Started with Replicated Applications</em> guide. |
| 1055 |
</p> |
| 1056 |
<p> |
| 1057 |
In-memory replicated applications can improve transaction |
| 1058 |
throughput by avoiding disk I/O. Network connections are |
| 1059 |
often faster than local synchronous disk writes, so |
| 1060 |
in-memory replicated applications can provide significantly |
| 1061 |
improved performance without entirely sacrificing |
| 1062 |
reliability. |
| 1063 |
</p> |
| 1064 |
<p> |
| 1065 |
All of your replication participants must be configured in |
| 1066 |
the same way. That is, they all must be configured for |
| 1067 |
in-memory storage of data, or they must all be configured |
| 1068 |
for on-disk storage of data. As a result, in-memory |
| 1069 |
replicated applications can achieve improved throughput |
| 1070 |
performance, but they do so at the cost of reduced |
| 1071 |
durability guarantees. This is because the transaction |
| 1072 |
commit is not backed by stable storage anywhere in the |
| 1073 |
replication group. However, this "commit to the network" |
| 1074 |
does not sacrifice all durability. Because running clients |
| 1075 |
should have in-memory copies of the data, a client can take |
| 1076 |
over in the event of an outage at the master and in this way |
| 1077 |
avoid the loss of data. |
| 1078 |
</p> |
| 1079 |
<p> |
| 1080 |
There are many internal resources used by the DB |
| 1081 |
replication subsystem which are by default backed by disk. |
| 1082 |
These internal resources help the DB subsystem ensure |
| 1083 |
election accuracy. While a complete description of these |
| 1084 |
resources is beyond the scope of this article, you should |
| 1085 |
know that you can cause all of these resources to be held |
| 1086 |
entirely in-memory. But you do so with some small chance |
| 1087 |
of operational errors in your replicated application. |
| 1088 |
</p> |
| 1089 |
<p> |
| 1090 |
If you cause a replicated application to keep its internal |
| 1091 |
replication resources in-memory, you run a small risk that |
| 1092 |
elections will fail or be unable to complete. However, |
| 1093 |
calling additional elections should eventually yield a |
| 1094 |
winner. |
| 1095 |
</p> |
| 1096 |
<p> |
| 1097 |
In addition, there is a slight possibility that the wrong |
| 1098 |
site might win an election, which could result in the loss |
| 1099 |
of data. This can happen if you have a site that is |
| 1100 |
repeatedly crashing and trying to come back up. A site like |
| 1101 |
this might be repeatedly sending out election information, |
| 1102 |
and the repeated messages might confuse other sites. For |
| 1103 |
replication applications that are not in-memory, these |
| 1104 |
extra messages would be ignored by other sites because they |
| 1105 |
would also contain some state information that allows other |
| 1106 |
sites to know which election messages are relevant. But |
| 1107 |
strictly in-memory replicated applications cannot maintain |
| 1108 |
this state information, and so some other sites might |
| 1109 |
become confused. The result might be that the wrong site |
| 1110 |
could be elected master due to the inconsistent information |
| 1111 |
that is available to them. |
| 1112 |
</p> |
| 1113 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 1114 |
<h3 class="title">Note</h3> |
| 1115 |
<p> |
| 1116 |
This is very much a corner case that you probably will |
| 1117 |
never see in your production systems, especially if |
| 1118 |
your sites are all stable and well-behaved. |
| 1119 |
</p> |
| 1120 |
</div> |
| 1121 |
<p> |
| 1122 |
If an election is won by the wrong site (site A), then some |
| 1123 |
other site (site B) probably has more recent log files than |
| 1124 |
the winner does. But since the wrong site A won the |
| 1125 |
election, site B will sync with the new master. This will |
| 1126 |
cause site B (and therefore, your entire replication group) |
| 1127 |
to lose any log files it contains that are more recent than |
| 1128 |
the files contained by site A. |
| 1129 |
</p> |
| 1130 |
<p> |
| 1131 |
If you are running a master that is configured to run with |
| 1132 |
internal replication resources in-memory, you should never |
| 1133 |
allow that site to appoint itself master again immediately |
| 1134 |
after crashing or rebooting. Doing so results in a |
| 1135 |
slightly higher risk of your client sites crashing. To |
| 1136 |
determine your next master, you should either hold an |
| 1137 |
election or appoint a different site to be master. |
| 1138 |
</p> |
| 1139 |
<p> |
| 1140 |
In order to cause a replication site to run entirely |
| 1141 |
in-memory, do all of the things described previously in |
| 1142 |
this document to place all other DB resources |
| 1143 |
in-memory. Then, when configuring replication, specify |
| 1144 |
<code class="literal">DB_REP_CONF_INMEM</code> to the |
| 1145 |
<code class="methodname">DB_ENV->rep_set_config()</code> |
| 1146 |
method. |
| 1147 |
</p> |
| 1148 |
</div> |
| 1149 |
<div class="sect1" lang="en" xml:lang="en"> |
| 1150 |
<div class="titlepage"> |
| 1151 |
<div> |
| 1152 |
<div> |
| 1153 |
<h2 class="title" style="clear: both"><a id="example_in-mem"></a>Example In-Memory Application</h2> |
| 1154 |
</div> |
| 1155 |
</div> |
| 1156 |
</div> |
| 1157 |
<p> |
| 1158 |
The following brief example illustrates how to open |
| 1159 |
an application that is entirely in-memory. The |
| 1160 |
application opens an environment and a single |
| 1161 |
database, and does this in a way that the database |
| 1162 |
is transaction-protected. |
| 1163 |
</p> |
| 1164 |
<p> |
| 1165 |
Transactions can be |
| 1166 |
desirable for an in-memory application even though |
| 1167 |
you discard your durability guarantees, because of |
| 1168 |
the other things that transactions offer such as |
| 1169 |
atomicity and isolation. |
| 1170 |
</p> |
| 1171 |
<p> |
| 1172 |
Notice that the example does nothing other than |
| 1173 |
open and close the environment and database. DB |
| 1174 |
database reads and writes work identically between |
| 1175 |
in-memory-only and durable applications (that is, |
| 1176 |
applications that write database application to |
| 1177 |
durable storage). Consequently, there is no point |
| 1178 |
in illustrating those actions here. |
| 1179 |
</p> |
| 1180 |
<pre class="programlisting">/* We assume an ANSI-compatible compiler */ |
| 1181 |
#include <stdio.h> |
| 1182 |
#include <stdlib.h> |
| 1183 |
#include <string.h> |
| 1184 |
#include <db.h> |
| 1185 |
|
| 1186 |
|
| 1187 |
int |
| 1188 |
main(void) |
| 1189 |
{ |
| 1190 |
/* Initialize our handles */ |
| 1191 |
DB *dbp = NULL; |
| 1192 |
DB_ENV *envp = NULL; |
| 1193 |
DB_MPOOLFILE *mpf = NULL; |
| 1194 |
|
| 1195 |
int ret, ret_t; |
| 1196 |
const char *db_name = "in_mem_db1"; |
| 1197 |
u_int32_t open_flags; |
| 1198 |
|
| 1199 |
/* Create the environment */ |
| 1200 |
ret = db_env_create(&envp, 0); |
| 1201 |
if (ret != 0) { |
| 1202 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 1203 |
db_strerror(ret)); |
| 1204 |
goto err; |
| 1205 |
} |
| 1206 |
|
| 1207 |
open_flags = |
| 1208 |
DB_CREATE | /* Create the environment if it does not exist */ |
| 1209 |
DB_INIT_LOCK | /* Initialize the locking subsystem */ |
| 1210 |
DB_INIT_LOG | /* Initialize the logging subsystem */ |
| 1211 |
DB_INIT_MPOOL | /* Initialize the memory pool (in-memory cache) */ |
| 1212 |
DB_INIT_TXN | |
| 1213 |
DB_PRIVATE; /* Region files are not backed by the filesystem. |
| 1214 |
* Instead, they are backed by heap memory. */ |
| 1215 |
|
| 1216 |
/* Specify in-memory logging */ |
| 1217 |
ret = envp->log_set_config(envp, DB_LOG_IN_MEMORY, 1); |
| 1218 |
if (ret != 0) { |
| 1219 |
fprintf(stderr, "Error setting log subsystem to in-memory: %s\n", |
| 1220 |
db_strerror(ret)); |
| 1221 |
goto err; |
| 1222 |
} |
| 1223 |
/* |
| 1224 |
* Specify the size of the in-memory log buffer. |
| 1225 |
*/ |
| 1226 |
ret = envp->set_lg_bsize(envp, 10 * 1024 * 1024); |
| 1227 |
if (ret != 0) { |
| 1228 |
fprintf(stderr, "Error increasing the log buffer size: %s\n", |
| 1229 |
db_strerror(ret)); |
| 1230 |
goto err; |
| 1231 |
} |
| 1232 |
|
| 1233 |
/* |
| 1234 |
* Specify the size of the in-memory cache. |
| 1235 |
*/ |
| 1236 |
ret = envp->set_cachesize(envp, 0, 10 * 1024 * 1024, 1); |
| 1237 |
if (ret != 0) { |
| 1238 |
fprintf(stderr, "Error increasing the cache size: %s\n", |
| 1239 |
db_strerror(ret)); |
| 1240 |
goto err; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/* |
| 1244 |
* Now actually open the environment. Notice that the environment home |
| 1245 |
* directory is NULL. This is required for an in-memory only |
| 1246 |
* application. |
| 1247 |
*/ |
| 1248 |
ret = envp->open(envp, NULL, open_flags, 0); |
| 1249 |
if (ret != 0) { |
| 1250 |
fprintf(stderr, "Error opening environment: %s\n", |
| 1251 |
db_strerror(ret)); |
| 1252 |
goto err; |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
/* Initialize the DB handle */ |
| 1257 |
ret = db_create(&dbp, envp, 0); |
| 1258 |
if (ret != 0) { |
| 1259 |
envp->err(envp, ret, |
| 1260 |
"Attempt to create db handle failed."); |
| 1261 |
goto err; |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
/* |
| 1266 |
* Set the database open flags. Autocommit is used because we are |
| 1267 |
* transactional. |
| 1268 |
*/ |
| 1269 |
open_flags = DB_CREATE | DB_AUTO_COMMIT; |
| 1270 |
ret = dbp->open(dbp, /* Pointer to the database */ |
| 1271 |
NULL, /* Txn pointer */ |
| 1272 |
NULL, /* File name -- Must be NULL for inmemory! */ |
| 1273 |
db_name, /* Logical db name */ |
| 1274 |
DB_BTREE, /* Database type (using btree) */ |
| 1275 |
open_flags, /* Open flags */ |
| 1276 |
0); /* File mode. Using defaults */ |
| 1277 |
|
| 1278 |
if (ret != 0) { |
| 1279 |
envp->err(envp, ret, |
| 1280 |
"Attempt to open db failed."); |
| 1281 |
goto err; |
| 1282 |
} |
| 1283 |
|
| 1284 |
/* Configure the cache file */ |
| 1285 |
mpf = dbp->get_mpf(dbp); |
| 1286 |
ret = mpf->set_flags(mpf, DB_MPOOL_NOFILE, 1); |
| 1287 |
|
| 1288 |
if (ret != 0) { |
| 1289 |
envp->err(envp, ret, |
| 1290 |
"Attempt failed to configure for no backing of temp files."); |
| 1291 |
goto err; |
| 1292 |
} |
| 1293 |
|
| 1294 |
err: |
| 1295 |
/* Close our database handle, if it was opened. */ |
| 1296 |
if (dbp != NULL) { |
| 1297 |
ret_t = dbp->close(dbp, 0); |
| 1298 |
if (ret_t != 0) { |
| 1299 |
fprintf(stderr, "%s database close failed.\n", |
| 1300 |
db_strerror(ret_t)); |
| 1301 |
ret = ret_t; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* Close our environment, if it was opened. */ |
| 1306 |
if (envp != NULL) { |
| 1307 |
ret_t = envp->close(envp, 0); |
| 1308 |
if (ret_t != 0) { |
| 1309 |
fprintf(stderr, "environment close failed: %s\n", |
| 1310 |
db_strerror(ret_t)); |
| 1311 |
ret = ret_t; |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
/* Final status message and return. */ |
| 1316 |
printf("I'm all done.\n"); |
| 1317 |
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 1318 |
} </pre> |
| 1319 |
</div> |
| 1320 |
</div> |
| 1321 |
<div class="navfooter"> |
| 1322 |
<hr /> |
| 1323 |
</div> |
| 1324 |
</body> |
| 1325 |
</html> |