MINOR: Switch anonymous classes to lambda expressions in tools module

Switch to lambda when ever possible instead of old anonymous way
in tools module

Author: Srinivas Reddy <srinivas96alluri@gmail.com>
Author: Srinivas Reddy <mrsrinivas@users.noreply.github.com>

Reviewers: Ryanne Dolan <ryannedolan@gmail.com>, Ismael Juma <ismael@juma.me.uk>, Manikumar Reddy <manikumar.reddy@gmail.com>

Closes #6013 from mrsrinivas/tools-switch-to-java8
This commit is contained in:
Srinivas Reddy 2018-12-21 14:20:57 +05:30 committed by Manikumar Reddy
parent ee370d3893
commit 85906d3d2b
19 changed files with 198 additions and 286 deletions

View File

@ -261,23 +261,47 @@ public class ClientCompatibilityTest {
nodes.size(), testConfig.numClusterNodes);
}
tryFeature("createTopics", testConfig.createTopicsSupported,
new Invoker() {
@Override
public void invoke() throws Throwable {
() -> {
try {
client.createTopics(Collections.singleton(
new NewTopic("newtopic", 1, (short) 1))).all().get();
} catch (ExecutionException e) {
throw e.getCause();
}
}
},
new ResultTester() {
@Override
public void test() throws Throwable {
() -> createTopicsResultTest(client, Collections.singleton("newtopic"))
);
while (true) {
Collection<TopicListing> listings = client.listTopics().listings().get();
if (!testConfig.createTopicsSupported)
break;
if (topicExists(listings, "newtopic"))
break;
Thread.sleep(1);
log.info("Did not see newtopic. Retrying listTopics...");
}
tryFeature("describeAclsSupported", testConfig.describeAclsSupported,
() -> {
try {
client.describeAcls(AclBindingFilter.ANY).values().get();
} catch (ExecutionException e) {
if (e.getCause() instanceof SecurityDisabledException)
return;
throw e.getCause();
}
});
}
}
private void createTopicsResultTest(AdminClient client, Collection<String> topics)
throws InterruptedException, ExecutionException {
while (true) {
try {
client.describeTopics(Collections.singleton("newtopic")).all().get();
client.describeTopics(topics).all().get();
break;
} catch (ExecutionException e) {
if (e.getCause() instanceof UnknownTopicOrPartitionException)
@ -286,38 +310,17 @@ public class ClientCompatibilityTest {
}
}
}
});
while (true) {
Collection<TopicListing> listings = client.listTopics().listings().get();
if (!testConfig.createTopicsSupported)
break;
boolean foundNewTopic = false;
private boolean topicExists(Collection<TopicListing> listings, String topicName) {
boolean foundTopic = false;
for (TopicListing listing : listings) {
if (listing.name().equals("newtopic")) {
if (listing.name().equals(topicName)) {
if (listing.isInternal())
throw new KafkaException("Did not expect newtopic to be an internal topic.");
foundNewTopic = true;
throw new KafkaException(String.format("Did not expect %s to be an internal topic.", topicName));
foundTopic = true;
}
}
if (foundNewTopic)
break;
Thread.sleep(1);
log.info("Did not see newtopic. Retrying listTopics...");
}
tryFeature("describeAclsSupported", testConfig.describeAclsSupported,
new Invoker() {
@Override
public void invoke() throws Throwable {
try {
client.describeAcls(AclBindingFilter.ANY).values().get();
} catch (ExecutionException e) {
if (e.getCause() instanceof SecurityDisabledException)
return;
throw e.getCause();
}
}
});
}
return foundTopic;
}
private static class OffsetsForTime {
@ -384,18 +387,8 @@ public class ClientCompatibilityTest {
}
final OffsetsForTime offsetsForTime = new OffsetsForTime();
tryFeature("offsetsForTimes", testConfig.offsetsForTimesSupported,
new Invoker() {
@Override
public void invoke() {
offsetsForTime.result = consumer.offsetsForTimes(timestampsToSearch);
}
},
new ResultTester() {
@Override
public void test() {
log.info("offsetsForTime = {}", offsetsForTime.result);
}
});
() -> offsetsForTime.result = consumer.offsetsForTimes(timestampsToSearch),
() -> log.info("offsetsForTime = {}", offsetsForTime.result));
// Whether or not offsetsForTimes works, beginningOffsets and endOffsets
// should work.
consumer.beginningOffsets(timestampsToSearch.keySet());
@ -486,11 +479,7 @@ public class ClientCompatibilityTest {
}
private void tryFeature(String featureName, boolean supported, Invoker invoker) throws Throwable {
tryFeature(featureName, supported, invoker, new ResultTester() {
@Override
public void test() {
}
});
tryFeature(featureName, supported, invoker, () -> { });
}
private void tryFeature(String featureName, boolean supported, Invoker invoker, ResultTester resultTester)

View File

@ -19,7 +19,6 @@ package org.apache.kafka.tools;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
@ -32,12 +31,7 @@ public class ToolsUtils {
public static void printMetrics(Map<MetricName, ? extends Metric> metrics) {
if (metrics != null && !metrics.isEmpty()) {
int maxLengthOfDisplayName = 0;
TreeMap<String, Object> sortedMetrics = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
TreeMap<String, Object> sortedMetrics = new TreeMap<>();
for (Metric metric : metrics.values()) {
MetricName mName = metric.metricName();
String mergedName = mName.group() + ":" + mName.name() + ":" + mName.tags();

View File

@ -263,9 +263,7 @@ public class TransactionalMessageCopier {
final AtomicBoolean isShuttingDown = new AtomicBoolean(false);
final AtomicLong remainingMessages = new AtomicLong(maxMessages);
final AtomicLong numMessagesProcessed = new AtomicLong(0);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
isShuttingDown.set(true);
// Flush any remaining messages
producer.close();
@ -273,8 +271,7 @@ public class TransactionalMessageCopier {
consumer.close();
}
System.out.println(shutDownString(numMessagesProcessed.get(), remainingMessages.get(), transactionalId));
}
});
}));
try {
Random random = new Random();

View File

@ -620,12 +620,7 @@ public class VerifiableConsumer implements Closeable, OffsetCommitCallback, Cons
try {
final VerifiableConsumer consumer = createFromArgs(parser, args);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
consumer.close();
}
});
Runtime.getRuntime().addShutdownHook(new Thread(() -> consumer.close()));
consumer.run();
} catch (ArgumentParserException e) {

View File

@ -241,13 +241,10 @@ public class VerifiableLog4jAppender {
final VerifiableLog4jAppender appender = createFromArgs(args);
boolean infinite = appender.maxMessages < 0;
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// Trigger main thread to stop producing messages
appender.stopLogging = true;
}
});
}));
long maxMessages = infinite ? Long.MAX_VALUE : appender.maxMessages;
for (long i = 0; i < maxMessages; i++) {

View File

@ -517,9 +517,7 @@ public class VerifiableProducer implements AutoCloseable {
final long startMs = System.currentTimeMillis();
ThroughputThrottler throttler = new ThroughputThrottler(producer.throughput, startMs);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// Trigger main thread to stop producing messages
producer.stopProducing = true;
@ -531,8 +529,7 @@ public class VerifiableProducer implements AutoCloseable {
double avgThroughput = 1000 * ((producer.numAcked) / (double) (stopMs - startMs));
producer.printJson(new ToolData(producer.numSent, producer.numAcked, producer.throughput, avgThroughput));
}
});
}));
producer.run(throttler);
} catch (ArgumentParserException e) {

View File

@ -147,9 +147,7 @@ public final class Agent {
log.info("Starting agent process.");
final Agent agent = new Agent(platform, Scheduler.SYSTEM, restServer, resource);
restServer.start(resource);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.warn("Running agent shutdown hook.");
try {
agent.beginShutdown();
@ -157,8 +155,7 @@ public final class Agent {
} catch (Exception e) {
log.error("Got exception while running agent shutdown hook.", e);
}
}
});
}));
agent.waitForShutdown();
}
};

View File

@ -317,9 +317,7 @@ public final class WorkerManager {
return;
}
KafkaFutureImpl<String> haltFuture = new KafkaFutureImpl<>();
haltFuture.thenApply(new KafkaFuture.BaseFunction<String, Void>() {
@Override
public Void apply(String errorString) {
haltFuture.thenApply((KafkaFuture.BaseFunction<String, Void>) errorString -> {
if (errorString == null)
errorString = "";
if (errorString.isEmpty()) {
@ -331,7 +329,6 @@ public final class WorkerManager {
stateChangeExecutor.submit(
new HandleWorkerHalting(worker, errorString, false));
return null;
}
});
try {
worker.taskWorker.start(platform, worker.status, haltFuture);

View File

@ -162,9 +162,7 @@ public final class Coordinator {
final Coordinator coordinator = new Coordinator(platform, Scheduler.SYSTEM,
restServer, resource, ThreadLocalRandom.current().nextLong(0, Long.MAX_VALUE / 2));
restServer.start(resource);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.warn("Running coordinator shutdown hook.");
try {
coordinator.beginShutdown(false);
@ -172,8 +170,7 @@ public final class Coordinator {
} catch (Exception e) {
log.error("Got exception while running coordinator shutdown hook.", e);
}
}
});
}));
coordinator.waitForShutdown();
}
};

View File

@ -132,9 +132,7 @@ public class JsonRestServer {
*/
public void beginShutdown() {
if (!shutdownExecutor.isShutdown()) {
shutdownExecutor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
shutdownExecutor.submit((Callable<Void>) () -> {
try {
log.info("Stopping REST server");
jettyServer.stop();
@ -147,7 +145,6 @@ public class JsonRestServer {
}
shutdownExecutor.shutdown();
return null;
}
});
}
}

View File

@ -19,7 +19,6 @@ package org.apache.kafka.trogdor.workload;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.kafka.trogdor.common.Topology;
import org.apache.kafka.trogdor.task.TaskController;
import org.apache.kafka.trogdor.task.TaskSpec;
import org.apache.kafka.trogdor.task.TaskWorker;
@ -28,7 +27,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
@ -98,12 +96,7 @@ public class ConnectionStressSpec extends TaskSpec {
}
public TaskController newController(String id) {
return new TaskController() {
@Override
public Set<String> targetNodes(Topology topology) {
return new TreeSet<>(clientNodes);
}
};
return topology -> new TreeSet<>(clientNodes);
}
@Override

View File

@ -19,7 +19,6 @@ package org.apache.kafka.trogdor.workload;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.kafka.trogdor.common.Topology;
import org.apache.kafka.trogdor.task.TaskController;
import org.apache.kafka.trogdor.task.TaskSpec;
import org.apache.kafka.trogdor.task.TaskWorker;
@ -27,7 +26,6 @@ import org.apache.kafka.trogdor.task.TaskWorker;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
* The specification for a benchmark that produces messages to a set of topics.
@ -170,12 +168,7 @@ public class ProduceBenchSpec extends TaskSpec {
@Override
public TaskController newController(String id) {
return new TaskController() {
@Override
public Set<String> targetNodes(Topology topology) {
return Collections.singleton(producerNode);
}
};
return topology -> Collections.singleton(producerNode);
}
@Override

View File

@ -25,11 +25,9 @@ import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.errors.TimeoutException;
@ -248,9 +246,7 @@ public class RoundTripWorker implements TaskWorker {
ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(partition.topic(),
partition.partition(), KEY_GENERATOR.generate(messageIndex),
spec.valueGenerator().generate(messageIndex));
producer.send(record, new Callback() {
@Override
public void onCompletion(RecordMetadata metadata, Exception exception) {
producer.send(record, (metadata, exception) -> {
if (exception == null) {
unackedSends.countDown();
} else {
@ -258,7 +254,6 @@ public class RoundTripWorker implements TaskWorker {
id, messageIndex, exception.getMessage());
toSendTracker.addFailed(messageIndex);
}
}
});
}
} catch (Throwable e) {

View File

@ -19,14 +19,12 @@ package org.apache.kafka.trogdor.workload;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.kafka.trogdor.common.Topology;
import org.apache.kafka.trogdor.task.TaskController;
import org.apache.kafka.trogdor.task.TaskSpec;
import org.apache.kafka.trogdor.task.TaskWorker;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
* The specification for a workload that sends messages to a broker and then
@ -124,12 +122,7 @@ public class RoundTripWorkloadSpec extends TaskSpec {
@Override
public TaskController newController(String id) {
return new TaskController() {
@Override
public Set<String> targetNodes(Topology topology) {
return Collections.singleton(clientNode);
}
};
return topology -> Collections.singleton(clientNode);
}
@Override

View File

@ -304,7 +304,7 @@ public class AgentTest {
try (MockKibosh mockKibosh = new MockKibosh()) {
Assert.assertEquals(KiboshControlFile.EMPTY, mockKibosh.read());
FilesUnreadableFaultSpec fooSpec = new FilesUnreadableFaultSpec(0, 900000,
Collections.singleton("myAgent"), mockKibosh.tempDir.getPath().toString(), "/foo", 123);
Collections.singleton("myAgent"), mockKibosh.tempDir.getPath(), "/foo", 123);
client.createWorker(new CreateWorkerRequest(0, "foo", fooSpec));
new ExpectedTasks().
addTask(new ExpectedTaskBuilder("foo").
@ -314,7 +314,7 @@ public class AgentTest {
Assert.assertEquals(new KiboshControlFile(Collections.<Kibosh.KiboshFaultSpec>singletonList(
new KiboshFilesUnreadableFaultSpec("/foo", 123))), mockKibosh.read());
FilesUnreadableFaultSpec barSpec = new FilesUnreadableFaultSpec(0, 900000,
Collections.singleton("myAgent"), mockKibosh.tempDir.getPath().toString(), "/bar", 456);
Collections.singleton("myAgent"), mockKibosh.tempDir.getPath(), "/bar", 456);
client.createWorker(new CreateWorkerRequest(1, "bar", barSpec));
new ExpectedTasks().
addTask(new ExpectedTaskBuilder("foo").

View File

@ -19,7 +19,6 @@ package org.apache.kafka.trogdor.common;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.kafka.test.TestCondition;
import org.apache.kafka.test.TestUtils;
import org.apache.kafka.trogdor.agent.AgentClient;
import org.apache.kafka.trogdor.coordinator.CoordinatorClient;
@ -142,9 +141,7 @@ public class ExpectedTasks {
}
public ExpectedTasks waitFor(final CoordinatorClient client) throws InterruptedException {
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
TestUtils.waitForCondition(() -> {
TasksResponse tasks = null;
try {
tasks = client.tasks(new TasksRequest(null, 0, 0, 0, 0, Optional.empty()));
@ -169,15 +166,12 @@ public class ExpectedTasks {
return false;
}
return true;
}
}, "Timed out waiting for expected tasks " + JsonUtil.toJsonString(expected));
return this;
}
public ExpectedTasks waitFor(final AgentClient client) throws InterruptedException {
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
TestUtils.waitForCondition(() -> {
AgentStatusResponse status = null;
try {
status = client.status();
@ -206,7 +200,6 @@ public class ExpectedTasks {
return false;
}
return true;
}
}, "Timed out waiting for expected workers " + JsonUtil.toJsonString(expected));
return this;
}

View File

@ -172,9 +172,7 @@ public class MiniTrogdorCluster implements AutoCloseable {
ThreadUtils.createThreadFactory("MiniTrogdorClusterStartupThread%d", false));
final AtomicReference<Exception> failure = new AtomicReference<Exception>(null);
for (final Map.Entry<String, NodeData> entry : nodes.entrySet()) {
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
executor.submit((Callable<Void>) () -> {
String nodeName = entry.getKey();
try {
NodeData node = entry.getValue();
@ -192,7 +190,6 @@ public class MiniTrogdorCluster implements AutoCloseable {
failure.compareAndSet(null, e);
}
return null;
}
});
}
executor.shutdown();

View File

@ -24,7 +24,6 @@ import org.apache.kafka.common.utils.MockScheduler;
import org.apache.kafka.common.utils.MockTime;
import org.apache.kafka.common.utils.Scheduler;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.test.TestCondition;
import org.apache.kafka.test.TestUtils;
import org.apache.kafka.trogdor.agent.AgentClient;
import org.apache.kafka.trogdor.common.CapturingCommandRunner;
@ -318,12 +317,8 @@ public class CoordinatorTest {
public ExpectedLines waitFor(final String nodeName,
final CapturingCommandRunner runner) throws InterruptedException {
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return linesMatch(nodeName, runner.lines(nodeName));
}
}, "failed to find the expected lines " + this.toString());
TestUtils.waitForCondition(() -> linesMatch(nodeName, runner.lines(nodeName)),
"failed to find the expected lines " + this.toString());
return this;
}
@ -473,7 +468,7 @@ public class CoordinatorTest {
assertEquals(0, coordinatorClient.tasks(
new TasksRequest(null, 10, 0, 10, 0, Optional.empty())).tasks().size());
TasksResponse resp1 = coordinatorClient.tasks(
new TasksRequest(Arrays.asList(new String[] {"foo", "baz" }), 0, 0, 0, 0, Optional.empty()));
new TasksRequest(Arrays.asList("foo", "baz"), 0, 0, 0, 0, Optional.empty()));
assertTrue(resp1.tasks().containsKey("foo"));
assertFalse(resp1.tasks().containsKey("bar"));
assertEquals(1, resp1.tasks().size());

View File

@ -22,7 +22,6 @@ import org.apache.kafka.common.internals.KafkaFutureImpl;
import org.apache.kafka.trogdor.common.Platform;
import org.apache.kafka.trogdor.common.ThreadUtils;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
@ -53,12 +52,9 @@ public class SampleTaskWorker implements TaskWorker {
if (exitMs == null) {
exitMs = Long.MAX_VALUE;
}
this.future = platform.scheduler().schedule(executor, new Callable<Void>() {
@Override
public Void call() throws Exception {
this.future = platform.scheduler().schedule(executor, () -> {
haltFuture.complete(spec.error());
return null;
}
}, exitMs);
}