Return to an async model for push notification latency.
This commit is contained in:
parent
0aa1b80e3e
commit
07bbe7dfb2
|
@ -3,15 +3,13 @@ package org.whispersystems.textsecuregcm.metrics;
|
||||||
import com.codahale.metrics.MetricRegistry;
|
import com.codahale.metrics.MetricRegistry;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import io.lettuce.core.SetArgs;
|
import io.lettuce.core.SetArgs;
|
||||||
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;
|
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
|
||||||
import io.micrometer.core.instrument.Metrics;
|
import io.micrometer.core.instrument.Metrics;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.whispersystems.textsecuregcm.redis.FaultTolerantRedisCluster;
|
import org.whispersystems.textsecuregcm.redis.FaultTolerantRedisCluster;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,8 +27,6 @@ public class PushLatencyManager {
|
||||||
|
|
||||||
private final FaultTolerantRedisCluster redisCluster;
|
private final FaultTolerantRedisCluster redisCluster;
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(PushLatencyManager.class);
|
|
||||||
|
|
||||||
public PushLatencyManager(final FaultTolerantRedisCluster redisCluster) {
|
public PushLatencyManager(final FaultTolerantRedisCluster redisCluster) {
|
||||||
this.redisCluster = redisCluster;
|
this.redisCluster = redisCluster;
|
||||||
}
|
}
|
||||||
|
@ -41,33 +37,29 @@ public class PushLatencyManager {
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void recordPushSent(final UUID accountUuid, final long deviceId, final long currentTime) {
|
void recordPushSent(final UUID accountUuid, final long deviceId, final long currentTime) {
|
||||||
try {
|
redisCluster.useCluster(connection ->
|
||||||
redisCluster.useCluster(connection ->
|
connection.async().set(getFirstUnacknowledgedPushKey(accountUuid, deviceId), String.valueOf(currentTime), SetArgs.Builder.nx().ex(TTL)));
|
||||||
connection.sync().set(getFirstUnacknowledgedPushKey(accountUuid, deviceId), String.valueOf(currentTime), SetArgs.Builder.nx().ex(TTL)));
|
|
||||||
} catch (final Exception e) {
|
|
||||||
log.warn("Failed to record \"push notification sent\" timestamp", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void recordQueueRead(final UUID accountUuid, final long deviceId, final String userAgent) {
|
public void recordQueueRead(final UUID accountUuid, final long deviceId, final String userAgent) {
|
||||||
final Optional<Long> maybeLatency = getLatencyAndClearTimestamp(accountUuid, deviceId, System.currentTimeMillis());
|
getLatencyAndClearTimestamp(accountUuid, deviceId, System.currentTimeMillis()).thenAccept(latency -> {
|
||||||
|
if (latency != null) {
|
||||||
if (maybeLatency.isPresent()) {
|
Metrics.timer(TIMER_NAME, UserAgentTagUtil.getUserAgentTags(userAgent)).record(latency, TimeUnit.MILLISECONDS);
|
||||||
Metrics.timer(TIMER_NAME, UserAgentTagUtil.getUserAgentTags(userAgent)).record(maybeLatency.get(), TimeUnit.MILLISECONDS);
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
Optional<Long> getLatencyAndClearTimestamp(final UUID accountUuid, final long deviceId, final long currentTimeMillis) {
|
CompletableFuture<Long> getLatencyAndClearTimestamp(final UUID accountUuid, final long deviceId, final long currentTimeMillis) {
|
||||||
final String key = getFirstUnacknowledgedPushKey(accountUuid, deviceId);
|
final String key = getFirstUnacknowledgedPushKey(accountUuid, deviceId);
|
||||||
|
|
||||||
return redisCluster.withCluster(connection -> {
|
return redisCluster.withCluster(connection -> {
|
||||||
final RedisAdvancedClusterCommands<String, String> commands = connection.sync();
|
final RedisAdvancedClusterAsyncCommands<String, String> commands = connection.async();
|
||||||
|
|
||||||
final String timestampString = commands.get(key);
|
final CompletableFuture<String> getFuture = commands.get(key).toCompletableFuture();
|
||||||
commands.del(key);
|
commands.del(key);
|
||||||
|
|
||||||
return timestampString != null ? Optional.of(currentTimeMillis - Long.parseLong(timestampString, 10)) : Optional.empty();
|
return getFuture.thenApply(timestampString -> timestampString != null ? currentTimeMillis - Long.parseLong(timestampString, 10) : null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,15 +3,16 @@ package org.whispersystems.textsecuregcm.metrics;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.whispersystems.textsecuregcm.redis.AbstractRedisClusterTest;
|
import org.whispersystems.textsecuregcm.redis.AbstractRedisClusterTest;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
public class PushLatencyManagerTest extends AbstractRedisClusterTest {
|
public class PushLatencyManagerTest extends AbstractRedisClusterTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLatency() {
|
public void testGetLatency() throws ExecutionException, InterruptedException {
|
||||||
final PushLatencyManager pushLatencyManager = new PushLatencyManager(getRedisCluster());
|
final PushLatencyManager pushLatencyManager = new PushLatencyManager(getRedisCluster());
|
||||||
final UUID accountUuid = UUID.randomUUID();
|
final UUID accountUuid = UUID.randomUUID();
|
||||||
final long deviceId = 1;
|
final long deviceId = 1;
|
||||||
|
@ -19,13 +20,13 @@ public class PushLatencyManagerTest extends AbstractRedisClusterTest {
|
||||||
final long pushSentTimestamp = System.currentTimeMillis();
|
final long pushSentTimestamp = System.currentTimeMillis();
|
||||||
final long clearQueueTimestamp = pushSentTimestamp + expectedLatency;
|
final long clearQueueTimestamp = pushSentTimestamp + expectedLatency;
|
||||||
|
|
||||||
assertEquals(Optional.empty(), pushLatencyManager.getLatencyAndClearTimestamp(accountUuid, deviceId, System.currentTimeMillis()));
|
assertNull(pushLatencyManager.getLatencyAndClearTimestamp(accountUuid, deviceId, System.currentTimeMillis()).get());
|
||||||
|
|
||||||
{
|
{
|
||||||
pushLatencyManager.recordPushSent(accountUuid, deviceId, pushSentTimestamp);
|
pushLatencyManager.recordPushSent(accountUuid, deviceId, pushSentTimestamp);
|
||||||
|
|
||||||
assertEquals(Optional.of(expectedLatency), pushLatencyManager.getLatencyAndClearTimestamp(accountUuid, deviceId, clearQueueTimestamp));
|
assertEquals(expectedLatency, (long)pushLatencyManager.getLatencyAndClearTimestamp(accountUuid, deviceId, clearQueueTimestamp).get());
|
||||||
assertEquals(Optional.empty(), pushLatencyManager.getLatencyAndClearTimestamp(accountUuid, deviceId, System.currentTimeMillis()));
|
assertNull(pushLatencyManager.getLatencyAndClearTimestamp(accountUuid, deviceId, System.currentTimeMillis()).get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue