📄 dbreg.c
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 1996-2004 * Sleepycat Software. All rights reserved. * * $Id: dbreg.c,v 11.90 2004/10/15 16:59:39 bostic Exp $ */#include "db_config.h"#ifndef NO_SYSTEM_INCLUDES#include <sys/types.h>#include <string.h>#endif#include "db_int.h"#include "dbinc/db_page.h"#include "dbinc/log.h"#include "dbinc/txn.h"#include "dbinc/db_am.h"static int __dbreg_push_id __P((DB_ENV *, int32_t));static int __dbreg_pop_id __P((DB_ENV *, int32_t *));static int __dbreg_pluck_id __P((DB_ENV *, int32_t));/* * The dbreg subsystem, as its name implies, registers database handles so * that we can associate log messages with them without logging a filename * or a full, unique DB ID. Instead, we assign each dbp an int32_t which is * easy and cheap to log, and use this subsystem to map back and forth. * * Overview of how dbreg ids are managed: * * OPEN * dbreg_setup (Creates FNAME struct.) * dbreg_new_id (Assigns new ID to dbp and logs it. May be postponed * until we attempt to log something else using that dbp, if the dbp * was opened on a replication client.) * * CLOSE * dbreg_close_id (Logs closure of dbp/revocation of ID.) * dbreg_revoke_id (As name implies, revokes ID.) * dbreg_teardown (Destroys FNAME.) * * RECOVERY * dbreg_setup * dbreg_assign_id (Assigns a particular ID we have in the log to a dbp.) * * sometimes: dbreg_revoke_id; dbreg_teardown * other times: normal close path * * A note about locking: * * FNAME structures are referenced only by their corresponding dbp's * until they have a valid id. * * Once they have a valid id, they must get linked into the log * region list so they can get logged on checkpoints. * * An FNAME that may/does have a valid id must be accessed under * protection of the fq_mutex, with the following exception: * * We don't want to have to grab the fq_mutex on every log * record, and it should be safe not to do so when we're just * looking at the id, because once allocated, the id should * not change under a handle until the handle is closed. * * If a handle is closed during an attempt by another thread to * log with it, well, the application doing the close deserves to * go down in flames and a lot else is about to fail anyway. * * When in the course of logging we encounter an invalid id * and go to allocate it lazily, we *do* need to check again * after grabbing the mutex, because it's possible to race with * another thread that has also decided that it needs to allocate * a id lazily. * * See SR #5623 for further discussion of the new dbreg design. *//* * __dbreg_setup -- * Allocate and initialize an FNAME structure. The FNAME structures * live in the log shared region and map one-to-one with open database handles. * When the handle needs to be logged, the FNAME should have a valid fid * allocated. If the handle currently isn't logged, it still has an FNAME * entry. If we later discover that the handle needs to be logged, we can * allocate a id for it later. (This happens when the handle is on a * replication client that later becomes a master.) * * PUBLIC: int __dbreg_setup __P((DB *, const char *, u_int32_t)); */int__dbreg_setup(dbp, name, create_txnid) DB *dbp; const char *name; u_int32_t create_txnid;{ DB_ENV *dbenv; DB_LOG *dblp; FNAME *fnp; REGINFO *infop; int ret; size_t len; void *namep; dbenv = dbp->dbenv; dblp = dbenv->lg_handle; infop = &dblp->reginfo; fnp = NULL; namep = NULL; /* Allocate an FNAME and, if necessary, a buffer for the name itself. */ R_LOCK(dbenv, infop); if ((ret = __db_shalloc(infop, sizeof(FNAME), 0, &fnp)) != 0) goto err; memset(fnp, 0, sizeof(FNAME)); if (name != NULL) { len = strlen(name) + 1; if ((ret = __db_shalloc(infop, len, 0, &namep)) != 0) goto err; fnp->name_off = R_OFFSET(infop, namep); memcpy(namep, name, len); } else fnp->name_off = INVALID_ROFF; R_UNLOCK(dbenv, infop); /* * Fill in all the remaining info that we'll need later to register * the file, if we use it for logging. */ fnp->id = DB_LOGFILEID_INVALID; fnp->s_type = dbp->type; memcpy(fnp->ufid, dbp->fileid, DB_FILE_ID_LEN); fnp->meta_pgno = dbp->meta_pgno; fnp->create_txnid = create_txnid; dbp->log_filename = fnp; return (0);err: R_UNLOCK(dbenv, infop); if (ret == ENOMEM) __db_err(dbenv, "Logging region out of memory; you may need to increase its size"); return (ret);}/* * __dbreg_teardown -- * Destroy a DB handle's FNAME struct. * * PUBLIC: int __dbreg_teardown __P((DB *)); */int__dbreg_teardown(dbp) DB *dbp;{ DB_ENV *dbenv; DB_LOG *dblp; REGINFO *infop; FNAME *fnp; dbenv = dbp->dbenv; dblp = dbenv->lg_handle; infop = &dblp->reginfo; fnp = dbp->log_filename; /* * We may not have an FNAME if we were never opened. This is not an * error. */ if (fnp == NULL) return (0); DB_ASSERT(fnp->id == DB_LOGFILEID_INVALID); R_LOCK(dbenv, infop); if (fnp->name_off != INVALID_ROFF) __db_shalloc_free(infop, R_ADDR(infop, fnp->name_off)); __db_shalloc_free(infop, fnp); R_UNLOCK(dbenv, infop); dbp->log_filename = NULL; return (0);}/* * __dbreg_new_id -- * Get an unused dbreg id to this database handle. * Used as a wrapper to acquire the mutex and * only set the id on success. * * PUBLIC: int __dbreg_new_id __P((DB *, DB_TXN *)); */int__dbreg_new_id(dbp, txn) DB *dbp; DB_TXN *txn;{ DB_ENV *dbenv; DB_LOG *dblp; FNAME *fnp; LOG *lp; int32_t id; int ret; dbenv = dbp->dbenv; dblp = dbenv->lg_handle; lp = dblp->reginfo.primary; fnp = dbp->log_filename; /* The fq_mutex protects the FNAME list and id management. */ MUTEX_LOCK(dbenv, &lp->fq_mutex); if (fnp->id != DB_LOGFILEID_INVALID) { MUTEX_UNLOCK(dbenv, &lp->fq_mutex); return (0); } if ((ret = __dbreg_get_id(dbp, txn, &id)) == 0) fnp->id = id; MUTEX_UNLOCK(dbenv, &lp->fq_mutex); return (ret);}/* * __dbreg_get_id -- * Assign an unused dbreg id to this database handle. * Assume the caller holds the fq_mutex locked. Assume the * caller will set the fnp->id field with the id we return. * * PUBLIC: int __dbreg_get_id __P((DB *, DB_TXN *, int32_t *)); */int__dbreg_get_id(dbp, txn, idp) DB *dbp; DB_TXN *txn; int32_t *idp;{ DBT fid_dbt, r_name; DB_ENV *dbenv; DB_LOG *dblp; DB_LSN unused; FNAME *fnp; LOG *lp; int32_t id; int ret; dbenv = dbp->dbenv; dblp = dbenv->lg_handle; lp = dblp->reginfo.primary; fnp = dbp->log_filename; /* * It's possible that after deciding we needed to call this function, * someone else allocated an ID before we grabbed the lock. Check * to make sure there was no race and we have something useful to do. */ /* Get an unused ID from the free list. */ if ((ret = __dbreg_pop_id(dbenv, &id)) != 0) goto err; /* If no ID was found, allocate a new one. */ if (id == DB_LOGFILEID_INVALID) id = lp->fid_max++; fnp->is_durable = !F_ISSET(dbp, DB_AM_NOT_DURABLE); /* Hook the FNAME into the list of open files. */ SH_TAILQ_INSERT_HEAD(&lp->fq, fnp, q, __fname); /* * Log the registry. We should only request a new ID in situations * where logging is reasonable. */ DB_ASSERT(!F_ISSET(dbp, DB_AM_RECOVER)); memset(&fid_dbt, 0, sizeof(fid_dbt)); memset(&r_name, 0, sizeof(r_name)); if (fnp->name_off != INVALID_ROFF) { r_name.data = R_ADDR(&dblp->reginfo, fnp->name_off); r_name.size = (u_int32_t)strlen((char *)r_name.data) + 1; } fid_dbt.data = dbp->fileid; fid_dbt.size = DB_FILE_ID_LEN; if ((ret = __dbreg_register_log(dbenv, txn, &unused, F_ISSET(dbp, DB_AM_NOT_DURABLE) ? DB_LOG_NOT_DURABLE : 0, DBREG_OPEN, r_name.size == 0 ? NULL : &r_name, &fid_dbt, id, fnp->s_type, fnp->meta_pgno, fnp->create_txnid)) != 0) goto err; /* * Once we log the create_txnid, we need to make sure we never * log it again (as might happen if this is a replication client * that later upgrades to a master). */ fnp->create_txnid = TXN_INVALID; DB_ASSERT(dbp->type == fnp->s_type); DB_ASSERT(dbp->meta_pgno == fnp->meta_pgno); if ((ret = __dbreg_add_dbentry(dbenv, dblp, dbp, id)) != 0) goto err; /* * If we have a successful call, set the ID. Otherwise * we have to revoke it and remove it from all the lists * it has been added to, and return an invalid id. */err: if (ret != 0 && id != DB_LOGFILEID_INVALID) { (void)__dbreg_revoke_id(dbp, 1, id); id = DB_LOGFILEID_INVALID; } *idp = id; return (ret);}/* * __dbreg_assign_id -- * Assign a particular dbreg id to this database handle.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -