add thread pool

This commit is contained in:
LingZhaoHui 2022-10-15 21:31:11 +08:00
parent d0dfa04129
commit d0775f0de3
5 changed files with 50 additions and 3 deletions

View File

@ -26,6 +26,7 @@ set(migrate_sources
src/hiredis/sds.c
src/hiredis/sockcompat.c
src/log.cpp
src/migrate_task.cpp
src/thread_pool.cpp
src/redis_migrate.cpp)

23
src/migrate_task.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "migrate_task.h"
MigrateTask::MigrateTask(migrateObj m, RedisModuleCtx *ctx, logObj mLog) {
this->mObj = m;
this->ctx = ctx;
}
void MigrateTask::process() {
RedisModuleCallReply *reply = RedisModule_Call(this->ctx, "type", "c", this->mObj.key);
long long items = RedisModule_CallReplyLength(reply);
if (items != 1) {
RedisModule_Log(ctx, WARNING, "type is error");
return;
}
RedisModuleCallReply *item1 = RedisModule_CallReplyArrayElement(reply, 0);
RedisModuleString *str = RedisModule_CreateStringFromCallReply(item1);
size_t len;
const char *type = RedisModule_StringPtrLen(str, &len);
migrateLog(this->mLog, LL_NOTICE, "type=%s", type);
}
MigrateTask::~MigrateTask() {
}

16
src/migrate_task.h Normal file
View File

@ -0,0 +1,16 @@
#include <string>
#include "redis_migrate.h"
class MigrateTask {
private:
migrateObj mObj;
RedisModuleCtx *ctx;
logObj mLog;
public:
MigrateTask(migrateObj m, RedisModuleCtx *ctx, logObj mLog);
void process();
~MigrateTask();
};

View File

@ -10,6 +10,8 @@
#include "hiredis/hiredis.h"
#include "redismodule.h"
#include "log.h"
#include "thread_pool.h"
#include "migrate_task.h"
static logObj mLog = {};
@ -17,6 +19,10 @@ static RedisModuleCommandFilter *filter;
static bool isMigrating = false;
static ThreadPool<MigrateTask> pool;
static std::map<std::string, migrateObj> migrating;
migrateObj createMigrateObject(RedisModuleString *host, int port, int slot, RedisModuleString *key, migrateObj m) {
size_t hostLen, keyLen;
const char *hostStr = RedisModule_StringPtrLen(host, &hostLen);
@ -94,6 +100,8 @@ int rm_migrateCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
m = createMigrateObject(host, (int)portDouble, (int)slotDouble, key, m);
migrating[keyStr] = m;
isMigrating = true;
MigrateTask task(m, ctx, mLog);
pool.submit(&task);
return RedisModule_ReplyWithSimpleString(ctx, "OK");
}

View File

@ -41,13 +41,12 @@ typedef struct migrateObject {
int isCache;
} migrateObj;
static std::map<std::string, migrateObj> migrating;
migrateObj createMigrateObject(RedisModuleString *host, int port, int slot, RedisModuleString *key, migrateObj m);
int rm_migrateCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
void migrateFinished(migrateObj m);
void rm_migrateFilter(RedisModuleCommandFilterCtx *filter);
#ifdef __cplusplus