1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
| int rdbLoad(char *filename, rdbSaveInfo *rsi, int rdbflags) { FILE *fp; rio rdb; int retval;
if ((fp = fopen(filename,"r")) == NULL) return C_ERR; startLoadingFile(fp, filename,rdbflags); rioInitWithFile(&rdb,fp); retval = rdbLoadRio(&rdb,rdbflags,rsi); fclose(fp); stopLoading(retval==C_OK); return retval; }
int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) { uint64_t dbid; int type, rdbver; redisDb *db = server.db+0; char buf[1024];
rdb->update_cksum = rdbLoadProgressCallback; rdb->max_processing_chunk = server.loading_process_events_interval_bytes; if (rioRead(rdb,buf,9) == 0) goto eoferr; buf[9] = '\0'; if (memcmp(buf,"REDIS",5) != 0) { serverLog(LL_WARNING,"Wrong signature trying to load DB from file"); errno = EINVAL; return C_ERR; } rdbver = atoi(buf+5); if (rdbver < 1 || rdbver > RDB_VERSION) { serverLog(LL_WARNING,"Can't handle RDB format version %d",rdbver); errno = EINVAL; return C_ERR; }
long long lru_idle = -1, lfu_freq = -1, expiretime = -1, now = mstime(); long long lru_clock = LRU_CLOCK();
while(1) { robj *key, *val;
if ((type = rdbLoadType(rdb)) == -1) goto eoferr;
if (type == RDB_OPCODE_EXPIRETIME) {
expiretime = rdbLoadTime(rdb); expiretime *= 1000; if (rioGetReadError(rdb)) goto eoferr; continue; } else if (type == RDB_OPCODE_EXPIRETIME_MS) {
expiretime = rdbLoadMillisecondTime(rdb,rdbver); if (rioGetReadError(rdb)) goto eoferr; continue; } else if (type == RDB_OPCODE_FREQ) { uint8_t byte; if (rioRead(rdb,&byte,1) == 0) goto eoferr; lfu_freq = byte; continue; } else if (type == RDB_OPCODE_IDLE) { uint64_t qword; if ((qword = rdbLoadLen(rdb,NULL)) == RDB_LENERR) goto eoferr; lru_idle = qword; continue; } else if (type == RDB_OPCODE_EOF) { break; } else if (type == RDB_OPCODE_SELECTDB) { if ((dbid = rdbLoadLen(rdb,NULL)) == RDB_LENERR) goto eoferr; if (dbid >= (unsigned)server.dbnum) { serverLog(LL_WARNING, "FATAL: Data file was created with a Redis " "server configured to handle more than %d " "databases. Exiting\n", server.dbnum); exit(1); } db = server.db+dbid; continue; } else if (type == RDB_OPCODE_RESIZEDB) {
uint64_t db_size, expires_size; if ((db_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR) goto eoferr; if ((expires_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR) goto eoferr; dictExpand(db->dict,db_size); dictExpand(db->expires,expires_size); continue; } else if (type == RDB_OPCODE_AUX) {
robj *auxkey, *auxval; if ((auxkey = rdbLoadStringObject(rdb)) == NULL) goto eoferr; if ((auxval = rdbLoadStringObject(rdb)) == NULL) goto eoferr;
if (((char*)auxkey->ptr)[0] == '%') {
serverLog(LL_NOTICE,"RDB '%s': %s", (char*)auxkey->ptr, (char*)auxval->ptr); } else if (!strcasecmp(auxkey->ptr,"repl-stream-db")) { if (rsi) rsi->repl_stream_db = atoi(auxval->ptr); } else if (!strcasecmp(auxkey->ptr,"repl-id")) { if (rsi && sdslen(auxval->ptr) == CONFIG_RUN_ID_SIZE) { memcpy(rsi->repl_id,auxval->ptr,CONFIG_RUN_ID_SIZE+1); rsi->repl_id_is_set = 1; } } else if (!strcasecmp(auxkey->ptr,"repl-offset")) { if (rsi) rsi->repl_offset = strtoll(auxval->ptr,NULL,10); } else if (!strcasecmp(auxkey->ptr,"lua")) { if (luaCreateFunction(NULL,server.lua,auxval) == NULL) { rdbExitReportCorruptRDB( "Can't load Lua script from RDB file! " "BODY: %s", auxval->ptr); } } else if (!strcasecmp(auxkey->ptr,"redis-ver")) { serverLog(LL_NOTICE,"Loading RDB produced by version %s", (char*)auxval->ptr); } else if (!strcasecmp(auxkey->ptr,"ctime")) { time_t age = time(NULL)-strtol(auxval->ptr,NULL,10); if (age < 0) age = 0; serverLog(LL_NOTICE,"RDB age %ld seconds", (unsigned long) age); } else if (!strcasecmp(auxkey->ptr,"used-mem")) { long long usedmem = strtoll(auxval->ptr,NULL,10); serverLog(LL_NOTICE,"RDB memory usage when created %.2f Mb", (double) usedmem / (1024*1024)); } else if (!strcasecmp(auxkey->ptr,"aof-preamble")) { long long haspreamble = strtoll(auxval->ptr,NULL,10); if (haspreamble) serverLog(LL_NOTICE,"RDB has an AOF tail"); } else if (!strcasecmp(auxkey->ptr,"redis-bits")) { } else {
serverLog(LL_DEBUG,"Unrecognized RDB AUX field: '%s'", (char*)auxkey->ptr); }
decrRefCount(auxkey); decrRefCount(auxval); continue; } else if (type == RDB_OPCODE_MODULE_AUX) {
uint64_t moduleid = rdbLoadLen(rdb,NULL); int when_opcode = rdbLoadLen(rdb,NULL); int when = rdbLoadLen(rdb,NULL); if (rioGetReadError(rdb)) goto eoferr; if (when_opcode != RDB_MODULE_OPCODE_UINT) rdbReportReadError("bad when_opcode"); moduleType *mt = moduleTypeLookupModuleByID(moduleid); char name[10]; moduleTypeNameByID(name,moduleid);
if (!rdbCheckMode && mt == NULL) { serverLog(LL_WARNING,"The RDB file contains AUX module data I can't load: no matching module '%s'", name); exit(1); } else if (!rdbCheckMode && mt != NULL) { if (!mt->aux_load) { serverLog(LL_WARNING,"The RDB file contains module AUX data, but the module '%s' doesn't seem to support it.", name); exit(1); }
RedisModuleIO io; moduleInitIOContext(io,mt,rdb,NULL); io.ver = 2;
if (mt->aux_load(&io,moduleid&1023, when) != REDISMODULE_OK || io.error) { moduleTypeNameByID(name,moduleid); serverLog(LL_WARNING,"The RDB file contains module AUX data for the module type '%s', that the responsible module is not able to load. Check for modules log above for additional clues.", name); exit(1); } if (io.ctx) { moduleFreeContext(io.ctx); zfree(io.ctx); } uint64_t eof = rdbLoadLen(rdb,NULL); if (eof != RDB_MODULE_OPCODE_EOF) { serverLog(LL_WARNING,"The RDB file contains module AUX data for the module '%s' that is not terminated by the proper module value EOF marker", name); exit(1); } continue; } else { robj *aux = rdbLoadCheckModuleValue(rdb,name); decrRefCount(aux); continue; } }
if ((key = rdbLoadStringObject(rdb)) == NULL) goto eoferr; if ((val = rdbLoadObject(type,rdb,key)) == NULL) { decrRefCount(key); goto eoferr; }
if (server.masterhost == NULL && !(rdbflags&RDBFLAGS_AOF_PREAMBLE) && expiretime != -1 && expiretime < now) { decrRefCount(key); decrRefCount(val); } else { dbAdd(db,key,val);
if (expiretime != -1) setExpire(NULL,db,key,expiretime);
objectSetLRUOrLFU(val,lfu_freq,lru_idle,lru_clock,1000);
decrRefCount(key); } if (server.key_load_delay) usleep(server.key_load_delay);
expiretime = -1; lfu_freq = -1; lru_idle = -1; } if (rdbver >= 5) { uint64_t cksum, expected = rdb->cksum;
if (rioRead(rdb,&cksum,8) == 0) goto eoferr; if (server.rdb_checksum) { memrev64ifbe(&cksum); if (cksum == 0) { serverLog(LL_WARNING,"RDB file was saved with checksum disabled: no check performed."); } else if (cksum != expected) { serverLog(LL_WARNING,"Wrong RDB checksum. Aborting now."); rdbExitReportCorruptRDB("RDB CRC error"); } } } return C_OK;
eoferr: serverLog(LL_WARNING, "Short read or OOM loading DB. Unrecoverable error, aborting now."); rdbReportReadError("Unexpected EOF reading RDB file"); return C_ERR; }
|