walMgmt.c 7.0 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"
S
Shengliang Guan 已提交
19
#include "tref.h"
S
TD-1895  
Shengliang Guan 已提交
20
#include "tfile.h"
L
Liu Jicong 已提交
21
#include "compare.h"
22
#include "walInt.h"
S
Shengliang Guan 已提交
23

L
Liu Jicong 已提交
24 25 26 27 28 29 30 31
//internal
int32_t walGetNextFile(SWal *pWal, int64_t *nextFileId);
int32_t walGetOldFile(SWal *pWal, int64_t curFileId, int32_t minDiff, int64_t *oldFileId);
int32_t walGetNewFile(SWal *pWal, int64_t *newFileId);

static pthread_mutex_t walInitLock = PTHREAD_MUTEX_INITIALIZER;
static int8_t walInited = 0;

S
Shengliang Guan 已提交
32
typedef struct {
L
Liu Jicong 已提交
33
  int32_t   refSetId;
S
Shengliang Guan 已提交
34 35 36 37 38 39
  int32_t   seq;
  int8_t    stop;
  pthread_t thread;
  pthread_mutex_t mutex;
} SWalMgmt;

S
TD-1848  
Shengliang Guan 已提交
40
static SWalMgmt tsWal = {0};
S
Shengliang Guan 已提交
41 42 43 44 45 46
static int32_t  walCreateThread();
static void     walStopThread();
static int32_t  walInitObj(SWal *pWal);
static void     walFreeObj(void *pWal);

int32_t walInit() {
L
Liu Jicong 已提交
47 48 49 50 51 52 53 54 55 56
  //TODO: change to atomic
  pthread_mutex_lock(&walInitLock);
  if(walInited) {
    pthread_mutex_unlock(&walInitLock);
    return 0;
  } else {
    walInited = 1;
    pthread_mutex_unlock(&walInitLock);
  }

J
Jun Li 已提交
57
  int32_t code = 0;
L
Liu Jicong 已提交
58
  tsWal.refSetId = taosOpenRef(TSDB_MIN_VNODES, walFreeObj);
S
Shengliang Guan 已提交
59

J
Jun Li 已提交
60
  code = pthread_mutex_init(&tsWal.mutex, NULL);
L
Liu Jicong 已提交
61
  if (code != 0) {
J
Jun Li 已提交
62 63 64 65 66
    wError("failed to init wal mutex since %s", tstrerror(code));
    return code;
  }

  code = walCreateThread();
L
Liu Jicong 已提交
67
  if (code != 0) {
S
TD-1846  
Shengliang Guan 已提交
68
    wError("failed to init wal module since %s", tstrerror(code));
S
Shengliang Guan 已提交
69 70 71
    return code;
  }

L
Liu Jicong 已提交
72
  wInfo("wal module is initialized, rsetId:%d", tsWal.refSetId);
S
Shengliang Guan 已提交
73 74 75 76 77
  return code;
}

void walCleanUp() {
  walStopThread();
L
Liu Jicong 已提交
78
  taosCloseRef(tsWal.refSetId);
J
Jun Li 已提交
79
  pthread_mutex_destroy(&tsWal.mutex);
S
Shengliang Guan 已提交
80 81 82
  wInfo("wal module is cleaned up");
}

L
Liu Jicong 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static int walLoadFileset(SWal *pWal) {
  DIR *dir = opendir(pWal->path);
  if (dir == NULL) {
    wError("vgId:%d, path:%s, failed to open since %s", pWal->vgId, pWal->path, strerror(errno));
    return -1;
  }

  struct dirent* ent;
  while ((ent = readdir(dir)) != NULL) {
    char *name = ent->d_name;
    name[WAL_NOSUFFIX_LEN] = 0;
    //validate file name by regex matching
    if(1 /* regex match */) {
      int64_t fnameInt64 = atoll(name);
      taosArrayPush(pWal->fileSet, &fnameInt64);
    }
  }
  taosArraySort(pWal->fileSet, compareInt64Val);
  return 0;
}

L
Liu Jicong 已提交
104
SWal *walOpen(const char *path, SWalCfg *pCfg) {
L
Liu Jicong 已提交
105
  SWal *pWal = malloc(sizeof(SWal));
S
Shengliang Guan 已提交
106 107 108 109 110
  if (pWal == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return NULL;
  }

S
TD-1846  
Shengliang Guan 已提交
111
  pWal->vgId = pCfg->vgId;
L
Liu Jicong 已提交
112
  pWal->curLogTfd = -1;
L
Liu Jicong 已提交
113
  pWal->curIdxTfd = -1;
S
Shengliang Guan 已提交
114 115
  pWal->level = pCfg->walLevel;
  pWal->fsyncPeriod = pCfg->fsyncPeriod;
L
Liu Jicong 已提交
116 117 118 119

  memset(&pWal->head, 0, sizeof(SWalHead));
  pWal->head.sver = 0;

S
TD-1846  
Shengliang Guan 已提交
120
  tstrncpy(pWal->path, path, sizeof(pWal->path));
S
Shengliang Guan 已提交
121 122
  pthread_mutex_init(&pWal->mutex, NULL);

S
TD-1847  
Shengliang Guan 已提交
123
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
S
Shengliang Guan 已提交
124 125
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;

L
Liu Jicong 已提交
126
  if (walInitObj(pWal) != 0) {
S
Shengliang Guan 已提交
127 128 129 130
    walFreeObj(pWal);
    return NULL;
  }

L
Liu Jicong 已提交
131 132
   pWal->refId = taosAddRef(tsWal.refSetId, pWal);
   if (pWal->refId < 0) {
S
Shengliang Guan 已提交
133 134 135 136
    walFreeObj(pWal);
    return NULL;
  }

S
TD-1846  
Shengliang Guan 已提交
137
  wDebug("vgId:%d, wal:%p is opened, level:%d fsyncPeriod:%d", pWal->vgId, pWal, pWal->level, pWal->fsyncPeriod);
S
Shengliang Guan 已提交
138 139 140 141

  return pWal;
}

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

  if (pWal->level == pCfg->walLevel && pWal->fsyncPeriod == pCfg->fsyncPeriod) {
    wDebug("vgId:%d, old walLevel:%d fsync:%d, new walLevel:%d fsync:%d not change", pWal->vgId, pWal->level,
           pWal->fsyncPeriod, pCfg->walLevel, pCfg->fsyncPeriod);
L
Liu Jicong 已提交
148
    return 0;
S
Shengliang Guan 已提交
149 150 151 152 153 154 155
  }

  wInfo("vgId:%d, change old walLevel:%d fsync:%d, new walLevel:%d fsync:%d", pWal->vgId, pWal->level,
        pWal->fsyncPeriod, pCfg->walLevel, pCfg->fsyncPeriod);

  pWal->level = pCfg->walLevel;
  pWal->fsyncPeriod = pCfg->fsyncPeriod;
156
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
S
TD-1846  
Shengliang Guan 已提交
157
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
S
Shengliang Guan 已提交
158

L
Liu Jicong 已提交
159
  return 0;
S
TD-1894  
Shengliang Guan 已提交
160 161
}

L
Liu Jicong 已提交
162 163
void walClose(SWal *pWal) {
  if (pWal == NULL) return;
S
TD-1846  
Shengliang Guan 已提交
164

165
  pthread_mutex_lock(&pWal->mutex);
L
Liu Jicong 已提交
166
  tfClose(pWal->curLogTfd);
167
  pthread_mutex_unlock(&pWal->mutex);
L
Liu Jicong 已提交
168
  taosRemoveRef(tsWal.refSetId, pWal->refId);
S
Shengliang Guan 已提交
169 170 171
}

static int32_t walInitObj(SWal *pWal) {
172
  if (taosMkDir(pWal->path) != 0) {
S
Shengliang Guan 已提交
173
    wError("vgId:%d, path:%s, failed to create directory since %s", pWal->vgId, pWal->path, strerror(errno));
S
TD-1846  
Shengliang Guan 已提交
174
    return TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
175
  }
L
Liu Jicong 已提交
176 177 178 179 180
  pWal->fileSet = taosArrayInit(0, sizeof(int64_t));
  if(pWal->fileSet == NULL) {
    wError("vgId:%d, path:%s, failed to init taosArray %s", pWal->vgId, pWal->path, strerror(errno));
    return TAOS_SYSTEM_ERROR(errno);
  }
S
Shengliang Guan 已提交
181

S
TD-1887  
Shengliang Guan 已提交
182
  wDebug("vgId:%d, object is initialized", pWal->vgId);
L
Liu Jicong 已提交
183
  return 0;
S
Shengliang Guan 已提交
184 185 186
}

static void walFreeObj(void *wal) {
S
TD-1846  
Shengliang Guan 已提交
187 188
  SWal *pWal = wal;
  wDebug("vgId:%d, wal:%p is freed", pWal->vgId, pWal);
S
Shengliang Guan 已提交
189

L
Liu Jicong 已提交
190
  tfClose(pWal->curLogTfd);
S
Shengliang Guan 已提交
191 192 193 194
  pthread_mutex_destroy(&pWal->mutex);
  tfree(pWal);
}

S
TD-1846  
Shengliang Guan 已提交
195 196 197 198
static bool walNeedFsync(SWal *pWal) {
  if (pWal->fsyncPeriod <= 0 || pWal->level != TAOS_WAL_FSYNC) {
    return false;
  }
S
Shengliang Guan 已提交
199

S
TD-1846  
Shengliang Guan 已提交
200 201 202
  if (tsWal.seq % pWal->fsyncSeq == 0) {
    return true;
  }
S
Shengliang Guan 已提交
203

S
TD-1846  
Shengliang Guan 已提交
204 205
  return false;
}
S
Shengliang Guan 已提交
206 207

static void walUpdateSeq() {
S
TD-1846  
Shengliang Guan 已提交
208
  taosMsleep(WAL_REFRESH_MS);
S
Shengliang Guan 已提交
209 210 211 212 213 214
  if (++tsWal.seq <= 0) {
    tsWal.seq = 1;
  }
}

static void walFsyncAll() {
L
Liu Jicong 已提交
215
  SWal *pWal = taosIterateRef(tsWal.refSetId, 0);
S
TD-1846  
Shengliang Guan 已提交
216 217 218
  while (pWal) {
    if (walNeedFsync(pWal)) {
      wTrace("vgId:%d, do fsync, level:%d seq:%d rseq:%d", pWal->vgId, pWal->level, pWal->fsyncSeq, tsWal.seq);
L
Liu Jicong 已提交
219
      int32_t code = tfFsync(pWal->curLogTfd);
S
TD-1846  
Shengliang Guan 已提交
220
      if (code != 0) {
L
Liu Jicong 已提交
221
        wError("vgId:%d, file:%"PRId64".log, failed to fsync since %s", pWal->vgId, pWal->curFileFirstVersion, strerror(code));
S
TD-1846  
Shengliang Guan 已提交
222 223
      }
    }
L
Liu Jicong 已提交
224
    pWal = taosIterateRef(tsWal.refSetId, pWal->refId);
S
TD-1846  
Shengliang Guan 已提交
225
  }
S
Shengliang Guan 已提交
226 227 228
}

static void *walThreadFunc(void *param) {
J
Jun Li 已提交
229
  int stop = 0;
H
Haojun Liao 已提交
230
  setThreadName("wal");
S
Shengliang Guan 已提交
231 232 233
  while (1) {
    walUpdateSeq();
    walFsyncAll();
J
Jun Li 已提交
234 235 236 237 238

    pthread_mutex_lock(&tsWal.mutex);
    stop = tsWal.stop;
    pthread_mutex_unlock(&tsWal.mutex);
    if (stop) break;
S
Shengliang Guan 已提交
239 240 241 242 243 244 245 246 247 248 249
  }

  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 已提交
250
    wError("failed to create wal thread since %s", strerror(errno));
S
Shengliang Guan 已提交
251 252 253 254
    return TAOS_SYSTEM_ERROR(errno);
  }

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

L
Liu Jicong 已提交
257
  return 0;
S
Shengliang Guan 已提交
258 259 260
}

static void walStopThread() {
J
Jun Li 已提交
261
  pthread_mutex_lock(&tsWal.mutex);
S
TD-1846  
Shengliang Guan 已提交
262
  tsWal.stop = 1;
J
Jun Li 已提交
263 264
  pthread_mutex_unlock(&tsWal.mutex);

S
TD-1207  
Shengliang Guan 已提交
265
  if (taosCheckPthreadValid(tsWal.thread)) {
S
Shengliang Guan 已提交
266 267 268 269 270
    pthread_join(tsWal.thread, NULL);
  }

  wDebug("wal thread is stopped");
}