walWrite.c 10.6 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
L
Liu Jicong 已提交
17

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

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

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

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

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

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

L
Liu Jicong 已提交
66
  // TODO:change to deserialize function
67
  if (pIdxTFile == NULL) {
L
Liu Jicong 已提交
68 69 70
    pthread_mutex_unlock(&pWal->mutex);
    return -1;
  }
L
Liu Jicong 已提交
71
  int64_t idxOff = walGetVerIdxOffset(pWal, ver);
72
  code = taosLSeekFile(pIdxTFile, idxOff, SEEK_SET);
L
Liu Jicong 已提交
73
  if (code < 0) {
L
Liu Jicong 已提交
74 75 76
    pthread_mutex_unlock(&pWal->mutex);
    return -1;
  }
L
Liu Jicong 已提交
77 78
  // read idx file and get log file pos
  // TODO:change to deserialize function
L
Liu Jicong 已提交
79
  SWalIdxEntry entry;
80
  if (taosReadFile(pIdxTFile, &entry, sizeof(SWalIdxEntry)) != sizeof(SWalIdxEntry)) {
L
Liu Jicong 已提交
81 82 83 84 85 86
    pthread_mutex_unlock(&pWal->mutex);
    return -1;
  }
  ASSERT(entry.ver == ver);

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

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

L
Liu Jicong 已提交
129
  // unlock
L
Liu Jicong 已提交
130 131 132
  pthread_mutex_unlock(&pWal->mutex);
  return 0;
}
L
Liu Jicong 已提交
133

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

  return 0;
}

L
Liu Jicong 已提交
144
int32_t walEndSnapshot(SWal *pWal) {
L
Liu Jicong 已提交
145
  int64_t ver = pWal->vers.verInSnapshotting;
S
Shengliang Guan 已提交
146
  if (ver == -1) return 0;
L
Liu Jicong 已提交
147

L
Liu Jicong 已提交
148
  pWal->vers.snapshotVer = ver;
L
Liu Jicong 已提交
149 150
  int ts = taosGetTimestampSec();

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

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

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

  return 0;
}

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

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

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

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

L
Liu Jicong 已提交
256
int64_t walWrite(SWal *pWal, int64_t index, tmsg_t msgType, const void *body, int32_t bodyLen) {
L
Liu Jicong 已提交
257
  if (pWal == NULL) return -1;
L
Liu Jicong 已提交
258
  int code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
259

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

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

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

286
  pthread_mutex_lock(&pWal->mutex);
L
Liu Jicong 已提交
287

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

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

L
Liu Jicong 已提交
296
  int64_t offset = walGetCurFileOffset(pWal);
L
Liu Jicong 已提交
297 298 299 300
  pWal->writeHead.head.len = bodyLen;
  pWal->writeHead.head.msgType = msgType;
  pWal->writeHead.cksumHead = walCalcHeadCksum(&pWal->writeHead);
  pWal->writeHead.cksumBody = walCalcBodyCksum(body, bodyLen);
301

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

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

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

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

L
Liu Jicong 已提交
330
  pthread_mutex_unlock(&pWal->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
331

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

L
Liu Jicong 已提交
335
void walFsync(SWal *pWal, bool forceFsync) {
L
Liu Jicong 已提交
336
  if (forceFsync || (pWal->cfg.level == TAOS_WAL_FSYNC && pWal->cfg.fsyncPeriod == 0)) {
L
Liu Jicong 已提交
337
    wTrace("vgId:%d, fileId:%" PRId64 ".log, do fsync", pWal->cfg.vgId, walGetCurFileFirstVer(pWal));
338
    if (taosFsyncFile(pWal->pWriteLogTFile) < 0) {
L
Liu Jicong 已提交
339 340
      wError("vgId:%d, file:%" PRId64 ".log, fsync failed since %s", pWal->cfg.vgId, walGetCurFileFirstVer(pWal),
             strerror(errno));
341 342
    }
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
343 344
}

L
Liu Jicong 已提交
345
/*static int walValidateOffset(SWal* pWal, int64_t ver) {*/
L
Liu Jicong 已提交
346 347 348 349 350 351 352
/*int code = 0;*/
/*SWalHead *pHead = NULL;*/
/*code = (int)walRead(pWal, &pHead, ver);*/
/*if(pHead->head.version != ver) {*/
/*return -1;*/
/*}*/
/*return 0;*/
L
Liu Jicong 已提交
353
/*}*/
L
Liu Jicong 已提交
354

L
Liu Jicong 已提交
355
/*static int64_t walGetOffset(SWal* pWal, int64_t ver) {*/
L
Liu Jicong 已提交
356 357 358 359
/*int code = walSeekVer(pWal, ver);*/
/*if(code != 0) {*/
/*return -1;*/
/*}*/
L
Liu Jicong 已提交
360

L
Liu Jicong 已提交
361 362 363 364
/*code = walValidateOffset(pWal, ver);*/
/*if(code != 0) {*/
/*return -1;*/
/*}*/
L
Liu Jicong 已提交
365

L
Liu Jicong 已提交
366
/*return 0;*/
L
Liu Jicong 已提交
367
/*}*/