Skip to content

Commit

Permalink
Refactor: move all valkey modules related declarations to module.h (#…
Browse files Browse the repository at this point in the history
…1489)

In this commit we move all structures and functions declarations related
to Valkey modules from `server.h` to the recently added `module.h` file.

This re-organization makes it easier for new contributors to find the
valkey modules related code, as well as reducing the compilation times
when changes are made to the modules code.

---------

Signed-off-by: Ricardo Dias <[email protected]>
  • Loading branch information
rjd15372 authored Jan 2, 2025
1 parent ede4add commit 8d764f2
Show file tree
Hide file tree
Showing 22 changed files with 283 additions and 252 deletions.
1 change: 1 addition & 0 deletions src/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "server.h"
#include "sha256.h"
#include "module.h"
#include <fcntl.h>
#include <ctype.h>

Expand Down
3 changes: 2 additions & 1 deletion src/aof.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "bio.h"
#include "rio.h"
#include "functions.h"
#include "module.h"

#include <signal.h>
#include <fcntl.h>
Expand Down Expand Up @@ -2161,7 +2162,7 @@ int rewriteModuleObject(rio *r, robj *key, robj *o, int dbid) {
ValkeyModuleIO io;
moduleValue *mv = o->ptr;
moduleType *mt = mv->type;
moduleInitIOContext(io, mt, r, key, dbid);
moduleInitIOContext(&io, mt, r, key, dbid);
mt->aof_rewrite(&io, key, mv->value);
if (io.ctx) {
moduleFreeContext(io.ctx);
Expand Down
1 change: 1 addition & 0 deletions src/blocked.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "latency.h"
#include "monotonic.h"
#include "cluster_slot_stats.h"
#include "module.h"

/* forward declarations */
static void unblockClientWaitingData(client *c);
Expand Down
1 change: 1 addition & 0 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "server.h"
#include "cluster.h"
#include "cluster_slot_stats.h"
#include "module.h"

#include <ctype.h>

Expand Down
1 change: 1 addition & 0 deletions src/cluster_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "cluster_slot_stats.h"
#include "endianconv.h"
#include "connection.h"
#include "module.h"

#include <stdlib.h>
#include <sys/types.h>
Expand Down
24 changes: 3 additions & 21 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "cluster.h"
#include "connection.h"
#include "bio.h"
#include "module.h"

#include <fcntl.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -371,20 +372,6 @@ void resetServerSaveParams(void) {
server.saveparamslen = 0;
}

void queueLoadModule(sds path, sds *argv, int argc) {
int i;
struct moduleLoadQueueEntry *loadmod;

loadmod = zmalloc(sizeof(struct moduleLoadQueueEntry));
loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL;
loadmod->path = sdsnew(path);
loadmod->argc = argc;
for (i = 0; i < argc; i++) {
loadmod->argv[i] = createRawStringObject(argv[i], sdslen(argv[i]));
}
listAddNodeTail(server.loadmodule_queue, loadmod);
}

/* Parse an array of `arg_len` sds strings, validate and populate
* server.client_obuf_limits if valid.
* Used in CONFIG SET and configuration file parsing. */
Expand Down Expand Up @@ -567,7 +554,7 @@ void loadServerConfigFromString(char *config) {
goto loaderr;
}
} else if (!strcasecmp(argv[0], "loadmodule") && argc >= 2) {
queueLoadModule(argv[1], &argv[2], argc - 2);
moduleEnqueueLoadModule(argv[1], &argv[2], argc - 2);
} else if (strchr(argv[0], '.')) {
if (argc < 2) {
err = "Module config specified without value";
Expand Down Expand Up @@ -1583,12 +1570,7 @@ void rewriteConfigLoadmoduleOption(struct rewriteConfigState *state) {
dictEntry *de;
while ((de = dictNext(di)) != NULL) {
struct ValkeyModule *module = dictGetVal(de);
line = sdsnew("loadmodule ");
line = sdscatsds(line, module->loadmod->path);
for (int i = 0; i < module->loadmod->argc; i++) {
line = sdscatlen(line, " ", 1);
line = sdscatsds(line, module->loadmod->argv[i]->ptr);
}
line = moduleLoadQueueEntryToLoadmoduleOptionStr(module, "loadmodule");
rewriteConfigRewriteLine(state, "loadmodule", line, 1);
}
dictReleaseIterator(di);
Expand Down
1 change: 1 addition & 0 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "script.h"
#include "functions.h"
#include "io_threads.h"
#include "module.h"

#include <signal.h>
#include <ctype.h>
Expand Down
3 changes: 2 additions & 1 deletion src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "threads_mngr.h"
#include "io_threads.h"
#include "sds.h"
#include "module.h"

#include <arpa/inet.h>
#include <signal.h>
Expand Down Expand Up @@ -263,7 +264,7 @@ void xorObjectDigest(serverDb *db, robj *keyobj, unsigned char *digest, robj *o)
ValkeyModuleDigest md = {{0}, {0}, keyobj, db->id};
moduleValue *mv = o->ptr;
moduleType *mt = mv->type;
moduleInitDigestContext(md);
moduleInitDigestContext(&md);
if (mt->digest) {
mt->digest(&md, mv->value);
xorDigest(digest, md.x, sizeof(md.x));
Expand Down
1 change: 1 addition & 0 deletions src/defrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "server.h"
#include "hashtable.h"
#include "script.h"
#include "module.h"
#include <stddef.h>

#ifdef HAVE_DEFRAG
Expand Down
1 change: 1 addition & 0 deletions src/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
typedef struct functionLibInfo functionLibInfo;

/* ValkeyModule type aliases for scripting engine structs and types. */
typedef struct ValkeyModule ValkeyModule;
typedef ValkeyModuleScriptingEngineCtx engineCtx;
typedef ValkeyModuleScriptingEngineFunctionCtx functionCtx;
typedef ValkeyModuleScriptingEngineCompiledFunction compiledFunction;
Expand Down
1 change: 1 addition & 0 deletions src/lazyfree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "bio.h"
#include "functions.h"
#include "cluster.h"
#include "module.h"

#include <stdatomic.h>

Expand Down
40 changes: 38 additions & 2 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "valkeymodule.h"
#include "io_threads.h"
#include "functions.h"
#include "module.h"
#include <dlfcn.h>
#include <sys/stat.h>
#include <sys/wait.h>
Expand All @@ -75,6 +76,12 @@
* pointers that have an API the module can call with them)
* -------------------------------------------------------------------------- */

struct moduleLoadQueueEntry {
sds path;
int argc;
robj **argv;
};

struct ValkeyModuleInfoCtx {
struct ValkeyModule *module;
dict *requested_sections;
Expand Down Expand Up @@ -644,6 +651,35 @@ void *VM_PoolAlloc(ValkeyModuleCtx *ctx, size_t bytes) {
* Helpers for modules API implementation
* -------------------------------------------------------------------------- */

void moduleEnqueueLoadModule(sds path, sds *argv, int argc) {
int i;
struct moduleLoadQueueEntry *loadmod;

loadmod = zmalloc(sizeof(struct moduleLoadQueueEntry));
loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL;
loadmod->path = sdsnew(path);
loadmod->argc = argc;
for (i = 0; i < argc; i++) {
loadmod->argv[i] = createRawStringObject(argv[i], sdslen(argv[i]));
}
listAddNodeTail(server.loadmodule_queue, loadmod);
}

sds moduleLoadQueueEntryToLoadmoduleOptionStr(ValkeyModule *module,
const char *config_option_str) {
sds line;

line = sdsnew(config_option_str);
line = sdscatlen(line, " ", 1);
line = sdscatsds(line, module->loadmod->path);
for (int i = 0; i < module->loadmod->argc; i++) {
line = sdscatlen(line, " ", 1);
line = sdscatsds(line, module->loadmod->argv[i]->ptr);
}

return line;
}

client *moduleAllocTempClient(void) {
client *c = NULL;

Expand Down Expand Up @@ -7401,7 +7437,7 @@ void *VM_LoadDataTypeFromStringEncver(const ValkeyModuleString *str, const modul
void *ret;

rioInitWithBuffer(&payload, str->ptr);
moduleInitIOContext(io, (moduleType *)mt, &payload, NULL, -1);
moduleInitIOContext(&io, (moduleType *)mt, &payload, NULL, -1);

/* All VM_Save*() calls always write a version 2 compatible format, so we
* need to make sure we read the same.
Expand Down Expand Up @@ -7433,7 +7469,7 @@ ValkeyModuleString *VM_SaveDataTypeToString(ValkeyModuleCtx *ctx, void *data, co
ValkeyModuleIO io;

rioInitWithBuffer(&payload, sdsempty());
moduleInitIOContext(io, (moduleType *)mt, &payload, NULL, -1);
moduleInitIOContext(&io, (moduleType *)mt, &payload, NULL, -1);
mt->rdb_save(&io, data);
if (io.ctx) {
moduleFreeContext(io.ctx);
Expand Down
Loading

0 comments on commit 8d764f2

Please sign in to comment.