From be429f0910385b1c3b753549027d101377fa1afd Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 6 Aug 2020 20:38:02 +1000 Subject: [PATCH 1/4] Issue #5122 - add specialized Connection.Listener for WebSocket ConnectionStatistics Signed-off-by: Lachlan Roberts --- jetty-websocket/jetty-websocket-tests/pom.xml | 6 ++ .../websocket/tests/WebSocketStatsTest.java | 58 ++++++++----------- .../util/WebSocketConnectionStatistics.java | 40 +++++++++++++ 3 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java diff --git a/jetty-websocket/jetty-websocket-tests/pom.xml b/jetty-websocket/jetty-websocket-tests/pom.xml index c27e463eb4bd..d9fbabf13a61 100644 --- a/jetty-websocket/jetty-websocket-tests/pom.xml +++ b/jetty-websocket/jetty-websocket-tests/pom.xml @@ -66,5 +66,11 @@ jetty-test-helper test + + org.eclipse.jetty + jetty-jmx + ${project.version} + test + diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java index f096535dcbe4..01a4c5f8dc52 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.websocket.tests; +import java.lang.management.ManagementFactory; import java.net.URI; import java.nio.ByteBuffer; import java.util.concurrent.CountDownLatch; @@ -28,7 +29,7 @@ import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.ConnectionStatistics; import org.eclipse.jetty.io.MappedByteBufferPool; -import org.eclipse.jetty.server.HttpConnection; +import org.eclipse.jetty.jmx.MBeanContainer; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -41,8 +42,9 @@ import org.eclipse.jetty.websocket.common.WebSocketFrame; import org.eclipse.jetty.websocket.common.frames.TextFrame; import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection; -import org.eclipse.jetty.websocket.servlet.WebSocketServlet; -import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; +import org.eclipse.jetty.websocket.common.util.WebSocketConnectionStatistics; +import org.eclipse.jetty.websocket.server.NativeWebSocketServletContainerInitializer; +import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -53,36 +55,23 @@ public class WebSocketStatsTest { - public static class MyWebSocketServlet extends WebSocketServlet - { - @Override - public void configure(WebSocketServletFactory factory) - { - factory.setCreator((req, resp) -> new EchoSocket()); - } - } - + private final CountDownLatch wsConnectionClosed = new CountDownLatch(1); private Server server; private ServerConnector connector; private WebSocketClient client; private ConnectionStatistics statistics; - private CountDownLatch wsUpgradeComplete = new CountDownLatch(1); - private CountDownLatch wsConnectionClosed = new CountDownLatch(1); @BeforeEach public void start() throws Exception { - statistics = new ConnectionStatistics() + statistics = new WebSocketConnectionStatistics() { @Override public void onClosed(Connection connection) { super.onClosed(connection); - if (connection instanceof AbstractWebSocketConnection) wsConnectionClosed.countDown(); - else if (connection instanceof HttpConnection) - wsUpgradeComplete.countDown(); } }; @@ -93,11 +82,17 @@ else if (connection instanceof HttpConnection) ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); contextHandler.setContextPath("/"); - contextHandler.addServlet(MyWebSocketServlet.class, "/testPath"); + NativeWebSocketServletContainerInitializer.configure(contextHandler, (context, container) -> + container.addMapping("/", EchoSocket.class)); + WebSocketUpgradeFilter.configure(contextHandler); server.setHandler(contextHandler); client = new WebSocketClient(); + // Setup JMX. + MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); + server.addBean(mbeanContainer); + server.start(); client.start(); } @@ -105,8 +100,8 @@ else if (connection instanceof HttpConnection) @AfterEach public void stop() throws Exception { - client.stop(); server.stop(); + client.stop(); } long getFrameByteSize(WebSocketFrame frame) @@ -122,22 +117,15 @@ long getFrameByteSize(WebSocketFrame frame) @Test public void echoStatsTest() throws Exception { - URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/testPath"); + URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/"); EventSocket socket = new EventSocket(); Future connect = client.connect(socket, uri); - final long numMessages = 10000; + final long numMessages = 1; final String msgText = "hello world"; - - long upgradeSentBytes; - long upgradeReceivedBytes; - try (Session session = connect.get(5, TimeUnit.SECONDS)) { - wsUpgradeComplete.await(5, TimeUnit.SECONDS); - upgradeSentBytes = statistics.getSentBytes(); - upgradeReceivedBytes = statistics.getReceivedBytes(); - + assertThat(statistics.getConnections(), is(1L)); for (int i = 0; i < numMessages; i++) { session.getRemote().sendString(msgText); @@ -150,18 +138,18 @@ public void echoStatsTest() throws Exception assertThat(statistics.getConnectionsMax(), is(1L)); assertThat(statistics.getConnections(), is(0L)); - assertThat(statistics.getSentMessages(), is(numMessages + 2L)); - assertThat(statistics.getReceivedMessages(), is(numMessages + 2L)); + // Sent and received all of the echo messages + 1 for the close frame. + assertThat(statistics.getSentMessages(), is(numMessages + 1L)); + assertThat(statistics.getReceivedMessages(), is(numMessages + 1L)); WebSocketFrame textFrame = new TextFrame().setPayload(msgText); WebSocketFrame closeFrame = new CloseInfo(socket.closeCode, socket.closeReason).asFrame(); - final long textFrameSize = getFrameByteSize(textFrame); final long closeFrameSize = getFrameByteSize(closeFrame); final int maskSize = 4; // We use 4 byte mask for client frames - final long expectedSent = upgradeSentBytes + numMessages * textFrameSize + closeFrameSize; - final long expectedReceived = upgradeReceivedBytes + numMessages * (textFrameSize + maskSize) + closeFrameSize + maskSize; + final long expectedSent = numMessages * textFrameSize + closeFrameSize; + final long expectedReceived = numMessages * (textFrameSize + maskSize) + (closeFrameSize + maskSize); assertThat("stats.sendBytes", statistics.getSentBytes(), is(expectedSent)); assertThat("stats.receivedBytes", statistics.getReceivedBytes(), is(expectedReceived)); diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java new file mode 100644 index 000000000000..c42edba546ee --- /dev/null +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java @@ -0,0 +1,40 @@ +// +// ======================================================================== +// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.common.util; + +import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.ConnectionStatistics; +import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection; + +public class WebSocketConnectionStatistics extends ConnectionStatistics +{ + @Override + public void onOpened(Connection connection) + { + if (connection instanceof AbstractWebSocketConnection) + super.onOpened(connection); + } + + @Override + public void onClosed(Connection connection) + { + if (connection instanceof AbstractWebSocketConnection) + super.onClosed(connection); + } +} From 8a3ff775d5bc285e066e5638623970496f32022d Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 6 Aug 2020 20:45:06 +1000 Subject: [PATCH 2/4] Issue #5122 - make number of active WS Sessions a managed attribute on SessionTracker Signed-off-by: Lachlan Roberts --- .../websocket/jsr356/JsrSessionTracker.java | 17 ++++++++++++++++- .../jetty/websocket/common/SessionTracker.java | 7 +++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java index 6af4830c59b4..303e77c8c902 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JsrSessionTracker.java @@ -18,16 +18,19 @@ package org.eclipse.jetty.websocket.jsr356; +import java.io.IOException; import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import javax.websocket.Session; +import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.component.AbstractLifeCycle; +import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; -public class JsrSessionTracker extends AbstractLifeCycle implements JsrSessionListener +public class JsrSessionTracker extends AbstractLifeCycle implements JsrSessionListener, Dumpable { private final Set sessions = Collections.newSetFromMap(new ConcurrentHashMap<>()); @@ -57,4 +60,16 @@ protected void doStop() throws Exception } super.doStop(); } + + @ManagedAttribute("Total number of active WebSocket Sessions") + public int getNumSessions() + { + return sessions.size(); + } + + @Override + public void dump(Appendable out, String indent) throws IOException + { + Dumpable.dumpObjects(out, indent, this, sessions); + } } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java index 635f1a9d2ce1..c75f1b103d65 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java @@ -24,6 +24,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.component.AbstractLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; @@ -61,6 +62,12 @@ protected void doStop() throws Exception super.doStop(); } + @ManagedAttribute("Total number of active WebSocket Sessions") + public int getNumSessions() + { + return sessions.size(); + } + @Override public void dump(Appendable out, String indent) throws IOException { From 1663a6d7af9d85390fef6deb6d15cd7bce30263e Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 14 Aug 2020 15:19:06 +1000 Subject: [PATCH 3/4] Issue #5125 - Generalise WebSocketConnectionStatistics into IncludeExcludeConnectionStatistics Signed-off-by: Lachlan Roberts --- .../IncludeExcludeConnectionStatistics.java | 114 ++++++++++++++++++ .../websocket/tests/WebSocketStatsTest.java | 12 +- .../util/WebSocketConnectionStatistics.java | 40 ------ 3 files changed, 121 insertions(+), 45 deletions(-) create mode 100644 jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java b/jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java new file mode 100644 index 000000000000..69c51178cb78 --- /dev/null +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java @@ -0,0 +1,114 @@ +// +// ======================================================================== +// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.io; + +import java.util.AbstractSet; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.function.Predicate; + +import org.eclipse.jetty.util.IncludeExcludeSet; + +public class IncludeExcludeConnectionStatistics extends ConnectionStatistics +{ + private final IncludeExcludeSet, Connection> _set = new IncludeExcludeSet<>(ConnectionSet.class); + + public void include(String className) throws ClassNotFoundException + { + _set.include(connectionForName(className)); + } + + public void include(Class clazz) + { + _set.include(clazz); + } + + public void exclude(String className) throws ClassNotFoundException + { + _set.exclude(connectionForName(className)); + } + + public void exclude(Class clazz) + { + _set.exclude(clazz); + } + + private Class connectionForName(String className) throws ClassNotFoundException + { + Class aClass = Class.forName(className); + if (!Connection.class.isAssignableFrom(aClass)) + throw new IllegalArgumentException("Class is not a Connection"); + + @SuppressWarnings("unchecked") + Class connectionClass = (Class)aClass; + return connectionClass; + } + + @Override + public void onOpened(Connection connection) + { + if (Boolean.TRUE.equals(_set.isIncludedAndNotExcluded(connection))) + super.onOpened(connection); + } + + @Override + public void onClosed(Connection connection) + { + if (Boolean.TRUE.equals(_set.isIncludedAndNotExcluded(connection))) + super.onClosed(connection); + } + + public static class ConnectionSet extends AbstractSet> implements Predicate + { + private final Set> set = new HashSet<>(); + + @Override + public boolean add(Class aClass) + { + return set.add(aClass); + } + + @Override + public boolean remove(Object o) + { + return set.remove(o); + } + + @Override + public Iterator> iterator() + { + return set.iterator(); + } + + @Override + public int size() + { + return set.size(); + } + + @Override + public boolean test(Connection connection) + { + if (connection == null) + return false; + return set.stream().anyMatch(c -> c.isAssignableFrom(connection.getClass())); + } + } +} diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java index 01a4c5f8dc52..e8c96b36f328 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java @@ -27,7 +27,7 @@ import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.Connection; -import org.eclipse.jetty.io.ConnectionStatistics; +import org.eclipse.jetty.io.IncludeExcludeConnectionStatistics; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.jmx.MBeanContainer; import org.eclipse.jetty.server.Server; @@ -42,7 +42,6 @@ import org.eclipse.jetty.websocket.common.WebSocketFrame; import org.eclipse.jetty.websocket.common.frames.TextFrame; import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection; -import org.eclipse.jetty.websocket.common.util.WebSocketConnectionStatistics; import org.eclipse.jetty.websocket.server.NativeWebSocketServletContainerInitializer; import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; import org.junit.jupiter.api.AfterEach; @@ -59,17 +58,19 @@ public class WebSocketStatsTest private Server server; private ServerConnector connector; private WebSocketClient client; - private ConnectionStatistics statistics; + private IncludeExcludeConnectionStatistics statistics; @BeforeEach public void start() throws Exception { - statistics = new WebSocketConnectionStatistics() + statistics = new IncludeExcludeConnectionStatistics(); + statistics.include(AbstractWebSocketConnection.class); + + Connection.Listener.Adapter wsCloseListener = new Connection.Listener.Adapter() { @Override public void onClosed(Connection connection) { - super.onClosed(connection); if (connection instanceof AbstractWebSocketConnection) wsConnectionClosed.countDown(); } @@ -78,6 +79,7 @@ public void onClosed(Connection connection) server = new Server(); connector = new ServerConnector(server); connector.addBean(statistics); + connector.addBean(wsCloseListener); server.addConnector(connector); ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java deleted file mode 100644 index c42edba546ee..000000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/util/WebSocketConnectionStatistics.java +++ /dev/null @@ -1,40 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.websocket.common.util; - -import org.eclipse.jetty.io.Connection; -import org.eclipse.jetty.io.ConnectionStatistics; -import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection; - -public class WebSocketConnectionStatistics extends ConnectionStatistics -{ - @Override - public void onOpened(Connection connection) - { - if (connection instanceof AbstractWebSocketConnection) - super.onOpened(connection); - } - - @Override - public void onClosed(Connection connection) - { - if (connection instanceof AbstractWebSocketConnection) - super.onClosed(connection); - } -} From c37d902ef7fe9b0095ff775b1885abec3e4a4999 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 18 Aug 2020 10:18:44 +1000 Subject: [PATCH 4/4] remove tri-state logic from IncludeExcludeConnectionStatistics Signed-off-by: Lachlan Roberts --- .../eclipse/jetty/io/IncludeExcludeConnectionStatistics.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java b/jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java index 69c51178cb78..364a48726cfd 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/IncludeExcludeConnectionStatistics.java @@ -64,14 +64,14 @@ private Class connectionForName(String className) throws C @Override public void onOpened(Connection connection) { - if (Boolean.TRUE.equals(_set.isIncludedAndNotExcluded(connection))) + if (_set.test(connection)) super.onOpened(connection); } @Override public void onClosed(Connection connection) { - if (Boolean.TRUE.equals(_set.isIncludedAndNotExcluded(connection))) + if (_set.test(connection)) super.onClosed(connection); }