Start a BGSAVE for replication goals, which is, selecting the disk or * socket target depending on the configuration, and making sure that * the script cache is flushed before to start. * * The mincapa argument is the bitwise AND among all the slaves capabilities * of the slaves waiting for this BGSAVE, so represents the slave capabilities * all the slaves support. Can be tested via SLAVE_CA
| 644 | * |
| 645 | * Returns C_OK on success or C_ERR otherwise. */ |
| 646 | int startBgsaveForReplication(int mincapa) { |
| 647 | int retval; |
| 648 | int socket_target = server.repl_diskless_sync && (mincapa & SLAVE_CAPA_EOF); |
| 649 | listIter li; |
| 650 | listNode *ln; |
| 651 | |
| 652 | serverLog(LL_NOTICE,"Starting BGSAVE for SYNC with target: %s", |
| 653 | socket_target ? "replicas sockets" : "disk"); |
| 654 | |
| 655 | rdbSaveInfo rsi, *rsiptr; |
| 656 | rsiptr = rdbPopulateSaveInfo(&rsi); |
| 657 | /* Only do rdbSave* when rsiptr is not NULL, |
| 658 | * otherwise slave will miss repl-stream-db. */ |
| 659 | if (rsiptr) { |
| 660 | if (socket_target) |
| 661 | retval = rdbSaveToSlavesSockets(rsiptr); |
| 662 | else |
| 663 | retval = rdbSaveBackground(server.rdb_filename,rsiptr); |
| 664 | } else { |
| 665 | serverLog(LL_WARNING,"BGSAVE for replication: replication information not available, can't generate the RDB file right now. Try later."); |
| 666 | retval = C_ERR; |
| 667 | } |
| 668 | |
| 669 | /* If we succeeded to start a BGSAVE with disk target, let's remember |
| 670 | * this fact, so that we can later delete the file if needed. Note |
| 671 | * that we don't set the flag to 1 if the feature is disabled, otherwise |
| 672 | * it would never be cleared: the file is not deleted. This way if |
| 673 | * the user enables it later with CONFIG SET, we are fine. */ |
| 674 | if (retval == C_OK && !socket_target && server.rdb_del_sync_files) |
| 675 | RDBGeneratedByReplication = 1; |
| 676 | |
| 677 | /* If we failed to BGSAVE, remove the slaves waiting for a full |
| 678 | * resynchronization from the list of slaves, inform them with |
| 679 | * an error about what happened, close the connection ASAP. */ |
| 680 | if (retval == C_ERR) { |
| 681 | serverLog(LL_WARNING,"BGSAVE for replication failed"); |
| 682 | listRewind(server.slaves,&li); |
| 683 | while((ln = listNext(&li))) { |
| 684 | client *slave = ln->value; |
| 685 | |
| 686 | if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START) { |
| 687 | slave->replstate = REPL_STATE_NONE; |
| 688 | slave->flags &= ~CLIENT_SLAVE; |
| 689 | listDelNode(server.slaves,ln); |
| 690 | addReplyError(slave, |
| 691 | "BGSAVE failed, replication can't continue"); |
| 692 | slave->flags |= CLIENT_CLOSE_AFTER_REPLY; |
| 693 | } |
| 694 | } |
| 695 | return retval; |
| 696 | } |
| 697 | |
| 698 | /* If the target is socket, rdbSaveToSlavesSockets() already setup |
| 699 | * the slaves for a full resync. Otherwise for disk target do it now.*/ |
| 700 | if (!socket_target) { |
| 701 | listRewind(server.slaves,&li); |
| 702 | while((ln = listNext(&li))) { |
| 703 | client *slave = ln->value; |
no test coverage detected