Skip to content

Commit

Permalink
Add info for command parse failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
deep011 committed Jan 25, 2016
1 parent 8ef71d5 commit 1f622e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
14 changes: 13 additions & 1 deletion command.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ redis_argeval(struct cmd *r)
void
redis_parse_cmd(struct cmd *r)
{
int len;
char *p, *m, *token = NULL;
char *cmd_end;
char ch;
Expand Down Expand Up @@ -1602,15 +1603,21 @@ redis_parse_cmd(struct cmd *r)

enomem:

r->result = CMD_PARSE_ERROR;
r->result = CMD_PARSE_ENOMEM;

return;

error:

r->result = CMD_PARSE_ERROR;
errno = EINVAL;
if(r->errstr == NULL){
r->errstr = hi_alloc(100*sizeof(*r->errstr));
}

len = _scnprintf(r->errstr, 100, "Parse command error. Cmd type: %d, state: %d, break position: %d.",
r->type, state, (int)(p - r->cmd));
r->errstr[len] = '\0';
}

struct cmd *command_get()
Expand All @@ -1624,6 +1631,7 @@ struct cmd *command_get()

command->id = ++cmd_id;
command->result = CMD_PARSE_OK;
command->errstr = NULL;
command->type = CMD_UNKNOWN;
command->cmd = NULL;
command->clen = 0;
Expand Down Expand Up @@ -1660,6 +1668,10 @@ void command_destroy(struct cmd *command)
free(command->cmd);
}

if(command->errstr != NULL){
hi_free(command->errstr);
}

if(command->keys != NULL)
{
command->keys->nelem = 0;
Expand Down
7 changes: 4 additions & 3 deletions command.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

typedef enum cmd_parse_result {
CMD_PARSE_OK, /* parsing ok */
CMD_PARSE_ENOMEM, /* out of memory */
CMD_PARSE_ERROR, /* parsing error */
CMD_PARSE_REPAIR, /* more to parse -> repair parsed & unparsed data */
CMD_PARSE_AGAIN, /* incomplete -> parse again */
Expand Down Expand Up @@ -145,13 +146,14 @@ struct cmd {
uint64_t id; /* command id */

cmd_parse_result_t result; /* command parsing result */
char *errstr; /* error info when the command parse failed */

cmd_type_t type; /* command type */

char *cmd;
uint32_t clen; /* command length */

struct hiarray *keys; /* array of keypos, for req */
struct hiarray *keys; /* array of keypos, for req */

char *narg_start; /* narg start (redis) */
char *narg_end; /* narg end (redis) */
Expand All @@ -161,13 +163,12 @@ struct cmd {
unsigned noforward:1; /* not need forward (example: ping) */

int slot_num; /* this command should send to witch slot?
* -1:the keys in this command cross different slots*/
* -1:the keys in this command cross different slots*/
struct cmd **frag_seq; /* sequence of fragment command, map from keys to fragments*/

redisReply *reply;

list *sub_commands; /* just for pipeline and multi-key commands */

};

void redis_parse_cmd(struct cmd *r);
Expand Down
11 changes: 8 additions & 3 deletions hircluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -2687,17 +2687,22 @@ static int command_format_by_slot(redisClusterContext *cc,


redis_parse_cmd(command);
if(command->result != CMD_PARSE_OK)
if(command->result == CMD_PARSE_ENOMEM)
{
__redisClusterSetError(cc, REDIS_ERR_PROTOCOL, "parse command error");
__redisClusterSetError(cc, REDIS_ERR_PROTOCOL, "Parse command error: out of memory");
goto done;
}
else if(command->result != CMD_PARSE_OK)
{
__redisClusterSetError(cc, REDIS_ERR_PROTOCOL, command->errstr);
goto done;
}

key_count = hiarray_n(command->keys);

if(key_count <= 0)
{
__redisClusterSetError(cc, REDIS_ERR_OTHER, "no keys in command(must have keys for redis cluster mode)");
__redisClusterSetError(cc, REDIS_ERR_OTHER, "No keys in command(must have keys for redis cluster mode)");
goto done;
}
else if(key_count == 1)
Expand Down

0 comments on commit 1f622e3

Please sign in to comment.