From b96b691bb32bd529f752202e0174810fdda5b78f Mon Sep 17 00:00:00 2001 From: Mark Hannum Date: Thu, 5 Sep 2024 20:50:52 -0400 Subject: [PATCH] Api fix & cdb2-open as debug msg-trap for test Signed-off-by: Mark Hannum --- cdb2api/cdb2api.c | 37 ++++++++++++++++++++----------------- db/debug.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/cdb2api/cdb2api.c b/cdb2api/cdb2api.c index 7df44fbed4..63f05abfac 100644 --- a/cdb2api/cdb2api.c +++ b/cdb2api/cdb2api.c @@ -1091,6 +1091,7 @@ struct cdb2_hndl { struct cdb2_hndl *fdb_hndl; int is_child_hndl; CDB2SQLQUERY__IdentityBlob *id_blob; + int comdb2db_resolved_from_config; }; static void *cdb2_protobuf_alloc(void *allocator_data, size_t size) @@ -1307,7 +1308,7 @@ static void only_read_config(cdb2_hndl_tp *, int, int); /* FORWARD */ static void read_comdb2db_cfg(cdb2_hndl_tp *hndl, SBUF2 *s, const char *comdb2db_name, const char *buf, char comdb2db_hosts[][CDB2HOSTNAME_LEN], int *num_hosts, int *comdb2db_num, const char *dbname, char db_hosts[][CDB2HOSTNAME_LEN], int *num_db_hosts, int *dbnum, - int *stack_at_open) + int *stack_at_open, int *comdb2db_found) { char line[PATH_MAX > 2048 ? PATH_MAX : 2048] = {0}; int line_no = 0; @@ -1329,6 +1330,8 @@ static void read_comdb2db_cfg(cdb2_hndl_tp *hndl, SBUF2 *s, const char *comdb2db strncpy(comdb2db_hosts[*num_hosts], tok, CDB2HOSTNAME_LEN - 1); (*num_hosts)++; tok = strtok_r(NULL, " :,", &last); + if (comdb2db_found) + *comdb2db_found = 1; } } else if (dbname && (strcasecmp(dbname, tok) == 0)) { tok = strtok_r(NULL, " :,", &last); @@ -1601,7 +1604,7 @@ static void set_cdb2_timeouts(cdb2_hndl_tp *hndl) static int read_available_comdb2db_configs(cdb2_hndl_tp *hndl, char comdb2db_hosts[][CDB2HOSTNAME_LEN], const char *comdb2db_name, int *num_hosts, int *comdb2db_num, const char *dbname, char db_hosts[][CDB2HOSTNAME_LEN], int *num_db_hosts, - int *dbnum, int noLock, int defaultOnly) + int *dbnum, int noLock, int defaultOnly, int *comdb2db_found) { char filename[PATH_MAX]; SBUF2 *s; @@ -1619,17 +1622,15 @@ static int read_available_comdb2db_configs(cdb2_hndl_tp *hndl, char comdb2db_hos int *send_stack = hndl ? (&hndl->send_stack) : NULL; if (!defaultOnly && CDB2DBCONFIG_BUF != NULL) { - read_comdb2db_cfg(NULL, NULL, comdb2db_name, CDB2DBCONFIG_BUF, - comdb2db_hosts, num_hosts, comdb2db_num, dbname, - db_hosts, num_db_hosts, dbnum, send_stack); + read_comdb2db_cfg(NULL, NULL, comdb2db_name, CDB2DBCONFIG_BUF, comdb2db_hosts, num_hosts, comdb2db_num, dbname, + db_hosts, num_db_hosts, dbnum, send_stack, comdb2db_found); fallback_on_bb_bin = 0; } else { if (!defaultOnly && *CDB2DBCONFIG_NOBBENV != '\0') { s = sbuf2openread(CDB2DBCONFIG_NOBBENV); if (s != NULL) { - read_comdb2db_cfg(NULL, s, comdb2db_name, NULL, comdb2db_hosts, - num_hosts, comdb2db_num, dbname, db_hosts, - num_db_hosts, dbnum, send_stack); + read_comdb2db_cfg(NULL, s, comdb2db_name, NULL, comdb2db_hosts, num_hosts, comdb2db_num, dbname, + db_hosts, num_db_hosts, dbnum, send_stack, comdb2db_found); sbuf2close(s); fallback_on_bb_bin = 0; } @@ -1644,9 +1645,8 @@ static int read_available_comdb2db_configs(cdb2_hndl_tp *hndl, char comdb2db_hos if (fallback_on_bb_bin) { s = sbuf2openread(CDB2DBCONFIG_TEMP_BB_BIN); if (s != NULL) { - read_comdb2db_cfg(NULL, s, comdb2db_name, NULL, comdb2db_hosts, - num_hosts, comdb2db_num, dbname, db_hosts, - num_db_hosts, dbnum, send_stack); + read_comdb2db_cfg(NULL, s, comdb2db_name, NULL, comdb2db_hosts, num_hosts, comdb2db_num, dbname, db_hosts, + num_db_hosts, dbnum, send_stack, comdb2db_found); sbuf2close(s); } } @@ -1655,7 +1655,7 @@ static int read_available_comdb2db_configs(cdb2_hndl_tp *hndl, char comdb2db_hos s = sbuf2openread(filename); if (s != NULL) { read_comdb2db_cfg(hndl, s, comdb2db_name, NULL, comdb2db_hosts, num_hosts, comdb2db_num, dbname, db_hosts, - num_db_hosts, dbnum, send_stack); + num_db_hosts, dbnum, send_stack, comdb2db_found); sbuf2close(s); } } @@ -1666,7 +1666,7 @@ static int read_available_comdb2db_configs(cdb2_hndl_tp *hndl, char comdb2db_hos int cdb2_get_comdb2db(char **comdb2dbname) { if (!strlen(cdb2_comdb2dbname)) { - read_available_comdb2db_configs(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0); + read_available_comdb2db_configs(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL); } (*comdb2dbname) = strdup(cdb2_comdb2dbname); return 0; @@ -1726,14 +1726,16 @@ static int get_comdb2db_hosts(cdb2_hndl_tp *hndl, char comdb2db_hosts[][CDB2HOST int dbinfo_or_dns) { int rc; + int comdb2db_found = 0; if (hndl) debugprint("entering\n"); rc = read_available_comdb2db_configs(hndl, comdb2db_hosts, comdb2db_name, num_hosts, comdb2db_num, dbname, db_hosts, - num_db_hosts, dbnum, 0, 0); + num_db_hosts, dbnum, 0, 0, &comdb2db_found); if (rc == -1) return rc; + hndl->comdb2db_resolved_from_config = comdb2db_found; if (master) *master = -1; set_cdb2_timeouts(hndl); @@ -5809,7 +5811,9 @@ static int comdb2db_get_dbhosts(cdb2_hndl_tp *hndl, const char *comdb2db_name, cdb2__sqlresponse__free_unpacked(sqlresponse, NULL); free(p); int timeoutms = 10 * 1000; - if (sbuf2free(ss) == 0) + if (hndl->comdb2db_resolved_from_config) + sbuf2close(ss); + else if (sbuf2free(ss) == 0) cdb2_socket_pool_donate_ext(newsql_typestr, fd, timeoutms / 1000, comdb2db_num); free_events(&tmp); return 0; @@ -5998,8 +6002,7 @@ static int cdb2_dbinfo_query(cdb2_hndl_tp *hndl, const char *type, const char *d static inline void only_read_config(cdb2_hndl_tp *hndl, int noLock, int defaultOnly) { - read_available_comdb2db_configs(NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, noLock, defaultOnly); + read_available_comdb2db_configs(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, noLock, defaultOnly, NULL); set_cdb2_timeouts(hndl); } diff --git a/db/debug.c b/db/debug.c index ccc0d1f7c2..b5511c854c 100644 --- a/db/debug.c +++ b/db/debug.c @@ -28,6 +28,7 @@ #include "logmsg.h" #include "time_accounting.h" #include "intern_strings.h" +#include "cdb2api.h" /* print a description of each tcm test */ static void tcmtest_printlist() @@ -62,6 +63,46 @@ void debug_trap(char *line, int lline) } tokcpy(tok, ltok, tag); del_tag_schema(table, tag); + } else if (tokcmp(tok, ltok, "cdb2open") == 0) { + char cdb2_dbname[64] = {0}; + char cdb2_tier[64] = {0}; + + /* grab comdb2db name */ + tok = segtok(line, lline, &st, <ok); + if (ltok > 64 || ltok <= 0) { + logmsg(LOGMSG_ERROR, "Invalid comdb2db name.\n"); + return; + } + tokcpy(tok, ltok, cdb2_dbname); + + /* grab comdb2db tier */ + tok = segtok(line, lline, &st, <ok); + if (ltok > 64 || ltok <= 0) { + logmsg(LOGMSG_ERROR, "Invalid tier.\n"); + return; + } + tokcpy(tok, ltok, cdb2_tier); + + /* iterate 10 times */ + for (int i = 0; i < 10; i++) { + cdb2_hndl_tp *hndl; + int rc = cdb2_open(&hndl, cdb2_dbname, cdb2_tier, 0); + if (!rc) { + logmsg(LOGMSG_USER, "Opened handle to %s/%s\n", cdb2_dbname, cdb2_tier); + if ((rc = cdb2_run_statement(hndl, "select comdb2_host()")) == CDB2_OK) { + while ((rc = cdb2_next_record(hndl)) != CDB2_OK_DONE) { + logmsg(LOGMSG_USER, "%s\n", (const char *)cdb2_column_value(hndl, 0)); + } + } else { + logmsg(LOGMSG_USER, "Failed to run-statement on %s/%s, %s\n", cdb2_dbname, cdb2_tier, + cdb2_errstr(hndl)); + } + cdb2_close(hndl); + } else { + logmsg(LOGMSG_USER, "Failed to open %s/%s, %s\n", cdb2_dbname, cdb2_tier, cdb2_errstr(hndl)); + } + } + } else if (tokcmp(tok, ltok, "tcmtest") == 0) { /* grab the tcmtest-name */ @@ -102,6 +143,7 @@ void debug_trap(char *line, int lline) logmsg(LOGMSG_USER, "getvers table - get schema version for table (or all)\n"); logmsg(LOGMSG_USER, "putvers table num - set schema version for table\n"); logmsg(LOGMSG_USER, "delsc table tag - delete a tag\n"); + logmsg(LOGMSG_USER, "cdb2open dbname tier - cdb2_open comdb2db test\n"); logmsg(LOGMSG_USER, "timings - print all accumulated " "timing measurements \n"); } else {