walWrite.c 19.2 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 25 26
int32_t walRestoreFromSnapshot(SWal *pWal, int64_t ver) {
  taosThreadMutexLock(&pWal->mutex);

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

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

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

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

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

  taosThreadMutexUnlock(&pWal->mutex);
  return 0;
79 80
}

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

L
Liu Jicong 已提交
87
int32_t walCommit(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
88 89
  ASSERT(pWal->vers.commitVer >= pWal->vers.snapshotVer);
  ASSERT(pWal->vers.commitVer <= pWal->vers.lastVer);
L
Liu Jicong 已提交
90 91 92 93
  if (ver < pWal->vers.commitVer) {
    return 0;
  }
  if (ver > pWal->vers.lastVer) {
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 104
  int64_t code;
  char    fnameStr[WAL_FILE_LEN];
105
  if (ver > pWal->vers.lastVer || ver < pWal->vers.commitVer || ver <= pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
106
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
107
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
108 109 110
    return -1;
  }

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

L
Liu Jicong 已提交
120
    // delete files
L
Liu Jicong 已提交
121
    int fileSetSize = taosArrayGetSize(pWal->fileInfoSet);
L
Liu Jicong 已提交
122
    for (int i = pWal->writeCur + 1; i < fileSetSize; i++) {
L
Liu Jicong 已提交
123
      walBuildLogName(pWal, ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, i))->firstVer, fnameStr);
124
      taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
125
      walBuildIdxName(pWal, ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, i))->firstVer, fnameStr);
126
      taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
127
    }
L
Liu Jicong 已提交
128
    // pop from fileInfoSet
L
Liu Jicong 已提交
129 130 131 132
    taosArraySetSize(pWal->fileInfoSet, pWal->writeCur + 1);
  }

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

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

  walBuildLogName(pWal, walGetCurFileFirstVer(pWal), fnameStr);
L
Liu Jicong 已提交
157 158
  TdFilePtr pLogFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND);
  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 174
  ASSERT(taosValidFile(pLogFile));
  int64_t size = taosReadFile(pLogFile, &head, sizeof(SWalCkHead));
L
Liu Jicong 已提交
175
  if (size != sizeof(SWalCkHead)) {
L
Liu Jicong 已提交
176
    ASSERT(0);
L
Liu Jicong 已提交
177
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
178 179 180 181 182
    return -1;
  }
  code = walValidHeadCksum(&head);

  ASSERT(code == 0);
L
Liu Jicong 已提交
183
  if (code != 0) {
L
Liu Jicong 已提交
184
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
M
Minghao Li 已提交
185
    ASSERT(0);
L
Liu Jicong 已提交
186
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
187 188
    return -1;
  }
L
Liu Jicong 已提交
189
  if (head.head.version != ver) {
L
Liu Jicong 已提交
190 191
    ASSERT(0);
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
L
Liu Jicong 已提交
192
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
193 194
    return -1;
  }
L
Liu Jicong 已提交
195

L
Liu Jicong 已提交
196
  // truncate old files
L
Liu Jicong 已提交
197
  code = taosFtruncateFile(pLogFile, entry.offset);
L
Liu Jicong 已提交
198
  if (code < 0) {
L
Liu Jicong 已提交
199 200
    ASSERT(0);
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
201
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
202 203
    return -1;
  }
L
Liu Jicong 已提交
204
  code = taosFtruncateFile(pIdxFile, idxOff);
L
Liu Jicong 已提交
205
  if (code < 0) {
L
Liu Jicong 已提交
206 207
    ASSERT(0);
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
208
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
209 210
    return -1;
  }
L
Liu Jicong 已提交
211
  pWal->vers.lastVer = ver - 1;
L
Liu Jicong 已提交
212 213 214 215
  if (pWal->vers.lastVer < pWal->vers.firstVer) {
    ASSERT(pWal->vers.lastVer == pWal->vers.firstVer - 1);
    pWal->vers.firstVer = -1;
  }
L
Liu Jicong 已提交
216 217
  ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->lastVer = ver - 1;
  ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->fileSize = entry.offset;
218 219 220 221
  if (((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->lastVer < ver - 1) {
    ASSERT(((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->fileSize == 0);
    ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->firstVer = -1;
  }
L
Liu Jicong 已提交
222 223 224
  taosCloseFile(&pIdxFile);
  taosCloseFile(&pLogFile);

225 226 227 228 229 230
  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 已提交
231

L
Liu Jicong 已提交
232
  // unlock
wafwerar's avatar
wafwerar 已提交
233
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
234 235
  return 0;
}
L
Liu Jicong 已提交
236

L
Liu Jicong 已提交
237 238 239 240 241
static FORCE_INLINE int32_t walCheckAndRoll(SWal *pWal) {
  if (taosArrayGetSize(pWal->fileInfoSet) == 0) {
    if (walRollImpl(pWal) < 0) {
      return -1;
    }
242 243 244 245 246 247 248 249 250 251 252
    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 已提交
253 254
    }
  }
255

256
  if (walGetLastFileCachedSize(pWal) > tsWalFsyncDataSizeLimit) {
257 258 259 260 261
    if (walSaveMeta(pWal) < 0) {
      return -1;
    }
  }

L
Liu Jicong 已提交
262 263 264
  return 0;
}

L
Liu Jicong 已提交
265
int32_t walBeginSnapshot(SWal *pWal, int64_t ver) {
266 267
  taosThreadMutexLock(&pWal->mutex);

L
Liu Jicong 已提交
268
  pWal->vers.verInSnapshotting = ver;
L
Liu Jicong 已提交
269 270
  wDebug("vgId:%d, wal begin snapshot for version %" PRId64 ", first ver %" PRId64 ", last ver %" PRId64,
         pWal->cfg.vgId, ver, pWal->vers.firstVer, pWal->vers.lastVer);
L
Liu Jicong 已提交
271 272
  // check file rolling
  if (pWal->cfg.retentionPeriod == 0) {
L
Liu Jicong 已提交
273
    if (walGetLastFileSize(pWal) != 0) {
274 275 276 277
      if (walRollImpl(pWal) < 0) {
        wError("vgId:%d, failed to roll wal files since %s", pWal->cfg.vgId, terrstr());
        goto _err;
      }
L
Liu Jicong 已提交
278
    }
L
Liu Jicong 已提交
279
  }
280
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
281
  return 0;
282 283 284 285

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

L
Liu Jicong 已提交
288
int32_t walEndSnapshot(SWal *pWal) {
L
Liu Jicong 已提交
289 290
  int32_t code = 0;
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
291
  int64_t ver = pWal->vers.verInSnapshotting;
L
Liu Jicong 已提交
292 293 294 295

  wDebug("vgId:%d, wal end snapshot for version %" PRId64 ", first ver %" PRId64 ", last ver %" PRId64, pWal->cfg.vgId,
         ver, pWal->vers.firstVer, pWal->vers.lastVer);

L
Liu Jicong 已提交
296 297 298 299
  if (ver == -1) {
    code = -1;
    goto END;
  };
L
Liu Jicong 已提交
300

L
Liu Jicong 已提交
301
  pWal->vers.snapshotVer = ver;
L
Liu Jicong 已提交
302 303
  int ts = taosGetTimestampSec();

304
  void *pIter = NULL;
305 306 307 308 309
  while (1) {
    pIter = taosHashIterate(pWal->pRefHash, pIter);
    if (pIter == NULL) break;
    SWalRef *pRef = *(SWalRef **)pIter;
    if (pRef->refVer == -1) continue;
L
Liu Jicong 已提交
310 311
    ver = TMIN(ver, pRef->refVer - 1);
    wDebug("vgId:%d, wal found ref %" PRId64 ", refId %" PRId64, pWal->cfg.vgId, pRef->refVer, pRef->refId);
312 313
  }

L
Liu Jicong 已提交
314 315 316
  int          deleteCnt = 0;
  int64_t      newTotSize = pWal->totSize;
  SWalFileInfo tmp;
L
Liu Jicong 已提交
317
  tmp.firstVer = ver;
L
Liu Jicong 已提交
318
  // find files safe to delete
L
Liu Jicong 已提交
319
  SWalFileInfo *pInfo = taosArraySearch(pWal->fileInfoSet, &tmp, compareWalFileInfo, TD_LE);
320 321
  if (pInfo) {
    if (ver >= pInfo->lastVer) {
L
Liu Jicong 已提交
322
      pInfo--;
323
    }
L
Liu Jicong 已提交
324 325 326 327
    if (POINTER_DISTANCE(pInfo, pWal->fileInfoSet->pData) > 0) {
      wDebug("vgId:%d, begin remove from %" PRId64, pWal->cfg.vgId, pInfo->firstVer);
    } else {
      wDebug("vgId:%d, no remove", pWal->cfg.vgId);
328 329 330 331 332 333 334 335 336 337
    }
    // 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;
      }
    }
338 339
    int32_t actualDelete = 0;
    char    fnameStr[WAL_FILE_LEN];
340 341 342 343
    // remove file
    for (int i = 0; i < deleteCnt; i++) {
      pInfo = taosArrayGet(pWal->fileInfoSet, i);
      walBuildLogName(pWal, pInfo->firstVer, fnameStr);
L
Liu Jicong 已提交
344
      wDebug("vgId:%d, remove file %s", pWal->cfg.vgId, fnameStr);
345 346 347
      if (taosRemoveFile(fnameStr) < 0) {
        goto UPDATE_META;
      }
348
      walBuildIdxName(pWal, pInfo->firstVer, fnameStr);
L
Liu Jicong 已提交
349
      wDebug("vgId:%d, remove file %s", pWal->cfg.vgId, fnameStr);
350 351 352 353
      if (taosRemoveFile(fnameStr) < 0) {
        ASSERT(0);
      }
      actualDelete++;
L
Liu Jicong 已提交
354 355
    }

356
  UPDATE_META:
357
    // make new array, remove files
358
    taosArrayPopFrontBatch(pWal->fileInfoSet, actualDelete);
359 360 361 362 363 364
    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 已提交
365
  }
L
Liu Jicong 已提交
366
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
Liu Jicong 已提交
367
  pWal->totSize = newTotSize;
L
Liu Jicong 已提交
368
  pWal->vers.verInSnapshotting = -1;
L
Liu Jicong 已提交
369

L
Liu Jicong 已提交
370
  // save snapshot ver, commit ver
L
Liu Jicong 已提交
371
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
372
  if (code < 0) {
L
Liu Jicong 已提交
373
    goto END;
L
Liu Jicong 已提交
374 375
  }

L
Liu Jicong 已提交
376 377 378
END:
  taosThreadMutexUnlock(&pWal->mutex);
  return code;
L
Liu Jicong 已提交
379 380
}

L
Liu Jicong 已提交
381
int32_t walRollImpl(SWal *pWal) {
L
Liu Jicong 已提交
382
  int32_t code = 0;
L
Liu Jicong 已提交
383 384
  if (pWal->pIdxFile != NULL) {
    code = taosCloseFile(&pWal->pIdxFile);
L
Liu Jicong 已提交
385
    if (code != 0) {
L
Liu Jicong 已提交
386
      terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
387
      goto END;
L
Liu Jicong 已提交
388
    }
L
Liu Jicong 已提交
389
  }
L
Liu Jicong 已提交
390 391
  if (pWal->pLogFile != NULL) {
    code = taosCloseFile(&pWal->pLogFile);
L
Liu Jicong 已提交
392
    if (code != 0) {
L
Liu Jicong 已提交
393
      terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
394
      goto END;
L
Liu Jicong 已提交
395
    }
L
Liu Jicong 已提交
396
  }
L
Liu Jicong 已提交
397
  TdFilePtr pIdxFile, pLogFile;
L
Liu Jicong 已提交
398
  // create new file
L
Liu Jicong 已提交
399
  int64_t newFileFirstVer = pWal->vers.lastVer + 1;
L
Liu Jicong 已提交
400
  char    fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
401 402 403
  walBuildIdxName(pWal, newFileFirstVer, fnameStr);
  pIdxFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
  if (pIdxFile == NULL) {
L
Liu Jicong 已提交
404
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
405 406
    code = -1;
    goto END;
L
Liu Jicong 已提交
407
  }
L
Liu Jicong 已提交
408 409 410
  walBuildLogName(pWal, newFileFirstVer, fnameStr);
  pLogFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
  if (pLogFile == NULL) {
L
Liu Jicong 已提交
411
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
412 413
    code = -1;
    goto END;
L
Liu Jicong 已提交
414
  }
L
Liu Jicong 已提交
415
  // error code was set inner
L
Liu Jicong 已提交
416
  code = walRollFileInfo(pWal);
L
Liu Jicong 已提交
417
  if (code != 0) {
L
Liu Jicong 已提交
418
    goto END;
L
Liu Jicong 已提交
419
  }
L
Liu Jicong 已提交
420

L
Liu Jicong 已提交
421
  // switch file
L
Liu Jicong 已提交
422 423
  pWal->pIdxFile = pIdxFile;
  pWal->pLogFile = pLogFile;
L
Liu Jicong 已提交
424
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
fix  
Liu Jicong 已提交
425
  ASSERT(pWal->writeCur >= 0);
L
Liu Jicong 已提交
426 427

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

429 430 431 432 433
  code = walSaveMeta(pWal);
  if (code < 0) {
    wError("vgId:%d, failed to save meta since %s", pWal->cfg.vgId, terrstr());
    goto END;
  }
L
Liu Jicong 已提交
434

L
Liu Jicong 已提交
435 436
END:
  return code;
L
Liu Jicong 已提交
437 438
}

L
Liu Jicong 已提交
439
static int32_t walWriteIndex(SWal *pWal, int64_t ver, int64_t offset) {
L
Liu Jicong 已提交
440
  SWalIdxEntry  entry = {.ver = ver, .offset = offset};
441 442 443 444
  SWalFileInfo *pFileInfo = walGetCurFileInfo(pWal);
  ASSERT(pFileInfo != NULL);
  ASSERT(pFileInfo->firstVer >= 0);
  int64_t idxOffset = (entry.ver - pFileInfo->firstVer) * sizeof(SWalIdxEntry);
S
Shengliang Guan 已提交
445
  wDebug("vgId:%d, write index, index:%" PRId64 ", offset:%" PRId64 ", at %" PRId64, pWal->cfg.vgId, ver, offset,
S
Shengliang Guan 已提交
446
         idxOffset);
447

L
Liu Jicong 已提交
448
  int64_t size = taosWriteFile(pWal->pIdxFile, &entry, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
449
  if (size != sizeof(SWalIdxEntry)) {
450
    wError("vgId:%d, failed to write idx entry due to %s. ver:%" PRId64, pWal->cfg.vgId, strerror(errno), ver);
L
Liu Jicong 已提交
451
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
452
    return -1;
L
Liu Jicong 已提交
453
  }
454

455 456 457 458 459
  // 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 已提交
460
  ASSERT(endOffset == idxOffset + sizeof(SWalIdxEntry) && "Offset of idx entries misaligned");
L
Liu Jicong 已提交
461 462 463
  return 0;
}

L
Liu Jicong 已提交
464 465 466
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) 已提交
467

L
Liu Jicong 已提交
468
  int64_t       offset = walGetCurFileOffset(pWal);
469 470
  SWalFileInfo *pFileInfo = walGetCurFileInfo(pWal);
  ASSERT(pFileInfo != NULL);
L
Liu Jicong 已提交
471

472 473 474
  if (pFileInfo->firstVer == -1) {
    pFileInfo->firstVer = index;
  }
L
Liu Jicong 已提交
475
  pWal->writeHead.head.version = index;
L
Liu Jicong 已提交
476
  pWal->writeHead.head.bodyLen = bodyLen;
L
Liu Jicong 已提交
477
  pWal->writeHead.head.msgType = msgType;
478
  pWal->writeHead.head.ingestTs = 0;
L
Liu Jicong 已提交
479

480
  // sync info for sync module
L
Liu Jicong 已提交
481
  pWal->writeHead.head.syncMeta = syncMeta;
L
Liu Jicong 已提交
482

L
Liu Jicong 已提交
483 484
  pWal->writeHead.cksumHead = walCalcHeadCksum(&pWal->writeHead);
  pWal->writeHead.cksumBody = walCalcBodyCksum(body, bodyLen);
485 486
  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 已提交
487

488 489 490 491 492
  code = walWriteIndex(pWal, index, offset);
  if (code < 0) {
    goto END;
  }

L
Liu Jicong 已提交
493
  if (taosWriteFile(pWal->pLogFile, &pWal->writeHead, sizeof(SWalCkHead)) != sizeof(SWalCkHead)) {
L
fix  
Liu Jicong 已提交
494
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
495 496
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
Liu Jicong 已提交
497 498
    code = -1;
    goto END;
J
Jeff Tao 已提交
499
  }
S
TD-1846  
Shengliang Guan 已提交
500

L
Liu Jicong 已提交
501
  if (taosWriteFile(pWal->pLogFile, (char *)body, bodyLen) != bodyLen) {
L
fix  
Liu Jicong 已提交
502
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
503 504
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
Liu Jicong 已提交
505 506
    code = -1;
    goto END;
L
Liu Jicong 已提交
507
  }
L
Liu Jicong 已提交
508

L
Liu Jicong 已提交
509
  // set status
510
  if (pWal->vers.firstVer == -1) pWal->vers.firstVer = index;
L
Liu Jicong 已提交
511
  pWal->vers.lastVer = index;
L
Liu Jicong 已提交
512
  pWal->totSize += sizeof(SWalCkHead) + bodyLen;
513 514
  pFileInfo->lastVer = index;
  pFileInfo->fileSize += sizeof(SWalCkHead) + bodyLen;
L
Liu Jicong 已提交
515

L
Liu Jicong 已提交
516
  return 0;
517

L
Liu Jicong 已提交
518
END:
519 520
  // recover in a reverse order
  if (taosFtruncateFile(pWal->pLogFile, offset) < 0) {
521 522
    wFatal("vgId:%d, failed to ftruncate logfile to offset:%" PRId64 " during recovery due to %s", pWal->cfg.vgId,
           offset, strerror(errno));
523 524 525 526 527 528
    terrno = TAOS_SYSTEM_ERROR(errno);
    ASSERT(0 && "failed to recover from error");
  }

  int64_t idxOffset = (index - pFileInfo->firstVer) * sizeof(SWalIdxEntry);
  if (taosFtruncateFile(pWal->pIdxFile, idxOffset) < 0) {
529 530
    wFatal("vgId:%d, failed to ftruncate idxfile to offset:%" PRId64 "during recovery due to %s", pWal->cfg.vgId,
           idxOffset, strerror(errno));
531 532 533
    terrno = TAOS_SYSTEM_ERROR(errno);
    ASSERT(0 && "failed to recover from error");
  }
L
Liu Jicong 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546
  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 已提交
547
  if (pWal->pLogFile == NULL || pWal->pIdxFile == NULL || pWal->writeCur < 0) {
L
Liu Jicong 已提交
548 549 550 551 552 553
    if (walInitWriteFile(pWal) < 0) {
      taosThreadMutexUnlock(&pWal->mutex);
      return -1;
    }
  }

L
Liu Jicong 已提交
554
  ASSERT(pWal->pLogFile != NULL && pWal->pIdxFile != NULL && pWal->writeCur >= 0);
L
Liu Jicong 已提交
555 556 557 558 559 560

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

wafwerar's avatar
wafwerar 已提交
561
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
562 563
  return index;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
564

L
Liu Jicong 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
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 已提交
585
  if (pWal->pIdxFile == NULL || pWal->pIdxFile == NULL || pWal->writeCur < 0) {
L
Liu Jicong 已提交
586 587 588 589 590 591
    if (walInitWriteFile(pWal) < 0) {
      taosThreadMutexUnlock(&pWal->mutex);
      return -1;
    }
  }

L
Liu Jicong 已提交
592
  ASSERT(pWal->pIdxFile != NULL && pWal->pLogFile != NULL && pWal->writeCur >= 0);
L
Liu Jicong 已提交
593 594 595 596 597 598 599 600

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

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

L
Liu Jicong 已提交
603
int32_t walWrite(SWal *pWal, int64_t index, tmsg_t msgType, const void *body, int32_t bodyLen) {
L
Liu Jicong 已提交
604
  SWalSyncInfo syncMeta = {
L
Liu Jicong 已提交
605 606 607 608
      .isWeek = -1,
      .seqNum = UINT64_MAX,
      .term = UINT64_MAX,
  };
L
Liu Jicong 已提交
609
  return walWriteWithSyncInfo(pWal, index, msgType, syncMeta, body, bodyLen);
L
Liu Jicong 已提交
610 611
}

L
Liu Jicong 已提交
612
void walFsync(SWal *pWal, bool forceFsync) {
L
Liu Jicong 已提交
613
  if (forceFsync || (pWal->cfg.level == TAOS_WAL_FSYNC && pWal->cfg.fsyncPeriod == 0)) {
614 615 616 617 618
    wTrace("vgId:%d, fileId:%" PRId64 ".idx, do fsync", pWal->cfg.vgId, walGetCurFileFirstVer(pWal));
    if (taosFsyncFile(pWal->pIdxFile) < 0) {
      wError("vgId:%d, file:%" PRId64 ".idx, fsync failed since %s", pWal->cfg.vgId, walGetCurFileFirstVer(pWal),
             strerror(errno));
    }
L
Liu Jicong 已提交
619
    wTrace("vgId:%d, fileId:%" PRId64 ".log, do fsync", pWal->cfg.vgId, walGetCurFileFirstVer(pWal));
L
Liu Jicong 已提交
620
    if (taosFsyncFile(pWal->pLogFile) < 0) {
L
Liu Jicong 已提交
621 622
      wError("vgId:%d, file:%" PRId64 ".log, fsync failed since %s", pWal->cfg.vgId, walGetCurFileFirstVer(pWal),
             strerror(errno));
623 624
    }
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
625
}