mndDump.c 25.5 KB
Newer Older
S
Shengliang Guan 已提交
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
/*
 * 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 "mndDb.h"
#include "mndInt.h"
#include "mndStb.h"
#include "sdb.h"
#include "tconfig.h"
#include "tjson.h"
#include "ttypes.h"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"

void reportStartup(const char *name, const char *desc) {}
void sendRsp(SRpcMsg *pMsg) { rpcFreeCont(pMsg->pCont); }

int32_t sendReq(const SEpSet *pEpSet, SRpcMsg *pMsg) {
  terrno = TSDB_CODE_INVALID_PTR;
  return -1;
}

char *i642str(int64_t val) {
  static char str[24] = {0};
  snprintf(str, sizeof(str), "%" PRId64, val);
  return str;
}

void dumpFunc(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "funcs");

  while (1) {
    SFuncObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_FUNC, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "name", pObj->name);
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
S
Shengliang Guan 已提交
55 56 57 58 59 60
    tjsonAddStringToObject(item, "funcType", i642str(pObj->funcType));
    tjsonAddStringToObject(item, "scriptType", i642str(pObj->scriptType));
    tjsonAddStringToObject(item, "align", i642str(pObj->align));
    tjsonAddStringToObject(item, "outputType", i642str(pObj->outputType));
    tjsonAddStringToObject(item, "outputLen", i642str(pObj->outputLen));
    tjsonAddStringToObject(item, "bufSize", i642str(pObj->bufSize));
S
Shengliang Guan 已提交
61
    tjsonAddStringToObject(item, "signature", i642str(pObj->signature));
S
Shengliang Guan 已提交
62 63
    tjsonAddStringToObject(item, "commentSize", i642str(pObj->commentSize));
    tjsonAddStringToObject(item, "codeSize", i642str(pObj->codeSize));
S
Shengliang Guan 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    sdbRelease(pSdb, pObj);
  }
}

void dumpDb(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonCreateObject();
  tjsonAddItemToObject(json, "dbs", items);

  while (1) {
    SDbObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToObject(items, "db", item);

    tjsonAddStringToObject(item, "name", mndGetDbStr(pObj->name));
    tjsonAddStringToObject(item, "acct", pObj->acct);
    tjsonAddStringToObject(item, "createUser", pObj->createUser);
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
    tjsonAddStringToObject(item, "uid", i642str(pObj->uid));
S
Shengliang Guan 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    tjsonAddStringToObject(item, "cfgVersion", i642str(pObj->cfgVersion));
    tjsonAddStringToObject(item, "vgVersion", i642str(pObj->vgVersion));
    tjsonAddStringToObject(item, "numOfVgroups", i642str(pObj->cfg.numOfVgroups));
    tjsonAddStringToObject(item, "numOfStables", i642str(pObj->cfg.numOfStables));
    tjsonAddStringToObject(item, "buffer", i642str(pObj->cfg.buffer));
    tjsonAddStringToObject(item, "pageSize", i642str(pObj->cfg.pageSize));
    tjsonAddStringToObject(item, "pages", i642str(pObj->cfg.pages));
    tjsonAddStringToObject(item, "cacheLastSize", i642str(pObj->cfg.cacheLastSize));
    tjsonAddStringToObject(item, "daysPerFile", i642str(pObj->cfg.daysPerFile));
    tjsonAddStringToObject(item, "daysToKeep0", i642str(pObj->cfg.daysToKeep0));
    tjsonAddStringToObject(item, "daysToKeep1", i642str(pObj->cfg.daysToKeep1));
    tjsonAddStringToObject(item, "daysToKeep2", i642str(pObj->cfg.daysToKeep2));
    tjsonAddStringToObject(item, "minRows", i642str(pObj->cfg.minRows));
    tjsonAddStringToObject(item, "maxRows", i642str(pObj->cfg.maxRows));
    tjsonAddStringToObject(item, "precision", i642str(pObj->cfg.precision));
    tjsonAddStringToObject(item, "compression", i642str(pObj->cfg.compression));
    tjsonAddStringToObject(item, "replications", i642str(pObj->cfg.replications));
    tjsonAddStringToObject(item, "strict", i642str(pObj->cfg.strict));
    tjsonAddStringToObject(item, "cacheLast",i642str( pObj->cfg.cacheLast));
    tjsonAddStringToObject(item, "hashMethod", i642str(pObj->cfg.hashMethod));
    tjsonAddStringToObject(item, "hashPrefix", i642str(pObj->cfg.hashPrefix));
    tjsonAddStringToObject(item, "hashSuffix", i642str(pObj->cfg.hashSuffix));
    tjsonAddStringToObject(item, "sstTrigger", i642str(pObj->cfg.sstTrigger));
    tjsonAddStringToObject(item, "tsdbPageSize",i642str( pObj->cfg.tsdbPageSize));
    tjsonAddStringToObject(item, "schemaless", i642str(pObj->cfg.schemaless));
    tjsonAddStringToObject(item, "walLevel",i642str( pObj->cfg.walLevel));
    tjsonAddStringToObject(item, "walFsyncPeriod", i642str(pObj->cfg.walFsyncPeriod));
    tjsonAddStringToObject(item, "walRetentionPeriod", i642str(pObj->cfg.walRetentionPeriod));
    tjsonAddStringToObject(item, "walRetentionSize",i642str( pObj->cfg.walRetentionSize));
    tjsonAddStringToObject(item, "walRollPeriod", i642str(pObj->cfg.walRollPeriod));
    tjsonAddStringToObject(item, "walSegmentSize", i642str(pObj->cfg.walSegmentSize));

    tjsonAddStringToObject(item, "numOfRetensions",i642str( pObj->cfg.numOfRetensions));
S
Shengliang Guan 已提交
120 121 122 123 124 125 126
    for (int32_t i = 0; i < pObj->cfg.numOfRetensions; ++i) {
      SJson *rentensions = tjsonAddArrayToObject(item, "rentensions");
      SJson *rentension = tjsonCreateObject();
      tjsonAddItemToArray(rentensions, rentension);

      SRetention *pRetension = taosArrayGet(pObj->cfg.pRetensions, i);
      tjsonAddStringToObject(item, "freq", i642str(pRetension->freq));
S
Shengliang Guan 已提交
127
      tjsonAddStringToObject(item, "freqUnit", i642str(pRetension->freqUnit));
S
Shengliang Guan 已提交
128
      tjsonAddStringToObject(item, "keep", i642str(pRetension->keep));
S
Shengliang Guan 已提交
129
      tjsonAddStringToObject(item, "keepUnit",i642str( pRetension->keepUnit));
S
Shengliang Guan 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }

    sdbRelease(pSdb, pObj);
  }
}

void dumpStb(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "stbs");

  while (1) {
    SStbObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "name", mndGetStbStr(pObj->name));
    tjsonAddStringToObject(item, "db", mndGetDbStr(pObj->db));
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
    tjsonAddStringToObject(item, "uid", i642str(pObj->uid));
    tjsonAddStringToObject(item, "dbUid", i642str(pObj->dbUid));
S
Shengliang Guan 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    tjsonAddStringToObject(item, "tagVer",i642str( pObj->tagVer));
    tjsonAddStringToObject(item, "colVer", i642str(pObj->colVer));
    tjsonAddStringToObject(item, "smaVer", i642str(pObj->smaVer));
    tjsonAddStringToObject(item, "nextColId", i642str(pObj->nextColId));
    tjsonAddStringToObject(item, "watermark1", i642str(pObj->watermark[0]));
    tjsonAddStringToObject(item, "watermark2", i642str(pObj->watermark[1]));
    tjsonAddStringToObject(item, "maxdelay0",i642str( pObj->maxdelay[0]));
    tjsonAddStringToObject(item, "maxdelay1",i642str( pObj->maxdelay[1]));
    tjsonAddStringToObject(item, "ttl",i642str( pObj->ttl));
    tjsonAddStringToObject(item, "numOfFuncs",i642str( pObj->numOfFuncs));
    tjsonAddStringToObject(item, "commentLen", i642str(pObj->commentLen));
    tjsonAddStringToObject(item, "ast1Len", i642str(pObj->ast1Len));
    tjsonAddStringToObject(item, "ast2Len",i642str( pObj->ast2Len));

    tjsonAddStringToObject(item, "numOfColumns",i642str( pObj->numOfColumns));
S
Shengliang Guan 已提交
168 169 170 171 172 173
    SJson *columns = tjsonAddArrayToObject(item, "columns");
    for (int32_t i = 0; i < pObj->numOfColumns; ++i) {
      SJson *column = tjsonCreateObject();
      tjsonAddItemToArray(columns, column);

      SSchema *pColumn = &pObj->pColumns[i];
S
Shengliang Guan 已提交
174
      tjsonAddStringToObject(column, "type", i642str(pColumn->type));
S
Shengliang Guan 已提交
175
      tjsonAddStringToObject(column, "typestr", tDataTypes[pColumn->type].name);
S
Shengliang Guan 已提交
176 177 178
      tjsonAddStringToObject(column, "flags", i642str(pColumn->flags));
      tjsonAddStringToObject(column, "colId", i642str(pColumn->colId));
      tjsonAddStringToObject(column, "bytes", i642str(pColumn->bytes));
S
Shengliang Guan 已提交
179 180 181
      tjsonAddStringToObject(column, "name", pColumn->name);
    }

S
Shengliang Guan 已提交
182
    tjsonAddStringToObject(item, "numOfTags", i642str(pObj->numOfTags));
S
Shengliang Guan 已提交
183 184 185 186 187 188
    SJson *tags = tjsonAddArrayToObject(item, "tags");
    for (int32_t i = 0; i < pObj->numOfTags; ++i) {
      SJson *tag = tjsonCreateObject();
      tjsonAddItemToArray(tags, tag);

      SSchema *pTag = &pObj->pTags[i];
S
Shengliang Guan 已提交
189
      tjsonAddStringToObject(tag, "type", i642str(pTag->type));
S
Shengliang Guan 已提交
190
      tjsonAddStringToObject(tag, "typestr", tDataTypes[pTag->type].name);
S
Shengliang Guan 已提交
191 192 193
      tjsonAddStringToObject(tag, "flags",i642str( pTag->flags));
      tjsonAddStringToObject(tag, "colId", i642str(pTag->colId));
      tjsonAddStringToObject(tag, "bytes", i642str(pTag->bytes));
S
Shengliang Guan 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
      tjsonAddStringToObject(tag, "name", pTag->name);
    }

    sdbRelease(pSdb, pObj);
  }
}

void dumpSma(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "smas");

  while (1) {
    SSmaObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "name", mndGetStbStr(pObj->name));
    tjsonAddStringToObject(item, "stb", mndGetStbStr(pObj->stb));
    tjsonAddStringToObject(item, "db", mndGetDbStr(pObj->db));
    tjsonAddStringToObject(item, "dstTbName", mndGetStbStr(pObj->dstTbName));
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "uid", i642str(pObj->uid));
    tjsonAddStringToObject(item, "stbUid", i642str(pObj->stbUid));
    tjsonAddStringToObject(item, "dbUid", i642str(pObj->dbUid));
    tjsonAddStringToObject(item, "dstTbUid", i642str(pObj->dstTbUid));
S
Shengliang Guan 已提交
221 222 223 224
    tjsonAddStringToObject(item, "intervalUnit", i642str(pObj->intervalUnit));
    tjsonAddStringToObject(item, "slidingUnit",i642str( pObj->slidingUnit));
    tjsonAddStringToObject(item, "timezone", i642str(pObj->timezone));
    tjsonAddStringToObject(item, "dstVgId",i642str( pObj->dstVgId));
S
Shengliang Guan 已提交
225 226 227
    tjsonAddStringToObject(item, "interval", i642str(pObj->interval));
    tjsonAddStringToObject(item, "offset", i642str(pObj->offset));
    tjsonAddStringToObject(item, "sliding", i642str(pObj->sliding));
S
Shengliang Guan 已提交
228 229 230 231
    tjsonAddStringToObject(item, "exprLen",i642str( pObj->exprLen));
    tjsonAddStringToObject(item, "tagsFilterLen", i642str(pObj->tagsFilterLen));
    tjsonAddStringToObject(item, "sqlLen",i642str( pObj->sqlLen));
    tjsonAddStringToObject(item, "astLen",i642str( pObj->astLen));
S
Shengliang Guan 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    sdbRelease(pSdb, pObj);
  }
}

void dumpVgroup(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "vgroups");

  while (1) {
    SVgObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
S
Shengliang Guan 已提交
247
    tjsonAddStringToObject(item, "vgId", i642str(pObj->vgId));
S
Shengliang Guan 已提交
248 249
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
S
Shengliang Guan 已提交
250 251 252
    tjsonAddStringToObject(item, "version",i642str(pObj->version));
    tjsonAddStringToObject(item, "hashBegin", i642str(pObj->hashBegin));
    tjsonAddStringToObject(item, "hashEnd", i642str(pObj->hashEnd));
S
Shengliang Guan 已提交
253 254
    tjsonAddStringToObject(item, "db", mndGetDbStr(pObj->dbName));
    tjsonAddStringToObject(item, "dbUid", i642str(pObj->dbUid));
S
Shengliang Guan 已提交
255 256
    tjsonAddStringToObject(item, "isTsma", i642str(pObj->isTsma));
    tjsonAddStringToObject(item, "replica",i642str( pObj->replica));
S
Shengliang Guan 已提交
257 258 259 260
    for (int32_t i = 0; i < pObj->replica; ++i) {
      SJson *replicas = tjsonAddArrayToObject(item, "replicas");
      SJson *replica = tjsonCreateObject();
      tjsonAddItemToArray(replicas, replica);
S
Shengliang Guan 已提交
261
      tjsonAddStringToObject(replica, "dnodeId", i642str(pObj->vnodeGid[i].dnodeId));
S
Shengliang Guan 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    }
    sdbRelease(pSdb, pObj);
  }
}

void dumpTopic(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "topics");

  while (1) {
    SMqTopicObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_TOPIC, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "name", mndGetDbStr(pObj->name));
    tjsonAddStringToObject(item, "name", mndGetDbStr(pObj->db));
    tjsonAddStringToObject(item, "createTime", i642str(pObj->createTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
    tjsonAddStringToObject(item, "uid", i642str(pObj->uid));
    tjsonAddStringToObject(item, "dbUid", i642str(pObj->dbUid));
S
Shengliang Guan 已提交
284 285 286
    tjsonAddStringToObject(item, "version",i642str( pObj->version));
    tjsonAddStringToObject(item, "subType",i642str( pObj->subType));
    tjsonAddStringToObject(item, "withMeta", i642str(pObj->withMeta));
S
Shengliang Guan 已提交
287
    tjsonAddStringToObject(item, "stbUid", i642str(pObj->stbUid));
S
Shengliang Guan 已提交
288 289 290
    tjsonAddStringToObject(item, "sqlLen", i642str(pObj->sqlLen));
    tjsonAddStringToObject(item, "astLen",i642str( pObj->astLen));
    tjsonAddStringToObject(item, "sqlLen",i642str( pObj->sqlLen));
S
Shengliang Guan 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    tjsonAddStringToObject(item, "ntbUid", i642str(pObj->ntbUid));
    tjsonAddStringToObject(item, "ctbStbUid", i642str(pObj->ctbStbUid));
    sdbRelease(pSdb, pObj);
  }
}

void dumpConsumer(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "consumers");

  while (1) {
    SMqConsumerObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_CONSUMER, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "consumerId", i642str(pObj->consumerId));
    tjsonAddStringToObject(item, "cgroup", pObj->cgroup);
    sdbRelease(pSdb, pObj);
  }
}

void dumpSubscribe(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "subscribes");

  while (1) {
    SMqSubscribeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_SUBSCRIBE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "key", pObj->key);
    tjsonAddStringToObject(item, "dbUid", i642str(pObj->dbUid));
    tjsonAddStringToObject(item, "stbUid", i642str(pObj->stbUid));
    sdbRelease(pSdb, pObj);
  }
}

void dumpOffset(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "offsets");

  while (1) {
    SMqOffsetObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_OFFSET, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "key", pObj->key);
    tjsonAddStringToObject(item, "dbUid", i642str(pObj->dbUid));
    tjsonAddStringToObject(item, "offset", i642str(pObj->offset));
    sdbRelease(pSdb, pObj);
  }
}

void dumpStream(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "streams");

  while (1) {
    SStreamObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "name", mndGetDbStr(pObj->name));
    tjsonAddStringToObject(item, "createTime", i642str(pObj->createTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
S
Shengliang Guan 已提交
364 365
    tjsonAddStringToObject(item, "version", i642str(pObj->version));
    tjsonAddStringToObject(item, "totalLevel", i642str(pObj->totalLevel));
S
Shengliang Guan 已提交
366 367
    tjsonAddStringToObject(item, "smaId", i642str(pObj->smaId));
    tjsonAddStringToObject(item, "uid", i642str(pObj->uid));
S
Shengliang Guan 已提交
368 369 370
    tjsonAddStringToObject(item, "status",i642str( pObj->status));
    tjsonAddStringToObject(item, "igExpired",i642str( pObj->igExpired));
    tjsonAddStringToObject(item, "trigger",i642str( pObj->trigger));
S
Shengliang Guan 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    tjsonAddStringToObject(item, "triggerParam", i642str(pObj->triggerParam));
    tjsonAddStringToObject(item, "watermark", i642str(pObj->watermark));
    tjsonAddStringToObject(item, "sourceDbUid", i642str(pObj->sourceDbUid));
    tjsonAddStringToObject(item, "targetDbUid", i642str(pObj->targetDbUid));
    tjsonAddStringToObject(item, "sourceDb", mndGetDbStr(pObj->sourceDb));
    tjsonAddStringToObject(item, "targetDb", mndGetDbStr(pObj->targetDb));
    tjsonAddStringToObject(item, "targetSTbName", mndGetStbStr(pObj->targetSTbName));
    tjsonAddStringToObject(item, "targetStbUid", i642str(pObj->targetStbUid));
    tjsonAddStringToObject(item, "fixedSinkVgId", i642str(pObj->fixedSinkVgId));
    sdbRelease(pSdb, pObj);
  }
}

void dumpAcct(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "accts");

  while (1) {
    SAcctObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_ACCT, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "acct", pObj->acct);
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
S
Shengliang Guan 已提交
398
    tjsonAddStringToObject(item, "acctId", i642str(pObj->acctId));
S
Shengliang Guan 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    sdbRelease(pSdb, pObj);
  }
}

void dumpAuth(SSdb *pSdb, SJson *json) {
  // todo
}

void dumpUser(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "users");

  while (1) {
    SUserObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "name", pObj->user);
    tjsonAddStringToObject(item, "acct", pObj->acct);
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
S
Shengliang Guan 已提交
422 423 424 425
    tjsonAddStringToObject(item, "superUser",i642str( pObj->superUser));
    tjsonAddStringToObject(item, "authVersion", i642str(pObj->authVersion));
    tjsonAddStringToObject(item, "numOfReadDbs",i642str( taosHashGetSize(pObj->readDbs)));
    tjsonAddStringToObject(item, "numOfWriteDbs", i642str(taosHashGetSize(pObj->writeDbs)));
S
Shengliang Guan 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    sdbRelease(pSdb, pObj);
  }
}

void dumpDnode(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "dnodes");

  while (1) {
    SDnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
S
Shengliang Guan 已提交
441
    tjsonAddStringToObject(item, "id",i642str( pObj->id));
S
Shengliang Guan 已提交
442 443
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
S
Shengliang Guan 已提交
444
    tjsonAddStringToObject(item, "port",i642str( pObj->port));
S
Shengliang Guan 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    tjsonAddStringToObject(item, "fqdn", pObj->fqdn);
    sdbRelease(pSdb, pObj);
  }
}

void dumpBnode(SSdb *pSdb, SJson *json) {
  // not implemented yet
}

void dumpSnode(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "snodes");

  while (1) {
    SSnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_QNODE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
S
Shengliang Guan 已提交
465
    tjsonAddStringToObject(item, "id",i642str( pObj->id));
S
Shengliang Guan 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
    sdbRelease(pSdb, pObj);
  }
}

void dumpQnode(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "qnodes");

  while (1) {
    SQnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_QNODE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
S
Shengliang Guan 已提交
483
    tjsonAddStringToObject(item, "id", i642str(pObj->id));
S
Shengliang Guan 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
    sdbRelease(pSdb, pObj);
  }
}

void dumpMnode(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "mnodes");

  while (1) {
    SMnodeObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_MNODE, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
S
Shengliang Guan 已提交
501
    tjsonAddStringToObject(item, "id", i642str(pObj->id));
S
Shengliang Guan 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
    sdbRelease(pSdb, pObj);
  }
}

void dumpCluster(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "clusters");

  while (1) {
    SClusterObj *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_CLUSTER, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
    tjsonAddStringToObject(item, "id", i642str(pObj->id));
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
    tjsonAddStringToObject(item, "name", pObj->name);
    sdbRelease(pSdb, pObj);
  }
}

void dumpTrans(SSdb *pSdb, SJson *json) {
  void  *pIter = NULL;
  SJson *items = tjsonAddArrayToObject(json, "transactions");

  while (1) {
    STrans *pObj = NULL;
    pIter = sdbFetch(pSdb, SDB_TRANS, pIter, (void **)&pObj);
    if (pIter == NULL) break;

    SJson *item = tjsonCreateObject();
    tjsonAddItemToArray(items, item);
S
Shengliang Guan 已提交
538 539 540 541 542 543
    tjsonAddStringToObject(item, "id", i642str(pObj->id));
    tjsonAddStringToObject(item, "stage", i642str(pObj->stage));
    tjsonAddStringToObject(item, "policy", i642str(pObj->policy));
    tjsonAddStringToObject(item, "conflict",i642str( pObj->conflict));
    tjsonAddStringToObject(item, "exec", i642str(pObj->exec));
    tjsonAddStringToObject(item, "oper", i642str(pObj->oper));
S
Shengliang Guan 已提交
544 545 546 547
    tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
    tjsonAddStringToObject(item, "dbname", pObj->dbname);
    tjsonAddStringToObject(item, "stbname", pObj->stbname);
    tjsonAddStringToObject(item, "opername", pObj->opername);
S
Shengliang Guan 已提交
548 549 550
    tjsonAddStringToObject(item, "commitLogNum",i642str( taosArrayGetSize(pObj->commitActions)));
    tjsonAddStringToObject(item, "redoActionNum",i642str(taosArrayGetSize(pObj->redoActions)));
    tjsonAddStringToObject(item, "undoActionNum", i642str(taosArrayGetSize(pObj->undoActions)));
S
Shengliang Guan 已提交
551 552 553 554 555
    sdbRelease(pSdb, pObj);
  }
}

void dumpHeader(SSdb *pSdb, SJson *json) {
S
Shengliang Guan 已提交
556
  tjsonAddStringToObject(json, "sver", i642str(1));
S
Shengliang Guan 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
  tjsonAddStringToObject(json, "applyIndex", i642str(pSdb->applyIndex));
  tjsonAddStringToObject(json, "applyTerm", i642str(pSdb->applyTerm));
  tjsonAddStringToObject(json, "applyConfig", i642str(pSdb->applyConfig));

  SJson *maxIdsJson = tjsonCreateObject();
  tjsonAddItemToObject(json, "maxIds", maxIdsJson);
  for (int32_t i = 0; i < SDB_MAX; ++i) {
    int64_t maxId = 0;
    if (i < SDB_MAX) {
      maxId = pSdb->maxId[i];
    }
    tjsonAddStringToObject(maxIdsJson, sdbTableName(i), i642str(maxId));
  }

  SJson *tableVersJson = tjsonCreateObject();
  tjsonAddItemToObject(json, "tableVers", tableVersJson);
  for (int32_t i = 0; i < SDB_MAX; ++i) {
    int64_t tableVer = 0;
    if (i < SDB_MAX) {
      tableVer = pSdb->tableVer[i];
    }
    tjsonAddStringToObject(tableVersJson, sdbTableName(i), i642str(tableVer));
  }
}

void mndDumpSdb() {
  mInfo("start to dump sdb info to sdb.json");

  char path[PATH_MAX * 2] = {0};
  snprintf(path, sizeof(path), "%s%smnode", tsDataDir, TD_DIRSEP);

  SMsgCb msgCb = {0};
  msgCb.reportStartupFp = reportStartup;
  msgCb.sendReqFp = sendReq;
  msgCb.sendRspFp = sendRsp;
  msgCb.mgmt = (SMgmtWrapper *)(&msgCb);  // hack
  tmsgSetDefault(&msgCb);

  walInit();
  syncInit();

  SMnodeOpt opt = {.msgCb = msgCb};
  SMnode   *pMnode = mndOpen(path, &opt);
  if (pMnode == NULL) return;

  SSdb  *pSdb = pMnode->pSdb;
  SJson *json = tjsonCreateObject();
  dumpHeader(pSdb, json);
  dumpFunc(pSdb, json);
  dumpDb(pSdb, json);
  dumpStb(pSdb, json);
  dumpSma(pSdb, json);
  dumpVgroup(pSdb, json);
  dumpTopic(pSdb, json);
  dumpConsumer(pSdb, json);
  dumpSubscribe(pSdb, json);
  dumpOffset(pSdb, json);
  dumpStream(pSdb, json);
  dumpAcct(pSdb, json);
  dumpAuth(pSdb, json);
  dumpUser(pSdb, json);
  dumpDnode(pSdb, json);
  dumpBnode(pSdb, json);
  dumpSnode(pSdb, json);
  dumpQnode(pSdb, json);
  dumpMnode(pSdb, json);
  dumpCluster(pSdb, json);
  dumpTrans(pSdb, json);

  char     *pCont = tjsonToString(json);
  int32_t   contLen = strlen(pCont);
  char      file[] = "sdb.json";
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    mError("failed to write %s since %s", file, terrstr());
    return;
  }
  taosWriteFile(pFile, pCont, contLen);
  taosWriteFile(pFile, "\n", 1);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
  tjsonDelete(json);
  taosMemoryFree(pCont);

  mInfo("dump sdb info success");
}

#pragma GCC diagnostic pop