walMgmt.c 7.4 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) {
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

84 85 86 87 88 89
  if (taosThreadMutexInit(&pWal->mutex, NULL) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    taosMemoryFree(pWal);
    return NULL;
  }

L
Liu Jicong 已提交
90
  // set config
L
Liu Jicong 已提交
91
  memcpy(&pWal->cfg, pCfg, sizeof(SWalCfg));
L
Liu Jicong 已提交
92

L
Liu Jicong 已提交
93
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
L
Liu Jicong 已提交
94 95 96 97 98 99 100 101
  if (pWal->cfg.retentionSize > 0) {
    pWal->cfg.retentionSize *= 1024;
  }

  if (pWal->cfg.segSize > 0) {
    pWal->cfg.segSize *= 1024;
  }

L
Liu Jicong 已提交
102
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
L
Liu Jicong 已提交
103

L
Liu Jicong 已提交
104
  tstrncpy(pWal->path, path, sizeof(pWal->path));
L
Liu Jicong 已提交
105
  if (taosMkDir(pWal->path) != 0) {
L
Liu Jicong 已提交
106
    wError("vgId:%d, path:%s, failed to create directory since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
107
    goto _err;
L
Liu Jicong 已提交
108
  }
L
Liu Jicong 已提交
109

110
  // init ref
111
  pWal->pRefHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
112
  if (pWal->pRefHash == NULL) {
113 114
      wError("failed to init hash since %s", tstrerror(terrno));
      goto _err;
115 116
  }

L
Liu Jicong 已提交
117
  // open meta
L
Liu Jicong 已提交
118
  walResetVer(&pWal->vers);
L
Liu Jicong 已提交
119 120
  pWal->pLogFile = NULL;
  pWal->pIdxFile = NULL;
L
Liu Jicong 已提交
121
  pWal->writeCur = -1;
L
Liu Jicong 已提交
122
  pWal->fileInfoSet = taosArrayInit(8, sizeof(SWalFileInfo));
L
Liu Jicong 已提交
123
  if (pWal->fileInfoSet == NULL) {
L
Liu Jicong 已提交
124
    wError("vgId:%d, path:%s, failed to init taosArray %s", pWal->cfg.vgId, pWal->path, strerror(errno));
125
    goto _err;
L
Liu Jicong 已提交
126
  }
L
Liu Jicong 已提交
127

L
Liu Jicong 已提交
128
  // init status
L
Liu Jicong 已提交
129
  pWal->totSize = 0;
L
Liu Jicong 已提交
130
  pWal->lastRollSeq = -1;
L
Liu Jicong 已提交
131

L
Liu Jicong 已提交
132
  // init write buffer
L
Liu Jicong 已提交
133 134
  memset(&pWal->writeHead, 0, sizeof(SWalCkHead));
  pWal->writeHead.head.protoVer = WAL_PROTO_VER;
L
Liu Jicong 已提交
135
  pWal->writeHead.magic = WAL_MAGIC;
S
Shengliang Guan 已提交
136

137
  (void)walLoadMeta(pWal);
L
Liu Jicong 已提交
138 139

  if (walCheckAndRepairMeta(pWal) < 0) {
L
Liu Jicong 已提交
140
    wError("vgId:%d cannot open wal since repair meta file failed", pWal->cfg.vgId);
141
    goto _err;
S
Shengliang Guan 已提交
142 143
  }

L
Liu Jicong 已提交
144
  if (walCheckAndRepairIdx(pWal) < 0) {
L
Liu Jicong 已提交
145
    wError("vgId:%d cannot open wal since repair idx file failed", pWal->cfg.vgId);
146 147 148 149 150 151 152 153
    goto _err;
  }

  // add ref
  pWal->refId = taosAddRef(tsWal.refSetId, pWal);
  if (pWal->refId < 0) {
      wError("failed to add ref for Wal since %s", tstrerror(terrno));
      goto _err;
L
Liu Jicong 已提交
154 155
  }

L
Liu Jicong 已提交
156 157
  wDebug("vgId:%d, wal:%p is opened, level:%d fsyncPeriod:%d", pWal->cfg.vgId, pWal, pWal->cfg.level,
         pWal->cfg.fsyncPeriod);
S
Shengliang Guan 已提交
158
  return pWal;
159 160 161 162 163 164 165 166

_err:
  taosArrayDestroy(pWal->fileInfoSet);
  taosHashCleanup(pWal->pRefHash);
  taosThreadMutexDestroy(&pWal->mutex);
  taosMemoryFree(pWal);
  pWal = NULL;
  return NULL;
S
Shengliang Guan 已提交
167 168
}

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

L
Liu Jicong 已提交
172 173 174
  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 已提交
175
    return 0;
S
Shengliang Guan 已提交
176 177
  }

L
Liu Jicong 已提交
178 179
  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 已提交
180

L
Liu Jicong 已提交
181 182
  pWal->cfg.level = pCfg->level;
  pWal->cfg.fsyncPeriod = pCfg->fsyncPeriod;
183
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
S
TD-1846  
Shengliang Guan 已提交
184
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
S
Shengliang Guan 已提交
185

L
Liu Jicong 已提交
186
  return 0;
S
TD-1894  
Shengliang Guan 已提交
187 188
}

L
Liu Jicong 已提交
189
void walClose(SWal *pWal) {
wafwerar's avatar
wafwerar 已提交
190
  taosThreadMutexLock(&pWal->mutex);
L
Liu Jicong 已提交
191 192 193 194
  taosCloseFile(&pWal->pLogFile);
  pWal->pLogFile = NULL;
  taosCloseFile(&pWal->pIdxFile);
  pWal->pIdxFile = NULL;
L
Liu Jicong 已提交
195
  walSaveMeta(pWal);
L
Liu Jicong 已提交
196 197
  taosArrayDestroy(pWal->fileInfoSet);
  pWal->fileInfoSet = NULL;
198
  taosHashCleanup(pWal->pRefHash);
wafwerar's avatar
wafwerar 已提交
199
  taosThreadMutexUnlock(&pWal->mutex);
S
Shengliang Guan 已提交
200

L
Liu Jicong 已提交
201
  taosRemoveRef(tsWal.refSetId, pWal->refId);
S
Shengliang Guan 已提交
202 203 204
}

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

wafwerar's avatar
wafwerar 已提交
208
  taosThreadMutexDestroy(&pWal->mutex);
wafwerar's avatar
wafwerar 已提交
209
  taosMemoryFreeClear(pWal);
S
Shengliang Guan 已提交
210 211
}

S
TD-1846  
Shengliang Guan 已提交
212
static bool walNeedFsync(SWal *pWal) {
L
Liu Jicong 已提交
213
  if (pWal->cfg.fsyncPeriod <= 0 || pWal->cfg.level != TAOS_WAL_FSYNC) {
S
TD-1846  
Shengliang Guan 已提交
214 215
    return false;
  }
S
Shengliang Guan 已提交
216

L
Liu Jicong 已提交
217
  if (atomic_load_32(&tsWal.seq) % pWal->fsyncSeq == 0) {
S
TD-1846  
Shengliang Guan 已提交
218 219
    return true;
  }
S
Shengliang Guan 已提交
220

S
TD-1846  
Shengliang Guan 已提交
221 222
  return false;
}
S
Shengliang Guan 已提交
223 224

static void walUpdateSeq() {
S
TD-1846  
Shengliang Guan 已提交
225
  taosMsleep(WAL_REFRESH_MS);
L
Liu Jicong 已提交
226
  atomic_add_fetch_32(&tsWal.seq, 1);
S
Shengliang Guan 已提交
227 228 229
}

static void walFsyncAll() {
L
Liu Jicong 已提交
230
  SWal *pWal = taosIterateRef(tsWal.refSetId, 0);
S
TD-1846  
Shengliang Guan 已提交
231 232
  while (pWal) {
    if (walNeedFsync(pWal)) {
L
Liu Jicong 已提交
233 234
      wTrace("vgId:%d, do fsync, level:%d seq:%d rseq:%d", pWal->cfg.vgId, pWal->cfg.level, pWal->fsyncSeq,
             atomic_load_32(&tsWal.seq));
L
Liu Jicong 已提交
235
      int32_t code = taosFsyncFile(pWal->pLogFile);
S
TD-1846  
Shengliang Guan 已提交
236
      if (code != 0) {
L
Liu Jicong 已提交
237 238
        wError("vgId:%d, file:%" PRId64 ".log, failed to fsync since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
               strerror(code));
S
TD-1846  
Shengliang Guan 已提交
239 240
      }
    }
L
Liu Jicong 已提交
241
    pWal = taosIterateRef(tsWal.refSetId, pWal->refId);
S
TD-1846  
Shengliang Guan 已提交
242
  }
S
Shengliang Guan 已提交
243 244 245
}

static void *walThreadFunc(void *param) {
H
Haojun Liao 已提交
246
  setThreadName("wal");
S
Shengliang Guan 已提交
247 248 249
  while (1) {
    walUpdateSeq();
    walFsyncAll();
J
Jun Li 已提交
250

L
Liu Jicong 已提交
251
    if (atomic_load_8(&tsWal.stop)) break;
S
Shengliang Guan 已提交
252 253 254 255 256 257
  }

  return NULL;
}

static int32_t walCreateThread() {
wafwerar's avatar
wafwerar 已提交
258 259 260
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
S
Shengliang Guan 已提交
261

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

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

L
Liu Jicong 已提交
271
  return 0;
S
Shengliang Guan 已提交
272 273 274
}

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

L
Liu Jicong 已提交
277
  if (taosCheckPthreadValid(tsWal.thread)) {
wafwerar's avatar
wafwerar 已提交
278
    taosThreadJoin(tsWal.thread, NULL);
279
    taosThreadClear(&tsWal.thread);
S
Shengliang Guan 已提交
280 281 282 283
  }

  wDebug("wal thread is stopped");
}