Skip to content

Commit 38d94de

Browse files
committed
More space for cnonce in hndl (match bb), save strlen
Signed-off-by: Salil Chandra <schandra107@bloomberg.net>
1 parent 4d8a007 commit 38d94de

2 files changed

Lines changed: 15 additions & 20 deletions

File tree

cdb2api/cdb2api.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,10 +4738,10 @@ static int cdb2_send_query(cdb2_hndl_tp *hndl, cdb2_hndl_tp *event_hndl, COMDB2B
47384738
features[n_features++] = CDB2_CLIENT_FEATURES__ALLOW_MASTER_EXEC;
47394739
}
47404740

4741-
if (hndl && hndl->cnonce[0]) { /* Have a query id associated with each transaction/query */
4741+
if (hndl && hndl->cnonce_len > 0) { /* Have a query id associated with each transaction/query */
47424742
sqlquery.has_cnonce = 1;
47434743
sqlquery.cnonce.data = (uint8_t *)hndl->cnonce;
4744-
sqlquery.cnonce.len = strlen(hndl->cnonce);
4744+
sqlquery.cnonce.len = hndl->cnonce_len;
47454745
}
47464746

47474747
CDB2SQLQUERY__Snapshotinfo snapshotinfo;
@@ -5286,12 +5286,20 @@ int cdb2_close(cdb2_hndl_tp *hndl)
52865286
return rc;
52875287
}
52885288

5289-
void next_cnonce(cdb2_hndl_tp *hndl)
5289+
static void make_random_str(char *str, size_t str_sz, int *len)
52905290
{
52915291
do_init_once(1);
52925292
struct timeval tv;
52935293
gettimeofday(&tv, NULL);
5294-
sprintf(hndl->cnonce, "%d-%d-%lld-%d", _MACHINE_ID, _PID, (long long)tv.tv_usec, cdb2_random_int());
5294+
int rc = snprintf(str, str_sz, "%d-%d-%lld-%d", _MACHINE_ID, _PID, (long long)tv.tv_usec, cdb2_random_int());
5295+
if (rc < 0 || (size_t)rc >= str_sz) {
5296+
// should not be possible to go here. 100 bytes is more than enough to fit the string for all numbers.
5297+
LOG_CALL("%s: snprintf failed or truncated (rc=%d str_sz=%zu)\n", __func__, rc, str_sz);
5298+
str[0] = '\0';
5299+
*len = 0;
5300+
} else {
5301+
*len = rc;
5302+
}
52955303
return;
52965304
}
52975305

@@ -6132,7 +6140,7 @@ static int cdb2_run_statement_typed_int(cdb2_hndl_tp *hndl, const char *sql, int
61326140

61336141
if (!hndl->in_trans) { /* only one cnonce for a transaction. */
61346142
clear_snapshot_info(hndl, __LINE__);
6135-
next_cnonce(hndl);
6143+
make_random_str(hndl->cnonce, sizeof(hndl->cnonce), &hndl->cnonce_len);
61366144
}
61376145
hndl->retry_all = 1;
61386146
int run_last = 1;

cdb2api/cdb2api_hndl.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,6 @@ struct cdb2_event {
4949
cdb2_event_arg argv[1];
5050
};
5151

52-
/* A cnonce is composed of
53-
- 32 bits of machine ID
54-
- 32 bits of process PID
55-
- 32 or 64 bits of handle address
56-
- 52 bits for the epoch time in microseconds
57-
- 12 bits for the sequence number
58-
59-
The allocation allows a handle to run at a maximum transaction rate of
60-
4096 txn/us (~4 billion transactions per second) till September 17, 2112.
61-
62-
See next_cnonce() for details. */
63-
#define CNONCE_STR_FMT "%lx-%x-%llx-"
64-
#define CNONCE_STR_SZ 60 /* 16 + 1 + 8 + 1 + 16 + 1 + 16 + 1 (NUL) */
65-
6652
struct cdb2_stmt_types;
6753

6854
struct cdb2_query {
@@ -117,7 +103,8 @@ struct cdb2_hndl {
117103
int connected_host;
118104
int flags;
119105
char errstr[1024];
120-
char cnonce[CNONCE_STR_SZ];
106+
char cnonce[100];
107+
int cnonce_len;
121108
char *sql;
122109
char partial_sql[64];
123110
int ntypes;

0 commit comments

Comments
 (0)