walMgmt.c 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
#include "taoserror.h"
19
#include "tcompare.h"
L
Liu Jicong 已提交
20
#include "tref.h"
21
#include "walInt.h"
S
Shengliang Guan 已提交
22 23

typedef struct {
24 25 26 27
  int8_t   stop;
  int8_t   inited;
  uint32_t seq;
  int32_t  refSetId;
wafwerar's avatar
wafwerar 已提交
28
  TdThread thread;
S
Shengliang Guan 已提交
29 30
} SWalMgmt;

L
Liu Jicong 已提交
31
static SWalMgmt tsWal = {0, .seq = 1};
S
Shengliang Guan 已提交
32 33 34 35
static int32_t  walCreateThread();
static void     walStopThread();
static void     walFreeObj(void *pWal);

L
Liu Jicong 已提交
36
int64_t walGetSeq() { return (int64_t)atomic_load_32(&tsWal.seq); }
L
Liu Jicong 已提交
37

S
Shengliang Guan 已提交
38
int32_t walInit() {
L
Liu Jicong 已提交
39 40 41 42 43
  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&tsWal.inited, 0, 2);
    if (old != 2) break;
  }
L
Liu Jicong 已提交
44

L
Liu Jicong 已提交
45 46
  if (old == 0) {
    tsWal.refSetId = taosOpenRef(TSDB_MIN_VNODES, walFreeObj);
S
Shengliang Guan 已提交
47

L
Liu Jicong 已提交
48 49 50 51 52 53 54 55 56
    int32_t code = walCreateThread();
    if (code != 0) {
      wError("failed to init wal module since %s", tstrerror(code));
      atomic_store_8(&tsWal.inited, 0);
      return code;
    }

    wInfo("wal module is initialized, rsetId:%d", tsWal.refSetId);
    atomic_store_8(&tsWal.inited, 1);
S
Shengliang Guan 已提交
57 58
  }

L
Liu Jicong 已提交
59
  return 0;
S
Shengliang Guan 已提交
60 61 62
}

void walCleanUp() {
L
Liu Jicong 已提交
63 64 65 66 67 68 69 70 71 72 73
  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&tsWal.inited, 1, 2);
    if (old != 2) break;
  }

  if (old == 1) {
    walStopThread();
    taosCloseRef(tsWal.refSetId);
    wInfo("wal module is cleaned up");
    atomic_store_8(&tsWal.inited, 0);
74
  }
S
Shengliang Guan 已提交
75 76
}

L
Liu Jicong 已提交
77
SWal *walOpen(const char *path, SWalCfg *pCfg) {
C
Cary Xu 已提交
78
  SWal *pWal = taosMemoryCalloc(1, sizeof(SWal));
S
Shengliang Guan 已提交
79 80 81 82
  if (pWal == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return NULL;
  }
L
Liu Jicong 已提交
83

L
Liu Jicong 已提交
84
  // set config
L
Liu Jicong 已提交
85
  memcpy(&pWal->cfg, pCfg, sizeof(SWalCfg));
L
Liu Jicong 已提交
86
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
L
Liu Jicong 已提交
87
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
L
Liu Jicong 已提交
88

L
Liu Jicong 已提交
89
  tstrncpy(pWal->path, path, sizeof(pWal->path));
L
Liu Jicong 已提交
90
  if (taosMkDir(pWal->path) != 0) {
L
Liu Jicong 已提交
91 92 93
    wError("vgId:%d, path:%s, failed to create directory since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
    return NULL;
  }
L
Liu Jicong 已提交
94

C
Cary Xu 已提交
95 96 97 98 99 100 101
  // init ref
  pWal->pRefHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_ENTRY_LOCK);
  if (pWal->pRefHash == NULL) {
    taosMemoryFree(pWal);
    return NULL;
  }

L
Liu Jicong 已提交
102
  // open meta
L
Liu Jicong 已提交
103
  walResetVer(&pWal->vers);
104 105
  pWal->pWriteLogTFile = NULL;
  pWal->pWriteIdxTFile = NULL;
L
Liu Jicong 已提交
106
  pWal->writeCur = -1;
L
Liu Jicong 已提交
107
  pWal->fileInfoSet = taosArrayInit(8, sizeof(SWalFileInfo));
L
Liu Jicong 已提交
108
  if (pWal->fileInfoSet == NULL) {
L
Liu Jicong 已提交
109
    wError("vgId:%d, path:%s, failed to init taosArray %s", pWal->cfg.vgId, pWal->path, strerror(errno));
C
Cary Xu 已提交
110
    taosHashCleanup(pWal->pRefHash);
wafwerar's avatar
wafwerar 已提交
111
    taosMemoryFree(pWal);
L
Liu Jicong 已提交
112 113
    return NULL;
  }
L
Liu Jicong 已提交
114

L
Liu Jicong 已提交
115
  // init status
L
Liu Jicong 已提交
116
  pWal->totSize = 0;
L
Liu Jicong 已提交
117
  pWal->lastRollSeq = -1;
L
Liu Jicong 已提交
118

L
Liu Jicong 已提交
119
  // init write buffer
L
Liu Jicong 已提交
120
  memset(&pWal->writeHead, 0, sizeof(SWalHead));
L
Liu Jicong 已提交
121
  pWal->writeHead.head.headVer = WAL_HEAD_VER;
L
Liu Jicong 已提交
122
  pWal->writeHead.magic = WAL_MAGIC;
S
Shengliang Guan 已提交
123

wafwerar's avatar
wafwerar 已提交
124
  if (taosThreadMutexInit(&pWal->mutex, NULL) < 0) {
L
Liu Jicong 已提交
125
    taosArrayDestroy(pWal->fileInfoSet);
C
Cary Xu 已提交
126
    taosHashCleanup(pWal->pRefHash);
wafwerar's avatar
wafwerar 已提交
127
    taosMemoryFree(pWal);
L
Liu Jicong 已提交
128 129
    return NULL;
  }
S
Shengliang Guan 已提交
130

L
Liu Jicong 已提交
131
  pWal->refId = taosAddRef(tsWal.refSetId, pWal);
L
Liu Jicong 已提交
132
  if (pWal->refId < 0) {
C
Cary Xu 已提交
133
    taosHashCleanup(pWal->pRefHash);
wafwerar's avatar
wafwerar 已提交
134
    taosThreadMutexDestroy(&pWal->mutex);
L
Liu Jicong 已提交
135
    taosArrayDestroy(pWal->fileInfoSet);
wafwerar's avatar
wafwerar 已提交
136
    taosMemoryFree(pWal);
S
Shengliang Guan 已提交
137 138 139
    return NULL;
  }

L
Liu Jicong 已提交
140 141 142
  walLoadMeta(pWal);

  if (walCheckAndRepairMeta(pWal) < 0) {
C
Cary Xu 已提交
143
    taosHashCleanup(pWal->pRefHash);
L
Liu Jicong 已提交
144
    taosRemoveRef(tsWal.refSetId, pWal->refId);
wafwerar's avatar
wafwerar 已提交
145
    taosThreadMutexDestroy(&pWal->mutex);
L
Liu Jicong 已提交
146
    taosArrayDestroy(pWal->fileInfoSet);
wafwerar's avatar
wafwerar 已提交
147
    taosMemoryFree(pWal);
S
Shengliang Guan 已提交
148 149 150
    return NULL;
  }

L
Liu Jicong 已提交
151
  if (walCheckAndRepairIdx(pWal) < 0) {
L
Liu Jicong 已提交
152 153
  }

L
Liu Jicong 已提交
154 155
  wDebug("vgId:%d, wal:%p is opened, level:%d fsyncPeriod:%d", pWal->cfg.vgId, pWal, pWal->cfg.level,
         pWal->cfg.fsyncPeriod);
S
Shengliang Guan 已提交
156 157 158 159

  return pWal;
}

L
Liu Jicong 已提交
160 161
int32_t walAlter(SWal *pWal, SWalCfg *pCfg) {
  if (pWal == NULL) return TSDB_CODE_WAL_APP_ERROR;
S
Shengliang Guan 已提交
162

L
Liu Jicong 已提交
163 164 165
  if (pWal->cfg.level == pCfg->level && pWal->cfg.fsyncPeriod == pCfg->fsyncPeriod) {
    wDebug("vgId:%d, old walLevel:%d fsync:%d, new walLevel:%d fsync:%d not change", pWal->cfg.vgId, pWal->cfg.level,
           pWal->cfg.fsyncPeriod, pCfg->level, pCfg->fsyncPeriod);
L
Liu Jicong 已提交
166
    return 0;
S
Shengliang Guan 已提交
167 168
  }

L
Liu Jicong 已提交
169 170
  wInfo("vgId:%d, change old walLevel:%d fsync:%d, new walLevel:%d fsync:%d", pWal->cfg.vgId, pWal->cfg.level,
        pWal->cfg.fsyncPeriod, pCfg->level, pCfg->fsyncPeriod);
S
Shengliang Guan 已提交
171

L
Liu Jicong 已提交
172 173
  pWal->cfg.level = pCfg->level;
  pWal->cfg.fsyncPeriod = pCfg->fsyncPeriod;
174
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
S
TD-1846  
Shengliang Guan 已提交
175
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
S
Shengliang Guan 已提交
176

L
Liu Jicong 已提交
177
  return 0;
S
TD-1894  
Shengliang Guan 已提交
178 179
}

L
Liu Jicong 已提交
180
void walClose(SWal *pWal) {
wafwerar's avatar
wafwerar 已提交
181
  taosThreadMutexLock(&pWal->mutex);
182 183 184 185
  taosCloseFile(&pWal->pWriteLogTFile);
  pWal->pWriteLogTFile = NULL;
  taosCloseFile(&pWal->pWriteIdxTFile);
  pWal->pWriteIdxTFile = NULL;
L
Liu Jicong 已提交
186
  walSaveMeta(pWal);
L
Liu Jicong 已提交
187 188
  taosArrayDestroy(pWal->fileInfoSet);
  pWal->fileInfoSet = NULL;
C
Cary Xu 已提交
189
  taosHashCleanup(pWal->pRefHash);
wafwerar's avatar
wafwerar 已提交
190
  taosThreadMutexUnlock(&pWal->mutex);
S
Shengliang Guan 已提交
191

L
Liu Jicong 已提交
192
  taosRemoveRef(tsWal.refSetId, pWal->refId);
S
Shengliang Guan 已提交
193 194 195
}

static void walFreeObj(void *wal) {
S
TD-1846  
Shengliang Guan 已提交
196
  SWal *pWal = wal;
L
Liu Jicong 已提交
197
  wDebug("vgId:%d, wal:%p is freed", pWal->cfg.vgId, pWal);
S
Shengliang Guan 已提交
198

wafwerar's avatar
wafwerar 已提交
199
  taosThreadMutexDestroy(&pWal->mutex);
wafwerar's avatar
wafwerar 已提交
200
  taosMemoryFreeClear(pWal);
S
Shengliang Guan 已提交
201 202
}

S
TD-1846  
Shengliang Guan 已提交
203
static bool walNeedFsync(SWal *pWal) {
L
Liu Jicong 已提交
204
  if (pWal->cfg.fsyncPeriod <= 0 || pWal->cfg.level != TAOS_WAL_FSYNC) {
S
TD-1846  
Shengliang Guan 已提交
205 206
    return false;
  }
S
Shengliang Guan 已提交
207

L
Liu Jicong 已提交
208
  if (atomic_load_32(&tsWal.seq) % pWal->fsyncSeq == 0) {
S
TD-1846  
Shengliang Guan 已提交
209 210
    return true;
  }
S
Shengliang Guan 已提交
211

S
TD-1846  
Shengliang Guan 已提交
212 213
  return false;
}
S
Shengliang Guan 已提交
214 215

static void walUpdateSeq() {
S
TD-1846  
Shengliang Guan 已提交
216
  taosMsleep(WAL_REFRESH_MS);
L
Liu Jicong 已提交
217
  atomic_add_fetch_32(&tsWal.seq, 1);
S
Shengliang Guan 已提交
218 219 220
}

static void walFsyncAll() {
L
Liu Jicong 已提交
221
  SWal *pWal = taosIterateRef(tsWal.refSetId, 0);
S
TD-1846  
Shengliang Guan 已提交
222 223
  while (pWal) {
    if (walNeedFsync(pWal)) {
L
Liu Jicong 已提交
224 225
      wTrace("vgId:%d, do fsync, level:%d seq:%d rseq:%d", pWal->cfg.vgId, pWal->cfg.level, pWal->fsyncSeq,
             atomic_load_32(&tsWal.seq));
226
      int32_t code = taosFsyncFile(pWal->pWriteLogTFile);
S
TD-1846  
Shengliang Guan 已提交
227
      if (code != 0) {
L
Liu Jicong 已提交
228 229
        wError("vgId:%d, file:%" PRId64 ".log, failed to fsync since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
               strerror(code));
S
TD-1846  
Shengliang Guan 已提交
230 231
      }
    }
L
Liu Jicong 已提交
232
    pWal = taosIterateRef(tsWal.refSetId, pWal->refId);
S
TD-1846  
Shengliang Guan 已提交
233
  }
S
Shengliang Guan 已提交
234 235 236
}

static void *walThreadFunc(void *param) {
H
Haojun Liao 已提交
237
  setThreadName("wal");
S
Shengliang Guan 已提交
238 239 240
  while (1) {
    walUpdateSeq();
    walFsyncAll();
J
Jun Li 已提交
241

L
Liu Jicong 已提交
242
    if (atomic_load_8(&tsWal.stop)) break;
S
Shengliang Guan 已提交
243 244 245 246 247 248
  }

  return NULL;
}

static int32_t walCreateThread() {
wafwerar's avatar
wafwerar 已提交
249 250 251
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
S
Shengliang Guan 已提交
252

wafwerar's avatar
wafwerar 已提交
253
  if (taosThreadCreate(&tsWal.thread, &thAttr, walThreadFunc, NULL) != 0) {
S
TD-1846  
Shengliang Guan 已提交
254
    wError("failed to create wal thread since %s", strerror(errno));
L
Liu Jicong 已提交
255 256
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
S
Shengliang Guan 已提交
257 258
  }

wafwerar's avatar
wafwerar 已提交
259
  taosThreadAttrDestroy(&thAttr);
S
TD-1207  
Shengliang Guan 已提交
260
  wDebug("wal thread is launched, thread:0x%08" PRIx64, taosGetPthreadId(tsWal.thread));
S
Shengliang Guan 已提交
261

L
Liu Jicong 已提交
262
  return 0;
S
Shengliang Guan 已提交
263 264 265
}

static void walStopThread() {
L
Liu Jicong 已提交
266
  atomic_store_8(&tsWal.stop, 1);
J
Jun Li 已提交
267

L
Liu Jicong 已提交
268
  if (taosCheckPthreadValid(tsWal.thread)) {
wafwerar's avatar
wafwerar 已提交
269
    taosThreadJoin(tsWal.thread, NULL);
270
    taosThreadClear(&tsWal.thread);
S
Shengliang Guan 已提交
271 272 273 274
  }

  wDebug("wal thread is stopped");
}