Skip to content

Commit

Permalink
chore: install checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
sauljabin committed Jan 31, 2024
1 parent 0569af8 commit 61bd0d0
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 85 deletions.
23 changes: 1 addition & 22 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,12 @@ repos:
# Tests

- id: java-build
name: Tests Build
name: Tests Java Projects
language: system
always_run: true
pass_filenames: false
entry: ./gradlew build

- id: server-tests
name: Running Server Tests
language: system
always_run: true
pass_filenames: false
entry: ./gradlew server:test

- id: canary-tests
name: Running Canary Java Tests
language: system
always_run: true
pass_filenames: false
entry: ./gradlew canary:test

- id: java-tests
name: Running SDK Java Tests
language: system
always_run: true
pass_filenames: false
entry: ./gradlew sdk-java:test

- id: javadoc
name: Running JavaDoc
language: system
Expand Down
10 changes: 10 additions & 0 deletions canary/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'java'
id 'application'
id 'checkstyle'
id 'com.diffplug.spotless' version '6.25.0'
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'io.freefair.lombok' version '8.4'
Expand Down Expand Up @@ -82,3 +83,12 @@ protobuf {
}
}
}

checkstyle {
configFile = file("${rootDir}/canary/checkstyle.xml")
checkstyleTest.enabled = false
}

tasks.withType(Checkstyle).configureEach {
exclude('**io/littlehorse/canary/proto**')
}
9 changes: 9 additions & 0 deletions canary/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="FinalParameters"/>
<module name="FinalLocalVariable"/>
</module>
</module>
11 changes: 1 addition & 10 deletions canary/src/main/java/io/littlehorse/canary/CanaryException.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package io.littlehorse.canary;

public class CanaryException extends RuntimeException {
public CanaryException() {}

public CanaryException(String message) {
super(message);
}

public CanaryException(String message, Throwable cause) {
super(message, cause);
}

public CanaryException(Throwable cause) {
public CanaryException(final Throwable cause) {
super(cause);
}
}
22 changes: 11 additions & 11 deletions canary/src/main/java/io/littlehorse/canary/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
@Slf4j
public class Main {

public static void main(String[] args) throws IOException, InterruptedException {
CanaryConfig config = args.length > 0 ? ConfigLoader.load(Paths.get(args[0])) : ConfigLoader.load();
public static void main(final String[] args) throws IOException, InterruptedException {
final CanaryConfig config = args.length > 0 ? ConfigLoader.load(Paths.get(args[0])) : ConfigLoader.load();

log.debug("Canary configurations: {}", config);
log.debug("KafkaAdmin configurations: {}", config.toKafkaAdminConfig());
log.debug("KafkaProducer configurations: {}", config.toKafkaProducerConfig());
log.debug("KafkaStreams configurations: {}", config.toKafkaStreamsConfig());
log.debug("LittleHorse configurations: {}", config.toLittleHorseConfig());

int latchSize = 1 + (config.isMetronomeEnabled() ? 1 : 0) + (config.isAggregatorEnabled() ? 1 : 0);
CountDownLatch latch = new CountDownLatch(latchSize);
final int latchSize = 1 + (config.isMetronomeEnabled() ? 1 : 0) + (config.isAggregatorEnabled() ? 1 : 0);
final CountDownLatch latch = new CountDownLatch(latchSize);

try {
initiateKafkaTopicBootstrap(config, latch);
Expand All @@ -45,30 +45,30 @@ public static void main(String[] args) throws IOException, InterruptedException
log.info("Stopped");
}

private static void initiateAggregatorBootstrap(CanaryConfig config, CountDownLatch latch) {
AggregatorBootstrap aggregatorBootstrap = new AggregatorBootstrap(
private static void initiateAggregatorBootstrap(final CanaryConfig config, final CountDownLatch latch) {
final AggregatorBootstrap aggregatorBootstrap = new AggregatorBootstrap(
config.getTopicName(), config.toKafkaStreamsConfig().toMap());
addShutdownHook(aggregatorBootstrap, latch);
}

private static void initiateMetronomeBootstrap(CanaryConfig config, CountDownLatch latch) {
MetronomeBootstrap metronomeBootstrap = new MetronomeBootstrap(
private static void initiateMetronomeBootstrap(final CanaryConfig config, final CountDownLatch latch) {
final MetronomeBootstrap metronomeBootstrap = new MetronomeBootstrap(
config.getTopicName(),
config.toKafkaProducerConfig().toMap(),
config.toLittleHorseConfig().toMap());
addShutdownHook(metronomeBootstrap, latch);
}

private static void initiateKafkaTopicBootstrap(CanaryConfig config, CountDownLatch latch) {
KafkaTopicBootstrap kafkaTopicBootstrap = new KafkaTopicBootstrap(
private static void initiateKafkaTopicBootstrap(final CanaryConfig config, final CountDownLatch latch) {
final KafkaTopicBootstrap kafkaTopicBootstrap = new KafkaTopicBootstrap(
config.getTopicName(),
config.getTopicPartitions(),
config.getTopicReplicas(),
config.toKafkaAdminConfig().toMap());
addShutdownHook(kafkaTopicBootstrap, latch);
}

private static void addShutdownHook(Bootstrap bootstrap, CountDownLatch latch) {
private static void addShutdownHook(final Bootstrap bootstrap, final CountDownLatch latch) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.trace("{} shutdown process started", bootstrap.getClass().getSimpleName());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class AggregatorBootstrap implements Bootstrap {
private static final Consumed<String, Bytes> SERDES = Consumed.with(Serdes.String(), Serdes.Bytes());
private final KafkaStreams kafkaStreams;

public AggregatorBootstrap(String topicName, Map<String, Object> kafkaStreamsConfigMap) {
kafkaStreams = new KafkaStreams(buildTopology(topicName), new StreamsConfig(kafkaStreamsConfigMap));
public AggregatorBootstrap(final String metricsTopicName, final Map<String, Object> kafkaStreamsConfigMap) {
kafkaStreams = new KafkaStreams(buildTopology(metricsTopicName), new StreamsConfig(kafkaStreamsConfigMap));
kafkaStreams.start();

log.trace("Initialized");
}

private static Metric toMetric(Bytes value) {
private static Metric toMetric(final Bytes value) {
try {
return Metric.parseFrom(value.get());
} catch (InvalidProtocolBufferException e) {
Expand All @@ -39,11 +39,11 @@ private static Metric toMetric(Bytes value) {
}
}

private Topology buildTopology(String topicName) {
StreamsBuilder builder = new StreamsBuilder();
private Topology buildTopology(final String metricsTopicName) {
final StreamsBuilder builder = new StreamsBuilder();

KStream<String, Metric> metricStream =
builder.stream(topicName, SERDES).mapValues(AggregatorBootstrap::toMetric);
final KStream<String, Metric> metricStream =
builder.stream(metricsTopicName, SERDES).mapValues(AggregatorBootstrap::toMetric);

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CanaryConfig implements Config {

private final Map<String, Object> configs;

public CanaryConfig(Map<String, Object> configs) {
public CanaryConfig(final Map<String, Object> configs) {
this.configs = configs.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(LH_CANARY_PREFIX))
.collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private ConfigLoader() {}
* @return A CanaryConfig with all system configuration
*/
public static CanaryConfig load() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
final SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.addDefaultSources()
.build();
Expand All @@ -42,8 +42,8 @@ public static CanaryConfig load() {
* @return A CanaryConfig with all system configuration
* @throws IOException If properties file was not found
*/
public static CanaryConfig load(Path path) throws IOException {
SmallRyeConfig config = new SmallRyeConfigBuilder()
public static CanaryConfig load(final Path path) throws IOException {
final SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.addDefaultSources()
.withSources(new PropertiesConfigSource(path.toUri().toURL(), 200))
Expand All @@ -61,8 +61,8 @@ public static CanaryConfig load(Path path) throws IOException {
* @param properties Properties object
* @return A CanaryConfig with all system configuration
*/
public static CanaryConfig load(Properties properties) {
SmallRyeConfig config = new SmallRyeConfigBuilder()
public static CanaryConfig load(final Properties properties) {
final SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.addDefaultSources()
.withSources(new PropertiesConfigSource(
Expand All @@ -71,8 +71,8 @@ public static CanaryConfig load(Properties properties) {
return new CanaryConfig(toMap(config));
}

private static Map<String, Object> toMap(Config config) {
Map<String, Object> configs = new TreeMap<>();
private static Map<String, Object> toMap(final Config config) {
final Map<String, Object> configs = new TreeMap<>();
for (String key : config.getPropertyNames()) {
configs.put(key, config.getOptionalValue(key, String.class).orElse(""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class KafkaAdminConfig implements Config {
private final Map<String, Object> configs;

public KafkaAdminConfig(Map<String, Object> configs) {
public KafkaAdminConfig(final Map<String, Object> configs) {
this.configs = configs.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(LH_CANARY_KAFKA_PREFIX))
.map(entry -> entry(entry.getKey().substring(LH_CANARY_KAFKA_PREFIX.length()), entry.getValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class KafkaProducerConfig implements Config {
private final Map<String, Object> configs;

public KafkaProducerConfig(Map<String, Object> configs) {
public KafkaProducerConfig(final Map<String, Object> configs) {
this.configs = configs.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(LH_CANARY_KAFKA_PREFIX))
.map(entry -> entry(entry.getKey().substring(LH_CANARY_KAFKA_PREFIX.length()), entry.getValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class KafkaStreamsConfig implements Config {
private final Map<String, Object> configs;

public KafkaStreamsConfig(Map<String, Object> configs) {
public KafkaStreamsConfig(final Map<String, Object> configs) {
this.configs = configs.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(LH_CANARY_KAFKA_PREFIX))
.map(entry -> entry(entry.getKey().substring(LH_CANARY_KAFKA_PREFIX.length()), entry.getValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
public class LittleHorseConfig implements Config {
private final Map<String, Object> configs;

public LittleHorseConfig(Map<String, Object> configs) {
public LittleHorseConfig(final Map<String, Object> configs) {
this.configs = configs.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(LH_CANARY_PREFIX))
.map(entry -> {
String formattedKey = entry.getKey()
final String formattedKey = entry.getKey()
.substring(LH_CANARY_PREFIX.length())
.toUpperCase()
.replace(".", "_")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ public class KafkaTopicBootstrap implements Bootstrap {
private final AdminClient adminClient;

public KafkaTopicBootstrap(
String topicName, int topicPartitions, short topicReplicas, Map<String, Object> kafkaAdminConfigMap) {
final String metricsTopicName,
final int topicPartitions,
final short topicReplicas,
final Map<String, Object> kafkaAdminConfigMap) {
adminClient = KafkaAdminClient.create(kafkaAdminConfigMap);

try {
NewTopic canaryTopic = new NewTopic(topicName, topicPartitions, topicReplicas);
final NewTopic canaryTopic = new NewTopic(metricsTopicName, topicPartitions, topicReplicas);

adminClient.createTopics(List.of(canaryTopic)).all().get();
log.info("Topics {} created", topicName);
log.info("Topics {} created", metricsTopicName);
} catch (Exception e) {
if (e.getCause() instanceof TopicExistsException) {
log.warn(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public class MetricsEmitter implements Closeable {
private final Producer<String, Bytes> producer;
private final String topicName;

public MetricsEmitter(String topicName, Map<String, Object> kafkaProducerConfigMap) {
public MetricsEmitter(final String topicName, final Map<String, Object> kafkaProducerConfigMap) {
this.producer = new KafkaProducer<>(kafkaProducerConfigMap);
this.topicName = topicName;
}

public Future<RecordMetadata> future(String key, Metric metric) {
ProducerRecord<String, Bytes> record = new ProducerRecord<>(topicName, key, Bytes.wrap(metric.toByteArray()));
public Future<RecordMetadata> future(final String key, final Metric metric) {
final ProducerRecord<String, Bytes> record =
new ProducerRecord<>(topicName, key, Bytes.wrap(metric.toByteArray()));

return producer.send(record, (metadata, exception) -> {
if (exception == null) {
Expand All @@ -36,9 +37,9 @@ public Future<RecordMetadata> future(String key, Metric metric) {
});
}

public void emit(String key, Metric metric) {
public RecordMetadata emit(final String key, final Metric metric) {
try {
future(key, metric).get();
return future(key, metric).get();
} catch (InterruptedException | ExecutionException e) {
throw new CanaryException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Metronome implements Closeable, Runnable {
private final ScheduledExecutorService executor;
private final MetricsEmitter emitter;

public Metronome(MetricsEmitter emitter) {
public Metronome(final MetricsEmitter emitter) {
this.emitter = emitter;
executor = Executors.newScheduledThreadPool(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ public class MetronomeBootstrap implements Bootstrap {
private final Metronome metronome;

public MetronomeBootstrap(
String topicName, Map<String, Object> kafkaProducerConfigMap, Map<String, Object> littleHorseConfigMap) {
final String metricsTopicName,
final Map<String, Object> kafkaProducerConfigMap,
final Map<String, Object> littleHorseConfigMap) {
// Initialize kafka producer
emitter = new MetricsEmitter(topicName, kafkaProducerConfigMap);
emitter = new MetricsEmitter(metricsTopicName, kafkaProducerConfigMap);
metronome = new Metronome(emitter);
metronome.start();

// Initialize task worker
LHConfig lhConfig = new LHConfig(littleHorseConfigMap);
final LHConfig lhConfig = new LHConfig(littleHorseConfigMap);
try {
worker = new LHTaskWorker(new MetronomeTask(emitter), TASK_NAME, lhConfig);
worker.registerTaskDef();
Expand All @@ -37,7 +39,7 @@ public MetronomeBootstrap(
}

// Initialize workflow
Workflow workflow = Workflow.newWorkflow(
final Workflow workflow = Workflow.newWorkflow(
"canary-workflow",
thread -> thread.execute(TASK_NAME, thread.addVariable(VARIABLE_NAME, VariableType.INT)));
try {
Expand Down
Loading

0 comments on commit 61bd0d0

Please sign in to comment.