From 945189f7b0e6abf380ff5daacf623c3e00b9df63 Mon Sep 17 00:00:00 2001 From: emeroad Date: Wed, 14 Aug 2024 17:38:58 +0900 Subject: [PATCH] [#noissue] Memory optimization for PingSession --- .../pinpoint/grpc/server/lifecycle/PingSession.java | 11 +++++++---- .../grpc/server/lifecycle/PingSessionTest.java | 13 +++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/grpc/src/main/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSession.java b/grpc/src/main/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSession.java index c53d7064020c..b8ee49d01a13 100644 --- a/grpc/src/main/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSession.java +++ b/grpc/src/main/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSession.java @@ -20,16 +20,20 @@ import com.navercorp.pinpoint.grpc.Header; import java.util.Objects; -import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; /** * @author Woonduk Kang(emeroad) * @author jaehong.kim */ public class PingSession { + private static final AtomicLongFieldUpdater UPDATER = AtomicLongFieldUpdater.newUpdater(PingSession.class, "eventIdAllocator"); + private final Long id; private final Header header; - private final AtomicLong eventIdAllocator; + + private volatile long eventIdAllocator = 0; + private short serviceType = ServiceType.UNDEFINED.getCode(); private boolean updated = false; private long lastPingTimeMillis; @@ -37,7 +41,6 @@ public class PingSession { public PingSession(Long id, Header header) { this.id = Objects.requireNonNull(id, "transportMetadata"); this.header = Objects.requireNonNull(header, "header"); - this.eventIdAllocator = new AtomicLong(); } public Header getHeader() { @@ -49,7 +52,7 @@ public Long getId() { } public long nextEventIdAllocator() { - return eventIdAllocator.incrementAndGet(); + return UPDATER.incrementAndGet(this); } public short getServiceType() { diff --git a/grpc/src/test/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSessionTest.java b/grpc/src/test/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSessionTest.java index 340701278dac..d70feefc3580 100644 --- a/grpc/src/test/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSessionTest.java +++ b/grpc/src/test/java/com/navercorp/pinpoint/grpc/server/lifecycle/PingSessionTest.java @@ -32,4 +32,17 @@ void getServiceType_undefined() { session.setServiceType(ServiceType.SPRING.getCode()); Assertions.assertEquals(ServiceType.SPRING.getCode(), session.getServiceType()); } + + @Test + void nextEventIdAllocator() { + Header header = new Header("name", "agentId", "agentName", "appName", + ServiceType.SPRING.getCode(), 11, 22, Collections.emptyList()); + + PingSession session = new PingSession(1L, header); + + Assertions.assertEquals(1, session.nextEventIdAllocator()); + Assertions.assertEquals(2, session.nextEventIdAllocator()); + + } + } \ No newline at end of file