漫话Redis源码之三十一

2024-02-06 09:48
文章标签 源码 redis 三十一 漫话

本文主要是介绍漫话Redis源码之三十一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里没什么特别的,考大家一个问题,为什么在mstime中需要加long long转化?是为了溢出,这里做得非常专业,我曾经踩过这个坑:

typedef struct _client {redisContext *context;sds obuf;char **randptr;         /* Pointers to :rand: strings inside the command buf */size_t randlen;         /* Number of pointers in client->randptr */size_t randfree;        /* Number of unused pointers in client->randptr */char **stagptr;         /* Pointers to slot hashtags (cluster mode only) */size_t staglen;         /* Number of pointers in client->stagptr */size_t stagfree;        /* Number of unused pointers in client->stagptr */size_t written;         /* Bytes of 'obuf' already written */long long start;        /* Start time of a request */long long latency;      /* Request latency */int pending;            /* Number of pending requests (replies to consume) */int prefix_pending;     /* If non-zero, number of pending prefix commands. Commandssuch as auth and select are prefixed to the pipeline ofbenchmark commands and discarded after the first send. */int prefixlen;          /* Size in bytes of the pending prefix commands */int thread_id;struct clusterNode *cluster_node;int slots_last_update;
} *client;/* Threads. */typedef struct benchmarkThread {int index;pthread_t thread;aeEventLoop *el;
} benchmarkThread;/* Cluster. */
typedef struct clusterNode {char *ip;int port;sds name;int flags;sds replicate;  /* Master ID if node is a slave */int *slots;int slots_count;int current_slot_index;int *updated_slots;         /* Used by updateClusterSlotsConfiguration */int updated_slots_count;    /* Used by updateClusterSlotsConfiguration */int replicas_count;sds *migrating; /* An array of sds where even strings are slots and odd* strings are the destination node IDs. */sds *importing; /* An array of sds where even strings are slots and odd* strings are the source node IDs. */int migrating_count; /* Length of the migrating array (migrating slots*2) */int importing_count; /* Length of the importing array (importing slots*2) */struct redisConfig *redis_config;
} clusterNode;typedef struct redisConfig {sds save;sds appendonly;
} redisConfig;/* Prototypes */
char *redisGitSHA1(void);
char *redisGitDirty(void);
static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);
static void createMissingClients(client c);
static benchmarkThread *createBenchmarkThread(int index);
static void freeBenchmarkThread(benchmarkThread *thread);
static void freeBenchmarkThreads();
static void *execBenchmarkThread(void *ptr);
static clusterNode *createClusterNode(char *ip, int port);
static redisConfig *getRedisConfig(const char *ip, int port,const char *hostsocket);
static redisContext *getRedisContext(const char *ip, int port,const char *hostsocket);
static void freeRedisConfig(redisConfig *cfg);
static int fetchClusterSlotsConfiguration(client c);
static void updateClusterSlotsConfiguration();
int showThroughput(struct aeEventLoop *eventLoop, long long id,void *clientData);static sds benchmarkVersion(void) {sds version;version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);/* Add git commit and working tree status when available */if (strtoll(redisGitSHA1(),NULL,16)) {version = sdscatprintf(version, " (git:%s", redisGitSHA1());if (strtoll(redisGitDirty(),NULL,10))version = sdscatprintf(version, "-dirty");version = sdscat(version, ")");}return version;
}/* Dict callbacks */
static uint64_t dictSdsHash(const void *key);
static int dictSdsKeyCompare(void *privdata, const void *key1,const void *key2);/* Implementation */
static long long ustime(void) {struct timeval tv;long long ust;gettimeofday(&tv, NULL);ust = ((long)tv.tv_sec)*1000000;ust += tv.tv_usec;return ust;
}static long long mstime(void) {struct timeval tv;long long mst;gettimeofday(&tv, NULL);mst = ((long long)tv.tv_sec)*1000;mst += tv.tv_usec/1000;return mst;
}static uint64_t dictSdsHash(const void *key) {return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
}static int dictSdsKeyCompare(void *privdata, const void *key1,const void *key2)
{int l1,l2;DICT_NOTUSED(privdata);l1 = sdslen((sds)key1);l2 = sdslen((sds)key2);if (l1 != l2) return 0;return memcmp(key1, key2, l1) == 0;
}/* _serverAssert is needed by dict */
void _serverAssert(const char *estr, const char *file, int line) {fprintf(stderr, "=== ASSERTION FAILED ===");fprintf(stderr, "==> %s:%d '%s' is not true",file,line,estr);*((char*)-1) = 'x';
}static redisContext *getRedisContext(const char *ip, int port,const char *hostsocket)
{redisContext *ctx = NULL;redisReply *reply =  NULL;if (hostsocket == NULL)ctx = redisConnect(ip, port);elsectx = redisConnectUnix(hostsocket);if (ctx == NULL || ctx->err) {fprintf(stderr,"Could not connect to Redis at ");char *err = (ctx != NULL ? ctx->errstr : "");if (hostsocket == NULL)fprintf(stderr,"%s:%d: %s\n",ip,port,err);elsefprintf(stderr,"%s: %s\n",hostsocket,err);goto cleanup;}if (config.tls==1) {const char *err = NULL;if (cliSecureConnection(ctx, config.sslconfig, &err) == REDIS_ERR && err) {fprintf(stderr, "Could not negotiate a TLS connection: %s\n", err);goto cleanup;}}if (config.auth == NULL)return ctx;if (config.user == NULL)reply = redisCommand(ctx,"AUTH %s", config.auth);elsereply = redisCommand(ctx,"AUTH %s %s", config.user, config.auth);if (reply != NULL) {if (reply->type == REDIS_REPLY_ERROR) {if (hostsocket == NULL)fprintf(stderr, "Node %s:%d replied with error:\n%s\n", ip, port, reply->str);elsefprintf(stderr, "Node %s replied with error:\n%s\n", hostsocket, reply->str);freeReplyObject(reply);redisFree(ctx);exit(1);}freeReplyObject(reply);return ctx;}fprintf(stderr, "ERROR: failed to fetch reply from ");if (hostsocket == NULL)fprintf(stderr, "%s:%d\n", ip, port);elsefprintf(stderr, "%s\n", hostsocket);
cleanup:freeReplyObject(reply);redisFree(ctx);return NULL;
}static redisConfig *getRedisConfig(const char *ip, int port,const char *hostsocket)
{redisConfig *cfg = zcalloc(sizeof(*cfg));if (!cfg) return NULL;redisContext *c = NULL;redisReply *reply = NULL, *sub_reply = NULL;c = getRedisContext(ip, port, hostsocket);if (c == NULL) {freeRedisConfig(cfg);return NULL;}redisAppendCommand(c, "CONFIG GET %s", "save");redisAppendCommand(c, "CONFIG GET %s", "appendonly");int i = 0;void *r = NULL;for (; i < 2; i++) {int res = redisGetReply(c, &r);if (reply) freeReplyObject(reply);reply = res == REDIS_OK ? ((redisReply *) r) : NULL;if (res != REDIS_OK || !r) goto fail;if (reply->type == REDIS_REPLY_ERROR) {fprintf(stderr, "ERROR: %s\n", reply->str);goto fail;}if (reply->type != REDIS_REPLY_ARRAY || reply->elements < 2) goto fail;sub_reply = reply->element[1];char *value = sub_reply->str;if (!value) value = "";switch (i) {case 0: cfg->save = sdsnew(value); break;case 1: cfg->appendonly = sdsnew(value); break;}}freeReplyObject(reply);redisFree(c);return cfg;
fail:fprintf(stderr, "ERROR: failed to fetch CONFIG from ");if (hostsocket == NULL) fprintf(stderr, "%s:%d\n", ip, port);else fprintf(stderr, "%s\n", hostsocket);int abort_test = 0;if (reply && reply->type == REDIS_REPLY_ERROR &&(!strncmp(reply->str,"NOAUTH",5) ||!strncmp(reply->str,"WRONGPASS",9) ||!strncmp(reply->str,"NOPERM",5)))abort_test = 1;freeReplyObject(reply);redisFree(c);freeRedisConfig(cfg);if (abort_test) exit(1);return NULL;
}
static void freeRedisConfig(redisConfig *cfg) {if (cfg->save) sdsfree(cfg->save);if (cfg->appendonly) sdsfree(cfg->appendonly);zfree(cfg);
}static void freeClient(client c) {aeEventLoop *el = CLIENT_GET_EVENTLOOP(c);listNode *ln;aeDeleteFileEvent(el,c->context->fd,AE_WRITABLE);aeDeleteFileEvent(el,c->context->fd,AE_READABLE);if (c->thread_id >= 0) {int requests_finished = 0;atomicGet(config.requests_finished, requests_finished);if (requests_finished >= config.requests) {aeStop(el);}}redisFree(c->context);sdsfree(c->obuf);zfree(c->randptr);zfree(c->stagptr);zfree(c);if (config.num_threads) pthread_mutex_lock(&(config.liveclients_mutex));config.liveclients--;ln = listSearchKey(config.clients,c);assert(ln != NULL);listDelNode(config.clients,ln);if (config.num_threads) pthread_mutex_unlock(&(config.liveclients_mutex));
}static void freeAllClients(void) {listNode *ln = config.clients->head, *next;while(ln) {next = ln->next;freeClient(ln->value);ln = next;}
}

这篇关于漫话Redis源码之三十一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683908

相关文章

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

redis+lua实现分布式限流的示例

《redis+lua实现分布式限流的示例》本文主要介绍了redis+lua实现分布式限流的示例,可以实现复杂的限流逻辑,如滑动窗口限流,并且避免了多步操作导致的并发问题,具有一定的参考价值,感兴趣的可... 目录为什么使用Redis+Lua实现分布式限流使用ZSET也可以实现限流,为什么选择lua的方式实现

Redis中管道操作pipeline的实现

《Redis中管道操作pipeline的实现》RedisPipeline是一种优化客户端与服务器通信的技术,通过批量发送和接收命令减少网络往返次数,提高命令执行效率,本文就来介绍一下Redis中管道操... 目录什么是pipeline场景一:我要向Redis新增大批量的数据分批处理事务( MULTI/EXE

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、