Skip to content

Commit

Permalink
a bit cleaner dbtable link functions
Browse files Browse the repository at this point in the history
Signed-off-by: Dorin Hogea <[email protected]>
  • Loading branch information
dorinhogea committed Jul 26, 2023
1 parent e9876ec commit 87e1438
Showing 7 changed files with 31 additions and 46 deletions.
34 changes: 13 additions & 21 deletions db/comdb2.c
Original file line number Diff line number Diff line change
@@ -1478,12 +1478,12 @@ void clean_exit_sigwrap(int signum) {
clean_exit_thd(NULL);
}

static void free_sqlite_table(struct dbenv *dbenv)
static void free_dbtables(struct dbenv *dbenv)
{
for (int i = dbenv->num_dbs - 1; i >= 0; i--) {
dbtable *tbl = dbenv->dbs[i];
delete_schema(tbl->tablename); // tags hash
delete_db(tbl->tablename); // will free db
rem_dbtable_from_thedb_dbs(tbl);
bdb_cleanup_fld_hints(tbl->handle);
freedb(tbl);
}
@@ -1529,7 +1529,7 @@ static void finish_clean()
net_cleanup();
cleanup_sqlite_master();

free_sqlite_table(thedb);
free_dbtables(thedb);

if (thedb->sqlalias_hash) {
hash_clear(thedb->sqlalias_hash);
@@ -1694,7 +1694,7 @@ void clean_exit(void)
net_cleanup();
cleanup_sqlite_master();

free_sqlite_table(thedb);
free_dbtables(thedb);

if (thedb->sqlalias_hash) {
hash_clear(thedb->sqlalias_hash);
@@ -5693,41 +5693,33 @@ int main(int argc, char **argv)
return 0;
}

int add_db(dbtable *db)
int add_dbtable_to_thedb_dbs(dbtable *table)
{
Pthread_rwlock_wrlock(&thedb_lock);

if (_db_hash_find(db->tablename) != 0) {
if (_db_hash_find(table->tablename) != 0) {
Pthread_rwlock_unlock(&thedb_lock);
return -1;
}

thedb->dbs = realloc(thedb->dbs, (thedb->num_dbs + 1) * sizeof(dbtable *));
db->dbs_idx = thedb->num_dbs;
thedb->dbs[thedb->num_dbs++] = db;
table->dbs_idx = thedb->num_dbs;
thedb->dbs[thedb->num_dbs++] = table;

/* Add table to the hash. */
_db_hash_add(db);
_db_hash_add(table);

Pthread_rwlock_unlock(&thedb_lock);
return 0;
}

void delete_db(char *db_name)
void rem_dbtable_from_thedb_dbs(dbtable *table)
{
int idx;

Pthread_rwlock_wrlock(&thedb_lock);
if ((idx = getdbidxbyname_ll(db_name)) < 0) {
logmsg(LOGMSG_FATAL, "%s: failed to find tbl for deletion: %s\n", __func__,
db_name);
exit(1);
}

/* Remove the table from hash. */
_db_hash_del(thedb->dbs[idx]);
_db_hash_del(table);

for (int i = idx; i < (thedb->num_dbs - 1); i++) {
for (int i = table->dbs_idx; i < (thedb->num_dbs - 1); i++) {
thedb->dbs[i] = thedb->dbs[i + 1];
thedb->dbs[i]->dbs_idx = i;
}
@@ -6131,7 +6123,7 @@ int comdb2_reload_schemas(void *dbenv, void *inlsn)
abort();
}

free_sqlite_table(thedb);
free_dbtables(thedb);
thedb->dbs = NULL;

if (thedb->db_hash)
4 changes: 2 additions & 2 deletions db/comdb2.h
Original file line number Diff line number Diff line change
@@ -2391,8 +2391,8 @@ int add_queue_to_environment(char *table, int avgitemsz, int pagesize);
void stop_threads(struct dbenv *env);
void resume_threads(struct dbenv *env);
void replace_db_idx(struct dbtable *p_db, int idx);
int add_db(struct dbtable *db);
void delete_db(char *db_name);
int add_dbtable_to_thedb_dbs(dbtable *table);
void rem_dbtable_from_thedb_dbs(dbtable *table);
void hash_sqlalias_db(dbtable *db, const char *newname);
int rename_db(struct dbtable *db, const char *newname);
int ix_find_rnum_by_recnum(struct ireq *iq, int recnum_in, int ixnum,
17 changes: 7 additions & 10 deletions schemachange/sc_add_table.c
Original file line number Diff line number Diff line change
@@ -101,7 +101,8 @@ static inline int init_bthashsize_tran(struct dbtable *newdb, tran_type *tran)
* the file written with the csc2. */
int add_table_to_environment(char *table, const char *csc2,
struct schema_change_type *s, struct ireq *iq,
tran_type *trans, const char *timepartition_name)
tran_type *trans, const char *timepartition_name,
struct dbtable **pnewdb)
{
int rc;
struct dbtable *newdb;
@@ -162,12 +163,6 @@ int add_table_to_environment(char *table, const char *csc2,
goto err;
}

/* must re add the dbs if you're a physical replicant */
if (newdb->dbenv->master != gbl_myhostname || gbl_is_physical_replicant) {
/* This is a replicant calling scdone_callback */
add_db(newdb);
}

rc = adjust_master_tables(newdb, csc2, iq, trans);
if (rc) {
if (rc == SC_CSC2_ERROR)
@@ -188,7 +183,9 @@ int add_table_to_environment(char *table, const char *csc2,

if (s)
s->newdb = newdb;


if (pnewdb)
*pnewdb = newdb;

return SC_OK;

@@ -234,7 +231,7 @@ int do_add_table(struct ireq *iq, struct schema_change_type *s,
}
Pthread_mutex_lock(&csc2_subsystem_mtx);
rc = add_table_to_environment(s->tablename, s->newcsc2, s, iq, trans,
s->timepartition_name);
s->timepartition_name, &db);

Pthread_mutex_unlock(&csc2_subsystem_mtx);
if (rc) {
@@ -305,7 +302,7 @@ int finalize_add_table(struct ireq *iq, struct schema_change_type *s,
/* Set instant schema-change */
db->instant_schema_change = db->odh && s->instant_sc;

rc = add_db(db);
rc = add_dbtable_to_thedb_dbs(db);
if (rc) {
sc_errf(s, "Failed to add db to thedb->dbs, rc %d\n", rc);
return rc;
3 changes: 2 additions & 1 deletion schemachange/sc_add_table.h
Original file line number Diff line number Diff line change
@@ -20,7 +20,8 @@
int do_add_table(struct ireq *, struct schema_change_type *, tran_type *);
int add_table_to_environment(char *table, const char *csc2,
struct schema_change_type *s, struct ireq *iq,
tran_type *trans, const char *timepartition_name);
tran_type *trans, const char *timepartition_name,
dbtable **pnewdb);
int finalize_add_table(struct ireq *, struct schema_change_type *, tran_type *);

#endif
13 changes: 4 additions & 9 deletions schemachange/sc_callbacks.c
Original file line number Diff line number Diff line change
@@ -580,7 +580,7 @@ static int delete_table_rep(char *table, void *tran)
return -1;
}

delete_db(table);
rem_dbtable_from_thedb_dbs(db);
MEMORY_SYNC;
delete_schema(table);
return 0;
@@ -772,25 +772,20 @@ static int scdone_add(const char tablename[], void *arg, scdone_t type)
logmsg(LOGMSG_INFO, "Replicant adding table:%s\n", tablename);

rc = add_table_to_environment(table_copy, csc2text, NULL, NULL, tran,
timepart_is_next_shard(table_copy, NULL));
timepart_is_next_shard(table_copy, NULL), &db);
if (rc) {
logmsg(LOGMSG_FATAL, "%s: error adding table %s.\n", __func__,
tablename);
exit(1);
}

add_dbtable_to_thedb_dbs(db);

_master_recs(tran, tablename, type);

free(table_copy);
free(csc2text);

db = get_dbtable_by_name(tablename);
if (!db) {
logmsg(LOGMSG_FATAL, "%s: could not find newly created db: %s.\n",
__func__, tablename);
exit(1);
}

set_odh_options_tran(db, tran);
db->tableversion = table_version_select(db, tran);

2 changes: 1 addition & 1 deletion schemachange/sc_drop_table.c
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ static int delete_table(struct dbtable *db, tran_type *tran)
}

char *table = db->tablename;
delete_db(table);
rem_dbtable_from_thedb_dbs(db);
MEMORY_SYNC;
delete_schema(table);
bdb_del_table_csonparameters(tran, table);
4 changes: 2 additions & 2 deletions schemachange/sc_logic.c
Original file line number Diff line number Diff line change
@@ -841,7 +841,7 @@ void *sc_resuming_watchdog(void *p)
if (iq.sc->kind == SC_ADDTABLE) {
delete_temp_table(&iq, iq.sc->db);
if (iq.sc->add_state == SC_DONE_ADD) {
delete_db(iq.sc->tablename);
rem_dbtable_from_thedb_dbs(iq.sc->db);
}
}
/* TODO: (NC) Also delete view? */
@@ -1547,7 +1547,7 @@ int backout_schema_changes(struct ireq *iq, tran_type *tran)
}
if (s->kind == SC_ADDTABLE) {
if (s->add_state == SC_DONE_ADD) {
delete_db(s->tablename);
rem_dbtable_from_thedb_dbs(s->db);
}
if (s->newdb) {
backout_schemas(s->newdb->tablename);

0 comments on commit 87e1438

Please sign in to comment.