From 5ab421a4bac14fe92a74d04cc44373267979b7eb Mon Sep 17 00:00:00 2001 From: emeroad Date: Mon, 14 Oct 2024 10:30:54 +0900 Subject: [PATCH] [#11497] Improve atomicity and performance of Redis call in ActiveThread --- .../service/state/RedisCollectorStatePublisherService.java | 3 +-- .../state/RedisCollectorStatePublisherServiceTest.java | 6 ++---- .../pinpoint/channel/redis/kv/RedisKVPubChannel.java | 3 +-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/realtime/realtime-collector/src/main/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherService.java b/realtime/realtime-collector/src/main/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherService.java index 7803334a821e..b0900262d887 100644 --- a/realtime/realtime-collector/src/main/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherService.java +++ b/realtime/realtime-collector/src/main/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherService.java @@ -61,8 +61,7 @@ public void publish(CollectorState state) { private void publishBytes(byte[] bytes) { String value = BytesUtils.toString(bytes); - this.redisTemplate.opsForValue().set(this.key, value); - this.redisTemplate.expire(this.key, this.ttl); + this.redisTemplate.opsForValue().set(this.key, value, this.ttl); } } diff --git a/realtime/realtime-collector/src/test/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherServiceTest.java b/realtime/realtime-collector/src/test/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherServiceTest.java index 703a5d7a74e0..9cf93dc8be6f 100644 --- a/realtime/realtime-collector/src/test/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherServiceTest.java +++ b/realtime/realtime-collector/src/test/java/com/navercorp/pinpoint/realtime/collector/service/state/RedisCollectorStatePublisherServiceTest.java @@ -52,8 +52,7 @@ public void test() throws IOException { doReturn(TEST_RESULT.getBytes()).when(serde).serializeToByteArray(eq(state)); doReturn(redisValueOperations).when(redisTemplate).opsForValue(); - doReturn(true).when(redisTemplate).expire(eq(TEST_KEY), any()); - doNothing().when(redisValueOperations).set(eq(TEST_KEY), eq(TEST_RESULT)); + doNothing().when(redisValueOperations).set(eq(TEST_KEY), eq(TEST_RESULT), any()); RedisCollectorStatePublisherService service = new RedisCollectorStatePublisherService( redisTemplate, @@ -63,8 +62,7 @@ public void test() throws IOException { ); service.publish(state); - verify(redisTemplate).expire(eq(TEST_KEY), any()); - verify(redisValueOperations).set(eq(TEST_KEY), eq(TEST_RESULT)); + verify(redisValueOperations).set(eq(TEST_KEY), eq(TEST_RESULT), any()); } } diff --git a/redis/src/main/java/com/navercorp/pinpoint/channel/redis/kv/RedisKVPubChannel.java b/redis/src/main/java/com/navercorp/pinpoint/channel/redis/kv/RedisKVPubChannel.java index 722a58797060..12d326fea328 100644 --- a/redis/src/main/java/com/navercorp/pinpoint/channel/redis/kv/RedisKVPubChannel.java +++ b/redis/src/main/java/com/navercorp/pinpoint/channel/redis/kv/RedisKVPubChannel.java @@ -39,8 +39,7 @@ class RedisKVPubChannel implements PubChannel { @Override public void publish(byte[] content) { - this.template.opsForValue().set(this.key, BytesUtils.toString(content)); - this.template.expire(this.key, expireMs, TimeUnit.MILLISECONDS); + this.template.opsForValue().set(this.key, BytesUtils.toString(content), expireMs, TimeUnit.MILLISECONDS); } }