forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#11640] Async timeseries command support
- Loading branch information
Showing
16 changed files
with
445 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...s/src/main/java/com/navercorp/pinpoint/redis/timeseries/RedisTimeseriesAsyncCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.navercorp.pinpoint.redis.timeseries; | ||
|
||
import com.navercorp.pinpoint.redis.timeseries.model.TimestampValuePair; | ||
import io.lettuce.core.RedisFuture; | ||
|
||
import java.util.List; | ||
|
||
public interface RedisTimeseriesAsyncCommands { | ||
|
||
|
||
RedisFuture<Long> tsAdd(String key, long timestamp, double value); | ||
|
||
RedisFuture<Long> tsAdd(String key, long timestamp, double value, TsAddArgs addOptions); | ||
|
||
|
||
/** | ||
* Delete data in the range of fromTimestamp to toTimestamp. | ||
* @param key key | ||
* @param fromTimestamp fromTimestamp | ||
* @param toTimestamp toTimestamp | ||
* @return timestamp | ||
*/ | ||
// long tsDel(String key, long fromTimestamp, long toTimestamp); | ||
// | ||
RedisFuture<List<TimestampValuePair>> tsRange(String key, long fromTimestamp, long toTimestamp); | ||
// | ||
// TimestampValuePair tsGet(String key); | ||
// | ||
// List<TimestampValuePair> tsRevrange(String key, long fromTimestamp, long toTimestamp); | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
...c/main/java/com/navercorp/pinpoint/redis/timeseries/RedisTimeseriesAsyncCommandsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.navercorp.pinpoint.redis.timeseries; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.navercorp.pinpoint.redis.timeseries.connection.AsyncConnection; | ||
import com.navercorp.pinpoint.redis.timeseries.connection.CommandDispatcher; | ||
import com.navercorp.pinpoint.redis.timeseries.model.TimestampValuePair; | ||
import com.navercorp.pinpoint.redis.timeseries.protocol.TS; | ||
import io.lettuce.core.CompositeArgument; | ||
import io.lettuce.core.RedisFuture; | ||
import io.lettuce.core.codec.RedisCodec; | ||
import io.lettuce.core.codec.StringCodec; | ||
import io.lettuce.core.output.CommandOutput; | ||
import io.lettuce.core.output.IntegerOutput; | ||
import io.lettuce.core.protocol.Command; | ||
import io.lettuce.core.protocol.CommandArgs; | ||
import io.lettuce.core.protocol.RedisCommand; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class RedisTimeseriesAsyncCommandsImpl implements RedisTimeseriesAsyncCommands { | ||
|
||
private final RedisCodec<String, String> commandCodec = StringCodec.ASCII; | ||
|
||
private final RedisCodec<String, String> outputCodec = StringCodec.UTF8; | ||
|
||
private final AsyncConnection<String, String> connection; | ||
|
||
public RedisTimeseriesAsyncCommandsImpl(AsyncConnection<String, String> connection) { | ||
this.connection = Objects.requireNonNull(connection, "connection"); | ||
} | ||
|
||
public RedisFuture<Long> tsAdd(String key, long timestamp, double value) { | ||
Preconditions.checkArgument(timestamp >= 0, "timestamp must be greater than or equal to 0"); | ||
|
||
return tsAdd(key, timestamp, value, null); | ||
} | ||
|
||
public RedisFuture<Long> tsAdd(String key, long timestamp, double value, TsAddArgs options) { | ||
Preconditions.checkArgument(timestamp >= 0, "timestamp must be greater than or equal to 0"); | ||
|
||
CommandArgs<String, String> args = new CommandArgs<>(commandCodec) | ||
.addKey(key) | ||
.add(timestamp) | ||
.addValue(Double.toString(value)); | ||
|
||
applyOptions(args, options); | ||
|
||
final CommandOutput<String, String, Long> output = new IntegerOutput<>(outputCodec); | ||
|
||
RedisCommand<String, String, Long> command = new Command<>(TS.ADD, output, args); | ||
|
||
return commands().dispatch(command); | ||
} | ||
|
||
private CommandDispatcher<String, String> commands() { | ||
return connection.async(); | ||
} | ||
|
||
private <K, V> void applyOptions(CommandArgs<K, V> args, CompositeArgument options) { | ||
if (options != null) { | ||
options.build(args); | ||
} | ||
} | ||
|
||
@Override | ||
public RedisFuture<List<TimestampValuePair>> tsRange(String key, long fromTimestamp, long toTimestamp) { | ||
Preconditions.checkArgument(fromTimestamp >= 0, "fromTimestamp must be greater than or equal to 0"); | ||
Preconditions.checkArgument(toTimestamp >= 0, "toTimestamp must be greater than or equal to 0"); | ||
|
||
CommandArgs<String, String> args = new CommandArgs<>(commandCodec) | ||
.addKey(key) | ||
.add(fromTimestamp) | ||
.add(toTimestamp); | ||
ArrayTimestampValueOutput<String, String> output = new ArrayTimestampValueOutput<>(outputCodec); | ||
|
||
RedisCommand<String, String, List<TimestampValuePair>> command = new Command<>(TS.RANGE, output, args); | ||
return commands().dispatch(command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...ies/src/main/java/com/navercorp/pinpoint/redis/timeseries/connection/AsyncConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.navercorp.pinpoint.redis.timeseries.connection; | ||
|
||
public interface AsyncConnection<K, V> extends AutoCloseable { | ||
CommandDispatcher<K, V> async(); | ||
|
||
@Override | ||
void close(); | ||
} |
53 changes: 53 additions & 0 deletions
53
.../main/java/com/navercorp/pinpoint/redis/timeseries/connection/ClusterAsyncConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.navercorp.pinpoint.redis.timeseries.connection; | ||
|
||
import io.lettuce.core.RedisFuture; | ||
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; | ||
import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands; | ||
import io.lettuce.core.protocol.AsyncCommand; | ||
import io.lettuce.core.protocol.RedisCommand; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ClusterAsyncConnection<K, V> implements AsyncConnection<K, V> { | ||
private final StatefulRedisClusterConnection<K, V> connection; | ||
|
||
public ClusterAsyncConnection(StatefulRedisClusterConnection<K, V> connection) { | ||
this.connection = Objects.requireNonNull(connection, "connection"); | ||
} | ||
|
||
@Override | ||
public CommandDispatcher<K, V> async() { | ||
return new CommandDispatcher<>() { | ||
@Override | ||
public <T> RedisFuture<T> dispatch(RedisCommand<K, V, T> command) { | ||
AsyncCommand<K, V, T> asyncCommand = new AsyncCommand<>(command); | ||
|
||
RedisCommand<K, V, T> result = connection.dispatch(asyncCommand); | ||
|
||
return (AsyncCommand<K, V, T>) result; | ||
} | ||
|
||
@Override | ||
public <T> Collection<RedisFuture<T>> dispatch(Collection<RedisCommand<K, V, T>> command) { | ||
List<AsyncCommand<K, V, T>> async = command.stream().map(AsyncCommand::new).toList(); | ||
|
||
Collection<RedisCommand<K, V, ?>> dispatch = connection.dispatch(async); | ||
|
||
return dispatch.stream().map(c -> (RedisFuture<T>) c).toList(); | ||
} | ||
}; | ||
} | ||
|
||
private RedisClusterAsyncCommands<K, V> commands() { | ||
// pipelining | ||
// connection.setAutoFlushCommands(false); | ||
return connection.async(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
connection.close(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...s/src/main/java/com/navercorp/pinpoint/redis/timeseries/connection/CommandDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.navercorp.pinpoint.redis.timeseries.connection; | ||
|
||
import io.lettuce.core.RedisFuture; | ||
import io.lettuce.core.protocol.RedisCommand; | ||
|
||
import java.util.Collection; | ||
|
||
public interface CommandDispatcher<K, V> { | ||
|
||
<T> RedisFuture<T> dispatch(RedisCommand<K, V, T> command); | ||
|
||
<T> Collection<RedisFuture<T>> dispatch(Collection<RedisCommand<K, V, T>> commands); | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...s/src/main/java/com/navercorp/pinpoint/redis/timeseries/connection/ConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.navercorp.pinpoint.redis.timeseries.connection; | ||
|
||
public interface ConnectionFactory<K, V> { | ||
AsyncConnection<K, V> getConnection(); | ||
} |
50 changes: 50 additions & 0 deletions
50
...c/main/java/com/navercorp/pinpoint/redis/timeseries/connection/SimpleAsyncConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.navercorp.pinpoint.redis.timeseries.connection; | ||
|
||
import io.lettuce.core.RedisFuture; | ||
import io.lettuce.core.api.StatefulRedisConnection; | ||
import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands; | ||
import io.lettuce.core.protocol.AsyncCommand; | ||
import io.lettuce.core.protocol.RedisCommand; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class SimpleAsyncConnection<K, V> implements AsyncConnection<K, V> { | ||
private final StatefulRedisConnection<K, V> connection; | ||
|
||
public SimpleAsyncConnection(StatefulRedisConnection<K, V> connection) { | ||
this.connection = Objects.requireNonNull(connection, "connection"); | ||
} | ||
|
||
public CommandDispatcher<K, V> async() { | ||
return new CommandDispatcher<>() { | ||
@Override | ||
public <T> RedisFuture<T> dispatch(RedisCommand<K, V, T> command) { | ||
AsyncCommand<K, V, T> asyncCommand = new AsyncCommand<>(command); | ||
RedisCommand<K, V, T> result = connection.dispatch(asyncCommand); | ||
return (AsyncCommand<K, V, T>) result; | ||
} | ||
|
||
@Override | ||
public <T> Collection<RedisFuture<T>> dispatch(Collection<RedisCommand<K, V, T>> command) { | ||
List<AsyncCommand<K, V, T>> async = command.stream().map(AsyncCommand::new).toList(); | ||
|
||
Collection<RedisCommand<K, V, ?>> dispatch = connection.dispatch(async); | ||
|
||
return dispatch.stream().map(c -> (RedisFuture<T>) c).toList(); | ||
} | ||
}; | ||
} | ||
|
||
private RedisClusterAsyncCommands<K, V> commands() { | ||
// pipelining | ||
// connection.setAutoFlushCommands(false); | ||
return connection.async(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
connection.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.