Fixed checkStyle and ErrorProne warns

This commit is contained in:
andsel
2018-04-26 22:23:06 +02:00
parent 1a6dd8a259
commit 033a428184
81 changed files with 680 additions and 424 deletions
@@ -21,19 +21,25 @@ import org.eclipse.paho.client.mqttv3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.CountDownLatch;
import static java.nio.charset.StandardCharsets.UTF_8;
class BenchmarkPublisher {
private static final Logger LOG = LoggerFactory.getLogger(BenchmarkPublisher.class);
static class PublishCallback implements IMqttActionListener {
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
LOG.error("PUB: Publish failed: " + exception);
LOG.error("PUB: Publish failed", exception);
exception.printStackTrace();
System.exit(2);
}
@Override
public void onSuccess(IMqttToken asyncActionToken) {
}
}
@@ -44,12 +50,13 @@ class BenchmarkPublisher {
private final long numToSend;
private final CountDownLatch stopLatch;
ExitTopicCallback(long startedTime, long numToSend, IMqttAsyncClient connection, CountDownLatch stopLatch) {
ExitTopicCallback(long startedTime, long numToSend, CountDownLatch stopLatch) {
this.startedTime = startedTime;
this.numToSend = numToSend;
this.stopLatch = stopLatch;
}
@Override
public void onSuccess(IMqttToken asyncActionToken) {
long stopTime = System.currentTimeMillis();
long spentTime = stopTime - startedTime;
@@ -65,6 +72,7 @@ class BenchmarkPublisher {
this.stopLatch.countDown();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable th) {
LOG.error("PUB: Publish failed: " + th);
System.exit(2);
@@ -90,11 +98,14 @@ class BenchmarkPublisher {
MqttConnectOptions connectOptions = new MqttConnectOptions();
connectOptions.setCleanSession(true);
this.client.connect(connectOptions, null, new IMqttActionListener() {
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
LOG.error("PUB: connect fail", exception);
System.exit(2);
}
@Override
public void onSuccess(IMqttToken asyncActionToken) {
LOG.info("PUB: Successfully connected to server");
}
@@ -103,7 +114,7 @@ class BenchmarkPublisher {
this.m_latch = new CountDownLatch(1);
}
public void firePublishes() throws MqttException {
public void firePublishes() throws MqttException, UnsupportedEncodingException {
long pauseMicroseconds = (int) ((1.0 / messagesPerSecond) * 1000 * 1000);
LOG.info("PUB: Pause over the each message sent {} microsecs", pauseMicroseconds);
@@ -116,14 +127,14 @@ class BenchmarkPublisher {
IMqttActionListener pubCallback = new PublishCallback();
for (int i = 0; i < numToSend; i++) {
long nanos = System.nanoTime();
byte[] message = ("Hello world!!-" + nanos).getBytes();
byte[] message = ("Hello world!!-" + nanos).getBytes(UTF_8);
this.client.publish("/topic" + dialog_id, message, qos, retain, null, pubCallback);
timer.sleep(pauseMicroseconds);
}
IMqttActionListener exitCallback = new ExitTopicCallback(startTime, numToSend, this.client, m_latch);
IMqttActionListener exitCallback = new ExitTopicCallback(startTime, numToSend, m_latch);
long nanosExit = System.nanoTime();
byte[] exitMessage = ("Hello world!!-" + nanosExit).getBytes();
byte[] exitMessage = ("Hello world!!-" + nanosExit).getBytes(UTF_8);
this.client.publish("/exit" + dialog_id, exitMessage, qos, retain, null, exitCallback);
}
@@ -23,12 +23,15 @@ import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import static java.nio.charset.StandardCharsets.UTF_8;
class BenchmarkSubscriber {
private static final Logger LOG = LoggerFactory.getLogger(BenchmarkSubscriber.class);
class SubscriberCallback implements MqttCallback {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
//used to catch the first message published
if (!alreadyStarted) {
@@ -54,7 +57,7 @@ class BenchmarkSubscriber {
histogram.outputPercentileDistribution(System.out, 1000.0); //nanos/1000
m_latch.countDown();
} else {
String payload = new String(message.getPayload());
String payload = new String(message.getPayload(), UTF_8);
//long sentTime = Long.parseLong(payload.split("-")[1]);
long sentTime = Long.parseLong(payload.split("-")[1]);
long delay = System.nanoTime() - sentTime;
@@ -67,6 +70,7 @@ class BenchmarkSubscriber {
}
}
@Override
public void connectionLost(Throwable cause) {
LOG.error("Client Subscriber lost connection, caused by ", cause);
try {
@@ -78,6 +82,7 @@ class BenchmarkSubscriber {
m_latch.countDown();
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
}
@@ -102,14 +107,18 @@ class BenchmarkSubscriber {
this.client.setCallback(new SubscriberCallback());
this.client.connect(connectOptions, null, new IMqttActionListener() {
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
LOG.error("SUB: connect fail", exception);
System.exit(2);
}
@Override
public void onSuccess(IMqttToken asyncActionToken) {
try {
client.subscribe("/topic" + dialog_id, 1, null, new IMqttActionListener() {
@Override
public void onFailure(IMqttToken asyncActionToken2, Throwable exception) {
try {
client.disconnect();
@@ -118,6 +127,7 @@ class BenchmarkSubscriber {
}
}
@Override
public void onSuccess(IMqttToken asyncActionToken2) {
LOG.info("SUB: subscribed to /topic{} qos: 1", dialog_id);
}
@@ -128,6 +138,8 @@ class BenchmarkSubscriber {
try {
client.subscribe("/exit" + dialog_id, 1, null, new IMqttActionListener() {
@Override
public void onFailure(IMqttToken asyncActionToken2, Throwable exception) {
try {
client.disconnect();
@@ -137,6 +149,7 @@ class BenchmarkSubscriber {
m_latch.countDown();
}
@Override
public void onSuccess(IMqttToken asyncActionToken2) {
LOG.info("SUB: subscribed to /exit{} qos: 1", dialog_id);
}
@@ -27,6 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static io.moquette.parser.netty.performance.NettyPublishReceiverHandler.payload2Str;
import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE;
import static io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader.from;
import static io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE;
import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE;
@@ -65,7 +66,7 @@ class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
return;
case DISCONNECT:
LOG.info("Received a message of type {} from <{}>", messageType, clientID);
ctx.close();
ctx.close().addListener(CLOSE_ON_FAILURE);
// case PUBACK:
// NettyChannel channel;
// synchronized (m_channelMapper) {
@@ -85,7 +86,7 @@ class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
false,
0);
MqttMessage pingResp = new MqttMessage(pingHeader);
ctx.writeAndFlush(pingResp);
ctx.writeAndFlush(pingResp).addListener(CLOSE_ON_FAILURE);
break;
default:
LOG.info("Received a message of type {} from <{}>", messageType, clientID);
@@ -114,7 +115,7 @@ class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader(msg.variableHeader().topicName(), 0);
MqttPublishMessage pubMessage = new MqttPublishMessage(fixedHeader, varHeader, msg.payload());
m_state.getSubscriberCh().writeAndFlush(pubMessage);
m_state.getSubscriberCh().writeAndFlush(pubMessage).addListener(CLOSE_ON_FAILURE);
/*Channel subscriberCh = m_state.getSubscriberCh();
if (subscriberCh.isWritable()) {
subscriberCh.write(pubMessage);
@@ -135,7 +136,7 @@ class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
fixedHeader,
from(msg.variableHeader().messageId()),
payload);
ctx.writeAndFlush(ackMessage);
ctx.writeAndFlush(ackMessage).addListener(CLOSE_ON_FAILURE);
LOG.debug("subscribed client to {}", msg.payload().topicSubscriptions());
}
@@ -156,8 +157,8 @@ class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(
MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED, false);
MqttConnAckMessage koResp = new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
ctx.writeAndFlush(koResp);
ctx.close();
ctx.writeAndFlush(koResp).addListener(CLOSE_ON_FAILURE);
ctx.close().addListener(CLOSE_ON_FAILURE);
return;
}
@@ -166,7 +167,7 @@ class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(
MqttConnectReturnCode.CONNECTION_ACCEPTED, false);
MqttConnAckMessage okResp = new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
ctx.writeAndFlush(okResp);
ctx.writeAndFlush(okResp).addListener(CLOSE_ON_FAILURE);
}
@Override
@@ -175,7 +176,7 @@ class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
// NettyChannel channel = m_channelMapper.get(ctx);
// String clientID = (String) channel.getAttribute(NettyChannel.ATTR_KEY_CLIENTID);
// m_messaging.lostConnection(channel, clientID);
ctx.channel().close(/*false*/);
ctx.channel().close().addListener(CLOSE_ON_FAILURE);
System.out.println("Processing time histogram (microsecs)");
this.processingTime.outputPercentileDistribution(System.out, 1000.0);
@@ -27,6 +27,9 @@ import org.HdrHistogram.Histogram;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE;
import static java.nio.charset.StandardCharsets.UTF_8;
@ChannelHandler.Sharable
class NettyPublishReceiverHandler extends ChannelInboundHandlerAdapter {
@@ -76,13 +79,13 @@ class NettyPublishReceiverHandler extends ChannelInboundHandlerAdapter {
rawBytes = new byte[size];
content.getBytes(content.readerIndex(), rawBytes);
}
return new String(rawBytes);
return new String(rawBytes, UTF_8);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
public void channelInactive(ChannelHandlerContext ctx) {
LOG.info("Received channel inactive");
ctx.channel().close();
ctx.channel().close().addListener(CLOSE_ON_FAILURE);
System.out.println("Network time histogram (microsecs)");
this.forthNetworkTime.outputPercentileDistribution(System.out, 1000.0);
@@ -32,6 +32,10 @@ import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE;
/**
* Emulates a broker, but doesn't apply any protocol logic, just forward the qos0 publishes
* to the lone subscriber, it's used just to measure the protocol decoding/encoding overhead.
@@ -39,7 +43,7 @@ import org.slf4j.LoggerFactory;
*/
public class ProtocolDecodingServer {
class MoquetteIdleTimeoutHandler extends ChannelDuplexHandler {
static class MoquetteIdleTimeoutHandler extends ChannelDuplexHandler {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
@@ -47,7 +51,7 @@ public class ProtocolDecodingServer {
if (e == IdleState.READER_IDLE) {
//fire a channelInactive to trigger publish of Will
ctx.fireChannelInactive();
ctx.close();
ctx.close().addListener(CLOSE_ON_FAILURE);
} /*else if (e.getState() == IdleState.WRITER_IDLE) {
ctx.writeAndFlush(new PingMessage());
}*/
@@ -125,12 +129,13 @@ public class ProtocolDecodingServer {
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(host, port);
LOG.info("Server binded host: {}, port: {}", host, port);
f.sync();
f.sync().addListener(CLOSE_ON_FAILURE);
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
}
@SuppressWarnings("FutureReturnValueIgnored")
public void stop() {
if (m_workerGroup == null) {
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
@@ -145,7 +150,7 @@ public class ProtocolDecodingServer {
System.out.println("Server stopped");
}
public static void main(String[] args) throws MqttException, InterruptedException {
public static void main(String[] args) throws MqttException, InterruptedException, UnsupportedEncodingException {
final ProtocolDecodingServer server = new ProtocolDecodingServer();
server.init();
@@ -155,7 +160,7 @@ public class ProtocolDecodingServer {
Runtime.getRuntime().addShutdownHook(new Thread(server::stop));
}
private static void startClientsTesting() throws MqttException, InterruptedException {
private static void startClientsTesting() throws MqttException, InterruptedException, UnsupportedEncodingException {
String host = "localhost";
int numToSend = 10;
int messagesPerSecond = 10000;
@@ -27,6 +27,10 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE;
/**
* Emulates a broker, but doesn't apply any protocol logic, just forward the qos0 publishes
* to the lone subscriber, it's used just to measure the protocol decoding/encoding overhead.
@@ -73,12 +77,13 @@ public class ProtocolPublishDecodingServer {
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(host, port);
LOG.info("Server binded host: {}, port: {}", host, port);
f.sync();
f.sync().addListener(CLOSE_ON_FAILURE);
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
}
@SuppressWarnings("FutureReturnValueIgnored")
public void stop() {
if (m_workerGroup == null) {
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
@@ -93,7 +98,7 @@ public class ProtocolPublishDecodingServer {
System.out.println("Server stopped");
}
public static void main(String[] args) throws MqttException, InterruptedException {
public static void main(String[] args) throws UnsupportedEncodingException {
final ProtocolPublishDecodingServer server = new ProtocolPublishDecodingServer();
server.init();
@@ -107,7 +112,7 @@ public class ProtocolPublishDecodingServer {
Runtime.getRuntime().addShutdownHook(new Thread(server::stop));
}
private static void publishBombing() {
private static void publishBombing() throws UnsupportedEncodingException {
LOG.info("Started publish loop");
PublishBomber heavyPublisher = new PublishBomber("localhost", 1883);
heavyPublisher.publishLoop(25000, 100000);
@@ -24,10 +24,15 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.mqtt.*;
import org.apache.commons.codec.CharEncoding;
import org.eclipse.jetty.toolchain.perf.PlatformTimer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE;
class PublishBomber {
private static final Logger LOG = LoggerFactory.getLogger(PublishBomber.class);
@@ -35,6 +40,7 @@ class PublishBomber {
private final EventLoopGroup workerGroup;
private Channel channel;
@SuppressWarnings("FutureReturnValueIgnored")
PublishBomber(String host, int port) {
workerGroup = new NioEventLoopGroup();
try {
@@ -61,13 +67,13 @@ class PublishBomber {
private void sendMessage(MqttMessage msg) {
try {
channel.writeAndFlush(msg).await();
channel.writeAndFlush(msg).await().addListener(CLOSE_ON_FAILURE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void publishLoop(int messagesPerSecond, int numToSend) {
public void publishLoop(int messagesPerSecond, int numToSend) throws UnsupportedEncodingException {
long pauseMicroseconds = (int) ((1.0 / messagesPerSecond) * 1000 * 1000);
LOG.warn("PUB: Pause over the each message sent {} microsecs", pauseMicroseconds);
@@ -78,7 +84,7 @@ class PublishBomber {
PlatformTimer timer = PlatformTimer.detect();
for (int i = 0; i < numToSend; i++) {
long nanos = System.nanoTime();
byte[] rawContent = ("Hello world!!-" + nanos).getBytes();
byte[] rawContent = ("Hello world!!-" + nanos).getBytes(CharEncoding.UTF_8);
ByteBuf payload = Unpooled.copiedBuffer(rawContent);
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_MOST_ONCE,
@@ -94,7 +100,7 @@ class PublishBomber {
public void disconnect() {
try {
this.channel.disconnect().await();
this.channel.disconnect().await().addListener(CLOSE_ON_FAILURE);
} catch (InterruptedException e) {
e.printStackTrace();
}
@@ -27,12 +27,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static io.moquette.parser.netty.performance.NettyPublishReceiverHandler.payload2Str;
import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE;
@ChannelHandler.Sharable
class PublishReceiverHandler extends ChannelInboundHandlerAdapter {
private static final Logger LOG = LoggerFactory.getLogger(PublishReceiverHandler.class);
Histogram forthNetworkTime = new Histogram(5);
private Histogram forthNetworkTime = new Histogram(5);
PublishReceiverHandler() {
}
@@ -69,9 +70,9 @@ class PublishReceiverHandler extends ChannelInboundHandlerAdapter {
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
public void channelInactive(ChannelHandlerContext ctx) {
LOG.info("Received channel inactive");
ctx.channel().close();
ctx.channel().close().addListener(CLOSE_ON_FAILURE);
System.out.println("Network time histogram (microsecs)");
this.forthNetworkTime.outputPercentileDistribution(System.out, 1000.0);