From 80b52182a552024f1bc0c9330a03217c998cabe6 Mon Sep 17 00:00:00 2001 From: Aakash Arayambeth Date: Wed, 22 Jan 2025 17:00:00 -0500 Subject: [PATCH] cleanup remote tables --- db/hash_partition.c | 82 ++++++++++++++++++++++++++-------------- sqlite/src/comdb2build.c | 4 ++ 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/db/hash_partition.c b/db/hash_partition.c index 5fcc6ac74f..e53f0774b1 100644 --- a/db/hash_partition.c +++ b/db/hash_partition.c @@ -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(...) * */ @@ -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); @@ -592,8 +601,7 @@ 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); @@ -601,7 +609,7 @@ void deleteRemoteTables(struct comdb2_partition *partition, int startIdx) { 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) { @@ -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) { @@ -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); @@ -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); @@ -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; diff --git a/sqlite/src/comdb2build.c b/sqlite/src/comdb2build.c index 528fbbe090..fea527dcc2 100644 --- a/sqlite/src/comdb2build.c +++ b/sqlite/src/comdb2build.c @@ -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) @@ -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 :