Skip to content

Commit e25f6bb

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

File tree

3 files changed

+129
-30
lines changed

3 files changed

+129
-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: 126 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15852,6 +15852,8 @@ static void close_listen_sockets(void)
1585215852
}
1585315853
}
1585415854

15855+
#define EXTENSION_PATH_MAX_SIZE 5
15856+
1585515857
int main (int argc, char **argv)
1585615858
{
1585715859
int c;
@@ -15898,6 +15900,103 @@ int main (int argc, char **argv)
1589815900
return EX_OSERR;
1589915901
}
1590015902

15903+
/* parse config file */
15904+
if (argc > 1 && argv[1][0] != '-') {
15905+
char *access_mask_str = NULL;
15906+
char *protocol_str = NULL;
15907+
char *extension_path[EXTENSION_PATH_MAX_SIZE] = {NULL, };
15908+
const char *config_file = argv[1];
15909+
15910+
struct config_item main_config_items[] = {
15911+
{ .key = "port", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.port },
15912+
{ .key = "udpport", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.udpport },
15913+
{ .key = "socketpath", .datatype = DT_STRING, .value.dt_string = &settings.socketpath },
15914+
{ .key = "access", .datatype = DT_STRING, .value.dt_string = &access_mask_str },
15915+
{ .key = "listen", .datatype = DT_STRING, .value.dt_string = &settings.inter },
15916+
{ .key = "daemonize", .datatype = DT_BOOL, .value.dt_bool = &do_daemonize },
15917+
{ .key = "maxcore", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&maxcore },
15918+
{ .key = "username", .datatype = DT_STRING, .value.dt_string = &username },
15919+
{ .key = "maxconns", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.maxconns },
15920+
{ .key = "lock_memory", .datatype = DT_BOOL, .value.dt_bool = &lock_memory },
15921+
{ .key = "verbosity", .datatype = DT_SIZE, .value.dt_size = (size_t*)&settings.verbose },
15922+
{ .key = "pid_file", .datatype = DT_STRING, .value.dt_string = &pid_file },
15923+
{ .key = "threads", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.num_threads },
15924+
{ .key = "reqs_per_event", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.reqs_per_event },
15925+
{ .key = "backlog", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&settings.backlog },
15926+
{ .key = "protocol", .datatype = DT_STRING, .value.dt_string = &protocol_str },
15927+
{ .key = "engine_path", .datatype = DT_STRING, .value.dt_string = (char**)&engine },
15928+
{ .key = "memory_limit", .datatype = DT_SIZE, .value.dt_size = &settings.maxbytes },
15929+
{ .key = "eviction", .datatype = DT_BOOL, .value.dt_bool = (bool*)&settings.evict_to_free },
15930+
{ .key = "sticky_limit", .datatype = DT_SIZE, .value.dt_size = &settings.sticky_limit },
15931+
{ .key = "factor", .datatype = DT_FLOAT, .value.dt_float = (float*)&settings.factor },
15932+
{ .key = "chunk_size", .datatype = DT_SIZE, .value.dt_size = (size_t*)&settings.chunk_size },
15933+
{ .key = "prefix_delimiter", .datatype = DT_CHAR, .value.dt_char = &settings.prefix_delimiter},
15934+
{ .key = "use_cas", .datatype = DT_BOOL, .value.dt_bool = &settings.use_cas },
15935+
{ .key = "item_size_max", .datatype = DT_SIZE, .value.dt_size = &settings.item_size_max },
15936+
{ .key = "engine_config", .datatype = DT_STRING, .value.dt_string = (char**)&engine_config },
15937+
{ .key = "preallocate", .datatype = DT_BOOL, .value.dt_bool = &preallocate },
15938+
{ .key = "extension1", .datatype = DT_STRING, .value.dt_string = &extension_path[0] },
15939+
{ .key = "extension2", .datatype = DT_STRING, .value.dt_string = &extension_path[1] },
15940+
{ .key = "extension3", .datatype = DT_STRING, .value.dt_string = &extension_path[2] },
15941+
{ .key = "extension4", .datatype = DT_STRING, .value.dt_string = &extension_path[3] },
15942+
{ .key = "extension5", .datatype = DT_STRING, .value.dt_string = &extension_path[4] },
15943+
#ifdef ENABLE_ZK_INTEGRATION
15944+
{ .key = "zookeeper", .datatype = DT_STRING, .value.dt_string = (char**)&arcus_zk_cfg },
15945+
{ .key = "zk_timeout", .datatype = DT_UINT32, .value.dt_uint32 = (uint32_t*)&arcus_zk_to },
15946+
#ifdef PROXY_SUPPORT
15947+
{ .key = "proxy_config", .datatype = DT_STRING, .value.dt_string = &arcus_proxy_cfg },
15948+
#endif
15949+
#endif
15950+
#ifdef SASL_ENABLED
15951+
{ .key = "require_sasl", .datatype = DT_BOOL, .value.dt_bool = &settings.require_sasl },
15952+
#endif
15953+
{ .key = NULL }
15954+
};
15955+
15956+
if (read_config_file(config_file, main_config_items, stderr) == -1) {
15957+
mc_logger->log(EXTENSION_LOG_WARNING, NULL, "Error parsing config file. Aborting.\n");
15958+
fprintf(stderr, "Error parsing config file '%s'.", config_file);
15959+
exit(EXIT_FAILURE);
15960+
}
15961+
15962+
if (access_mask_str != NULL) {
15963+
settings.access = strtol(access_mask_str, NULL, 8);
15964+
free(access_mask_str);
15965+
}
15966+
15967+
if (protocol_str != NULL) {
15968+
if (strcmp(protocol_str, "auto") == 0) {
15969+
settings.binding_protocol = negotiating_prot;
15970+
} else if (strcmp(protocol_str, "binary") == 0) {
15971+
settings.binding_protocol = binary_prot;
15972+
} else if (strcmp(protocol_str, "ascii") == 0) {
15973+
settings.binding_protocol = ascii_prot;
15974+
} else {
15975+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
15976+
"Invalid value for binding protocol in config file: %s\n", protocol_str);
15977+
exit(EXIT_FAILURE);
15978+
}
15979+
free(protocol_str);
15980+
}
15981+
15982+
for (int i = 0; i < EXTENSION_PATH_MAX_SIZE; i++) {
15983+
if (extension_path[i] != NULL) {
15984+
char *ptr = strchr(extension_path[i], ',');
15985+
if (ptr != NULL) {
15986+
*ptr = '\0';
15987+
ptr++;
15988+
}
15989+
if (!load_extension(extension_path[i], ptr)) {
15990+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
15991+
"Failed to load extension: %s\n", extension_path[i]);
15992+
exit(EXIT_FAILURE);
15993+
}
15994+
free(extension_path[i]);
15995+
}
15996+
}
15997+
optind++;
15998+
}
15999+
1590116000
/* process arguments */
1590216001
while (-1 != (c = getopt(argc, argv,
1590316002
"a:" /* access mask for unix socket */
@@ -16021,11 +16120,6 @@ int main (int argc, char **argv)
1602116120
break;
1602216121
case 'f':
1602316122
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-
}
1602916123
break;
1603016124
case 'n':
1603116125
settings.chunk_size = atoi(optarg);
@@ -16097,26 +16191,6 @@ int main (int argc, char **argv)
1609716191
} else {
1609816192
settings.item_size_max = atoi(optarg);
1609916193
}
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-
}
1612016194
break;
1612116195
case 'E':
1612216196
engine = optarg;
@@ -16173,6 +16247,33 @@ int main (int argc, char **argv)
1617316247
return 1;
1617416248
}
1617516249
}
16250+
16251+
/* small memory allocator needs the maximum item size larger than 20 KB */
16252+
//if (settings.item_size_max < 1024) {
16253+
if (settings.item_size_max < 1024 * 20) {
16254+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16255+
"Item max size cannot be less than 20KB.\n");
16256+
return 1;
16257+
}
16258+
if (settings.item_size_max > 1024 * 1024 * 128) {
16259+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16260+
"Cannot set item size limit higher than 128 mb.\n");
16261+
return 1;
16262+
}
16263+
if (settings.item_size_max > 1024 * 1024) {
16264+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16265+
"WARNING: Setting item max size above 1MB is not"
16266+
" recommended!\n"
16267+
" Raising this limit increases the minimum memory requirements\n"
16268+
" and will decrease your memory efficiency.\n"
16269+
);
16270+
}
16271+
if (settings.factor <= 1.0) {
16272+
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
16273+
"Factor must be greater than 1\n");
16274+
return 1;
16275+
}
16276+
1617616277
old_opts += sprintf(old_opts, "num_threads=%lu;", (unsigned long)settings.num_threads);
1617716278
old_opts += sprintf(old_opts, "cache_size=%llu;", (unsigned long long)settings.maxbytes);
1617816279
if (settings.evict_to_free == 0) {

0 commit comments

Comments
 (0)