Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curvefs: add memcached startup check #2296

Merged
merged 1 commit into from
Mar 8, 2023

Conversation

Ziy1-Tan
Copy link
Contributor

@Ziy1-Tan Ziy1-Tan commented Mar 7, 2023

What problem does this PR solve?

Issue Number: #2295

Problem Summary:

What is changed and how it works?

What's Changed:

How it Works:

Side effects(Breaking backward compatibility? Performance regression?):

Check List

  • Relevant documentation/comments is changed or added
  • I acknowledge that all my contributions will be made under the project's license

@Ziy1-Tan Ziy1-Tan requested a review from ilixiaocui March 7, 2023 10:40
};


TEST_F(MemCachedTest, MultiThreadTask) {
if(!isRunning_) {
return;
}
// prepare data
std::vector<std::thread> workers;
std::vector<std::pair<std::string, std::string>> kvstr = {
Copy link

Choose a reason for hiding this comment

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

with a brief overview of the code patch. The patch is for MemCachedTest, a testing class that is used to test the functionality of the memcache server. It starts by adding a boolean flag, isRunning_, and sets it to true or false depending on if the server was started correctly. If the server wasn't started correctly, an error message is logged. Then, in the MultiThreadTask test case, the flag is checked before continuing. This ensures that the code does not proceed if the server was not successfully started.

From a risk perspective, there are no major bugs in the code patch. However, I would suggest that the patch is refactored to make the code clearer and easier to read. For example, instead of nesting the if statement in the loop, the check could be done after the loop and the error message could be printed outside of the loop as well. Additionally, more descriptive variable names can be used to help clarify the logic.

@Ziy1-Tan
Copy link
Contributor Author

Ziy1-Tan commented Mar 7, 2023

cicheck

};


TEST_F(MemCachedTest, MultiThreadTask) {
if (!isRunning_) {
return;
}
// prepare data
std::vector<std::thread> workers;
std::vector<std::pair<std::string, std::string>> kvstr = {
Copy link

Choose a reason for hiding this comment

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

the code review:

  1. Add logging to indicate if memcached server start failed in setup.
  2. Add a boolean variable to indicate the status of memcache starting, and return immediately when it is false.
  3. The do-while loop should be changed to while loop since there is no need to run the loop once before testing the condition.
  4. The LOG(INFO) line should be moved inside the if-block to avoid unnecessary printing when the server fails to start.
  5. The LOG(ERROR) message should include more useful information such as the errorlog string.

isRunning_ = ret && retry <= 100;
if (!isRunning_) {
LOG(ERROR) << absl::StrCat(
"=============== memcache start failed, errorlog: ", errorlog);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use ASSERT_EQ checks instead of output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if Use ASSERT_EQ, The ci still failed. I use isRunning_ to skip the following test.

break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
retry++;
} while (1);
ASSERT_TRUE(ret && retry <= 0)
<< absl::StrCat("memcache start failed, errorlog: ", errorlog);
LOG(INFO) << "=============== memcache start ok";
}

Copy link

Choose a reason for hiding this comment

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

to do the code review:

  1. The code looks generally good.
  2. A new variable "ret" is added and is used in the if statement. It should be initialized to false before it is used.
  3. An assertion is added to check if the memcache start failed. This is a good practice as it helps to detect errors earlier on.
  4. The retry number is set to 100, which might be too high. A lower value should be considered.

@Ziy1-Tan
Copy link
Contributor Author

Ziy1-Tan commented Mar 7, 2023

cicheck

break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
retry++;
} while (1);
ASSERT_TRUE(ret && retry <= 0)
Copy link
Contributor

@ilixiaocui ilixiaocui Mar 8, 2023

Choose a reason for hiding this comment

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

ASSERT_TRUE(ret)

break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
retry++;
} while (1);
ASSERT_TRUE(ret)
<< absl::StrCat("memcache start failed, errorlog: ", errorlog);
LOG(INFO) << "=============== memcache start ok";
}

Copy link

Choose a reason for hiding this comment

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

with a brief examination of the code.

This is a test class for memcached server. It appears to loop until a Set command is issued successfully or until the retry count reaches 100. Once the loop completes, an assertion is made that the Set command must have succeeded, and an error message is provided if it did not.

From a code review perspective, there are a few potential issues worth considering:

  1. The maximum number of retries should be configurable, rather than hard-coded. This will make it easier to adjust the retry behavior if needed.

  2. The assertion should check that the return value of the Set command was true, rather than just checking whether the loop exited successfully. This will ensure that the command actually succeeded, rather than just indicating that it was attempted.

  3. A timeout should be implemented to prevent the loop from running indefinitely in the event of a network issue or other unexpected problem.

Overall, this code looks well-structured and should work as intended. However, the improvements suggested above would make the code more robust and reliable.

@Ziy1-Tan
Copy link
Contributor Author

Ziy1-Tan commented Mar 8, 2023

cicheck

@Ziy1-Tan
Copy link
Contributor Author

Ziy1-Tan commented Mar 8, 2023

It seems that it must fail when memcached -u root 18080 wit root user
image

@ilixiaocui
Copy link
Contributor

The error is observed as follows:

image

When we use sudo to execute, the identified user is root. There is an extra space before root.

I have tried following and it's ok.

 ASSERT_EQ(0, execlp("memcached", "memcached", "-uroot",
                                memcached_port.c_str(), nullptr));

Signed-off-by: Ziy1-Tan <ajb459684460@gmail.com>
};
manager_.Get(task);
taskEnvent.Wait();
taskEvent.Wait();
ASSERT_EQ(0, memcmp(result, kvstr[i].second.c_str(), 4));
ASSERT_TRUE(task->res);
});
Copy link

Choose a reason for hiding this comment

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

with a brief review of the given code patch.

The first change is that the option for starting memcached server is changed from "-u root" to "-uroot", this change can make the command more concise, but we need to verify if it will cause any issue.

The second change is that an assertion is added after calling Set() to ensure the set operation succeeds, this is good practice and can prevent potential bugs in the future.

The third change is that CountDownEvent variable is renamed from taskEnvent to taskEvent, which is a better name that reflects what the variable is used for.

Overall, the code patch looks good, but we should run some tests to make sure the changes don't break existing functionality.

@Ziy1-Tan
Copy link
Contributor Author

Ziy1-Tan commented Mar 8, 2023

cicheck

@ilixiaocui ilixiaocui merged commit 45b4366 into opencurve:master Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants