Skip to content

Commit

Permalink
Api fix & cdb2-open as debug msg-trap for test
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Hannum <[email protected]>
  • Loading branch information
markhannum committed Sep 14, 2024
1 parent 2673a3a commit 0ba41b2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
37 changes: 20 additions & 17 deletions cdb2api/cdb2api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@ struct cdb2_hndl {
int auto_consume_timeout_ms;
struct cdb2_hndl *fdb_hndl;
int is_child_hndl;
int comdb2db_resolved_from_config;
};

static void *cdb2_protobuf_alloc(void *allocator_data, size_t size)
Expand Down Expand Up @@ -1308,7 +1309,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;
Expand All @@ -1330,6 +1331,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);
Expand Down Expand Up @@ -1602,7 +1605,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;
Expand All @@ -1620,17 +1623,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;
}
Expand All @@ -1645,9 +1646,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);
}
}
Expand All @@ -1656,7 +1656,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);
}
}
Expand All @@ -1667,7 +1667,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;
Expand Down Expand Up @@ -1727,15 +1727,17 @@ 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);
dbname, db_hosts, 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);
Expand Down Expand Up @@ -5704,7 +5706,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;
Expand Down Expand Up @@ -5893,8 +5897,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);
}

Expand Down
42 changes: 42 additions & 0 deletions db/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "logmsg.h"
#include "time_accounting.h"
#include "intern_strings.h"
#include "cdb2api.h"

int osql_disable_net_test(void);
int osql_enable_net_test(int testnum);
Expand Down Expand Up @@ -83,6 +84,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, &ltok);
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, &ltok);
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 */
Expand Down Expand Up @@ -189,6 +230,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 {
Expand Down
3 changes: 2 additions & 1 deletion tests/cdb2api_unit.test/unit_get_comdb2db_hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ static int get_comdb2db_hosts(cdb2_hndl_tp *hndl, char comdb2db_hosts[][COMDB2HO
static int read_available_comdb2db_configs(
cdb2_hndl_tp *hndl, char comdb2db_hosts[][COMDB2HOSTNAME_LEN], const char *comdb2db_name,
int *num_hosts, int *comdb2db_num, const char *dbname, char db_hosts[][COMDB2HOSTNAME_LEN],
int *num_db_hosts, int *dbnum, int noLock, int defaultOnly)
int *num_db_hosts, int *dbnum, int noLock, int defaultOnly, int *comdb2db_found)
{
if (global_state == 1) return -1;

if (num_hosts) *num_hosts = 0;
if (num_db_hosts) *num_db_hosts = 0;
if (comdb2db_found) *comdb2db_found = 0;

if (global_state == 2) {
printf("read_available_comdb2db_configs global_state %d\n", global_state);
Expand Down
10 changes: 7 additions & 3 deletions tests/tools/cdb2api_unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ void test_read_comdb2db_cfg()
int num_db_hosts = 0;
int dbnum = 0;
int stack_at_open = 0;
int comdb2db_found;

const char *buf =
"\
Expand All @@ -206,8 +207,9 @@ void test_read_comdb2db_cfg()
buf, comdb2db_hosts,
&num_hosts, &comdb2db_num, dbname,
db_hosts, &num_db_hosts,
&dbnum, &stack_at_open);
&dbnum, &stack_at_open, &comdb2db_found);

assert(comdb2db_found == 0);
assert(num_hosts == 0);
assert(comdb2db_num == 0);
assert(num_db_hosts == 0);
Expand All @@ -226,8 +228,9 @@ void test_read_comdb2db_cfg()
buf2, comdb2db_hosts,
&num_hosts, &comdb2db_num, dbname,
db_hosts, &num_db_hosts,
&dbnum, &stack_at_open);
&dbnum, &stack_at_open, &comdb2db_found);

assert(comdb2db_found == 1);
assert(num_hosts == 5);
assert(comdb2db_num == 0);
assert(strcmp(comdb2db_hosts[0], "a") == 0);
Expand Down Expand Up @@ -262,8 +265,9 @@ void test_read_comdb2db_cfg()
buf3, comdb2db_hosts,
&num_hosts, &comdb2db_num, dbname,
db_hosts, &num_db_hosts,
&dbnum, &stack_at_open);
&dbnum, &stack_at_open, &comdb2db_found);

assert(comdb2db_found == 1);
assert(num_db_hosts == 3);
assert(num_hosts == 3);
assert(strcmp(comdb2db_hosts[0], "1estsuite_longname_to_testbuffer_overflow_when_assigning_hostna") == 0);
Expand Down

0 comments on commit 0ba41b2

Please sign in to comment.