walWrite.c 10.5 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
TD-1895  
Shengliang Guan 已提交
21
#include "tfile.h"
S
Shengliang Guan 已提交
22
#include "walInt.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
23

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

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

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

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

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

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

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

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

L
Liu Jicong 已提交
133
  // unlock
L
Liu Jicong 已提交
134 135 136
  pthread_mutex_unlock(&pWal->mutex);
  return 0;
}
L
Liu Jicong 已提交
137

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

  return 0;
}

L
Liu Jicong 已提交
148
int32_t walEndSnapshot(SWal *pWal) {
L
Liu Jicong 已提交
149
  int64_t ver = pWal->vers.verInSnapshotting;
S
Shengliang Guan 已提交
150
  if (ver == -1) return 0;
L
Liu Jicong 已提交
151

L
Liu Jicong 已提交
152
  pWal->vers.snapshotVer = ver;
L
Liu Jicong 已提交
153 154
  int ts = taosGetTimestampSec();

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

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

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

  return 0;
}

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

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

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

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

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

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

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

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

289
  pthread_mutex_lock(&pWal->mutex);
L
Liu Jicong 已提交
290

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

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

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

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

312
  if (taosWriteFile(pWal->pWriteLogTFile, (char *)body, bodyLen) != bodyLen) {
L
Liu Jicong 已提交
313
    // ftruncate
L
Liu Jicong 已提交
314
    code = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
315 316
    wError("vgId:%d, file:%" PRId64 ".log, failed to write since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
           strerror(errno));
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

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

S
TD-1846  
Shengliang Guan 已提交
333
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
334 335
}

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

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

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

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

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