walWrite.c 15.7 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "os.h"
S
Shengliang Guan 已提交
17
#include "taoserror.h"
18
#include "tchecksum.h"
S
Shengliang Guan 已提交
19
#include "walInt.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
20

21 22 23 24 25
int32_t walRestoreFromSnapshot(SWal *pWal, int64_t ver) {
  taosThreadMutexLock(&pWal->mutex);

  void *pIter = NULL;
  while (1) {
L
Liu Jicong 已提交
26
    pIter = taosHashIterate(pWal->pRefHash, pIter);
27 28
    if (pIter == NULL) break;
    SWalRef *pRef = (SWalRef *)pIter;
L
Liu Jicong 已提交
29
    if (pRef->refVer != -1 && pRef->refVer <= ver) {
30
      taosHashCancelIterate(pWal->pRefHash, pIter);
L
Liu Jicong 已提交
31
      taosThreadMutexUnlock(&pWal->mutex);
32 33 34 35
      return -1;
    }
  }

L
Liu Jicong 已提交
36 37
  taosCloseFile(&pWal->pLogFile);
  taosCloseFile(&pWal->pIdxFile);
38 39 40 41 42 43 44 45

  if (pWal->vers.firstVer != -1) {
    int32_t fileSetSize = taosArrayGetSize(pWal->fileInfoSet);
    for (int32_t i = 0; i < fileSetSize; i++) {
      SWalFileInfo *pFileInfo = taosArrayGet(pWal->fileInfoSet, i);
      char          fnameStr[WAL_FILE_LEN];
      walBuildLogName(pWal, pFileInfo->firstVer, fnameStr);
      taosRemoveFile(fnameStr);
46 47 48

      walBuildIdxName(pWal, pFileInfo->firstVer, fnameStr);
      taosRemoveFile(fnameStr);
49 50 51 52 53 54 55 56 57 58
    }
  }
  walRemoveMeta(pWal);

  pWal->writeCur = -1;
  pWal->totSize = 0;
  pWal->lastRollSeq = -1;

  taosArrayClear(pWal->fileInfoSet);
  pWal->vers.firstVer = -1;
59 60 61 62
  pWal->vers.lastVer = ver;
  pWal->vers.commitVer = ver - 1;
  pWal->vers.snapshotVer = ver - 1;
  pWal->vers.verInSnapshotting = -1;
63 64 65

  taosThreadMutexUnlock(&pWal->mutex);
  return 0;
66 67
}

L
Liu Jicong 已提交
68 69 70 71 72 73
int32_t walApplyVer(SWal *pWal, int64_t ver) {
  // TODO: error check
  pWal->vers.appliedVer = ver;
  return 0;
}

L
Liu Jicong 已提交
74
int32_t walCommit(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
75 76
  ASSERT(pWal->vers.commitVer >= pWal->vers.snapshotVer);
  ASSERT(pWal->vers.commitVer <= pWal->vers.lastVer);
L
Liu Jicong 已提交
77 78 79 80
  if (ver < pWal->vers.commitVer) {
    return 0;
  }
  if (ver > pWal->vers.lastVer) {
L
Liu Jicong 已提交
81
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
82 83
    return -1;
  }
L
Liu Jicong 已提交
84
  pWal->vers.commitVer = ver;
L
Liu Jicong 已提交
85 86 87 88
  return 0;
}

int32_t walRollback(SWal *pWal, int64_t ver) {
89
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
90 91
  int64_t code;
  char    fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
92
  if (ver > pWal->vers.lastVer || ver < pWal->vers.commitVer) {
L
Liu Jicong 已提交
93
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
94
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
95 96 97
    return -1;
  }

L
Liu Jicong 已提交
98 99
  // find correct file
  if (ver < walGetLastFileFirstVer(pWal)) {
L
Liu Jicong 已提交
100 101 102
    // change current files
    code = walChangeWrite(pWal, ver);
    if (code < 0) {
L
Liu Jicong 已提交
103
      taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
104 105 106
      return -1;
    }

L
Liu Jicong 已提交
107
    // delete files
L
Liu Jicong 已提交
108
    int fileSetSize = taosArrayGetSize(pWal->fileInfoSet);
L
Liu Jicong 已提交
109
    for (int i = pWal->writeCur + 1; i < fileSetSize; i++) {
L
Liu Jicong 已提交
110
      walBuildLogName(pWal, ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, i))->firstVer, fnameStr);
111
      taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
112
      walBuildIdxName(pWal, ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, i))->firstVer, fnameStr);
113
      taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
114
    }
L
Liu Jicong 已提交
115
    // pop from fileInfoSet
L
Liu Jicong 已提交
116 117 118 119
    taosArraySetSize(pWal->fileInfoSet, pWal->writeCur + 1);
  }

  walBuildIdxName(pWal, walGetCurFileFirstVer(pWal), fnameStr);
L
Liu Jicong 已提交
120
  TdFilePtr pIdxFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND);
L
Liu Jicong 已提交
121

L
Liu Jicong 已提交
122
  if (pIdxFile == NULL) {
L
Liu Jicong 已提交
123
    ASSERT(0);
wafwerar's avatar
wafwerar 已提交
124
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
125 126
    return -1;
  }
L
Liu Jicong 已提交
127
  int64_t idxOff = walGetVerIdxOffset(pWal, ver);
L
Liu Jicong 已提交
128
  code = taosLSeekFile(pIdxFile, idxOff, SEEK_SET);
L
Liu Jicong 已提交
129
  if (code < 0) {
L
Liu Jicong 已提交
130
    ASSERT(0);
wafwerar's avatar
wafwerar 已提交
131
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
132 133
    return -1;
  }
L
Liu Jicong 已提交
134
  // read idx file and get log file pos
L
Liu Jicong 已提交
135
  SWalIdxEntry entry;
L
Liu Jicong 已提交
136
  if (taosReadFile(pIdxFile, &entry, sizeof(SWalIdxEntry)) != sizeof(SWalIdxEntry)) {
L
Liu Jicong 已提交
137
    ASSERT(0);
wafwerar's avatar
wafwerar 已提交
138
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
139 140 141 142 143
    return -1;
  }
  ASSERT(entry.ver == ver);

  walBuildLogName(pWal, walGetCurFileFirstVer(pWal), fnameStr);
L
Liu Jicong 已提交
144 145
  TdFilePtr pLogFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND);
  if (pLogFile == NULL) {
L
Liu Jicong 已提交
146
    // TODO
L
Liu Jicong 已提交
147
    terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
148
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
149 150
    return -1;
  }
L
Liu Jicong 已提交
151
  code = taosLSeekFile(pLogFile, entry.offset, SEEK_SET);
L
Liu Jicong 已提交
152 153
  if (code < 0) {
    // TODO
L
Liu Jicong 已提交
154
    terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
155
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
156 157
    return -1;
  }
L
Liu Jicong 已提交
158
  // validate offset
L
Liu Jicong 已提交
159
  SWalCkHead head;
L
Liu Jicong 已提交
160 161
  ASSERT(taosValidFile(pLogFile));
  int64_t size = taosReadFile(pLogFile, &head, sizeof(SWalCkHead));
L
Liu Jicong 已提交
162
  if (size != sizeof(SWalCkHead)) {
L
Liu Jicong 已提交
163
    ASSERT(0);
L
Liu Jicong 已提交
164
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
165 166 167 168 169
    return -1;
  }
  code = walValidHeadCksum(&head);

  ASSERT(code == 0);
L
Liu Jicong 已提交
170
  if (code != 0) {
L
Liu Jicong 已提交
171
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
M
Minghao Li 已提交
172
    ASSERT(0);
L
Liu Jicong 已提交
173
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
174 175
    return -1;
  }
L
Liu Jicong 已提交
176
  if (head.head.version != ver) {
L
Liu Jicong 已提交
177 178
    ASSERT(0);
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
179
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
180 181
    return -1;
  }
L
Liu Jicong 已提交
182

L
Liu Jicong 已提交
183
  // truncate old files
L
Liu Jicong 已提交
184
  code = taosFtruncateFile(pLogFile, entry.offset);
L
Liu Jicong 已提交
185
  if (code < 0) {
L
Liu Jicong 已提交
186 187
    ASSERT(0);
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
188
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
189 190
    return -1;
  }
L
Liu Jicong 已提交
191
  code = taosFtruncateFile(pIdxFile, idxOff);
L
Liu Jicong 已提交
192
  if (code < 0) {
L
Liu Jicong 已提交
193 194
    ASSERT(0);
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
195
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
196 197
    return -1;
  }
L
Liu Jicong 已提交
198
  pWal->vers.lastVer = ver - 1;
L
Liu Jicong 已提交
199 200 201 202
  if (pWal->vers.lastVer < pWal->vers.firstVer) {
    ASSERT(pWal->vers.lastVer == pWal->vers.firstVer - 1);
    pWal->vers.firstVer = -1;
  }
L
Liu Jicong 已提交
203 204
  ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->lastVer = ver - 1;
  ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->fileSize = entry.offset;
205 206 207 208
  if (((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->lastVer < ver - 1) {
    ASSERT(((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->fileSize == 0);
    ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->firstVer = -1;
  }
L
Liu Jicong 已提交
209 210 211
  taosCloseFile(&pIdxFile);
  taosCloseFile(&pLogFile);

212 213 214
  taosFsyncFile(pWal->pLogFile);
  taosFsyncFile(pWal->pIdxFile);

L
Liu Jicong 已提交
215
  walSaveMeta(pWal);
L
Liu Jicong 已提交
216

L
Liu Jicong 已提交
217
  // unlock
wafwerar's avatar
wafwerar 已提交
218
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
219 220
  return 0;
}
L
Liu Jicong 已提交
221

L
Liu Jicong 已提交
222 223 224 225 226
static FORCE_INLINE int32_t walCheckAndRoll(SWal *pWal) {
  if (taosArrayGetSize(pWal->fileInfoSet) == 0) {
    if (walRollImpl(pWal) < 0) {
      return -1;
    }
227 228 229 230 231 232 233 234 235 236 237
    return 0;
  }

  int64_t passed = walGetSeq() - pWal->lastRollSeq;
  if (pWal->cfg.rollPeriod != -1 && pWal->cfg.rollPeriod != 0 && passed > pWal->cfg.rollPeriod) {
    if (walRollImpl(pWal) < 0) {
      return -1;
    }
  } else if (pWal->cfg.segSize != -1 && pWal->cfg.segSize != 0 && walGetLastFileSize(pWal) > pWal->cfg.segSize) {
    if (walRollImpl(pWal) < 0) {
      return -1;
L
Liu Jicong 已提交
238 239
    }
  }
240

L
Liu Jicong 已提交
241 242 243
  return 0;
}

L
Liu Jicong 已提交
244
int32_t walBeginSnapshot(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
245
  pWal->vers.verInSnapshotting = ver;
L
Liu Jicong 已提交
246 247
  // check file rolling
  if (pWal->cfg.retentionPeriod == 0) {
L
Liu Jicong 已提交
248
    taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
249 250 251
    if (walGetLastFileSize(pWal) != 0) {
      walRollImpl(pWal);
    }
L
Liu Jicong 已提交
252
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
253 254 255 256 257
  }

  return 0;
}

L
Liu Jicong 已提交
258
int32_t walEndSnapshot(SWal *pWal) {
L
Liu Jicong 已提交
259 260
  int32_t code = 0;
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
261
  int64_t ver = pWal->vers.verInSnapshotting;
L
Liu Jicong 已提交
262 263 264 265
  if (ver == -1) {
    code = -1;
    goto END;
  };
L
Liu Jicong 已提交
266

L
Liu Jicong 已提交
267
  pWal->vers.snapshotVer = ver;
L
Liu Jicong 已提交
268 269
  int ts = taosGetTimestampSec();

270
  void *pIter = NULL;
271 272 273 274 275
  while (1) {
    pIter = taosHashIterate(pWal->pRefHash, pIter);
    if (pIter == NULL) break;
    SWalRef *pRef = *(SWalRef **)pIter;
    if (pRef->refVer == -1) continue;
276
    ver = TMIN(ver, pRef->refVer);
277 278
  }

L
Liu Jicong 已提交
279 280 281
  int          deleteCnt = 0;
  int64_t      newTotSize = pWal->totSize;
  SWalFileInfo tmp;
L
Liu Jicong 已提交
282
  tmp.firstVer = ver;
L
Liu Jicong 已提交
283
  // find files safe to delete
L
Liu Jicong 已提交
284
  SWalFileInfo *pInfo = taosArraySearch(pWal->fileInfoSet, &tmp, compareWalFileInfo, TD_LE);
285 286 287 288 289 290 291 292 293 294 295 296 297
  if (pInfo) {
    if (ver >= pInfo->lastVer) {
      pInfo++;
    }
    // iterate files, until the searched result
    for (SWalFileInfo *iter = pWal->fileInfoSet->pData; iter < pInfo; iter++) {
      if ((pWal->cfg.retentionSize != -1 && newTotSize > pWal->cfg.retentionSize) ||
          (pWal->cfg.retentionPeriod != -1 && iter->closeTs + pWal->cfg.retentionPeriod > ts)) {
        // delete according to file size or close time
        deleteCnt++;
        newTotSize -= iter->fileSize;
      }
    }
298 299
    int32_t actualDelete = 0;
    char    fnameStr[WAL_FILE_LEN];
300 301 302 303
    // remove file
    for (int i = 0; i < deleteCnt; i++) {
      pInfo = taosArrayGet(pWal->fileInfoSet, i);
      walBuildLogName(pWal, pInfo->firstVer, fnameStr);
304 305 306
      if (taosRemoveFile(fnameStr) < 0) {
        goto UPDATE_META;
      }
307
      walBuildIdxName(pWal, pInfo->firstVer, fnameStr);
308 309 310 311
      if (taosRemoveFile(fnameStr) < 0) {
        ASSERT(0);
      }
      actualDelete++;
L
Liu Jicong 已提交
312 313
    }

314
  UPDATE_META:
315
    // make new array, remove files
316
    taosArrayPopFrontBatch(pWal->fileInfoSet, actualDelete);
317 318 319 320 321 322
    if (taosArrayGetSize(pWal->fileInfoSet) == 0) {
      pWal->writeCur = -1;
      pWal->vers.firstVer = -1;
    } else {
      pWal->vers.firstVer = ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, 0))->firstVer;
    }
L
Liu Jicong 已提交
323
  }
L
Liu Jicong 已提交
324
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
Liu Jicong 已提交
325
  pWal->totSize = newTotSize;
L
Liu Jicong 已提交
326
  pWal->vers.verInSnapshotting = -1;
L
Liu Jicong 已提交
327

L
Liu Jicong 已提交
328
  // save snapshot ver, commit ver
L
Liu Jicong 已提交
329
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
330
  if (code < 0) {
L
Liu Jicong 已提交
331
    goto END;
L
Liu Jicong 已提交
332 333
  }

L
Liu Jicong 已提交
334 335 336
END:
  taosThreadMutexUnlock(&pWal->mutex);
  return code;
L
Liu Jicong 已提交
337 338
}

L
Liu Jicong 已提交
339
int32_t walRollImpl(SWal *pWal) {
L
Liu Jicong 已提交
340
  int32_t code = 0;
L
Liu Jicong 已提交
341 342
  if (pWal->pIdxFile != NULL) {
    code = taosCloseFile(&pWal->pIdxFile);
L
Liu Jicong 已提交
343
    if (code != 0) {
L
Liu Jicong 已提交
344
      terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
345
      goto END;
L
Liu Jicong 已提交
346
    }
L
Liu Jicong 已提交
347
  }
L
Liu Jicong 已提交
348 349
  if (pWal->pLogFile != NULL) {
    code = taosCloseFile(&pWal->pLogFile);
L
Liu Jicong 已提交
350
    if (code != 0) {
L
Liu Jicong 已提交
351
      terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
352
      goto END;
L
Liu Jicong 已提交
353
    }
L
Liu Jicong 已提交
354
  }
L
Liu Jicong 已提交
355
  TdFilePtr pIdxFile, pLogFile;
L
Liu Jicong 已提交
356
  // create new file
L
Liu Jicong 已提交
357
  int64_t newFileFirstVer = pWal->vers.lastVer + 1;
L
Liu Jicong 已提交
358
  char    fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
359 360 361
  walBuildIdxName(pWal, newFileFirstVer, fnameStr);
  pIdxFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
  if (pIdxFile == NULL) {
L
Liu Jicong 已提交
362
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
363 364
    code = -1;
    goto END;
L
Liu Jicong 已提交
365
  }
L
Liu Jicong 已提交
366 367 368
  walBuildLogName(pWal, newFileFirstVer, fnameStr);
  pLogFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
  if (pLogFile == NULL) {
L
Liu Jicong 已提交
369
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
370 371
    code = -1;
    goto END;
L
Liu Jicong 已提交
372
  }
L
Liu Jicong 已提交
373
  // error code was set inner
L
Liu Jicong 已提交
374
  code = walRollFileInfo(pWal);
L
Liu Jicong 已提交
375
  if (code != 0) {
L
Liu Jicong 已提交
376
    goto END;
L
Liu Jicong 已提交
377
  }
L
Liu Jicong 已提交
378

L
Liu Jicong 已提交
379
  // switch file
L
Liu Jicong 已提交
380 381
  pWal->pIdxFile = pIdxFile;
  pWal->pLogFile = pLogFile;
L
Liu Jicong 已提交
382
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
fix  
Liu Jicong 已提交
383
  ASSERT(pWal->writeCur >= 0);
L
Liu Jicong 已提交
384 385

  pWal->lastRollSeq = walGetSeq();
L
Liu Jicong 已提交
386

L
Liu Jicong 已提交
387 388
  walSaveMeta(pWal);

L
Liu Jicong 已提交
389 390
END:
  return code;
L
Liu Jicong 已提交
391 392
}

L
Liu Jicong 已提交
393
static int32_t walWriteIndex(SWal *pWal, int64_t ver, int64_t offset) {
L
Liu Jicong 已提交
394
  SWalIdxEntry entry = {.ver = ver, .offset = offset};
L
Liu Jicong 已提交
395
  int64_t      idxOffset = taosLSeekFile(pWal->pIdxFile, 0, SEEK_END);
S
Shengliang Guan 已提交
396
  wDebug("vgId:%d, write index, index:%" PRId64 ", offset:%" PRId64 ", at %" PRId64, pWal->cfg.vgId, ver, offset,
S
Shengliang Guan 已提交
397
         idxOffset);
L
Liu Jicong 已提交
398
  int64_t size = taosWriteFile(pWal->pIdxFile, &entry, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
399
  if (size != sizeof(SWalIdxEntry)) {
L
Liu Jicong 已提交
400
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
401
    // TODO truncate
L
Liu Jicong 已提交
402
    return -1;
L
Liu Jicong 已提交
403 404 405 406
  }
  return 0;
}

L
Liu Jicong 已提交
407 408 409 410
// TODO  gurantee atomicity by truncate failed writing
static FORCE_INLINE int32_t walWriteImpl(SWal *pWal, int64_t index, tmsg_t msgType, SWalSyncInfo syncMeta,
                                         const void *body, int32_t bodyLen) {
  int64_t code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
411

L
Liu Jicong 已提交
412
  int64_t offset = walGetCurFileOffset(pWal);
L
Liu Jicong 已提交
413

L
Liu Jicong 已提交
414
  pWal->writeHead.head.version = index;
L
Liu Jicong 已提交
415
  pWal->writeHead.head.bodyLen = bodyLen;
L
Liu Jicong 已提交
416
  pWal->writeHead.head.msgType = msgType;
417
  pWal->writeHead.head.ingestTs = 0;
L
Liu Jicong 已提交
418

419
  // sync info for sync module
L
Liu Jicong 已提交
420
  pWal->writeHead.head.syncMeta = syncMeta;
L
Liu Jicong 已提交
421

L
Liu Jicong 已提交
422 423
  pWal->writeHead.cksumHead = walCalcHeadCksum(&pWal->writeHead);
  pWal->writeHead.cksumBody = walCalcBodyCksum(body, bodyLen);
424

L
Liu Jicong 已提交
425 426
  wDebug("vgId:%d, wal write log %ld, msgType: %s", pWal->cfg.vgId, index, TMSG_INFO(msgType));

L
Liu Jicong 已提交
427
  if (taosWriteFile(pWal->pLogFile, &pWal->writeHead, sizeof(SWalCkHead)) != sizeof(SWalCkHead)) {
428
    // TODO ftruncate
L
fix  
Liu Jicong 已提交
429
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
430 431
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
Liu Jicong 已提交
432 433
    code = -1;
    goto END;
J
Jeff Tao 已提交
434
  }
S
TD-1846  
Shengliang Guan 已提交
435

L
Liu Jicong 已提交
436
  if (taosWriteFile(pWal->pLogFile, (char *)body, bodyLen) != bodyLen) {
437
    // TODO ftruncate
L
fix  
Liu Jicong 已提交
438
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
439 440
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
Liu Jicong 已提交
441 442
    code = -1;
    goto END;
L
Liu Jicong 已提交
443
  }
L
Liu Jicong 已提交
444

L
Liu Jicong 已提交
445
  code = walWriteIndex(pWal, index, offset);
L
Liu Jicong 已提交
446 447 448
  if (code < 0) {
    // TODO ftruncate
    goto END;
L
Liu Jicong 已提交
449
  }
450

L
Liu Jicong 已提交
451
  // set status
452
  if (pWal->vers.firstVer == -1) pWal->vers.firstVer = index;
L
Liu Jicong 已提交
453
  pWal->vers.lastVer = index;
L
Liu Jicong 已提交
454
  pWal->totSize += sizeof(SWalCkHead) + bodyLen;
455 456 457
  if (walGetCurFileInfo(pWal)->firstVer == -1) {
    walGetCurFileInfo(pWal)->firstVer = index;
  }
L
Liu Jicong 已提交
458
  walGetCurFileInfo(pWal)->lastVer = index;
L
Liu Jicong 已提交
459
  walGetCurFileInfo(pWal)->fileSize += sizeof(SWalCkHead) + bodyLen;
L
Liu Jicong 已提交
460

L
Liu Jicong 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  return 0;
END:
  return -1;
}

int64_t walAppendLog(SWal *pWal, tmsg_t msgType, SWalSyncInfo syncMeta, const void *body, int32_t bodyLen) {
  taosThreadMutexLock(&pWal->mutex);

  int64_t index = pWal->vers.lastVer + 1;

  if (walCheckAndRoll(pWal) < 0) {
    taosThreadMutexUnlock(&pWal->mutex);
    return -1;
  }

L
Liu Jicong 已提交
476
  if (pWal->pLogFile == NULL || pWal->pIdxFile == NULL || pWal->writeCur < 0) {
L
Liu Jicong 已提交
477 478 479 480 481 482
    if (walInitWriteFile(pWal) < 0) {
      taosThreadMutexUnlock(&pWal->mutex);
      return -1;
    }
  }

L
Liu Jicong 已提交
483
  ASSERT(pWal->pLogFile != NULL && pWal->pIdxFile != NULL && pWal->writeCur >= 0);
L
Liu Jicong 已提交
484 485 486 487 488 489

  if (walWriteImpl(pWal, index, msgType, syncMeta, body, bodyLen) < 0) {
    taosThreadMutexUnlock(&pWal->mutex);
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
490
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
491 492
  return index;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
493

L
Liu Jicong 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
int32_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SWalSyncInfo syncMeta, const void *body,
                             int32_t bodyLen) {
  int32_t code = 0;

  taosThreadMutexLock(&pWal->mutex);

  // concurrency control:
  // if logs are write with assigned index,
  // smaller index must be write before larger one
  if (index != pWal->vers.lastVer + 1) {
    terrno = TSDB_CODE_WAL_INVALID_VER;
    taosThreadMutexUnlock(&pWal->mutex);
    return -1;
  }

  if (walCheckAndRoll(pWal) < 0) {
    taosThreadMutexUnlock(&pWal->mutex);
    return -1;
  }

L
Liu Jicong 已提交
514
  if (pWal->pIdxFile == NULL || pWal->pIdxFile == NULL || pWal->writeCur < 0) {
L
Liu Jicong 已提交
515 516 517 518 519 520
    if (walInitWriteFile(pWal) < 0) {
      taosThreadMutexUnlock(&pWal->mutex);
      return -1;
    }
  }

L
Liu Jicong 已提交
521
  ASSERT(pWal->pIdxFile != NULL && pWal->pLogFile != NULL && pWal->writeCur >= 0);
L
Liu Jicong 已提交
522 523 524 525 526 527 528 529

  if (walWriteImpl(pWal, index, msgType, syncMeta, body, bodyLen) < 0) {
    taosThreadMutexUnlock(&pWal->mutex);
    return -1;
  }

  taosThreadMutexUnlock(&pWal->mutex);
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
530 531
}

L
Liu Jicong 已提交
532
int32_t walWrite(SWal *pWal, int64_t index, tmsg_t msgType, const void *body, int32_t bodyLen) {
L
Liu Jicong 已提交
533
  SWalSyncInfo syncMeta = {
L
Liu Jicong 已提交
534 535 536 537
      .isWeek = -1,
      .seqNum = UINT64_MAX,
      .term = UINT64_MAX,
  };
L
Liu Jicong 已提交
538
  return walWriteWithSyncInfo(pWal, index, msgType, syncMeta, body, bodyLen);
L
Liu Jicong 已提交
539 540
}

L
Liu Jicong 已提交
541
void walFsync(SWal *pWal, bool forceFsync) {
L
Liu Jicong 已提交
542
  if (forceFsync || (pWal->cfg.level == TAOS_WAL_FSYNC && pWal->cfg.fsyncPeriod == 0)) {
L
Liu Jicong 已提交
543
    wTrace("vgId:%d, fileId:%" PRId64 ".log, do fsync", pWal->cfg.vgId, walGetCurFileFirstVer(pWal));
L
Liu Jicong 已提交
544
    if (taosFsyncFile(pWal->pLogFile) < 0) {
L
Liu Jicong 已提交
545 546
      wError("vgId:%d, file:%" PRId64 ".log, fsync failed since %s", pWal->cfg.vgId, walGetCurFileFirstVer(pWal),
             strerror(errno));
547 548
    }
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
549
}