Skip to content

Commit

Permalink
[improve][websocket][branch-2.11] Add ping support (#19255)
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
  • Loading branch information
nodece authored Jan 18, 2023
1 parent ed1a8c4 commit 6c611d5
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,12 @@ public class ServiceConfiguration implements PulsarConfiguration {
)
private int webSocketSessionIdleTimeoutMillis = 300000;

@FieldContext(
category = CATEGORY_WEBSOCKET,
doc = "Interval of time to sending the ping to keep alive in WebSocket proxy. "
+ "This value greater than 0 means enabled")
private int webSocketPingDurationSeconds = -1;

@FieldContext(
category = CATEGORY_WEBSOCKET,
doc = "The maximum size of a text message during parsing in WebSocket proxy."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* 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.
*/
package org.apache.pulsar.websocket.proxy;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.pulsar.broker.BrokerTestUtil.spyWithClassAndConstructorArgs;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import java.net.URI;
import java.util.Optional;
import java.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.ProducerConsumerBase;
import org.apache.pulsar.metadata.impl.ZKMetadataStore;
import org.apache.pulsar.websocket.WebSocketService;
import org.apache.pulsar.websocket.service.ProxyServer;
import org.apache.pulsar.websocket.service.WebSocketProxyConfiguration;
import org.apache.pulsar.websocket.service.WebSocketServiceStarter;
import org.awaitility.Awaitility;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@Test(groups = "websocket")
@Slf4j
public class ProxyIdleTimeoutTest extends ProducerConsumerBase {
protected String methodName;
private ProxyServer proxyServer;
private WebSocketService service;

private static final int TIME_TO_CHECK_BACKLOG_QUOTA = 5;

@BeforeMethod
public void setup() throws Exception {
conf.setBacklogQuotaCheckIntervalInSeconds(TIME_TO_CHECK_BACKLOG_QUOTA);

super.internalSetup();
super.producerBaseSetup();

WebSocketProxyConfiguration config = new WebSocketProxyConfiguration();
config.setWebServicePort(Optional.of(0));
config.setClusterName("test");
config.setConfigurationStoreServers(GLOBAL_DUMMY_VALUE);
config.setWebSocketSessionIdleTimeoutMillis(3 * 1000);
service = spyWithClassAndConstructorArgs(WebSocketService.class, config);
doReturn(new ZKMetadataStore(mockZooKeeperGlobal)).when(service).createMetadataStore(anyString(), anyInt());
proxyServer = new ProxyServer(config);
WebSocketServiceStarter.start(proxyServer, service);
log.info("Proxy Server Started");
}

@AfterMethod(alwaysRun = true)
protected void cleanup() throws Exception {
super.internalCleanup();
if (service != null) {
service.close();
}
if (proxyServer != null) {
proxyServer.stop();
}
log.info("Finished Cleaning Up Test setup");
}

@Test
public void testIdleTimeout() throws Exception {
String producerUri = "ws://localhost:" + proxyServer.getListenPortHTTP().get() +
"/ws/v2/producer/persistent/my-property/my-ns/my-topic1/";

URI produceUri = URI.create(producerUri);
WebSocketClient produceClient = new WebSocketClient();
SimpleProducerSocket produceSocket = new SimpleProducerSocket();

try {
produceClient.start();
ClientUpgradeRequest produceRequest = new ClientUpgradeRequest();
Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest);
assertThat(producerFuture).succeedsWithin(2, SECONDS);
Session session = producerFuture.get();
Awaitility.await().during(5, SECONDS).untilAsserted(() -> assertThat(session.isOpen()).isFalse());
} finally {
produceClient.stop();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* 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.
*/
package org.apache.pulsar.websocket.proxy;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.pulsar.broker.BrokerTestUtil.spyWithClassAndConstructorArgs;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import java.net.URI;
import java.util.Optional;
import java.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.ProducerConsumerBase;
import org.apache.pulsar.metadata.impl.ZKMetadataStore;
import org.apache.pulsar.websocket.WebSocketService;
import org.apache.pulsar.websocket.service.ProxyServer;
import org.apache.pulsar.websocket.service.WebSocketProxyConfiguration;
import org.apache.pulsar.websocket.service.WebSocketServiceStarter;
import org.awaitility.Awaitility;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@Test(groups = "websocket")
@Slf4j
public class ProxyPingTest extends ProducerConsumerBase {
protected String methodName;

private ProxyServer proxyServer;
private WebSocketService service;

private static final int TIME_TO_CHECK_BACKLOG_QUOTA = 5;

@BeforeMethod
public void setup() throws Exception {
conf.setBacklogQuotaCheckIntervalInSeconds(TIME_TO_CHECK_BACKLOG_QUOTA);

super.internalSetup();
super.producerBaseSetup();

WebSocketProxyConfiguration config = new WebSocketProxyConfiguration();
config.setWebServicePort(Optional.of(0));
config.setClusterName("test");
config.setConfigurationStoreServers(GLOBAL_DUMMY_VALUE);
config.setWebSocketSessionIdleTimeoutMillis(3 * 1000);
config.setWebSocketPingDurationSeconds(2);
service = spyWithClassAndConstructorArgs(WebSocketService.class, config);
doReturn(new ZKMetadataStore(mockZooKeeperGlobal)).when(service).createMetadataStore(anyString(), anyInt());
proxyServer = new ProxyServer(config);
WebSocketServiceStarter.start(proxyServer, service);
log.info("Proxy Server Started");
}

@AfterMethod(alwaysRun = true)
protected void cleanup() throws Exception {
super.internalCleanup();
if (service != null) {
service.close();
}
if (proxyServer != null) {
proxyServer.stop();
}
log.info("Finished Cleaning Up Test setup");
}

@Test
public void testPing() throws Exception {
String producerUri = "ws://localhost:" + proxyServer.getListenPortHTTP().get() +
"/ws/v2/producer/persistent/my-property/my-ns/my-topic1/";

URI produceUri = URI.create(producerUri);
WebSocketClient produceClient = new WebSocketClient();
SimpleProducerSocket produceSocket = new SimpleProducerSocket();

try {
produceClient.start();
ClientUpgradeRequest produceRequest = new ClientUpgradeRequest();
Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest);
assertThat(producerFuture).succeedsWithin(2, SECONDS);
Session session = producerFuture.get();
Awaitility.await().during(5, SECONDS).untilAsserted(() -> assertThat(session.isOpen()).isTrue());
} finally {
produceClient.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@ public class ProxyConfiguration implements PulsarConfiguration {
)
private boolean webSocketServiceEnabled = false;

@FieldContext(
category = CATEGORY_WEBSOCKET,
doc = "Interval of time to sending the ping to keep alive in WebSocket proxy. "
+ "This value greater than 0 means enabled")
private int webSocketPingDurationSeconds = -1;

@FieldContext(
category = CATEGORY_WEBSOCKET,
doc = "Name of the cluster to which this broker belongs to"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
package org.apache.pulsar.websocket;

import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -62,6 +67,8 @@ public abstract class AbstractWebSocketHandler extends WebSocketAdapter implemen
protected final Map<String, String> queryParams;
private static final String PULSAR_AUTH_METHOD_NAME = "X-Pulsar-Auth-Method-Name";

private ScheduledFuture<?> pingFuture;

public AbstractWebSocketHandler(WebSocketService service,
HttpServletRequest request,
ServletUpgradeResponse response) {
Expand Down Expand Up @@ -169,9 +176,25 @@ protected static String getErrorMessage(Exception e) {
}
}

private void closePingFuture() {
if (pingFuture != null && !pingFuture.isDone()) {
pingFuture.cancel(true);
}
}

@Override
public void onWebSocketConnect(Session session) {
super.onWebSocketConnect(session);
int webSocketPingDurationSeconds = service.getConfig().getWebSocketPingDurationSeconds();
if (webSocketPingDurationSeconds > 0) {
pingFuture = service.getExecutor().scheduleAtFixedRate(() -> {
try {
session.getRemote().sendPing(ByteBuffer.wrap("PING".getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
log.warn("[{}] WebSocket send ping", getSession().getRemoteAddress(), e);
}
}, webSocketPingDurationSeconds, webSocketPingDurationSeconds, TimeUnit.SECONDS);
}
log.info("[{}] New WebSocket session on topic {}", session.getRemoteAddress(), topic);
}

Expand All @@ -180,6 +203,7 @@ public void onWebSocketError(Throwable cause) {
super.onWebSocketError(cause);
log.info("[{}] WebSocket error on topic {} : {}", getSession().getRemoteAddress(), topic, cause.getMessage());
try {
closePingFuture();
close();
} catch (IOException e) {
log.error("Failed in closing WebSocket session for topic {} with error: {}", topic, e.getMessage());
Expand All @@ -191,6 +215,7 @@ public void onWebSocketClose(int statusCode, String reason) {
log.info("[{}] Closed WebSocket session on topic {}. status: {} - reason: {}", getSession().getRemoteAddress(),
topic, statusCode, reason);
try {
closePingFuture();
close();
} catch (IOException e) {
log.warn("[{}] Failed to close handler for topic {}. ", getSession().getRemoteAddress(), topic, e);
Expand Down Expand Up @@ -259,6 +284,11 @@ private TopicName extractTopicName(HttpServletRequest request) {
return TopicName.get(domain, namespace, name);
}

@VisibleForTesting
public ScheduledFuture<?> getPingFuture() {
return pingFuture;
}

protected abstract Boolean isAuthorized(String authRole,
AuthenticationDataSource authenticationData) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public class WebSocketProxyConfiguration implements PulsarConfiguration {
@FieldContext(doc = "Timeout of idling WebSocket session (in milliseconds)")
private int webSocketSessionIdleTimeoutMillis = 300000;

@FieldContext(doc = "Interval of time to sending the ping to keep alive. This value greater than 0 means enabled")
private int webSocketPingDurationSeconds = -1;

@FieldContext(doc = "When this parameter is not empty, unauthenticated users perform as anonymousUserRole")
private String anonymousUserRole = null;

Expand Down
Loading

0 comments on commit 6c611d5

Please sign in to comment.