walWrite.c 10.9 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

L
Liu Jicong 已提交
21
int32_t walCommit(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
22 23
  ASSERT(pWal->vers.commitVer >= pWal->vers.snapshotVer);
  ASSERT(pWal->vers.commitVer <= pWal->vers.lastVer);
L
Liu Jicong 已提交
24
  if (ver < pWal->vers.commitVer || ver > pWal->vers.lastVer) {
L
Liu Jicong 已提交
25
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
26 27
    return -1;
  }
L
Liu Jicong 已提交
28
  pWal->vers.commitVer = ver;
L
Liu Jicong 已提交
29 30 31 32
  return 0;
}

int32_t walRollback(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
33
  int  code;
L
Liu Jicong 已提交
34
  char fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
35
  if (ver > pWal->vers.lastVer || ver < pWal->vers.commitVer) {
L
Liu Jicong 已提交
36
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
37 38
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
39
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
40

L
Liu Jicong 已提交
41 42
  // find correct file
  if (ver < walGetLastFileFirstVer(pWal)) {
L
Liu Jicong 已提交
43 44 45
    // change current files
    code = walChangeWrite(pWal, ver);
    if (code < 0) {
L
Liu Jicong 已提交
46 47 48
      return -1;
    }

L
Liu Jicong 已提交
49
    // delete files
L
Liu Jicong 已提交
50
    int fileSetSize = taosArrayGetSize(pWal->fileInfoSet);
L
Liu Jicong 已提交
51
    for (int i = pWal->writeCur; i < fileSetSize; i++) {
L
Liu Jicong 已提交
52
      walBuildLogName(pWal, ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, i))->firstVer, fnameStr);
53
      taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
54
      walBuildIdxName(pWal, ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, i))->firstVer, fnameStr);
55
      taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
56
    }
L
Liu Jicong 已提交
57
    // pop from fileInfoSet
L
Liu Jicong 已提交
58 59 60 61
    taosArraySetSize(pWal->fileInfoSet, pWal->writeCur + 1);
  }

  walBuildIdxName(pWal, walGetCurFileFirstVer(pWal), fnameStr);
62
  TdFilePtr pIdxTFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ);
L
Liu Jicong 已提交
63

64
  if (pIdxTFile == NULL) {
wafwerar's avatar
wafwerar 已提交
65
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
66 67
    return -1;
  }
L
Liu Jicong 已提交
68
  int64_t idxOff = walGetVerIdxOffset(pWal, ver);
69
  code = taosLSeekFile(pIdxTFile, idxOff, SEEK_SET);
L
Liu Jicong 已提交
70
  if (code < 0) {
wafwerar's avatar
wafwerar 已提交
71
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
72 73
    return -1;
  }
L
Liu Jicong 已提交
74
  // read idx file and get log file pos
L
Liu Jicong 已提交
75
  SWalIdxEntry entry;
76
  if (taosReadFile(pIdxTFile, &entry, sizeof(SWalIdxEntry)) != sizeof(SWalIdxEntry)) {
wafwerar's avatar
wafwerar 已提交
77
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
78 79 80 81 82
    return -1;
  }
  ASSERT(entry.ver == ver);

  walBuildLogName(pWal, walGetCurFileFirstVer(pWal), fnameStr);
83
  TdFilePtr pLogTFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ);
84
  if (pLogTFile == NULL) {
L
Liu Jicong 已提交
85
    // TODO
wafwerar's avatar
wafwerar 已提交
86
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
87 88
    return -1;
  }
89
  code = taosLSeekFile(pLogTFile, entry.offset, SEEK_SET);
L
Liu Jicong 已提交
90 91
  if (code < 0) {
    // TODO
wafwerar's avatar
wafwerar 已提交
92
    taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
93 94
    return -1;
  }
L
Liu Jicong 已提交
95
  // validate offset
L
Liu Jicong 已提交
96
  SWalHead head;
97 98
  ASSERT(taosValidFile(pLogTFile));
  int size = taosReadFile(pLogTFile, &head, sizeof(SWalHead));
L
Liu Jicong 已提交
99
  if (size != sizeof(SWalHead)) {
L
Liu Jicong 已提交
100 101 102 103 104
    return -1;
  }
  code = walValidHeadCksum(&head);

  ASSERT(code == 0);
L
Liu Jicong 已提交
105
  if (code != 0) {
L
Liu Jicong 已提交
106 107
    return -1;
  }
L
Liu Jicong 已提交
108 109
  if (head.head.version != ver) {
    // TODO
L
Liu Jicong 已提交
110 111
    return -1;
  }
L
Liu Jicong 已提交
112
  // truncate old files
113
  code = taosFtruncateFile(pLogTFile, entry.offset);
L
Liu Jicong 已提交
114
  if (code < 0) {
L
Liu Jicong 已提交
115 116
    return -1;
  }
117
  code = taosFtruncateFile(pIdxTFile, idxOff);
L
Liu Jicong 已提交
118
  if (code < 0) {
L
Liu Jicong 已提交
119 120
    return -1;
  }
L
Liu Jicong 已提交
121
  pWal->vers.lastVer = ver - 1;
L
Liu Jicong 已提交
122 123
  ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->lastVer = ver - 1;
  ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->fileSize = entry.offset;
L
Liu Jicong 已提交
124

L
Liu Jicong 已提交
125
  // unlock
wafwerar's avatar
wafwerar 已提交
126
  taosThreadMutexUnlock(&pWal->mutex);
L
Liu Jicong 已提交
127 128
  return 0;
}
L
Liu Jicong 已提交
129

L
Liu Jicong 已提交
130
int32_t walBeginSnapshot(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
131
  pWal->vers.verInSnapshotting = ver;
L
Liu Jicong 已提交
132 133
  // check file rolling
  if (pWal->cfg.retentionPeriod == 0) {
L
Liu Jicong 已提交
134 135 136 137 138 139
    walRoll(pWal);
  }

  return 0;
}

L
Liu Jicong 已提交
140
int32_t walEndSnapshot(SWal *pWal) {
L
Liu Jicong 已提交
141
  int64_t ver = pWal->vers.verInSnapshotting;
S
Shengliang Guan 已提交
142
  if (ver == -1) return 0;
L
Liu Jicong 已提交
143

L
Liu Jicong 已提交
144
  pWal->vers.snapshotVer = ver;
L
Liu Jicong 已提交
145 146
  int ts = taosGetTimestampSec();

L
Liu Jicong 已提交
147 148 149
  int          deleteCnt = 0;
  int64_t      newTotSize = pWal->totSize;
  SWalFileInfo tmp;
L
Liu Jicong 已提交
150
  tmp.firstVer = ver;
L
Liu Jicong 已提交
151
  // find files safe to delete
L
Liu Jicong 已提交
152
  SWalFileInfo *pInfo = taosArraySearch(pWal->fileInfoSet, &tmp, compareWalFileInfo, TD_LE);
L
Liu Jicong 已提交
153
  if (ver >= pInfo->lastVer) {
L
Liu Jicong 已提交
154 155
    pInfo++;
  }
L
Liu Jicong 已提交
156
  // iterate files, until the searched result
L
Liu Jicong 已提交
157
  for (SWalFileInfo *iter = pWal->fileInfoSet->pData; iter < pInfo; iter++) {
L
fix  
Liu Jicong 已提交
158 159
    if ((pWal->cfg.retentionSize != -1 && pWal->totSize > pWal->cfg.retentionSize) ||
        (pWal->cfg.retentionPeriod != -1 && iter->closeTs + pWal->cfg.retentionPeriod > ts)) {
L
Liu Jicong 已提交
160
      // delete according to file size or close time
L
Liu Jicong 已提交
161 162 163 164 165
      deleteCnt++;
      newTotSize -= iter->fileSize;
    }
  }
  char fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
166 167
  // remove file
  for (int i = 0; i < deleteCnt; i++) {
L
Liu Jicong 已提交
168
    pInfo = taosArrayGet(pWal->fileInfoSet, i);
L
Liu Jicong 已提交
169
    walBuildLogName(pWal, pInfo->firstVer, fnameStr);
170
    taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
171
    walBuildIdxName(pWal, pInfo->firstVer, fnameStr);
172
    taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
173 174
  }

L
Liu Jicong 已提交
175 176 177
  // make new array, remove files
  taosArrayPopFrontBatch(pWal->fileInfoSet, deleteCnt);
  if (taosArrayGetSize(pWal->fileInfoSet) == 0) {
L
Liu Jicong 已提交
178
    pWal->writeCur = -1;
L
Liu Jicong 已提交
179
    pWal->vers.firstVer = -1;
L
Liu Jicong 已提交
180
  } else {
L
Liu Jicong 已提交
181
    pWal->vers.firstVer = ((SWalFileInfo *)taosArrayGet(pWal->fileInfoSet, 0))->firstVer;
L
Liu Jicong 已提交
182
  }
L
Liu Jicong 已提交
183
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
Liu Jicong 已提交
184
  pWal->totSize = newTotSize;
L
Liu Jicong 已提交
185
  pWal->vers.verInSnapshotting = -1;
L
Liu Jicong 已提交
186

L
Liu Jicong 已提交
187
  // save snapshot ver, commit ver
L
Liu Jicong 已提交
188
  int code = walSaveMeta(pWal);
L
Liu Jicong 已提交
189
  if (code < 0) {
L
Liu Jicong 已提交
190 191 192 193 194 195
    return -1;
  }

  return 0;
}

L
Liu Jicong 已提交
196
int walRoll(SWal *pWal) {
L
Liu Jicong 已提交
197
  int code = 0;
198 199
  if (pWal->pWriteIdxTFile != NULL) {
    code = taosCloseFile(&pWal->pWriteIdxTFile);
L
Liu Jicong 已提交
200
    if (code != 0) {
L
Liu Jicong 已提交
201 202
      return -1;
    }
L
Liu Jicong 已提交
203
  }
204 205
  if (pWal->pWriteLogTFile != NULL) {
    code = taosCloseFile(&pWal->pWriteLogTFile);
L
Liu Jicong 已提交
206
    if (code != 0) {
L
Liu Jicong 已提交
207 208
      return -1;
    }
L
Liu Jicong 已提交
209
  }
210
  TdFilePtr pIdxTFile, pLogTFile;
L
Liu Jicong 已提交
211
  // create new file
L
Liu Jicong 已提交
212
  int64_t newFileFirstVersion = pWal->vers.lastVer + 1;
L
Liu Jicong 已提交
213
  char    fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
214
  walBuildIdxName(pWal, newFileFirstVersion, fnameStr);
215
  pIdxTFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
216
  if (pIdxTFile == NULL) {
L
Liu Jicong 已提交
217
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
218 219 220
    return -1;
  }
  walBuildLogName(pWal, newFileFirstVersion, fnameStr);
221
  pLogTFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
222
  if (pLogTFile == NULL) {
L
Liu Jicong 已提交
223
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
224 225 226
    return -1;
  }
  code = walRollFileInfo(pWal);
L
Liu Jicong 已提交
227
  if (code != 0) {
L
Liu Jicong 已提交
228 229
    return -1;
  }
L
Liu Jicong 已提交
230

L
Liu Jicong 已提交
231
  // switch file
232 233
  pWal->pWriteIdxTFile = pIdxTFile;
  pWal->pWriteLogTFile = pLogTFile;
L
Liu Jicong 已提交
234
  pWal->writeCur = taosArrayGetSize(pWal->fileInfoSet) - 1;
L
fix  
Liu Jicong 已提交
235
  ASSERT(pWal->writeCur >= 0);
L
Liu Jicong 已提交
236 237 238 239 240

  pWal->lastRollSeq = walGetSeq();
  return 0;
}

L
Liu Jicong 已提交
241
static int walWriteIndex(SWal *pWal, int64_t ver, int64_t offset) {
L
Liu Jicong 已提交
242
  SWalIdxEntry entry = {.ver = ver, .offset = offset};
243
  int          size = taosWriteFile(pWal->pWriteIdxTFile, &entry, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
244
  if (size != sizeof(SWalIdxEntry)) {
L
Liu Jicong 已提交
245
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
246
    // TODO truncate
L
Liu Jicong 已提交
247
    return -1;
L
Liu Jicong 已提交
248 249 250 251
  }
  return 0;
}

L
Liu Jicong 已提交
252
int64_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SSyncLogMeta syncMeta, const void *body,
L
Liu Jicong 已提交
253
                             int32_t bodyLen) {
L
Liu Jicong 已提交
254
  if (pWal == NULL) return -1;
L
Liu Jicong 已提交
255
  int code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
256

S
TD-1652  
Shengliang Guan 已提交
257
  // no wal
L
Liu Jicong 已提交
258
  if (pWal->cfg.level == TAOS_WAL_NOLOG) return 0;
L
Liu Jicong 已提交
259

L
Liu Jicong 已提交
260
  if (index == pWal->vers.lastVer + 1) {
L
Liu Jicong 已提交
261
    if (taosArrayGetSize(pWal->fileInfoSet) == 0) {
L
Liu Jicong 已提交
262
      pWal->vers.firstVer = index;
L
Liu Jicong 已提交
263 264
      code = walRoll(pWal);
      ASSERT(code == 0);
L
Liu Jicong 已提交
265
    } else {
L
Liu Jicong 已提交
266
      int64_t passed = walGetSeq() - pWal->lastRollSeq;
L
Liu Jicong 已提交
267
      if (pWal->cfg.rollPeriod != -1 && pWal->cfg.rollPeriod != 0 && passed > pWal->cfg.rollPeriod) {
L
Liu Jicong 已提交
268
        walRoll(pWal);
L
Liu Jicong 已提交
269
      } else if (pWal->cfg.segSize != -1 && pWal->cfg.segSize != 0 && walGetLastFileSize(pWal) > pWal->cfg.segSize) {
L
Liu Jicong 已提交
270 271
        walRoll(pWal);
      }
L
Liu Jicong 已提交
272 273
    }
  } else {
L
Liu Jicong 已提交
274 275
    // reject skip log or rewrite log
    // must truncate explicitly first
L
fix  
Liu Jicong 已提交
276
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
277 278
    return -1;
  }
279
  /*if (!tfValid(pWal->pWriteLogTFile)) return -1;*/
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
280

L
Liu Jicong 已提交
281 282
  ASSERT(pWal->writeCur >= 0);

wafwerar's avatar
wafwerar 已提交
283
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
284

285
  if (pWal->pWriteIdxTFile == NULL || pWal->pWriteLogTFile == NULL) {
L
Liu Jicong 已提交
286
    walSetWrite(pWal);
287 288
    taosLSeekFile(pWal->pWriteLogTFile, 0, SEEK_END);
    taosLSeekFile(pWal->pWriteIdxTFile, 0, SEEK_END);
L
Liu Jicong 已提交
289 290
  }

L
Liu Jicong 已提交
291
  pWal->writeHead.head.version = index;
L
Liu Jicong 已提交
292

L
Liu Jicong 已提交
293
  int64_t offset = walGetCurFileOffset(pWal);
L
Liu Jicong 已提交
294
  pWal->writeHead.head.bodyLen = bodyLen;
L
Liu Jicong 已提交
295
  pWal->writeHead.head.msgType = msgType;
L
Liu Jicong 已提交
296

297
  // sync info for sync module
L
Liu Jicong 已提交
298
  pWal->writeHead.head.syncMeta = syncMeta;
L
Liu Jicong 已提交
299

L
Liu Jicong 已提交
300 301
  pWal->writeHead.cksumHead = walCalcHeadCksum(&pWal->writeHead);
  pWal->writeHead.cksumBody = walCalcBodyCksum(body, bodyLen);
302

303
  if (taosWriteFile(pWal->pWriteLogTFile, &pWal->writeHead, sizeof(SWalHead)) != sizeof(SWalHead)) {
304
    // TODO ftruncate
L
fix  
Liu Jicong 已提交
305
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
306 307
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
fix  
Liu Jicong 已提交
308
    return -1;
J
Jeff Tao 已提交
309
  }
S
TD-1846  
Shengliang Guan 已提交
310

311
  if (taosWriteFile(pWal->pWriteLogTFile, (char *)body, bodyLen) != bodyLen) {
312
    // TODO ftruncate
L
fix  
Liu Jicong 已提交
313
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
314 315
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
L
fix  
Liu Jicong 已提交
316
    return -1;
L
Liu Jicong 已提交
317
  }
L
Liu Jicong 已提交
318

L
Liu Jicong 已提交
319
  code = walWriteIndex(pWal, index, offset);
L
Liu Jicong 已提交
320 321
  if (code != 0) {
    // TODO
L
Liu Jicong 已提交
322
    return -1;
L
Liu Jicong 已提交
323
  }
324

L
Liu Jicong 已提交
325
  // set status
L
Liu Jicong 已提交
326
  pWal->vers.lastVer = index;
L
Liu Jicong 已提交
327
  pWal->totSize += sizeof(SWalHead) + bodyLen;
L
Liu Jicong 已提交
328 329
  walGetCurFileInfo(pWal)->lastVer = index;
  walGetCurFileInfo(pWal)->fileSize += sizeof(SWalHead) + bodyLen;
L
Liu Jicong 已提交
330

wafwerar's avatar
wafwerar 已提交
331
  taosThreadMutexUnlock(&pWal->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
332

L
fix  
Liu Jicong 已提交
333
  return 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
334 335
}

L
Liu Jicong 已提交
336
int64_t walWrite(SWal *pWal, int64_t index, tmsg_t msgType, const void *body, int32_t bodyLen) {
L
Liu Jicong 已提交
337
  SSyncLogMeta syncMeta = {
L
Liu Jicong 已提交
338 339 340 341
      .isWeek = -1,
      .seqNum = UINT64_MAX,
      .term = UINT64_MAX,
  };
L
Liu Jicong 已提交
342
  return walWriteWithSyncInfo(pWal, index, msgType, syncMeta, body, bodyLen);
L
Liu Jicong 已提交
343 344
}

L
Liu Jicong 已提交
345
void walFsync(SWal *pWal, bool forceFsync) {
L
Liu Jicong 已提交
346
  if (forceFsync || (pWal->cfg.level == TAOS_WAL_FSYNC && pWal->cfg.fsyncPeriod == 0)) {
L
Liu Jicong 已提交
347
    wTrace("vgId:%d, fileId:%" PRId64 ".log, do fsync", pWal->cfg.vgId, walGetCurFileFirstVer(pWal));
348
    if (taosFsyncFile(pWal->pWriteLogTFile) < 0) {
L
Liu Jicong 已提交
349 350
      wError("vgId:%d, file:%" PRId64 ".log, fsync failed since %s", pWal->cfg.vgId, walGetCurFileFirstVer(pWal),
             strerror(errno));
351 352
    }
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
353 354
}

L
Liu Jicong 已提交
355
/*static int walValidateOffset(SWal* pWal, int64_t ver) {*/
L
Liu Jicong 已提交
356 357 358 359 360 361 362
/*int code = 0;*/
/*SWalHead *pHead = NULL;*/
/*code = (int)walRead(pWal, &pHead, ver);*/
/*if(pHead->head.version != ver) {*/
/*return -1;*/
/*}*/
/*return 0;*/
L
Liu Jicong 已提交
363
/*}*/
L
Liu Jicong 已提交
364

L
Liu Jicong 已提交
365
/*static int64_t walGetOffset(SWal* pWal, int64_t ver) {*/
L
Liu Jicong 已提交
366 367 368 369
/*int code = walSeekVer(pWal, ver);*/
/*if(code != 0) {*/
/*return -1;*/
/*}*/
L
Liu Jicong 已提交
370

L
Liu Jicong 已提交
371 372 373 374
/*code = walValidateOffset(pWal, ver);*/
/*if(code != 0) {*/
/*return -1;*/
/*}*/
L
Liu Jicong 已提交
375

L
Liu Jicong 已提交
376
/*return 0;*/
L
Liu Jicong 已提交
377
/*}*/