Skip to content

Commit

Permalink
Java: update INFO command (#2274)
Browse files Browse the repository at this point in the history
* Update `INFO` command.

Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
  • Loading branch information
Yury-Fridlyand authored Sep 11, 2024
1 parent bc52197 commit e419229
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 107 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
* Python: Add Script commands ([#2208](https://github.com/valkey-io/valkey-glide/pull/2208))

#### Breaking Changes
* Java: Update INFO command ([#2274](https://github.com/valkey-io/valkey-glide/pull/2274))
* Node: (Refactor) Convert types to interfaces ([#2263](https://github.com/valkey-io/valkey-glide/pull/2263))
* Node: (Refactor) Convert classes to types ([#2005](https://github.com/valkey-io/valkey-glide/pull/2005))
* Core: Change FUNCTION STATS command to return multi node response for standalone mode ([#2117](https://github.com/valkey-io/valkey-glide/pull/2117))
Expand Down
10 changes: 7 additions & 3 deletions java/client/src/main/java/glide/api/GlideClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import glide.api.models.GlideString;
import glide.api.models.Transaction;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.SortOptions;
import glide.api.models.commands.SortOptionsBinary;
import glide.api.models.commands.function.FunctionRestorePolicy;
Expand All @@ -60,6 +60,7 @@
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import lombok.NonNull;
import org.apache.commons.lang3.ArrayUtils;

Expand Down Expand Up @@ -135,8 +136,11 @@ public CompletableFuture<String> info() {
}

@Override
public CompletableFuture<String> info(@NonNull InfoOptions options) {
return commandManager.submitNewCommand(Info, options.toArgs(), this::handleStringResponse);
public CompletableFuture<String> info(@NonNull Section[] sections) {
return commandManager.submitNewCommand(
Info,
Stream.of(sections).map(Enum::toString).toArray(String[]::new),
this::handleStringResponse);
}

@Override
Expand Down
13 changes: 8 additions & 5 deletions java/client/src/main/java/glide/api/GlideClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.SortClusterOptions;
import glide.api.models.commands.function.FunctionRestorePolicy;
import glide.api.models.commands.scan.ClusterScanCursor;
Expand All @@ -70,6 +70,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import lombok.NonNull;
import org.apache.commons.lang3.ArrayUtils;
import response.ResponseOuterClass.Response;
Expand Down Expand Up @@ -224,17 +225,19 @@ public CompletableFuture<ClusterValue<String>> info(@NonNull Route route) {
}

@Override
public CompletableFuture<ClusterValue<String>> info(@NonNull InfoOptions options) {
public CompletableFuture<ClusterValue<String>> info(@NonNull Section[] sections) {
return commandManager.submitNewCommand(
Info, options.toArgs(), response -> ClusterValue.of(handleMapResponse(response)));
Info,
Stream.of(sections).map(Enum::toString).toArray(String[]::new),
response -> ClusterValue.of(handleMapResponse(response)));
}

@Override
public CompletableFuture<ClusterValue<String>> info(
@NonNull InfoOptions options, @NonNull Route route) {
@NonNull Section[] sections, @NonNull Route route) {
return commandManager.submitNewCommand(
Info,
options.toArgs(),
Stream.of(sections).map(Enum::toString).toArray(String[]::new),
route,
response ->
route instanceof SingleNodeRoute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,33 @@ public interface ServerManagementClusterCommands {
CompletableFuture<ClusterValue<String>> info(Route route);

/**
* Gets information and statistics about the server. The command will be routed to all primary
* nodes.
* Gets information and statistics about the server.<br>
* Starting from server version 7, command supports multiple section arguments.<br>
* The command will be routed to all primary nodes.
*
* @see <a href="https://valkey.io/commands/info/">valkey.io</a> for details.
* @param options A list of {@link InfoOptions.Section} values specifying which sections of
* @param sections A list of {@link InfoOptions.Section} values specifying which sections of
* information to retrieve. When no parameter is provided, the {@link
* InfoOptions.Section#DEFAULT} option is assumed.
* @return A <code>Map{@literal <String, String>}</code> with each address as the key and its
* corresponding value is the information of the sections requested for the node.
* @example
* <pre>{@code
* ClusterValue<String> payload = clusterClient.info(InfoOptions.builder().section(STATS).build()).get();
* ClusterValue<String> payload = clusterClient.info(new Section[] { Section.STATS }).get();
* // By default, the command is sent to multiple nodes, expecting a MultiValue result.
* for (Map.Entry<String, String> entry : payload.getMultiValue().entrySet()) {
* System.out.println("Node [" + entry.getKey() + "]: " + entry.getValue());
* }
* }</pre>
*/
CompletableFuture<ClusterValue<String>> info(InfoOptions options);
CompletableFuture<ClusterValue<String>> info(Section[] sections);

/**
* Gets information and statistics about the server.
* Gets information and statistics about the server.<br>
* Starting from server version 7, command supports multiple section arguments.
*
* @see <a href="https://valkey.io/commands/info/">valkey.io</a> for details.
* @param options A list of {@link InfoOptions.Section} values specifying which sections of
* @param sections A list of {@link InfoOptions.Section} values specifying which sections of
* information to retrieve. When no parameter is provided, the {@link
* InfoOptions.Section#DEFAULT} option is assumed.
* @param route Specifies the routing configuration for the command. The client will route the
Expand All @@ -93,12 +95,12 @@ public interface ServerManagementClusterCommands {
* value is the information of the sections requested for the node.
* @example
* <pre>{@code
* ClusterValue<String> payload = clusterClient.info(InfoOptions.builder().section(STATS).build(), RANDOM).get();
* ClusterValue<String> payload = clusterClient.info(new Section[] { Section.STATS }, RANDOM).get();
* // Command sent to a single random node via RANDOM route, expecting SingleValue result.
* assert data.getSingleValue().contains("total_net_input_bytes");
* }</pre>
*/
CompletableFuture<ClusterValue<String>> info(InfoOptions options, Route route);
CompletableFuture<ClusterValue<String>> info(Section[] sections, Route route);

/**
* Rewrites the configuration file with the current configuration.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package glide.api.commands;

import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -31,19 +30,20 @@ public interface ServerManagementCommands {
CompletableFuture<String> info();

/**
* Get information and statistics about the server.
* Get information and statistics about the server.<br>
* Starting from server version 7, command supports multiple section arguments.
*
* @see <a href="https://valkey.io/commands/info/">valkey.io</a> for details.
* @param options A list of {@link Section} values specifying which sections of information to
* @param sections A list of {@link Section} values specifying which sections of information to
* retrieve. When no parameter is provided, the {@link Section#DEFAULT} option is assumed.
* @return A <code>String</code> containing the information for the sections requested.
* @example
* <pre>{@code
* String response = regularClient.info(InfoOptions.builder().section(STATS).build()).get();
* String response = regularClient.info(new Section[] { Section.STATS }).get();
* assert response.contains("total_net_input_bytes");
* }</pre>
*/
CompletableFuture<String> info(InfoOptions options);
CompletableFuture<String> info(Section[] sections);

/**
* Changes the currently selected database.
Expand Down
10 changes: 5 additions & 5 deletions java/client/src/main/java/glide/api/models/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@
import glide.api.models.commands.ExpireOptions;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.GetExOptions;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.LInsertOptions.InsertPosition;
import glide.api.models.commands.LPosOptions;
Expand Down Expand Up @@ -406,15 +405,16 @@ public T info() {
}

/**
* Gets information and statistics about the server.
* Gets information and statistics about the server.<br>
* Starting from server version 7, command supports multiple section arguments.
*
* @see <a href="https://valkey.io/commands/info/">valkey.io</a> for details.
* @param options A list of {@link Section} values specifying which sections of information to
* @param sections A list of {@link Section} values specifying which sections of information to
* retrieve. When no parameter is provided, the {@link Section#DEFAULT} option is assumed.
* @return Command Response - A <code>String</code> containing the requested {@link Section}s.
*/
public T info(@NonNull InfoOptions options) {
protobufTransaction.addCommands(buildCommand(Info, newArgsBuilder().add(options.toArgs())));
public T info(@NonNull Section[] sections) {
protobufTransaction.addCommands(buildCommand(Info, newArgsBuilder().add(sections)));
return getThis();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
package glide.api.models.commands;

import glide.api.commands.ServerManagementCommands;
import java.util.List;
import lombok.Builder;
import lombok.Singular;

/**
* Optional arguments to {@link ServerManagementCommands#info(InfoOptions)}
*
* @see <a href="https://valkey.io/commands/info/">valkey.io</a>
*/
@Builder
public final class InfoOptions {

@Singular private final List<Section> sections;

public enum Section {
/** SERVER: General information about the server */
SERVER,
Expand Down Expand Up @@ -52,13 +46,4 @@ public enum Section {
/** EVERYTHING: Includes all and modules */
EVERYTHING,
}

/**
* Converts options enum into a String[] to add to the command request.
*
* @return String[]
*/
public String[] toArgs() {
return sections.stream().map(Object::toString).toArray(String[]::new);
}
}
15 changes: 5 additions & 10 deletions java/client/src/test/java/glide/api/GlideClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
import glide.api.models.commands.ExpireOptions;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.GetExOptions;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.LPosOptions;
import glide.api.models.commands.ListDirection;
import glide.api.models.commands.RangeOptions;
Expand Down Expand Up @@ -1692,21 +1692,16 @@ public void info_returns_success() {
@Test
public void info_with_multiple_InfoOptions_returns_success() {
// setup
String[] arguments =
new String[] {InfoOptions.Section.ALL.toString(), InfoOptions.Section.DEFAULT.toString()};
String[] arguments = new String[] {Section.ALL.toString(), Section.DEFAULT.toString()};
String testPayload = "Key: Value";
CompletableFuture<String> testResponse = new CompletableFuture<>();
testResponse.complete(testPayload);
when(commandManager.<String>submitNewCommand(eq(Info), eq(arguments), any()))
.thenReturn(testResponse);

// exercise
InfoOptions options =
InfoOptions.builder()
.section(InfoOptions.Section.ALL)
.section(InfoOptions.Section.DEFAULT)
.build();
CompletableFuture<String> response = service.info(options);
Section[] sections = {Section.ALL, Section.DEFAULT};
CompletableFuture<String> response = service.info(sections);
String payload = response.get();

// verify
Expand All @@ -1725,7 +1720,7 @@ public void info_with_empty_InfoOptions_returns_success() {
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.info(InfoOptions.builder().build());
CompletableFuture<String> response = service.info(new Section[0]);
String payload = response.get();

// verify
Expand Down
14 changes: 5 additions & 9 deletions java/client/src/test/java/glide/api/GlideClusterClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.commands.FlushMode;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.SortBaseOptions.Limit;
import glide.api.models.commands.SortClusterOptions;
import glide.api.models.commands.function.FunctionLoadOptions;
Expand Down Expand Up @@ -605,12 +605,8 @@ public void info_with_route_with_infoOptions_returns_string() {
.thenReturn(testResponse);

// exercise
InfoOptions options =
InfoOptions.builder()
.section(InfoOptions.Section.ALL)
.section(InfoOptions.Section.DEFAULT)
.build();
CompletableFuture<ClusterValue<String>> response = service.info(options, route);
Section[] sections = {Section.ALL, Section.DEFAULT};
CompletableFuture<ClusterValue<String>> response = service.info(sections, route);

// verify
assertEquals(testResponse.get(), response.get());
Expand Down Expand Up @@ -655,7 +651,7 @@ public void info_with_options_and_single_node_route_returns_single_value() {

var data = "info string";
try (var client = new TestClient(commandManager, data)) {
var value = client.info(InfoOptions.builder().build(), RANDOM).get();
var value = client.info(new Section[0], RANDOM).get();
assertAll(
() -> assertTrue(value.hasSingleData()),
() -> assertEquals(data, value.getSingleValue()));
Expand All @@ -669,7 +665,7 @@ public void info_with_options_and_multi_node_route_returns_multi_value() {

var data = Map.of("key1", "value1", "key2", "value2");
try (var client = new TestClient(commandManager, data)) {
var value = client.info(InfoOptions.builder().build(), ALL_NODES).get();
var value = client.info(new Section[0], ALL_NODES).get();
assertAll(
() -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
import command_request.CommandRequestOuterClass.RequestType;
import glide.api.models.commands.ConditionalChange;
import glide.api.models.commands.GetExOptions;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.LPosOptions;
import glide.api.models.commands.ListDirection;
import glide.api.models.commands.RangeOptions;
Expand Down Expand Up @@ -352,7 +352,7 @@ public void transaction_builds_protobuf_request(BaseTransaction<?> transaction)
transaction.info();
results.add(Pair.of(Info, buildArgs()));

transaction.info(InfoOptions.builder().section(EVERYTHING).build());
transaction.info(new Section[] {EVERYTHING});
results.add(Pair.of(Info, buildArgs(EVERYTHING.toString())));

transaction.mset(Map.of("key", "value"));
Expand Down
5 changes: 2 additions & 3 deletions java/integTest/src/test/java/glide/TestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import glide.api.GlideClusterClient;
import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.configuration.GlideClientConfiguration;
import glide.api.models.configuration.GlideClusterClientConfiguration;
import glide.api.models.configuration.NodeAddress;
Expand Down Expand Up @@ -388,8 +388,7 @@ public static void waitForNotBusy(BaseClient client) {
*/
@SneakyThrows
public static String getServerVersion(@NonNull final GlideClient glideClient) {
String infoResponse =
glideClient.info(InfoOptions.builder().section(InfoOptions.Section.SERVER).build()).get();
String infoResponse = glideClient.info(new Section[] {Section.SERVER}).get();
Map<String, String> infoResponseMap = parseInfoResponseToMap(infoResponse);
if (infoResponseMap.containsKey(VALKEY_VERSION_KEY)) {
return infoResponseMap.get(VALKEY_VERSION_KEY);
Expand Down
Loading

0 comments on commit e419229

Please sign in to comment.