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

Function sentinelProcessHelloMessage

src/sentinel.cpp:2859–2958  ·  view source on GitHub ↗

Process a hello message received via Pub/Sub in master or slave instance, * or sent directly to this sentinel via the (fake) PUBLISH command of Sentinel. * * If the master name specified in the message is not known, the message is * discarded. */

Source from the content-addressed store, hash-verified

2857 * If the master name specified in the message is not known, the message is
2858 * discarded. */
2859void sentinelProcessHelloMessage(const char *hello, int hello_len) {
2860 /* Format is composed of 8 tokens:
2861 * 0=ip,1=port,2=runid,3=current_epoch,4=master_name,
2862 * 5=master_ip,6=master_port,7=master_config_epoch. */
2863 int numtokens, port, removed, master_port;
2864 uint64_t current_epoch, master_config_epoch;
2865 char **token = sdssplitlen(hello, hello_len, ",", 1, &numtokens);
2866 sentinelRedisInstance *si, *master;
2867
2868 if (numtokens == 8) {
2869 /* Obtain a reference to the master this hello message is about */
2870 master = sentinelGetMasterByName(token[4]);
2871 if (!master) goto cleanup; /* Unknown master, skip the message. */
2872
2873 /* First, try to see if we already have this sentinel. */
2874 port = atoi(token[1]);
2875 master_port = atoi(token[6]);
2876 si = getSentinelRedisInstanceByAddrAndRunID(
2877 master->sentinels,token[0],port,token[2]);
2878 current_epoch = strtoull(token[3],NULL,10);
2879 master_config_epoch = strtoull(token[7],NULL,10);
2880
2881 if (!si) {
2882 /* If not, remove all the sentinels that have the same runid
2883 * because there was an address change, and add the same Sentinel
2884 * with the new address back. */
2885 removed = removeMatchingSentinelFromMaster(master,token[2]);
2886 if (removed) {
2887 sentinelEvent(LL_NOTICE,"+sentinel-address-switch",master,
2888 "%@ ip %s port %d for %s", token[0],port,token[2]);
2889 } else {
2890 /* Check if there is another Sentinel with the same address this
2891 * new one is reporting. What we do if this happens is to set its
2892 * port to 0, to signal the address is invalid. We'll update it
2893 * later if we get an HELLO message. */
2894 sentinelRedisInstance *other =
2895 getSentinelRedisInstanceByAddrAndRunID(
2896 master->sentinels, token[0],port,NULL);
2897 if (other) {
2898 sentinelEvent(LL_NOTICE,"+sentinel-invalid-addr",other,"%@");
2899 other->addr->port = 0; /* It means: invalid address. */
2900 sentinelUpdateSentinelAddressInAllMasters(other);
2901 }
2902 }
2903
2904 /* Add the new sentinel. */
2905 si = createSentinelRedisInstance(token[2],SRI_SENTINEL,
2906 token[0],port,master->quorum,master);
2907
2908 if (si) {
2909 if (!removed) sentinelEvent(LL_NOTICE,"+sentinel",si,"%@");
2910 /* The runid is NULL after a new instance creation and
2911 * for Sentinels we don't have a later chance to fill it,
2912 * so do it now. */
2913 si->runid = sdsnew(token[2]);
2914 sentinelTryConnectionSharing(si);
2915 if (removed) sentinelUpdateSentinelAddressInAllMasters(si);
2916 sentinelFlushConfig();

Callers 2

sentinelPublishCommandFunction · 0.85

Calls 15

sdssplitlenFunction · 0.85
sentinelGetMasterByNameFunction · 0.85
sentinelEventFunction · 0.85
sdsnewFunction · 0.85
sentinelFlushConfigFunction · 0.85
announceSentinelAddrFunction · 0.85

Tested by

no test coverage detected