diff --git a/src/config.cpp b/src/config.cpp index badedcee4..fd284e5d8 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -864,6 +864,8 @@ void loadServerConfigFromString(char *config) { } else if (!strcasecmp(argv[0], "version-override") && argc == 2) { KEYDB_SET_VERSION = zstrdup(argv[1]); serverLog(LL_WARNING, "Warning version is overriden to: %s\n", KEYDB_SET_VERSION); + } else if (!strcasecmp(argv[0],"testmode") && argc == 2){ + g_fTestMode = yesnotoi(argv[1]); } else { err = "Bad directive or wrong number of arguments"; goto loaderr; } diff --git a/src/networking.cpp b/src/networking.cpp index 7cf0ae55f..1ffc1fea4 100644 --- a/src/networking.cpp +++ b/src/networking.cpp @@ -1097,10 +1097,30 @@ void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) { serverLog(LL_VERBOSE,"Accepted %s:%d", cip, cport); int ielCur = ielFromEventLoop(el); - // We always accept on the same thread - aeAcquireLock(); - acceptCommonHandler(cfd,0,cip, ielCur); - aeReleaseLock(); + if (!g_fTestMode) + { + // We always accept on the same thread + LLocalThread: + aeAcquireLock(); + acceptCommonHandler(cfd,0,cip, ielCur); + aeReleaseLock(); + } + else + { + // In test mode we want a good distribution among threads and avoid the main thread + // since the main thread is most likely to work + int iel = IDX_EVENT_LOOP_MAIN; + while (cserver.cthreads > 1 && iel == IDX_EVENT_LOOP_MAIN) + iel = rand() % cserver.cthreads; + if (iel == ielFromEventLoop(el)) + goto LLocalThread; + char *szT = (char*)zmalloc(NET_IP_STR_LEN, MALLOC_LOCAL); + memcpy(szT, cip, NET_IP_STR_LEN); + aePostFunction(g_pserver->rgthreadvar[iel].el, [cfd, iel, szT]{ + acceptCommonHandler(cfd,0,szT, iel); + zfree(szT); + }); + } } } diff --git a/src/server.cpp b/src/server.cpp index 8afe5641c..8ccbab059 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -61,6 +61,8 @@ #include #include "aelocker.h" +int g_fTestMode = false; + /* Our shared "common" objects */ struct sharedObjectsStruct shared; diff --git a/src/server.h b/src/server.h index 8a762e239..912aba251 100644 --- a/src/server.h +++ b/src/server.h @@ -88,6 +88,8 @@ typedef long long mstime_t; /* millisecond time type. */ #include "endianconv.h" #include "crc64.h" +extern int g_fTestMode; + struct redisObject; class robj_roptr {