#include "portable.h"
#include <stdio.h>
+#include <ac/ctype.h>
#include <ac/string.h>
#include "back-bdb.h"
# define SLAP_BDB_ALLOW_DIRTY_READ
#endif
-static ObjectClass *bdb_oc;
+#define bdb_cf_gen BDB_SYMBOL(cf_gen)
+#define bdb_cf_cleanup BDB_SYMBOL(cf_cleanup)
+#define bdb_checkpoint BDB_SYMBOL(checkpoint)
+#define bdb_online_index BDB_SYMBOL(online_index)
-static ConfigDriver bdb_cf_oc, bdb_cf_gen;
+static ConfigDriver bdb_cf_gen;
enum {
BDB_CHKPT = 1,
};
static ConfigTable bdbcfg[] = {
- { "", "", 0, 0, 0, ARG_MAGIC,
- bdb_cf_oc, NULL, NULL, NULL },
{ "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
"DESC 'Directory for database content' "
bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
"DESC 'Database checkpoint interval in kbytes and minutes' "
"SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
- { "dbconfig", "DB_CONFIG setting", 3, 0, 0, ARG_MAGIC|BDB_CONFIG,
+ { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
"DESC 'BerkeleyDB DB_CONFIG configuration directives' "
- "SYNTAX OMsDirectoryString )",NULL, NULL },
+ "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
{ "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
"DESC 'Disable synchronous database writes' "
};
static ConfigOCs bdbocs[] = {
- { "( OLcfgDbOc:1.1 "
+ {
+#ifdef BDB_HIER
+ "( OLcfgDbOc:1.2 "
+ "NAME 'olcHdbConfig' "
+ "DESC 'HDB backend configuration' "
+#else
+ "( OLcfgDbOc:1.1 "
"NAME 'olcBdbConfig' "
"DESC 'BDB backend configuration' "
+#endif
"SUP olcDatabaseConfig "
"MUST olcDbDirectory "
"MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
"olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
"olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
"olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
- Cft_Database, &bdb_oc },
+ Cft_Database, bdbcfg },
{ NULL, 0, NULL }
};
-static int
-bdb_cf_oc(ConfigArgs *c)
-{
- if ( c->op == SLAP_CONFIG_EMIT ) {
- value_add_one( &c->rvalue_vals, &bdb_oc->soc_cname );
- return 0;
- }
- return 1;
-}
-
static slap_verbmasks bdb_lockd[] = {
{ BER_BVC("default"), DB_LOCK_DEFAULT },
{ BER_BVC("oldest"), DB_LOCK_OLDEST },
{ BER_BVNULL, 0 }
};
+/* perform periodic checkpoints */
+static void *
+bdb_checkpoint( void *ctx, void *arg )
+{
+ struct re_s *rtask = arg;
+ struct bdb_info *bdb = rtask->arg;
+
+ TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
+ bdb->bi_txn_cp_min, 0 );
+ ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
+ ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
+ ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
+ return NULL;
+}
+
/* reindex entries on the fly */
static void *
bdb_online_index( void *ctx, void *arg )
id++;
getnext = 1;
}
-out:
+
ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
+ bdb->bi_index_task = NULL;
ldap_pvt_runqueue_remove( &slapd_rq, rtask );
ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
bdb_cf_cleanup( ConfigArgs *c )
{
struct bdb_info *bdb = c->be->be_private;
+ int rc = 0;
if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
if ( bdb->bi_db_config ) {
bdb_attr_flush( bdb );
bdb->bi_flags ^= BDB_DEL_INDEX;
}
-
- return 0;
+
+ if ( bdb->bi_flags & BDB_RE_OPEN ) {
+ bdb->bi_flags ^= BDB_RE_OPEN;
+ rc = c->be->bd_info->bi_db_close( c->be );
+ if ( rc == 0 )
+ rc = c->be->bd_info->bi_db_open( c->be );
+ /* If this fails, we need to restart */
+ if ( rc ) {
+ slapd_shutdown = 2;
+ Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
+ ": failed to reopen database, rc=%d", rc, 0, 0 );
+ }
+ }
+ return rc;
}
static int
break;
case BDB_CONFIG:
+ if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
+ && !bdb->bi_db_config ) {
+ char buf[SLAP_TEXT_BUFLEN];
+ FILE *f = fopen( bdb->bi_db_config_path, "r" );
+ struct berval bv;
+
+ if ( f ) {
+ bdb->bi_flags |= BDB_HAS_CONFIG;
+ while ( fgets( buf, sizeof(buf), f )) {
+ ber_str2bv( buf, 0, 1, &bv );
+ if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
+ bv.bv_len--;
+ bv.bv_val[bv.bv_len] = '\0';
+ }
+ /* shouldn't need this, but ... */
+ if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
+ bv.bv_len--;
+ bv.bv_val[bv.bv_len] = '\0';
+ }
+ ber_bvarray_add( &bdb->bi_db_config, &bv );
+ }
+ fclose( f );
+ }
+ }
if ( bdb->bi_db_config ) {
int i;
struct berval bv;
break;
case BDB_CHKPT:
- /* FIXME: should stop the checkpoint task too */
+ if ( bdb->bi_txn_cp_task ) {
+ struct re_s *re = bdb->bi_txn_cp_task;
+ bdb->bi_txn_cp_task = NULL;
+ if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
+ ldap_pvt_runqueue_stoptask( &slapd_rq, re );
+ ldap_pvt_runqueue_remove( &slapd_rq, re );
+ }
bdb->bi_txn_cp = 0;
break;
case BDB_CONFIG:
c->cleanup = bdb_cf_cleanup;
break;
case BDB_DIRECTORY:
- rc = 1;
- /* FIXME: what does this mean? */
+ bdb->bi_flags |= BDB_RE_OPEN;
+ bdb->bi_flags ^= BDB_HAS_CONFIG;
+ ch_free( bdb->bi_dbenv_home );
+ bdb->bi_dbenv_home = NULL;
+ ch_free( bdb->bi_db_config_path );
+ bdb->bi_db_config_path = NULL;
+ c->cleanup = bdb_cf_cleanup;
+ ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
+ ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
break;
case BDB_NOSYNC:
bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
bdb->bi_txn_cp = 1;
bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
+ /* If we're in server mode and time-based checkpointing is enabled,
+ * submit a task to perform periodic checkpoints.
+ */
+ if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
+ struct re_s *re = bdb->bi_txn_cp_task;
+ if ( re ) {
+ re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
+ } else {
+ if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
+ fprintf( stderr, "%s: "
+ "\"checkpoint\" must occur after \"suffix\".\n",
+ c->log );
+ return 1;
+ }
+ bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
+ bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
+ LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
+ }
+ }
break;
case BDB_CONFIG: {
- char *ptr = c->line + STRLENOF("dbconfig");
+ char *ptr = c->line;
struct berval bv;
- while (!isspace(*ptr)) ptr++;
- while (isspace(*ptr)) ptr++;
-
+
+ if ( c->op == SLAP_CONFIG_ADD ) {
+ ptr += STRLENOF("dbconfig");
+ while (!isspace(*ptr)) ptr++;
+ while (isspace(*ptr)) ptr++;
+ }
+
if ( bdb->bi_flags & BDB_IS_OPEN ) {
bdb->bi_flags |= BDB_UPD_CONFIG;
c->cleanup = bdb_cf_cleanup;
FILE *f;
char *ptr;
+ if ( bdb->bi_dbenv_home )
+ ch_free( bdb->bi_dbenv_home );
bdb->bi_dbenv_home = c->value_string;
/* See if a DB_CONFIG file already exists here */
+ if ( bdb->bi_db_config_path )
+ ch_free( bdb->bi_db_config_path );
bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
c->argc - 1, &c->argv[1] );
if( rc != LDAP_SUCCESS ) return 1;
- if ( bdb->bi_flags & BDB_IS_OPEN ) {
- /* Start the task as soon as we finish here */
- ldap_pvt_runqueue_insert( &slapd_rq, 60,
- bdb_online_index, c->be );
+ if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
+ /* Start the task as soon as we finish here. Set a long
+ * interval (10 hours) so that it only gets scheduled once.
+ */
+ if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
+ fprintf( stderr, "%s: "
+ "\"index\" must occur after \"suffix\".\n",
+ c->log );
+ return 1;
+ }
+ bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
+ bdb_online_index, c->be,
+ LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
}
break;
int bdb_back_init_cf( BackendInfo *bi )
{
int rc;
- bi->bi_cf_table = bdbcfg;
+ bi->bi_cf_ocs = bdbocs;
rc = config_register_schema( bdbcfg, bdbocs );
if ( rc ) return rc;
- bdbcfg[0].ad = slap_schema.si_ad_objectClass;
return 0;
}