Neha
08/05/2023, 9:47 AMDanny Cranmer
08/05/2023, 11:35 AMNeha
08/05/2023, 3:23 PMNeha
08/05/2023, 3:24 PMpublic class RedisStateSerializer extends
AsyncSinkWriterStateSerializer<Tuple3<String, String, Long>> {
@Override
protected void serializeRequestToStream(Tuple3<String, String, Long> request,
DataOutputStream out) throws IOException {
out.writeInt(request.f0.length());
out.writeBytes(request.f0);
out.writeInt(request.f1.length());
out.writeBytes(request.f1);
out.writeLong(request.f2);
}
@Override
protected Tuple3<String, String, Long> deserializeRequestFromStream(long requestSize,
DataInputStream in)
throws IOException {
return new Tuple3<>(readString(in), readString(in), in.readLong());
}
public String readString(DataInputStream dataInputStream) throws IOException {
int length = dataInputStream.readInt();
byte[] bytes = new byte[length];
int bytesRead = 0;
while (bytesRead < length) {
int result = dataInputStream.read(bytes, bytesRead, length - bytesRead);
if (result == -1) {
throw new IOException("Premature end of stream.");
}
bytesRead += result;
}
return new String(bytes, StandardCharsets.UTF_8);
}
@Override
public int getVersion() {
return 1;
}
}
Is this class the problem in any way?Danny Cranmer
08/05/2023, 6:57 PMNeha
08/06/2023, 2:52 AMpublic class RedisSinkWriter<T> extends AsyncSinkWriter<T, Tuple3<String, String, Long>> {
private static final ch.qos.logback.classic.Logger LOG =
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger("RollingAppender");
private RedisURI redisUrl;
private transient RedisClusterClient client = null;
private transient StatefulRedisClusterConnection<String, String> clusterConnection = null;
private transient RedisAdvancedClusterAsyncCommands<String, String> asyncCall = null;
public RedisSinkWriter(RedisURI redisUrl,
ElementConverter<T, Tuple3<String, String, Long>> elementConverter,
Sink.InitContext context, int maxBatchSize, int maxInFlightRequests,
int maxBufferedRequests, long maxBatchSizeInBytes,
long maxTimeInBufferMS, long maxRecordSizeInBytes,
Collection<BufferedRequestState<Tuple3<String, String, Long>>>
bufferedRequestStates) {
super(elementConverter, context, maxBatchSize, maxInFlightRequests, maxBufferedRequests,
maxBatchSizeInBytes, maxTimeInBufferMS, maxRecordSizeInBytes,
bufferedRequestStates);
this.redisUrl = redisUrl;
if (client == null) {
client = RedisClusterClient.create(redisUrl);
ClusterTopologyRefreshOptions topologyRefreshOptions =
ClusterTopologyRefreshOptions.builder()
.enableAllAdaptiveRefreshTriggers()
.enablePeriodicRefresh(Duration.ofHours(1))
.dynamicRefreshSources(false)
.build();
client.setOptions(ClusterClientOptions.builder().
topologyRefreshOptions(topologyRefreshOptions).build());
}
if (clusterConnection == null) {
clusterConnection = client.connect();
}
if (asyncCall == null) {
asyncCall = clusterConnection.async();
}
}
@Override
protected void submitRequestEntries(List<Tuple3<String, String, Long>> requestEntries,
Consumer<List<Tuple3<String, String, Long>>>
requestToRetry) {
for (Tuple3<String, String, Long> requestEntry : requestEntries) {
if(requestEntry.f0!=null){
RedisFuture<String> future = asyncCall.set(requestEntry.f0, requestEntry.f1);
future.whenComplete((s, throwable) -> {
if (throwable != null) {
requestToRetry.accept(Collections.singletonList(requestEntry));
}
});
} else {
LOG.error("redis.async.makeRequest: {} ", "Null key");
}
}
}
@Override
protected long getSizeInBytes(Tuple3<String, String, Long> requestEntry) {
return requestEntry.f0.length() * 2 +
requestEntry.f1.length() * 2;
}
@Override
public void close(){
if (client != null) {
client.shutdown();
}
}
}Danny Cranmer
08/06/2023, 7:56 AMsubmitRequestEntries you're missing a requestEntriesToRetry(emptyList()). When throwable is null. This signals the sink that the request is completeDanny Cranmer
08/06/2023, 7:57 AMNeha
08/08/2023, 12:14 AMDanny Cranmer
08/08/2023, 9:37 AMNeha
08/09/2023, 8:32 AM