MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / clusterBuildMessageHdr

Function clusterBuildMessageHdr

src/cluster.cpp:2492–2562  ·  view source on GitHub ↗

Build the message header. hdr must point to a buffer at least * sizeof(clusterMsg) in bytes. */

Source from the content-addressed store, hash-verified

2490/* Build the message header. hdr must point to a buffer at least
2491 * sizeof(clusterMsg) in bytes. */
2492void clusterBuildMessageHdr(clusterMsg *hdr, int type) {
2493 int totlen = 0;
2494 uint64_t offset;
2495 clusterNode *master;
2496
2497 /* If this node is a master, we send its slots bitmap and configEpoch.
2498 * If this node is a slave we send the master's information instead (the
2499 * node is flagged as slave so the receiver knows that it is NOT really
2500 * in charge for this slots. */
2501 master = (nodeIsSlave(myself) && myself->slaveof) ?
2502 myself->slaveof : myself;
2503
2504 memset(hdr,0,sizeof(*hdr));
2505 hdr->ver = htons(CLUSTER_PROTO_VER);
2506 hdr->sig[0] = 'R';
2507 hdr->sig[1] = 'C';
2508 hdr->sig[2] = 'm';
2509 hdr->sig[3] = 'b';
2510 hdr->type = htons(type);
2511 memcpy(hdr->sender,myself->name,CLUSTER_NAMELEN);
2512
2513 /* If cluster-announce-ip option is enabled, force the receivers of our
2514 * packets to use the specified address for this node. Otherwise if the
2515 * first byte is zero, they'll do auto discovery. */
2516 memset(hdr->myip,0,NET_IP_STR_LEN);
2517 if (g_pserver->cluster_announce_ip) {
2518 strncpy(hdr->myip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN-1);
2519 hdr->myip[NET_IP_STR_LEN-1] = '\0';
2520 }
2521
2522 /* Handle cluster-announce-[tls-|bus-]port. */
2523 int announced_port, announced_pport, announced_cport;
2524 deriveAnnouncedPorts(&announced_port, &announced_pport, &announced_cport);
2525
2526 memcpy(hdr->myslots,master->slots,sizeof(hdr->myslots));
2527 memset(hdr->slaveof,0,CLUSTER_NAMELEN);
2528 if (myself->slaveof != NULL)
2529 memcpy(hdr->slaveof,myself->slaveof->name, CLUSTER_NAMELEN);
2530 hdr->port = htons(announced_port);
2531 hdr->pport = htons(announced_pport);
2532 hdr->cport = htons(announced_cport);
2533 hdr->flags = htons(myself->flags);
2534 hdr->state = g_pserver->cluster->state;
2535
2536 /* Set the currentEpoch and configEpochs. */
2537 hdr->currentEpoch = htonu64(g_pserver->cluster->currentEpoch);
2538 hdr->configEpoch = htonu64(master->configEpoch);
2539
2540 /* Set the replication offset. */
2541 if (nodeIsSlave(myself))
2542 offset = replicationGetSlaveOffset(getFirstMaster());
2543 else
2544 offset = g_pserver->master_repl_offset;
2545 hdr->offset = htonu64(offset);
2546
2547 /* Set the message flags. */
2548 if (nodeIsMaster(myself) && g_pserver->cluster->mf_end)
2549 hdr->mflags[0] |= CLUSTERMSG_FLAG0_PAUSED;

Callers 8

clusterSendPingFunction · 0.85
clusterSendPublishFunction · 0.85
clusterSendFailFunction · 0.85
clusterSendUpdateFunction · 0.85
clusterSendModuleFunction · 0.85
clusterSendFailoverAuthFunction · 0.85
clusterSendMFStartFunction · 0.85

Calls 3

deriveAnnouncedPortsFunction · 0.85
getFirstMasterFunction · 0.85

Tested by

no test coverage detected