This is executed 10 times every second */
| 3555 | |
| 3556 | /* This is executed 10 times every second */ |
| 3557 | void clusterCron(void) { |
| 3558 | serverAssert(ielFromEventLoop(serverTL->el) == IDX_EVENT_LOOP_MAIN); |
| 3559 | dictIterator *di; |
| 3560 | dictEntry *de; |
| 3561 | int update_state = 0; |
| 3562 | int orphaned_masters; /* How many masters there are without ok slaves. */ |
| 3563 | int max_slaves; /* Max number of ok slaves for a single master. */ |
| 3564 | int this_slaves; /* Number of ok slaves for our master (if we are slave). */ |
| 3565 | mstime_t min_pong = 0, now = mstime(); |
| 3566 | clusterNode *min_pong_node = NULL; |
| 3567 | static unsigned long long iteration = 0; |
| 3568 | mstime_t handshake_timeout; |
| 3569 | |
| 3570 | iteration++; /* Number of times this function was called so far. */ |
| 3571 | |
| 3572 | /* We want to take myself->ip in sync with the cluster-announce-ip option. |
| 3573 | * The option can be set at runtime via CONFIG SET, so we periodically check |
| 3574 | * if the option changed to reflect this into myself->ip. */ |
| 3575 | { |
| 3576 | static char *prev_ip = NULL; |
| 3577 | char *curr_ip = g_pserver->cluster_announce_ip; |
| 3578 | int changed = 0; |
| 3579 | |
| 3580 | if (prev_ip == NULL && curr_ip != NULL) changed = 1; |
| 3581 | else if (prev_ip != NULL && curr_ip == NULL) changed = 1; |
| 3582 | else if (prev_ip && curr_ip && strcmp(prev_ip,curr_ip)) changed = 1; |
| 3583 | |
| 3584 | if (changed) { |
| 3585 | if (prev_ip) zfree(prev_ip); |
| 3586 | prev_ip = curr_ip; |
| 3587 | |
| 3588 | if (curr_ip) { |
| 3589 | /* We always take a copy of the previous IP address, by |
| 3590 | * duplicating the string. This way later we can check if |
| 3591 | * the address really changed. */ |
| 3592 | prev_ip = zstrdup(prev_ip); |
| 3593 | strncpy(myself->ip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN-1); |
| 3594 | myself->ip[NET_IP_STR_LEN-1] = '\0'; |
| 3595 | } else { |
| 3596 | myself->ip[0] = '\0'; /* Force autodetection. */ |
| 3597 | } |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | /* The handshake timeout is the time after which a handshake node that was |
| 3602 | * not turned into a normal node is removed from the nodes. Usually it is |
| 3603 | * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use |
| 3604 | * the value of 1 second. */ |
| 3605 | handshake_timeout = g_pserver->cluster_node_timeout; |
| 3606 | if (handshake_timeout < 1000) handshake_timeout = 1000; |
| 3607 | |
| 3608 | /* Update myself flags. */ |
| 3609 | clusterUpdateMyselfFlags(); |
| 3610 | |
| 3611 | /* Check if we have disconnected nodes and re-establish the connection. |
| 3612 | * Also update a few stats while we are here, that can be used to make |
| 3613 | * better decisions in other part of the code. */ |
| 3614 | di = dictGetSafeIterator(g_pserver->cluster->nodes); |
no test coverage detected