Add short client info log to CLUSTER MEET / FORGET / RESET commands (#1249)

These commands are all administrator commands. If they are operated
incorrectly, serious consequences may occur. Print the full client
info by using catClientInfoString, the info is useful when we want
to identify the source of request.

Since the origin client info is very large and might complicate the
output, we added a catClientInfoShortString function, it will only
print some basic fields, we want these fields that are useful to
identify the client. These fields are:
- id
- addr
- laddr
- connection info
- name
- user
- lib-name
- lib-ver

And also used it to replace the origin client info where it has the
same purpose. Some logging is changed from full client info to short
client info:
- CLUSTER FAILOVER
- FAILOVER / PSYNC
- REPLICAOF NO ONE
- SHUTDOWN

Signed-off-by: Binbin <binloveplay1314@qq.com>
This commit is contained in:
Binbin 2024-11-23 00:23:38 +08:00 committed by GitHub
parent b9d224097a
commit 9851006d6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 5 deletions

View File

@ -6596,6 +6596,10 @@ int clusterCommandSpecial(client *c) {
addReplyErrorFormat(c, "Invalid node address specified: %s:%s", (char *)c->argv[2]->ptr,
(char *)c->argv[3]->ptr);
} else {
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
serverLog(LL_NOTICE, "Cluster meet %s:%lld (user request from '%s').", (char *)c->argv[2]->ptr, port,
client);
sdsfree(client);
addReply(c, shared.ok);
}
} else if (!strcasecmp(c->argv[1]->ptr, "flushslots") && c->argc == 2) {
@ -6710,6 +6714,9 @@ int clusterCommandSpecial(client *c) {
addReplyError(c, "Can't forget my master!");
return 1;
}
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
serverLog(LL_NOTICE, "Cluster forget %s (user request from '%s').", (char *)c->argv[2]->ptr, client);
sdsfree(client);
clusterBlacklistAddNode(n);
clusterDelNode(n);
clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_SAVE_CONFIG);
@ -6798,7 +6805,7 @@ int clusterCommandSpecial(client *c) {
}
resetManualFailover();
server.cluster->mf_end = mstime() + CLUSTER_MF_TIMEOUT;
sds client = catClientInfoString(sdsempty(), c, server.hide_user_data_from_log);
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
if (takeover) {
/* A takeover does not perform any initial check. It just
@ -6877,6 +6884,9 @@ int clusterCommandSpecial(client *c) {
"master nodes containing keys");
return 1;
}
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
serverLog(LL_NOTICE, "Cluster reset (user request from '%s').", client);
sdsfree(client);
clusterReset(hard);
addReply(c, shared.ok);
} else if (!strcasecmp(c->argv[1]->ptr, "links") && c->argc == 2) {

View File

@ -3385,6 +3385,29 @@ sds catClientInfoString(sds s, client *client, int hide_user_data) {
return ret;
}
/* Concatenate a string representing the state of a client in a human
* readable format, into the sds string 's'.
*
* This is a simplified and shortened version of catClientInfoString,
* it only added some basic fields for tracking clients. */
sds catClientInfoShortString(sds s, client *client, int hide_user_data) {
if (!server.crashed) waitForClientIO(client);
char conninfo[CONN_INFO_LEN];
sds ret = sdscatfmt(
s,
FMTARGS(
"id=%U", (unsigned long long)client->id,
" addr=%s", getClientPeerId(client),
" laddr=%s", getClientSockname(client),
" %s", connGetInfo(client->conn, conninfo, sizeof(conninfo)),
" name=%s", hide_user_data ? "*redacted*" : (client->name ? (char *)client->name->ptr : ""),
" user=%s", hide_user_data ? "*redacted*" : (client->user ? client->user->name : "(superuser)"),
" lib-name=%s", client->lib_name ? (char *)client->lib_name->ptr : "",
" lib-ver=%s", client->lib_ver ? (char *)client->lib_ver->ptr : ""));
return ret;
}
sds getAllClientsInfoString(int type, int hide_user_data) {
listNode *ln;
listIter li;

View File

@ -1051,7 +1051,7 @@ void syncCommand(client *c) {
} else {
replicationUnsetPrimary();
}
sds client = catClientInfoString(sdsempty(), c, server.hide_user_data_from_log);
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
serverLog(LL_NOTICE, "PRIMARY MODE enabled (failover request from '%s')", client);
sdsfree(client);
} else {
@ -3971,7 +3971,7 @@ void replicaofCommand(client *c) {
if (!strcasecmp(c->argv[1]->ptr, "no") && !strcasecmp(c->argv[2]->ptr, "one")) {
if (server.primary_host) {
replicationUnsetPrimary();
sds client = catClientInfoString(sdsempty(), c, server.hide_user_data_from_log);
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
serverLog(LL_NOTICE, "PRIMARY MODE enabled (user request from '%s')", client);
sdsfree(client);
}
@ -4000,7 +4000,7 @@ void replicaofCommand(client *c) {
/* There was no previous primary or the user specified a different one,
* we can continue. */
replicationSetPrimary(c->argv[1]->ptr, port, 0);
sds client = catClientInfoString(sdsempty(), c, server.hide_user_data_from_log);
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
serverLog(LL_NOTICE, "REPLICAOF %s:%d enabled (user request from '%s')", server.primary_host,
server.primary_port, client);
sdsfree(client);

View File

@ -4325,7 +4325,7 @@ int prepareForShutdown(client *c, int flags) {
server.shutdown_flags = flags;
if (c != NULL) {
sds client = catClientInfoString(sdsempty(), c, server.hide_user_data_from_log);
sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
serverLog(LL_NOTICE, "User requested shutdown... (user request from '%s')", client);
sdsfree(client);
} else {

View File

@ -2853,6 +2853,7 @@ char *getClientPeerId(client *client);
char *getClientSockName(client *client);
int isClientConnIpV6(client *c);
sds catClientInfoString(sds s, client *client, int hide_user_data);
sds catClientInfoShortString(sds s, client *client, int hide_user_data);
sds getAllClientsInfoString(int type, int hide_user_data);
int clientSetName(client *c, robj *name, const char **err);
void rewriteClientCommandVector(client *c, int argc, ...);