Skip to content

Commit c97f696

Browse files
zliang-scGitHub Enterprise
authored andcommitted
Merge pull request #219 from Snapchat/emit-non-empty-shard-less-replica-issue
add metric emit for non-empty primary with less than 2 connected replicas
2 parents bf29926 + cec570f commit c97f696

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/modules/keydb_modstatsd/modmain.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class StatsdClientWrapper
6767

6868
/* constants */
6969
static time_t c_infoUpdateSeconds = 10;
70+
// the current Redis Cluster setup we configure replication factor as 2, each non-empty master node should have 2 replicas, given that there are 3 zones in each regions
71+
static const int EXPECTED_NUMBER_OF_REPLICAS = 2;
7072

7173
StatsdClientWrapper *g_stats = nullptr;
7274
std::string m_strPrefix { "keydb" };
@@ -544,6 +546,34 @@ void emit_system_free_memory() {
544546
}
545547
}
546548

549+
void emit_metrics_for_insufficient_replicas(struct RedisModuleCtx *ctx, long long keys) {
550+
// non-empty
551+
if (keys <= 0) {
552+
return;
553+
}
554+
RedisModuleCallReply *reply = RedisModule_Call(ctx, "ROLE", "");
555+
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) {
556+
RedisModule_FreeCallReply(reply);
557+
return;
558+
}
559+
RedisModuleCallReply *roleReply = RedisModule_CallReplyArrayElement(reply, 0);
560+
if (RedisModule_CallReplyType(roleReply) != REDISMODULE_REPLY_STRING) {
561+
RedisModule_FreeCallReply(reply);
562+
return;
563+
}
564+
size_t len;
565+
const char *role = RedisModule_CallReplyStringPtr(roleReply, &len);
566+
// check if the current node is a primary
567+
if (strncmp(role, "master", len) == 0) {
568+
RedisModuleCallReply *replicasReply = RedisModule_CallReplyArrayElement(reply, 2);
569+
// check if there are less than 2 connected replicas
570+
if (RedisModule_CallReplyLength(replicasReply) < EXPECTED_NUMBER_OF_REPLICAS) {
571+
g_stats->increment("lessThanExpectedReplicas_error", 1);
572+
}
573+
}
574+
RedisModule_FreeCallReply(reply);
575+
}
576+
547577
void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data) {
548578
static time_t lastTime = 0;
549579
time_t curTime = time(nullptr);
@@ -619,11 +649,16 @@ void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64
619649
g_stats->timing("emit_free_system_memory_time_taken_us", ustime() - commandStartTime);
620650

621651
/* Log Keys */
652+
commandStartTime = ustime();
622653
reply = RedisModule_Call(ctx, "dbsize", "");
623654
long long keys = RedisModule_CallReplyInteger(reply);
624655
RedisModule_FreeCallReply(reply);
625656
g_stats->gauge("keys", keys);
626657
RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"keys\": %llu", keys);
658+
g_stats->timing("emit_keys_metric_time_taken_us", ustime() - commandStartTime);
659+
660+
emit_metrics_for_insufficient_replicas(ctx, keys);
661+
627662
g_stats->timing("metrics_time_taken_us", ustime() - startTime);
628663

629664
lastTime = curTime;

0 commit comments

Comments
 (0)