walRead.c 13.4 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 34 35
  pRead->curVersion = -1;
  pRead->curFileFirstVer = -1;
  pRead->capacity = 0;
L
Liu Jicong 已提交
36 37 38 39 40 41
  if (cond)
    pRead->cond = *cond;
  else {
    pRead->cond.scanMeta = 0;
    pRead->cond.scanUncommited = 0;
  }
L
fix  
Liu Jicong 已提交
42 43 44

  taosThreadMutexInit(&pRead->mutex, NULL);

L
Liu Jicong 已提交
45
  pRead->pHead = taosMemoryMalloc(sizeof(SWalCkHead));
L
Liu Jicong 已提交
46
  if (pRead->pHead == NULL) {
L
Liu Jicong 已提交
47
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
48
    taosMemoryFree(pRead);
L
Liu Jicong 已提交
49 50
    return NULL;
  }
L
Liu Jicong 已提交
51

L
Liu Jicong 已提交
52
  return pRead;
L
Liu Jicong 已提交
53 54
}

L
Liu Jicong 已提交
55 56 57
void walCloseReader(SWalReader *pRead) {
  taosCloseFile(&pRead->pIdxFile);
  taosCloseFile(&pRead->pLogFile);
wafwerar's avatar
wafwerar 已提交
58 59
  taosMemoryFreeClear(pRead->pHead);
  taosMemoryFree(pRead);
L
Liu Jicong 已提交
60 61
}

L
Liu Jicong 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
int32_t walNextValidMsg(SWalReader *pRead) {
  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 已提交
84
}
L
Liu Jicong 已提交
85

L
Liu Jicong 已提交
86
static int64_t walReadSeekFilePos(SWalReader *pRead, int64_t fileFirstVer, int64_t ver) {
L
Liu Jicong 已提交
87
  int64_t ret = 0;
L
Liu Jicong 已提交
88

L
Liu Jicong 已提交
89 90
  TdFilePtr pIdxTFile = pRead->pIdxFile;
  TdFilePtr pLogTFile = pRead->pLogFile;
L
Liu Jicong 已提交
91 92

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

L
Liu Jicong 已提交
114
  ASSERT(entry.ver == ver);
L
Liu Jicong 已提交
115 116
  ret = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET);
  if (ret < 0) {
L
Liu Jicong 已提交
117
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
118 119
    wError("vgId:%d, failed to seek log file, ver:%" PRId64 ", pos:%" PRId64 ", since %s", pRead->pWal->cfg.vgId, ver,
           entry.offset, terrstr());
L
Liu Jicong 已提交
120 121
    return -1;
  }
L
Liu Jicong 已提交
122
  return ret;
L
Liu Jicong 已提交
123 124
}

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

L
Liu Jicong 已提交
128 129
  taosCloseFile(&pRead->pIdxFile);
  taosCloseFile(&pRead->pLogFile);
L
Liu Jicong 已提交
130 131

  walBuildLogName(pRead->pWal, fileFirstVer, fnameStr);
132 133
  TdFilePtr pLogTFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pLogTFile == NULL) {
L
Liu Jicong 已提交
134
    terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
135
    wError("vgId:%d, cannot open file %s, since %s", pRead->pWal->cfg.vgId, fnameStr, terrstr());
L
Liu Jicong 已提交
136 137 138
    return -1;
  }

L
Liu Jicong 已提交
139
  pRead->pLogFile = pLogTFile;
L
Liu Jicong 已提交
140

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

L
Liu Jicong 已提交
149
  pRead->pIdxFile = pIdxTFile;
L
Liu Jicong 已提交
150 151 152
  return 0;
}

L
Liu Jicong 已提交
153
static int32_t walReadSeekVer(SWalReader *pRead, int64_t ver) {
L
Liu Jicong 已提交
154
  SWal *pWal = pRead->pWal;
L
Liu Jicong 已提交
155
  if (ver == pRead->curVersion) {
L
Liu Jicong 已提交
156 157
    return 0;
  }
L
Liu Jicong 已提交
158
  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
S
Shengliang Guan 已提交
159 160
    wError("vgId:$d, invalid version:%" PRId64 ", first ver:%" PRId64 ", last ver:%" PRId64, pRead->pWal->cfg.vgId, ver,
           pWal->vers.firstVer, pWal->vers.lastVer);
L
Liu Jicong 已提交
161
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
L
Liu Jicong 已提交
162 163
    return -1;
  }
L
Liu Jicong 已提交
164
  if (ver < pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
165 166
  }

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

L
Liu Jicong 已提交
179
  // error code set inner
L
Liu Jicong 已提交
180
  if (walReadSeekFilePos(pRead, pRet->firstVer, ver) < 0) {
L
Liu Jicong 已提交
181 182
    return -1;
  }
L
Liu Jicong 已提交
183

L
Liu Jicong 已提交
184
  pRead->curVersion = ver;
L
Liu Jicong 已提交
185

L
Liu Jicong 已提交
186 187 188
  return 0;
}

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

L
Liu Jicong 已提交
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
static int32_t walFetchHeadNew(SWalReader *pRead, int64_t fetchVer) {
  int64_t contLen;
  if (pRead->curVersion != fetchVer) {
    if (walReadSeekVer(pRead, fetchVer) < 0) return -1;
  }
  contLen = taosReadFile(pRead->pLogFile, pRead->pHead, sizeof(SWalCkHead));
  if (contLen != sizeof(SWalCkHead)) {
    if (contLen < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
    } else {
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    }
    pRead->curVersion = -1;
    return -1;
  }
  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 已提交
227 228
      wError("vgId:%d, wal fetch body error: %" PRId64 ", read request version:%" PRId64 ", since %s",
             pRead->pWal->cfg.vgId, pRead->pHead->head.version, ver, tstrerror(terrno));
L
Liu Jicong 已提交
229
    } else {
S
Shengliang Guan 已提交
230 231
      wError("vgId:%d, wal fetch body error: %" PRId64 ", read request version:%" PRId64 ", since file corrupted",
             pRead->pWal->cfg.vgId, pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
232 233 234 235 236 237 238 239
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    }
    pRead->curVersion = -1;
    ASSERT(0);
    return -1;
  }

  if (pReadHead->version != ver) {
S
Shengliang Guan 已提交
240 241
    wError("vgId:%d, wal fetch body error: %" PRId64 ", read request version:%" PRId64, pRead->pWal->cfg.vgId,
           pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
242 243 244 245 246 247 248
    pRead->curVersion = -1;
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    ASSERT(0);
    return -1;
  }

  if (walValidBodyCksum(pRead->pHead) != 0) {
S
Shengliang Guan 已提交
249
    wError("vgId:%d, wal fetch body error: % %" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
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
    pRead->curVersion = -1;
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    ASSERT(0);
    return -1;
  }

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

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

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

  code = taosLSeekFile(pRead->pLogFile, pRead->pHead->head.bodyLen, SEEK_CUR);
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    pRead->curVersion = -1;
    return -1;
  }

  pRead->curVersion++;

  return 0;
}

L
Liu Jicong 已提交
277
int32_t walFetchHead(SWalReader *pRead, int64_t ver, SWalCkHead *pHead) {
L
Liu Jicong 已提交
278
  int64_t code;
L
Liu Jicong 已提交
279

280
  // TODO: valid ver
L
Liu Jicong 已提交
281 282 283
  if (ver > pRead->pWal->vers.commitVer) {
    return -1;
  }
284 285 286 287 288 289

  if (pRead->curVersion != ver) {
    code = walReadSeekVer(pRead, ver);
    if (code < 0) return -1;
  }

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

L
Liu Jicong 已提交
292
  code = taosReadFile(pRead->pLogFile, pHead, sizeof(SWalCkHead));
L
Liu Jicong 已提交
293
  if (code != sizeof(SWalCkHead)) {
294 295 296 297 298 299
    return -1;
  }

  code = walValidHeadCksum(pHead);

  if (code != 0) {
S
Shengliang Guan 已提交
300 301
    wError("vgId:%d, unexpected wal log version:%" PRId64 ", since head checksum not passed", pRead->pWal->cfg.vgId,
           ver);
302 303 304 305 306 307 308
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  return 0;
}

L
Liu Jicong 已提交
309
int32_t walSkipFetchBody(SWalReader *pRead, const SWalCkHead *pHead) {
L
Liu Jicong 已提交
310
  int64_t code;
311 312 313

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

L
Liu Jicong 已提交
314
  code = taosLSeekFile(pRead->pLogFile, pHead->head.bodyLen, SEEK_CUR);
315 316 317 318 319 320 321 322 323 324 325
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    pRead->curVersion = -1;
    return -1;
  }

  pRead->curVersion++;

  return 0;
}

L
Liu Jicong 已提交
326
int32_t walFetchBody(SWalReader *pRead, SWalCkHead **ppHead) {
L
Liu Jicong 已提交
327 328
  SWalCont *pReadHead = &((*ppHead)->head);
  int64_t   ver = pReadHead->version;
329 330

  if (pRead->capacity < pReadHead->bodyLen) {
L
Liu Jicong 已提交
331
    void *ptr = taosMemoryRealloc(*ppHead, sizeof(SWalCkHead) + pReadHead->bodyLen);
332 333 334 335 336
    if (ptr == NULL) {
      terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
      return -1;
    }
    *ppHead = ptr;
L
Liu Jicong 已提交
337
    pReadHead = &((*ppHead)->head);
338 339 340
    pRead->capacity = pReadHead->bodyLen;
  }

L
Liu Jicong 已提交
341
  if (pReadHead->bodyLen != taosReadFile(pRead->pLogFile, pReadHead->body, pReadHead->bodyLen)) {
L
Liu Jicong 已提交
342
    ASSERT(0);
343 344 345 346
    return -1;
  }

  if (pReadHead->version != ver) {
S
Shengliang Guan 已提交
347 348
    wError("vgId:%d, wal fetch body error: %" PRId64 ", read request version:%" PRId64, pRead->pWal->cfg.vgId,
           pRead->pHead->head.version, ver);
349 350 351 352 353 354
    pRead->curVersion = -1;
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  if (walValidBodyCksum(*ppHead) != 0) {
S
Shengliang Guan 已提交
355
    wError("vgId:%d, wal fetch body error: % %" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId, ver);
356 357 358 359 360 361 362 363 364
    pRead->curVersion = -1;
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

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

L
Liu Jicong 已提交
365
int32_t walReadWithHandle_s(SWalReader *pRead, int64_t ver, SWalCont **ppHead) {
L
fix  
Liu Jicong 已提交
366
  taosThreadMutexLock(&pRead->mutex);
L
Liu Jicong 已提交
367
  if (walReadVer(pRead, ver) < 0) {
L
fix  
Liu Jicong 已提交
368 369 370
    taosThreadMutexUnlock(&pRead->mutex);
    return -1;
  }
L
Liu Jicong 已提交
371
  *ppHead = taosMemoryMalloc(sizeof(SWalCont) + pRead->pHead->head.bodyLen);
L
fix  
Liu Jicong 已提交
372 373 374 375
  if (*ppHead == NULL) {
    taosThreadMutexUnlock(&pRead->mutex);
    return -1;
  }
L
Liu Jicong 已提交
376
  memcpy(*ppHead, &pRead->pHead->head, sizeof(SWalCont) + pRead->pHead->head.bodyLen);
L
fix  
Liu Jicong 已提交
377 378 379 380
  taosThreadMutexUnlock(&pRead->mutex);
  return 0;
}

L
Liu Jicong 已提交
381
int32_t walReadVer(SWalReader *pRead, int64_t ver) {
L
Liu Jicong 已提交
382
  int64_t code;
383 384 385 386 387 388

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

L
Liu Jicong 已提交
389 390
  // TODO: check wal life
  if (pRead->curVersion != ver) {
L
Liu Jicong 已提交
391
    if (walReadSeekVer(pRead, ver) < 0) {
S
Shengliang Guan 已提交
392
      wError("vgId:%d, unexpected wal log version:%" PRId64 ", since %s", pRead->pWal->cfg.vgId, ver, terrstr());
L
Liu Jicong 已提交
393 394
      return -1;
    }
L
Liu Jicong 已提交
395 396
  }

M
Minghao Li 已提交
397
  if (ver > pRead->pWal->vers.lastVer || ver < pRead->pWal->vers.firstVer) {
S
Shengliang Guan 已提交
398 399
    wError("vgId:%d, invalid version:%" PRId64 ", first ver:%" PRId64 ", last ver:%" PRId64, pRead->pWal->cfg.vgId, ver,
           pRead->pWal->vers.firstVer, pRead->pWal->vers.lastVer);
M
Minghao Li 已提交
400 401 402 403
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

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

L
Liu Jicong 已提交
406
  code = taosReadFile(pRead->pLogFile, pRead->pHead, sizeof(SWalCkHead));
L
Liu Jicong 已提交
407
  if (code != sizeof(SWalCkHead)) {
L
Liu Jicong 已提交
408 409
    if (code < 0)
      terrno = TAOS_SYSTEM_ERROR(errno);
M
Minghao Li 已提交
410
    else {
L
Liu Jicong 已提交
411
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
M
Minghao Li 已提交
412 413
      ASSERT(0);
    }
L
Liu Jicong 已提交
414 415
    return -1;
  }
416

L
Liu Jicong 已提交
417
  code = walValidHeadCksum(pRead->pHead);
L
Liu Jicong 已提交
418
  if (code != 0) {
S
Shengliang Guan 已提交
419 420
    wError("vgId:%d, unexpected wal log version:%" PRId64 ", since head checksum not passed", pRead->pWal->cfg.vgId,
           ver);
L
Liu Jicong 已提交
421
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
422 423
    return -1;
  }
424

L
Liu Jicong 已提交
425
  if (pRead->capacity < pRead->pHead->head.bodyLen) {
L
Liu Jicong 已提交
426
    void *ptr = taosMemoryRealloc(pRead->pHead, sizeof(SWalCkHead) + pRead->pHead->head.bodyLen);
L
Liu Jicong 已提交
427
    if (ptr == NULL) {
L
Liu Jicong 已提交
428
      terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
429 430
      return -1;
    }
L
Liu Jicong 已提交
431
    pRead->pHead = ptr;
L
Liu Jicong 已提交
432
    pRead->capacity = pRead->pHead->head.bodyLen;
L
Liu Jicong 已提交
433
  }
L
Liu Jicong 已提交
434

L
Liu Jicong 已提交
435
  if ((code = taosReadFile(pRead->pLogFile, pRead->pHead->head.body, pRead->pHead->head.bodyLen)) !=
L
Liu Jicong 已提交
436 437 438
      pRead->pHead->head.bodyLen) {
    if (code < 0)
      terrno = TAOS_SYSTEM_ERROR(errno);
M
Minghao Li 已提交
439
    else {
L
Liu Jicong 已提交
440
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
M
Minghao Li 已提交
441 442
      ASSERT(0);
    }
L
Liu Jicong 已提交
443 444
    return -1;
  }
L
Liu Jicong 已提交
445

L
Liu Jicong 已提交
446
  if (pRead->pHead->head.version != ver) {
S
Shengliang Guan 已提交
447 448
    wError("vgId:%d, unexpected wal log version:%" PRId64 ", read request version:%" PRId64, pRead->pWal->cfg.vgId,
           pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
449 450 451 452
    pRead->curVersion = -1;
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }
L
Liu Jicong 已提交
453

L
Liu Jicong 已提交
454
  code = walValidBodyCksum(pRead->pHead);
L
Liu Jicong 已提交
455
  if (code != 0) {
S
Shengliang Guan 已提交
456 457
    wError("vgId:%d, unexpected wal log version:%" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId,
           ver);
L
Liu Jicong 已提交
458
    pRead->curVersion = -1;
L
Liu Jicong 已提交
459
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
460 461
    return -1;
  }
L
Liu Jicong 已提交
462
  pRead->curVersion++;
L
Liu Jicong 已提交
463 464 465

  return 0;
}