2016-09-19 07:45:20 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "server.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-12-20 13:23:20 -05:00
|
|
|
typedef struct {
|
2021-02-16 09:06:51 -05:00
|
|
|
size_t keys;
|
|
|
|
size_t cow;
|
|
|
|
double progress;
|
|
|
|
childInfoType information_type; /* Type of information */
|
2020-12-20 13:23:20 -05:00
|
|
|
} child_info_data;
|
|
|
|
|
2016-09-19 07:45:20 -04:00
|
|
|
/* Open a child-parent channel used in order to move information about the
|
|
|
|
* RDB / AOF saving process from the child to the parent (for instance
|
|
|
|
* the amount of copy on write memory used) */
|
|
|
|
void openChildInfoPipe(void) {
|
|
|
|
if (pipe(server.child_info_pipe) == -1) {
|
|
|
|
/* On error our two file descriptors should be still set to -1,
|
2020-10-28 02:51:35 -04:00
|
|
|
* but we call anyway closeChildInfoPipe() since can't hurt. */
|
2016-09-19 07:45:20 -04:00
|
|
|
closeChildInfoPipe();
|
|
|
|
} else if (anetNonBlock(NULL,server.child_info_pipe[0]) != ANET_OK) {
|
|
|
|
closeChildInfoPipe();
|
2016-09-19 08:11:17 -04:00
|
|
|
} else {
|
2020-12-20 13:23:20 -05:00
|
|
|
server.child_info_nread = 0;
|
2016-09-19 07:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the pipes opened with openChildInfoPipe(). */
|
|
|
|
void closeChildInfoPipe(void) {
|
|
|
|
if (server.child_info_pipe[0] != -1 ||
|
|
|
|
server.child_info_pipe[1] != -1)
|
|
|
|
{
|
|
|
|
close(server.child_info_pipe[0]);
|
|
|
|
close(server.child_info_pipe[1]);
|
|
|
|
server.child_info_pipe[0] = -1;
|
|
|
|
server.child_info_pipe[1] = -1;
|
2020-12-20 13:23:20 -05:00
|
|
|
server.child_info_nread = 0;
|
2016-09-19 07:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
/* Send save data to parent. */
|
|
|
|
void sendChildInfoGeneric(childInfoType info_type, size_t keys, double progress, char *pname) {
|
2016-09-19 07:45:20 -04:00
|
|
|
if (server.child_info_pipe[1] == -1) return;
|
2020-12-20 13:23:20 -05:00
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
child_info_data data = {.information_type=info_type,
|
|
|
|
.keys=keys,
|
|
|
|
.cow=zmalloc_get_private_dirty(-1),
|
|
|
|
.progress=progress};
|
2020-12-20 13:23:20 -05:00
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
if (data.cow) {
|
|
|
|
serverLog((info_type == CHILD_INFO_TYPE_CURRENT_INFO) ? LL_VERBOSE : LL_NOTICE,
|
|
|
|
"%s: %zu MB of memory used by copy-on-write",
|
|
|
|
pname, data.cow/(1024*1024));
|
2016-09-19 07:45:20 -04:00
|
|
|
}
|
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
ssize_t wlen = sizeof(data);
|
|
|
|
|
|
|
|
if (write(server.child_info_pipe[1], &data, wlen) != wlen) {
|
|
|
|
/* Nothing to do on error, this will be detected by the other side. */
|
2020-12-20 13:23:20 -05:00
|
|
|
}
|
2021-02-16 09:06:51 -05:00
|
|
|
}
|
2020-12-20 13:23:20 -05:00
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
/* Update Child info. */
|
|
|
|
void updateChildInfo(childInfoType information_type, size_t cow, size_t keys, double progress) {
|
|
|
|
if (information_type == CHILD_INFO_TYPE_CURRENT_INFO) {
|
|
|
|
server.stat_current_cow_bytes = cow;
|
|
|
|
server.stat_current_save_keys_processed = keys;
|
|
|
|
if (progress != -1) server.stat_module_progress = progress;
|
|
|
|
} else if (information_type == CHILD_INFO_TYPE_AOF_COW_SIZE) {
|
|
|
|
server.stat_aof_cow_bytes = cow;
|
|
|
|
} else if (information_type == CHILD_INFO_TYPE_RDB_COW_SIZE) {
|
|
|
|
server.stat_rdb_cow_bytes = cow;
|
|
|
|
} else if (information_type == CHILD_INFO_TYPE_MODULE_COW_SIZE) {
|
|
|
|
server.stat_module_cow_bytes = cow;
|
2020-12-20 13:23:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
/* Read child info data from the pipe.
|
|
|
|
* if complete data read into the buffer,
|
|
|
|
* data is stored into *buffer, and returns 1.
|
2020-12-20 13:23:20 -05:00
|
|
|
* otherwise, the partial data is left in the buffer, waiting for the next read, and returns 0. */
|
2021-02-16 09:06:51 -05:00
|
|
|
int readChildInfo(childInfoType *information_type, size_t *cow, size_t *keys, double* progress) {
|
2020-12-20 13:23:20 -05:00
|
|
|
/* We are using here a static buffer in combination with the server.child_info_nread to handle short reads */
|
|
|
|
static child_info_data buffer;
|
|
|
|
ssize_t wlen = sizeof(buffer);
|
|
|
|
|
|
|
|
/* Do not overlap */
|
|
|
|
if (server.child_info_nread == wlen) server.child_info_nread = 0;
|
|
|
|
|
|
|
|
int nread = read(server.child_info_pipe[0], (char *)&buffer + server.child_info_nread, wlen - server.child_info_nread);
|
|
|
|
if (nread > 0) {
|
|
|
|
server.child_info_nread += nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have complete child info */
|
|
|
|
if (server.child_info_nread == wlen) {
|
2021-02-16 09:06:51 -05:00
|
|
|
*information_type = buffer.information_type;
|
|
|
|
*cow = buffer.cow;
|
|
|
|
*keys = buffer.keys;
|
|
|
|
*progress = buffer.progress;
|
2020-12-20 13:23:20 -05:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
/* Receive info data from child. */
|
2016-09-19 07:45:20 -04:00
|
|
|
void receiveChildInfo(void) {
|
|
|
|
if (server.child_info_pipe[0] == -1) return;
|
2020-12-20 13:23:20 -05:00
|
|
|
|
2021-02-16 09:06:51 -05:00
|
|
|
size_t cow;
|
|
|
|
size_t keys;
|
|
|
|
double progress;
|
|
|
|
childInfoType information_type;
|
2020-12-20 13:23:20 -05:00
|
|
|
|
2021-01-08 16:35:30 -05:00
|
|
|
/* Drain the pipe and update child info so that we get the final message. */
|
2021-02-16 09:06:51 -05:00
|
|
|
while (readChildInfo(&information_type, &cow, &keys, &progress)) {
|
|
|
|
updateChildInfo(information_type, cow, keys, progress);
|
2016-09-19 07:45:20 -04:00
|
|
|
}
|
|
|
|
}
|