use of io.lettuce.core.RedisFuture in project jetcache by alibaba.
the class RedisLettuceCacheFailTest method mockFuture.
private RedisFuture mockFuture(Object value, Throwable ex) {
RedisFuture redisFuture = mock(RedisFuture.class);
when(redisFuture.handle(any())).thenAnswer((invoke) -> {
BiFunction function = invoke.getArgument(0);
Object resultData = function.apply(value, ex);
return CompletableFuture.completedFuture(resultData);
});
return redisFuture;
}
use of io.lettuce.core.RedisFuture in project austin by ZhongFuCheng3y.
the class AustinSink method realTimeData.
/**
* 实时数据存入Redis
* 1.用户维度(查看用户当天收到消息的链路详情),数量级大,只保留当天
* 2.消息模板维度(查看消息模板整体下发情况),数量级小,保留30天
*
* @param info
*/
private void realTimeData(AnchorInfo info) {
try {
LettuceRedisUtils.pipeline(redisAsyncCommands -> {
List<RedisFuture<?>> redisFutures = new ArrayList<>();
/**
* 1.构建userId维度的链路信息 数据结构list:{key,list}
* key:userId,listValue:[{timestamp,state,businessId},{timestamp,state,businessId}]
*/
SimpleAnchorInfo simpleAnchorInfo = SimpleAnchorInfo.builder().businessId(info.getBusinessId()).state(info.getState()).timestamp(info.getTimestamp()).build();
for (String id : info.getIds()) {
redisFutures.add(redisAsyncCommands.lpush(id.getBytes(), JSON.toJSONString(simpleAnchorInfo).getBytes()));
redisFutures.add(redisAsyncCommands.expire(id.getBytes(), (DateUtil.endOfDay(new Date()).getTime() - DateUtil.current()) / 1000));
}
/**
* 2.构建消息模板维度的链路信息 数据结构hash:{key,hash}
* key:businessId,hashValue:{state,stateCount}
*/
redisFutures.add(redisAsyncCommands.hincrby(String.valueOf(info.getBusinessId()).getBytes(), String.valueOf(info.getState()).getBytes(), info.getIds().size()));
redisFutures.add(redisAsyncCommands.expire(String.valueOf(info.getBusinessId()).getBytes(), (DateUtil.offsetDay(new Date(), 30).getTime()) / 1000));
return redisFutures;
});
} catch (Exception e) {
log.error("AustinSink#invoke error: {}", Throwables.getStackTraceAsString(e));
}
}
use of io.lettuce.core.RedisFuture in project api-layer by zowe.
the class RedisOperatorTest method givenRedisExecutionExceptionNotOutOfMemory_thenThrowRetryableRedisException.
@Test
void givenRedisExecutionExceptionNotOutOfMemory_thenThrowRetryableRedisException() throws ExecutionException, InterruptedException {
RedisFuture<Boolean> future = (RedisFuture<Boolean>) mock(RedisFuture.class);
when(redisCommands.hsetnx(any(), any(), any())).thenReturn(future);
when(future.get()).thenThrow(new ExecutionException(new RedisCommandExecutionException("error")));
assertThrows(RetryableRedisException.class, () -> underTest.create(REDIS_ENTRY));
}
use of io.lettuce.core.RedisFuture in project lettuce-core by lettuce-io.
the class RedisClusterSetupTest method atLeastOnceForgetNodeFailover.
@Test
public void atLeastOnceForgetNodeFailover() throws Exception {
clusterClient.setOptions(ClusterClientOptions.builder().topologyRefreshOptions(PERIODIC_REFRESH_ENABLED).build());
RedisAdvancedClusterAsyncCommands<String, String> clusterConnection = clusterClient.connect().async();
clusterClient.setOptions(ClusterClientOptions.create());
ClusterSetup.setup2Masters(clusterHelper);
assertRoutedExecution(clusterConnection);
RedisClusterNode partition1 = getOwnPartition(redis1);
RedisClusterNode partition2 = getOwnPartition(redis2);
RedisClusterAsyncCommands<String, String> node1Connection = clusterConnection.getConnection(partition1.getUri().getHost(), partition1.getUri().getPort());
RedisClusterAsyncCommands<String, String> node2Connection = clusterConnection.getConnection(partition2.getUri().getHost(), partition2.getUri().getPort());
shiftAllSlotsToNode1();
suspendConnection(node2Connection);
List<RedisFuture<String>> futures = new ArrayList<>();
// 15891
futures.add(clusterConnection.set("t", "value"));
// 16023
futures.add(clusterConnection.set("p", "value"));
// 6373
clusterConnection.set("A", "value").get(1, TimeUnit.SECONDS);
for (RedisFuture<String> future : futures) {
assertThat(future.isDone()).isFalse();
assertThat(future.isCancelled()).isFalse();
}
redis1.clusterForget(partition2.getNodeId());
redis2.clusterForget(partition1.getNodeId());
Partitions partitions = clusterClient.getPartitions();
partitions.remove(partition2);
partitions.getPartition(0).setSlots(Arrays.stream(createSlots(0, 16384)).boxed().collect(Collectors.toList()));
partitions.updateCache();
clusterClient.updatePartitionsInConnections();
Wait.untilTrue(() -> TestFutures.areAllDone(futures)).waitOrTimeout();
assertRoutedExecution(clusterConnection);
clusterConnection.getStatefulConnection().close();
}
use of io.lettuce.core.RedisFuture in project micronaut-redis by micronaut-projects.
the class RedisSessionStore method saveSessionDelta.
private CompletableFuture<RedisSession> saveSessionDelta(RedisSession session, Map<byte[], byte[]> changes) {
Duration maxInactiveInterval = session.getMaxInactiveInterval();
long expirySeconds = maxInactiveInterval.getSeconds();
byte[] sessionKey = getSessionKey(session.getId());
byte[] sessionIdBytes = session.getId().getBytes(charset);
if (expirySeconds == 0) {
// delete the expired session
RedisFuture<Long> deleteOp = redisKeyAsyncCommands.del(getExpiryKey(session));
return deleteOp.thenCompose(ignore -> redisHashAsyncCommands.hmset(sessionKey, changes)).thenApply(ignore -> session).toCompletableFuture();
}
return redisHashAsyncCommands.hmset(sessionKey, changes).thenCompose(ignore -> {
try {
if (session.isNew()) {
session.clearModifications();
baseRedisAsyncCommands.publish(sessionCreatedTopic, sessionIdBytes).whenComplete((aLong, throwable12) -> {
if (throwable12 != null) {
if (LOG.isErrorEnabled()) {
LOG.error("Error publishing session creation event: " + throwable12.getMessage(), throwable12);
}
}
});
} else {
session.clearModifications();
}
} catch (Throwable e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error publishing session creation event: " + e.getMessage(), e);
}
}
long fiveMinutesAfterExpires = expirySeconds + TimeUnit.MINUTES.toSeconds(EXPIRATION_SECONDS);
byte[] expiryKey = getExpiryKey(session);
double expireTimeScore = Long.valueOf(Instant.now().plus(expirySeconds, ChronoUnit.SECONDS).toEpochMilli()).doubleValue();
CompletableFuture<Boolean> expireOp = redisKeyAsyncCommands.expire(sessionKey, fiveMinutesAfterExpires).toCompletableFuture();
CompletableFuture<String> saveExpiryOp = redisStringAsyncCommands.setex(expiryKey, expirySeconds, String.valueOf(expirySeconds).getBytes()).toCompletableFuture();
CompletableFuture<Long> saveActiveSessionOp = redisSortedSetAsyncCommands.zadd(activeSessionsSet, expireTimeScore, sessionIdBytes).toCompletableFuture();
return CompletableFuture.allOf(expireOp, saveExpiryOp, saveActiveSessionOp).thenApply(ignore2 -> session);
}).toCompletableFuture();
}
Aggregations