MCPcopy Create free account
hub / github.com/F-Stack/f-stack / syncWithMaster

Function syncWithMaster

app/redis-6.2.6/src/replication.c:2228–2535  ·  view source on GitHub ↗

This handler fires when the non blocking connect was able to * establish a connection with the master. */

Source from the content-addressed store, hash-verified

2226/* This handler fires when the non blocking connect was able to
2227 * establish a connection with the master. */
2228void syncWithMaster(connection *conn) {
2229 char tmpfile[256], *err = NULL;
2230 int dfd = -1, maxtries = 5;
2231 int psync_result;
2232
2233 /* If this event fired after the user turned the instance into a master
2234 * with SLAVEOF NO ONE we must just return ASAP. */
2235 if (server.repl_state == REPL_STATE_NONE) {
2236 connClose(conn);
2237 return;
2238 }
2239
2240 /* Check for errors in the socket: after a non blocking connect() we
2241 * may find that the socket is in error state. */
2242 if (connGetState(conn) != CONN_STATE_CONNECTED) {
2243 serverLog(LL_WARNING,"Error condition on socket for SYNC: %s",
2244 connGetLastError(conn));
2245 goto error;
2246 }
2247
2248 /* Send a PING to check the master is able to reply without errors. */
2249 if (server.repl_state == REPL_STATE_CONNECTING) {
2250 serverLog(LL_NOTICE,"Non blocking connect for SYNC fired the event.");
2251 /* Delete the writable event so that the readable event remains
2252 * registered and we can wait for the PONG reply. */
2253 connSetReadHandler(conn, syncWithMaster);
2254 connSetWriteHandler(conn, NULL);
2255 server.repl_state = REPL_STATE_RECEIVE_PING_REPLY;
2256 /* Send the PING, don't check for errors at all, we have the timeout
2257 * that will take care about this. */
2258 err = sendCommand(conn,"PING",NULL);
2259 if (err) goto write_error;
2260 return;
2261 }
2262
2263 /* Receive the PONG command. */
2264 if (server.repl_state == REPL_STATE_RECEIVE_PING_REPLY) {
2265 err = receiveSynchronousResponse(conn);
2266
2267 /* We accept only two replies as valid, a positive +PONG reply
2268 * (we just check for "+") or an authentication error.
2269 * Note that older versions of Redis replied with "operation not
2270 * permitted" instead of using a proper error code, so we test
2271 * both. */
2272 if (err[0] != '+' &&
2273 strncmp(err,"-NOAUTH",7) != 0 &&
2274 strncmp(err,"-NOPERM",7) != 0 &&
2275 strncmp(err,"-ERR operation not permitted",28) != 0)
2276 {
2277 serverLog(LL_WARNING,"Error reply to PING from master: '%s'",err);
2278 sdsfree(err);
2279 goto error;
2280 } else {
2281 serverLog(LL_NOTICE,
2282 "Master replied to PING, replication can continue...");
2283 }
2284 sdsfree(err);
2285 err = NULL;

Callers

nothing calls this directly

Calls 15

connCloseFunction · 0.85
connGetStateFunction · 0.85
connGetLastErrorFunction · 0.85
connSetReadHandlerFunction · 0.85
connSetWriteHandlerFunction · 0.85
sendCommandFunction · 0.85
strncmpFunction · 0.85
sdsfreeFunction · 0.85
sdslenFunction · 0.85
sendCommandArgvFunction · 0.85
sdsfromlonglongFunction · 0.85

Tested by

no test coverage detected