walMgmt.c 6.7 KB
Newer Older
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/>.
 */

#define _DEFAULT_SOURCE
L
Liu Jicong 已提交
17
#include "compare.h"
18 19
#include "os.h"
#include "taoserror.h"
S
TD-1895  
Shengliang Guan 已提交
20
#include "tfile.h"
L
Liu Jicong 已提交
21
#include "tref.h"
22
#include "walInt.h"
S
Shengliang Guan 已提交
23 24 25

typedef struct {
  int8_t    stop;
L
Liu Jicong 已提交
26
  int8_t    inited;
L
Liu Jicong 已提交
27 28
  uint32_t  seq;
  int32_t   refSetId;
S
Shengliang Guan 已提交
29 30 31
  pthread_t thread;
} SWalMgmt;

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

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

S
Shengliang Guan 已提交
39
int32_t walInit() {
L
Liu Jicong 已提交
40
  int8_t old = atomic_val_compare_exchange_8(&tsWal.inited, 0, 1);
L
Liu Jicong 已提交
41
  if (old == 1) return 0;
L
Liu Jicong 已提交
42

L
Liu Jicong 已提交
43
  int code = tfInit();
L
Liu Jicong 已提交
44
  if (code != 0) {
L
Liu Jicong 已提交
45 46 47 48
    wError("failed to init tfile since %s", tstrerror(code));
    atomic_store_8(&tsWal.inited, 0);
    return code;
  }
L
Liu Jicong 已提交
49
  tsWal.refSetId = taosOpenRef(TSDB_MIN_VNODES, walFreeObj);
S
Shengliang Guan 已提交
50

L
Liu Jicong 已提交
51
  code = walCreateThread();
L
Liu Jicong 已提交
52
  if (code != 0) {
S
TD-1846  
Shengliang Guan 已提交
53
    wError("failed to init wal module since %s", tstrerror(code));
L
Liu Jicong 已提交
54
    atomic_store_8(&tsWal.inited, 0);
S
Shengliang Guan 已提交
55 56 57
    return code;
  }

L
Liu Jicong 已提交
58
  wInfo("wal module is initialized, rsetId:%d", tsWal.refSetId);
L
Liu Jicong 已提交
59
  return 0;
S
Shengliang Guan 已提交
60 61 62
}

void walCleanUp() {
L
Liu Jicong 已提交
63
  int8_t old = atomic_val_compare_exchange_8(&tsWal.inited, 1, 0);
L
Liu Jicong 已提交
64
  if (old == 0) {
65 66
    return;
  }
S
Shengliang Guan 已提交
67
  walStopThread();
L
Liu Jicong 已提交
68
  taosCloseRef(tsWal.refSetId);
S
Shengliang Guan 已提交
69 70 71
  wInfo("wal module is cleaned up");
}

L
Liu Jicong 已提交
72
SWal *walOpen(const char *path, SWalCfg *pCfg) {
L
Liu Jicong 已提交
73
  SWal *pWal = malloc(sizeof(SWal));
S
Shengliang Guan 已提交
74 75 76 77
  if (pWal == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return NULL;
  }
L
Liu Jicong 已提交
78

L
Liu Jicong 已提交
79
  // set config
L
Liu Jicong 已提交
80
  memcpy(&pWal->cfg, pCfg, sizeof(SWalCfg));
L
Liu Jicong 已提交
81
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
L
Liu Jicong 已提交
82
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
L
Liu Jicong 已提交
83

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

L
Liu Jicong 已提交
90
  // open meta
L
Liu Jicong 已提交
91
  walResetVer(&pWal->vers);
L
Liu Jicong 已提交
92 93 94
  pWal->writeLogTfd = -1;
  pWal->writeIdxTfd = -1;
  pWal->writeCur = -1;
L
Liu Jicong 已提交
95
  pWal->fileInfoSet = taosArrayInit(8, sizeof(SWalFileInfo));
L
Liu Jicong 已提交
96
  if (pWal->fileInfoSet == NULL) {
L
Liu Jicong 已提交
97 98 99 100
    wError("vgId:%d, path:%s, failed to init taosArray %s", pWal->cfg.vgId, pWal->path, strerror(errno));
    free(pWal);
    return NULL;
  }
L
Liu Jicong 已提交
101

L
Liu Jicong 已提交
102
  // init status
L
Liu Jicong 已提交
103
  pWal->totSize = 0;
L
Liu Jicong 已提交
104
  pWal->lastRollSeq = -1;
L
Liu Jicong 已提交
105

L
Liu Jicong 已提交
106
  // init write buffer
L
Liu Jicong 已提交
107
  memset(&pWal->writeHead, 0, sizeof(SWalHead));
L
Liu Jicong 已提交
108
  pWal->writeHead.head.headVer = WAL_HEAD_VER;
L
Liu Jicong 已提交
109
  pWal->writeHead.magic = WAL_MAGIC;
S
Shengliang Guan 已提交
110

L
Liu Jicong 已提交
111
  if (pthread_mutex_init(&pWal->mutex, NULL) < 0) {
L
Liu Jicong 已提交
112 113 114 115
    taosArrayDestroy(pWal->fileInfoSet);
    free(pWal);
    return NULL;
  }
S
Shengliang Guan 已提交
116

L
Liu Jicong 已提交
117
  pWal->refId = taosAddRef(tsWal.refSetId, pWal);
L
Liu Jicong 已提交
118
  if (pWal->refId < 0) {
L
Liu Jicong 已提交
119 120 121
    pthread_mutex_destroy(&pWal->mutex);
    taosArrayDestroy(pWal->fileInfoSet);
    free(pWal);
S
Shengliang Guan 已提交
122 123 124
    return NULL;
  }

L
Liu Jicong 已提交
125 126 127
  walLoadMeta(pWal);

  if (walCheckAndRepairMeta(pWal) < 0) {
L
Liu Jicong 已提交
128 129 130 131
    taosRemoveRef(tsWal.refSetId, pWal->refId);
    pthread_mutex_destroy(&pWal->mutex);
    taosArrayDestroy(pWal->fileInfoSet);
    free(pWal);
S
Shengliang Guan 已提交
132 133 134
    return NULL;
  }

L
Liu Jicong 已提交
135
  if (walCheckAndRepairIdx(pWal) < 0) {
L
Liu Jicong 已提交
136

L
Liu Jicong 已提交
137 138
  }

L
Liu Jicong 已提交
139 140
  wDebug("vgId:%d, wal:%p is opened, level:%d fsyncPeriod:%d", pWal->cfg.vgId, pWal, pWal->cfg.level,
         pWal->cfg.fsyncPeriod);
S
Shengliang Guan 已提交
141 142 143 144

  return pWal;
}

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

L
Liu Jicong 已提交
148 149 150
  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 已提交
151
    return 0;
S
Shengliang Guan 已提交
152 153
  }

L
Liu Jicong 已提交
154 155
  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 已提交
156

L
Liu Jicong 已提交
157 158
  pWal->cfg.level = pCfg->level;
  pWal->cfg.fsyncPeriod = pCfg->fsyncPeriod;
159
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
S
TD-1846  
Shengliang Guan 已提交
160
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
S
Shengliang Guan 已提交
161

L
Liu Jicong 已提交
162
  return 0;
S
TD-1894  
Shengliang Guan 已提交
163 164
}

L
Liu Jicong 已提交
165
void walClose(SWal *pWal) {
166
  pthread_mutex_lock(&pWal->mutex);
L
Liu Jicong 已提交
167
  tfClose(pWal->writeLogTfd);
L
Liu Jicong 已提交
168
  pWal->writeLogTfd = -1;
L
Liu Jicong 已提交
169
  tfClose(pWal->writeIdxTfd);
L
Liu Jicong 已提交
170
  pWal->writeIdxTfd = -1;
L
Liu Jicong 已提交
171
  walSaveMeta(pWal);
L
Liu Jicong 已提交
172 173
  taosArrayDestroy(pWal->fileInfoSet);
  pWal->fileInfoSet = NULL;
174
  pthread_mutex_unlock(&pWal->mutex);
S
Shengliang Guan 已提交
175

L
Liu Jicong 已提交
176
  taosRemoveRef(tsWal.refSetId, pWal->refId);
S
Shengliang Guan 已提交
177 178 179
}

static void walFreeObj(void *wal) {
S
TD-1846  
Shengliang Guan 已提交
180
  SWal *pWal = wal;
L
Liu Jicong 已提交
181
  wDebug("vgId:%d, wal:%p is freed", pWal->cfg.vgId, pWal);
S
Shengliang Guan 已提交
182 183 184 185 186

  pthread_mutex_destroy(&pWal->mutex);
  tfree(pWal);
}

S
TD-1846  
Shengliang Guan 已提交
187
static bool walNeedFsync(SWal *pWal) {
L
Liu Jicong 已提交
188
  if (pWal->cfg.fsyncPeriod <= 0 || pWal->cfg.level != TAOS_WAL_FSYNC) {
S
TD-1846  
Shengliang Guan 已提交
189 190
    return false;
  }
S
Shengliang Guan 已提交
191

L
Liu Jicong 已提交
192
  if (atomic_load_32(&tsWal.seq) % pWal->fsyncSeq == 0) {
S
TD-1846  
Shengliang Guan 已提交
193 194
    return true;
  }
S
Shengliang Guan 已提交
195

S
TD-1846  
Shengliang Guan 已提交
196 197
  return false;
}
S
Shengliang Guan 已提交
198 199

static void walUpdateSeq() {
S
TD-1846  
Shengliang Guan 已提交
200
  taosMsleep(WAL_REFRESH_MS);
L
Liu Jicong 已提交
201
  atomic_add_fetch_32(&tsWal.seq, 1);
S
Shengliang Guan 已提交
202 203 204
}

static void walFsyncAll() {
L
Liu Jicong 已提交
205
  SWal *pWal = taosIterateRef(tsWal.refSetId, 0);
S
TD-1846  
Shengliang Guan 已提交
206 207
  while (pWal) {
    if (walNeedFsync(pWal)) {
L
Liu Jicong 已提交
208 209
      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 已提交
210
      int32_t code = tfFsync(pWal->writeLogTfd);
S
TD-1846  
Shengliang Guan 已提交
211
      if (code != 0) {
L
Liu Jicong 已提交
212 213
        wError("vgId:%d, file:%" PRId64 ".log, failed to fsync since %s", pWal->cfg.vgId, walGetLastFileFirstVer(pWal),
               strerror(code));
S
TD-1846  
Shengliang Guan 已提交
214 215
      }
    }
L
Liu Jicong 已提交
216
    pWal = taosIterateRef(tsWal.refSetId, pWal->refId);
S
TD-1846  
Shengliang Guan 已提交
217
  }
S
Shengliang Guan 已提交
218 219 220
}

static void *walThreadFunc(void *param) {
H
Haojun Liao 已提交
221
  setThreadName("wal");
S
Shengliang Guan 已提交
222 223 224
  while (1) {
    walUpdateSeq();
    walFsyncAll();
J
Jun Li 已提交
225

L
Liu Jicong 已提交
226
    if (atomic_load_8(&tsWal.stop)) break;
S
Shengliang Guan 已提交
227 228 229 230 231 232 233 234 235 236 237
  }

  return NULL;
}

static int32_t walCreateThread() {
  pthread_attr_t thAttr;
  pthread_attr_init(&thAttr);
  pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE);

  if (pthread_create(&tsWal.thread, &thAttr, walThreadFunc, NULL) != 0) {
S
TD-1846  
Shengliang Guan 已提交
238
    wError("failed to create wal thread since %s", strerror(errno));
L
Liu Jicong 已提交
239 240
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
S
Shengliang Guan 已提交
241 242 243
  }

  pthread_attr_destroy(&thAttr);
S
TD-1207  
Shengliang Guan 已提交
244
  wDebug("wal thread is launched, thread:0x%08" PRIx64, taosGetPthreadId(tsWal.thread));
S
Shengliang Guan 已提交
245

L
Liu Jicong 已提交
246
  return 0;
S
Shengliang Guan 已提交
247 248 249
}

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

L
Liu Jicong 已提交
252
  if (taosCheckPthreadValid(tsWal.thread)) {
S
Shengliang Guan 已提交
253 254 255 256 257
    pthread_join(tsWal.thread, NULL);
  }

  wDebug("wal thread is stopped");
}