vnodeCfg.c 11.2 KB
Newer Older
H
save  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

H
Hongze Cheng 已提交
16
#include "vnd.h"
H
save  
Hongze Cheng 已提交
17

L
Liu Jicong 已提交
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
const SVnodeCfg vnodeCfgDefault = {.vgId = -1,
                                   .dbname = "",
                                   .dbId = 0,
                                   .szPage = 4096,
                                   .szCache = 256,
                                   .szBuf = 96 * 1024 * 1024,
                                   .isHeap = false,
                                   .isWeak = 0,
                                   .tsdbCfg = {.precision = TSDB_TIME_PRECISION_MILLI,
                                               .update = 1,
                                               .compression = 2,
                                               .slLevel = 5,
                                               .days = 14400,
                                               .minRows = 100,
                                               .maxRows = 4096,
                                               .keep2 = 5256000,
                                               .keep0 = 5256000,
                                               .keep1 = 5256000},
                                   .walCfg =
                                       {
                                           .vgId = -1,
                                           .fsyncPeriod = 0,
                                           .retentionPeriod = -1,
                                           .rollPeriod = -1,
                                           .segSize = -1,
                                           .retentionSize = -1,
                                           .level = TAOS_WAL_WRITE,
                                       },
                                   .hashBegin = 0,
                                   .hashEnd = 0,
                                   .hashMethod = 0};
H
Hongze Cheng 已提交
49 50 51 52 53 54

int vnodeCheckCfg(const SVnodeCfg *pCfg) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
55 56 57 58 59 60
int vnodeEncodeConfig(const void *pObj, SJson *pJson) {
  const SVnodeCfg *pCfg = (SVnodeCfg *)pObj;

  if (tjsonAddIntegerToObject(pJson, "vgId", pCfg->vgId) < 0) return -1;
  if (tjsonAddStringToObject(pJson, "dbname", pCfg->dbname) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "dbId", pCfg->dbId) < 0) return -1;
H
Hongze Cheng 已提交
61 62
  if (tjsonAddIntegerToObject(pJson, "szPage", pCfg->szPage) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "szCache", pCfg->szCache) < 0) return -1;
H
Hongze Cheng 已提交
63 64
  if (tjsonAddIntegerToObject(pJson, "szBuf", pCfg->szBuf) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "isHeap", pCfg->isHeap) < 0) return -1;
H
Hongze Cheng 已提交
65
  if (tjsonAddIntegerToObject(pJson, "isWeak", pCfg->isWeak) < 0) return -1;
C
Cary Xu 已提交
66
  if (tjsonAddIntegerToObject(pJson, "isTsma", pCfg->isTsma) < 0) return -1;
C
Cary Xu 已提交
67
  if (tjsonAddIntegerToObject(pJson, "isRsma", pCfg->isRsma) < 0) return -1;
H
Hongze Cheng 已提交
68 69 70
  if (tjsonAddIntegerToObject(pJson, "precision", pCfg->tsdbCfg.precision) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "update", pCfg->tsdbCfg.update) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "compression", pCfg->tsdbCfg.compression) < 0) return -1;
H
Hongze Cheng 已提交
71
  if (tjsonAddIntegerToObject(pJson, "slLevel", pCfg->tsdbCfg.slLevel) < 0) return -1;
H
Hongze Cheng 已提交
72 73 74 75 76 77
  if (tjsonAddIntegerToObject(pJson, "daysPerFile", pCfg->tsdbCfg.days) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "minRows", pCfg->tsdbCfg.minRows) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "maxRows", pCfg->tsdbCfg.maxRows) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "keep0", pCfg->tsdbCfg.keep0) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "keep1", pCfg->tsdbCfg.keep1) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "keep2", pCfg->tsdbCfg.keep2) < 0) return -1;
C
Cary Xu 已提交
78 79 80 81 82 83 84 85 86 87 88
  if (pCfg->tsdbCfg.retentions[0].freq > 0) {
    int32_t nRetention = 1;
    if (pCfg->tsdbCfg.retentions[1].freq > 0) {
      ++nRetention;
      if (pCfg->tsdbCfg.retentions[2].freq > 0) {
        ++nRetention;
      }
    }
    SJson *pNodeRetentions = tjsonCreateArray();
    tjsonAddItemToObject(pJson, "retentions", pNodeRetentions);
    for (int32_t i = 0; i < nRetention; ++i) {
L
Liu Jicong 已提交
89
      SJson            *pNodeRetention = tjsonCreateObject();
C
Cary Xu 已提交
90 91 92 93 94 95 96 97
      const SRetention *pRetention = pCfg->tsdbCfg.retentions + i;
      tjsonAddIntegerToObject(pNodeRetention, "freq", pRetention->freq);
      tjsonAddIntegerToObject(pNodeRetention, "freqUnit", pRetention->freqUnit);
      tjsonAddIntegerToObject(pNodeRetention, "keep", pRetention->keep);
      tjsonAddIntegerToObject(pNodeRetention, "keepUnit", pRetention->keepUnit);
      tjsonAddItemToArray(pNodeRetentions, pNodeRetention);
    }
  }
H
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106 107 108
  if (tjsonAddIntegerToObject(pJson, "wal.vgId", pCfg->walCfg.vgId) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "wal.fsyncPeriod", pCfg->walCfg.fsyncPeriod) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "wal.retentionPeriod", pCfg->walCfg.retentionPeriod) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "wal.rollPeriod", pCfg->walCfg.rollPeriod) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "wal.retentionSize", pCfg->walCfg.retentionSize) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "wal.segSize", pCfg->walCfg.segSize) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "wal.level", pCfg->walCfg.level) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "hashBegin", pCfg->hashBegin) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "hashEnd", pCfg->hashEnd) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "hashMethod", pCfg->hashMethod) < 0) return -1;

M
Minghao Li 已提交
109 110 111 112 113 114 115 116 117 118 119
  if (tjsonAddIntegerToObject(pJson, "syncCfg.replicaNum", pCfg->syncCfg.replicaNum) < 0) return -1;
  if (tjsonAddIntegerToObject(pJson, "syncCfg.myIndex", pCfg->syncCfg.myIndex) < 0) return -1;
  SJson *pNodeInfoArr = tjsonCreateArray();
  tjsonAddItemToObject(pJson, "syncCfg.nodeInfo", pNodeInfoArr);
  for (int i = 0; i < pCfg->syncCfg.replicaNum; ++i) {
    SJson *pNodeInfo = tjsonCreateObject();
    tjsonAddIntegerToObject(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort);
    tjsonAddStringToObject(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn);
    tjsonAddItemToArray(pNodeInfoArr, pNodeInfo);
  }

H
Hongze Cheng 已提交
120 121 122 123 124 125
  return 0;
}

int vnodeDecodeConfig(const SJson *pJson, void *pObj) {
  SVnodeCfg *pCfg = (SVnodeCfg *)pObj;

126 127
  int32_t code;
  tjsonGetNumberValue(pJson, "vgId", pCfg->vgId, code);
dengyihao's avatar
dengyihao 已提交
128
  if (code < 0) return -1;
H
Hongze Cheng 已提交
129
  if (tjsonGetStringValue(pJson, "dbname", pCfg->dbname) < 0) return -1;
130
  tjsonGetNumberValue(pJson, "dbId", pCfg->dbId, code);
dengyihao's avatar
dengyihao 已提交
131
  if (code < 0) return -1;
132
  tjsonGetNumberValue(pJson, "szPage", pCfg->szPage, code);
dengyihao's avatar
dengyihao 已提交
133
  if (code < 0) return -1;
134
  tjsonGetNumberValue(pJson, "szCache", pCfg->szCache, code);
dengyihao's avatar
dengyihao 已提交
135
  if (code < 0) return -1;
136
  tjsonGetNumberValue(pJson, "szBuf", pCfg->szBuf, code);
dengyihao's avatar
dengyihao 已提交
137
  if (code < 0) return -1;
138
  tjsonGetNumberValue(pJson, "isHeap", pCfg->isHeap, code);
dengyihao's avatar
dengyihao 已提交
139
  if (code < 0) return -1;
140
  tjsonGetNumberValue(pJson, "isWeak", pCfg->isWeak, code);
dengyihao's avatar
dengyihao 已提交
141
  if (code < 0) return -1;
C
Cary Xu 已提交
142
  tjsonGetNumberValue(pJson, "isTsma", pCfg->isTsma, code);
dengyihao's avatar
dengyihao 已提交
143
  if (code < 0) return -1;
C
Cary Xu 已提交
144
  tjsonGetNumberValue(pJson, "isRsma", pCfg->isRsma, code);
dengyihao's avatar
dengyihao 已提交
145
  if (code < 0) return -1;
146
  tjsonGetNumberValue(pJson, "precision", pCfg->tsdbCfg.precision, code);
dengyihao's avatar
dengyihao 已提交
147
  if (code < 0) return -1;
148
  tjsonGetNumberValue(pJson, "update", pCfg->tsdbCfg.update, code);
dengyihao's avatar
dengyihao 已提交
149
  if (code < 0) return -1;
150
  tjsonGetNumberValue(pJson, "compression", pCfg->tsdbCfg.compression, code);
dengyihao's avatar
dengyihao 已提交
151
  if (code < 0) return -1;
152
  tjsonGetNumberValue(pJson, "slLevel", pCfg->tsdbCfg.slLevel, code);
dengyihao's avatar
dengyihao 已提交
153
  if (code < 0) return -1;
154
  tjsonGetNumberValue(pJson, "daysPerFile", pCfg->tsdbCfg.days, code);
dengyihao's avatar
dengyihao 已提交
155
  if (code < 0) return -1;
156
  tjsonGetNumberValue(pJson, "minRows", pCfg->tsdbCfg.minRows, code);
dengyihao's avatar
dengyihao 已提交
157
  if (code < 0) return -1;
158
  tjsonGetNumberValue(pJson, "maxRows", pCfg->tsdbCfg.maxRows, code);
dengyihao's avatar
dengyihao 已提交
159
  if (code < 0) return -1;
160
  tjsonGetNumberValue(pJson, "keep0", pCfg->tsdbCfg.keep0, code);
dengyihao's avatar
dengyihao 已提交
161
  if (code < 0) return -1;
162
  tjsonGetNumberValue(pJson, "keep1", pCfg->tsdbCfg.keep1, code);
dengyihao's avatar
dengyihao 已提交
163
  if (code < 0) return -1;
164
  tjsonGetNumberValue(pJson, "keep2", pCfg->tsdbCfg.keep2, code);
dengyihao's avatar
dengyihao 已提交
165
  if (code < 0) return -1;
L
Liu Jicong 已提交
166
  SJson  *pNodeRetentions = tjsonGetObjectItem(pJson, "retentions");
C
Cary Xu 已提交
167 168 169 170
  int32_t nRetention = tjsonGetArraySize(pNodeRetentions);
  if (nRetention > TSDB_RETENTION_MAX) {
    nRetention = TSDB_RETENTION_MAX;
  }
C
Cary Xu 已提交
171 172 173
  for (int32_t i = 0; i < nRetention; ++i) {
    SJson *pNodeRetention = tjsonGetArrayItem(pNodeRetentions, i);
    ASSERT(pNodeRetention != NULL);
174 175 176 177
    tjsonGetNumberValue(pNodeRetention, "freq", (pCfg->tsdbCfg.retentions)[i].freq, code);
    tjsonGetNumberValue(pNodeRetention, "freqUnit", (pCfg->tsdbCfg.retentions)[i].freqUnit, code);
    tjsonGetNumberValue(pNodeRetention, "keep", (pCfg->tsdbCfg.retentions)[i].keep, code);
    tjsonGetNumberValue(pNodeRetention, "keepUnit", (pCfg->tsdbCfg.retentions)[i].keepUnit, code);
C
Cary Xu 已提交
178
  }
179
  tjsonGetNumberValue(pJson, "wal.vgId", pCfg->walCfg.vgId, code);
dengyihao's avatar
dengyihao 已提交
180
  if (code < 0) return -1;
181
  tjsonGetNumberValue(pJson, "wal.fsyncPeriod", pCfg->walCfg.fsyncPeriod, code);
dengyihao's avatar
dengyihao 已提交
182
  if (code < 0) return -1;
183
  tjsonGetNumberValue(pJson, "wal.retentionPeriod", pCfg->walCfg.retentionPeriod, code);
dengyihao's avatar
dengyihao 已提交
184
  if (code < 0) return -1;
185
  tjsonGetNumberValue(pJson, "wal.rollPeriod", pCfg->walCfg.rollPeriod, code);
dengyihao's avatar
dengyihao 已提交
186
  if (code < 0) return -1;
187
  tjsonGetNumberValue(pJson, "wal.retentionSize", pCfg->walCfg.retentionSize, code);
dengyihao's avatar
dengyihao 已提交
188
  if (code < 0) return -1;
189
  tjsonGetNumberValue(pJson, "wal.segSize", pCfg->walCfg.segSize, code);
dengyihao's avatar
dengyihao 已提交
190
  if (code < 0) return -1;
191
  tjsonGetNumberValue(pJson, "wal.level", pCfg->walCfg.level, code);
dengyihao's avatar
dengyihao 已提交
192
  if (code < 0) return -1;
193
  tjsonGetNumberValue(pJson, "hashBegin", pCfg->hashBegin, code);
dengyihao's avatar
dengyihao 已提交
194
  if (code < 0) return -1;
195
  tjsonGetNumberValue(pJson, "hashEnd", pCfg->hashEnd, code);
dengyihao's avatar
dengyihao 已提交
196
  if (code < 0) return -1;
197
  tjsonGetNumberValue(pJson, "hashMethod", pCfg->hashMethod, code);
dengyihao's avatar
dengyihao 已提交
198
  if (code < 0) return -1;
199 200

  tjsonGetNumberValue(pJson, "syncCfg.replicaNum", pCfg->syncCfg.replicaNum, code);
dengyihao's avatar
dengyihao 已提交
201
  if (code < 0) return -1;
202
  tjsonGetNumberValue(pJson, "syncCfg.myIndex", pCfg->syncCfg.myIndex, code);
dengyihao's avatar
dengyihao 已提交
203
  if (code < 0) return -1;
M
Minghao Li 已提交
204 205 206 207 208 209

  SJson *pNodeInfoArr = tjsonGetObjectItem(pJson, "syncCfg.nodeInfo");
  int    arraySize = tjsonGetArraySize(pNodeInfoArr);
  assert(arraySize == pCfg->syncCfg.replicaNum);

  for (int i = 0; i < arraySize; ++i) {
210
    SJson *pNodeInfo = tjsonGetArrayItem(pNodeInfoArr, i);
M
Minghao Li 已提交
211
    assert(pNodeInfo != NULL);
212
    tjsonGetNumberValue(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort, code);
M
Minghao Li 已提交
213 214 215
    tjsonGetStringValue(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn);
  }

H
Hongze Cheng 已提交
216 217 218
  return 0;
}

H
Hongze Cheng 已提交
219
int vnodeValidateTableHash(SVnode *pVnode, char *tableFName) {
D
dapan1121 已提交
220
  uint32_t hashValue = 0;
H
Hongze Cheng 已提交
221

H
Hongze Cheng 已提交
222
  switch (pVnode->config.hashMethod) {
D
dapan1121 已提交
223
    default:
D
dapan1121 已提交
224
      hashValue = MurmurHash3_32(tableFName, strlen(tableFName));
D
dapan1121 已提交
225 226 227
      break;
  }

D
dapan1121 已提交
228
  if (hashValue < pVnode->config.hashBegin || hashValue > pVnode->config.hashEnd) {
D
dapan1121 已提交
229 230 231 232
    terrno = TSDB_CODE_VND_HASH_MISMATCH;
    return TSDB_CODE_VND_HASH_MISMATCH;
  }

H
Hongze Cheng 已提交
233
  return 0;
D
dapan1121 已提交
234
}