-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathmodule.c
More file actions
181 lines (147 loc) · 6.04 KB
/
Copy pathmodule.c
File metadata and controls
181 lines (147 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright 2018-2020 Redis Labs Ltd. and Contributors
*
* This file is available under the Redis Labs Source Available License Agreement
*/
#include <unistd.h>
#include <pthread.h>
#include "redismodule.h"
#include "debug.h"
#include "errors.h"
#include "version.h"
#include "util/arr.h"
#include "util/cron.h"
#include "query_ctx.h"
#include "redisearch_api.h"
#include "arithmetic/funcs.h"
#include "commands/commands.h"
#include "util/thpool/pools.h"
#include "graph/graphcontext.h"
#include "util/redis_version.h"
#include "configuration/config.h"
#include "ast/cypher_whitelist.h"
#include "procedures/procedure.h"
#include "module_event_handlers.h"
#include "serializers/graphmeta_type.h"
#include "configuration/reconf_handler.h"
#include "serializers/graphcontext_type.h"
#include "arithmetic/arithmetic_expression.h"
//------------------------------------------------------------------------------
// Minimal supported Redis version
//------------------------------------------------------------------------------
#define MIN_REDIS_VERION_MAJOR 6
#define MIN_REDIS_VERION_MINOR 0
#define MIN_REDIS_VERION_PATCH 0
//------------------------------------------------------------------------------
// Module-level global variables
//------------------------------------------------------------------------------
GraphContext **graphs_in_keyspace; // Global array tracking all extant GraphContexts.
bool process_is_child; // Flag indicating whether the running process is a child.
extern CommandCtx **command_ctxs;
static int _RegisterDataTypes(RedisModuleCtx *ctx) {
if(GraphContextType_Register(ctx) == REDISMODULE_ERR) {
printf("Failed to register GraphContext type\n");
return REDISMODULE_ERR;
}
if(GraphMetaType_Register(ctx) == REDISMODULE_ERR) {
printf("Failed to register GraphMeta type\n");
return REDISMODULE_ERR;
}
return REDISMODULE_OK;
}
static void _PrepareModuleGlobals(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
graphs_in_keyspace = array_new(GraphContext *, 1);
process_is_child = false;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if(RedisModule_Init(ctx, "graph", REDISGRAPH_MODULE_VERSION,
REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
// GraphBLAS should use Redis allocator
GrB_Info res = GxB_init(GrB_NONBLOCKING, RedisModule_Alloc,
RedisModule_Calloc, RedisModule_Realloc, RedisModule_Free, true);
if(res != GrB_SUCCESS) {
RedisModule_Log(ctx, "warning", "Encountered error initializing GraphBLAS");
return REDISMODULE_ERR;
}
GxB_set(GxB_FORMAT, GxB_BY_ROW); // all matrices in CSR format
// validate minimum redis-server version
if(!Redis_Version_GreaterOrEqual(MIN_REDIS_VERION_MAJOR,
MIN_REDIS_VERION_MINOR, MIN_REDIS_VERION_PATCH)) {
RedisModule_Log(ctx, "warning", "RedisGraph requires redis-server version %d.%d.%d and up",
MIN_REDIS_VERION_MAJOR, MIN_REDIS_VERION_MINOR, MIN_REDIS_VERION_PATCH);
return REDISMODULE_ERR;
}
if(RediSearch_Init(ctx, REDISEARCH_INIT_LIBRARY) != REDISMODULE_OK) {
return REDISMODULE_ERR;
}
RedisModule_Log(ctx, "notice", "Starting up RedisGraph version %d.%d.%d.",
REDISGRAPH_VERSION_MAJOR, REDISGRAPH_VERSION_MINOR, REDISGRAPH_VERSION_PATCH);
Proc_Register(); // Register procedures.
AR_RegisterFuncs(); // Register arithmetic functions.
Cron_Start(); // Start CRON
// Set up global lock and variables scoped to the entire module.
_PrepareModuleGlobals(ctx, argv, argc);
// set up the module's configurable variables,
// using user-defined values where provided
// register for config updates
Config_Subscribe_Changes(reconf_handler);
if(Config_Init(ctx, argv, argc) != REDISMODULE_OK) return REDISMODULE_ERR;
RegisterEventHandlers(ctx);
CypherWhitelist_Build(); // Build whitelist of supported Cypher elements.
// Create thread local storage keys for query and error contexts.
if(!QueryCtx_Init()) return REDISMODULE_ERR;
if(!ErrorCtx_Init()) return REDISMODULE_ERR;
if(!ThreadPools_Init()) return REDISMODULE_ERR;
RedisModule_Log(ctx, "notice", "Thread pool created, using %d threads.",
ThreadPools_ReadersCount());
int ompThreadCount;
Config_Option_get(Config_OPENMP_NTHREAD, &ompThreadCount);
if(GxB_set(GxB_NTHREADS, ompThreadCount) != GrB_SUCCESS) {
RedisModule_Log(ctx, "warning", "Failed to set OpenMP thread count to %d", ompThreadCount);
return REDISMODULE_ERR;
}
RedisModule_Log(ctx, "notice", "Maximum number of OpenMP threads set to %d", ompThreadCount);
// initialize array of command contexts
command_ctxs = calloc(ThreadPools_ThreadCount() + 1, sizeof(CommandCtx *));
if(_RegisterDataTypes(ctx) != REDISMODULE_OK) return REDISMODULE_ERR;
if(RedisModule_CreateCommand(ctx, "graph.QUERY", CommandDispatch, "write deny-oom", 1, 1,
1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.RO_QUERY", CommandDispatch, "readonly", 1, 1,
1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.DELETE", Graph_Delete, "write", 1, 1,
1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.EXPLAIN", CommandDispatch, "write deny-oom", 1, 1,
1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.PROFILE", CommandDispatch, "write deny-oom", 1, 1,
1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.BULK", Graph_BulkInsert, "write deny-oom", 1, 1,
1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.SLOWLOG", CommandDispatch, "readonly", 1, 1,
1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.CONFIG", Graph_Config, "readonly", 0, 0,
0) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.LIST", Graph_List, "readonly", 0, 0,
0) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
setupCrashHandlers(ctx);
return REDISMODULE_OK;
}