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

Java: Add PING command #914

Merged
merged 14 commits into from
Feb 9, 2024

Conversation

acarbonetto
Copy link
Contributor

@acarbonetto acarbonetto commented Feb 7, 2024

Issue #, if available:

This PR is dependent to: #917

Description of changes:
This PR adds get/set/ping/info calls to the standalone, and cluster-mode:

  • standalone
    • ping
    • ping with message
  • cluster
    • ping
    • ping with message
    • ping with route
    • ping with message and route

Examples:

// Single commands
RedisClient client = RedisClient.createClient(config).get();
String pingResult = client.ping().get();
String pingResult2 = client.ping("message").get();

// Cluster-mode single commands
RedisClusterClient clusterClient = RedisClusterClient.createClient(config).get();
String pingResult3 = clusterClient.ping(ALL_NODES).get();
String pingResult4 = clusterClient.ping("message", ALL_NODES).get();

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@acarbonetto acarbonetto requested a review from a team as a code owner February 7, 2024 17:43
java/client/src/main/java/glide/api/BaseClient.java Outdated Show resolved Hide resolved
throw new RedisException(
"Unexpected return type from Redis: got " + className + " expected Map");
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should make sections/regions - for handlers, for commands for all rest

Copy link
Contributor Author

@acarbonetto acarbonetto Feb 7, 2024

Choose a reason for hiding this comment

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

Sure - but let's leave this for a future update. This will be useful when we have more than 3 commands.

}

@Override
public CompletableFuture<String> ping(String str) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Saw that you added @nonnull annotations for cluster why not here?

@acarbonetto acarbonetto self-assigned this Feb 8, 2024
@acarbonetto acarbonetto added the java issues and fixes related to the java client label Feb 8, 2024
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
@acarbonetto acarbonetto force-pushed the java/integ_acarbo_add_ping_only branch from 177857c to 0b2aa76 Compare February 8, 2024 19:10
* @return Response as a <code>String</code>
* @throws RedisException if there's a type mismatch
*/
protected String handleStringResponse(Response response) {
Copy link
Contributor

Choose a reason for hiding this comment

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

not necessarily for this PR - this has a very similar structure to the handleArrayResponse.
Consider making a generic function with some refinement of this imaginary code:

protected T handleResponse<T>(Response response) {
        Object value = handleObjectResponse(response);
        if (value instanceof T || value == null) {
            return (T) value;
        }
        throw new RedisException(
                "Unexpected return type from Redis: got "
                        + value.getClass().getSimpleName()
                        + " expected " + T.getType());
    }
    ```
of course, `T.getType()` isn't really possible and this doesn't handle primitives well, but I believe this is possible, and will remove a lot of glue code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea. I've updated to include the generic function - this will reduce code duplication.

return CreateClient(config, RedisClient::new);
}

@Override
public CompletableFuture<Object> customCommand(String[] args) {
public CompletableFuture<Object> customCommand(@NonNull String[] args) {
Copy link
Contributor

Choose a reason for hiding this comment

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

correct me if I'm wrong, but @NonNull applies here to the array - the strings themselves might still be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct... and that would cause a protobuf builder error (NullPointerException).

Copy link
Contributor

Choose a reason for hiding this comment

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

so how would you solve this?

java/integTest/src/test/java/glide/SharedCommandTests.java Outdated Show resolved Hide resolved
@@ -25,12 +26,13 @@ protected RedisClient(ConnectionManager connectionManager, CommandManager comman
* @param config Redis client Configuration
* @return A Future to connect and return a RedisClient
*/
public static CompletableFuture<RedisClient> CreateClient(RedisClientConfiguration config) {
public static CompletableFuture<RedisClient> CreateClient(
@NonNull RedisClientConfiguration config) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding for consistency across all methods. @nonnull will return a NullPointerException if the argument is not defined.

Signed-off-by: Andrew Carbonetto <[email protected]>
*/
private <T> T handleRedisResponse(Class classType, boolean isNullable, Response response) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
private <T> T handleRedisResponse(Class classType, boolean isNullable, Response response) {
protected <T> T handleRedisResponse(Class<T> classType, boolean isNullable, Response response) throws RedisException {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks @shachlanAmazon for the idea

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why not private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated in 2cbf558

java/client/src/main/java/glide/api/BaseClient.java Outdated Show resolved Hide resolved
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
Signed-off-by: Andrew Carbonetto <[email protected]>
@acarbonetto acarbonetto merged commit ceaa793 into valkey-io:main Feb 9, 2024
11 checks passed
@acarbonetto acarbonetto deleted the java/integ_acarbo_add_ping_only branch February 9, 2024 06:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
java issues and fixes related to the java client
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants