-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor duplicated online retriever code
Signed-off-by: Terence <terencelimxp@gmail.com>
- Loading branch information
1 parent
68973c9
commit aacce71
Showing
6 changed files
with
209 additions
and
249 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
57 changes: 57 additions & 0 deletions
57
.../connectors/redis/src/main/java/feast/storage/connectors/redis/retriever/RedisClient.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,57 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright 2018-2020 The Feast Authors | ||
* | ||
* 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 feast.storage.connectors.redis.retriever; | ||
|
||
import io.lettuce.core.KeyValue; | ||
import io.lettuce.core.RedisFuture; | ||
import io.lettuce.core.RedisURI; | ||
import io.lettuce.core.api.StatefulRedisConnection; | ||
import io.lettuce.core.api.async.RedisAsyncCommands; | ||
import io.lettuce.core.codec.ByteArrayCodec; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RedisClient implements RedisClientWrapper { | ||
|
||
public final RedisAsyncCommands<byte[], byte[]> asyncCommands; | ||
|
||
@Override | ||
public RedisFuture<List<KeyValue<byte[], byte[]>>> hmget(byte[] key, byte[]... fields) { | ||
return asyncCommands.hmget(key, fields); | ||
} | ||
|
||
@Override | ||
public void flushCommands() { | ||
asyncCommands.flushCommands(); | ||
} | ||
|
||
private RedisClient(StatefulRedisConnection<byte[], byte[]> connection) { | ||
this.asyncCommands = connection.async(); | ||
|
||
// Disable auto-flushing | ||
this.asyncCommands.setAutoFlushCommands(false); | ||
} | ||
|
||
public static RedisClientWrapper create(Map<String, String> config) { | ||
StatefulRedisConnection<byte[], byte[]> connection = | ||
io.lettuce.core.RedisClient.create( | ||
RedisURI.create(config.get("host"), Integer.parseInt(config.get("port")))) | ||
.connect(new ByteArrayCodec()); | ||
|
||
return new RedisClient(connection); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...tors/redis/src/main/java/feast/storage/connectors/redis/retriever/RedisClientWrapper.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,26 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright 2018-2020 The Feast Authors | ||
* | ||
* 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 feast.storage.connectors.redis.retriever; | ||
|
||
import io.lettuce.core.*; | ||
import java.util.List; | ||
|
||
public interface RedisClientWrapper { | ||
RedisFuture<List<KeyValue<byte[], byte[]>>> hmget(byte[] key, byte[]... fields); | ||
|
||
void flushCommands(); | ||
} |
106 changes: 106 additions & 0 deletions
106
...tors/redis/src/main/java/feast/storage/connectors/redis/retriever/RedisClusterClient.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,106 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright 2018-2020 The Feast Authors | ||
* | ||
* 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 feast.storage.connectors.redis.retriever; | ||
|
||
import feast.storage.connectors.redis.serializer.RedisKeyPrefixSerializerV2; | ||
import feast.storage.connectors.redis.serializer.RedisKeySerializerV2; | ||
import io.lettuce.core.KeyValue; | ||
import io.lettuce.core.RedisFuture; | ||
import io.lettuce.core.RedisURI; | ||
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; | ||
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands; | ||
import io.lettuce.core.codec.ByteArrayCodec; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
|
||
public class RedisClusterClient implements RedisClientWrapper { | ||
|
||
public final RedisAdvancedClusterAsyncCommands<byte[], byte[]> asyncCommands; | ||
public final RedisKeySerializerV2 serializer; | ||
@Nullable public final RedisKeySerializerV2 fallbackSerializer; | ||
|
||
@Override | ||
public RedisFuture<List<KeyValue<byte[], byte[]>>> hmget(byte[] key, byte[]... fields) { | ||
return asyncCommands.hmget(key, fields); | ||
} | ||
|
||
@Override | ||
public void flushCommands() { | ||
asyncCommands.flushCommands(); | ||
} | ||
|
||
static class Builder { | ||
private final StatefulRedisClusterConnection<byte[], byte[]> connection; | ||
private final RedisKeySerializerV2 serializer; | ||
@Nullable private RedisKeySerializerV2 fallbackSerializer; | ||
|
||
Builder( | ||
StatefulRedisClusterConnection<byte[], byte[]> connection, | ||
RedisKeySerializerV2 serializer) { | ||
this.connection = connection; | ||
this.serializer = serializer; | ||
} | ||
|
||
Builder withFallbackSerializer(RedisKeySerializerV2 fallbackSerializer) { | ||
this.fallbackSerializer = fallbackSerializer; | ||
return this; | ||
} | ||
|
||
RedisClusterClient build() { | ||
return new RedisClusterClient(this); | ||
} | ||
} | ||
|
||
private RedisClusterClient(Builder builder) { | ||
this.asyncCommands = builder.connection.async(); | ||
this.serializer = builder.serializer; | ||
this.fallbackSerializer = builder.fallbackSerializer; | ||
|
||
// Disable auto-flushing | ||
this.asyncCommands.setAutoFlushCommands(false); | ||
} | ||
|
||
public static RedisClientWrapper create(Map<String, String> config) { | ||
List<RedisURI> redisURIList = | ||
Arrays.stream(config.get("connection_string").split(",")) | ||
.map( | ||
hostPort -> { | ||
String[] hostPortSplit = hostPort.trim().split(":"); | ||
return RedisURI.create(hostPortSplit[0], Integer.parseInt(hostPortSplit[1])); | ||
}) | ||
.collect(Collectors.toList()); | ||
StatefulRedisClusterConnection<byte[], byte[]> connection = | ||
io.lettuce.core.cluster.RedisClusterClient.create(redisURIList) | ||
.connect(new ByteArrayCodec()); | ||
|
||
RedisKeySerializerV2 serializer = | ||
new RedisKeyPrefixSerializerV2(config.getOrDefault("key_prefix", "")); | ||
|
||
Builder builder = new Builder(connection, serializer); | ||
|
||
if (Boolean.parseBoolean(config.getOrDefault("enable_fallback", "false"))) { | ||
RedisKeySerializerV2 fallbackSerializer = | ||
new RedisKeyPrefixSerializerV2(config.getOrDefault("fallback_prefix", "")); | ||
builder = builder.withFallbackSerializer(fallbackSerializer); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
Oops, something went wrong.