2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 2001-2002
5
* Sleepycat Software. All rights reserved.
10
static const char revid[] = "$Id$";
13
#ifndef NO_SYSTEM_INCLUDES
19
#include "dbinc/rep.h"
20
#include "dbinc/log.h"
23
* __rep_region_init --
24
* Initialize the shared memory state for the replication system.
26
* PUBLIC: int __rep_region_init __P((DB_ENV *));
29
__rep_region_init(dbenv)
39
db_rep = dbenv->rep_handle;
40
infop = dbenv->reginfo;
41
renv = infop->primary;
44
MUTEX_LOCK(dbenv, &renv->mutex);
45
if (renv->rep_off == INVALID_ROFF) {
46
/* Must create the region. */
47
if ((ret = __db_shalloc(infop->addr,
48
sizeof(REP), MUTEX_ALIGN, &rep)) != 0)
50
memset(rep, 0, sizeof(*rep));
51
rep->tally_off = INVALID_ROFF;
52
renv->rep_off = R_OFFSET(infop, rep);
54
if ((ret = __db_mutex_setup(dbenv, infop, &rep->mutex,
55
MUTEX_NO_RECORD)) != 0)
59
* We must create a place for the db_mutex separately;
60
* mutexes have to be aligned to MUTEX_ALIGN, and the only way
61
* to guarantee that is to make sure they're at the beginning
62
* of a shalloc'ed chunk.
64
if ((ret = __db_shalloc(infop->addr, sizeof(DB_MUTEX),
65
MUTEX_ALIGN, &db_mutexp)) != 0)
67
rep->db_mutex_off = R_OFFSET(infop, db_mutexp);
70
* Because we have no way to prevent deadlocks and cannot log
71
* changes made to it, we single-thread access to the client
72
* bookkeeping database. This is suboptimal, but it only gets
73
* accessed when messages arrive out-of-order, so it should
74
* stay small and not be used in a high-performance app.
76
if ((ret = __db_mutex_setup(dbenv, infop, db_mutexp,
77
MUTEX_NO_RECORD)) != 0)
80
/* We have the region; fill in the values. */
81
rep->eid = DB_EID_INVALID;
82
rep->master_id = DB_EID_INVALID;
86
* Set default values for the min and max log records that we
87
* wait before requesting a missing log record.
89
rep->request_gap = DB_REP_REQUEST_GAP;
90
rep->max_gap = DB_REP_MAX_GAP;
92
rep = R_ADDR(infop, renv->rep_off);
93
MUTEX_UNLOCK(dbenv, &renv->mutex);
95
db_rep->mutexp = &rep->mutex;
96
db_rep->db_mutexp = R_ADDR(infop, rep->db_mutex_off);
101
err: MUTEX_UNLOCK(dbenv, &renv->mutex);
106
* __rep_region_destroy --
107
* Destroy any system resources allocated in the replication region.
109
* PUBLIC: int __rep_region_destroy __P((DB_ENV *));
112
__rep_region_destroy(dbenv)
119
db_rep = (DB_REP *)dbenv->rep_handle;
121
if (db_rep != NULL) {
122
if (db_rep->mutexp != NULL)
123
ret = __db_mutex_destroy(db_rep->mutexp);
124
if (db_rep->db_mutexp != NULL)
125
t_ret = __db_mutex_destroy(db_rep->db_mutexp);
128
return (ret == 0 ? t_ret : ret);
132
* __rep_dbenv_close --
133
* Replication-specific destruction of the DB_ENV structure.
135
* PUBLIC: int __rep_dbenv_close __P((DB_ENV *));
138
__rep_dbenv_close(dbenv)
143
db_rep = (DB_REP *)dbenv->rep_handle;
145
if (db_rep != NULL) {
146
__os_free(dbenv, db_rep);
147
dbenv->rep_handle = NULL;
155
* If we are a client, shut down our client database and, if we're
156
* actually closing the environment, close all databases we've opened
157
* while applying messages.
159
* PUBLIC: int __rep_preclose __P((DB_ENV *, int));
162
__rep_preclose(dbenv, do_closefiles)
172
/* If replication is not initialized, we have nothing to do. */
173
if ((db_rep = (DB_REP *)dbenv->rep_handle) == NULL)
176
if ((dbp = db_rep->rep_db) != NULL) {
177
MUTEX_LOCK(dbenv, db_rep->db_mutexp);
178
ret = dbp->close(dbp, 0);
179
db_rep->rep_db = NULL;
180
MUTEX_UNLOCK(dbenv, db_rep->db_mutexp);
184
t_ret = __dbreg_close_files(dbenv);
186
return (ret == 0 ? t_ret : ret);