Skip to content

Commit

Permalink
Implemented dynamic parsing of the result returned from CLIENT TRACKI…
Browse files Browse the repository at this point in the history
…NGINFO
  • Loading branch information
tishun committed Aug 5, 2024
1 parent 843f8fd commit 69e0814
Show file tree
Hide file tree
Showing 23 changed files with 763 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.lettuce.core.output.KeyValueStreamingChannel;
import io.lettuce.core.output.ScoredValueStreamingChannel;
import io.lettuce.core.output.ValueStreamingChannel;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.AsyncCommand;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
Expand Down Expand Up @@ -390,7 +391,7 @@ public RedisFuture<String> clientTracking(TrackingArgs args) {
}

@Override
public RedisFuture<List<Object>> clientTrackinginfo() {
public RedisFuture<DynamicAggregateData> clientTrackinginfo() {
return dispatch(commandBuilder.clientTrackinginfo());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.lettuce.core.output.KeyValueStreamingChannel;
import io.lettuce.core.output.ScoredValueStreamingChannel;
import io.lettuce.core.output.ValueStreamingChannel;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandType;
Expand Down Expand Up @@ -408,8 +409,8 @@ public Mono<String> clientTracking(TrackingArgs args) {
}

@Override
public Flux<Object> clientTrackinginfo() {
return createDissolvingFlux(commandBuilder::clientTrackinginfo);
public Mono<DynamicAggregateData> clientTrackinginfo() {
return createMono(commandBuilder::clientTrackinginfo);
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.lettuce.core.models.stream.PendingMessage;
import io.lettuce.core.models.stream.PendingMessages;
import io.lettuce.core.output.*;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.BaseRedisCommandBuilder;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
Expand Down Expand Up @@ -528,10 +529,10 @@ Command<K, V, String> clientTracking(TrackingArgs trackingArgs) {
return createCommand(CLIENT, new StatusOutput<>(codec), args);
}

Command<K, V, List<Object>> clientTrackinginfo() {
Command<K, V, DynamicAggregateData> clientTrackinginfo() {
CommandArgs<K, V> args = new CommandArgs<>(codec).add(TRACKINGINFO);

return new Command<>(CLIENT, new ArrayOutput<>(codec), args);
return new Command<>(CLIENT, new DynamicAggregateOutput<>(codec), args);
}

Command<K, V, Long> clientUnblock(long id, UnblockType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.lettuce.core.ShutdownArgs;
import io.lettuce.core.TrackingArgs;
import io.lettuce.core.UnblockType;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.CommandType;

/**
Expand Down Expand Up @@ -180,12 +181,12 @@ public interface RedisServerAsyncCommands<K, V> {
/**
* Returns information about the current client connection's use of the server assisted client side caching feature.
*
* @return List&lt;Object&gt; array-list-reply, for more information check the documentation
* @see io.lettuce.core.cluster.models.tracking.TrackingInfoParser
* @see io.lettuce.core.cluster.models.tracking.TrackingInfo
* @return {@link DynamicAggregateData}, for more information check the documentation
* @see io.lettuce.core.api.parsers.tracking.TrackingInfoParser
* @see io.lettuce.core.api.parsers.tracking.TrackingInfo
* @since 7.0
*/
RedisFuture<List<Object>> clientTrackinginfo();
RedisFuture<DynamicAggregateData> clientTrackinginfo();

/**
* Unblock the specified blocked client.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
package io.lettuce.core.cluster.models.tracking;
/*
* Copyright 2024, Redis Ltd. and Contributors
* All rights reserved.
*
* Licensed under the MIT License.
*
* This file contains contributions from third-party contributors
* licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.lettuce.core.api.parsers.tracking;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.List;

Expand Down Expand Up @@ -57,6 +78,26 @@ public List<String> getPrefixes() {
return Collections.unmodifiableList(prefixes);
}

@Override
public String toString() {
return "TrackingInfo{" + "flags=" + flags + ", redirect=" + redirect + ", prefixes=" + prefixes + '}';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
TrackingInfo that = (TrackingInfo) o;
return redirect == that.redirect && Objects.equals(flags, that.flags) && Objects.equals(prefixes, that.prefixes);
}

@Override
public int hashCode() {
return Objects.hash(flags, redirect, prefixes);
}

/**
* CLIENT TRACKINGINFO flags
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024, Redis Ltd. and Contributors
* All rights reserved.
*
* Licensed under the MIT License.
*
* This file contains contributions from third-party contributors
* licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.lettuce.core.api.parsers.tracking;

import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.CommandKeyword;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Parser for Redis <a href="https://redis.io/docs/latest/commands/client-trackinginfo/">CLIENT TRACKINGINFO</a> command output.
*
* @author Tihomir Mateev
* @since 7.0
*/
public class TrackingInfoParser {

/**
* Utility constructor.
*/
private TrackingInfoParser() {
}

/**
* Parse the output of the Redis CLIENT TRACKINGINFO command and convert it to a {@link TrackingInfo}
*
* @param trackinginfoOutput output of CLIENT TRACKINGINFO command
* @return an {@link TrackingInfo} instance
*/
public static TrackingInfo parse(DynamicAggregateData trackinginfoOutput) {

verifyStructure(trackinginfoOutput);

Map<Object, Object> data = trackinginfoOutput.getDynamicMap();
Set<?> flags = ((DynamicAggregateData) data.get(CommandKeyword.FLAGS.toString().toLowerCase())).getDynamicSet();
Long clientId = (Long) data.get(CommandKeyword.REDIRECT.toString().toLowerCase());
List<?> prefixes = ((DynamicAggregateData) data.get(CommandKeyword.PREFIXES.toString().toLowerCase())).getDynamicList();

Set<TrackingInfo.TrackingFlag> parsedFlags = new HashSet<>();
List<String> parsedPrefixes = new ArrayList<>();

for (Object flag : flags) {
String toParse = (String) flag;
parsedFlags.add(TrackingInfo.TrackingFlag.from(toParse));
}

for (Object prefix : prefixes) {
parsedPrefixes.add((String) prefix);
}

return new TrackingInfo(parsedFlags, clientId, parsedPrefixes);
}

private static void verifyStructure(DynamicAggregateData trackinginfoOutput) {

if (trackinginfoOutput == null || trackinginfoOutput.getDynamicMap().isEmpty()) {
throw new IllegalArgumentException("trackinginfoOutput must not be null or empty");
}

Map<Object, Object> data = trackinginfoOutput.getDynamicMap();

if (!data.containsKey(CommandKeyword.FLAGS.toString().toLowerCase())
|| !data.containsKey(CommandKeyword.REDIRECT.toString().toLowerCase())
|| !data.containsKey(CommandKeyword.PREFIXES.toString().toLowerCase())) {
throw new IllegalArgumentException("trackinginfoOutput has missing flags");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024, Redis Ltd. and Contributors
* All rights reserved.
*
* Licensed under the MIT License.
*
* This file contains contributions from third-party contributors
* licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Model and parser for the {@code CLIENT TRACKINGINFO} output.
*/
package io.lettuce.core.api.parsers.tracking;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.lettuce.core.ShutdownArgs;
import io.lettuce.core.TrackingArgs;
import io.lettuce.core.UnblockType;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.CommandType;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -180,12 +181,12 @@ public interface RedisServerReactiveCommands<K, V> {
/**
* Returns information about the current client connection's use of the server assisted client side caching feature.
*
* @return Object array-list-reply, for more information check the documentation
* @see io.lettuce.core.cluster.models.tracking.TrackingInfoParser
* @see io.lettuce.core.cluster.models.tracking.TrackingInfo
* @return {@link DynamicAggregateData}, for more information check the documentation
* @see io.lettuce.core.api.parsers.tracking.TrackingInfoParser
* @see io.lettuce.core.api.parsers.tracking.TrackingInfo
* @since 7.0
*/
Flux<Object> clientTrackinginfo();
Mono<DynamicAggregateData> clientTrackinginfo();

/**
* Unblock the specified blocked client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.lettuce.core.ShutdownArgs;
import io.lettuce.core.TrackingArgs;
import io.lettuce.core.UnblockType;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.CommandType;

/**
Expand Down Expand Up @@ -179,12 +180,12 @@ public interface RedisServerCommands<K, V> {
/**
* Returns information about the current client connection's use of the server assisted client side caching feature.
*
* @return List&lt;Object&gt; array-list-reply, for more information check the documentation
* @see io.lettuce.core.cluster.models.tracking.TrackingInfoParser
* @see io.lettuce.core.cluster.models.tracking.TrackingInfo
* @return {@link DynamicAggregateData}, for more information check the documentation
* @see io.lettuce.core.api.parsers.tracking.TrackingInfoParser
* @see io.lettuce.core.api.parsers.tracking.TrackingInfo
* @since 7.0
*/
List<Object> clientTrackinginfo();
DynamicAggregateData clientTrackinginfo();

/**
* Unblock the specified blocked client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.lettuce.core.KillArgs;
import io.lettuce.core.TrackingArgs;
import io.lettuce.core.UnblockType;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.CommandType;

/**
Expand Down Expand Up @@ -178,12 +179,12 @@ public interface NodeSelectionServerAsyncCommands<K, V> {
/**
* Returns information about the current client connection's use of the server assisted client side caching feature.
*
* @return List&lt;Object&gt; array-list-reply, for more information check the documentation
* @see io.lettuce.core.cluster.models.tracking.TrackingInfoParser
* @see io.lettuce.core.cluster.models.tracking.TrackingInfo
* @return {@link DynamicAggregateData}, for more information check the documentation
* @see io.lettuce.core.api.parsers.tracking.TrackingInfoParser
* @see io.lettuce.core.api.parsers.tracking.TrackingInfo
* @since 7.0
*/
AsyncExecutions<List<Object>> clientTrackinginfo();
AsyncExecutions<DynamicAggregateData> clientTrackinginfo();

/**
* Unblock the specified blocked client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.lettuce.core.KillArgs;
import io.lettuce.core.TrackingArgs;
import io.lettuce.core.UnblockType;
import io.lettuce.core.output.data.DynamicAggregateData;
import io.lettuce.core.protocol.CommandType;

/**
Expand Down Expand Up @@ -178,12 +179,12 @@ public interface NodeSelectionServerCommands<K, V> {
/**
* Returns information about the current client connection's use of the server assisted client side caching feature.
*
* @return List&lt;Object&gt; array-list-reply, for more information check the documentation
* @see io.lettuce.core.cluster.models.tracking.TrackingInfoParser
* @see io.lettuce.core.cluster.models.tracking.TrackingInfo
* @return {@link DynamicAggregateData}, for more information check the documentation
* @see io.lettuce.core.api.parsers.tracking.TrackingInfoParser
* @see io.lettuce.core.api.parsers.tracking.TrackingInfo
* @since 7.0
*/
Executions<List<Object>> clientTrackinginfo();
Executions<DynamicAggregateData> clientTrackinginfo();

/**
* Unblock the specified blocked client.
Expand Down
Loading

0 comments on commit 69e0814

Please sign in to comment.