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
/*
 * 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"
19
#include "tglobal.h"
20 21
#include "dnodeCfg.h"

22 23 24 25 26 27 28 29 30
static struct DnCfg {
  int32_t         dnodeId;
  int32_t         dropped;
  char            clusterId[TSDB_CLUSTER_ID_LEN];
  char            file[PATH_MAX + 20];
  pthread_mutex_t mutex;
} tsDcfg;

static int32_t dnodeReadCfg() {
31 32 33 34 35 36
  int32_t len = 0;
  int32_t maxLen = 200;
  char *  content = calloc(1, maxLen + 1);
  cJSON * root = NULL;
  FILE *  fp = NULL;

37
  fp = fopen(tsDcfg.file, "r");
38
  if (!fp) {
39
    dDebug("file %s not exist", tsDcfg.file);
40 41 42 43 44
    goto PARSE_CFG_OVER;
  }

  len = (int32_t)fread(content, 1, maxLen, fp);
  if (len <= 0) {
45
    dError("failed to read %s since content is null", tsDcfg.file);
46 47 48 49 50 51
    goto PARSE_CFG_OVER;
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
52
    dError("failed to read %s since invalid json format", tsDcfg.file);
53 54 55 56 57
    goto PARSE_CFG_OVER;
  }

  cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId");
  if (!dnodeId || dnodeId->type != cJSON_Number) {
58
    dError("failed to read %s since dnodeId not found", tsDcfg.file);
59 60
    goto PARSE_CFG_OVER;
  }
61
  tsDcfg.dnodeId = (int32_t)dnodeId->valueint;
62 63 64

  cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
  if (!dropped || dropped->type != cJSON_Number) {
65
    dError("failed to read %s since dropped not found", tsDcfg.file);
66 67
    goto PARSE_CFG_OVER;
  }
68
  tsDcfg.dropped = (int32_t)dropped->valueint;
69 70 71

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

77
  dInfo("successed to read %s", tsDcfg.file);
78 79 80 81 82 83 84 85 86 87

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

  return 0;
}

88 89
static int32_t dnodeWriteCfg() {
  FILE *fp = fopen(tsDcfg.file, "w");
90
  if (!fp) {
91
    dError("failed to write %s since %s", tsDcfg.file, strerror(errno));
92 93 94 95 96 97 98 99
    return -1;
  }

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

  len += snprintf(content + len, maxLen - len, "{\n");
100 101 102
  len += snprintf(content + len, maxLen - len, "  \"dnodeId\": %d,\n", tsDcfg.dnodeId);
  len += snprintf(content + len, maxLen - len, "  \"dropped\": %d,\n", tsDcfg.dropped);
  len += snprintf(content + len, maxLen - len, "  \"clusterId\": \"%s\"\n", tsDcfg.clusterId);
103 104 105
  len += snprintf(content + len, maxLen - len, "}\n");

  fwrite(content, 1, len, fp);
S
Shengliang Guan 已提交
106
  taosFsyncFile(fileno(fp));
107 108 109 110
  fclose(fp);
  free(content);
  terrno = 0;

111
  dInfo("successed to write %s", tsDcfg.file);
112 113 114
  return 0;
}

115 116 117 118 119 120 121 122
int32_t dnodeInitCfg() {
  tsDcfg.dnodeId = 0;
  tsDcfg.dropped = 0;
  tsDcfg.clusterId[0] = 0;
  snprintf(tsDcfg.file, sizeof(tsDcfg.file), "%s/dnodeCfg.json", tsDnodeDir);
  pthread_mutex_init(&tsDcfg.mutex, NULL);
  
  int32_t ret = dnodeReadCfg();
123 124 125 126
  if (ret == 0) {
    dInfo("dnode cfg is initialized");
  }

127
  if (tsDcfg.dropped) {
128 129 130 131 132 133 134
    dInfo("dnode is dropped and start to exit");
    return -1;
  }

  return ret;
}

135 136
void dnodeCleanupCfg() {
  pthread_mutex_destroy(&tsDcfg.mutex);
137 138
}

139 140
void dnodeUpdateCfg(SDnodeCfg *data) {
  if (tsDcfg.dnodeId != 0) return;
141

142
  pthread_mutex_lock(&tsDcfg.mutex);
143

144 145 146
  tsDcfg.dnodeId = data->dnodeId;
  tstrncpy(tsDcfg.clusterId, data->clusterId, TSDB_CLUSTER_ID_LEN);
  dInfo("dnodeId is set to %d, clusterId is set to %s", data->dnodeId, data->clusterId);
147

148 149
  dnodeWriteCfg();
  pthread_mutex_unlock(&tsDcfg.mutex);
150 151
}

152 153 154 155 156
void dnodeSetDropped() {
  pthread_mutex_lock(&tsDcfg.mutex);
  tsDcfg.dropped = 1;
  dnodeWriteCfg();
  pthread_mutex_unlock(&tsDcfg.mutex);
157 158
}

159
int32_t dnodeGetDnodeId() {
160
  int32_t dnodeId = 0;
161 162 163
  pthread_mutex_lock(&tsDcfg.mutex);
  dnodeId = tsDcfg.dnodeId;
  pthread_mutex_unlock(&tsDcfg.mutex);
164 165 166
  return dnodeId;
}

167 168 169 170
void dnodeGetClusterId(char *clusterId) {
  pthread_mutex_lock(&tsDcfg.mutex);
  tstrncpy(clusterId, tsDcfg.clusterId, TSDB_CLUSTER_ID_LEN);
  pthread_mutex_unlock(&tsDcfg.mutex);
171 172
}

173 174 175 176 177
void dnodeGetCfg(int32_t *dnodeId, char *clusterId) {
  pthread_mutex_lock(&tsDcfg.mutex);
  *dnodeId = tsDcfg.dnodeId;
  tstrncpy(clusterId, tsDcfg.clusterId, TSDB_CLUSTER_ID_LEN);
  pthread_mutex_unlock(&tsDcfg.mutex);
178
}