From 03dbaf127ac2e15ed2152054b02e5736d7b87d43 Mon Sep 17 00:00:00 2001 From: Armin Date: Mon, 23 Apr 2018 08:17:03 +0200 Subject: [PATCH] MINOR: Cleanup redundant casts and other minor points in JRuby extensions Fixes #9416 --- .../src/main/java/org/logstash/RubyUtil.java | 5 +++-- .../ir/compiler/OutputDelegatorExt.java | 15 ++++++------- .../config/ir/compiler/OutputStrategyExt.java | 14 ++++++------ .../logstash/execution/MemoryReadBatch.java | 4 ---- .../execution/QueueReadClientBase.java | 22 ++++++++++--------- .../ext/JrubyAckedWriteClientExt.java | 2 +- .../ext/JrubyTimestampExtLibrary.java | 4 ++-- .../ext/JrubyMemoryReadClientExtTest.java | 5 ++--- 8 files changed, 34 insertions(+), 37 deletions(-) diff --git a/logstash-core/src/main/java/org/logstash/RubyUtil.java b/logstash-core/src/main/java/org/logstash/RubyUtil.java index 9ac35ebf687..d27f53825c0 100644 --- a/logstash-core/src/main/java/org/logstash/RubyUtil.java +++ b/logstash-core/src/main/java/org/logstash/RubyUtil.java @@ -127,8 +127,9 @@ public final class RubyUtil { OUTPUT_STRATEGY_SINGLE.defineAnnotatedMethods(OutputStrategyExt.SingleOutputStrategyExt.class); OUTPUT_STRATEGY_LEGACY.defineAnnotatedMethods(OutputStrategyExt.LegacyOutputStrategyExt.class); final OutputStrategyExt.OutputStrategyRegistryExt outputStrategyRegistry = - (OutputStrategyExt.OutputStrategyRegistryExt) OutputStrategyExt.OutputStrategyRegistryExt - .instance(RUBY.getCurrentContext(), OUTPUT_DELEGATOR_STRATEGIES); + OutputStrategyExt.OutputStrategyRegistryExt.instance( + RUBY.getCurrentContext(), OUTPUT_DELEGATOR_STRATEGIES + ); outputStrategyRegistry.register( RUBY.getCurrentContext(), RUBY.newSymbol("shared"), OUTPUT_STRATEGY_SHARED ); diff --git a/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputDelegatorExt.java b/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputDelegatorExt.java index 6493c9f2165..84f809322d1 100644 --- a/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputDelegatorExt.java +++ b/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputDelegatorExt.java @@ -62,14 +62,13 @@ public IRubyObject init(final ThreadContext context, final IRubyObject[] argumen eventMetricTime = LongCounter.fromRubyBase( metricEvents, MetricKeys.DURATION_IN_MILLIS_KEY ); - strategy = (OutputStrategyExt.AbstractOutputStrategyExt) ((RubyClass) - ((OutputStrategyExt.OutputStrategyRegistryExt) arguments[3]) - .classFor(context, concurrency(context)) - ).newInstance( - context, - new IRubyObject[]{outputClass, namespacedMetric, arguments[2], args}, - Block.NULL_BLOCK - ); + strategy = (OutputStrategyExt.AbstractOutputStrategyExt) ( + (OutputStrategyExt.OutputStrategyRegistryExt) arguments[3]) + .classFor(context, concurrency(context)).newInstance( + context, + new IRubyObject[]{outputClass, namespacedMetric, arguments[2], args}, + Block.NULL_BLOCK + ); return this; } diff --git a/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputStrategyExt.java b/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputStrategyExt.java index 194e366714c..9df9db38b1b 100644 --- a/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputStrategyExt.java +++ b/logstash-core/src/main/java/org/logstash/config/ir/compiler/OutputStrategyExt.java @@ -24,7 +24,7 @@ private OutputStrategyExt() { @JRubyClass(name = "OutputDelegatorStrategyRegistry") public static final class OutputStrategyRegistryExt extends RubyObject { - private static OutputStrategyRegistryExt instance; + private static OutputStrategyExt.OutputStrategyRegistryExt instance; private RubyHash map; @@ -33,10 +33,10 @@ public OutputStrategyRegistryExt(final Ruby runtime, final RubyClass metaClass) } @JRubyMethod(meta = true) - public static synchronized IRubyObject instance(final ThreadContext context, - final IRubyObject recv) { + public static synchronized OutputStrategyExt.OutputStrategyRegistryExt instance( + final ThreadContext context, final IRubyObject recv) { if (instance == null) { - instance = new OutputStrategyRegistryExt( + instance = new OutputStrategyExt.OutputStrategyRegistryExt( context.runtime, RubyUtil.OUTPUT_STRATEGY_REGISTRY ); instance.init(context); @@ -68,7 +68,7 @@ public IRubyObject register(final ThreadContext context, final IRubyObject type, @JRubyMethod(name = "class_for") @SuppressWarnings("unchecked") - public IRubyObject classFor(final ThreadContext context, final IRubyObject type) { + public RubyClass classFor(final ThreadContext context, final IRubyObject type) { final IRubyObject klass = map.op_aref(context, type); if (!klass.isTrue()) { throw new IllegalArgumentException( @@ -80,7 +80,7 @@ public IRubyObject classFor(final ThreadContext context, final IRubyObject type) ) ); } - return klass; + return (RubyClass) klass; } } @@ -192,7 +192,7 @@ public abstract static class SimpleAbstractOutputStrategyExt private IRubyObject output; - public SimpleAbstractOutputStrategyExt(final Ruby runtime, final RubyClass metaClass) { + protected SimpleAbstractOutputStrategyExt(final Ruby runtime, final RubyClass metaClass) { super(runtime, metaClass); } diff --git a/logstash-core/src/main/java/org/logstash/execution/MemoryReadBatch.java b/logstash-core/src/main/java/org/logstash/execution/MemoryReadBatch.java index cb97f90da39..26ef1c6bec3 100644 --- a/logstash-core/src/main/java/org/logstash/execution/MemoryReadBatch.java +++ b/logstash-core/src/main/java/org/logstash/execution/MemoryReadBatch.java @@ -13,10 +13,6 @@ public final class MemoryReadBatch implements QueueBatch { private final LinkedHashSet events; - public MemoryReadBatch() { - this(new LinkedHashSet<>()); - } - public MemoryReadBatch(final LinkedHashSet events) { this.events = events; } diff --git a/logstash-core/src/main/java/org/logstash/execution/QueueReadClientBase.java b/logstash-core/src/main/java/org/logstash/execution/QueueReadClientBase.java index e8501e5edbd..3f29c752ec0 100644 --- a/logstash-core/src/main/java/org/logstash/execution/QueueReadClientBase.java +++ b/logstash-core/src/main/java/org/logstash/execution/QueueReadClientBase.java @@ -22,25 +22,27 @@ @JRubyClass(name = "QueueReadClientBase") public abstract class QueueReadClientBase extends RubyObject implements QueueReadClient { - protected final ConcurrentHashMap inflightBatches = - new ConcurrentHashMap<>(); - protected final ConcurrentHashMap inflightClocks = new ConcurrentHashMap<>(); protected int batchSize = 125; protected long waitForNanos = 50 * 1000 * 1000; // 50 millis to nanos protected long waitForMillis = 50; - protected LongCounter eventMetricOut; - protected LongCounter eventMetricFiltered; - protected LongCounter eventMetricTime; - protected LongCounter pipelineMetricOut; - protected LongCounter pipelineMetricFiltered; - protected LongCounter pipelineMetricTime; + + private final ConcurrentHashMap inflightBatches = + new ConcurrentHashMap<>(); + private final ConcurrentHashMap inflightClocks = new ConcurrentHashMap<>(); + + private LongCounter eventMetricOut; + private LongCounter eventMetricFiltered; + private LongCounter eventMetricTime; + private LongCounter pipelineMetricOut; + private LongCounter pipelineMetricFiltered; + private LongCounter pipelineMetricTime; protected QueueReadClientBase(final Ruby runtime, final RubyClass metaClass) { super(runtime, metaClass); } @JRubyMethod(name = "inflight_batches") - public IRubyObject rubyGetInflightBatches(final ThreadContext context) { + public RubyHash rubyGetInflightBatches(final ThreadContext context) { final RubyHash result = RubyHash.newHash(context.runtime); result.putAll(inflightBatches); return result; diff --git a/logstash-core/src/main/java/org/logstash/ext/JrubyAckedWriteClientExt.java b/logstash-core/src/main/java/org/logstash/ext/JrubyAckedWriteClientExt.java index ebe14b681d5..0e2edcd8490 100644 --- a/logstash-core/src/main/java/org/logstash/ext/JrubyAckedWriteClientExt.java +++ b/logstash-core/src/main/java/org/logstash/ext/JrubyAckedWriteClientExt.java @@ -20,7 +20,7 @@ public final class JrubyAckedWriteClientExt extends RubyObject { private AtomicBoolean closed = new AtomicBoolean(); @JRubyMethod(meta = true, required = 2) - public static IRubyObject create(final ThreadContext context, IRubyObject recv, + public static JrubyAckedWriteClientExt create(final ThreadContext context, IRubyObject recv, final IRubyObject queue, final IRubyObject closed) { return new JrubyAckedWriteClientExt( context.runtime, RubyUtil.ACKED_WRITE_CLIENT_CLASS, diff --git a/logstash-core/src/main/java/org/logstash/ext/JrubyTimestampExtLibrary.java b/logstash-core/src/main/java/org/logstash/ext/JrubyTimestampExtLibrary.java index 49c98671a6a..6f4a866b2a2 100644 --- a/logstash-core/src/main/java/org/logstash/ext/JrubyTimestampExtLibrary.java +++ b/logstash-core/src/main/java/org/logstash/ext/JrubyTimestampExtLibrary.java @@ -77,7 +77,7 @@ public IRubyObject ruby_initialize(ThreadContext context, IRubyObject[] args) } @JRubyMethod(name = "time") - public IRubyObject ruby_time(ThreadContext context) + public RubyTime ruby_time(ThreadContext context) { return RubyTime.newTime(context.runtime, this.timestamp.getTime()); } @@ -225,7 +225,7 @@ public IRubyObject ruby_year(ThreadContext context) @JRubyMethod(name = "<=>", required = 1) public IRubyObject op_cmp(final ThreadContext context, final IRubyObject other) { if (other instanceof JrubyTimestampExtLibrary.RubyTimestamp) { - return ((RubyTime) ruby_time(context)).op_cmp( + return ruby_time(context).op_cmp( context, ((JrubyTimestampExtLibrary.RubyTimestamp) other).ruby_time(context) ); } diff --git a/logstash-core/src/test/java/org/logstash/ext/JrubyMemoryReadClientExtTest.java b/logstash-core/src/test/java/org/logstash/ext/JrubyMemoryReadClientExtTest.java index c3d9ac531d6..cb7521bc496 100644 --- a/logstash-core/src/test/java/org/logstash/ext/JrubyMemoryReadClientExtTest.java +++ b/logstash-core/src/test/java/org/logstash/ext/JrubyMemoryReadClientExtTest.java @@ -1,7 +1,6 @@ package org.logstash.ext; import java.io.IOException; -import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import org.jruby.RubyHash; @@ -26,10 +25,10 @@ public void testInflightBatchesTracking() throws InterruptedException, IOExcepti JrubyMemoryReadClientExt.create(queue, 5, 50); final ThreadContext context = WorkerLoop.THREAD_CONTEXT.get(); final QueueBatch batch = client.readBatch(); - final RubyHash inflight = (RubyHash) client.rubyGetInflightBatches(context); + final RubyHash inflight = client.rubyGetInflightBatches(context); assertThat(inflight.size(), is(1)); assertThat(inflight.get(Thread.currentThread().getId()), is(batch)); client.closeBatch(batch); - assertThat(((Map) client.rubyGetInflightBatches(context)).size(), is(0)); + assertThat(client.rubyGetInflightBatches(context).size(), is(0)); } }