Append data to the AOF rewrite buffer, allocating new blocks if needed. */
| 141 | |
| 142 | /* Append data to the AOF rewrite buffer, allocating new blocks if needed. */ |
| 143 | void aofRewriteBufferAppend(unsigned char *s, unsigned long len) { |
| 144 | listNode *ln = listLast(g_pserver->aof_rewrite_buf_blocks); |
| 145 | aofrwblock *block = (aofrwblock*)(ln ? ln->value : NULL); |
| 146 | |
| 147 | while(len) { |
| 148 | /* If we already got at least an allocated block, try appending |
| 149 | * at least some piece into it. */ |
| 150 | if (block) { |
| 151 | unsigned long thislen = (block->free < len) ? block->free : len; |
| 152 | if (thislen) { /* The current block is not already full. */ |
| 153 | memcpy(block->buf+block->used, s, thislen); |
| 154 | block->used += thislen; |
| 155 | block->free -= thislen; |
| 156 | s += thislen; |
| 157 | len -= thislen; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (len) { /* First block to allocate, or need another block. */ |
| 162 | int numblocks; |
| 163 | |
| 164 | block = (aofrwblock*)zmalloc(sizeof(*block), MALLOC_LOCAL); |
| 165 | block->free = AOF_RW_BUF_BLOCK_SIZE; |
| 166 | block->used = 0; |
| 167 | listAddNodeTail(g_pserver->aof_rewrite_buf_blocks,block); |
| 168 | |
| 169 | /* Log every time we cross more 10 or 100 blocks, respectively |
| 170 | * as a notice or warning. */ |
| 171 | numblocks = listLength(g_pserver->aof_rewrite_buf_blocks); |
| 172 | if (((numblocks+1) % 10) == 0) { |
| 173 | int level = ((numblocks+1) % 100) == 0 ? LL_WARNING : |
| 174 | LL_NOTICE; |
| 175 | serverLog(level,"Background AOF buffer size: %lu MB", |
| 176 | aofRewriteBufferSize()/(1024*1024)); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* Install a file event to send data to the rewrite child if there is |
| 182 | * not one already. */ |
| 183 | installAofRewriteEvent(); |
| 184 | } |
| 185 | |
| 186 | /* Write the buffer (possibly composed of multiple blocks) into the specified |
| 187 | * fd. If a short write or any other error happens -1 is returned, |
no test coverage detected