Append data to the AOF rewrite buffer, allocating new blocks if needed. */
| 123 | |
| 124 | /* Append data to the AOF rewrite buffer, allocating new blocks if needed. */ |
| 125 | void aofRewriteBufferAppend(unsigned char *s, unsigned long len) { |
| 126 | listNode *ln = listLast(server.aof_rewrite_buf_blocks); |
| 127 | aofrwblock *block = ln ? ln->value : NULL; |
| 128 | |
| 129 | while(len) { |
| 130 | /* If we already got at least an allocated block, try appending |
| 131 | * at least some piece into it. */ |
| 132 | if (block) { |
| 133 | unsigned long thislen = (block->free < len) ? block->free : len; |
| 134 | if (thislen) { /* The current block is not already full. */ |
| 135 | memcpy(block->buf+block->used, s, thislen); |
| 136 | block->used += thislen; |
| 137 | block->free -= thislen; |
| 138 | s += thislen; |
| 139 | len -= thislen; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (len) { /* First block to allocate, or need another block. */ |
| 144 | int numblocks; |
| 145 | |
| 146 | block = zmalloc(sizeof(*block)); |
| 147 | block->free = AOF_RW_BUF_BLOCK_SIZE; |
| 148 | block->used = 0; |
| 149 | listAddNodeTail(server.aof_rewrite_buf_blocks,block); |
| 150 | |
| 151 | /* Log every time we cross more 10 or 100 blocks, respectively |
| 152 | * as a notice or warning. */ |
| 153 | numblocks = listLength(server.aof_rewrite_buf_blocks); |
| 154 | if (((numblocks+1) % 10) == 0) { |
| 155 | int level = ((numblocks+1) % 100) == 0 ? LL_WARNING : |
| 156 | LL_NOTICE; |
| 157 | serverLog(level,"Background AOF buffer size: %lu MB", |
| 158 | aofRewriteBufferSize()/(1024*1024)); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* Install a file event to send data to the rewrite child if there is |
| 164 | * not one already. */ |
| 165 | if (!server.aof_stop_sending_diff && |
| 166 | aeGetFileEvents(server.el,server.aof_pipe_write_data_to_child) == 0) |
| 167 | { |
| 168 | aeCreateFileEvent(server.el, server.aof_pipe_write_data_to_child, |
| 169 | AE_WRITABLE, aofChildWriteDiffData, NULL); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* Write the buffer (possibly composed of multiple blocks) into the specified |
| 174 | * fd. If a short write or any other error happens -1 is returned, |
no test coverage detected