walMgmt.c 6.8 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
//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);

S
Shengliang Guan 已提交
29
typedef struct {
L
Liu Jicong 已提交
30
  int32_t   refSetId;
L
Liu Jicong 已提交
31
  uint32_t  seq;
S
Shengliang Guan 已提交
32
  int8_t    stop;
L
Liu Jicong 已提交
33
  int8_t    inited;
S
Shengliang Guan 已提交
34 35 36
  pthread_t thread;
} SWalMgmt;

L
Liu Jicong 已提交
37
static SWalMgmt tsWal = {0, .seq = 1};
S
Shengliang Guan 已提交
38 39 40 41 42
static int32_t  walCreateThread();
static void     walStopThread();
static int32_t  walInitObj(SWal *pWal);
static void     walFreeObj(void *pWal);

L
Liu Jicong 已提交
43 44 45 46
int64_t walGetSeq() {
  return (int64_t)atomic_load_32(&tsWal.seq);
}

S
Shengliang Guan 已提交
47
int32_t walInit() {
L
Liu Jicong 已提交
48 49
  int8_t old = atomic_val_compare_exchange_8(&tsWal.inited, 0, 1);
  if(old == 1) return 0;
L
Liu Jicong 已提交
50

L
Liu Jicong 已提交
51
  tsWal.refSetId = taosOpenRef(TSDB_MIN_VNODES, walFreeObj);
S
Shengliang Guan 已提交
52

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

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

void walCleanUp() {
  walStopThread();
L
Liu Jicong 已提交
66
  taosCloseRef(tsWal.refSetId);
S
Shengliang Guan 已提交
67 68 69
  wInfo("wal module is cleaned up");
}

L
Liu Jicong 已提交
70 71 72 73 74 75 76 77 78 79 80 81
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
L
Liu Jicong 已提交
82
    if(1 /* TODO:regex match */) {
L
Liu Jicong 已提交
83 84 85 86 87 88 89 90
      int64_t fnameInt64 = atoll(name);
      taosArrayPush(pWal->fileSet, &fnameInt64);
    }
  }
  taosArraySort(pWal->fileSet, compareInt64Val);
  return 0;
}

L
Liu Jicong 已提交
91
SWal *walOpen(const char *path, SWalCfg *pCfg) {
L
Liu Jicong 已提交
92
  SWal *pWal = malloc(sizeof(SWal));
S
Shengliang Guan 已提交
93 94 95 96 97
  if (pWal == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return NULL;
  }

S
TD-1846  
Shengliang Guan 已提交
98
  pWal->vgId = pCfg->vgId;
L
Liu Jicong 已提交
99
  pWal->curLogTfd = -1;
L
Liu Jicong 已提交
100
  pWal->curIdxTfd = -1;
S
Shengliang Guan 已提交
101 102
  pWal->level = pCfg->walLevel;
  pWal->fsyncPeriod = pCfg->fsyncPeriod;
L
Liu Jicong 已提交
103 104 105 106

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

S
TD-1846  
Shengliang Guan 已提交
107
  tstrncpy(pWal->path, path, sizeof(pWal->path));
S
Shengliang Guan 已提交
108 109
  pthread_mutex_init(&pWal->mutex, NULL);

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

L
Liu Jicong 已提交
113
  if (walInitObj(pWal) != 0) {
S
Shengliang Guan 已提交
114 115 116 117
    walFreeObj(pWal);
    return NULL;
  }

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

S
TD-1846  
Shengliang Guan 已提交
125
  wDebug("vgId:%d, wal:%p is opened, level:%d fsyncPeriod:%d", pWal->vgId, pWal, pWal->level, pWal->fsyncPeriod);
S
Shengliang Guan 已提交
126 127 128 129

  return pWal;
}

L
Liu Jicong 已提交
130 131
int32_t walAlter(SWal *pWal, SWalCfg *pCfg) {
  if (pWal == NULL) return TSDB_CODE_WAL_APP_ERROR;
S
Shengliang Guan 已提交
132 133 134 135

  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 已提交
136
    return 0;
S
Shengliang Guan 已提交
137 138 139 140 141 142 143
  }

  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;
144
  pWal->fsyncSeq = pCfg->fsyncPeriod / 1000;
S
TD-1846  
Shengliang Guan 已提交
145
  if (pWal->fsyncSeq <= 0) pWal->fsyncSeq = 1;
S
Shengliang Guan 已提交
146

L
Liu Jicong 已提交
147
  return 0;
S
TD-1894  
Shengliang Guan 已提交
148 149
}

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

153
  pthread_mutex_lock(&pWal->mutex);
L
Liu Jicong 已提交
154
  tfClose(pWal->curLogTfd);
L
Liu Jicong 已提交
155 156 157
  tfClose(pWal->curIdxTfd);
  taosArrayDestroy(pWal->fileSet);
  pWal->fileSet = NULL;
158
  pthread_mutex_unlock(&pWal->mutex);
L
Liu Jicong 已提交
159
  taosRemoveRef(tsWal.refSetId, pWal->refId);
S
Shengliang Guan 已提交
160 161 162
}

static int32_t walInitObj(SWal *pWal) {
163
  if (taosMkDir(pWal->path) != 0) {
S
Shengliang Guan 已提交
164
    wError("vgId:%d, path:%s, failed to create directory since %s", pWal->vgId, pWal->path, strerror(errno));
S
TD-1846  
Shengliang Guan 已提交
165
    return TAOS_SYSTEM_ERROR(errno);
S
Shengliang Guan 已提交
166
  }
L
Liu Jicong 已提交
167 168 169 170 171
  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 已提交
172

S
TD-1887  
Shengliang Guan 已提交
173
  wDebug("vgId:%d, object is initialized", pWal->vgId);
L
Liu Jicong 已提交
174
  return 0;
S
Shengliang Guan 已提交
175 176 177
}

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

L
Liu Jicong 已提交
181
  tfClose(pWal->curLogTfd);
L
Liu Jicong 已提交
182 183 184
  tfClose(pWal->curIdxTfd);
  taosArrayDestroy(pWal->fileSet);
  pWal->fileSet = NULL;
S
Shengliang Guan 已提交
185 186 187 188
  pthread_mutex_destroy(&pWal->mutex);
  tfree(pWal);
}

S
TD-1846  
Shengliang Guan 已提交
189 190 191 192
static bool walNeedFsync(SWal *pWal) {
  if (pWal->fsyncPeriod <= 0 || pWal->level != TAOS_WAL_FSYNC) {
    return false;
  }
S
Shengliang Guan 已提交
193

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

S
TD-1846  
Shengliang Guan 已提交
198 199
  return false;
}
S
Shengliang Guan 已提交
200 201

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

static void walFsyncAll() {
L
Liu Jicong 已提交
207
  SWal *pWal = taosIterateRef(tsWal.refSetId, 0);
S
TD-1846  
Shengliang Guan 已提交
208 209
  while (pWal) {
    if (walNeedFsync(pWal)) {
L
Liu Jicong 已提交
210
      wTrace("vgId:%d, do fsync, level:%d seq:%d rseq:%d", pWal->vgId, pWal->level, pWal->fsyncSeq, atomic_load_32(&tsWal.seq));
L
Liu Jicong 已提交
211
      int32_t code = tfFsync(pWal->curLogTfd);
S
TD-1846  
Shengliang Guan 已提交
212
      if (code != 0) {
L
Liu Jicong 已提交
213
        wError("vgId:%d, file:%"PRId64".log, failed to fsync since %s", pWal->vgId, pWal->curFileFirstVersion, 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));
S
Shengliang Guan 已提交
239 240 241 242
    return TAOS_SYSTEM_ERROR(errno);
  }

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

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

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

S
TD-1207  
Shengliang Guan 已提交
251
  if (taosCheckPthreadValid(tsWal.thread)) {
S
Shengliang Guan 已提交
252 253 254 255 256
    pthread_join(tsWal.thread, NULL);
  }

  wDebug("wal thread is stopped");
}