dnodeCfg.c 4.8 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
 * 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 "cJSON.h"
#include "dnodeCfg.h"

static int32_t dnodeReadCfg(DnCfg *cfg) {
  int32_t len = 0;
  int32_t maxLen = 200;
  char *  content = calloc(1, maxLen + 1);
  cJSON * root = NULL;
  FILE *  fp = NULL;

  fp = fopen(cfg->file, "r");
  if (!fp) {
    dDebug("file %s not exist", cfg->file);
    goto PARSE_CFG_OVER;
  }

  len = (int32_t)fread(content, 1, maxLen, fp);
  if (len <= 0) {
    dError("failed to read %s since content is null", cfg->file);
    goto PARSE_CFG_OVER;
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", cfg->file);
    goto PARSE_CFG_OVER;
  }

  cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId");
  if (!dnodeId || dnodeId->type != cJSON_Number) {
    dError("failed to read %s since dnodeId not found", cfg->file);
    goto PARSE_CFG_OVER;
  }
  cfg->dnodeId = (int32_t)dnodeId->valueint;

  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_Number) {
    dError("failed to read %s since dropped not found", cfg->file);
    goto PARSE_CFG_OVER;
  }
  cfg->dropped = (int32_t)dropped->valueint;

  cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId");
  if (!clusterId || clusterId->type != cJSON_String) {
    dError("failed to read %s since clusterId not found", cfg->file);
    goto PARSE_CFG_OVER;
  }
  tstrncpy(cfg->clusterId, clusterId->valuestring, TSDB_CLUSTER_ID_LEN);

  dInfo("successed to read %s", cfg->file);

PARSE_CFG_OVER:
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
  if (fp != NULL) fclose(fp);
  terrno = 0;

  return 0;
}

static int32_t dnodeWriteCfg(DnCfg *cfg) {
  FILE *fp = fopen(cfg->file, "w");
  if (!fp) {
    dError("failed to write %s since %s", cfg->file, strerror(errno));
    return -1;
  }

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

  len += snprintf(content + len, maxLen - len, "{\n");
  len += snprintf(content + len, maxLen - len, "  \"dnodeId\": %d,\n", cfg->dnodeId);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": %d,\n", cfg->dropped);
  len += snprintf(content + len, maxLen - len, "  \"clusterId\": \"%s\"\n", cfg->clusterId);
  len += snprintf(content + len, maxLen - len, "}\n");

  fwrite(content, 1, len, fp);
S
Shengliang Guan 已提交
97
  taosFsyncFile(fileno(fp));
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  fclose(fp);
  free(content);
  terrno = 0;

  dInfo("successed to write %s", cfg->file);
  return 0;
}

int32_t dnodeInitCfg(Dnode *dnode, DnCfg **out) {
  DnCfg* cfg = calloc(1, sizeof(DnCfg));
  if (cfg == NULL) return -1;

  cfg->dnode = dnode;
  cfg->dnodeId = 0;
  cfg->dropped = 0;
  cfg->clusterId[0] = 0;
  snprintf(cfg->file, sizeof(cfg->file), "%s/dnodeCfg.json", tsDnodeDir);
  pthread_mutex_init(&cfg->mutex, NULL);
  *out = cfg;

  int32_t ret = dnodeReadCfg(cfg);
  if (ret == 0) {
    dInfo("dnode cfg is initialized");
  }

  if (cfg->dropped) {
    dInfo("dnode is dropped and start to exit");
    return -1;
  }

  return ret;
}

S
Shengliang Guan 已提交
131
void dnodeCleanupCfg(DnCfg **out) {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  DnCfg* cfg = *out;
  *out = NULL;

  pthread_mutex_destroy(&cfg->mutex);
  free(cfg);
}

void dnodeUpdateCfg(DnCfg *cfg, SDnodeCfg *data) {
  if (cfg == NULL || cfg->dnodeId == 0) return;

  pthread_mutex_lock(&cfg->mutex);

  cfg->dnodeId = data->dnodeId;
  tstrncpy(cfg->clusterId, data->clusterId, TSDB_CLUSTER_ID_LEN);
  dInfo("dnodeId is set to %d, clusterId is set to %s", cfg->dnodeId, cfg->clusterId);

  dnodeWriteCfg(cfg);
  pthread_mutex_unlock(&cfg->mutex);
}

void dnodeSetDropped(DnCfg *cfg) {
  pthread_mutex_lock(&cfg->mutex);
  cfg->dropped = 1;
  dnodeWriteCfg(cfg);
  pthread_mutex_unlock(&cfg->mutex);
}

int32_t dnodeGetDnodeId(DnCfg *cfg) {
  int32_t dnodeId = 0;
  pthread_mutex_lock(&cfg->mutex);
  dnodeId = cfg->dnodeId;
  pthread_mutex_unlock(&cfg->mutex);
  return dnodeId;
}

void dnodeGetClusterId(DnCfg *cfg, char *clusterId) {
  pthread_mutex_lock(&cfg->mutex);
  tstrncpy(clusterId, cfg->clusterId, TSDB_CLUSTER_ID_LEN);
  pthread_mutex_unlock(&cfg->mutex);
}

void dnodeGetCfg(DnCfg *cfg, int32_t *dnodeId, char *clusterId) {
  pthread_mutex_lock(&cfg->mutex);
  *dnodeId = cfg->dnodeId;
  tstrncpy(clusterId, cfg->clusterId, TSDB_CLUSTER_ID_LEN);
  pthread_mutex_unlock(&cfg->mutex);
}