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

Function sendBulkToSlave

src/replication.cpp:1856–1926  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1854}
1855
1856void sendBulkToSlave(connection *conn) {
1857 serverAssert(GlobalLocksAcquired());
1858
1859 client *replica = (client*)connGetPrivateData(conn);
1860 serverAssert(FCorrectThread(replica));
1861 ssize_t nwritten;
1862 AeLocker aeLock;
1863 std::unique_lock<fastlock> ul(replica->lock);
1864
1865 /* Before sending the RDB file, we send the preamble as configured by the
1866 * replication process. Currently the preamble is just the bulk count of
1867 * the file in the form "$<length>\r\n". */
1868 if (replica->replpreamble) {
1869 nwritten = connWrite(conn,replica->replpreamble,sdslen(replica->replpreamble));
1870 if (nwritten == -1) {
1871 serverLog(LL_VERBOSE,
1872 "Write error sending RDB preamble to replica: %s",
1873 connGetLastError(conn));
1874 ul.unlock();
1875 aeLock.arm(nullptr);
1876 freeClient(replica);
1877 return;
1878 }
1879 g_pserver->stat_net_output_bytes += nwritten;
1880 sdsrange(replica->replpreamble,nwritten,-1);
1881 if (sdslen(replica->replpreamble) == 0) {
1882 sdsfree(replica->replpreamble);
1883 replica->replpreamble = NULL;
1884 /* fall through sending data. */
1885 } else {
1886 return;
1887 }
1888 }
1889
1890 /* If the preamble was already transferred, send the RDB bulk data.
1891 * try to use sendfile system call if supported, unless tls is enabled.
1892 * fallback to normal read+write otherwise. */
1893 nwritten = 0;
1894 ssize_t buflen;
1895 char buf[PROTO_IOBUF_LEN];
1896
1897 lseek(replica->repldbfd,replica->repldboff,SEEK_SET);
1898 buflen = read(replica->repldbfd,buf,PROTO_IOBUF_LEN);
1899 if (buflen <= 0) {
1900 serverLog(LL_WARNING,"Read error sending DB to replica: %s",
1901 (buflen == 0) ? "premature EOF" : strerror(errno));
1902 ul.unlock();
1903 aeLock.arm(nullptr);
1904 freeClient(replica);
1905 return;
1906 }
1907 if ((nwritten = connWrite(conn,buf,buflen)) == -1) {
1908 if (connGetState(conn) != CONN_STATE_CONNECTED) {
1909 serverLog(LL_WARNING,"Write error sending DB to replica: %s",
1910 connGetLastError(conn));
1911 ul.unlock();
1912 aeLock.arm(nullptr);
1913 freeClient(replica);

Callers

nothing calls this directly

Calls 15

GlobalLocksAcquiredFunction · 0.85
connGetPrivateDataFunction · 0.85
FCorrectThreadFunction · 0.85
connWriteFunction · 0.85
sdslenFunction · 0.85
serverLogFunction · 0.85
connGetLastErrorFunction · 0.85
sdsrangeFunction · 0.85
sdsfreeFunction · 0.85
connGetStateFunction · 0.85
connSetWriteHandlerFunction · 0.85
putSlaveOnlineFunction · 0.85

Tested by

no test coverage detected