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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2001-2002
* Sleepycat Software. All rights reserved.
*
* $Id: ex_rq_main.c,v 1.1.1.1 2003/11/20 22:13:29 toshok Exp $
*/
#include <sys/types.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <db.h>
#include "ex_repquote.h"
/*
* Process globals (we could put these in the machtab I suppose.
*/
int master_eid;
char *myaddr;
static int env_init __P((const char *, const char *, DB_ENV **, machtab_t *,
u_int32_t));
static void usage __P((const char *));
int
main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
DB_ENV *dbenv;
DBT local;
enum { MASTER, CLIENT, UNKNOWN } whoami;
all_args aa;
connect_args ca;
machtab_t *machtab;
pthread_t all_thr, conn_thr;
repsite_t site, *sitep, self, *selfp;
struct sigaction sigact;
int maxsites, nsites, ret, priority, totalsites;
char *c, ch;
const char *home, *progname;
void *astatus, *cstatus;
master_eid = DB_EID_INVALID;
dbenv = NULL;
whoami = UNKNOWN;
machtab = NULL;
selfp = sitep = NULL;
maxsites = nsites = ret = totalsites = 0;
priority = 100;
home = "TESTDIR";
progname = "ex_repquote";
while ((ch = getopt(argc, argv, "Ch:Mm:n:o:p:")) != EOF)
switch (ch) {
case 'M':
whoami = MASTER;
master_eid = SELF_EID;
break;
case 'C':
whoami = CLIENT;
break;
case 'h':
home = optarg;
break;
case 'm':
if ((myaddr = strdup(optarg)) == NULL) {
fprintf(stderr,
"System error %s\n", strerror(errno));
goto err;
}
self.host = optarg;
self.host = strtok(self.host, ":");
if ((c = strtok(NULL, ":")) == NULL) {
fprintf(stderr, "Bad host specification.\n");
goto err;
}
self.port = atoi(c);
selfp = &self;
break;
case 'n':
totalsites = atoi(optarg);
break;
case 'o':
site.host = optarg;
site.host = strtok(site.host, ":");
if ((c = strtok(NULL, ":")) == NULL) {
fprintf(stderr, "Bad host specification.\n");
goto err;
}
site.port = atoi(c);
if (sitep == NULL || nsites >= maxsites) {
maxsites = maxsites == 0 ? 10 : 2 * maxsites;
if ((sitep = realloc(sitep,
maxsites * sizeof(repsite_t))) == NULL) {
fprintf(stderr, "System error %s\n",
strerror(errno));
goto err;
}
}
sitep[nsites++] = site;
break;
case 'p':
priority = atoi(optarg);
break;
case '?':
default:
usage(progname);
}
/* Error check command line. */
if (whoami == UNKNOWN) {
fprintf(stderr, "Must specify -M or -C.\n");
goto err;
}
if (selfp == NULL)
usage(progname);
if (home == NULL)
usage(progname);
/*
* Turn off SIGPIPE so that we don't kill processes when they
* happen to lose a connection at the wrong time.
*/
memset(&sigact, 0, sizeof(sigact));
sigact.sa_handler = SIG_IGN;
if ((ret = sigaction(SIGPIPE, &sigact, NULL)) != 0) {
fprintf(stderr,
"Unable to turn off SIGPIPE: %s\n", strerror(ret));
goto err;
}
/*
* We are hardcoding priorities here that all clients have the
* same priority except for a designated master who gets a higher
* priority.
*/
if ((ret =
machtab_init(&machtab, priority, totalsites)) != 0)
goto err;
/*
* We can know open our environment, although we're not ready to
* begin replicating. However, we want to have a dbenv around
* so that we can send it into any of our message handlers.
*/
if ((ret = env_init(progname, home, &dbenv, machtab, DB_RECOVER)) != 0)
goto err;
/*
* Now sets up comm infrastructure. There are two phases. First,
* we open our port for listening for incoming connections. Then
* we attempt to connect to every host we know about.
*/
ca.dbenv = dbenv;
ca.home = home;
ca.progname = progname;
ca.machtab = machtab;
ca.port = selfp->port;
if ((ret = pthread_create(&conn_thr, NULL, connect_thread, &ca)) != 0)
goto err;
aa.dbenv = dbenv;
aa.progname = progname;
aa.home = home;
aa.machtab = machtab;
aa.sites = sitep;
aa.nsites = nsites;
if ((ret = pthread_create(&all_thr, NULL, connect_all, &aa)) != 0)
goto err;
/*
* We have now got the entire communication infrastructure set up.
* It's time to declare ourselves to be a client or master.
*/
if (whoami == MASTER) {
if ((ret = dbenv->rep_start(dbenv, NULL, DB_REP_MASTER)) != 0) {
dbenv->err(dbenv, ret, "dbenv->rep_start failed");
goto err;
}
if ((ret = domaster(dbenv, progname)) != 0) {
dbenv->err(dbenv, ret, "Master failed");
goto err;
}
} else {
memset(&local, 0, sizeof(local));
local.data = myaddr;
local.size = strlen(myaddr) + 1;
if ((ret =
dbenv->rep_start(dbenv, &local, DB_REP_CLIENT)) != 0) {
dbenv->err(dbenv, ret, "dbenv->rep_start failed");
goto err;
}
/* Sleep to give ourselves a minute to find a master. */
sleep(5);
if ((ret = doclient(dbenv, progname, machtab)) != 0) {
dbenv->err(dbenv, ret, "Client failed");
goto err;
}
}
/* Wait on the connection threads. */
if (pthread_join(all_thr, &astatus) || pthread_join(conn_thr, &cstatus))
ret = errno;
if (ret == 0 &&
((int)astatus != EXIT_SUCCESS || (int)cstatus != EXIT_SUCCESS))
ret = -1;
err: if (machtab != NULL)
free(machtab);
if (dbenv != NULL)
(void)dbenv->close(dbenv, 0);
return (ret);
}
/*
* In this application, we specify all communication via the command line.
* In a real application, we would expect that information about the other
* sites in the system would be maintained in some sort of configuration
* file. The critical part of this interface is that we assume at startup
* that we can find out 1) what host/port we wish to listen on for connections,
* 2) a (possibly empty) list of other sites we should attempt to connect to.
* 3) whether we are a master or client (if we don't know, we should come up
* as a client and see if there is a master out there) and 4) what our
* Berkeley DB home environment is.
*
* These pieces of information are expressed by the following flags.
* -m host:port (required; m stands for me)
* -o host:port (optional; o stands for other; any number of these may be
* specified)
* -[MC] M for master/C for client
* -h home directory
* -n nsites (optional; number of sites in replication group; defaults to 0
* in which case we try to dynamically compute the number of sites in
* the replication group.)
* -p priority (optional: defaults to 100)
*/
static void
usage(progname)
const char *progname;
{
fprintf(stderr, "usage: %s ", progname);
fprintf(stderr, "[-CM][-h home][-o host:port][-m host:port]%s",
"[-n nsites][-p priority]\n");
exit(EXIT_FAILURE);
}
/* Open and configure an environment. */
int
env_init(progname, home, dbenvp, machtab, flags)
const char *progname, *home;
DB_ENV **dbenvp;
machtab_t *machtab;
u_int32_t flags;
{
DB_ENV *dbenv;
int ret;
char *prefix;
if ((prefix = malloc(strlen(progname) + 2)) == NULL) {
fprintf(stderr,
"%s: System error: %s\n", progname, strerror(errno));
return (errno);
}
sprintf(prefix, "%s:", progname);
if ((ret = db_env_create(&dbenv, 0)) != 0) {
fprintf(stderr, "%s: env create failed: %s\n",
progname, db_strerror(ret));
return (ret);
}
dbenv->set_errfile(dbenv, stderr);
dbenv->set_errpfx(dbenv, prefix);
/* (void)dbenv->set_verbose(dbenv, DB_VERB_REPLICATION, 1); */
(void)dbenv->set_cachesize(dbenv, 0, CACHESIZE, 0);
/* (void)dbenv->set_flags(dbenv, DB_TXN_NOSYNC, 1); */
dbenv->app_private = machtab;
(void)dbenv->set_rep_transport(dbenv, SELF_EID, quote_send);
flags |= DB_CREATE | DB_THREAD |
DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN;
ret = dbenv->open(dbenv, home, flags, 0);
*dbenvp = dbenv;
return (ret);
}
|