Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes updates to traffic shaping options with shared servers #5282

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/io/vertx/core/net/TrafficShapingOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,28 @@ public long getCheckIntervalForStats() {
public TimeUnit getCheckIntervalForStatsTimeUnit() {
return checkIntervalForStatsTimeUnit;
}

@Override
public boolean equals(Object obj) {
TrafficShapingOptions that = (TrafficShapingOptions) obj;
return inboundGlobalBandwidth == that.inboundGlobalBandwidth &&
outboundGlobalBandwidth == that.outboundGlobalBandwidth &&
peakOutboundGlobalBandwidth == that.peakOutboundGlobalBandwidth &&
maxDelayToWait == that.maxDelayToWait &&
maxDelayToWaitTimeUnit == that.maxDelayToWaitTimeUnit &&
checkIntervalForStats == that.checkIntervalForStats &&
checkIntervalForStatsTimeUnit == that.checkIntervalForStatsTimeUnit;
}

@Override
public int hashCode() {
return Objects.hash(inboundGlobalBandwidth,
outboundGlobalBandwidth,
peakOutboundGlobalBandwidth,
maxDelayToWait,
maxDelayToWaitTimeUnit,
checkIntervalForStats,
checkIntervalForStatsTimeUnit);
}

}
37 changes: 24 additions & 13 deletions src/main/java/io/vertx/core/net/impl/TCPServerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,32 @@ public void updateTrafficShapingOptions(TrafficShapingOptions options) {
if (options == null) {
throw new IllegalArgumentException("Invalid null value passed for traffic shaping options update");
}
if (trafficShapingHandler == null) {
throw new IllegalStateException("Unable to update traffic shaping options because the server was not configured " +
"to use traffic shaping during startup");
}
TCPServerBase server = actualServer;
// Update the traffic shaping options only for the actual/main server
if (server != null && server != this) {
server.updateTrafficShapingOptions(options);
} else {
long checkIntervalForStatsInMillis = options.getCheckIntervalForStatsTimeUnit().toMillis(options.getCheckIntervalForStats());
trafficShapingHandler.configure(options.getOutboundGlobalBandwidth(), options.getInboundGlobalBandwidth(), checkIntervalForStatsInMillis);
if (trafficShapingHandler == null) {
throw new IllegalStateException("Unable to update traffic shaping options because the server was not configured " +
"to use traffic shaping during startup");
}

if (options.getPeakOutboundGlobalBandwidth() != 0) {
trafficShapingHandler.setMaxGlobalWriteSize(options.getPeakOutboundGlobalBandwidth());
// Compare with existing traffic-shaping options to ensure they are updated only when they differ.
if(!options.equals(server.options.getTrafficShapingOptions())) {
server.options.setTrafficShapingOptions(options);
long checkIntervalForStatsInMillis = options.getCheckIntervalForStatsTimeUnit().toMillis(options.getCheckIntervalForStats());
trafficShapingHandler.configure(options.getOutboundGlobalBandwidth(), options.getInboundGlobalBandwidth(), checkIntervalForStatsInMillis);

if (options.getPeakOutboundGlobalBandwidth() != 0) {
trafficShapingHandler.setMaxGlobalWriteSize(options.getPeakOutboundGlobalBandwidth());
}
if (options.getMaxDelayToWait() != 0) {
long maxDelayToWaitInMillis = options.getMaxDelayToWaitTimeUnit().toMillis(options.getMaxDelayToWait());
trafficShapingHandler.setMaxWriteDelay(maxDelayToWaitInMillis);
}
}
if (options.getMaxDelayToWait() != 0) {
long maxDelayToWaitInMillis = options.getMaxDelayToWaitTimeUnit().toMillis(options.getMaxDelayToWait());
trafficShapingHandler.setMaxWriteDelay(maxDelayToWaitInMillis);
else {
log.info("Not updating traffic shaping options as they have not changed");
}
}
}
Expand Down Expand Up @@ -292,8 +301,10 @@ private synchronized Future<Channel> listen(SocketAddress localAddress, ContextI
} else {
// Server already exists with that host/port - we will use that
actualServer = main;
metrics = main.metrics;
childHandler = childHandler(listenContext, localAddress, main.trafficShapingHandler);
metrics = actualServer.metrics;
// Ensure the workers inherit the actual server's traffic-shaping handler
trafficShapingHandler = actualServer.trafficShapingHandler;
childHandler = childHandler(listenContext, localAddress, actualServer.trafficShapingHandler);
worker = ch -> childHandler.accept(ch, actualServer.sslChannelProvider.result().sslChannelProvider());
actualServer.servers.add(this);
actualServer.channelBalancer.addWorker(eventLoop, worker);
Expand Down
51 changes: 50 additions & 1 deletion src/test/java/io/vertx/core/http/HttpBandwidthLimitingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -204,6 +205,54 @@ public void start(Promise<Void> startPromise) {
Assert.assertTrue(elapsedMillis > expectedTimeMillis(totalReceivedLength.get(), OUTBOUND_LIMIT)); // because there are simultaneous 2 requests
}

@Test
public void testDynamicOutboundRateUpdateSharedServers() throws IOException, InterruptedException
{
int numEventLoops = 5; // We start a shared TCP server with 2 event-loops
List<HttpServer> servers = new ArrayList<>();
Future<String> listenLatch = vertx.deployVerticle(() -> new AbstractVerticle() {
@Override
public void start(Promise<Void> startPromise) {
HttpServer testServer = serverFactory.apply(vertx);
servers.add(testServer);
testServer.requestHandler(HANDLERS.getFile(sampleF))
.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST).<Void>mapEmpty().onComplete(startPromise);
}
}, new DeploymentOptions().setInstances(numEventLoops));

HttpClient testClient = clientFactory.apply(vertx);
CountDownLatch waitForResponse = new CountDownLatch(2);
AtomicLong startTime = new AtomicLong();
AtomicLong totalReceivedLength = new AtomicLong();
long expectedLength = Files.size(Paths.get(sampleF.getAbsolutePath()));
listenLatch.onComplete(onSuccess(v -> {
startTime.set(System.nanoTime());
for (int i = 0; i < 2; i++) {
testClient.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/get-file")
.compose(req -> req.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::body))
.onComplete(onSuccess(body -> {
long receivedBytes = body.getBytes().length;
totalReceivedLength.addAndGet(receivedBytes);
Assert.assertEquals(expectedLength, receivedBytes);
waitForResponse.countDown();
}));
}
}));
awaitLatch(waitForResponse);
TrafficShapingOptions updatedTrafficOptions = new TrafficShapingOptions()
.setInboundGlobalBandwidth(INBOUND_LIMIT) // unchanged
.setOutboundGlobalBandwidth(2 * OUTBOUND_LIMIT);

for (int i = 0; i < numEventLoops; i++) {
servers.forEach(s -> s.updateTrafficShapingOptions(updatedTrafficOptions));
}

long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime.get());
Assert.assertTrue(elapsedMillis > expectedTimeMillis(totalReceivedLength.get(), OUTBOUND_LIMIT));
}

@Test
public void testDynamicOutboundRateUpdate() throws Exception {
Buffer expectedBuffer = TestUtils.randomBuffer(TEST_CONTENT_SIZE);
Expand Down