Skip to content

Commit 2a9ac09

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 1785421 commit 2a9ac09

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
@@ -1624,8 +1624,8 @@ static void read_comdb2db_environment_cfg(cdb2_hndl_tp *hndl, const char *comdb2
16241624
}
16251625

16261626
if (hndl) {
1627-
if ((strcasecmp(hndl->cluster, "default") == 0) && cdb2_default_cluster_set_from_env)
1628-
strcpy(hndl->cluster, cdb2_default_cluster);
1627+
if ((strcasecmp(hndl->type, "default") == 0) && cdb2_default_cluster_set_from_env)
1628+
strcpy(hndl->type, cdb2_default_cluster);
16291629
if (cdb2_connect_timeout_set_from_env)
16301630
hndl->connect_timeout = CDB2_CONNECT_TIMEOUT;
16311631
if (cdb2_sockpool_send_timeoutms_set_from_env)
@@ -1649,7 +1649,7 @@ static void read_comdb2db_environment_cfg(cdb2_hndl_tp *hndl, const char *comdb2
16491649
pthread_mutex_unlock(&cdb2_sockpool_mutex);
16501650
}
16511651

1652-
static void only_read_config(cdb2_hndl_tp *); /* FORWARD */
1652+
static void only_read_config(cdb2_hndl_tp *, int *); /* FORWARD */
16531653

16541654
static int cdb2_max_room_num = 0;
16551655
static int cdb2_has_room_distance = 0;
@@ -1814,8 +1814,8 @@ static void read_comdb2db_cfg(cdb2_hndl_tp *hndl, SBUF2 *s, const char *comdb2db
18141814
if (!cdb2_default_cluster_set_from_env && strcasecmp("default_type", tok) == 0) {
18151815
tok = strtok_r(NULL, " :,", &last);
18161816
if (tok) {
1817-
if (hndl && (strcasecmp(hndl->cluster, "default") == 0)) {
1818-
strncpy(hndl->cluster, tok, sizeof(cdb2_default_cluster) - 1);
1817+
if (hndl && (strcasecmp(hndl->type, "default") == 0)) {
1818+
strncpy(hndl->type, tok, sizeof(cdb2_default_cluster) - 1);
18191819
} else if (!hndl) {
18201820
strncpy(cdb2_default_cluster, tok, sizeof(cdb2_default_cluster) - 1);
18211821
}
@@ -2240,6 +2240,11 @@ static int read_available_comdb2db_configs(cdb2_hndl_tp *hndl, char comdb2db_hos
22402240
#ifdef CDB2API_TEST
22412241
}
22422242
#endif
2243+
2244+
if (hndl && strcasecmp(hndl->type, "default") == 0 && cdb2_default_cluster[0] != '\0') {
2245+
strncpy(hndl->type, cdb2_default_cluster, sizeof(hndl->type) - 1);
2246+
}
2247+
22432248
pthread_mutex_unlock(&cdb2_cfg_lock);
22442249
return 0;
22452250
}
@@ -3072,6 +3077,20 @@ int cdb2_socket_pool_get_fd(cdb2_hndl_tp *hndl, const char *typestr, int dbnum,
30723077
return fd;
30733078
}
30743079

3080+
static int typestr_type_is_default(const char *typestr)
3081+
{
3082+
const int TYPESTR_TIER_IDX = 2;
3083+
const char *type_start = typestr;
3084+
for (int i = 0; i < TYPESTR_TIER_IDX; i++) {
3085+
type_start = strchr(type_start, '/');
3086+
assert(type_start);
3087+
type_start++;
3088+
}
3089+
const char *const type_end = strchr(type_start, '/');
3090+
assert(type_end);
3091+
return strncmp(type_start, "default", type_end - type_start) == 0;
3092+
}
3093+
30753094
/* Get the sbuf of a socket matching the given type string from
30763095
* the pool. Returns NULL if none is available or the sbuf on
30773096
* success. */
@@ -3089,6 +3108,13 @@ SBUF2 *cdb2_socket_pool_get(cdb2_hndl_tp *hndl, const char *typestr, int dbnum,
30893108
}
30903109
#endif
30913110

3111+
#ifdef CDB2API_TEST
3112+
if (typestr_type_is_default(typestr)) {
3113+
fprintf(stderr, "%s: ERR: Did not expect default type in typestring\n", __func__);
3114+
abort();
3115+
}
3116+
#endif
3117+
30923118
if (was_from_local_cache) {
30933119
*was_from_local_cache = 0;
30943120
sb = local_connection_cache_get(hndl, typestr);
@@ -3112,6 +3138,13 @@ void cdb2_socket_pool_donate_ext(const cdb2_hndl_tp *hndl, const char *typestr,
31123138
{
31133139
if (log_calls)
31143140
fprintf(stderr, "%p> %s(%s,%d): fd=%d\n", (void *)pthread_self(), __func__, typestr, dbnum, fd);
3141+
#ifdef CDB2API_TEST
3142+
if (typestr_type_is_default(typestr)) {
3143+
fprintf(stderr, "%s: ERR: Did not expect default type in typestring\n", __func__);
3144+
abort();
3145+
}
3146+
#endif
3147+
31153148
int enabled = 0;
31163149
int sockpool_fd = -1;
31173150
int sp_generation = -1;
@@ -7769,9 +7802,17 @@ static int cdb2_dbinfo_query(cdb2_hndl_tp *hndl, const char *type, const char *d
77697802
return rc;
77707803
}
77717804

7772-
static inline void only_read_config(cdb2_hndl_tp *hndl)
7805+
static inline void only_read_config(cdb2_hndl_tp *hndl, int *default_err)
77737806
{
77747807
read_available_comdb2db_configs(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
7808+
if (default_err && strcasecmp(hndl->type, "default") == 0) {
7809+
if (cdb2_default_cluster[0] != '\0') {
7810+
strncpy(hndl->type, cdb2_default_cluster, sizeof(hndl->type) - 1);
7811+
} else {
7812+
snprintf(hndl->errstr, sizeof(hndl->errstr), "No default_type entry in comdb2db config.\n");
7813+
*default_err = -1;
7814+
}
7815+
}
77757816
set_cdb2_timeouts(hndl);
77767817
}
77777818

@@ -7797,6 +7838,11 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
77977838
&comdb2db_num, hndl->dbname, hndl->hosts, &(hndl->num_hosts), &hndl->dbnum, 0, hndl->shards,
77987839
&(hndl->num_shards));
77997840

7841+
if (strcasecmp(hndl->type, "default") == 0) {
7842+
sprintf(hndl->errstr, "No default_type entry in comdb2db config.\n");
7843+
return -1;
7844+
}
7845+
78007846
/* Before database destination discovery */
78017847
cdb2_event *e = NULL;
78027848
void *callbackrc;
@@ -7836,20 +7882,7 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
78367882
strcpy(comdb2db_name, cdb2_comdb2dbname);
78377883
}
78387884

7839-
if (strcasecmp(hndl->cluster, "default") == 0) {
7840-
if (cdb2_default_cluster[0] == '\0') {
7841-
sprintf(hndl->errstr, "cdb2_get_dbhosts: no default_type "
7842-
"entry in comdb2db config.");
7843-
rc = -1;
7844-
goto after_callback;
7845-
}
7846-
strncpy(hndl->cluster, cdb2_default_cluster, sizeof(hndl->cluster) - 1);
7847-
if (cdb2cfg_override || (cdb2_use_env_vars && default_type_override_env)) {
7848-
strncpy(hndl->type, cdb2_default_cluster, sizeof(hndl->type) - 1);
7849-
}
7850-
}
7851-
7852-
if (strcasecmp(hndl->cluster, "local") == 0) {
7885+
if (strcasecmp(hndl->type, "local") == 0) {
78537886
hndl->num_hosts = 1;
78547887
strcpy(hndl->hosts[0], "localhost");
78557888
hndl->flags |= CDB2_DIRECT_CPU;
@@ -7915,7 +7948,7 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
79157948
if (i == master)
79167949
continue;
79177950
rc = comdb2db_get_dbhosts(hndl, comdb2db_name, comdb2db_num, comdb2db_hosts[i], comdb2db_ports[i],
7918-
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->cluster, &hndl->dbnum,
7951+
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->type, &hndl->dbnum,
79197952
&hndl->num_hosts_sameroom, num_retry, use_bmsd, hndl->shards, &hndl->num_shards,
79207953
&hndl->num_shards_sameroom);
79217954
if (rc == 0 || time(NULL) >= max_time) {
@@ -7928,7 +7961,7 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
79287961
}
79297962
if (rc == -1 && time(NULL) < max_time) {
79307963
rc = comdb2db_get_dbhosts(hndl, comdb2db_name, comdb2db_num, comdb2db_hosts[master], comdb2db_ports[master],
7931-
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->cluster, &hndl->dbnum,
7964+
hndl->hosts, &hndl->num_hosts, hndl->dbname, hndl->type, &hndl->dbnum,
79327965
&hndl->num_hosts_sameroom, num_retry, use_bmsd, hndl->shards, &hndl->num_shards,
79337966
&hndl->num_shards_sameroom);
79347967
}
@@ -7943,9 +7976,10 @@ static int cdb2_get_dbhosts(cdb2_hndl_tp *hndl)
79437976
goto after_callback;
79447977
}
79457978
if (hndl->num_hosts == 0) {
7946-
sprintf(hndl->errstr, "cdb2_get_dbhosts: comdb2db has no entry of "
7947-
"db %s of cluster type %s.",
7948-
hndl->dbname, hndl->cluster);
7979+
sprintf(hndl->errstr,
7980+
"cdb2_get_dbhosts: comdb2db has no entry of "
7981+
"db %s of cluster type %s.",
7982+
hndl->dbname, hndl->type);
79497983
rc = -1;
79507984
goto after_callback;
79517985
}
@@ -8084,10 +8118,10 @@ static void after_discovery(cdb2_hndl_tp *hndl)
80848118
}
80858119
}
80868120

8087-
static int get_connection_int(cdb2_hndl_tp *hndl)
8121+
static int get_connection_int(cdb2_hndl_tp *hndl, int *err)
80888122
{
8089-
only_read_config(hndl);
8090-
if (get_dbinfo)
8123+
only_read_config(hndl, err);
8124+
if (get_dbinfo || *err)
80918125
return -1;
80928126
before_discovery(hndl);
80938127
SBUF2 *sb = sockpool_get(hndl);
@@ -8097,7 +8131,7 @@ static int get_connection_int(cdb2_hndl_tp *hndl)
80978131
return init_connection(hndl, sb);
80988132
}
80998133

8100-
static int get_connection(cdb2_hndl_tp *hndl)
8134+
static int get_connection(cdb2_hndl_tp *hndl, int *err)
81018135
{
81028136
if (hndl->is_admin || (hndl->flags & CDB2_MASTER)) // don't grab from sockpool
81038137
return -1;
@@ -8117,7 +8151,7 @@ static int get_connection(cdb2_hndl_tp *hndl)
81178151
} else if (get_dbinfo || sockpool_enabled == -1 || cdb2cfg_override) {
81188152
return -1;
81198153
}
8120-
int rc = get_connection_int(hndl);
8154+
int rc = get_connection_int(hndl, err);
81218155
return rc;
81228156
}
81238157

@@ -8174,7 +8208,7 @@ static int configure_from_literal(cdb2_hndl_tp *hndl, const char *type)
81748208
assert(type_copy[0] == '@');
81758209
char *s = type_copy + 1; // advance past the '@'
81768210

8177-
only_read_config(hndl);
8211+
only_read_config(hndl, NULL); // don't care about default here?
81788212

81798213
char *machine;
81808214
machine = strtok_r(s, ",", &eomachine);
@@ -8522,8 +8556,7 @@ static cdb2_ssl_sess *cdb2_get_ssl_sessions(cdb2_hndl_tp *hndl)
85228556
return NULL;
85238557

85248558
for (pos = cdb2_ssl_sess_cache.next; pos != NULL; pos = pos->next) {
8525-
if (strcasecmp(hndl->dbname, pos->dbname) == 0 &&
8526-
strcasecmp(hndl->cluster, pos->cluster) == 0) {
8559+
if (strcasecmp(hndl->dbname, pos->dbname) == 0 && strcasecmp(hndl->type, pos->cluster) == 0) {
85278560
if (!pos->ref) {
85288561
pos->ref = 1;
85298562
break;
@@ -8562,8 +8595,8 @@ static int cdb2_add_ssl_session(cdb2_hndl_tp *hndl)
85628595
return ENOMEM;
85638596
strncpy(hndl->sess->dbname, hndl->dbname, sizeof(hndl->dbname) - 1);
85648597
hndl->sess->dbname[sizeof(hndl->dbname) - 1] = '\0';
8565-
strncpy(hndl->sess->cluster, hndl->cluster, sizeof(hndl->cluster) - 1);
8566-
hndl->sess->cluster[sizeof(hndl->cluster) - 1] = '\0';
8598+
strncpy(hndl->sess->cluster, hndl->type, sizeof(hndl->type) - 1);
8599+
hndl->sess->cluster[sizeof(hndl->type) - 1] = '\0';
85678600
hndl->sess->ref = 1;
85688601
hndl->sess->sessobj = NULL;
85698602

@@ -8618,7 +8651,6 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
86188651
hndl->gbl_event_version = calloc(1, sizeof(*hndl->gbl_event_version));
86198652
TAILQ_INIT(&hndl->queries);
86208653
strncpy(hndl->dbname, dbname, sizeof(hndl->dbname) - 1);
8621-
strncpy(hndl->cluster, type, sizeof(hndl->cluster) - 1);
86228654
strncpy(hndl->type, type, sizeof(hndl->type) - 1);
86238655
hndl->flags = flags;
86248656
hndl->dbnum = 1;
@@ -8659,8 +8691,7 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
86598691
fprintf(stderr, "WARNING: %s:Value of %s is not valid. Using value '%s'.\n", __func__,
86608692
COMDB2_CONFIG_DB_DEFAULT_TYPE, db_default_type);
86618693
}
8662-
strncpy(hndl->cluster, db_default_type, sizeof(hndl->cluster) - 1);
8663-
strncpy(hndl->type, hndl->cluster, sizeof(hndl->type) - 1);
8694+
strncpy(hndl->type, db_default_type, sizeof(hndl->type) - 1);
86648695
hndl->db_default_type_override_env = 1;
86658696
}
86668697
}
@@ -8699,7 +8730,9 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
86998730

87008731
if (hndl->flags & CDB2_DIRECT_CPU) {
87018732
/* Get defaults from comdb2db.cfg */
8702-
only_read_config(hndl);
8733+
only_read_config(hndl, &rc);
8734+
if (rc)
8735+
goto out;
87038736
hndl->got_dbinfo = 1;
87048737
hndl->num_hosts = 1;
87058738
strncpy(hndl->hosts[0], type, sizeof(hndl->hosts[0]) - 1);
@@ -8727,7 +8760,10 @@ int cdb2_open(cdb2_hndl_tp **handle, const char *dbname, const char *type,
87278760
hndl->sockpool_enabled = db_host_info ? -1 : 0;
87288761
}
87298762

8730-
if (get_connection(hndl) != 0) {
8763+
const int should_get_dbinfo = get_connection(hndl, &rc);
8764+
if (rc)
8765+
goto out;
8766+
if (should_get_dbinfo) {
87318767
hndl->got_dbinfo = 1;
87328768
rc = cdb2_get_dbhosts(hndl);
87338769
}
@@ -9047,7 +9083,7 @@ static void *cdb2_invoke_callback(cdb2_hndl_tp *hndl, cdb2_event *e, int argc,
90479083
void *rc;
90489084
int state;
90499085
char *fp;
9050-
const char *dbtype = (hndl == NULL) ? NULL : hndl->type;
9086+
const char *dbtype;
90519087

90529088
/* Fast return if no arguments need to be passed to the callback. */
90539089
if (e->argc == 0)
@@ -9069,6 +9105,12 @@ static void *cdb2_invoke_callback(cdb2_hndl_tp *hndl, cdb2_event *e, int argc,
90699105
hostname = hndl->hosts[hndl->connected_host];
90709106
port = hndl->ports[hndl->connected_host];
90719107
}
9108+
9109+
if (hndl == NULL || strcasecmp(hndl->type, "default") == 0)
9110+
dbtype = cdb2_default_cluster;
9111+
else
9112+
dbtype = hndl->type;
9113+
90729114
rc = 0;
90739115
state = 0;
90749116

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)