1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.
*
* $Id$
*/
#include "db_config.h"
#include "db_int.h"
#ifndef lint
static const char copyright[] =
"Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved.\n";
#endif
int main __P((int, char *[]));
#ifndef HAVE_REPLICATION_THREADS
int
main(argc, argv)
int argc;
char *argv[];
{
fprintf(stderr, DB_STR_A("5092",
"Cannot run %s without Replication Manager.\n", "%s\n"),
argv[0]);
COMPQUIET(argc, 0);
exit (1);
}
#else
static int usage __P((void));
static int version_check __P((void));
static void event_callback __P((DB_ENV *, u_int32_t, void *));
static int db_replicate_logmsg __P((DB_ENV *, const char *));
static void prog_close __P((DB_ENV *, int));
/* * Buffer for logging messages. */
#define MSG_SIZE 256
char log_msg[MSG_SIZE];
char *logfile;
FILE *logfp;
pid_t pid;
char progname[MSG_SIZE];
int panic_exit;
#define REP_NTHREADS 3
#define MAX_RETRY 3
int
main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
DB_ENV *dbenv;
time_t now;
long argval;
db_timeout_t max_req;
u_int32_t flags, repmgr_th, seconds, start_state;
int ch, count, done, exitval, ret, verbose;
char *home, *passwd, *prog, time_buf[CTIME_BUFLEN];
dbenv = NULL;
logfp = NULL;
log_msg[MSG_SIZE - 1] = '\0';
__os_id(NULL, &pid, NULL);
panic_exit = 0;
if ((prog = __db_rpath(argv[0])) == NULL)
prog = argv[0];
else
++prog;
if ((size_t)(count = snprintf(progname, sizeof(progname), "%s(%lu)",
prog, (u_long)pid)) >= sizeof(progname)) {
fprintf(stderr, DB_STR("5093", "Program name too long\n"));
goto err;
}
if ((ret = version_check()) != 0)
goto err;
/*
* !!!
* Don't allow a fully unsigned 32-bit number, some compilers get
* upset and require it to be specified in hexadecimal and so on.
*/
#define MAX_UINT32_T 2147483647
/*
* Create an environment object and initialize it for error
* reporting. Create it before parsing args so that we can
* call methods to set the values directly.
*/
if ((ret = db_env_create(&dbenv, 0)) != 0) {
fprintf(stderr,
"%s: db_env_create: %s\n", progname, db_strerror(ret));
goto err;
}
(void)dbenv->set_event_notify(dbenv, event_callback);
dbenv->set_errfile(dbenv, stderr);
dbenv->set_errpfx(dbenv, progname);
exitval = verbose = 0;
flags = 0;
home = logfile = passwd = NULL;
seconds = 30;
start_state = DB_REP_ELECTION;
repmgr_th = REP_NTHREADS;
while ((ch = getopt(argc, argv, "h:L:MP:T:t:Vv")) != EOF)
switch (ch) {
case 'h':
home = optarg;
break;
case 'L':
logfile = optarg;
break;
case 'M':
start_state = DB_REP_MASTER;
break;
case 'P':
passwd = strdup(optarg);
memset(optarg, 0, strlen(optarg));
if (passwd == NULL) {
fprintf(stderr, DB_STR_A("5094",
"%s: strdup: %s\n", "%s %s\n"),
progname, strerror(errno));
return (EXIT_FAILURE);
}
ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES);
free(passwd);
if (ret != 0) {
dbenv->err(dbenv, ret, "set_passwd");
goto err;
}
break;
case 'T':
if (__db_getlong(NULL, progname,
optarg, 1, (long)MAX_UINT32_T, &argval))
return (EXIT_FAILURE);
repmgr_th = (u_int32_t)argval;
break;
case 't':
if (__db_getlong(NULL, progname,
optarg, 1, (long)MAX_UINT32_T, &argval))
return (EXIT_FAILURE);
seconds = (u_int32_t)argval;
break;
case 'V':
printf("%s\n", db_version(NULL, NULL, NULL));
return (EXIT_SUCCESS);
case 'v':
verbose = 1;
break;
case '?':
default:
return (usage());
}
argc -= optind;
argv += optind;
if (argc != 0)
return (usage());
/* Handle possible interruptions. */
__db_util_siginit();
/*
* Log our process ID. This is a specialized case of
* __db_util_logset because we retain the logfp and keep
* the file open for additional logging.
*/
if (logfile != NULL) {
if ((logfp = fopen(logfile, "w")) == NULL)
goto err;
if ((ret = db_replicate_logmsg(dbenv, "STARTED")) != 0)
goto err;
}
/*
* If attaching to a pre-existing environment fails, error.
*/
#define ENV_FLAGS (DB_THREAD | DB_USE_ENVIRON)
if ((ret = dbenv->open(dbenv, home, ENV_FLAGS, 0)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->open");
goto err;
}
/*
* Confirm that replication is configured in the underlying
* environment. We need the max request value anyway and
* the method to get the value returns an error if replication
* is not configured.
*/
if ((ret = dbenv->rep_get_request(dbenv, NULL, &max_req)) != 0) {
dbenv->err(dbenv, ret, "rep_get_request");
goto err;
}
/*
* Start replication.
*/
if (verbose && ((ret = dbenv->set_verbose(dbenv,
DB_VERB_REPLICATION, 1)) != 0)) {
dbenv->err(dbenv, ret, "set_verbose");
goto err;
}
count = done = 0;
while (!done && count < MAX_RETRY) {
/*
* Retry if we get an error that indicates that the port is
* in use. An old version of this program could still be
* running. The application restarts with recovery, and that
* should panic the old environment, but it may take a little
* bit of time for the old program to notice the panic.
*
* We wait the max_req time because at worst the rerequest
* thread runs every max_req time and should notice a panic. On
* the other hand, if we're joining the replication group for
* the first time and the master is not available
* (DB_REP_UNAVAIL), it makes sense to pause a bit longer before
* retrying.
*/
if ((ret = dbenv->repmgr_start(dbenv,
repmgr_th, start_state)) == DB_REP_UNAVAIL) {
count++;
__os_yield(dbenv->env, 5, 0);
} else if (ret != 0) {
count++;
__os_yield(dbenv->env, 0, max_req);
} else
done = 1;
}
if (!done) {
dbenv->err(dbenv, ret, "repmgr_start");
goto err;
}
/* Main loop of the program. */
while (!__db_util_interrupted() && !panic_exit) {
/*
* The program itself does not have much to do. All the
* interesting replication stuff is happening underneath.
* Each period, we'll wake up and call rep_flush just to
* force a log record and cause any gaps to fill as well as
* check program status to see if it was interrupted.
*/
__os_yield(dbenv->env, seconds, 0);
if (verbose) {
(void)time(&now);
dbenv->errx(dbenv, DB_STR_A("5095",
"db_replicate begin: %s", "%s"),
__os_ctime(&now, time_buf));
}
/*
* Hmm, do we really want to exit on error here? This is
* a non-essential piece of the program, so if it gets
* an error, we may just want to ignore it. Note we call
* rep_flush without checking if we're a master or client.
*/
if ((ret = dbenv->rep_flush(dbenv)) != 0) {
dbenv->err(dbenv, ret, "rep_flush");
goto err;
}
}
if (panic_exit)
err: exitval = 1;
prog_close(dbenv, exitval);
return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
static void
prog_close(dbenv, exitval)
DB_ENV *dbenv;
int exitval;
{
int ret;
if (logfp != NULL) {
fclose(logfp);
(void)remove(logfile);
}
/* Clean up the environment. */
if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
exitval = 1;
fprintf(stderr,
"%s: dbenv->close: %s\n", progname, db_strerror(ret));
}
/* Resend any caught signal. */
__db_util_sigresend();
exit (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
static void
event_callback(dbenv, which, info)
DB_ENV *dbenv;
u_int32_t which;
void *info;
{
COMPQUIET(info, NULL);
switch (which) {
case DB_EVENT_PANIC:
/*
* If the app restarted with recovery, and we're an old
* program running against the old regions, we'll discover
* the panic and want to exit quickly to give a new
* instantiation of the program access to the port.
*/
printf(DB_STR("5096", "received panic event\n"));
db_replicate_logmsg(dbenv, "PANIC");
panic_exit = 1;
break;
case DB_EVENT_REP_CLIENT:
db_replicate_logmsg(dbenv, "CLIENT");
break;
case DB_EVENT_REP_CONNECT_BROKEN:
db_replicate_logmsg(dbenv, "CONNECTIONBROKEN");
break;
case DB_EVENT_REP_DUPMASTER:
db_replicate_logmsg(dbenv, "DUPMASTER");
break;
case DB_EVENT_REP_ELECTED:
db_replicate_logmsg(dbenv, "ELECTED");
break;
case DB_EVENT_REP_MASTER:
db_replicate_logmsg(dbenv, "MASTER");
break;
case DB_EVENT_REP_NEWMASTER:
db_replicate_logmsg(dbenv, "NEWMASTER");
break;
case DB_EVENT_REP_STARTUPDONE:
db_replicate_logmsg(dbenv, "STARTUPDONE");
break;
case DB_EVENT_REP_CONNECT_ESTD:
case DB_EVENT_REP_CONNECT_TRY_FAILED:
case DB_EVENT_REP_INIT_DONE:
case DB_EVENT_REP_LOCAL_SITE_REMOVED:
case DB_EVENT_REP_PERM_FAILED:
case DB_EVENT_REP_SITE_ADDED:
case DB_EVENT_REP_SITE_REMOVED:
/* We don't care about these, for now. */
break;
default:
db_replicate_logmsg(dbenv, "IGNORED");
dbenv->errx(dbenv, DB_STR_A("5097", "ignoring event %d",
"%d"), which);
}
}
static int
usage()
{
(void)fprintf(stderr, "usage: %s [-MVv]\n\t%s\n", progname,
"[-h home] [-P password] [-T nthreads] [-t seconds]");
return (EXIT_FAILURE);
}
static int
version_check()
{
int v_major, v_minor, v_patch;
/* Make sure we're loaded with the right version of the DB library. */
(void)db_version(&v_major, &v_minor, &v_patch);
if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
fprintf(stderr, DB_STR_A("5098",
"%s: version %d.%d doesn't match library version %d.%d\n",
"%s %d %d %d %d\n"), progname,
DB_VERSION_MAJOR, DB_VERSION_MINOR,
v_major, v_minor);
return (EXIT_FAILURE);
}
return (0);
}
static int
db_replicate_logmsg(dbenv, msg)
DB_ENV *dbenv;
const char *msg;
{
time_t now;
int cnt;
char time_buf[CTIME_BUFLEN];
if (logfp == NULL)
return (0);
(void)time(&now);
(void)__os_ctime(&now, time_buf);
if ((size_t)(cnt = snprintf(log_msg, sizeof(log_msg), "%s: %lu %s %s",
progname, (u_long)pid, time_buf, msg)) >= sizeof(log_msg)) {
dbenv->errx(dbenv, DB_STR_A("5099",
"%s: %lu %s %s: message too long", "%s %lu %s %s"),
progname, (u_long)pid, time_buf, msg);
return (1);
}
fprintf(logfp, "%s\n", log_msg);
return (0);
}
#endif
|