Skip to content

Commit eecc2a1

Browse files
committed
ucancel cnonce simple test working
1 parent fa6bfec commit eecc2a1

File tree

5 files changed

+147
-12
lines changed

5 files changed

+147
-12
lines changed

db/process_message.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ static const char *HELP_SQL[] = {
237237
"hist - show recently run statements",
238238
"cancel N - cancel running statement with id N",
239239
"cancelcnonce N - cancel running statement with cnonce N",
240+
"ucancel cnonce N - cancel running statement with unified uuid N (per comdb2_connections)",
241+
"ucancel fp N - cancel running statement with fingerprint N (per comdb2_connections)",
242+
"ucancel running - cancel all running statement (leaves queued statements intact)",
243+
"ucancel queued - cancel all queued statement (leaves running statements intact)",
244+
"ucancel all - cancel all queued and running statements",
240245
"wrtimeout N - set write timeout in ms",
241246
"help - this information",
242247
NULL,
@@ -3118,6 +3123,34 @@ int process_command(struct dbenv *dbenv, char *line, int lline, int st)
31183123
cancel_sql_statement_with_cnonce(cnonce);
31193124
free(cnonce);
31203125
}
3126+
} else if (tokcmp(tok, ltok, "ucancel") == 0) {
3127+
char *uuid = NULL;
3128+
enum ucancel_type t = UCANCEL_INV;
3129+
tok = segtok(line, lline, &st, &ltok);
3130+
if (ltok) {
3131+
if (tokcmp(tok, ltok, "all") == 0)
3132+
t = UCANCEL_ALL;
3133+
else if (tokcmp(tok, ltok, "running") == 0)
3134+
t = UCANCEL_RUN;
3135+
else if (tokcmp(tok, ltok, "queued") == 0)
3136+
t = UCANCEL_QUE;
3137+
else if (tokcmp(tok, ltok, "cnonce") == 0)
3138+
t = UCANCEL_CNO;
3139+
else if (tokcmp(tok, ltok, "fp") == 0)
3140+
t = UCANCEL_FPT;
3141+
3142+
if (t == UCANCEL_CNO || t == UCANCEL_FPT) {
3143+
tok = segtok(line, lline, &st, &ltok);
3144+
if (ltok)
3145+
uuid = tokdup(tok, ltok);
3146+
else
3147+
t = UCANCEL_INV;
3148+
}
3149+
if (t != UCANCEL_INV)
3150+
ucancel_sql_statements(t, uuid);
3151+
}
3152+
if (t == UCANCEL_INV)
3153+
logmsg(LOGMSG_ERROR, "Usage sql ucancel [all|queued|running|fp N|cnonce N]");
31213154
} else if (tokcmp(tok, ltok, "help") == 0) {
31223155
print_help_page(HELP_SQL);
31233156
} else if (tokcmp(tok, ltok, "debug") == 0) {

db/sql.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,17 @@ typedef struct sqlclntstate_fdb {
322322
int failed_heartbeats; /* used to signal failed communication with remotes */
323323
} sqlclntstate_fdb_t;
324324

325+
enum ucancel_type {
326+
UCANCEL_INV = 0,
327+
UCANCEL_ALL = 1, /* both queued and running */
328+
UCANCEL_RUN = 2, /* running only */
329+
UCANCEL_QUE = 4, /* queued only */
330+
UCANCEL_CNO = 8, /* filter by cnonce */
331+
UCANCEL_FPT = 16 /* filter by fp */
332+
};
333+
334+
int ucancel_sql_statements(enum ucancel_type type, char *uuid);
335+
325336
CurRange *currange_new();
326337
#define CURRANGEARR_INIT_CAP 2
327338
void currangearr_init(CurRangeArr *arr);

db/sqlglue.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9701,6 +9701,72 @@ void cancel_sql_statement_with_cnonce(const char *cnonce)
97019701
logmsg(LOGMSG_USER, "Query with cnonce %s not found (finished?)\n", cnonce);
97029702
}
97039703

9704+
static int _ucancel_sql_statements_cno(char *uuidstr)
9705+
{
9706+
struct sql_thread *thd;
9707+
int rc = -1;
9708+
9709+
uuid_t uuid;
9710+
9711+
if (uuid_parse(uuidstr, uuid)) {
9712+
logmsg(LOGMSG_ERROR, "Failed to parse uuid \"%s\"\n", uuidstr);
9713+
return rc;
9714+
}
9715+
9716+
Pthread_mutex_lock(&gbl_sql_lock);
9717+
LISTC_FOR_EACH(&thedb->sql_threads, thd, lnk) {
9718+
if (thd->clnt && memcmp(thd->clnt->unifieduuid, uuid, sizeof(uuid_t)) == 0) {
9719+
logmsg(LOGMSG_ERROR, "Cancelling sql %s\n", thd->clnt->sql);
9720+
thd->stop_this_statement = 1;
9721+
rc = 0;
9722+
break;
9723+
}
9724+
}
9725+
Pthread_mutex_unlock(&gbl_sql_lock);
9726+
if (rc)
9727+
logmsg(LOGMSG_ERROR, "Sql cnonce \"%s\" not found\n", uuidstr);
9728+
return rc;
9729+
}
9730+
9731+
static int _ucancel_sql_statements_run(void)
9732+
{
9733+
struct sql_thread *thd;
9734+
9735+
Pthread_mutex_lock(&gbl_sql_lock);
9736+
LISTC_FOR_EACH(&thedb->sql_threads, thd, lnk) {
9737+
if (thd->clnt)
9738+
thd->stop_this_statement = 1;
9739+
}
9740+
Pthread_mutex_unlock(&gbl_sql_lock);
9741+
9742+
return 0;
9743+
}
9744+
9745+
/*
9746+
* "Unified" sql statements cancel routine
9747+
* MODES:
9748+
* type == UCANCEL_ALL #cancel all running and queued statements
9749+
* type == UCANCEL_RUN #cancel all running statements
9750+
* type == UCANCEL_QUE #cancel all queued statements
9751+
* type == UCANCEL_CNO #cancel one running statement with cnonce = "uuid"
9752+
* type == UCANCEL_FPT #cancel all running statement with fingerprint = "uuid"
9753+
*
9754+
*/
9755+
int ucancel_sql_statements(enum ucancel_type type, char *uuid) {
9756+
switch(type) {
9757+
case UCANCEL_CNO:
9758+
return _ucancel_sql_statements_cno(uuid);
9759+
case UCANCEL_RUN:
9760+
return _ucancel_sql_statements_run();
9761+
case UCANCEL_ALL:
9762+
case UCANCEL_QUE:
9763+
case UCANCEL_FPT:
9764+
logmsg(LOGMSG_ERROR, "%d unimplemented\n", type);
9765+
return -1;
9766+
}
9767+
return 0;
9768+
}
9769+
97049770
void sql_dump_running_statements(void)
97059771
{
97069772
struct sql_thread *thd;

lua/syssp.c.in

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,39 @@ static int db_send(Lua L)
491491

492492
static int db_unified_cancel(Lua L)
493493
{
494-
char *uuid;
494+
char *type;
495+
char *uuid = NULL;
495496
char cmd[256];
496497

497-
if (!lua_isstring(L, 1))
498-
return luaL_error(L, "Expected string uuid argument");
499-
uuid = (char*) lua_tostring(L, -1);
498+
int nargs = lua_gettop(L);
499+
/* type */
500+
if (nargs == 2) {
501+
if (!lua_isstring(L, -2))
502+
return luaL_error(L, "Expected string type argument: all|queued|running|cnonce|fp");
503+
type = (char*) lua_tostring(L, -2);
504+
if (strncasecmp(type, "cnonce", sizeof("cnonce")) != 0 &&
505+
strncasecmp(type, "fp", sizeof("fp")) != 0) {
506+
return luaL_error(L, "Extra argument present\n");
507+
}
508+
} else {
509+
if (!lua_isstring(L, -1))
510+
return luaL_error(L, "Expected string type argument: all|queued|running|cnonce|fp");
511+
type = (char*) lua_tostring(L, -1);
512+
if (strncasecmp(type, "all", sizeof("all")) != 0 &&
513+
strncasecmp(type, "running", sizeof("running")) != 0 &&
514+
strncasecmp(type, "queued", sizeof("queued")) != 0) {
515+
return luaL_error(L, "Expected string type argument: all|queued|running|cnonce|fp");
516+
}
517+
}
518+
519+
if (nargs == 2) {
520+
if (!lua_isstring(L, -1))
521+
return luaL_error(L, "Wrong type uuid for %s type", type);
522+
uuid = (char*) lua_tostring(L, -1);
523+
}
500524
lua_settop(L, 0);
501525

502-
snprintf(cmd, sizeof(cmd), "cancel %s\n", uuid);
526+
snprintf(cmd, sizeof(cmd), "sql ucancel %s%s%s\n", type, uuid ? " ": "", uuid ? uuid : "");
503527

504528
return _db_process_command(L, cmd, "result");
505529
}
@@ -795,7 +819,7 @@ static struct sp_source syssps[] = {
795819
}
796820
,{
797821
"sys.cmd.cancel",
798-
"local function main(cmd)\n"
822+
"local function main(cmd, uuid)\n"
799823
" local schema = {\n"
800824
" { 'string', 'result' },\n"
801825
" }\n"
@@ -804,7 +828,7 @@ static struct sp_source syssps[] = {
804828
" db:column_name(v[2], i)\n"
805829
" db:column_type(v[1], i)\n"
806830
" end\n"
807-
" local msg = sys.cancel(cmd)\n"
831+
" local msg = sys.cancel(cmd, uuid)\n"
808832
" for i, v in ipairs(msg) do\n"
809833
" db:emit(v)\n"
810834
" end\n"

tests/unifiedcancel.test/test_cancel.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@ function header
3131

3232
header 1 "running a long query, retrieve it and cancel it"
3333
echo "Running tests on node ${mach}"
34-
echo "inserting 3 records"
35-
$S_SQL "insert into t values (1), (2), (3)"
34+
echo "inserting 60 records"
35+
$S_SQL "insert into t select * from generate_series(1,60)"
3636
if [[ $? != 0 ]] ; then
3737
echo "Failed to insert"
3838
exit 1
3939
fi
4040

4141
echo "running async select sleep"
42-
#$S_SQL "select *, sleep(10, 1) from t order by id"
43-
$S_SQL "select *, sleep(60) from t order by id" &
42+
$S_SQL "select *, sleep(1) from t order by 1" &
4443
if [[ $? != 0 ]] ; then
4544
echo "Failed to async run sleep"
4645
exit 1
@@ -60,12 +59,14 @@ if [[ -z ${uuid} ]] ; then
6059
fi
6160

6261
echo "running cancel ${uuid} trap"
63-
$S_SQL "exec procedure sys.cmd.cancel('${uuid}')"
62+
$S_SQL "exec procedure sys.cmd.cancel('cnonce', '${uuid}')"
6463
if [[ $? != 0 ]] ; then
6564
echo "Failed to run cancel message"
6665
exit 1
6766
fi
6867

68+
sleep 2
69+
6970
echo "check if the ${uuid} is gone"
7071
found_uuid=`$S_TSQL "select uuid from comdb2_connections where sql like '%sleep%' and sql not like '%connections%'"`
7172
if [[ $? != 0 ]] ; then

0 commit comments

Comments
 (0)