From 1b22a4e29face3de3c2c665528090bbc5036f9e3 Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Wed, 17 Dec 2025 11:15:43 +0000 Subject: [PATCH 01/17] Add basic test assertions and GH workflow --- .github/workflows/ci.yml | 32 +++ pom.xml | 37 ++++ .../aeron/MarketDataFragmentHandler.java | 2 +- .../aeron/MarketDataFragmentHandlerTest.java | 186 ++++++++++++++++++ .../websocket/MarketDataBroadcasterTest.java | 160 +++++++++++++++ 5 files changed, 416 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml create mode 100644 src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java create mode 100644 src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..73eb9d1 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: CI/CD Pipeline + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] + +jobs: + build-and-test: + name: Build and Test + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' + cache: 'maven' + + - name: Build with Maven + run: ./mvnw clean compile + + - name: Generate SBE sources + run: ./mvnw generate-sources + + - name: Run tests + run: ./mvnw test diff --git a/pom.xml b/pom.xml index f9cd6cb..85220be 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,32 @@ sbe-all ${sbe.version} + + + + org.junit.jupiter + junit-jupiter + 5.13.4 + test + + + org.mockito + mockito-core + 5.20.0 + test + + + org.mockito + mockito-junit-jupiter + 5.20.0 + test + + + org.assertj + assertj-core + 3.27.6 + test + @@ -183,6 +209,17 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.3 + + + **/*Test.java + + + diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java index c3de352..cf5c3cc 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java @@ -44,7 +44,7 @@ public class MarketDataFragmentHandler implements FragmentHandler { private long sampleCounter = 0; @Inject - private MarketDataBroadcaster broadcaster; + MarketDataBroadcaster broadcaster; @Override public void onFragment(DirectBuffer buffer, int offset, int length, Header header) { diff --git a/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java b/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java new file mode 100644 index 0000000..934387a --- /dev/null +++ b/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java @@ -0,0 +1,186 @@ +package fish.payara.trader.aeron; + +import fish.payara.trader.websocket.MarketDataBroadcaster; +import io.aeron.logbuffer.Header; +import org.agrona.DirectBuffer; +import org.agrona.concurrent.UnsafeBuffer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import fish.payara.trader.sbe.*; + +import java.nio.ByteBuffer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class MarketDataFragmentHandlerTest { + + private MarketDataFragmentHandler handler; + + @Mock + private MarketDataBroadcaster broadcaster; + + @Mock + private Header header; + + @BeforeEach + void setUp() { + handler = new MarketDataFragmentHandler(); + handler.broadcaster = broadcaster; + } + + @Test + void testProcessTradeMessage() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + TradeEncoder tradeEncoder = new TradeEncoder(); + + // Send 50 messages to ensure sampling triggers + for (int i = 0; i < 50; i++) { + tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + tradeEncoder.timestamp(System.currentTimeMillis()); + tradeEncoder.symbol("AAPL"); + tradeEncoder.price(15000); + tradeEncoder.quantity(100); + + int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); + } + + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); + + String broadcastedMessage = messageCaptor.getValue(); + assertThat(broadcastedMessage).contains("\"type\":\"trade\""); + assertThat(broadcastedMessage).contains("\"symbol\":\"AAPL\""); + assertThat(broadcastedMessage).contains("\"price\":1.5000"); + assertThat(broadcastedMessage).contains("\"quantity\":100"); + } + + @Test + void testProcessQuoteMessage() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + QuoteEncoder quoteEncoder = new QuoteEncoder(); + + // Send 50 messages to ensure sampling triggers + for (int i = 0; i < 50; i++) { + quoteEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + quoteEncoder.timestamp(System.currentTimeMillis()); + quoteEncoder.symbol("MSFT"); + quoteEncoder.bidPrice(30000); + quoteEncoder.askPrice(30100); + quoteEncoder.bidSize(200); + quoteEncoder.askSize(150); + + int encodedLength = headerEncoder.encodedLength() + quoteEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); + } + + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); + + String broadcastedMessage = messageCaptor.getValue(); + assertThat(broadcastedMessage).contains("\"type\":\"quote\""); + assertThat(broadcastedMessage).contains("\"symbol\":\"MSFT\""); + assertThat(broadcastedMessage).contains("\"bid\":{\"price\":3.0000"); + assertThat(broadcastedMessage).contains("\"ask\":{\"price\":3.0100"); + } + + @Test + void testProcessHeartbeatMessage() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + HeartbeatEncoder heartbeatEncoder = new HeartbeatEncoder(); + + heartbeatEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + heartbeatEncoder.timestamp(System.currentTimeMillis()); + + int encodedLength = headerEncoder.encodedLength() + heartbeatEncoder.encodedLength(); + + handler.onFragment(buffer, 0, encodedLength, header); + + verify(broadcaster, never()).broadcast(any()); + } + + @Test + void testMessageSampling() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + TradeEncoder tradeEncoder = new TradeEncoder(); + + for (int i = 0; i < 100; i++) { + tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + tradeEncoder.timestamp(System.currentTimeMillis()); + tradeEncoder.symbol("TEST"); + tradeEncoder.price(10000 + i); + tradeEncoder.quantity(100); + + int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); + } + + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, times(2)).broadcast(messageCaptor.capture()); + } + + @Test + void testInvalidTemplateId() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + headerEncoder.wrap(buffer, 0); + headerEncoder.blockLength(0); + headerEncoder.templateId(9999); + headerEncoder.schemaId(1); + headerEncoder.version(0); + + int encodedLength = headerEncoder.encodedLength(); + + handler.onFragment(buffer, 0, encodedLength, header); + + verify(broadcaster, never()).broadcast(any()); + } + + @Test + void testPriceConversion() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + TradeEncoder tradeEncoder = new TradeEncoder(); + + // Send 50 messages to ensure sampling triggers + for (int i = 0; i < 50; i++) { + tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + tradeEncoder.timestamp(System.currentTimeMillis()); + tradeEncoder.symbol("XYZ"); + tradeEncoder.price(123456); + tradeEncoder.quantity(1); + + int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); + } + + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); + + String broadcastedMessage = messageCaptor.getValue(); + assertThat(broadcastedMessage).contains("\"price\":12.3456"); + } +} diff --git a/src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java b/src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java new file mode 100644 index 0000000..25fe6c7 --- /dev/null +++ b/src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java @@ -0,0 +1,160 @@ +package fish.payara.trader.websocket; + +import jakarta.websocket.RemoteEndpoint.Async; +import jakarta.websocket.Session; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.lenient; + +@ExtendWith(MockitoExtension.class) +class MarketDataBroadcasterTest { + + private MarketDataBroadcaster broadcaster; + + @Mock + private Session session1; + + @Mock + private Session session2; + + @Mock + private Async asyncRemote1; + + @Mock + private Async asyncRemote2; + + @BeforeEach + void setUp() { + broadcaster = new MarketDataBroadcaster(); + lenient().when(session1.getId()).thenReturn("session-1"); + lenient().when(session2.getId()).thenReturn("session-2"); + lenient().when(session1.getAsyncRemote()).thenReturn(asyncRemote1); + lenient().when(session2.getAsyncRemote()).thenReturn(asyncRemote2); + lenient().when(session1.isOpen()).thenReturn(true); + lenient().when(session2.isOpen()).thenReturn(true); + } + + @Test + void testAddSession() { + broadcaster.addSession(session1); + + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } + + @Test + void testAddMultipleSessions() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); + + assertThat(broadcaster.getSessionCount()).isEqualTo(2); + } + + @Test + void testRemoveSession() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); + + broadcaster.removeSession(session1); + + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } + + @Test + void testBroadcastToAllSessions() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); + + String message = "{\"type\":\"trade\",\"price\":100}"; + broadcaster.broadcast(message); + + verify(asyncRemote1).sendText(eq(message)); + verify(asyncRemote2).sendText(eq(message)); + } + + @Test + void testBroadcastRemovesClosedSessions() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); + + when(session1.isOpen()).thenReturn(false); + + String message = "{\"type\":\"trade\",\"price\":100}"; + broadcaster.broadcast(message); + + verify(asyncRemote1, never()).sendText(any()); + verify(asyncRemote2).sendText(eq(message)); + + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } + + @Test + void testBroadcastHandlesFailure() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); + + String message = "{\"type\":\"trade\",\"price\":100}"; + + doThrow(new RuntimeException("Send failed")) + .when(asyncRemote1).sendText(message); + + broadcaster.broadcast(message); + + verify(asyncRemote2).sendText(eq(message)); + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } + + @Test + void testGetSessionCountWhenEmpty() { + assertThat(broadcaster.getSessionCount()).isEqualTo(0); + } + + @Test + void testBroadcastWithArtificialLoad() { + broadcaster.addSession(session1); + + String baseMessage = "{\"type\":\"trade\",\"price\":100}"; + broadcaster.broadcastWithArtificialLoad(baseMessage); + + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(asyncRemote1).sendText(messageCaptor.capture()); + + String sentMessage = messageCaptor.getValue(); + assertThat(sentMessage).contains(baseMessage); + assertThat(sentMessage).contains("\"wrapped\""); + assertThat(sentMessage).contains("\"padding\""); + assertThat(sentMessage.length()).isGreaterThan(baseMessage.length()); + } + + @Test + void testConcurrentSessionManagement() throws InterruptedException { + int threadCount = 10; + Thread[] threads = new Thread[threadCount]; + + for (int i = 0; i < threadCount; i++) { + final int index = i; + threads[i] = new Thread(() -> { + Session mockSession = mock(Session.class); + when(mockSession.getId()).thenReturn("session-" + index); + when(mockSession.getAsyncRemote()).thenReturn(mock(Async.class)); + when(mockSession.isOpen()).thenReturn(true); + broadcaster.addSession(mockSession); + }); + threads[i].start(); + } + + for (Thread thread : threads) { + thread.join(); + } + + assertThat(broadcaster.getSessionCount()).isEqualTo(threadCount); + } +} From b16a0b4212ac6906d956129ec492a1ad99c07645 Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Wed, 17 Dec 2025 11:45:06 +0000 Subject: [PATCH 02/17] Add Maven wrapper files --- .mvn/wrapper/maven-wrapper.jar | Bin 0 -> 62547 bytes .mvn/wrapper/maven-wrapper.properties | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 .mvn/wrapper/maven-wrapper.jar create mode 100644 .mvn/wrapper/maven-wrapper.properties diff --git a/.mvn/wrapper/maven-wrapper.jar b/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 0000000000000000000000000000000000000000..cb28b0e37c7d206feb564310fdeec0927af4123a GIT binary patch literal 62547 zcmb5V1CS=sk~Z9!wr$(CZEL#U=Co~N+O}=mwr$(Cds^S@-Tij=#=rmlVk@E|Dyp8$ z$UKz?`Q$l@GN3=8fq)=^fVx`E)Pern1@-q?PE1vZPD);!LGdpP^)C$aAFx&{CzjH` zpQV9;fd0PyFPNN=yp*_@iYmRFcvOrKbU!1a*o)t$0ex(~3z5?bw11HQYW_uDngyer za60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B%`(V4e3%sj5f|i(eIhiSg-fHK1Pb0-mS^ zeh?WA7#{hhNci5e;?n*iVy|)iJiR>|8{TN3!=VBC2dN)~^ISSW_(g<^rHr$)nVrdA z39BMa5wl5q+5F@)4b%5-> zA^-P20l_e^S2PTa&HE2wf3jf)#)2ITVXzndeuMpPo8}kphQKhegB%QO+yBpDpgkcl z1nlPp14#+^bIA7__h16pMFECzKJ3p4`;Rf$gnr%{!5#oG42AH&X8hV8061%4W91ku z`OW_hyI+uBOqYXkVC&BqoKWmv;|{O|4d#Nay<)gkxBr^^N48(VDF7Sj#H1i3>9138 zkhxAU7;M)I18&d!Yw!V9zQA0tp(G4<8U5GX{YoYCQ?p56FxcD-2FwO5fqyx@__=$L zeK6Sg3>XQv)qz1?zW-k$_j`-)tf+yRU_%fXrenc>$^70d1Q-W?T#vy;6#Y-Q-<2)+ z5iTl6MA7j9m&oBhRXTKr*$3gec z3E;zX457RGZwUvD$l&8e42Qb^cbq>zYy@ive8`2N9vk=#6+AQlZZ7qk=?(ap1q0n0 z{B9Fte-{Gi-Tvax1)M+d1}Fyg@9X~sh1m|hsDcZuYOnxriBPN;z)q3<=-yBN2iM6V A?*IS* literal 0 HcmV?d00001 diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..346d645 --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar From d3e8d321db72011bb067e584c782a637014c28b4 Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Wed, 17 Dec 2025 12:08:20 +0000 Subject: [PATCH 03/17] Add catch for throwable --- .../java/fish/payara/trader/aeron/MarketDataPublisher.java | 5 +++-- src/main/webapp/index.html | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index 1cf1345..64db936 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -171,8 +171,9 @@ private void startPublishing() { LockSupport.parkNanos(PARK_NANOS); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Error in publisher loop", e); + } catch (Throwable e) { + LOGGER.log(Level.SEVERE, "Critical error in publisher loop", e); + // Brief pause on error, then continue LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); } } diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 0896ab6..c794eb0 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -777,13 +777,14 @@

System Log

}; ws.onclose=()=>{ document.getElementById('statusIndicator').classList.remove('connected'); - document.getElementById('statusText').textContent='Disconnected'; + document.getElementById('statusText').textContent='Disconnected (Reconnecting...)'; document.getElementById('connectBtn').textContent='Connect'; document.getElementById('modeDisplay').textContent = ''; document.getElementById('chartModeLabel').textContent = 'Disconnected'; - addLog('WebSocket disconnected'); + addLog('WebSocket disconnected. Attempting auto-reconnect in 3s...'); if (window.chartInterval) clearInterval(window.chartInterval); if (window.gcStatsInterval) clearInterval(window.gcStatsInterval); + setTimeout(connect, 3000); }; ws.onerror=error=>addLog('WebSocket error: '+error); ws.onmessage=event=>{ From e6c8b8e87f0a74ec1792c43d08f7be6e685b407b Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Wed, 17 Dec 2025 12:58:02 +0000 Subject: [PATCH 04/17] Add log circuit breaker, show total backedn message count on UI --- .../trader/aeron/MarketDataPublisher.java | 21 +++++++++++ src/main/webapp/index.html | 35 +++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index 64db936..eccc076 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -23,6 +23,7 @@ import java.util.Random; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.LockSupport; import java.util.logging.Level; @@ -64,6 +65,8 @@ public class MarketDataPublisher { private final AtomicLong tradeIdGenerator = new AtomicLong(1000); private final AtomicLong messagesPublished = new AtomicLong(0); + private final AtomicInteger consecutiveFailures = new AtomicInteger(0); + private static final int MAX_CONSECUTIVE_FAILURES = 50; private long sampleCounter = 0; private volatile boolean initialized = false; private volatile boolean running = false; @@ -202,6 +205,9 @@ private void startPublishing() { messagesPerSecond )); + String statsJson = String.format("{\"type\":\"stats\",\"total\":%d,\"rate\":%.0f}", currentCount, messagesPerSecond); + broadcaster.broadcast(statsJson); + lastCount = currentCount; lastTime = currentTime; } catch (InterruptedException e) { @@ -424,6 +430,7 @@ private void offer(UnsafeBuffer buffer, int offset, int length, String messageTy if (result > 0) { messagesPublished.incrementAndGet(); + consecutiveFailures.set(0); return; } else if (result == Publication.BACK_PRESSURED) { LOGGER.fine("Back pressured on " + messageType); @@ -436,14 +443,28 @@ private void offer(UnsafeBuffer buffer, int offset, int length, String messageTy } } else if (result == Publication.NOT_CONNECTED) { LOGGER.warning("Publication not connected"); + handlePublishFailure(messageType); return; } else { LOGGER.warning("Offer failed for " + messageType + ": " + result); + handlePublishFailure(messageType); return; } } LOGGER.warning("Failed to publish " + messageType + " after retries"); + handlePublishFailure(messageType); + } + + private void handlePublishFailure(String messageType) { + int failures = consecutiveFailures.incrementAndGet(); + if (failures >= MAX_CONSECUTIVE_FAILURES) { + LOGGER.severe(String.format( + "Circuit breaker triggered: %d consecutive publish failures (last attempted: %s). Stopping message generation.", + failures, messageType + )); + running = false; + } } @PreDestroy diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index c794eb0..836059b 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -115,6 +115,17 @@ display: flex; gap: 40px; } + + .stat-group { + display: flex; + gap: 20px; + padding-right: 20px; + border-right: 1px solid #e2e8f0; + } + + .stat-group:last-child { + border-right: none; + } .stat { text-align: center; } @@ -123,6 +134,10 @@ font-weight: 700; color: #1976d2; } + + .stat-value.backend { + color: #7c3aed; /* Purple for backend stats */ + } .stat-label { font-size: 0.85em; @@ -376,8 +391,14 @@

TradeStreamEE

-
0
Messages
-
0
Msg/sec
+
+
0
Backend Msg/sec
+
0
Total Generated
+
+
+
0
UI Msg/sec
+
0
UI Received
+
@@ -775,7 +796,7 @@

System Log

window.gcStatsInterval = setInterval(pollGCStats, 3000); pollGCStats(); // Initial poll }; - ws.onclose=()=>{ + ws.onclose=( )=>{ document.getElementById('statusIndicator').classList.remove('connected'); document.getElementById('statusText').textContent='Disconnected (Reconnecting...)'; document.getElementById('connectBtn').textContent='Connect'; @@ -836,6 +857,14 @@

System Log

document.getElementById('chartModeLabel').textContent = modeText; } break; + case 'stats': + if (data.total) { + document.getElementById('backendTotal').textContent = data.total.toLocaleString(); + } + if (data.rate) { + document.getElementById('backendRate').textContent = parseInt(data.rate).toLocaleString(); + } + break; } } From 6c397e6158b8b1030b5f6b4be0dbf192fe318f0b Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Wed, 17 Dec 2025 15:13:19 +0000 Subject: [PATCH 05/17] refactor impl, increase heap to 8GB --- Dockerfile | 4 +- Dockerfile.standard | 4 +- .../aeron/MarketDataFragmentHandler.java | 185 +++++++++--------- .../trader/aeron/MarketDataPublisher.java | 71 ++++--- 4 files changed, 138 insertions(+), 126 deletions(-) diff --git a/Dockerfile b/Dockerfile index d41c266..08ccf6d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,8 +41,8 @@ EXPOSE 8080 # Default JVM Options for Azul Platform Prime # Note: These are defaults - can be overridden via docker-compose or docker run -e # Azul Platform Prime uses C4 GC by default - no need to specify -XX:+UseZGC -ENV JAVA_OPTS="-Xms2g \ - -Xmx4g \ +ENV JAVA_OPTS="-Xms8g \ + -Xmx8g \ -Xlog:gc*:file=/opt/payara/gc.log:time,uptime:filecount=5,filesize=10M \ -XX:+UnlockDiagnosticVMOptions \ -XX:+UnlockExperimentalVMOptions \ diff --git a/Dockerfile.standard b/Dockerfile.standard index 0496ab8..5039cd2 100644 --- a/Dockerfile.standard +++ b/Dockerfile.standard @@ -39,8 +39,8 @@ COPY --from=build /app/target/*.war ROOT.war EXPOSE 8080 # Standard JVM Options (G1GC) -ENV JAVA_OPTS="-Xms2g \ - -Xmx4g \ +ENV JAVA_OPTS="-Xms8g \ + -Xmx8g \ -XX:+UseG1GC \ -Djava.net.preferIPv4Stack=true" diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java index cf5c3cc..d3c4e09 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java @@ -46,6 +46,10 @@ public class MarketDataFragmentHandler implements FragmentHandler { @Inject MarketDataBroadcaster broadcaster; + // Optimization: Reusable buffers to reduce allocation + private final byte[] symbolBuffer = new byte[128]; + private final StringBuilder sb = new StringBuilder(1024); + @Override public void onFragment(DirectBuffer buffer, int offset, int length, Header header) { try { @@ -100,6 +104,10 @@ public void onFragment(DirectBuffer buffer, int offset, int length, Header heade * Process Trade message using SBE decoder (zero-copy) */ private void processTrade(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; + } + tradeDecoder.wrap(buffer, offset, blockLength, version); // Extract fields from SBE decoder @@ -109,30 +117,30 @@ private void processTrade(DirectBuffer buffer, int offset, int blockLength, int final long quantity = tradeDecoder.quantity(); final Side side = tradeDecoder.side(); - // Extract variable-length symbol string + // Extract variable-length symbol string using reusable buffer final int symbolLength = tradeDecoder.symbolLength(); - final byte[] symbolBytes = new byte[symbolLength]; - tradeDecoder.getSymbol(symbolBytes, 0, symbolLength); - final String symbol = new String(symbolBytes); - - // Only broadcast sampled messages to avoid overwhelming browser - if (shouldBroadcast) { - // Convert to JSON (intentionally creating garbage for GC testing) - String json = String.format( - "{\"type\":\"trade\",\"timestamp\":%d,\"tradeId\":%d,\"symbol\":\"%s\"," + - "\"price\":%.4f,\"quantity\":%d,\"side\":\"%s\"}", - timestamp, tradeId, symbol, - price / 10000.0, quantity, side - ); - - broadcaster.broadcast(json); - } + tradeDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); + + sb.setLength(0); + sb.append("{\"type\":\"trade\",\"timestamp\":").append(timestamp) + .append(",\"tradeId\":").append(tradeId) + .append(",\"symbol\":\"").append(symbol).append("\"") + .append(",\"price\":").append(price / 10000.0) + .append(",\"quantity\":").append(quantity) + .append(",\"side\":\"").append(side).append("\"}"); + + broadcaster.broadcast(sb.toString()); } /** * Process Quote message using SBE decoder (zero-copy) */ private void processQuote(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; + } + quoteDecoder.wrap(buffer, offset, blockLength, version); final long timestamp = quoteDecoder.timestamp(); @@ -142,84 +150,76 @@ private void processQuote(DirectBuffer buffer, int offset, int blockLength, int final long askSize = quoteDecoder.askSize(); final int symbolLength = quoteDecoder.symbolLength(); - final byte[] symbolBytes = new byte[symbolLength]; - quoteDecoder.getSymbol(symbolBytes, 0, symbolLength); - final String symbol = new String(symbolBytes); - - // Only broadcast sampled messages - if (shouldBroadcast) { - String json = String.format( - "{\"type\":\"quote\",\"timestamp\":%d,\"symbol\":\"%s\"," + - "\"bid\":{\"price\":%.4f,\"size\":%d},\"ask\":{\"price\":%.4f,\"size\":%d}}", - timestamp, symbol, - bidPrice / 10000.0, bidSize, - askPrice / 10000.0, askSize - ); - - broadcaster.broadcast(json); - } + quoteDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); + + sb.setLength(0); + sb.append("{\"type\":\"quote\",\"timestamp\":").append(timestamp) + .append(",\"symbol\":\"").append(symbol).append("\"") + .append(",\"bid\":{\"price\":").append(bidPrice / 10000.0).append(",\"size\":").append(bidSize).append("}") + .append(",\"ask\":{\"price\":").append(askPrice / 10000.0).append(",\"size\":").append(askSize).append("}}"); + + broadcaster.broadcast(sb.toString()); } /** * Process MarketDepth message with repeating groups (zero-copy) */ private void processMarketDepth(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; + } + marketDepthDecoder.wrap(buffer, offset, blockLength, version); final long timestamp = marketDepthDecoder.timestamp(); final long sequenceNumber = marketDepthDecoder.sequenceNumber(); - // Only broadcast sampled messages (but always decode for GC stress) - if (shouldBroadcast) { - // Build JSON for bids - StringBuilder bidsJson = new StringBuilder("["); - MarketDepthDecoder.BidsDecoder bids = marketDepthDecoder.bids(); - int bidCount = 0; - while (bids.hasNext()) { - bids.next(); - if (bidCount > 0) bidsJson.append(","); - bidsJson.append(String.format( - "{\"price\":%.4f,\"quantity\":%d}", - bids.price() / 10000.0, bids.quantity() - )); - bidCount++; - } - bidsJson.append("]"); - - // Build JSON for asks - StringBuilder asksJson = new StringBuilder("["); - MarketDepthDecoder.AsksDecoder asks = marketDepthDecoder.asks(); - int askCount = 0; - while (asks.hasNext()) { - asks.next(); - if (askCount > 0) asksJson.append(","); - asksJson.append(String.format( - "{\"price\":%.4f,\"quantity\":%d}", - asks.price() / 10000.0, asks.quantity() - )); - askCount++; - } - asksJson.append("]"); - - final int symbolLength = marketDepthDecoder.symbolLength(); - final byte[] symbolBytes = new byte[symbolLength]; - marketDepthDecoder.getSymbol(symbolBytes, 0, symbolLength); - final String symbol = new String(symbolBytes); + sb.setLength(0); + sb.append("{\"type\":\"depth\",\"timestamp\":").append(timestamp) + .append(",\"sequence\":").append(sequenceNumber) + .append(",\"bids\":["); + + MarketDepthDecoder.BidsDecoder bids = marketDepthDecoder.bids(); + int bidCount = 0; + while (bids.hasNext()) { + bids.next(); + if (bidCount > 0) sb.append(","); + sb.append("{\"price\":").append(bids.price() / 10000.0) + .append(",\"quantity\":").append(bids.quantity()).append("}"); + bidCount++; + } + sb.append("],\"asks\":["); + + MarketDepthDecoder.AsksDecoder asks = marketDepthDecoder.asks(); + int askCount = 0; + while (asks.hasNext()) { + asks.next(); + if (askCount > 0) sb.append(","); + sb.append("{\"price\":").append(asks.price() / 10000.0) + .append(",\"quantity\":").append(asks.quantity()).append("}"); + askCount++; + } + sb.append("]"); - String json = String.format( - "{\"type\":\"depth\",\"timestamp\":%d,\"symbol\":\"%s\",\"sequence\":%d," + - "\"bids\":%s,\"asks\":%s}", - timestamp, symbol, sequenceNumber, bidsJson, asksJson - ); + // Must extract symbol after traversing groups in SBE for correct position in buffer + final int symbolLength = marketDepthDecoder.symbolLength(); + marketDepthDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); + + sb.append(",\"symbol\":\"").append(symbol).append("\"}"); - broadcaster.broadcast(json); - } + broadcaster.broadcast(sb.toString()); } /** * Process OrderAck message */ private void processOrderAck(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; + } + orderAckDecoder.wrap(buffer, offset, blockLength, version); final long timestamp = orderAckDecoder.timestamp(); @@ -234,22 +234,23 @@ private void processOrderAck(DirectBuffer buffer, int offset, int blockLength, i final long cumQty = orderAckDecoder.cumQty(); final int symbolLength = orderAckDecoder.symbolLength(); - final byte[] symbolBytes = new byte[symbolLength]; - orderAckDecoder.getSymbol(symbolBytes, 0, symbolLength); - final String symbol = new String(symbolBytes); - - // Only broadcast sampled messages - if (shouldBroadcast) { - String json = String.format( - "{\"type\":\"orderAck\",\"timestamp\":%d,\"orderId\":%d,\"clientOrderId\":%d," + - "\"symbol\":\"%s\",\"side\":\"%s\",\"orderType\":\"%s\",\"price\":%.4f," + - "\"quantity\":%d,\"execType\":\"%s\",\"leavesQty\":%d,\"cumQty\":%d}", - timestamp, orderId, clientOrderId, symbol, side, orderType, - price / 10000.0, quantity, execType, leavesQty, cumQty - ); - - broadcaster.broadcast(json); - } + orderAckDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); + + sb.setLength(0); + sb.append("{\"type\":\"orderAck\",\"timestamp\":").append(timestamp) + .append(",\"orderId\":").append(orderId) + .append(",\"clientOrderId\":").append(clientOrderId) + .append(",\"symbol\":\"").append(symbol).append("\"") + .append(",\"side\":\"").append(side).append("\"") + .append(",\"orderType\":\"").append(orderType).append("\"") + .append(",\"price\":").append(price / 10000.0) + .append(",\"quantity\":").append(quantity) + .append(",\"execType\":\"").append(execType).append("\"") + .append(",\"leavesQty\":").append(leavesQty) + .append(",\"cumQty\":").append(cumQty).append("}"); + + broadcaster.broadcast(sb.toString()); } /** diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index eccc076..6e42d93 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -1,5 +1,7 @@ package fish.payara.trader.aeron; +import java.util.concurrent.ThreadLocalRandom; + import fish.payara.trader.concurrency.VirtualThreadExecutor; import fish.payara.trader.sbe.*; import fish.payara.trader.websocket.MarketDataBroadcaster; @@ -20,8 +22,9 @@ import org.eclipse.microprofile.config.inject.ConfigProperty; import java.nio.ByteBuffer; -import java.util.Random; + import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; @@ -60,7 +63,7 @@ public class MarketDataPublisher { // Buffer for encoding messages private final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(BUFFER_SIZE)); - private final Random random = new Random(); + private final AtomicLong sequenceNumber = new AtomicLong(0); private final AtomicLong tradeIdGenerator = new AtomicLong(1000); @@ -70,6 +73,7 @@ public class MarketDataPublisher { private long sampleCounter = 0; private volatile boolean initialized = false; private volatile boolean running = false; + private boolean isDirectMode; private Future publisherFuture; private Future statsFuture; @@ -98,6 +102,7 @@ public void init() { if ("DIRECT".equalsIgnoreCase(ingestionMode)) { LOGGER.info("Running in DIRECT mode - Bypassing Aeron/SBE setup."); initialized = true; + isDirectMode = true; startPublishing(); return; } @@ -222,11 +227,12 @@ private void startPublishing() { * Publish a Trade message */ private void publishTrade() { - if ("DIRECT".equalsIgnoreCase(ingestionMode)) { - final String symbol = SYMBOLS[random.nextInt(SYMBOLS.length)]; - final double price = 100.0 + random.nextDouble() * 400.0; - final int quantity = random.nextInt(1000) + 100; - final String side = random.nextBoolean() ? "BUY" : "SELL"; + if (isDirectMode) { + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); // Use ThreadLocalRandom + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double price = 100.0 + currentRandom.nextDouble() * 400.0; + final int quantity = currentRandom.nextInt(1000) + 100; + final String side = currentRandom.nextBoolean() ? "BUY" : "SELL"; String json = String.format( "{\"type\":\"trade\",\"timestamp\":%d,\"tradeId\":%d,\"symbol\":\"%s\",\"price\":%.4f,\"quantity\":%d,\"side\":\"%s\"}", @@ -245,7 +251,8 @@ private void publishTrade() { } int bufferOffset = 0; - final String symbol = SYMBOLS[random.nextInt(SYMBOLS.length)]; + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; headerEncoder.wrap(buffer, bufferOffset) .blockLength(tradeEncoder.sbeBlockLength()) @@ -258,9 +265,9 @@ private void publishTrade() { tradeEncoder.wrap(buffer, bufferOffset) .timestamp(System.currentTimeMillis()) .tradeId(tradeIdGenerator.incrementAndGet()) - .price((long) ((100.0 + random.nextDouble() * 400.0) * 10000)) // $100-$500 - .quantity(random.nextInt(1000) + 100) - .side(random.nextBoolean() ? Side.BUY : Side.SELL) + .price((long) ((100.0 + currentRandom.nextDouble() * 400.0) * 10000)) // $100-$500 + .quantity(currentRandom.nextInt(1000) + 100) + .side(currentRandom.nextBoolean() ? Side.BUY : Side.SELL) .symbol(symbol); final int length = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); @@ -271,13 +278,14 @@ private void publishTrade() { * Publish a Quote message */ private void publishQuote() { - if ("DIRECT".equalsIgnoreCase(ingestionMode)) { - final String symbol = SYMBOLS[random.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + random.nextDouble() * 400.0; + if (isDirectMode) { + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; final double bidPrice = basePrice - 0.01; final double askPrice = basePrice + 0.01; - final int bidSize = random.nextInt(10000) + 100; - final int askSize = random.nextInt(10000) + 100; + final int bidSize = currentRandom.nextInt(10000) + 100; + final int askSize = currentRandom.nextInt(10000) + 100; String json = String.format( "{\"type\":\"quote\",\"timestamp\":%d,\"symbol\":\"%s\",\"bid\":{\"price\":%.4f,\"size\":%d},\"ask\":{\"price\":%.4f,\"size\":%d}}", @@ -293,8 +301,9 @@ private void publishQuote() { } int bufferOffset = 0; - final String symbol = SYMBOLS[random.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + random.nextDouble() * 400.0; + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; headerEncoder.wrap(buffer, bufferOffset) .blockLength(quoteEncoder.sbeBlockLength()) @@ -307,9 +316,9 @@ private void publishQuote() { quoteEncoder.wrap(buffer, bufferOffset) .timestamp(System.currentTimeMillis()) .bidPrice((long) ((basePrice - 0.01) * 10000)) - .bidSize(random.nextInt(10000) + 100) + .bidSize(currentRandom.nextInt(10000) + 100) .askPrice((long) ((basePrice + 0.01) * 10000)) - .askSize(random.nextInt(10000) + 100) + .askSize(currentRandom.nextInt(10000) + 100) .symbol(symbol); final int length = headerEncoder.encodedLength() + quoteEncoder.encodedLength(); @@ -320,9 +329,10 @@ private void publishQuote() { * Publish a MarketDepth message */ private void publishMarketDepth() { - if ("DIRECT".equalsIgnoreCase(ingestionMode)) { - final String symbol = SYMBOLS[random.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + random.nextDouble() * 400.0; + if (isDirectMode) { + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; final long timestamp = System.currentTimeMillis(); final long seq = sequenceNumber.incrementAndGet(); @@ -330,7 +340,7 @@ private void publishMarketDepth() { for (int i = 0; i < 5; i++) { if (i > 0) bidsJson.append(","); bidsJson.append(String.format("{\"price\":%.4f,\"quantity\":%d}", - basePrice - (i + 1) * 0.01, random.nextInt(5000) + 100)); + basePrice - (i + 1) * 0.01, currentRandom.nextInt(5000) + 100)); } bidsJson.append("]"); @@ -338,7 +348,7 @@ private void publishMarketDepth() { for (int i = 0; i < 5; i++) { if (i > 0) asksJson.append(","); asksJson.append(String.format("{\"price\":%.4f,\"quantity\":%d}", - basePrice + (i + 1) * 0.01, random.nextInt(5000) + 100)); + basePrice + (i + 1) * 0.01, currentRandom.nextInt(5000) + 100)); } asksJson.append("]"); @@ -354,8 +364,9 @@ private void publishMarketDepth() { } int bufferOffset = 0; - final String symbol = SYMBOLS[random.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + random.nextDouble() * 400.0; + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; headerEncoder.wrap(buffer, bufferOffset) .blockLength(marketDepthEncoder.sbeBlockLength()) @@ -373,14 +384,14 @@ private void publishMarketDepth() { for (int i = 0; i < 5; i++) { bidsEncoder.next() .price((long) ((basePrice - (i + 1) * 0.01) * 10000)) - .quantity(random.nextInt(5000) + 100); + .quantity(currentRandom.nextInt(5000) + 100); } MarketDepthEncoder.AsksEncoder asksEncoder = marketDepthEncoder.asksCount(5); for (int i = 0; i < 5; i++) { asksEncoder.next() .price((long) ((basePrice + (i + 1) * 0.01) * 10000)) - .quantity(random.nextInt(5000) + 100); + .quantity(currentRandom.nextInt(5000) + 100); } marketDepthEncoder.symbol(symbol); @@ -393,7 +404,7 @@ private void publishMarketDepth() { * Publish a Heartbeat message */ private void publishHeartbeat() { - if ("DIRECT".equalsIgnoreCase(ingestionMode)) { + if (isDirectMode) { // In DIRECT mode, we just increment counter but don't broadcast heartbeats to UI // as they are mainly for system health checks in the Aeron log messagesPublished.incrementAndGet(); From faea38d35746909b2398b4036ec46728077a5541 Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Wed, 17 Dec 2025 15:15:56 +0000 Subject: [PATCH 06/17] Fix tests --- .../payara/trader/aeron/MarketDataFragmentHandlerTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java b/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java index 934387a..1d33ce9 100644 --- a/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java +++ b/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java @@ -61,7 +61,7 @@ void testProcessTradeMessage() { String broadcastedMessage = messageCaptor.getValue(); assertThat(broadcastedMessage).contains("\"type\":\"trade\""); assertThat(broadcastedMessage).contains("\"symbol\":\"AAPL\""); - assertThat(broadcastedMessage).contains("\"price\":1.5000"); + assertThat(broadcastedMessage).contains("\"price\":1.5"); assertThat(broadcastedMessage).contains("\"quantity\":100"); } @@ -93,8 +93,8 @@ void testProcessQuoteMessage() { String broadcastedMessage = messageCaptor.getValue(); assertThat(broadcastedMessage).contains("\"type\":\"quote\""); assertThat(broadcastedMessage).contains("\"symbol\":\"MSFT\""); - assertThat(broadcastedMessage).contains("\"bid\":{\"price\":3.0000"); - assertThat(broadcastedMessage).contains("\"ask\":{\"price\":3.0100"); + assertThat(broadcastedMessage).contains("\"bid\":{\"price\":3.0"); + assertThat(broadcastedMessage).contains("\"ask\":{\"price\":3.01"); } @Test From e21ea53767afdeef527575119eda3590f13ac96b Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Wed, 17 Dec 2025 16:05:59 +0000 Subject: [PATCH 07/17] use docker ADD directive in place of wget. Remove remnants of JDD Poland... copy driven development --- Dockerfile | 9 +- Dockerfile.standard | 9 +- keycloak/imports/jdd-poland-realm.json | 312 ------------------------- postgres/init.sql | 62 ----- 4 files changed, 4 insertions(+), 388 deletions(-) delete mode 100644 keycloak/imports/jdd-poland-realm.json delete mode 100644 postgres/init.sql diff --git a/Dockerfile b/Dockerfile index 08ccf6d..9d02baa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,14 +24,9 @@ LABEL description="High-frequency trading dashboard with Aeron + SBE + Payara Mi WORKDIR /opt/payara -# Download Payara Micro +# Add Payara Micro from URL ARG PAYARA_VERSION=7.2025.2 -RUN apt-get update && \ - apt-get install -y wget curl && \ - wget -q -O payara-micro.jar \ - "https://nexus.payara.fish/repository/payara-community/fish/payara/extras/payara-micro/${PAYARA_VERSION}/payara-micro-${PAYARA_VERSION}.jar" && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* +ADD https://nexus.payara.fish/repository/payara-community/fish/payara/extras/payara-micro/${PAYARA_VERSION}/payara-micro-${PAYARA_VERSION}.jar /opt/payara/payara-micro.jar # Copy WAR file from build stage COPY --from=build /app/target/*.war ROOT.war diff --git a/Dockerfile.standard b/Dockerfile.standard index 5039cd2..55e50ea 100644 --- a/Dockerfile.standard +++ b/Dockerfile.standard @@ -24,14 +24,9 @@ LABEL description="High-frequency trading dashboard (Standard JVM Comparison)" WORKDIR /opt/payara -# Download Payara Micro +# Add Payara Micro from URL ARG PAYARA_VERSION=7.2025.2 -RUN apt-get update && \ - apt-get install -y wget curl && \ - wget -q -O payara-micro.jar \ - "https://nexus.payara.fish/repository/payara-community/fish/payara/extras/payara-micro/${PAYARA_VERSION}/payara-micro-${PAYARA_VERSION}.jar" && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* +ADD https://nexus.payara.fish/repository/payara-community/fish/payara/extras/payara-micro/${PAYARA_VERSION}/payara-micro-${PAYARA_VERSION}.jar /opt/payara/payara-micro.jar # Copy WAR file from build stage COPY --from=build /app/target/*.war ROOT.war diff --git a/keycloak/imports/jdd-poland-realm.json b/keycloak/imports/jdd-poland-realm.json deleted file mode 100644 index 1bb21f8..0000000 --- a/keycloak/imports/jdd-poland-realm.json +++ /dev/null @@ -1,312 +0,0 @@ -{ - "realm": "jdd-poland", - "enabled": true, - "sslRequired": "none", - "registrationAllowed": false, - "loginWithEmailAllowed": true, - "duplicateEmailsAllowed": false, - "resetPasswordAllowed": true, - "editUsernameAllowed": false, - "bruteForceProtected": true, - "permanentLockout": false, - "maxFailureWaitSeconds": 900, - "minimumQuickLoginWaitSeconds": 60, - "waitIncrementSeconds": 60, - "quickLoginCheckMilliSeconds": 1000, - "maxDeltaTimeSeconds": 43200, - "failureFactor": 5, - "accessTokenLifespan": 300, - "accessTokenLifespanForImplicitFlow": 900, - "ssoSessionIdleTimeout": 1800, - "ssoSessionMaxLifespan": 36000, - "offlineSessionIdleTimeout": 2592000, - "accessCodeLifespan": 60, - "accessCodeLifespanUserAction": 300, - "accessCodeLifespanLogin": 1800, - "roles": { - "realm": [ - { - "name": "DOCTOR", - "description": "Doctor role with full patient access" - }, - { - "name": "NURSE", - "description": "Nurse role with limited patient access" - }, - { - "name": "ADMIN", - "description": "System administrator" - }, - { - "name": "PATIENT", - "description": "Patient role with self-access only" - } - ] - }, - "clients": [ - { - "clientId": "jdd-healthcare-app", - "enabled": true, - "protocol": "openid-connect", - "publicClient": false, - "bearerOnly": false, - "standardFlowEnabled": true, - "implicitFlowEnabled": false, - "directAccessGrantsEnabled": true, - "serviceAccountsEnabled": true, - "authorizationServicesEnabled": true, - "clientAuthenticatorType": "client-secret", - "secret": "jdd-healthcare-secret-2024", - "redirectUris": [ - "http://localhost:8080/*", - "http://localhost:8080/callback" - ], - "webOrigins": [ - "http://localhost:8080" - ], - "attributes": { - "access.token.lifespan": "300", - "pkce.code.challenge.method": "S256" - }, - "protocolMappers": [ - { - "name": "roles", - "protocol": "openid-connect", - "protocolMapper": "oidc-usermodel-realm-role-mapper", - "consentRequired": false, - "config": { - "multivalued": "true", - "userinfo.token.claim": "true", - "id.token.claim": "true", - "access.token.claim": "true", - "claim.name": "groups", - "jsonType.label": "String" - } - }, - { - "name": "email", - "protocol": "openid-connect", - "protocolMapper": "oidc-usermodel-property-mapper", - "consentRequired": false, - "config": { - "userinfo.token.claim": "true", - "user.attribute": "email", - "id.token.claim": "true", - "access.token.claim": "true", - "claim.name": "email", - "jsonType.label": "String" - } - }, - { - "name": "department", - "protocol": "openid-connect", - "protocolMapper": "oidc-usermodel-attribute-mapper", - "consentRequired": false, - "config": { - "userinfo.token.claim": "true", - "user.attribute": "department", - "id.token.claim": "true", - "access.token.claim": "true", - "claim.name": "department", - "jsonType.label": "String" - } - }, - { - "name": "audience-mapper", - "protocol": "openid-connect", - "protocolMapper": "oidc-audience-mapper", - "consentRequired": false, - "config": { - "included.client.audience": "jdd-healthcare-app", - "id.token.claim": "false", - "access.token.claim": "true", - "userinfo.token.claim": "false" - } - }, - { - "name": "subject-mapper", - "protocol": "openid-connect", - "protocolMapper": "oidc-usermodel-property-mapper", - "consentRequired": false, - "config": { - "userinfo.token.claim": "true", - "user.attribute": "username", - "id.token.claim": "true", - "access.token.claim": "true", - "claim.name": "sub", - "jsonType.label": "String" - } - }, - { - "name": "upn-mapper", - "protocol": "openid-connect", - "protocolMapper": "oidc-usermodel-property-mapper", - "consentRequired": false, - "config": { - "userinfo.token.claim": "true", - "user.attribute": "username", - "id.token.claim": "true", - "access.token.claim": "true", - "claim.name": "upn", - "jsonType.label": "String" - } - } - ], - "defaultClientScopes": [ - "web-origins", - "acr", - "profile", - "roles", - "email" - ], - "optionalClientScopes": [ - "address", - "phone", - "offline_access", - "microprofile-jwt" - ] - }, - { - "clientId": "service-client", - "enabled": true, - "protocol": "openid-connect", - "publicClient": false, - "bearerOnly": false, - "standardFlowEnabled": false, - "implicitFlowEnabled": false, - "directAccessGrantsEnabled": false, - "serviceAccountsEnabled": true, - "clientAuthenticatorType": "client-secret", - "secret": "service-client-secret-2024", - "attributes": { - "access.token.lifespan": "600" - } - } - ], - "users": [ - { - "username": "dr.smith", - "enabled": true, - "email": "dr.smith@hospital.com", - "firstName": "John", - "lastName": "Smith", - "credentials": [ - { - "type": "password", - "value": "doctor123" - } - ], - "realmRoles": [ - "DOCTOR" - ], - "attributes": { - "department": [ - "Cardiology" - ], - "license": [ - "MD-12345" - ] - } - }, - { - "username": "nurse.jones", - "enabled": true, - "email": "nurse.jones@hospital.com", - "firstName": "Sarah", - "lastName": "Jones", - "credentials": [ - { - "type": "password", - "value": "nurse123" - } - ], - "realmRoles": [ - "NURSE" - ], - "attributes": { - "department": [ - "Emergency" - ], - "license": [ - "RN-67890" - ] - } - }, - { - "username": "admin", - "enabled": true, - "email": "admin@hospital.com", - "firstName": "Admin", - "lastName": "User", - "credentials": [ - { - "type": "password", - "value": "admin123" - } - ], - "realmRoles": [ - "ADMIN" - ] - }, - { - "username": "patient.doe", - "enabled": true, - "email": "patient.doe@email.com", - "firstName": "Jane", - "lastName": "Doe", - "credentials": [ - { - "type": "password", - "value": "patient123" - } - ], - "realmRoles": [ - "PATIENT" - ], - "attributes": { - "patientId": [ - "P-001" - ], - "dateOfBirth": [ - "1985-05-15" - ] - } - } - ], - "requiredActions": [ - { - "alias": "CONFIGURE_TOTP", - "name": "Configure OTP", - "providerId": "CONFIGURE_TOTP", - "enabled": true, - "defaultAction": false, - "priority": 10 - }, - { - "alias": "UPDATE_PASSWORD", - "name": "Update Password", - "providerId": "UPDATE_PASSWORD", - "enabled": true, - "defaultAction": true, - "priority": 30 - } - ], - "authenticationFlows": [ - { - "alias": "browser", - "description": "browser based authentication", - "providerId": "basic-flow", - "topLevel": true, - "builtIn": true, - "authenticationExecutions": [] - } - ], - "browserSecurityHeaders": { - "contentSecurityPolicy": "frame-src 'self'; frame-ancestors 'self'; object-src 'none';", - "xContentTypeOptions": "nosniff", - "xRobotsTag": "none", - "xFrameOptions": "SAMEORIGIN", - "xXSSProtection": "1; mode=block", - "strictTransportSecurity": "max-age=31536000; includeSubDomains" - } -} diff --git a/postgres/init.sql b/postgres/init.sql deleted file mode 100644 index d770781..0000000 --- a/postgres/init.sql +++ /dev/null @@ -1,62 +0,0 @@ -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; - --- Audit log table for security events -CREATE TABLE audit_log -( - id VARCHAR(36) PRIMARY KEY, - timestamp TIMESTAMP NOT NULL, - username VARCHAR(100), - ip_address VARCHAR(45), - action VARCHAR(100) NOT NULL, - resource VARCHAR(255), - method_name VARCHAR(255), - class_name VARCHAR(255), - sensitivity_level VARCHAR(20), - success BOOLEAN NOT NULL, - duration_ms BIGINT, - error_message VARCHAR(1000), - user_agent VARCHAR(500), - details TEXT -); - --- Indexes for audit log queries -CREATE INDEX idx_audit_username ON audit_log (username); -CREATE INDEX idx_audit_timestamp ON audit_log (timestamp); -CREATE INDEX idx_audit_action ON audit_log (action); -CREATE INDEX idx_audit_level ON audit_log (sensitivity_level); - --- Patient table -CREATE TABLE patient -( - id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), - first_name VARCHAR(50) NOT NULL, - last_name VARCHAR(50) NOT NULL, - date_of_birth DATE NOT NULL, - ssn VARCHAR(255), - email VARCHAR(255), - phone VARCHAR(255), - address VARCHAR(255), - blood_type VARCHAR(10), - allergies VARCHAR(500), - medical_conditions VARCHAR(1000), - assigned_doctor VARCHAR(100), - department VARCHAR(50), - created_at TIMESTAMP, - updated_at TIMESTAMP -); - -INSERT INTO patient (id, first_name, last_name, date_of_birth, ssn, email, phone, address, blood_type, allergies, - medical_conditions, assigned_doctor, department, created_at, updated_at) -VALUES ('a8b8b4f0-3e5b-48b0-8b4a-0e1b6d4b0b1b', 'Jane', 'Doe', '1985-05-15', '123-45-6789', 'jane.doe@email.com', - '555-0101', '123 Main St, Springfield', 'O+', 'Penicillin', 'Hypertension', 'Dr. Smith', 'Cardiology', NOW(), - NOW()), - ('f8b8b4f0-3e5b-48b0-8b4a-0e1b6d4b0b1c', 'John', 'Smith', '1978-03-22', '987-65-4321', 'john.smith@email.com', - '555-0102', '456 Oak Ave, Springfield', 'A+', 'None', 'Diabetes Type 2', 'Dr. Smith', 'Cardiology', NOW(), - NOW()), - ('e8b8b4f0-3e5b-48b0-8b4a-0e1b6d4b0b1d', 'Alice', 'Johnson', '1992-11-08', '456-78-9012', 'alice.j@email.com', - '555-0103', '789 Pine Rd, Springfield', 'B-', 'Latex', 'Asthma', 'Dr. Williams', 'Emergency', NOW(), NOW()), - ('d8b8b4f0-3e5b-48b0-8b4a-0e1b6d4b0b1e', 'Robert', 'Brown', '1965-07-30', '321-54-9876', 'rbrown@email.com', - '555-0104', '321 Elm St, Springfield', 'AB+', 'Shellfish', 'Coronary artery disease', 'Dr. Smith', 'Cardiology', - NOW(), NOW()), - ('c8b8b4f0-3e5b-48b0-8b4a-0e1b6d4b0b1f', 'Emma', 'Davis', '1988-09-14', '654-32-1098', 'emma.d@email.com', - '555-0105', '555 Maple Ln, Springfield', 'O-', 'None', 'None', 'Dr. Williams', 'Emergency', NOW(), NOW()); From 4ca1c2b5a8b29bf123c65664d62f4b0cbe30df05 Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Thu, 18 Dec 2025 10:46:20 +0000 Subject: [PATCH 08/17] Add GC stress endpoints --- Dockerfile | 3 +- Dockerfile.standard | 3 + .../java/fish/payara/trader/gc/GCStats.java | 77 +++++++ .../fish/payara/trader/gc/GCStatsService.java | 128 +++++++++++ .../trader/pressure/AllocationMode.java | 35 +++ .../pressure/MemoryPressureService.java | 212 ++++++++++++++++++ .../payara/trader/rest/GCStatsResource.java | 36 +++ .../trader/rest/MemoryPressureResource.java | 74 ++++++ src/main/webapp/index.html | 202 +++++++++++++++++ start.sh | 8 +- 10 files changed, 773 insertions(+), 5 deletions(-) create mode 100644 src/main/java/fish/payara/trader/gc/GCStats.java create mode 100644 src/main/java/fish/payara/trader/gc/GCStatsService.java create mode 100644 src/main/java/fish/payara/trader/pressure/AllocationMode.java create mode 100644 src/main/java/fish/payara/trader/pressure/MemoryPressureService.java create mode 100644 src/main/java/fish/payara/trader/rest/GCStatsResource.java create mode 100644 src/main/java/fish/payara/trader/rest/MemoryPressureResource.java diff --git a/Dockerfile b/Dockerfile index 9d02baa..32aa741 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,10 +38,11 @@ EXPOSE 8080 # Azul Platform Prime uses C4 GC by default - no need to specify -XX:+UseZGC ENV JAVA_OPTS="-Xms8g \ -Xmx8g \ - -Xlog:gc*:file=/opt/payara/gc.log:time,uptime:filecount=5,filesize=10M \ + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M \ -XX:+UnlockDiagnosticVMOptions \ -XX:+UnlockExperimentalVMOptions \ -XX:+AlwaysPreTouch \ + -XX:+UseTransparentHugePages \ -XX:-UseBiasedLocking \ -Djava.net.preferIPv4Stack=true" diff --git a/Dockerfile.standard b/Dockerfile.standard index 55e50ea..34dfa4d 100644 --- a/Dockerfile.standard +++ b/Dockerfile.standard @@ -37,6 +37,9 @@ EXPOSE 8080 ENV JAVA_OPTS="-Xms8g \ -Xmx8g \ -XX:+UseG1GC \ + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M \ + -XX:+AlwaysPreTouch \ + -XX:+UseTransparentHugePages \ -Djava.net.preferIPv4Stack=true" # Health check diff --git a/src/main/java/fish/payara/trader/gc/GCStats.java b/src/main/java/fish/payara/trader/gc/GCStats.java new file mode 100644 index 0000000..23aa082 --- /dev/null +++ b/src/main/java/fish/payara/trader/gc/GCStats.java @@ -0,0 +1,77 @@ +package fish.payara.trader.gc; + +import java.util.List; + +public class GCStats { + private String gcName; + private long collectionCount; + private long collectionTime; + private long lastPauseDuration; + private List recentPauses; + private PausePercentiles percentiles; + private long totalMemory; + private long usedMemory; + private long freeMemory; + + public static class PausePercentiles { + private long p50; + private long p95; + private long p99; + private long p999; + private long max; + + public PausePercentiles() {} + + public PausePercentiles(long p50, long p95, long p99, long p999, long max) { + this.p50 = p50; + this.p95 = p95; + this.p99 = p99; + this.p999 = p999; + this.max = max; + } + + public long getP50() { return p50; } + public void setP50(long p50) { this.p50 = p50; } + + public long getP95() { return p95; } + public void setP95(long p95) { this.p95 = p95; } + + public long getP99() { return p99; } + public void setP99(long p99) { this.p99 = p99; } + + public long getP999() { return p999; } + public void setP999(long p999) { this.p999 = p999; } + + public long getMax() { return max; } + public void setMax(long max) { this.max = max; } + } + + public GCStats() {} + + public String getGcName() { return gcName; } + public void setGcName(String gcName) { this.gcName = gcName; } + + public long getCollectionCount() { return collectionCount; } + public void setCollectionCount(long collectionCount) { this.collectionCount = collectionCount; } + + public long getCollectionTime() { return collectionTime; } + public void setCollectionTime(long collectionTime) { this.collectionTime = collectionTime; } + + public long getLastPauseDuration() { return lastPauseDuration; } + public void setLastPauseDuration(long lastPauseDuration) { this.lastPauseDuration = lastPauseDuration; } + + public List getRecentPauses() { return recentPauses; } + public void setRecentPauses(List recentPauses) { this.recentPauses = recentPauses; } + + public PausePercentiles getPercentiles() { return percentiles; } + public void setPercentiles(PausePercentiles percentiles) { this.percentiles = percentiles; } + + public long getTotalMemory() { return totalMemory; } + public void setTotalMemory(long totalMemory) { this.totalMemory = totalMemory; } + + public long getUsedMemory() { return usedMemory; } + public void setUsedMemory(long usedMemory) { this.usedMemory = usedMemory; } + + public long getFreeMemory() { return freeMemory; } + public void setFreeMemory(long freeMemory) { this.freeMemory = freeMemory; } +} diff --git a/src/main/java/fish/payara/trader/gc/GCStatsService.java b/src/main/java/fish/payara/trader/gc/GCStatsService.java new file mode 100644 index 0000000..52c80c8 --- /dev/null +++ b/src/main/java/fish/payara/trader/gc/GCStatsService.java @@ -0,0 +1,128 @@ +package fish.payara.trader.gc; + +import jakarta.enterprise.context.ApplicationScoped; + +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryUsage; +import java.util.*; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +@ApplicationScoped +public class GCStatsService { + + private static final Logger LOGGER = Logger.getLogger(GCStatsService.class.getName()); + private static final int MAX_PAUSE_HISTORY = 1000; + + private final Map> pauseHistory = new HashMap<>(); + private final Map lastCollectionCount = new HashMap<>(); + private final Map lastCollectionTime = new HashMap<>(); + + public List collectGCStats() { + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); + MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); + + List statsList = new ArrayList<>(); + + for (GarbageCollectorMXBean gcBean : gcBeans) { + String gcName = gcBean.getName(); + long currentCount = gcBean.getCollectionCount(); + long currentTime = gcBean.getCollectionTime(); + + GCStats stats = new GCStats(); + stats.setGcName(gcName); + stats.setCollectionCount(currentCount); + stats.setCollectionTime(currentTime); + + // Calculate pause duration since last check + Long prevCount = lastCollectionCount.get(gcName); + Long prevTime = lastCollectionTime.get(gcName); + + if (prevCount != null && prevTime != null) { + long countDelta = currentCount - prevCount; + long timeDelta = currentTime - prevTime; + + if (countDelta > 0) { + long avgPauseDuration = timeDelta / countDelta; + stats.setLastPauseDuration(avgPauseDuration); + + // Track pause history + ConcurrentLinkedDeque history = pauseHistory.computeIfAbsent( + gcName, k -> new ConcurrentLinkedDeque<>() + ); + + // Add new pauses (simplified - one entry per collection) + for (int i = 0; i < countDelta; i++) { + history.addLast(avgPauseDuration); + if (history.size() > MAX_PAUSE_HISTORY) { + history.removeFirst(); + } + } + } + } + + // Update tracking + lastCollectionCount.put(gcName, currentCount); + lastCollectionTime.put(gcName, currentTime); + + // Get recent pauses + ConcurrentLinkedDeque history = pauseHistory.get(gcName); + if (history != null && !history.isEmpty()) { + stats.setRecentPauses(new ArrayList<>(history).subList( + Math.max(0, history.size() - 100), history.size() + )); + + // Calculate percentiles + List sortedPauses = history.stream() + .sorted() + .collect(Collectors.toList()); + + stats.setPercentiles(calculatePercentiles(sortedPauses)); + } else { + stats.setRecentPauses(Collections.emptyList()); + stats.setPercentiles(new GCStats.PausePercentiles(0, 0, 0, 0, 0)); + } + + // Memory stats + stats.setTotalMemory(heapUsage.getMax()); + stats.setUsedMemory(heapUsage.getUsed()); + stats.setFreeMemory(heapUsage.getMax() - heapUsage.getUsed()); + + statsList.add(stats); + } + + return statsList; + } + + private GCStats.PausePercentiles calculatePercentiles(List sortedPauses) { + if (sortedPauses.isEmpty()) { + return new GCStats.PausePercentiles(0, 0, 0, 0, 0); + } + + int size = sortedPauses.size(); + return new GCStats.PausePercentiles( + percentile(sortedPauses, 0.50), + percentile(sortedPauses, 0.95), + percentile(sortedPauses, 0.99), + percentile(sortedPauses, 0.999), + sortedPauses.get(size - 1) + ); + } + + private long percentile(List sortedValues, double percentile) { + int index = (int) Math.ceil(percentile * sortedValues.size()) - 1; + index = Math.max(0, Math.min(index, sortedValues.size() - 1)); + return sortedValues.get(index); + } + + public void resetStats() { + pauseHistory.clear(); + lastCollectionCount.clear(); + lastCollectionTime.clear(); + LOGGER.info("GC statistics reset"); + } +} diff --git a/src/main/java/fish/payara/trader/pressure/AllocationMode.java b/src/main/java/fish/payara/trader/pressure/AllocationMode.java new file mode 100644 index 0000000..0f73137 --- /dev/null +++ b/src/main/java/fish/payara/trader/pressure/AllocationMode.java @@ -0,0 +1,35 @@ +package fish.payara.trader.pressure; + +public enum AllocationMode { + OFF(0, 0, "No additional allocation"), + LOW(10, 1024, "10 KB/iteration - Light pressure"), + MEDIUM(50, 1024, "50 KB/iteration - Moderate pressure"), + HIGH(200, 1024, "200 KB/iteration - Heavy pressure"), + EXTREME(1000, 1024, "1 MB/iteration - Extreme pressure"); + + private final int allocationsPerIteration; + private final int bytesPerAllocation; + private final String description; + + AllocationMode(int allocationsPerIteration, int bytesPerAllocation, String description) { + this.allocationsPerIteration = allocationsPerIteration; + this.bytesPerAllocation = bytesPerAllocation; + this.description = description; + } + + public int getAllocationsPerIteration() { + return allocationsPerIteration; + } + + public int getBytesPerAllocation() { + return bytesPerAllocation; + } + + public String getDescription() { + return description; + } + + public long getBytesPerSecond() { + return (long) allocationsPerIteration * bytesPerAllocation * 10; // 10 iterations/sec + } +} diff --git a/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java new file mode 100644 index 0000000..a131352 --- /dev/null +++ b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java @@ -0,0 +1,212 @@ +package fish.payara.trader.pressure; + +import fish.payara.trader.concurrency.VirtualThreadExecutor; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.concurrent.ManagedExecutorService; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; +import java.util.logging.Logger; + +/** + * Service to generate controlled memory pressure for GC stress testing. + * Based on 1BRC techniques - intentional allocation to demonstrate GC behavior. + */ +@ApplicationScoped +public class MemoryPressureService { + + private static final Logger LOGGER = Logger.getLogger(MemoryPressureService.class.getName()); + + private volatile AllocationMode currentMode = AllocationMode.OFF; + private volatile boolean running = false; + private Future pressureTask; + + private long totalBytesAllocated = 0; + private long lastStatsTime = System.currentTimeMillis(); + + @Inject + @VirtualThreadExecutor + private ManagedExecutorService executorService; + + @PostConstruct + public void init() { + LOGGER.info("MemoryPressureService initialized"); + } + + public synchronized void setAllocationMode(AllocationMode mode) { + if (mode == currentMode) { + return; + } + + LOGGER.info("Changing allocation mode from " + currentMode + " to " + mode); + currentMode = mode; + + if (mode == AllocationMode.OFF) { + stopPressure(); + } else { + startPressure(); + } + } + + private synchronized void startPressure() { + if (running) { + return; + } + + running = true; + totalBytesAllocated = 0; + lastStatsTime = System.currentTimeMillis(); + + pressureTask = executorService.submit(() -> { + LOGGER.info("Memory pressure generator started with mode: " + currentMode); + + while (running) { + try { + AllocationMode mode = currentMode; + if (mode == AllocationMode.OFF) { + break; + } + + // Generate garbage for this iteration + generateGarbage(mode); + + // Sleep 100ms between iterations (10 iterations/sec) + Thread.sleep(100); + + // Log stats every 5 seconds + logStats(); + + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } catch (Exception e) { + LOGGER.warning("Error in memory pressure generator: " + e.getMessage()); + } + } + + LOGGER.info("Memory pressure generator stopped"); + }); + } + + private synchronized void stopPressure() { + running = false; + if (pressureTask != null && !pressureTask.isDone()) { + pressureTask.cancel(true); + pressureTask = null; + } + } + + private void generateGarbage(AllocationMode mode) { + int allocations = mode.getAllocationsPerIteration(); + int bytesPerAlloc = mode.getBytesPerAllocation(); + + for (int i = 0; i < allocations; i++) { + // Mix different allocation patterns + int pattern = i % 4; + + switch (pattern) { + case 0: + // String allocations (most common in real apps) + generateStringGarbage(bytesPerAlloc); + break; + case 1: + // Byte array allocations + generateByteArrayGarbage(bytesPerAlloc); + break; + case 2: + // Object allocations + generateObjectGarbage(bytesPerAlloc / 64); // ~64 bytes per object + break; + case 3: + // Collection allocations + generateCollectionGarbage(bytesPerAlloc / 100); // ~100 bytes per list item + break; + } + + totalBytesAllocated += bytesPerAlloc; + } + } + + private void generateStringGarbage(int bytes) { + // Create strings via concatenation (generates intermediate garbage) + StringBuilder sb = new StringBuilder(bytes); + for (int i = 0; i < bytes / 10; i++) { + sb.append("GARBAGE"); + } + String garbage = sb.toString(); + // String is now eligible for GC + } + + private void generateByteArrayGarbage(int bytes) { + byte[] garbage = new byte[bytes]; + // Fill with random data to prevent compiler optimization + ThreadLocalRandom.current().nextBytes(garbage); + // Array is now eligible for GC + } + + private void generateObjectGarbage(int count) { + List garbage = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + garbage.add(new DummyObject(i, "data-" + i, System.nanoTime())); + } + // List and objects are now eligible for GC + } + + private void generateCollectionGarbage(int count) { + List garbage = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + garbage.add(ThreadLocalRandom.current().nextInt()); + } + // List is now eligible for GC + } + + private void logStats() { + long now = System.currentTimeMillis(); + if (now - lastStatsTime >= 5000) { + double elapsedSeconds = (now - lastStatsTime) / 1000.0; + double mbPerSec = (totalBytesAllocated / (1024.0 * 1024.0)) / elapsedSeconds; + + LOGGER.info(String.format( + "Memory Pressure Stats - Mode: %s | Allocated: %.2f MB | Rate: %.2f MB/sec", + currentMode, totalBytesAllocated / (1024.0 * 1024.0), mbPerSec + )); + + totalBytesAllocated = 0; + lastStatsTime = now; + } + } + + @PreDestroy + public void shutdown() { + LOGGER.info("Shutting down MemoryPressureService"); + stopPressure(); + } + + public AllocationMode getCurrentMode() { + return currentMode; + } + + public boolean isRunning() { + return running; + } + + /** + * Dummy object for allocation testing + */ + private static class DummyObject { + private final int id; + private final String data; + private final long timestamp; + + DummyObject(int id, String data, long timestamp) { + this.id = id; + this.data = data; + this.timestamp = timestamp; + } + } +} diff --git a/src/main/java/fish/payara/trader/rest/GCStatsResource.java b/src/main/java/fish/payara/trader/rest/GCStatsResource.java new file mode 100644 index 0000000..056cc4f --- /dev/null +++ b/src/main/java/fish/payara/trader/rest/GCStatsResource.java @@ -0,0 +1,36 @@ +package fish.payara.trader.rest; + +import fish.payara.trader.gc.GCStats; +import fish.payara.trader.gc.GCStatsService; +import jakarta.inject.Inject; +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import java.util.List; + +/** + * REST endpoint for GC statistics monitoring + */ +@Path("/gc") +public class GCStatsResource { + + @Inject + private GCStatsService gcStatsService; + + @GET + @Path("/stats") + @Produces(MediaType.APPLICATION_JSON) + public Response getGCStats() { + List stats = gcStatsService.collectGCStats(); + return Response.ok(stats).build(); + } + + @POST + @Path("/reset") + @Produces(MediaType.APPLICATION_JSON) + public Response resetStats() { + gcStatsService.resetStats(); + return Response.ok().entity("{\"status\":\"reset\"}").build(); + } +} diff --git a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java new file mode 100644 index 0000000..85e2ea3 --- /dev/null +++ b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java @@ -0,0 +1,74 @@ +package fish.payara.trader.rest; + +import fish.payara.trader.pressure.AllocationMode; +import fish.payara.trader.pressure.MemoryPressureService; +import jakarta.inject.Inject; +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import java.util.HashMap; +import java.util.Map; + +/** + * REST endpoint for controlling memory pressure testing + */ +@Path("/pressure") +public class MemoryPressureResource { + + @Inject + private MemoryPressureService pressureService; + + @GET + @Path("/status") + @Produces(MediaType.APPLICATION_JSON) + public Response getStatus() { + Map status = new HashMap<>(); + status.put("currentMode", pressureService.getCurrentMode().name()); + status.put("description", pressureService.getCurrentMode().getDescription()); + status.put("running", pressureService.isRunning()); + status.put("bytesPerSecond", pressureService.getCurrentMode().getBytesPerSecond()); + return Response.ok(status).build(); + } + + @POST + @Path("/mode/{mode}") + @Produces(MediaType.APPLICATION_JSON) + public Response setMode(@PathParam("mode") String modeStr) { + try { + AllocationMode mode = AllocationMode.valueOf(modeStr.toUpperCase()); + pressureService.setAllocationMode(mode); + + Map result = new HashMap<>(); + result.put("success", true); + result.put("mode", mode.name()); + result.put("description", mode.getDescription()); + result.put("bytesPerSecond", mode.getBytesPerSecond()); + + return Response.ok(result).build(); + } catch (IllegalArgumentException e) { + Map error = new HashMap<>(); + error.put("success", false); + error.put("error", "Invalid mode: " + modeStr); + error.put("validModes", new String[]{"OFF", "LOW", "MEDIUM", "HIGH", "EXTREME"}); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); + } + } + + @GET + @Path("/modes") + @Produces(MediaType.APPLICATION_JSON) + public Response getModes() { + Map> modes = new HashMap<>(); + + for (AllocationMode mode : AllocationMode.values()) { + Map modeInfo = new HashMap<>(); + modeInfo.put("name", mode.name()); + modeInfo.put("description", mode.getDescription()); + modeInfo.put("bytesPerSecond", mode.getBytesPerSecond()); + modes.put(mode.name(), modeInfo); + } + + return Response.ok(modes).build(); + } +} diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 836059b..3d7d30d 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -354,6 +354,8 @@ letter-spacing: 0.05em; transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); box-shadow: 0 2px 4px rgba(0,0,0,0.2); + position: relative; + overflow: hidden; } button:hover { @@ -365,6 +367,26 @@ box-shadow: 0 1px 2px rgba(0,0,0,0.2); } + button.processing { + opacity: 0.7; + transform: scale(0.98); + } + + button.processing::after { + content: ''; + position: absolute; + top: 0; + left: -100%; + width: 100%; + height: 100%; + background: rgba(255, 255, 255, 0.3); + animation: slide 0.6s; + } + + @keyframes slide { + to { left: 100%; } + } + /* Chart specific styles */ .chart-mode-label { background: rgba(255,255,255,0.2); @@ -471,6 +493,69 @@

GC Statistics

+ +
+
+
+ GC Challenge Mode + OFF + Click to learn +
+
+
+
Allocation Rate
+
+ 0 MB/sec +
+
+
+ + + + + +
+
+
+
+

GC Challenge Mode

+

Intentionally generates garbage to stress-test the GC under load. Watch how each collector responds.

+

Azul C4: Maintains low, consistent pause times even under EXTREME allocation pressure. Concurrent collection keeps up.

+

G1GC: Pause times spike as allocation rate increases. You'll see stuttering in message delivery and chart updates.

+

Rates: LOW=0.1MB/s, MEDIUM=0.5MB/s, HIGH=2MB/s, EXTREME=10MB/s

+
+ Technique: Mixed allocation patterns (Strings, arrays, objects, collections)
+ Inspired by: 1BRC stress testing +
+
+
+ + +
+
+
+ GC Pause Time (Live) + -- + Click to learn +
+
+ +
+
+
+

GC Pause Time Monitor

+

Real-time visualization of garbage collection pause times. Lower is better.

+

Azul C4 (Pauseless): Flat line near zero. Collections happen concurrently without stopping application threads.

+

G1GC (Stop-the-World): Visible spikes when GC pauses occur. Higher message rates = higher pause times.

+

This chart demonstrates why C4 is ideal for latency-sensitive applications - predictable, consistent performance without pause spikes.

+
+ API: /api/gc/stats
+ Metric: Last pause duration (ms)
+ Update Rate: Every 3 seconds +
+
+
+
@@ -560,6 +645,7 @@

System Log

let lastGcCount=0,lastGcPollTime=0; let priceChart = null; let hiccupChart = null; + let gcPauseChart = null; let lastRateUpdate = Date.now(); let maxGapInWindow = 0; @@ -627,6 +713,40 @@

System Log

} } }); + + // GC Pause Time Chart (Line) + const ctxGcPause = document.getElementById('gcPauseChart').getContext('2d'); + gcPauseChart = new Chart(ctxGcPause, { + type: 'line', + data: { + labels: Array(50).fill(''), + datasets: [{ + label: 'GC Pause Time (ms)', + data: Array(50).fill(0), + borderColor: '#7c3aed', + backgroundColor: 'rgba(124, 58, 237, 0.1)', + borderWidth: 2, + tension: 0.1, + fill: true, + pointRadius: 0 + }] + }, + options: { + responsive: true, + maintainAspectRatio: false, + animation: false, + plugins: { legend: { display: false } }, + scales: { + x: { display: false }, + y: { + beginAtZero: true, + suggestedMax: 10, + grid: { color: 'rgba(0,0,0,0.05)' }, + title: { display: true, text: 'Pause Duration (ms)', font: { size: 12, weight: 'bold' } } + } + } + } + }); } function updateCharts() { @@ -765,6 +885,78 @@

System Log

document.getElementById('heapPercent').textContent = heapPercent; } + function pollGCPauseStats() { + fetch('/api/gc/stats') + .then(response => response.json()) + .then(stats => { + if (stats && stats.length > 0) { + const gcStat = stats[0]; + + // Update collector name + document.getElementById('gcPauseCollectorName').textContent = gcStat.gcName || '--'; + + // Update chart with last pause duration + if (gcPauseChart) { + const pauseMs = gcStat.lastPauseDuration || 0; + gcPauseChart.data.labels.push(''); + gcPauseChart.data.datasets[0].data.push(pauseMs); + + if (gcPauseChart.data.labels.length > 50) { + gcPauseChart.data.labels.shift(); + gcPauseChart.data.datasets[0].data.shift(); + } + + gcPauseChart.update('none'); + } + } + }) + .catch(error => console.error('Error fetching GC pause stats:', error)); + } + + function setPressureMode(mode) { + // Find the button that was clicked + const buttons = document.querySelectorAll('[onclick*="setPressureMode"]'); + let clickedButton = null; + buttons.forEach(btn => { + if (btn.textContent.trim() === mode) { + clickedButton = btn; + btn.classList.add('processing'); + } + }); + + fetch('/api/pressure/mode/' + mode, { method: 'POST' }) + .then(response => response.json()) + .then(result => { + if (clickedButton) { + clickedButton.classList.remove('processing'); + } + if (result.success) { + addLog('Memory pressure mode set to: ' + mode); + pollPressureStatus(); // Immediate update + } else { + addLog('Error setting pressure mode: ' + result.error); + } + }) + .catch(error => { + if (clickedButton) { + clickedButton.classList.remove('processing'); + } + console.error('Error setting pressure mode:', error); + addLog('Failed to set pressure mode: ' + error.message); + }); + } + + function pollPressureStatus() { + fetch('/api/pressure/status') + .then(response => response.json()) + .then(status => { + document.getElementById('pressureMode').textContent = status.currentMode || 'OFF'; + const rateMBps = (status.bytesPerSecond / (1024 * 1024)).toFixed(1); + document.getElementById('pressureRate').textContent = rateMBps + ' MB/sec'; + }) + .catch(error => console.error('Error fetching pressure status:', error)); + } + function flipCard(card) { card.classList.toggle('flipped'); } @@ -795,6 +987,16 @@

System Log

if (window.gcStatsInterval) clearInterval(window.gcStatsInterval); window.gcStatsInterval = setInterval(pollGCStats, 3000); pollGCStats(); // Initial poll + + // Start GC pause stats polling (every 3 seconds) + if (window.gcPauseStatsInterval) clearInterval(window.gcPauseStatsInterval); + window.gcPauseStatsInterval = setInterval(pollGCPauseStats, 3000); + pollGCPauseStats(); // Initial poll + + // Start memory pressure status polling (every 3 seconds) + if (window.pressureStatusInterval) clearInterval(window.pressureStatusInterval); + window.pressureStatusInterval = setInterval(pollPressureStatus, 3000); + pollPressureStatus(); // Initial poll }; ws.onclose=( )=>{ document.getElementById('statusIndicator').classList.remove('connected'); diff --git a/start.sh b/start.sh index fe779e1..50b9e5e 100755 --- a/start.sh +++ b/start.sh @@ -44,7 +44,7 @@ case "$ACTION" in echo "🚀 [Azul Prime] Starting with AERON Architecture (Optimized)..." echo " > Dockerfile (Azul) + MODE=AERON" echo "" - MODE=AERON $DOCKER_COMPOSE -f docker-compose.yml up -d --build + MODE=AERON $DOCKER_COMPOSE -f docker-compose.yml up -d --build --force-recreate ;; azul-direct) @@ -52,7 +52,7 @@ case "$ACTION" in echo " > Dockerfile (Azul) + MODE=DIRECT" echo " ℹ️ Observe how C4 handles high-allocation legacy code." echo "" - MODE=DIRECT $DOCKER_COMPOSE -f docker-compose.yml up -d --build + MODE=DIRECT $DOCKER_COMPOSE -f docker-compose.yml up -d --build --force-recreate ;; # --- Standard OpenJDK (G1 GC) Scenarios --- @@ -62,7 +62,7 @@ case "$ACTION" in echo " > Dockerfile.standard (Temurin) + MODE=DIRECT" echo " ℹ️ Baseline performance: High allocation on G1GC." echo "" - MODE=DIRECT $DOCKER_COMPOSE -f docker-compose-standard.yml up -d --build + MODE=DIRECT $DOCKER_COMPOSE -f docker-compose-standard.yml up -d --build --force-recreate ;; standard-aeron) @@ -70,7 +70,7 @@ case "$ACTION" in echo " > Dockerfile.standard (Temurin) + MODE=AERON" echo " ℹ️ Observe if off-heap transport helps G1GC." echo "" - MODE=AERON $DOCKER_COMPOSE -f docker-compose-standard.yml up -d --build + MODE=AERON $DOCKER_COMPOSE -f docker-compose-standard.yml up -d --build --force-recreate ;; # --- Utilities --- From 13576949396df00de05e01151c58e7a027f6b4d3 Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Thu, 18 Dec 2025 10:55:40 +0000 Subject: [PATCH 09/17] reduce logs --- .../trader/aeron/MarketDataPublisher.java | 18 ++++++++++++++---- .../payara/trader/rest/GCStatsResource.java | 6 ++++++ .../trader/rest/MemoryPressureResource.java | 19 ++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index 6e42d93..e46a216 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -74,6 +74,8 @@ public class MarketDataPublisher { private volatile boolean initialized = false; private volatile boolean running = false; private boolean isDirectMode; + private long lastWarningLogTime = 0; + private static final long WARNING_LOG_INTERVAL_MS = 5000; // Log warnings at most once per 5 seconds private Future publisherFuture; private Future statsFuture; @@ -444,7 +446,7 @@ private void offer(UnsafeBuffer buffer, int offset, int length, String messageTy consecutiveFailures.set(0); return; } else if (result == Publication.BACK_PRESSURED) { - LOGGER.fine("Back pressured on " + messageType); + // Back pressure is normal at high throughput, don't log retries--; try { Thread.sleep(1); @@ -453,20 +455,28 @@ private void offer(UnsafeBuffer buffer, int offset, int length, String messageTy return; } } else if (result == Publication.NOT_CONNECTED) { - LOGGER.warning("Publication not connected"); + logWarningRateLimited("Publication not connected"); handlePublishFailure(messageType); return; } else { - LOGGER.warning("Offer failed for " + messageType + ": " + result); + logWarningRateLimited("Offer failed for " + messageType + ": " + result); handlePublishFailure(messageType); return; } } - LOGGER.warning("Failed to publish " + messageType + " after retries"); + logWarningRateLimited("Failed to publish " + messageType + " after retries"); handlePublishFailure(messageType); } + private void logWarningRateLimited(String message) { + long now = System.currentTimeMillis(); + if (now - lastWarningLogTime >= WARNING_LOG_INTERVAL_MS) { + LOGGER.warning(message); + lastWarningLogTime = now; + } + } + private void handlePublishFailure(String messageType) { int failures = consecutiveFailures.incrementAndGet(); if (failures >= MAX_CONSECUTIVE_FAILURES) { diff --git a/src/main/java/fish/payara/trader/rest/GCStatsResource.java b/src/main/java/fish/payara/trader/rest/GCStatsResource.java index 056cc4f..edd07b6 100644 --- a/src/main/java/fish/payara/trader/rest/GCStatsResource.java +++ b/src/main/java/fish/payara/trader/rest/GCStatsResource.java @@ -8,6 +8,7 @@ import jakarta.ws.rs.core.Response; import java.util.List; +import java.util.logging.Logger; /** * REST endpoint for GC statistics monitoring @@ -15,6 +16,8 @@ @Path("/gc") public class GCStatsResource { + private static final Logger LOGGER = Logger.getLogger(GCStatsResource.class.getName()); + @Inject private GCStatsService gcStatsService; @@ -22,7 +25,9 @@ public class GCStatsResource { @Path("/stats") @Produces(MediaType.APPLICATION_JSON) public Response getGCStats() { + LOGGER.fine("GET /api/gc/stats - Collecting GC statistics"); List stats = gcStatsService.collectGCStats(); + LOGGER.info(String.format("GET /api/gc/stats - Returned %d GC collector stats", stats.size())); return Response.ok(stats).build(); } @@ -30,6 +35,7 @@ public Response getGCStats() { @Path("/reset") @Produces(MediaType.APPLICATION_JSON) public Response resetStats() { + LOGGER.info("POST /api/gc/reset - Resetting GC statistics"); gcStatsService.resetStats(); return Response.ok().entity("{\"status\":\"reset\"}").build(); } diff --git a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java index 85e2ea3..97440b9 100644 --- a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java +++ b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.logging.Logger; /** * REST endpoint for controlling memory pressure testing @@ -16,6 +17,8 @@ @Path("/pressure") public class MemoryPressureResource { + private static final Logger LOGGER = Logger.getLogger(MemoryPressureResource.class.getName()); + @Inject private MemoryPressureService pressureService; @@ -23,11 +26,14 @@ public class MemoryPressureResource { @Path("/status") @Produces(MediaType.APPLICATION_JSON) public Response getStatus() { + AllocationMode currentMode = pressureService.getCurrentMode(); + LOGGER.fine(String.format("GET /api/pressure/status - Current mode: %s", currentMode.name())); + Map status = new HashMap<>(); - status.put("currentMode", pressureService.getCurrentMode().name()); - status.put("description", pressureService.getCurrentMode().getDescription()); + status.put("currentMode", currentMode.name()); + status.put("description", currentMode.getDescription()); status.put("running", pressureService.isRunning()); - status.put("bytesPerSecond", pressureService.getCurrentMode().getBytesPerSecond()); + status.put("bytesPerSecond", currentMode.getBytesPerSecond()); return Response.ok(status).build(); } @@ -37,6 +43,9 @@ public Response getStatus() { public Response setMode(@PathParam("mode") String modeStr) { try { AllocationMode mode = AllocationMode.valueOf(modeStr.toUpperCase()); + LOGGER.info(String.format("POST /api/pressure/mode/%s - Setting memory pressure mode to: %s (%.2f MB/sec)", + modeStr, mode.name(), mode.getBytesPerSecond() / (1024.0 * 1024.0))); + pressureService.setAllocationMode(mode); Map result = new HashMap<>(); @@ -47,6 +56,8 @@ public Response setMode(@PathParam("mode") String modeStr) { return Response.ok(result).build(); } catch (IllegalArgumentException e) { + LOGGER.warning(String.format("POST /api/pressure/mode/%s - Invalid mode requested", modeStr)); + Map error = new HashMap<>(); error.put("success", false); error.put("error", "Invalid mode: " + modeStr); @@ -59,6 +70,8 @@ public Response setMode(@PathParam("mode") String modeStr) { @Path("/modes") @Produces(MediaType.APPLICATION_JSON) public Response getModes() { + LOGGER.fine("GET /api/pressure/modes - Listing all allocation modes"); + Map> modes = new HashMap<>(); for (AllocationMode mode : AllocationMode.values()) { From 3c526eab0cb421c99ae318234964bec70c518aeb Mon Sep 17 00:00:00 2001 From: Luqman <27694244+pedanticdev@users.noreply.github.com> Date: Sat, 20 Dec 2025 11:17:37 +0000 Subject: [PATCH 10/17] Scalability This pull request introduces horizontal scalability and clustering support using Hazelcast, enabling the application to run across multiple instances with shared state and synchronized message broadcasting. Clustering and Synchronization * Hazelcast Integration: Added Hazelcast configuration and integrated HazelcastInstance to manage cluster-wide state and messaging. * Distributed Broadcasting: Updated MarketDataBroadcaster to use Hazelcast ITopic for synchronizing market data across all cluster members. * Global Counters: Implemented a cluster-wide message counter using Hazelcast IAtomicLong to track total throughput across all instances. * Conditional Publishing: Added an ENABLE_PUBLISHER flag to control which instances generate market data while others act solely as consumers. Infrastructure and Orchestration * Scalable Docker Configurations: Created new Dockerfiles (Dockerfile.scale, Dockerfile.scale.standard) optimized for clustered environments using both Azul Prime and Standard OpenJDK. * Load Balancing: Introduced docker-compose-scale.yml featuring Traefik as a load balancer to distribute traffic across multiple service instances. * OOM Monitor: Added a monitor-oom.sh utility script that tracks container logs for memory-related errors and performs automatic restarts. * Enhanced Tooling: Updated start.sh with new commands for launching fixed clusters, dynamic scaling, and monitoring cluster status. Monitoring and UI * Cluster Endpoints: Added /api/status/cluster and expanded the status resource to report instance names and global message counts. * Frontend Updates: Modified the dashboard to display instance-specific identity, local throughput rates, and aggregate cluster statistics in real-time. --- .gitignore | 1 + Dockerfile.scale | 63 ++++ Dockerfile.scale.standard | 59 ++++ docker-compose-scale.yml | 211 ++++++++++++ monitor-oom.sh | 324 ++++++++++++++++++ pom.xml | 11 + .../trader/aeron/AeronSubscriberBean.java | 2 +- .../trader/aeron/MarketDataPublisher.java | 89 ++++- .../payara/trader/rest/StatusResource.java | 65 +++- .../websocket/MarketDataBroadcaster.java | 68 +++- src/main/resources/hazelcast-config.xml | 77 +++++ src/main/webapp/index.html | 47 ++- start.sh | 131 ++++++- 13 files changed, 1130 insertions(+), 18 deletions(-) create mode 100644 Dockerfile.scale create mode 100644 Dockerfile.scale.standard create mode 100644 docker-compose-scale.yml create mode 100755 monitor-oom.sh create mode 100644 src/main/resources/hazelcast-config.xml diff --git a/.gitignore b/.gitignore index a8b3edc..dd3e571 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ .settings .project .classpath +deployment/ *.iml .DS_Store diff --git a/Dockerfile.scale b/Dockerfile.scale new file mode 100644 index 0000000..2fb0d3f --- /dev/null +++ b/Dockerfile.scale @@ -0,0 +1,63 @@ +# Multi-stage Dockerfile for TradeStreamEE with Hazelcast Clustering +# Uses Azul Platform Prime (Zing) for Pauseless Garbage Collection demonstration + +FROM azul/zulu-openjdk:21 AS build +WORKDIR /app + +# Copy Maven wrapper and pom.xml first for better layer caching +COPY mvnw . +COPY mvnw.cmd . +COPY .mvn .mvn +COPY pom.xml . + +RUN ./mvnw dependency:go-offline -B + +COPY src ./src + +RUN ./mvnw clean package -DskipTests + +# Use Azul Platform Prime for C4 GC +FROM azul/prime:21 + +LABEL maintainer="TradeStreamEE" +LABEL description="High-frequency trading dashboard with Aeron + SBE + Payara Micro + Azul C4 + Hazelcast Clustering" + +WORKDIR /opt/payara + +# Add Payara Micro from URL +ARG PAYARA_VERSION=7.2025.2 +ADD https://nexus.payara.fish/repository/payara-community/fish/payara/extras/payara-micro/${PAYARA_VERSION}/payara-micro-${PAYARA_VERSION}.jar /opt/payara/payara-micro.jar + +# Copy WAR file from build stage +COPY --from=build /app/target/*.war trader-stream-ee.war + +EXPOSE 8080 +EXPOSE 5701 + +# Default JVM Options for Azul Platform Prime +# Note: Reduced heap size for multi-instance deployments +# Azul Platform Prime uses C4 GC by default - no need to specify -XX:+UseZGC +ENV JAVA_OPTS="-Xms4g \ + -Xmx4g \ + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M \ + -XX:+UnlockDiagnosticVMOptions \ + -XX:+UnlockExperimentalVMOptions \ + -XX:+AlwaysPreTouch \ + -XX:+UseTransparentHugePages \ + -XX:-UseBiasedLocking \ + -Djava.net.preferIPv4Stack=true" + +# Health check +HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ + CMD wget --spider -q http://localhost:8080/trader-stream-ee/api/status || exit 1 + +# Copy Hazelcast configuration +COPY src/main/resources/hazelcast-config.xml /opt/payara/hazelcast-config.xml + +# Run Payara Micro with the WAR +# NOTE: --nohazelcast flag is REMOVED to enable clustering +# Use custom Hazelcast configuration with split-brain protection +CMD java ${JAVA_OPTS} -jar payara-micro.jar \ + --deploy /opt/payara/trader-stream-ee.war \ + --contextroot trader-stream-ee \ + --hzconfigfile /opt/payara/hazelcast-config.xml diff --git a/Dockerfile.scale.standard b/Dockerfile.scale.standard new file mode 100644 index 0000000..3ab2e3c --- /dev/null +++ b/Dockerfile.scale.standard @@ -0,0 +1,59 @@ +# Multi-stage Dockerfile for TradeStreamEE with Hazelcast Clustering +# Uses Eclipse Temurin (Standard OpenJDK) for comparison against Azul Platform Prime + +FROM azul/zulu-openjdk:21 AS build +WORKDIR /app + +# Copy Maven wrapper and pom.xml first for better layer caching +COPY mvnw . +COPY mvnw.cmd . +COPY .mvn .mvn +COPY pom.xml . + +RUN ./mvnw dependency:go-offline -B + +COPY src ./src + +RUN ./mvnw clean package -DskipTests + +# Use Eclipse Temurin (Standard OpenJDK) +FROM eclipse-temurin:21 + +LABEL maintainer="TradeStreamEE" +LABEL description="High-frequency trading dashboard with Hazelcast Clustering (Standard JVM Comparison)" + +WORKDIR /opt/payara + +# Add Payara Micro from URL +ARG PAYARA_VERSION=7.2025.2 +ADD https://nexus.payara.fish/repository/payara-community/fish/payara/extras/payara-micro/${PAYARA_VERSION}/payara-micro-${PAYARA_VERSION}.jar /opt/payara/payara-micro.jar + +# Copy WAR file from build stage +COPY --from=build /app/target/*.war ROOT.war + +EXPOSE 8080 +EXPOSE 5701 + +# Standard JVM Options (G1GC) +# Note: Reduced heap size for multi-instance deployments +ENV JAVA_OPTS="-Xms4g \ + -Xmx4g \ + -XX:+UseG1GC \ + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M \ + -XX:+AlwaysPreTouch \ + -XX:+UseTransparentHugePages \ + -Djava.net.preferIPv4Stack=true" + +# Health check +HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ + CMD wget --spider -q http://localhost:8080/trader-stream-ee/api/status || exit 1 + +# Copy Hazelcast configuration +COPY src/main/resources/hazelcast-config.xml /opt/payara/hazelcast-config.xml + +# Run Payara Micro with the WAR +# NOTE: --nohazelcast flag is REMOVED to enable clustering +# Use custom Hazelcast configuration with split-brain protection +CMD java ${JAVA_OPTS} -jar payara-micro.jar \ + --deploy ROOT.war \ + --hzconfigfile /opt/payara/hazelcast-config.xml diff --git a/docker-compose-scale.yml b/docker-compose-scale.yml new file mode 100644 index 0000000..92dc70b --- /dev/null +++ b/docker-compose-scale.yml @@ -0,0 +1,211 @@ +services: + # Traefik Load Balancer + traefik: + image: traefik:v3.6.5 + container_name: trader-traefik + command: + # API and dashboard + - "--api.dashboard=true" + - "--api.insecure=true" + # Providers + - "--providers.docker=true" + - "--providers.docker.exposedbydefault=false" + - "--providers.docker.network=trader-stream-ee_trader-network" + # Entrypoints + - "--entrypoints.web.address=:80" + # Access logs + - "--accesslog=true" + # Metrics + - "--metrics.prometheus=true" + # Health check + - "--ping=true" + # Debug logging + - "--log.level=DEBUG" + ports: + - "8080:80" # Application traffic + - "8084:8080" # Traefik dashboard + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + networks: + - trader-network + restart: unless-stopped + healthcheck: + test: ["CMD", "traefik", "healthcheck", "--ping"] + interval: 10s + timeout: 5s + retries: 3 + + # Payara Micro Instance 1 (Publisher enabled for GC stress testing) + trader-stream-1: + image: trader-stream-ee:scale + build: + context: . + dockerfile: ${DOCKERFILE:-Dockerfile.scale} + container_name: trader-stream-1 + hostname: trader-stream-1 + ports: + - "8081:8080" # Direct access to instance-1 + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=${MODE:-AERON} + - PAYARA_INSTANCE_NAME=instance-1 + - HAZELCAST_MEMBER_NAME=trader-stream-1 + - ENABLE_PUBLISHER=true + labels: + - "traefik.enable=true" + - "traefik.http.routers.trader-stream.rule=PathPrefix(`/`)" + - "traefik.http.routers.trader-stream.entrypoints=web" + - "traefik.http.services.trader-stream.loadbalancer.server.port=8080" + # Health check + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.path=/trader-stream-ee/api/status" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.interval=30s" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.timeout=10s" + # Sticky sessions for WebSocket (optional) + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie=true" + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie.name=trader-session" + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/trader-stream-ee/api/status"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 60s + networks: + - trader-network + restart: unless-stopped + + # Payara Micro Instance 2 (Publisher enabled for GC stress testing) + trader-stream-2: + image: trader-stream-ee:scale + build: + context: . + dockerfile: ${DOCKERFILE:-Dockerfile.scale} + container_name: trader-stream-2 + hostname: trader-stream-2 + ports: + - "8082:8080" # Direct access to instance-2 + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=${MODE:-AERON} + - PAYARA_INSTANCE_NAME=instance-2 + - HAZELCAST_MEMBER_NAME=trader-stream-2 + - ENABLE_PUBLISHER=true + labels: + - "traefik.enable=true" + - "traefik.http.routers.trader-stream.rule=PathPrefix(`/`)" + - "traefik.http.routers.trader-stream.entrypoints=web" + - "traefik.http.services.trader-stream.loadbalancer.server.port=8080" + # Health check + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.path=/trader-stream-ee/api/status" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.interval=30s" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.timeout=10s" + # Sticky sessions for WebSocket (optional) + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie=true" + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie.name=trader-session" + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/trader-stream-ee/api/status"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 60s + networks: + - trader-network + restart: unless-stopped + + # Payara Micro Instance 3 (Publisher enabled for GC stress testing) + trader-stream-3: + image: trader-stream-ee:scale + build: + context: . + dockerfile: ${DOCKERFILE:-Dockerfile.scale} + container_name: trader-stream-3 + hostname: trader-stream-3 + ports: + - "8083:8080" # Direct access to instance-3 + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=${MODE:-AERON} + - PAYARA_INSTANCE_NAME=instance-3 + - HAZELCAST_MEMBER_NAME=trader-stream-3 + - ENABLE_PUBLISHER=true + labels: + - "traefik.enable=true" + - "traefik.http.routers.trader-stream.rule=PathPrefix(`/`)" + - "traefik.http.routers.trader-stream.entrypoints=web" + - "traefik.http.services.trader-stream.loadbalancer.server.port=8080" + # Health check + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.path=/trader-stream-ee/api/status" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.interval=30s" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.timeout=10s" + # Sticky sessions for WebSocket (optional) + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie=true" + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie.name=trader-session" + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/trader-stream-ee/api/status"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 60s + networks: + - trader-network + restart: unless-stopped + + # Scalable Payara Micro Instances + # Use this service for dynamic scaling: docker compose -f docker-compose-scale.yml up -d --scale trader-stream=5 + trader-stream: + image: trader-stream-ee:scale + build: + context: . + dockerfile: ${DOCKERFILE:-Dockerfile.scale} + # Note: No container_name or hostname - allows multiple instances + expose: + - "8080" + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=${MODE:-AERON} + # Instance name will be auto-generated by Docker (e.g., trader-stream-1, trader-stream-2) + labels: + - "traefik.enable=true" + - "traefik.http.routers.trader-stream.rule=PathPrefix(`/`)" + - "traefik.http.routers.trader-stream.entrypoints=web" + - "traefik.http.services.trader-stream.loadbalancer.server.port=8080" + # Health check + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.path=/trader-stream-ee/api/status" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.interval=30s" + - "traefik.http.services.trader-stream.loadbalancer.healthcheck.timeout=10s" + # Sticky sessions for WebSocket + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie=true" + - "traefik.http.services.trader-stream.loadbalancer.sticky.cookie.name=trader-session" + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/trader-stream-ee/api/status"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 60s + networks: + - trader-network + restart: unless-stopped + deploy: + replicas: 0 # Start with 0, use --scale to set desired count + + # Optional: Hazelcast Management Center for cluster monitoring + # Uncomment to enable + # hazelcast-mc: + # image: hazelcast/management-center:latest + # container_name: hazelcast-mc + # ports: + # - "8082:8080" + # networks: + # - trader-network + # environment: + # - MC_INIT_CMD=./bin/mc-conf.sh cluster add --cluster-name=payara-cluster --member-addresses=trader-stream-1:5701,trader-stream-2:5701,trader-stream-3:5701 + +networks: + trader-network: + driver: bridge diff --git a/monitor-oom.sh b/monitor-oom.sh new file mode 100755 index 0000000..cd9fadd --- /dev/null +++ b/monitor-oom.sh @@ -0,0 +1,324 @@ +#!/bin/bash + +############################################################################# +# OOM Monitor for Payara Micro Containers +# +# This script monitors Payara Micro containers for Out of Memory errors +# and automatically restarts containers when OOM is detected. +# +# Usage: +# ./monitor-oom.sh [interval_minutes] [container_prefix] +# +# Arguments: +# interval_minutes - How often to check (default: 5 minutes) +# container_prefix - Container name prefix to monitor (default: trader-stream-) +# +# Examples: +# ./monitor-oom.sh # Check every 5 minutes +# ./monitor-oom.sh 2 # Check every 2 minutes +# ./monitor-oom.sh 10 trader-stream- # Check every 10 minutes for trader-stream-* containers +# +# To run in background: +# ./monitor-oom.sh & +# echo $! > monitor-oom.pid +# +# To stop: +# kill $(cat monitor-oom.pid) +# +############################################################################# + +set -euo pipefail + +# Show help if requested +if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ] || [ "${1:-}" = "help" ]; then + echo "================================================" + echo " Payara Micro OOM Monitor - Help" + echo "================================================" + echo "" + echo "Usage:" + echo " ./monitor-oom.sh [interval_minutes] [container_prefix]" + echo "" + echo "Arguments:" + echo " interval_minutes - How often to check for OOM errors (default: 5 minutes)" + echo " container_prefix - Container name prefix to monitor (default: trader-stream-)" + echo "" + echo "Examples:" + echo " ./monitor-oom.sh # Check every 5 minutes for trader-stream-* containers" + echo " ./monitor-oom.sh 2 # Check every 2 minutes" + echo " ./monitor-oom.sh 10 trader-stream- # Check every 10 minutes for trader-stream-* containers" + echo "" + echo "Background Execution:" + echo " ./monitor-oom.sh & # Run in background" + echo " echo \$! > monitor-oom.pid # Save process ID for later" + echo " kill \$(cat monitor-oom.pid) # Stop the monitor" + echo "" + echo "Features:" + echo " - Monitors Docker container logs for OOM errors and WebSocket exceptions" + echo " - Automatically restarts containers when errors are detected" + echo " - Logs all activity to oom-monitor.log" + echo " - Automatic log rotation when file exceeds 10MB" + echo " - Waits for container health checks after restart" + echo "" + echo "Monitored Error Patterns:" + echo " - OutOfMemoryError (all variants)" + echo " - GC overhead limit exceeded" + echo " - WebSocket/Tyrus connection errors" + echo " - Broken pipe / Connection reset" + echo "" + echo "Signals:" + echo " Ctrl+C or SIGTERM - Gracefully stop monitoring" + echo "" + exit 0 +fi + +# Configuration +CHECK_INTERVAL_MINUTES=${1:-5} +CONTAINER_PREFIX=${2:-trader-stream-} +LOG_FILE="oom-monitor.log" +MAX_LOG_SIZE=10485760 # 10MB + +# OOM patterns to search for in logs +OOM_PATTERNS=( + "java.lang.OutOfMemoryError" + "OutOfMemoryError" + "Out of memory" + "GC overhead limit exceeded" + "Requested array size exceeds VM limit" + "unable to create new native thread" + "Metaspace" + # Eclipse Tyrus WebSocket exceptions that indicate container issues + "org.glassfish.tyrus" + "TyrusWebSocketEngine" + "Connection reset by peer" + "Broken pipe" + "UpgradeException" + "WebSocket connection closed" + "DeploymentException" + "HandshakeException" + "SessionException" + "Unexpected error during WebSocket" + "Failed to process WebSocket frame" + "WebSocket frame buffer overflow" +) + +# ANSI color codes +RED='\033[0;31m' +YELLOW='\033[1;33m' +GREEN='\033[0;32m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Logging functions +log() { + echo -e "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" +} + +log_info() { + log "${BLUE}[INFO]${NC} $1" +} + +log_warn() { + log "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + log "${RED}[ERROR]${NC} $1" +} + +log_success() { + log "${GREEN}[SUCCESS]${NC} $1" +} + +# Rotate log file if too large +rotate_log_if_needed() { + if [ -f "$LOG_FILE" ]; then + local size=$(stat -f%z "$LOG_FILE" 2>/dev/null || stat -c%s "$LOG_FILE" 2>/dev/null || echo 0) + if [ "$size" -gt "$MAX_LOG_SIZE" ]; then + mv "$LOG_FILE" "${LOG_FILE}.old" + log_info "Rotated log file (size: $size bytes)" + fi + fi +} + +# Check if Docker is available +check_docker() { + if ! command -v docker &> /dev/null; then + log_error "Docker is not installed or not in PATH" + exit 1 + fi + + if ! docker ps &> /dev/null; then + log_error "Cannot connect to Docker daemon. Is Docker running?" + exit 1 + fi +} + +# Get list of containers matching prefix +get_containers() { + docker ps --filter "name=${CONTAINER_PREFIX}" --format "{{.Names}}" 2>/dev/null || true +} + +# Check if container has OOM errors in logs +check_container_oom() { + local container=$1 + local found_oom=false + local oom_pattern="" + + # Get logs from last check interval (plus buffer) + local since_time="${CHECK_INTERVAL_MINUTES}m" + local logs=$(docker logs --since "$since_time" "$container" 2>&1 || true) + + if [ -z "$logs" ]; then + return 1 + fi + + # Check each OOM pattern + for pattern in "${OOM_PATTERNS[@]}"; do + if echo "$logs" | grep -q "$pattern"; then + found_oom=true + oom_pattern="$pattern" + break + fi + done + + if [ "$found_oom" = true ]; then + log_error "OOM detected in container '$container' - Pattern: '$oom_pattern'" + + # Extract and log the actual OOM error lines + local oom_lines=$(echo "$logs" | grep -A 3 "$oom_pattern" | head -10) + echo "$oom_lines" | while IFS= read -r line; do + log " | $line" + done + + return 0 + fi + + return 1 +} + +# Restart container +restart_container() { + local container=$1 + + log_warn "Restarting container '$container' due to OOM..." + + if docker restart "$container" &> /dev/null; then + log_success "Container '$container' restarted successfully" + + # Wait for container to be healthy + local max_wait=60 + local waited=0 + while [ $waited -lt $max_wait ]; do + local health=$(docker inspect --format='{{.State.Health.Status}}' "$container" 2>/dev/null || echo "none") + + if [ "$health" = "healthy" ]; then + log_success "Container '$container' is healthy after restart" + return 0 + elif [ "$health" = "none" ]; then + # No healthcheck defined, just check if running + if docker ps --filter "name=$container" --filter "status=running" | grep -q "$container"; then + log_info "Container '$container' is running (no healthcheck defined)" + return 0 + fi + fi + + sleep 2 + waited=$((waited + 2)) + done + + log_warn "Container '$container' restarted but health check timed out after ${max_wait}s" + return 0 + else + log_error "Failed to restart container '$container'" + return 1 + fi +} + +# Main monitoring loop +monitor() { + log_info "Starting OOM monitor for containers matching '$CONTAINER_PREFIX*'" + log_info "Check interval: ${CHECK_INTERVAL_MINUTES} minutes" + log_info "Log file: $LOG_FILE" + log_info "Press Ctrl+C to stop" + echo "" + + local iteration=0 + + while true; do + iteration=$((iteration + 1)) + rotate_log_if_needed + + log_info "Check #${iteration} - Scanning for OOM errors..." + + local containers=$(get_containers) + + if [ -z "$containers" ]; then + log_warn "No containers found matching prefix '$CONTAINER_PREFIX'" + else + local container_count=$(echo "$containers" | wc -l | tr -d ' ') + log_info "Found $container_count container(s) to monitor" + + local oom_detected=false + local restart_count=0 + + # Check each container + while IFS= read -r container; do + if [ -n "$container" ]; then + if check_container_oom "$container"; then + oom_detected=true + if restart_container "$container"; then + restart_count=$((restart_count + 1)) + fi + fi + fi + done <<< "$containers" + + if [ "$oom_detected" = false ]; then + log_success "No OOM errors detected in any container" + else + log_warn "Total containers restarted: $restart_count" + fi + fi + + log_info "Next check in ${CHECK_INTERVAL_MINUTES} minutes..." + echo "" + + # Sleep for specified interval + sleep $((CHECK_INTERVAL_MINUTES * 60)) + done +} + +# Signal handler for graceful shutdown +cleanup() { + echo "" + log_info "Received shutdown signal. Exiting..." + exit 0 +} + +trap cleanup SIGINT SIGTERM + +# Main entry point +main() { + echo "================================================" + echo " Payara Micro OOM Monitor" + echo "================================================" + echo "" + + # Validate interval - show help if invalid + if ! [[ "$CHECK_INTERVAL_MINUTES" =~ ^[0-9]+$ ]] || [ "$CHECK_INTERVAL_MINUTES" -lt 1 ]; then + echo "Error: Invalid parameter '$CHECK_INTERVAL_MINUTES'" + echo "Check interval must be a positive integer (minutes)" + echo "" + echo "For help, run: $0 --help" + echo "" + echo "Usage: $0 [interval_minutes] [container_prefix]" + echo "Example: $0 5 trader-stream-" + exit 1 + fi + + check_docker + monitor +} + +# Run main function +main diff --git a/pom.xml b/pom.xml index 85220be..4416df2 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ 11.0.0 7.2025.2 payara6 + 5.5.0 1.46.7 1.34.0 @@ -48,6 +49,12 @@ payara-api provided + + + com.hazelcast + hazelcast + provided + io.aeron @@ -164,6 +171,7 @@ false + maven-resources-plugin 3.3.1 + diff --git a/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java b/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java index 192229d..c8b303e 100644 --- a/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java +++ b/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java @@ -53,7 +53,7 @@ public class AeronSubscriberBean { private ManagedExecutorService managedExecutorService; void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { - init(); + managedExecutorService.submit(() -> init()); } diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index e46a216..41c385b 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -2,6 +2,8 @@ import java.util.concurrent.ThreadLocalRandom; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.cp.IAtomicLong; import fish.payara.trader.concurrency.VirtualThreadExecutor; import fish.payara.trader.sbe.*; import fish.payara.trader.websocket.MarketDataBroadcaster; @@ -87,24 +89,53 @@ public class MarketDataPublisher { @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") private String ingestionMode; + @Inject + @ConfigProperty(name = "ENABLE_PUBLISHER", defaultValue = "true") + String enablePublisherEnv; + @Inject private MarketDataBroadcaster broadcaster; - + + @Inject + private HazelcastInstance hazelcastInstance; + @Inject @VirtualThreadExecutor private ManagedExecutorService managedExecutorService; + // Cluster-wide message counter (shared across all instances) + private IAtomicLong clusterMessageCounter; + void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { +// managedExecutorService.submit(this::init); init(); } public void init() { - LOGGER.info("Initializing Market Data Publisher. Mode: " + ingestionMode); + // Check if publisher should be enabled on this instance + if (enablePublisherEnv != null && !"true".equalsIgnoreCase(enablePublisherEnv)) { + LOGGER.info("Market Data Publisher DISABLED on this instance (ENABLE_PUBLISHER=" + enablePublisherEnv + ")"); + LOGGER.info("This instance will only consume messages from the cluster topic via Hazelcast"); + return; + } + + LOGGER.info("Initializing Market Data Publisher (ENABLE_PUBLISHER=" + enablePublisherEnv + "). Mode: " + ingestionMode); if ("DIRECT".equalsIgnoreCase(ingestionMode)) { LOGGER.info("Running in DIRECT mode - Bypassing Aeron/SBE setup."); initialized = true; isDirectMode = true; + + // Initialize cluster-wide message counter + if (hazelcastInstance != null) { + try { + clusterMessageCounter = hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count"); + LOGGER.info("Initialized cluster-wide message counter (DIRECT mode)"); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to initialize cluster message counter, using local counter only", e); + } + } + startPublishing(); return; } @@ -146,6 +177,17 @@ public void init() { if (publication.isConnected()) { LOGGER.info("Market Data Publisher initialized successfully"); initialized = true; + + // Initialize cluster-wide message counter + if (hazelcastInstance != null) { + try { + clusterMessageCounter = hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count"); + LOGGER.info("Initialized cluster-wide message counter"); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to initialize cluster message counter, using local counter only", e); + } + } + startPublishing(); } else { LOGGER.warning("Publisher not connected after waiting"); @@ -249,6 +291,14 @@ private void publishTrade() { broadcaster.broadcastWithArtificialLoad(json); } messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical + } + } return; } @@ -299,6 +349,14 @@ private void publishQuote() { broadcaster.broadcastWithArtificialLoad(json); } messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical + } + } return; } @@ -362,6 +420,14 @@ private void publishMarketDepth() { broadcaster.broadcastWithArtificialLoad(json); } messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical + } + } return; } @@ -443,6 +509,14 @@ private void offer(UnsafeBuffer buffer, int offset, int length, String messageTy if (result > 0) { messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical + } + } consecutiveFailures.set(0); return; } else if (result == Publication.BACK_PRESSURED) { @@ -515,4 +589,15 @@ public void shutdown() { public long getMessagesPublished() { return messagesPublished.get(); } + + public long getClusterMessagesPublished() { + if (clusterMessageCounter != null) { + try { + return clusterMessageCounter.get(); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to read cluster message counter", e); + } + } + return 0; + } } diff --git a/src/main/java/fish/payara/trader/rest/StatusResource.java b/src/main/java/fish/payara/trader/rest/StatusResource.java index 06f01ed..676e70e 100644 --- a/src/main/java/fish/payara/trader/rest/StatusResource.java +++ b/src/main/java/fish/payara/trader/rest/StatusResource.java @@ -1,5 +1,7 @@ package fish.payara.trader.rest; +import com.hazelcast.cluster.Member; +import com.hazelcast.core.HazelcastInstance; import fish.payara.trader.aeron.AeronSubscriberBean; import fish.payara.trader.aeron.MarketDataPublisher; import fish.payara.trader.websocket.MarketDataBroadcaster; @@ -12,6 +14,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; /** * REST endpoint for monitoring system status @@ -28,17 +31,31 @@ public class StatusResource { @Inject private MarketDataBroadcaster broadcaster; + @Inject + private HazelcastInstance hazelcastInstance; + @GET @Produces(MediaType.APPLICATION_JSON) public Response getStatus() { Map status = new HashMap<>(); + // Get instance name from environment variable + String instanceName = System.getenv("PAYARA_INSTANCE_NAME"); + if (instanceName == null) { + instanceName = "standalone"; + } + status.put("application", "TradeStreamEE"); status.put("description", "High-frequency trading dashboard with Aeron and SBE"); + status.put("instance", instanceName); status.put("subscriber", subscriber.getStatus()); - status.put("publisher", Map.of( - "messagesPublished", publisher.getMessagesPublished() - )); + + // Include both local and cluster-wide message counts + Map publisherStats = new HashMap<>(); + publisherStats.put("localMessagesPublished", publisher.getMessagesPublished()); + publisherStats.put("clusterMessagesPublished", publisher.getClusterMessagesPublished()); + status.put("publisher", publisherStats); + status.put("websocket", Map.of( "activeSessions", broadcaster.getSessionCount() )); @@ -46,4 +63,46 @@ public Response getStatus() { return Response.ok(status).build(); } + + /** + * Get cluster status and membership information + */ + @GET + @Path("/cluster") + @Produces(MediaType.APPLICATION_JSON) + public Response getClusterStatus() { + Map clusterInfo = new HashMap<>(); + + try { + if (hazelcastInstance == null) { + clusterInfo.put("clustered", false); + clusterInfo.put("message", "Running in standalone mode (Hazelcast not available)"); + return Response.ok(clusterInfo).build(); + } + + clusterInfo.put("clustered", true); + clusterInfo.put("clusterSize", hazelcastInstance.getCluster().getMembers().size()); + clusterInfo.put("clusterTime", hazelcastInstance.getCluster().getClusterTime()); + clusterInfo.put("localMemberUuid", hazelcastInstance.getCluster().getLocalMember().getUuid().toString()); + + clusterInfo.put("members", hazelcastInstance.getCluster().getMembers().stream() + .map(member -> Map.of( + "address", member.getAddress().toString(), + "uuid", member.getUuid().toString(), + "localMember", member.localMember(), + "liteMember", member.isLiteMember() + )) + .collect(Collectors.toList()) + ); + + return Response.ok(clusterInfo).build(); + + } catch (Exception e) { + clusterInfo.put("clustered", false); + clusterInfo.put("error", e.getMessage()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(clusterInfo) + .build(); + } + } } diff --git a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java index 0e2bdd7..c106e59 100644 --- a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java +++ b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java @@ -1,6 +1,12 @@ package fish.payara.trader.websocket; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.topic.ITopic; +import com.hazelcast.topic.Message; +import com.hazelcast.topic.MessageListener; +import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; import jakarta.websocket.Session; import java.io.IOException; @@ -15,6 +21,10 @@ * Maintains a set of active WebSocket sessions and broadcasts * JSON messages to all connected clients. * + * In clustered mode, uses Hazelcast distributed topics to broadcast + * messages across all cluster members, ensuring all WebSocket clients + * receive data regardless of which instance they connect to. + * * Note: JSON string creation intentionally generates garbage * to stress-test Azul's Pauseless GC (C4). */ @@ -22,13 +32,44 @@ public class MarketDataBroadcaster { private static final Logger LOGGER = Logger.getLogger(MarketDataBroadcaster.class.getName()); + private static final String TOPIC_NAME = "market-data-broadcast"; private final Set sessions = ConcurrentHashMap.newKeySet(); + @Inject + private HazelcastInstance hazelcastInstance; + + private ITopic clusterTopic; + // Statistics private long messagesSent = 0; private long lastStatsTime = System.currentTimeMillis(); + /** + * Initialize Hazelcast topic subscription for cluster-wide broadcasting + */ + @PostConstruct + public void init() { + try { + if (hazelcastInstance != null) { + clusterTopic = hazelcastInstance.getTopic(TOPIC_NAME); + clusterTopic.addMessageListener(new MessageListener() { + @Override + public void onMessage(Message message) { + // Broadcast to local WebSocket sessions only + broadcastLocal(message.getMessageObject()); + } + }); + LOGGER.info("Subscribed to Hazelcast topic: " + TOPIC_NAME + " (clustered mode)"); + } else { + LOGGER.info("Hazelcast not available - running in standalone mode"); + } + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Failed to initialize Hazelcast topic subscription", e); + // Continue without clustering + } + } + /** * Register a new WebSocket session */ @@ -46,12 +87,37 @@ public void removeSession(Session session) { } /** - * Broadcast JSON message to all connected clients + * Broadcast JSON message to all connected clients across the cluster. + * + * In clustered mode, publishes to Hazelcast topic which distributes + * to all cluster members. Each member then broadcasts to its local + * WebSocket sessions. + * + * In standalone mode, broadcasts directly to local sessions. * * This method intentionally creates string allocations to * generate garbage and stress the garbage collector. */ public void broadcast(String jsonMessage) { + if (clusterTopic != null) { + // Publish to cluster-wide topic + try { + clusterTopic.publish(jsonMessage); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Failed to publish to Hazelcast topic, falling back to local broadcast", e); + broadcastLocal(jsonMessage); + } + } else { + // Standalone mode - broadcast locally only + broadcastLocal(jsonMessage); + } + } + + /** + * Broadcast message to local WebSocket sessions only. + * Called either in standalone mode or by Hazelcast topic listener. + */ + private void broadcastLocal(String jsonMessage) { if (sessions.isEmpty()) { return; } diff --git a/src/main/resources/hazelcast-config.xml b/src/main/resources/hazelcast-config.xml new file mode 100644 index 0000000..c586391 --- /dev/null +++ b/src/main/resources/hazelcast-config.xml @@ -0,0 +1,77 @@ + + + + + payara-trader-cluster + + + + 5701 + + + + + + + + + + trader-stream-1 + trader-stream-2 + trader-stream-3 + + + trader-stream-1:5701 + trader-stream-2:5701 + trader-stream-3:5701 + + + + + + + + + + + 10 + BLOCK + true + + + + + + + + + 5 + + + + + + + + jdk + + + 4 + 3 + + + NOISY + 30 + + + false + + + 300 + 120 + + + diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 3d7d30d..2aeeccb 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -411,11 +411,16 @@

TradeStreamEE

Disconnected +
-
0
Backend Msg/sec
-
0
Total Generated
+
0
Local Msg/sec
+
0
Local Total
+
+
+
0
Cluster Total
+
0
Cluster Msg/sec
0
UI Msg/sec
@@ -957,6 +962,38 @@

System Log

.catch(error => console.error('Error fetching pressure status:', error)); } + let lastClusterTotal = 0; + let lastClusterTime = Date.now(); + + function pollInstanceStatus() { + fetch(window.location.pathname + 'api/status') + .then(response => response.json()) + .then(status => { + // Update instance name + if (status.instance) { + document.getElementById('instanceDisplay').textContent = '[' + status.instance + ']'; + } + + // Update cluster total and calculate rate + if (status.publisher && status.publisher.clusterMessagesPublished !== undefined) { + const clusterTotal = status.publisher.clusterMessagesPublished; + document.getElementById('clusterTotal').textContent = clusterTotal.toLocaleString(); + + // Calculate cluster message rate + const now = Date.now(); + const timeDiff = (now - lastClusterTime) / 1000; // seconds + const messageDiff = clusterTotal - lastClusterTotal; + if (timeDiff > 0 && lastClusterTotal > 0) { + const clusterRate = Math.round(messageDiff / timeDiff); + document.getElementById('clusterRate').textContent = clusterRate.toLocaleString(); + } + lastClusterTotal = clusterTotal; + lastClusterTime = now; + } + }) + .catch(error => console.error('Error fetching instance status:', error)); + } + function flipCard(card) { card.classList.toggle('flipped'); } @@ -997,6 +1034,11 @@

System Log

if (window.pressureStatusInterval) clearInterval(window.pressureStatusInterval); window.pressureStatusInterval = setInterval(pollPressureStatus, 3000); pollPressureStatus(); // Initial poll + + // Start instance status polling (every 2 seconds for cluster stats) + if (window.instanceStatusInterval) clearInterval(window.instanceStatusInterval); + window.instanceStatusInterval = setInterval(pollInstanceStatus, 2000); + pollInstanceStatus(); // Initial poll }; ws.onclose=( )=>{ document.getElementById('statusIndicator').classList.remove('connected'); @@ -1083,6 +1125,7 @@

System Log

window.addEventListener('load', () => { initCharts(); + pollInstanceStatus(); // Get instance info immediately setTimeout(connect, 1000); }); diff --git a/start.sh b/start.sh index 50b9e5e..2dbd34c 100755 --- a/start.sh +++ b/start.sh @@ -72,67 +72,180 @@ case "$ACTION" in echo "" MODE=AERON $DOCKER_COMPOSE -f docker-compose-standard.yml up -d --build --force-recreate ;; - + + # --- Clustered Scenarios --- + + cluster|cluster-azul) + echo "🚀 [Azul Prime Cluster] Starting 3-instance cluster with AERON..." + echo " > Dockerfile.scale (Azul) + MODE=AERON + Nginx LB" + echo " ℹ️ Demonstrates horizontal scalability with Hazelcast clustering." + echo "" + MODE=AERON DOCKERFILE=Dockerfile.scale $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --build --force-recreate + echo "" + echo "✅ Cluster started. Waiting for instances to be ready..." + sleep 10 + echo "" + echo "📊 Cluster Status:" + curl -s http://localhost:8080/trader-stream-ee/api/status/cluster 2>/dev/null | jq . || echo "Cluster info endpoint not available yet" + echo "" + echo "🌐 Access: http://localhost:8080/trader-stream-ee/" + ;; + + cluster-standard) + echo "🚀 [Standard JDK Cluster] Starting 3-instance cluster with AERON..." + echo " > Dockerfile.scale.standard (Temurin) + MODE=AERON + Nginx LB" + echo " ℹ️ Compare cluster performance with G1GC." + echo "" + MODE=AERON DOCKERFILE=Dockerfile.scale.standard $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --build --force-recreate + echo "" + echo "✅ Cluster started. Waiting for instances to be ready..." + sleep 10 + echo "" + echo "📊 Cluster Status:" + curl -s http://localhost:8080/trader-stream-ee/api/status/cluster 2>/dev/null | jq . || echo "Cluster info endpoint not available yet" + echo "" + echo "🌐 Access: http://localhost:8080/trader-stream-ee/" + ;; + + cluster-direct) + echo "🚀 [Azul Prime Cluster] Starting 3-instance cluster with DIRECT mode..." + echo " > Dockerfile.scale (Azul) + MODE=DIRECT + Traefik LB" + echo " ℹ️ Test cluster with high-allocation legacy mode." + echo "" + MODE=DIRECT DOCKERFILE=Dockerfile.scale $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --build --force-recreate + ;; + + cluster-dynamic) + echo "🚀 [Dynamic Cluster] Starting scalable cluster..." + if [ -z "$2" ]; then + INSTANCES=3 + echo " > No instance count specified, defaulting to 3" + else + INSTANCES=$2 + fi + echo " > Dockerfile.scale (Azul) + MODE=AERON + $INSTANCES scalable instances" + echo " ℹ️ Uses generic service for true dynamic scaling." + echo "" + MODE=AERON DOCKERFILE=Dockerfile.scale $DOCKER_COMPOSE -f docker-compose-scale.yml build trader-stream + $DOCKER_COMPOSE -f docker-compose-scale.yml up -d traefik + $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --scale trader-stream=$INSTANCES --no-recreate trader-stream + echo "" + echo "✅ Cluster started. Waiting for instances to be ready..." + sleep 15 + echo "" + echo "📊 Cluster Status:" + curl -s http://localhost:8080/trader-stream-ee/api/status/cluster 2>/dev/null | jq . || echo "Cluster info endpoint not available yet" + echo "" + echo "🌐 Access: http://localhost:8080/trader-stream-ee/" + ;; + + scale) + echo "🚀 Scaling cluster..." + if [ -z "$2" ]; then + echo "Usage: ./start.sh scale " + echo "Example: ./start.sh scale 5" + exit 1 + fi + INSTANCES=$2 + echo " > Scaling to $INSTANCES instances (using generic trader-stream service)" + echo " > Note: This uses the scalable service, not the named instances (trader-stream-1/2/3)" + $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --scale trader-stream=$INSTANCES --no-recreate + echo "" + echo "✅ Scaled to $INSTANCES instances" + sleep 10 + echo "" + echo "📊 Checking cluster status..." + curl -s http://localhost:8080/trader-stream-ee/api/status/cluster 2>/dev/null | jq . || echo "Cluster info not available yet" + ;; + # --- Utilities --- down|stop) echo "🛑 Stopping TradeStreamEE..." $DOCKER_COMPOSE -f docker-compose.yml down $DOCKER_COMPOSE -f docker-compose-standard.yml down + $DOCKER_COMPOSE -f docker-compose-scale.yml down echo "✅ Stopped" ;; - + restart) echo "🔄 Restarting..." ./start.sh stop ./start.sh start ;; - + logs) echo "📋 Showing logs..." # Try to find which container is running - if docker ps | grep -q "trader-stream-ee"; then - docker logs -f trader-stream-ee + if docker ps | grep -q "trader-stream"; then + if docker ps | grep -q "trader-stream-1"; then + # Cluster mode - show all instances + echo "Cluster mode detected - showing logs from all instances..." + $DOCKER_COMPOSE -f docker-compose-scale.yml logs -f + else + # Single instance mode + docker logs -f trader-stream-ee + fi else echo "❌ No running container found." fi ;; - + status) echo "📊 Checking status..." echo "" if curl -f http://localhost:8080/trader-stream-ee/api/status 2>/dev/null | jq .; then echo "" echo "✅ Application is running" + echo "" + # Check if cluster mode + if curl -s http://localhost:8080/trader-stream-ee/api/status/cluster 2>/dev/null | grep -q "clustered"; then + echo "📡 Cluster Status:" + curl -s http://localhost:8080/trader-stream-ee/api/status/cluster 2>/dev/null | jq . + fi else echo "❌ Application is not responding" echo "Try: ./start.sh logs" fi ;; - + clean) echo "🧹 Cleaning up..." $DOCKER_COMPOSE -f docker-compose.yml down -v $DOCKER_COMPOSE -f docker-compose-standard.yml down -v + $DOCKER_COMPOSE -f docker-compose-scale.yml down -v docker system prune -f echo "✅ Cleaned" ;; - + *) echo "Usage: ./start.sh [command]" echo "" - echo "JVM Comparison Matrix:" + echo "Single Instance Modes:" echo " azul-aeron - Azul Prime (C4) + Aeron (Optimized) [Default]" echo " azul-direct - Azul Prime (C4) + Direct (Legacy)" echo " standard-direct - Standard JDK (G1) + Direct (Legacy) [Baseline]" echo " standard-aeron - Standard JDK (G1) + Aeron (Optimized)" echo "" + echo "Clustered Modes (Fixed 3 instances + Traefik LB):" + echo " cluster - Azul Prime (C4) + Aeron + Hazelcast Cluster" + echo " cluster-azul - Same as 'cluster'" + echo " cluster-standard - Standard JDK (G1) + Aeron + Hazelcast Cluster" + echo " cluster-direct - Azul Prime (C4) + Direct + Hazelcast Cluster" + echo "" + echo "Dynamic Scaling (Generic service + Traefik LB):" + echo " cluster-dynamic [N] - Start N scalable instances (default: 3)" + echo " scale N - Scale existing dynamic cluster to N instances" + echo "" echo "Utilities:" echo " logs - Show application logs" echo " status - Check if application is running" echo " stop - Stop the application" echo " clean - Stop and clean all containers/volumes" echo "" + echo "Documentation:" + echo " See SCALABILITY.md for clustering details" + echo "" exit 1 ;; esac \ No newline at end of file From 2729a99fa32a824f568a69e00510cc5e959cdf60 Mon Sep 17 00:00:00 2001 From: Luqman <27694244+pedanticdev@users.noreply.github.com> Date: Sat, 20 Dec 2025 20:28:04 +0000 Subject: [PATCH 11/17] Observability This pull request introduces a complete JVM performance comparison environment, enabling high-fidelity monitoring and stress testing of Azul C4 against Standard G1GC. Observability & Monitoring * Full Stack Deployment: Added Prometheus, Grafana, Loki, and Promtail to collect and visualize JVM metrics and container logs. * High-Fidelity GC Tracking: Refactored GCStatsService and introduced GCPauseMonitor to use JMX Notifications, capturing individual pause events with exact timing instead of averaged polling. * Custom Dashboards: Provisioned a Grafana dashboard featuring P99 pause comparisons, collection rates, and heap usage heatmaps. * JMX Integration: Configured JMX Exporter 1.0.1 as a Java Agent to expose low-level JVM internals to Prometheus. Performance & Stress Testing * Massive Allocation: Scaled EXTREME allocation mode to ~2GB/sec to demonstrate collector behavior under heavy load. * Tenured Generation Pressure: Implemented aggressive old-gen promotion (~200MB/s) to trigger expensive G1GC Mixed/Full collections. * Reactive Metrics: Reduced percentile history windows to ensure real-time responsiveness to workload changes. * Scenario Presets: Added one-click "Demo Scenarios" (Baseline, Normal, Stress, Extreme) for consistent presentation results. Infrastructure & Isolation * Cluster Separation: Isolated C4 and G1 clusters using unique Hazelcast names and Traefik constraints, preventing cross-cluster data pollution. * Load Balancing: Configured Traefik load balancers for each cluster with healthcheck-aware routing. * Automation: Provided start-comparison.sh and stop-comparison.sh for streamlined deployment and cleanup. User Interface * Comparison Panel: Added a real-time metrics grid to the dashboard showing P99 pauses, all-time max pauses, and SLA violations. * Context Root Fixes: Corrected backend API endpoint paths to ensure compatibility with the application context root. --- .gitignore | 4 +- Dockerfile.scale | 4 + Dockerfile.scale.standard | 9 +- README.md | 264 +++++++++++++- docker-compose-c4.yml | 141 ++++++++ docker-compose-g1.yml | 144 ++++++++ docker-compose-monitoring.yml | 82 +++++ .../grafana/dashboards/jvm-comparison.json | 155 ++++++++ .../provisioning/dashboards/dashboards.yml | 12 + .../provisioning/datasources/datasources.yml | 19 + .../jmx-exporter/jmx-exporter-config.yml | 57 +++ monitoring/loki/loki-config.yml | 37 ++ monitoring/prometheus/prometheus.yml | 57 +++ monitoring/promtail/promtail-config.yml | 67 ++++ .../trader/aeron/MarketDataPublisher.java | 88 +++-- .../fish/payara/trader/gc/GCStatsService.java | 123 ++++--- .../trader/monitoring/GCPauseMonitor.java | 236 ++++++++++++ .../trader/monitoring/SLAMonitorService.java | 101 ++++++ .../trader/pressure/AllocationMode.java | 8 +- .../pressure/MemoryPressureService.java | 58 ++- .../payara/trader/rest/GCStatsResource.java | 104 ++++++ .../trader/rest/MemoryPressureResource.java | 60 ++++ .../websocket/MarketDataBroadcaster.java | 20 +- src/main/webapp/index.html | 335 +++++++++++++++++- start-comparison.sh | 83 +++++ stop-comparison.sh | 12 + 26 files changed, 2168 insertions(+), 112 deletions(-) create mode 100644 docker-compose-c4.yml create mode 100644 docker-compose-g1.yml create mode 100644 docker-compose-monitoring.yml create mode 100644 monitoring/grafana/dashboards/jvm-comparison.json create mode 100644 monitoring/grafana/provisioning/dashboards/dashboards.yml create mode 100644 monitoring/grafana/provisioning/datasources/datasources.yml create mode 100644 monitoring/jmx-exporter/jmx-exporter-config.yml create mode 100644 monitoring/loki/loki-config.yml create mode 100644 monitoring/prometheus/prometheus.yml create mode 100644 monitoring/promtail/promtail-config.yml create mode 100644 src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java create mode 100644 src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java create mode 100755 start-comparison.sh create mode 100755 stop-comparison.sh diff --git a/.gitignore b/.gitignore index dd3e571..e55e6e9 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,6 @@ container/ docs/* *.md !README.md -*.pdf \ No newline at end of file +*.pdf +*.jar +monitoring/jmx-exporter/*.jar diff --git a/Dockerfile.scale b/Dockerfile.scale index 2fb0d3f..de8b009 100644 --- a/Dockerfile.scale +++ b/Dockerfile.scale @@ -54,6 +54,10 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ # Copy Hazelcast configuration COPY src/main/resources/hazelcast-config.xml /opt/payara/hazelcast-config.xml +# Set unique cluster name for C4 environment and update discovery members +RUN sed -i 's/payara-trader-cluster/payara-trader-c4/' /opt/payara/hazelcast-config.xml && \ + sed -i 's/trader-stream-/trader-stream-c4-/' /opt/payara/hazelcast-config.xml + # Run Payara Micro with the WAR # NOTE: --nohazelcast flag is REMOVED to enable clustering # Use custom Hazelcast configuration with split-brain protection diff --git a/Dockerfile.scale.standard b/Dockerfile.scale.standard index 3ab2e3c..a46ac40 100644 --- a/Dockerfile.scale.standard +++ b/Dockerfile.scale.standard @@ -29,7 +29,7 @@ ARG PAYARA_VERSION=7.2025.2 ADD https://nexus.payara.fish/repository/payara-community/fish/payara/extras/payara-micro/${PAYARA_VERSION}/payara-micro-${PAYARA_VERSION}.jar /opt/payara/payara-micro.jar # Copy WAR file from build stage -COPY --from=build /app/target/*.war ROOT.war +COPY --from=build /app/target/*.war trader-stream-ee.war EXPOSE 8080 EXPOSE 5701 @@ -51,9 +51,14 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ # Copy Hazelcast configuration COPY src/main/resources/hazelcast-config.xml /opt/payara/hazelcast-config.xml +# Set unique cluster name for G1 environment and update discovery members +RUN sed -i 's/payara-trader-cluster/payara-trader-g1/' /opt/payara/hazelcast-config.xml && \ + sed -i 's/trader-stream-/trader-stream-g1-/' /opt/payara/hazelcast-config.xml + # Run Payara Micro with the WAR # NOTE: --nohazelcast flag is REMOVED to enable clustering # Use custom Hazelcast configuration with split-brain protection CMD java ${JAVA_OPTS} -jar payara-micro.jar \ - --deploy ROOT.war \ + --deploy trader-stream-ee.war \ + --contextroot trader-stream-ee \ --hzconfigfile /opt/payara/hazelcast-config.xml diff --git a/README.md b/README.md index 06d9ffe..83d22d7 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,14 @@ The `start.sh` script provides commands to run the TradeStreamEE application in | **3. Fixing Legacy Code** | `./start.sh azul-direct` | Azul Prime (C4) | Direct (Heavy) | Show how C4 can stabilize a high-allocation app without code changes. | | **4. Optimizing Standard Java** | `./start.sh standard-aeron` | Standard JDK (G1GC) | Aeron (Optimized) | See if architectural optimization helps G1GC performance. | +### Observability Commands +* `./start-comparison.sh` - Deploy complete JVM comparison stack (recommended) +* `./stop-comparison.sh` - Stop all comparison services +* `docker-compose -f docker-compose-monitoring.yml up -d` - Start monitoring stack only +* `docker-compose -f docker-compose-c4.yml up -d` - Start C4 cluster only +* `docker-compose -f docker-compose-g1.yml up -d` - Start G1GC cluster only +* `docker-compose -f docker-compose-monitoring.yml ps` - Check monitoring status + ### Utilities * `./start.sh logs` - View live logs * `./start.sh stop` - Stop containers @@ -211,19 +219,243 @@ Controls how data moves from the Publisher to the Processor. * `AERON` (Default): Uses the high-speed binary ring buffer. * `DIRECT`: Bypasses Aeron; generates Strings directly in the Publisher loop. Useful for isolating Transport vs. GC overhead. -### JVM Tuning (Azul Prime) +### JVM Tuning & Configuration -The `Dockerfile` is pre-configured with best practices for the C4 collector: +The Docker configurations are optimized with enhanced settings for performance testing: +**Azul Prime (C4) Configuration:** ```dockerfile -ENV JAVA_OPTS="-Xms2g -Xmx2g -XX:+AlwaysPreTouch -Djava.net.preferIPv4Stack=true" +ENV JAVA_OPTS="-Xms8g -Xmx8g -XX:+AlwaysPreTouch -XX:+UseTransparentHugePages -Djava.net.preferIPv4Stack=true" +``` + +**Standard JDK (G1GC) Configuration:** +```dockerfile +ENV JAVA_OPTS="-Xms8g -Xmx8g -XX:+UseG1GC -XX:+AlwaysPreTouch -XX:+UseTransparentHugePages -Djava.net.preferIPv4Stack=true" +``` + +**Infrastructure Improvements:** + +* **8GB Heap Size**: Increased from 2GB to 8GB to handle extreme memory pressure testing +* **Pre-touch Memory** (`-XX:+AlwaysPreTouch`): Pre-allocates heap pages to eliminate runtime allocation overhead +* **Transparent Huge Pages** (`-XX:+UseTransparentHugePages`): Reduces TLB misses for large memory operations +* **Enhanced GC Logging**: Detailed GC event logging with decorators for comprehensive analysis +* **Rate-Limited Logging**: Prevents log flooding during high-throughput operations +* **Maven Wrapper**: Ensures consistent build environments across platforms + +**Note:** We purposefully **do not** use `-XX:+UseZGC` in the Azul Prime image, as C4 is the native, optimized collector for Azul Platform Prime. + +### GC Monitoring & Stress Testing + +The application includes comprehensive GC monitoring and memory pressure testing capabilities to demonstrate JVM performance differences: + +#### GC Statistics Collection + +**GCStatsService** collects real-time garbage collection metrics via JMX MXBeans: + +* **Collection Metrics**: Total collection count and time for each GC type +* **Pause Time Analysis**: Individual pause durations with percentile calculations (P50, P95, P99, P99.9, Max) +* **Memory Usage**: Heap memory utilization (total, used, free) +* **Recent Pause History**: Last 100 pause times for trend analysis + +#### Memory Pressure Testing + +**MemoryPressureService** provides controlled memory allocation to stress test GC performance: + +**Allocation Modes:** +* **OFF**: No additional allocation +* **LOW**: 1 MB/sec - Light pressure +* **MEDIUM**: 10 MB/sec - Moderate pressure +* **HIGH**: 500 MB/sec - Heavy pressure +* **EXTREME**: 2 GB/sec - Extreme pressure + +Each mode allocates byte arrays in a background thread to create realistic memory pressure, allowing observation of: +* C4's concurrent collection vs G1GC's "stop-the-world" pauses +* Latency impact under increasing memory pressure +* Throughput degradation patterns + +#### GC Challenge Mode + +The web UI includes **GC Challenge Mode** controls that allow: +* Real-time switching between allocation modes +* Visual feedback showing current stress level +* Side-by-side pause time visualization +* Immediate observation of collector behavior under load + +This feature enables live demonstration of how Azul C4 maintains low pause times even under extreme memory pressure, while G1GC shows increasingly long pauses. + + + +## 📊 Monitoring & Observability + +TradeStreamEE includes comprehensive monitoring infrastructure to compare JVM performance between Azul C4 and standard G1GC configurations. + +### Monitoring Stack + +| Component | Technology | Purpose | Access | +|:---|:---|:---|:---| +| **Metrics Collection** | Prometheus + JMX Exporter | JVM GC metrics, memory, threads | http://localhost:9090 | +| **Visualization** | Grafana | Performance dashboards | http://localhost:3000 (admin/admin) | +| **Log Aggregation** | Loki + Promtail | Centralized log management | http://localhost:3100 | +| **Load Balancing** | Traefik | Traffic distribution + metrics | http://localhost:8080 (C4), http://localhost:9080 (G1) | + +### JVM Comparison Dashboard + +The pre-configured Grafana dashboard provides: + +* **GC Pause Time Comparison** - P99 latency comparison between C4 and G1GC +* **GC Collection Count Rate** - Collection frequency analysis +* **Heap Memory Usage** - Real-time memory utilization +* **Thread Count** - Concurrent thread monitoring +* **GC Pause Distribution** - Heatmap showing pause time patterns +* **Performance Summary** - Key metrics with threshold alerts + +### Starting the Observability Stack + +#### Option 1: Automated Setup (Recommended) + +Use the provided `start-comparison.sh` script for complete automated deployment: + +```bash +# Deploy entire JVM comparison stack +./start-comparison.sh +``` + +This script automatically: +* Creates the monitoring directory structure +* Downloads the JMX Prometheus exporter +* Builds the application and Docker images +* Creates required Docker networks +* Starts the complete monitoring stack (Prometheus, Grafana, Loki) +* Deploys both C4 and G1GC clusters with load balancers + +#### Option 2: Manual Setup + +For granular control, start components manually: + +```bash +# Create required networks +docker network create trader-network +docker network create monitoring + +# Start monitoring infrastructure +docker-compose -f docker-compose-monitoring.yml up -d + +# Start C4 cluster (Azul Prime) +docker-compose -f docker-compose-c4.yml up -d + +# Start G1 cluster (Eclipse Temurin) +docker-compose -f docker-compose-g1.yml up -d + +# View monitoring stack status +docker-compose -f docker-compose-monitoring.yml ps ``` -* **Note:** We purposefully **do not** use `-XX:+UseZGC` in the optimized image, as C4 is the native collector for Azul Prime. +#### Stopping the Comparison + +Use the provided stop script: + +```bash +# Stop all comparison services +./stop-comparison.sh + +# Stop all comparison services and remove volumes +./stop-comparison.sh --prune +``` + +### Access Points + +After starting the observability stack: + +* **Grafana Dashboard**: http://localhost:3000 (admin/admin) +* **C4 Application**: http://localhost:8080 (via Traefik load balancer) +* **G1 Application**: http://localhost:9080 (via Traefik load balancer) +* **Prometheus**: http://localhost:9090 +* **Individual C4 instances**: http://localhost:8081, http://localhost:8082, http://localhost:8083 +* **Individual G1 instances**: http://localhost:9081, http://localhost:9082, http://localhost:9083 + +### Monitoring Configuration + +#### JMX Exporter +Each JVM instance runs a JMX exporter agent that exposes: +* Garbage collection metrics (pause times, collection counts) +* Memory pool usage (heap/non-heap) +* Thread information +* Custom application metrics + +#### Prometheus Configuration +The Prometheus setup (`monitoring/prometheus/prometheus.yml`) scrapes: +* JMX metrics from all JVM instances (ports 9010-9022) +* Traefik metrics for load balancer performance +* Self-monitoring metrics + +#### Log Collection +Promtail automatically collects and ships container logs to Loki, enabling: +* Log-based troubleshooting +* Correlation of performance issues with application events +* JVM type and instance label-based filtering +### Stress Testing the Comparison +After deploying the observability stack, you can stress test both JVM configurations to observe the performance differences: -## 📊 Monitoring & Metrics +#### Memory Pressure API Endpoints + +```bash +# Set allocation mode for memory pressure testing +curl -X POST http://localhost:8080/api/pressure/mode/EXTREME # C4 cluster +curl -X POST http://localhost:9080/api/pressure/mode/EXTREME # G1GC cluster + +# Available modes: OFF, LOW, MEDIUM, HIGH, EXTREME + +# Get current GC statistics +curl http://localhost:8080/api/gc/stats +curl http://localhost:9080/api/gc/stats + +# Reset GC statistics +curl -X POST http://localhost:8080/api/gc/reset +curl -X POST http://localhost:9080/api/gc/reset +``` + +#### UI-Based Testing + +The web interface provides interactive controls: + +* **GC Challenge Mode Panel**: Select allocation modes with visual buttons +* **Real-time Pause Time Chart**: Shows GC pauses as they occur +* **Backend Message Rate Display**: Monitor throughput impact +* **Visual Feedback**: Immediate color-coded response to mode changes + +#### Expected Results + +The stress tests will: +1. Generate controlled allocation rates (1 MB to 2 GB per second) +2. Create realistic memory pressure scenarios +3. Allow real-time comparison of pause times between C4 and G1GC +4. Demonstrate C4's concurrent collection vs G1GC's "stop-the-world" pauses +5. Show latency impact and throughput degradation patterns +6. Visualize the "pauseless" characteristics of C4 under extreme load + +**Sample GC Stats Response:** +```json +{ + "gcName": "C4", + "collectionCount": 1543, + "collectionTime": 8934, + "lastPauseDuration": 0.5, + "percentiles": { + "p50": 0.3, + "p95": 1.2, + "p99": 2.8, + "p999": 5.6, + "max": 12.4 + }, + "totalMemory": 8589934592, + "usedMemory": 3221225472, + "freeMemory": 5368709120 +} +``` + +## 📊 Application Metrics The application exposes a lightweight REST endpoint for health checks and internal metrics. @@ -257,11 +489,31 @@ src/main/ │ ├── aeron/ # Aeron Publisher, Subscriber, FragmentHandler │ ├── sbe/ # Generated SBE Codecs (Flyweights) │ ├── websocket/ # Jakarta WebSocket Endpoint -│ └── rest/ # Status Resource +│ ├── rest/ # Status, GC Stats, and Memory Pressure Resources +│ ├── gc/ # GC statistics collection and monitoring +│ ├── pressure/ # Memory pressure testing services +│ └── monitoring/ # GC monitoring services (GCPauseMonitor, MemoryPressure) ├── resources/sbe/ │ └── market-data.xml # SBE Schema Definition └── webapp/ └── index.html # Dashboard UI (Chart.js + WebSocket) + +monitoring/ +├── grafana/ +│ ├── provisioning/ # Grafana datasources and dashboard configs +│ └── dashboards/ # Pre-configured JVM comparison dashboard +├── jmx-exporter/ # JMX exporter configuration and JAR +├── prometheus/ # Prometheus configuration +├── loki/ # Loki log aggregation config +└── promtail/ # Promtail log shipping config + +start-comparison.sh # Automated deployment script for JVM comparison +stop-comparison.sh # Stop script for comparison services +docker-compose-c4.yml # Azul Prime C4 cluster setup +docker-compose-g1.yml # Eclipse Temurin G1GC cluster setup +docker-compose-monitoring.yml # Monitoring stack (Prometheus, Grafana, Loki) +Dockerfile.scale # Multi-stage build for C4 instances +Dockerfile.scale.standard # Build for G1GC instances ``` ## 📜 License diff --git a/docker-compose-c4.yml b/docker-compose-c4.yml new file mode 100644 index 0000000..3469212 --- /dev/null +++ b/docker-compose-c4.yml @@ -0,0 +1,141 @@ +services: + traefik-c4: + image: traefik:v3.6.5 + container_name: trader-traefik-c4 + command: + - "--api.dashboard=true" + - "--api.insecure=true" + - "--providers.docker=true" + - "--providers.docker.exposedbydefault=false" + - "--providers.docker.constraints=Label(`cluster`, `c4`)" + - "--entrypoints.web.address=:80" + - "--metrics.prometheus=true" + - "--metrics.prometheus.addServicesLabels=true" + ports: + - "8080:80" + - "8084:8080" + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + networks: + - trader-network + - monitoring + labels: + - "jvm_type=azul-c4" + restart: unless-stopped + + trader-stream-c4-1: + image: trader-stream-ee:c4 + build: + context: . + dockerfile: Dockerfile.scale + container_name: trader-stream-c4-1 + hostname: trader-stream-c4-1 + ports: + - "8081:8080" + - "9010:9010" # JMX Exporter + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=AERON + - PAYARA_INSTANCE_NAME=c4-instance-1 + - HAZELCAST_MEMBER_NAME=trader-stream-c4-1 + - ENABLE_PUBLISHER=true + - JVM_TYPE=azul-c4 + - JAVA_OPTS=-Xms4g -Xmx4g + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M + -XX:+AlwaysPreTouch + -XX:+UseTransparentHugePages + -javaagent:/opt/payara/jmx_prometheus_javaagent.jar=9010:/opt/payara/jmx-exporter-config.yml + -Djava.net.preferIPv4Stack=true + volumes: + - ./monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar:/opt/payara/jmx_prometheus_javaagent.jar:ro + - ./monitoring/jmx-exporter/jmx-exporter-config.yml:/opt/payara/jmx-exporter-config.yml:ro + - ./monitoring/logs/c4-1:/opt/payara/logs + labels: + - "traefik.enable=true" + - "cluster=c4" + - "traefik.http.routers.trader-stream-c4.rule=PathPrefix(`/`)" + - "traefik.http.services.trader-stream-c4.loadbalancer.server.port=8080" + - "jvm_type=azul-c4" + - "instance=c4-1" + networks: + - trader-network + - monitoring + restart: unless-stopped + + trader-stream-c4-2: + image: trader-stream-ee:c4 + container_name: trader-stream-c4-2 + hostname: trader-stream-c4-2 + ports: + - "8082:8080" + - "9011:9010" + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=AERON + - PAYARA_INSTANCE_NAME=c4-instance-2 + - HAZELCAST_MEMBER_NAME=trader-stream-c4-2 + - ENABLE_PUBLISHER=false + - JVM_TYPE=azul-c4 + - JAVA_OPTS=-Xms4g -Xmx4g + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M + -javaagent:/opt/payara/jmx_prometheus_javaagent.jar=9010:/opt/payara/jmx-exporter-config.yml + volumes: + - ./monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar:/opt/payara/jmx_prometheus_javaagent.jar:ro + - ./monitoring/jmx-exporter/jmx-exporter-config.yml:/opt/payara/jmx-exporter-config.yml:ro + - ./monitoring/logs/c4-2:/opt/payara/logs + labels: + - "traefik.enable=true" + - "cluster=c4" + - "traefik.http.routers.trader-stream-c4.rule=PathPrefix(`/`)" + - "traefik.http.services.trader-stream-c4.loadbalancer.server.port=8080" + - "jvm_type=azul-c4" + - "instance=c4-2" + networks: + - trader-network + - monitoring + restart: unless-stopped + + trader-stream-c4-3: + image: trader-stream-ee:c4 + container_name: trader-stream-c4-3 + hostname: trader-stream-c4-3 + ports: + - "8083:8080" + - "9012:9010" + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=AERON + - PAYARA_INSTANCE_NAME=c4-instance-3 + - HAZELCAST_MEMBER_NAME=trader-stream-c4-3 + - ENABLE_PUBLISHER=false + - JVM_TYPE=azul-c4 + - JAVA_OPTS=-Xms4g -Xmx4g + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M + -javaagent:/opt/payara/jmx_prometheus_javaagent.jar=9010:/opt/payara/jmx-exporter-config.yml + volumes: + - ./monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar:/opt/payara/jmx_prometheus_javaagent.jar:ro + - ./monitoring/jmx-exporter/jmx-exporter-config.yml:/opt/payara/jmx-exporter-config.yml:ro + - ./monitoring/logs/c4-3:/opt/payara/logs + labels: + - "traefik.enable=true" + - "cluster=c4" + - "traefik.http.routers.trader-stream-c4.rule=PathPrefix(`/`)" + - "traefik.http.services.trader-stream-c4.loadbalancer.server.port=8080" + - "jvm_type=azul-c4" + - "instance=c4-3" + networks: + - trader-network + - monitoring + restart: unless-stopped + +networks: + trader-network: + external: true + monitoring: + external: true diff --git a/docker-compose-g1.yml b/docker-compose-g1.yml new file mode 100644 index 0000000..74a7048 --- /dev/null +++ b/docker-compose-g1.yml @@ -0,0 +1,144 @@ +services: + traefik-g1: + image: traefik:v3.6.5 + container_name: trader-traefik-g1 + command: + - "--api.dashboard=true" + - "--api.insecure=true" + - "--providers.docker=true" + - "--providers.docker.exposedbydefault=false" + - "--providers.docker.constraints=Label(`cluster`, `g1`)" + - "--entrypoints.web.address=:80" + - "--metrics.prometheus=true" + - "--metrics.prometheus.addServicesLabels=true" + ports: + - "9080:80" + - "9084:8080" + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + networks: + - trader-network + - monitoring + labels: + - "jvm_type=eclipse-temurin-g1" + restart: unless-stopped + + trader-stream-g1-1: + image: trader-stream-ee:g1 + build: + context: . + dockerfile: Dockerfile.scale.standard + container_name: trader-stream-g1-1 + hostname: trader-stream-g1-1 + ports: + - "9081:8080" + - "9020:9010" # JMX Exporter + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=AERON + - PAYARA_INSTANCE_NAME=g1-instance-1 + - HAZELCAST_MEMBER_NAME=trader-stream-g1-1 + - ENABLE_PUBLISHER=true + - JVM_TYPE=eclipse-temurin-g1 + - JAVA_OPTS=-Xms4g -Xmx4g + -XX:+UseG1GC + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M + -XX:+AlwaysPreTouch + -XX:+UseTransparentHugePages + -javaagent:/opt/payara/jmx_prometheus_javaagent.jar=9010:/opt/payara/jmx-exporter-config.yml + -Djava.net.preferIPv4Stack=true + volumes: + - ./monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar:/opt/payara/jmx_prometheus_javaagent.jar:ro + - ./monitoring/jmx-exporter/jmx-exporter-config.yml:/opt/payara/jmx-exporter-config.yml:ro + - ./monitoring/logs/g1-1:/opt/payara/logs + labels: + - "traefik.enable=true" + - "cluster=g1" + - "traefik.http.routers.trader-stream-g1.rule=PathPrefix(`/`)" + - "traefik.http.services.trader-stream-g1.loadbalancer.server.port=8080" + - "jvm_type=eclipse-temurin-g1" + - "instance=g1-1" + networks: + - trader-network + - monitoring + restart: unless-stopped + + trader-stream-g1-2: + image: trader-stream-ee:g1 + container_name: trader-stream-g1-2 + hostname: trader-stream-g1-2 + ports: + - "9082:8080" + - "9021:9010" + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=AERON + - PAYARA_INSTANCE_NAME=g1-instance-2 + - HAZELCAST_MEMBER_NAME=trader-stream-g1-2 + - ENABLE_PUBLISHER=false + - JVM_TYPE=eclipse-temurin-g1 + - JAVA_OPTS=-Xms4g -Xmx4g + -XX:+UseG1GC + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M + -javaagent:/opt/payara/jmx_prometheus_javaagent.jar=9010:/opt/payara/jmx-exporter-config.yml + volumes: + - ./monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar:/opt/payara/jmx_prometheus_javaagent.jar:ro + - ./monitoring/jmx-exporter/jmx-exporter-config.yml:/opt/payara/jmx-exporter-config.yml:ro + - ./monitoring/logs/g1-2:/opt/payara/logs + labels: + - "traefik.enable=true" + - "cluster=g1" + - "traefik.http.routers.trader-stream-g1.rule=PathPrefix(`/`)" + - "traefik.http.services.trader-stream-g1.loadbalancer.server.port=8080" + - "jvm_type=eclipse-temurin-g1" + - "instance=g1-2" + networks: + - trader-network + - monitoring + restart: unless-stopped + + trader-stream-g1-3: + image: trader-stream-ee:g1 + container_name: trader-stream-g1-3 + hostname: trader-stream-g1-3 + ports: + - "9083:8080" + - "9022:9010" + expose: + - "5701" + shm_size: 512m + environment: + - TRADER_INGESTION_MODE=AERON + - PAYARA_INSTANCE_NAME=g1-instance-3 + - HAZELCAST_MEMBER_NAME=trader-stream-g1-3 + - ENABLE_PUBLISHER=false + - JVM_TYPE=eclipse-temurin-g1 + - JAVA_OPTS=-Xms4g -Xmx4g + -XX:+UseG1GC + -Xlog:gc*:file=/opt/payara/gc.log:time,uptime,level,tags:filecount=5,filesize=10M + -javaagent:/opt/payara/jmx_prometheus_javaagent.jar=9010:/opt/payara/jmx-exporter-config.yml + volumes: + - ./monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar:/opt/payara/jmx_prometheus_javaagent.jar:ro + - ./monitoring/jmx-exporter/jmx-exporter-config.yml:/opt/payara/jmx-exporter-config.yml:ro + - ./monitoring/logs/g1-3:/opt/payara/logs + labels: + - "traefik.enable=true" + - "cluster=g1" + - "traefik.http.routers.trader-stream-g1.rule=PathPrefix(`/`)" + - "traefik.http.services.trader-stream-g1.loadbalancer.server.port=8080" + - "jvm_type=eclipse-temurin-g1" + - "instance=g1-3" + networks: + - trader-network + - monitoring + restart: unless-stopped + +networks: + trader-network: + external: true + monitoring: + external: true diff --git a/docker-compose-monitoring.yml b/docker-compose-monitoring.yml new file mode 100644 index 0000000..bce2738 --- /dev/null +++ b/docker-compose-monitoring.yml @@ -0,0 +1,82 @@ +services: + # Prometheus - Metrics Collection + prometheus: + image: prom/prometheus:latest + container_name: trader-prometheus + command: + - '--config.file=/etc/prometheus/prometheus.yml' + - '--storage.tsdb.path=/prometheus' + - '--storage.tsdb.retention.time=30d' + - '--web.console.libraries=/usr/share/prometheus/console_libraries' + - '--web.console.templates=/usr/share/prometheus/consoles' + ports: + - "9090:9090" + volumes: + - ./monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro + - prometheus-data:/prometheus + networks: + - trader-network + - monitoring + restart: unless-stopped + + # Grafana - Visualization + grafana: + image: grafana/grafana:latest + container_name: trader-grafana + ports: + - "3000:3000" + environment: + - GF_SECURITY_ADMIN_USER=admin + - GF_SECURITY_ADMIN_PASSWORD=admin + - GF_USERS_ALLOW_SIGN_UP=false + - GF_INSTALL_PLUGINS=grafana-piechart-panel + volumes: + - grafana-data:/var/lib/grafana + - ./monitoring/grafana/provisioning:/etc/grafana/provisioning:ro + - ./monitoring/grafana/dashboards:/var/lib/grafana/dashboards:ro + networks: + - monitoring + restart: unless-stopped + depends_on: + - prometheus + - loki + + # Loki - Log Aggregation + loki: + image: grafana/loki:latest + container_name: trader-loki + ports: + - "3100:3100" + command: -config.file=/etc/loki/local-config.yaml + volumes: + - ./monitoring/loki/loki-config.yml:/etc/loki/local-config.yaml:ro + - loki-data:/loki + networks: + - monitoring + restart: unless-stopped + + # Promtail - Log Shipper (for Docker logs) + promtail: + image: grafana/promtail:latest + container_name: trader-promtail + command: -config.file=/etc/promtail/config.yml + volumes: + - ./monitoring/promtail/promtail-config.yml:/etc/promtail/config.yml:ro + - /var/lib/docker/containers:/var/lib/docker/containers:ro + - /var/run/docker.sock:/var/run/docker.sock + networks: + - monitoring + restart: unless-stopped + depends_on: + - loki + +volumes: + prometheus-data: + grafana-data: + loki-data: + +networks: + trader-network: + external: true + monitoring: + driver: bridge diff --git a/monitoring/grafana/dashboards/jvm-comparison.json b/monitoring/grafana/dashboards/jvm-comparison.json new file mode 100644 index 0000000..3c85d9b --- /dev/null +++ b/monitoring/grafana/dashboards/jvm-comparison.json @@ -0,0 +1,155 @@ +{ + "dashboard": { + "title": "JVM Performance Comparison - C4 vs G1GC", + "tags": ["jvm", "gc", "performance", "comparison"], + "timezone": "browser", + "editable": true, + "panels": [ + { + "id": 1, + "title": "GC Pause Time Comparison (P99)", + "type": "timeseries", + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 0}, + "targets": [ + { + "expr": "histogram_quantile(0.99, rate(jvm_gc_collection_time_ms[5m])) by (jvm_type, gc)", + "legendFormat": "{{jvm_type}} - {{gc}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "ms", + "custom": { + "axisLabel": "Pause Time (ms)" + } + }, + "overrides": [ + { + "matcher": {"id": "byRegexp", "options": ".*azul-c4.*"}, + "properties": [ + {"id": "color", "value": {"mode": "fixed", "fixedColor": "green"}} + ] + }, + { + "matcher": {"id": "byRegexp", "options": ".*g1.*"}, + "properties": [ + {"id": "color", "value": {"mode": "fixed", "fixedColor": "red"}} + ] + } + ] + } + }, + { + "id": 2, + "title": "GC Collection Count Rate", + "type": "timeseries", + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 0}, + "targets": [ + { + "expr": "rate(jvm_gc_collection_count[5m]) by (jvm_type, gc)", + "legendFormat": "{{jvm_type}} - {{gc}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "ops", + "custom": { + "axisLabel": "Collections/sec" + } + } + } + }, + { + "id": 3, + "title": "Heap Memory Usage", + "type": "timeseries", + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 8}, + "targets": [ + { + "expr": "jvm_memory_heap_used by (jvm_type, instance) / jvm_memory_heap_max * 100", + "legendFormat": "{{jvm_type}} - {{instance}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "percent", + "custom": { + "axisLabel": "Heap Usage %" + } + } + } + }, + { + "id": 4, + "title": "Thread Count", + "type": "timeseries", + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 8}, + "targets": [ + { + "expr": "jvm_threads_current by (jvm_type, instance)", + "legendFormat": "{{jvm_type}} - {{instance}}", + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "unit": "short", + "custom": { + "axisLabel": "Thread Count" + } + } + } + }, + { + "id": 5, + "title": "GC Pause Time Distribution (Heatmap)", + "type": "heatmap", + "gridPos": {"h": 8, "w": 24, "x": 0, "y": 16}, + "targets": [ + { + "expr": "rate(jvm_gc_collection_time_ms[1m]) by (jvm_type, le)", + "legendFormat": "{{jvm_type}}", + "refId": "A", + "format": "heatmap" + } + ] + }, + { + "id": 6, + "title": "Performance Summary", + "type": "stat", + "gridPos": {"h": 8, "w": 24, "x": 0, "y": 24}, + "targets": [ + { + "expr": "histogram_quantile(0.99, rate(jvm_gc_collection_time_ms[5m])) by (jvm_type)", + "legendFormat": "{{jvm_type}} P99", + "refId": "A" + }, + { + "expr": "max(jvm_gc_collection_time_ms) by (jvm_type)", + "legendFormat": "{{jvm_type}} Max", + "refId": "B" + } + ], + "fieldConfig": { + "defaults": { + "unit": "ms", + "thresholds": { + "mode": "absolute", + "steps": [ + {"value": 0, "color": "green"}, + {"value": 10, "color": "yellow"}, + {"value": 50, "color": "red"} + ] + } + } + } + } + ], + "refresh": "5s", + "time": {"from": "now-30m", "to": "now"} + } +} diff --git a/monitoring/grafana/provisioning/dashboards/dashboards.yml b/monitoring/grafana/provisioning/dashboards/dashboards.yml new file mode 100644 index 0000000..8984841 --- /dev/null +++ b/monitoring/grafana/provisioning/dashboards/dashboards.yml @@ -0,0 +1,12 @@ +apiVersion: 1 + +providers: + - name: 'JVM Comparison' + orgId: 1 + folder: 'Trader Stream' + type: file + disableDeletion: false + updateIntervalSeconds: 10 + allowUiUpdates: true + options: + path: /var/lib/grafana/dashboards diff --git a/monitoring/grafana/provisioning/datasources/datasources.yml b/monitoring/grafana/provisioning/datasources/datasources.yml new file mode 100644 index 0000000..3958205 --- /dev/null +++ b/monitoring/grafana/provisioning/datasources/datasources.yml @@ -0,0 +1,19 @@ +apiVersion: 1 + +datasources: + - name: Prometheus + type: prometheus + access: proxy + url: http://trader-prometheus:9090 + isDefault: true + editable: true + jsonData: + timeInterval: 15s + + - name: Loki + type: loki + access: proxy + url: http://trader-loki:3100 + editable: true + jsonData: + maxLines: 1000 diff --git a/monitoring/jmx-exporter/jmx-exporter-config.yml b/monitoring/jmx-exporter/jmx-exporter-config.yml new file mode 100644 index 0000000..a4b8208 --- /dev/null +++ b/monitoring/jmx-exporter/jmx-exporter-config.yml @@ -0,0 +1,57 @@ +# JMX Exporter Configuration for GC Metrics +--- +startDelaySeconds: 0 +lowercaseOutputName: false +lowercaseOutputLabelNames: false + +rules: + # GC Metrics + - pattern: 'java.lang<>CollectionCount' + name: jvm_gc_collection_count + labels: + gc: "$1" + jvm_type: "$2" + type: COUNTER + + - pattern: 'java.lang<>CollectionTime' + name: jvm_gc_collection_time_ms + labels: + gc: "$1" + type: COUNTER + + - pattern: 'java.lang<>LastGcInfo' + name: jvm_gc_last_info + labels: + gc: "$1" + type: GAUGE + + # Memory Metrics + - pattern: 'java.lang(.+)' + name: jvm_memory_heap_$1 + type: GAUGE + + - pattern: 'java.lang(.+)' + name: jvm_memory_nonheap_$1 + type: GAUGE + + - pattern: 'java.lang(.+)' + name: jvm_memory_pool_$2 + labels: + pool: "$1" + type: GAUGE + + # Thread Metrics + - pattern: 'java.lang<>ThreadCount' + name: jvm_threads_current + type: GAUGE + + - pattern: 'java.lang<>PeakThreadCount' + name: jvm_threads_peak + type: GAUGE + + # Application-specific metrics (if using MicroProfile Metrics) + - pattern: 'fish.payara.trader<>(.+)' + name: trader_$1_$3 + labels: + metric: "$2" + type: GAUGE diff --git a/monitoring/loki/loki-config.yml b/monitoring/loki/loki-config.yml new file mode 100644 index 0000000..0e4527e --- /dev/null +++ b/monitoring/loki/loki-config.yml @@ -0,0 +1,37 @@ +auth_enabled: false + +server: + http_listen_port: 3100 + grpc_listen_port: 9096 + +common: + path_prefix: /loki + storage: + filesystem: + chunks_directory: /loki/chunks + rules_directory: /loki/rules + replication_factor: 1 + ring: + instance_addr: 127.0.0.1 + kvstore: + store: inmemory + +schema_config: + configs: + - from: 2024-01-01 + store: boltdb-shipper + object_store: filesystem + schema: v11 + index: + prefix: index_ + period: 24h + +ruler: + alertmanager_url: http://localhost:9093 + +limits_config: + allow_structured_metadata: false + reject_old_samples: true + reject_old_samples_max_age: 168h + ingestion_rate_mb: 10 + ingestion_burst_size_mb: 20 diff --git a/monitoring/prometheus/prometheus.yml b/monitoring/prometheus/prometheus.yml new file mode 100644 index 0000000..842589a --- /dev/null +++ b/monitoring/prometheus/prometheus.yml @@ -0,0 +1,57 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s + external_labels: + cluster: 'trader-stream-comparison' + +scrape_configs: + # Azul C4 Instances - JMX Metrics + - job_name: 'trader-c4-jmx' + static_configs: + - targets: + - 'trader-stream-c4-1:9010' + - 'trader-stream-c4-2:9010' + - 'trader-stream-c4-3:9010' + labels: + jvm_type: 'azul-c4' + cluster: 'c4-cluster' + metric_relabel_configs: + - source_labels: [__name__] + regex: 'jvm_.*' + action: keep + + # Eclipse Temurin G1GC Instances - JMX Metrics + - job_name: 'trader-g1-jmx' + static_configs: + - targets: + - 'trader-stream-g1-1:9010' + - 'trader-stream-g1-2:9010' + - 'trader-stream-g1-3:9010' + labels: + jvm_type: 'eclipse-temurin-g1' + cluster: 'g1-cluster' + metric_relabel_configs: + - source_labels: [__name__] + regex: 'jvm_.*' + action: keep + + # Traefik Metrics (Load Balancer) + - job_name: 'traefik-c4' + static_configs: + - targets: + - 'trader-traefik-c4:8080' + labels: + jvm_type: 'azul-c4' + + - job_name: 'traefik-g1' + static_configs: + - targets: + - 'trader-traefik-g1:8080' + labels: + jvm_type: 'eclipse-temurin-g1' + + # Prometheus Self-Monitoring + - job_name: 'prometheus' + static_configs: + - targets: + - 'localhost:9090' diff --git a/monitoring/promtail/promtail-config.yml b/monitoring/promtail/promtail-config.yml new file mode 100644 index 0000000..0596e0a --- /dev/null +++ b/monitoring/promtail/promtail-config.yml @@ -0,0 +1,67 @@ +server: + http_listen_port: 9080 + grpc_listen_port: 0 + +positions: + filename: /tmp/positions.yaml + +clients: + - url: http://trader-loki:3100/loki/api/v1/push + +scrape_configs: + # Docker container logs for C4 instances + - job_name: docker-c4 + docker_sd_configs: + - host: unix:///var/run/docker.sock + refresh_interval: 5s + filters: + - name: label + values: ["jvm_type=azul-c4"] + relabel_configs: + - source_labels: ['__meta_docker_container_name'] + regex: '/(.*)' + target_label: 'container' + - source_labels: ['__meta_docker_container_log_stream'] + target_label: 'stream' + - source_labels: ['__meta_docker_container_label_jvm_type'] + target_label: 'jvm_type' + - source_labels: ['__meta_docker_container_label_instance'] + target_label: 'instance' + + # Docker container logs for G1 instances + - job_name: docker-g1 + docker_sd_configs: + - host: unix:///var/run/docker.sock + refresh_interval: 5s + filters: + - name: label + values: ["jvm_type=eclipse-temurin-g1"] + relabel_configs: + - source_labels: ['__meta_docker_container_name'] + regex: '/(.*)' + target_label: 'container' + - source_labels: ['__meta_docker_container_log_stream'] + target_label: 'stream' + - source_labels: ['__meta_docker_container_label_jvm_type'] + target_label: 'jvm_type' + - source_labels: ['__meta_docker_container_label_instance'] + target_label: 'instance' + + # GC log files (if writing to files) + - job_name: gc-logs-c4 + static_configs: + - targets: + - localhost + labels: + job: gc-logs + jvm_type: azul-c4 + __path__: /var/log/trader/c4-*/gc.log + + - job_name: gc-logs-g1 + static_configs: + - targets: + - localhost + labels: + job: gc-logs + jvm_type: eclipse-temurin-g1 + __path__: /var/log/trader/g1-*/gc.log diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index 41c385b..972603a 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -112,6 +112,17 @@ void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object e } public void init() { + // Initialize cluster-wide message counter on ALL instances (even non-publishers) + // This allows all instances to read the shared counter value via Hazelcast CP Subsystem + if (hazelcastInstance != null) { + try { + clusterMessageCounter = hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count"); + LOGGER.info("Initialized cluster-wide message counter"); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to initialize cluster message counter, using local counter only", e); + } + } + // Check if publisher should be enabled on this instance if (enablePublisherEnv != null && !"true".equalsIgnoreCase(enablePublisherEnv)) { LOGGER.info("Market Data Publisher DISABLED on this instance (ENABLE_PUBLISHER=" + enablePublisherEnv + ")"); @@ -126,16 +137,6 @@ public void init() { initialized = true; isDirectMode = true; - // Initialize cluster-wide message counter - if (hazelcastInstance != null) { - try { - clusterMessageCounter = hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count"); - LOGGER.info("Initialized cluster-wide message counter (DIRECT mode)"); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to initialize cluster message counter, using local counter only", e); - } - } - startPublishing(); return; } @@ -178,16 +179,6 @@ public void init() { LOGGER.info("Market Data Publisher initialized successfully"); initialized = true; - // Initialize cluster-wide message counter - if (hazelcastInstance != null) { - try { - clusterMessageCounter = hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count"); - LOGGER.info("Initialized cluster-wide message counter"); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to initialize cluster message counter, using local counter only", e); - } - } - startPublishing(); } else { LOGGER.warning("Publisher not connected after waiting"); @@ -204,14 +195,26 @@ public void init() { private void startPublishing() { running = true; publisherFuture = managedExecutorService.submit(() -> { - LOGGER.info("Market data publisher task started - targeting 50k-100k messages/sec"); + LOGGER.info("Market data publisher task started - targeting 50k-100k messages/sec with burst patterns"); - final int BURST_SIZE = 500; - final long PARK_NANOS = 5_000; // 5 microseconds = ~100k messages/sec + final int BASE_BURST_SIZE = 500; + final long PARK_NANOS = 5_000; // 5 microseconds base rate while (running && !Thread.currentThread().isInterrupted()) { try { - for (int i = 0; i < BURST_SIZE && running; i++) { + // Apply burst multiplier for realistic market spikes + int burstMultiplier = getBurstMultiplier(); + int adjustedBurstSize = BASE_BURST_SIZE * burstMultiplier; + + // Log burst transitions + if (burstMultiplier > 1) { + long secondOfMinute = (System.currentTimeMillis() / 1000) % 60; + if (secondOfMinute == 20 || secondOfMinute == 45) { + LOGGER.info("BURST MODE: " + burstMultiplier + "x allocation spike started"); + } + } + + for (int i = 0; i < adjustedBurstSize && running; i++) { publishTrade(); publishQuote(); publishMarketDepth(); @@ -247,14 +250,19 @@ private void startPublishing() { double elapsedSeconds = (currentTime - lastTime) / 1000.0; double messagesPerSecond = messagesSinceLastLog / elapsedSeconds; + int currentMultiplier = getBurstMultiplier(); + String burstStatus = currentMultiplier > 1 ? + " [BURST: " + currentMultiplier + "x]" : ""; + LOGGER.info(String.format( - "Publisher Stats - Total: %,d | Last 5s: %,d (%.0f msg/sec)", - currentCount, - messagesSinceLastLog, - messagesPerSecond + "Publisher Stats - Total: %,d | Last 5s: %,d (%.0f msg/sec)%s", + currentCount, messagesSinceLastLog, messagesPerSecond, burstStatus )); - String statsJson = String.format("{\"type\":\"stats\",\"total\":%d,\"rate\":%.0f}", currentCount, messagesPerSecond); + String statsJson = String.format( + "{\"type\":\"stats\",\"total\":%d,\"rate\":%.0f,\"burstMultiplier\":%d}", + currentCount, messagesPerSecond, currentMultiplier + ); broadcaster.broadcast(statsJson); lastCount = currentCount; @@ -267,6 +275,28 @@ private void startPublishing() { }); } + /** + * Calculate burst multiplier based on time pattern simulating market events + * + * Pattern (60-second cycle): + * - 00-20s: Normal trading (1x) + * - 20-25s: News event burst (5x allocation spike) + * - 25-45s: Normal trading (1x) + * - 45-50s: Market close spike (3x allocation) + * - 50-60s: Normal trading (1x) + */ + private int getBurstMultiplier() { + long secondOfMinute = (System.currentTimeMillis() / 1000) % 60; + + if (secondOfMinute >= 20 && secondOfMinute < 25) { + return 5; // News event - 5x burst + } else if (secondOfMinute >= 45 && secondOfMinute < 50) { + return 3; // Market close - 3x spike + } + + return 1; // Normal + } + /** * Publish a Trade message */ diff --git a/src/main/java/fish/payara/trader/gc/GCStatsService.java b/src/main/java/fish/payara/trader/gc/GCStatsService.java index 52c80c8..85315ae 100644 --- a/src/main/java/fish/payara/trader/gc/GCStatsService.java +++ b/src/main/java/fish/payara/trader/gc/GCStatsService.java @@ -1,7 +1,12 @@ package fish.payara.trader.gc; +import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; +import javax.management.openmbean.CompositeData; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; @@ -11,15 +16,69 @@ import java.util.logging.Logger; import java.util.stream.Collectors; +import com.sun.management.GarbageCollectionNotificationInfo; + @ApplicationScoped -public class GCStatsService { +public class GCStatsService implements NotificationListener { private static final Logger LOGGER = Logger.getLogger(GCStatsService.class.getName()); private static final int MAX_PAUSE_HISTORY = 1000; private final Map> pauseHistory = new HashMap<>(); - private final Map lastCollectionCount = new HashMap<>(); - private final Map lastCollectionTime = new HashMap<>(); + + @PostConstruct + public void init() { + LOGGER.info("Initializing GC Notification Listener..."); + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + for (GarbageCollectorMXBean gcBean : gcBeans) { + LOGGER.info("Registering listener for GC Bean: " + gcBean.getName()); + if (gcBean instanceof NotificationEmitter) { + ((NotificationEmitter) gcBean).addNotificationListener(this, null, null); + } + } + } + + @Override + public void handleNotification(Notification notification, Object handback) { + if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { + GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()); + + String gcName = info.getGcName(); + String gcAction = info.getGcAction(); + String gcCause = info.getGcCause(); + long duration = info.getGcInfo().getDuration(); + + // FILTERING LOGIC: + // Azul C4 exposes "GPGC" (Concurrent Cycle) and "GPGC Pauses" (STW Pauses). + // We MUST ignore "GPGC" because it reports cycle time (hundreds of ms) which is NOT a pause. + if ("GPGC".equals(gcName)) { + return; + } + + // Also ignore other known concurrent cycle beans if they appear + if (gcName.contains("Cycles") && !gcName.contains("Pauses")) { + return; + } + + // Only record if duration > 0 (sub-millisecond pauses might show as 0 or 1) + // Storing all for fidelity. + + ConcurrentLinkedDeque history = pauseHistory.computeIfAbsent( + gcName, k -> new ConcurrentLinkedDeque<>() + ); + + history.addLast(duration); + while (history.size() > MAX_PAUSE_HISTORY) { + history.removeFirst(); + } + + // Log significant pauses (> 10ms) + if (duration > 10) { + LOGGER.info(String.format("GC Pause detected: %s | Action: %s | Cause: %s | Duration: %d ms", + gcName, gcAction, gcCause, duration)); + } + } + } public List collectGCStats() { List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); @@ -30,59 +89,35 @@ public List collectGCStats() { for (GarbageCollectorMXBean gcBean : gcBeans) { String gcName = gcBean.getName(); - long currentCount = gcBean.getCollectionCount(); - long currentTime = gcBean.getCollectionTime(); - + + // Exclude concurrent cycle collectors from the public stats to avoid confusion + if ("GPGC".equals(gcName) || (gcName.contains("Cycles") && !gcName.contains("Pauses"))) { + continue; + } + GCStats stats = new GCStats(); stats.setGcName(gcName); - stats.setCollectionCount(currentCount); - stats.setCollectionTime(currentTime); - - // Calculate pause duration since last check - Long prevCount = lastCollectionCount.get(gcName); - Long prevTime = lastCollectionTime.get(gcName); - - if (prevCount != null && prevTime != null) { - long countDelta = currentCount - prevCount; - long timeDelta = currentTime - prevTime; - - if (countDelta > 0) { - long avgPauseDuration = timeDelta / countDelta; - stats.setLastPauseDuration(avgPauseDuration); - - // Track pause history - ConcurrentLinkedDeque history = pauseHistory.computeIfAbsent( - gcName, k -> new ConcurrentLinkedDeque<>() - ); - - // Add new pauses (simplified - one entry per collection) - for (int i = 0; i < countDelta; i++) { - history.addLast(avgPauseDuration); - if (history.size() > MAX_PAUSE_HISTORY) { - history.removeFirst(); - } - } - } - } - - // Update tracking - lastCollectionCount.put(gcName, currentCount); - lastCollectionTime.put(gcName, currentTime); + stats.setCollectionCount(gcBean.getCollectionCount()); + stats.setCollectionTime(gcBean.getCollectionTime()); - // Get recent pauses + // Get recent pauses from accurate history ConcurrentLinkedDeque history = pauseHistory.get(gcName); if (history != null && !history.isEmpty()) { - stats.setRecentPauses(new ArrayList<>(history).subList( - Math.max(0, history.size() - 100), history.size() + List pauses = new ArrayList<>(history); + stats.setLastPauseDuration(pauses.get(pauses.size() - 1)); + + stats.setRecentPauses(pauses.subList( + Math.max(0, pauses.size() - 100), pauses.size() )); // Calculate percentiles - List sortedPauses = history.stream() + List sortedPauses = pauses.stream() .sorted() .collect(Collectors.toList()); stats.setPercentiles(calculatePercentiles(sortedPauses)); } else { + stats.setLastPauseDuration(0); stats.setRecentPauses(Collections.emptyList()); stats.setPercentiles(new GCStats.PausePercentiles(0, 0, 0, 0, 0)); } @@ -121,8 +156,6 @@ private long percentile(List sortedValues, double percentile) { public void resetStats() { pauseHistory.clear(); - lastCollectionCount.clear(); - lastCollectionTime.clear(); LOGGER.info("GC statistics reset"); } } diff --git a/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java b/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java new file mode 100644 index 0000000..5f0b0a5 --- /dev/null +++ b/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java @@ -0,0 +1,236 @@ +package fish.payara.trader.monitoring; + +import com.sun.management.GarbageCollectionNotificationInfo; +import com.sun.management.GcInfo; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; + +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; +import javax.management.openmbean.CompositeData; +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.atomic.AtomicLong; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +/** + * Real-time GC pause monitoring using JMX notifications. + * Captures individual GC events with exact pause times (not averaged). + */ +@ApplicationScoped +public class GCPauseMonitor implements NotificationListener { + + private static final Logger LOGGER = Logger.getLogger(GCPauseMonitor.class.getName()); + + // Keep last N pauses for percentile calculations (reactive window) + private static final int MAX_PAUSE_HISTORY = 500; + + // Pause history (milliseconds) + private final ConcurrentLinkedDeque pauseHistory = new ConcurrentLinkedDeque<>(); + + // All-time statistics + private final AtomicLong totalPauseCount = new AtomicLong(0); + private final AtomicLong totalPauseTimeMs = new AtomicLong(0); + private volatile long maxPauseMs = 0; + + // SLA violation counters (all-time) + private final AtomicLong violationsOver10ms = new AtomicLong(0); + private final AtomicLong violationsOver50ms = new AtomicLong(0); + private final AtomicLong violationsOver100ms = new AtomicLong(0); + + private final List emitters = new ArrayList<>(); + + @PostConstruct + public void init() { + LOGGER.info("Initializing GC Pause Monitor with JMX notifications"); + + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + + for (GarbageCollectorMXBean gcBean : gcBeans) { + if (gcBean instanceof NotificationEmitter) { + NotificationEmitter emitter = (NotificationEmitter) gcBean; + emitter.addNotificationListener(this, null, null); + emitters.add(emitter); + LOGGER.info("Registered GC notification listener for: " + gcBean.getName()); + } + } + + if (emitters.isEmpty()) { + LOGGER.warning("No GC notification emitters found - pause monitoring may be limited"); + } + } + + @PreDestroy + public void cleanup() { + for (NotificationEmitter emitter : emitters) { + try { + emitter.removeNotificationListener(this); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to remove GC notification listener", e); + } + } + emitters.clear(); + } + + @Override + public void handleNotification(Notification notification, Object handback) { + if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { + return; + } + + try { + CompositeData cd = (CompositeData) notification.getUserData(); + GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd); + String gcName = info.getGcName(); + + // FILTERING LOGIC: + // Azul C4 exposes "GPGC" (Concurrent Cycle) and "GPGC Pauses" (STW Pauses). + // We MUST ignore "GPGC" because it reports cycle time (hundreds of ms) which is NOT a pause. + if ("GPGC".equals(gcName) || (gcName.contains("Cycles") && !gcName.contains("Pauses"))) { + return; + } + + GcInfo gcInfo = info.getGcInfo(); + long pauseMs = gcInfo.getDuration(); + + // Record pause + recordPause(pauseMs, gcName, info.getGcAction()); + + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Error processing GC notification", e); + } + } + + private void recordPause(long pauseMs, String gcName, String gcAction) { + // Add to history + pauseHistory.addLast(pauseMs); + if (pauseHistory.size() > MAX_PAUSE_HISTORY) { + pauseHistory.removeFirst(); + } + + // Update statistics + totalPauseCount.incrementAndGet(); + totalPauseTimeMs.addAndGet(pauseMs); + + // Update max (thread-safe but may miss true max in race condition - acceptable for monitoring) + if (pauseMs > maxPauseMs) { + synchronized (this) { + if (pauseMs > maxPauseMs) { + maxPauseMs = pauseMs; + } + } + } + + // Track SLA violations + if (pauseMs > 100) { + violationsOver100ms.incrementAndGet(); + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + } else if (pauseMs > 50) { + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + } else if (pauseMs > 10) { + violationsOver10ms.incrementAndGet(); + } + + // Log significant pauses + if (pauseMs > 100) { + LOGGER.warning(String.format("Large GC pause detected: %d ms [%s - %s]", pauseMs, gcName, gcAction)); + } else if (pauseMs > 50) { + LOGGER.info(String.format("Notable GC pause: %d ms [%s - %s]", pauseMs, gcName, gcAction)); + } + } + + public GCPauseStats getStats() { + List pauses = new ArrayList<>(pauseHistory); + + if (pauses.isEmpty()) { + return new GCPauseStats(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + } + + Collections.sort(pauses); + + long p50 = percentile(pauses, 0.50); + long p95 = percentile(pauses, 0.95); + long p99 = percentile(pauses, 0.99); + long p999 = percentile(pauses, 0.999); + long max = pauses.get(pauses.size() - 1); + + long count = totalPauseCount.get(); + long totalTime = totalPauseTimeMs.get(); + double avgPause = count > 0 ? (double) totalTime / count : 0; + + return new GCPauseStats( + count, + totalTime, + avgPause, + p50, + p95, + p99, + p999, + maxPauseMs, // All-time max + violationsOver10ms.get(), + violationsOver50ms.get(), + violationsOver100ms.get(), + pauses.size() // Sample size for percentiles + ); + } + + private long percentile(List sortedValues, double percentile) { + int index = (int) Math.ceil(percentile * sortedValues.size()) - 1; + index = Math.max(0, Math.min(index, sortedValues.size() - 1)); + return sortedValues.get(index); + } + + public void reset() { + pauseHistory.clear(); + totalPauseCount.set(0); + totalPauseTimeMs.set(0); + maxPauseMs = 0; + violationsOver10ms.set(0); + violationsOver50ms.set(0); + violationsOver100ms.set(0); + LOGGER.info("GC pause statistics reset"); + } + + public static class GCPauseStats { + public final long totalPauseCount; + public final long totalPauseTimeMs; + public final double avgPauseMs; + public final long p50Ms; + public final long p95Ms; + public final long p99Ms; + public final long p999Ms; + public final long maxMs; // All-time max since startup/reset + public final long violationsOver10ms; + public final long violationsOver50ms; + public final long violationsOver100ms; + public final int sampleSize; + + public GCPauseStats(long totalPauseCount, long totalPauseTimeMs, double avgPauseMs, + long p50Ms, long p95Ms, long p99Ms, long p999Ms, long maxMs, + long violationsOver10ms, long violationsOver50ms, long violationsOver100ms, + int sampleSize) { + this.totalPauseCount = totalPauseCount; + this.totalPauseTimeMs = totalPauseTimeMs; + this.avgPauseMs = avgPauseMs; + this.p50Ms = p50Ms; + this.p95Ms = p95Ms; + this.p99Ms = p99Ms; + this.p999Ms = p999Ms; + this.maxMs = maxMs; + this.violationsOver10ms = violationsOver10ms; + this.violationsOver50ms = violationsOver50ms; + this.violationsOver100ms = violationsOver100ms; + this.sampleSize = sampleSize; + } + } +} diff --git a/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java b/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java new file mode 100644 index 0000000..51f06f3 --- /dev/null +++ b/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java @@ -0,0 +1,101 @@ +package fish.payara.trader.monitoring; + +import jakarta.enterprise.context.ApplicationScoped; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.logging.Logger; + +@ApplicationScoped +public class SLAMonitorService { + + private static final Logger LOGGER = Logger.getLogger(SLAMonitorService.class.getName()); + + // SLA thresholds + private static final long SLA_10MS = 10; + private static final long SLA_50MS = 50; + private static final long SLA_100MS = 100; + + // Violation counters + private final AtomicLong violationsOver10ms = new AtomicLong(0); + private final AtomicLong violationsOver50ms = new AtomicLong(0); + private final AtomicLong violationsOver100ms = new AtomicLong(0); + private final AtomicLong totalOperations = new AtomicLong(0); + + // Rolling window (last 5 minutes) + private final ConcurrentHashMap violationsByMinute = new ConcurrentHashMap<>(); + + /** + * Record an operation latency and check for SLA violations + */ + public void recordOperation(long latencyMs) { + totalOperations.incrementAndGet(); + + if (latencyMs > SLA_100MS) { + violationsOver100ms.incrementAndGet(); + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + recordViolation(); + } else if (latencyMs > SLA_50MS) { + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + recordViolation(); + } else if (latencyMs > SLA_10MS) { + violationsOver10ms.incrementAndGet(); + recordViolation(); + } + } + + private void recordViolation() { + long currentMinute = System.currentTimeMillis() / 60000; + violationsByMinute.merge(currentMinute, 1L, Long::sum); + + // Clean up old entries (> 5 minutes) + long fiveMinutesAgo = currentMinute - 5; + violationsByMinute.keySet().removeIf(minute -> minute < fiveMinutesAgo); + } + + /** + * Get SLA compliance statistics + */ + public SLAStats getStats() { + long total = totalOperations.get(); + + return new SLAStats( + total, + violationsOver10ms.get(), + violationsOver50ms.get(), + violationsOver100ms.get(), + total > 0 ? (double) violationsOver10ms.get() / total * 100 : 0, + violationsByMinute.values().stream().mapToLong(Long::longValue).sum() + ); + } + + public void reset() { + violationsOver10ms.set(0); + violationsOver50ms.set(0); + violationsOver100ms.set(0); + totalOperations.set(0); + violationsByMinute.clear(); + LOGGER.info("SLA statistics reset"); + } + + public static class SLAStats { + public final long totalOperations; + public final long violationsOver10ms; + public final long violationsOver50ms; + public final long violationsOver100ms; + public final double violationRate; + public final long recentViolations; // Last 5 minutes + + public SLAStats(long totalOperations, long violationsOver10ms, + long violationsOver50ms, long violationsOver100ms, + double violationRate, long recentViolations) { + this.totalOperations = totalOperations; + this.violationsOver10ms = violationsOver10ms; + this.violationsOver50ms = violationsOver50ms; + this.violationsOver100ms = violationsOver100ms; + this.violationRate = violationRate; + this.recentViolations = recentViolations; + } + } +} diff --git a/src/main/java/fish/payara/trader/pressure/AllocationMode.java b/src/main/java/fish/payara/trader/pressure/AllocationMode.java index 0f73137..446c57d 100644 --- a/src/main/java/fish/payara/trader/pressure/AllocationMode.java +++ b/src/main/java/fish/payara/trader/pressure/AllocationMode.java @@ -2,10 +2,10 @@ public enum AllocationMode { OFF(0, 0, "No additional allocation"), - LOW(10, 1024, "10 KB/iteration - Light pressure"), - MEDIUM(50, 1024, "50 KB/iteration - Moderate pressure"), - HIGH(200, 1024, "200 KB/iteration - Heavy pressure"), - EXTREME(1000, 1024, "1 MB/iteration - Extreme pressure"); + LOW(10, 10240, "1 MB/sec - Light pressure"), + MEDIUM(100, 10240, "10 MB/sec - Moderate pressure"), + HIGH(5000, 10240, "500 MB/sec - Heavy pressure"), + EXTREME(20000, 10240, "2 GB/sec - Extreme pressure"); private final int allocationsPerIteration; private final int bytesPerAllocation; diff --git a/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java index a131352..2181960 100644 --- a/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java +++ b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java @@ -9,8 +9,10 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Future; import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Logger; /** @@ -29,6 +31,11 @@ public class MemoryPressureService { private long totalBytesAllocated = 0; private long lastStatsTime = System.currentTimeMillis(); + // Long-lived objects that survive to tenured/old generation + private final List tenuredObjects = new CopyOnWriteArrayList<>(); + private static final int TENURED_TARGET_MB = 1024; // Target 1GB in old gen + private final AtomicLong tenuredBytesAllocated = new AtomicLong(0); + @Inject @VirtualThreadExecutor private ManagedExecutorService executorService; @@ -111,25 +118,52 @@ private void generateGarbage(AllocationMode mode) { switch (pattern) { case 0: - // String allocations (most common in real apps) generateStringGarbage(bytesPerAlloc); break; case 1: - // Byte array allocations generateByteArrayGarbage(bytesPerAlloc); break; case 2: - // Object allocations - generateObjectGarbage(bytesPerAlloc / 64); // ~64 bytes per object + generateObjectGarbage(bytesPerAlloc / 64); break; case 3: - // Collection allocations - generateCollectionGarbage(bytesPerAlloc / 100); // ~100 bytes per list item + generateCollectionGarbage(bytesPerAlloc / 100); break; } + // NEW: Create long-lived objects inside the loop for higher impact + if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { + // 0.1% chance in EXTREME (20 objects/iteration = 200MB/s) + // 0.02% chance in HIGH (1 object/iteration = 10MB/s) + int chance = (mode == AllocationMode.EXTREME) ? 10 : 2; + if (ThreadLocalRandom.current().nextInt(10000) < chance) { + byte[] longLived = new byte[1024 * 1024]; // 1MB object + ThreadLocalRandom.current().nextBytes(longLived); // Prevent optimization + tenuredObjects.add(longLived); + tenuredBytesAllocated.addAndGet(1024 * 1024); + } + } + totalBytesAllocated += bytesPerAlloc; } + + // Maintain target size - remove oldest when limit reached + if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { + while (tenuredBytesAllocated.get() > TENURED_TARGET_MB * 1024L * 1024L) { + if (!tenuredObjects.isEmpty()) { + tenuredObjects.remove(0); + tenuredBytesAllocated.addAndGet(-1024 * 1024); + } else { + break; + } + } + } else if (mode == AllocationMode.OFF || mode == AllocationMode.LOW) { + // Clear tenured objects when stress is reduced + if (!tenuredObjects.isEmpty()) { + tenuredObjects.clear(); + tenuredBytesAllocated.set(0); + } + } } private void generateStringGarbage(int bytes) { @@ -172,14 +206,22 @@ private void logStats() { double mbPerSec = (totalBytesAllocated / (1024.0 * 1024.0)) / elapsedSeconds; LOGGER.info(String.format( - "Memory Pressure Stats - Mode: %s | Allocated: %.2f MB | Rate: %.2f MB/sec", - currentMode, totalBytesAllocated / (1024.0 * 1024.0), mbPerSec + "Memory Pressure Stats - Mode: %s | Allocated: %.2f MB/sec | Tenured: %d MB (%d objects)", + currentMode, mbPerSec, getTenuredObjectsMB(), getTenuredObjectCount() )); totalBytesAllocated = 0; lastStatsTime = now; } } + + public long getTenuredObjectsMB() { + return tenuredBytesAllocated.get() / (1024 * 1024); + } + + public int getTenuredObjectCount() { + return tenuredObjects.size(); + } @PreDestroy public void shutdown() { diff --git a/src/main/java/fish/payara/trader/rest/GCStatsResource.java b/src/main/java/fish/payara/trader/rest/GCStatsResource.java index edd07b6..6761627 100644 --- a/src/main/java/fish/payara/trader/rest/GCStatsResource.java +++ b/src/main/java/fish/payara/trader/rest/GCStatsResource.java @@ -1,14 +1,21 @@ package fish.payara.trader.rest; +import fish.payara.trader.aeron.MarketDataPublisher; import fish.payara.trader.gc.GCStats; import fish.payara.trader.gc.GCStatsService; +import fish.payara.trader.pressure.MemoryPressureService; import jakarta.inject.Inject; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * REST endpoint for GC statistics monitoring @@ -21,6 +28,103 @@ public class GCStatsResource { @Inject private GCStatsService gcStatsService; + @Inject + private MemoryPressureService memoryPressureService; + + @Inject + private MarketDataPublisher publisher; + + @Inject + private fish.payara.trader.monitoring.SLAMonitorService slaMonitor; + + @Inject + private fish.payara.trader.monitoring.GCPauseMonitor gcPauseMonitor; + + @GET + @Path("/sla") + @Produces(MediaType.APPLICATION_JSON) + public Response getSLAStats() { + return Response.ok(slaMonitor.getStats()).build(); + } + + @POST + @Path("/sla/reset") + public Response resetSLAStats() { + slaMonitor.reset(); + return Response.ok(Map.of("status", "reset")).build(); + } + + @GET + @Path("/pauses") + @Produces(MediaType.APPLICATION_JSON) + public Response getGCPauseStats() { + return Response.ok(gcPauseMonitor.getStats()).build(); + } + + @POST + @Path("/pauses/reset") + public Response resetGCPauseStats() { + gcPauseMonitor.reset(); + return Response.ok(Map.of("status", "reset")).build(); + } + + @GET + @Path("/comparison") + @Produces(MediaType.APPLICATION_JSON) + public Response getComparison() { + Map comparison = new HashMap<>(); + + // Identify which instance is responding + String instanceName = System.getenv("PAYARA_INSTANCE_NAME"); + if (instanceName == null) { + instanceName = "standalone"; + } + comparison.put("instanceName", instanceName); + + // Identify which JVM is running + String jvmVendor = System.getProperty("java.vm.vendor"); + String jvmName = System.getProperty("java.vm.name"); + String gcName = ManagementFactory.getGarbageCollectorMXBeans() + .stream() + .map(GarbageCollectorMXBean::getName) + .collect(Collectors.joining(", ")); + + boolean isAzulC4 = jvmVendor != null && jvmVendor.contains("Azul"); + + comparison.put("jvmVendor", jvmVendor); + comparison.put("jvmName", jvmName); + comparison.put("gcCollectors", gcName); + comparison.put("isAzulC4", isAzulC4); + comparison.put("heapSizeMB", Runtime.getRuntime().maxMemory() / (1024 * 1024)); + + // Current stress level + comparison.put("allocationMode", memoryPressureService.getCurrentMode()); + comparison.put("messageRate", publisher.getMessagesPublished()); + + // GC Performance Metrics (keep old stats for backward compatibility) + List stats = gcStatsService.collectGCStats(); + comparison.put("gcStats", stats); + + // Critical comparison metrics - USE ACCURATE GC PAUSE MONITOR + fish.payara.trader.monitoring.GCPauseMonitor.GCPauseStats pauseStats = gcPauseMonitor.getStats(); + comparison.put("pauseP50Ms", pauseStats.p50Ms); + comparison.put("pauseP95Ms", pauseStats.p95Ms); + comparison.put("pauseP99Ms", pauseStats.p99Ms); + comparison.put("pauseP999Ms", pauseStats.p999Ms); + comparison.put("pauseMaxMs", pauseStats.maxMs); // All-time max + comparison.put("pauseAvgMs", pauseStats.avgPauseMs); + comparison.put("totalPauseCount", pauseStats.totalPauseCount); + comparison.put("totalPauseTimeMs", pauseStats.totalPauseTimeMs); + + // SLA violation tracking (accurate counts since startup/reset) + comparison.put("slaViolations10ms", pauseStats.violationsOver10ms); + comparison.put("slaViolations50ms", pauseStats.violationsOver50ms); + comparison.put("slaViolations100ms", pauseStats.violationsOver100ms); + comparison.put("pauseSampleSize", pauseStats.sampleSize); // For transparency + + return Response.ok(comparison).build(); + } + @GET @Path("/stats") @Produces(MediaType.APPLICATION_JSON) diff --git a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java index 97440b9..c915578 100644 --- a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java +++ b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java @@ -7,9 +7,12 @@ import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * REST endpoint for controlling memory pressure testing @@ -22,6 +25,24 @@ public class MemoryPressureResource { @Inject private MemoryPressureService pressureService; + public enum StressScenario { + DEMO_BASELINE(AllocationMode.OFF, "Baseline - No artificial stress"), + DEMO_NORMAL(AllocationMode.LOW, "Normal Trading - Light load to show steady-state"), + DEMO_STRESS(AllocationMode.HIGH, "High Stress - Heavy allocation + burst patterns"), + DEMO_EXTREME(AllocationMode.EXTREME, "Extreme Stress - Maximum pressure + tenured pollution"); + + private final AllocationMode mode; + private final String description; + + StressScenario(AllocationMode mode, String description) { + this.mode = mode; + this.description = description; + } + + public AllocationMode getMode() { return mode; } + public String getDescription() { return description; } + } + @GET @Path("/status") @Produces(MediaType.APPLICATION_JSON) @@ -66,6 +87,45 @@ public Response setMode(@PathParam("mode") String modeStr) { } } + @POST + @Path("/scenario/{scenario}") + @Produces(MediaType.APPLICATION_JSON) + public Response applyScenario(@PathParam("scenario") String scenarioName) { + try { + StressScenario scenario = StressScenario.valueOf(scenarioName.toUpperCase()); + LOGGER.info("Applying scenario: " + scenario.name()); + + pressureService.setAllocationMode(scenario.getMode()); + + return Response.ok(Map.of( + "scenario", scenario.name(), + "description", scenario.getDescription(), + "mode", scenario.getMode(), + "status", "applied" + )).build(); + + } catch (IllegalArgumentException e) { + return Response.status(Response.Status.BAD_REQUEST) + .entity(Map.of("error", "Invalid scenario: " + scenarioName)) + .build(); + } + } + + @GET + @Path("/scenarios") + @Produces(MediaType.APPLICATION_JSON) + public Response listScenarios() { + List> scenarios = Arrays.stream(StressScenario.values()) + .map(s -> Map.of( + "name", s.name(), + "description", s.getDescription(), + "mode", s.getMode().toString() + )) + .collect(Collectors.toList()); + + return Response.ok(scenarios).build(); + } + @GET @Path("/modes") @Produces(MediaType.APPLICATION_JSON) diff --git a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java index c106e59..af0d892 100644 --- a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java +++ b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java @@ -39,6 +39,9 @@ public class MarketDataBroadcaster { @Inject private HazelcastInstance hazelcastInstance; + @Inject + private fish.payara.trader.monitoring.SLAMonitorService slaMonitor; + private ITopic clusterTopic; // Statistics @@ -122,25 +125,26 @@ private void broadcastLocal(String jsonMessage) { return; } - // Iterate through all sessions and send the message + long startTime = System.currentTimeMillis(); + sessions.removeIf(session -> { if (!session.isOpen()) { - LOGGER.fine("Removing closed session: " + session.getId()); return true; } - try { - // Send async to avoid blocking session.getAsyncRemote().sendText(jsonMessage); - messagesSent++; return false; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to send message to session: " + session.getId(), e); - return true; // Remove problematic session + LOGGER.log(Level.WARNING, "Failed to send message", e); + return true; } }); + long latency = System.currentTimeMillis() - startTime; + if (slaMonitor != null) { + slaMonitor.recordOperation(latency); + } + logStatistics(); } diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 2aeeccb..bcfe09d 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -396,6 +396,136 @@ font-weight: 500; color: white; } + + /* GC Comparison Panel */ + .gc-comparison-panel { + background: white; + padding: 24px 32px; + border-radius: 8px; + box-shadow: 0 2px 4px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06); + margin-bottom: 24px; + } + + .gc-comparison-header { + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: 20px; + border-bottom: 1px solid #e2e8f0; + padding-bottom: 16px; + } + + .gc-comparison-header h2 { + font-size: 1.5em; + color: #1e293b; + font-weight: 700; + } + + .jvm-info { + display: flex; + align-items: center; + gap: 12px; + font-size: 0.95em; + } + + .badge { + padding: 4px 12px; + border-radius: 12px; + font-weight: 600; + font-size: 0.85em; + } + .badge.azul { background: #dbeafe; color: #1e40af; } + .badge.standard { background: #fee2e2; color: #991b1b; } + + .metrics-grid { + display: grid; + grid-template-columns: repeat(4, 1fr); + gap: 24px; + } + + .metric-card { + background: #f8fafc; + padding: 20px; + border-radius: 8px; + border: 1px solid #e2e8f0; + text-align: center; + } + + .metric-card h3 { + font-size: 0.85em; + text-transform: uppercase; + letter-spacing: 0.05em; + color: #64748b; + margin-bottom: 8px; + } + + .metric-value { + font-size: 2em; + font-weight: 800; + color: #0f172a; + display: block; + } + + .metric-unit { + font-size: 0.8em; + color: #94a3b8; + } + + .status-indicator.excellent { color: #10b981; } + .status-indicator.good { color: #3b82f6; } + .status-indicator.warning { color: #f59e0b; } + .status-indicator.critical { color: #ef4444; font-weight: bold; } + + /* Stress Scenario Buttons */ + .stress-scenarios { + margin-bottom: 16px; + background: white; + padding: 16px 32px; + border-radius: 8px; + display: flex; + align-items: center; + gap: 24px; + box-shadow: 0 2px 4px rgba(0,0,0,0.1); + } + + .scenario-buttons { + display: flex; + gap: 12px; + flex-wrap: wrap; + } + + .scenario-btn { + background: white; + color: #475569; + border: 1px solid #cbd5e1; + padding: 8px 16px; + font-size: 0.9em; + display: flex; + flex-direction: column; + align-items: center; + gap: 2px; + min-width: 120px; + } + + .scenario-btn:hover { + background: #f1f5f9; + border-color: #94a3b8; + transform: translateY(-1px); + } + + .scenario-btn.active { + background: #eff6ff; + border-color: #3b82f6; + color: #1d4ed8; + box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2); + } + + .scenario-btn .hint { + font-size: 0.7em; + font-weight: 400; + opacity: 0.8; + text-transform: none; + } @@ -430,6 +560,76 @@

TradeStreamEE

+ +
+

Demo Scenarios

+
+ + + + + + + +
+
+ + +
+
+

GC Performance Comparison

+
+ Instance: + Unknown + JVM: + Detecting... + -- +
+
+ +
+
+

Pause Time P99

+ - + ms +
+
+
+ +
+

Pause Time Max

+ - + ms +
All-time since startup
+
+ +
+

SLA Violations

+ - + pauses > 10ms +
Total since startup
+
+ +
+

Allocation Rate

+ - + MB/sec +
+
+
+
@@ -891,20 +1091,33 @@

System Log

} function pollGCPauseStats() { - fetch('/api/gc/stats') + fetch('/trader-stream-ee/api/gc/stats') .then(response => response.json()) .then(stats => { if (stats && stats.length > 0) { - const gcStat = stats[0]; + // Find the maximum pause duration across all collectors in this poll + let maxPauseMs = 0; + let activeCollectors = []; + + stats.forEach(gcStat => { + const pause = gcStat.lastPauseDuration || 0; + if (pause > maxPauseMs) { + maxPauseMs = pause; + } + // Only list collectors that have actually performed a collection + if (gcStat.collectionCount > 0) { + activeCollectors.push(gcStat.gcName); + } + }); - // Update collector name - document.getElementById('gcPauseCollectorName').textContent = gcStat.gcName || '--'; + // Update collector names display (unique and simplified) + const collectorNames = [...new Set(activeCollectors)].join(' / '); + document.getElementById('gcPauseCollectorName').textContent = collectorNames || '--'; - // Update chart with last pause duration + // Update chart with the maximum pause duration detected if (gcPauseChart) { - const pauseMs = gcStat.lastPauseDuration || 0; gcPauseChart.data.labels.push(''); - gcPauseChart.data.datasets[0].data.push(pauseMs); + gcPauseChart.data.datasets[0].data.push(maxPauseMs); if (gcPauseChart.data.labels.length > 50) { gcPauseChart.data.labels.shift(); @@ -929,7 +1142,7 @@

System Log

} }); - fetch('/api/pressure/mode/' + mode, { method: 'POST' }) + fetch('/trader-stream-ee/api/pressure/mode/' + mode, { method: 'POST' }) .then(response => response.json()) .then(result => { if (clickedButton) { @@ -952,7 +1165,7 @@

System Log

} function pollPressureStatus() { - fetch('/api/pressure/status') + fetch('/trader-stream-ee/api/pressure/status') .then(response => response.json()) .then(status => { document.getElementById('pressureMode').textContent = status.currentMode || 'OFF'; @@ -994,6 +1207,104 @@

System Log

.catch(error => console.error('Error fetching instance status:', error)); } + async function applyScenario(scenario) { + // Reset all buttons + document.querySelectorAll('.scenario-btn').forEach(btn => btn.classList.remove('active')); + + // Set active state on clicked button + const btn = document.getElementById('btn-' + scenario); + if (btn) btn.classList.add('active'); + + try { + const response = await fetch('/trader-stream-ee/api/pressure/scenario/' + scenario, { + method: 'POST' + }); + const result = await response.json(); + console.log('Applied scenario:', result); + + // Also update the pressure mode display + pollPressureStatus(); + + addLog('Scenario applied: ' + result.description); + } catch (error) { + console.error('Error applying scenario:', error); + addLog('Failed to apply scenario: ' + error.message); + } + } + + async function pollGCComparison() { + try { + const response = await fetch('/trader-stream-ee/api/gc/comparison'); + const data = await response.json(); + + // Update instance identification + const instanceName = data.instanceName || 'unknown'; + document.getElementById('instance-name').textContent = instanceName; + + // Color-code instance name by cluster + const instanceEl = document.getElementById('instance-name'); + if (instanceName.includes('c4')) { + instanceEl.style.color = '#10b981'; // green for C4 + } else if (instanceName.includes('g1')) { + instanceEl.style.color = '#ef4444'; // red for G1 + } else { + instanceEl.style.color = '#2563eb'; // blue for unknown + } + + // Update JVM info + document.getElementById('jvm-name').textContent = data.jvmName || 'Unknown'; + const gcTypeEl = document.getElementById('gc-type'); + if (data.isAzulC4) { + gcTypeEl.textContent = 'Azul C4'; + gcTypeEl.className = 'badge azul'; + } else { + gcTypeEl.textContent = 'Standard G1'; // Assuming G1 if not Azul for now, or use data.gcCollectors + gcTypeEl.className = 'badge standard'; + } + + // Update metrics + document.getElementById('pause-p99').textContent = (data.pauseP99Ms || 0).toFixed(2); + document.getElementById('pause-max').textContent = (data.pauseMaxMs || 0).toFixed(2); + document.getElementById('sla-violations').textContent = (data.slaViolations10ms || 0).toLocaleString(); + + // Show sample size for transparency + const sampleSize = data.pauseSampleSize || 0; + const totalPauses = data.totalPauseCount || 0; + document.getElementById('pause-sample-size').textContent = + `${sampleSize.toLocaleString()} samples (${totalPauses.toLocaleString()} total pauses)`; + + // Allocation rate calculation (if available, or estimate) + // data.messageRate is available + // data.allocationMode can give us a hint + if (data.allocationMode) { + // We might need to fetch status to get bytesPerSecond, or use what we have + // For now let's leave it or fetch separate status if needed. + // Actually pollPressureStatus does this. + } + + // Color-code P99 status + const p99Status = document.getElementById('pause-p99-status'); + const p99 = data.pauseP99Ms || 0; + + if (p99 < 1) { + p99Status.className = 'status-indicator excellent'; + p99Status.textContent = '✓ Excellent'; + } else if (p99 < 10) { + p99Status.className = 'status-indicator good'; + p99Status.textContent = '✓ Good'; + } else if (p99 < 50) { + p99Status.className = 'status-indicator warning'; + p99Status.textContent = '⚠ Degraded'; + } else { + p99Status.className = 'status-indicator critical'; + p99Status.textContent = '✗ Critical'; + } + + } catch (error) { + console.error('Failed to fetch GC comparison metrics:', error); + } + } + function flipCard(card) { card.classList.toggle('flipped'); } @@ -1029,6 +1340,11 @@

System Log

if (window.gcPauseStatsInterval) clearInterval(window.gcPauseStatsInterval); window.gcPauseStatsInterval = setInterval(pollGCPauseStats, 3000); pollGCPauseStats(); // Initial poll + + // Start GC Comparison polling (every 2 seconds) + if (window.gcComparisonInterval) clearInterval(window.gcComparisonInterval); + window.gcComparisonInterval = setInterval(pollGCComparison, 2000); + pollGCComparison(); // Start memory pressure status polling (every 3 seconds) if (window.pressureStatusInterval) clearInterval(window.pressureStatusInterval); @@ -1049,6 +1365,7 @@

System Log

addLog('WebSocket disconnected. Attempting auto-reconnect in 3s...'); if (window.chartInterval) clearInterval(window.chartInterval); if (window.gcStatsInterval) clearInterval(window.gcStatsInterval); + if (window.gcComparisonInterval) clearInterval(window.gcComparisonInterval); setTimeout(connect, 3000); }; ws.onerror=error=>addLog('WebSocket error: '+error); diff --git a/start-comparison.sh b/start-comparison.sh new file mode 100755 index 0000000..e9c987c --- /dev/null +++ b/start-comparison.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +set -e + +echo "================================================ட்டான" +echo " TradeStreamEE - JVM Performance Comparison" +echo " C4 vs G1GC Side-by-Side Demo" +echo "================================================ட்டான" +echo "" + +# Create monitoring directory structure +mkdir -p monitoring/{prometheus,grafana/{provisioning/{datasources,dashboards},dashboards},loki,promtail,logs/{c4-{1,2,3},g1-{1,2,3}}} + +# Download JMX Exporter if not present +if [ ! -f "monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar" ]; then + echo "Downloading JMX Prometheus Exporter 1.0.1..." + mkdir -p monitoring/jmx-exporter + wget -q -O monitoring/jmx-exporter/jmx_prometheus_javaagent-1.0.1.jar \ + https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/1.0.1/jmx_prometheus_javaagent-1.0.1.jar + echo "✓ JMX Exporter downloaded" +fi + +# Build application +echo "Building application..." +./mvnw clean package -DskipTests +echo "✓ Build complete" + +# Create networks +echo "Creating Docker networks..." +docker network create trader-network 2>/dev/null || echo "Network trader-network already exists" +docker network create monitoring 2>/dev/null || echo "Network monitoring already exists" +echo "✓ Networks ready" + +# Build Docker images +echo "Building Docker images..." +docker build -t trader-stream-ee:c4 -f Dockerfile.scale . +docker build -t trader-stream-ee:g1 -f Dockerfile.scale.standard . +echo "✓ Images built" + +# Start monitoring stack +echo "Starting monitoring stack (Prometheus, Grafana, Loki)..." +docker compose -f docker-compose-monitoring.yml up -d +echo "✓ Monitoring stack started" + +# Wait for monitoring to be ready +echo "Waiting for monitoring stack to initialize..." +sleep 10 + +# Start C4 cluster +echo "Starting Azul C4 cluster (ports 8080-8083)..." +docker compose -f docker-compose-c4.yml up -d +echo "✓ C4 cluster started" + +# Start G1 cluster +echo "Starting G1GC cluster (ports 9080-9083)..." +docker compose -f docker-compose-g1.yml up -d +echo "✓ G1 cluster started" + +echo "" +echo "================================================ட்டான" +echo " Deployment Complete!" +echo "================================================ட்டான" +echo "" +echo "Application Endpoints:" +echo " C4 Cluster: http://localhost:8080/trader-stream-ee/" +echo " G1 Cluster: http://localhost:9080/trader-stream-ee/" +echo "" +echo "Monitoring:" +echo " Prometheus: http://localhost:9090" +echo " Grafana: http://localhost:3000 (admin/admin)" +echo " Loki: http://localhost:3100" +echo "" +echo "Load Balancers:" +echo " Traefik C4: http://localhost:8084" +echo " Traefik G1: http://localhost:9084" +echo "" +echo "To apply stress test:" +echo " curl -X POST http://localhost:8080/trader-stream-ee/api/memory/mode/EXTREME" +echo " curl -X POST http://localhost:9080/trader-stream-ee/api/memory/mode/EXTREME" +echo "" +echo "To stop everything:" +echo " ./stop-comparison.sh" +echo "" diff --git a/stop-comparison.sh b/stop-comparison.sh new file mode 100755 index 0000000..20a3e6f --- /dev/null +++ b/stop-comparison.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +echo "Stopping JVM comparison demo..." + +docker compose -f docker-compose-g1.yml down +docker compose -f docker-compose-c4.yml down +docker compose -f docker-compose-monitoring.yml down + +echo "All services stopped." +echo "" +echo "To preserve data, volumes were not removed." +echo "To remove all data: docker volume prune" From fd29d4e95c90ea21482d55a29f8a6264a156d52a Mon Sep 17 00:00:00 2001 From: Luqman <27694244+pedanticdev@users.noreply.github.com> Date: Sat, 20 Dec 2025 20:38:00 +0000 Subject: [PATCH 12/17] Fix G1 UI colors to be neutral (#6) Co-authored-by: Luqman Saeed --- src/main/webapp/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index bcfe09d..a51b6b6 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -435,7 +435,7 @@ font-size: 0.85em; } .badge.azul { background: #dbeafe; color: #1e40af; } - .badge.standard { background: #fee2e2; color: #991b1b; } + .badge.standard { background: #dbeafe; color: #1e40af; } .metrics-grid { display: grid; @@ -1246,7 +1246,7 @@

System Log

if (instanceName.includes('c4')) { instanceEl.style.color = '#10b981'; // green for C4 } else if (instanceName.includes('g1')) { - instanceEl.style.color = '#ef4444'; // red for G1 + instanceEl.style.color = '#10b981'; // same as c4 } else { instanceEl.style.color = '#2563eb'; // blue for unknown } From 342387caf1db73264b7c9999b74d9a363dbba869 Mon Sep 17 00:00:00 2001 From: Luqman <27694244+pedanticdev@users.noreply.github.com> Date: Sun, 21 Dec 2025 08:11:07 +0000 Subject: [PATCH 13/17] Adds comprehensive test infrastructure, improves CI/CD pipeline, and introduces code formatting standards for TradeStreamEE. Key Changes Testing Framework: - 19 unit tests with 100% pass rate covering core components (MarketDataFragmentHandler, MarketDataBroadcaster, GC monitoring) - JUnit 5 + Mockito + AssertJ stack with JaCoCo coverage reporting - Performance testing utilities including GCTestUtil for memory pressure testing - Test execution scripts (test.sh) with quick (30s) and full (2-5min) modes Code Quality & Formatting: - Added Spotless Maven plugin (v3.0.0) with Google Java Format for consistent code style - Automatic formatting enforcement with unused import removal and whitespace cleanup - Standardized code formatting across the project CI/CD Improvements: - Enhanced GitHub Actions workflow with JaCoCo integration and 15% coverage threshold enforcement - Automated coverage reporting and validation in build pipeline Monitoring & Reliability: - Optimized OOM monitoring with efficient pattern matching and improved log rotation - Updated Grafana dashboard with enhanced JVM comparison visualizations - Added monitoring data directories to .gitignore The changes provide consistent code formatting standards and a solid foundation for ensuring application reliability and performance validation across different JVM configurations. --- .github/workflows/ci.yml | 16 +- .gitignore | 4 + README.md | 200 +-- monitor-oom.sh | 25 +- .../grafana/dashboards/jvm-comparison.json | 289 +++-- pom.xml | 284 ++++- .../payara/resource/HelloWorldResource.java | 59 +- .../trader/aeron/AeronSubscriberBean.java | 271 ++--- .../aeron/MarketDataFragmentHandler.java | 513 ++++---- .../trader/aeron/MarketDataPublisher.java | 1078 +++++++++-------- .../trader/concurrency/ConcurrencyConfig.java | 10 +- .../concurrency/VirtualThreadExecutor.java | 10 +- .../java/fish/payara/trader/gc/GCStats.java | 190 ++- .../fish/payara/trader/gc/GCStatsService.java | 269 ++-- .../trader/monitoring/GCPauseMonitor.java | 374 +++--- .../trader/monitoring/SLAMonitorService.java | 151 ++- .../trader/pressure/AllocationMode.java | 50 +- .../pressure/MemoryPressureService.java | 409 ++++--- .../payara/trader/rest/ApplicationConfig.java | 6 +- .../payara/trader/rest/GCStatsResource.java | 213 ++-- .../trader/rest/MemoryPressureResource.java | 230 ++-- .../payara/trader/rest/StatusResource.java | 149 ++- .../websocket/MarketDataBroadcaster.java | 314 +++-- .../trader/websocket/MarketDataWebSocket.java | 93 +- .../payara/trader/BasicFunctionalityTest.java | 86 ++ .../aeron/MarketDataFragmentHandlerTest.java | 273 ++--- .../concurrency/ConcurrencyConfigTest.java | 312 +++++ .../VirtualThreadExecutorTest.java | 207 ++++ .../payara/trader/gc/GCStatsServiceTest.java | 141 +++ .../fish/payara/trader/gc/GCStatsTest.java | 559 +++++++++ .../monitoring/SLAMonitorServiceTest.java | 119 ++ .../trader/pressure/AllocationModeTest.java | 357 ++++++ .../trader/rest/GCStatsResourceTest.java | 100 ++ .../rest/MemoryPressureResourceTest.java | 107 ++ .../trader/rest/StatusResourceTest.java | 379 ++++++ .../fish/payara/trader/utils/GCTestUtil.java | 203 ++++ .../websocket/MarketDataBroadcasterTest.java | 209 ++-- src/test/resources/logback-test.xml | 47 + start.sh | 48 +- stop.sh | 6 +- test.sh | 313 +++++ 41 files changed, 5982 insertions(+), 2691 deletions(-) create mode 100644 src/test/java/fish/payara/trader/BasicFunctionalityTest.java create mode 100644 src/test/java/fish/payara/trader/concurrency/ConcurrencyConfigTest.java create mode 100644 src/test/java/fish/payara/trader/concurrency/VirtualThreadExecutorTest.java create mode 100644 src/test/java/fish/payara/trader/gc/GCStatsServiceTest.java create mode 100644 src/test/java/fish/payara/trader/gc/GCStatsTest.java create mode 100644 src/test/java/fish/payara/trader/monitoring/SLAMonitorServiceTest.java create mode 100644 src/test/java/fish/payara/trader/pressure/AllocationModeTest.java create mode 100644 src/test/java/fish/payara/trader/rest/GCStatsResourceTest.java create mode 100644 src/test/java/fish/payara/trader/rest/MemoryPressureResourceTest.java create mode 100644 src/test/java/fish/payara/trader/rest/StatusResourceTest.java create mode 100644 src/test/java/fish/payara/trader/utils/GCTestUtil.java create mode 100644 src/test/resources/logback-test.xml create mode 100755 test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 73eb9d1..040b68a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,5 +28,17 @@ jobs: - name: Generate SBE sources run: ./mvnw generate-sources - - name: Run tests - run: ./mvnw test + - name: Run tests with JaCoCo + run: ./mvnw clean test jacoco:report -Pcoverage + + - name: Check coverage thresholds + run: | + COVERAGE=$(grep -oP '(?<=Total.*?">).*?(?=%)' target/site/jacoco/index.html | head -1) + echo "Coverage: $COVERAGE%" + + if (( $(echo "$COVERAGE < 15" | bc -l) )); then + echo "❌ Coverage ($COVERAGE%) is below minimum threshold (15%)" + exit 1 + else + echo "✅ Coverage ($COVERAGE%) meets minimum threshold (15%)" + fi diff --git a/.gitignore b/.gitignore index e55e6e9..e4e0412 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,7 @@ docs/* *.pdf *.jar monitoring/jmx-exporter/*.jar +monitoring/logs/ +monitoring/prometheus/data/ +monitoring/grafana/data/ +monitoring/loki/data/ diff --git a/README.md b/README.md index 83d22d7..7ccf9a9 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,6 @@ It simulates a high-frequency trading (HFT) dashboard that ingests tens of thousands of market data messages per second, processes them in real-time, and broadcasts updates to a web frontend—all without the latency spikes ("jitter") associated with standard Java Garbage Collection. - - ## ⚡ The Core Technologies TradeStreamEE gets its speed by removing the middleman. We swapped out heavy, traditional methods (REST/JSON) for 'Mechanical Sympathy', an approach that respects the underlying hardware to squeeze out maximum efficiency. @@ -32,26 +30,25 @@ Computers do not natively understand text; they understand bits. Unlike JSON, where you just write data, SBE is **Schema-Driven**. This ensures strict structure and maximum speed. -1. **Define the Schema (`market-data.xml`):** You define your messages in XML. This acts as the contract between Publisher and Subscriber. - ```xml - - - - - ``` -2. **Generate Code:** During the build process (`mvn generate-sources`), the **SbeTool** reads the XML and generates Java classes (Encoders and Decoders). -3. **Zero-Copy Encoding/Decoding:** - * **The Flyweight Pattern:** The generated Java classes are "Flyweights." They do not hold data themselves. Instead, they act as a "window" over the raw byte buffer. - * **No Allocation:** When we read a Trade message, **we do not create a `Trade` object**. We simply move the "window" to the correct position in memory and read the `long` value for price. This generates **zero garbage** for the Garbage Collector to clean up. - +1. **Define the Schema (`market-data.xml`):** You define your messages in XML. This acts as the contract between Publisher and Subscriber. + ```xml + + + + + ``` +2. **Generate Code:** During the build process (`mvn generate-sources`), the **SbeTool** reads the XML and generates Java classes (Encoders and Decoders). +3. **Zero-Copy Encoding/Decoding:** + * **The Flyweight Pattern:** The generated Java classes are "Flyweights." They do not hold data themselves. Instead, they act as a "window" over the raw byte buffer. + * **No Allocation:** When we read a Trade message, **we do not create a `Trade` object**. We simply move the "window" to the correct position in memory and read the `long` value for price. This generates **zero garbage** for the Garbage Collector to clean up. ## 🚀 The Rationale: Why This Project Exists Enterprise Java applications often struggle with two competing requirements: -1. **High Throughput:** Ingesting massive data streams (IoT, Financial Data). -2. **Low Latency:** Processing that data without "Stop-the-World" pauses. +1. **High Throughput:** Ingesting massive data streams (IoT, Financial Data). +2. **Low Latency:** Processing that data without "Stop-the-World" pauses. Standard JVMs (using G1GC or ParallelGC) often "hiccup" under high load, causing UI freezes or missed SLAs. **TradeStreamEE** proves that by combining a modern, broker-less transport (**Aeron**) with a pauseless runtime (**Azul C4**), standard Jakarta EE applications can achieve microsecond-level latency and massive throughput. @@ -62,26 +59,22 @@ This project includes built-in tools to benchmark "The Old Way" vs. "The New Way * **Scenario A (Baseline):** Standard OpenJDK + Naive String Processing. * **Scenario B (Optimized):** Azul Platform Prime + Aeron IPC + Zero-Copy SBE. - - ## 🏗️ Technical Architecture The application implements a **Hybrid Architecture**: -1. **Ingestion Layer (Broker-less):** - * Uses **Aeron IPC** (Inter-Process Communication) via an Embedded Media Driver. - * Bypasses the network stack for ultra-low latency between components. -2. **Serialization Layer (Zero-Copy):** - * Uses **Simple Binary Encoding (SBE)**. - * Decodes messages directly from memory buffers (Flyweight pattern) without allocating Java Objects, reducing GC pressure. -3. **Application Layer (Jakarta EE 11):** - * **Payara Micro 7** serves as the container. - * **CDI** manages the lifecycle of the Aeron Publisher and Subscriber. - * **WebSockets** push updates to the browser. -4. **Runtime Layer:** - * **Azul Platform Prime** uses the **C4 Collector** to clean up the "garbage" created by the WebSocket layer concurrently, ensuring a flat latency profile. - - +1. **Ingestion Layer (Broker-less):** + * Uses **Aeron IPC** (Inter-Process Communication) via an Embedded Media Driver. + * Bypasses the network stack for ultra-low latency between components. +2. **Serialization Layer (Zero-Copy):** + * Uses **Simple Binary Encoding (SBE)**. + * Decodes messages directly from memory buffers (Flyweight pattern) without allocating Java Objects, reducing GC pressure. +3. **Application Layer (Jakarta EE 11):** + * **Payara Micro 7** serves as the container. + * **CDI** manages the lifecycle of the Aeron Publisher and Subscriber. + * **WebSockets** push updates to the browser. +4. **Runtime Layer:** + * **Azul Platform Prime** uses the **C4 Collector** to clean up the "garbage" created by the WebSocket layer concurrently, ensuring a flat latency profile. ## 🛠️ Tech Stack @@ -94,8 +87,6 @@ The application implements a **Hybrid Architecture**: | **Frontend** | **HTML5 / Chart.js** | Real-time visualization via WebSockets. | | **Build** | **Docker / Maven** | Containerized deployment. | - - ## 🔍 Understanding the Modes This demo allows you to switch between two distinct ingestion pipelines to visualize the impact of architectural choices on JVM performance. @@ -125,12 +116,12 @@ graph TD linkStyle 3 stroke:#764ba2,stroke-width:2px; ``` -1. **Publisher:** Generates synthetic market data as standard Java Objects. -2. **Allocation:** Immediately converts data to a JSON `String` using `StringBuilder` (high allocation). -3. **Artificial Load:** Wraps the JSON in a large "envelope" with 1KB of padding to stress the Garbage Collector. -4. **Transport:** Direct method call to `MarketDataBroadcaster`. -5. **WebSocket:** Pushes the heavy JSON string to the browser. -6. **Browser:** Unwraps the payload and renders the chart. +1. **Publisher:** Generates synthetic market data as standard Java Objects. +2. **Allocation:** Immediately converts data to a JSON `String` using `StringBuilder` (high allocation). +3. **Artificial Load:** Wraps the JSON in a large "envelope" with 1KB of padding to stress the Garbage Collector. +4. **Transport:** Direct method call to `MarketDataBroadcaster`. +5. **WebSocket:** Pushes the heavy JSON string to the browser. +6. **Browser:** Unwraps the payload and renders the chart. **Performance Characteristics:** @@ -164,15 +155,15 @@ graph TD linkStyle 4 stroke:#667eea,stroke-width:2px; ``` -1. **Publisher:** Generates synthetic market data. -2. **Encoding:** Encodes data into a compact binary format using **SBE**. - * *Zero-Copy:* Writes directly to an off-heap direct buffer. -3. **Transport (Aeron):** Publishes the binary message to the **Aeron IPC** ring buffer. - * *Kernel Bypass:* Data moves via shared memory, avoiding the OS network stack. -4. **Subscriber (Fragment Handler):** Reads the binary message using SBE "Flyweights" (reusable view objects). - * *Zero-Allocation:* No new Java objects are created during decoding. -5. **Transformation:** Converts the binary data to a compact, flat JSON string (minimal allocation). -6. **WebSocket:** Pushes the lightweight JSON to the browser. +1. **Publisher:** Generates synthetic market data. +2. **Encoding:** Encodes data into a compact binary format using **SBE**. + * *Zero-Copy:* Writes directly to an off-heap direct buffer. +3. **Transport (Aeron):** Publishes the binary message to the **Aeron IPC** ring buffer. + * *Kernel Bypass:* Data moves via shared memory, avoiding the OS network stack. +4. **Subscriber (Fragment Handler):** Reads the binary message using SBE "Flyweights" (reusable view objects). + * *Zero-Allocation:* No new Java objects are created during decoding. +5. **Transformation:** Converts the binary data to a compact, flat JSON string (minimal allocation). +6. **WebSocket:** Pushes the lightweight JSON to the browser. **Performance Characteristics:** @@ -180,8 +171,6 @@ graph TD * **Pauseless:** Azul C4 collector handles the WebSocket strings concurrently, maintaining a flat latency profile. * **High Throughput:** Aeron IPC handles millions of messages/sec with sub-microsecond latency. - - ## 🚦 Quick Start: The Comparison Matrix The `start.sh` script provides commands to run the TradeStreamEE application in various configurations, allowing for a comprehensive comparison of JVM and architectural performance. @@ -194,19 +183,19 @@ The `start.sh` script provides commands to run the TradeStreamEE application in | **4. Optimizing Standard Java** | `./start.sh standard-aeron` | Standard JDK (G1GC) | Aeron (Optimized) | See if architectural optimization helps G1GC performance. | ### Observability Commands -* `./start-comparison.sh` - Deploy complete JVM comparison stack (recommended) -* `./stop-comparison.sh` - Stop all comparison services -* `docker-compose -f docker-compose-monitoring.yml up -d` - Start monitoring stack only -* `docker-compose -f docker-compose-c4.yml up -d` - Start C4 cluster only -* `docker-compose -f docker-compose-g1.yml up -d` - Start G1GC cluster only -* `docker-compose -f docker-compose-monitoring.yml ps` - Check monitoring status -### Utilities -* `./start.sh logs` - View live logs -* `./start.sh stop` - Stop containers -* `./start.sh clean` - Deep clean (remove volumes/images) +* `./start-comparison.sh` - Deploy complete JVM comparison stack (recommended) +* `./stop-comparison.sh` - Stop all comparison services +* `docker-compose -f docker-compose-monitoring.yml up -d` - Start monitoring stack only +* `docker-compose -f docker-compose-c4.yml up -d` - Start C4 cluster only +* `docker-compose -f docker-compose-g1.yml up -d` - Start G1GC cluster only +* `docker-compose -f docker-compose-monitoring.yml ps` - Check monitoring status +### Utilities +* `./start.sh logs` - View live logs +* `./start.sh stop` - Stop containers +* `./start.sh clean` - Deep clean (remove volumes/images) ## ⚙️ Configuration & Tuning @@ -224,11 +213,13 @@ Controls how data moves from the Publisher to the Processor. The Docker configurations are optimized with enhanced settings for performance testing: **Azul Prime (C4) Configuration:** + ```dockerfile ENV JAVA_OPTS="-Xms8g -Xmx8g -XX:+AlwaysPreTouch -XX:+UseTransparentHugePages -Djava.net.preferIPv4Stack=true" ``` **Standard JDK (G1GC) Configuration:** + ```dockerfile ENV JAVA_OPTS="-Xms8g -Xmx8g -XX:+UseG1GC -XX:+AlwaysPreTouch -XX:+UseTransparentHugePages -Djava.net.preferIPv4Stack=true" ``` @@ -283,20 +274,18 @@ The web UI includes **GC Challenge Mode** controls that allow: This feature enables live demonstration of how Azul C4 maintains low pause times even under extreme memory pressure, while G1GC shows increasingly long pauses. - - ## 📊 Monitoring & Observability TradeStreamEE includes comprehensive monitoring infrastructure to compare JVM performance between Azul C4 and standard G1GC configurations. ### Monitoring Stack -| Component | Technology | Purpose | Access | -|:---|:---|:---|:---| -| **Metrics Collection** | Prometheus + JMX Exporter | JVM GC metrics, memory, threads | http://localhost:9090 | -| **Visualization** | Grafana | Performance dashboards | http://localhost:3000 (admin/admin) | -| **Log Aggregation** | Loki + Promtail | Centralized log management | http://localhost:3100 | -| **Load Balancing** | Traefik | Traffic distribution + metrics | http://localhost:8080 (C4), http://localhost:9080 (G1) | +| Component | Technology | Purpose | Access | +|:-----------------------|:--------------------------|:--------------------------------|:-------------------------------------------------------| +| **Metrics Collection** | Prometheus + JMX Exporter | JVM GC metrics, memory, threads | http://localhost:9090 | +| **Visualization** | Grafana | Performance dashboards | http://localhost:3000 (admin/admin) | +| **Log Aggregation** | Loki + Promtail | Centralized log management | http://localhost:3100 | +| **Load Balancing** | Traefik | Traffic distribution + metrics | http://localhost:8080 (C4), http://localhost:9080 (G1) | ### JVM Comparison Dashboard @@ -376,6 +365,7 @@ After starting the observability stack: ### Monitoring Configuration #### JMX Exporter + Each JVM instance runs a JMX exporter agent that exposes: * Garbage collection metrics (pause times, collection counts) * Memory pool usage (heap/non-heap) @@ -383,12 +373,14 @@ Each JVM instance runs a JMX exporter agent that exposes: * Custom application metrics #### Prometheus Configuration + The Prometheus setup (`monitoring/prometheus/prometheus.yml`) scrapes: * JMX metrics from all JVM instances (ports 9010-9022) * Traefik metrics for load balancer performance * Self-monitoring metrics #### Log Collection + Promtail automatically collects and ships container logs to Loki, enabling: * Log-based troubleshooting * Correlation of performance issues with application events @@ -436,6 +428,7 @@ The stress tests will: 6. Visualize the "pauseless" characteristics of C4 under extreme load **Sample GC Stats Response:** + ```json { "gcName": "C4", @@ -479,8 +472,6 @@ The application exposes a lightweight REST endpoint for health checks and intern } ``` - - ## 📂 Project Structure ```text @@ -516,6 +507,71 @@ Dockerfile.scale # Multi-stage build for C4 instances Dockerfile.scale.standard # Build for G1GC instances ``` +## 🧪 Testing & Quality Assurance + +TradeStreamEE includes a comprehensive testing infrastructure designed to ensure reliability and performance validation. + +### Test Framework Stack + +| Component | Technology | Purpose | +|-------------------------|-----------------------------|---------------------------------| +| **Unit Testing** | JUnit 5 + Mockito + AssertJ | Core component validation | +| **Integration Testing** | Custom framework | Service layer interactions | +| **Performance Testing** | JMH + Custom utilities | Benchmarking and load testing | +| **Code Coverage** | JaCoCo Maven Plugin | Coverage analysis and reporting | + +### Quick Test Execution + +```bash +# Quick test (unit tests only, ~30 seconds) +./test.sh quick + +# Full test suite (unit + integration, 2-5 minutes) +./test.sh full + +# Maven commands +./mvnw test # Unit tests +./mvnw jacoco:report # Generate coverage report +``` + +### Current Test Coverage + +**✅ Working Tests (19/19 passing):** +- **BasicFunctionalityTest**: Core components and allocation modes +- **MarketDataFragmentHandlerTest**: SBE message processing +- **MarketDataBroadcasterTest**: WebSocket session management + +**📊 Coverage Metrics:** +- **Tests**: 19 unit tests with 100% pass rate +- **Instruction Coverage**: ~45% (core components) +- **Test Execution**: < 30 seconds for quick run + +### Test Categories + +1. **Unit Tests**: Core component testing in isolation +2. **Integration Tests**: Service layer interactions +3. **Performance Tests**: Load testing and benchmarks +4. **Memory Pressure Tests**: GC behavior validation + +### Test Utilities + +**GCTestUtil**: Provides GC testing and memory pressure utilities: + +```java +// Capture GC statistics +GCTestUtil.GCStatistics before = GCTestUtil.captureInitialStats(); + +// Create controlled memory pressure +GCTestUtil.allocateMemory(100); // 100MB + +// Analyze GC impact +GCTestUtil.GCStatistics after = GCTestUtil.calculateDelta(before); +``` + +### Testing Documentation + +For detailed testing information, see: [TESTING.md](TESTING.md) - Complete testing guide with examples and best practices. + ## 📜 License -This project is a reference implementation provided for demonstration purposes. \ No newline at end of file +This project is a reference implementation provided for demonstration purposes. diff --git a/monitor-oom.sh b/monitor-oom.sh index cd9fadd..9d18dff 100755 --- a/monitor-oom.sh +++ b/monitor-oom.sh @@ -134,7 +134,8 @@ rotate_log_if_needed() { if [ -f "$LOG_FILE" ]; then local size=$(stat -f%z "$LOG_FILE" 2>/dev/null || stat -c%s "$LOG_FILE" 2>/dev/null || echo 0) if [ "$size" -gt "$MAX_LOG_SIZE" ]; then - mv "$LOG_FILE" "${LOG_FILE}.old" + cp "$LOG_FILE" "${LOG_FILE}.old" + > "$LOG_FILE" # Truncate in place log_info "Rotated log file (size: $size bytes)" fi fi @@ -172,20 +173,18 @@ check_container_oom() { return 1 fi - # Check each OOM pattern - for pattern in "${OOM_PATTERNS[@]}"; do - if echo "$logs" | grep -q "$pattern"; then - found_oom=true - oom_pattern="$pattern" - break - fi - done + # Combine patterns for a single grep search (more efficient) + local combined_pattern=$(printf "|%s" "${OOM_PATTERNS[@]}") + combined_pattern="${combined_pattern:1}" # Remove leading pipe - if [ "$found_oom" = true ]; then - log_error "OOM detected in container '$container' - Pattern: '$oom_pattern'" + if echo "$logs" | grep -Eq "$combined_pattern"; then + found_oom=true + # Find the specific matching line for reporting + local matching_line=$(echo "$logs" | grep -E "$combined_pattern" | head -1) + log_error "OOM detected in container '$container' - Match: '$matching_line'" - # Extract and log the actual OOM error lines - local oom_lines=$(echo "$logs" | grep -A 3 "$oom_pattern" | head -10) + # Extract and log context around the error + local oom_lines=$(echo "$logs" | grep -B 1 -A 5 -E "$combined_pattern" | head -20) echo "$oom_lines" | while IFS= read -r line; do log " | $line" done diff --git a/monitoring/grafana/dashboards/jvm-comparison.json b/monitoring/grafana/dashboards/jvm-comparison.json index 3c85d9b..080bda3 100644 --- a/monitoring/grafana/dashboards/jvm-comparison.json +++ b/monitoring/grafana/dashboards/jvm-comparison.json @@ -1,155 +1,184 @@ { "dashboard": { "title": "JVM Performance Comparison - C4 vs G1GC", - "tags": ["jvm", "gc", "performance", "comparison"], + "tags": [ "jvm", "gc", "performance", "comparison" ], "timezone": "browser", "editable": true, - "panels": [ - { - "id": 1, - "title": "GC Pause Time Comparison (P99)", - "type": "timeseries", - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 0}, - "targets": [ - { - "expr": "histogram_quantile(0.99, rate(jvm_gc_collection_time_ms[5m])) by (jvm_type, gc)", - "legendFormat": "{{jvm_type}} - {{gc}}", - "refId": "A" + "panels": [ { + "id": 1, + "title": "GC Pause Time Comparison (P99)", + "type": "timeseries", + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 0 + }, + "targets": [ { + "expr": "histogram_quantile(0.99, rate(jvm_gc_collection_time_ms[5m])) by (jvm_type, gc)", + "legendFormat": "{{jvm_type}} - {{gc}}", + "refId": "A" + } ], + "fieldConfig": { + "defaults": { + "unit": "ms", + "custom": { + "axisLabel": "Pause Time (ms)" } - ], - "fieldConfig": { - "defaults": { - "unit": "ms", - "custom": { - "axisLabel": "Pause Time (ms)" + }, + "overrides": [ { + "matcher": { + "id": "byRegexp", + "options": ".*azul-c4.*" + }, + "properties": [ { + "id": "color", + "value": { + "mode": "fixed", + "fixedColor": "green" } + } ] + }, { + "matcher": { + "id": "byRegexp", + "options": ".*g1.*" }, - "overrides": [ - { - "matcher": {"id": "byRegexp", "options": ".*azul-c4.*"}, - "properties": [ - {"id": "color", "value": {"mode": "fixed", "fixedColor": "green"}} - ] - }, - { - "matcher": {"id": "byRegexp", "options": ".*g1.*"}, - "properties": [ - {"id": "color", "value": {"mode": "fixed", "fixedColor": "red"}} - ] + "properties": [ { + "id": "color", + "value": { + "mode": "fixed", + "fixedColor": "red" } - ] - } + } ] + } ] + } + }, { + "id": 2, + "title": "GC Collection Count Rate", + "type": "timeseries", + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 0 }, - { - "id": 2, - "title": "GC Collection Count Rate", - "type": "timeseries", - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 0}, - "targets": [ - { - "expr": "rate(jvm_gc_collection_count[5m]) by (jvm_type, gc)", - "legendFormat": "{{jvm_type}} - {{gc}}", - "refId": "A" - } - ], - "fieldConfig": { - "defaults": { - "unit": "ops", - "custom": { - "axisLabel": "Collections/sec" - } + "targets": [ { + "expr": "rate(jvm_gc_collection_count[5m]) by (jvm_type, gc)", + "legendFormat": "{{jvm_type}} - {{gc}}", + "refId": "A" + } ], + "fieldConfig": { + "defaults": { + "unit": "ops", + "custom": { + "axisLabel": "Collections/sec" } } + } + }, { + "id": 3, + "title": "Heap Memory Usage", + "type": "timeseries", + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 8 }, - { - "id": 3, - "title": "Heap Memory Usage", - "type": "timeseries", - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 8}, - "targets": [ - { - "expr": "jvm_memory_heap_used by (jvm_type, instance) / jvm_memory_heap_max * 100", - "legendFormat": "{{jvm_type}} - {{instance}}", - "refId": "A" - } - ], - "fieldConfig": { - "defaults": { - "unit": "percent", - "custom": { - "axisLabel": "Heap Usage %" - } + "targets": [ { + "expr": "jvm_memory_heap_used by (jvm_type, instance) / jvm_memory_heap_max * 100", + "legendFormat": "{{jvm_type}} - {{instance}}", + "refId": "A" + } ], + "fieldConfig": { + "defaults": { + "unit": "percent", + "custom": { + "axisLabel": "Heap Usage %" } } + } + }, { + "id": 4, + "title": "Thread Count", + "type": "timeseries", + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 8 }, - { - "id": 4, - "title": "Thread Count", - "type": "timeseries", - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 8}, - "targets": [ - { - "expr": "jvm_threads_current by (jvm_type, instance)", - "legendFormat": "{{jvm_type}} - {{instance}}", - "refId": "A" - } - ], - "fieldConfig": { - "defaults": { - "unit": "short", - "custom": { - "axisLabel": "Thread Count" - } + "targets": [ { + "expr": "jvm_threads_current by (jvm_type, instance)", + "legendFormat": "{{jvm_type}} - {{instance}}", + "refId": "A" + } ], + "fieldConfig": { + "defaults": { + "unit": "short", + "custom": { + "axisLabel": "Thread Count" } } + } + }, { + "id": 5, + "title": "GC Pause Time Distribution (Heatmap)", + "type": "heatmap", + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 16 }, - { - "id": 5, - "title": "GC Pause Time Distribution (Heatmap)", - "type": "heatmap", - "gridPos": {"h": 8, "w": 24, "x": 0, "y": 16}, - "targets": [ - { - "expr": "rate(jvm_gc_collection_time_ms[1m]) by (jvm_type, le)", - "legendFormat": "{{jvm_type}}", - "refId": "A", - "format": "heatmap" - } - ] + "targets": [ { + "expr": "rate(jvm_gc_collection_time_ms[1m]) by (jvm_type, le)", + "legendFormat": "{{jvm_type}}", + "refId": "A", + "format": "heatmap" + } ] + }, { + "id": 6, + "title": "Performance Summary", + "type": "stat", + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 24 }, - { - "id": 6, - "title": "Performance Summary", - "type": "stat", - "gridPos": {"h": 8, "w": 24, "x": 0, "y": 24}, - "targets": [ - { - "expr": "histogram_quantile(0.99, rate(jvm_gc_collection_time_ms[5m])) by (jvm_type)", - "legendFormat": "{{jvm_type}} P99", - "refId": "A" - }, - { - "expr": "max(jvm_gc_collection_time_ms) by (jvm_type)", - "legendFormat": "{{jvm_type}} Max", - "refId": "B" - } - ], - "fieldConfig": { - "defaults": { - "unit": "ms", - "thresholds": { - "mode": "absolute", - "steps": [ - {"value": 0, "color": "green"}, - {"value": 10, "color": "yellow"}, - {"value": 50, "color": "red"} - ] - } + "targets": [ { + "expr": "histogram_quantile(0.99, rate(jvm_gc_collection_time_ms[5m])) by (jvm_type)", + "legendFormat": "{{jvm_type}} P99", + "refId": "A" + }, { + "expr": "max(jvm_gc_collection_time_ms) by (jvm_type)", + "legendFormat": "{{jvm_type}} Max", + "refId": "B" + } ], + "fieldConfig": { + "defaults": { + "unit": "ms", + "thresholds": { + "mode": "absolute", + "steps": [ { + "value": 0, + "color": "green" + }, { + "value": 10, + "color": "yellow" + }, { + "value": 50, + "color": "red" + } ] } } } - ], + } ], "refresh": "5s", - "time": {"from": "now-30m", "to": "now"} + "time": { + "from": "now-30m", + "to": "now" + } } -} +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4416df2..ab96153 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,18 @@ 3.27.6 test + + + + org.glassfish.jersey.core + jersey-common + test + + + org.glassfish.jersey.inject + jersey-hk2 + test + @@ -171,66 +183,184 @@ false - + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.3 + + + **/*Test.java + + + ${surefire.argLine} + + + + + org.jacoco + jacoco-maven-plugin + 0.8.14 - swagger-ui - generate-resources + prepare-agent - wget + prepare-agent - true - https://github.com/swagger-api/swagger-ui/archive/master.tar.gz - true - ${project.build.directory} + surefire.argLine - - - --> - - maven-resources-plugin - 3.3.1 - - - + + org.apache.maven.plugins - maven-surefire-plugin + maven-failsafe-plugin 3.2.3 + + + + integration-test + verify + + + - **/*Test.java + **/integration/*Test.java + ${failsafe.argLine} -Xmx4g -XX:+UseG1GC + + ${project.basedir}/src/test/resources/logback-test.xml + + + + + com.diffplug.spotless + spotless-maven-plugin + 3.0.0 + + + + 1.17.0 + + + + + + + + + src/**/*.json + monitoring/**/*.json + + + 2.15.2 + + true + + + + + + + **/*.md + + + + + + + + check + + compile + + + @@ -249,6 +379,88 @@ + + + coverage + + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + prepare-agent + + prepare-agent + + + + report + test + + report + + + + XML + HTML + + + + + check + + check + + + + + BUNDLE + + + INSTRUCTION + COVEREDRATIO + 0.15 + + + CLASS + MISSEDCOUNT + 40 + + + + + + + + + + + + + + + quick-test + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*Test.java + + + **/integration/** + **/*LoadTest.java + **/*BenchmarkTest.java + + + + + + diff --git a/src/main/java/fish/payara/resource/HelloWorldResource.java b/src/main/java/fish/payara/resource/HelloWorldResource.java index b82e4f5..42aa048 100644 --- a/src/main/java/fish/payara/resource/HelloWorldResource.java +++ b/src/main/java/fish/payara/resource/HelloWorldResource.java @@ -12,42 +12,45 @@ import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Timed; import org.eclipse.microprofile.openapi.annotations.Operation; +import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; -import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; @Path("hello") public class HelloWorldResource { - @Inject - @ConfigProperty(name = "defaultName", defaultValue = "world") - private String defaultName; + @Inject + @ConfigProperty(name = "defaultName", defaultValue = "world") + private String defaultName; - @GET - @Operation(summary = "Get a personalized greeting") - @APIResponses(value = { + @GET + @Operation(summary = "Get a personalized greeting") + @APIResponses( + value = { @APIResponse(responseCode = "200", description = "Successful operation"), @APIResponse(responseCode = "400", description = "Invalid input") - }) - @Counted(name = "helloEndpointCount", description = "Count of calls to the hello endpoint") - @Timed(name = "helloEndpointTime", description = "Time taken to execute the hello endpoint") - @Timeout(3000) // Timeout after 3 seconds - @Retry(maxRetries = 3) // Retry the request up to 3 times on failure - @Fallback(fallbackMethod = "fallbackMethod") - public Response hello(@QueryParam("name") @Parameter(name = "name", description = "Name to include in the greeting", required = false, example = "John") String name) { - if ((name == null) || name.trim().isEmpty()) { - name = defaultName; - } - return Response - .ok(name) - .build(); - } - - public Response fallbackMethod(@QueryParam("name") String name) { - // Fallback logic when the hello method fails or exceeds retries - return Response - .ok("Fallback data") - .build(); + }) + @Counted(name = "helloEndpointCount", description = "Count of calls to the hello endpoint") + @Timed(name = "helloEndpointTime", description = "Time taken to execute the hello endpoint") + @Timeout(3000) // Timeout after 3 seconds + @Retry(maxRetries = 3) // Retry the request up to 3 times on failure + @Fallback(fallbackMethod = "fallbackMethod") + public Response hello( + @QueryParam("name") + @Parameter( + name = "name", + description = "Name to include in the greeting", + required = false, + example = "John") + String name) { + if ((name == null) || name.trim().isEmpty()) { + name = defaultName; } + return Response.ok(name).build(); + } -} \ No newline at end of file + public Response fallbackMethod(@QueryParam("name") String name) { + // Fallback logic when the hello method fails or exceeds retries + return Response.ok("Fallback data").build(); + } +} diff --git a/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java b/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java index c8b303e..9202bb4 100644 --- a/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java +++ b/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java @@ -5,181 +5,158 @@ import io.aeron.Subscription; import io.aeron.driver.MediaDriver; import io.aeron.driver.ThreadingMode; -import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; -import jakarta.ejb.Singleton; -import jakarta.ejb.Startup; import jakarta.enterprise.concurrent.ManagedExecutorService; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Initialized; import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; -import org.agrona.CloseHelper; -import org.agrona.concurrent.BackoffIdleStrategy; -import org.agrona.concurrent.IdleStrategy; - import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; +import org.agrona.CloseHelper; +import org.agrona.concurrent.BackoffIdleStrategy; +import org.agrona.concurrent.IdleStrategy; /** - * Aeron Ingress Singleton Bean - * Launches an embedded MediaDriver and subscribes to market data stream. - * Uses SBE decoders for zero-copy message processing. - * Runs in a dedicated thread to continuously poll for messages. - * IMPORTANT: This must initialize BEFORE MarketDataPublisher + * Aeron Ingress Singleton Bean Launches an embedded MediaDriver and subscribes to market data + * stream. Uses SBE decoders for zero-copy message processing. Runs in a dedicated thread to + * continuously poll for messages. IMPORTANT: This must initialize BEFORE MarketDataPublisher */ @ApplicationScoped public class AeronSubscriberBean { - private static final Logger LOGGER = Logger.getLogger(AeronSubscriberBean.class.getName()); - - private static final String CHANNEL = "aeron:ipc"; - private static final int STREAM_ID = 1001; - private static final int FRAGMENT_LIMIT = 10; - - private MediaDriver mediaDriver; - private Aeron aeron; - private Subscription subscription; - private volatile boolean running = false; - private Future pollingFuture; - - @Inject - private MarketDataFragmentHandler fragmentHandler; - - @Inject - @VirtualThreadExecutor - private ManagedExecutorService managedExecutorService; - - void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { - managedExecutorService.submit(() -> init()); + private static final Logger LOGGER = Logger.getLogger(AeronSubscriberBean.class.getName()); + + private static final String CHANNEL = "aeron:ipc"; + private static final int STREAM_ID = 1001; + private static final int FRAGMENT_LIMIT = 10; + + private MediaDriver mediaDriver; + private Aeron aeron; + private Subscription subscription; + private volatile boolean running = false; + private Future pollingFuture; + + @Inject private MarketDataFragmentHandler fragmentHandler; + + @Inject @VirtualThreadExecutor private ManagedExecutorService managedExecutorService; + + void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { + managedExecutorService.submit(() -> init()); + } + + public void init() { + LOGGER.info("Initializing Aeron Subscriber Bean..."); + + try { + LOGGER.info("Launching embedded MediaDriver..."); + mediaDriver = + MediaDriver.launchEmbedded( + new MediaDriver.Context() + .threadingMode(ThreadingMode.SHARED) + .dirDeleteOnStart(true) + .dirDeleteOnShutdown(true)); + + LOGGER.info("MediaDriver launched at: " + mediaDriver.aeronDirectoryName()); + LOGGER.info("Connecting Aeron client..."); + aeron = + Aeron.connect( + new Aeron.Context() + .aeronDirectoryName(mediaDriver.aeronDirectoryName()) + .errorHandler(this::onError) + .availableImageHandler( + image -> LOGGER.info("Available image: " + image.sourceIdentity())) + .unavailableImageHandler( + image -> LOGGER.info("Unavailable image: " + image.sourceIdentity()))); + LOGGER.info("Adding subscription on channel: " + CHANNEL + ", stream: " + STREAM_ID); + subscription = aeron.addSubscription(CHANNEL, STREAM_ID); + startPolling(); + LOGGER.info("Aeron Subscriber Bean initialized successfully"); + + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Failed to initialize Aeron Subscriber Bean", e); + cleanup(); + throw new RuntimeException("Failed to initialize Aeron", e); } - - - public void init() { - LOGGER.info("Initializing Aeron Subscriber Bean..."); - - try { - LOGGER.info("Launching embedded MediaDriver..."); - mediaDriver = MediaDriver.launchEmbedded( - new MediaDriver.Context() - .threadingMode(ThreadingMode.SHARED) - .dirDeleteOnStart(true) - .dirDeleteOnShutdown(true) - ); - - LOGGER.info("MediaDriver launched at: " + mediaDriver.aeronDirectoryName()); - LOGGER.info("Connecting Aeron client..."); - aeron = Aeron.connect( - new Aeron.Context() - .aeronDirectoryName(mediaDriver.aeronDirectoryName()) - .errorHandler(this::onError) - .availableImageHandler(image -> - LOGGER.info("Available image: " + image.sourceIdentity())) - .unavailableImageHandler(image -> - LOGGER.info("Unavailable image: " + image.sourceIdentity())) - ); - LOGGER.info("Adding subscription on channel: " + CHANNEL + ", stream: " + STREAM_ID); - subscription = aeron.addSubscription(CHANNEL, STREAM_ID); - startPolling(); - LOGGER.info("Aeron Subscriber Bean initialized successfully"); - - } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Failed to initialize Aeron Subscriber Bean", e); - cleanup(); - throw new RuntimeException("Failed to initialize Aeron", e); - } - } - - /** - * Start background task to continuously poll for messages - */ - private void startPolling() { - running = true; - pollingFuture = managedExecutorService.submit(() -> { - LOGGER.info("Aeron polling task started"); - - final IdleStrategy idleStrategy = new BackoffIdleStrategy( - 100, 10, - TimeUnit.MICROSECONDS.toNanos(1), - TimeUnit.MICROSECONDS.toNanos(100) - ); - - while (running && !Thread.currentThread().isInterrupted()) { + } + + /** Start background task to continuously poll for messages */ + private void startPolling() { + running = true; + pollingFuture = + managedExecutorService.submit( + () -> { + LOGGER.info("Aeron polling task started"); + + final IdleStrategy idleStrategy = + new BackoffIdleStrategy( + 100, + 10, + TimeUnit.MICROSECONDS.toNanos(1), + TimeUnit.MICROSECONDS.toNanos(100)); + + while (running && !Thread.currentThread().isInterrupted()) { try { - final int fragmentsRead = subscription.poll(fragmentHandler, FRAGMENT_LIMIT); + final int fragmentsRead = subscription.poll(fragmentHandler, FRAGMENT_LIMIT); - idleStrategy.idle(fragmentsRead); + idleStrategy.idle(fragmentsRead); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Error polling subscription", e); + LOGGER.log(Level.SEVERE, "Error polling subscription", e); } - } + } - LOGGER.info("Aeron polling task stopped"); - }); - } + LOGGER.info("Aeron polling task stopped"); + }); + } - /** - * Error handler for Aeron - */ - private void onError(Throwable throwable) { - LOGGER.log(Level.SEVERE, "Aeron error occurred", throwable); - } + /** Error handler for Aeron */ + private void onError(Throwable throwable) { + LOGGER.log(Level.SEVERE, "Aeron error occurred", throwable); + } - @PreDestroy - public void shutdown() { - LOGGER.info("Shutting down Aeron Subscriber Bean..."); - running = false; - - if (pollingFuture != null) { - pollingFuture.cancel(true); - } - - cleanup(); - LOGGER.info("Aeron Subscriber Bean shut down"); - } - - /** - * Clean up all Aeron resources - */ - private void cleanup() { - CloseHelper.quietClose(subscription); - CloseHelper.quietClose(aeron); - CloseHelper.quietClose(mediaDriver); - } + @PreDestroy + public void shutdown() { + LOGGER.info("Shutting down Aeron Subscriber Bean..."); + running = false; - /** - * Get subscription statistics - */ - public String getStatus() { - if (subscription != null) { - return String.format( - "Channel: %s, Stream: %d, Images: %d, Running: %b", - subscription.channel(), - subscription.streamId(), - subscription.imageCount(), - running - ); - } - return "Not initialized"; + if (pollingFuture != null) { + pollingFuture.cancel(true); } - /** - * Get the Aeron directory name for connecting publishers - */ - public String getAeronDirectoryName() { - if (mediaDriver != null) { - return mediaDriver.aeronDirectoryName(); - } - return null; + cleanup(); + LOGGER.info("Aeron Subscriber Bean shut down"); + } + + /** Clean up all Aeron resources */ + private void cleanup() { + CloseHelper.quietClose(subscription); + CloseHelper.quietClose(aeron); + CloseHelper.quietClose(mediaDriver); + } + + /** Get subscription statistics */ + public String getStatus() { + if (subscription != null) { + return String.format( + "Channel: %s, Stream: %d, Images: %d, Running: %b", + subscription.channel(), subscription.streamId(), subscription.imageCount(), running); } + return "Not initialized"; + } - /** - * Check if the MediaDriver is ready - */ - public boolean isReady() { - return mediaDriver != null && aeron != null && subscription != null && running; + /** Get the Aeron directory name for connecting publishers */ + public String getAeronDirectoryName() { + if (mediaDriver != null) { + return mediaDriver.aeronDirectoryName(); } -} \ No newline at end of file + return null; + } + + /** Check if the MediaDriver is ready */ + public boolean isReady() { + return mediaDriver != null && aeron != null && subscription != null && running; + } +} diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java index d3c4e09..4b743d1 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java @@ -6,286 +6,317 @@ import io.aeron.logbuffer.Header; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.agrona.DirectBuffer; - import java.util.logging.Level; import java.util.logging.Logger; +import org.agrona.DirectBuffer; /** - * FragmentHandler that uses SBE Flyweights for zero-copy message decoding. - * This handler processes Aeron fragments by: - * 1. Decoding the SBE message header to identify message type - * 2. Using the appropriate SBE decoder (flyweight pattern) - * 3. Extracting data without object allocation - * 4. Broadcasting as JSON (intentionally creating garbage for GC stress testing) + * FragmentHandler that uses SBE Flyweights for zero-copy message decoding. This handler processes + * Aeron fragments by: 1. Decoding the SBE message header to identify message type 2. Using the + * appropriate SBE decoder (flyweight pattern) 3. Extracting data without object allocation 4. + * Broadcasting as JSON (intentionally creating garbage for GC stress testing) */ @ApplicationScoped public class MarketDataFragmentHandler implements FragmentHandler { - private static final Logger LOGGER = Logger.getLogger(MarketDataFragmentHandler.class.getName()); + private static final Logger LOGGER = Logger.getLogger(MarketDataFragmentHandler.class.getName()); - // SBE Message Header Decoder (reusable flyweight) - private final MessageHeaderDecoder headerDecoder = new MessageHeaderDecoder(); + // SBE Message Header Decoder (reusable flyweight) + private final MessageHeaderDecoder headerDecoder = new MessageHeaderDecoder(); - // SBE Message Decoders (reusable flyweights) - private final TradeDecoder tradeDecoder = new TradeDecoder(); - private final QuoteDecoder quoteDecoder = new QuoteDecoder(); - private final MarketDepthDecoder marketDepthDecoder = new MarketDepthDecoder(); - private final OrderAckDecoder orderAckDecoder = new OrderAckDecoder(); - private final HeartbeatDecoder heartbeatDecoder = new HeartbeatDecoder(); + // SBE Message Decoders (reusable flyweights) + private final TradeDecoder tradeDecoder = new TradeDecoder(); + private final QuoteDecoder quoteDecoder = new QuoteDecoder(); + private final MarketDepthDecoder marketDepthDecoder = new MarketDepthDecoder(); + private final OrderAckDecoder orderAckDecoder = new OrderAckDecoder(); + private final HeartbeatDecoder heartbeatDecoder = new HeartbeatDecoder(); - // Statistics - private long messagesProcessed = 0; - private long messagesBroadcast = 0; - private long lastLogTime = System.currentTimeMillis(); + // Statistics + private long messagesProcessed = 0; + private long messagesBroadcast = 0; + private long lastLogTime = System.currentTimeMillis(); - // Sampling: Only broadcast 1 in N messages to avoid overwhelming browser - private static final int SAMPLE_RATE = 50; - private long sampleCounter = 0; + // Sampling: Only broadcast 1 in N messages to avoid overwhelming browser + private static final int SAMPLE_RATE = 50; + private long sampleCounter = 0; - @Inject - MarketDataBroadcaster broadcaster; + @Inject MarketDataBroadcaster broadcaster; - // Optimization: Reusable buffers to reduce allocation - private final byte[] symbolBuffer = new byte[128]; - private final StringBuilder sb = new StringBuilder(1024); + // Optimization: Reusable buffers to reduce allocation + private final byte[] symbolBuffer = new byte[128]; + private final StringBuilder sb = new StringBuilder(1024); - @Override - public void onFragment(DirectBuffer buffer, int offset, int length, Header header) { - try { - headerDecoder.wrap(buffer, offset); + @Override + public void onFragment(DirectBuffer buffer, int offset, int length, Header header) { + try { + headerDecoder.wrap(buffer, offset); - final int templateId = headerDecoder.templateId(); - final int actingBlockLength = headerDecoder.blockLength(); - final int actingVersion = headerDecoder.version(); + final int templateId = headerDecoder.templateId(); + final int actingBlockLength = headerDecoder.blockLength(); + final int actingVersion = headerDecoder.version(); - offset += headerDecoder.encodedLength(); + offset += headerDecoder.encodedLength(); - sampleCounter++; - final boolean shouldBroadcast = (sampleCounter % SAMPLE_RATE == 0); + sampleCounter++; + final boolean shouldBroadcast = (sampleCounter % SAMPLE_RATE == 0); - switch (templateId) { - case TradeDecoder.TEMPLATE_ID: - processTrade(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); - break; + switch (templateId) { + case TradeDecoder.TEMPLATE_ID: + processTrade(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); + break; - case QuoteDecoder.TEMPLATE_ID: - processQuote(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); - break; + case QuoteDecoder.TEMPLATE_ID: + processQuote(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); + break; - case MarketDepthDecoder.TEMPLATE_ID: - processMarketDepth(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); - break; + case MarketDepthDecoder.TEMPLATE_ID: + processMarketDepth(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); + break; - case OrderAckDecoder.TEMPLATE_ID: - processOrderAck(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); - break; + case OrderAckDecoder.TEMPLATE_ID: + processOrderAck(buffer, offset, actingBlockLength, actingVersion, shouldBroadcast); + break; - case HeartbeatDecoder.TEMPLATE_ID: - processHeartbeat(buffer, offset, actingBlockLength, actingVersion); - break; + case HeartbeatDecoder.TEMPLATE_ID: + processHeartbeat(buffer, offset, actingBlockLength, actingVersion); + break; - default: - LOGGER.warning("Unknown message template ID: " + templateId); - } + default: + LOGGER.warning("Unknown message template ID: " + templateId); + } - messagesProcessed++; - if (shouldBroadcast) { - messagesBroadcast++; - } - logStatistics(); + messagesProcessed++; + if (shouldBroadcast) { + messagesBroadcast++; + } + logStatistics(); - } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Error processing fragment", e); - } + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Error processing fragment", e); } + } - /** - * Process Trade message using SBE decoder (zero-copy) - */ - private void processTrade(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { - if (!shouldBroadcast) { - return; - } - - tradeDecoder.wrap(buffer, offset, blockLength, version); - - // Extract fields from SBE decoder - final long timestamp = tradeDecoder.timestamp(); - final long tradeId = tradeDecoder.tradeId(); - final long price = tradeDecoder.price(); - final long quantity = tradeDecoder.quantity(); - final Side side = tradeDecoder.side(); - - // Extract variable-length symbol string using reusable buffer - final int symbolLength = tradeDecoder.symbolLength(); - tradeDecoder.getSymbol(symbolBuffer, 0, symbolLength); - final String symbol = new String(symbolBuffer, 0, symbolLength); - - sb.setLength(0); - sb.append("{\"type\":\"trade\",\"timestamp\":").append(timestamp) - .append(",\"tradeId\":").append(tradeId) - .append(",\"symbol\":\"").append(symbol).append("\"") - .append(",\"price\":").append(price / 10000.0) - .append(",\"quantity\":").append(quantity) - .append(",\"side\":\"").append(side).append("\"}"); - - broadcaster.broadcast(sb.toString()); + /** Process Trade message using SBE decoder (zero-copy) */ + private void processTrade( + DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; } - /** - * Process Quote message using SBE decoder (zero-copy) - */ - private void processQuote(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { - if (!shouldBroadcast) { - return; - } - - quoteDecoder.wrap(buffer, offset, blockLength, version); - - final long timestamp = quoteDecoder.timestamp(); - final long bidPrice = quoteDecoder.bidPrice(); - final long bidSize = quoteDecoder.bidSize(); - final long askPrice = quoteDecoder.askPrice(); - final long askSize = quoteDecoder.askSize(); - - final int symbolLength = quoteDecoder.symbolLength(); - quoteDecoder.getSymbol(symbolBuffer, 0, symbolLength); - final String symbol = new String(symbolBuffer, 0, symbolLength); - - sb.setLength(0); - sb.append("{\"type\":\"quote\",\"timestamp\":").append(timestamp) - .append(",\"symbol\":\"").append(symbol).append("\"") - .append(",\"bid\":{\"price\":").append(bidPrice / 10000.0).append(",\"size\":").append(bidSize).append("}") - .append(",\"ask\":{\"price\":").append(askPrice / 10000.0).append(",\"size\":").append(askSize).append("}}"); - - broadcaster.broadcast(sb.toString()); + tradeDecoder.wrap(buffer, offset, blockLength, version); + + // Extract fields from SBE decoder + final long timestamp = tradeDecoder.timestamp(); + final long tradeId = tradeDecoder.tradeId(); + final long price = tradeDecoder.price(); + final long quantity = tradeDecoder.quantity(); + final Side side = tradeDecoder.side(); + + // Extract variable-length symbol string using reusable buffer + final int symbolLength = tradeDecoder.symbolLength(); + tradeDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); + + sb.setLength(0); + sb.append("{\"type\":\"trade\",\"timestamp\":") + .append(timestamp) + .append(",\"tradeId\":") + .append(tradeId) + .append(",\"symbol\":\"") + .append(symbol) + .append("\"") + .append(",\"price\":") + .append(price / 10000.0) + .append(",\"quantity\":") + .append(quantity) + .append(",\"side\":\"") + .append(side) + .append("\"}"); + + broadcaster.broadcast(sb.toString()); + } + + /** Process Quote message using SBE decoder (zero-copy) */ + private void processQuote( + DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; } - /** - * Process MarketDepth message with repeating groups (zero-copy) - */ - private void processMarketDepth(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { - if (!shouldBroadcast) { - return; - } - - marketDepthDecoder.wrap(buffer, offset, blockLength, version); - - final long timestamp = marketDepthDecoder.timestamp(); - final long sequenceNumber = marketDepthDecoder.sequenceNumber(); - - sb.setLength(0); - sb.append("{\"type\":\"depth\",\"timestamp\":").append(timestamp) - .append(",\"sequence\":").append(sequenceNumber) - .append(",\"bids\":["); - - MarketDepthDecoder.BidsDecoder bids = marketDepthDecoder.bids(); - int bidCount = 0; - while (bids.hasNext()) { - bids.next(); - if (bidCount > 0) sb.append(","); - sb.append("{\"price\":").append(bids.price() / 10000.0) - .append(",\"quantity\":").append(bids.quantity()).append("}"); - bidCount++; - } - sb.append("],\"asks\":["); - - MarketDepthDecoder.AsksDecoder asks = marketDepthDecoder.asks(); - int askCount = 0; - while (asks.hasNext()) { - asks.next(); - if (askCount > 0) sb.append(","); - sb.append("{\"price\":").append(asks.price() / 10000.0) - .append(",\"quantity\":").append(asks.quantity()).append("}"); - askCount++; - } - sb.append("]"); - - // Must extract symbol after traversing groups in SBE for correct position in buffer - final int symbolLength = marketDepthDecoder.symbolLength(); - marketDepthDecoder.getSymbol(symbolBuffer, 0, symbolLength); - final String symbol = new String(symbolBuffer, 0, symbolLength); - - sb.append(",\"symbol\":\"").append(symbol).append("\"}"); - - broadcaster.broadcast(sb.toString()); + quoteDecoder.wrap(buffer, offset, blockLength, version); + + final long timestamp = quoteDecoder.timestamp(); + final long bidPrice = quoteDecoder.bidPrice(); + final long bidSize = quoteDecoder.bidSize(); + final long askPrice = quoteDecoder.askPrice(); + final long askSize = quoteDecoder.askSize(); + + final int symbolLength = quoteDecoder.symbolLength(); + quoteDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); + + sb.setLength(0); + sb.append("{\"type\":\"quote\",\"timestamp\":") + .append(timestamp) + .append(",\"symbol\":\"") + .append(symbol) + .append("\"") + .append(",\"bid\":{\"price\":") + .append(bidPrice / 10000.0) + .append(",\"size\":") + .append(bidSize) + .append("}") + .append(",\"ask\":{\"price\":") + .append(askPrice / 10000.0) + .append(",\"size\":") + .append(askSize) + .append("}}"); + + broadcaster.broadcast(sb.toString()); + } + + /** Process MarketDepth message with repeating groups (zero-copy) */ + private void processMarketDepth( + DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; } - /** - * Process OrderAck message - */ - private void processOrderAck(DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { - if (!shouldBroadcast) { - return; - } - - orderAckDecoder.wrap(buffer, offset, blockLength, version); - - final long timestamp = orderAckDecoder.timestamp(); - final long orderId = orderAckDecoder.orderId(); - final long clientOrderId = orderAckDecoder.clientOrderId(); - final Side side = orderAckDecoder.side(); - final OrderType orderType = orderAckDecoder.orderType(); - final long price = orderAckDecoder.price(); - final long quantity = orderAckDecoder.quantity(); - final ExecType execType = orderAckDecoder.execType(); - final long leavesQty = orderAckDecoder.leavesQty(); - final long cumQty = orderAckDecoder.cumQty(); - - final int symbolLength = orderAckDecoder.symbolLength(); - orderAckDecoder.getSymbol(symbolBuffer, 0, symbolLength); - final String symbol = new String(symbolBuffer, 0, symbolLength); - - sb.setLength(0); - sb.append("{\"type\":\"orderAck\",\"timestamp\":").append(timestamp) - .append(",\"orderId\":").append(orderId) - .append(",\"clientOrderId\":").append(clientOrderId) - .append(",\"symbol\":\"").append(symbol).append("\"") - .append(",\"side\":\"").append(side).append("\"") - .append(",\"orderType\":\"").append(orderType).append("\"") - .append(",\"price\":").append(price / 10000.0) - .append(",\"quantity\":").append(quantity) - .append(",\"execType\":\"").append(execType).append("\"") - .append(",\"leavesQty\":").append(leavesQty) - .append(",\"cumQty\":").append(cumQty).append("}"); - - broadcaster.broadcast(sb.toString()); + marketDepthDecoder.wrap(buffer, offset, blockLength, version); + + final long timestamp = marketDepthDecoder.timestamp(); + final long sequenceNumber = marketDepthDecoder.sequenceNumber(); + + sb.setLength(0); + sb.append("{\"type\":\"depth\",\"timestamp\":") + .append(timestamp) + .append(",\"sequence\":") + .append(sequenceNumber) + .append(",\"bids\":["); + + MarketDepthDecoder.BidsDecoder bids = marketDepthDecoder.bids(); + int bidCount = 0; + while (bids.hasNext()) { + bids.next(); + if (bidCount > 0) sb.append(","); + sb.append("{\"price\":") + .append(bids.price() / 10000.0) + .append(",\"quantity\":") + .append(bids.quantity()) + .append("}"); + bidCount++; } + sb.append("],\"asks\":["); + + MarketDepthDecoder.AsksDecoder asks = marketDepthDecoder.asks(); + int askCount = 0; + while (asks.hasNext()) { + asks.next(); + if (askCount > 0) sb.append(","); + sb.append("{\"price\":") + .append(asks.price() / 10000.0) + .append(",\"quantity\":") + .append(asks.quantity()) + .append("}"); + askCount++; + } + sb.append("]"); + + // Must extract symbol after traversing groups in SBE for correct position in buffer + final int symbolLength = marketDepthDecoder.symbolLength(); + marketDepthDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); - /** - * Process Heartbeat message - */ - private void processHeartbeat(DirectBuffer buffer, int offset, int blockLength, int version) { - heartbeatDecoder.wrap(buffer, offset, blockLength, version); + sb.append(",\"symbol\":\"").append(symbol).append("\"}"); - final long timestamp = heartbeatDecoder.timestamp(); - final long sequenceNumber = heartbeatDecoder.sequenceNumber(); + broadcaster.broadcast(sb.toString()); + } - // Log heartbeats but don't broadcast them - if (LOGGER.isLoggable(Level.FINE)) { - LOGGER.fine("Heartbeat: timestamp=" + timestamp + ", seq=" + sequenceNumber); - } + /** Process OrderAck message */ + private void processOrderAck( + DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { + if (!shouldBroadcast) { + return; } - /** - * Log statistics periodically - */ - private void logStatistics() { - long now = System.currentTimeMillis(); - if (now - lastLogTime > 5000) { // Log every 5 seconds - double elapsedSeconds = (now - lastLogTime) / 1000.0; - LOGGER.info(String.format( - "FragmentHandler Stats - Processed: %,d (%.0f msg/sec) | Broadcast to UI: %,d (%.0f msg/sec) | Sample rate: 1 in %d", - messagesProcessed, - messagesProcessed / elapsedSeconds, - messagesBroadcast, - messagesBroadcast / elapsedSeconds, - SAMPLE_RATE - )); - lastLogTime = now; - messagesProcessed = 0; - messagesBroadcast = 0; - } + orderAckDecoder.wrap(buffer, offset, blockLength, version); + + final long timestamp = orderAckDecoder.timestamp(); + final long orderId = orderAckDecoder.orderId(); + final long clientOrderId = orderAckDecoder.clientOrderId(); + final Side side = orderAckDecoder.side(); + final OrderType orderType = orderAckDecoder.orderType(); + final long price = orderAckDecoder.price(); + final long quantity = orderAckDecoder.quantity(); + final ExecType execType = orderAckDecoder.execType(); + final long leavesQty = orderAckDecoder.leavesQty(); + final long cumQty = orderAckDecoder.cumQty(); + + final int symbolLength = orderAckDecoder.symbolLength(); + orderAckDecoder.getSymbol(symbolBuffer, 0, symbolLength); + final String symbol = new String(symbolBuffer, 0, symbolLength); + + sb.setLength(0); + sb.append("{\"type\":\"orderAck\",\"timestamp\":") + .append(timestamp) + .append(",\"orderId\":") + .append(orderId) + .append(",\"clientOrderId\":") + .append(clientOrderId) + .append(",\"symbol\":\"") + .append(symbol) + .append("\"") + .append(",\"side\":\"") + .append(side) + .append("\"") + .append(",\"orderType\":\"") + .append(orderType) + .append("\"") + .append(",\"price\":") + .append(price / 10000.0) + .append(",\"quantity\":") + .append(quantity) + .append(",\"execType\":\"") + .append(execType) + .append("\"") + .append(",\"leavesQty\":") + .append(leavesQty) + .append(",\"cumQty\":") + .append(cumQty) + .append("}"); + + broadcaster.broadcast(sb.toString()); + } + + /** Process Heartbeat message */ + private void processHeartbeat(DirectBuffer buffer, int offset, int blockLength, int version) { + heartbeatDecoder.wrap(buffer, offset, blockLength, version); + + final long timestamp = heartbeatDecoder.timestamp(); + final long sequenceNumber = heartbeatDecoder.sequenceNumber(); + + // Log heartbeats but don't broadcast them + if (LOGGER.isLoggable(Level.FINE)) { + LOGGER.fine("Heartbeat: timestamp=" + timestamp + ", seq=" + sequenceNumber); + } + } + + /** Log statistics periodically */ + private void logStatistics() { + long now = System.currentTimeMillis(); + if (now - lastLogTime > 5000) { // Log every 5 seconds + double elapsedSeconds = (now - lastLogTime) / 1000.0; + LOGGER.info( + String.format( + "FragmentHandler Stats - Processed: %,d (%.0f msg/sec) | Broadcast to UI: %,d (%.0f msg/sec) | Sample rate: 1 in %d", + messagesProcessed, + messagesProcessed / elapsedSeconds, + messagesBroadcast, + messagesBroadcast / elapsedSeconds, + SAMPLE_RATE)); + lastLogTime = now; + messagesProcessed = 0; + messagesBroadcast = 0; } + } } diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index 972603a..84af2ec 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -1,7 +1,5 @@ package fish.payara.trader.aeron; -import java.util.concurrent.ThreadLocalRandom; - import com.hazelcast.core.HazelcastInstance; import com.hazelcast.cp.IAtomicLong; import fish.payara.trader.concurrency.VirtualThreadExecutor; @@ -9,22 +7,13 @@ import fish.payara.trader.websocket.MarketDataBroadcaster; import io.aeron.Aeron; import io.aeron.Publication; -import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; -import jakarta.ejb.DependsOn; -import jakarta.ejb.Singleton; -import jakarta.ejb.Startup; -import jakarta.enterprise.concurrent.ManagedExecutorDefinition; import jakarta.enterprise.concurrent.ManagedExecutorService; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Initialized; import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; -import org.agrona.concurrent.UnsafeBuffer; -import org.eclipse.microprofile.config.inject.ConfigProperty; - import java.nio.ByteBuffer; - import java.util.concurrent.Future; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; @@ -33,601 +22,614 @@ import java.util.concurrent.locks.LockSupport; import java.util.logging.Level; import java.util.logging.Logger; +import org.agrona.concurrent.UnsafeBuffer; +import org.eclipse.microprofile.config.inject.ConfigProperty; /** - * Market Data Publisher Simulator - * Generates synthetic market data and publishes via Aeron using SBE encoding. - * This simulates a high-frequency data feed for testing the ingestion pipeline. + * Market Data Publisher Simulator Generates synthetic market data and publishes via Aeron using SBE + * encoding. This simulates a high-frequency data feed for testing the ingestion pipeline. * IMPORTANT: Depends on AeronSubscriberBean to initialize first (provides MediaDriver) */ @ApplicationScoped public class MarketDataPublisher { - private static final Logger LOGGER = Logger.getLogger(MarketDataPublisher.class.getName()); - - private static final String CHANNEL = "aeron:ipc"; - private static final int STREAM_ID = 1001; - private static final int BUFFER_SIZE = 4096; - private static final int SAMPLE_RATE = 50; // Broadcast 1 in 50 messages to prevent flooding - - private static final String[] SYMBOLS = {"AAPL", "GOOGL", "MSFT", "AMZN", "TSLA", "NVDA", "META", "NFLX"}; - - private Aeron aeron; - private Publication publication; - - // SBE encoders (reusable flyweights) - private final MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); - private final TradeEncoder tradeEncoder = new TradeEncoder(); - private final QuoteEncoder quoteEncoder = new QuoteEncoder(); - private final MarketDepthEncoder marketDepthEncoder = new MarketDepthEncoder(); - private final HeartbeatEncoder heartbeatEncoder = new HeartbeatEncoder(); - - // Buffer for encoding messages - private final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(BUFFER_SIZE)); - - - private final AtomicLong sequenceNumber = new AtomicLong(0); - private final AtomicLong tradeIdGenerator = new AtomicLong(1000); - - private final AtomicLong messagesPublished = new AtomicLong(0); - private final AtomicInteger consecutiveFailures = new AtomicInteger(0); - private static final int MAX_CONSECUTIVE_FAILURES = 50; - private long sampleCounter = 0; - private volatile boolean initialized = false; - private volatile boolean running = false; - private boolean isDirectMode; - private long lastWarningLogTime = 0; - private static final long WARNING_LOG_INTERVAL_MS = 5000; // Log warnings at most once per 5 seconds - - private Future publisherFuture; - private Future statsFuture; - - @Inject - private AeronSubscriberBean subscriberBean; - - @Inject - @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") - private String ingestionMode; - - @Inject - @ConfigProperty(name = "ENABLE_PUBLISHER", defaultValue = "true") - String enablePublisherEnv; - - @Inject - private MarketDataBroadcaster broadcaster; - - @Inject - private HazelcastInstance hazelcastInstance; - - @Inject - @VirtualThreadExecutor - private ManagedExecutorService managedExecutorService; - - // Cluster-wide message counter (shared across all instances) - private IAtomicLong clusterMessageCounter; - - void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { -// managedExecutorService.submit(this::init); - init(); + private static final Logger LOGGER = Logger.getLogger(MarketDataPublisher.class.getName()); + + private static final String CHANNEL = "aeron:ipc"; + private static final int STREAM_ID = 1001; + private static final int BUFFER_SIZE = 4096; + private static final int SAMPLE_RATE = 50; // Broadcast 1 in 50 messages to prevent flooding + + private static final String[] SYMBOLS = { + "AAPL", "GOOGL", "MSFT", "AMZN", "TSLA", "NVDA", "META", "NFLX" + }; + + private Aeron aeron; + private Publication publication; + + // SBE encoders (reusable flyweights) + private final MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + private final TradeEncoder tradeEncoder = new TradeEncoder(); + private final QuoteEncoder quoteEncoder = new QuoteEncoder(); + private final MarketDepthEncoder marketDepthEncoder = new MarketDepthEncoder(); + private final HeartbeatEncoder heartbeatEncoder = new HeartbeatEncoder(); + + // Buffer for encoding messages + private final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(BUFFER_SIZE)); + + private final AtomicLong sequenceNumber = new AtomicLong(0); + private final AtomicLong tradeIdGenerator = new AtomicLong(1000); + + private final AtomicLong messagesPublished = new AtomicLong(0); + private final AtomicInteger consecutiveFailures = new AtomicInteger(0); + private static final int MAX_CONSECUTIVE_FAILURES = 50; + private long sampleCounter = 0; + private volatile boolean initialized = false; + private volatile boolean running = false; + private boolean isDirectMode; + private long lastWarningLogTime = 0; + private static final long WARNING_LOG_INTERVAL_MS = + 5000; // Log warnings at most once per 5 seconds + + private Future publisherFuture; + private Future statsFuture; + + @Inject private AeronSubscriberBean subscriberBean; + + @Inject + @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") + private String ingestionMode; + + @Inject + @ConfigProperty(name = "ENABLE_PUBLISHER", defaultValue = "true") + String enablePublisherEnv; + + @Inject private MarketDataBroadcaster broadcaster; + + @Inject private HazelcastInstance hazelcastInstance; + + @Inject @VirtualThreadExecutor private ManagedExecutorService managedExecutorService; + + // Cluster-wide message counter (shared across all instances) + private IAtomicLong clusterMessageCounter; + + void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { + // managedExecutorService.submit(this::init); + init(); + } + + public void init() { + // Initialize cluster-wide message counter on ALL instances (even non-publishers) + // This allows all instances to read the shared counter value via Hazelcast CP Subsystem + if (hazelcastInstance != null) { + try { + clusterMessageCounter = + hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count"); + LOGGER.info("Initialized cluster-wide message counter"); + } catch (Exception e) { + LOGGER.log( + Level.WARNING, + "Failed to initialize cluster message counter, using local counter only", + e); + } } - public void init() { - // Initialize cluster-wide message counter on ALL instances (even non-publishers) - // This allows all instances to read the shared counter value via Hazelcast CP Subsystem - if (hazelcastInstance != null) { - try { - clusterMessageCounter = hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count"); - LOGGER.info("Initialized cluster-wide message counter"); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to initialize cluster message counter, using local counter only", e); - } - } - - // Check if publisher should be enabled on this instance - if (enablePublisherEnv != null && !"true".equalsIgnoreCase(enablePublisherEnv)) { - LOGGER.info("Market Data Publisher DISABLED on this instance (ENABLE_PUBLISHER=" + enablePublisherEnv + ")"); - LOGGER.info("This instance will only consume messages from the cluster topic via Hazelcast"); - return; - } - - LOGGER.info("Initializing Market Data Publisher (ENABLE_PUBLISHER=" + enablePublisherEnv + "). Mode: " + ingestionMode); + // Check if publisher should be enabled on this instance + if (enablePublisherEnv != null && !"true".equalsIgnoreCase(enablePublisherEnv)) { + LOGGER.info( + "Market Data Publisher DISABLED on this instance (ENABLE_PUBLISHER=" + + enablePublisherEnv + + ")"); + LOGGER.info("This instance will only consume messages from the cluster topic via Hazelcast"); + return; + } - if ("DIRECT".equalsIgnoreCase(ingestionMode)) { - LOGGER.info("Running in DIRECT mode - Bypassing Aeron/SBE setup."); - initialized = true; - isDirectMode = true; + LOGGER.info( + "Initializing Market Data Publisher (ENABLE_PUBLISHER=" + + enablePublisherEnv + + "). Mode: " + + ingestionMode); - startPublishing(); - return; - } + if ("DIRECT".equalsIgnoreCase(ingestionMode)) { + LOGGER.info("Running in DIRECT mode - Bypassing Aeron/SBE setup."); + initialized = true; + isDirectMode = true; - try { - // Wait for AeronSubscriberBean to be ready (both observers fire at roughly same time) - LOGGER.info("Waiting for AeronSubscriberBean to be ready..."); - int waitAttempts = 0; - while (!subscriberBean.isReady() && waitAttempts < 60) { - Thread.sleep(500); - waitAttempts++; - } - - if (!subscriberBean.isReady()) { - LOGGER.severe("AeronSubscriberBean did not become ready in time"); - return; - } - - String aeronDir = subscriberBean.getAeronDirectoryName(); - LOGGER.info("Connecting to embedded MediaDriver at: " + aeronDir); - - aeron = Aeron.connect(new Aeron.Context() - .aeronDirectoryName(aeronDir) - .errorHandler(throwable -> - LOGGER.log(Level.SEVERE, "Aeron publisher error", throwable)) - ); - - LOGGER.info("Connected to Aeron. Adding publication..."); - - publication = aeron.addPublication(CHANNEL, STREAM_ID); - - LOGGER.info("Waiting for subscriber to connect to publication..."); - int attempts = 0; - while (!publication.isConnected() && attempts < 20) { - Thread.sleep(500); - attempts++; - } - - if (publication.isConnected()) { - LOGGER.info("Market Data Publisher initialized successfully"); - initialized = true; - - startPublishing(); - } else { - LOGGER.warning("Publisher not connected after waiting"); - } + startPublishing(); + return; + } - } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Failed to initialize publisher", e); - } + try { + // Wait for AeronSubscriberBean to be ready (both observers fire at roughly same time) + LOGGER.info("Waiting for AeronSubscriberBean to be ready..."); + int waitAttempts = 0; + while (!subscriberBean.isReady() && waitAttempts < 60) { + Thread.sleep(500); + waitAttempts++; + } + + if (!subscriberBean.isReady()) { + LOGGER.severe("AeronSubscriberBean did not become ready in time"); + return; + } + + String aeronDir = subscriberBean.getAeronDirectoryName(); + LOGGER.info("Connecting to embedded MediaDriver at: " + aeronDir); + + aeron = + Aeron.connect( + new Aeron.Context() + .aeronDirectoryName(aeronDir) + .errorHandler( + throwable -> LOGGER.log(Level.SEVERE, "Aeron publisher error", throwable))); + + LOGGER.info("Connected to Aeron. Adding publication..."); + + publication = aeron.addPublication(CHANNEL, STREAM_ID); + + LOGGER.info("Waiting for subscriber to connect to publication..."); + int attempts = 0; + while (!publication.isConnected() && attempts < 20) { + Thread.sleep(500); + attempts++; + } + + if (publication.isConnected()) { + LOGGER.info("Market Data Publisher initialized successfully"); + initialized = true; + + startPublishing(); + } else { + LOGGER.warning("Publisher not connected after waiting"); + } + + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Failed to initialize publisher", e); } + } - /** - * Start background thread to continuously publish market data at high throughput - */ - private void startPublishing() { - running = true; - publisherFuture = managedExecutorService.submit(() -> { - LOGGER.info("Market data publisher task started - targeting 50k-100k messages/sec with burst patterns"); + /** Start background thread to continuously publish market data at high throughput */ + private void startPublishing() { + running = true; + publisherFuture = + managedExecutorService.submit( + () -> { + LOGGER.info( + "Market data publisher task started - targeting 50k-100k messages/sec with burst patterns"); - final int BASE_BURST_SIZE = 500; - final long PARK_NANOS = 5_000; // 5 microseconds base rate + final int BASE_BURST_SIZE = 500; + final long PARK_NANOS = 5_000; // 5 microseconds base rate - while (running && !Thread.currentThread().isInterrupted()) { + while (running && !Thread.currentThread().isInterrupted()) { try { - // Apply burst multiplier for realistic market spikes - int burstMultiplier = getBurstMultiplier(); - int adjustedBurstSize = BASE_BURST_SIZE * burstMultiplier; - - // Log burst transitions - if (burstMultiplier > 1) { - long secondOfMinute = (System.currentTimeMillis() / 1000) % 60; - if (secondOfMinute == 20 || secondOfMinute == 45) { - LOGGER.info("BURST MODE: " + burstMultiplier + "x allocation spike started"); - } + // Apply burst multiplier for realistic market spikes + int burstMultiplier = getBurstMultiplier(); + int adjustedBurstSize = BASE_BURST_SIZE * burstMultiplier; + + // Log burst transitions + if (burstMultiplier > 1) { + long secondOfMinute = (System.currentTimeMillis() / 1000) % 60; + if (secondOfMinute == 20 || secondOfMinute == 45) { + LOGGER.info("BURST MODE: " + burstMultiplier + "x allocation spike started"); } + } - for (int i = 0; i < adjustedBurstSize && running; i++) { - publishTrade(); - publishQuote(); - publishMarketDepth(); + for (int i = 0; i < adjustedBurstSize && running; i++) { + publishTrade(); + publishQuote(); + publishMarketDepth(); - if (i % 100 == 0) { - publishHeartbeat(); - } + if (i % 100 == 0) { + publishHeartbeat(); } + } - LockSupport.parkNanos(PARK_NANOS); + LockSupport.parkNanos(PARK_NANOS); } catch (Throwable e) { - LOGGER.log(Level.SEVERE, "Critical error in publisher loop", e); - // Brief pause on error, then continue - LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); + LOGGER.log(Level.SEVERE, "Critical error in publisher loop", e); + // Brief pause on error, then continue + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); } - } + } - LOGGER.info("Market data publisher task stopped"); - }); + LOGGER.info("Market data publisher task stopped"); + }); - statsFuture = managedExecutorService.submit(() -> { - long lastCount = 0; - long lastTime = System.currentTimeMillis(); + statsFuture = + managedExecutorService.submit( + () -> { + long lastCount = 0; + long lastTime = System.currentTimeMillis(); - while (running && !Thread.currentThread().isInterrupted()) { + while (running && !Thread.currentThread().isInterrupted()) { try { - Thread.sleep(5000); - - long currentCount = messagesPublished.get(); - long currentTime = System.currentTimeMillis(); - long messagesSinceLastLog = currentCount - lastCount; - double elapsedSeconds = (currentTime - lastTime) / 1000.0; - double messagesPerSecond = messagesSinceLastLog / elapsedSeconds; - - int currentMultiplier = getBurstMultiplier(); - String burstStatus = currentMultiplier > 1 ? - " [BURST: " + currentMultiplier + "x]" : ""; - - LOGGER.info(String.format( - "Publisher Stats - Total: %,d | Last 5s: %,d (%.0f msg/sec)%s", - currentCount, messagesSinceLastLog, messagesPerSecond, burstStatus - )); - - String statsJson = String.format( - "{\"type\":\"stats\",\"total\":%d,\"rate\":%.0f,\"burstMultiplier\":%d}", - currentCount, messagesPerSecond, currentMultiplier - ); - broadcaster.broadcast(statsJson); - - lastCount = currentCount; - lastTime = currentTime; + Thread.sleep(5000); + + long currentCount = messagesPublished.get(); + long currentTime = System.currentTimeMillis(); + long messagesSinceLastLog = currentCount - lastCount; + double elapsedSeconds = (currentTime - lastTime) / 1000.0; + double messagesPerSecond = messagesSinceLastLog / elapsedSeconds; + + int currentMultiplier = getBurstMultiplier(); + String burstStatus = + currentMultiplier > 1 ? " [BURST: " + currentMultiplier + "x]" : ""; + + LOGGER.info( + String.format( + "Publisher Stats - Total: %,d | Last 5s: %,d (%.0f msg/sec)%s", + currentCount, messagesSinceLastLog, messagesPerSecond, burstStatus)); + + String statsJson = + String.format( + "{\"type\":\"stats\",\"total\":%d,\"rate\":%.0f,\"burstMultiplier\":%d}", + currentCount, messagesPerSecond, currentMultiplier); + broadcaster.broadcast(statsJson); + + lastCount = currentCount; + lastTime = currentTime; } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; + Thread.currentThread().interrupt(); + break; } - } - }); + } + }); + } + + /** + * Calculate burst multiplier based on time pattern simulating market events + * + *

Pattern (60-second cycle): - 00-20s: Normal trading (1x) - 20-25s: News event burst (5x + * allocation spike) - 25-45s: Normal trading (1x) - 45-50s: Market close spike (3x allocation) - + * 50-60s: Normal trading (1x) + */ + private int getBurstMultiplier() { + long secondOfMinute = (System.currentTimeMillis() / 1000) % 60; + + if (secondOfMinute >= 20 && secondOfMinute < 25) { + return 5; // News event - 5x burst + } else if (secondOfMinute >= 45 && secondOfMinute < 50) { + return 3; // Market close - 3x spike } - /** - * Calculate burst multiplier based on time pattern simulating market events - * - * Pattern (60-second cycle): - * - 00-20s: Normal trading (1x) - * - 20-25s: News event burst (5x allocation spike) - * - 25-45s: Normal trading (1x) - * - 45-50s: Market close spike (3x allocation) - * - 50-60s: Normal trading (1x) - */ - private int getBurstMultiplier() { - long secondOfMinute = (System.currentTimeMillis() / 1000) % 60; - - if (secondOfMinute >= 20 && secondOfMinute < 25) { - return 5; // News event - 5x burst - } else if (secondOfMinute >= 45 && secondOfMinute < 50) { - return 3; // Market close - 3x spike + return 1; // Normal + } + + /** Publish a Trade message */ + private void publishTrade() { + if (isDirectMode) { + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); // Use ThreadLocalRandom + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double price = 100.0 + currentRandom.nextDouble() * 400.0; + final int quantity = currentRandom.nextInt(1000) + 100; + final String side = currentRandom.nextBoolean() ? "BUY" : "SELL"; + + String json = + String.format( + "{\"type\":\"trade\",\"timestamp\":%d,\"tradeId\":%d,\"symbol\":\"%s\",\"price\":%.4f,\"quantity\":%d,\"side\":\"%s\"}", + System.currentTimeMillis(), + tradeIdGenerator.incrementAndGet(), + symbol, + price, + quantity, + side); + if (++sampleCounter % SAMPLE_RATE == 0) { + broadcaster.broadcastWithArtificialLoad(json); + } + messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical } - - return 1; // Normal + } + return; } - /** - * Publish a Trade message - */ - private void publishTrade() { - if (isDirectMode) { - final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); // Use ThreadLocalRandom - final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; - final double price = 100.0 + currentRandom.nextDouble() * 400.0; - final int quantity = currentRandom.nextInt(1000) + 100; - final String side = currentRandom.nextBoolean() ? "BUY" : "SELL"; - - String json = String.format( - "{\"type\":\"trade\",\"timestamp\":%d,\"tradeId\":%d,\"symbol\":\"%s\",\"price\":%.4f,\"quantity\":%d,\"side\":\"%s\"}", - System.currentTimeMillis(), - tradeIdGenerator.incrementAndGet(), - symbol, - price, - quantity, - side - ); - if (++sampleCounter % SAMPLE_RATE == 0) { - broadcaster.broadcastWithArtificialLoad(json); - } - messagesPublished.incrementAndGet(); - // Increment cluster-wide counter - if (clusterMessageCounter != null) { - try { - clusterMessageCounter.incrementAndGet(); - } catch (Exception e) { - // Ignore cluster counter errors, not critical - } - } - return; + int bufferOffset = 0; + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + + headerEncoder + .wrap(buffer, bufferOffset) + .blockLength(tradeEncoder.sbeBlockLength()) + .templateId(tradeEncoder.sbeTemplateId()) + .schemaId(tradeEncoder.sbeSchemaId()) + .version(tradeEncoder.sbeSchemaVersion()); + + bufferOffset += headerEncoder.encodedLength(); + + tradeEncoder + .wrap(buffer, bufferOffset) + .timestamp(System.currentTimeMillis()) + .tradeId(tradeIdGenerator.incrementAndGet()) + .price((long) ((100.0 + currentRandom.nextDouble() * 400.0) * 10000)) // $100-$500 + .quantity(currentRandom.nextInt(1000) + 100) + .side(currentRandom.nextBoolean() ? Side.BUY : Side.SELL) + .symbol(symbol); + + final int length = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); + offer(buffer, 0, length, "Trade"); + } + + /** Publish a Quote message */ + private void publishQuote() { + if (isDirectMode) { + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; + final double bidPrice = basePrice - 0.01; + final double askPrice = basePrice + 0.01; + final int bidSize = currentRandom.nextInt(10000) + 100; + final int askSize = currentRandom.nextInt(10000) + 100; + + String json = + String.format( + "{\"type\":\"quote\",\"timestamp\":%d,\"symbol\":\"%s\",\"bid\":{\"price\":%.4f,\"size\":%d},\"ask\":{\"price\":%.4f,\"size\":%d}}", + System.currentTimeMillis(), symbol, bidPrice, bidSize, askPrice, askSize); + if (++sampleCounter % SAMPLE_RATE == 0) { + broadcaster.broadcastWithArtificialLoad(json); + } + messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical } - - int bufferOffset = 0; - final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); - final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; - - headerEncoder.wrap(buffer, bufferOffset) - .blockLength(tradeEncoder.sbeBlockLength()) - .templateId(tradeEncoder.sbeTemplateId()) - .schemaId(tradeEncoder.sbeSchemaId()) - .version(tradeEncoder.sbeSchemaVersion()); - - bufferOffset += headerEncoder.encodedLength(); - - tradeEncoder.wrap(buffer, bufferOffset) - .timestamp(System.currentTimeMillis()) - .tradeId(tradeIdGenerator.incrementAndGet()) - .price((long) ((100.0 + currentRandom.nextDouble() * 400.0) * 10000)) // $100-$500 - .quantity(currentRandom.nextInt(1000) + 100) - .side(currentRandom.nextBoolean() ? Side.BUY : Side.SELL) - .symbol(symbol); - - final int length = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); - offer(buffer, 0, length, "Trade"); + } + return; } - /** - * Publish a Quote message - */ - private void publishQuote() { - if (isDirectMode) { - final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); - final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; - final double bidPrice = basePrice - 0.01; - final double askPrice = basePrice + 0.01; - final int bidSize = currentRandom.nextInt(10000) + 100; - final int askSize = currentRandom.nextInt(10000) + 100; - - String json = String.format( - "{\"type\":\"quote\",\"timestamp\":%d,\"symbol\":\"%s\",\"bid\":{\"price\":%.4f,\"size\":%d},\"ask\":{\"price\":%.4f,\"size\":%d}}", - System.currentTimeMillis(), symbol, - bidPrice, bidSize, - askPrice, askSize - ); - if (++sampleCounter % SAMPLE_RATE == 0) { - broadcaster.broadcastWithArtificialLoad(json); - } - messagesPublished.incrementAndGet(); - // Increment cluster-wide counter - if (clusterMessageCounter != null) { - try { - clusterMessageCounter.incrementAndGet(); - } catch (Exception e) { - // Ignore cluster counter errors, not critical - } - } - return; + int bufferOffset = 0; + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; + + headerEncoder + .wrap(buffer, bufferOffset) + .blockLength(quoteEncoder.sbeBlockLength()) + .templateId(quoteEncoder.sbeTemplateId()) + .schemaId(quoteEncoder.sbeSchemaId()) + .version(quoteEncoder.sbeSchemaVersion()); + + bufferOffset += headerEncoder.encodedLength(); + + quoteEncoder + .wrap(buffer, bufferOffset) + .timestamp(System.currentTimeMillis()) + .bidPrice((long) ((basePrice - 0.01) * 10000)) + .bidSize(currentRandom.nextInt(10000) + 100) + .askPrice((long) ((basePrice + 0.01) * 10000)) + .askSize(currentRandom.nextInt(10000) + 100) + .symbol(symbol); + + final int length = headerEncoder.encodedLength() + quoteEncoder.encodedLength(); + offer(buffer, 0, length, "Quote"); + } + + /** Publish a MarketDepth message */ + private void publishMarketDepth() { + if (isDirectMode) { + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; + final long timestamp = System.currentTimeMillis(); + final long seq = sequenceNumber.incrementAndGet(); + + StringBuilder bidsJson = new StringBuilder("["); + for (int i = 0; i < 5; i++) { + if (i > 0) bidsJson.append(","); + bidsJson.append( + String.format( + "{\"price\":%.4f,\"quantity\":%d}", + basePrice - (i + 1) * 0.01, currentRandom.nextInt(5000) + 100)); + } + bidsJson.append("]"); + + StringBuilder asksJson = new StringBuilder("["); + for (int i = 0; i < 5; i++) { + if (i > 0) asksJson.append(","); + asksJson.append( + String.format( + "{\"price\":%.4f,\"quantity\":%d}", + basePrice + (i + 1) * 0.01, currentRandom.nextInt(5000) + 100)); + } + asksJson.append("]"); + + String json = + String.format( + "{\"type\":\"depth\",\"timestamp\":%d,\"symbol\":\"%s\",\"sequence\":%d,\"bids\":%s,\"asks\":%s}", + timestamp, symbol, seq, bidsJson, asksJson); + if (++sampleCounter % SAMPLE_RATE == 0) { + broadcaster.broadcastWithArtificialLoad(json); + } + messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical } - - int bufferOffset = 0; - final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); - final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; - - headerEncoder.wrap(buffer, bufferOffset) - .blockLength(quoteEncoder.sbeBlockLength()) - .templateId(quoteEncoder.sbeTemplateId()) - .schemaId(quoteEncoder.sbeSchemaId()) - .version(quoteEncoder.sbeSchemaVersion()); - - bufferOffset += headerEncoder.encodedLength(); - - quoteEncoder.wrap(buffer, bufferOffset) - .timestamp(System.currentTimeMillis()) - .bidPrice((long) ((basePrice - 0.01) * 10000)) - .bidSize(currentRandom.nextInt(10000) + 100) - .askPrice((long) ((basePrice + 0.01) * 10000)) - .askSize(currentRandom.nextInt(10000) + 100) - .symbol(symbol); - - final int length = headerEncoder.encodedLength() + quoteEncoder.encodedLength(); - offer(buffer, 0, length, "Quote"); + } + return; } - /** - * Publish a MarketDepth message - */ - private void publishMarketDepth() { - if (isDirectMode) { - final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); - final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; - final long timestamp = System.currentTimeMillis(); - final long seq = sequenceNumber.incrementAndGet(); - - StringBuilder bidsJson = new StringBuilder("["); - for (int i = 0; i < 5; i++) { - if (i > 0) bidsJson.append(","); - bidsJson.append(String.format("{\"price\":%.4f,\"quantity\":%d}", - basePrice - (i + 1) * 0.01, currentRandom.nextInt(5000) + 100)); - } - bidsJson.append("]"); - - StringBuilder asksJson = new StringBuilder("["); - for (int i = 0; i < 5; i++) { - if (i > 0) asksJson.append(","); - asksJson.append(String.format("{\"price\":%.4f,\"quantity\":%d}", - basePrice + (i + 1) * 0.01, currentRandom.nextInt(5000) + 100)); - } - asksJson.append("]"); - - String json = String.format( - "{\"type\":\"depth\",\"timestamp\":%d,\"symbol\":\"%s\",\"sequence\":%d,\"bids\":%s,\"asks\":%s}", - timestamp, symbol, seq, bidsJson, asksJson - ); - if (++sampleCounter % SAMPLE_RATE == 0) { - broadcaster.broadcastWithArtificialLoad(json); - } - messagesPublished.incrementAndGet(); - // Increment cluster-wide counter - if (clusterMessageCounter != null) { - try { - clusterMessageCounter.incrementAndGet(); - } catch (Exception e) { - // Ignore cluster counter errors, not critical - } - } - return; - } - - int bufferOffset = 0; - final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); - final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; - final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; - - headerEncoder.wrap(buffer, bufferOffset) - .blockLength(marketDepthEncoder.sbeBlockLength()) - .templateId(marketDepthEncoder.sbeTemplateId()) - .schemaId(marketDepthEncoder.sbeSchemaId()) - .version(marketDepthEncoder.sbeSchemaVersion()); - - bufferOffset += headerEncoder.encodedLength(); + int bufferOffset = 0; + final ThreadLocalRandom currentRandom = ThreadLocalRandom.current(); + final String symbol = SYMBOLS[currentRandom.nextInt(SYMBOLS.length)]; + final double basePrice = 100.0 + currentRandom.nextDouble() * 400.0; + + headerEncoder + .wrap(buffer, bufferOffset) + .blockLength(marketDepthEncoder.sbeBlockLength()) + .templateId(marketDepthEncoder.sbeTemplateId()) + .schemaId(marketDepthEncoder.sbeSchemaId()) + .version(marketDepthEncoder.sbeSchemaVersion()); + + bufferOffset += headerEncoder.encodedLength(); + + marketDepthEncoder + .wrap(buffer, bufferOffset) + .timestamp(System.currentTimeMillis()) + .sequenceNumber(sequenceNumber.incrementAndGet()); + + MarketDepthEncoder.BidsEncoder bidsEncoder = marketDepthEncoder.bidsCount(5); + for (int i = 0; i < 5; i++) { + bidsEncoder + .next() + .price((long) ((basePrice - (i + 1) * 0.01) * 10000)) + .quantity(currentRandom.nextInt(5000) + 100); + } - marketDepthEncoder.wrap(buffer, bufferOffset) - .timestamp(System.currentTimeMillis()) - .sequenceNumber(sequenceNumber.incrementAndGet()); + MarketDepthEncoder.AsksEncoder asksEncoder = marketDepthEncoder.asksCount(5); + for (int i = 0; i < 5; i++) { + asksEncoder + .next() + .price((long) ((basePrice + (i + 1) * 0.01) * 10000)) + .quantity(currentRandom.nextInt(5000) + 100); + } - MarketDepthEncoder.BidsEncoder bidsEncoder = marketDepthEncoder.bidsCount(5); - for (int i = 0; i < 5; i++) { - bidsEncoder.next() - .price((long) ((basePrice - (i + 1) * 0.01) * 10000)) - .quantity(currentRandom.nextInt(5000) + 100); - } + marketDepthEncoder.symbol(symbol); - MarketDepthEncoder.AsksEncoder asksEncoder = marketDepthEncoder.asksCount(5); - for (int i = 0; i < 5; i++) { - asksEncoder.next() - .price((long) ((basePrice + (i + 1) * 0.01) * 10000)) - .quantity(currentRandom.nextInt(5000) + 100); - } + final int length = headerEncoder.encodedLength() + marketDepthEncoder.encodedLength(); + offer(buffer, 0, length, "MarketDepth"); + } - marketDepthEncoder.symbol(symbol); - - final int length = headerEncoder.encodedLength() + marketDepthEncoder.encodedLength(); - offer(buffer, 0, length, "MarketDepth"); + /** Publish a Heartbeat message */ + private void publishHeartbeat() { + if (isDirectMode) { + // In DIRECT mode, we just increment counter but don't broadcast heartbeats to UI + // as they are mainly for system health checks in the Aeron log + messagesPublished.incrementAndGet(); + return; } - /** - * Publish a Heartbeat message - */ - private void publishHeartbeat() { - if (isDirectMode) { - // In DIRECT mode, we just increment counter but don't broadcast heartbeats to UI - // as they are mainly for system health checks in the Aeron log - messagesPublished.incrementAndGet(); - return; - } + int bufferOffset = 0; - int bufferOffset = 0; + headerEncoder + .wrap(buffer, bufferOffset) + .blockLength(heartbeatEncoder.sbeBlockLength()) + .templateId(heartbeatEncoder.sbeTemplateId()) + .schemaId(heartbeatEncoder.sbeSchemaId()) + .version(heartbeatEncoder.sbeSchemaVersion()); - headerEncoder.wrap(buffer, bufferOffset) - .blockLength(heartbeatEncoder.sbeBlockLength()) - .templateId(heartbeatEncoder.sbeTemplateId()) - .schemaId(heartbeatEncoder.sbeSchemaId()) - .version(heartbeatEncoder.sbeSchemaVersion()); + bufferOffset += headerEncoder.encodedLength(); - bufferOffset += headerEncoder.encodedLength(); + heartbeatEncoder + .wrap(buffer, bufferOffset) + .timestamp(System.currentTimeMillis()) + .sequenceNumber(sequenceNumber.get()); - heartbeatEncoder.wrap(buffer, bufferOffset) - .timestamp(System.currentTimeMillis()) - .sequenceNumber(sequenceNumber.get()); + final int length = headerEncoder.encodedLength() + heartbeatEncoder.encodedLength(); + offer(buffer, 0, length, "Heartbeat"); + } - final int length = headerEncoder.encodedLength() + heartbeatEncoder.encodedLength(); - offer(buffer, 0, length, "Heartbeat"); - } + /** Offer buffer to Aeron publication with retry logic */ + private void offer(UnsafeBuffer buffer, int offset, int length, String messageType) { + long result; + int retries = 3; - /** - * Offer buffer to Aeron publication with retry logic - */ - private void offer(UnsafeBuffer buffer, int offset, int length, String messageType) { - long result; - int retries = 3; - - while (retries > 0) { - result = publication.offer(buffer, offset, length); - - if (result > 0) { - messagesPublished.incrementAndGet(); - // Increment cluster-wide counter - if (clusterMessageCounter != null) { - try { - clusterMessageCounter.incrementAndGet(); - } catch (Exception e) { - // Ignore cluster counter errors, not critical - } - } - consecutiveFailures.set(0); - return; - } else if (result == Publication.BACK_PRESSURED) { - // Back pressure is normal at high throughput, don't log - retries--; - try { - Thread.sleep(1); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return; - } - } else if (result == Publication.NOT_CONNECTED) { - logWarningRateLimited("Publication not connected"); - handlePublishFailure(messageType); - return; - } else { - logWarningRateLimited("Offer failed for " + messageType + ": " + result); - handlePublishFailure(messageType); - return; - } - } + while (retries > 0) { + result = publication.offer(buffer, offset, length); - logWarningRateLimited("Failed to publish " + messageType + " after retries"); + if (result > 0) { + messagesPublished.incrementAndGet(); + // Increment cluster-wide counter + if (clusterMessageCounter != null) { + try { + clusterMessageCounter.incrementAndGet(); + } catch (Exception e) { + // Ignore cluster counter errors, not critical + } + } + consecutiveFailures.set(0); + return; + } else if (result == Publication.BACK_PRESSURED) { + // Back pressure is normal at high throughput, don't log + retries--; + try { + Thread.sleep(1); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } else if (result == Publication.NOT_CONNECTED) { + logWarningRateLimited("Publication not connected"); + handlePublishFailure(messageType); + return; + } else { + logWarningRateLimited("Offer failed for " + messageType + ": " + result); handlePublishFailure(messageType); + return; + } } - private void logWarningRateLimited(String message) { - long now = System.currentTimeMillis(); - if (now - lastWarningLogTime >= WARNING_LOG_INTERVAL_MS) { - LOGGER.warning(message); - lastWarningLogTime = now; - } - } + logWarningRateLimited("Failed to publish " + messageType + " after retries"); + handlePublishFailure(messageType); + } - private void handlePublishFailure(String messageType) { - int failures = consecutiveFailures.incrementAndGet(); - if (failures >= MAX_CONSECUTIVE_FAILURES) { - LOGGER.severe(String.format( - "Circuit breaker triggered: %d consecutive publish failures (last attempted: %s). Stopping message generation.", - failures, messageType - )); - running = false; - } + private void logWarningRateLimited(String message) { + long now = System.currentTimeMillis(); + if (now - lastWarningLogTime >= WARNING_LOG_INTERVAL_MS) { + LOGGER.warning(message); + lastWarningLogTime = now; + } + } + + private void handlePublishFailure(String messageType) { + int failures = consecutiveFailures.incrementAndGet(); + if (failures >= MAX_CONSECUTIVE_FAILURES) { + LOGGER.severe( + String.format( + "Circuit breaker triggered: %d consecutive publish failures (last attempted: %s). Stopping message generation.", + failures, messageType)); + running = false; } + } - @PreDestroy - public void shutdown() { - LOGGER.info("Shutting down Market Data Publisher..."); + @PreDestroy + public void shutdown() { + LOGGER.info("Shutting down Market Data Publisher..."); - running = false; - - if (publisherFuture != null) { - publisherFuture.cancel(true); - } - - if (statsFuture != null) { - statsFuture.cancel(true); - } + running = false; - if (publication != null) { - publication.close(); - } - if (aeron != null) { - aeron.close(); - } + if (publisherFuture != null) { + publisherFuture.cancel(true); + } - LOGGER.info("Market Data Publisher shut down. Total messages published: " + messagesPublished.get()); + if (statsFuture != null) { + statsFuture.cancel(true); } - public long getMessagesPublished() { - return messagesPublished.get(); + if (publication != null) { + publication.close(); + } + if (aeron != null) { + aeron.close(); } - public long getClusterMessagesPublished() { - if (clusterMessageCounter != null) { - try { - return clusterMessageCounter.get(); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to read cluster message counter", e); - } - } - return 0; + LOGGER.info( + "Market Data Publisher shut down. Total messages published: " + messagesPublished.get()); + } + + public long getMessagesPublished() { + return messagesPublished.get(); + } + + public long getClusterMessagesPublished() { + if (clusterMessageCounter != null) { + try { + return clusterMessageCounter.get(); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to read cluster message counter", e); + } } + return 0; + } } diff --git a/src/main/java/fish/payara/trader/concurrency/ConcurrencyConfig.java b/src/main/java/fish/payara/trader/concurrency/ConcurrencyConfig.java index a9079aa..1317877 100644 --- a/src/main/java/fish/payara/trader/concurrency/ConcurrencyConfig.java +++ b/src/main/java/fish/payara/trader/concurrency/ConcurrencyConfig.java @@ -4,14 +4,12 @@ import jakarta.enterprise.context.ApplicationScoped; /** - * Configuration for Jakarta Concurrency resources. - * Defines a ManagedExecutorService that uses Virtual Threads (Project Loom). + * Configuration for Jakarta Concurrency resources. Defines a ManagedExecutorService that uses + * Virtual Threads (Project Loom). */ @ApplicationScoped @ManagedExecutorDefinition( name = "java:module/concurrent/VirtualThreadExecutor", virtual = true, - qualifiers = {VirtualThreadExecutor.class} -) -public class ConcurrencyConfig { -} + qualifiers = {VirtualThreadExecutor.class}) +public class ConcurrencyConfig {} diff --git a/src/main/java/fish/payara/trader/concurrency/VirtualThreadExecutor.java b/src/main/java/fish/payara/trader/concurrency/VirtualThreadExecutor.java index 10c85c6..eb1d993 100644 --- a/src/main/java/fish/payara/trader/concurrency/VirtualThreadExecutor.java +++ b/src/main/java/fish/payara/trader/concurrency/VirtualThreadExecutor.java @@ -6,16 +6,12 @@ import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; +import jakarta.inject.Qualifier; import java.lang.annotation.Retention; import java.lang.annotation.Target; -import jakarta.inject.Qualifier; - -/** - * Qualifier for Virtual Thread ManagedExecutorService - */ +/** Qualifier for Virtual Thread ManagedExecutorService */ @Qualifier @Retention(RUNTIME) @Target({METHOD, FIELD, PARAMETER, TYPE}) -public @interface VirtualThreadExecutor { -} +public @interface VirtualThreadExecutor {} diff --git a/src/main/java/fish/payara/trader/gc/GCStats.java b/src/main/java/fish/payara/trader/gc/GCStats.java index 23aa082..289d6cd 100644 --- a/src/main/java/fish/payara/trader/gc/GCStats.java +++ b/src/main/java/fish/payara/trader/gc/GCStats.java @@ -3,75 +3,145 @@ import java.util.List; public class GCStats { - private String gcName; - private long collectionCount; - private long collectionTime; - private long lastPauseDuration; - private List recentPauses; - private PausePercentiles percentiles; - private long totalMemory; - private long usedMemory; - private long freeMemory; - - public static class PausePercentiles { - private long p50; - private long p95; - private long p99; - private long p999; - private long max; - - public PausePercentiles() {} - - public PausePercentiles(long p50, long p95, long p99, long p999, long max) { - this.p50 = p50; - this.p95 = p95; - this.p99 = p99; - this.p999 = p999; - this.max = max; - } - - public long getP50() { return p50; } - public void setP50(long p50) { this.p50 = p50; } - - public long getP95() { return p95; } - public void setP95(long p95) { this.p95 = p95; } - - public long getP99() { return p99; } - public void setP99(long p99) { this.p99 = p99; } - - public long getP999() { return p999; } - public void setP999(long p999) { this.p999 = p999; } - - public long getMax() { return max; } - public void setMax(long max) { this.max = max; } + private String gcName; + private long collectionCount; + private long collectionTime; + private long lastPauseDuration; + private List recentPauses; + private PausePercentiles percentiles; + private long totalMemory; + private long usedMemory; + private long freeMemory; + + public static class PausePercentiles { + private long p50; + private long p95; + private long p99; + private long p999; + private long max; + + public PausePercentiles() {} + + public PausePercentiles(long p50, long p95, long p99, long p999, long max) { + this.p50 = p50; + this.p95 = p95; + this.p99 = p99; + this.p999 = p999; + this.max = max; } - public GCStats() {} + public long getP50() { + return p50; + } + + public void setP50(long p50) { + this.p50 = p50; + } + + public long getP95() { + return p95; + } + + public void setP95(long p95) { + this.p95 = p95; + } + + public long getP99() { + return p99; + } + + public void setP99(long p99) { + this.p99 = p99; + } + + public long getP999() { + return p999; + } + + public void setP999(long p999) { + this.p999 = p999; + } + + public long getMax() { + return max; + } + + public void setMax(long max) { + this.max = max; + } + } + + public GCStats() {} + + public String getGcName() { + return gcName; + } + + public void setGcName(String gcName) { + this.gcName = gcName; + } + + public long getCollectionCount() { + return collectionCount; + } + + public void setCollectionCount(long collectionCount) { + this.collectionCount = collectionCount; + } + + public long getCollectionTime() { + return collectionTime; + } + + public void setCollectionTime(long collectionTime) { + this.collectionTime = collectionTime; + } + + public long getLastPauseDuration() { + return lastPauseDuration; + } + + public void setLastPauseDuration(long lastPauseDuration) { + this.lastPauseDuration = lastPauseDuration; + } + + public List getRecentPauses() { + return recentPauses; + } - public String getGcName() { return gcName; } - public void setGcName(String gcName) { this.gcName = gcName; } + public void setRecentPauses(List recentPauses) { + this.recentPauses = recentPauses; + } - public long getCollectionCount() { return collectionCount; } - public void setCollectionCount(long collectionCount) { this.collectionCount = collectionCount; } + public PausePercentiles getPercentiles() { + return percentiles; + } - public long getCollectionTime() { return collectionTime; } - public void setCollectionTime(long collectionTime) { this.collectionTime = collectionTime; } + public void setPercentiles(PausePercentiles percentiles) { + this.percentiles = percentiles; + } - public long getLastPauseDuration() { return lastPauseDuration; } - public void setLastPauseDuration(long lastPauseDuration) { this.lastPauseDuration = lastPauseDuration; } + public long getTotalMemory() { + return totalMemory; + } - public List getRecentPauses() { return recentPauses; } - public void setRecentPauses(List recentPauses) { this.recentPauses = recentPauses; } + public void setTotalMemory(long totalMemory) { + this.totalMemory = totalMemory; + } - public PausePercentiles getPercentiles() { return percentiles; } - public void setPercentiles(PausePercentiles percentiles) { this.percentiles = percentiles; } + public long getUsedMemory() { + return usedMemory; + } - public long getTotalMemory() { return totalMemory; } - public void setTotalMemory(long totalMemory) { this.totalMemory = totalMemory; } + public void setUsedMemory(long usedMemory) { + this.usedMemory = usedMemory; + } - public long getUsedMemory() { return usedMemory; } - public void setUsedMemory(long usedMemory) { this.usedMemory = usedMemory; } + public long getFreeMemory() { + return freeMemory; + } - public long getFreeMemory() { return freeMemory; } - public void setFreeMemory(long freeMemory) { this.freeMemory = freeMemory; } + public void setFreeMemory(long freeMemory) { + this.freeMemory = freeMemory; + } } diff --git a/src/main/java/fish/payara/trader/gc/GCStatsService.java b/src/main/java/fish/payara/trader/gc/GCStatsService.java index 85315ae..5239af2 100644 --- a/src/main/java/fish/payara/trader/gc/GCStatsService.java +++ b/src/main/java/fish/payara/trader/gc/GCStatsService.java @@ -1,12 +1,8 @@ package fish.payara.trader.gc; +import com.sun.management.GarbageCollectionNotificationInfo; import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; - -import javax.management.Notification; -import javax.management.NotificationEmitter; -import javax.management.NotificationListener; -import javax.management.openmbean.CompositeData; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; @@ -15,147 +11,148 @@ import java.util.concurrent.ConcurrentLinkedDeque; import java.util.logging.Logger; import java.util.stream.Collectors; - -import com.sun.management.GarbageCollectionNotificationInfo; +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; +import javax.management.openmbean.CompositeData; @ApplicationScoped public class GCStatsService implements NotificationListener { - private static final Logger LOGGER = Logger.getLogger(GCStatsService.class.getName()); - private static final int MAX_PAUSE_HISTORY = 1000; - - private final Map> pauseHistory = new HashMap<>(); - - @PostConstruct - public void init() { - LOGGER.info("Initializing GC Notification Listener..."); - List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); - for (GarbageCollectorMXBean gcBean : gcBeans) { - LOGGER.info("Registering listener for GC Bean: " + gcBean.getName()); - if (gcBean instanceof NotificationEmitter) { - ((NotificationEmitter) gcBean).addNotificationListener(this, null, null); - } - } - } + private static final Logger LOGGER = Logger.getLogger(GCStatsService.class.getName()); + private static final int MAX_PAUSE_HISTORY = 1000; - @Override - public void handleNotification(Notification notification, Object handback) { - if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { - GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()); - - String gcName = info.getGcName(); - String gcAction = info.getGcAction(); - String gcCause = info.getGcCause(); - long duration = info.getGcInfo().getDuration(); - - // FILTERING LOGIC: - // Azul C4 exposes "GPGC" (Concurrent Cycle) and "GPGC Pauses" (STW Pauses). - // We MUST ignore "GPGC" because it reports cycle time (hundreds of ms) which is NOT a pause. - if ("GPGC".equals(gcName)) { - return; - } - - // Also ignore other known concurrent cycle beans if they appear - if (gcName.contains("Cycles") && !gcName.contains("Pauses")) { - return; - } - - // Only record if duration > 0 (sub-millisecond pauses might show as 0 or 1) - // Storing all for fidelity. - - ConcurrentLinkedDeque history = pauseHistory.computeIfAbsent( - gcName, k -> new ConcurrentLinkedDeque<>() - ); - - history.addLast(duration); - while (history.size() > MAX_PAUSE_HISTORY) { - history.removeFirst(); - } - - // Log significant pauses (> 10ms) - if (duration > 10) { - LOGGER.info(String.format("GC Pause detected: %s | Action: %s | Cause: %s | Duration: %d ms", - gcName, gcAction, gcCause, duration)); - } - } - } + private final Map> pauseHistory = new HashMap<>(); - public List collectGCStats() { - List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); - MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); - MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); - - List statsList = new ArrayList<>(); - - for (GarbageCollectorMXBean gcBean : gcBeans) { - String gcName = gcBean.getName(); - - // Exclude concurrent cycle collectors from the public stats to avoid confusion - if ("GPGC".equals(gcName) || (gcName.contains("Cycles") && !gcName.contains("Pauses"))) { - continue; - } - - GCStats stats = new GCStats(); - stats.setGcName(gcName); - stats.setCollectionCount(gcBean.getCollectionCount()); - stats.setCollectionTime(gcBean.getCollectionTime()); - - // Get recent pauses from accurate history - ConcurrentLinkedDeque history = pauseHistory.get(gcName); - if (history != null && !history.isEmpty()) { - List pauses = new ArrayList<>(history); - stats.setLastPauseDuration(pauses.get(pauses.size() - 1)); - - stats.setRecentPauses(pauses.subList( - Math.max(0, pauses.size() - 100), pauses.size() - )); - - // Calculate percentiles - List sortedPauses = pauses.stream() - .sorted() - .collect(Collectors.toList()); - - stats.setPercentiles(calculatePercentiles(sortedPauses)); - } else { - stats.setLastPauseDuration(0); - stats.setRecentPauses(Collections.emptyList()); - stats.setPercentiles(new GCStats.PausePercentiles(0, 0, 0, 0, 0)); - } - - // Memory stats - stats.setTotalMemory(heapUsage.getMax()); - stats.setUsedMemory(heapUsage.getUsed()); - stats.setFreeMemory(heapUsage.getMax() - heapUsage.getUsed()); - - statsList.add(stats); - } - - return statsList; + @PostConstruct + public void init() { + LOGGER.info("Initializing GC Notification Listener..."); + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + for (GarbageCollectorMXBean gcBean : gcBeans) { + LOGGER.info("Registering listener for GC Bean: " + gcBean.getName()); + if (gcBean instanceof NotificationEmitter) { + ((NotificationEmitter) gcBean).addNotificationListener(this, null, null); + } } - - private GCStats.PausePercentiles calculatePercentiles(List sortedPauses) { - if (sortedPauses.isEmpty()) { - return new GCStats.PausePercentiles(0, 0, 0, 0, 0); - } - - int size = sortedPauses.size(); - return new GCStats.PausePercentiles( - percentile(sortedPauses, 0.50), - percentile(sortedPauses, 0.95), - percentile(sortedPauses, 0.99), - percentile(sortedPauses, 0.999), - sortedPauses.get(size - 1) - ); + } + + @Override + public void handleNotification(Notification notification, Object handback) { + if (notification + .getType() + .equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { + GarbageCollectionNotificationInfo info = + GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()); + + String gcName = info.getGcName(); + String gcAction = info.getGcAction(); + String gcCause = info.getGcCause(); + long duration = info.getGcInfo().getDuration(); + + // FILTERING LOGIC: + // Azul C4 exposes "GPGC" (Concurrent Cycle) and "GPGC Pauses" (STW Pauses). + // We MUST ignore "GPGC" because it reports cycle time (hundreds of ms) which is NOT a pause. + if ("GPGC".equals(gcName)) { + return; + } + + // Also ignore other known concurrent cycle beans if they appear + if (gcName.contains("Cycles") && !gcName.contains("Pauses")) { + return; + } + + // Only record if duration > 0 (sub-millisecond pauses might show as 0 or 1) + // Storing all for fidelity. + + ConcurrentLinkedDeque history = + pauseHistory.computeIfAbsent(gcName, k -> new ConcurrentLinkedDeque<>()); + + history.addLast(duration); + while (history.size() > MAX_PAUSE_HISTORY) { + history.removeFirst(); + } + + // Log significant pauses (> 10ms) + if (duration > 10) { + LOGGER.info( + String.format( + "GC Pause detected: %s | Action: %s | Cause: %s | Duration: %d ms", + gcName, gcAction, gcCause, duration)); + } } + } + + public List collectGCStats() { + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); + MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); + + List statsList = new ArrayList<>(); + + for (GarbageCollectorMXBean gcBean : gcBeans) { + String gcName = gcBean.getName(); + + // Exclude concurrent cycle collectors from the public stats to avoid confusion + if ("GPGC".equals(gcName) || (gcName.contains("Cycles") && !gcName.contains("Pauses"))) { + continue; + } + + GCStats stats = new GCStats(); + stats.setGcName(gcName); + stats.setCollectionCount(gcBean.getCollectionCount()); + stats.setCollectionTime(gcBean.getCollectionTime()); + + // Get recent pauses from accurate history + ConcurrentLinkedDeque history = pauseHistory.get(gcName); + if (history != null && !history.isEmpty()) { + List pauses = new ArrayList<>(history); + stats.setLastPauseDuration(pauses.get(pauses.size() - 1)); - private long percentile(List sortedValues, double percentile) { - int index = (int) Math.ceil(percentile * sortedValues.size()) - 1; - index = Math.max(0, Math.min(index, sortedValues.size() - 1)); - return sortedValues.get(index); + stats.setRecentPauses(pauses.subList(Math.max(0, pauses.size() - 100), pauses.size())); + + // Calculate percentiles + List sortedPauses = pauses.stream().sorted().collect(Collectors.toList()); + + stats.setPercentiles(calculatePercentiles(sortedPauses)); + } else { + stats.setLastPauseDuration(0); + stats.setRecentPauses(Collections.emptyList()); + stats.setPercentiles(new GCStats.PausePercentiles(0, 0, 0, 0, 0)); + } + + // Memory stats + stats.setTotalMemory(heapUsage.getMax()); + stats.setUsedMemory(heapUsage.getUsed()); + stats.setFreeMemory(heapUsage.getMax() - heapUsage.getUsed()); + + statsList.add(stats); } - public void resetStats() { - pauseHistory.clear(); - LOGGER.info("GC statistics reset"); + return statsList; + } + + private GCStats.PausePercentiles calculatePercentiles(List sortedPauses) { + if (sortedPauses.isEmpty()) { + return new GCStats.PausePercentiles(0, 0, 0, 0, 0); } + + int size = sortedPauses.size(); + return new GCStats.PausePercentiles( + percentile(sortedPauses, 0.50), + percentile(sortedPauses, 0.95), + percentile(sortedPauses, 0.99), + percentile(sortedPauses, 0.999), + sortedPauses.get(size - 1)); + } + + private long percentile(List sortedValues, double percentile) { + int index = (int) Math.ceil(percentile * sortedValues.size()) - 1; + index = Math.max(0, Math.min(index, sortedValues.size() - 1)); + return sortedValues.get(index); + } + + public void resetStats() { + pauseHistory.clear(); + LOGGER.info("GC statistics reset"); + } } diff --git a/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java b/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java index 5f0b0a5..3774922 100644 --- a/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java +++ b/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java @@ -5,11 +5,6 @@ import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.enterprise.context.ApplicationScoped; - -import javax.management.Notification; -import javax.management.NotificationEmitter; -import javax.management.NotificationListener; -import javax.management.openmbean.CompositeData; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.ArrayList; @@ -19,218 +14,233 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; +import javax.management.openmbean.CompositeData; /** - * Real-time GC pause monitoring using JMX notifications. - * Captures individual GC events with exact pause times (not averaged). + * Real-time GC pause monitoring using JMX notifications. Captures individual GC events with exact + * pause times (not averaged). */ @ApplicationScoped public class GCPauseMonitor implements NotificationListener { - private static final Logger LOGGER = Logger.getLogger(GCPauseMonitor.class.getName()); - - // Keep last N pauses for percentile calculations (reactive window) - private static final int MAX_PAUSE_HISTORY = 500; + private static final Logger LOGGER = Logger.getLogger(GCPauseMonitor.class.getName()); - // Pause history (milliseconds) - private final ConcurrentLinkedDeque pauseHistory = new ConcurrentLinkedDeque<>(); + // Keep last N pauses for percentile calculations (reactive window) + private static final int MAX_PAUSE_HISTORY = 500; - // All-time statistics - private final AtomicLong totalPauseCount = new AtomicLong(0); - private final AtomicLong totalPauseTimeMs = new AtomicLong(0); - private volatile long maxPauseMs = 0; + // Pause history (milliseconds) + private final ConcurrentLinkedDeque pauseHistory = new ConcurrentLinkedDeque<>(); - // SLA violation counters (all-time) - private final AtomicLong violationsOver10ms = new AtomicLong(0); - private final AtomicLong violationsOver50ms = new AtomicLong(0); - private final AtomicLong violationsOver100ms = new AtomicLong(0); + // All-time statistics + private final AtomicLong totalPauseCount = new AtomicLong(0); + private final AtomicLong totalPauseTimeMs = new AtomicLong(0); + private volatile long maxPauseMs = 0; - private final List emitters = new ArrayList<>(); + // SLA violation counters (all-time) + private final AtomicLong violationsOver10ms = new AtomicLong(0); + private final AtomicLong violationsOver50ms = new AtomicLong(0); + private final AtomicLong violationsOver100ms = new AtomicLong(0); - @PostConstruct - public void init() { - LOGGER.info("Initializing GC Pause Monitor with JMX notifications"); + private final List emitters = new ArrayList<>(); - List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + @PostConstruct + public void init() { + LOGGER.info("Initializing GC Pause Monitor with JMX notifications"); - for (GarbageCollectorMXBean gcBean : gcBeans) { - if (gcBean instanceof NotificationEmitter) { - NotificationEmitter emitter = (NotificationEmitter) gcBean; - emitter.addNotificationListener(this, null, null); - emitters.add(emitter); - LOGGER.info("Registered GC notification listener for: " + gcBean.getName()); - } - } + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); - if (emitters.isEmpty()) { - LOGGER.warning("No GC notification emitters found - pause monitoring may be limited"); - } + for (GarbageCollectorMXBean gcBean : gcBeans) { + if (gcBean instanceof NotificationEmitter) { + NotificationEmitter emitter = (NotificationEmitter) gcBean; + emitter.addNotificationListener(this, null, null); + emitters.add(emitter); + LOGGER.info("Registered GC notification listener for: " + gcBean.getName()); + } } - @PreDestroy - public void cleanup() { - for (NotificationEmitter emitter : emitters) { - try { - emitter.removeNotificationListener(this); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to remove GC notification listener", e); - } - } - emitters.clear(); + if (emitters.isEmpty()) { + LOGGER.warning("No GC notification emitters found - pause monitoring may be limited"); + } + } + + @PreDestroy + public void cleanup() { + for (NotificationEmitter emitter : emitters) { + try { + emitter.removeNotificationListener(this); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to remove GC notification listener", e); + } + } + emitters.clear(); + } + + @Override + public void handleNotification(Notification notification, Object handback) { + if (!notification + .getType() + .equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { + return; } - @Override - public void handleNotification(Notification notification, Object handback) { - if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { - return; - } - - try { - CompositeData cd = (CompositeData) notification.getUserData(); - GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd); - String gcName = info.getGcName(); + try { + CompositeData cd = (CompositeData) notification.getUserData(); + GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd); + String gcName = info.getGcName(); - // FILTERING LOGIC: - // Azul C4 exposes "GPGC" (Concurrent Cycle) and "GPGC Pauses" (STW Pauses). - // We MUST ignore "GPGC" because it reports cycle time (hundreds of ms) which is NOT a pause. - if ("GPGC".equals(gcName) || (gcName.contains("Cycles") && !gcName.contains("Pauses"))) { - return; - } + // FILTERING LOGIC: + // Azul C4 exposes "GPGC" (Concurrent Cycle) and "GPGC Pauses" (STW Pauses). + // We MUST ignore "GPGC" because it reports cycle time (hundreds of ms) which is NOT a pause. + if ("GPGC".equals(gcName) || (gcName.contains("Cycles") && !gcName.contains("Pauses"))) { + return; + } - GcInfo gcInfo = info.getGcInfo(); - long pauseMs = gcInfo.getDuration(); + GcInfo gcInfo = info.getGcInfo(); + long pauseMs = gcInfo.getDuration(); - // Record pause - recordPause(pauseMs, gcName, info.getGcAction()); + // Record pause + recordPause(pauseMs, gcName, info.getGcAction()); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Error processing GC notification", e); - } + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Error processing GC notification", e); } + } - private void recordPause(long pauseMs, String gcName, String gcAction) { - // Add to history - pauseHistory.addLast(pauseMs); - if (pauseHistory.size() > MAX_PAUSE_HISTORY) { - pauseHistory.removeFirst(); - } + private void recordPause(long pauseMs, String gcName, String gcAction) { + // Add to history + pauseHistory.addLast(pauseMs); + if (pauseHistory.size() > MAX_PAUSE_HISTORY) { + pauseHistory.removeFirst(); + } - // Update statistics - totalPauseCount.incrementAndGet(); - totalPauseTimeMs.addAndGet(pauseMs); + // Update statistics + totalPauseCount.incrementAndGet(); + totalPauseTimeMs.addAndGet(pauseMs); - // Update max (thread-safe but may miss true max in race condition - acceptable for monitoring) + // Update max (thread-safe but may miss true max in race condition - acceptable for monitoring) + if (pauseMs > maxPauseMs) { + synchronized (this) { if (pauseMs > maxPauseMs) { - synchronized (this) { - if (pauseMs > maxPauseMs) { - maxPauseMs = pauseMs; - } - } - } - - // Track SLA violations - if (pauseMs > 100) { - violationsOver100ms.incrementAndGet(); - violationsOver50ms.incrementAndGet(); - violationsOver10ms.incrementAndGet(); - } else if (pauseMs > 50) { - violationsOver50ms.incrementAndGet(); - violationsOver10ms.incrementAndGet(); - } else if (pauseMs > 10) { - violationsOver10ms.incrementAndGet(); - } - - // Log significant pauses - if (pauseMs > 100) { - LOGGER.warning(String.format("Large GC pause detected: %d ms [%s - %s]", pauseMs, gcName, gcAction)); - } else if (pauseMs > 50) { - LOGGER.info(String.format("Notable GC pause: %d ms [%s - %s]", pauseMs, gcName, gcAction)); + maxPauseMs = pauseMs; } + } } - public GCPauseStats getStats() { - List pauses = new ArrayList<>(pauseHistory); - - if (pauses.isEmpty()) { - return new GCPauseStats(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - } - - Collections.sort(pauses); - - long p50 = percentile(pauses, 0.50); - long p95 = percentile(pauses, 0.95); - long p99 = percentile(pauses, 0.99); - long p999 = percentile(pauses, 0.999); - long max = pauses.get(pauses.size() - 1); - - long count = totalPauseCount.get(); - long totalTime = totalPauseTimeMs.get(); - double avgPause = count > 0 ? (double) totalTime / count : 0; - - return new GCPauseStats( - count, - totalTime, - avgPause, - p50, - p95, - p99, - p999, - maxPauseMs, // All-time max - violationsOver10ms.get(), - violationsOver50ms.get(), - violationsOver100ms.get(), - pauses.size() // Sample size for percentiles - ); + // Track SLA violations + if (pauseMs > 100) { + violationsOver100ms.incrementAndGet(); + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + } else if (pauseMs > 50) { + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + } else if (pauseMs > 10) { + violationsOver10ms.incrementAndGet(); } - private long percentile(List sortedValues, double percentile) { - int index = (int) Math.ceil(percentile * sortedValues.size()) - 1; - index = Math.max(0, Math.min(index, sortedValues.size() - 1)); - return sortedValues.get(index); + // Log significant pauses + if (pauseMs > 100) { + LOGGER.warning( + String.format("Large GC pause detected: %d ms [%s - %s]", pauseMs, gcName, gcAction)); + } else if (pauseMs > 50) { + LOGGER.info(String.format("Notable GC pause: %d ms [%s - %s]", pauseMs, gcName, gcAction)); } + } - public void reset() { - pauseHistory.clear(); - totalPauseCount.set(0); - totalPauseTimeMs.set(0); - maxPauseMs = 0; - violationsOver10ms.set(0); - violationsOver50ms.set(0); - violationsOver100ms.set(0); - LOGGER.info("GC pause statistics reset"); + public GCPauseStats getStats() { + List pauses = new ArrayList<>(pauseHistory); + + if (pauses.isEmpty()) { + return new GCPauseStats(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } - public static class GCPauseStats { - public final long totalPauseCount; - public final long totalPauseTimeMs; - public final double avgPauseMs; - public final long p50Ms; - public final long p95Ms; - public final long p99Ms; - public final long p999Ms; - public final long maxMs; // All-time max since startup/reset - public final long violationsOver10ms; - public final long violationsOver50ms; - public final long violationsOver100ms; - public final int sampleSize; - - public GCPauseStats(long totalPauseCount, long totalPauseTimeMs, double avgPauseMs, - long p50Ms, long p95Ms, long p99Ms, long p999Ms, long maxMs, - long violationsOver10ms, long violationsOver50ms, long violationsOver100ms, - int sampleSize) { - this.totalPauseCount = totalPauseCount; - this.totalPauseTimeMs = totalPauseTimeMs; - this.avgPauseMs = avgPauseMs; - this.p50Ms = p50Ms; - this.p95Ms = p95Ms; - this.p99Ms = p99Ms; - this.p999Ms = p999Ms; - this.maxMs = maxMs; - this.violationsOver10ms = violationsOver10ms; - this.violationsOver50ms = violationsOver50ms; - this.violationsOver100ms = violationsOver100ms; - this.sampleSize = sampleSize; - } + Collections.sort(pauses); + + long p50 = percentile(pauses, 0.50); + long p95 = percentile(pauses, 0.95); + long p99 = percentile(pauses, 0.99); + long p999 = percentile(pauses, 0.999); + long max = pauses.get(pauses.size() - 1); + + long count = totalPauseCount.get(); + long totalTime = totalPauseTimeMs.get(); + double avgPause = count > 0 ? (double) totalTime / count : 0; + + return new GCPauseStats( + count, + totalTime, + avgPause, + p50, + p95, + p99, + p999, + maxPauseMs, // All-time max + violationsOver10ms.get(), + violationsOver50ms.get(), + violationsOver100ms.get(), + pauses.size() // Sample size for percentiles + ); + } + + private long percentile(List sortedValues, double percentile) { + int index = (int) Math.ceil(percentile * sortedValues.size()) - 1; + index = Math.max(0, Math.min(index, sortedValues.size() - 1)); + return sortedValues.get(index); + } + + public void reset() { + pauseHistory.clear(); + totalPauseCount.set(0); + totalPauseTimeMs.set(0); + maxPauseMs = 0; + violationsOver10ms.set(0); + violationsOver50ms.set(0); + violationsOver100ms.set(0); + LOGGER.info("GC pause statistics reset"); + } + + public static class GCPauseStats { + public final long totalPauseCount; + public final long totalPauseTimeMs; + public final double avgPauseMs; + public final long p50Ms; + public final long p95Ms; + public final long p99Ms; + public final long p999Ms; + public final long maxMs; // All-time max since startup/reset + public final long violationsOver10ms; + public final long violationsOver50ms; + public final long violationsOver100ms; + public final int sampleSize; + + public GCPauseStats( + long totalPauseCount, + long totalPauseTimeMs, + double avgPauseMs, + long p50Ms, + long p95Ms, + long p99Ms, + long p999Ms, + long maxMs, + long violationsOver10ms, + long violationsOver50ms, + long violationsOver100ms, + int sampleSize) { + this.totalPauseCount = totalPauseCount; + this.totalPauseTimeMs = totalPauseTimeMs; + this.avgPauseMs = avgPauseMs; + this.p50Ms = p50Ms; + this.p95Ms = p95Ms; + this.p99Ms = p99Ms; + this.p999Ms = p999Ms; + this.maxMs = maxMs; + this.violationsOver10ms = violationsOver10ms; + this.violationsOver50ms = violationsOver50ms; + this.violationsOver100ms = violationsOver100ms; + this.sampleSize = sampleSize; } + } } diff --git a/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java b/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java index 51f06f3..5e48f73 100644 --- a/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java +++ b/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java @@ -8,94 +8,93 @@ @ApplicationScoped public class SLAMonitorService { - private static final Logger LOGGER = Logger.getLogger(SLAMonitorService.class.getName()); + private static final Logger LOGGER = Logger.getLogger(SLAMonitorService.class.getName()); - // SLA thresholds - private static final long SLA_10MS = 10; - private static final long SLA_50MS = 50; - private static final long SLA_100MS = 100; + // SLA thresholds + private static final long SLA_10MS = 10; + private static final long SLA_50MS = 50; + private static final long SLA_100MS = 100; - // Violation counters - private final AtomicLong violationsOver10ms = new AtomicLong(0); - private final AtomicLong violationsOver50ms = new AtomicLong(0); - private final AtomicLong violationsOver100ms = new AtomicLong(0); - private final AtomicLong totalOperations = new AtomicLong(0); + // Violation counters + private final AtomicLong violationsOver10ms = new AtomicLong(0); + private final AtomicLong violationsOver50ms = new AtomicLong(0); + private final AtomicLong violationsOver100ms = new AtomicLong(0); + private final AtomicLong totalOperations = new AtomicLong(0); - // Rolling window (last 5 minutes) - private final ConcurrentHashMap violationsByMinute = new ConcurrentHashMap<>(); + // Rolling window (last 5 minutes) + private final ConcurrentHashMap violationsByMinute = new ConcurrentHashMap<>(); - /** - * Record an operation latency and check for SLA violations - */ - public void recordOperation(long latencyMs) { - totalOperations.incrementAndGet(); + /** Record an operation latency and check for SLA violations */ + public void recordOperation(long latencyMs) { + totalOperations.incrementAndGet(); - if (latencyMs > SLA_100MS) { - violationsOver100ms.incrementAndGet(); - violationsOver50ms.incrementAndGet(); - violationsOver10ms.incrementAndGet(); - recordViolation(); - } else if (latencyMs > SLA_50MS) { - violationsOver50ms.incrementAndGet(); - violationsOver10ms.incrementAndGet(); - recordViolation(); - } else if (latencyMs > SLA_10MS) { - violationsOver10ms.incrementAndGet(); - recordViolation(); - } + if (latencyMs > SLA_100MS) { + violationsOver100ms.incrementAndGet(); + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + recordViolation(); + } else if (latencyMs > SLA_50MS) { + violationsOver50ms.incrementAndGet(); + violationsOver10ms.incrementAndGet(); + recordViolation(); + } else if (latencyMs > SLA_10MS) { + violationsOver10ms.incrementAndGet(); + recordViolation(); } + } - private void recordViolation() { - long currentMinute = System.currentTimeMillis() / 60000; - violationsByMinute.merge(currentMinute, 1L, Long::sum); + private void recordViolation() { + long currentMinute = System.currentTimeMillis() / 60000; + violationsByMinute.merge(currentMinute, 1L, Long::sum); - // Clean up old entries (> 5 minutes) - long fiveMinutesAgo = currentMinute - 5; - violationsByMinute.keySet().removeIf(minute -> minute < fiveMinutesAgo); - } + // Clean up old entries (> 5 minutes) + long fiveMinutesAgo = currentMinute - 5; + violationsByMinute.keySet().removeIf(minute -> minute < fiveMinutesAgo); + } - /** - * Get SLA compliance statistics - */ - public SLAStats getStats() { - long total = totalOperations.get(); + /** Get SLA compliance statistics */ + public SLAStats getStats() { + long total = totalOperations.get(); - return new SLAStats( - total, - violationsOver10ms.get(), - violationsOver50ms.get(), - violationsOver100ms.get(), - total > 0 ? (double) violationsOver10ms.get() / total * 100 : 0, - violationsByMinute.values().stream().mapToLong(Long::longValue).sum() - ); - } + return new SLAStats( + total, + violationsOver10ms.get(), + violationsOver50ms.get(), + violationsOver100ms.get(), + total > 0 ? (double) violationsOver10ms.get() / total * 100 : 0, + violationsByMinute.values().stream().mapToLong(Long::longValue).sum()); + } - public void reset() { - violationsOver10ms.set(0); - violationsOver50ms.set(0); - violationsOver100ms.set(0); - totalOperations.set(0); - violationsByMinute.clear(); - LOGGER.info("SLA statistics reset"); - } + public void reset() { + violationsOver10ms.set(0); + violationsOver50ms.set(0); + violationsOver100ms.set(0); + totalOperations.set(0); + violationsByMinute.clear(); + LOGGER.info("SLA statistics reset"); + } - public static class SLAStats { - public final long totalOperations; - public final long violationsOver10ms; - public final long violationsOver50ms; - public final long violationsOver100ms; - public final double violationRate; - public final long recentViolations; // Last 5 minutes + public static class SLAStats { + public final long totalOperations; + public final long violationsOver10ms; + public final long violationsOver50ms; + public final long violationsOver100ms; + public final double violationRate; + public final long recentViolations; // Last 5 minutes - public SLAStats(long totalOperations, long violationsOver10ms, - long violationsOver50ms, long violationsOver100ms, - double violationRate, long recentViolations) { - this.totalOperations = totalOperations; - this.violationsOver10ms = violationsOver10ms; - this.violationsOver50ms = violationsOver50ms; - this.violationsOver100ms = violationsOver100ms; - this.violationRate = violationRate; - this.recentViolations = recentViolations; - } + public SLAStats( + long totalOperations, + long violationsOver10ms, + long violationsOver50ms, + long violationsOver100ms, + double violationRate, + long recentViolations) { + this.totalOperations = totalOperations; + this.violationsOver10ms = violationsOver10ms; + this.violationsOver50ms = violationsOver50ms; + this.violationsOver100ms = violationsOver100ms; + this.violationRate = violationRate; + this.recentViolations = recentViolations; } + } } diff --git a/src/main/java/fish/payara/trader/pressure/AllocationMode.java b/src/main/java/fish/payara/trader/pressure/AllocationMode.java index 446c57d..e8ea920 100644 --- a/src/main/java/fish/payara/trader/pressure/AllocationMode.java +++ b/src/main/java/fish/payara/trader/pressure/AllocationMode.java @@ -1,35 +1,35 @@ package fish.payara.trader.pressure; public enum AllocationMode { - OFF(0, 0, "No additional allocation"), - LOW(10, 10240, "1 MB/sec - Light pressure"), - MEDIUM(100, 10240, "10 MB/sec - Moderate pressure"), - HIGH(5000, 10240, "500 MB/sec - Heavy pressure"), - EXTREME(20000, 10240, "2 GB/sec - Extreme pressure"); + OFF(0, 0, "No additional allocation"), + LOW(10, 10240, "1 MB/sec - Light pressure"), + MEDIUM(100, 10240, "10 MB/sec - Moderate pressure"), + HIGH(5000, 10240, "500 MB/sec - Heavy pressure"), + EXTREME(20000, 10240, "2 GB/sec - Extreme pressure"); - private final int allocationsPerIteration; - private final int bytesPerAllocation; - private final String description; + private final int allocationsPerIteration; + private final int bytesPerAllocation; + private final String description; - AllocationMode(int allocationsPerIteration, int bytesPerAllocation, String description) { - this.allocationsPerIteration = allocationsPerIteration; - this.bytesPerAllocation = bytesPerAllocation; - this.description = description; - } + AllocationMode(int allocationsPerIteration, int bytesPerAllocation, String description) { + this.allocationsPerIteration = allocationsPerIteration; + this.bytesPerAllocation = bytesPerAllocation; + this.description = description; + } - public int getAllocationsPerIteration() { - return allocationsPerIteration; - } + public int getAllocationsPerIteration() { + return allocationsPerIteration; + } - public int getBytesPerAllocation() { - return bytesPerAllocation; - } + public int getBytesPerAllocation() { + return bytesPerAllocation; + } - public String getDescription() { - return description; - } + public String getDescription() { + return description; + } - public long getBytesPerSecond() { - return (long) allocationsPerIteration * bytesPerAllocation * 10; // 10 iterations/sec - } + public long getBytesPerSecond() { + return (long) allocationsPerIteration * bytesPerAllocation * 10; // 10 iterations/sec + } } diff --git a/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java index 2181960..29008a9 100644 --- a/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java +++ b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java @@ -6,7 +6,6 @@ import jakarta.enterprise.concurrent.ManagedExecutorService; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; - import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -16,239 +15,237 @@ import java.util.logging.Logger; /** - * Service to generate controlled memory pressure for GC stress testing. - * Based on 1BRC techniques - intentional allocation to demonstrate GC behavior. + * Service to generate controlled memory pressure for GC stress testing. Based on 1BRC techniques - + * intentional allocation to demonstrate GC behavior. */ @ApplicationScoped public class MemoryPressureService { - private static final Logger LOGGER = Logger.getLogger(MemoryPressureService.class.getName()); + private static final Logger LOGGER = Logger.getLogger(MemoryPressureService.class.getName()); - private volatile AllocationMode currentMode = AllocationMode.OFF; - private volatile boolean running = false; - private Future pressureTask; + private volatile AllocationMode currentMode = AllocationMode.OFF; + private volatile boolean running = false; + private Future pressureTask; - private long totalBytesAllocated = 0; - private long lastStatsTime = System.currentTimeMillis(); + private long totalBytesAllocated = 0; + private long lastStatsTime = System.currentTimeMillis(); - // Long-lived objects that survive to tenured/old generation - private final List tenuredObjects = new CopyOnWriteArrayList<>(); - private static final int TENURED_TARGET_MB = 1024; // Target 1GB in old gen - private final AtomicLong tenuredBytesAllocated = new AtomicLong(0); + // Long-lived objects that survive to tenured/old generation + private final List tenuredObjects = new CopyOnWriteArrayList<>(); + private static final int TENURED_TARGET_MB = 1024; // Target 1GB in old gen + private final AtomicLong tenuredBytesAllocated = new AtomicLong(0); - @Inject - @VirtualThreadExecutor - private ManagedExecutorService executorService; + @Inject @VirtualThreadExecutor private ManagedExecutorService executorService; - @PostConstruct - public void init() { - LOGGER.info("MemoryPressureService initialized"); - } + @PostConstruct + public void init() { + LOGGER.info("MemoryPressureService initialized"); + } - public synchronized void setAllocationMode(AllocationMode mode) { - if (mode == currentMode) { - return; - } + public synchronized void setAllocationMode(AllocationMode mode) { + if (mode == currentMode) { + return; + } - LOGGER.info("Changing allocation mode from " + currentMode + " to " + mode); - currentMode = mode; + LOGGER.info("Changing allocation mode from " + currentMode + " to " + mode); + currentMode = mode; - if (mode == AllocationMode.OFF) { - stopPressure(); - } else { - startPressure(); - } + if (mode == AllocationMode.OFF) { + stopPressure(); + } else { + startPressure(); } + } - private synchronized void startPressure() { - if (running) { - return; - } + private synchronized void startPressure() { + if (running) { + return; + } - running = true; - totalBytesAllocated = 0; - lastStatsTime = System.currentTimeMillis(); + running = true; + totalBytesAllocated = 0; + lastStatsTime = System.currentTimeMillis(); - pressureTask = executorService.submit(() -> { - LOGGER.info("Memory pressure generator started with mode: " + currentMode); + pressureTask = + executorService.submit( + () -> { + LOGGER.info("Memory pressure generator started with mode: " + currentMode); - while (running) { + while (running) { try { - AllocationMode mode = currentMode; - if (mode == AllocationMode.OFF) { - break; - } + AllocationMode mode = currentMode; + if (mode == AllocationMode.OFF) { + break; + } - // Generate garbage for this iteration - generateGarbage(mode); + // Generate garbage for this iteration + generateGarbage(mode); - // Sleep 100ms between iterations (10 iterations/sec) - Thread.sleep(100); + // Sleep 100ms between iterations (10 iterations/sec) + Thread.sleep(100); - // Log stats every 5 seconds - logStats(); + // Log stats every 5 seconds + logStats(); } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; + Thread.currentThread().interrupt(); + break; } catch (Exception e) { - LOGGER.warning("Error in memory pressure generator: " + e.getMessage()); - } - } - - LOGGER.info("Memory pressure generator stopped"); - }); - } - - private synchronized void stopPressure() { - running = false; - if (pressureTask != null && !pressureTask.isDone()) { - pressureTask.cancel(true); - pressureTask = null; - } - } - - private void generateGarbage(AllocationMode mode) { - int allocations = mode.getAllocationsPerIteration(); - int bytesPerAlloc = mode.getBytesPerAllocation(); - - for (int i = 0; i < allocations; i++) { - // Mix different allocation patterns - int pattern = i % 4; - - switch (pattern) { - case 0: - generateStringGarbage(bytesPerAlloc); - break; - case 1: - generateByteArrayGarbage(bytesPerAlloc); - break; - case 2: - generateObjectGarbage(bytesPerAlloc / 64); - break; - case 3: - generateCollectionGarbage(bytesPerAlloc / 100); - break; - } - - // NEW: Create long-lived objects inside the loop for higher impact - if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { - // 0.1% chance in EXTREME (20 objects/iteration = 200MB/s) - // 0.02% chance in HIGH (1 object/iteration = 10MB/s) - int chance = (mode == AllocationMode.EXTREME) ? 10 : 2; - if (ThreadLocalRandom.current().nextInt(10000) < chance) { - byte[] longLived = new byte[1024 * 1024]; // 1MB object - ThreadLocalRandom.current().nextBytes(longLived); // Prevent optimization - tenuredObjects.add(longLived); - tenuredBytesAllocated.addAndGet(1024 * 1024); - } - } - - totalBytesAllocated += bytesPerAlloc; - } - - // Maintain target size - remove oldest when limit reached - if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { - while (tenuredBytesAllocated.get() > TENURED_TARGET_MB * 1024L * 1024L) { - if (!tenuredObjects.isEmpty()) { - tenuredObjects.remove(0); - tenuredBytesAllocated.addAndGet(-1024 * 1024); - } else { - break; + LOGGER.warning("Error in memory pressure generator: " + e.getMessage()); } - } - } else if (mode == AllocationMode.OFF || mode == AllocationMode.LOW) { - // Clear tenured objects when stress is reduced - if (!tenuredObjects.isEmpty()) { - tenuredObjects.clear(); - tenuredBytesAllocated.set(0); - } - } - } - - private void generateStringGarbage(int bytes) { - // Create strings via concatenation (generates intermediate garbage) - StringBuilder sb = new StringBuilder(bytes); - for (int i = 0; i < bytes / 10; i++) { - sb.append("GARBAGE"); - } - String garbage = sb.toString(); - // String is now eligible for GC - } - - private void generateByteArrayGarbage(int bytes) { - byte[] garbage = new byte[bytes]; - // Fill with random data to prevent compiler optimization - ThreadLocalRandom.current().nextBytes(garbage); - // Array is now eligible for GC - } - - private void generateObjectGarbage(int count) { - List garbage = new ArrayList<>(count); - for (int i = 0; i < count; i++) { - garbage.add(new DummyObject(i, "data-" + i, System.nanoTime())); + } + + LOGGER.info("Memory pressure generator stopped"); + }); + } + + private synchronized void stopPressure() { + running = false; + if (pressureTask != null && !pressureTask.isDone()) { + pressureTask.cancel(true); + pressureTask = null; + } + } + + private void generateGarbage(AllocationMode mode) { + int allocations = mode.getAllocationsPerIteration(); + int bytesPerAlloc = mode.getBytesPerAllocation(); + + for (int i = 0; i < allocations; i++) { + // Mix different allocation patterns + int pattern = i % 4; + + switch (pattern) { + case 0: + generateStringGarbage(bytesPerAlloc); + break; + case 1: + generateByteArrayGarbage(bytesPerAlloc); + break; + case 2: + generateObjectGarbage(bytesPerAlloc / 64); + break; + case 3: + generateCollectionGarbage(bytesPerAlloc / 100); + break; + } + + // NEW: Create long-lived objects inside the loop for higher impact + if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { + // 0.1% chance in EXTREME (20 objects/iteration = 200MB/s) + // 0.02% chance in HIGH (1 object/iteration = 10MB/s) + int chance = (mode == AllocationMode.EXTREME) ? 10 : 2; + if (ThreadLocalRandom.current().nextInt(10000) < chance) { + byte[] longLived = new byte[1024 * 1024]; // 1MB object + ThreadLocalRandom.current().nextBytes(longLived); // Prevent optimization + tenuredObjects.add(longLived); + tenuredBytesAllocated.addAndGet(1024 * 1024); } - // List and objects are now eligible for GC - } + } - private void generateCollectionGarbage(int count) { - List garbage = new ArrayList<>(count); - for (int i = 0; i < count; i++) { - garbage.add(ThreadLocalRandom.current().nextInt()); - } - // List is now eligible for GC + totalBytesAllocated += bytesPerAlloc; } - private void logStats() { - long now = System.currentTimeMillis(); - if (now - lastStatsTime >= 5000) { - double elapsedSeconds = (now - lastStatsTime) / 1000.0; - double mbPerSec = (totalBytesAllocated / (1024.0 * 1024.0)) / elapsedSeconds; - - LOGGER.info(String.format( - "Memory Pressure Stats - Mode: %s | Allocated: %.2f MB/sec | Tenured: %d MB (%d objects)", - currentMode, mbPerSec, getTenuredObjectsMB(), getTenuredObjectCount() - )); - - totalBytesAllocated = 0; - lastStatsTime = now; - } - } - - public long getTenuredObjectsMB() { - return tenuredBytesAllocated.get() / (1024 * 1024); - } - - public int getTenuredObjectCount() { - return tenuredObjects.size(); - } - - @PreDestroy - public void shutdown() { - LOGGER.info("Shutting down MemoryPressureService"); - stopPressure(); - } - - public AllocationMode getCurrentMode() { - return currentMode; - } - - public boolean isRunning() { - return running; - } - - /** - * Dummy object for allocation testing - */ - private static class DummyObject { - private final int id; - private final String data; - private final long timestamp; - - DummyObject(int id, String data, long timestamp) { - this.id = id; - this.data = data; - this.timestamp = timestamp; + // Maintain target size - remove oldest when limit reached + if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { + while (tenuredBytesAllocated.get() > TENURED_TARGET_MB * 1024L * 1024L) { + if (!tenuredObjects.isEmpty()) { + tenuredObjects.remove(0); + tenuredBytesAllocated.addAndGet(-1024 * 1024); + } else { + break; } - } + } + } else if (mode == AllocationMode.OFF || mode == AllocationMode.LOW) { + // Clear tenured objects when stress is reduced + if (!tenuredObjects.isEmpty()) { + tenuredObjects.clear(); + tenuredBytesAllocated.set(0); + } + } + } + + private void generateStringGarbage(int bytes) { + // Create strings via concatenation (generates intermediate garbage) + StringBuilder sb = new StringBuilder(bytes); + for (int i = 0; i < bytes / 10; i++) { + sb.append("GARBAGE"); + } + String garbage = sb.toString(); + // String is now eligible for GC + } + + private void generateByteArrayGarbage(int bytes) { + byte[] garbage = new byte[bytes]; + // Fill with random data to prevent compiler optimization + ThreadLocalRandom.current().nextBytes(garbage); + // Array is now eligible for GC + } + + private void generateObjectGarbage(int count) { + List garbage = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + garbage.add(new DummyObject(i, "data-" + i, System.nanoTime())); + } + // List and objects are now eligible for GC + } + + private void generateCollectionGarbage(int count) { + List garbage = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + garbage.add(ThreadLocalRandom.current().nextInt()); + } + // List is now eligible for GC + } + + private void logStats() { + long now = System.currentTimeMillis(); + if (now - lastStatsTime >= 5000) { + double elapsedSeconds = (now - lastStatsTime) / 1000.0; + double mbPerSec = (totalBytesAllocated / (1024.0 * 1024.0)) / elapsedSeconds; + + LOGGER.info( + String.format( + "Memory Pressure Stats - Mode: %s | Allocated: %.2f MB/sec | Tenured: %d MB (%d objects)", + currentMode, mbPerSec, getTenuredObjectsMB(), getTenuredObjectCount())); + + totalBytesAllocated = 0; + lastStatsTime = now; + } + } + + public long getTenuredObjectsMB() { + return tenuredBytesAllocated.get() / (1024 * 1024); + } + + public int getTenuredObjectCount() { + return tenuredObjects.size(); + } + + @PreDestroy + public void shutdown() { + LOGGER.info("Shutting down MemoryPressureService"); + stopPressure(); + } + + public AllocationMode getCurrentMode() { + return currentMode; + } + + public boolean isRunning() { + return running; + } + + /** Dummy object for allocation testing */ + private static class DummyObject { + private final int id; + private final String data; + private final long timestamp; + + DummyObject(int id, String data, long timestamp) { + this.id = id; + this.data = data; + this.timestamp = timestamp; + } + } } diff --git a/src/main/java/fish/payara/trader/rest/ApplicationConfig.java b/src/main/java/fish/payara/trader/rest/ApplicationConfig.java index 528ef7e..1dd3f0f 100644 --- a/src/main/java/fish/payara/trader/rest/ApplicationConfig.java +++ b/src/main/java/fish/payara/trader/rest/ApplicationConfig.java @@ -3,10 +3,8 @@ import jakarta.ws.rs.ApplicationPath; import jakarta.ws.rs.core.Application; -/** - * JAX-RS application configuration - */ +/** JAX-RS application configuration */ @ApplicationPath("/api") public class ApplicationConfig extends Application { - // All REST resources will be available at /api/* + // All REST resources will be available at /api/* } diff --git a/src/main/java/fish/payara/trader/rest/GCStatsResource.java b/src/main/java/fish/payara/trader/rest/GCStatsResource.java index 6761627..b07a427 100644 --- a/src/main/java/fish/payara/trader/rest/GCStatsResource.java +++ b/src/main/java/fish/payara/trader/rest/GCStatsResource.java @@ -8,7 +8,6 @@ import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; - import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.HashMap; @@ -17,130 +16,124 @@ import java.util.logging.Logger; import java.util.stream.Collectors; -/** - * REST endpoint for GC statistics monitoring - */ +/** REST endpoint for GC statistics monitoring */ @Path("/gc") public class GCStatsResource { - private static final Logger LOGGER = Logger.getLogger(GCStatsResource.class.getName()); + private static final Logger LOGGER = Logger.getLogger(GCStatsResource.class.getName()); - @Inject - private GCStatsService gcStatsService; + @Inject private GCStatsService gcStatsService; - @Inject - private MemoryPressureService memoryPressureService; + @Inject private MemoryPressureService memoryPressureService; - @Inject - private MarketDataPublisher publisher; + @Inject private MarketDataPublisher publisher; - @Inject - private fish.payara.trader.monitoring.SLAMonitorService slaMonitor; + @Inject private fish.payara.trader.monitoring.SLAMonitorService slaMonitor; - @Inject - private fish.payara.trader.monitoring.GCPauseMonitor gcPauseMonitor; + @Inject private fish.payara.trader.monitoring.GCPauseMonitor gcPauseMonitor; - @GET - @Path("/sla") - @Produces(MediaType.APPLICATION_JSON) - public Response getSLAStats() { - return Response.ok(slaMonitor.getStats()).build(); - } + @GET + @Path("/sla") + @Produces(MediaType.APPLICATION_JSON) + public Response getSLAStats() { + return Response.ok(slaMonitor.getStats()).build(); + } - @POST - @Path("/sla/reset") - public Response resetSLAStats() { - slaMonitor.reset(); - return Response.ok(Map.of("status", "reset")).build(); - } + @POST + @Path("/sla/reset") + public Response resetSLAStats() { + slaMonitor.reset(); + return Response.ok(Map.of("status", "reset")).build(); + } - @GET - @Path("/pauses") - @Produces(MediaType.APPLICATION_JSON) - public Response getGCPauseStats() { - return Response.ok(gcPauseMonitor.getStats()).build(); - } + @GET + @Path("/pauses") + @Produces(MediaType.APPLICATION_JSON) + public Response getGCPauseStats() { + return Response.ok(gcPauseMonitor.getStats()).build(); + } - @POST - @Path("/pauses/reset") - public Response resetGCPauseStats() { - gcPauseMonitor.reset(); - return Response.ok(Map.of("status", "reset")).build(); - } + @POST + @Path("/pauses/reset") + public Response resetGCPauseStats() { + gcPauseMonitor.reset(); + return Response.ok(Map.of("status", "reset")).build(); + } - @GET - @Path("/comparison") - @Produces(MediaType.APPLICATION_JSON) - public Response getComparison() { - Map comparison = new HashMap<>(); - - // Identify which instance is responding - String instanceName = System.getenv("PAYARA_INSTANCE_NAME"); - if (instanceName == null) { - instanceName = "standalone"; - } - comparison.put("instanceName", instanceName); - - // Identify which JVM is running - String jvmVendor = System.getProperty("java.vm.vendor"); - String jvmName = System.getProperty("java.vm.name"); - String gcName = ManagementFactory.getGarbageCollectorMXBeans() - .stream() - .map(GarbageCollectorMXBean::getName) - .collect(Collectors.joining(", ")); + @GET + @Path("/comparison") + @Produces(MediaType.APPLICATION_JSON) + public Response getComparison() { + Map comparison = new HashMap<>(); - boolean isAzulC4 = jvmVendor != null && jvmVendor.contains("Azul"); - - comparison.put("jvmVendor", jvmVendor); - comparison.put("jvmName", jvmName); - comparison.put("gcCollectors", gcName); - comparison.put("isAzulC4", isAzulC4); - comparison.put("heapSizeMB", Runtime.getRuntime().maxMemory() / (1024 * 1024)); - - // Current stress level - comparison.put("allocationMode", memoryPressureService.getCurrentMode()); - comparison.put("messageRate", publisher.getMessagesPublished()); - - // GC Performance Metrics (keep old stats for backward compatibility) - List stats = gcStatsService.collectGCStats(); - comparison.put("gcStats", stats); - - // Critical comparison metrics - USE ACCURATE GC PAUSE MONITOR - fish.payara.trader.monitoring.GCPauseMonitor.GCPauseStats pauseStats = gcPauseMonitor.getStats(); - comparison.put("pauseP50Ms", pauseStats.p50Ms); - comparison.put("pauseP95Ms", pauseStats.p95Ms); - comparison.put("pauseP99Ms", pauseStats.p99Ms); - comparison.put("pauseP999Ms", pauseStats.p999Ms); - comparison.put("pauseMaxMs", pauseStats.maxMs); // All-time max - comparison.put("pauseAvgMs", pauseStats.avgPauseMs); - comparison.put("totalPauseCount", pauseStats.totalPauseCount); - comparison.put("totalPauseTimeMs", pauseStats.totalPauseTimeMs); - - // SLA violation tracking (accurate counts since startup/reset) - comparison.put("slaViolations10ms", pauseStats.violationsOver10ms); - comparison.put("slaViolations50ms", pauseStats.violationsOver50ms); - comparison.put("slaViolations100ms", pauseStats.violationsOver100ms); - comparison.put("pauseSampleSize", pauseStats.sampleSize); // For transparency - - return Response.ok(comparison).build(); + // Identify which instance is responding + String instanceName = System.getenv("PAYARA_INSTANCE_NAME"); + if (instanceName == null) { + instanceName = "standalone"; } + comparison.put("instanceName", instanceName); - @GET - @Path("/stats") - @Produces(MediaType.APPLICATION_JSON) - public Response getGCStats() { - LOGGER.fine("GET /api/gc/stats - Collecting GC statistics"); - List stats = gcStatsService.collectGCStats(); - LOGGER.info(String.format("GET /api/gc/stats - Returned %d GC collector stats", stats.size())); - return Response.ok(stats).build(); - } + // Identify which JVM is running + String jvmVendor = System.getProperty("java.vm.vendor"); + String jvmName = System.getProperty("java.vm.name"); + String gcName = + ManagementFactory.getGarbageCollectorMXBeans().stream() + .map(GarbageCollectorMXBean::getName) + .collect(Collectors.joining(", ")); - @POST - @Path("/reset") - @Produces(MediaType.APPLICATION_JSON) - public Response resetStats() { - LOGGER.info("POST /api/gc/reset - Resetting GC statistics"); - gcStatsService.resetStats(); - return Response.ok().entity("{\"status\":\"reset\"}").build(); - } + boolean isAzulC4 = jvmVendor != null && jvmVendor.contains("Azul"); + + comparison.put("jvmVendor", jvmVendor); + comparison.put("jvmName", jvmName); + comparison.put("gcCollectors", gcName); + comparison.put("isAzulC4", isAzulC4); + comparison.put("heapSizeMB", Runtime.getRuntime().maxMemory() / (1024 * 1024)); + + // Current stress level + comparison.put("allocationMode", memoryPressureService.getCurrentMode()); + comparison.put("messageRate", publisher.getMessagesPublished()); + + // GC Performance Metrics (keep old stats for backward compatibility) + List stats = gcStatsService.collectGCStats(); + comparison.put("gcStats", stats); + + // Critical comparison metrics - USE ACCURATE GC PAUSE MONITOR + fish.payara.trader.monitoring.GCPauseMonitor.GCPauseStats pauseStats = + gcPauseMonitor.getStats(); + comparison.put("pauseP50Ms", pauseStats.p50Ms); + comparison.put("pauseP95Ms", pauseStats.p95Ms); + comparison.put("pauseP99Ms", pauseStats.p99Ms); + comparison.put("pauseP999Ms", pauseStats.p999Ms); + comparison.put("pauseMaxMs", pauseStats.maxMs); // All-time max + comparison.put("pauseAvgMs", pauseStats.avgPauseMs); + comparison.put("totalPauseCount", pauseStats.totalPauseCount); + comparison.put("totalPauseTimeMs", pauseStats.totalPauseTimeMs); + + // SLA violation tracking (accurate counts since startup/reset) + comparison.put("slaViolations10ms", pauseStats.violationsOver10ms); + comparison.put("slaViolations50ms", pauseStats.violationsOver50ms); + comparison.put("slaViolations100ms", pauseStats.violationsOver100ms); + comparison.put("pauseSampleSize", pauseStats.sampleSize); // For transparency + + return Response.ok(comparison).build(); + } + + @GET + @Path("/stats") + @Produces(MediaType.APPLICATION_JSON) + public Response getGCStats() { + LOGGER.fine("GET /api/gc/stats - Collecting GC statistics"); + List stats = gcStatsService.collectGCStats(); + LOGGER.info(String.format("GET /api/gc/stats - Returned %d GC collector stats", stats.size())); + return Response.ok(stats).build(); + } + + @POST + @Path("/reset") + @Produces(MediaType.APPLICATION_JSON) + public Response resetStats() { + LOGGER.info("POST /api/gc/reset - Resetting GC statistics"); + gcStatsService.resetStats(); + return Response.ok().entity("{\"status\":\"reset\"}").build(); + } } diff --git a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java index c915578..5165d5c 100644 --- a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java +++ b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java @@ -6,7 +6,6 @@ import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; - import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -14,134 +13,141 @@ import java.util.logging.Logger; import java.util.stream.Collectors; -/** - * REST endpoint for controlling memory pressure testing - */ +/** REST endpoint for controlling memory pressure testing */ @Path("/pressure") public class MemoryPressureResource { - private static final Logger LOGGER = Logger.getLogger(MemoryPressureResource.class.getName()); - - @Inject - private MemoryPressureService pressureService; + private static final Logger LOGGER = Logger.getLogger(MemoryPressureResource.class.getName()); - public enum StressScenario { - DEMO_BASELINE(AllocationMode.OFF, "Baseline - No artificial stress"), - DEMO_NORMAL(AllocationMode.LOW, "Normal Trading - Light load to show steady-state"), - DEMO_STRESS(AllocationMode.HIGH, "High Stress - Heavy allocation + burst patterns"), - DEMO_EXTREME(AllocationMode.EXTREME, "Extreme Stress - Maximum pressure + tenured pollution"); + @Inject private MemoryPressureService pressureService; - private final AllocationMode mode; - private final String description; + public enum StressScenario { + DEMO_BASELINE(AllocationMode.OFF, "Baseline - No artificial stress"), + DEMO_NORMAL(AllocationMode.LOW, "Normal Trading - Light load to show steady-state"), + DEMO_STRESS(AllocationMode.HIGH, "High Stress - Heavy allocation + burst patterns"), + DEMO_EXTREME(AllocationMode.EXTREME, "Extreme Stress - Maximum pressure + tenured pollution"); - StressScenario(AllocationMode mode, String description) { - this.mode = mode; - this.description = description; - } + private final AllocationMode mode; + private final String description; - public AllocationMode getMode() { return mode; } - public String getDescription() { return description; } + StressScenario(AllocationMode mode, String description) { + this.mode = mode; + this.description = description; } - @GET - @Path("/status") - @Produces(MediaType.APPLICATION_JSON) - public Response getStatus() { - AllocationMode currentMode = pressureService.getCurrentMode(); - LOGGER.fine(String.format("GET /api/pressure/status - Current mode: %s", currentMode.name())); - - Map status = new HashMap<>(); - status.put("currentMode", currentMode.name()); - status.put("description", currentMode.getDescription()); - status.put("running", pressureService.isRunning()); - status.put("bytesPerSecond", currentMode.getBytesPerSecond()); - return Response.ok(status).build(); + public AllocationMode getMode() { + return mode; } - @POST - @Path("/mode/{mode}") - @Produces(MediaType.APPLICATION_JSON) - public Response setMode(@PathParam("mode") String modeStr) { - try { - AllocationMode mode = AllocationMode.valueOf(modeStr.toUpperCase()); - LOGGER.info(String.format("POST /api/pressure/mode/%s - Setting memory pressure mode to: %s (%.2f MB/sec)", - modeStr, mode.name(), mode.getBytesPerSecond() / (1024.0 * 1024.0))); - - pressureService.setAllocationMode(mode); - - Map result = new HashMap<>(); - result.put("success", true); - result.put("mode", mode.name()); - result.put("description", mode.getDescription()); - result.put("bytesPerSecond", mode.getBytesPerSecond()); - - return Response.ok(result).build(); - } catch (IllegalArgumentException e) { - LOGGER.warning(String.format("POST /api/pressure/mode/%s - Invalid mode requested", modeStr)); - - Map error = new HashMap<>(); - error.put("success", false); - error.put("error", "Invalid mode: " + modeStr); - error.put("validModes", new String[]{"OFF", "LOW", "MEDIUM", "HIGH", "EXTREME"}); - return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); - } + public String getDescription() { + return description; } - - @POST - @Path("/scenario/{scenario}") - @Produces(MediaType.APPLICATION_JSON) - public Response applyScenario(@PathParam("scenario") String scenarioName) { - try { - StressScenario scenario = StressScenario.valueOf(scenarioName.toUpperCase()); - LOGGER.info("Applying scenario: " + scenario.name()); - - pressureService.setAllocationMode(scenario.getMode()); - - return Response.ok(Map.of( - "scenario", scenario.name(), - "description", scenario.getDescription(), - "mode", scenario.getMode(), - "status", "applied" - )).build(); - - } catch (IllegalArgumentException e) { - return Response.status(Response.Status.BAD_REQUEST) - .entity(Map.of("error", "Invalid scenario: " + scenarioName)) - .build(); - } + } + + @GET + @Path("/status") + @Produces(MediaType.APPLICATION_JSON) + public Response getStatus() { + AllocationMode currentMode = pressureService.getCurrentMode(); + LOGGER.fine(String.format("GET /api/pressure/status - Current mode: %s", currentMode.name())); + + Map status = new HashMap<>(); + status.put("currentMode", currentMode.name()); + status.put("description", currentMode.getDescription()); + status.put("running", pressureService.isRunning()); + status.put("bytesPerSecond", currentMode.getBytesPerSecond()); + return Response.ok(status).build(); + } + + @POST + @Path("/mode/{mode}") + @Produces(MediaType.APPLICATION_JSON) + public Response setMode(@PathParam("mode") String modeStr) { + try { + AllocationMode mode = AllocationMode.valueOf(modeStr.toUpperCase()); + LOGGER.info( + String.format( + "POST /api/pressure/mode/%s - Setting memory pressure mode to: %s (%.2f MB/sec)", + modeStr, mode.name(), mode.getBytesPerSecond() / (1024.0 * 1024.0))); + + pressureService.setAllocationMode(mode); + + Map result = new HashMap<>(); + result.put("success", true); + result.put("mode", mode.name()); + result.put("description", mode.getDescription()); + result.put("bytesPerSecond", mode.getBytesPerSecond()); + + return Response.ok(result).build(); + } catch (IllegalArgumentException e) { + LOGGER.warning(String.format("POST /api/pressure/mode/%s - Invalid mode requested", modeStr)); + + Map error = new HashMap<>(); + error.put("success", false); + error.put("error", "Invalid mode: " + modeStr); + error.put("validModes", new String[] {"OFF", "LOW", "MEDIUM", "HIGH", "EXTREME"}); + return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } - - @GET - @Path("/scenarios") - @Produces(MediaType.APPLICATION_JSON) - public Response listScenarios() { - List> scenarios = Arrays.stream(StressScenario.values()) - .map(s -> Map.of( - "name", s.name(), - "description", s.getDescription(), - "mode", s.getMode().toString() - )) - .collect(Collectors.toList()); - - return Response.ok(scenarios).build(); + } + + @POST + @Path("/scenario/{scenario}") + @Produces(MediaType.APPLICATION_JSON) + public Response applyScenario(@PathParam("scenario") String scenarioName) { + try { + StressScenario scenario = StressScenario.valueOf(scenarioName.toUpperCase()); + LOGGER.info("Applying scenario: " + scenario.name()); + + pressureService.setAllocationMode(scenario.getMode()); + + return Response.ok( + Map.of( + "scenario", scenario.name(), + "description", scenario.getDescription(), + "mode", scenario.getMode(), + "status", "applied")) + .build(); + + } catch (IllegalArgumentException e) { + return Response.status(Response.Status.BAD_REQUEST) + .entity(Map.of("error", "Invalid scenario: " + scenarioName)) + .build(); } + } + + @GET + @Path("/scenarios") + @Produces(MediaType.APPLICATION_JSON) + public Response listScenarios() { + List> scenarios = + Arrays.stream(StressScenario.values()) + .map( + s -> + Map.of( + "name", s.name(), + "description", s.getDescription(), + "mode", s.getMode().toString())) + .collect(Collectors.toList()); - @GET - @Path("/modes") - @Produces(MediaType.APPLICATION_JSON) - public Response getModes() { - LOGGER.fine("GET /api/pressure/modes - Listing all allocation modes"); + return Response.ok(scenarios).build(); + } - Map> modes = new HashMap<>(); + @GET + @Path("/modes") + @Produces(MediaType.APPLICATION_JSON) + public Response getModes() { + LOGGER.fine("GET /api/pressure/modes - Listing all allocation modes"); - for (AllocationMode mode : AllocationMode.values()) { - Map modeInfo = new HashMap<>(); - modeInfo.put("name", mode.name()); - modeInfo.put("description", mode.getDescription()); - modeInfo.put("bytesPerSecond", mode.getBytesPerSecond()); - modes.put(mode.name(), modeInfo); - } + Map> modes = new HashMap<>(); - return Response.ok(modes).build(); + for (AllocationMode mode : AllocationMode.values()) { + Map modeInfo = new HashMap<>(); + modeInfo.put("name", mode.name()); + modeInfo.put("description", mode.getDescription()); + modeInfo.put("bytesPerSecond", mode.getBytesPerSecond()); + modes.put(mode.name(), modeInfo); } + + return Response.ok(modes).build(); + } } diff --git a/src/main/java/fish/payara/trader/rest/StatusResource.java b/src/main/java/fish/payara/trader/rest/StatusResource.java index 676e70e..c91f006 100644 --- a/src/main/java/fish/payara/trader/rest/StatusResource.java +++ b/src/main/java/fish/payara/trader/rest/StatusResource.java @@ -1,6 +1,5 @@ package fish.payara.trader.rest; -import com.hazelcast.cluster.Member; import com.hazelcast.core.HazelcastInstance; import fish.payara.trader.aeron.AeronSubscriberBean; import fish.payara.trader.aeron.MarketDataPublisher; @@ -11,98 +10,88 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; - import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; -/** - * REST endpoint for monitoring system status - */ +/** REST endpoint for monitoring system status */ @Path("/status") public class StatusResource { - @Inject - private AeronSubscriberBean subscriber; - - @Inject - private MarketDataPublisher publisher; - - @Inject - private MarketDataBroadcaster broadcaster; - - @Inject - private HazelcastInstance hazelcastInstance; - - @GET - @Produces(MediaType.APPLICATION_JSON) - public Response getStatus() { - Map status = new HashMap<>(); + @Inject private AeronSubscriberBean subscriber; - // Get instance name from environment variable - String instanceName = System.getenv("PAYARA_INSTANCE_NAME"); - if (instanceName == null) { - instanceName = "standalone"; - } + @Inject private MarketDataPublisher publisher; - status.put("application", "TradeStreamEE"); - status.put("description", "High-frequency trading dashboard with Aeron and SBE"); - status.put("instance", instanceName); - status.put("subscriber", subscriber.getStatus()); + @Inject private MarketDataBroadcaster broadcaster; - // Include both local and cluster-wide message counts - Map publisherStats = new HashMap<>(); - publisherStats.put("localMessagesPublished", publisher.getMessagesPublished()); - publisherStats.put("clusterMessagesPublished", publisher.getClusterMessagesPublished()); - status.put("publisher", publisherStats); + @Inject private HazelcastInstance hazelcastInstance; - status.put("websocket", Map.of( - "activeSessions", broadcaster.getSessionCount() - )); - status.put("status", "UP"); + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getStatus() { + Map status = new HashMap<>(); - return Response.ok(status).build(); + // Get instance name from environment variable + String instanceName = System.getenv("PAYARA_INSTANCE_NAME"); + if (instanceName == null) { + instanceName = "standalone"; } - /** - * Get cluster status and membership information - */ - @GET - @Path("/cluster") - @Produces(MediaType.APPLICATION_JSON) - public Response getClusterStatus() { - Map clusterInfo = new HashMap<>(); - - try { - if (hazelcastInstance == null) { - clusterInfo.put("clustered", false); - clusterInfo.put("message", "Running in standalone mode (Hazelcast not available)"); - return Response.ok(clusterInfo).build(); - } - - clusterInfo.put("clustered", true); - clusterInfo.put("clusterSize", hazelcastInstance.getCluster().getMembers().size()); - clusterInfo.put("clusterTime", hazelcastInstance.getCluster().getClusterTime()); - clusterInfo.put("localMemberUuid", hazelcastInstance.getCluster().getLocalMember().getUuid().toString()); - - clusterInfo.put("members", hazelcastInstance.getCluster().getMembers().stream() - .map(member -> Map.of( - "address", member.getAddress().toString(), - "uuid", member.getUuid().toString(), - "localMember", member.localMember(), - "liteMember", member.isLiteMember() - )) - .collect(Collectors.toList()) - ); - - return Response.ok(clusterInfo).build(); - - } catch (Exception e) { - clusterInfo.put("clustered", false); - clusterInfo.put("error", e.getMessage()); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(clusterInfo) - .build(); - } + status.put("application", "TradeStreamEE"); + status.put("description", "High-frequency trading dashboard with Aeron and SBE"); + status.put("instance", instanceName); + status.put("subscriber", subscriber.getStatus()); + + // Include both local and cluster-wide message counts + Map publisherStats = new HashMap<>(); + publisherStats.put("localMessagesPublished", publisher.getMessagesPublished()); + publisherStats.put("clusterMessagesPublished", publisher.getClusterMessagesPublished()); + status.put("publisher", publisherStats); + + status.put("websocket", Map.of("activeSessions", broadcaster.getSessionCount())); + status.put("status", "UP"); + + return Response.ok(status).build(); + } + + /** Get cluster status and membership information */ + @GET + @Path("/cluster") + @Produces(MediaType.APPLICATION_JSON) + public Response getClusterStatus() { + Map clusterInfo = new HashMap<>(); + + try { + if (hazelcastInstance == null) { + clusterInfo.put("clustered", false); + clusterInfo.put("message", "Running in standalone mode (Hazelcast not available)"); + return Response.ok(clusterInfo).build(); + } + + clusterInfo.put("clustered", true); + clusterInfo.put("clusterSize", hazelcastInstance.getCluster().getMembers().size()); + clusterInfo.put("clusterTime", hazelcastInstance.getCluster().getClusterTime()); + clusterInfo.put( + "localMemberUuid", hazelcastInstance.getCluster().getLocalMember().getUuid().toString()); + + clusterInfo.put( + "members", + hazelcastInstance.getCluster().getMembers().stream() + .map( + member -> + Map.of( + "address", member.getAddress().toString(), + "uuid", member.getUuid().toString(), + "localMember", member.localMember(), + "liteMember", member.isLiteMember())) + .collect(Collectors.toList())); + + return Response.ok(clusterInfo).build(); + + } catch (Exception e) { + clusterInfo.put("clustered", false); + clusterInfo.put("error", e.getMessage()); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(clusterInfo).build(); } + } } diff --git a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java index af0d892..c03953d 100644 --- a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java +++ b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java @@ -8,8 +8,6 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.websocket.Session; - -import java.io.IOException; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -18,181 +16,177 @@ /** * Broadcaster for market data WebSocket messages. * - * Maintains a set of active WebSocket sessions and broadcasts - * JSON messages to all connected clients. + *

Maintains a set of active WebSocket sessions and broadcasts JSON messages to all connected + * clients. * - * In clustered mode, uses Hazelcast distributed topics to broadcast - * messages across all cluster members, ensuring all WebSocket clients - * receive data regardless of which instance they connect to. + *

In clustered mode, uses Hazelcast distributed topics to broadcast messages across all cluster + * members, ensuring all WebSocket clients receive data regardless of which instance they connect + * to. * - * Note: JSON string creation intentionally generates garbage - * to stress-test Azul's Pauseless GC (C4). + *

Note: JSON string creation intentionally generates garbage to stress-test Azul's Pauseless GC + * (C4). */ @ApplicationScoped public class MarketDataBroadcaster { - private static final Logger LOGGER = Logger.getLogger(MarketDataBroadcaster.class.getName()); - private static final String TOPIC_NAME = "market-data-broadcast"; - - private final Set sessions = ConcurrentHashMap.newKeySet(); - - @Inject - private HazelcastInstance hazelcastInstance; - - @Inject - private fish.payara.trader.monitoring.SLAMonitorService slaMonitor; - - private ITopic clusterTopic; - - // Statistics - private long messagesSent = 0; - private long lastStatsTime = System.currentTimeMillis(); - - /** - * Initialize Hazelcast topic subscription for cluster-wide broadcasting - */ - @PostConstruct - public void init() { - try { - if (hazelcastInstance != null) { - clusterTopic = hazelcastInstance.getTopic(TOPIC_NAME); - clusterTopic.addMessageListener(new MessageListener() { - @Override - public void onMessage(Message message) { - // Broadcast to local WebSocket sessions only - broadcastLocal(message.getMessageObject()); - } - }); - LOGGER.info("Subscribed to Hazelcast topic: " + TOPIC_NAME + " (clustered mode)"); - } else { - LOGGER.info("Hazelcast not available - running in standalone mode"); - } - } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Failed to initialize Hazelcast topic subscription", e); - // Continue without clustering - } + private static final Logger LOGGER = Logger.getLogger(MarketDataBroadcaster.class.getName()); + private static final String TOPIC_NAME = "market-data-broadcast"; + + private final Set sessions = ConcurrentHashMap.newKeySet(); + + @Inject private HazelcastInstance hazelcastInstance; + + @Inject private fish.payara.trader.monitoring.SLAMonitorService slaMonitor; + + private ITopic clusterTopic; + + // Statistics + private long messagesSent = 0; + private long lastStatsTime = System.currentTimeMillis(); + + /** Initialize Hazelcast topic subscription for cluster-wide broadcasting */ + @PostConstruct + public void init() { + try { + if (hazelcastInstance != null) { + clusterTopic = hazelcastInstance.getTopic(TOPIC_NAME); + clusterTopic.addMessageListener( + new MessageListener() { + @Override + public void onMessage(Message message) { + // Broadcast to local WebSocket sessions only + broadcastLocal(message.getMessageObject()); + } + }); + LOGGER.info("Subscribed to Hazelcast topic: " + TOPIC_NAME + " (clustered mode)"); + } else { + LOGGER.info("Hazelcast not available - running in standalone mode"); + } + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "Failed to initialize Hazelcast topic subscription", e); + // Continue without clustering } - - /** - * Register a new WebSocket session - */ - public void addSession(Session session) { - sessions.add(session); - LOGGER.info("WebSocket session added. Total sessions: " + sessions.size()); + } + + /** Register a new WebSocket session */ + public void addSession(Session session) { + sessions.add(session); + LOGGER.info("WebSocket session added. Total sessions: " + sessions.size()); + } + + /** Unregister a WebSocket session */ + public void removeSession(Session session) { + sessions.remove(session); + LOGGER.info("WebSocket session removed. Total sessions: " + sessions.size()); + } + + /** + * Broadcast JSON message to all connected clients across the cluster. + * + *

In clustered mode, publishes to Hazelcast topic which distributes to all cluster members. + * Each member then broadcasts to its local WebSocket sessions. + * + *

In standalone mode, broadcasts directly to local sessions. + * + *

This method intentionally creates string allocations to generate garbage and stress the + * garbage collector. + */ + public void broadcast(String jsonMessage) { + if (clusterTopic != null) { + // Publish to cluster-wide topic + try { + clusterTopic.publish(jsonMessage); + } catch (Exception e) { + LOGGER.log( + Level.SEVERE, + "Failed to publish to Hazelcast topic, falling back to local broadcast", + e); + broadcastLocal(jsonMessage); + } + } else { + // Standalone mode - broadcast locally only + broadcastLocal(jsonMessage); } - - /** - * Unregister a WebSocket session - */ - public void removeSession(Session session) { - sessions.remove(session); - LOGGER.info("WebSocket session removed. Total sessions: " + sessions.size()); - } - - /** - * Broadcast JSON message to all connected clients across the cluster. - * - * In clustered mode, publishes to Hazelcast topic which distributes - * to all cluster members. Each member then broadcasts to its local - * WebSocket sessions. - * - * In standalone mode, broadcasts directly to local sessions. - * - * This method intentionally creates string allocations to - * generate garbage and stress the garbage collector. - */ - public void broadcast(String jsonMessage) { - if (clusterTopic != null) { - // Publish to cluster-wide topic - try { - clusterTopic.publish(jsonMessage); - } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Failed to publish to Hazelcast topic, falling back to local broadcast", e); - broadcastLocal(jsonMessage); - } - } else { - // Standalone mode - broadcast locally only - broadcastLocal(jsonMessage); - } + } + + /** + * Broadcast message to local WebSocket sessions only. Called either in standalone mode or by + * Hazelcast topic listener. + */ + private void broadcastLocal(String jsonMessage) { + if (sessions.isEmpty()) { + return; } - /** - * Broadcast message to local WebSocket sessions only. - * Called either in standalone mode or by Hazelcast topic listener. - */ - private void broadcastLocal(String jsonMessage) { - if (sessions.isEmpty()) { - return; - } - - long startTime = System.currentTimeMillis(); - - sessions.removeIf(session -> { - if (!session.isOpen()) { - return true; - } - try { - session.getAsyncRemote().sendText(jsonMessage); - return false; - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to send message", e); - return true; - } + long startTime = System.currentTimeMillis(); + + sessions.removeIf( + session -> { + if (!session.isOpen()) { + return true; + } + try { + session.getAsyncRemote().sendText(jsonMessage); + return false; + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to send message", e); + return true; + } }); - long latency = System.currentTimeMillis() - startTime; - if (slaMonitor != null) { - slaMonitor.recordOperation(latency); - } - - logStatistics(); + long latency = System.currentTimeMillis() - startTime; + if (slaMonitor != null) { + slaMonitor.recordOperation(latency); } - /** - * Broadcast to all clients with additional garbage generation for GC stress. - * - * This method wraps the original message in a larger JSON envelope with padding. - * This increases both memory allocation (String construction) and network bandwidth usage, - * simulating a heavier protocol or inefficient data packaging. - */ - public void broadcastWithArtificialLoad(String jsonMessage) { - // Generate 1KB of padding to increase payload size and allocation - String padding = "X".repeat(1024); - - // Wrap the original message in a new JSON structure - // We use StringBuilder to explicitly construct the new JSON string - String enrichedMessage = new StringBuilder(jsonMessage.length() + padding.length() + 100) + logStatistics(); + } + + /** + * Broadcast to all clients with additional garbage generation for GC stress. + * + *

This method wraps the original message in a larger JSON envelope with padding. This + * increases both memory allocation (String construction) and network bandwidth usage, simulating + * a heavier protocol or inefficient data packaging. + */ + public void broadcastWithArtificialLoad(String jsonMessage) { + // Generate 1KB of padding to increase payload size and allocation + String padding = "X".repeat(1024); + + // Wrap the original message in a new JSON structure + // We use StringBuilder to explicitly construct the new JSON string + String enrichedMessage = + new StringBuilder(jsonMessage.length() + padding.length() + 100) .append("{\"wrapped\":true,") - .append("\"timestamp\":").append(System.nanoTime()).append(",") - .append("\"padding\":\"").append(padding).append("\",") - .append("\"data\":").append(jsonMessage) + .append("\"timestamp\":") + .append(System.nanoTime()) + .append(",") + .append("\"padding\":\"") + .append(padding) + .append("\",") + .append("\"data\":") + .append(jsonMessage) .append("}") .toString(); - broadcast(enrichedMessage); - } - - /** - * Get count of active sessions - */ - public int getSessionCount() { - return sessions.size(); - } - - /** - * Log statistics periodically - */ - private void logStatistics() { - long now = System.currentTimeMillis(); - if (now - lastStatsTime > 10000) { // Log every 10 seconds - LOGGER.info(String.format( - "WebSocket Stats - Active sessions: %d, Messages sent: %,d (%.1f msg/sec)", - sessions.size(), - messagesSent, - messagesSent / ((now - lastStatsTime) / 1000.0) - )); - lastStatsTime = now; - messagesSent = 0; - } + broadcast(enrichedMessage); + } + + /** Get count of active sessions */ + public int getSessionCount() { + return sessions.size(); + } + + /** Log statistics periodically */ + private void logStatistics() { + long now = System.currentTimeMillis(); + if (now - lastStatsTime > 10000) { // Log every 10 seconds + LOGGER.info( + String.format( + "WebSocket Stats - Active sessions: %d, Messages sent: %,d (%.1f msg/sec)", + sessions.size(), messagesSent, messagesSent / ((now - lastStatsTime) / 1000.0))); + lastStatsTime = now; + messagesSent = 0; } + } } diff --git a/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java b/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java index e222e34..0ed3f73 100644 --- a/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java +++ b/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java @@ -3,71 +3,70 @@ import jakarta.inject.Inject; import jakarta.websocket.*; import jakarta.websocket.server.ServerEndpoint; -import org.eclipse.microprofile.config.inject.ConfigProperty; - import java.util.logging.Level; import java.util.logging.Logger; +import org.eclipse.microprofile.config.inject.ConfigProperty; /** * WebSocket endpoint for streaming market data to clients. * - * Clients connect to ws://host:port/context/market-data - * and receive real-time JSON market data updates. + *

Clients connect to ws://host:port/context/market-data and receive real-time JSON market data + * updates. */ @ServerEndpoint("/market-data") public class MarketDataWebSocket { - private static final Logger LOGGER = Logger.getLogger(MarketDataWebSocket.class.getName()); + private static final Logger LOGGER = Logger.getLogger(MarketDataWebSocket.class.getName()); - @Inject - private MarketDataBroadcaster broadcaster; + @Inject private MarketDataBroadcaster broadcaster; - @Inject - @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") - private String ingestionMode; + @Inject + @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") + private String ingestionMode; - @OnOpen - public void onOpen(Session session) { - LOGGER.info("WebSocket connection opened: " + session.getId()); - broadcaster.addSession(session); + @OnOpen + public void onOpen(Session session) { + LOGGER.info("WebSocket connection opened: " + session.getId()); + broadcaster.addSession(session); - // Send welcome message with current mode - try { - String welcomeJson = String.format( - "{\"type\":\"info\",\"message\":\"Connected to TradeStreamEE market data feed\",\"mode\":\"%s\"}", - ingestionMode - ); - session.getBasicRemote().sendText(welcomeJson); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to send welcome message", e); - } + // Send welcome message with current mode + try { + String welcomeJson = + String.format( + "{\"type\":\"info\",\"message\":\"Connected to TradeStreamEE market data feed\",\"mode\":\"%s\"}", + ingestionMode); + session.getBasicRemote().sendText(welcomeJson); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to send welcome message", e); } + } - @OnClose - public void onClose(Session session, CloseReason closeReason) { - LOGGER.info("WebSocket connection closed: " + session.getId() + - ", reason: " + closeReason.getReasonPhrase()); - broadcaster.removeSession(session); - } + @OnClose + public void onClose(Session session, CloseReason closeReason) { + LOGGER.info( + "WebSocket connection closed: " + + session.getId() + + ", reason: " + + closeReason.getReasonPhrase()); + broadcaster.removeSession(session); + } - @OnError - public void onError(Session session, Throwable throwable) { - LOGGER.log(Level.WARNING, "WebSocket error for session: " + session.getId(), throwable); - broadcaster.removeSession(session); - } + @OnError + public void onError(Session session, Throwable throwable) { + LOGGER.log(Level.WARNING, "WebSocket error for session: " + session.getId(), throwable); + broadcaster.removeSession(session); + } - @OnMessage - public void onMessage(String message, Session session) { - // Handle client messages if needed (e.g., subscription requests) - LOGGER.fine("Received message from client: " + message); + @OnMessage + public void onMessage(String message, Session session) { + // Handle client messages if needed (e.g., subscription requests) + LOGGER.fine("Received message from client: " + message); - // Echo back for now - try { - session.getBasicRemote().sendText( - "{\"type\":\"ack\",\"message\":\"Message received\"}" - ); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to send acknowledgment", e); - } + // Echo back for now + try { + session.getBasicRemote().sendText("{\"type\":\"ack\",\"message\":\"Message received\"}"); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to send acknowledgment", e); } + } } diff --git a/src/test/java/fish/payara/trader/BasicFunctionalityTest.java b/src/test/java/fish/payara/trader/BasicFunctionalityTest.java new file mode 100644 index 0000000..f15c20e --- /dev/null +++ b/src/test/java/fish/payara/trader/BasicFunctionalityTest.java @@ -0,0 +1,86 @@ +package fish.payara.trader; + +import static org.junit.jupiter.api.Assertions.*; + +import fish.payara.trader.pressure.AllocationMode; +import fish.payara.trader.pressure.MemoryPressureService; +import fish.payara.trader.utils.GCTestUtil; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** Basic functionality tests to ensure the test infrastructure works */ +@DisplayName("Basic Functionality Tests") +class BasicFunctionalityTest { + + @Test + @DisplayName("Should verify allocation modes work correctly") + void shouldVerifyAllocationModesWorkCorrectly() { + // Test AllocationMode enum + assertEquals(AllocationMode.OFF, AllocationMode.valueOf("OFF")); + assertEquals(AllocationMode.LOW, AllocationMode.valueOf("LOW")); + assertEquals(AllocationMode.MEDIUM, AllocationMode.valueOf("MEDIUM")); + assertEquals(AllocationMode.HIGH, AllocationMode.valueOf("HIGH")); + assertEquals(AllocationMode.EXTREME, AllocationMode.valueOf("EXTREME")); + + // Test allocation rates + assertEquals(0L, AllocationMode.OFF.getBytesPerSecond()); + assertTrue(AllocationMode.LOW.getBytesPerSecond() > 0); + assertTrue(AllocationMode.EXTREME.getBytesPerSecond() > AllocationMode.LOW.getBytesPerSecond()); + } + + @Test + @DisplayName("Should initialize memory pressure service without CDI") + void shouldInitializeMemoryPressureServiceWithoutCDI() { + MemoryPressureService service = new MemoryPressureService(); + service.init(); + + // Can't test mode changes without CDI injection, but can verify initialization + assertNotNull(service); + assertEquals(AllocationMode.OFF, service.getCurrentMode()); // Default should be OFF + } + + @Test + @DisplayName("Should capture and calculate GC statistics") + void shouldCaptureAndCalculateGCStatistics() { + // Test GC utility functionality + GCTestUtil.GCStatistics beforeStats = GCTestUtil.captureInitialStats(); + assertNotNull(beforeStats); + assertTrue(beforeStats.collections >= 0); + assertTrue(beforeStats.time >= 0); + + // Create some memory pressure + GCTestUtil.allocateMemory(10); // 10MB + + GCTestUtil.GCStatistics afterStats = GCTestUtil.calculateDelta(beforeStats); + assertNotNull(afterStats); + assertTrue(afterStats.collections >= 0); + assertTrue(afterStats.time >= 0); + } + + @Test + @DisplayName("Should measure operation time correctly") + void shouldMeasureOperationTimeCorrectly() { + long startTime = System.nanoTime(); + + // Simulate some work + try { + Thread.sleep(10); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + long pauseTime = + GCTestUtil.measurePauseTime( + () -> { + // Simulate GC pause + try { + Thread.sleep(5); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }); + + assertTrue(pauseTime >= 0, "Pause time should be non-negative"); + // Note: This might not measure actual GC pause time in all cases, but tests the utility + } +} diff --git a/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java b/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java index 1d33ce9..90aa3a3 100644 --- a/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java +++ b/src/test/java/fish/payara/trader/aeron/MarketDataFragmentHandlerTest.java @@ -1,8 +1,12 @@ package fish.payara.trader.aeron; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +import fish.payara.trader.sbe.*; import fish.payara.trader.websocket.MarketDataBroadcaster; import io.aeron.logbuffer.Header; -import org.agrona.DirectBuffer; +import java.nio.ByteBuffer; import org.agrona.concurrent.UnsafeBuffer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -11,176 +15,167 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import fish.payara.trader.sbe.*; +@ExtendWith(MockitoExtension.class) +class MarketDataFragmentHandlerTest { -import java.nio.ByteBuffer; + private MarketDataFragmentHandler handler; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.*; + @Mock private MarketDataBroadcaster broadcaster; -@ExtendWith(MockitoExtension.class) -class MarketDataFragmentHandlerTest { + @Mock private Header header; - private MarketDataFragmentHandler handler; + @BeforeEach + void setUp() { + handler = new MarketDataFragmentHandler(); + handler.broadcaster = broadcaster; + } - @Mock - private MarketDataBroadcaster broadcaster; + @Test + void testProcessTradeMessage() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); - @Mock - private Header header; + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + TradeEncoder tradeEncoder = new TradeEncoder(); - @BeforeEach - void setUp() { - handler = new MarketDataFragmentHandler(); - handler.broadcaster = broadcaster; - } + // Send 50 messages to ensure sampling triggers + for (int i = 0; i < 50; i++) { + tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + tradeEncoder.timestamp(System.currentTimeMillis()); + tradeEncoder.symbol("AAPL"); + tradeEncoder.price(15000); + tradeEncoder.quantity(100); - @Test - void testProcessTradeMessage() { - ByteBuffer byteBuffer = ByteBuffer.allocate(256); - UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); - - MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); - TradeEncoder tradeEncoder = new TradeEncoder(); - - // Send 50 messages to ensure sampling triggers - for (int i = 0; i < 50; i++) { - tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); - tradeEncoder.timestamp(System.currentTimeMillis()); - tradeEncoder.symbol("AAPL"); - tradeEncoder.price(15000); - tradeEncoder.quantity(100); - - int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); - handler.onFragment(buffer, 0, encodedLength, header); - } - - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); - - String broadcastedMessage = messageCaptor.getValue(); - assertThat(broadcastedMessage).contains("\"type\":\"trade\""); - assertThat(broadcastedMessage).contains("\"symbol\":\"AAPL\""); - assertThat(broadcastedMessage).contains("\"price\":1.5"); - assertThat(broadcastedMessage).contains("\"quantity\":100"); + int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); } - @Test - void testProcessQuoteMessage() { - ByteBuffer byteBuffer = ByteBuffer.allocate(256); - UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); - - MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); - QuoteEncoder quoteEncoder = new QuoteEncoder(); - - // Send 50 messages to ensure sampling triggers - for (int i = 0; i < 50; i++) { - quoteEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); - quoteEncoder.timestamp(System.currentTimeMillis()); - quoteEncoder.symbol("MSFT"); - quoteEncoder.bidPrice(30000); - quoteEncoder.askPrice(30100); - quoteEncoder.bidSize(200); - quoteEncoder.askSize(150); - - int encodedLength = headerEncoder.encodedLength() + quoteEncoder.encodedLength(); - handler.onFragment(buffer, 0, encodedLength, header); - } - - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); - - String broadcastedMessage = messageCaptor.getValue(); - assertThat(broadcastedMessage).contains("\"type\":\"quote\""); - assertThat(broadcastedMessage).contains("\"symbol\":\"MSFT\""); - assertThat(broadcastedMessage).contains("\"bid\":{\"price\":3.0"); - assertThat(broadcastedMessage).contains("\"ask\":{\"price\":3.01"); + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); + + String broadcastedMessage = messageCaptor.getValue(); + assertThat(broadcastedMessage).contains("\"type\":\"trade\""); + assertThat(broadcastedMessage).contains("\"symbol\":\"AAPL\""); + assertThat(broadcastedMessage).contains("\"price\":1.5"); + assertThat(broadcastedMessage).contains("\"quantity\":100"); + } + + @Test + void testProcessQuoteMessage() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + QuoteEncoder quoteEncoder = new QuoteEncoder(); + + // Send 50 messages to ensure sampling triggers + for (int i = 0; i < 50; i++) { + quoteEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + quoteEncoder.timestamp(System.currentTimeMillis()); + quoteEncoder.symbol("MSFT"); + quoteEncoder.bidPrice(30000); + quoteEncoder.askPrice(30100); + quoteEncoder.bidSize(200); + quoteEncoder.askSize(150); + + int encodedLength = headerEncoder.encodedLength() + quoteEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); } - @Test - void testProcessHeartbeatMessage() { - ByteBuffer byteBuffer = ByteBuffer.allocate(256); - UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); - MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); - HeartbeatEncoder heartbeatEncoder = new HeartbeatEncoder(); + String broadcastedMessage = messageCaptor.getValue(); + assertThat(broadcastedMessage).contains("\"type\":\"quote\""); + assertThat(broadcastedMessage).contains("\"symbol\":\"MSFT\""); + assertThat(broadcastedMessage).contains("\"bid\":{\"price\":3.0"); + assertThat(broadcastedMessage).contains("\"ask\":{\"price\":3.01"); + } - heartbeatEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); - heartbeatEncoder.timestamp(System.currentTimeMillis()); + @Test + void testProcessHeartbeatMessage() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); - int encodedLength = headerEncoder.encodedLength() + heartbeatEncoder.encodedLength(); + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + HeartbeatEncoder heartbeatEncoder = new HeartbeatEncoder(); - handler.onFragment(buffer, 0, encodedLength, header); + heartbeatEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + heartbeatEncoder.timestamp(System.currentTimeMillis()); - verify(broadcaster, never()).broadcast(any()); - } + int encodedLength = headerEncoder.encodedLength() + heartbeatEncoder.encodedLength(); - @Test - void testMessageSampling() { - ByteBuffer byteBuffer = ByteBuffer.allocate(256); - UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + handler.onFragment(buffer, 0, encodedLength, header); - MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); - TradeEncoder tradeEncoder = new TradeEncoder(); + verify(broadcaster, never()).broadcast(any()); + } - for (int i = 0; i < 100; i++) { - tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); - tradeEncoder.timestamp(System.currentTimeMillis()); - tradeEncoder.symbol("TEST"); - tradeEncoder.price(10000 + i); - tradeEncoder.quantity(100); + @Test + void testMessageSampling() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); - int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); - handler.onFragment(buffer, 0, encodedLength, header); - } + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + TradeEncoder tradeEncoder = new TradeEncoder(); - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(broadcaster, times(2)).broadcast(messageCaptor.capture()); - } + for (int i = 0; i < 100; i++) { + tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + tradeEncoder.timestamp(System.currentTimeMillis()); + tradeEncoder.symbol("TEST"); + tradeEncoder.price(10000 + i); + tradeEncoder.quantity(100); - @Test - void testInvalidTemplateId() { - ByteBuffer byteBuffer = ByteBuffer.allocate(256); - UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); + } - MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); - headerEncoder.wrap(buffer, 0); - headerEncoder.blockLength(0); - headerEncoder.templateId(9999); - headerEncoder.schemaId(1); - headerEncoder.version(0); + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, times(2)).broadcast(messageCaptor.capture()); + } - int encodedLength = headerEncoder.encodedLength(); + @Test + void testInvalidTemplateId() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); - handler.onFragment(buffer, 0, encodedLength, header); + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + headerEncoder.wrap(buffer, 0); + headerEncoder.blockLength(0); + headerEncoder.templateId(9999); + headerEncoder.schemaId(1); + headerEncoder.version(0); - verify(broadcaster, never()).broadcast(any()); - } + int encodedLength = headerEncoder.encodedLength(); - @Test - void testPriceConversion() { - ByteBuffer byteBuffer = ByteBuffer.allocate(256); - UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); + handler.onFragment(buffer, 0, encodedLength, header); - MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); - TradeEncoder tradeEncoder = new TradeEncoder(); + verify(broadcaster, never()).broadcast(any()); + } - // Send 50 messages to ensure sampling triggers - for (int i = 0; i < 50; i++) { - tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); - tradeEncoder.timestamp(System.currentTimeMillis()); - tradeEncoder.symbol("XYZ"); - tradeEncoder.price(123456); - tradeEncoder.quantity(1); + @Test + void testPriceConversion() { + ByteBuffer byteBuffer = ByteBuffer.allocate(256); + UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer); - int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); - handler.onFragment(buffer, 0, encodedLength, header); - } + MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); + TradeEncoder tradeEncoder = new TradeEncoder(); - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); + // Send 50 messages to ensure sampling triggers + for (int i = 0; i < 50; i++) { + tradeEncoder.wrapAndApplyHeader(buffer, 0, headerEncoder); + tradeEncoder.timestamp(System.currentTimeMillis()); + tradeEncoder.symbol("XYZ"); + tradeEncoder.price(123456); + tradeEncoder.quantity(1); - String broadcastedMessage = messageCaptor.getValue(); - assertThat(broadcastedMessage).contains("\"price\":12.3456"); + int encodedLength = headerEncoder.encodedLength() + tradeEncoder.encodedLength(); + handler.onFragment(buffer, 0, encodedLength, header); } + + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(broadcaster, atLeastOnce()).broadcast(messageCaptor.capture()); + + String broadcastedMessage = messageCaptor.getValue(); + assertThat(broadcastedMessage).contains("\"price\":12.3456"); + } } diff --git a/src/test/java/fish/payara/trader/concurrency/ConcurrencyConfigTest.java b/src/test/java/fish/payara/trader/concurrency/ConcurrencyConfigTest.java new file mode 100644 index 0000000..4fa300b --- /dev/null +++ b/src/test/java/fish/payara/trader/concurrency/ConcurrencyConfigTest.java @@ -0,0 +1,312 @@ +package fish.payara.trader.concurrency; + +import static org.junit.jupiter.api.Assertions.*; + +import jakarta.enterprise.concurrent.ManagedExecutorDefinition; +import jakarta.enterprise.context.ApplicationScoped; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** Unit tests for ConcurrencyConfig class */ +@DisplayName("ConcurrencyConfig Tests") +class ConcurrencyConfigTest { + + @Test + @DisplayName("Should have ApplicationScoped annotation") + void shouldHaveApplicationScopedAnnotation() { + // Act + ApplicationScoped annotation = ConcurrencyConfig.class.getAnnotation(ApplicationScoped.class); + + // Assert + assertNotNull(annotation, "ApplicationScoped annotation should be present"); + } + + @Test + @DisplayName("Should have ManagedExecutorDefinition annotation") + void shouldHaveManagedExecutorDefinitionAnnotation() { + // Act + ManagedExecutorDefinition annotation = + ConcurrencyConfig.class.getAnnotation(ManagedExecutorDefinition.class); + + // Assert + assertNotNull(annotation, "ManagedExecutorDefinition annotation should be present"); + } + + @Test + @DisplayName("Should have correct executor definition properties") + void shouldHaveCorrectExecutorDefinitionProperties() { + // Act + ManagedExecutorDefinition annotation = + ConcurrencyConfig.class.getAnnotation(ManagedExecutorDefinition.class); + + // Assert + assertNotNull(annotation, "Annotation should be present"); + + // Check executor name + assertEquals( + "java:module/concurrent/VirtualThreadExecutor", + annotation.name(), + "Executor name should match expected value"); + + // Check virtual threads enabled + assertTrue(annotation.virtual(), "Virtual threads should be enabled"); + + // Check qualifiers + assertEquals(1, annotation.qualifiers().length, "Should have exactly one qualifier"); + assertEquals( + VirtualThreadExecutor.class, + annotation.qualifiers()[0], + "Should use VirtualThreadExecutor qualifier"); + } + + @Test + @DisplayName("Should be a concrete class") + void shouldBeAConcreteClass() { + // Assert + assertFalse( + ConcurrencyConfig.class.isInterface(), "ConcurrencyConfig should be a concrete class"); + assertFalse( + java.lang.reflect.Modifier.isAbstract(ConcurrencyConfig.class.getModifiers()), + "ConcurrencyConfig should not be abstract"); + assertTrue( + ConcurrencyConfig.class.isSynthetic() == false, + "ConcurrencyConfig should not be synthetic"); + } + + @Test + @DisplayName("Should be in correct package") + void shouldBeInCorrectPackage() { + // Assert + assertEquals( + "fish.payara.trader.concurrency", + ConcurrencyConfig.class.getPackage().getName(), + "Should be in fish.payara.trader.concurrency package"); + } + + @Test + @DisplayName("Should have correct simple name") + void shouldHaveCorrectSimpleName() { + // Assert + assertEquals( + "ConcurrencyConfig", + ConcurrencyConfig.class.getSimpleName(), + "Should have correct simple name"); + } + + @Test + @DisplayName("Should have public default constructor") + void shouldHavePublicDefaultConstructor() { + // Act + java.lang.reflect.Constructor[] constructors = ConcurrencyConfig.class.getConstructors(); + + // Assert + assertTrue(constructors.length > 0, "Should have at least one constructor"); + + // Should have a public no-args constructor + boolean hasPublicNoArgsConstructor = false; + for (java.lang.reflect.Constructor constructor : constructors) { + if (constructor.getParameterCount() == 0 + && java.lang.reflect.Modifier.isPublic(constructor.getModifiers())) { + hasPublicNoArgsConstructor = true; + break; + } + } + + assertTrue(hasPublicNoArgsConstructor, "Should have a public no-args constructor"); + } + + @Test + @DisplayName("Should create instances successfully") + void shouldCreateInstancesSuccessfully() { + // Act & Assert + assertDoesNotThrow( + () -> { + ConcurrencyConfig config = new ConcurrencyConfig(); + assertNotNull(config, "Should create instance without throwing exception"); + }, + "Should be able to create instance"); + } + + @Test + @DisplayName("Should not implement any interfaces") + void shouldNotImplementAnyInterfaces() { + // Act + Class[] interfaces = ConcurrencyConfig.class.getInterfaces(); + + // Assert + assertEquals(0, interfaces.length, "Should not implement any interfaces"); + } + + @Test + @DisplayName("Should have correct inheritance hierarchy") + void shouldHaveCorrectInheritanceHierarchy() { + // Act + Class superClass = ConcurrencyConfig.class.getSuperclass(); + + // Assert + assertEquals(Object.class, superClass, "Should extend Object class directly"); + } + + @Test + @DisplayName("Should be annotated with proper lifecycle scope") + void shouldBeAnnotatedWithProperLifecycleScope() { + // Act + ApplicationScoped applicationScoped = + ConcurrencyConfig.class.getAnnotation(ApplicationScoped.class); + + // Assert + assertNotNull(applicationScoped, "Should have ApplicationScoped annotation"); + assertEquals( + "jakarta.enterprise.context.ApplicationScoped", + applicationScoped.annotationType().getName(), + "Should use Jakarta EE ApplicationScoped"); + } + + @Test + @DisplayName("Should configure virtual threads for executor") + void shouldConfigureVirtualThreadsForExecutor() { + // Act + ManagedExecutorDefinition annotation = + ConcurrencyConfig.class.getAnnotation(ManagedExecutorDefinition.class); + + // Assert + assertNotNull(annotation, "Should have ManagedExecutorDefinition"); + assertTrue(annotation.virtual(), "Should enable virtual threads"); + assertEquals( + "java:module/concurrent/VirtualThreadExecutor", + annotation.name(), + "Should use specific jndi name for virtual thread executor"); + } + + @Test + @DisplayName("Should have VirtualThreadExecutor qualifier") + void shouldHaveVirtualThreadExecutorQualifier() { + // Act + ManagedExecutorDefinition annotation = + ConcurrencyConfig.class.getAnnotation(ManagedExecutorDefinition.class); + + // Assert + assertNotNull(annotation.qualifiers(), "Qualifiers array should not be null"); + assertEquals(1, annotation.qualifiers().length, "Should have exactly one qualifier"); + assertEquals( + VirtualThreadExecutor.class, + annotation.qualifiers()[0], + "Should use VirtualThreadExecutor as qualifier"); + } + + @Test + @DisplayName("Should have correct JNDI name pattern") + void shouldHaveCorrectJNDINamePattern() { + // Act + ManagedExecutorDefinition annotation = + ConcurrencyConfig.class.getAnnotation(ManagedExecutorDefinition.class); + String jndiName = annotation.name(); + + // Assert + assertNotNull(jndiName, "JNDI name should not be null"); + assertEquals( + "java:module/concurrent/VirtualThreadExecutor", + jndiName, + "JNDI name should follow expected pattern"); + assertTrue( + jndiName.startsWith("java:module/concurrent/"), + "JNDI name should start with expected prefix"); + assertTrue( + jndiName.endsWith("VirtualThreadExecutor"), "JNDI name should end with expected suffix"); + assertFalse(jndiName.trim().isEmpty(), "JNDI name should not be empty"); + } + + @Test + @DisplayName("Should have no declared fields") + void shouldHaveNoDeclaredFields() { + // Act + java.lang.reflect.Field[] fields = ConcurrencyConfig.class.getDeclaredFields(); + + // Assert + assertEquals(0, fields.length, "Should have no declared fields"); + } + + @Test + @DisplayName("Should have no declared methods") + void shouldHaveNoDeclaredMethods() { + // Act + java.lang.reflect.Method[] methods = ConcurrencyConfig.class.getDeclaredMethods(); + + // Assert + // Java compiler may add synthetic methods, so we check for user-declared methods + long userMethods = + java.util.Arrays.stream(methods).filter(method -> !method.isSynthetic()).count(); + assertEquals(0, userMethods, "Should have no user-declared methods"); + } + + @Test + @DisplayName("Should have exactly two annotations") + void shouldHaveExactlyTwoAnnotations() { + // Act + java.lang.annotation.Annotation[] annotations = ConcurrencyConfig.class.getAnnotations(); + + // Assert + assertEquals(2, annotations.length, "Should have exactly two annotations"); + + // Verify annotation types + boolean hasApplicationScoped = false; + boolean hasManagedExecutorDefinition = false; + + for (java.lang.annotation.Annotation annotation : annotations) { + if (annotation instanceof ApplicationScoped) { + hasApplicationScoped = true; + } else if (annotation instanceof ManagedExecutorDefinition) { + hasManagedExecutorDefinition = true; + } + } + + assertTrue(hasApplicationScoped, "Should have ApplicationScoped annotation"); + assertTrue(hasManagedExecutorDefinition, "Should have ManagedExecutorDefinition annotation"); + } + + @Test + @DisplayName("Should be suitable for CDI bean registration") + void shouldBeSuitableForCDIBeanRegistration() { + // Act & Assert - This test verifies that the class has the right properties + // for CDI bean registration + + // Should have ApplicationScoped annotation + ApplicationScoped applicationScoped = + ConcurrencyConfig.class.getAnnotation(ApplicationScoped.class); + assertNotNull(applicationScoped, "Should have ApplicationScoped for CDI"); + + // Should have ManagedExecutorDefinition for executor configuration + ManagedExecutorDefinition managedExecutorDefinition = + ConcurrencyConfig.class.getAnnotation(ManagedExecutorDefinition.class); + assertNotNull(managedExecutorDefinition, "Should have ManagedExecutorDefinition for CDI"); + + // Should be a concrete class with public constructor + assertDoesNotThrow( + () -> { + new ConcurrencyConfig(); + }, + "Should be instantiable by CDI"); + } + + @Test + @DisplayName("Should be properly configured for virtual threads") + void shouldBeProperlyConfiguredForVirtualThreads() { + // Act + ManagedExecutorDefinition definition = + ConcurrencyConfig.class.getAnnotation(ManagedExecutorDefinition.class); + + // Assert + assertTrue(definition.virtual(), "Should enable virtual threads"); + assertEquals( + "java:module/concurrent/VirtualThreadExecutor", + definition.name(), + "Should have correct JNDI name for virtual threads"); + + // Verify it uses VirtualThreadExecutor qualifier + assertEquals(1, definition.qualifiers().length); + assertEquals( + VirtualThreadExecutor.class, + definition.qualifiers()[0], + "Should use VirtualThreadExecutor qualifier"); + } +} diff --git a/src/test/java/fish/payara/trader/concurrency/VirtualThreadExecutorTest.java b/src/test/java/fish/payara/trader/concurrency/VirtualThreadExecutorTest.java new file mode 100644 index 0000000..0e4ce7f --- /dev/null +++ b/src/test/java/fish/payara/trader/concurrency/VirtualThreadExecutorTest.java @@ -0,0 +1,207 @@ +package fish.payara.trader.concurrency; + +import static org.junit.jupiter.api.Assertions.*; + +import jakarta.inject.Qualifier; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +/** Unit tests for VirtualThreadExecutor annotation */ +@DisplayName("VirtualThreadExecutor Tests") +class VirtualThreadExecutorTest { + + @Test + @DisplayName("Should have correct annotation properties") + void shouldHaveCorrectAnnotationProperties() { + // Act - an annotation cannot annotate itself, so we test its properties instead + Class annotationType = VirtualThreadExecutor.class; + + // Assert + assertTrue(annotationType.isAnnotation(), "VirtualThreadExecutor should be an annotation type"); + assertNotNull(annotationType, "VirtualThreadExecutor class should be present"); + } + + @Test + @DisplayName("Should have Qualifier annotation") + void shouldHaveQualifierAnnotation() { + // Act + Qualifier qualifier = VirtualThreadExecutor.class.getAnnotation(Qualifier.class); + + // Assert + assertNotNull(qualifier, "VirtualThreadExecutor should have Qualifier annotation"); + } + + @Test + @DisplayName("Should have correct target types") + void shouldHaveCorrectTargetTypes() { + // Act + Target target = VirtualThreadExecutor.class.getAnnotation(Target.class); + + // Assert + assertNotNull(target, "Target annotation should be present"); + ElementType[] elementTypes = target.value(); + + assertTrue(containsTarget(elementTypes, ElementType.METHOD), "Should target METHOD"); + assertTrue(containsTarget(elementTypes, ElementType.FIELD), "Should target FIELD"); + assertTrue(containsTarget(elementTypes, ElementType.PARAMETER), "Should target PARAMETER"); + assertTrue(containsTarget(elementTypes, ElementType.TYPE), "Should target TYPE"); + } + + @Test + @DisplayName("Should have correct retention policy") + void shouldHaveCorrectRetentionPolicy() { + // Act + Retention retention = VirtualThreadExecutor.class.getAnnotation(Retention.class); + + // Assert + assertNotNull(retention, "Retention annotation should be present"); + assertEquals( + RetentionPolicy.RUNTIME, retention.value(), "Should have RUNTIME retention policy"); + } + + @Test + @DisplayName("Should be an interface") + void shouldBeAnInterface() { + // Assert + assertTrue( + VirtualThreadExecutor.class.isInterface(), "VirtualThreadExecutor should be an interface"); + } + + @Test + @DisplayName("Should be in correct package") + void shouldBeInCorrectPackage() { + // Assert + assertEquals( + "fish.payara.trader.concurrency", + VirtualThreadExecutor.class.getPackage().getName(), + "Should be in fish.payara.trader.concurrency package"); + } + + @Test + @DisplayName("Should have correct simple name") + void shouldHaveCorrectSimpleName() { + // Assert + assertEquals( + "VirtualThreadExecutor", + VirtualThreadExecutor.class.getSimpleName(), + "Should have correct simple name"); + } + + @Test + @DisplayName("Should be an annotation interface") + void shouldBeAnAnnotationInterface() { + // Act + boolean isAnnotation = VirtualThreadExecutor.class.isAnnotation(); + + // Assert + assertTrue(isAnnotation, "VirtualThreadExecutor should be an annotation interface"); + } + + @Test + @DisplayName("Should have no declared methods") + void shouldHaveNoDeclaredMethods() { + // Act + java.lang.reflect.Method[] methods = VirtualThreadExecutor.class.getDeclaredMethods(); + + // Assert + assertEquals(0, methods.length, "Should have no declared methods"); + } + + @Test + @DisplayName("Should have no declared fields") + void shouldHaveNoDeclaredFields() { + // Act + java.lang.reflect.Field[] fields = VirtualThreadExecutor.class.getDeclaredFields(); + + // Assert + assertEquals(0, fields.length, "Should have no declared fields"); + } + + @Test + @DisplayName("Should have no declared constructors") + void shouldHaveNoDeclaredConstructors() { + // Act + java.lang.reflect.Constructor[] constructors = + VirtualThreadExecutor.class.getDeclaredConstructors(); + + // Assert + assertEquals(0, constructors.length, "Should have no declared constructors"); + } + + @Test + @DisplayName("Should have no declared annotations except standard ones") + void shouldHaveNoDeclaredAnnotationsExceptStandardOnes() { + // Act + java.lang.annotation.Annotation[] annotations = + VirtualThreadExecutor.class.getDeclaredAnnotations(); + + // Assert + // Should have exactly 3 annotations: @Target, @Retention, @Qualifier + assertEquals(3, annotations.length, "Should have exactly 3 declared annotations"); + + // Verify the types of annotations + boolean hasTarget = false; + boolean hasRetention = false; + boolean hasQualifier = false; + + for (java.lang.annotation.Annotation annotation : annotations) { + if (annotation instanceof Target) { + hasTarget = true; + } else if (annotation instanceof Retention) { + hasRetention = true; + } else if (annotation instanceof Qualifier) { + hasQualifier = true; + } + } + + assertTrue(hasTarget, "Should have @Target annotation"); + assertTrue(hasRetention, "Should have @Retention annotation"); + assertTrue(hasQualifier, "Should have @Qualifier annotation"); + } + + @Test + @DisplayName("Should be suitable for dependency injection") + void shouldBeSuitableForDependencyInjection() { + // Act & Assert - This test verifies that the annotation has the right properties + // for use in CDI/dependency injection frameworks + + // Should be a qualifier annotation (jakarta.inject.Qualifier) + Qualifier qualifier = VirtualThreadExecutor.class.getAnnotation(Qualifier.class); + assertNotNull(qualifier, "Should be a CDI Qualifier annotation"); + + // Should be retained at runtime for injection + Retention retention = VirtualThreadExecutor.class.getAnnotation(Retention.class); + assertEquals( + RetentionPolicy.RUNTIME, retention.value(), "Should be retained at runtime for injection"); + } + + @Test + @DisplayName("Should have descriptive documentation") + void shouldHaveDescriptiveDocumentation() { + // Act + String className = VirtualThreadExecutor.class.getSimpleName(); + Package pkg = VirtualThreadExecutor.class.getPackage(); + + // Assert + assertEquals("VirtualThreadExecutor", className, "Should have descriptive name"); + assertEquals( + "fish.payara.trader.concurrency", pkg.getName(), "Should be in meaningful package"); + + // Note: We can't easily test javadoc content without additional libraries, + // but we can verify that the class is documented by its existence and naming + } + + /** Helper method to check if target array contains specific element type */ + private boolean containsTarget(ElementType[] targets, ElementType targetType) { + for (ElementType target : targets) { + if (target == targetType) { + return true; + } + } + return false; + } +} diff --git a/src/test/java/fish/payara/trader/gc/GCStatsServiceTest.java b/src/test/java/fish/payara/trader/gc/GCStatsServiceTest.java new file mode 100644 index 0000000..68ad242 --- /dev/null +++ b/src/test/java/fish/payara/trader/gc/GCStatsServiceTest.java @@ -0,0 +1,141 @@ +package fish.payara.trader.gc; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +import com.sun.management.GarbageCollectionNotificationInfo; +import com.sun.management.GcInfo; +import java.util.List; +import javax.management.Notification; +import javax.management.openmbean.CompositeData; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +@DisplayName("GCStatsService Tests") +class GCStatsServiceTest { + + private GCStatsService gcStatsService; + + @BeforeEach + void setUp() { + gcStatsService = new GCStatsService(); + } + + @Test + @DisplayName("Should handle GC notification and record pause") + void shouldHandleGCNotificationAndRecordPause() { + Notification notification = + new Notification( + GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION, "test", 1L); + CompositeData compositeData = mock(CompositeData.class); + notification.setUserData(compositeData); + + GarbageCollectionNotificationInfo info = mock(GarbageCollectionNotificationInfo.class); + GcInfo gcInfo = mock(GcInfo.class); + + when(info.getGcName()).thenReturn("G1 Young Generation"); + when(info.getGcAction()).thenReturn("end of minor GC"); + when(info.getGcCause()).thenReturn("System.gc()"); + when(info.getGcInfo()).thenReturn(gcInfo); + when(gcInfo.getDuration()).thenReturn(50L); + + try (MockedStatic mockedStatic = + mockStatic(GarbageCollectionNotificationInfo.class)) { + mockedStatic + .when(() -> GarbageCollectionNotificationInfo.from(compositeData)) + .thenReturn(info); + + gcStatsService.handleNotification(notification, null); + } + + List stats = gcStatsService.collectGCStats(); + // We expect at least the G1 Young Generation to be present if the JVM is running G1 + // But since collectGCStats calls ManagementFactory.getGarbageCollectorMXBeans(), + // it depends on the actual JVM. + // However, we can verify the pauseHistory internal state by calling collectGCStats + // and looking for the one we just injected. + + GCStats g1Stats = + stats.stream() + .filter(s -> s.getGcName().equals("G1 Young Generation")) + .findFirst() + .orElse(null); + + if (g1Stats != null) { + assertThat(g1Stats.getLastPauseDuration()).isEqualTo(50L); + assertThat(g1Stats.getRecentPauses()).contains(50L); + } + } + + @Test + @DisplayName("Should filter GPGC concurrent cycle notifications") + void shouldFilterGPGCConcurrentCycleNotifications() { + Notification notification = + new Notification( + GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION, "test", 1L); + CompositeData compositeData = mock(CompositeData.class); + notification.setUserData(compositeData); + + GarbageCollectionNotificationInfo info = mock(GarbageCollectionNotificationInfo.class); + GcInfo gcInfo = mock(GcInfo.class); + + when(info.getGcName()).thenReturn("GPGC"); // This should be filtered + when(info.getGcInfo()).thenReturn(gcInfo); + when(gcInfo.getDuration()).thenReturn(500L); + + try (MockedStatic mockedStatic = + mockStatic(GarbageCollectionNotificationInfo.class)) { + mockedStatic + .when(() -> GarbageCollectionNotificationInfo.from(compositeData)) + .thenReturn(info); + + gcStatsService.handleNotification(notification, null); + } + + List stats = gcStatsService.collectGCStats(); + GCStats gpgcStats = + stats.stream().filter(s -> s.getGcName().equals("GPGC")).findFirst().orElse(null); + + // GPGC should be excluded from stats + assertThat(gpgcStats).isNull(); + } + + @Test + @DisplayName("Should reset statistics") + void shouldResetStatistics() { + Notification notification = + new Notification( + GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION, "test", 1L); + CompositeData compositeData = mock(CompositeData.class); + notification.setUserData(compositeData); + + GarbageCollectionNotificationInfo info = mock(GarbageCollectionNotificationInfo.class); + GcInfo gcInfo = mock(GcInfo.class); + + when(info.getGcName()).thenReturn("Test GC"); + when(info.getGcInfo()).thenReturn(gcInfo); + when(gcInfo.getDuration()).thenReturn(100L); + + try (MockedStatic mockedStatic = + mockStatic(GarbageCollectionNotificationInfo.class)) { + mockedStatic + .when(() -> GarbageCollectionNotificationInfo.from(compositeData)) + .thenReturn(info); + gcStatsService.handleNotification(notification, null); + } + + gcStatsService.resetStats(); + + // After reset, even if the bean exists in JVM, the pause history should be gone + List stats = gcStatsService.collectGCStats(); + for (GCStats s : stats) { + assertThat(s.getRecentPauses()).isEmpty(); + assertThat(s.getLastPauseDuration()).isZero(); + } + } +} diff --git a/src/test/java/fish/payara/trader/gc/GCStatsTest.java b/src/test/java/fish/payara/trader/gc/GCStatsTest.java new file mode 100644 index 0000000..b22e594 --- /dev/null +++ b/src/test/java/fish/payara/trader/gc/GCStatsTest.java @@ -0,0 +1,559 @@ +package fish.payara.trader.gc; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +/** Unit tests for GCStats data class */ +@DisplayName("GCStats Tests") +class GCStatsTest { + + @Nested + @DisplayName("Constructor and Basic Properties Tests") + class ConstructorAndBasicPropertiesTests { + + @Test + @DisplayName("Should create GCStats with default constructor") + void shouldCreateGCStatsWithDefaultConstructor() { + GCStats stats = new GCStats(); + + assertNotNull(stats, "GCStats should not be null"); + assertNull(stats.getGcName(), "GC name should be null by default"); + assertEquals(0L, stats.getCollectionCount(), "Collection count should default to 0"); + assertEquals(0L, stats.getCollectionTime(), "Collection time should default to 0"); + assertEquals(0L, stats.getLastPauseDuration(), "Last pause duration should default to 0"); + assertNull(stats.getRecentPauses(), "Recent pauses should be null by default"); + assertNull(stats.getPercentiles(), "Percentiles should be null by default"); + assertEquals(0L, stats.getTotalMemory(), "Total memory should default to 0"); + assertEquals(0L, stats.getUsedMemory(), "Used memory should default to 0"); + assertEquals(0L, stats.getFreeMemory(), "Free memory should default to 0"); + } + } + + @Nested + @DisplayName("GC Name Tests") + class GCNameTests { + + @Test + @DisplayName("Should set and get GC name correctly") + void shouldSetAndGetGCNameCorrectly() { + GCStats stats = new GCStats(); + String gcName = "G1 Young Generation"; + + stats.setGcName(gcName); + assertEquals(gcName, stats.getGcName(), "GC name should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should handle null GC name") + void shouldHandleNullGCName() { + GCStats stats = new GCStats(); + + assertDoesNotThrow(() -> stats.setGcName(null), "Setting null GC name should not throw"); + assertNull(stats.getGcName(), "GC name should be null"); + } + + @Test + @DisplayName("Should handle empty GC name") + void shouldHandleEmptyGCName() { + GCStats stats = new GCStats(); + String emptyName = ""; + + stats.setGcName(emptyName); + assertEquals(emptyName, stats.getGcName(), "Empty GC name should be handled"); + } + + @Test + @DisplayName("Should handle special characters in GC name") + void shouldHandleSpecialCharactersInGCName() { + GCStats stats = new GCStats(); + String specialName = "G1/Young (Test) @#$%^&*()"; + + stats.setGcName(specialName); + assertEquals(specialName, stats.getGcName(), "Special characters should be preserved"); + } + } + + @Nested + @DisplayName("Collection Statistics Tests") + class CollectionStatisticsTests { + + @Test + @DisplayName("Should set and get collection count correctly") + void shouldSetAndGetCollectionCountCorrectly() { + GCStats stats = new GCStats(); + long collectionCount = 12345L; + + stats.setCollectionCount(collectionCount); + assertEquals( + collectionCount, + stats.getCollectionCount(), + "Collection count should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should handle zero collection count") + void shouldHandleZeroCollectionCount() { + GCStats stats = new GCStats(); + + stats.setCollectionCount(0L); + assertEquals(0L, stats.getCollectionCount(), "Zero collection count should be handled"); + } + + @Test + @DisplayName("Should handle negative collection count") + void shouldHandleNegativeCollectionCount() { + GCStats stats = new GCStats(); + long negativeCount = -100L; + + stats.setCollectionCount(negativeCount); + assertEquals( + negativeCount, stats.getCollectionCount(), "Negative collection count should be handled"); + } + + @Test + @DisplayName("Should handle very large collection count") + void shouldHandleVeryLargeCollectionCount() { + GCStats stats = new GCStats(); + long largeCount = Long.MAX_VALUE; + + stats.setCollectionCount(largeCount); + assertEquals( + largeCount, stats.getCollectionCount(), "Very large collection count should be handled"); + } + + @Test + @DisplayName("Should set and get collection time correctly") + void shouldSetAndGetCollectionTimeCorrectly() { + GCStats stats = new GCStats(); + long collectionTime = 98765L; + + stats.setCollectionTime(collectionTime); + assertEquals( + collectionTime, + stats.getCollectionTime(), + "Collection time should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should handle zero collection time") + void shouldHandleZeroCollectionTime() { + GCStats stats = new GCStats(); + + stats.setCollectionTime(0L); + assertEquals(0L, stats.getCollectionTime(), "Zero collection time should be handled"); + } + + @Test + @DisplayName("Should handle negative collection time") + void shouldHandleNegativeCollectionTime() { + GCStats stats = new GCStats(); + long negativeTime = -500L; + + stats.setCollectionTime(negativeTime); + assertEquals( + negativeTime, stats.getCollectionTime(), "Negative collection time should be handled"); + } + } + + @Nested + @DisplayName("Pause Duration Tests") + class PauseDurationTests { + + @Test + @DisplayName("Should set and get last pause duration correctly") + void shouldSetAndGetLastPauseDurationCorrectly() { + GCStats stats = new GCStats(); + long pauseDuration = 150L; + + stats.setLastPauseDuration(pauseDuration); + assertEquals( + pauseDuration, + stats.getLastPauseDuration(), + "Last pause duration should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should handle zero pause duration") + void shouldHandleZeroPauseDuration() { + GCStats stats = new GCStats(); + + stats.setLastPauseDuration(0L); + assertEquals(0L, stats.getLastPauseDuration(), "Zero pause duration should be handled"); + } + + @Test + @DisplayName("Should handle very long pause duration") + void shouldHandleVeryLongPauseDuration() { + GCStats stats = new GCStats(); + long longPause = 30000L; // 30 seconds + + stats.setLastPauseDuration(longPause); + assertEquals( + longPause, stats.getLastPauseDuration(), "Very long pause duration should be handled"); + } + + @Test + @DisplayName("Should handle microsecond-level pause duration") + void shouldHandleMicrosecondLevelPauseDuration() { + GCStats stats = new GCStats(); + long microPause = 1L; // 1 millisecond (micro in practice) + + stats.setLastPauseDuration(microPause); + assertEquals( + microPause, + stats.getLastPauseDuration(), + "Microsecond-level pause duration should be handled"); + } + } + + @Nested + @DisplayName("Recent Pauses Tests") + class RecentPausesTests { + + @Test + @DisplayName("Should set and get recent pauses list correctly") + void shouldSetAndGetRecentPausesListCorrectly() { + GCStats stats = new GCStats(); + List pauses = Arrays.asList(10L, 25L, 15L, 30L, 12L); + + stats.setRecentPauses(pauses); + assertEquals( + pauses, + stats.getRecentPauses(), + "Recent pauses list should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should handle empty recent pauses list") + void shouldHandleEmptyRecentPausesList() { + GCStats stats = new GCStats(); + List emptyList = List.of(); + + stats.setRecentPauses(emptyList); + assertEquals( + emptyList, stats.getRecentPauses(), "Empty recent pauses list should be handled"); + } + + @Test + @DisplayName("Should handle null recent pauses list") + void shouldHandleNullRecentPausesList() { + GCStats stats = new GCStats(); + + assertDoesNotThrow( + () -> stats.setRecentPauses(null), "Setting null recent pauses should not throw"); + assertNull(stats.getRecentPauses(), "Recent pauses should be null"); + } + + @Test + @DisplayName("Should handle recent pauses with zero values") + void shouldHandleRecentPausesWithZeroValues() { + GCStats stats = new GCStats(); + List zeros = Arrays.asList(0L, 0L, 0L, 0L); + + stats.setRecentPauses(zeros); + assertEquals(zeros, stats.getRecentPauses(), "Zero pause values should be handled"); + } + + @Test + @DisplayName("Should handle recent pauses with mixed values") + void shouldHandleRecentPausesWithMixedValues() { + GCStats stats = new GCStats(); + List mixedValues = Arrays.asList(5L, 0L, 1000L, 1L, 500L); + + stats.setRecentPauses(mixedValues); + assertEquals(mixedValues, stats.getRecentPauses(), "Mixed pause values should be handled"); + } + } + + @Nested + @DisplayName("Percentiles Tests") + class PercentilesTests { + + @Test + @DisplayName("Should set and get percentiles correctly") + void shouldSetAndGetPercentilesCorrectly() { + GCStats stats = new GCStats(); + GCStats.PausePercentiles percentiles = + new GCStats.PausePercentiles(10L, 25L, 50L, 100L, 200L); + + stats.setPercentiles(percentiles); + assertEquals( + percentiles, stats.getPercentiles(), "Percentiles should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should handle null percentiles") + void shouldHandleNullPercentiles() { + GCStats stats = new GCStats(); + + assertDoesNotThrow( + () -> stats.setPercentiles(null), "Setting null percentiles should not throw"); + assertNull(stats.getPercentiles(), "Percentiles should be null"); + } + + @Test + @DisplayName("Should handle all zero percentile values") + void shouldHandleAllZeroPercentileValues() { + GCStats stats = new GCStats(); + GCStats.PausePercentiles zeroPercentiles = new GCStats.PausePercentiles(0L, 0L, 0L, 0L, 0L); + + stats.setPercentiles(zeroPercentiles); + assertEquals(0L, stats.getPercentiles().getP50(), "P50 should be zero"); + assertEquals(0L, stats.getPercentiles().getP95(), "P95 should be zero"); + assertEquals(0L, stats.getPercentiles().getP99(), "P99 should be zero"); + assertEquals(0L, stats.getPercentiles().getP999(), "P999 should be zero"); + assertEquals(0L, stats.getPercentiles().getMax(), "Max should be zero"); + } + + @Test + @DisplayName("Should handle percentile with constructor") + void shouldHandlePercentileWithConstructor() { + long p50 = 15L, p95 = 50L, p99 = 120L, p999 = 300L, max = 500L; + GCStats.PausePercentiles percentiles = new GCStats.PausePercentiles(p50, p95, p99, p999, max); + + assertEquals(p50, percentiles.getP50(), "P50 should match constructor value"); + assertEquals(p95, percentiles.getP95(), "P95 should match constructor value"); + assertEquals(p99, percentiles.getP99(), "P99 should match constructor value"); + assertEquals(p999, percentiles.getP999(), "P999 should match constructor value"); + assertEquals(max, percentiles.getMax(), "Max should match constructor value"); + } + + @Test + @DisplayName("Should handle percentile setters and getters") + void shouldHandlePercentileSettersAndGetters() { + GCStats.PausePercentiles percentiles = new GCStats.PausePercentiles(); + + long p50 = 25L, p95 = 75L, p99 = 150L, p999 = 400L, max = 800L; + + percentiles.setP50(p50); + percentiles.setP95(p95); + percentiles.setP99(p99); + percentiles.setP999(p999); + percentiles.setMax(max); + + assertEquals(p50, percentiles.getP50(), "P50 should be set correctly"); + assertEquals(p95, percentiles.getP95(), "P95 should be set correctly"); + assertEquals(p99, percentiles.getP99(), "P99 should be set correctly"); + assertEquals(p999, percentiles.getP999(), "P999 should be set correctly"); + assertEquals(max, percentiles.getMax(), "Max should be set correctly"); + } + } + + @Nested + @DisplayName("Memory Statistics Tests") + class MemoryStatisticsTests { + + @Test + @DisplayName("Should set and get total memory correctly") + void shouldSetAndGetTotalMemoryCorrectly() { + GCStats stats = new GCStats(); + long totalMemory = 1024L * 1024L * 1024L; // 1GB + + stats.setTotalMemory(totalMemory); + assertEquals( + totalMemory, + stats.getTotalMemory(), + "Total memory should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should set and get used memory correctly") + void shouldSetAndGetUsedMemoryCorrectly() { + GCStats stats = new GCStats(); + long usedMemory = 512L * 1024L * 1024L; // 512MB + + stats.setUsedMemory(usedMemory); + assertEquals( + usedMemory, stats.getUsedMemory(), "Used memory should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should set and get free memory correctly") + void shouldSetAndGetFreeMemoryCorrectly() { + GCStats stats = new GCStats(); + long freeMemory = 256L * 1024L * 1024L; // 256MB + + stats.setFreeMemory(freeMemory); + assertEquals( + freeMemory, stats.getFreeMemory(), "Free memory should be set and retrieved correctly"); + } + + @Test + @DisplayName("Should maintain memory relationship: total >= used") + void shouldMaintainMemoryRelationshipTotalGTEUsed() { + GCStats stats = new GCStats(); + long total = 1000L; + long used = 750L; + + stats.setTotalMemory(total); + stats.setUsedMemory(used); + + assertTrue( + stats.getTotalMemory() >= stats.getUsedMemory(), + "Total memory should be greater than or equal to used memory"); + } + + @Test + @DisplayName("Should maintain memory relationship: total >= free") + void shouldMaintainMemoryRelationshipTotalGTFree() { + GCStats stats = new GCStats(); + long total = 1000L; + long free = 300L; + + stats.setTotalMemory(total); + stats.setFreeMemory(free); + + assertTrue( + stats.getTotalMemory() >= stats.getFreeMemory(), + "Total memory should be greater than or equal to free memory"); + } + + @Test + @DisplayName("Should maintain memory relationship: total >= used + free") + void shouldMaintainMemoryRelationshipTotalGEUsedPlusFree() { + GCStats stats = new GCStats(); + long total = 1000L; + long used = 600L; + long free = 400L; + + stats.setTotalMemory(total); + stats.setUsedMemory(used); + stats.setFreeMemory(free); + + assertTrue( + stats.getTotalMemory() >= stats.getUsedMemory() + stats.getFreeMemory(), + "Total memory should be greater than or equal to used + free memory"); + } + + @Test + @DisplayName("Should handle zero memory values") + void shouldHandleZeroMemoryValues() { + GCStats stats = new GCStats(); + + stats.setTotalMemory(0L); + stats.setUsedMemory(0L); + stats.setFreeMemory(0L); + + assertEquals(0L, stats.getTotalMemory(), "Total memory should handle zero"); + assertEquals(0L, stats.getUsedMemory(), "Used memory should handle zero"); + assertEquals(0L, stats.getFreeMemory(), "Free memory should handle zero"); + } + + @Test + @DisplayName("Should handle large memory values") + void shouldHandleLargeMemoryValues() { + GCStats stats = new GCStats(); + long largeValue = Long.MAX_VALUE / 2; // Half of max long value + + stats.setTotalMemory(largeValue); + stats.setUsedMemory(largeValue / 2); + stats.setFreeMemory(largeValue / 4); + + assertEquals(largeValue, stats.getTotalMemory(), "Large total memory should be handled"); + assertEquals(largeValue / 2, stats.getUsedMemory(), "Large used memory should be handled"); + assertEquals(largeValue / 4, stats.getFreeMemory(), "Large free memory should be handled"); + } + } + + @Nested + @DisplayName("Consistency Tests") + class ConsistencyTests { + + @Test + @DisplayName("Should maintain consistent state across multiple property changes") + void shouldMaintainConsistentStateAcrossMultiplePropertyChanges() { + GCStats stats = new GCStats(); + + // Set all properties + stats.setGcName("Test GC"); + stats.setCollectionCount(100L); + stats.setCollectionTime(5000L); + stats.setLastPauseDuration(50L); + stats.setRecentPauses(List.of(10L, 20L, 30L)); + stats.setPercentiles(new GCStats.PausePercentiles(15L, 25L, 40L, 60L, 100L)); + stats.setTotalMemory(1024L); + stats.setUsedMemory(512L); + stats.setFreeMemory(256L); + + // Verify all properties are still correct + assertEquals("Test GC", stats.getGcName()); + assertEquals(100L, stats.getCollectionCount()); + assertEquals(5000L, stats.getCollectionTime()); + assertEquals(50L, stats.getLastPauseDuration()); + assertEquals(List.of(10L, 20L, 30L), stats.getRecentPauses()); + assertEquals(15L, stats.getPercentiles().getP50()); + assertEquals(1024L, stats.getTotalMemory()); + assertEquals(512L, stats.getUsedMemory()); + assertEquals(256L, stats.getFreeMemory()); + } + + @Test + @DisplayName("Should handle independent property modifications") + void shouldHandleIndependentPropertyModifications() { + GCStats stats = new GCStats(); + + // Set initial values + stats.setCollectionCount(100L); + stats.setCollectionTime(5000L); + + // Modify one property + stats.setCollectionCount(200L); + + // Verify only the modified property changed + assertEquals(200L, stats.getCollectionCount(), "Modified property should change"); + assertEquals(5000L, stats.getCollectionTime(), "Unmodified property should remain unchanged"); + } + } + + @Nested + @DisplayName("Edge Cases Tests") + class EdgeCasesTests { + + @Test + @DisplayName("Should handle Long.MAX_VALUE for all numeric properties") + void shouldHandleLongMaxValueForAllNumericProperties() { + GCStats stats = new GCStats(); + long maxValue = Long.MAX_VALUE; + + stats.setCollectionCount(maxValue); + stats.setCollectionTime(maxValue); + stats.setLastPauseDuration(maxValue); + stats.setTotalMemory(maxValue); + stats.setUsedMemory(maxValue); + stats.setFreeMemory(maxValue); + + assertEquals(maxValue, stats.getCollectionCount()); + assertEquals(maxValue, stats.getCollectionTime()); + assertEquals(maxValue, stats.getLastPauseDuration()); + assertEquals(maxValue, stats.getTotalMemory()); + assertEquals(maxValue, stats.getUsedMemory()); + assertEquals(maxValue, stats.getFreeMemory()); + } + + @Test + @DisplayName("Should handle Long.MIN_VALUE for all numeric properties") + void shouldHandleLongMinValueForAllNumericProperties() { + GCStats stats = new GCStats(); + long minValue = Long.MIN_VALUE; + + stats.setCollectionCount(minValue); + stats.setCollectionTime(minValue); + stats.setLastPauseDuration(minValue); + stats.setTotalMemory(minValue); + stats.setUsedMemory(minValue); + stats.setFreeMemory(minValue); + + assertEquals(minValue, stats.getCollectionCount()); + assertEquals(minValue, stats.getCollectionTime()); + assertEquals(minValue, stats.getLastPauseDuration()); + assertEquals(minValue, stats.getTotalMemory()); + assertEquals(minValue, stats.getUsedMemory()); + assertEquals(minValue, stats.getFreeMemory()); + } + } +} diff --git a/src/test/java/fish/payara/trader/monitoring/SLAMonitorServiceTest.java b/src/test/java/fish/payara/trader/monitoring/SLAMonitorServiceTest.java new file mode 100644 index 0000000..afbda1f --- /dev/null +++ b/src/test/java/fish/payara/trader/monitoring/SLAMonitorServiceTest.java @@ -0,0 +1,119 @@ +package fish.payara.trader.monitoring; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("SLAMonitorService Tests") +class SLAMonitorServiceTest { + + private SLAMonitorService slaMonitorService; + + @BeforeEach + void setUp() { + slaMonitorService = new SLAMonitorService(); + } + + @Test + @DisplayName("Should initialize with zeroed stats") + void shouldInitializeWithZeroedStats() { + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + + assertEquals(0, stats.totalOperations); + assertEquals(0, stats.violationsOver10ms); + assertEquals(0, stats.violationsOver50ms); + assertEquals(0, stats.violationsOver100ms); + assertEquals(0.0, stats.violationRate); + assertEquals(0, stats.recentViolations); + } + + @Test + @DisplayName("Should record normal operation below thresholds") + void shouldRecordNormalOperationBelowThresholds() { + slaMonitorService.recordOperation(5); + + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + assertEquals(1, stats.totalOperations); + assertEquals(0, stats.violationsOver10ms); + assertEquals(0.0, stats.violationRate); + } + + @Test + @DisplayName("Should record violation over 10ms") + void shouldRecordViolationOver10ms() { + slaMonitorService.recordOperation(15); + + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + assertEquals(1, stats.totalOperations); + assertEquals(1, stats.violationsOver10ms); + assertEquals(0, stats.violationsOver50ms); + assertEquals(0, stats.violationsOver100ms); + assertEquals(100.0, stats.violationRate); + assertEquals(1, stats.recentViolations); + } + + @Test + @DisplayName("Should record violation over 50ms") + void shouldRecordViolationOver50ms() { + slaMonitorService.recordOperation(55); + + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + assertEquals(1, stats.totalOperations); + assertEquals(1, stats.violationsOver10ms); + assertEquals(1, stats.violationsOver50ms); + assertEquals(0, stats.violationsOver100ms); + assertEquals(1, stats.recentViolations); + } + + @Test + @DisplayName("Should record violation over 100ms") + void shouldRecordViolationOver100ms() { + slaMonitorService.recordOperation(105); + + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + assertEquals(1, stats.totalOperations); + assertEquals(1, stats.violationsOver10ms); + assertEquals(1, stats.violationsOver50ms); + assertEquals(1, stats.violationsOver100ms); + assertEquals(1, stats.recentViolations); + } + + @Test + @DisplayName("Should calculate violation rate correctly") + void shouldCalculateViolationRateCorrectly() { + slaMonitorService.recordOperation(5); // No violation + slaMonitorService.recordOperation(15); // Violation > 10ms + slaMonitorService.recordOperation(5); // No violation + slaMonitorService.recordOperation(20); // Violation > 10ms + + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + assertEquals(4, stats.totalOperations); + assertEquals(2, stats.violationsOver10ms); + assertEquals(50.0, stats.violationRate); + } + + @Test + @DisplayName("Should reset statistics") + void shouldResetStatistics() { + slaMonitorService.recordOperation(105); + slaMonitorService.reset(); + + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + assertEquals(0, stats.totalOperations); + assertEquals(0, stats.violationsOver10ms); + assertEquals(0, stats.recentViolations); + } + + @Test + @DisplayName("Should handle multiple violations in a window") + void shouldHandleMultipleViolationsInWindow() { + for (int i = 0; i < 10; i++) { + slaMonitorService.recordOperation(15); + } + + SLAMonitorService.SLAStats stats = slaMonitorService.getStats(); + assertEquals(10, stats.recentViolations); + } +} diff --git a/src/test/java/fish/payara/trader/pressure/AllocationModeTest.java b/src/test/java/fish/payara/trader/pressure/AllocationModeTest.java new file mode 100644 index 0000000..ee63a43 --- /dev/null +++ b/src/test/java/fish/payara/trader/pressure/AllocationModeTest.java @@ -0,0 +1,357 @@ +package fish.payara.trader.pressure; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.ValueSource; + +/** Unit tests for AllocationMode enum */ +@DisplayName("AllocationMode Tests") +class AllocationModeTest { + + @Nested + @DisplayName("Enum Values Tests") + class EnumValuesTests { + + @Test + @DisplayName("Should have all required allocation modes") + void shouldHaveAllRequiredAllocationModes() { + AllocationMode[] modes = AllocationMode.values(); + assertEquals(5, modes.length, "Should have exactly 5 allocation modes"); + + assertTrue(containsMode(modes, AllocationMode.OFF), "Should contain OFF mode"); + assertTrue(containsMode(modes, AllocationMode.LOW), "Should contain LOW mode"); + assertTrue(containsMode(modes, AllocationMode.MEDIUM), "Should contain MEDIUM mode"); + assertTrue(containsMode(modes, AllocationMode.HIGH), "Should contain HIGH mode"); + assertTrue(containsMode(modes, AllocationMode.EXTREME), "Should contain EXTREME mode"); + } + + @Test + @DisplayName("Should have correct enum order from low to high pressure") + void shouldHaveCorrectEnumOrderFromLowToHighPressure() { + AllocationMode[] modes = AllocationMode.values(); + + assertEquals(AllocationMode.OFF, modes[0], "OFF should be first"); + assertEquals(AllocationMode.LOW, modes[1], "LOW should be second"); + assertEquals(AllocationMode.MEDIUM, modes[2], "MEDIUM should be third"); + assertEquals(AllocationMode.HIGH, modes[3], "HIGH should be fourth"); + assertEquals(AllocationMode.EXTREME, modes[4], "EXTREME should be last"); + } + } + + @Nested + @DisplayName("Allocation Properties Tests") + class AllocationPropertiesTests { + + @ParameterizedTest + @EnumSource(AllocationMode.class) + @DisplayName("Should have correct allocation per iteration values") + void shouldHaveCorrectAllocationPerIterationValues(AllocationMode mode) { + int expected = + switch (mode) { + case OFF -> 0; + case LOW -> 10; + case MEDIUM -> 100; + case HIGH -> 5000; + case EXTREME -> 20000; + }; + + assertEquals( + expected, + mode.getAllocationsPerIteration(), + String.format("Mode %s should have %d allocations per iteration", mode.name(), expected)); + } + + @ParameterizedTest + @EnumSource(AllocationMode.class) + @DisplayName("Should have correct bytes per allocation values") + void shouldHaveCorrectBytesPerAllocationValues(AllocationMode mode) { + int expected = + switch (mode) { + case OFF -> 0; + default -> 10240; + }; + assertEquals( + expected, + mode.getBytesPerAllocation(), + String.format("Mode %s should have %d bytes per allocation", mode.name(), expected)); + } + + @ParameterizedTest + @EnumSource(AllocationMode.class) + @DisplayName("Should have meaningful descriptions") + void shouldHaveMeaningfulDescriptions(AllocationMode mode) { + String description = mode.getDescription(); + + assertNotNull(description, "Description should not be null"); + assertFalse(description.trim().isEmpty(), "Description should not be empty"); + assertTrue(description.length() > 10, "Description should be descriptive"); + } + } + + @Nested + @DisplayName("Bytes Per Second Calculation Tests") + class BytesPerSecondCalculationTests { + + @Test + @DisplayName("Should calculate bytes per second correctly for OFF mode") + void shouldCalculateBytesPerSecondCorrectlyForOffMode() { + assertEquals( + 0L, + AllocationMode.OFF.getBytesPerSecond(), + "OFF mode should allocate 0 bytes per second"); + } + + @Test + @DisplayName("Should calculate bytes per second correctly for LOW mode") + void shouldCalculateBytesPerSecondCorrectlyForLowMode() { + // 10 allocations * 10240 bytes * 10 iterations/sec = 1,024,000 bytes/sec + assertEquals( + 1_024_000L, AllocationMode.LOW.getBytesPerSecond(), "LOW mode should allocate 1 MB/sec"); + } + + @Test + @DisplayName("Should calculate bytes per second correctly for MEDIUM mode") + void shouldCalculateBytesPerSecondCorrectlyForMediumMode() { + // 100 allocations * 10240 bytes * 10 iterations/sec = 10,240,000 bytes/sec + assertEquals( + 10_240_000L, + AllocationMode.MEDIUM.getBytesPerSecond(), + "MEDIUM mode should allocate 10 MB/sec"); + } + + @Test + @DisplayName("Should calculate bytes per second correctly for HIGH mode") + void shouldCalculateBytesPerSecondCorrectlyForHighMode() { + // 5000 allocations * 10240 bytes * 10 iterations/sec = 512,000,000 bytes/sec + assertEquals( + 512_000_000L, + AllocationMode.HIGH.getBytesPerSecond(), + "HIGH mode should allocate 500 MB/sec"); + } + + @Test + @DisplayName("Should calculate bytes per second correctly for EXTREME mode") + void shouldCalculateBytesPerSecondCorrectlyForExtremeMode() { + // 20000 allocations * 10240 bytes * 10 iterations/sec = 2,048,000,000 bytes/sec + assertEquals( + 2_048_000_000L, + AllocationMode.EXTREME.getBytesPerSecond(), + "EXTREME mode should allocate 2 GB/sec"); + } + } + + @Nested + @DisplayName("Progressive Intensity Tests") + class ProgressiveIntensityTests { + + @Test + @DisplayName("Should have progressively increasing allocation rates") + void shouldHaveProgressivelyIncreasingAllocationRates() { + long previousRate = -1; + + for (AllocationMode mode : AllocationMode.values()) { + long currentRate = mode.getBytesPerSecond(); + + assertTrue( + currentRate >= previousRate, + String.format( + "%s (%d bytes/sec) should be >= previous rate (%d)", + mode.name(), currentRate, previousRate)); + + previousRate = currentRate; + } + } + + @Test + @DisplayName("Should have monotonic allocation per iteration progression") + void shouldHaveMonotonicAllocationPerIterationProgression() { + int previousAllocations = -1; + + for (AllocationMode mode : AllocationMode.values()) { + int currentAllocations = mode.getAllocationsPerIteration(); + + assertTrue( + currentAllocations >= previousAllocations, + String.format( + "%s (%d allocations) should be >= previous (%d)", + mode.name(), currentAllocations, previousAllocations)); + + previousAllocations = currentAllocations; + } + } + + @Test + @DisplayName("Should have significant gaps between modes") + void shouldHaveSignificantGapsBetweenModes() { + // Verify that there are meaningful differences between modes + assertTrue( + AllocationMode.LOW.getBytesPerSecond() > AllocationMode.OFF.getBytesPerSecond(), + "LOW should be significantly higher than OFF"); + + assertTrue( + AllocationMode.MEDIUM.getBytesPerSecond() > AllocationMode.LOW.getBytesPerSecond() * 5, + "MEDIUM should be at least 5x higher than LOW"); + + assertTrue( + AllocationMode.HIGH.getBytesPerSecond() > AllocationMode.MEDIUM.getBytesPerSecond() * 40, + "HIGH should be at least 40x higher than MEDIUM"); + + assertTrue( + AllocationMode.EXTREME.getBytesPerSecond() > AllocationMode.HIGH.getBytesPerSecond() * 2, + "EXTREME should be at least 2x higher than HIGH"); + } + } + + @Nested + @DisplayName("Description Content Tests") + class DescriptionContentTests { + + @Test + @DisplayName("Descriptions should contain pressure level indicators") + void descriptionsShouldContainPressureLevelIndicators() { + assertTrue( + AllocationMode.LOW.getDescription().contains("Light"), + "LOW description should contain 'Light'"); + + assertTrue( + AllocationMode.MEDIUM.getDescription().contains("Moderate"), + "MEDIUM description should contain 'Moderate'"); + + assertTrue( + AllocationMode.HIGH.getDescription().contains("Heavy"), + "HIGH description should contain 'Heavy'"); + + assertTrue( + AllocationMode.EXTREME.getDescription().contains("Extreme"), + "EXTREME description should contain 'Extreme'"); + + assertTrue( + AllocationMode.OFF.getDescription().contains("No"), + "OFF description should contain 'No'"); + } + + @Test + @DisplayName("Descriptions should contain rate information") + void descriptionsShouldContainRateInformation() { + assertTrue( + AllocationMode.LOW.getDescription().contains("1 MB/sec"), + "LOW description should contain rate information"); + + assertTrue( + AllocationMode.MEDIUM.getDescription().contains("10 MB/sec"), + "MEDIUM description should contain rate information"); + + assertTrue( + AllocationMode.HIGH.getDescription().contains("500 MB/sec"), + "HIGH description should contain rate information"); + + assertTrue( + AllocationMode.EXTREME.getDescription().contains("2 GB/sec"), + "EXTREME description should contain rate information"); + } + } + + @Nested + @DisplayName("Edge Cases Tests") + class EdgeCasesTests { + + @Test + @DisplayName("Should handle zero allocations correctly") + void shouldHandleZeroAllocationsCorrectly() { + assertEquals( + 0, + AllocationMode.OFF.getAllocationsPerIteration(), + "OFF mode should have zero allocations"); + + assertEquals( + 0L, + AllocationMode.OFF.getBytesPerSecond(), + "OFF mode should result in zero bytes per second"); + } + + @Test + @DisplayName("Should handle large allocations correctly") + void shouldHandleLargeAllocationsCorrectly() { + long extremeRate = AllocationMode.EXTREME.getBytesPerSecond(); + + assertTrue( + extremeRate > 1_000_000_000L, // 1GB + "EXTREME mode should handle large allocations (>1GB/sec)"); + + assertEquals(2_048_000_000L, extremeRate, "EXTREME mode should handle exactly 2GB/sec"); + } + + @ParameterizedTest + @ValueSource(strings = {"OFF", "LOW", "MEDIUM", "HIGH", "EXTREME"}) + @DisplayName("Should handle enum valueOf correctly") + void shouldHandleEnumValueOfCorrectly(String modeName) { + assertDoesNotThrow( + () -> { + AllocationMode mode = AllocationMode.valueOf(modeName); + assertNotNull(mode, "Mode should not be null"); + assertEquals(modeName, mode.name(), "Mode name should match"); + }, + "valueOf should work for all valid mode names"); + } + + @Test + @DisplayName("Should throw IllegalArgumentException for invalid mode names") + void shouldThrowIllegalArgumentExceptionForInvalidModeNames() { + String[] invalidNames = {"INVALID", "low", "high", "", " ", null}; + + for (String invalidName : invalidNames) { + if (invalidName != null) { + assertThrows( + IllegalArgumentException.class, + () -> { + AllocationMode.valueOf(invalidName); + }, + () -> String.format("Should throw for invalid mode name: '%s'", invalidName)); + } + } + } + } + + @Nested + @DisplayName("Constants Validation Tests") + class ConstantsValidationTests { + + @Test + @DisplayName("Should validate constant values") + void shouldValidateConstantValues() { + // Validate that bytes per allocation follows expected pattern + for (AllocationMode mode : AllocationMode.values()) { + int expectedBytes = (mode == AllocationMode.OFF) ? 0 : 10240; + assertEquals( + expectedBytes, + mode.getBytesPerAllocation(), + String.format("%s should use %d bytes per allocation", mode.name(), expectedBytes)); + } + + // Validate that calculation formula is consistent + for (AllocationMode mode : AllocationMode.values()) { + long expected = + (long) mode.getAllocationsPerIteration() * mode.getBytesPerAllocation() * 10; + assertEquals( + expected, + mode.getBytesPerSecond(), + String.format("Calculation should be consistent for %s", mode.name())); + } + } + } + + /** Helper method to check if array contains specific mode */ + private boolean containsMode(AllocationMode[] modes, AllocationMode targetMode) { + for (AllocationMode mode : modes) { + if (mode == targetMode) { + return true; + } + } + return false; + } +} diff --git a/src/test/java/fish/payara/trader/rest/GCStatsResourceTest.java b/src/test/java/fish/payara/trader/rest/GCStatsResourceTest.java new file mode 100644 index 0000000..a4e3397 --- /dev/null +++ b/src/test/java/fish/payara/trader/rest/GCStatsResourceTest.java @@ -0,0 +1,100 @@ +package fish.payara.trader.rest; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import fish.payara.trader.aeron.MarketDataPublisher; +import fish.payara.trader.gc.GCStatsService; +import fish.payara.trader.monitoring.GCPauseMonitor; +import fish.payara.trader.monitoring.SLAMonitorService; +import fish.payara.trader.pressure.AllocationMode; +import fish.payara.trader.pressure.MemoryPressureService; +import jakarta.ws.rs.core.Response; +import java.util.Collections; +import java.util.Map; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +@DisplayName("GCStatsResource Tests") +class GCStatsResourceTest { + + @Mock private GCStatsService gcStatsService; + @Mock private MemoryPressureService memoryPressureService; + @Mock private MarketDataPublisher publisher; + @Mock private SLAMonitorService slaMonitor; + @Mock private GCPauseMonitor gcPauseMonitor; + + @InjectMocks private GCStatsResource resource; + + @Test + @DisplayName("Should return SLA stats") + void shouldReturnSLAStats() { + SLAMonitorService.SLAStats mockStats = new SLAMonitorService.SLAStats(100, 5, 2, 1, 5.0, 3); + when(slaMonitor.getStats()).thenReturn(mockStats); + + Response response = resource.getSLAStats(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + assertEquals(mockStats, response.getEntity()); + } + + @Test + @DisplayName("Should reset SLA stats") + void shouldResetSLAStats() { + Response response = resource.resetSLAStats(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + verify(slaMonitor).reset(); + } + + @Test + @DisplayName("Should return GC pause stats") + void shouldReturnGCPauseStats() { + GCPauseMonitor.GCPauseStats mockStats = + new GCPauseMonitor.GCPauseStats(10, 100, 10.0, 10, 20, 30, 40, 50, 5, 2, 1, 10); + when(gcPauseMonitor.getStats()).thenReturn(mockStats); + + Response response = resource.getGCPauseStats(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + assertEquals(mockStats, response.getEntity()); + } + + @Test + @DisplayName("Should return accurate comparison data") + void shouldReturnAccurateComparisonData() { + when(memoryPressureService.getCurrentMode()).thenReturn(AllocationMode.MEDIUM); + when(publisher.getMessagesPublished()).thenReturn(5000L); + when(gcStatsService.collectGCStats()).thenReturn(Collections.emptyList()); + + GCPauseMonitor.GCPauseStats mockPauseStats = + new GCPauseMonitor.GCPauseStats(10, 100, 10.0, 10, 20, 30, 40, 50, 5, 2, 1, 10); + when(gcPauseMonitor.getStats()).thenReturn(mockPauseStats); + + Response response = resource.getComparison(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map entity = (Map) response.getEntity(); + + assertEquals(AllocationMode.MEDIUM, entity.get("allocationMode")); + assertEquals(5000L, entity.get("messageRate")); + assertEquals(10.0, ((Number) entity.get("pauseP50Ms")).doubleValue()); + assertEquals(50L, entity.get("pauseMaxMs")); + assertEquals(5L, entity.get("slaViolations10ms")); + } + + @Test + @DisplayName("Should reset GC stats") + void shouldResetGCStats() { + Response response = resource.resetStats(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + verify(gcStatsService).resetStats(); + } +} diff --git a/src/test/java/fish/payara/trader/rest/MemoryPressureResourceTest.java b/src/test/java/fish/payara/trader/rest/MemoryPressureResourceTest.java new file mode 100644 index 0000000..2876a74 --- /dev/null +++ b/src/test/java/fish/payara/trader/rest/MemoryPressureResourceTest.java @@ -0,0 +1,107 @@ +package fish.payara.trader.rest; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import fish.payara.trader.pressure.AllocationMode; +import fish.payara.trader.pressure.MemoryPressureService; +import jakarta.ws.rs.core.Response; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +@DisplayName("MemoryPressureResource Tests") +class MemoryPressureResourceTest { + + @Mock private MemoryPressureService pressureService; + + @InjectMocks private MemoryPressureResource resource; + + @Test + @DisplayName("Should return current status") + void shouldReturnCurrentStatus() { + when(pressureService.getCurrentMode()).thenReturn(AllocationMode.LOW); + when(pressureService.isRunning()).thenReturn(true); + + Response response = resource.getStatus(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map entity = (Map) response.getEntity(); + assertEquals("LOW", entity.get("currentMode")); + assertEquals(true, entity.get("running")); + } + + @Test + @DisplayName("Should set allocation mode successfully") + void shouldSetAllocationModeSuccessfully() { + Response response = resource.setMode("high"); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + verify(pressureService).setAllocationMode(AllocationMode.HIGH); + + @SuppressWarnings("unchecked") + Map entity = (Map) response.getEntity(); + assertTrue((Boolean) entity.get("success")); + assertEquals("HIGH", entity.get("mode")); + } + + @Test + @DisplayName("Should return bad request for invalid mode") + void shouldReturnBadRequestForInvalidMode() { + Response response = resource.setMode("invalid_mode"); + + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map entity = (Map) response.getEntity(); + assertFalse((Boolean) entity.get("success")); + assertTrue(((String) entity.get("error")).contains("Invalid mode")); + } + + @Test + @DisplayName("Should apply scenario successfully") + void shouldApplyScenarioSuccessfully() { + Response response = resource.applyScenario("demo_stress"); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + verify(pressureService).setAllocationMode(AllocationMode.HIGH); + + @SuppressWarnings("unchecked") + Map entity = (Map) response.getEntity(); + assertEquals("DEMO_STRESS", entity.get("scenario")); + assertEquals("applied", entity.get("status")); + } + + @Test + @DisplayName("Should list all scenarios") + void shouldListAllScenarios() { + Response response = resource.listScenarios(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + List> entity = (List>) response.getEntity(); + assertFalse(entity.isEmpty()); + assertTrue(entity.stream().anyMatch(s -> s.get("name").equals("DEMO_EXTREME"))); + } + + @Test + @DisplayName("Should return all modes") + void shouldReturnAllModes() { + Response response = resource.getModes(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map> entity = + (Map>) response.getEntity(); + assertTrue(entity.containsKey("EXTREME")); + assertEquals( + "2 GB/sec - Extreme pressure", + ((Map) entity.get("EXTREME")).get("description")); + } +} diff --git a/src/test/java/fish/payara/trader/rest/StatusResourceTest.java b/src/test/java/fish/payara/trader/rest/StatusResourceTest.java new file mode 100644 index 0000000..827c0a0 --- /dev/null +++ b/src/test/java/fish/payara/trader/rest/StatusResourceTest.java @@ -0,0 +1,379 @@ +package fish.payara.trader.rest; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import fish.payara.trader.aeron.AeronSubscriberBean; +import fish.payara.trader.aeron.MarketDataPublisher; +import fish.payara.trader.websocket.MarketDataBroadcaster; +import jakarta.ws.rs.core.Response; +import java.util.Map; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +/** Unit tests for StatusResource REST endpoint */ +@ExtendWith(MockitoExtension.class) +@DisplayName("StatusResource Tests") +class StatusResourceTest { + + @Mock private AeronSubscriberBean subscriber; + + @Mock private MarketDataPublisher publisher; + + @Mock private MarketDataBroadcaster broadcaster; + + @InjectMocks private StatusResource statusResource; + + @Nested + @DisplayName("getStatus Method Tests") + class GetStatusMethodTests { + + @Test + @DisplayName("Should return complete status information") + void shouldReturnCompleteStatusInformation() { + // Arrange + String subscriberStatus = "Channel: aeron:ipc, Stream: 1001, Running: true"; + long localMessagesPublished = 5000L; + long clusterMessagesPublished = 25000L; + int activeSessions = 42; + String expectedInstanceName = "test-instance-1"; + + when(subscriber.getStatus()).thenReturn(subscriberStatus); + when(publisher.getMessagesPublished()).thenReturn(localMessagesPublished); + when(publisher.getClusterMessagesPublished()).thenReturn(clusterMessagesPublished); + when(broadcaster.getSessionCount()).thenReturn(activeSessions); + + // Set environment variable - note: tests can't modify real env vars, so we expect default + // behavior + // In this test, we expect "standalone" since we can't set actual environment variables + + try { + // Act + Response response = statusResource.getStatus(); + + // Assert + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + // Skip media type check as Response.getMediaType() may return null in tests + + @SuppressWarnings("unchecked") + Map status = (Map) response.getEntity(); + + // Verify required fields + assertEquals("TradeStreamEE", status.get("application")); + assertEquals( + "High-frequency trading dashboard with Aeron and SBE", status.get("description")); + assertEquals("standalone", status.get("instance")); // Default value + assertEquals(subscriberStatus, status.get("subscriber")); + assertEquals("UP", status.get("status")); + + // Verify publisher stats + @SuppressWarnings("unchecked") + Map publisherStats = (Map) status.get("publisher"); + assertEquals(localMessagesPublished, publisherStats.get("localMessagesPublished")); + assertEquals(clusterMessagesPublished, publisherStats.get("clusterMessagesPublished")); + + // Verify websocket stats + @SuppressWarnings("unchecked") + Map websocketStats = (Map) status.get("websocket"); + assertEquals(activeSessions, websocketStats.get("activeSessions")); + + } finally { + // No cleanup needed since we can't modify env vars in tests + } + + // Verify mocks were called + verify(subscriber).getStatus(); + verify(publisher).getMessagesPublished(); + verify(publisher).getClusterMessagesPublished(); + verify(broadcaster).getSessionCount(); + } + + @Test + @DisplayName("Should use default instance name when environment variable is not set") + void shouldUseDefaultInstanceNameWhenEnvironmentVariableIsNotSet() { + // Arrange + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(1000L); + when(publisher.getClusterMessagesPublished()).thenReturn(5000L); + when(broadcaster.getSessionCount()).thenReturn(10); + + // Note: Tests can't modify real environment variables, so we expect default behavior + try { + // Act + Response response = statusResource.getStatus(); + + // Assert + @SuppressWarnings("unchecked") + Map status = (Map) response.getEntity(); + assertEquals("standalone", status.get("instance"), "Should use default instance name"); + + } finally { + // No cleanup needed + } + } + + @Test + @DisplayName("Should handle null subscriber status gracefully") + void shouldHandleNullSubscriberStatusGracefully() { + // Arrange + when(subscriber.getStatus()).thenReturn(null); + when(publisher.getMessagesPublished()).thenReturn(1000L); + when(publisher.getClusterMessagesPublished()).thenReturn(5000L); + when(broadcaster.getSessionCount()).thenReturn(10); + + // Act + Response response = statusResource.getStatus(); + + // Assert + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map status = (Map) response.getEntity(); + assertNull(status.get("subscriber"), "Null subscriber status should be handled"); + } + + @Test + @DisplayName("Should handle zero values gracefully") + void shouldHandleZeroValuesGracefully() { + // Arrange + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(0L); + when(publisher.getClusterMessagesPublished()).thenReturn(0L); + when(broadcaster.getSessionCount()).thenReturn(0); + + // Act + Response response = statusResource.getStatus(); + + // Assert + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map status = (Map) response.getEntity(); + + @SuppressWarnings("unchecked") + Map publisherStats = (Map) status.get("publisher"); + assertEquals(0L, publisherStats.get("localMessagesPublished")); + assertEquals(0L, publisherStats.get("clusterMessagesPublished")); + + @SuppressWarnings("unchecked") + Map websocketStats = (Map) status.get("websocket"); + assertEquals(0, websocketStats.get("activeSessions")); + } + + @Test + @DisplayName("Should handle very large values gracefully") + void shouldHandleVeryLargeValuesGracefully() { + // Arrange + long largeLocalMessages = Long.MAX_VALUE; + long largeClusterMessages = Long.MAX_VALUE - 1; + int largeSessions = Integer.MAX_VALUE - 1; // Leave room for additions + + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(largeLocalMessages); + when(publisher.getClusterMessagesPublished()).thenReturn(largeClusterMessages); + when(broadcaster.getSessionCount()).thenReturn(largeSessions); + + // Act + Response response = statusResource.getStatus(); + + // Assert + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map status = (Map) response.getEntity(); + + @SuppressWarnings("unchecked") + Map publisherStats = (Map) status.get("publisher"); + assertEquals(largeLocalMessages, publisherStats.get("localMessagesPublished")); + assertEquals(largeClusterMessages, publisherStats.get("clusterMessagesPublished")); + + @SuppressWarnings("unchecked") + Map websocketStats = (Map) status.get("websocket"); + assertEquals(largeSessions, websocketStats.get("activeSessions")); + } + } + + @Nested + @DisplayName("getClusterStatus Method Tests") + class GetClusterStatusMethodTests { + + @Test + @DisplayName("Should return standalone status when Hazelcast is null") + void shouldReturnStandaloneStatusWhenHazelcastIsNull() { + // Act - hazelcastInstance is injected as null in this test class + + // Act + Response response = statusResource.getClusterStatus(); + + // Assert + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + @SuppressWarnings("unchecked") + Map clusterInfo = (Map) response.getEntity(); + + assertFalse((Boolean) clusterInfo.get("clustered")); + assertEquals( + "Running in standalone mode (Hazelcast not available)", clusterInfo.get("message")); + } + } + + @Nested + @DisplayName("Dependency Injection Tests") + class DependencyInjectionTests { + + @Test + @DisplayName("Should properly inject all dependencies") + void shouldProperlyInjectAllDependencies() { + // Test that all mocks are properly injected + assertNotNull(subscriber, "Subscriber should be injected"); + assertNotNull(publisher, "Publisher should be injected"); + assertNotNull(broadcaster, "Broadcaster should be injected"); + assertNotNull(statusResource, "StatusResource should be created"); + } + } + + @Nested + @DisplayName("Response Format Tests") + class ResponseFormatTests { + + @Test + @DisplayName("Should always return JSON response") + void shouldAlwaysReturnJSONResponse() { + // Arrange + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(1000L); + when(publisher.getClusterMessagesPublished()).thenReturn(5000L); + when(broadcaster.getSessionCount()).thenReturn(10); + + // Act + Response statusResponse = statusResource.getStatus(); + Response clusterResponse = statusResource.getClusterStatus(); + + // Assert - skip media type checks as they may return null in tests + assertNotNull(statusResponse, "Status response should not be null"); + assertNotNull(clusterResponse, "Cluster response should not be null"); + } + + @Test + @DisplayName("Should always return success status codes for normal operations") + void shouldAlwaysReturnSuccessStatusCodesForNormalOperations() { + // Arrange + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(1000L); + when(publisher.getClusterMessagesPublished()).thenReturn(5000L); + when(broadcaster.getSessionCount()).thenReturn(10); + + // Act + Response statusResponse = statusResource.getStatus(); + Response clusterResponse = statusResource.getClusterStatus(); + + // Assert + assertEquals(Response.Status.OK.getStatusCode(), statusResponse.getStatus()); + assertEquals(Response.Status.OK.getStatusCode(), clusterResponse.getStatus()); + } + } + + @Nested + @DisplayName("Environment Variable Tests") + class EnvironmentVariableTests { + + @Test + @DisplayName("Should handle different instance name environment variables") + void shouldHandleDifferentInstanceNameEnvironmentVariables() { + String[] testNames = { + "", " ", "\tinstance-1", "instance-1", "test-instance-123", "production-instance" + }; + + for (String instanceName : testNames) { + // Arrange + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(1000L); + when(publisher.getClusterMessagesPublished()).thenReturn(5000L); + when(broadcaster.getSessionCount()).thenReturn(10); + + // Note: Tests can't modify real environment variables, so we always expect "standalone" + try { + // Act + Response response = statusResource.getStatus(); + + // Assert + @SuppressWarnings("unchecked") + Map status = (Map) response.getEntity(); + assertEquals("standalone", status.get("instance")); + + } finally { + // No cleanup needed + } + } + } + } + + @Nested + @DisplayName("Edge Cases Tests") + class EdgeCasesTests { + + @Test + @DisplayName("Should handle multiple rapid calls correctly") + void shouldHandleMultipleRapidCallsCorrectly() { + // Arrange + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(1000L, 2000L, 3000L); + when(publisher.getClusterMessagesPublished()).thenReturn(5000L, 10000L, 15000L); + when(broadcaster.getSessionCount()).thenReturn(10, 20, 30); + + // Act + Response response1 = statusResource.getStatus(); + Response response2 = statusResource.getStatus(); + Response response3 = statusResource.getStatus(); + + // Assert - All calls should succeed + assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus()); + assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus()); + assertEquals(Response.Status.OK.getStatusCode(), response3.getStatus()); + + // Verify mocks were called correct number of times + verify(publisher, times(3)).getMessagesPublished(); + verify(publisher, times(3)).getClusterMessagesPublished(); + } + + @Test + @DisplayName("Should handle concurrent access safely") + void shouldHandleConcurrentAccessSafely() throws InterruptedException { + // Arrange + when(subscriber.getStatus()).thenReturn("Running"); + when(publisher.getMessagesPublished()).thenReturn(1000L); + when(publisher.getClusterMessagesPublished()).thenReturn(5000L); + when(broadcaster.getSessionCount()).thenReturn(10); + + int threadCount = 10; + Thread[] threads = new Thread[threadCount]; + Response[] responses = new Response[threadCount]; + + // Act + for (int i = 0; i < threadCount; i++) { + final int index = i; + threads[i] = + new Thread( + () -> { + responses[index] = statusResource.getStatus(); + }); + threads[i].start(); + } + + // Wait for all threads to complete + for (Thread thread : threads) { + thread.join(5000); + } + + // Assert - All responses should be successful + for (Response response : responses) { + assertNotNull(response, "Response should not be null"); + assertEquals( + Response.Status.OK.getStatusCode(), + response.getStatus(), + "All responses should be successful"); + } + } + } +} diff --git a/src/test/java/fish/payara/trader/utils/GCTestUtil.java b/src/test/java/fish/payara/trader/utils/GCTestUtil.java new file mode 100644 index 0000000..6a8679b --- /dev/null +++ b/src/test/java/fish/payara/trader/utils/GCTestUtil.java @@ -0,0 +1,203 @@ +package fish.payara.trader.utils; + +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryUsage; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +/** Utility class for GC testing and simulation */ +public class GCTestUtil { + + private static final MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); + private static final List gcBeans = + ManagementFactory.getGarbageCollectorMXBeans(); + + /** Captures initial GC statistics */ + public static GCStatistics captureInitialStats() { + long totalCollections = 0; + long totalTime = 0; + + for (GarbageCollectorMXBean gcBean : gcBeans) { + totalCollections += gcBean.getCollectionCount(); + totalTime += gcBean.getCollectionTime(); + } + + return new GCStatistics(totalCollections, totalTime, getHeapUsage()); + } + + /** Calculates GC statistics after a test */ + public static GCStatistics calculateDelta(GCStatistics initial) { + long totalCollections = 0; + long totalTime = 0; + + for (GarbageCollectorMXBean gcBean : gcBeans) { + totalCollections += gcBean.getCollectionCount(); + totalTime += gcBean.getCollectionTime(); + } + + return new GCStatistics( + totalCollections - initial.collections, totalTime - initial.time, getHeapUsage()); + } + + /** Gets current heap usage */ + public static MemoryUsage getHeapUsage() { + return memoryBean.getHeapMemoryUsage(); + } + + /** Allocates memory to trigger GC */ + public static void allocateMemory(int sizeMB) { + allocateMemory(sizeMB, 10); + } + + /** Allocates memory with controlled pattern */ + public static void allocateMemory(int sizeMB, int chunks) { + int chunkSize = (sizeMB * 1024 * 1024) / chunks; + List allocations = new ArrayList<>(); + + try { + for (int i = 0; i < chunks; i++) { + allocations.add(new byte[chunkSize]); + + // Small delay to simulate real usage pattern + if (i % 10 == 0) { + Thread.yield(); + } + } + } finally { + // Let allocations go out of scope to trigger GC + allocations.clear(); + } + } + + /** Creates memory pressure in background thread */ + public static MemoryPressureThread createMemoryPressure( + int allocationRateMB, int durationSeconds) { + MemoryPressureThread pressureThread = + new MemoryPressureThread(allocationRateMB, durationSeconds); + return pressureThread; + } + + /** Forces a garbage collection and waits for completion */ + public static void forceGC() { + System.gc(); + System.runFinalization(); + + // Small delay to allow GC to complete + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + /** Measures GC pause time for a specific operation */ + public static long measurePauseTime(Runnable operation) { + GCStatistics before = captureInitialStats(); + + long startTime = System.nanoTime(); + operation.run(); + long endTime = System.nanoTime(); + + GCStatistics after = calculateDelta(before); + + return after.time; + } + + /** GC Statistics holder */ + public static class GCStatistics { + public final long collections; + public final long time; + public final MemoryUsage heapUsage; + + public GCStatistics(long collections, long time, MemoryUsage heapUsage) { + this.collections = collections; + this.time = time; + this.heapUsage = heapUsage; + } + + @Override + public String toString() { + return String.format("GC{collections=%d, time=%dms, heap=%s}", collections, time, heapUsage); + } + } + + /** Background thread for creating controlled memory pressure */ + public static class MemoryPressureThread extends Thread { + private final int allocationRateMB; + private final int durationSeconds; + private final AtomicLong allocatedBytes = new AtomicLong(0); + private volatile boolean running = true; + + public MemoryPressureThread(int allocationRateMB, int durationSeconds) { + this.allocationRateMB = allocationRateMB; + this.durationSeconds = durationSeconds; + setDaemon(true); + setName("MemoryPressureThread"); + } + + @Override + public void run() { + long startTime = System.currentTimeMillis(); + long endTime = startTime + (durationSeconds * 1000L); + + int chunkSize = 1024 * 1024; // 1MB chunks + int allocationsPerSecond = allocationRateMB; + long allocationInterval = 1000L / allocationsPerSecond; // milliseconds + + List allocations = new ArrayList<>(); + + while (running && System.currentTimeMillis() < endTime) { + try { + allocations.add(new byte[chunkSize]); + allocatedBytes.addAndGet(chunkSize); + + // Control allocation rate + if (allocationInterval > 0) { + Thread.sleep(allocationInterval); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } + } + + // Clear allocations to trigger GC + allocations.clear(); + allocatedBytes.set(0); + } + + public void stopPressure() { + running = false; + interrupt(); + } + + public long getAllocatedBytes() { + return allocatedBytes.get(); + } + } + + /** Wait for GC to occur within timeout */ + public static boolean waitForGC(long timeoutMillis) { + GCStatistics initial = captureInitialStats(); + long startTime = System.currentTimeMillis(); + + while (System.currentTimeMillis() - startTime < timeoutMillis) { + GCStatistics current = calculateDelta(initial); + if (current.collections > 0) { + return true; + } + + try { + Thread.sleep(10); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } + } + + return false; + } +} diff --git a/src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java b/src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java index 25fe6c7..c6ffa35 100644 --- a/src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java +++ b/src/test/java/fish/payara/trader/websocket/MarketDataBroadcasterTest.java @@ -1,5 +1,11 @@ package fish.payara.trader.websocket; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.lenient; + import jakarta.websocket.RemoteEndpoint.Async; import jakarta.websocket.Session; import org.junit.jupiter.api.BeforeEach; @@ -9,152 +15,143 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.*; -import static org.mockito.Mockito.lenient; - @ExtendWith(MockitoExtension.class) class MarketDataBroadcasterTest { - private MarketDataBroadcaster broadcaster; + private MarketDataBroadcaster broadcaster; - @Mock - private Session session1; + @Mock private Session session1; - @Mock - private Session session2; + @Mock private Session session2; - @Mock - private Async asyncRemote1; + @Mock private Async asyncRemote1; - @Mock - private Async asyncRemote2; + @Mock private Async asyncRemote2; - @BeforeEach - void setUp() { - broadcaster = new MarketDataBroadcaster(); - lenient().when(session1.getId()).thenReturn("session-1"); - lenient().when(session2.getId()).thenReturn("session-2"); - lenient().when(session1.getAsyncRemote()).thenReturn(asyncRemote1); - lenient().when(session2.getAsyncRemote()).thenReturn(asyncRemote2); - lenient().when(session1.isOpen()).thenReturn(true); - lenient().when(session2.isOpen()).thenReturn(true); - } + @BeforeEach + void setUp() { + broadcaster = new MarketDataBroadcaster(); + lenient().when(session1.getId()).thenReturn("session-1"); + lenient().when(session2.getId()).thenReturn("session-2"); + lenient().when(session1.getAsyncRemote()).thenReturn(asyncRemote1); + lenient().when(session2.getAsyncRemote()).thenReturn(asyncRemote2); + lenient().when(session1.isOpen()).thenReturn(true); + lenient().when(session2.isOpen()).thenReturn(true); + } - @Test - void testAddSession() { - broadcaster.addSession(session1); + @Test + void testAddSession() { + broadcaster.addSession(session1); - assertThat(broadcaster.getSessionCount()).isEqualTo(1); - } + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } - @Test - void testAddMultipleSessions() { - broadcaster.addSession(session1); - broadcaster.addSession(session2); + @Test + void testAddMultipleSessions() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); - assertThat(broadcaster.getSessionCount()).isEqualTo(2); - } + assertThat(broadcaster.getSessionCount()).isEqualTo(2); + } - @Test - void testRemoveSession() { - broadcaster.addSession(session1); - broadcaster.addSession(session2); + @Test + void testRemoveSession() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); - broadcaster.removeSession(session1); + broadcaster.removeSession(session1); - assertThat(broadcaster.getSessionCount()).isEqualTo(1); - } + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } - @Test - void testBroadcastToAllSessions() { - broadcaster.addSession(session1); - broadcaster.addSession(session2); + @Test + void testBroadcastToAllSessions() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); - String message = "{\"type\":\"trade\",\"price\":100}"; - broadcaster.broadcast(message); + String message = "{\"type\":\"trade\",\"price\":100}"; + broadcaster.broadcast(message); - verify(asyncRemote1).sendText(eq(message)); - verify(asyncRemote2).sendText(eq(message)); - } + verify(asyncRemote1).sendText(eq(message)); + verify(asyncRemote2).sendText(eq(message)); + } - @Test - void testBroadcastRemovesClosedSessions() { - broadcaster.addSession(session1); - broadcaster.addSession(session2); + @Test + void testBroadcastRemovesClosedSessions() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); - when(session1.isOpen()).thenReturn(false); + when(session1.isOpen()).thenReturn(false); - String message = "{\"type\":\"trade\",\"price\":100}"; - broadcaster.broadcast(message); + String message = "{\"type\":\"trade\",\"price\":100}"; + broadcaster.broadcast(message); - verify(asyncRemote1, never()).sendText(any()); - verify(asyncRemote2).sendText(eq(message)); + verify(asyncRemote1, never()).sendText(any()); + verify(asyncRemote2).sendText(eq(message)); - assertThat(broadcaster.getSessionCount()).isEqualTo(1); - } + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } - @Test - void testBroadcastHandlesFailure() { - broadcaster.addSession(session1); - broadcaster.addSession(session2); + @Test + void testBroadcastHandlesFailure() { + broadcaster.addSession(session1); + broadcaster.addSession(session2); - String message = "{\"type\":\"trade\",\"price\":100}"; + String message = "{\"type\":\"trade\",\"price\":100}"; - doThrow(new RuntimeException("Send failed")) - .when(asyncRemote1).sendText(message); + doThrow(new RuntimeException("Send failed")).when(asyncRemote1).sendText(message); - broadcaster.broadcast(message); + broadcaster.broadcast(message); - verify(asyncRemote2).sendText(eq(message)); - assertThat(broadcaster.getSessionCount()).isEqualTo(1); - } + verify(asyncRemote2).sendText(eq(message)); + assertThat(broadcaster.getSessionCount()).isEqualTo(1); + } - @Test - void testGetSessionCountWhenEmpty() { - assertThat(broadcaster.getSessionCount()).isEqualTo(0); - } + @Test + void testGetSessionCountWhenEmpty() { + assertThat(broadcaster.getSessionCount()).isEqualTo(0); + } - @Test - void testBroadcastWithArtificialLoad() { - broadcaster.addSession(session1); + @Test + void testBroadcastWithArtificialLoad() { + broadcaster.addSession(session1); - String baseMessage = "{\"type\":\"trade\",\"price\":100}"; - broadcaster.broadcastWithArtificialLoad(baseMessage); + String baseMessage = "{\"type\":\"trade\",\"price\":100}"; + broadcaster.broadcastWithArtificialLoad(baseMessage); - ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); - verify(asyncRemote1).sendText(messageCaptor.capture()); + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + verify(asyncRemote1).sendText(messageCaptor.capture()); - String sentMessage = messageCaptor.getValue(); - assertThat(sentMessage).contains(baseMessage); - assertThat(sentMessage).contains("\"wrapped\""); - assertThat(sentMessage).contains("\"padding\""); - assertThat(sentMessage.length()).isGreaterThan(baseMessage.length()); - } + String sentMessage = messageCaptor.getValue(); + assertThat(sentMessage).contains(baseMessage); + assertThat(sentMessage).contains("\"wrapped\""); + assertThat(sentMessage).contains("\"padding\""); + assertThat(sentMessage.length()).isGreaterThan(baseMessage.length()); + } - @Test - void testConcurrentSessionManagement() throws InterruptedException { - int threadCount = 10; - Thread[] threads = new Thread[threadCount]; + @Test + void testConcurrentSessionManagement() throws InterruptedException { + int threadCount = 10; + Thread[] threads = new Thread[threadCount]; - for (int i = 0; i < threadCount; i++) { - final int index = i; - threads[i] = new Thread(() -> { + for (int i = 0; i < threadCount; i++) { + final int index = i; + threads[i] = + new Thread( + () -> { Session mockSession = mock(Session.class); when(mockSession.getId()).thenReturn("session-" + index); when(mockSession.getAsyncRemote()).thenReturn(mock(Async.class)); when(mockSession.isOpen()).thenReturn(true); broadcaster.addSession(mockSession); - }); - threads[i].start(); - } - - for (Thread thread : threads) { - thread.join(); - } + }); + threads[i].start(); + } - assertThat(broadcaster.getSessionCount()).isEqualTo(threadCount); + for (Thread thread : threads) { + thread.join(); } + + assertThat(broadcaster.getSessionCount()).isEqualTo(threadCount); + } } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..5ccbd29 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,47 @@ + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + target/test.log + + target/test.%d{yyyy-MM-dd}.log + 7 + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/start.sh b/start.sh index 2dbd34c..05915c6 100755 --- a/start.sh +++ b/start.sh @@ -24,11 +24,13 @@ if ! docker compose version &> /dev/null && ! docker-compose --version &> /dev/n fi # Use docker compose (v2) or docker-compose (v1) -if docker compose version &> /dev/null; then - DOCKER_COMPOSE="docker compose" -else - DOCKER_COMPOSE="docker-compose" -fi +run_compose() { + if docker compose version &> /dev/null; then + docker compose "$@" + else + docker-compose "$@" + fi +} echo "✅ Docker is installed" echo "✅ Docker Compose is installed" @@ -44,7 +46,7 @@ case "$ACTION" in echo "🚀 [Azul Prime] Starting with AERON Architecture (Optimized)..." echo " > Dockerfile (Azul) + MODE=AERON" echo "" - MODE=AERON $DOCKER_COMPOSE -f docker-compose.yml up -d --build --force-recreate + MODE=AERON run_compose -f docker-compose.yml up -d --build --force-recreate ;; azul-direct) @@ -52,7 +54,7 @@ case "$ACTION" in echo " > Dockerfile (Azul) + MODE=DIRECT" echo " ℹ️ Observe how C4 handles high-allocation legacy code." echo "" - MODE=DIRECT $DOCKER_COMPOSE -f docker-compose.yml up -d --build --force-recreate + MODE=DIRECT run_compose -f docker-compose.yml up -d --build --force-recreate ;; # --- Standard OpenJDK (G1 GC) Scenarios --- @@ -62,7 +64,7 @@ case "$ACTION" in echo " > Dockerfile.standard (Temurin) + MODE=DIRECT" echo " ℹ️ Baseline performance: High allocation on G1GC." echo "" - MODE=DIRECT $DOCKER_COMPOSE -f docker-compose-standard.yml up -d --build --force-recreate + MODE=DIRECT run_compose -f docker-compose-standard.yml up -d --build --force-recreate ;; standard-aeron) @@ -70,7 +72,7 @@ case "$ACTION" in echo " > Dockerfile.standard (Temurin) + MODE=AERON" echo " ℹ️ Observe if off-heap transport helps G1GC." echo "" - MODE=AERON $DOCKER_COMPOSE -f docker-compose-standard.yml up -d --build --force-recreate + MODE=AERON run_compose -f docker-compose-standard.yml up -d --build --force-recreate ;; # --- Clustered Scenarios --- @@ -80,7 +82,7 @@ case "$ACTION" in echo " > Dockerfile.scale (Azul) + MODE=AERON + Nginx LB" echo " ℹ️ Demonstrates horizontal scalability with Hazelcast clustering." echo "" - MODE=AERON DOCKERFILE=Dockerfile.scale $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --build --force-recreate + MODE=AERON DOCKERFILE=Dockerfile.scale run_compose -f docker-compose-scale.yml up -d --build --force-recreate echo "" echo "✅ Cluster started. Waiting for instances to be ready..." sleep 10 @@ -96,7 +98,7 @@ case "$ACTION" in echo " > Dockerfile.scale.standard (Temurin) + MODE=AERON + Nginx LB" echo " ℹ️ Compare cluster performance with G1GC." echo "" - MODE=AERON DOCKERFILE=Dockerfile.scale.standard $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --build --force-recreate + MODE=AERON DOCKERFILE=Dockerfile.scale.standard run_compose -f docker-compose-scale.yml up -d --build --force-recreate echo "" echo "✅ Cluster started. Waiting for instances to be ready..." sleep 10 @@ -112,7 +114,7 @@ case "$ACTION" in echo " > Dockerfile.scale (Azul) + MODE=DIRECT + Traefik LB" echo " ℹ️ Test cluster with high-allocation legacy mode." echo "" - MODE=DIRECT DOCKERFILE=Dockerfile.scale $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --build --force-recreate + MODE=DIRECT DOCKERFILE=Dockerfile.scale run_compose -f docker-compose-scale.yml up -d --build --force-recreate ;; cluster-dynamic) @@ -126,9 +128,9 @@ case "$ACTION" in echo " > Dockerfile.scale (Azul) + MODE=AERON + $INSTANCES scalable instances" echo " ℹ️ Uses generic service for true dynamic scaling." echo "" - MODE=AERON DOCKERFILE=Dockerfile.scale $DOCKER_COMPOSE -f docker-compose-scale.yml build trader-stream - $DOCKER_COMPOSE -f docker-compose-scale.yml up -d traefik - $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --scale trader-stream=$INSTANCES --no-recreate trader-stream + MODE=AERON DOCKERFILE=Dockerfile.scale run_compose -f docker-compose-scale.yml build trader-stream + run_compose -f docker-compose-scale.yml up -d traefik + run_compose -f docker-compose-scale.yml up -d --scale trader-stream=$INSTANCES --no-recreate trader-stream echo "" echo "✅ Cluster started. Waiting for instances to be ready..." sleep 15 @@ -149,7 +151,7 @@ case "$ACTION" in INSTANCES=$2 echo " > Scaling to $INSTANCES instances (using generic trader-stream service)" echo " > Note: This uses the scalable service, not the named instances (trader-stream-1/2/3)" - $DOCKER_COMPOSE -f docker-compose-scale.yml up -d --scale trader-stream=$INSTANCES --no-recreate + run_compose -f docker-compose-scale.yml up -d --scale trader-stream=$INSTANCES --no-recreate echo "" echo "✅ Scaled to $INSTANCES instances" sleep 10 @@ -162,9 +164,9 @@ case "$ACTION" in down|stop) echo "🛑 Stopping TradeStreamEE..." - $DOCKER_COMPOSE -f docker-compose.yml down - $DOCKER_COMPOSE -f docker-compose-standard.yml down - $DOCKER_COMPOSE -f docker-compose-scale.yml down + run_compose -f docker-compose.yml down + run_compose -f docker-compose-standard.yml down + run_compose -f docker-compose-scale.yml down echo "✅ Stopped" ;; @@ -181,7 +183,7 @@ case "$ACTION" in if docker ps | grep -q "trader-stream-1"; then # Cluster mode - show all instances echo "Cluster mode detected - showing logs from all instances..." - $DOCKER_COMPOSE -f docker-compose-scale.yml logs -f + run_compose -f docker-compose-scale.yml logs -f else # Single instance mode docker logs -f trader-stream-ee @@ -211,9 +213,9 @@ case "$ACTION" in clean) echo "🧹 Cleaning up..." - $DOCKER_COMPOSE -f docker-compose.yml down -v - $DOCKER_COMPOSE -f docker-compose-standard.yml down -v - $DOCKER_COMPOSE -f docker-compose-scale.yml down -v + run_compose -f docker-compose.yml down -v + run_compose -f docker-compose-standard.yml down -v + run_compose -f docker-compose-scale.yml down -v docker system prune -f echo "✅ Cleaned" ;; diff --git a/stop.sh b/stop.sh index f6ebbce..2d1aa95 100755 --- a/stop.sh +++ b/stop.sh @@ -2,10 +2,6 @@ # Quick stop script -if docker compose version &> /dev/null; then - docker compose down -else - docker-compose down -fi +docker compose down 2>/dev/null || docker-compose down echo "✅ TradeStreamEE stopped" diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..86c9ad2 --- /dev/null +++ b/test.sh @@ -0,0 +1,313 @@ +#!/bin/bash + +# TradeStreamEE Test Execution Script +# Provides different test execution profiles for comprehensive testing + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Function to print colored output +print_status() { + echo -e "${BLUE}[INFO]${NC} $1" +} + +print_success() { + echo -e "${GREEN}[SUCCESS]${NC} $1" +} + +print_warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +print_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# Function to check if a command exists +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Check prerequisites +check_prerequisites() { + print_status "Checking prerequisites..." + + if ! command_exists java; then + print_error "Java is not installed or not in PATH" + exit 1 + fi + + # Check for maven wrapper or installed maven + if [ ! -x "./mvnw" ] && ! command_exists mvn; then + print_error "Maven wrapper (mvnw) not found/executable and 'mvn' not in PATH" + exit 1 + fi + + # Robust Java version check + local java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}') + local major_version=$(echo "$java_version" | awk -F. '{if ($1 == 1) print $2; else print $1}') + + if [[ "$major_version" -lt 17 ]]; then + print_warning "Java 17+ recommended. Current version: $java_version" + fi + + print_success "Prerequisites check completed" +} + +# Clean previous test results +clean_test_results() { + print_status "Cleaning previous test results..." + ./mvnw clean -q + rm -rf target/site/jacoco/ + rm -rf target/surefire-reports/ + rm -rf target/failsafe-reports/ + print_success "Test results cleaned" +} + +# Run unit tests +run_unit_tests() { + print_status "Running unit tests..." + + echo "Compiling and running unit tests..." + if ./mvnw test -q; then + print_success "Unit tests completed successfully" + + # Show test summary if available + if [ -f "target/surefire-reports/TEST-fish.payara.trader.TestRunner.xml" ]; then + local test_count=$(grep -o 'tests="[0-9]*"' target/surefire-reports/*.xml | head -1 | grep -o '[0-9]*') + local failure_count=$(grep -o 'failures="[0-9]*"' target/surefire-reports/*.xml | head -1 | grep -o '[0-9]*') + local error_count=$(grep -o 'errors="[0-9]*"' target/surefire-reports/*.xml | head -1 | grep -o '[0-9]*') + + echo "Test Summary: $test_count tests, $failure_count failures, $error_count errors" + + if [[ "$failure_count" -eq "0" && "$error_count" -eq "0" ]]; then + print_success "All unit tests passed!" + else + print_warning "Some unit tests failed or had errors" + fi + fi + else + print_error "Unit tests failed" + return 1 + fi +} + +# Run integration tests +run_integration_tests() { + print_status "Running integration tests..." + + echo "Compiling and running integration tests..." + if ./mvnw verify -Pintegration-tests -q; then + print_success "Integration tests completed successfully" + else + print_error "Integration tests failed" + return 1 + fi +} + +# Generate coverage report +generate_coverage_report() { + print_status "Generating test coverage report..." + + if ./mvnw jacoco:report -q; then + print_success "Coverage report generated" + + if [ -f "target/site/jacoco/index.html" ]; then + # Extract total coverage percentage more reliably + local instruction_coverage=$(grep -A1 "Total" target/site/jacoco/index.html | grep -oE '[0-9]+%' | head -1) + echo "Instruction Coverage: ${instruction_coverage:-N/A}" + + # Open coverage report in browser if available + if command_exists xdg-open; then + print_status "Opening coverage report in browser..." + xdg-open target/site/jacoco/index.html + elif command_exists open; then + print_status "Opening coverage report in browser..." + open target/site/jacoco/index.html + else + print_status "Coverage report available at: target/site/jacoco/index.html" + fi + fi + else + print_warning "Failed to generate coverage report" + fi +} + +# Run performance benchmarks +run_benchmarks() { + print_status "Running JMH performance benchmarks..." + + echo "This may take several minutes..." + if ./mvnw exec:java@run-benchmarks -q; then + print_success "Benchmarks completed successfully" + else + print_warning "Benchmarks failed or were interrupted" + fi +} + +# Run load tests +run_load_tests() { + print_status "Running load tests..." + + echo "This may take several minutes..." + if ./mvnw failsafe:integration-test -Dtest="LoadTest" -q; then + print_success "Load tests completed successfully" + else + print_warning "Load tests failed or were interrupted" + fi +} + +# Quick test run (unit tests only) +quick_test() { + print_status "Running quick test (unit tests only)..." + clean_test_results + run_unit_tests + generate_coverage_report +} + +# Full test suite +full_test_suite() { + print_status "Running full test suite..." + + clean_test_results + + # Run unit tests + if run_unit_tests; then + # Run integration tests + if run_integration_tests; then + # Generate coverage report + generate_coverage_report + + # Optional: run benchmarks + if [[ "$SKIP_BENCHMARKS" != "true" ]]; then + read -p "Run performance benchmarks? (y/N): " -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + run_benchmarks + fi + fi + + # Optional: run load tests + if [[ "$SKIP_LOAD_TESTS" != "true" ]]; then + read -p "Run load tests? (y/N): " -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + run_load_tests + fi + fi + + print_success "Full test suite completed successfully!" + else + print_error "Integration tests failed" + return 1 + fi + else + print_error "Unit tests failed" + return 1 + fi +} + +# Show test results summary +show_results() { + print_status "Test Results Summary:" + + echo "" + echo "Reports available:" + if [ -f "target/site/jacoco/index.html" ]; then + echo " • Coverage Report: target/site/jacoco/index.html" + fi + + if [ -d "target/surefire-reports" ]; then + echo " • Unit Test Reports: target/surefire-reports/" + fi + + if [ -d "target/failsafe-reports" ]; then + echo " • Integration Test Reports: target/failsafe-reports/" + fi + + echo "" +} + +# Help function +show_help() { + echo "TradeStreamEE Test Execution Script" + echo "" + echo "Usage: $0 [OPTION]" + echo "" + echo "Options:" + echo " quick Run quick unit tests only" + echo " full Run full test suite (unit + integration)" + echo " unit Run unit tests" + echo " integration Run integration tests" + echo " coverage Generate coverage report only" + echo " benchmarks Run JMH performance benchmarks" + echo " load Run load tests" + echo " clean Clean test results" + echo " results Show test results summary" + echo " help Show this help message" + echo "" + echo "Environment Variables:" + echo " SKIP_BENCHMARKS=true Skip running benchmarks in full suite" + echo " SKIP_LOAD_TESTS=true Skip running load tests in full suite" + echo "" +} + +# Main script logic +main() { + echo "=========================================" + echo " TradeStreamEE Test Runner" + echo "=========================================" + echo "" + + check_prerequisites + + case "${1:-help}" in + "quick") + quick_test + show_results + ;; + "full") + full_test_suite + show_results + ;; + "unit") + clean_test_results + run_unit_tests + show_results + ;; + "integration") + clean_test_results + run_integration_tests + show_results + ;; + "coverage") + generate_coverage_report + ;; + "benchmarks") + run_benchmarks + ;; + "load") + clean_test_results + run_load_tests + show_results + ;; + "clean") + clean_test_results + ;; + "results") + show_results + ;; + "help"|*) + show_help + ;; + esac +} + +# Run main function with all arguments +main "$@" \ No newline at end of file From a0396756d4e0f01e895983bc8a43df4434867b3c Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Mon, 22 Dec 2025 23:18:31 +0000 Subject: [PATCH 14/17] Nitpick from jee-10 branch --- .../trader/aeron/AeronSubscriberBean.java | 12 +- .../trader/aeron/MarketDataPublisher.java | 6 +- .../payara/trader/rest/GCStatsResource.java | 11 +- .../websocket/MarketDataBroadcaster.java | 11 +- src/main/webapp/blog.html | 422 +++++++ src/main/webapp/index.html | 1024 ++++++++++++----- 6 files changed, 1160 insertions(+), 326 deletions(-) create mode 100644 src/main/webapp/blog.html diff --git a/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java b/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java index 9202bb4..a5335c0 100644 --- a/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java +++ b/src/main/java/fish/payara/trader/aeron/AeronSubscriberBean.java @@ -18,6 +18,7 @@ import org.agrona.CloseHelper; import org.agrona.concurrent.BackoffIdleStrategy; import org.agrona.concurrent.IdleStrategy; +import org.eclipse.microprofile.config.inject.ConfigProperty; /** * Aeron Ingress Singleton Bean Launches an embedded MediaDriver and subscribes to market data @@ -43,11 +44,20 @@ public class AeronSubscriberBean { @Inject @VirtualThreadExecutor private ManagedExecutorService managedExecutorService; + @Inject + @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") + private String ingestionMode; + void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { - managedExecutorService.submit(() -> init()); + managedExecutorService.submit(this::init); } public void init() { + if ("DIRECT".equalsIgnoreCase(ingestionMode)) { + LOGGER.info("Running in DIRECT mode - Skipping Aeron/MediaDriver initialization."); + return; + } + LOGGER.info("Initializing Aeron Subscriber Bean..."); try { diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java index 84af2ec..285b07d 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataPublisher.java @@ -54,8 +54,7 @@ public class MarketDataPublisher { private final MarketDepthEncoder marketDepthEncoder = new MarketDepthEncoder(); private final HeartbeatEncoder heartbeatEncoder = new HeartbeatEncoder(); - // Buffer for encoding messages - private final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(BUFFER_SIZE)); + private UnsafeBuffer buffer; private final AtomicLong sequenceNumber = new AtomicLong(0); private final AtomicLong tradeIdGenerator = new AtomicLong(1000); @@ -139,6 +138,9 @@ public void init() { return; } + // Initialize buffer only if in AERON mode + this.buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(BUFFER_SIZE)); + try { // Wait for AeronSubscriberBean to be ready (both observers fire at roughly same time) LOGGER.info("Waiting for AeronSubscriberBean to be ready..."); diff --git a/src/main/java/fish/payara/trader/rest/GCStatsResource.java b/src/main/java/fish/payara/trader/rest/GCStatsResource.java index b07a427..15a5ce1 100644 --- a/src/main/java/fish/payara/trader/rest/GCStatsResource.java +++ b/src/main/java/fish/payara/trader/rest/GCStatsResource.java @@ -76,12 +76,12 @@ public Response getComparison() { // Identify which JVM is running String jvmVendor = System.getProperty("java.vm.vendor"); String jvmName = System.getProperty("java.vm.name"); + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); String gcName = - ManagementFactory.getGarbageCollectorMXBeans().stream() - .map(GarbageCollectorMXBean::getName) - .collect(Collectors.joining(", ")); + gcBeans.stream().map(GarbageCollectorMXBean::getName).collect(Collectors.joining(", ")); - boolean isAzulC4 = jvmVendor != null && jvmVendor.contains("Azul"); + // More accurate check: C4 collector's MXBean is named "GPGC" + boolean isAzulC4 = gcBeans.stream().anyMatch(bean -> "GPGC".equals(bean.getName())); comparison.put("jvmVendor", jvmVendor); comparison.put("jvmName", jvmName); @@ -91,6 +91,9 @@ public Response getComparison() { // Current stress level comparison.put("allocationMode", memoryPressureService.getCurrentMode()); + comparison.put( + "allocationRateMBps", + memoryPressureService.getCurrentMode().getBytesPerSecond() / (1024 * 1024)); comparison.put("messageRate", publisher.getMessagesPublished()); // GC Performance Metrics (keep old stats for backward compatibility) diff --git a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java index c03953d..61c997a 100644 --- a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java +++ b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java @@ -2,8 +2,6 @@ import com.hazelcast.core.HazelcastInstance; import com.hazelcast.topic.ITopic; -import com.hazelcast.topic.Message; -import com.hazelcast.topic.MessageListener; import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -51,12 +49,9 @@ public void init() { if (hazelcastInstance != null) { clusterTopic = hazelcastInstance.getTopic(TOPIC_NAME); clusterTopic.addMessageListener( - new MessageListener() { - @Override - public void onMessage(Message message) { - // Broadcast to local WebSocket sessions only - broadcastLocal(message.getMessageObject()); - } + message -> { + // Broadcast to local WebSocket sessions only + broadcastLocal(message.getMessageObject()); }); LOGGER.info("Subscribed to Hazelcast topic: " + TOPIC_NAME + " (clustered mode)"); } else { diff --git a/src/main/webapp/blog.html b/src/main/webapp/blog.html new file mode 100644 index 0000000..383301e --- /dev/null +++ b/src/main/webapp/blog.html @@ -0,0 +1,422 @@ + + + + + + Azul C4 and Payara: High-Speed Financial Applications - TradeStreamEE Blog + + + + +

+
+ + + + + Back to Dashboard + +

Azul C4 and Payara: The Ideal Base for High-Speed Financial Applications

+

How Enterprise Java with pauseless garbage collection enables low-latency high-frequency trading systems

+
+
+ + + + 8 min read +
+
+ + + + Azul C4, Payara Micro, Jakarta EE, HFT +
+
+
+ +
+

In high-frequency trading (HFT) and financial services, every microsecond counts. The difference between profit and loss often comes down to how quickly your system can process market data and execute trades. For years, Java has been viewed with skepticism in the world of low-latency finance, primarily due to its notorious "stop-the-world" garbage collection pauses that can wreck predictable performance.

+ +

But what if I told you that Java is now one of the best choices for latency-sensitive financial applications?

+ +

I developed TradeStreamEE, a sophisticated high-frequency trading dashboard to demonstrate that HFT is not only possible but also highly performant in Enterprise Java, particularly when combining Azul Platform Prime with the C4 garbage collector and Payara Micro. This technology stack changes Java's performance profile, offering measurable, real-world improvements that are now leading financial institutions to reconsider their current technology choices.

+ +

The Evidence: 2GB/s Pressure, 1ms Latency

+ +

We subjected TradeStreamEE to an extreme torture test: generating 2 Gigabytes of garbage per second while simultaneously processing market data.

+ +

In a standard OpenJDK environment using G1GC, this level of pressure typically results in "Stop-the-World" pauses lasting hundreds of milliseconds. The user interface freezes. Tickers jump. Traders lose money.

+ +

With Azul C4, the results were staggering. We queried the live application status while it was under this 2GB/s bombardment. Here is the raw JSON returned from our Payara endpoints:

+ +
{
+  "running": true,
+  "description": "2 GB/sec - Extreme pressure",
+  "bytesPerSecond": 2048000000,
+  "currentMode": "EXTREME",
+  "percentiles": {
+    "max": 1,
+    "p50": 0,
+    "p99": 1,
+    "p999": 1
+  }
+}
+ +
+

Read that again.

+

Allocating 2GB of data every second, the application's garbage collection pauses peaked at 1 millisecond. The 50th percentile (median) was 0ms. The SLA monitor reported zero violations over 10ms.

+
+ +

Payara Micro: Enterprise Jakarta EE for Demanding Applications

+ +

While Azul C4 eliminates Java's performance limitations, Payara Micro provides the enterprise-grade Jakarta EE runtime that makes sophisticated financial applications possible.

+ +

What makes Payara Micro well suited for financial services is its thoughtful balance of enterprise features and performance optimization. The platform weighs just 100MB and starts in seconds, making it perfect for the microservices architectures that dominate modern financial technology stacks. But do not be deceived by the small footprint. It packs the full power of Jakarta EE 11, featuring critical implementations like Jakarta CDI for dependency injection, WebSocket support for real-time communication, and Jakarta REST for RESTful APIs.

+ +

Built-in Clustering with Hazelcast

+ +

Using Hazelcast under the hood, Payara provides automatic data replication across cluster nodes. Through such deep integration, creating a cluster of Payara Micro instances is a breeze.

+ +

In our code, we didn't have to write complex distributed consensus logic. We simply injected the Hazelcast instance to track global throughput across the cluster:

+ +
// Payara + Hazelcast: Instant cluster-wide metrics without complex setup
+@Inject private HazelcastInstance hazelcastInstance;
+
+public void init() {
+    // This counter is automatically shared across all cluster nodes
+    // No extra infrastructure code required
+    clusterMessageCounter =
+        hazelcastInstance.getCPSubsystem().getAtomicLong("cluster-message-count");
+}
+ +

This means financial firms can build highly available trading systems that can withstand node failures without losing market data or transaction state.

+ +

Architecture Deep Dive Into TradeStreamEE

+ +

The TradeStreamEE application serves as a perfect showcase for what's possible when you combine Azul C4 and Payara Micro. The architecture demonstrates thoughtful engineering that addresses the specific challenges of financial trading systems.

+ +

The "Hero": Zero-Copy SBE

+ +

At the core of the system is Aeron IPC with Simple Binary Encoding (SBE), technologies that enable zero-copy message processing. The approach is elegant: instead of creating new Java objects for every market data message, the system uses "flyweight" objects that act as windows over shared memory buffers. This eliminates object allocation entirely during the critical path of message processing.

+ +
// SBE Flyweight pattern - zero object allocation during processing
+private final TradeDecoder tradeDecoder = new TradeDecoder();
+private final MessageHeaderDecoder headerDecoder = new MessageHeaderDecoder();
+
+@Override
+public void onFragment(DirectBuffer buffer, int offset, int length, Header header) {
+    headerDecoder.wrap(buffer, offset);
+
+    // Move the "window" to the data without allocating a new object
+    tradeDecoder.wrap(buffer, offset + headerDecoder.encodedLength(),
+                     headerDecoder.blockLength(), headerDecoder.version());
+
+    // Extract data directly from the buffer
+    final long timestamp = tradeDecoder.timestamp();
+    final long price = tradeDecoder.price();
+    final long quantity = tradeDecoder.quantity();
+}
+ +

The "Villain": The Memory Pressure Service

+ +

To prove the reliability of the system, we included a component designed specifically to break it. The MemoryPressureService generates massive amounts of random data to simulate extreme market volatility or memory leaks.

+ +
// The "Villain": Generating 2GB/sec of garbage to stress the JVM
+private void generateGarbage(AllocationMode mode) {
+    // 20,000 allocations per 100ms iteration = ~2GB/sec
+    for (int i = 0; i < mode.getAllocationsPerIteration(); i++) {
+        // Create 100KB byte array, touch it, and immediately discard it
+        byte[] garbage = new byte[1024 * 100];
+        ThreadLocalRandom.current().nextBytes(garbage); // Prevent optimization
+    }
+}
+ +

Even with this "villain" running full throttle, the Azul C4 collector cleaned up the mess concurrently, ensuring the trading threads never stopped.

+ +

The Business Impact

+ +

The business implications of this technology combination extend far beyond technical performance metrics. For trading firms, predictable latency directly translates to competitive advantage in the market.

+ +

Perhaps more importantly, the operational complexity that has traditionally plagued Java performance tuning virtually disappears. The standard approach to Java latency problems involved teams of performance engineers spending months tuning JVM parameters (-XX:NewRatio, -XX:SurvivorRatio). With Azul C4 and Payara Micro, most of that complexity evaporates. C4 adapts automatically to workload patterns, eliminating the need for manual parameter tuning.

+ +

In our tests, a single Payara Micro instance effortlessly handled ingestion rates exceeding 30 million messages, with zero degradation in SLA compliance. This means financial firms can process more trades per second, handle higher client volumes without performance degradation, and maintain consistent service levels even during market stress events.

+ +

Conclusion

+ +

Azul C4 and Payara Micro fundamentally redefine the potential of Java for low-latency financial applications. This is more than an incremental gain; it's a paradigm shift.

+ +

By combining the operational maturity of Payara with the runtime supremacy of Azul Platform Prime, organizations can have their cake and eat it too. You can build maintainable, secure, and scalable Jakarta EE applications that meet the most demanding latency SLAs of the financial industry.

+ +

The question is no longer whether Java can handle low-latency financial workloads. With Azul C4 and Payara, it's now one of the best choices available.

+ +
+

Ready to See It in Action?

+

The theory is compelling, but the real proof is in the running system. I encourage you to:

+
    +
  • Explore the Code: Check out the TradeStreamEE repository on GitHub to see the complete implementation. The codebase demos best practices for low-latency Java development.
  • +
  • Try Payara Micro: Download Payara Micro and experience the lightweight, enterprise-grade application server for yourself.
  • +
  • Try Payara Qube: Deploy to Payara Qube (formerly Payara Cloud) for effortless cloud deployment without the infrastructure overhead.
  • +
  • Evaluate Azul Platform Prime: Request a trial of Azul Platform Prime to see C4 garbage collection in action. The performance improvements are immediately noticeable, especially under memory pressure.
  • +
  • Run Your Own Tests: Use the built-in stress testing capabilities in TradeStreamEE to see how your applications perform under realistic load conditions.
  • +
+
+
+
+ + + + diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index a51b6b6..3e33d64 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -1,5 +1,6 @@ + @@ -8,15 +9,41 @@ +

TradeStreamEE

-

High-Frequency Trading Dashboard • Aeron + SBE + Payara Micro + Azul Platform Prime

+

High-Frequency Trading Dashboard • Aeron + SBE + Payara Micro + Azul Platform Prime +

-
-
-
-
- Disconnected - - +
+ + + + + Read Blog
+ + + +
-
0
Local Msg/sec
-
0
Local Total
+
+
0
+
Local Msg/sec
+
+
+
0
+
Local Total
+
-
0
Cluster Total
-
0
Cluster Msg/sec
+
+
0
+
Cluster Total
+
+
+
0
+
Cluster Msg/sec
+
-
0
UI Msg/sec
-
0
UI Received
+
+
0
+
UI Msg/sec
+
+
+
0
+
UI Received
+
+
+ + +
+
+
+ Disconnected +
+ + + +
- - + +

Demo Scenarios

@@ -586,7 +779,7 @@

GC Performance Comparison

@@ -629,9 +822,9 @@

Allocation Rate

- +
- +
@@ -645,15 +838,17 @@

Allocation Rate

Hiccup Monitor

-

Tracks inter-arrival time gaps between messages. Spikes indicate "Stop-the-World" GC pauses where the application freezes.

-

With Azul C4's pauseless GC, you should see a flat line even at 100k+ msg/sec. Standard OpenJDK G1GC shows frequent spikes as it struggles to clean up heap.

+

Tracks inter-arrival time gaps between messages. Spikes indicate "Stop-the-World" GC pauses where + the application freezes.

+

With Azul C4's pauseless GC, you should see a flat line even at 100k+ msg/sec. Standard OpenJDK + G1GC shows frequent spikes as it struggles to clean up heap.

Measured: Performance.now() delta between WebSocket messages
- +
@@ -687,9 +882,13 @@

Hiccup Monitor

GC Statistics

-

Real-time garbage collection metrics. Collection count matters less than pause duration.

-

G1GC: Fewer collections (48), but longer pauses (4ms avg). At 100k msg/sec, a 4ms pause = 400 dropped messages.

-

Azul C4: More collections (500), but sub-millisecond pauses (0.1ms avg). Same load = only 10 dropped messages.

+

Real-time garbage collection metrics. Collection count matters less than + pause duration. +

+

G1GC: Fewer collections (48), but longer pauses (4ms avg). At 100k msg/sec, a + 4ms pause = 400 dropped messages.

+

Azul C4: More collections (500), but sub-millisecond pauses (0.1ms avg). Same + load = only 10 dropped messages.

C4 does work concurrently while G1GC batches cleanup into painful stops.

Source: /metrics endpoint (Prometheus format)
@@ -698,7 +897,7 @@

GC Statistics

- +
@@ -714,28 +913,37 @@

GC Statistics

- - - - - + + + + +

GC Challenge Mode

-

Intentionally generates garbage to stress-test the GC under load. Watch how each collector responds.

-

Azul C4: Maintains low, consistent pause times even under EXTREME allocation pressure. Concurrent collection keeps up.

-

G1GC: Pause times spike as allocation rate increases. You'll see stuttering in message delivery and chart updates.

+

Intentionally generates garbage to stress-test the GC under load. Watch how each collector + responds.

+

Azul C4: Maintains low, consistent pause times even under EXTREME allocation + pressure. Concurrent collection keeps up.

+

G1GC: Pause times spike as allocation rate increases. You'll see stuttering in + message delivery and chart updates.

Rates: LOW=0.1MB/s, MEDIUM=0.5MB/s, HIGH=2MB/s, EXTREME=10MB/s

- Technique: Mixed allocation patterns (Strings, arrays, objects, collections)
+ Technique: Mixed allocation patterns (Strings, arrays, objects, + collections)
Inspired by: 1BRC stress testing
- +
@@ -750,9 +958,12 @@

GC Challenge Mode

GC Pause Time Monitor

Real-time visualization of garbage collection pause times. Lower is better.

-

Azul C4 (Pauseless): Flat line near zero. Collections happen concurrently without stopping application threads.

-

G1GC (Stop-the-World): Visible spikes when GC pauses occur. Higher message rates = higher pause times.

-

This chart demonstrates why C4 is ideal for latency-sensitive applications - predictable, consistent performance without pause spikes.

+

Azul C4 (Pauseless): Flat line near zero. Collections happen concurrently + without stopping application threads.

+

G1GC (Stop-the-World): Visible spikes when GC pauses occur. Higher message rates + = higher pause times.

+

This chart demonstrates why C4 is ideal for latency-sensitive applications - predictable, + consistent performance without pause spikes.

API: /api/gc/stats
Metric: Last pause duration (ms)
@@ -761,7 +972,7 @@

GC Pause Time Monitor

- +
@@ -774,8 +985,10 @@

GC Pause Time Monitor

Price Monitor

-

Displays the last 20 trade prices received via WebSocket. Updates in real-time as messages stream in.

-

Watch for smooth, continuous updates with Aeron mode. Any stuttering indicates GC pauses blocking the ingestion pipeline.

+

Displays the last 20 trade prices received via WebSocket. Updates in real-time as messages stream + in.

+

Watch for smooth, continuous updates with Aeron mode. Any stuttering indicates GC pauses blocking + the ingestion pipeline.

Chart: Chart.js (Bar)
Updates: On each Trade message @@ -783,7 +996,7 @@

Price Monitor

- +
Recent Trades
@@ -791,7 +1004,8 @@

Price Monitor

Trade Messages

-

Shows the 5 most recent trade executions decoded from SBE binary format. Each trade includes timestamp, symbol, price, and quantity.

+

Shows the 5 most recent trade executions decoded from SBE binary format. Each trade includes + timestamp, symbol, price, and quantity.

SBE Template: Trade (ID: 1)
Decoding: Zero-copy Flyweight pattern @@ -806,7 +1020,8 @@

Trade Messages

Quote Messages

-

Displays bid/ask spreads for active symbols. Generated synthetically by the publisher and encoded using SBE.

+

Displays bid/ask spreads for active symbols. Generated synthetically by the publisher and encoded + using SBE.

SBE Template: Quote (ID: 2)
Fields: Bid Price, Ask Price, Bid Size, Ask Size @@ -821,7 +1036,8 @@

Quote Messages

Market Depth

-

Level 2 order book data showing buy/sell orders at different price levels. Demonstrates SBE's ability to encode complex nested structures.

+

Level 2 order book data showing buy/sell orders at different price levels. Demonstrates SBE's + ability to encode complex nested structures.

SBE Template: MarketDepth (ID: 3)
Structure: Repeating groups for each price level @@ -836,7 +1052,8 @@

Market Depth

System Log

-

Connection events and diagnostic messages. Logs WebSocket lifecycle and ingestion mode switches.

+

Connection events and diagnostic messages. Logs WebSocket lifecycle and ingestion mode switches. +

Purpose: Debugging and monitoring
Events: Connect, Disconnect, Mode changes @@ -846,8 +1063,8 @@

System Log

+ \ No newline at end of file From 4e06d9452bca23c6f44f7d4349895303cf8088ab Mon Sep 17 00:00:00 2001 From: Luqman Saeed Date: Fri, 26 Dec 2025 10:33:06 +0000 Subject: [PATCH 15/17] Mobile friendly --- Dockerfile | 1 + Dockerfile.scale | 1 + Dockerfile.scale.standard | 1 + Dockerfile.standard | 1 + README.md | 476 ++-- .../payara/resource/HelloWorldResource.java | 5 +- .../aeron/MarketDataFragmentHandler.java | 14 +- .../fish/payara/trader/gc/GCStatsService.java | 4 - .../trader/monitoring/GCPauseMonitor.java | 19 +- .../trader/monitoring/SLAMonitorService.java | 6 - .../pressure/MemoryPressureService.java | 23 +- .../payara/trader/rest/ApplicationConfig.java | 5 +- .../payara/trader/rest/GCStatsResource.java | 12 +- .../trader/rest/MemoryPressureResource.java | 2 - .../payara/trader/rest/StatusResource.java | 1 - .../websocket/MarketDataBroadcaster.java | 5 - .../trader/websocket/MarketDataWebSocket.java | 3 - src/main/webapp/index.html | 2326 +++++++++-------- start-comparison.sh | 117 +- start.sh | 4 +- 20 files changed, 1573 insertions(+), 1453 deletions(-) diff --git a/Dockerfile b/Dockerfile index 32aa741..f4d748e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ RUN ./mvnw dependency:go-offline -B COPY src ./src +RUN ./mvnw spotless:apply RUN ./mvnw clean package -DskipTests # Use Azul Platform Prime for C4 GC diff --git a/Dockerfile.scale b/Dockerfile.scale index de8b009..dbaff61 100644 --- a/Dockerfile.scale +++ b/Dockerfile.scale @@ -14,6 +14,7 @@ RUN ./mvnw dependency:go-offline -B COPY src ./src +RUN ./mvnw spotless:apply RUN ./mvnw clean package -DskipTests # Use Azul Platform Prime for C4 GC diff --git a/Dockerfile.scale.standard b/Dockerfile.scale.standard index a46ac40..8ca0647 100644 --- a/Dockerfile.scale.standard +++ b/Dockerfile.scale.standard @@ -14,6 +14,7 @@ RUN ./mvnw dependency:go-offline -B COPY src ./src +RUN ./mvnw spotless:apply RUN ./mvnw clean package -DskipTests # Use Eclipse Temurin (Standard OpenJDK) diff --git a/Dockerfile.standard b/Dockerfile.standard index 34dfa4d..0754372 100644 --- a/Dockerfile.standard +++ b/Dockerfile.standard @@ -14,6 +14,7 @@ RUN ./mvnw dependency:go-offline -B COPY src ./src +RUN ./mvnw spotless:apply RUN ./mvnw clean package -DskipTests # Use Eclipse Temurin (Standard OpenJDK) diff --git a/README.md b/README.md index c3886ec..b7248da 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,30 @@ # TradeStreamEE: High-Frequency Trading Reference Architecture -**TradeStreamEE** is a technical demonstration application designed to showcase the "Pauseless Performance" symbiosis between **Payara Server Enterprise** (Jakarta EE) and **Azul Platform Prime** (High-Performance JVM). +**TradeStreamEE** demonstrates how Jakarta EE applications can achieve low-latency, high-throughput performance by combining modern messaging (Aeron), efficient serialization (SBE), and a concurrent garbage collector (Azul C4). -It simulates a high-frequency trading (HFT) dashboard that ingests tens of thousands of market data messages per second, processes them in real-time, and broadcasts updates to a web frontend—all without the latency spikes ("jitter") associated with standard Java Garbage Collection. +It simulates a high-frequency trading dashboard that ingests tens of thousands of market data messages per second, processes them in real-time, and broadcasts updates to a web frontend—without the latency spikes associated with traditional Java garbage collection. ## ⚡ The Core Technologies -TradeStreamEE gets its speed by removing the middleman. We swapped out heavy, traditional methods (REST/JSON) for 'Mechanical Sympathy', an approach that respects the underlying hardware to squeeze out maximum efficiency. +TradeStreamEE achieves high performance by combining four key technologies: -### 1\. What is Binary Encoding? +### Aeron (Transport) -Computers do not natively understand text; they understand bits. +Ultra-low latency messaging that bypasses the network stack when components run on the same machine. Data moves directly through shared memory instead of through kernel networking. -* **Text Encoding (JSON/XML):** Easy for humans (`{"price": 100}`), but expensive for computers. The CPU must parse every character, handle whitespace, and convert strings to numbers. This burns CPU cycles and creates massive amounts of temporary memory "garbage." -* **Binary Encoding:** Stores data exactly as the machine represents it in memory (e.g., `100` is stored as 4 raw bytes). No parsing is required. This results in **deterministic latency** and significantly reduced CPU usage. +### SBE (Serialization) -### 2\. Aeron (The Transport) +Simple Binary Encoding - the financial industry standard for high-frequency trading. Encodes data as compact binary rather than human-readable text, eliminating parsing overhead and reducing memory allocation. -[Aeron](https://aeron.io/) is a peer-to-peer, broker-less transport protocol designed for **ultra-low latency** applications. +### Payara Micro (Jakarta EE Runtime) -* **Broker-Less:** There is no central server "middleman." The Publisher sends data directly to the Subscriber's memory address. -* **IPC (Inter-Process Communication):** When components run on the same machine (like in this demo), Aeron bypasses the network stack entirely, writing data directly to shared memory (RAM). +A cloud-native Jakarta EE 11 application server that provides Jakarta CDI for dependency injection, Jakarta WebSocket for real-time browser communication, and Jakarta REST for REST endpoints. Lightweight (~100MB) and fast-starting. -### 3\. SBE (Simple Binary Encoding) +### Azul Platform Prime (Runtime) -[SBE](https://github.com/real-logic/simple-binary-encoding) is the standard for high-frequency financial trading (FIX SBE). It serves as the **Presentation Layer**, defining how business data (Trades, Quotes) is structured inside the Aeron buffers. +A JVM with the C4 garbage collector that performs cleanup concurrently with application execution, avoiding the "stop-the-world" pauses that cause latency spikes in traditional JVMs. -#### **How SBE Works** - -Unlike JSON, where you just write data, SBE is **Schema-Driven**. This ensures strict structure and maximum speed. - -1. **Define the Schema (`market-data.xml`):** You define your messages in XML. This acts as the contract between Publisher and Subscriber. - - ```xml - - - - - ``` -2. **Generate Code:** During the build process (`mvn generate-sources`), the **SbeTool** reads the XML and generates Java classes (Encoders and Decoders). -3. **Zero-Copy Encoding/Decoding:** - * **The Flyweight Pattern:** The generated Java classes are "Flyweights." They do not hold data themselves. Instead, they act as a "window" over the raw byte buffer. - * **No Allocation:** When we read a Trade message, **we do not create a `Trade` object**. We simply move the "window" to the correct position in memory and read the `long` value for price. This generates **zero garbage** for the Garbage Collector to clean up. +**How they work together:** Payara Micro manages the Aeron publisher/subscriber lifecycle via CDI. The publisher encodes market data into binary using SBE, sends it through Aeron's shared memory transport, and the subscriber decodes it without allocating Java objects. This minimal garbage generation lets C4's concurrent collection easily handle the cleanup, maintaining flat latency even at high throughput. ## 🚀 The Rationale: Why This Project Exists @@ -54,10 +37,72 @@ Standard JVMs (using G1GC or ParallelGC) often "hiccup" under high load, causing ### The "A/B" Comparison -This project includes built-in tools to benchmark "The Old Way" vs. "The New Way": +This project enables side-by-side JVM comparison with identical architectural choices: + +* **Both clusters** use **AERON IPC + Zero-Copy SBE** by default (optimized architecture) +* **Both clusters** can optionally run **DIRECT mode** (naive string processing) via `TRADER_INGESTION_MODE` flag +* **The key difference** is the JVM: Azul C4 (pauseless) vs Standard G1GC (stop-the-world) + +This isolates the GC as the variable, letting you see how the same application behaves under different garbage collectors. + +## 🚦 Quick Start + +### Primary Use Case: Side-by-Side JVM Comparison + +**Why this matters:** Most benchmarking tools run tests serially, start JVM A, test, shut down, start JVM B, test, then manually compare spreadsheets. That's not how real systems behave. + +TradeStreamEE deploys both JVMs simultaneously, enabling you to see real-time behavior differences under identical conditions. + +**The key point:** Both clusters run the **exact same WAR file**, same code, same configuration, same workload. The **only difference** is the JVM. This isolates the garbage collector as the sole variable, making the comparison fair and meaningful. + +```bash +./start-comparison.sh all +``` + +**What this does:** + +1. Builds and deploys **Azul C4 cluster** (3 instances, ports 8080-8083) +2. Builds and deploys **G1GC cluster** (3 instances, ports 9080-9083) +3. Starts **Prometheus** for metrics collection +4. Starts **Grafana** with pre-configured JVM comparison dashboards +5. Starts **Loki/Promtail** for centralized logging + +**Then open both URLs in separate browser tabs:** + +- **C4 Cluster:** http://localhost:8080/trader-stream-ee/ +- **G1 Cluster:** http://localhost:9080/trader-stream-ee/ + +**Watch the "GC Pause Time (Live)" chart** to see the difference: + +| Metric | Azul C4 | G1GC | What You'll See | +|:-------------------------|:-------------------|:------------------|:------------------------------------------------------------------| +| **GC Pause Time (Live)** | Flatline | Spikes | C4 maintains consistent latency; G1GC shows stop-the-world pauses | +| **Heap Usage** | Smooth oscillation | Sawtooth patterns | Different collection strategies visualized in real-time | +| **Under Load** | Barely moves | Jitter increases | Apply stress via UI and watch the gap widen | + +**Other options:** + +- `./start-comparison.sh` - Clusters only, no monitoring stack (faster for rapid development) +- `./stop-comparison.sh` - Stop all comparison services + +--- + +### Single-JVM Testing (start.sh) + +Use `./start.sh` for testing individual configurations or architectural modes. This runs ONE JVM at a time for focused testing. + +| Scenario | Command | JVM | Architecture | Purpose | +|:-------------------------|:-----------------------------|:--------------------|:------------------|:-----------------------------------------------------------------| +| Modern Stack | `./start.sh azul-aeron` | Azul Prime (C4) | Aeron (Optimized) | Peak performance with pauseless GC + zero-copy transport | +| Legacy Baseline | `./start.sh standard-direct` | Standard JDK (G1GC) | Direct (Heavy) | Baseline: high allocation on G1GC, expect jitter | +| Fixing Legacy Code | `./start.sh azul-direct` | Azul Prime (C4) | Direct (Heavy) | Show how C4 stabilizes high-allocation apps without code changes | +| Optimizing Standard Java | `./start.sh standard-aeron` | Standard JDK (G1GC) | Aeron (Optimized) | Test if architectural optimization helps G1GC performance | -* **Scenario A (Baseline):** Standard OpenJDK + Naive String Processing. -* **Scenario B (Optimized):** Azul Platform Prime + Aeron IPC + Zero-Copy SBE. +**Utilities:** + +- `./start.sh logs` - View live logs +- `./start.sh stop` - Stop containers +- `./start.sh clean` - Deep clean (remove volumes/images) ## 🏗️ Technical Architecture @@ -89,113 +134,70 @@ The application implements a **Hybrid Architecture**: ## 🔍 Understanding the Modes -This demo allows you to switch between two distinct ingestion pipelines to visualize the impact of architectural choices on JVM performance. - -### 1\. DIRECT Mode (The "Heavy" Path) +This demo supports two ingestion architectures that you can use with **either JVM**. Set the `TRADER_INGESTION_MODE` environment variable (`AERON` or `DIRECT`) to switch between them. -**Goal:** Simulate a standard, naive enterprise application with high object allocation rates. -**Runtime:** Standard OpenJDK (Eclipse Temurin 21) with G1GC. - -**Data Flow:** +**Important:** Both Azul C4 and G1GC clusters can run either mode. This lets you test whether architectural optimization (AERON) helps G1GC performance, or how C4 handles high-allocation legacy code (DIRECT). ```mermaid graph TD - classDef purple fill:#667eea,stroke:#4a5be7,stroke-width:2px,color:#ffffff,font-weight:bold; - classDef red fill:#dc3545,stroke:#c82333,stroke-width:2px,color:#ffffff,font-weight:bold; - classDef lightgray fill:#f8f9fa,stroke:#ced4da,stroke-width:1px,color:#333333; - classDef darkgray fill:#6c757d,stroke:#5a6268,stroke-width:1px,color:#ffffff; - - A[Publisher]:::purple -->|Generates POJOs| B(JSON Builder):::lightgray - B -->|Large String Allocation| C(Heavy JSON):::red - C -->|Direct Method Call| D[Broadcaster]:::purple - D -->|WebSocket Payload > 1KB| E[Browser]:::darkgray - - linkStyle 0 stroke:#667eea,stroke-width:2px; - linkStyle 1 stroke:#dc3545,stroke-width:2px; - linkStyle 2 stroke:#667eea,stroke-width:2px; - linkStyle 3 stroke:#764ba2,stroke-width:2px; + subgraph DIRECT["DIRECT Mode (High Allocation)"] + D1[Publisher] --> D2["JSON + 1KB padding"] + D2 --> D3[Broadcaster] + D3 --> D4["WebSocket → Browser"] + end + + subgraph AERON["AERON Mode (Zero-Copy, Default)"] + A1[Publisher] --> A2["SBE Binary"] + A2 --> A3["Aeron IPC"] + A3 --> A4["Fragment Handler"] + A4 --> A5[Broadcaster] + A5 --> A6["WebSocket → Browser"] + end + + classDef heavy fill:#dc3545,stroke:#c82333,stroke-width:2px,color:#fff + classDef optimized fill:#28a745,stroke:#218838,stroke-width:2px,color:#fff + + class D1,D2,D3,D4 heavy + class A1,A2,A3,A4,A5,A6 optimized ``` -1. **Publisher:** Generates synthetic market data as standard Java Objects. -2. **Allocation:** Immediately converts data to a JSON `String` using `StringBuilder` (high allocation). -3. **Artificial Load:** Wraps the JSON in a large "envelope" with 1KB of padding to stress the Garbage Collector. -4. **Transport:** Direct method call to `MarketDataBroadcaster`. -5. **WebSocket:** Pushes the heavy JSON string to the browser. -6. **Browser:** Unwraps the payload and renders the chart. - -**Performance Characteristics:** - -* **High Allocation Rate:** Gigabytes of temporary String objects created per second. -* **GC Pressure:** Frequent "Stop-the-World" pauses from G1GC lead to "jitter" in the UI charts. +### 1\. DIRECT Mode (The "Heavy" Path) -### 2\. AERON Mode (The "Optimized" Path) +**Goal:** Simulate a standard, naive enterprise application with high object allocation rates. +**Use for:** Stress-testing GC behavior under high allocation pressure. -**Goal:** Simulate a low-latency financial pipeline using off-heap memory and zero-copy semantics. -**Runtime:** Azul Platform Prime (Zulu Prime 21) with C4 Pauseless GC. +**How it works:** -**Data Flow:** - -```mermaid -graph TD - classDef green fill:#28a745,stroke:#218838,stroke-width:2px,color:#ffffff,font-weight:bold; - classDef blue fill:#007bff,stroke:#0069d9,stroke-width:2px,color:#ffffff,font-weight:bold; - classDef purple fill:#667eea,stroke:#4a5be7,stroke-width:2px,color:#ffffff,font-weight:bold; - classDef darkgray fill:#6c757d,stroke:#5a6268,stroke-width:1px,color:#ffffff; - - A[Publisher]:::purple -->|Generates POJOs| B(SBE Encoder):::green - B -->|Binary IPC| C{Aeron Ring Buffer}:::blue - C -->|Shared Memory| D(Fragment Handler):::green - D -->|Decode & JSON| E[Broadcaster]:::purple - E -->|WebSocket Payload| F[Browser]:::darkgray - - linkStyle 0 stroke:#667eea,stroke-width:2px; - linkStyle 1 stroke:#28a745,stroke-width:2px; - linkStyle 2 stroke:#007bff,stroke-width:2px; - linkStyle 3 stroke:#28a745,stroke-width:2px; - linkStyle 4 stroke:#667eea,stroke-width:2px; -``` - -1. **Publisher:** Generates synthetic market data. -2. **Encoding:** Encodes data into a compact binary format using **SBE**. - * *Zero-Copy:* Writes directly to an off-heap direct buffer. -3. **Transport (Aeron):** Publishes the binary message to the **Aeron IPC** ring buffer. - * *Kernel Bypass:* Data moves via shared memory, avoiding the OS network stack. -4. **Subscriber (Fragment Handler):** Reads the binary message using SBE "Flyweights" (reusable view objects). - * *Zero-Allocation:* No new Java objects are created during decoding. -5. **Transformation:** Converts the binary data to a compact, flat JSON string (minimal allocation). -6. **WebSocket:** Pushes the lightweight JSON to the browser. +1. **Publisher** generates synthetic market data as POJOs +2. **Allocation** converts data to JSON `String` using `StringBuilder` (high allocation) +3. **Artificial Load** wraps JSON in 1KB padding to stress the Garbage Collector +4. **Transport** via direct method call to `MarketDataBroadcaster` +5. **WebSocket** pushes heavy JSON to browser **Performance Characteristics:** -* **Low Allocation:** Almost no garbage generated in the ingestion hot-path. -* **Pauseless:** Azul C4 collector handles the WebSocket strings concurrently, maintaining a flat latency profile. -* **High Throughput:** Aeron IPC handles millions of messages/sec with sub-microsecond latency. - -## 🚦 Quick Start: The Comparison Matrix +- **High Allocation:** Gigabytes of temporary String objects per second +- **GC Pressure:** Frequent pauses on G1GC; C4 handles it better but still creates work -The `start.sh` script provides commands to run the TradeStreamEE application in various configurations, allowing for a comprehensive comparison of JVM and architectural performance. +### 2\. AERON Mode (The "Optimized" Path, Default) -| Scenario | Command | JVM | Ingestion Architecture | Goal | -|:--------------------------------|:-----------------------------|:--------------------|:-----------------------|:----------------------------------------------------------------------| -| **1. Modern Stack** | `./start.sh azul-aeron` | Azul Prime (C4) | Aeron (Optimized) | Demonstrate peak performance: Pauseless GC + Zero-Copy Transport. | -| **2. Legacy Baseline** | `./start.sh standard-direct` | Standard JDK (G1GC) | Direct (Heavy) | Establish the baseline: High allocation on G1GC. Expect jitter. | -| **3. Fixing Legacy Code** | `./start.sh azul-direct` | Azul Prime (C4) | Direct (Heavy) | Show how C4 can stabilize a high-allocation app without code changes. | -| **4. Optimizing Standard Java** | `./start.sh standard-aeron` | Standard JDK (G1GC) | Aeron (Optimized) | See if architectural optimization helps G1GC performance. | +**Goal:** Low-latency financial pipeline using off-heap memory and zero-copy semantics. +**Use for:** Production-grade performance with minimal GC impact. -### Observability Commands +**How it works:** -* `./start-comparison.sh` - Deploy complete JVM comparison stack (recommended) -* `./stop-comparison.sh` - Stop all comparison services -* `docker-compose -f docker-compose-monitoring.yml up -d` - Start monitoring stack only -* `docker-compose -f docker-compose-c4.yml up -d` - Start C4 cluster only -* `docker-compose -f docker-compose-g1.yml up -d` - Start G1GC cluster only -* `docker-compose -f docker-compose-monitoring.yml ps` - Check monitoring status +1. **Publisher** generates synthetic market data +2. **SBE Encoder** writes binary format to off-heap direct buffer (zero-copy) +3. **Aeron IPC** publishes to shared memory ring buffer (kernel bypass) +4. **Fragment Handler** reads using SBE "Flyweights" (reusable views, no allocation) +5. **Transformation** converts to compact JSON for WebSocket +6. **WebSocket** pushes lightweight JSON to browser -### Utilities +**Performance Characteristics:** -* `./start.sh logs` - View live logs -* `./start.sh stop` - Stop containers -* `./start.sh clean` - Deep clean (remove volumes/images) +- **Low Allocation:** Almost no garbage in the ingestion hot-path +- **High Throughput:** Aeron IPC handles millions of messages/sec with sub-microsecond latency +- **Both JVMs benefit** from reduced allocation, but C4 maintains flat latency profile ## ⚙️ Configuration & Tuning @@ -210,30 +212,31 @@ Controls how data moves from the Publisher to the Processor. ### JVM Tuning & Configuration -The Docker configurations are optimized with enhanced settings for performance testing: +Heap size varies by deployment type: + +| Deployment | Dockerfiles | Heap Size | Reason | +|:--------------------------------------|:------------------------------------------------|:----------|:--------------------------------------------------------| +| **Single Instance** (`start.sh`) | `Dockerfile`, `Dockerfile.standard` | 8GB | Full heap for maximum throughput | +| **Clustered** (`start-comparison.sh`) | `Dockerfile.scale`, `Dockerfile.scale.standard` | 4GB | Balanced for 3-instance deployments (~12GB per cluster) | -**Azul Prime (C4) Configuration:** +**Common JVM options:** ```dockerfile +# Azul Prime (C4) - 8GB single instance example ENV JAVA_OPTS="-Xms8g -Xmx8g -XX:+AlwaysPreTouch -XX:+UseTransparentHugePages -Djava.net.preferIPv4Stack=true" -``` -**Standard JDK (G1GC) Configuration:** - -```dockerfile -ENV JAVA_OPTS="-Xms8g -Xmx8g -XX:+UseG1GC -XX:+AlwaysPreTouch -XX:+UseTransparentHugePages -Djava.net.preferIPv4Stack=true" +# Standard JDK (G1GC) - 4GB cluster example +ENV JAVA_OPTS="-Xms4g -Xmx4g -XX:+UseG1GC -XX:+AlwaysPreTouch -XX:+UseTransparentHugePages -Djava.net.preferIPv4Stack=true" ``` -**Infrastructure Improvements:** +**Infrastructure improvements:** -* **8GB Heap Size**: Increased from 2GB to 8GB to handle extreme memory pressure testing -* **Pre-touch Memory** (`-XX:+AlwaysPreTouch`): Pre-allocates heap pages to eliminate runtime allocation overhead -* **Transparent Huge Pages** (`-XX:+UseTransparentHugePages`): Reduces TLB misses for large memory operations -* **Enhanced GC Logging**: Detailed GC event logging with decorators for comprehensive analysis -* **Rate-Limited Logging**: Prevents log flooding during high-throughput operations -* **Maven Wrapper**: Ensures consistent build environments across platforms +- **Pre-touch Memory** (`-XX:+AlwaysPreTouch`): Pre-allocates heap pages to eliminate runtime allocation overhead +- **Transparent Huge Pages** (`-XX:+UseTransparentHugePages`): Reduces TLB misses for large memory operations +- **GC Logging**: Detailed event logging with decorators for analysis +- **Rate-Limited Logging**: Prevents log flooding during high-throughput operations -**Note:** We purposefully **do not** use `-XX:+UseZGC` in the Azul Prime image, as C4 is the native, optimized collector for Azul Platform Prime. +**Note:** Azul Platform Prime uses C4 by default; we don't specify `-XX:+UseZGC` since C4 is the native, optimized collector for Azul Prime. ### GC Monitoring & Stress Testing @@ -253,6 +256,7 @@ The application includes comprehensive GC monitoring and memory pressure testing **MemoryPressureService** provides controlled memory allocation to stress test GC performance: **Allocation Modes:** + * **OFF**: No additional allocation * **LOW**: 1 MB/sec - Light pressure * **MEDIUM**: 10 MB/sec - Moderate pressure @@ -260,6 +264,7 @@ The application includes comprehensive GC monitoring and memory pressure testing * **EXTREME**: 2 GB/sec - Extreme pressure Each mode allocates byte arrays in a background thread to create realistic memory pressure, allowing observation of: + * C4's concurrent collection vs G1GC's "stop-the-world" pauses * Latency impact under increasing memory pressure * Throughput degradation patterns @@ -267,6 +272,7 @@ Each mode allocates byte arrays in a background thread to create realistic memor #### GC Challenge Mode The web UI includes **GC Challenge Mode** controls that allow: + * Real-time switching between allocation modes * Visual feedback showing current stress level * Side-by-side pause time visualization @@ -276,200 +282,36 @@ This feature enables live demonstration of how Azul C4 maintains low pause times ## 📊 Monitoring & Observability -TradeStreamEE includes comprehensive monitoring infrastructure to compare JVM performance between Azul C4 and standard G1GC configurations. - -### Monitoring Stack +When running `./start-comparison.sh all`, the following monitoring stack is deployed: -| Component | Technology | Purpose | Access | -|:-----------------------|:--------------------------|:--------------------------------|:-------------------------------------------------------| -| **Metrics Collection** | Prometheus + JMX Exporter | JVM GC metrics, memory, threads | http://localhost:9090 | -| **Visualization** | Grafana | Performance dashboards | http://localhost:3000 (admin/admin) | -| **Log Aggregation** | Loki + Promtail | Centralized log management | http://localhost:3100 | -| **Load Balancing** | Traefik | Traffic distribution + metrics | http://localhost:8080 (C4), http://localhost:9080 (G1) | +| Component | Access | Purpose | +|:----------------------|:------------------------------------|:------------------------------| +| **Grafana Dashboard** | http://localhost:3000 (admin/admin) | JVM comparison dashboards | +| **Prometheus** | http://localhost:9090 | Metrics collection | +| **Loki** | http://localhost:3100 | Log aggregation | +| **C4 Application** | http://localhost:8080 (Traefik LB) | Azul C4 cluster (3 instances) | +| **G1 Application** | http://localhost:9080 (Traefik LB) | G1GC cluster (3 instances) | -### JVM Comparison Dashboard +**Direct instance access:** -The pre-configured Grafana dashboard provides: +- C4 instances: http://localhost:8081, http://localhost:8082, http://localhost:8083 +- G1 instances: http://localhost:9081, http://localhost:9082, http://localhost:9083 -* **GC Pause Time Comparison** - P99 latency comparison between C4 and G1GC -* **GC Collection Count Rate** - Collection frequency analysis -* **Heap Memory Usage** - Real-time memory utilization -* **Thread Count** - Concurrent thread monitoring -* **GC Pause Distribution** - Heatmap showing pause time patterns -* **Performance Summary** - Key metrics with threshold alerts +### Stress Testing -### Starting the Observability Stack - -#### Option 1: Automated Setup (Recommended) - -Use the provided `start-comparison.sh` script for complete automated deployment: +Use the UI or API to apply memory pressure and observe GC behavior differences: ```bash -# Deploy entire JVM comparison stack -./start-comparison.sh -``` - -This script automatically: -* Creates the monitoring directory structure -* Downloads the JMX Prometheus exporter -* Builds the application and Docker images -* Creates required Docker networks -* Starts the complete monitoring stack (Prometheus, Grafana, Loki) -* Deploys both C4 and G1GC clusters with load balancers - -#### Option 2: Manual Setup - -For granular control, start components manually: - -```bash -# Create required networks -docker network create trader-network -docker network create monitoring - -# Start monitoring infrastructure -docker-compose -f docker-compose-monitoring.yml up -d - -# Start C4 cluster (Azul Prime) -docker-compose -f docker-compose-c4.yml up -d - -# Start G1 cluster (Eclipse Temurin) -docker-compose -f docker-compose-g1.yml up -d - -# View monitoring stack status -docker-compose -f docker-compose-monitoring.yml ps -``` - -#### Stopping the Comparison - -Use the provided stop script: - -```bash -# Stop all comparison services -./stop-comparison.sh - -# Stop all comparison services and remove volumes -./stop-comparison.sh --prune -``` - -### Access Points - -After starting the observability stack: - -* **Grafana Dashboard**: http://localhost:3000 (admin/admin) -* **C4 Application**: http://localhost:8080 (via Traefik load balancer) -* **G1 Application**: http://localhost:9080 (via Traefik load balancer) -* **Prometheus**: http://localhost:9090 -* **Individual C4 instances**: http://localhost:8081, http://localhost:8082, http://localhost:8083 -* **Individual G1 instances**: http://localhost:9081, http://localhost:9082, http://localhost:9083 - -### Monitoring Configuration - -#### JMX Exporter - -Each JVM instance runs a JMX exporter agent that exposes: -* Garbage collection metrics (pause times, collection counts) -* Memory pool usage (heap/non-heap) -* Thread information -* Custom application metrics - -#### Prometheus Configuration - -The Prometheus setup (`monitoring/prometheus/prometheus.yml`) scrapes: -* JMX metrics from all JVM instances (ports 9010-9022) -* Traefik metrics for load balancer performance -* Self-monitoring metrics - -#### Log Collection - -Promtail automatically collects and ships container logs to Loki, enabling: -* Log-based troubleshooting -* Correlation of performance issues with application events -* JVM type and instance label-based filtering - -### Stress Testing the Comparison - -After deploying the observability stack, you can stress test both JVM configurations to observe the performance differences: - -#### Memory Pressure API Endpoints - -```bash -# Set allocation mode for memory pressure testing +# Apply extreme memory pressure via API curl -X POST http://localhost:8080/api/pressure/mode/EXTREME # C4 cluster curl -X POST http://localhost:9080/api/pressure/mode/EXTREME # G1GC cluster -# Available modes: OFF, LOW, MEDIUM, HIGH, EXTREME - # Get current GC statistics curl http://localhost:8080/api/gc/stats curl http://localhost:9080/api/gc/stats -# Reset GC statistics -curl -X POST http://localhost:8080/api/gc/reset -curl -X POST http://localhost:9080/api/gc/reset -``` - -#### UI-Based Testing - -The web interface provides interactive controls: - -* **GC Challenge Mode Panel**: Select allocation modes with visual buttons -* **Real-time Pause Time Chart**: Shows GC pauses as they occur -* **Backend Message Rate Display**: Monitor throughput impact -* **Visual Feedback**: Immediate color-coded response to mode changes - -#### Expected Results - -The stress tests will: -1. Generate controlled allocation rates (1 MB to 2 GB per second) -2. Create realistic memory pressure scenarios -3. Allow real-time comparison of pause times between C4 and G1GC -4. Demonstrate C4's concurrent collection vs G1GC's "stop-the-world" pauses -5. Show latency impact and throughput degradation patterns -6. Visualize the "pauseless" characteristics of C4 under extreme load - -**Sample GC Stats Response:** - -```json -{ - "gcName": "C4", - "collectionCount": 1543, - "collectionTime": 8934, - "lastPauseDuration": 0.5, - "percentiles": { - "p50": 0.3, - "p95": 1.2, - "p99": 2.8, - "p999": 5.6, - "max": 12.4 - }, - "totalMemory": 8589934592, - "usedMemory": 3221225472, - "freeMemory": 5368709120 -} -``` - -## 📊 Application Metrics - -The application exposes a lightweight REST endpoint for health checks and internal metrics. - -**Check Status:** - -```bash -./start.sh status -``` - -**Sample Output:** - -```json -{ - "application": "TradeStreamEE", - "subscriber": "Channel: aeron:ipc, Stream: 1001, Running: true", - "publisher": { "messagesPublished": 1543021 }, - "runtime": { - "gcType": "GPGC", - "freeMemory": "1450 MB" - } -} +# Available modes: OFF, LOW, MEDIUM, HIGH, EXTREME +# Allocation rates: 0 MB/s, 1 MB/s, 10 MB/s, 500 MB/s, 2 GB/s ``` ## 📂 Project Structure @@ -526,12 +368,14 @@ Dockerfile.scale.standard # Build for G1GC instances ### Current Test Coverage **✅ Working Tests (160/160 passing):** + - **Monitoring & GC**: Full coverage of SLA monitoring logic and GC notification handling. - **REST Resources**: Comprehensive tests for Memory Pressure and GC Stats endpoints. - **Core Logic**: Validated `AllocationMode` and concurrency configurations. - **WebSockets**: Verified `MarketDataBroadcaster` functionality and session management. **📊 Coverage Metrics:** + - **Tests**: 160 unit tests with 100% pass rate - **Monitoring Coverage**: >90% (SLAMonitor, GCStatsService) - **REST API Coverage**: >85% (Resources and DTOs) diff --git a/src/main/java/fish/payara/resource/HelloWorldResource.java b/src/main/java/fish/payara/resource/HelloWorldResource.java index 42aa048..83991fa 100644 --- a/src/main/java/fish/payara/resource/HelloWorldResource.java +++ b/src/main/java/fish/payara/resource/HelloWorldResource.java @@ -32,8 +32,8 @@ public class HelloWorldResource { }) @Counted(name = "helloEndpointCount", description = "Count of calls to the hello endpoint") @Timed(name = "helloEndpointTime", description = "Time taken to execute the hello endpoint") - @Timeout(3000) // Timeout after 3 seconds - @Retry(maxRetries = 3) // Retry the request up to 3 times on failure + @Timeout(3000) + @Retry(maxRetries = 3) @Fallback(fallbackMethod = "fallbackMethod") public Response hello( @QueryParam("name") @@ -50,7 +50,6 @@ public Response hello( } public Response fallbackMethod(@QueryParam("name") String name) { - // Fallback logic when the hello method fails or exceeds retries return Response.ok("Fallback data").build(); } } diff --git a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java index 4b743d1..d0ea6a6 100644 --- a/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java +++ b/src/main/java/fish/payara/trader/aeron/MarketDataFragmentHandler.java @@ -21,28 +21,23 @@ public class MarketDataFragmentHandler implements FragmentHandler { private static final Logger LOGGER = Logger.getLogger(MarketDataFragmentHandler.class.getName()); - // SBE Message Header Decoder (reusable flyweight) private final MessageHeaderDecoder headerDecoder = new MessageHeaderDecoder(); - // SBE Message Decoders (reusable flyweights) private final TradeDecoder tradeDecoder = new TradeDecoder(); private final QuoteDecoder quoteDecoder = new QuoteDecoder(); private final MarketDepthDecoder marketDepthDecoder = new MarketDepthDecoder(); private final OrderAckDecoder orderAckDecoder = new OrderAckDecoder(); private final HeartbeatDecoder heartbeatDecoder = new HeartbeatDecoder(); - // Statistics private long messagesProcessed = 0; private long messagesBroadcast = 0; private long lastLogTime = System.currentTimeMillis(); - // Sampling: Only broadcast 1 in N messages to avoid overwhelming browser private static final int SAMPLE_RATE = 50; private long sampleCounter = 0; @Inject MarketDataBroadcaster broadcaster; - // Optimization: Reusable buffers to reduce allocation private final byte[] symbolBuffer = new byte[128]; private final StringBuilder sb = new StringBuilder(1024); @@ -105,14 +100,12 @@ private void processTrade( tradeDecoder.wrap(buffer, offset, blockLength, version); - // Extract fields from SBE decoder final long timestamp = tradeDecoder.timestamp(); final long tradeId = tradeDecoder.tradeId(); final long price = tradeDecoder.price(); final long quantity = tradeDecoder.quantity(); final Side side = tradeDecoder.side(); - // Extract variable-length symbol string using reusable buffer final int symbolLength = tradeDecoder.symbolLength(); tradeDecoder.getSymbol(symbolBuffer, 0, symbolLength); final String symbol = new String(symbolBuffer, 0, symbolLength); @@ -222,7 +215,6 @@ private void processMarketDepth( } sb.append("]"); - // Must extract symbol after traversing groups in SBE for correct position in buffer final int symbolLength = marketDepthDecoder.symbolLength(); marketDepthDecoder.getSymbol(symbolBuffer, 0, symbolLength); final String symbol = new String(symbolBuffer, 0, symbolLength); @@ -232,7 +224,6 @@ private void processMarketDepth( broadcaster.broadcast(sb.toString()); } - /** Process OrderAck message */ private void processOrderAck( DirectBuffer buffer, int offset, int blockLength, int version, boolean shouldBroadcast) { if (!shouldBroadcast) { @@ -288,23 +279,20 @@ private void processOrderAck( broadcaster.broadcast(sb.toString()); } - /** Process Heartbeat message */ private void processHeartbeat(DirectBuffer buffer, int offset, int blockLength, int version) { heartbeatDecoder.wrap(buffer, offset, blockLength, version); final long timestamp = heartbeatDecoder.timestamp(); final long sequenceNumber = heartbeatDecoder.sequenceNumber(); - // Log heartbeats but don't broadcast them if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Heartbeat: timestamp=" + timestamp + ", seq=" + sequenceNumber); } } - /** Log statistics periodically */ private void logStatistics() { long now = System.currentTimeMillis(); - if (now - lastLogTime > 5000) { // Log every 5 seconds + if (now - lastLogTime > 5000) { double elapsedSeconds = (now - lastLogTime) / 1000.0; LOGGER.info( String.format( diff --git a/src/main/java/fish/payara/trader/gc/GCStatsService.java b/src/main/java/fish/payara/trader/gc/GCStatsService.java index 5239af2..187ddcb 100644 --- a/src/main/java/fish/payara/trader/gc/GCStatsService.java +++ b/src/main/java/fish/payara/trader/gc/GCStatsService.java @@ -72,7 +72,6 @@ public void handleNotification(Notification notification, Object handback) { history.removeFirst(); } - // Log significant pauses (> 10ms) if (duration > 10) { LOGGER.info( String.format( @@ -102,7 +101,6 @@ public List collectGCStats() { stats.setCollectionCount(gcBean.getCollectionCount()); stats.setCollectionTime(gcBean.getCollectionTime()); - // Get recent pauses from accurate history ConcurrentLinkedDeque history = pauseHistory.get(gcName); if (history != null && !history.isEmpty()) { List pauses = new ArrayList<>(history); @@ -110,7 +108,6 @@ public List collectGCStats() { stats.setRecentPauses(pauses.subList(Math.max(0, pauses.size() - 100), pauses.size())); - // Calculate percentiles List sortedPauses = pauses.stream().sorted().collect(Collectors.toList()); stats.setPercentiles(calculatePercentiles(sortedPauses)); @@ -120,7 +117,6 @@ public List collectGCStats() { stats.setPercentiles(new GCStats.PausePercentiles(0, 0, 0, 0, 0)); } - // Memory stats stats.setTotalMemory(heapUsage.getMax()); stats.setUsedMemory(heapUsage.getUsed()); stats.setFreeMemory(heapUsage.getMax() - heapUsage.getUsed()); diff --git a/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java b/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java index 3774922..c45adcb 100644 --- a/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java +++ b/src/main/java/fish/payara/trader/monitoring/GCPauseMonitor.java @@ -19,27 +19,19 @@ import javax.management.NotificationListener; import javax.management.openmbean.CompositeData; -/** - * Real-time GC pause monitoring using JMX notifications. Captures individual GC events with exact - * pause times (not averaged). - */ @ApplicationScoped public class GCPauseMonitor implements NotificationListener { private static final Logger LOGGER = Logger.getLogger(GCPauseMonitor.class.getName()); - // Keep last N pauses for percentile calculations (reactive window) private static final int MAX_PAUSE_HISTORY = 500; - // Pause history (milliseconds) private final ConcurrentLinkedDeque pauseHistory = new ConcurrentLinkedDeque<>(); - // All-time statistics private final AtomicLong totalPauseCount = new AtomicLong(0); private final AtomicLong totalPauseTimeMs = new AtomicLong(0); private volatile long maxPauseMs = 0; - // SLA violation counters (all-time) private final AtomicLong violationsOver10ms = new AtomicLong(0); private final AtomicLong violationsOver50ms = new AtomicLong(0); private final AtomicLong violationsOver100ms = new AtomicLong(0); @@ -91,9 +83,6 @@ public void handleNotification(Notification notification, Object handback) { GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd); String gcName = info.getGcName(); - // FILTERING LOGIC: - // Azul C4 exposes "GPGC" (Concurrent Cycle) and "GPGC Pauses" (STW Pauses). - // We MUST ignore "GPGC" because it reports cycle time (hundreds of ms) which is NOT a pause. if ("GPGC".equals(gcName) || (gcName.contains("Cycles") && !gcName.contains("Pauses"))) { return; } @@ -101,7 +90,6 @@ public void handleNotification(Notification notification, Object handback) { GcInfo gcInfo = info.getGcInfo(); long pauseMs = gcInfo.getDuration(); - // Record pause recordPause(pauseMs, gcName, info.getGcAction()); } catch (Exception e) { @@ -110,17 +98,14 @@ public void handleNotification(Notification notification, Object handback) { } private void recordPause(long pauseMs, String gcName, String gcAction) { - // Add to history pauseHistory.addLast(pauseMs); if (pauseHistory.size() > MAX_PAUSE_HISTORY) { pauseHistory.removeFirst(); } - // Update statistics totalPauseCount.incrementAndGet(); totalPauseTimeMs.addAndGet(pauseMs); - // Update max (thread-safe but may miss true max in race condition - acceptable for monitoring) if (pauseMs > maxPauseMs) { synchronized (this) { if (pauseMs > maxPauseMs) { @@ -129,7 +114,6 @@ private void recordPause(long pauseMs, String gcName, String gcAction) { } } - // Track SLA violations if (pauseMs > 100) { violationsOver100ms.incrementAndGet(); violationsOver50ms.incrementAndGet(); @@ -141,7 +125,6 @@ private void recordPause(long pauseMs, String gcName, String gcAction) { violationsOver10ms.incrementAndGet(); } - // Log significant pauses if (pauseMs > 100) { LOGGER.warning( String.format("Large GC pause detected: %d ms [%s - %s]", pauseMs, gcName, gcAction)); @@ -210,7 +193,7 @@ public static class GCPauseStats { public final long p95Ms; public final long p99Ms; public final long p999Ms; - public final long maxMs; // All-time max since startup/reset + public final long maxMs; public final long violationsOver10ms; public final long violationsOver50ms; public final long violationsOver100ms; diff --git a/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java b/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java index 5e48f73..09518d2 100644 --- a/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java +++ b/src/main/java/fish/payara/trader/monitoring/SLAMonitorService.java @@ -10,21 +10,17 @@ public class SLAMonitorService { private static final Logger LOGGER = Logger.getLogger(SLAMonitorService.class.getName()); - // SLA thresholds private static final long SLA_10MS = 10; private static final long SLA_50MS = 50; private static final long SLA_100MS = 100; - // Violation counters private final AtomicLong violationsOver10ms = new AtomicLong(0); private final AtomicLong violationsOver50ms = new AtomicLong(0); private final AtomicLong violationsOver100ms = new AtomicLong(0); private final AtomicLong totalOperations = new AtomicLong(0); - // Rolling window (last 5 minutes) private final ConcurrentHashMap violationsByMinute = new ConcurrentHashMap<>(); - /** Record an operation latency and check for SLA violations */ public void recordOperation(long latencyMs) { totalOperations.incrementAndGet(); @@ -47,12 +43,10 @@ private void recordViolation() { long currentMinute = System.currentTimeMillis() / 60000; violationsByMinute.merge(currentMinute, 1L, Long::sum); - // Clean up old entries (> 5 minutes) long fiveMinutesAgo = currentMinute - 5; violationsByMinute.keySet().removeIf(minute -> minute < fiveMinutesAgo); } - /** Get SLA compliance statistics */ public SLAStats getStats() { long total = totalOperations.get(); diff --git a/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java index 29008a9..9946760 100644 --- a/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java +++ b/src/main/java/fish/payara/trader/pressure/MemoryPressureService.java @@ -30,9 +30,8 @@ public class MemoryPressureService { private long totalBytesAllocated = 0; private long lastStatsTime = System.currentTimeMillis(); - // Long-lived objects that survive to tenured/old generation private final List tenuredObjects = new CopyOnWriteArrayList<>(); - private static final int TENURED_TARGET_MB = 1024; // Target 1GB in old gen + private static final int TENURED_TARGET_MB = 1024; private final AtomicLong tenuredBytesAllocated = new AtomicLong(0); @Inject @VirtualThreadExecutor private ManagedExecutorService executorService; @@ -78,13 +77,10 @@ private synchronized void startPressure() { break; } - // Generate garbage for this iteration generateGarbage(mode); - // Sleep 100ms between iterations (10 iterations/sec) Thread.sleep(100); - // Log stats every 5 seconds logStats(); } catch (InterruptedException e) { @@ -112,7 +108,6 @@ private void generateGarbage(AllocationMode mode) { int bytesPerAlloc = mode.getBytesPerAllocation(); for (int i = 0; i < allocations; i++) { - // Mix different allocation patterns int pattern = i % 4; switch (pattern) { @@ -130,14 +125,11 @@ private void generateGarbage(AllocationMode mode) { break; } - // NEW: Create long-lived objects inside the loop for higher impact if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { - // 0.1% chance in EXTREME (20 objects/iteration = 200MB/s) - // 0.02% chance in HIGH (1 object/iteration = 10MB/s) int chance = (mode == AllocationMode.EXTREME) ? 10 : 2; if (ThreadLocalRandom.current().nextInt(10000) < chance) { - byte[] longLived = new byte[1024 * 1024]; // 1MB object - ThreadLocalRandom.current().nextBytes(longLived); // Prevent optimization + byte[] longLived = new byte[1024 * 1024]; + ThreadLocalRandom.current().nextBytes(longLived); tenuredObjects.add(longLived); tenuredBytesAllocated.addAndGet(1024 * 1024); } @@ -146,7 +138,6 @@ private void generateGarbage(AllocationMode mode) { totalBytesAllocated += bytesPerAlloc; } - // Maintain target size - remove oldest when limit reached if (mode == AllocationMode.HIGH || mode == AllocationMode.EXTREME) { while (tenuredBytesAllocated.get() > TENURED_TARGET_MB * 1024L * 1024L) { if (!tenuredObjects.isEmpty()) { @@ -157,7 +148,6 @@ private void generateGarbage(AllocationMode mode) { } } } else if (mode == AllocationMode.OFF || mode == AllocationMode.LOW) { - // Clear tenured objects when stress is reduced if (!tenuredObjects.isEmpty()) { tenuredObjects.clear(); tenuredBytesAllocated.set(0); @@ -166,20 +156,16 @@ private void generateGarbage(AllocationMode mode) { } private void generateStringGarbage(int bytes) { - // Create strings via concatenation (generates intermediate garbage) StringBuilder sb = new StringBuilder(bytes); for (int i = 0; i < bytes / 10; i++) { sb.append("GARBAGE"); } String garbage = sb.toString(); - // String is now eligible for GC } private void generateByteArrayGarbage(int bytes) { byte[] garbage = new byte[bytes]; - // Fill with random data to prevent compiler optimization ThreadLocalRandom.current().nextBytes(garbage); - // Array is now eligible for GC } private void generateObjectGarbage(int count) { @@ -187,7 +173,6 @@ private void generateObjectGarbage(int count) { for (int i = 0; i < count; i++) { garbage.add(new DummyObject(i, "data-" + i, System.nanoTime())); } - // List and objects are now eligible for GC } private void generateCollectionGarbage(int count) { @@ -195,7 +180,6 @@ private void generateCollectionGarbage(int count) { for (int i = 0; i < count; i++) { garbage.add(ThreadLocalRandom.current().nextInt()); } - // List is now eligible for GC } private void logStats() { @@ -236,7 +220,6 @@ public boolean isRunning() { return running; } - /** Dummy object for allocation testing */ private static class DummyObject { private final int id; private final String data; diff --git a/src/main/java/fish/payara/trader/rest/ApplicationConfig.java b/src/main/java/fish/payara/trader/rest/ApplicationConfig.java index 1dd3f0f..22bacc1 100644 --- a/src/main/java/fish/payara/trader/rest/ApplicationConfig.java +++ b/src/main/java/fish/payara/trader/rest/ApplicationConfig.java @@ -3,8 +3,5 @@ import jakarta.ws.rs.ApplicationPath; import jakarta.ws.rs.core.Application; -/** JAX-RS application configuration */ @ApplicationPath("/api") -public class ApplicationConfig extends Application { - // All REST resources will be available at /api/* -} +public class ApplicationConfig extends Application {} diff --git a/src/main/java/fish/payara/trader/rest/GCStatsResource.java b/src/main/java/fish/payara/trader/rest/GCStatsResource.java index 15a5ce1..bf4eb25 100644 --- a/src/main/java/fish/payara/trader/rest/GCStatsResource.java +++ b/src/main/java/fish/payara/trader/rest/GCStatsResource.java @@ -73,15 +73,14 @@ public Response getComparison() { } comparison.put("instanceName", instanceName); - // Identify which JVM is running String jvmVendor = System.getProperty("java.vm.vendor"); String jvmName = System.getProperty("java.vm.name"); List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); String gcName = gcBeans.stream().map(GarbageCollectorMXBean::getName).collect(Collectors.joining(", ")); - // More accurate check: C4 collector's MXBean is named "GPGC" - boolean isAzulC4 = gcBeans.stream().anyMatch(bean -> "GPGC".equals(bean.getName())); + boolean isAzulC4 = + jvmVendor != null && (jvmVendor.contains("Azul") || jvmName.contains("Zing")); comparison.put("jvmVendor", jvmVendor); comparison.put("jvmName", jvmName); @@ -89,18 +88,15 @@ public Response getComparison() { comparison.put("isAzulC4", isAzulC4); comparison.put("heapSizeMB", Runtime.getRuntime().maxMemory() / (1024 * 1024)); - // Current stress level comparison.put("allocationMode", memoryPressureService.getCurrentMode()); comparison.put( "allocationRateMBps", memoryPressureService.getCurrentMode().getBytesPerSecond() / (1024 * 1024)); comparison.put("messageRate", publisher.getMessagesPublished()); - // GC Performance Metrics (keep old stats for backward compatibility) List stats = gcStatsService.collectGCStats(); comparison.put("gcStats", stats); - // Critical comparison metrics - USE ACCURATE GC PAUSE MONITOR fish.payara.trader.monitoring.GCPauseMonitor.GCPauseStats pauseStats = gcPauseMonitor.getStats(); comparison.put("pauseP50Ms", pauseStats.p50Ms); @@ -112,11 +108,10 @@ public Response getComparison() { comparison.put("totalPauseCount", pauseStats.totalPauseCount); comparison.put("totalPauseTimeMs", pauseStats.totalPauseTimeMs); - // SLA violation tracking (accurate counts since startup/reset) comparison.put("slaViolations10ms", pauseStats.violationsOver10ms); comparison.put("slaViolations50ms", pauseStats.violationsOver50ms); comparison.put("slaViolations100ms", pauseStats.violationsOver100ms); - comparison.put("pauseSampleSize", pauseStats.sampleSize); // For transparency + comparison.put("pauseSampleSize", pauseStats.sampleSize); return Response.ok(comparison).build(); } @@ -125,7 +120,6 @@ public Response getComparison() { @Path("/stats") @Produces(MediaType.APPLICATION_JSON) public Response getGCStats() { - LOGGER.fine("GET /api/gc/stats - Collecting GC statistics"); List stats = gcStatsService.collectGCStats(); LOGGER.info(String.format("GET /api/gc/stats - Returned %d GC collector stats", stats.size())); return Response.ok(stats).build(); diff --git a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java index 5165d5c..5922478 100644 --- a/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java +++ b/src/main/java/fish/payara/trader/rest/MemoryPressureResource.java @@ -13,7 +13,6 @@ import java.util.logging.Logger; import java.util.stream.Collectors; -/** REST endpoint for controlling memory pressure testing */ @Path("/pressure") public class MemoryPressureResource { @@ -49,7 +48,6 @@ public String getDescription() { @Produces(MediaType.APPLICATION_JSON) public Response getStatus() { AllocationMode currentMode = pressureService.getCurrentMode(); - LOGGER.fine(String.format("GET /api/pressure/status - Current mode: %s", currentMode.name())); Map status = new HashMap<>(); status.put("currentMode", currentMode.name()); diff --git a/src/main/java/fish/payara/trader/rest/StatusResource.java b/src/main/java/fish/payara/trader/rest/StatusResource.java index c91f006..46b895c 100644 --- a/src/main/java/fish/payara/trader/rest/StatusResource.java +++ b/src/main/java/fish/payara/trader/rest/StatusResource.java @@ -31,7 +31,6 @@ public class StatusResource { public Response getStatus() { Map status = new HashMap<>(); - // Get instance name from environment variable String instanceName = System.getenv("PAYARA_INSTANCE_NAME"); if (instanceName == null) { instanceName = "standalone"; diff --git a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java index 61c997a..5dd4370 100644 --- a/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java +++ b/src/main/java/fish/payara/trader/websocket/MarketDataBroadcaster.java @@ -50,7 +50,6 @@ public void init() { clusterTopic = hazelcastInstance.getTopic(TOPIC_NAME); clusterTopic.addMessageListener( message -> { - // Broadcast to local WebSocket sessions only broadcastLocal(message.getMessageObject()); }); LOGGER.info("Subscribed to Hazelcast topic: " + TOPIC_NAME + " (clustered mode)"); @@ -69,7 +68,6 @@ public void addSession(Session session) { LOGGER.info("WebSocket session added. Total sessions: " + sessions.size()); } - /** Unregister a WebSocket session */ public void removeSession(Session session) { sessions.remove(session); LOGGER.info("WebSocket session removed. Total sessions: " + sessions.size()); @@ -88,7 +86,6 @@ public void removeSession(Session session) { */ public void broadcast(String jsonMessage) { if (clusterTopic != null) { - // Publish to cluster-wide topic try { clusterTopic.publish(jsonMessage); } catch (Exception e) { @@ -99,7 +96,6 @@ public void broadcast(String jsonMessage) { broadcastLocal(jsonMessage); } } else { - // Standalone mode - broadcast locally only broadcastLocal(jsonMessage); } } @@ -145,7 +141,6 @@ private void broadcastLocal(String jsonMessage) { * a heavier protocol or inefficient data packaging. */ public void broadcastWithArtificialLoad(String jsonMessage) { - // Generate 1KB of padding to increase payload size and allocation String padding = "X".repeat(1024); // Wrap the original message in a new JSON structure diff --git a/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java b/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java index 0ed3f73..c1978e6 100644 --- a/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java +++ b/src/main/java/fish/payara/trader/websocket/MarketDataWebSocket.java @@ -29,7 +29,6 @@ public void onOpen(Session session) { LOGGER.info("WebSocket connection opened: " + session.getId()); broadcaster.addSession(session); - // Send welcome message with current mode try { String welcomeJson = String.format( @@ -59,10 +58,8 @@ public void onError(Session session, Throwable throwable) { @OnMessage public void onMessage(String message, Session session) { - // Handle client messages if needed (e.g., subscription requests) LOGGER.fine("Received message from client: " + message); - // Echo back for now try { session.getBasicRemote().sendText("{\"type\":\"ack\",\"message\":\"Message received\"}"); } catch (Exception e) { diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 3e33d64..d9567ee 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -9,7 +9,6 @@