walWrite.c 20.1 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"
19
#include "tglobal.h"
S
Shengliang Guan 已提交
20
#include "walInt.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
21

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

L
Liu Jicong 已提交
25 26
  wInfo("vgId:%d, restore from snapshot, version %" PRId64, pWal->cfg.vgId, ver);

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

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

  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);
L
Liu Jicong 已提交
48 49
      if (taosRemoveFile(fnameStr) < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
50
        wError("vgId:%d, restore from snapshot, cannot remove file %s since %s", pWal->cfg.vgId, fnameStr, terrstr());
L
Liu Jicong 已提交
51
        taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
52 53
        return -1;
      }
S
Shengliang Guan 已提交
54
      wInfo("vgId:%d, restore from snapshot, remove file %s", pWal->cfg.vgId, fnameStr);
55 56

      walBuildIdxName(pWal, pFileInfo->firstVer, fnameStr);
L
Liu Jicong 已提交
57 58
      if (taosRemoveFile(fnameStr) < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
59
        wError("vgId:%d, cannot remove file %s since %s", pWal->cfg.vgId, fnameStr, terrstr());
L
Liu Jicong 已提交
60
        taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
61 62
        return -1;
      }
S
Shengliang Guan 已提交
63
      wInfo("vgId:%d, restore from snapshot, remove file %s", pWal->cfg.vgId, fnameStr);
64 65
    }
  }
66
  (void)walRemoveMeta(pWal);
67 68 69 70 71 72

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

  taosArrayClear(pWal->fileInfoSet);
73
  pWal->vers.firstVer = ver + 1;
74
  pWal->vers.lastVer = ver;
75 76
  pWal->vers.commitVer = ver;
  pWal->vers.snapshotVer = ver;
77
  pWal->vers.verInSnapshotting = -1;
78 79 80

  taosThreadMutexUnlock(&pWal->mutex);
  return 0;
81 82
}

L
Liu Jicong 已提交
83 84 85 86 87 88
int32_t walApplyVer(SWal *pWal, int64_t ver) {
  // TODO: error check
  pWal->vers.appliedVer = ver;
  return 0;
}

L
Liu Jicong 已提交
89
int32_t walCommit(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
90 91 92
  if (ver < pWal->vers.commitVer) {
    return 0;
  }
L
Liu Jicong 已提交
93
  if (ver > pWal->vers.lastVer || pWal->vers.commitVer < pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
94
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
95 96
    return -1;
  }
L
Liu Jicong 已提交
97
  pWal->vers.commitVer = ver;
L
Liu Jicong 已提交
98 99 100 101
  return 0;
}

int32_t walRollback(SWal *pWal, int64_t ver) {
102
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
103
  wInfo("vgId:%d, wal rollback for version %" PRId64, pWal->cfg.vgId, ver);
L
Liu Jicong 已提交
104 105
  int64_t code;
  char    fnameStr[WAL_FILE_LEN];
106
  if (ver > pWal->vers.lastVer || ver <= pWal->vers.commitVer || ver <= pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
107
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
108
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
109 110 111
    return -1;
  }

L
Liu Jicong 已提交
112 113
  // find correct file
  if (ver < walGetLastFileFirstVer(pWal)) {
L
Liu Jicong 已提交
114 115 116
    // change current files
    code = walChangeWrite(pWal, ver);
    if (code < 0) {
L
Liu Jicong 已提交
117
      taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
118 119 120
      return -1;
    }

121
    // delete files in descending order
L
Liu Jicong 已提交
122
    int fileSetSize = taosArrayGetSize(pWal->fileInfoSet);
123
    for (int i = pWal->writeCur + 1; i < fileSetSize; i++) {
X
Xiaoyu Wang 已提交
124
      SWalFileInfo *pInfo = taosArrayPop(pWal->fileInfoSet);
125 126

      walBuildLogName(pWal, pInfo->firstVer, fnameStr);
L
Liu Jicong 已提交
127
      wDebug("vgId:%d, wal remove file %s for rollback", pWal->cfg.vgId, fnameStr);
128
      taosRemoveFile(fnameStr);
129
      walBuildIdxName(pWal, pInfo->firstVer, fnameStr);
L
Liu Jicong 已提交
130
      wDebug("vgId:%d, wal remove file %s for rollback", pWal->cfg.vgId, fnameStr);
131
      taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
132 133 134 135
    }
  }

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

L
Liu Jicong 已提交
138
  if (pIdxFile == NULL) {
wafwerar's avatar
wafwerar 已提交
139
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
140 141
    return -1;
  }
L
Liu Jicong 已提交
142
  int64_t idxOff = walGetVerIdxOffset(pWal, ver);
L
Liu Jicong 已提交
143
  code = taosLSeekFile(pIdxFile, idxOff, SEEK_SET);
L
Liu Jicong 已提交
144
  if (code < 0) {
wafwerar's avatar
wafwerar 已提交
145
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
146 147
    return -1;
  }
L
Liu Jicong 已提交
148
  // read idx file and get log file pos
L
Liu Jicong 已提交
149
  SWalIdxEntry entry;
L
Liu Jicong 已提交
150
  if (taosReadFile(pIdxFile, &entry, sizeof(SWalIdxEntry)) != sizeof(SWalIdxEntry)) {
wafwerar's avatar
wafwerar 已提交
151
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
152 153 154 155
    return -1;
  }

  walBuildLogName(pWal, walGetCurFileFirstVer(pWal), fnameStr);
L
Liu Jicong 已提交
156
  TdFilePtr pLogFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND);
L
Liu Jicong 已提交
157
  wDebug("vgId:%d, wal truncate file %s", pWal->cfg.vgId, fnameStr);
L
Liu Jicong 已提交
158
  if (pLogFile == NULL) {
L
Liu Jicong 已提交
159
    // TODO
L
Liu Jicong 已提交
160
    terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
161
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
162 163
    return -1;
  }
L
Liu Jicong 已提交
164
  code = taosLSeekFile(pLogFile, entry.offset, SEEK_SET);
L
Liu Jicong 已提交
165 166
  if (code < 0) {
    // TODO
L
Liu Jicong 已提交
167
    terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
168
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
169 170
    return -1;
  }
L
Liu Jicong 已提交
171
  // validate offset
L
Liu Jicong 已提交
172
  SWalCkHead head;
L
Liu Jicong 已提交
173
  int64_t    size = taosReadFile(pLogFile, &head, sizeof(SWalCkHead));
L
Liu Jicong 已提交
174
  if (size != sizeof(SWalCkHead)) {
L
Liu Jicong 已提交
175
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
176 177 178 179
    return -1;
  }
  code = walValidHeadCksum(&head);

L
Liu Jicong 已提交
180
  if (code != 0) {
L
Liu Jicong 已提交
181
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
182
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
183 184
    return -1;
  }
L
Liu Jicong 已提交
185
  if (head.head.version != ver) {
L
Liu Jicong 已提交
186
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
187
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
188 189
    return -1;
  }
L
Liu Jicong 已提交
190

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

210 211 212 213 214 215
  code = walSaveMeta(pWal);
  if (code < 0) {
    wError("vgId:%d, failed to save meta since %s", pWal->cfg.vgId, terrstr());
    taosThreadMutexUnlock(&pWal->mutex);
    return -1;
  }
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

241
  if (walGetLastFileCachedSize(pWal) > tsWalFsyncDataSizeLimit) {
242 243 244 245 246
    if (walSaveMeta(pWal) < 0) {
      return -1;
    }
  }

L
Liu Jicong 已提交
247 248 249
  return 0;
}

250
int32_t walBeginSnapshot(SWal *pWal, int64_t ver, int64_t logRetention) {
251
  taosThreadMutexLock(&pWal->mutex);
252
  ASSERT(logRetention >= 0);
L
Liu Jicong 已提交
253
  pWal->vers.verInSnapshotting = ver;
254 255 256 257 258
  pWal->vers.logRetention = logRetention;

  wDebug("vgId:%d, wal begin snapshot for version %" PRId64 ", log retention %" PRId64 " first ver %" PRId64
         ", last ver %" PRId64,
         pWal->cfg.vgId, ver, pWal->vers.logRetention, pWal->vers.firstVer, pWal->vers.lastVer);
L
Liu Jicong 已提交
259
  // check file rolling
260 261 262 263
  if (walGetLastFileSize(pWal) != 0) {
    if (walRollImpl(pWal) < 0) {
      wError("vgId:%d, failed to roll wal files since %s", pWal->cfg.vgId, terrstr());
      goto _err;
L
Liu Jicong 已提交
264
    }
L
Liu Jicong 已提交
265
  }
266

267
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
268
  return 0;
269 270 271 272

_err:
  taosThreadMutexUnlock(&pWal->mutex);
  return -1;
L
Liu Jicong 已提交
273 274
}

L
Liu Jicong 已提交
275
int32_t walEndSnapshot(SWal *pWal) {
L
Liu Jicong 已提交
276 277
  int32_t code = 0;
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
278
  int64_t ver = pWal->vers.verInSnapshotting;
L
Liu Jicong 已提交
279

280 281 282
  wDebug("vgId:%d, wal end snapshot for version %" PRId64 ", log retention %" PRId64 " first ver %" PRId64
         ", last ver %" PRId64,
         pWal->cfg.vgId, ver, pWal->vers.logRetention, pWal->vers.firstVer, pWal->vers.lastVer);
L
Liu Jicong 已提交
283

L
Liu Jicong 已提交
284 285 286 287
  if (ver == -1) {
    code = -1;
    goto END;
  };
L
Liu Jicong 已提交
288

L
Liu Jicong 已提交
289
  pWal->vers.snapshotVer = ver;
L
Liu Jicong 已提交
290 291
  int ts = taosGetTimestampSec();

292
  ver = TMAX(ver - pWal->vers.logRetention, pWal->vers.firstVer - 1);
293
  void *pIter = NULL;
294 295 296 297 298
  while (1) {
    pIter = taosHashIterate(pWal->pRefHash, pIter);
    if (pIter == NULL) break;
    SWalRef *pRef = *(SWalRef **)pIter;
    if (pRef->refVer == -1) continue;
L
Liu Jicong 已提交
299 300
    ver = TMIN(ver, pRef->refVer - 1);
    wDebug("vgId:%d, wal found ref %" PRId64 ", refId %" PRId64, pWal->cfg.vgId, pRef->refVer, pRef->refId);
301 302
  }

L
Liu Jicong 已提交
303 304 305
  int          deleteCnt = 0;
  int64_t      newTotSize = pWal->totSize;
  SWalFileInfo tmp;
L
Liu Jicong 已提交
306
  tmp.firstVer = ver;
L
Liu Jicong 已提交
307
  // find files safe to delete
L
Liu Jicong 已提交
308
  SWalFileInfo *pInfo = taosArraySearch(pWal->fileInfoSet, &tmp, compareWalFileInfo, TD_LE);
309
  if (pInfo) {
L
Liu Jicong 已提交
310
    SWalFileInfo *pLastFileInfo = taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
311
    wDebug("vgId:%d, wal search found file info: first:%" PRId64 " last:%" PRId64, pWal->cfg.vgId, pInfo->firstVer,
L
Liu Jicong 已提交
312
           pInfo->lastVer);
313
    if (ver >= pInfo->lastVer) {
314
      pInfo++;
L
Liu Jicong 已提交
315
      wDebug("vgId:%d, wal remove advance one file: first:%" PRId64 " last:%" PRId64, pWal->cfg.vgId, pInfo->firstVer,
L
Liu Jicong 已提交
316
             pInfo->lastVer);
317
    }
L
Liu Jicong 已提交
318
    if (pInfo <= pLastFileInfo) {
L
Liu Jicong 已提交
319
      wDebug("vgId:%d, wal end remove for first:%" PRId64 " last:%" PRId64, pWal->cfg.vgId, pInfo->firstVer,
L
Liu Jicong 已提交
320
             pInfo->lastVer);
L
Liu Jicong 已提交
321
    } else {
L
Liu Jicong 已提交
322
      wDebug("vgId:%d, wal no remove", pWal->cfg.vgId);
323
    }
324

325 326
    // iterate files, until the searched result
    for (SWalFileInfo *iter = pWal->fileInfoSet->pData; iter < pInfo; iter++) {
L
Liu Jicong 已提交
327 328 329
      wDebug("vgId:%d, wal check remove file %" PRId64 "(file size %" PRId64 " close ts %" PRId64
             "), new tot size %" PRId64,
             pWal->cfg.vgId, iter->firstVer, iter->fileSize, iter->closeTs, newTotSize);
330
      if ((pWal->cfg.retentionSize != -1 && pWal->cfg.retentionSize != 0 && newTotSize > pWal->cfg.retentionSize) ||
331 332
          ((pWal->cfg.retentionPeriod == 0) || (pWal->cfg.retentionPeriod != -1 && iter->closeTs != -1 &&
                                                iter->closeTs + pWal->cfg.retentionPeriod < ts))) {
333
        // delete according to file size or close time
L
Liu Jicong 已提交
334
        wDebug("vgId:%d, check pass", pWal->cfg.vgId);
335 336
        deleteCnt++;
        newTotSize -= iter->fileSize;
337
        taosArrayPush(pWal->toDeleteFiles, iter);
338
      }
L
Liu Jicong 已提交
339
      wDebug("vgId:%d, check not pass", pWal->cfg.vgId);
340
    }
L
Liu Jicong 已提交
341

342
  UPDATE_META:
343
    // make new array, remove files
344
    taosArrayPopFrontBatch(pWal->fileInfoSet, deleteCnt);
345 346
    if (taosArrayGetSize(pWal->fileInfoSet) == 0) {
      pWal->writeCur = -1;
347
      pWal->vers.firstVer = pWal->vers.lastVer + 1;
348 349 350
    } else {
      pWal->vers.firstVer = ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, 0))->firstVer;
    }
L
Liu Jicong 已提交
351
  }
L
Liu Jicong 已提交
352
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
Liu Jicong 已提交
353
  pWal->totSize = newTotSize;
L
Liu Jicong 已提交
354
  pWal->vers.verInSnapshotting = -1;
L
Liu Jicong 已提交
355

L
Liu Jicong 已提交
356
  // save snapshot ver, commit ver
L
Liu Jicong 已提交
357
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
358
  if (code < 0) {
L
Liu Jicong 已提交
359
    goto END;
L
Liu Jicong 已提交
360 361
  }

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  // delete files
  deleteCnt = taosArrayGetSize(pWal->toDeleteFiles);
  wDebug("vgId:%d, wal should delete %d files", pWal->cfg.vgId, deleteCnt);
  char fnameStr[WAL_FILE_LEN];
  for (int i = 0; i < deleteCnt; i++) {
    pInfo = taosArrayGet(pWal->toDeleteFiles, i);
    walBuildLogName(pWal, pInfo->firstVer, fnameStr);
    wDebug("vgId:%d, wal remove file %s", pWal->cfg.vgId, fnameStr);
    if (taosRemoveFile(fnameStr) < 0 && errno != ENOENT) {
      wError("vgId:%d, failed to remove log file %s due to %s", pWal->cfg.vgId, fnameStr, strerror(errno));
      goto END;
    }
    walBuildIdxName(pWal, pInfo->firstVer, fnameStr);
    wDebug("vgId:%d, wal remove file %s", pWal->cfg.vgId, fnameStr);
    if (taosRemoveFile(fnameStr) < 0 && errno != ENOENT) {
L
Liu Jicong 已提交
377 378
      wError("vgId:%d, failed to remove idx file %s due to %s", pWal->cfg.vgId, fnameStr, strerror(errno));
      goto END;
379 380 381 382
    }
  }
  taosArrayClear(pWal->toDeleteFiles);

L
Liu Jicong 已提交
383 384 385
END:
  taosThreadMutexUnlock(&pWal->mutex);
  return code;
L
Liu Jicong 已提交
386 387
}

L
Liu Jicong 已提交
388
int32_t walRollImpl(SWal *pWal) {
L
Liu Jicong 已提交
389
  int32_t code = 0;
390

L
Liu Jicong 已提交
391
  if (pWal->pIdxFile != NULL) {
392 393 394 395 396
    code = taosFsyncFile(pWal->pIdxFile);
    if (code != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto END;
    }
L
Liu Jicong 已提交
397
    code = taosCloseFile(&pWal->pIdxFile);
L
Liu Jicong 已提交
398
    if (code != 0) {
L
Liu Jicong 已提交
399
      terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
400
      goto END;
L
Liu Jicong 已提交
401
    }
L
Liu Jicong 已提交
402
  }
403

L
Liu Jicong 已提交
404
  if (pWal->pLogFile != NULL) {
405 406 407 408 409
    code = taosFsyncFile(pWal->pLogFile);
    if (code != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto END;
    }
L
Liu Jicong 已提交
410
    code = taosCloseFile(&pWal->pLogFile);
L
Liu Jicong 已提交
411
    if (code != 0) {
L
Liu Jicong 已提交
412
      terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
413
      goto END;
L
Liu Jicong 已提交
414
    }
L
Liu Jicong 已提交
415
  }
416

L
Liu Jicong 已提交
417
  TdFilePtr pIdxFile, pLogFile;
L
Liu Jicong 已提交
418
  // create new file
L
Liu Jicong 已提交
419
  int64_t newFileFirstVer = pWal->vers.lastVer + 1;
L
Liu Jicong 已提交
420
  char    fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
421 422 423
  walBuildIdxName(pWal, newFileFirstVer, fnameStr);
  pIdxFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
  if (pIdxFile == NULL) {
L
Liu Jicong 已提交
424
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
425 426
    code = -1;
    goto END;
L
Liu Jicong 已提交
427
  }
L
Liu Jicong 已提交
428 429
  walBuildLogName(pWal, newFileFirstVer, fnameStr);
  pLogFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
430
  wDebug("vgId:%d, wal create new file for write:%s", pWal->cfg.vgId, fnameStr);
L
Liu Jicong 已提交
431
  if (pLogFile == NULL) {
L
Liu Jicong 已提交
432
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
433 434
    code = -1;
    goto END;
L
Liu Jicong 已提交
435
  }
L
Liu Jicong 已提交
436
  // error code was set inner
L
Liu Jicong 已提交
437
  code = walRollFileInfo(pWal);
L
Liu Jicong 已提交
438
  if (code != 0) {
L
Liu Jicong 已提交
439
    goto END;
L
Liu Jicong 已提交
440
  }
L
Liu Jicong 已提交
441

L
Liu Jicong 已提交
442
  // switch file
L
Liu Jicong 已提交
443 444
  pWal->pIdxFile = pIdxFile;
  pWal->pLogFile = pLogFile;
L
Liu Jicong 已提交
445
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
Liu Jicong 已提交
446 447

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

449 450 451 452 453
  code = walSaveMeta(pWal);
  if (code < 0) {
    wError("vgId:%d, failed to save meta since %s", pWal->cfg.vgId, terrstr());
    goto END;
  }
L
Liu Jicong 已提交
454

L
Liu Jicong 已提交
455 456
END:
  return code;
L
Liu Jicong 已提交
457 458
}

L
Liu Jicong 已提交
459
static int32_t walWriteIndex(SWal *pWal, int64_t ver, int64_t offset) {
L
Liu Jicong 已提交
460
  SWalIdxEntry  entry = {.ver = ver, .offset = offset};
461
  SWalFileInfo *pFileInfo = walGetCurFileInfo(pWal);
L
Liu Jicong 已提交
462

463
  int64_t idxOffset = (entry.ver - pFileInfo->firstVer) * sizeof(SWalIdxEntry);
S
Shengliang Guan 已提交
464
  wDebug("vgId:%d, write index, index:%" PRId64 ", offset:%" PRId64 ", at %" PRId64, pWal->cfg.vgId, ver, offset,
S
Shengliang Guan 已提交
465
         idxOffset);
466

L
Liu Jicong 已提交
467
  int64_t size = taosWriteFile(pWal->pIdxFile, &entry, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
468
  if (size != sizeof(SWalIdxEntry)) {
469
    wError("vgId:%d, failed to write idx entry due to %s. ver:%" PRId64, pWal->cfg.vgId, strerror(errno), ver);
L
Liu Jicong 已提交
470
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
471
    return -1;
L
Liu Jicong 已提交
472
  }
473

474 475 476 477 478
  // check alignment of idx entries
  int64_t endOffset = taosLSeekFile(pWal->pIdxFile, 0, SEEK_END);
  if (endOffset < 0) {
    wFatal("vgId:%d, failed to seek end of idxfile due to %s. ver:%" PRId64 "", pWal->cfg.vgId, strerror(errno), ver);
  }
L
Liu Jicong 已提交
479 480 481
  return 0;
}

L
Liu Jicong 已提交
482 483 484
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) 已提交
485

L
Liu Jicong 已提交
486
  int64_t       offset = walGetCurFileOffset(pWal);
487
  SWalFileInfo *pFileInfo = walGetCurFileInfo(pWal);
L
Liu Jicong 已提交
488

L
Liu Jicong 已提交
489
  pWal->writeHead.head.version = index;
L
Liu Jicong 已提交
490
  pWal->writeHead.head.bodyLen = bodyLen;
L
Liu Jicong 已提交
491
  pWal->writeHead.head.msgType = msgType;
492
  pWal->writeHead.head.ingestTs = 0;
L
Liu Jicong 已提交
493

494
  // sync info for sync module
L
Liu Jicong 已提交
495
  pWal->writeHead.head.syncMeta = syncMeta;
L
Liu Jicong 已提交
496

L
Liu Jicong 已提交
497 498
  pWal->writeHead.cksumHead = walCalcHeadCksum(&pWal->writeHead);
  pWal->writeHead.cksumBody = walCalcBodyCksum(body, bodyLen);
499 500
  wDebug("vgId:%d, wal write log %" PRId64 ", msgType: %s, cksum head %u cksum body %u", pWal->cfg.vgId, index,
         TMSG_INFO(msgType), pWal->writeHead.cksumHead, pWal->writeHead.cksumBody);
L
Liu Jicong 已提交
501

502 503 504 505 506
  code = walWriteIndex(pWal, index, offset);
  if (code < 0) {
    goto END;
  }

L
Liu Jicong 已提交
507
  if (taosWriteFile(pWal->pLogFile, &pWal->writeHead, sizeof(SWalCkHead)) != sizeof(SWalCkHead)) {
L
fix  
Liu Jicong 已提交
508
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
509 510
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
Liu Jicong 已提交
511 512
    code = -1;
    goto END;
J
Jeff Tao 已提交
513
  }
S
TD-1846  
Shengliang Guan 已提交
514

L
Liu Jicong 已提交
515
  if (taosWriteFile(pWal->pLogFile, (char *)body, bodyLen) != bodyLen) {
L
fix  
Liu Jicong 已提交
516
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
517 518
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
Liu Jicong 已提交
519 520
    code = -1;
    goto END;
L
Liu Jicong 已提交
521
  }
L
Liu Jicong 已提交
522

L
Liu Jicong 已提交
523
  // set status
524 525 526
  if (pWal->vers.firstVer == -1) {
    pWal->vers.firstVer = 0;
  }
L
Liu Jicong 已提交
527
  pWal->vers.lastVer = index;
L
Liu Jicong 已提交
528
  pWal->totSize += sizeof(SWalCkHead) + bodyLen;
529 530
  pFileInfo->lastVer = index;
  pFileInfo->fileSize += sizeof(SWalCkHead) + bodyLen;
L
Liu Jicong 已提交
531

L
Liu Jicong 已提交
532
  return 0;
533

L
Liu Jicong 已提交
534
END:
535 536
  // recover in a reverse order
  if (taosFtruncateFile(pWal->pLogFile, offset) < 0) {
537 538
    wFatal("vgId:%d, failed to ftruncate logfile to offset:%" PRId64 " during recovery due to %s", pWal->cfg.vgId,
           offset, strerror(errno));
539 540 541 542 543
    terrno = TAOS_SYSTEM_ERROR(errno);
  }

  int64_t idxOffset = (index - pFileInfo->firstVer) * sizeof(SWalIdxEntry);
  if (taosFtruncateFile(pWal->pIdxFile, idxOffset) < 0) {
544 545
    wFatal("vgId:%d, failed to ftruncate idxfile to offset:%" PRId64 "during recovery due to %s", pWal->cfg.vgId,
           idxOffset, strerror(errno));
546 547
    terrno = TAOS_SYSTEM_ERROR(errno);
  }
L
Liu Jicong 已提交
548 549 550
  return -1;
}

B
Benguang Zhao 已提交
551 552
int64_t walAppendLog(SWal *pWal, int64_t index, tmsg_t msgType, SWalSyncInfo syncMeta, const void *body,
                     int32_t bodyLen) {
L
Liu Jicong 已提交
553 554
  taosThreadMutexLock(&pWal->mutex);

B
Benguang Zhao 已提交
555 556 557 558 559
  if (index != pWal->vers.lastVer + 1) {
    terrno = TSDB_CODE_WAL_INVALID_VER;
    taosThreadMutexUnlock(&pWal->mutex);
    return -1;
  }
L
Liu Jicong 已提交
560 561 562 563 564 565

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

L
Liu Jicong 已提交
566
  if (pWal->pLogFile == NULL || pWal->pIdxFile == NULL || pWal->writeCur < 0) {
L
Liu Jicong 已提交
567 568 569 570 571 572 573 574 575 576 577
    if (walInitWriteFile(pWal) < 0) {
      taosThreadMutexUnlock(&pWal->mutex);
      return -1;
    }
  }

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

wafwerar's avatar
wafwerar 已提交
578
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
579 580
  return index;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
581

L
Liu Jicong 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
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 已提交
602
  if (pWal->pIdxFile == NULL || pWal->pIdxFile == NULL || pWal->writeCur < 0) {
L
Liu Jicong 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615
    if (walInitWriteFile(pWal) < 0) {
      taosThreadMutexUnlock(&pWal->mutex);
      return -1;
    }
  }

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

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

L
Liu Jicong 已提交
618
int32_t walWrite(SWal *pWal, int64_t index, tmsg_t msgType, const void *body, int32_t bodyLen) {
L
Liu Jicong 已提交
619
  SWalSyncInfo syncMeta = {
L
Liu Jicong 已提交
620 621 622 623
      .isWeek = -1,
      .seqNum = UINT64_MAX,
      .term = UINT64_MAX,
  };
L
Liu Jicong 已提交
624
  return walWriteWithSyncInfo(pWal, index, msgType, syncMeta, body, bodyLen);
L
Liu Jicong 已提交
625 626
}

L
Liu Jicong 已提交
627
void walFsync(SWal *pWal, bool forceFsync) {
628
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
629
  if (forceFsync || (pWal->cfg.level == TAOS_WAL_FSYNC && pWal->cfg.fsyncPeriod == 0)) {
L
Liu Jicong 已提交
630
    wTrace("vgId:%d, fileId:%" PRId64 ".log, do fsync", pWal->cfg.vgId, walGetCurFileFirstVer(pWal));
L
Liu Jicong 已提交
631
    if (taosFsyncFile(pWal->pLogFile) < 0) {
L
Liu Jicong 已提交
632 633
      wError("vgId:%d, file:%" PRId64 ".log, fsync failed since %s", pWal->cfg.vgId, walGetCurFileFirstVer(pWal),
             strerror(errno));
634 635
    }
  }
636
  taosThreadMutexUnlock(&pWal->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
637
}