walRead.c 18.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
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
  pReader->pWal = pWal;
  pReader->readerId = tGenIdPI64();
  pReader->pIdxFile = NULL;
  pReader->pLogFile = NULL;
  pReader->curVersion = -1;
  pReader->curFileFirstVer = -1;
  pReader->capacity = 0;
L
Liu Jicong 已提交
37
  if (cond) {
38
    pReader->cond = *cond;
L
Liu Jicong 已提交
39
  } else {
40
//    pReader->cond.scanUncommited = 0;
41 42 43
    pReader->cond.scanNotApplied = 0;
    pReader->cond.scanMeta = 0;
    pReader->cond.enableRef = 0;
L
Liu Jicong 已提交
44
  }
L
fix  
Liu Jicong 已提交
45

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

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

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

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

62 63 64 65 66 67 68 69
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 已提交
70 71
}

72 73 74 75 76
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);
77 78 79
  if(appliedVer < committedVer){   // wait apply ver equal to commit ver, otherwise may lost data when consume data [TD-24010]
    wDebug("vgId:%d, wal apply ver:%"PRId64" smaller than commit ver:%"PRId64, pReader->pWal->cfg.vgId, appliedVer, committedVer);
//    taosMsleep(10);
80 81
  }
//  int64_t endVer = pReader->cond.scanUncommited ? lastVer : committedVer;
82
  int64_t endVer = TMIN(appliedVer, committedVer);
L
Liu Jicong 已提交
83

S
Shengliang Guan 已提交
84
  wDebug("vgId:%d, wal start to fetch, index:%" PRId64 ", last index:%" PRId64 " commit index:%" PRId64
85 86 87
         ", applied index:%" PRId64", end index:%" PRId64,
         pReader->pWal->cfg.vgId, fetchVer, lastVer, committedVer, appliedVer, endVer);
  while (fetchVer <= endVer) {
88
    if (walFetchHeadNew(pReader, fetchVer) < 0) {
L
Liu Jicong 已提交
89 90
      return -1;
    }
91 92 93
    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 已提交
94 95 96 97
        return -1;
      }
      return 0;
    } else {
98
      if (walSkipFetchBodyNew(pReader) < 0) {
L
Liu Jicong 已提交
99 100
        return -1;
      }
wmmhello's avatar
wmmhello 已提交
101
      fetchVer = pReader->curVersion;
L
Liu Jicong 已提交
102 103 104
    }
  }
  return -1;
L
Liu Jicong 已提交
105
}
L
Liu Jicong 已提交
106

107 108
int64_t walReaderGetCurrentVer(const SWalReader *pReader) { return pReader->curVersion; }

109
static int64_t walReadSeekFilePos(SWalReader *pReader, int64_t fileFirstVer, int64_t ver) {
L
Liu Jicong 已提交
110
  int64_t ret = 0;
L
Liu Jicong 已提交
111

112 113
  TdFilePtr pIdxTFile = pReader->pIdxFile;
  TdFilePtr pLogTFile = pReader->pLogFile;
L
Liu Jicong 已提交
114 115

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

  ret = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET);
  if (ret < 0) {
L
Liu Jicong 已提交
139
    terrno = TAOS_SYSTEM_ERROR(errno);
140 141
    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 已提交
142 143
    return -1;
  }
L
Liu Jicong 已提交
144
  return ret;
L
Liu Jicong 已提交
145 146
}

147
static int32_t walReadChangeFile(SWalReader *pReader, int64_t fileFirstVer) {
wmmhello's avatar
wmmhello 已提交
148
  char fnameStr[WAL_FILE_LEN] = {0};
L
Liu Jicong 已提交
149

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

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

161
  pReader->pLogFile = pLogFile;
L
Liu Jicong 已提交
162

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

171
  pReader->pIdxFile = pIdxFile;
172 173 174

  pReader->curFileFirstVer = fileFirstVer;

L
Liu Jicong 已提交
175 176 177
  return 0;
}

178 179
int32_t walReadSeekVerImpl(SWalReader *pReader, int64_t ver) {
  SWal *pWal = pReader->pWal;
L
Liu Jicong 已提交
180

181
  // bsearch in fileSet
L
Liu Jicong 已提交
182
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
183
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
184
  SWalFileInfo *pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
185
  if (pRet == NULL) {
186
    wError("failed to find WAL log file with ver:%" PRId64, ver);
187 188 189
    terrno = TSDB_CODE_WAL_INVALID_VER;
    return -1;
  }
190
  if (pReader->curFileFirstVer != pRet->firstVer) {
L
Liu Jicong 已提交
191
    // error code was set inner
192
    if (walReadChangeFile(pReader, pRet->firstVer) < 0) {
L
Liu Jicong 已提交
193 194 195 196
      return -1;
    }
  }

L
Liu Jicong 已提交
197
  // error code was set inner
198
  if (walReadSeekFilePos(pReader, pRet->firstVer, ver) < 0) {
L
Liu Jicong 已提交
199 200
    return -1;
  }
L
Liu Jicong 已提交
201

202 203
  wDebug("vgId:%d, wal version reset from %" PRId64 " to %" PRId64, pReader->pWal->cfg.vgId,
         pReader->curVersion, ver);
L
Liu Jicong 已提交
204

205
  pReader->curVersion = ver;
L
Liu Jicong 已提交
206 207 208
  return 0;
}

209 210
int32_t walReadSeekVer(SWalReader *pReader, int64_t ver) {
  SWal *pWal = pReader->pWal;
211
  if (ver == pReader->curVersion) {
S
Shengliang Guan 已提交
212
    wDebug("vgId:%d, wal index:%" PRId64 " match, no need to reset", pReader->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
213 214 215 216
    return 0;
  }

  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
217
    wInfo("vgId:%d, invalid index:%" PRId64 ", first index:%" PRId64 ", last index:%" PRId64, pReader->pWal->cfg.vgId,
L
Liu Jicong 已提交
218 219 220 221 222
           ver, pWal->vers.firstVer, pWal->vers.lastVer);
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

223
  if (walReadSeekVerImpl(pReader, ver) < 0) {
L
Liu Jicong 已提交
224 225 226
    return -1;
  }

L
Liu Jicong 已提交
227 228 229
  return 0;
}

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

L
Liu Jicong 已提交
232 233
static int32_t walFetchHeadNew(SWalReader *pRead, int64_t fetchVer) {
  int64_t contLen;
L
Liu Jicong 已提交
234 235
  bool    seeked = false;

S
Shengliang Guan 已提交
236
  wDebug("vgId:%d, wal starts to fetch head, index:%" PRId64, pRead->pWal->cfg.vgId, fetchVer);
237

238
  if (pRead->curVersion != fetchVer) {
L
Liu Jicong 已提交
239 240 241
    if (walReadSeekVer(pRead, fetchVer) < 0) {
      return -1;
    }
L
Liu Jicong 已提交
242 243
    seeked = true;
  }
244

L
Liu Jicong 已提交
245 246 247 248 249
  while (1) {
    contLen = taosReadFile(pRead->pLogFile, pRead->pHead, sizeof(SWalCkHead));
    if (contLen == sizeof(SWalCkHead)) {
      break;
    } else if (contLen == 0 && !seeked) {
250 251 252
      if(walReadSeekVerImpl(pRead, fetchVer) < 0){
        return -1;
      }
L
Liu Jicong 已提交
253 254
      seeked = true;
      continue;
L
Liu Jicong 已提交
255
    } else {
L
Liu Jicong 已提交
256 257 258 259 260 261
      if (contLen < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
      } else {
        terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      }
      return -1;
L
Liu Jicong 已提交
262 263
    }
  }
264
//  pRead->curInvalid = 0;
L
Liu Jicong 已提交
265 266 267
  return 0;
}

L
Liu Jicong 已提交
268 269
static int32_t walFetchBodyNew(SWalReader *pReader) {
  SWalCont *pReadHead = &pReader->pHead->head;
L
Liu Jicong 已提交
270 271
  int64_t   ver = pReadHead->version;

272 273
  wDebug("vgId:%d, wal starts to fetch body, ver:%" PRId64 " ,len:%d, total", pReader->pWal->cfg.vgId, ver,
         pReadHead->bodyLen);
274

L
Liu Jicong 已提交
275 276
  if (pReader->capacity < pReadHead->bodyLen) {
    SWalCkHead *ptr = (SWalCkHead *)taosMemoryRealloc(pReader->pHead, sizeof(SWalCkHead) + pReadHead->bodyLen);
L
Liu Jicong 已提交
277
    if (ptr == NULL) {
S
Shengliang Guan 已提交
278
      terrno = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
279 280
      return -1;
    }
L
Liu Jicong 已提交
281 282 283
    pReader->pHead = ptr;
    pReadHead = &pReader->pHead->head;
    pReader->capacity = pReadHead->bodyLen;
L
Liu Jicong 已提交
284 285
  }

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

L
Liu Jicong 已提交
300 301
  if (walValidBodyCksum(pReader->pHead) != 0) {
    wError("vgId:%d, wal fetch body error:%" PRId64 ", since body checksum not passed", pReader->pWal->cfg.vgId, ver);
302
//    pRead->curInvalid = 1;
wmmhello's avatar
wmmhello 已提交
303

L
Liu Jicong 已提交
304 305 306 307
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

L
Liu Jicong 已提交
308 309
  wDebug("vgId:%d, index:%" PRId64 " is fetched, cursor advance", pReader->pWal->cfg.vgId, ver);
  pReader->curVersion = ver + 1;
L
Liu Jicong 已提交
310 311 312 313 314 315 316 317 318
  return 0;
}

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

  code = taosLSeekFile(pRead->pLogFile, pRead->pHead->head.bodyLen, SEEK_CUR);
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
319
//    pRead->curInvalid = 1;
L
Liu Jicong 已提交
320 321 322 323
    return -1;
  }

  pRead->curVersion++;
S
Shengliang Guan 已提交
324
  wDebug("vgId:%d, version advance to %" PRId64 ", skip fetch", pRead->pWal->cfg.vgId, pRead->curVersion);
L
Liu Jicong 已提交
325 326 327 328

  return 0;
}

L
Liu Jicong 已提交
329
int32_t walFetchHead(SWalReader *pRead, int64_t ver, SWalCkHead *pHead) {
L
Liu Jicong 已提交
330
  int64_t code;
331 332 333
  int64_t contLen;
  bool    seeked = false;

S
Shengliang Guan 已提交
334
  wDebug("vgId:%d, try to fetch ver %" PRId64 ", first ver:%" PRId64 ", commit ver:%" PRId64 ", last ver:%" PRId64
L
Liu Jicong 已提交
335 336 337
         ", applied ver:%" PRId64,
         pRead->pWal->cfg.vgId, ver, pRead->pWal->vers.firstVer, pRead->pWal->vers.commitVer, pRead->pWal->vers.lastVer,
         pRead->pWal->vers.appliedVer);
L
Liu Jicong 已提交
338

339
  // TODO: valid ver
340
  if (ver > pRead->pWal->vers.appliedVer) {
L
Liu Jicong 已提交
341 342
    return -1;
  }
343

344
  if (pRead->curVersion != ver) {
345
    code = walReadSeekVer(pRead, ver);
346
    if (code < 0) {
347 348
//      pRead->curVersion = ver;
//      pRead->curInvalid = 1;
349 350 351
      return -1;
    }
    seeked = true;
352 353
  }

354 355 356 357 358
  while (1) {
    contLen = taosReadFile(pRead->pLogFile, pHead, sizeof(SWalCkHead));
    if (contLen == sizeof(SWalCkHead)) {
      break;
    } else if (contLen == 0 && !seeked) {
359 360 361
      if(walReadSeekVerImpl(pRead, ver) < 0){
        return -1;
      }
362 363 364 365 366 367 368 369
      seeked = true;
      continue;
    } else {
      if (contLen < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
      } else {
        terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      }
370
//      pRead->curInvalid = 1;
371 372
      return -1;
    }
373 374 375 376 377
  }

  code = walValidHeadCksum(pHead);

  if (code != 0) {
L
Liu Jicong 已提交
378
    wError("vgId:%d, unexpected wal log index:%" PRId64 ", since head checksum not passed", pRead->pWal->cfg.vgId, ver);
379 380 381 382
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

383
//  pRead->curInvalid = 0;
384 385 386
  return 0;
}

L
Liu Jicong 已提交
387
int32_t walSkipFetchBody(SWalReader *pRead, const SWalCkHead *pHead) {
L
Liu Jicong 已提交
388
  int64_t code;
389

S
Shengliang Guan 已提交
390
  wDebug("vgId:%d, skip fetch body %" PRId64 ", first ver:%" PRId64 ", commit ver:%" PRId64 ", last ver:%" PRId64
L
Liu Jicong 已提交
391 392 393 394
         ", applied ver:%" PRId64,
         pRead->pWal->cfg.vgId, pHead->head.version, pRead->pWal->vers.firstVer, pRead->pWal->vers.commitVer,
         pRead->pWal->vers.lastVer, pRead->pWal->vers.appliedVer);

L
Liu Jicong 已提交
395
  code = taosLSeekFile(pRead->pLogFile, pHead->head.bodyLen, SEEK_CUR);
396 397
  if (code < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
398
//    pRead->curInvalid = 1;
399 400 401 402 403 404 405 406
    return -1;
  }

  pRead->curVersion++;

  return 0;
}

L
Liu Jicong 已提交
407
int32_t walFetchBody(SWalReader *pRead, SWalCkHead **ppHead) {
L
Liu Jicong 已提交
408 409
  SWalCont *pReadHead = &((*ppHead)->head);
  int64_t   ver = pReadHead->version;
410

S
Shengliang Guan 已提交
411
  wDebug("vgId:%d, fetch body %" PRId64 ", first ver:%" PRId64 ", commit ver:%" PRId64 ", last ver:%" PRId64
L
Liu Jicong 已提交
412 413 414 415
         ", applied ver:%" PRId64,
         pRead->pWal->cfg.vgId, ver, pRead->pWal->vers.firstVer, pRead->pWal->vers.commitVer, pRead->pWal->vers.lastVer,
         pRead->pWal->vers.appliedVer);

416
  if (pRead->capacity < pReadHead->bodyLen) {
L
Liu Jicong 已提交
417
    SWalCkHead *ptr = (SWalCkHead *)taosMemoryRealloc(*ppHead, sizeof(SWalCkHead) + pReadHead->bodyLen);
418
    if (ptr == NULL) {
S
Shengliang Guan 已提交
419
      terrno = TSDB_CODE_OUT_OF_MEMORY;
420 421
      return -1;
    }
L
Liu Jicong 已提交
422
    *ppHead = ptr;
L
Liu Jicong 已提交
423
    pReadHead = &((*ppHead)->head);
424 425 426
    pRead->capacity = pReadHead->bodyLen;
  }

L
Liu Jicong 已提交
427
  if (pReadHead->bodyLen != taosReadFile(pRead->pLogFile, pReadHead->body, pReadHead->bodyLen)) {
428 429 430 431 432 433 434 435 436
    if (pReadHead->bodyLen < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since %s",
             pRead->pWal->cfg.vgId, pReadHead->version, ver, tstrerror(terrno));
    } else {
      wError("vgId:%d, wal fetch body error:%" PRId64 ", read request index:%" PRId64 ", since file corrupted",
             pRead->pWal->cfg.vgId, pReadHead->version, ver);
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    }
437
//    pRead->curInvalid = 1;
438 439 440 441
    return -1;
  }

  if (pReadHead->version != ver) {
S
Shengliang Guan 已提交
442
    wError("vgId:%d, wal fetch body error, index:%" PRId64 ", read request index:%" PRId64, pRead->pWal->cfg.vgId,
443
           pReadHead->version, ver);
444
//    pRead->curInvalid = 1;
445 446 447 448 449
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

  if (walValidBodyCksum(*ppHead) != 0) {
450 451
    wError("vgId:%d, wal fetch body error, index:%" PRId64 ", since body checksum not passed", pRead->pWal->cfg.vgId,
           ver);
452
//    pRead->curInvalid = 1;
453 454 455 456 457 458 459 460
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }

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

461
int32_t walReadVer(SWalReader *pReader, int64_t ver) {
S
Shengliang Guan 已提交
462
  wDebug("vgId:%d, wal start to read index:%" PRId64, pReader->pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
463
  int64_t contLen;
464
  int32_t code;
L
Liu Jicong 已提交
465
  bool    seeked = false;
466

467
  if (walIsEmpty(pReader->pWal)) {
468 469 470 471
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

472 473 474
  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 已提交
475 476 477 478
    terrno = TSDB_CODE_WAL_LOG_NOT_EXIST;
    return -1;
  }

479 480
  taosThreadMutexLock(&pReader->mutex);

481
  if (pReader->curVersion != ver) {
482 483
    if (walReadSeekVer(pReader, ver) < 0) {
      wError("vgId:%d, unexpected wal log, index:%" PRId64 ", since %s", pReader->pWal->cfg.vgId, ver, terrstr());
484
      taosThreadMutexUnlock(&pReader->mutex);
L
Liu Jicong 已提交
485 486 487 488
      return -1;
    }
    seeked = true;
  }
L
Liu Jicong 已提交
489

L
Liu Jicong 已提交
490
  while (1) {
491
    contLen = taosReadFile(pReader->pLogFile, pReader->pHead, sizeof(SWalCkHead));
L
Liu Jicong 已提交
492 493 494
    if (contLen == sizeof(SWalCkHead)) {
      break;
    } else if (contLen == 0 && !seeked) {
495 496 497 498
      if(walReadSeekVerImpl(pReader, ver) < 0){
        taosThreadMutexUnlock(&pReader->mutex);
        return -1;
      }
L
Liu Jicong 已提交
499 500 501 502 503 504 505 506
      seeked = true;
      continue;
    } else {
      if (contLen < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
      } else {
        terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      }
507 508
      wError("vgId:%d, failed to read WAL record head, index:%" PRId64 ", from log file since %s",
             pReader->pWal->cfg.vgId, ver, terrstr());
509
      taosThreadMutexUnlock(&pReader->mutex);
L
Liu Jicong 已提交
510
      return -1;
M
Minghao Li 已提交
511
    }
L
Liu Jicong 已提交
512
  }
513

514 515 516
  code = walValidHeadCksum(pReader->pHead);
  if (code != 0) {
    wError("vgId:%d, unexpected wal log, index:%" PRId64 ", since head checksum not passed", pReader->pWal->cfg.vgId,
517
           ver);
L
Liu Jicong 已提交
518
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
519
    taosThreadMutexUnlock(&pReader->mutex);
L
Liu Jicong 已提交
520 521
    return -1;
  }
522

523
  if (pReader->capacity < pReader->pHead->head.bodyLen) {
L
Liu Jicong 已提交
524 525
    SWalCkHead *ptr =
        (SWalCkHead *)taosMemoryRealloc(pReader->pHead, sizeof(SWalCkHead) + pReader->pHead->head.bodyLen);
L
Liu Jicong 已提交
526
    if (ptr == NULL) {
S
Shengliang Guan 已提交
527
      terrno = TSDB_CODE_OUT_OF_MEMORY;
528
      taosThreadMutexUnlock(&pReader->mutex);
L
Liu Jicong 已提交
529 530
      return -1;
    }
L
Liu Jicong 已提交
531
    pReader->pHead = ptr;
532
    pReader->capacity = pReader->pHead->head.bodyLen;
L
Liu Jicong 已提交
533
  }
L
Liu Jicong 已提交
534

535 536
  if ((contLen = taosReadFile(pReader->pLogFile, pReader->pHead->head.body, pReader->pHead->head.bodyLen)) !=
      pReader->pHead->head.bodyLen) {
L
Liu Jicong 已提交
537
    if (contLen < 0)
L
Liu Jicong 已提交
538
      terrno = TAOS_SYSTEM_ERROR(errno);
M
Minghao Li 已提交
539
    else {
L
Liu Jicong 已提交
540
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
M
Minghao Li 已提交
541
    }
542 543
    wError("vgId:%d, failed to read WAL record body, index:%" PRId64 ", from log file since %s",
           pReader->pWal->cfg.vgId, ver, terrstr());
544
    taosThreadMutexUnlock(&pReader->mutex);
L
Liu Jicong 已提交
545 546
    return -1;
  }
L
Liu Jicong 已提交
547

548 549 550
  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);
551
//    pReader->curInvalid = 1;
L
Liu Jicong 已提交
552
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
553
    taosThreadMutexUnlock(&pReader->mutex);
L
Liu Jicong 已提交
554 555
    return -1;
  }
L
Liu Jicong 已提交
556

557 558 559
  code = walValidBodyCksum(pReader->pHead);
  if (code != 0) {
    wError("vgId:%d, unexpected wal log, index:%" PRId64 ", since body checksum not passed", pReader->pWal->cfg.vgId,
560
           ver);
561 562
    uint32_t readCkSum = walCalcBodyCksum(pReader->pHead->head.body, pReader->pHead->head.bodyLen);
    uint32_t logCkSum = pReader->pHead->cksumBody;
S
Shengliang Guan 已提交
563
    wError("checksum written into log:%u, checksum calculated:%u", logCkSum, readCkSum);
564
//    pReader->curInvalid = 1;
L
Liu Jicong 已提交
565
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
566
    taosThreadMutexUnlock(&pReader->mutex);
L
Liu Jicong 已提交
567 568
    return -1;
  }
569
  pReader->curVersion++;
L
Liu Jicong 已提交
570

571 572
  taosThreadMutexUnlock(&pReader->mutex);

L
Liu Jicong 已提交
573 574
  return 0;
}
575 576 577 578 579 580

void walReadReset(SWalReader *pReader) {
  taosThreadMutexLock(&pReader->mutex);
  taosCloseFile(&pReader->pIdxFile);
  taosCloseFile(&pReader->pLogFile);
  pReader->curFileFirstVer = -1;
581
  pReader->curVersion = -1;
582 583
  taosThreadMutexUnlock(&pReader->mutex);
}