Skip to content

Commit 82a1c98

Browse files
committed
REFACTOR: refactor config file support for server configuration
1 parent 9e93d37 commit 82a1c98

File tree

3 files changed

+177
-30
lines changed

3 files changed

+177
-30
lines changed

config_parser.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
#include <memcached/config_parser.h>
2727
#include <memcached/util.h>
2828

29-
static int read_config_file(const char *fname, struct config_item items[],
30-
FILE *error);
31-
3229
/**
3330
* Copy a string and trim of leading and trailing white space characters.
3431
* Allow the user to escape out the stop character by putting a backslash before
@@ -224,8 +221,8 @@ int parse_config(const char *str, struct config_item *items, FILE *error) {
224221
return ret;
225222
}
226223

227-
static int read_config_file(const char *fname, struct config_item items[],
228-
FILE *error) {
224+
int read_config_file(const char *fname, struct config_item items[],
225+
FILE *error) {
229226
FILE *fp = fopen(fname, "r");
230227
if (fp == NULL) {
231228
(void)fprintf(error, "Failed to open file: %s\n", fname);

include/memcached/config_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct config_item {
7777
*/
7878
MEMCACHED_PUBLIC_API int parse_config(const char *str, struct config_item items[], FILE *error);
7979

80+
int read_config_file(const char *fname, struct config_item items[], FILE *error);
8081
#ifdef __cplusplus
8182
}
8283
#endif

memcached.c

Lines changed: 174 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15804,6 +15804,125 @@ static bool sanitycheck(void)
1580415804
return true;
1580515805
}
1580615806

15807+
typedef struct {
15808+
bool *daemonize;
15809+
bool *maxcore;
15810+
char **username;
15811+
bool *lock_memory;
15812+
char **pid_file;
15813+
char *protocol;
15814+
char **engine;
15815+
const char **engine_config;
15816+
bool *preallocate;
15817+
#ifdef ENABLE_ZK_INTEGRATION
15818+
int *arcus_zk_to;
15819+
#ifdef PROXY_SUPPORT
15820+
char **arcus_proxy_cfg;
15821+
#endif
15822+
#endif
15823+
} config_opt_t;
15824+
15825+
static int try_load_config_file(const char *path, config_opt_t *opt)
15826+
{
15827+
char *access_mask_str = NULL;
15828+
char *protocol_str = NULL;
15829+
char *extension_path[5] = {NULL,};
15830+
15831+
struct config_item main_config_items[] = {
15832+
{ .key = "port", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.port },
15833+
{ .key = "udpport", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.udpport },
15834+
{ .key = "socketpath", .datatype = DT_STRING, .value.dt_string = &settings.socketpath },
15835+
{ .key = "access", .datatype = DT_STRING, .value.dt_string = &access_mask_str },
15836+
{ .key = "listen", .datatype = DT_STRING, .value.dt_string = &settings.inter },
15837+
{ .key = "daemonize", .datatype = DT_BOOL, .value.dt_bool = opt->daemonize },
15838+
{ .key = "maxcore", .datatype = DT_BOOL, .value.dt_bool = opt->maxcore },
15839+
{ .key = "username", .datatype = DT_STRING, .value.dt_string = opt->username },
15840+
{ .key = "maxconns", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.maxconns },
15841+
{ .key = "lock_memory", .datatype = DT_BOOL, .value.dt_bool = opt->lock_memory },
15842+
{ .key = "verbosity", .datatype = DT_SIZE, .value.dt_size = (size_t*)&settings.verbose },
15843+
{ .key = "pid_file", .datatype = DT_STRING, .value.dt_string = opt->pid_file },
15844+
{ .key = "threads", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.num_threads },
15845+
{ .key = "reqs_per_event", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.reqs_per_event },
15846+
{ .key = "backlog", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.backlog },
15847+
{ .key = "protocol", .datatype = DT_STRING, .value.dt_string = &protocol_str },
15848+
{ .key = "engine_path", .datatype = DT_STRING, .value.dt_string = opt->engine },
15849+
{ .key = "memory_limit", .datatype = DT_SIZE, .value.dt_size = &settings.maxbytes },
15850+
{ .key = "eviction", .datatype = DT_BOOL, .value.dt_bool = (bool*)&settings.evict_to_free },
15851+
{ .key = "sticky_limit", .datatype = DT_SIZE, .value.dt_size = &settings.sticky_limit },
15852+
{ .key = "factor", .datatype = DT_FLOAT, .value.dt_float = (float*)&settings.factor },
15853+
{ .key = "chunk_size", .datatype = DT_SIZE, .value.dt_size = (size_t*)&settings.chunk_size },
15854+
{ .key = "prefix_delimiter", .datatype = DT_CHAR, .value.dt_char = &settings.prefix_delimiter},
15855+
{ .key = "use_cas", .datatype = DT_BOOL, .value.dt_bool = &settings.use_cas },
15856+
{ .key = "item_size_max", .datatype = DT_SIZE, .value.dt_size = &settings.item_size_max },
15857+
{ .key = "engine_config", .datatype = DT_STRING, .value.dt_string = (char**)opt->engine_config },
15858+
{ .key = "preallocate", .datatype = DT_BOOL, .value.dt_bool = opt->preallocate },
15859+
{ .key = "extension1", .datatype = DT_STRING, .value.dt_string = &extension_path[0] },
15860+
{ .key = "extension2", .datatype = DT_STRING, .value.dt_string = &extension_path[1] },
15861+
{ .key = "extension3", .datatype = DT_STRING, .value.dt_string = &extension_path[2] },
15862+
{ .key = "extension4", .datatype = DT_STRING, .value.dt_string = &extension_path[3] },
15863+
{ .key = "extension5", .datatype = DT_STRING, .value.dt_string = &extension_path[4] },
15864+
#ifdef ENABLE_ZK_INTEGRATION
15865+
{ .key = "zookeeper", .datatype = DT_STRING, .value.dt_string = (char**)&arcus_zk_cfg },
15866+
{ .key = "zk_timeout", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)opt->arcus_zk_to },
15867+
#ifdef PROXY_SUPPORT
15868+
{ .key = "proxy_config", .datatype = DT_STRING, .value.dt_string = opt->arcus_proxy_cfg },
15869+
#endif
15870+
#endif
15871+
15872+
#ifdef SASL_ENABLED
15873+
{ .key = "require_sasl", .datatype = DT_BOOL, .value.dt_bool = &settings.require_sasl },
15874+
#endif
15875+
{ .key = NULL }
15876+
};
15877+
15878+
if (read_config_file(path, main_config_items, stderr) == -1) {
15879+
mc_logger->log(EXTENSION_LOG_WARNING, NULL, "Error parsing config file. Aborting.\n");
15880+
return -1;
15881+
}
15882+
15883+
if (access_mask_str != NULL) {
15884+
settings.access = strtol(access_mask_str, NULL, 8);
15885+
free(access_mask_str);
15886+
}
15887+
15888+
if (protocol_str != NULL) {
15889+
if (strcmp(protocol_str, "auto") == 0) {
15890+
settings.binding_protocol = negotiating_prot;
15891+
} else if (strcmp(protocol_str, "binary") == 0) {
15892+
settings.binding_protocol = binary_prot;
15893+
} else if (strcmp(protocol_str, "ascii") == 0) {
15894+
settings.binding_protocol = ascii_prot;
15895+
} else {
15896+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
15897+
"Invalid value for binding protocol in config file: %s\n", protocol_str);
15898+
free(protocol_str);
15899+
return -1;
15900+
}
15901+
free(protocol_str);
15902+
}
15903+
15904+
for (int i = 0; i < 5; i++) {
15905+
if (extension_path[i] != NULL) {
15906+
char *ptr = strchr(extension_path[i], ',');
15907+
if (ptr != NULL) {
15908+
*ptr = '\0';
15909+
ptr++;
15910+
}
15911+
if (!load_extension(extension_path[i], ptr)) {
15912+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
15913+
"Failed to load extension: %s\n", extension_path[i]);
15914+
for (int j = 0; j < 5; j++)
15915+
if (extension_path[j]) free(extension_path[j]);
15916+
return -1;
15917+
}
15918+
free(extension_path[i]);
15919+
extension_path[i] = NULL;
15920+
}
15921+
}
15922+
15923+
return 0;
15924+
}
15925+
1580715926
static void settings_reload_engine_config(void)
1580815927
{
1580915928
ENGINE_ERROR_CODE ret;
@@ -15898,6 +16017,34 @@ int main (int argc, char **argv)
1589816017
return EX_OSERR;
1589916018
}
1590016019

16020+
/* parse config file */
16021+
if (argc > 1 && argv[1][0] != '-') {
16022+
const char *config_file = argv[1];
16023+
config_opt_t opt = {
16024+
.daemonize = &do_daemonize,
16025+
.maxcore = (bool*)&maxcore,
16026+
.username = &username,
16027+
.lock_memory = &lock_memory,
16028+
.pid_file = &pid_file,
16029+
.engine = (char**)&engine,
16030+
.engine_config = &engine_config,
16031+
.preallocate = &preallocate,
16032+
#ifdef ENABLE_ZK_INTEGRATION
16033+
.arcus_zk_to = &arcus_zk_to,
16034+
#ifdef PROXY_SUPPORT
16035+
.arcus_proxy_cfg = &arcus_proxy_cfg,
16036+
#endif
16037+
#endif
16038+
};
16039+
if (try_load_config_file(config_file, &opt) != 0) {
16040+
fprintf(stderr, "Error parsing config file '%s'.", config_file);
16041+
exit(EXIT_FAILURE);
16042+
}
16043+
optind = 2;
16044+
} else {
16045+
optind = 1;
16046+
}
16047+
1590116048
/* process arguments */
1590216049
while (-1 != (c = getopt(argc, argv,
1590316050
"a:" /* access mask for unix socket */
@@ -16021,11 +16168,6 @@ int main (int argc, char **argv)
1602116168
break;
1602216169
case 'f':
1602316170
settings.factor = atof(optarg);
16024-
if (settings.factor <= 1.0) {
16025-
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16026-
"Factor must be greater than 1\n");
16027-
return 1;
16028-
}
1602916171
break;
1603016172
case 'n':
1603116173
settings.chunk_size = atoi(optarg);
@@ -16097,26 +16239,6 @@ int main (int argc, char **argv)
1609716239
} else {
1609816240
settings.item_size_max = atoi(optarg);
1609916241
}
16100-
/* small memory allocator needs the maximum item size larger than 20 KB */
16101-
//if (settings.item_size_max < 1024) {
16102-
if (settings.item_size_max < 1024 * 20) {
16103-
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16104-
"Item max size cannot be less than 20KB.\n");
16105-
return 1;
16106-
}
16107-
if (settings.item_size_max > 1024 * 1024 * 128) {
16108-
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16109-
"Cannot set item size limit higher than 128 mb.\n");
16110-
return 1;
16111-
}
16112-
if (settings.item_size_max > 1024 * 1024) {
16113-
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16114-
"WARNING: Setting item max size above 1MB is not"
16115-
" recommended!\n"
16116-
" Raising this limit increases the minimum memory requirements\n"
16117-
" and will decrease your memory efficiency.\n"
16118-
);
16119-
}
1612016242
break;
1612116243
case 'E':
1612216244
engine = optarg;
@@ -16173,6 +16295,33 @@ int main (int argc, char **argv)
1617316295
return 1;
1617416296
}
1617516297
}
16298+
16299+
/* small memory allocator needs the maximum item size larger than 20 KB */
16300+
//if (settings.item_size_max < 1024) {
16301+
if (settings.item_size_max < 1024 * 20) {
16302+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16303+
"Item max size cannot be less than 20KB.\n");
16304+
return 1;
16305+
}
16306+
if (settings.item_size_max > 1024 * 1024 * 128) {
16307+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16308+
"Cannot set item size limit higher than 128 mb.\n");
16309+
return 1;
16310+
}
16311+
if (settings.item_size_max > 1024 * 1024) {
16312+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16313+
"WARNING: Setting item max size above 1MB is not"
16314+
" recommended!\n"
16315+
" Raising this limit increases the minimum memory requirements\n"
16316+
" and will decrease your memory efficiency.\n"
16317+
);
16318+
}
16319+
if (settings.factor <= 1.0) {
16320+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16321+
"Factor must be greater than 1\n");
16322+
return 1;
16323+
}
16324+
1617616325
old_opts += sprintf(old_opts, "num_threads=%lu;", (unsigned long)settings.num_threads);
1617716326
old_opts += sprintf(old_opts, "cache_size=%llu;", (unsigned long long)settings.maxbytes);
1617816327
if (settings.evict_to_free == 0) {

0 commit comments

Comments
 (0)