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

fix: update provider to null #1452

Merged
merged 1 commit into from
Oct 14, 2024
Merged

Conversation

Lo1nt
Copy link
Collaborator

@Lo1nt Lo1nt commented Oct 12, 2024

Motivation:

fix connection leakage.

Modification:

Describe the idea and modifications you've done.

Result:

Fixes #1442 .

Summary by CodeRabbit

  • New Features

    • Improved provider management logic for better handling of empty provider groups.
    • Enhanced event posting for provider updates.
  • Bug Fixes

    • Corrected logging behavior to ensure warnings are only logged when appropriate.
  • Tests

    • Introduced a new test class to validate the functionality of updating provider information within a cluster.

@sofastack-cla sofastack-cla bot added bug Something isn't working cla:yes CLA is ok size/L labels Oct 12, 2024
Copy link
Contributor

coderabbitai bot commented Oct 12, 2024

Walkthrough

The changes in this pull request focus on the AbstractCluster class to enhance provider management logic. Key modifications include refactoring the updateProviders and updateAllProviders methods to improve the handling of empty provider groups and associated logging. Additionally, a new test class, ClusterProviderUpdateTest, has been introduced to validate the functionality of updating provider information, ensuring that the internal state reflects these updates accurately.

Changes

File Path Change Summary
core-impl/client/src/main/java/com/alipay/sofa/rpc/client/AbstractCluster.java Refactored updateProviders and updateAllProviders methods for better empty provider handling and logging.
core-impl/client/src/test/java/com/alipay/sofa/rpc/client/ClusterProviderUpdateTest.java Added ClusterProviderUpdateTest class with testUpdateProvider method to validate provider updates.
core-impl/client/src/test/java/com/alipay/sofa/rpc/client/ClusterProviderUpdateTest.java Introduced TestUseConnectionHolder class for testing connection management interactions.

Assessment against linked issues

Objective Addressed Explanation
Ensure proper handling of provider updates in updateProviders method (#[1442])
Improve logging and connection management during provider updates (#[1442])
Clarify the design choice for not executing connectionHolder.updateProviders when empty (#[1442]) The change does not address the design rationale.

🐇 In the code, I hop and play,
Fixing bugs along the way.
With tests to check and logs so bright,
Provider updates now feel just right!
Hooray for changes, let’s all cheer,
For smooth connections, year after year! 🥳


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (1)
core-impl/client/src/main/java/com/alipay/sofa/rpc/client/AbstractCluster.java (1)

Line range hint 232-257: Consistent improvement in handling empty provider groups

The changes in the updateAllProviders method align with the improvements made in the updateProviders method. The additional check for CommonUtils.isNotEmpty(currentProviderList) before logging a warning and closing transports prevents unnecessary actions when the provider list becomes empty.

For consistency with the updateProviders method, consider introducing a previouslyEmpty variable here as well. This would make the logic more explicit and easier to understand.

Consider refactoring the code as follows for improved consistency:

 if (count == 0) {
     Collection<ProviderInfo> currentProviderList = currentProviderList();
+    boolean previouslyEmpty = CommonUtils.isEmpty(currentProviderList);
     addressHolder.updateAllProviders(providerGroups);
-    if (CommonUtils.isNotEmpty(currentProviderList)) {
+    if (!previouslyEmpty) {
         if (LOGGER.isWarnEnabled(consumerConfig.getAppName())) {
             LOGGER.warnWithApp(consumerConfig.getAppName(), "Provider list is emptied, may be all " +
                 "providers has been closed, or this consumer has been add to blacklist");
-            closeTransports();
         }
+        closeTransports();
     }
 } else {
     // ... (rest of the code remains unchanged)
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7150804 and d20bea8.

📒 Files selected for processing (2)
  • core-impl/client/src/main/java/com/alipay/sofa/rpc/client/AbstractCluster.java (1 hunks)
  • core-impl/client/src/test/java/com/alipay/sofa/rpc/client/ClusterProviderUpdateTest.java (1 hunks)
🧰 Additional context used
🔇 Additional comments (4)
core-impl/client/src/test/java/com/alipay/sofa/rpc/client/ClusterProviderUpdateTest.java (2)

1-40: LGTM: File structure and imports are well-organized.

The file has appropriate license information, necessary imports, and is correctly structured. The class name and package are appropriate for its purpose.


1-194: Overall assessment: Good start, but room for improvement

This new test file for AbstractCluster is a valuable addition to the test suite. It covers basic scenarios for updating providers and aligns with the PR objectives. However, there are several areas where it can be enhanced:

  1. Consider using @BeforeClass for test setup instead of static initialization.
  2. Expand the test coverage in testUpdateProvider to include more scenarios and stronger assertions.
  3. Enhance the TestUseConnectionHolder class to be more versatile and provide better support for detailed testing.

These improvements will result in a more robust and comprehensive test suite, ensuring better code quality and reducing the likelihood of future regressions.

core-impl/client/src/main/java/com/alipay/sofa/rpc/client/AbstractCluster.java (2)

213-228: Improved handling of empty provider groups

The changes in the updateProviders method address the issue mentioned in the PR objectives. The introduction of the previouslyEmpty variable and the refactored logic prevent unnecessary warnings and connection closures when the provider list transitions from non-empty to empty. This should help reduce excessive warning logs in the scenarios described in the linked issue #1442.

The updated event posting logic also ensures that the ProviderInfoUpdateEvent is triggered with the correct information, addressing the swallow copy problem mentioned in the comment.


Line range hint 1-1009: Overall improvement in provider management

The changes made to the updateProviders and updateAllProviders methods in this file significantly improve the handling of empty provider groups. These modifications address the issues outlined in the PR objectives and the linked issue #1442, particularly:

  1. Preventing unnecessary warning logs when provider lists become empty.
  2. Avoiding redundant connection closures when transitioning from non-empty to empty provider lists.
  3. Ensuring correct event posting with updated provider information.

These improvements should lead to more efficient connection management and reduced log clutter in scenarios where provider services are restarted or temporarily unavailable.

The changes are focused and don't introduce any apparent regressions in other parts of the file. The overall structure and functionality of the AbstractCluster class remain intact, with the modifications enhancing its robustness in handling provider updates.

Comment on lines +41 to +86
public class ClusterProviderUpdateTest {
private static final AbstractCluster cluster;

static {
ConsumerBootstrap bootstrap = new ConsumerBootstrap(new ConsumerConfig()) {
@Override
public Object refer() {
return null;
}

@Override
public void unRefer() {

}

@Override
public Object getProxyIns() {
return null;
}

@Override
public Cluster getCluster() {
return null;
}

@Override
public List<ProviderGroup> subscribe() {
return null;
}

@Override
public boolean isSubscribed() {
return false;
}
};

cluster = new AbstractCluster(bootstrap) {
@Override
protected SofaResponse doInvoke(SofaRequest msg) throws SofaRpcException {
return null;
}
};

cluster.addressHolder = new SingleGroupAddressHolder(null);
cluster.connectionHolder = new TestUseConnectionHolder(null);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider using @BeforeClass instead of static initialization.

While the current setup allows for isolated testing of the cluster's behavior, using static initialization for test setup can lead to issues with test isolation and make it harder to reset the state between tests. Consider refactoring this to use JUnit's @BeforeClass annotation for setup, which would provide better control over the test lifecycle.

Example refactoring:

public class ClusterProviderUpdateTest {
    private static AbstractCluster cluster;

    @BeforeClass
    public static void setUp() {
        ConsumerBootstrap bootstrap = new ConsumerBootstrap(new ConsumerConfig()) {
            // ... (existing mock implementation)
        };

        cluster = new AbstractCluster(bootstrap) {
            // ... (existing mock implementation)
        };

        cluster.addressHolder = new SingleGroupAddressHolder(null);
        cluster.connectionHolder = new TestUseConnectionHolder(null);
    }

    // ... (rest of the test class)
}

This approach would maintain the same functionality while improving test isolation and maintainability.

Comment on lines +88 to +104
@Test
public void testUpdateProvider() {
String groupName = "testUpdateProvider-Group";
List<ProviderInfo> providerList = Arrays.asList(
ProviderHelper.toProviderInfo("127.0.0.1:12200"),
ProviderHelper.toProviderInfo("127.0.0.1:12201"),
ProviderHelper.toProviderInfo("127.0.0.1:12202"));
ProviderGroup g = new ProviderGroup(groupName, providerList);
cluster.updateProviders(g);

Assert.assertEquals(cluster.currentProviderList().size(), providerList.size());

cluster.updateProviders(new ProviderGroup(groupName, null));

Assert.assertTrue(cluster.getAddressHolder().getProviderGroup(groupName).isEmpty());
Assert.assertEquals( 1, ((TestUseConnectionHolder)cluster.connectionHolder).calledCloseAllClientTransports.get());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage and assertions in testUpdateProvider method.

The current test covers basic scenarios, but consider the following improvements:

  1. Test with an empty provider list in addition to null.
  2. Verify the exact contents of the provider list after update, not just its size.
  3. Add a test case for updating with a different set of providers to ensure proper addition and removal.
  4. Consider testing edge cases, such as updating with duplicate providers.

Example of enhanced assertions:

@Test
public void testUpdateProvider() {
    String groupName = "testUpdateProvider-Group";
    List<ProviderInfo> providerList = Arrays.asList(
            ProviderHelper.toProviderInfo("127.0.0.1:12200"),
            ProviderHelper.toProviderInfo("127.0.0.1:12201"),
            ProviderHelper.toProviderInfo("127.0.0.1:12202"));
    ProviderGroup g = new ProviderGroup(groupName, providerList);
    cluster.updateProviders(g);

    List<ProviderInfo> currentProviders = cluster.currentProviderList();
    Assert.assertEquals("Provider list size should match", providerList.size(), currentProviders.size());
    Assert.assertTrue("All providers should be present", currentProviders.containsAll(providerList));

    // Test with empty list
    cluster.updateProviders(new ProviderGroup(groupName, Collections.emptyList()));
    Assert.assertTrue("Provider group should be empty", cluster.getAddressHolder().getProviderGroup(groupName).isEmpty());

    // Test with null list
    cluster.updateProviders(new ProviderGroup(groupName, null));
    Assert.assertTrue("Provider group should be empty", cluster.getAddressHolder().getProviderGroup(groupName).isEmpty());

    Assert.assertEquals("closeAllClientTransports should be called twice", 2, ((TestUseConnectionHolder)cluster.connectionHolder).calledCloseAllClientTransports.get());
}

These enhancements will provide more comprehensive coverage and stronger assertions.

Comment on lines +107 to +193
private static class TestUseConnectionHolder extends ConnectionHolder {
Set<ProviderInfo> connections = new HashSet<>();

AtomicInteger calledCloseAllClientTransports = new AtomicInteger();

/**
* 构造函数
*
* @param consumerBootstrap 服务消费者配置
*/
protected TestUseConnectionHolder(ConsumerBootstrap consumerBootstrap) {
super(consumerBootstrap);
}

@Override
public void closeAllClientTransports(DestroyHook destroyHook) {
calledCloseAllClientTransports.getAndIncrement();
}

@Override
public ConcurrentMap<ProviderInfo, ClientTransport> getAvailableConnections() {
return null;
}

@Override
public List<ProviderInfo> getAvailableProviders() {
return null;
}

@Override
public ClientTransport getAvailableClientTransport(ProviderInfo providerInfo) {
return null;
}

@Override
public boolean isAvailableEmpty() {
return false;
}

@Override
public Collection<ProviderInfo> currentProviderList() {
return null;
}

@Override
public void setUnavailable(ProviderInfo providerInfo, ClientTransport transport) {

}

@Override
public void addProvider(ProviderGroup providerGroup) {
throw new UnsupportedOperationException("not implemented");
}

@Override
public void removeProvider(ProviderGroup providerGroup) {
throw new UnsupportedOperationException("not implemented");

}

@Override
public void updateProviders(ProviderGroup providerGroup) {
for (ProviderInfo i : providerGroup.getProviderInfos()) {
connections.add(i);
}
}

@Override
public void updateAllProviders(List<ProviderGroup> providerGroups) {

}

@Override
public void destroy() {

}

@Override
public void destroy(DestroyHook hook) {

}

@Override
public void init() {

}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance TestUseConnectionHolder for more comprehensive testing.

The current implementation of TestUseConnectionHolder is minimal and might not provide enough flexibility for future tests. Consider the following improvements:

  1. Implement the unimplemented methods with basic functionality or throw UnsupportedOperationException with a clear message.
  2. Add more tracking variables (similar to calledCloseAllClientTransports) for other important methods to allow for more detailed assertions in tests.
  3. Consider using a Map to store connections instead of a Set for more flexibility in testing.
  4. Implement updateAllProviders method to be consistent with updateProviders.

Example improvements:

private static class TestUseConnectionHolder extends ConnectionHolder {
    private final Map<ProviderInfo, ClientTransport> connections = new ConcurrentHashMap<>();
    final AtomicInteger calledCloseAllClientTransports = new AtomicInteger();
    final AtomicInteger calledAddProvider = new AtomicInteger();
    final AtomicInteger calledRemoveProvider = new AtomicInteger();

    // ... (existing constructor)

    @Override
    public ConcurrentMap<ProviderInfo, ClientTransport> getAvailableConnections() {
        return new ConcurrentHashMap<>(connections);
    }

    @Override
    public List<ProviderInfo> getAvailableProviders() {
        return new ArrayList<>(connections.keySet());
    }

    @Override
    public ClientTransport getAvailableClientTransport(ProviderInfo providerInfo) {
        return connections.get(providerInfo);
    }

    @Override
    public boolean isAvailableEmpty() {
        return connections.isEmpty();
    }

    @Override
    public Collection<ProviderInfo> currentProviderList() {
        return new ArrayList<>(connections.keySet());
    }

    @Override
    public void addProvider(ProviderGroup providerGroup) {
        calledAddProvider.incrementAndGet();
        updateProviders(providerGroup);
    }

    @Override
    public void removeProvider(ProviderGroup providerGroup) {
        calledRemoveProvider.incrementAndGet();
        for (ProviderInfo info : providerGroup.getProviderInfos()) {
            connections.remove(info);
        }
    }

    @Override
    public void updateProviders(ProviderGroup providerGroup) {
        for (ProviderInfo info : providerGroup.getProviderInfos()) {
            connections.put(info, null); // or mock ClientTransport if needed
        }
    }

    @Override
    public void updateAllProviders(List<ProviderGroup> providerGroups) {
        connections.clear();
        for (ProviderGroup group : providerGroups) {
            updateProviders(group);
        }
    }

    // ... (other methods remain the same)
}

These enhancements will make the TestUseConnectionHolder more versatile for current and future tests.

Copy link
Collaborator

@EvenLjj EvenLjj left a comment

Choose a reason for hiding this comment

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

LGTM

@Lo1nt Lo1nt merged commit 1979f23 into sofastack:master Oct 14, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cla:yes CLA is ok size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AllConnectConnectionHolder心跳重试线程优化讨论
3 participants