You need to sign in or sign up before continuing.
walRead.c 14.3 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 24
SWalReader *walOpenReader(SWal *pWal, SWalFilterCond *cond) {
  SWalReader *pRead = taosMemoryMalloc(sizeof(SWalReader));
L
Liu Jicong 已提交
25
  if (pRead == NULL) {
26
    terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
27 28
    return NULL;
  }
29

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

  taosThreadMutexInit(&pRead->mutex, NULL);

L
Liu Jicong 已提交
47 48 49 50
  /*if (pRead->cond.enableRef) {*/
  /*walOpenRef(pWal);*/
  /*}*/

L
Liu Jicong 已提交
51
  pRead->pHead = taosMemoryMalloc(sizeof(SWalCkHead));
L
Liu Jicong 已提交
52
  if (pRead->pHead == NULL) {
L
Liu Jicong 已提交
53
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
54
    taosMemoryFree(pRead);
L
Liu Jicong 已提交
55 56
    return NULL;
  }
L
Liu Jicong 已提交
57

L
Liu Jicong 已提交
58
  return pRead;
L
Liu Jicong 已提交
59 60
}

L
Liu Jicong 已提交
61 62 63
void walCloseReader(SWalReader *pRead) {
  taosCloseFile(&pRead->pIdxFile);
  taosCloseFile(&pRead->pLogFile);
wafwerar's avatar
wafwerar 已提交
64 65
  taosMemoryFreeClear(pRead->pHead);
  taosMemoryFree(pRead);
L
Liu Jicong 已提交
66 67
}

L
Liu Jicong 已提交
68
int32_t walNextValidMsg(SWalReader *pRead) {
L
Liu Jicong 已提交
69
  wDebug("vgId:%d wal start to fetch", pRead->pWal->cfg.vgId);
L
Liu Jicong 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  int64_t fetchVer = pRead->curVersion;
  int64_t endVer = pRead->cond.scanUncommited ? walGetLastVer(pRead->pWal) : walGetCommittedVer(pRead->pWal);
  while (fetchVer <= endVer) {
    if (walFetchHeadNew(pRead, fetchVer) < 0) {
      return -1;
    }
    if (pRead->pHead->head.msgType == TDMT_VND_SUBMIT ||
        (IS_META_MSG(pRead->pHead->head.msgType) && pRead->cond.scanMeta)) {
      if (walFetchBodyNew(pRead) < 0) {
        return -1;
      }
      return 0;
    } else {
      if (walSkipFetchBodyNew(pRead) < 0) {
        return -1;
      }
      fetchVer++;
      ASSERT(fetchVer == pRead->curVersion);
    }
  }
  return -1;
L
Liu Jicong 已提交
91
}
L
Liu Jicong 已提交
92

L
Liu Jicong 已提交
93
static int64_t walReadSeekFilePos(SWalReader *pRead, int64_t fileFirstVer, int64_t ver) {
L
Liu Jicong 已提交
94
  int64_t ret = 0;
L
Liu Jicong 已提交
95

L
Liu Jicong 已提交
96 97
  TdFilePtr pIdxTFile = pRead->pIdxFile;
  TdFilePtr pLogTFile = pRead->pLogFile;
L
Liu Jicong 已提交
98 99

  // seek position
L
Liu Jicong 已提交
100
  int64_t offset = (ver - fileFirstVer) * sizeof(SWalIdxEntry);
L
Liu Jicong 已提交
101 102
  ret = taosLSeekFile(pIdxTFile, offset, SEEK_SET);
  if (ret < 0) {
L
Liu Jicong 已提交
103
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
104
    wError("vgId:%d, failed to seek idx file, index:%" PRId64 ", pos:%" PRId64 ", since %s", pRead->pWal->cfg.vgId, ver,
S
Shengliang Guan 已提交
105
           offset, terrstr());
L
Liu Jicong 已提交
106 107
    return -1;
  }
L
Liu Jicong 已提交
108
  SWalIdxEntry entry = {0};
L
Liu Jicong 已提交
109 110 111
  if ((ret = taosReadFile(pIdxTFile, &entry, sizeof(SWalIdxEntry))) != sizeof(SWalIdxEntry)) {
    if (ret < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
112
      wError("vgId:%d, failed to read idx file, since %s", pRead->pWal->cfg.vgId, terrstr());
L
Liu Jicong 已提交
113 114
    } else {
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
S
Shengliang Guan 已提交
115 116
      wError("vgId:%d, read idx file incompletely, read bytes %" PRId64 ", bytes should be %" PRIu64,
             pRead->pWal->cfg.vgId, ret, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
117
    }
L
Liu Jicong 已提交
118 119
    return -1;
  }
L
Liu Jicong 已提交
120

L
Liu Jicong 已提交
121
  ASSERT(entry.ver == ver);
L
Liu Jicong 已提交
122 123
  ret = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET);
  if (ret < 0) {
L
Liu Jicong 已提交
124
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
125
    wError("vgId:%d, failed to seek log file, index:%" PRId64 ", pos:%" PRId64 ", since %s", pRead->pWal->cfg.vgId, ver,
S
Shengliang Guan 已提交
126
           entry.offset, terrstr());
L
Liu Jicong 已提交
127 128
    return -1;
  }
L
Liu Jicong 已提交
129
  return ret;
L
Liu Jicong 已提交
130 131
}

L
Liu Jicong 已提交
132
static int32_t walReadChangeFile(SWalReader *pRead, int64_t fileFirstVer) {
L
Liu Jicong 已提交
133 134
  char fnameStr[WAL_FILE_LEN];

L
Liu Jicong 已提交
135 136
  taosCloseFile(&pRead->pIdxFile);
  taosCloseFile(&pRead->pLogFile);
L
Liu Jicong 已提交
137 138

  walBuildLogName(pRead->pWal, fileFirstVer, fnameStr);
139 140
  TdFilePtr pLogTFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pLogTFile == NULL) {
L
Liu Jicong 已提交
141
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
142
    wError("vgId:%d, cannot open file %s, since %s", pRead->pWal->cfg.vgId, fnameStr, terrstr());
L
Liu Jicong 已提交
143 144 145
    return -1;
  }

L
Liu Jicong 已提交
146
  pRead->pLogFile = pLogTFile;
L
Liu Jicong 已提交
147

L
Liu Jicong 已提交
148
  walBuildIdxName(pRead->pWal, fileFirstVer, fnameStr);
149 150
  TdFilePtr pIdxTFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pIdxTFile == NULL) {
L
Liu Jicong 已提交
151
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
152
    wError("vgId:%d, cannot open file %s, since %s", pRead->pWal->cfg.vgId, fnameStr, terrstr());
L
Liu Jicong 已提交
153 154 155
    return -1;
  }

L
Liu Jicong 已提交
156
  pRead->pIdxFile = pIdxTFile;
L
Liu Jicong 已提交
157 158 159
  return 0;
}

L
Liu Jicong 已提交
160
int32_t walReadSeekVerImpl(SWalReader *pRead, int64_t ver) {
L
Liu Jicong 已提交
161 162
  SWal *pWal = pRead->pWal;

L
Liu Jicong 已提交
163
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
164
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
165
  // bsearch in fileSet
L
Liu Jicong 已提交
166
  SWalFileInfo *pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
L
Liu Jicong 已提交
167
  ASSERT(pRet != NULL);
L
Liu Jicong 已提交
168
  if (pRead->curFileFirstVer != pRet->firstVer) {
L
Liu Jicong 已提交
169
    // error code was set inner
L
Liu Jicong 已提交
170
    if (walReadChangeFile(pRead, pRet->firstVer) < 0) {
L
Liu Jicong 已提交
171 172 173 174
      return -1;
    }
  }

L
Liu Jicong 已提交
175
  // error code was set inner
L
Liu Jicong 已提交
176
  if (walReadSeekFilePos(pRead, pRet->firstVer, ver) < 0) {
L
Liu Jicong 已提交
177 178
    return -1;
  }
L
Liu Jicong 已提交
179

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

L
Liu Jicong 已提交
182 183 184 185 186 187 188 189 190 191 192 193
  pRead->curVersion = ver;
  return 0;
}

int32_t walReadSeekVer(SWalReader *pRead, int64_t ver) {
  SWal *pWal = pRead->pWal;
  if (!pRead->curInvalid && ver == pRead->curVersion) {
    wDebug("wal version %ld match, no need to reset", ver);
    return 0;
  }

  pRead->curInvalid = 1;
L
Liu Jicong 已提交
194
  pRead->curVersion = ver;
L
Liu Jicong 已提交
195

L
Liu Jicong 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208
  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
    wDebug("vgId:%d, invalid index:%" PRId64 ", first index:%" PRId64 ", last index:%" PRId64, pRead->pWal->cfg.vgId,
           ver, pWal->vers.firstVer, pWal->vers.lastVer);
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }
  if (ver < pWal->vers.snapshotVer) {
  }

  if (walReadSeekVerImpl(pRead, ver) < 0) {
    return -1;
  }

L
Liu Jicong 已提交
209 210 211
  return 0;
}

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

L
Liu Jicong 已提交
214 215
static int32_t walFetchHeadNew(SWalReader *pRead, int64_t fetchVer) {
  int64_t contLen;
L
Liu Jicong 已提交
216 217
  bool    seeked = false;

L
Liu Jicong 已提交
218
  if (pRead->curInvalid || pRead->curVersion != fetchVer) {
L
Liu Jicong 已提交
219 220
    if (walReadSeekVer(pRead, fetchVer) < 0) {
      ASSERT(0);
L
Liu Jicong 已提交
221 222
      pRead->curVersion = fetchVer;
      pRead->curInvalid = 1;
L
Liu Jicong 已提交
223 224
      return -1;
    }
L
Liu Jicong 已提交
225 226 227 228 229 230 231 232 233 234
    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 已提交
235
    } else {
L
Liu Jicong 已提交
236 237 238 239 240 241 242 243
      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 已提交
244 245
    }
  }
L
Liu Jicong 已提交
246
  pRead->curInvalid = 0;
L
Liu Jicong 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  return 0;
}

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

  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 已提交
268
      wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since %s",
S
Shengliang Guan 已提交
269
             pRead->pWal->cfg.vgId, pRead->pHead->head.version, ver, tstrerror(terrno));
L
Liu Jicong 已提交
270
    } else {
S
Shengliang Guan 已提交
271
      wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since file corrupted",
S
Shengliang Guan 已提交
272
             pRead->pWal->cfg.vgId, pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
273 274
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    }
L
Liu Jicong 已提交
275
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
276 277 278 279 280
    ASSERT(0);
    return -1;
  }

  if (pReadHead->version != ver) {
S
Shengliang Guan 已提交
281
    wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64, pRead->pWal->cfg.vgId,
S
Shengliang Guan 已提交
282
           pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
283
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
284 285 286 287 288 289
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    ASSERT(0);
    return -1;
  }

  if (walValidBodyCksum(pRead->pHead) != 0) {
S
Shengliang Guan 已提交
290
    wError("vgId:%d, wal fetch body error:%" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
291
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
292 293 294 295 296 297
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    ASSERT(0);
    return -1;
  }

  pRead->curVersion = ver + 1;
L
Liu Jicong 已提交
298
  wDebug("version advance to %ld, fetch body", pRead->curVersion);
L
Liu Jicong 已提交
299 300 301 302 303 304 305
  return 0;
}

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

  ASSERT(pRead->curVersion == pRead->pHead->head.version);
L
Liu Jicong 已提交
306
  ASSERT(pRead->curInvalid == 0);
L
Liu Jicong 已提交
307 308 309 310

  code = taosLSeekFile(pRead->pLogFile, pRead->pHead->head.bodyLen, SEEK_CUR);
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
311
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
312
    ASSERT(0);
L
Liu Jicong 已提交
313 314 315 316
    return -1;
  }

  pRead->curVersion++;
L
Liu Jicong 已提交
317
  wDebug("version advance to %ld, skip fetch", pRead->curVersion);
L
Liu Jicong 已提交
318 319 320 321

  return 0;
}

L
Liu Jicong 已提交
322
int32_t walFetchHead(SWalReader *pRead, int64_t ver, SWalCkHead *pHead) {
L
Liu Jicong 已提交
323
  int64_t code;
L
Liu Jicong 已提交
324

325
  // TODO: valid ver
L
Liu Jicong 已提交
326 327 328
  if (ver > pRead->pWal->vers.commitVer) {
    return -1;
  }
329

L
Liu Jicong 已提交
330
  if (pRead->curInvalid || pRead->curVersion != ver) {
331 332 333 334
    code = walReadSeekVer(pRead, ver);
    if (code < 0) return -1;
  }

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

L
Liu Jicong 已提交
337
  code = taosReadFile(pRead->pLogFile, pHead, sizeof(SWalCkHead));
L
Liu Jicong 已提交
338
  if (code != sizeof(SWalCkHead)) {
339 340 341 342 343 344
    return -1;
  }

  code = walValidHeadCksum(pHead);

  if (code != 0) {
L
Liu Jicong 已提交
345
    wError("vgId:%d, unexpected wal log index:%" PRId64 ", since head checksum not passed", pRead->pWal->cfg.vgId, ver);
346 347 348 349 350 351 352
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  return 0;
}

L
Liu Jicong 已提交
353
int32_t walSkipFetchBody(SWalReader *pRead, const SWalCkHead *pHead) {
L
Liu Jicong 已提交
354
  int64_t code;
355 356 357

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

L
Liu Jicong 已提交
358
  code = taosLSeekFile(pRead->pLogFile, pHead->head.bodyLen, SEEK_CUR);
359 360
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
361
    pRead->curInvalid = 1;
362 363 364 365 366 367 368 369
    return -1;
  }

  pRead->curVersion++;

  return 0;
}

L
Liu Jicong 已提交
370
int32_t walFetchBody(SWalReader *pRead, SWalCkHead **ppHead) {
L
Liu Jicong 已提交
371 372
  SWalCont *pReadHead = &((*ppHead)->head);
  int64_t   ver = pReadHead->version;
373 374

  if (pRead->capacity < pReadHead->bodyLen) {
L
Liu Jicong 已提交
375
    void *ptr = taosMemoryRealloc(*ppHead, sizeof(SWalCkHead) + pReadHead->bodyLen);
376 377 378 379 380
    if (ptr == NULL) {
      terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
      return -1;
    }
    *ppHead = ptr;
L
Liu Jicong 已提交
381
    pReadHead = &((*ppHead)->head);
382 383 384
    pRead->capacity = pReadHead->bodyLen;
  }

L
Liu Jicong 已提交
385
  if (pReadHead->bodyLen != taosReadFile(pRead->pLogFile, pReadHead->body, pReadHead->bodyLen)) {
L
Liu Jicong 已提交
386
    ASSERT(0);
387 388 389 390
    return -1;
  }

  if (pReadHead->version != ver) {
S
Shengliang Guan 已提交
391
    wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64, pRead->pWal->cfg.vgId,
S
Shengliang Guan 已提交
392
           pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
393
    pRead->curInvalid = 1;
394 395 396 397 398
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  if (walValidBodyCksum(*ppHead) != 0) {
S
Shengliang Guan 已提交
399
    wError("vgId:%d, wal fetch body error:%" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
400
    pRead->curInvalid = 1;
401 402 403 404 405 406 407 408
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

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

L
Liu Jicong 已提交
409
int32_t walReadVer(SWalReader *pRead, int64_t ver) {
L
Liu Jicong 已提交
410
  wDebug("vgId:%d wal start to read ver %ld", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
411 412
  int64_t contLen;
  bool    seeked = false;
413 414 415 416 417 418

  if (pRead->pWal->vers.firstVer == -1) {
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

M
Minghao Li 已提交
419
  if (ver > pRead->pWal->vers.lastVer || ver < pRead->pWal->vers.firstVer) {
L
Liu Jicong 已提交
420 421
    wError("vgId:%d, invalid index:%" PRId64 ", first index:%" PRId64 ", last index:%" PRId64, pRead->pWal->cfg.vgId,
           ver, pRead->pWal->vers.firstVer, pRead->pWal->vers.lastVer);
M
Minghao Li 已提交
422 423 424 425
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

L
Liu Jicong 已提交
426 427 428 429 430 431 432
  if (pRead->curInvalid || pRead->curVersion != ver) {
    if (walReadSeekVer(pRead, ver) < 0) {
      wError("vgId:%d, unexpected wal log index:%" PRId64 ", since %s", pRead->pWal->cfg.vgId, ver, terrstr());
      return -1;
    }
    seeked = true;
  }
L
Liu Jicong 已提交
433

L
Liu Jicong 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447
  while (1) {
    contLen = taosReadFile(pRead->pLogFile, pRead->pHead, sizeof(SWalCkHead));
    if (contLen == sizeof(SWalCkHead)) {
      break;
    } else if (contLen == 0 && !seeked) {
      walReadSeekVerImpl(pRead, ver);
      seeked = true;
      continue;
    } else {
      if (contLen < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
      } else {
        terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      }
M
Minghao Li 已提交
448
      ASSERT(0);
L
Liu Jicong 已提交
449
      return -1;
M
Minghao Li 已提交
450
    }
L
Liu Jicong 已提交
451
  }
452

L
Liu Jicong 已提交
453 454
  contLen = walValidHeadCksum(pRead->pHead);
  if (contLen != 0) {
L
Liu Jicong 已提交
455
    wError("vgId:%d, unexpected wal log index:%" PRId64 ", since head checksum not passed", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
456
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
457 458
    return -1;
  }
459

L
Liu Jicong 已提交
460
  if (pRead->capacity < pRead->pHead->head.bodyLen) {
L
Liu Jicong 已提交
461
    void *ptr = taosMemoryRealloc(pRead->pHead, sizeof(SWalCkHead) + pRead->pHead->head.bodyLen);
L
Liu Jicong 已提交
462
    if (ptr == NULL) {
L
Liu Jicong 已提交
463
      terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
464 465
      return -1;
    }
L
Liu Jicong 已提交
466
    pRead->pHead = ptr;
L
Liu Jicong 已提交
467
    pRead->capacity = pRead->pHead->head.bodyLen;
L
Liu Jicong 已提交
468
  }
L
Liu Jicong 已提交
469

L
Liu Jicong 已提交
470
  if ((contLen = taosReadFile(pRead->pLogFile, pRead->pHead->head.body, pRead->pHead->head.bodyLen)) !=
L
Liu Jicong 已提交
471
      pRead->pHead->head.bodyLen) {
L
Liu Jicong 已提交
472
    if (contLen < 0)
L
Liu Jicong 已提交
473
      terrno = TAOS_SYSTEM_ERROR(errno);
M
Minghao Li 已提交
474
    else {
L
Liu Jicong 已提交
475
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
M
Minghao Li 已提交
476 477
      ASSERT(0);
    }
L
Liu Jicong 已提交
478 479
    return -1;
  }
L
Liu Jicong 已提交
480

L
Liu Jicong 已提交
481
  if (pRead->pHead->head.version != ver) {
S
Shengliang Guan 已提交
482
    wError("vgId:%d, unexpected wal log index:%" PRId64 ", read request index:%" PRId64, pRead->pWal->cfg.vgId,
S
Shengliang Guan 已提交
483
           pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
484
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
485
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
486
    ASSERT(0);
L
Liu Jicong 已提交
487 488
    return -1;
  }
L
Liu Jicong 已提交
489

L
Liu Jicong 已提交
490 491
  contLen = walValidBodyCksum(pRead->pHead);
  if (contLen != 0) {
L
Liu Jicong 已提交
492
    wError("vgId:%d, unexpected wal log index:%" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
493
    pRead->curInvalid = 1;
L
Liu Jicong 已提交
494
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
495
    ASSERT(0);
L
Liu Jicong 已提交
496 497
    return -1;
  }
L
Liu Jicong 已提交
498
  pRead->curVersion++;
L
Liu Jicong 已提交
499 500 501

  return 0;
}