Skip to content
This repository was archived by the owner on Nov 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ To build fatcache from source with _debug logs enabled_ and _assertions disabled
## Help

Usage: fatcache [-?hVdS] [-o output file] [-v verbosity level]
[-p port] [-a addr] [-e hash power]
[-p port] [-a addr] [-e hash power] [-c max connections]
[-f factor] [-n min item chunk size] [-I slab size]
[-i max index memory[ [-m max slab memory]
[-z slab profile] [-D ssd device] [-s server id]
Expand All @@ -105,6 +105,7 @@ To build fatcache from source with _debug logs enabled_ and _assertions disabled
-I, --slab-size=N : set slab size in bytes (default: 1048576 bytes)
-i, --max-index-memory=N : set the maximum memory to use for item indexes in MB (default: 64 MB)
-m, --max-slab-memory=N : set the maximum memory to use for slabs in MB (default: 64 MB)
-c, --max-connections=N : set max simultaneous connections (default: 10240)
-z, --slab-profile=S : set the profile of slab item chunk sizes (default: n/a)
-D, --ssd-device=S : set the path to the ssd device file (default: n/a)
-s, --server-id=I/N : set fatcache instance to be I out of total N instances (default: 0/1)
Expand Down
20 changes: 16 additions & 4 deletions src/fc.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

#define FC_INDEX_MEMORY (64 * MB)
#define FC_SLAB_MEMORY (64 * MB)
#define FC_MAX_CONNS (10240)

#define FC_SERVER_ID 0
#define FC_SERVER_N 1
Expand Down Expand Up @@ -97,6 +98,7 @@ static char short_options[] =
"I:" /* slab size in MB */
"i:" /* max memory for item index in MB */
"m:" /* max memory for slab in MB */
"c:" /* max connections */
"z:" /* profile of slab item sizes */
"D:" /* path to ssd device file */
"s:" /* server instance id */
Expand All @@ -107,7 +109,7 @@ fc_show_usage(void)
{
log_stderr(
"Usage: fatcache [-?hVdS] [-o output file] [-v verbosity level]" CRLF
" [-p port] [-a addr] [-e hash power]" CRLF
" [-p port] [-a addr] [-e hash power] [-c max connections]" CRLF
" [-f factor] [-n min item chunk size] [-I slab size]" CRLF
" [-i max index memory[ [-m max slab memory]" CRLF
" [-z slab profile] [-D ssd device] [-s server id]" CRLF
Expand Down Expand Up @@ -138,13 +140,15 @@ fc_show_usage(void)
" -n, --min-item-chunk-size=N : set the minimum item chunk size in bytes (default: %d bytes)" CRLF
" -I, --slab-size=N : set slab size in bytes (default: %d bytes)" CRLF
" -i, --max-index-memory=N : set the maximum memory to use for item indexes in MB (default: %d MB)" CRLF
" -m, --max-slab-memory=N : set the maximum memory to use for slabs in MB (default: %d MB)"
" -m, --max-slab-memory=N : set the maximum memory to use for slabs in MB (default: %d MB)" CRLF
" -c, --max-connections=N : set max simultaneous connections (default: %d)"
"",
FC_FACTOR,
FC_CHUNK_SIZE,
SLAB_SIZE,
FC_INDEX_MEMORY / MB,
FC_SLAB_MEMORY / MB);
FC_SLAB_MEMORY / MB,
FC_MAX_CONNS);
log_stderr(
" -z, --slab-profile=S : set the profile of slab item chunk sizes (default: n/a)" CRLF
" -D, --ssd-device=S : set the path to the ssd device file (default: n/a)" CRLF
Expand Down Expand Up @@ -270,6 +274,7 @@ fc_set_default_options(void)
settings.max_slab_memory = FC_SLAB_MEMORY;
settings.chunk_size = FC_CHUNK_SIZE;
settings.slab_size = FC_SLAB_SIZE;
settings.max_conns = FC_MAX_CONNS;

memset(settings.profile, 0, sizeof(settings.profile));
settings.profile_last_id = SLABCLASS_MAX_ID;
Expand Down Expand Up @@ -429,7 +434,14 @@ fc_get_options(int argc, char **argv)

settings.max_slab_memory = (size_t)value * MB;
break;

case 'c':
value = fc_atoi(optarg, strlen(optarg));
if (value < 0) {
log_stderr("fatcache: option -c should no less than zero");
return FC_ERROR;
}
settings.max_conns = value;
break;
case 'z':
parse_profile = 1;
profile_optarg = (uint8_t *)optarg;
Expand Down
2 changes: 1 addition & 1 deletion src/fc_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ core_core(struct context *ctx, struct conn *conn, uint32_t events)
/* read takes precedence over write */
if (events & (EPOLLIN | EPOLLHUP)) {
status = core_recv(ctx, conn);
if (status != FC_OK || conn->done || conn->err) {
if (conn->client && (status != FC_OK || conn->done || conn->err)) {
core_close(ctx, conn);
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/fc_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ struct settings {

char *ssd_device; /* path to ssd device file */

int max_conns; /* maximum connection num */
uint32_t server_id; /* server id */
uint32_t server_n; /* # server */
};
Expand Down
6 changes: 6 additions & 0 deletions src/fc_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ server_accept(struct context *ctx, struct conn *s)
break;
}

if (settings.max_conns > 0 && conn_nused() >= settings.max_conns) {
close(sd);
log_warn("server reached max connections");
return FC_OK;
}

c = conn_get(sd, true);
if (c == NULL) {
log_error("get conn for c %d from s %d failed: %s", sd, s->sd,
Expand Down
1 change: 1 addition & 0 deletions src/fc_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ stats_settings(void)
APPEND_STAT(stats_buf, "chunk_size", "%u", settings.chunk_size);
APPEND_STAT(stats_buf, "max_chunk_size", "%u", settings.max_chunk_size);
APPEND_STAT(stats_buf, "slab_size", "%u", settings.slab_size);
APPEND_STAT(stats_buf, "max_conns", "%d", settings.max_conns);
APPEND_STAT(stats_buf, "ssd_device", "%s", settings.ssd_device);
APPEND_STAT(stats_buf, "server_id", "%u", settings.server_id);
APPEND_STAT(stats_buf, "server_count", "%u", settings.server_n);
Expand Down