Skip to content

Commit

Permalink
cleanup remote tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Aakash Arayambeth committed Jan 22, 2025
1 parent fb8ef7e commit 80b5218
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
82 changes: 54 additions & 28 deletions db/hash_partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,15 @@ char *extractSchema(const char *insertQuery) {
return result;
}

/*Get the tablename from a create query*/
static void getTableName(const char *insertQuery, char *tableName) {
if (strcasestr(insertQuery, "CREATE TABLE IF NOT EXISTS ")) {
sscanf(insertQuery + 27, "%s", tableName);
} else if (strcasestr(insertQuery, "CREATE TABLE ")){
sscanf(insertQuery + 13, "%s", tableName);
}
}

/* Create an insert query against shards
* create table tableName(...)
* */
Expand Down Expand Up @@ -567,19 +576,19 @@ static char *getDropStatement(const char *tableName) {

/*
*/
void deleteRemoteTables(struct comdb2_partition *partition, int startIdx) {
int deleteRemoteTables(const char *viewName, char **tables, int startIdx) {
cdb2_hndl_tp *hndl;
int rc;
char *savePtr = NULL, *remoteDbName = NULL, *remoteTableName = NULL;
char *tier = NULL;
int i;
char remoteDbName[MAX_DBNAME_LENGTH],remoteTableName[MAXTABLELEN];
for(i = startIdx; i >= 0; i--) {
char *p = partition->u.hash.partitions[i];
remoteDbName = strtok_r(p,".", &savePtr);
remoteTableName = strtok_r(NULL, ".", &savePtr);
if (remoteTableName == NULL) {
remoteTableName = remoteDbName;
remoteDbName = gbl_dbname;
memset(remoteDbName, 0, MAX_DBNAME_LENGTH);
memset(remoteTableName, 0, MAXTABLELEN);
rc = sscanf(tables[i], "%[^.].%[^.]", remoteDbName, remoteTableName);
if (strlen(remoteTableName)==0) {
strncpy(remoteTableName, remoteDbName, sizeof(remoteTableName));
strncpy(remoteDbName, gbl_dbname, sizeof(remoteDbName));
}
logmsg(LOGMSG_USER, "The db is %s, the table is %s\n", remoteDbName, remoteTableName);

Expand All @@ -592,16 +601,15 @@ void deleteRemoteTables(struct comdb2_partition *partition, int startIdx) {
tier = (char *)mach_class_class2name(get_my_mach_class());
}
if (!tier) {
logmsg(LOGMSG_ERROR, "Failed to get tier for remotedb %s\n", p);
abort();
logmsg(LOGMSG_ERROR, "Failed to get tier for remotedb %s\n", remoteDbName);
}
logmsg(LOGMSG_USER, "GOT THE TIER AS %s\n", tier);
rc = getDbHndl(&hndl, remoteDbName, tier);
}
if (rc) {
logmsg(LOGMSG_ERROR, "Failed to get handle. rc: %d, err: %s\n", rc, cdb2_errstr(hndl));
logmsg(LOGMSG_ERROR, "Failed to drop table %s on remote db %s\n", remoteDbName, remoteTableName);
goto cleanup;
goto close_handle;
}
char *dropStatement = getDropStatement(remoteTableName);
if (!dropStatement) {
Expand All @@ -615,13 +623,27 @@ void deleteRemoteTables(struct comdb2_partition *partition, int startIdx) {
rc = cdb2_run_statement(hndl, dropStatement);
if (rc) {
logmsg(LOGMSG_ERROR, "Failed to drop table %s on database %s. rc: %d, err: %s\n", remoteTableName, remoteDbName, rc, cdb2_errstr(hndl));
goto close_handle;
}
close_handle:
cdb2_close(hndl);
cleanup:
free(remoteDbName);
free(remoteTableName);

/* now delete the partition metadata */
char *deletePartition = getDropStatement(viewName);
if (deletePartition == NULL) {
logmsg(LOGMSG_ERROR, "Failed to generate drop statement for partition %s on database %s\n", viewName, remoteDbName);
goto close_handle;
}
rc = cdb2_run_statement(hndl, deletePartition);
if (rc) {
logmsg(LOGMSG_ERROR, "Failed to drop partition . rc: %d, err: %s\n", rc, cdb2_errstr(hndl));
goto close_handle;
}

free(dropStatement);
}
return 0;
close_handle:
cdb2_close(hndl);
return -1;
}

int createRemoteTables(struct comdb2_partition *partition) {
Expand All @@ -630,14 +652,14 @@ int createRemoteTables(struct comdb2_partition *partition) {
int num_partitions = partition->u.hash.num_partitions;
int i;
char *tier = NULL;
char *savePtr = NULL, *remoteDbName = NULL, *remoteTableName = NULL, *p = NULL;
char remoteDbName[MAX_DBNAME_LENGTH],remoteTableName[MAXTABLELEN];
for (i = 0; i < num_partitions; i++) {
p = strdup(partition->u.hash.partitions[i]);
remoteDbName = strtok_r(p,".", &savePtr);
remoteTableName = strtok_r(NULL, ".", &savePtr);
if (remoteTableName == NULL) {
remoteTableName = remoteDbName;
remoteDbName = gbl_dbname;
memset(remoteDbName, 0, MAX_DBNAME_LENGTH);
memset(remoteTableName, 0, MAXTABLELEN);
rc = sscanf(partition->u.hash.partitions[i], "%[^.].%[^.]", remoteDbName, remoteTableName);
if (strlen(remoteTableName)==0) {
strncpy(remoteTableName, remoteDbName, sizeof(remoteTableName));
strncpy(remoteDbName, gbl_dbname, sizeof(remoteDbName));
}

logmsg(LOGMSG_USER, "The db is %s, the table is %s\n", remoteDbName, remoteTableName);
Expand All @@ -651,7 +673,7 @@ int createRemoteTables(struct comdb2_partition *partition) {
tier = (char *)mach_class_class2name(get_my_mach_class());
}
if (!tier) {
logmsg(LOGMSG_ERROR, "Failed to get tier for remotedb %s\n", p);
logmsg(LOGMSG_ERROR, "Failed to get tier for remotedb %s\n", remoteDbName);
abort();
}
logmsg(LOGMSG_USER, "GOT THE TIER AS %s\n", tier);
Expand Down Expand Up @@ -686,17 +708,21 @@ int createRemoteTables(struct comdb2_partition *partition) {
goto cleanup_tables;
}
cdb2_close(hndl);
free(p);
}
return 0;
cleanup_tables:
/* close most recent handle*/
cdb2_close(hndl);
free(p);
deleteRemoteTables(partition, i);
/*TODO AAR: Also delete partition info from remote tables*/
char table[MAXTABLELEN] = {0};
getTableName(partition->u.hash.createQuery, table);
if (strlen(table)==0) {
logmsg(LOGMSG_ERROR, "Failed to extract tablename. Not cleaning up remote databases!\n");
return -1;
}
deleteRemoteTables(table,(char **)partition->u.hash.partitions, i);
return -1;
}

int remove_alias(const char *);
void deleteLocalAliases(struct comdb2_partition *partition, int startIdx) {
int i, rc;
Expand Down
4 changes: 4 additions & 0 deletions sqlite/src/comdb2build.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extern int comdb2_save_ddl_context(char *name, void *ctx, comdb2ma mem);
extern void *comdb2_get_ddl_context(char *name);
int createRemoteTables(struct comdb2_partition *partition);
int createLocalAliases(struct comdb2_partition *partition);
void deleteRemoteTables(const char *, char **, int startIdx);
/******************* Utility ****************************/

static inline int setError(Parse *pParse, int rc, const char *msg)
Expand Down Expand Up @@ -893,6 +894,9 @@ void comdb2DropTable(Parse *pParse, SrcList *pName)
sc->partition.type = PARTITION_REMOVE_COL_HASH;
strncpy0(sc->tablename, hash_view_get_tablename(hashView), MAXTABLELEN);
logmsg(LOGMSG_USER, "SC->TABLENAME is %s\n", sc->tablename);
/* delete remote tables here */
if (gbl_create_remote_tables)
deleteRemoteTables(hash_view_get_viewname(hashView), hash_view_get_partitions(hashView), hash_view_get_num_partitions(hashView) - 1);
}
tran_type *tran = curtran_gettran();
int rc = get_csc2_file_tran(partition_first_shard ? partition_first_shard :
Expand Down

0 comments on commit 80b5218

Please sign in to comment.