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

Small fix in JedisClusterInfoCache.discoverClusterNodesAndSlots. #2682

Merged
merged 2 commits into from
Dec 4, 2021

Conversation

adiamzn
Copy link
Contributor

@adiamzn adiamzn commented Nov 1, 2021

Description:

The change calls Jedis.cluserSlots before reset is called instead of the other way around.

Why the change?

the reset function deletes the nodes and slots fields.
If the Jedis.clusterSlots throws an exception in the try-catch block
the function fails and the data is deleted.
It would be nicer if the data (nodes and slots) were left untouched if jedis.clusterSlots throws an exception.

@nbraun-amazon
Copy link

Approved

@yangbodong22011
Copy link
Collaborator

yangbodong22011 commented Nov 2, 2021

we just call discoverClusterNodesAndSlots at 1:

private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig clientConfig) {
  ArrayList<HostAndPort> startNodeList = new ArrayList<>(startNodes);
  Collections.shuffle(startNodeList);
    for (HostAndPort hostAndPort: startNodeList) {
    try (Jedis jedis = new Jedis(hostAndPort, clientConfig)) {
      cache.discoverClusterNodesAndSlots(jedis); // 1
      return; // 2
    } catch (JedisConnectionException e) {
      // try next nodes
    }
  }
}


  public void discoverClusterNodesAndSlots(Jedis jedis) {
    w.lock();

    try {
      reset(); // 3
      List<Object> slots = jedis.clusterSlots(); // 4
      ...

When jedis.clusterSlots() does not throw an exception, the return at 2 will be executed. On the contrary, if an exception is thrown, enter discoverClusterNodesAndSlots again, it does not matter to execute 3 or 4 first, because at this time the variables of nodes and slots are empty (they were not filled last time, because jedis.clusterSlots() throws an exception NS)

I would like to ask, does this PR fix the actual problems encountered in the production environment, or is the idea obtained by reading the code?

@adiamzn
Copy link
Contributor Author

adiamzn commented Nov 2, 2021

Hi Yang,
Agree that this does not cause any issues the way the function is currently called.
As you mentioned this function is currently only called from [1] and this is fine.

Since the function discoverClusterNodesandSlots is part of the API for the class JedisClusterInfoCache, I think it is a good idea to add a better safety guarantee.
The idea came up just from reading the code, not from prod environment
All the best,
Adi

@@ -107,8 +107,8 @@ public void discoverClusterNodesAndSlots(Jedis jedis) {
w.lock();
Copy link
Collaborator

Choose a reason for hiding this comment

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

List<Object> slots = jedis.clusterSlots();
w.lock();

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the function discoverClusterNodesandSlots is part of the API for the class JedisClusterInfoCache, I think it is a good idea to add a better safety guarantee.

@adiamzn Agree, I think we can move jedis.clusterSlots() to the top of w.lock(), just like the discoverClusterSlots function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, made the change

Copy link
Collaborator

Choose a reason for hiding this comment

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

@adiamzn jedis.clusterSlots() to the top of w.lock(), the lock time can be shortened.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true, fixing

Calls jedis.Clusterslots before calling reset, instead of the other way around.

Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
@adiamzn adiamzn requested a review from sazzad16 November 2, 2021 12:54
@sazzad16 sazzad16 added this to the 3.8.0 milestone Nov 2, 2021
@sazzad16 sazzad16 merged commit b82967a into redis:master Dec 4, 2021
sazzad16 added a commit that referenced this pull request Dec 4, 2021
Calls jedis.Clusterslots before calling reset, instead of the other way around.

Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>

Co-authored-by: EC2 Default User <ec2-user@ip-172-31-47-142.us-west-2.compute.internal>
Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
@chayim chayim mentioned this pull request Jun 2, 2022
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

5 participants