vnodeFile.c 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * 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 "cJSON.h"
#include "vnodeFile.h"

int32_t vnodeReadCfg(int32_t vgId, SVnodeCfg *pCfg) {
  int32_t ret = TSDB_CODE_VND_APP_ERROR;
  int32_t len = 0;
  int     maxLen = 1000;
  char   *content = calloc(1, maxLen + 1);
  cJSON  *root = NULL;
  FILE   *fp = NULL;

  char file[PATH_MAX + 30] = {0};
  sprintf(file, "%s/vnode%d/config.json", tsVnodeDir, vgId);

  fp = fopen(file, "r");
  if (!fp) {
S
Shengliang Guan 已提交
33
    vError("vgId:%d, failed to open vnode cfg file:%s to read since %s", vgId, file, strerror(errno));
34 35 36 37 38 39
    ret = TAOS_SYSTEM_ERROR(errno);
    goto PARSE_VCFG_ERROR;
  }

  len = (int32_t)fread(content, 1, maxLen, fp);
  if (len <= 0) {
S
Shengliang Guan 已提交
40
    vError("vgId:%d, failed to read %s since content is null", vgId, file);
41 42 43 44 45 46
    goto PARSE_VCFG_ERROR;
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
S
Shengliang Guan 已提交
47
    vError("vgId:%d, failed to read %s since invalid json format", vgId, file);
48 49 50 51 52
    goto PARSE_VCFG_ERROR;
  }

  cJSON *db = cJSON_GetObjectItem(root, "db");
  if (!db || db->type != cJSON_String || db->valuestring == NULL) {
S
Shengliang Guan 已提交
53
    vError("vgId:%d, failed to read %s since db not found", vgId, file);
54 55 56 57 58 59
    goto PARSE_VCFG_ERROR;
  }
  tstrncpy(pCfg->db, db->valuestring, sizeof(pCfg->db));

  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_Number) {
S
Shengliang Guan 已提交
60
    vError("vgId:%d, failed to read %s since dropped not found", vgId, file);
61 62 63 64
    goto PARSE_VCFG_ERROR;
  }
  pCfg->dropped = (int32_t)dropped->valueint;

S
Shengliang Guan 已提交
65 66 67 68 69 70 71
  cJSON *quorum = cJSON_GetObjectItem(root, "quorum");
  if (!quorum || quorum->type != cJSON_Number) {
    vError("vgId: %d, failed to read %s, quorum not found", vgId, file);
    goto PARSE_VCFG_ERROR;
  }
  pCfg->quorum = (int8_t)quorum->valueint;

72 73
  cJSON *cacheBlockSize = cJSON_GetObjectItem(root, "cacheBlockSize");
  if (!cacheBlockSize || cacheBlockSize->type != cJSON_Number) {
S
Shengliang Guan 已提交
74
    vError("vgId:%d, failed to read %s since cacheBlockSize not found", vgId, file);
75 76 77 78 79 80
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.cacheBlockSize = (int32_t)cacheBlockSize->valueint;

  cJSON *totalBlocks = cJSON_GetObjectItem(root, "totalBlocks");
  if (!totalBlocks || totalBlocks->type != cJSON_Number) {
S
Shengliang Guan 已提交
81
    vError("vgId:%d, failed to read %s since totalBlocks not found", vgId, file);
82 83 84 85 86 87
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.totalBlocks = (int32_t)totalBlocks->valueint;

  cJSON *daysPerFile = cJSON_GetObjectItem(root, "daysPerFile");
  if (!daysPerFile || daysPerFile->type != cJSON_Number) {
S
Shengliang Guan 已提交
88
    vError("vgId:%d, failed to read %s since daysPerFile not found", vgId, file);
89 90 91 92 93 94
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.daysPerFile = (int32_t)daysPerFile->valueint;

  cJSON *daysToKeep0 = cJSON_GetObjectItem(root, "daysToKeep0");
  if (!daysToKeep0 || daysToKeep0->type != cJSON_Number) {
S
Shengliang Guan 已提交
95
    vError("vgId:%d, failed to read %s since daysToKeep0 not found", vgId, file);
96 97 98 99 100 101
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.daysToKeep0 = (int32_t)daysToKeep0->valueint;

  cJSON *daysToKeep1 = cJSON_GetObjectItem(root, "daysToKeep1");
  if (!daysToKeep1 || daysToKeep1->type != cJSON_Number) {
S
Shengliang Guan 已提交
102
    vError("vgId:%d, failed to read %s since daysToKeep1 not found", vgId, file);
103 104 105 106 107 108
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.daysToKeep1 = (int32_t)daysToKeep1->valueint;

  cJSON *daysToKeep2 = cJSON_GetObjectItem(root, "daysToKeep2");
  if (!daysToKeep2 || daysToKeep2->type != cJSON_Number) {
S
Shengliang Guan 已提交
109
    vError("vgId:%d, failed to read %s since daysToKeep2 not found", vgId, file);
110 111 112 113 114 115
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.daysToKeep2 = (int32_t)daysToKeep2->valueint;

  cJSON *minRowsPerFileBlock = cJSON_GetObjectItem(root, "minRowsPerFileBlock");
  if (!minRowsPerFileBlock || minRowsPerFileBlock->type != cJSON_Number) {
S
Shengliang Guan 已提交
116
    vError("vgId:%d, failed to read %s since minRowsPerFileBlock not found", vgId, file);
117 118 119 120 121 122
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.minRowsPerFileBlock = (int32_t)minRowsPerFileBlock->valueint;

  cJSON *maxRowsPerFileBlock = cJSON_GetObjectItem(root, "maxRowsPerFileBlock");
  if (!maxRowsPerFileBlock || maxRowsPerFileBlock->type != cJSON_Number) {
S
Shengliang Guan 已提交
123
    vError("vgId:%d, failed to read %s since maxRowsPerFileBlock not found", vgId, file);
124 125 126 127 128 129
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.maxRowsPerFileBlock = (int32_t)maxRowsPerFileBlock->valueint;

  cJSON *precision = cJSON_GetObjectItem(root, "precision");
  if (!precision || precision->type != cJSON_Number) {
S
Shengliang Guan 已提交
130
    vError("vgId:%d, failed to read %s since precision not found", vgId, file);
131 132 133 134 135 136
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.precision = (int8_t)precision->valueint;

  cJSON *compression = cJSON_GetObjectItem(root, "compression");
  if (!compression || compression->type != cJSON_Number) {
S
Shengliang Guan 已提交
137
    vError("vgId:%d, failed to read %s since compression not found", vgId, file);
138 139 140 141 142 143
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.compression = (int8_t)compression->valueint;

  cJSON *update = cJSON_GetObjectItem(root, "update");
  if (!update || update->type != cJSON_Number) {
S
Shengliang Guan 已提交
144
    vError("vgId: %d, failed to read %s since update not found", vgId, file);
145 146 147 148 149 150
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.update = (int8_t)update->valueint;

  cJSON *cacheLastRow = cJSON_GetObjectItem(root, "cacheLastRow");
  if (!cacheLastRow || cacheLastRow->type != cJSON_Number) {
S
Shengliang Guan 已提交
151
    vError("vgId: %d, failed to read %s since cacheLastRow not found", vgId, file);
152 153 154 155 156 157
    goto PARSE_VCFG_ERROR;
  }
  pCfg->tsdb.cacheLastRow = (int8_t)cacheLastRow->valueint;

  cJSON *walLevel = cJSON_GetObjectItem(root, "walLevel");
  if (!walLevel || walLevel->type != cJSON_Number) {
S
Shengliang Guan 已提交
158
    vError("vgId:%d, failed to read %s since walLevel not found", vgId, file);
159 160 161 162 163 164
    goto PARSE_VCFG_ERROR;
  }
  pCfg->wal.walLevel = (int8_t)walLevel->valueint;

  cJSON *fsyncPeriod = cJSON_GetObjectItem(root, "fsyncPeriod");
  if (!walLevel || walLevel->type != cJSON_Number) {
S
Shengliang Guan 已提交
165
    vError("vgId:%d, failed to read %s since fsyncPeriod not found", vgId, file);
166 167 168 169
    goto PARSE_VCFG_ERROR;
  }
  pCfg->wal.fsyncPeriod = (int32_t)fsyncPeriod->valueint;

S
Shengliang Guan 已提交
170 171 172
  cJSON *selfIndex = cJSON_GetObjectItem(root, "selfIndex");
  if (!selfIndex || selfIndex->type != cJSON_Number) {
    vError("vgId:%d, failed to read %s since selfIndex not found", vgId, file);
173 174
    goto PARSE_VCFG_ERROR;
  }
S
Shengliang Guan 已提交
175
  pCfg->sync.selfIndex = selfIndex->valueint;
176

S
Shengliang Guan 已提交
177 178 179
  cJSON *replica = cJSON_GetObjectItem(root, "replica");
  if (!replica || replica->type != cJSON_Number) {
    vError("vgId:%d, failed to read %s since replica not found", vgId, file);
180 181
    goto PARSE_VCFG_ERROR;
  }
S
Shengliang Guan 已提交
182
  pCfg->sync.replica = replica->valueint;
183 184 185 186 187 188 189 190 191

  cJSON *nodes = cJSON_GetObjectItem(root, "nodes");
  if (!nodes || nodes->type != cJSON_Array) {
    vError("vgId:%d, failed to read %s, nodes not found", vgId, file);
    goto PARSE_VCFG_ERROR;
  }

  int size = cJSON_GetArraySize(nodes);
  if (size != pCfg->sync.replica) {
S
Shengliang Guan 已提交
192
    vError("vgId:%d, failed to read %s since nodes size not matched", vgId, file);
193 194 195 196 197 198
    goto PARSE_VCFG_ERROR;
  }

  for (int i = 0; i < size; ++i) {
    cJSON *nodeInfo = cJSON_GetArrayItem(nodes, i);
    if (nodeInfo == NULL) continue;
S
Shengliang Guan 已提交
199 200 201 202 203 204 205 206
    SNodeInfo *node = &pCfg->sync.nodeInfo[i];

    cJSON *nodeId = cJSON_GetObjectItem(nodeInfo, "id");
    if (!nodeId || nodeId->type != cJSON_Number) {
      vError("vgId:%d, failed to read %s since nodeId not found", vgId, file);
      goto PARSE_VCFG_ERROR;
    }
    node->nodeId = nodeId->valueint;
207

S
Shengliang Guan 已提交
208 209 210
    cJSON *nodePort = cJSON_GetObjectItem(nodeInfo, "port");
    if (!nodePort || nodePort->type != cJSON_Number) {
      vError("vgId:%d, failed to read %s sincenodePort not found", vgId, file);
211 212
      goto PARSE_VCFG_ERROR;
    }
S
Shengliang Guan 已提交
213
    node->nodePort = (uint16_t)nodePort->valueint;
214

S
Shengliang Guan 已提交
215 216 217
    cJSON *nodeFqdn = cJSON_GetObjectItem(nodeInfo, "fqdn");
    if (!nodeFqdn || nodeFqdn->type != cJSON_String || nodeFqdn->valuestring == NULL) {
      vError("vgId:%d, failed to read %s since nodeFqdn not found", vgId, file);
218 219
      goto PARSE_VCFG_ERROR;
    }
S
Shengliang Guan 已提交
220
    tstrncpy(node->nodeFqdn, nodeFqdn->valuestring, TSDB_FQDN_LEN);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  }

  ret = TSDB_CODE_SUCCESS;

PARSE_VCFG_ERROR:
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
  if (fp != NULL) fclose(fp);

  terrno = 0;
  return ret;
}

int32_t vnodeWriteCfg(int32_t vgId, SVnodeCfg *pCfg) {
  int32_t code = 0;
  char    file[PATH_MAX + 30] = {0};
  sprintf(file, "%s/vnode%d/config.json", tsVnodeDir, vgId);

  FILE *fp = fopen(file, "w");
  if (!fp) {
    vError("vgId:%d, failed to write %s error:%s", vgId, file, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return terrno;
  }

  int32_t len = 0;
  int32_t maxLen = 1000;
  char   *content = calloc(1, maxLen + 1);

  len += snprintf(content + len, maxLen - len, "{\n");
  // vnode
  len += snprintf(content + len, maxLen - len, "  \"vgId\": %d,\n", vgId);
  len += snprintf(content + len, maxLen - len, "  \"db\": \"%s\",\n", pCfg->db);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": %d,\n", pCfg->dropped);
S
Shengliang Guan 已提交
255
  len += snprintf(content + len, maxLen - len, "  \"quorum\": %d,\n", pCfg->quorum);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  // tsdb
  len += snprintf(content + len, maxLen - len, "  \"cacheBlockSize\": %d,\n", pCfg->tsdb.cacheBlockSize);
  len += snprintf(content + len, maxLen - len, "  \"totalBlocks\": %d,\n", pCfg->tsdb.totalBlocks);
  len += snprintf(content + len, maxLen - len, "  \"daysPerFile\": %d,\n", pCfg->tsdb.daysPerFile);
  len += snprintf(content + len, maxLen - len, "  \"daysToKeep0\": %d,\n", pCfg->tsdb.daysToKeep0);
  len += snprintf(content + len, maxLen - len, "  \"daysToKeep1\": %d,\n", pCfg->tsdb.daysToKeep1);
  len += snprintf(content + len, maxLen - len, "  \"daysToKeep2\": %d,\n", pCfg->tsdb.daysToKeep2);
  len += snprintf(content + len, maxLen - len, "  \"minRowsPerFileBlock\": %d,\n", pCfg->tsdb.minRowsPerFileBlock);
  len += snprintf(content + len, maxLen - len, "  \"maxRowsPerFileBlock\": %d,\n", pCfg->tsdb.maxRowsPerFileBlock);
  len += snprintf(content + len, maxLen - len, "  \"precision\": %d,\n", pCfg->tsdb.precision);
  len += snprintf(content + len, maxLen - len, "  \"compression\": %d,\n", pCfg->tsdb.compression);
  len += snprintf(content + len, maxLen - len, "  \"cacheLastRow\": %d,\n", pCfg->tsdb.cacheLastRow);
  len += snprintf(content + len, maxLen - len, "  \"update\": %d,\n", pCfg->tsdb.update);
  // wal
  len += snprintf(content + len, maxLen - len, "  \"walLevel\": %d,\n", pCfg->wal.walLevel);
  len += snprintf(content + len, maxLen - len, "  \"fsyncPeriod\": %d,\n", pCfg->wal.fsyncPeriod);
  // sync
  len += snprintf(content + len, maxLen - len, "  \"replica\": %d,\n", pCfg->sync.replica);
S
Shengliang Guan 已提交
274
  len += snprintf(content + len, maxLen - len, "  \"selfIndex\": %d,\n", pCfg->sync.selfIndex);
275 276
  len += snprintf(content + len, maxLen - len, "  \"nodes\": [{\n");
  for (int32_t i = 0; i < pCfg->sync.replica; i++) {
S
Shengliang Guan 已提交
277 278
    SNodeInfo *node = &pCfg->sync.nodeInfo[i];
    len += snprintf(content + len, maxLen - len, "    \"id\": %d,\n", node->nodeId);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    len += snprintf(content + len, maxLen - len, "    \"port\": %u,\n", node->nodePort);
    len += snprintf(content + len, maxLen - len, "    \"fqdn\": \"%s\"\n", node->nodeFqdn);
    if (i < pCfg->sync.replica - 1) {
      len += snprintf(content + len, maxLen - len, "  },{\n");
    } else {
      len += snprintf(content + len, maxLen - len, "  }]\n");
    }
  }
  len += snprintf(content + len, maxLen - len, "}\n");

  fwrite(content, 1, len, fp);
  taosFsyncFile(fileno(fp));
  fclose(fp);
  free(content);
  terrno = 0;

  vInfo("vgId:%d, successed to write %s", vgId, file);
  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
299
int32_t vnodeReadState(int32_t vgId, SSyncServerState *pState) {
S
Shengliang Guan 已提交
300
  int32_t ret = TSDB_CODE_VND_APP_ERROR;
301 302
  int32_t len = 0;
  int32_t maxLen = 100;
S
Shengliang Guan 已提交
303 304 305
  char   *content = calloc(1, maxLen + 1);
  cJSON  *root = NULL;
  FILE   *fp = NULL;
306

S
Shengliang Guan 已提交
307
  char file[PATH_MAX + 30] = {0};
S
Shengliang Guan 已提交
308
  sprintf(file, "%s/vnode%d/state.json", tsVnodeDir, vgId);
309 310 311

  len = (int32_t)fread(content, 1, maxLen, fp);
  if (len <= 0) {
S
Shengliang Guan 已提交
312 313
    vError("vgId:%d, failed to read %s since content is null", vgId, file);
    goto PARSE_TERM_ERROR;
314 315 316 317
  }

  root = cJSON_Parse(content);
  if (root == NULL) {
S
Shengliang Guan 已提交
318 319
    vError("vgId:%d, failed to read %s since invalid json format", vgId, file);
    goto PARSE_TERM_ERROR;
320 321
  }

S
Shengliang Guan 已提交
322
  cJSON *term = cJSON_GetObjectItem(root, "term");
S
Shengliang Guan 已提交
323
  if (!term || term->type != cJSON_String) {
S
Shengliang Guan 已提交
324 325
    vError("vgId:%d, failed to read %s since term not found", vgId, file);
    goto PARSE_TERM_ERROR;
326
  }
S
Shengliang Guan 已提交
327
  pState->term = atoll(term->valuestring);
328

S
Shengliang Guan 已提交
329
  cJSON *voteFor = cJSON_GetObjectItem(root, "voteFor");
S
Shengliang Guan 已提交
330
  if (!voteFor || voteFor->type != cJSON_String) {
S
Shengliang Guan 已提交
331 332 333
    vError("vgId:%d, failed to read %s since voteFor not found", vgId, file);
    goto PARSE_TERM_ERROR;
  }
S
Shengliang Guan 已提交
334
  pState->voteFor = atoi(voteFor->valuestring);
335

S
Shengliang Guan 已提交
336
  vInfo("vgId:%d, read %s success, voteFor:%d, term:%" PRIu64, vgId, file, pState->voteFor, pState->term);
S
Shengliang Guan 已提交
337 338

PARSE_TERM_ERROR:
339 340 341 342
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
  if (fp != NULL) fclose(fp);

S
Shengliang Guan 已提交
343
  return ret;
344 345
}

S
Shengliang Guan 已提交
346
int32_t vnodeSaveState(int32_t vgId, SSyncServerState *pState) {
S
Shengliang Guan 已提交
347
  char file[PATH_MAX + 30] = {0};
S
Shengliang Guan 已提交
348
  sprintf(file, "%s/vnode%d/state.json", tsVnodeDir, vgId);
349 350 351

  FILE *fp = fopen(file, "w");
  if (!fp) {
S
Shengliang Guan 已提交
352
    vError("vgId:%d, failed to write %s since %s", vgId, file, strerror(errno));
353 354 355 356 357
    return -1;
  }

  int32_t len = 0;
  int32_t maxLen = 100;
S
Shengliang Guan 已提交
358
  char   *content = calloc(1, maxLen + 1);
359 360

  len += snprintf(content + len, maxLen - len, "{\n");
S
Shengliang Guan 已提交
361 362
  len += snprintf(content + len, maxLen - len, "  \"term\": \"%" PRIu64 "\",\n", pState->term);
  len += snprintf(content + len, maxLen - len, "  \"voteFor\": \"%d\"\n", pState->voteFor);
363
  len += snprintf(content + len, maxLen - len, "}\n");
S
Shengliang Guan 已提交
364

365 366 367 368 369
  fwrite(content, 1, len, fp);
  taosFsyncFile(fileno(fp));
  fclose(fp);
  free(content);

S
Shengliang Guan 已提交
370
  vInfo("vgId:%d, write %s success, voteFor:%d, term:%" PRIu64, vgId, file, pState->voteFor, pState->term);
371 372
  return TSDB_CODE_SUCCESS;
}