Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{177663687}: Exposing cluster type during lifecycle events #4947

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cdb2api/cdb2api.c
Original file line number Diff line number Diff line change
Expand Up @@ -7097,6 +7097,7 @@ static void *cdb2_invoke_callback(cdb2_hndl_tp *hndl, cdb2_event *e, int argc,
void *rc;
int state;
char *fp;
const char *dbtype = (hndl == NULL) ? NULL : hndl->type;

/* Fast return if no arguments need to be passed to the callback. */
if (e->argc == 0)
Expand Down Expand Up @@ -7150,6 +7151,9 @@ static void *cdb2_invoke_callback(cdb2_hndl_tp *hndl, cdb2_event *e, int argc,
case CDB2_FINGERPRINT:
fp = va_arg(ap, char *);
break;
case CDB2_DBTYPE:
dbtype = va_arg(ap, char *);
break;
default:
(void)va_arg(ap, void *);
break;
Expand Down Expand Up @@ -7177,6 +7181,10 @@ static void *cdb2_invoke_callback(cdb2_hndl_tp *hndl, cdb2_event *e, int argc,
break;
case CDB2_FINGERPRINT:
argv[i] = (void *)fp;
break;
case CDB2_DBTYPE:
argv[i] = (void *)dbtype;
break;
default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion cdb2api/cdb2api.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ typedef enum cdb2_event_arg {
CDB2_SQL,
CDB2_RETURN_VALUE,
CDB2_QUERY_STATE,
CDB2_FINGERPRINT
CDB2_FINGERPRINT,
CDB2_DBTYPE
} cdb2_event_arg;

typedef struct cdb2_event cdb2_event;
Expand Down
38 changes: 19 additions & 19 deletions docs/pages/programming/c_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,25 +631,25 @@ where `cb_hndl` is the handle upon which the event is fired.

Besides the user argument, one can request additional arguments by setting `argc` to the number of the arguments, followed by the argument types. The arguments will be passed to `cb` in `argv`. The table below lists the argument types.

|Event Type|`CDB2_HOSTNAME`|`CDB2_PORT`|`CDB2_SQL`|`CDB2_RETURN_VALUE`|`CDB2_FINGERPRINT`|
|---|---|---|---|---|---|
| `CDB2_BEFORE_CONNECT` | The hostname to connect to | The port to connect to | N/A | N/A | N/A |
| `CDB2_AFTER_CONNECT` | The hostname to connect to | The port to connect to | N/A | file descriptor | N/A |
| `CDB2_BEFORE_PMUX` | The server hostname | The pmux port | N/A | N/A | N/A |
| `CDB2_AFTER_PMUX` | The server hostname | The pmux port | N/A | The database port | N/A |
| `CDB2_BEFORE_DBINFO` | The server hostname | The database port | N/A | N/A | N/A |
| `CDB2_AFTER_DBINFO` | The server hostname | The database port | N/A | 0 on success; Non-zero on failure | N/A |
| `CDB2_BEFORE_SEND_QUERY` | The server hostname | The database port | The SQL query | N/A | N/A |
| `CDB2_AFTER_SEND_QUERY` | The server hostname | The database port | The SQL query | 0 on success; Non-zero on failure | N/A |
| `CDB2_BEFORE_READ_RECORD` | The server hostname | The database port | N/A | N/A | N/A |
| `CDB2_AFTER_READ_RECORD` | The server hostname | The database port | N/A | 0 on success; Non-zero on failure | N/A |
| `CDB2_AT_ENTER_RUN_STATEMENT` | The server hostname | The database port | The SQL query | See [cdb2api errors](#cdb2api-errors) | N/A |
| `CDB2_AT_EXIT_RUN_STATEMENT` | The server hostname | The database port | The SQL query | See [cdb2api errors](#cdb2api-errors) | Fingerprint of the SQL query |
| `CDB2_AT_ENTER_NEXT_RECORD` | The server hostname | The database port | N/A | See [cdb2api errors](#cdb2api-errors) | Fingerprint of the SQL query |
| `CDB2_AT_EXIT_NEXT_RECORD` | The server hostname | The database port | N/A | See [cdb2api errors](#cdb2api-errors) | Fingerprint of the SQL query |
| `CDB2_AT_DISCOVERY` | N/A | N/A | N/A | N/A | N/A |
| `CDB2_AT_OPEN` | N/A | N/A | N/A | See [cdb2api errors](#cdb2api-errors) | N/A |
| `CDB2_AT_CLOSE` | N/A | N/A | N/A | See [cdb2api errors](#cdb2api-errors) | N/A |
|Event Type|`CDB2_HOSTNAME`|`CDB2_PORT`|`CDB2_SQL`|`CDB2_RETURN_VALUE`|`CDB2_FINGERPRINT`|`CDB2_DBTYPE`|
|---|---|---|---|---|---|---|
| `CDB2_BEFORE_CONNECT` | The hostname to connect to | The port to connect to | N/A | N/A | N/A | Resolved dbtype |
| `CDB2_AFTER_CONNECT` | The hostname to connect to | The port to connect to | N/A | file descriptor | N/A | Resolved dbtype |
| `CDB2_BEFORE_PMUX` | The server hostname | The pmux port | N/A | N/A | N/A | Resolved dbtype |
| `CDB2_AFTER_PMUX` | The server hostname | The pmux port | N/A | The database port | N/A | Resolved dbtype |
| `CDB2_BEFORE_DBINFO` | The server hostname | The database port | N/A | N/A | N/A | Resolved dbtype |
| `CDB2_AFTER_DBINFO` | The server hostname | The database port | N/A | 0 on success; Non-zero on failure | N/A | Resolved dbtype |
| `CDB2_BEFORE_SEND_QUERY` | The server hostname | The database port | The SQL query | N/A | N/A | Resolved dbtype |
| `CDB2_AFTER_SEND_QUERY` | The server hostname | The database port | The SQL query | 0 on success; Non-zero on failure | N/A | Resolved dbtype |
| `CDB2_BEFORE_READ_RECORD` | The server hostname | The database port | N/A | N/A | N/A | Resolved dbtype |
| `CDB2_AFTER_READ_RECORD` | The server hostname | The database port | N/A | 0 on success; Non-zero on failure | N/A | Resolved dbtype |
| `CDB2_AT_ENTER_RUN_STATEMENT` | The server hostname | The database port | The SQL query | See [cdb2api errors](#cdb2api-errors) | N/A | Resolved dbtype |
| `CDB2_AT_EXIT_RUN_STATEMENT` | The server hostname | The database port | The SQL query | See [cdb2api errors](#cdb2api-errors) | Fingerprint of the SQL query | Resolved dbtype |
| `CDB2_AT_ENTER_NEXT_RECORD` | The server hostname | The database port | N/A | See [cdb2api errors](#cdb2api-errors) | Fingerprint of the SQL query | Resolved dbtype |
| `CDB2_AT_EXIT_NEXT_RECORD` | The server hostname | The database port | N/A | See [cdb2api errors](#cdb2api-errors) | Fingerprint of the SQL query | Resolved dbtype |
| `CDB2_AT_DISCOVERY` | N/A | N/A | N/A | N/A | N/A | Unresolved dbtype |
| `CDB2_AT_OPEN` | N/A | N/A | N/A | See [cdb2api errors](#cdb2api-errors) | N/A | Resolved dbtype |
| `CDB2_AT_CLOSE` | N/A | N/A | N/A | See [cdb2api errors](#cdb2api-errors) | N/A | Resolved dbtype |

Return Value:

Expand Down
6 changes: 6 additions & 0 deletions tests/api_events.test/expected
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ SQL is SELECT "You may say I'm a dreamer, but I'm not the only one."
FP is 4f16a8ec9db90f803e406659938b2602
RC is 0
RC is 1
====== VERIFYING DBTYPE ARG ======
DBTYPE is unresolved
DBTYPE is resolved
DBTYPE is resolved
DBTYPE is resolved
DBTYPE is example.com
51 changes: 50 additions & 1 deletion tests/tools/api_events.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#include <cdb2api.h>
Expand Down Expand Up @@ -44,6 +46,24 @@ static void *my_hostname_hook(cdb2_hndl_tp *hndl, void *user_arg, int argc, void
return NULL;
}

static char *resolved_dbtype = NULL;
static void *my_dbtype_hook(cdb2_hndl_tp *hndl, void *user_arg, int argc, void **argv)
{
char *got = (char *)argv[0];
if (strcasecmp(got, "default") != 0) {
free(resolved_dbtype);
resolved_dbtype = strdup(got);
}

if (strcasecmp(got, "default") == 0)
puts("DBTYPE is unresolved");
else if (strcasecmp(got, "local") == 0 || strcasecmp(got, "testsuite") == 0)
puts("DBTYPE is resolved");
else
printf("DBTYPE is %s\n", got);
return NULL;
}

static cdb2_event *init_once_event;

static void register_once(void)
Expand Down Expand Up @@ -143,7 +163,7 @@ static int TEST_modify_rc_event(const char *db, const char *tier)
cdb2_event *e;
cdb2_hndl_tp *hndl = NULL;

e = cdb2_register_event(NULL, CDB2_BEFORE_PMUX, CDB2_OVERWRITE_RETURN_VALUE, my_overwrite_rc_hook, NULL, 0, NULL);
e = cdb2_register_event(NULL, CDB2_BEFORE_SEND_QUERY, CDB2_OVERWRITE_RETURN_VALUE, my_overwrite_rc_hook, NULL, 0, NULL);
cdb2_open(&hndl, db, tier, 0);
rc = cdb2_run_statement(hndl, "SELECT 1");
if (rc == 0)
Expand Down Expand Up @@ -226,6 +246,30 @@ static int TEST_run_stmt_next_record_events(const char *db, const char *tier)
return 0;
}

static int TEST_dbtype_arg(const char *db, const char *tier)
{
cdb2_hndl_tp *h;
cdb2_event *e1, *e2;

e1 = cdb2_register_event(NULL, CDB2_BEFORE_DISCOVERY, 0, my_dbtype_hook, NULL, 1, CDB2_DBTYPE);
e2 = cdb2_register_event(NULL, CDB2_AT_OPEN, 0, my_dbtype_hook, NULL, 1, CDB2_DBTYPE);

/* default */
cdb2_open(&h, db, tier, 0);
cdb2_close(h);
/* actual */
cdb2_open(&h, db, resolved_dbtype, 0);
cdb2_close(h);
/* direct-cpu */
cdb2_open(&h, db, "example.com", CDB2_DIRECT_CPU);
cdb2_close(h);

cdb2_unregister_event(NULL, e1);
cdb2_unregister_event(NULL, e2);

return 0;
}

int main(int argc, char **argv)
{
char *conf = getenv("CDB2_CONFIG");
Expand Down Expand Up @@ -276,5 +320,10 @@ int main(int argc, char **argv)
if (rc != 0)
return rc;

puts("====== VERIFYING DBTYPE ARG ======");
rc = TEST_dbtype_arg(db, tier);
if (rc != 0)
return rc;

return 0;
}