]> git.saurik.com Git - redis.git/blame - src/scripting.c
Replicate EVALSHA as EVAL taking a dictionary of sha1 -> script source code.
[redis.git] / src / scripting.c
CommitLineData
7585836e 1#include "redis.h"
2#include "sha1.h"
3
4#include <lua.h>
5#include <lauxlib.h>
6#include <lualib.h>
7229d60d 7#include <ctype.h>
7585836e 8
532e0f5d 9char *redisProtocolToLuaType_Int(lua_State *lua, char *reply);
10char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply);
11char *redisProtocolToLuaType_Status(lua_State *lua, char *reply);
3791000f 12char *redisProtocolToLuaType_Error(lua_State *lua, char *reply);
13char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply);
532e0f5d 14
15/* Take a Redis reply in the Redis protocol format and convert it into a
16 * Lua type. Thanks to this function, and the introduction of not connected
17 * clients, it is trvial to implement the redis() lua function.
18 *
19 * Basically we take the arguments, execute the Redis command in the context
20 * of a non connected client, then take the generated reply and convert it
21 * into a suitable Lua type. With this trick the scripting feature does not
22 * need the introduction of a full Redis internals API. Basically the script
23 * is like a normal client that bypasses all the slow I/O paths.
24 *
25 * Note: in this function we do not do any sanity check as the reply is
26 * generated by Redis directly. This allows use to go faster.
27 * The reply string can be altered during the parsing as it is discared
28 * after the conversion is completed.
29 *
30 * Errors are returned as a table with a single 'err' field set to the
31 * error string.
32 */
33
34char *redisProtocolToLuaType(lua_State *lua, char* reply) {
35 char *p = reply;
36
37 switch(*p) {
38 case ':':
39 p = redisProtocolToLuaType_Int(lua,reply);
40 break;
41 case '$':
42 p = redisProtocolToLuaType_Bulk(lua,reply);
43 break;
44 case '+':
45 p = redisProtocolToLuaType_Status(lua,reply);
46 break;
3791000f 47 case '-':
48 p = redisProtocolToLuaType_Error(lua,reply);
49 break;
50 case '*':
51 p = redisProtocolToLuaType_MultiBulk(lua,reply);
52 break;
532e0f5d 53 }
54 return p;
55}
56
57char *redisProtocolToLuaType_Int(lua_State *lua, char *reply) {
58 char *p = strchr(reply+1,'\r');
59 long long value;
60
61 string2ll(reply+1,p-reply-1,&value);
62 lua_pushnumber(lua,(lua_Number)value);
63 return p+2;
64}
65
66char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) {
67 char *p = strchr(reply+1,'\r');
68 long long bulklen;
69
70 string2ll(reply+1,p-reply-1,&bulklen);
379789cc 71 if (bulklen == -1) {
82c6b825 72 lua_pushboolean(lua,0);
532e0f5d 73 return p+2;
74 } else {
75 lua_pushlstring(lua,p+2,bulklen);
76 return p+2+bulklen+2;
77 }
78}
79
80char *redisProtocolToLuaType_Status(lua_State *lua, char *reply) {
81 char *p = strchr(reply+1,'\r');
82
0d916763 83 lua_newtable(lua);
84 lua_pushstring(lua,"ok");
532e0f5d 85 lua_pushlstring(lua,reply+1,p-reply-1);
0d916763 86 lua_settable(lua,-3);
532e0f5d 87 return p+2;
88}
89
3791000f 90char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {
91 char *p = strchr(reply+1,'\r');
92
93 lua_newtable(lua);
94 lua_pushstring(lua,"err");
95 lua_pushlstring(lua,reply+1,p-reply-1);
96 lua_settable(lua,-3);
97 return p+2;
98}
99
100char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) {
101 char *p = strchr(reply+1,'\r');
102 long long mbulklen;
103 int j = 0;
104
3791000f 105 string2ll(reply+1,p-reply-1,&mbulklen);
106 p += 2;
107 if (mbulklen == -1) {
82c6b825 108 lua_pushboolean(lua,0);
3791000f 109 return p;
110 }
3791000f 111 lua_newtable(lua);
112 for (j = 0; j < mbulklen; j++) {
10a6da7a 113 lua_pushnumber(lua,j+1);
3791000f 114 p = redisProtocolToLuaType(lua,p);
115 lua_settable(lua,-3);
116 }
117 return p;
118}
119
379789cc 120void luaPushError(lua_State *lua, char *error) {
121 lua_newtable(lua);
122 lua_pushstring(lua,"err");
123 lua_pushstring(lua, error);
124 lua_settable(lua,-3);
125}
126
0f1d64ca 127int luaRedisCommand(lua_State *lua) {
128 int j, argc = lua_gettop(lua);
129 struct redisCommand *cmd;
130 robj **argv;
131 redisClient *c = server.lua_client;
132 sds reply;
133
532e0f5d 134 /* Build the arguments vector */
0f1d64ca 135 argv = zmalloc(sizeof(robj*)*argc);
379789cc 136 for (j = 0; j < argc; j++) {
137 if (!lua_isstring(lua,j+1)) break;
532e0f5d 138 argv[j] = createStringObject((char*)lua_tostring(lua,j+1),
139 lua_strlen(lua,j+1));
379789cc 140 }
141
142 /* Check if one of the arguments passed by the Lua script
143 * is not a string or an integer (lua_isstring() return true for
144 * integers as well). */
145 if (j != argc) {
146 j--;
147 while (j >= 0) {
148 decrRefCount(argv[j]);
149 j--;
150 }
151 zfree(argv);
152 luaPushError(lua,
153 "Lua redis() command arguments must be strings or integers");
154 return 1;
155 }
0f1d64ca 156
157 /* Command lookup */
158 cmd = lookupCommand(argv[0]->ptr);
3791000f 159 if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
160 (argc < -cmd->arity)))
161 {
532e0f5d 162 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
0f1d64ca 163 zfree(argv);
3791000f 164 if (cmd)
379789cc 165 luaPushError(lua,
3791000f 166 "Wrong number of args calling Redis command From Lua script");
167 else
379789cc 168 luaPushError(lua,"Unknown Redis command called from Lua script");
532e0f5d 169 return 1;
0f1d64ca 170 }
532e0f5d 171
0f1d64ca 172 /* Run the command in the context of a fake client */
173 c->argv = argv;
174 c->argc = argc;
175 cmd->proc(c);
176
177 /* Convert the result of the Redis command into a suitable Lua type.
178 * The first thing we need is to create a single string from the client
179 * output buffers. */
180 reply = sdsempty();
181 if (c->bufpos) {
7156f43c 182 reply = sdscatlen(reply,c->buf,c->bufpos);
0f1d64ca 183 c->bufpos = 0;
184 }
185 while(listLength(c->reply)) {
186 robj *o = listNodeValue(listFirst(c->reply));
187
09ab5591 188 reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
0f1d64ca 189 listDelNode(c->reply,listFirst(c->reply));
190 }
532e0f5d 191 redisProtocolToLuaType(lua,reply);
7156f43c 192 sdsfree(reply);
0f1d64ca 193
194 /* Clean up. Command code may have changed argv/argc so we use the
195 * argv/argc of the client instead of the local variables. */
196 for (j = 0; j < c->argc; j++)
197 decrRefCount(c->argv[j]);
198 zfree(c->argv);
199
200 return 1;
201}
202
288f811f 203int luaLogCommand(lua_State *lua) {
204 int j, argc = lua_gettop(lua);
205 int level;
206 sds log;
207
208 if (argc < 2) {
209 luaPushError(lua, "redis.log() requires two arguments or more.");
210 return 1;
211 } else if (!lua_isnumber(lua,-argc)) {
e927a246 212 luaPushError(lua, "First argument must be a number (log level).");
288f811f 213 return 1;
214 }
215 level = lua_tonumber(lua,-argc);
e927a246 216 if (level < REDIS_DEBUG || level > REDIS_WARNING) {
288f811f 217 luaPushError(lua, "Invalid debug level.");
218 return 1;
219 }
220
221 /* Glue together all the arguments */
222 log = sdsempty();
223 for (j = 1; j < argc; j++) {
224 size_t len;
225 char *s;
226
227 s = (char*)lua_tolstring(lua,(-argc)+j,&len);
228 if (s) {
229 if (j != 1) log = sdscatlen(log," ",1);
230 log = sdscatlen(log,s,len);
231 }
232 }
233 redisLogRaw(level,log);
234 sdsfree(log);
235 return 0;
236}
237
eeffcf38 238void luaMaskCountHook(lua_State *lua, lua_Debug *ar) {
239 long long elapsed;
240 REDIS_NOTUSED(ar);
241
eeffcf38 242 elapsed = (ustime()/1000) - server.lua_time_start;
243 if (elapsed >= server.lua_time_limit) {
87c21924 244 redisLog(REDIS_NOTICE,"Lua script aborted for max execution time after %lld milliseconds of running time.",elapsed);
245 lua_pushstring(lua,"Script aborted for max execution time.");
eeffcf38 246 lua_error(lua);
eeffcf38 247 }
248}
249
7585836e 250void scriptingInit(void) {
251 lua_State *lua = lua_open();
252 luaL_openlibs(lua);
0f1d64ca 253
4dd444bb 254 /* Initialize a dictionary we use to map SHAs to scripts.
255 * This is useful for replication, as we need to replicate EVALSHA
256 * as EVAL, so we need to remember the associated script. */
257 server.lua_scripts = dictCreate(&dbDictType,NULL);
258
288f811f 259 /* Register the redis commands table and fields */
260 lua_newtable(lua);
261
262 /* redis.call */
263 lua_pushstring(lua,"call");
0f1d64ca 264 lua_pushcfunction(lua,luaRedisCommand);
288f811f 265 lua_settable(lua,-3);
266
267 /* redis.log and log levels. */
268 lua_pushstring(lua,"log");
269 lua_pushcfunction(lua,luaLogCommand);
270 lua_settable(lua,-3);
271
272 lua_pushstring(lua,"LOG_DEBUG");
273 lua_pushnumber(lua,REDIS_DEBUG);
274 lua_settable(lua,-3);
275
276 lua_pushstring(lua,"LOG_VERBOSE");
277 lua_pushnumber(lua,REDIS_VERBOSE);
278 lua_settable(lua,-3);
279
280 lua_pushstring(lua,"LOG_NOTICE");
281 lua_pushnumber(lua,REDIS_NOTICE);
282 lua_settable(lua,-3);
283
284 lua_pushstring(lua,"LOG_WARNING");
285 lua_pushnumber(lua,REDIS_WARNING);
286 lua_settable(lua,-3);
287
288 /* Finally set the table as 'redis' global var. */
00b7541b 289 lua_setglobal(lua,"redis");
0f1d64ca 290
291 /* Create the (non connected) client that we use to execute Redis commands
292 * inside the Lua interpreter */
293 server.lua_client = createClient(-1);
7156f43c 294 server.lua_client->flags |= REDIS_LUA_CLIENT;
0f1d64ca 295
7585836e 296 server.lua = lua;
297}
298
299/* Hash the scripit into a SHA1 digest. We use this as Lua function name.
300 * Digest should point to a 41 bytes buffer: 40 for SHA1 converted into an
301 * hexadecimal number, plus 1 byte for null term. */
302void hashScript(char *digest, char *script, size_t len) {
303 SHA1_CTX ctx;
304 unsigned char hash[20];
305 char *cset = "0123456789abcdef";
306 int j;
307
308 SHA1Init(&ctx);
309 SHA1Update(&ctx,(unsigned char*)script,len);
310 SHA1Final(hash,&ctx);
311
312 for (j = 0; j < 20; j++) {
313 digest[j*2] = cset[((hash[j]&0xF0)>>4)];
314 digest[j*2+1] = cset[(hash[j]&0xF)];
315 }
316 digest[40] = '\0';
317}
318
319void luaReplyToRedisReply(redisClient *c, lua_State *lua) {
82c6b825 320 int t = lua_type(lua,-1);
7585836e 321
322 switch(t) {
323 case LUA_TSTRING:
82c6b825 324 addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1));
7585836e 325 break;
326 case LUA_TBOOLEAN:
82c6b825 327 addReply(c,lua_toboolean(lua,-1) ? shared.cone : shared.nullbulk);
7585836e 328 break;
329 case LUA_TNUMBER:
82c6b825 330 addReplyLongLong(c,(long long)lua_tonumber(lua,-1));
7585836e 331 break;
532e0f5d 332 case LUA_TTABLE:
0d916763 333 /* We need to check if it is an array, an error, or a status reply.
334 * Error are returned as a single element table with 'err' field.
335 * Status replies are returned as single elment table with 'ok' field */
532e0f5d 336 lua_pushstring(lua,"err");
337 lua_gettable(lua,-2);
338 t = lua_type(lua,-1);
339 if (t == LUA_TSTRING) {
3bb818df 340 sds err = sdsnew(lua_tostring(lua,-1));
341 sdsmapchars(err,"\r\n"," ",2);
342 addReplySds(c,sdscatprintf(sdsempty(),"-%s\r\n",err));
343 sdsfree(err);
0d916763 344 lua_pop(lua,2);
345 return;
346 }
347
348 lua_pop(lua,1);
349 lua_pushstring(lua,"ok");
350 lua_gettable(lua,-2);
351 t = lua_type(lua,-1);
352 if (t == LUA_TSTRING) {
3bb818df 353 sds ok = sdsnew(lua_tostring(lua,-1));
354 sdsmapchars(ok,"\r\n"," ",2);
355 addReplySds(c,sdscatprintf(sdsempty(),"+%s\r\n",ok));
356 sdsfree(ok);
532e0f5d 357 lua_pop(lua,1);
358 } else {
359 void *replylen = addDeferredMultiBulkLength(c);
360 int j = 1, mbulklen = 0;
361
0d916763 362 lua_pop(lua,1); /* Discard the 'ok' field value we popped */
532e0f5d 363 while(1) {
364 lua_pushnumber(lua,j++);
365 lua_gettable(lua,-2);
366 t = lua_type(lua,-1);
367 if (t == LUA_TNIL) {
368 lua_pop(lua,1);
369 break;
532e0f5d 370 }
82c6b825 371 luaReplyToRedisReply(c, lua);
372 mbulklen++;
532e0f5d 373 }
374 setDeferredMultiBulkLength(c,replylen,mbulklen);
375 }
376 break;
7585836e 377 default:
378 addReply(c,shared.nullbulk);
379 }
380 lua_pop(lua,1);
381}
382
4ae5b5e1 383/* Set an array of Redis String Objects as a Lua array (table) stored into a
384 * global variable. */
385void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
386 int j;
387
388 lua_newtable(lua);
389 for (j = 0; j < elec; j++) {
390 lua_pushlstring(lua,(char*)elev[j]->ptr,sdslen(elev[j]->ptr));
391 lua_rawseti(lua,-2,j+1);
392 }
393 lua_setglobal(lua,var);
394}
395
7229d60d 396void evalGenericCommand(redisClient *c, int evalsha) {
7585836e 397 lua_State *lua = server.lua;
398 char funcname[43];
4ae5b5e1 399 long long numkeys;
400
401 /* Get the number of arguments that are keys */
402 if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != REDIS_OK)
403 return;
404 if (numkeys > (c->argc - 3)) {
405 addReplyError(c,"Number of keys can't be greater than number of args");
406 return;
407 }
7585836e 408
409 /* We obtain the script SHA1, then check if this function is already
410 * defined into the Lua state */
411 funcname[0] = 'f';
412 funcname[1] = '_';
7229d60d 413 if (!evalsha) {
414 /* Hash the code if this is an EVAL call */
415 hashScript(funcname+2,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
416 } else {
417 /* We already have the SHA if it is a EVALSHA */
418 int j;
419 char *sha = c->argv[1]->ptr;
420
421 for (j = 0; j < 40; j++)
422 funcname[j+2] = tolower(sha[j]);
423 funcname[42] = '\0';
424 }
425
7585836e 426 lua_getglobal(lua, funcname);
427 if (lua_isnil(lua,1)) {
7229d60d 428 sds funcdef;
429
430 /* Function not defined... let's define it if we have the
431 * body of the funciton. If this is an EVALSHA call we can just
432 * return an error. */
433 if (evalsha) {
434 addReply(c, shared.noscripterr);
435 lua_pop(lua,1); /* remove the nil from the stack */
436 return;
437 }
438 funcdef = sdsempty();
7585836e 439
440 lua_pop(lua,1); /* remove the nil from the stack */
441 funcdef = sdscat(funcdef,"function ");
442 funcdef = sdscatlen(funcdef,funcname,42);
443 funcdef = sdscatlen(funcdef," ()\n",4);
444 funcdef = sdscatlen(funcdef,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
445 funcdef = sdscatlen(funcdef,"\nend\n",5);
379789cc 446 /* printf("Defining:\n%s\n",funcdef); */
7585836e 447
448 if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"func definition")) {
449 addReplyErrorFormat(c,"Error compiling script (new function): %s\n",
450 lua_tostring(lua,-1));
451 lua_pop(lua,1);
7156f43c 452 sdsfree(funcdef);
7585836e 453 return;
454 }
7156f43c 455 sdsfree(funcdef);
7585836e 456 if (lua_pcall(lua,0,0,0)) {
457 addReplyErrorFormat(c,"Error running script (new function): %s\n",
458 lua_tostring(lua,-1));
459 lua_pop(lua,1);
460 return;
461 }
462 lua_getglobal(lua, funcname);
4dd444bb 463
464 /* We also save a SHA1 -> Original script map in a dictionary
465 * so that we can replicate / write in the AOF all the
466 * EVALSHA commands as EVAL using the original script. */
467 {
468 int retval = dictAdd(server.lua_scripts,
469 sdsnewlen(funcname+2,40),c->argv[1]);
470 redisAssert(retval == DICT_OK);
471 incrRefCount(c->argv[1]);
472 }
7585836e 473 }
4ae5b5e1 474
475 /* Populate the argv and keys table accordingly to the arguments that
476 * EVAL received. */
477 luaSetGlobalArray(lua,"KEYS",c->argv+3,numkeys);
478 luaSetGlobalArray(lua,"ARGV",c->argv+3+numkeys,c->argc-3-numkeys);
c2a7dd85 479
480 /* Select the right DB in the context of the Lua client */
481 selectDb(server.lua_client,c->db->id);
7585836e 482
da386cdf 483 /* Set an hook in order to be able to stop the script execution if it
484 * is running for too much time.
485 * We set the hook only if the time limit is enabled as the hook will
486 * make the Lua script execution slower. */
487 if (server.lua_time_limit > 0) {
488 lua_sethook(lua,luaMaskCountHook,LUA_MASKCOUNT,100000);
489 server.lua_time_start = ustime()/1000;
490 } else {
491 lua_sethook(lua,luaMaskCountHook,0,0);
492 }
493
7585836e 494 /* At this point whatever this script was never seen before or if it was
495 * already defined, we can call it. We have zero arguments and expect
496 * a single return value. */
497 if (lua_pcall(lua,0,1,0)) {
c2a7dd85 498 selectDb(c,server.lua_client->db->id); /* set DB ID from Lua client */
7585836e 499 addReplyErrorFormat(c,"Error running script (call to %s): %s\n",
500 funcname, lua_tostring(lua,-1));
501 lua_pop(lua,1);
40531be0 502 lua_gc(lua,LUA_GCCOLLECT,0);
7585836e 503 return;
504 }
c2a7dd85 505 selectDb(c,server.lua_client->db->id); /* set DB ID from Lua client */
7585836e 506 luaReplyToRedisReply(c,lua);
40531be0 507 lua_gc(lua,LUA_GCSTEP,1);
4dd444bb 508
509 /* If we have slaves attached we want to replicate this command as
510 * EVAL instead of EVALSHA. We do this also in the AOF as currently there
511 * is no easy way to propagate a command in a different way in the AOF
512 * and in the replication link.
513 *
514 * IMPROVEMENT POSSIBLE:
515 * 1) Replicate this command as EVALSHA in the AOF.
516 * 2) Remember what slave already received a given script, and replicate
517 * the EVALSHA against this slaves when possible.
518 */
519 if (evalsha) {
520 robj *script = dictFetchValue(server.lua_scripts,c->argv[1]->ptr);
521
522 redisAssert(script != NULL);
523 rewriteClientCommandArgument(c,0,
524 resetRefCount(createStringObject("EVAL",4)));
525 rewriteClientCommandArgument(c,1,script);
526 }
7585836e 527}
7229d60d 528
529void evalCommand(redisClient *c) {
530 evalGenericCommand(c,0);
531}
532
533void evalShaCommand(redisClient *c) {
534 if (sdslen(c->argv[1]->ptr) != 40) {
535 /* We know that a match is not possible if the provided SHA is
536 * not the right length. So we return an error ASAP, this way
537 * evalGenericCommand() can be implemented without string length
538 * sanity check */
539 addReply(c, shared.noscripterr);
540 return;
541 }
542 evalGenericCommand(c,1);
543}