-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
redis scan command support #678
base: master
Are you sure you want to change the base?
Changes from 2 commits
fa1a7a7
6e45af7
4ed8a9e
b6e9807
3f904e2
6d3304f
758cb66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -474,6 +474,15 @@ req_make_reply(struct context *ctx, struct conn *conn, struct msg *req) | |
static bool | ||
req_filter(struct conn *conn, struct msg *msg) | ||
{ | ||
uint8_t *key; | ||
uint32_t keylen; | ||
struct keypos *kpos; | ||
uint32_t idx; | ||
unsigned long long cursor; | ||
unsigned long real_cursor; | ||
char arr[16]; | ||
char format[16]; | ||
|
||
ASSERT(conn->client && !conn->proxy); | ||
|
||
if (msg_empty(msg)) { | ||
|
@@ -511,6 +520,31 @@ req_filter(struct conn *conn, struct msg *msg) | |
msg->noforward = 1; | ||
} | ||
|
||
if (msg->type == MSG_REQ_REDIS_SCAN) { | ||
ASSERT(array_n(msg->keys) > 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this into a function and call the function here like what you did in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good advice |
||
kpos = array_get(msg->keys, 0); | ||
key = kpos->start; | ||
keylen = (uint32_t)(kpos->end - kpos->start); | ||
|
||
if (keylen == 1 && key[0] == '0') { | ||
idx=0; | ||
}else{ | ||
/* If the user request is "scan 45066", | ||
the cursor 45066 in the request, | ||
we get server_index=45066 & NC_MAX_NSERVER_MASK=10, | ||
real_cursor = 45066>>NC_MAX_NSERVER_BITS = 11, | ||
and finally the request sent by the proxy to the redis server will be "scan 00011". | ||
*/ | ||
cursor=strtoull(key,NULL,10); | ||
idx = cursor & NC_MAX_NSERVER_MASK; | ||
real_cursor = (cursor >> NC_MAX_NSERVER_BITS); | ||
sprintf(format,"%%0%dd",keylen); | ||
sprintf(arr,format,real_cursor); | ||
nc_memcpy(key,arr,keylen); | ||
} | ||
msg->server_index=idx; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
@@ -573,7 +607,7 @@ req_forward(struct context *ctx, struct conn *c_conn, struct msg *msg) | |
key = kpos->start; | ||
keylen = (uint32_t)(kpos->end - kpos->start); | ||
|
||
s_conn = server_pool_conn(ctx, c_conn->owner, key, keylen); | ||
s_conn = server_pool_conn(ctx, c_conn->owner, msg, key, keylen); | ||
if (s_conn == NULL) { | ||
/* | ||
* Handle a failure to establish a new connection to a server, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -700,12 +700,16 @@ server_pool_idx(const struct server_pool *pool, const uint8_t *key, uint32_t key | |
} | ||
|
||
static struct server * | ||
server_pool_server(struct server_pool *pool, const uint8_t *key, uint32_t keylen) | ||
server_pool_server(struct server_pool *pool, struct msg *r, const uint8_t *key, uint32_t keylen) | ||
{ | ||
struct server *server; | ||
uint32_t idx; | ||
|
||
idx = server_pool_idx(pool, key, keylen); | ||
if (r->type == MSG_REQ_REDIS_SCAN) { | ||
idx = r->server_index; | ||
}else{ | ||
idx = server_pool_idx(pool, key, keylen); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change the cursor of request in |
||
server = array_get(&pool->server, idx); | ||
|
||
log_debug(LOG_VERB, "key '%.*s' on dist %d maps to server '%.*s'", keylen, | ||
|
@@ -715,8 +719,7 @@ server_pool_server(struct server_pool *pool, const uint8_t *key, uint32_t keylen | |
} | ||
|
||
struct conn * | ||
server_pool_conn(struct context *ctx, struct server_pool *pool, const uint8_t *key, | ||
uint32_t keylen) | ||
server_pool_conn(struct context *ctx, struct server_pool *pool, struct msg *msg, const uint8_t *key, uint32_t keylen) | ||
{ | ||
rstatus_t status; | ||
struct server *server; | ||
|
@@ -728,7 +731,7 @@ server_pool_conn(struct context *ctx, struct server_pool *pool, const uint8_t *k | |
} | ||
|
||
/* from a given {key, keylen} pick a server from pool */ | ||
server = server_pool_server(pool, key, keylen); | ||
server = server_pool_server(pool, msg, key, keylen); | ||
if (server == NULL) { | ||
return NULL; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this comment is not clear enough. how about this one:
the server index which the requstion should be forwarded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok