Skip to content

Commit 2291d06

Browse files
committed
Port remove hndl->cluster, check for default, don't ask sockpool for default
Signed-off-by: Salil Chandra <[email protected]>
1 parent debb147 commit 2291d06

File tree

2 files changed

+83
-42
lines changed

2 files changed

+83
-42
lines changed

cdb2api/cdb2api.c

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,8 +1601,8 @@ static void read_comdb2db_environment_cfg(cdb2_hndl_tp *hndl, const char *comdb2
16011601
}
16021602

16031603
if (hndl) {
1604-
if ((strcasecmp(hndl->cluster, "default") == 0) && cdb2_default_cluster_set_from_env)
1605-
strcpy(hndl->cluster, cdb2_default_cluster);
1604+
if ((strcasecmp(hndl->type, "default") == 0) && cdb2_default_cluster_set_from_env)
1605+
strcpy(hndl->type, cdb2_default_cluster);
16061606
if (cdb2_connect_timeout_set_from_env)
16071607
hndl->connect_timeout = CDB2_CONNECT_TIMEOUT;
16081608
if (cdb2_sockpool_send_timeoutms_set_from_env)
@@ -1626,7 +1626,7 @@ static void read_comdb2db_environment_cfg(cdb2_hndl_tp *hndl, const char *comdb2
16261626
pthread_mutex_unlock(&cdb2_sockpool_mutex);
16271627
}
16281628

1629-
static void only_read_config(cdb2_hndl_tp *); /* FORWARD */
1629+
static void only_read_config(cdb2_hndl_tp *, int *); /* FORWARD */
16301630

16311631
static int cdb2_max_room_num = 0;
16321632
static int cdb2_has_room_distance = 0;
@@ -1772,8 +1772,8 @@ static void read_comdb2db_cfg(cdb2_hndl_tp *hndl, SBUF2 *s, const char *comdb2db
17721772
if (!cdb2_default_cluster_set_from_env && strcasecmp("default_type", tok) == 0) {
17731773
tok = strtok_r(NULL, " :,", &last);
17741774
if (tok) {
1775-
if (hndl && (strcasecmp(hndl->cluster, "default") == 0)) {
1776-
strncpy(hndl->cluster, tok, sizeof(cdb2_default_cluster) - 1);
1775+
if (hndl && (strcasecmp(hndl->type, "default") == 0)) {
1776+
strncpy(hndl->type, tok, sizeof(cdb2_default_cluster) - 1);
17771777
} else if (!hndl) {
17781778
strncpy(cdb2_default_cluster, tok, sizeof(cdb2_default_cluster) - 1);
17791779
}
@@ -2192,6 +2192,11 @@ static int read_available_comdb2db_configs(cdb2_hndl_tp *hndl, char comdb2db_hos
21922192
#ifdef CDB2API_TEST
21932193
}
21942194
#endif
2195+
2196+
if (hndl && strcasecmp(hndl->type, "default") == 0 && cdb2_default_cluster[0] != '\0') {
2197+
strncpy(hndl->type, cdb2_default_cluster, sizeof(hndl->type) - 1);
2198+
}
2199+
21952200
pthread_mutex_unlock(&cdb2_cfg_lock);
21962201
return 0;
21972202
}
@@ -3014,6 +3019,20 @@ int cdb2_socket_pool_get_fd(cdb2_hndl_tp *hndl, const char *typestr, int dbnum,
30143019
return fd;
30153020
}
30163021

3022+
static int typestr_type_is_default(const char *typestr)
3023+
{
3024+
const int TYPESTR_TIER_IDX = 2;
3025+
const char *type_start = typestr;
3026+
for (int i = 0; i < TYPESTR_TIER_IDX; i++) {
3027+
type_start = strchr(type_start, '/');
3028+
assert(type_start);
3029+
type_start++;
3030+
}
3031+
const char *const type_end = strchr(type_start, '/');
3032+
assert(type_end);
3033+
return strncmp(type_start, "default", type_end - type_start) == 0;
3034+
}
3035+
30173036
/* Get the sbuf of a socket matching the given type string from
30183037
* the pool. Returns NULL if none is available or the sbuf on
30193038
* success. */
@@ -3031,6 +3050,13 @@ SBUF2 *cdb2_socket_pool_get(cdb2_hndl_tp *hndl, const char *typestr, int dbnum,
30313050
}
30323051
#endif
30333052

3053+
#ifdef CDB2API_TEST
3054+
if (typestr_type_is_default(typestr)) {
3055+
fprintf(stderr, "%s: ERR: Did not expect default type in typestring\n", __func__);
3056+
abort();
3057+
}
3058+
#endif
3059+
30343060
if (was_from_local_cache) {
30353061
*was_from_local_cache = 0;
30363062
sb = local_connection_cache_get(hndl, typestr);
@@ -3054,6 +3080,13 @@ void cdb2_socket_pool_donate_ext(const cdb2_hndl_tp *hndl, const char *typestr,
30543080
{
30553081
if (log_calls)
30563082
fprintf(stderr, "%p> %s(%s,%d): fd=%d\n", (void *)pthread_self(), __func__, typestr, dbnum, fd);
3083+
#ifdef CDB2API_TEST
3084+
if (typestr_type_is_default(typestr)) {
3085+
fprintf(stderr, "%s: ERR: Did not expect default type in typestring\n", __func__);
3086+
abort();
3087+
}
3088+
#endif
3089+
30573090
int enabled = 0;
30583091
int sockpool_fd = -1;
30593092
int sp_generation = -1;
@@ -7579,9 +7612,17 @@ static int cdb2_dbinfo_query(cdb2_hndl_tp *hndl, const char *type, const char *d
75797612
return rc;
75807613
}
75817614

7582-
static inline void only_read_config(cdb2_hndl_tp *hndl)
7615+
static inline void only_read_config(cdb2_hndl_tp *hndl, int *default_err)
75837616
{
75847617
read_available_comdb2db_configs(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
7618+
if (default_err && strcasecmp(hndl->type, "default") == 0) {
7619+
if (cdb2_default_cluster[0] != '\0') {
7620+
strncpy(hndl->type, cdb2_default_cluster, sizeof(hndl->type) - 1);
7621+
} else {
7622+
snprintf(hndl->errstr, sizeof(hndl->errstr), "No default_type entry in comdb2db config.\n");
7623+
*default_err = -1;
7624+
}
7625+
}
75857626
set_cdb2_timeouts(hndl);
75867627
}
75877628

@@ -7607,6 +7648,11 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
76077648
&comdb2db_num, hndl->dbname, hndl->hosts, &(hndl->num_hosts), &hndl->dbnum, 0, hndl->shards,
76087649
&(hndl->num_shards));
76097650

7651+
if (strcasecmp(hndl->type, "default") == 0) {
7652+
sprintf(hndl->errstr, "No default_type entry in comdb2db config.\n");
7653+
return -1;
7654+
}
7655+
76107656
/* Before database destination discovery */
76117657
cdb2_event *e = NULL;
76127658
void *callbackrc;
@@ -7646,20 +7692,7 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
76467692
strcpy(comdb2db_name, cdb2_comdb2dbname);
76477693
}
76487694

7649-
if (strcasecmp(hndl->cluster, "default") == 0) {
7650-
if (cdb2_default_cluster[0] == '\0') {
7651-
sprintf(hndl->errstr, "cdb2_get_dbhosts: no default_type "
7652-
"entry in comdb2db config.");
7653-
rc = -1;
7654-
goto after_callback;
7655-
}
7656-
strncpy(hndl->cluster, cdb2_default_cluster, sizeof(hndl->cluster) - 1);
7657-
if (cdb2cfg_override || (cdb2_use_env_vars && default_type_override_env)) {
7658-
strncpy(hndl->type, cdb2_default_cluster, sizeof(hndl->type) - 1);
7659-
}
7660-
}
7661-
7662-
if (strcasecmp(hndl->cluster, "local") == 0) {
7695+
if (strcasecmp(hndl->type, "local") == 0) {
76637696
hndl->num_hosts = 1;
76647697
strcpy(hndl->hosts[0], "localhost");
76657698
hndl->flags |= CDB2_DIRECT_CPU;
@@ -7725,7 +7758,7 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
77257758
if (i == master)
77267759
continue;
77277760
rc = comdb2db_get_dbhosts(hndl, comdb2db_name, comdb2db_num, comdb2db_hosts[i], comdb2db_ports[i],
7728-
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->cluster, &hndl->dbnum,
7761+
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->type, &hndl->dbnum,
77297762
&hndl->num_hosts_sameroom, num_retry, use_bmsd, hndl->shards, &hndl->num_shards,
77307763
&hndl->num_shards_sameroom);
77317764
if (rc == 0 || time(NULL) >= max_time) {
@@ -7738,7 +7771,7 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
77387771
}
77397772
if (rc == -1 && time(NULL) < max_time) {
77407773
rc = comdb2db_get_dbhosts(hndl, comdb2db_name, comdb2db_num, comdb2db_hosts[master], comdb2db_ports[master],
7741-
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->cluster, &hndl->dbnum,
7774+
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->type, &hndl->dbnum,
77427775
&hndl->num_hosts_sameroom, num_retry, use_bmsd, hndl->shards, &hndl->num_shards,
77437776
&hndl->num_shards_sameroom);
77447777
}
@@ -7753,9 +7786,10 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
77537786
goto after_callback;
77547787
}
77557788
if (hndl->num_hosts == 0) {
7756-
sprintf(hndl->errstr, "cdb2_get_dbhosts: comdb2db has no entry of "
7757-
"db %s of cluster type %s.",
7758-
hndl->dbname, hndl->cluster);
7789+
sprintf(hndl->errstr,
7790+
"cdb2_get_dbhosts: comdb2db has no entry of "
7791+
"db %s of cluster type %s.",
7792+
hndl->dbname, hndl->type);
77597793
rc = -1;
77607794
goto after_callback;
77617795
}
@@ -7893,10 +7927,10 @@ static void after_discovery(cdb2_hndl_tp *hndl)
78937927
}
78947928
}
78957929

7896-
static int get_connection_int(cdb2_hndl_tp *hndl)
7930+
static int get_connection_int(cdb2_hndl_tp *hndl, int *err)
78977931
{
7898-
only_read_config(hndl);
7899-
if (get_dbinfo)
7932+
only_read_config(hndl, err);
7933+
if (get_dbinfo || *err)
79007934
return -1;
79017935
before_discovery(hndl);
79027936
SBUF2 *sb = sockpool_get(hndl);
@@ -7906,7 +7940,7 @@ static int get_connection_int(cdb2_hndl_tp *hndl)
79067940
return init_connection(hndl, sb);
79077941
}
79087942

7909-
static int get_connection(cdb2_hndl_tp *hndl)
7943+
static int get_connection(cdb2_hndl_tp *hndl, int *err)
79107944
{
79117945
if (hndl->is_admin || (hndl->flags & CDB2_MASTER)) // don't grab from sockpool
79127946
return -1;
@@ -7926,7 +7960,7 @@ static int get_connection(cdb2_hndl_tp *hndl)
79267960
} else if (get_dbinfo || sockpool_enabled == -1 || cdb2cfg_override) {
79277961
return -1;
79287962
}
7929-
int rc = get_connection_int(hndl);
7963+
int rc = get_connection_int(hndl, err);
79307964
return rc;
79317965
}
79327966

@@ -7983,7 +8017,7 @@ static int configure_from_literal(cdb2_hndl_tp *hndl, const char *type)
79838017
assert(type_copy[0] == '@');
79848018
char *s = type_copy + 1; // advance past the '@'
79858019

7986-
only_read_config(hndl);
8020+
only_read_config(hndl, NULL); // don't care about default here?
79878021

79888022
char *machine;
79898023
machine = strtok_r(s, ",", &eomachine);
@@ -8331,8 +8365,7 @@ static cdb2_ssl_sess *cdb2_get_ssl_sessions(cdb2_hndl_tp *hndl)
83318365
return NULL;
83328366

83338367
for (pos = cdb2_ssl_sess_cache.next; pos != NULL; pos = pos->next) {
8334-
if (strcasecmp(hndl->dbname, pos->dbname) == 0 &&
8335-
strcasecmp(hndl->cluster, pos->cluster) == 0) {
8368+
if (strcasecmp(hndl->dbname, pos->dbname) == 0 && strcasecmp(hndl->type, pos->cluster) == 0) {
83368369
if (!pos->ref) {
83378370
pos->ref = 1;
83388371
break;
@@ -8371,8 +8404,8 @@ static int cdb2_add_ssl_session(cdb2_hndl_tp *hndl)
83718404
return ENOMEM;
83728405
strncpy(hndl->sess->dbname, hndl->dbname, sizeof(hndl->dbname) - 1);
83738406
hndl->sess->dbname[sizeof(hndl->dbname) - 1] = '\0';
8374-
strncpy(hndl->sess->cluster, hndl->cluster, sizeof(hndl->cluster) - 1);
8375-
hndl->sess->cluster[sizeof(hndl->cluster) - 1] = '\0';
8407+
strncpy(hndl->sess->cluster, hndl->type, sizeof(hndl->type) - 1);
8408+
hndl->sess->cluster[sizeof(hndl->type) - 1] = '\0';
83768409
hndl->sess->ref = 1;
83778410
hndl->sess->sessobj = NULL;
83788411

@@ -8427,7 +8460,6 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
84278460
hndl->gbl_event_version = calloc(1, sizeof(*hndl->gbl_event_version));
84288461
TAILQ_INIT(&hndl->queries);
84298462
strncpy(hndl->dbname, dbname, sizeof(hndl->dbname) - 1);
8430-
strncpy(hndl->cluster, type, sizeof(hndl->cluster) - 1);
84318463
strncpy(hndl->type, type, sizeof(hndl->type) - 1);
84328464
hndl->flags = flags;
84338465
hndl->dbnum = 1;
@@ -8468,8 +8500,7 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
84688500
fprintf(stderr, "WARNING: %s:Value of %s is not valid. Using value '%s'.\n", __func__,
84698501
COMDB2_CONFIG_DB_DEFAULT_TYPE, db_default_type);
84708502
}
8471-
strncpy(hndl->cluster, db_default_type, sizeof(hndl->cluster) - 1);
8472-
strncpy(hndl->type, hndl->cluster, sizeof(hndl->type) - 1);
8503+
strncpy(hndl->type, db_default_type, sizeof(hndl->type) - 1);
84738504
hndl->db_default_type_override_env = 1;
84748505
}
84758506
}
@@ -8508,7 +8539,9 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
85088539

85098540
if (hndl->flags & CDB2_DIRECT_CPU) {
85108541
/* Get defaults from comdb2db.cfg */
8511-
only_read_config(hndl);
8542+
only_read_config(hndl, &rc);
8543+
if (rc)
8544+
goto out;
85128545
hndl->got_dbinfo = 1;
85138546
hndl->num_hosts = 1;
85148547
strncpy(hndl->hosts[0], type, sizeof(hndl->hosts[0]) - 1);
@@ -8536,7 +8569,10 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
85368569
hndl->sockpool_enabled = db_host_info ? -1 : 0;
85378570
}
85388571

8539-
if (get_connection(hndl) != 0) {
8572+
const int should_get_dbinfo = get_connection(hndl, &rc);
8573+
if (rc)
8574+
goto out;
8575+
if (should_get_dbinfo) {
85408576
hndl->got_dbinfo = 1;
85418577
rc = cdb2_get_dbhosts(hndl);
85428578
}
@@ -8856,7 +8892,7 @@ static void *cdb2_invoke_callback(cdb2_hndl_tp *hndl, cdb2_event *e, int argc,
88568892
void *rc;
88578893
int state;
88588894
char *fp;
8859-
const char *dbtype = (hndl == NULL) ? NULL : hndl->type;
8895+
const char *dbtype;
88608896

88618897
/* Fast return if no arguments need to be passed to the callback. */
88628898
if (e->argc == 0)
@@ -8878,6 +8914,12 @@ static void *cdb2_invoke_callback(cdb2_hndl_tp *hndl, cdb2_event *e, int argc,
88788914
hostname = hndl->hosts[hndl->connected_host];
88798915
port = hndl->ports[hndl->connected_host];
88808916
}
8917+
8918+
if (hndl == NULL || strcasecmp(hndl->type, "default") == 0)
8919+
dbtype = cdb2_default_cluster;
8920+
else
8921+
dbtype = hndl->type;
8922+
88818923
rc = 0;
88828924
state = 0;
88838925

cdb2api/cdb2api_hndl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ typedef struct cdb2_ssl_sess cdb2_ssl_sess;
111111

112112
struct cdb2_hndl {
113113
char dbname[DBNAME_LEN];
114-
char cluster[64];
115114
char type[TYPE_LEN];
116115
char hosts[MAX_NODES][CDB2HOSTNAME_LEN];
117116
uint64_t timestampus; // client query timestamp of first try

0 commit comments

Comments
 (0)