Skip to content

Conversation

@guanshengliang
Copy link
Contributor

Description

Please briefly describe the code changes in this pull request.

Checklist

Please check the items in the checklist if applicable.

  • Is the user manual updated?
  • Are the test cases passed and automated?
  • Is there no significant decrease in test coverage?

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @guanshengliang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates a new administrative feature that allows for the modification of the System Database (SDB) through a command-line utility. Users can now provide a JSON file to update vgroup configurations, streamlining the process of managing database metadata. The changes also include significant refactoring of SDB handling logic to improve modularity and maintainability.

Highlights

  • New SDB Modification Utility: Introduced a command-line option (-mSdb) to the dmMain executable, allowing for the modification of the System Database (SDB) using a specified JSON configuration file.
  • JSON-based Vgroup Configuration: Added functionality to parse a JSON file containing vgroup configurations, extract relevant properties, and apply these updates to the SDB, enabling programmatic management of vgroup metadata.
  • SDB Write Mechanism Refactoring: Refactored the SDB writing process by introducing a skip_type parameter to sdbWriteFileForDump and extracting common mnode initialization logic into a new helper function, mndPrepareMnode.
  • Vgroup Database Name Update: Ensured that the database name (dbName) is correctly updated during vgroup action updates within the mndVgroupActionUpdate function.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/taoskeeper-build.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new feature to modify the SDB (state database) from a JSON file, exposed via the -mSdb command-line option. The changes include argument parsing, implementation of the modification logic, and refactoring of related SDB dump functions. While the new feature is a good addition, the review identified several critical issues. There are resource leaks in dmMain.c (file handle) and mndDump.c (JSON object) due to improper error handling. Additionally, a change in tests/parallel_test/run.sh effectively disables multi-host testing, which is likely an unintended side effect. There are also minor issues with missing newlines in console output and opportunities to improve code maintainability by reducing repetitive code. These issues should be addressed to ensure code quality and correctness.

Comment on lines +597 to +611
if (global.modifySdb) {
int32_t code = 0;
TdFilePtr pFile;
if ((code = dmCheckRunning(tsDataDir, &pFile)) != 0) {
printf("failed to modify sdb since taosd is running, please stop it first, reason:%s", tstrerror(code));
return code;
}

TAOS_CHECK_RETURN(mndModifySdb(global.sdbJsonFile));
taosCleanupCfg();
taosCloseLog();
taosCleanupArgs();
taosConvDestroy();
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There is a resource leak in this block. The file pointer pFile obtained from dmCheckRunning is not closed if mndModifySdb fails. The TAOS_CHECK_RETURN macro will cause an early return, leaking the file handle. The file should be closed in all execution paths. Additionally, the cleanup functions are not called on failure.

  if (global.modifySdb) {
    int32_t   code = 0;
    TdFilePtr pFile = NULL;
    if ((code = dmCheckRunning(tsDataDir, &pFile)) != 0) {
      printf("failed to modify sdb since taosd is running, please stop it first, reason:%s\n", tstrerror(code));
      return code;
    }

    code = mndModifySdb(global.sdbJsonFile);
    if (taosCloseFile(&pFile) != 0) {
      dError("failed to close lock file, reason: %s", tstrerror(terrno));
    }

    taosCleanupCfg();
    taosCloseLog();
    taosCleanupArgs();
    taosConvDestroy();
    return code;
  }

Comment on lines +960 to +986
int32_t mndModifySdb(char *path) {
int32_t code = 0;
SJson *pJson = NULL;

mInfo("start to modify sdb info from sdb.json");

SMnode *pMnode = mndPrepareMnode();
if (pMnode == NULL) return terrno;

pJson = mndLoadSdbJson(path);
if (pJson == NULL) return terrno;

TAOS_CHECK_RETURN(mndGenerateVgroup(pMnode, pJson));

mInfo("write back to sdb file");
TAOS_CHECK_RETURN(sdbWriteFileForDump(pMnode->pSdb, -1));

return 0;
_OVER:
if (pJson != NULL) cJSON_Delete(pJson);

if (code != 0) {
mError("failed to modify sdb, file:%s since %s", path, tstrerror(code));
}

TAOS_RETURN(code);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There is a memory leak in mndModifySdb. If mndGenerateVgroup or sdbWriteFileForDump fails, the TAOS_CHECK_RETURN macro causes an early return, leaking the pJson object allocated by mndLoadSdbJson. The function should be refactored to use the goto _OVER pattern for cleanup, which is already present but unreachable. This ensures that pJson is always freed.

int32_t mndModifySdb(char *path) {
  int32_t code = 0;
  SJson  *pJson = NULL;
  SMnode *pMnode = NULL;

  mInfo("start to modify sdb info from sdb.json");

  pMnode = mndPrepareMnode();
  if (pMnode == NULL) {
    code = terrno;
    goto _OVER;
  }

  pJson = mndLoadSdbJson(path);
  if (pJson == NULL) {
    code = terrno;
    goto _OVER;
  }

  if ((code = mndGenerateVgroup(pMnode, pJson)) != 0) {
    goto _OVER;
  }

  mInfo("write back to sdb file");
  if ((code = sdbWriteFileForDump(pMnode->pSdb, -1)) != 0) {
    goto _OVER;
  }

_OVER:
  if (pJson != NULL) cJSON_Delete(pJson);

  if (code != 0) {
    mError("failed to modify sdb, file:%s since %s", path, tstrerror(code));
  }

  TAOS_RETURN(code);
}


i=0
while true; do
while [ $i -lt 1 ]; do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The while loop condition has been changed from true to [ $i -lt 1 ]. This change limits the test execution to only the first host defined in the configuration file, effectively disabling multi-host parallel testing. This seems like a temporary debugging change that was accidentally committed and should be reverted.

Suggested change
while [ $i -lt 1 ]; do
while true; do

int32_t code = 0;
TdFilePtr pFile;
if ((code = dmCheckRunning(tsDataDir, &pFile)) != 0) {
printf("failed to modify sdb since taosd is running, please stop it first, reason:%s", tstrerror(code));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error message printed to the console is missing a newline character. This can lead to messy output and make it harder for users to read.

      printf("failed to modify sdb since taosd is running, please stop it first, reason:%s\n", tstrerror(code));

Comment on lines +908 to +930
tjsonGetNumberValue(pNodeVgroup, "vgId", group.vgId, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "createdTime", group.createdTime, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "updateTime", group.updateTime, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "version", group.version, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "hashBegin", group.hashBegin, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "hashEnd", group.hashEnd, code);
if (code) return code;
code = tjsonGetStringValue(pNodeVgroup, "db", group.dbName);
if (code) return code;
mInfo("group.dbname %s", group.dbName);
tjsonGetNumberValue(pNodeVgroup, "dbUid", group.dbUid, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "isTsma", group.isTsma, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "replica", group.replica, code);
if (code) return code;
tjsonGetNumberValue(pNodeVgroup, "syncConfChangeVer", group.syncConfChangeVer, code);
if (code) return code;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error checking pattern tjsonGet...; if (code) return code; is repeated for every field parsed from the JSON. This makes the code verbose and harder to maintain. Consider creating a helper macro to reduce this boilerplate code, which would improve readability and reduce the chance of errors.

@guanshengliang guanshengliang merged commit 68f6735 into main Nov 5, 2025
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants