| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2006, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* $Id$ |
| 7 |
*/ |
| 8 |
|
| 9 |
// NOTE: This example is a simplified version of the RepQuoteExample.cxx |
| 10 |
// example that can be found in the db/examples/cxx/excxx_repquote directory. |
| 11 |
// |
| 12 |
// This example is intended only as an aid in learning Replication Manager |
| 13 |
// concepts. It is not complete in that many features are not exercised |
| 14 |
// in it, nor are many error conditions properly handled. |
| 15 |
|
| 16 |
#include <iostream> |
| 17 |
#include <errno.h> |
| 18 |
|
| 19 |
#include <db_cxx.h> |
| 20 |
#include "RepConfigInfo.h" |
| 21 |
|
| 22 |
using std::cout; |
| 23 |
using std::cin; |
| 24 |
using std::cerr; |
| 25 |
using std::endl; |
| 26 |
using std::flush; |
| 27 |
|
| 28 |
#define CACHESIZE (10 * 1024 * 1024) |
| 29 |
#define DATABASE "quote.db" |
| 30 |
#define SLEEPTIME 3 |
| 31 |
|
| 32 |
const char *progname = "excxx_repquote_gsg_repmgr"; |
| 33 |
|
| 34 |
#ifdef _WIN32 |
| 35 |
#define WIN32_LEAN_AND_MEAN |
| 36 |
#include <windows.h> |
| 37 |
#include <direct.h> |
| 38 |
#define sleep(s) Sleep(1000 * (s)) |
| 39 |
|
| 40 |
extern "C" { |
| 41 |
extern int getopt(int, char * const *, const char *); |
| 42 |
extern char *optarg; |
| 43 |
} |
| 44 |
#endif |
| 45 |
|
| 46 |
// Struct used to store information in Db app_private field. |
| 47 |
typedef struct { |
| 48 |
int is_master; |
| 49 |
} APP_DATA; |
| 50 |
|
| 51 |
class RepMgrGSG |
| 52 |
{ |
| 53 |
public: |
| 54 |
RepMgrGSG(); |
| 55 |
int init(RepConfigInfo* config); |
| 56 |
int doloop(); |
| 57 |
int terminate(); |
| 58 |
|
| 59 |
static void event_callback(DbEnv * dbenv, u_int32_t which, void *info); |
| 60 |
|
| 61 |
private: |
| 62 |
// Disable copy constructor. |
| 63 |
RepMgrGSG(const RepMgrGSG &); |
| 64 |
void operator = (const RepMgrGSG &); |
| 65 |
|
| 66 |
// Internal data members. |
| 67 |
APP_DATA app_data; |
| 68 |
RepConfigInfo *app_config; |
| 69 |
DbEnv dbenv; |
| 70 |
|
| 71 |
// Private methods. |
| 72 |
static int print_stocks(Db *dbp); |
| 73 |
}; |
| 74 |
|
| 75 |
static void usage() |
| 76 |
{ |
| 77 |
cerr << "usage: " << progname << endl |
| 78 |
<< "-h home -l|-L host:port [-r host:port] [-p priority]" << endl; |
| 79 |
|
| 80 |
cerr |
| 81 |
<< "\t -h home directory (required)" << endl |
| 82 |
<< "\t -l host:port (required unless -L is specified;" |
| 83 |
<< "\t l stands for local)" << endl |
| 84 |
<< "\t -L host:port (optional, L means group creator)" << endl |
| 85 |
<< "\t -r host:port (optional; r stands for remote; any " |
| 86 |
<< "number of these" << endl |
| 87 |
<< "\t may be specified)" << endl |
| 88 |
<< "\t -p priority (optional; defaults to 100)" << endl; |
| 89 |
|
| 90 |
exit(EXIT_FAILURE); |
| 91 |
} |
| 92 |
|
| 93 |
int main(int argc, char **argv) |
| 94 |
{ |
| 95 |
RepConfigInfo config; |
| 96 |
char ch, *portstr, *tmphost; |
| 97 |
int tmpport; |
| 98 |
int ret; |
| 99 |
|
| 100 |
// Extract the command line parameters. |
| 101 |
while ((ch = getopt(argc, argv, "h:l:L:p:r:")) != EOF) { |
| 102 |
switch (ch) { |
| 103 |
case 'h': |
| 104 |
config.home = optarg; |
| 105 |
break; |
| 106 |
case 'L': |
| 107 |
config.this_host.creator = true; // FALLTHROUGH |
| 108 |
case 'l': |
| 109 |
config.this_host.host = strtok(optarg, ":"); |
| 110 |
if ((portstr = strtok(NULL, ":")) == NULL) { |
| 111 |
cerr << "Bad host specification." << endl; |
| 112 |
usage(); |
| 113 |
} |
| 114 |
config.this_host.port = (unsigned short)atoi(portstr); |
| 115 |
config.got_listen_address = true; |
| 116 |
break; |
| 117 |
case 'p': |
| 118 |
config.priority = atoi(optarg); |
| 119 |
break; |
| 120 |
case 'r': |
| 121 |
tmphost = strtok(optarg, ":"); |
| 122 |
if ((portstr = strtok(NULL, ":")) == NULL) { |
| 123 |
cerr << "Bad host specification." << endl; |
| 124 |
usage(); |
| 125 |
} |
| 126 |
tmpport = (unsigned short)atoi(portstr); |
| 127 |
config.addOtherHost(tmphost, tmpport); |
| 128 |
break; |
| 129 |
case '?': |
| 130 |
default: |
| 131 |
usage(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// Error check command line. |
| 136 |
if ((!config.got_listen_address) || config.home == NULL) |
| 137 |
usage(); |
| 138 |
|
| 139 |
RepMgrGSG runner; |
| 140 |
try { |
| 141 |
if((ret = runner.init(&config)) != 0) |
| 142 |
goto err; |
| 143 |
if((ret = runner.doloop()) != 0) |
| 144 |
goto err; |
| 145 |
} catch (DbException dbe) { |
| 146 |
cerr << "Caught an exception during initialization or" |
| 147 |
<< " processing: " << dbe.what() << endl; |
| 148 |
} |
| 149 |
err: |
| 150 |
runner.terminate(); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
RepMgrGSG::RepMgrGSG() : app_config(0), dbenv(0) |
| 155 |
{ |
| 156 |
app_data.is_master = 0; // By default, assume this site is not a master. |
| 157 |
} |
| 158 |
|
| 159 |
int RepMgrGSG::init(RepConfigInfo *config) |
| 160 |
{ |
| 161 |
int ret = 0; |
| 162 |
|
| 163 |
app_config = config; |
| 164 |
|
| 165 |
dbenv.set_errfile(stderr); |
| 166 |
dbenv.set_errpfx(progname); |
| 167 |
dbenv.set_app_private(&app_data); |
| 168 |
dbenv.set_event_notify(event_callback); |
| 169 |
dbenv.repmgr_set_ack_policy(DB_REPMGR_ACKS_ALL); |
| 170 |
|
| 171 |
DbSite *dbsite; |
| 172 |
dbenv.repmgr_site(app_config->this_host.host, |
| 173 |
app_config->this_host.port, &dbsite, 0); |
| 174 |
dbsite->set_config(DB_LOCAL_SITE, 1); |
| 175 |
if (app_config->this_host.creator) |
| 176 |
dbsite->set_config(DB_GROUP_CREATOR, 1); |
| 177 |
|
| 178 |
dbsite->close(); |
| 179 |
|
| 180 |
int i = 1; |
| 181 |
for ( REP_HOST_INFO *cur = app_config->other_hosts; |
| 182 |
cur != NULL && i <= app_config->nrsites; |
| 183 |
cur = cur->next, i++) { |
| 184 |
|
| 185 |
dbenv.repmgr_site(cur->host, cur->port, &dbsite, 0); |
| 186 |
dbsite->set_config(DB_BOOTSTRAP_HELPER, 1); |
| 187 |
|
| 188 |
dbsite->close(); |
| 189 |
} |
| 190 |
|
| 191 |
dbenv.rep_set_priority(app_config->priority); |
| 192 |
|
| 193 |
// Permanent messages require at least one ack. |
| 194 |
dbenv.repmgr_set_ack_policy(DB_REPMGR_ACKS_ONE); |
| 195 |
// Give 500 microseconds to receive the ack. |
| 196 |
dbenv.rep_set_timeout(DB_REP_ACK_TIMEOUT, 5); |
| 197 |
|
| 198 |
// We can now open our environment, although we're not ready to |
| 199 |
// begin replicating. However, we want to have a dbenv around |
| 200 |
// so that we can send it into any of our message handlers. |
| 201 |
dbenv.set_cachesize(0, CACHESIZE, 0); |
| 202 |
dbenv.set_flags(DB_TXN_NOSYNC, 1); |
| 203 |
|
| 204 |
try { |
| 205 |
dbenv.open(app_config->home, DB_CREATE | DB_RECOVER | |
| 206 |
DB_THREAD | DB_INIT_REP | DB_INIT_LOCK | DB_INIT_LOG | |
| 207 |
DB_INIT_MPOOL | DB_INIT_TXN, 0); |
| 208 |
} catch(DbException dbe) { |
| 209 |
cerr << "Caught an exception during DB environment open." << endl |
| 210 |
<< "Ensure that the home directory is created prior to starting" |
| 211 |
<< " the application." << endl; |
| 212 |
ret = ENOENT; |
| 213 |
goto err; |
| 214 |
} |
| 215 |
|
| 216 |
if ((ret = dbenv.repmgr_start(3, app_config->start_policy)) != 0) |
| 217 |
goto err; |
| 218 |
|
| 219 |
err: |
| 220 |
return ret; |
| 221 |
} |
| 222 |
|
| 223 |
int RepMgrGSG::terminate() |
| 224 |
{ |
| 225 |
try { |
| 226 |
dbenv.close(0); |
| 227 |
} catch (DbException dbe) { |
| 228 |
cerr << "error closing environment: " << dbe.what() << endl; |
| 229 |
} |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
// Provides the main data processing function for our application. |
| 234 |
// This function provides a command line prompt to which the user |
| 235 |
// can provide a ticker string and a stock price. Once a value is |
| 236 |
// entered to the application, the application writes the value to |
| 237 |
// the database and then displays the entire database. |
| 238 |
#define BUFSIZE 1024 |
| 239 |
int RepMgrGSG::doloop() |
| 240 |
{ |
| 241 |
Dbt key, data; |
| 242 |
Db *dbp; |
| 243 |
char buf[BUFSIZE], *rbuf; |
| 244 |
int ret; |
| 245 |
|
| 246 |
dbp = 0; |
| 247 |
memset(&key, 0, sizeof(key)); |
| 248 |
memset(&data, 0, sizeof(data)); |
| 249 |
ret = 0; |
| 250 |
|
| 251 |
for (;;) { |
| 252 |
if (dbp == 0) { |
| 253 |
dbp = new Db(&dbenv, 0); |
| 254 |
|
| 255 |
try { |
| 256 |
dbp->open(NULL, DATABASE, NULL, DB_BTREE, |
| 257 |
app_data.is_master ? DB_CREATE | DB_AUTO_COMMIT : |
| 258 |
DB_AUTO_COMMIT, 0); |
| 259 |
} catch(DbException dbe) { |
| 260 |
// It is expected that this condition will be triggered |
| 261 |
// when client sites start up. It can take a while for |
| 262 |
// the master site to be found and synced, and no DB will |
| 263 |
// be available until then. |
| 264 |
if (dbe.get_errno() == ENOENT) { |
| 265 |
cout << "No stock db available yet - retrying." << endl; |
| 266 |
try { |
| 267 |
dbp->close(0); |
| 268 |
} catch (DbException dbe2) { |
| 269 |
cout << "Unexpected error closing after failed" << |
| 270 |
" open, message: " << dbe2.what() << endl; |
| 271 |
dbp = NULL; |
| 272 |
goto err; |
| 273 |
} |
| 274 |
dbp = NULL; |
| 275 |
sleep(SLEEPTIME); |
| 276 |
continue; |
| 277 |
} else { |
| 278 |
dbenv.err(ret, "DB->open"); |
| 279 |
throw dbe; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
cout << "QUOTESERVER" ; |
| 285 |
if (!app_data.is_master) |
| 286 |
cout << "(read-only)"; |
| 287 |
cout << "> " << flush; |
| 288 |
|
| 289 |
if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 290 |
break; |
| 291 |
if (strtok(&buf[0], " \t\n") == NULL) { |
| 292 |
switch ((ret = print_stocks(dbp))) { |
| 293 |
case 0: |
| 294 |
continue; |
| 295 |
case DB_REP_HANDLE_DEAD: |
| 296 |
(void)dbp->close(DB_NOSYNC); |
| 297 |
cout << "closing db handle due to rep handle dead" << endl; |
| 298 |
dbp = NULL; |
| 299 |
continue; |
| 300 |
default: |
| 301 |
dbp->err(ret, "Error traversing data"); |
| 302 |
goto err; |
| 303 |
} |
| 304 |
} |
| 305 |
rbuf = strtok(NULL, " \t\n"); |
| 306 |
if (rbuf == NULL || rbuf[0] == '\0') { |
| 307 |
if (strncmp(buf, "exit", 4) == 0 || |
| 308 |
strncmp(buf, "quit", 4) == 0) |
| 309 |
break; |
| 310 |
dbenv.errx("Format: TICKER VALUE"); |
| 311 |
continue; |
| 312 |
} |
| 313 |
|
| 314 |
if (!app_data.is_master) { |
| 315 |
dbenv.errx("Can't update at client"); |
| 316 |
continue; |
| 317 |
} |
| 318 |
|
| 319 |
key.set_data(buf); |
| 320 |
key.set_size((u_int32_t)strlen(buf)); |
| 321 |
|
| 322 |
data.set_data(rbuf); |
| 323 |
data.set_size((u_int32_t)strlen(rbuf)); |
| 324 |
|
| 325 |
if ((ret = dbp->put(NULL, &key, &data, 0)) != 0) |
| 326 |
{ |
| 327 |
dbp->err(ret, "DB->put"); |
| 328 |
if (ret != DB_KEYEXIST) |
| 329 |
goto err; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
err: if (dbp != 0) { |
| 334 |
(void)dbp->close(DB_NOSYNC); |
| 335 |
} |
| 336 |
|
| 337 |
return (ret); |
| 338 |
} |
| 339 |
|
| 340 |
// Handle replication events of interest to this application. |
| 341 |
void RepMgrGSG::event_callback(DbEnv* dbenv, u_int32_t which, void *info) |
| 342 |
{ |
| 343 |
APP_DATA *app = (APP_DATA*)dbenv->get_app_private(); |
| 344 |
|
| 345 |
info = 0; // Currently unused. |
| 346 |
|
| 347 |
switch (which) { |
| 348 |
case DB_EVENT_REP_MASTER: |
| 349 |
app->is_master = 1; |
| 350 |
break; |
| 351 |
|
| 352 |
case DB_EVENT_REP_CLIENT: |
| 353 |
app->is_master = 0; |
| 354 |
break; |
| 355 |
|
| 356 |
case DB_EVENT_REP_STARTUPDONE: // FALLTHROUGH |
| 357 |
case DB_EVENT_REP_NEWMASTER: |
| 358 |
// Ignore. |
| 359 |
break; |
| 360 |
|
| 361 |
default: |
| 362 |
dbenv->errx("ignoring event %d", which); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
// Display all the stock quote information in the database. |
| 367 |
int RepMgrGSG::print_stocks(Db *dbp) |
| 368 |
{ |
| 369 |
Dbc *dbc; |
| 370 |
Dbt key, data; |
| 371 |
#define MAXKEYSIZE 10 |
| 372 |
#define MAXDATASIZE 20 |
| 373 |
char keybuf[MAXKEYSIZE + 1], databuf[MAXDATASIZE + 1]; |
| 374 |
int ret, t_ret; |
| 375 |
u_int32_t keysize, datasize; |
| 376 |
|
| 377 |
if ((ret = dbp->cursor(NULL, &dbc, 0)) != 0) { |
| 378 |
dbp->err(ret, "can't open cursor"); |
| 379 |
return (ret); |
| 380 |
} |
| 381 |
|
| 382 |
memset(&key, 0, sizeof(key)); |
| 383 |
memset(&data, 0, sizeof(data)); |
| 384 |
|
| 385 |
cout << "\tSymbol\tPrice" << endl |
| 386 |
<< "\t======\t=====" << endl; |
| 387 |
|
| 388 |
for (ret = dbc->get(&key, &data, DB_FIRST); |
| 389 |
ret == 0; |
| 390 |
ret = dbc->get(&key, &data, DB_NEXT)) { |
| 391 |
keysize = key.get_size() > MAXKEYSIZE ? MAXKEYSIZE : key.get_size(); |
| 392 |
memcpy(keybuf, key.get_data(), keysize); |
| 393 |
keybuf[keysize] = '\0'; |
| 394 |
|
| 395 |
datasize = data.get_size() >= |
| 396 |
MAXDATASIZE ? MAXDATASIZE : data.get_size(); |
| 397 |
memcpy(databuf, data.get_data(), datasize); |
| 398 |
databuf[datasize] = '\0'; |
| 399 |
|
| 400 |
cout << "\t" << keybuf << "\t" << databuf << endl; |
| 401 |
} |
| 402 |
cout << endl << flush; |
| 403 |
|
| 404 |
if ((t_ret = dbc->close()) != 0 && ret == 0) { |
| 405 |
cout << "closed cursor" << endl; |
| 406 |
ret = t_ret; |
| 407 |
} |
| 408 |
|
| 409 |
switch (ret) { |
| 410 |
case 0: |
| 411 |
case DB_NOTFOUND: |
| 412 |
case DB_LOCK_DEADLOCK: |
| 413 |
return (0); |
| 414 |
default: |
| 415 |
return (ret); |
| 416 |
} |
| 417 |
} |
| 418 |
|