walRead.c 15.5 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
S
Shengliang Guan 已提交
14 15
 */

L
Liu Jicong 已提交
16
#include "taoserror.h"
L
Liu Jicong 已提交
17
#include "walInt.h"
S
Shengliang Guan 已提交
18

L
Liu Jicong 已提交
19 20 21 22
static int32_t walFetchHeadNew(SWalReader *pRead, int64_t fetchVer);
static int32_t walFetchBodyNew(SWalReader *pRead);
static int32_t walSkipFetchBodyNew(SWalReader *pRead);

L
Liu Jicong 已提交
23
SWalReader *walOpenReader(SWal *pWal, SWalFilterCond *cond) {
24 25
  SWalReader *pReader = taosMemoryCalloc(1, sizeof(SWalReader));
  if (pReader == NULL) {
26
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
27 28
    return NULL;
  }
29

30 31 32 33 34 35 36 37
  pReader->pWal = pWal;
  pReader->readerId = tGenIdPI64();
  pReader->pIdxFile = NULL;
  pReader->pLogFile = NULL;
  pReader->curVersion = -1;
  pReader->curFileFirstVer = -1;
  pReader->curInvalid = 1;
  pReader->capacity = 0;
L
Liu Jicong 已提交
38
  if (cond) {
39
    pReader->cond = *cond;
L
Liu Jicong 已提交
40
  } else {
41 42 43 44
    pReader->cond.scanUncommited = 0;
    pReader->cond.scanNotApplied = 0;
    pReader->cond.scanMeta = 0;
    pReader->cond.enableRef = 0;
L
Liu Jicong 已提交
45
  }
L
fix  
Liu Jicong 已提交
46

47
  taosThreadMutexInit(&pReader->mutex, NULL);
L
fix  
Liu Jicong 已提交
48

49 50
  pReader->pHead = taosMemoryMalloc(sizeof(SWalCkHead));
  if (pReader->pHead == NULL) {
L
Liu Jicong 已提交
51
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
52
    taosMemoryFree(pReader);
L
Liu Jicong 已提交
53 54
    return NULL;
  }
L
Liu Jicong 已提交
55

56
  /*if (pReader->cond.enableRef) {*/
L
Liu Jicong 已提交
57
  /* taosHashPut(pWal->pRefHash, &pReader->readerId, sizeof(int64_t), &pReader, sizeof(void *));*/
58 59 60
  /*}*/

  return pReader;
L
Liu Jicong 已提交
61 62
}

63 64 65 66 67 68 69 70
void walCloseReader(SWalReader *pReader) {
  taosCloseFile(&pReader->pIdxFile);
  taosCloseFile(&pReader->pLogFile);
  /*if (pReader->cond.enableRef) {*/
  /*taosHashRemove(pReader->pWal->pRefHash, &pReader->readerId, sizeof(int64_t));*/
  /*}*/
  taosMemoryFreeClear(pReader->pHead);
  taosMemoryFree(pReader);
L
Liu Jicong 已提交
71 72
}

73 74 75 76 77 78
int32_t walNextValidMsg(SWalReader *pReader) {
  int64_t fetchVer = pReader->curVersion;
  int64_t lastVer = walGetLastVer(pReader->pWal);
  int64_t committedVer = walGetCommittedVer(pReader->pWal);
  int64_t appliedVer = walGetAppliedVer(pReader->pWal);
  int64_t endVer = pReader->cond.scanUncommited ? lastVer : committedVer;
L
Liu Jicong 已提交
79 80 81
  endVer = TMIN(appliedVer, endVer);

  wDebug("vgId:%d wal start to fetch, ver %ld, last ver %ld commit ver %ld, applied ver %ld, end ver %ld",
82 83
         pReader->pWal->cfg.vgId, fetchVer, lastVer, committedVer, appliedVer, endVer);
  pReader->curStopped = 0;
L
Liu Jicong 已提交
84
  while (fetchVer <= endVer) {
85
    if (walFetchHeadNew(pReader, fetchVer) < 0) {
L
Liu Jicong 已提交
86 87
      return -1;
    }
88 89 90
    if (pReader->pHead->head.msgType == TDMT_VND_SUBMIT ||
        (IS_META_MSG(pReader->pHead->head.msgType) && pReader->cond.scanMeta)) {
      if (walFetchBodyNew(pReader) < 0) {
L
Liu Jicong 已提交
91 92 93 94
        return -1;
      }
      return 0;
    } else {
95
      if (walSkipFetchBodyNew(pReader) < 0) {
L
Liu Jicong 已提交
96 97 98
        return -1;
      }
      fetchVer++;
99
      ASSERT(fetchVer == pReader->curVersion);
L
Liu Jicong 已提交
100 101
    }
  }
102
  pReader->curStopped = 1;
L
Liu Jicong 已提交
103
  return -1;
L
Liu Jicong 已提交
104
}
L
Liu Jicong 已提交
105

106
static int64_t walReadSeekFilePos(SWalReader *pReader, int64_t fileFirstVer, int64_t ver) {
L
Liu Jicong 已提交
107
  int64_t ret = 0;
L
Liu Jicong 已提交
108

109 110
  TdFilePtr pIdxTFile = pReader->pIdxFile;
  TdFilePtr pLogTFile = pReader->pLogFile;
L
Liu Jicong 已提交
111 112

  // seek position
L
Liu Jicong 已提交
113
  int64_t offset = (ver - fileFirstVer) * sizeof(SWalIdxEntry);
L
Liu Jicong 已提交
114 115
  ret = taosLSeekFile(pIdxTFile, offset, SEEK_SET);
  if (ret < 0) {
L
Liu Jicong 已提交
116
    terrno = TAOS_SYSTEM_ERROR(errno);
117 118
    wError("vgId:%d, failed to seek idx file, index:%" PRId64 ", pos:%" PRId64 ", since %s", pReader->pWal->cfg.vgId,
           ver, offset, terrstr());
L
Liu Jicong 已提交
119 120
    return -1;
  }
L
Liu Jicong 已提交
121
  SWalIdxEntry entry = {0};
L
Liu Jicong 已提交
122 123 124
  if ((ret = taosReadFile(pIdxTFile, &entry, sizeof(SWalIdxEntry))) != sizeof(SWalIdxEntry)) {
    if (ret < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
125
      wError("vgId:%d, failed to read idx file, since %s", pReader->pWal->cfg.vgId, terrstr());
L
Liu Jicong 已提交
126 127
    } else {
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
S
Shengliang Guan 已提交
128
      wError("vgId:%d, read idx file incompletely, read bytes %" PRId64 ", bytes should be %" PRIu64,
129
             pReader->pWal->cfg.vgId, ret, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
130
    }
L
Liu Jicong 已提交
131 132
    return -1;
  }
L
Liu Jicong 已提交
133

L
Liu Jicong 已提交
134
  ASSERT(entry.ver == ver);
L
Liu Jicong 已提交
135 136
  ret = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET);
  if (ret < 0) {
L
Liu Jicong 已提交
137
    terrno = TAOS_SYSTEM_ERROR(errno);
138 139
    wError("vgId:%d, failed to seek log file, index:%" PRId64 ", pos:%" PRId64 ", since %s", pReader->pWal->cfg.vgId,
           ver, entry.offset, terrstr());
L
Liu Jicong 已提交
140 141
    return -1;
  }
L
Liu Jicong 已提交
142
  return ret;
L
Liu Jicong 已提交
143 144
}

145
static int32_t walReadChangeFile(SWalReader *pReader, int64_t fileFirstVer) {
L
Liu Jicong 已提交
146 147
  char fnameStr[WAL_FILE_LEN];

148 149
  taosCloseFile(&pReader->pIdxFile);
  taosCloseFile(&pReader->pLogFile);
L
Liu Jicong 已提交
150

151 152 153
  walBuildLogName(pReader->pWal, fileFirstVer, fnameStr);
  TdFilePtr pLogFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pLogFile == NULL) {
L
Liu Jicong 已提交
154
    terrno = TAOS_SYSTEM_ERROR(errno);
155
    wError("vgId:%d, cannot open file %s, since %s", pReader->pWal->cfg.vgId, fnameStr, terrstr());
L
Liu Jicong 已提交
156 157 158
    return -1;
  }

159
  pReader->pLogFile = pLogFile;
L
Liu Jicong 已提交
160

161 162 163
  walBuildIdxName(pReader->pWal, fileFirstVer, fnameStr);
  TdFilePtr pIdxFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pIdxFile == NULL) {
L
Liu Jicong 已提交
164
    terrno = TAOS_SYSTEM_ERROR(errno);
165
    wError("vgId:%d, cannot open file %s, since %s", pReader->pWal->cfg.vgId, fnameStr, terrstr());
L
Liu Jicong 已提交
166 167 168
    return -1;
  }

169
  pReader->pIdxFile = pIdxFile;
L
Liu Jicong 已提交
170 171 172
  return 0;
}

173 174
int32_t walReadSeekVerImpl(SWalReader *pReader, int64_t ver) {
  SWal *pWal = pReader->pWal;
L
Liu Jicong 已提交
175

176
  // bsearch in fileSet
L
Liu Jicong 已提交
177
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
178
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
179
  SWalFileInfo *pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
L
Liu Jicong 已提交
180
  ASSERT(pRet != NULL);
181
  if (pReader->curFileFirstVer != pRet->firstVer) {
L
Liu Jicong 已提交
182
    // error code was set inner
183
    if (walReadChangeFile(pReader, pRet->firstVer) < 0) {
L
Liu Jicong 已提交
184 185 186 187
      return -1;
    }
  }

L
Liu Jicong 已提交
188
  // error code was set inner
189
  if (walReadSeekFilePos(pReader, pRet->firstVer, ver) < 0) {
L
Liu Jicong 已提交
190 191
    return -1;
  }
L
Liu Jicong 已提交
192

193
  wDebug("wal version reset from %ld(invalid: %d) to %ld", pReader->curVersion, pReader->curInvalid, ver);
L
Liu Jicong 已提交
194

195
  pReader->curVersion = ver;
L
Liu Jicong 已提交
196 197 198
  return 0;
}

199 200 201
int32_t walReadSeekVer(SWalReader *pReader, int64_t ver) {
  SWal *pWal = pReader->pWal;
  if (!pReader->curInvalid && ver == pReader->curVersion) {
L
Liu Jicong 已提交
202 203 204 205
    wDebug("wal version %ld match, no need to reset", ver);
    return 0;
  }

206 207
  pReader->curInvalid = 1;
  pReader->curVersion = ver;
L
Liu Jicong 已提交
208

L
Liu Jicong 已提交
209
  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
210
    wDebug("vgId:%d, invalid index:%" PRId64 ", first index:%" PRId64 ", last index:%" PRId64, pReader->pWal->cfg.vgId,
L
Liu Jicong 已提交
211 212 213 214 215 216 217
           ver, pWal->vers.firstVer, pWal->vers.lastVer);
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }
  if (ver < pWal->vers.snapshotVer) {
  }

218
  if (walReadSeekVerImpl(pReader, ver) < 0) {
L
Liu Jicong 已提交
219 220 221
    return -1;
  }

L
Liu Jicong 已提交
222 223 224
  return 0;
}

L
Liu Jicong 已提交
225
void walSetReaderCapacity(SWalReader *pRead, int32_t capacity) { pRead->capacity = capacity; }
226

L
Liu Jicong 已提交
227 228
static int32_t walFetchHeadNew(SWalReader *pRead, int64_t fetchVer) {
  int64_t contLen;
L
Liu Jicong 已提交
229 230
  bool    seeked = false;

231 232
  wDebug("vgId:%d, wal starts to fetch head %d", pRead->pWal->cfg.vgId, fetchVer);

L
Liu Jicong 已提交
233
  if (pRead->curInvalid || pRead->curVersion != fetchVer) {
L
Liu Jicong 已提交
234 235
    if (walReadSeekVer(pRead, fetchVer) < 0) {
      ASSERT(0);
L
Liu Jicong 已提交
236 237
      pRead->curVersion = fetchVer;
      pRead->curInvalid = 1;
L
Liu Jicong 已提交
238 239
      return -1;
    }
L
Liu Jicong 已提交
240 241 242 243 244 245 246 247 248 249
    seeked = true;
  }
  while (1) {
    contLen = taosReadFile(pRead->pLogFile, pRead->pHead, sizeof(SWalCkHead));
    if (contLen == sizeof(SWalCkHead)) {
      break;
    } else if (contLen == 0 && !seeked) {
      walReadSeekVerImpl(pRead, fetchVer);
      seeked = true;
      continue;
L
Liu Jicong 已提交
250
    } else {
L
Liu Jicong 已提交
251 252 253 254 255 256 257 258
      if (contLen < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
      } else {
        terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      }
      ASSERT(0);
      pRead->curInvalid = 1;
      return -1;
L
Liu Jicong 已提交
259 260
    }
  }
L
Liu Jicong 已提交
261
  pRead->curInvalid = 0;
L
Liu Jicong 已提交
262 263 264 265 266 267 268
  return 0;
}

static int32_t walFetchBodyNew(SWalReader *pRead) {
  SWalCont *pReadHead = &pRead->pHead->head;
  int64_t   ver = pReadHead->version;

269 270
  wDebug("vgId:%d, wal starts to fetch body %ld", pRead->pWal->cfg.vgId, ver);

L
Liu Jicong 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284
  if (pRead->capacity < pReadHead->bodyLen) {
    void *ptr = taosMemoryRealloc(pRead->pHead, sizeof(SWalCkHead) + pReadHead->bodyLen);
    if (ptr == NULL) {
      terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
      return -1;
    }
    pRead->pHead = ptr;
    pReadHead = &pRead->pHead->head;
    pRead->capacity = pReadHead->bodyLen;
  }

  if (pReadHead->bodyLen != taosReadFile(pRead->pLogFile, pReadHead->body, pReadHead->bodyLen)) {
    if (pReadHead->bodyLen < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
285
      wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since %s",
S
Shengliang Guan 已提交
286
             pRead->pWal->cfg.vgId, pRead->pHead->head.version, ver, tstrerror(terrno));
L
Liu Jicong 已提交
287
    } else {
S
Shengliang Guan 已提交
288
      wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since file corrupted",
S
Shengliang Guan 已提交
289
             pRead->pWal->cfg.vgId, pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
290 291
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    }
L
Liu Jicong 已提交
292
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
293 294 295 296 297
    ASSERT(0);
    return -1;
  }

  if (pReadHead->version != ver) {
S
Shengliang Guan 已提交
298
    wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64, pRead->pWal->cfg.vgId,
S
Shengliang Guan 已提交
299
           pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
300
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
301 302 303 304 305 306
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    ASSERT(0);
    return -1;
  }

  if (walValidBodyCksum(pRead->pHead) != 0) {
S
Shengliang Guan 已提交
307
    wError("vgId:%d, wal fetch body error:%" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
308
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
309 310 311 312 313
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    ASSERT(0);
    return -1;
  }

314
  wDebug("version %ld is fetched, cursor advance", ver);
L
Liu Jicong 已提交
315 316 317 318 319 320 321 322
  pRead->curVersion = ver + 1;
  return 0;
}

static int32_t walSkipFetchBodyNew(SWalReader *pRead) {
  int64_t code;

  ASSERT(pRead->curVersion == pRead->pHead->head.version);
L
Liu Jicong 已提交
323
  ASSERT(pRead->curInvalid == 0);
L
Liu Jicong 已提交
324 325 326 327

  code = taosLSeekFile(pRead->pLogFile, pRead->pHead->head.bodyLen, SEEK_CUR);
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
328
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
329
    ASSERT(0);
L
Liu Jicong 已提交
330 331 332 333
    return -1;
  }

  pRead->curVersion++;
L
Liu Jicong 已提交
334
  wDebug("version advance to %ld, skip fetch", pRead->curVersion);
L
Liu Jicong 已提交
335 336 337 338

  return 0;
}

L
Liu Jicong 已提交
339
int32_t walFetchHead(SWalReader *pRead, int64_t ver, SWalCkHead *pHead) {
L
Liu Jicong 已提交
340
  int64_t code;
L
Liu Jicong 已提交
341

342
  // TODO: valid ver
L
Liu Jicong 已提交
343 344 345
  if (ver > pRead->pWal->vers.commitVer) {
    return -1;
  }
346

L
Liu Jicong 已提交
347
  if (pRead->curInvalid || pRead->curVersion != ver) {
348 349 350 351
    code = walReadSeekVer(pRead, ver);
    if (code < 0) return -1;
  }

L
Liu Jicong 已提交
352
  ASSERT(taosValidFile(pRead->pLogFile) == true);
353

L
Liu Jicong 已提交
354
  code = taosReadFile(pRead->pLogFile, pHead, sizeof(SWalCkHead));
L
Liu Jicong 已提交
355
  if (code != sizeof(SWalCkHead)) {
356 357 358 359 360 361
    return -1;
  }

  code = walValidHeadCksum(pHead);

  if (code != 0) {
L
Liu Jicong 已提交
362
    wError("vgId:%d, unexpected wal log index:%" PRId64 ", since head checksum not passed", pRead->pWal->cfg.vgId, ver);
363 364 365 366 367 368 369
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  return 0;
}

L
Liu Jicong 已提交
370
int32_t walSkipFetchBody(SWalReader *pRead, const SWalCkHead *pHead) {
L
Liu Jicong 已提交
371
  int64_t code;
372 373 374

  ASSERT(pRead->curVersion == pHead->head.version);

L
Liu Jicong 已提交
375
  code = taosLSeekFile(pRead->pLogFile, pHead->head.bodyLen, SEEK_CUR);
376 377
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
378
    pRead->curInvalid = 1;
379 380 381 382 383 384 385 386
    return -1;
  }

  pRead->curVersion++;

  return 0;
}

L
Liu Jicong 已提交
387
int32_t walFetchBody(SWalReader *pRead, SWalCkHead **ppHead) {
L
Liu Jicong 已提交
388 389
  SWalCont *pReadHead = &((*ppHead)->head);
  int64_t   ver = pReadHead->version;
390 391

  if (pRead->capacity < pReadHead->bodyLen) {
L
Liu Jicong 已提交
392
    void *ptr = taosMemoryRealloc(*ppHead, sizeof(SWalCkHead) + pReadHead->bodyLen);
393 394 395 396 397
    if (ptr == NULL) {
      terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
      return -1;
    }
    *ppHead = ptr;
L
Liu Jicong 已提交
398
    pReadHead = &((*ppHead)->head);
399 400 401
    pRead->capacity = pReadHead->bodyLen;
  }

L
Liu Jicong 已提交
402
  if (pReadHead->bodyLen != taosReadFile(pRead->pLogFile, pReadHead->body, pReadHead->bodyLen)) {
L
Liu Jicong 已提交
403
    ASSERT(0);
404 405 406 407
    return -1;
  }

  if (pReadHead->version != ver) {
S
Shengliang Guan 已提交
408
    wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64, pRead->pWal->cfg.vgId,
S
Shengliang Guan 已提交
409
           pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
410
    pRead->curInvalid = 1;
411 412 413 414 415
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  if (walValidBodyCksum(*ppHead) != 0) {
S
Shengliang Guan 已提交
416
    wError("vgId:%d, wal fetch body error:%" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
417
    pRead->curInvalid = 1;
418 419 420 421 422 423 424 425
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  pRead->curVersion = ver + 1;
  return 0;
}

426 427
int32_t walReadVer(SWalReader *pReader, int64_t ver) {
  wDebug("vgId:%d wal start to read ver %ld", pReader->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
428
  int64_t contLen;
429
  int32_t code;
L
Liu Jicong 已提交
430
  bool    seeked = false;
431

432
  if (pReader->pWal->vers.firstVer == -1) {
433 434 435 436
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

437 438 439
  if (ver > pReader->pWal->vers.lastVer || ver < pReader->pWal->vers.firstVer) {
    wDebug("vgId:%d, invalid index:%" PRId64 ", first index:%" PRId64 ", last index:%" PRId64, pReader->pWal->cfg.vgId,
           ver, pReader->pWal->vers.firstVer, pReader->pWal->vers.lastVer);
M
Minghao Li 已提交
440 441 442 443
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

444 445 446
  if (pReader->curInvalid || pReader->curVersion != ver) {
    if (walReadSeekVer(pReader, ver) < 0) {
      wError("vgId:%d, unexpected wal log, index:%" PRId64 ", since %s", pReader->pWal->cfg.vgId, ver, terrstr());
L
Liu Jicong 已提交
447 448 449 450
      return -1;
    }
    seeked = true;
  }
L
Liu Jicong 已提交
451

L
Liu Jicong 已提交
452
  while (1) {
453
    contLen = taosReadFile(pReader->pLogFile, pReader->pHead, sizeof(SWalCkHead));
L
Liu Jicong 已提交
454 455 456
    if (contLen == sizeof(SWalCkHead)) {
      break;
    } else if (contLen == 0 && !seeked) {
457
      walReadSeekVerImpl(pReader, ver);
L
Liu Jicong 已提交
458 459 460 461 462 463 464 465
      seeked = true;
      continue;
    } else {
      if (contLen < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
      } else {
        terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      }
M
Minghao Li 已提交
466
      ASSERT(0);
L
Liu Jicong 已提交
467
      return -1;
M
Minghao Li 已提交
468
    }
L
Liu Jicong 已提交
469
  }
470

471 472 473
  code = walValidHeadCksum(pReader->pHead);
  if (code != 0) {
    wError("vgId:%d, unexpected wal log, index:%" PRId64 ", since head checksum not passed", pReader->pWal->cfg.vgId,
474
           ver);
L
Liu Jicong 已提交
475
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
476 477
    return -1;
  }
478

479 480
  if (pReader->capacity < pReader->pHead->head.bodyLen) {
    void *ptr = taosMemoryRealloc(pReader->pHead, sizeof(SWalCkHead) + pReader->pHead->head.bodyLen);
L
Liu Jicong 已提交
481
    if (ptr == NULL) {
L
Liu Jicong 已提交
482
      terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
483 484
      return -1;
    }
485 486
    pReader->pHead = ptr;
    pReader->capacity = pReader->pHead->head.bodyLen;
L
Liu Jicong 已提交
487
  }
L
Liu Jicong 已提交
488

489 490
  if ((contLen = taosReadFile(pReader->pLogFile, pReader->pHead->head.body, pReader->pHead->head.bodyLen)) !=
      pReader->pHead->head.bodyLen) {
L
Liu Jicong 已提交
491
    if (contLen < 0)
L
Liu Jicong 已提交
492
      terrno = TAOS_SYSTEM_ERROR(errno);
M
Minghao Li 已提交
493
    else {
L
Liu Jicong 已提交
494
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
M
Minghao Li 已提交
495 496
      ASSERT(0);
    }
L
Liu Jicong 已提交
497 498
    return -1;
  }
L
Liu Jicong 已提交
499

500 501 502 503
  if (pReader->pHead->head.version != ver) {
    wError("vgId:%d, unexpected wal log, index:%" PRId64 ", read request index:%" PRId64, pReader->pWal->cfg.vgId,
           pReader->pHead->head.version, ver);
    pReader->curInvalid = 1;
L
Liu Jicong 已提交
504
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
505
    ASSERT(0);
L
Liu Jicong 已提交
506 507
    return -1;
  }
L
Liu Jicong 已提交
508

509 510 511
  code = walValidBodyCksum(pReader->pHead);
  if (code != 0) {
    wError("vgId:%d, unexpected wal log, index:%" PRId64 ", since body checksum not passed", pReader->pWal->cfg.vgId,
512
           ver);
513 514 515 516
    uint32_t readCkSum = walCalcBodyCksum(pReader->pHead->head.body, pReader->pHead->head.bodyLen);
    uint32_t logCkSum = pReader->pHead->cksumBody;
    wError("checksum written into log: %u, checksum calculated: %u", logCkSum, readCkSum);
    pReader->curInvalid = 1;
L
Liu Jicong 已提交
517
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
518
    ASSERT(0);
L
Liu Jicong 已提交
519 520
    return -1;
  }
521
  pReader->curVersion++;
L
Liu Jicong 已提交
522 523 524

  return 0;
}