Skip to content

Commit

Permalink
MINOR: Cleanup redundant casts and other minor points in JRuby extens…
Browse files Browse the repository at this point in the history
…ions

Fixes elastic#9416
  • Loading branch information
original-brownbear committed Apr 25, 2018
1 parent 86ec5df commit 03dbaf1
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 37 deletions.
5 changes: 3 additions & 2 deletions logstash-core/src/main/java/org/logstash/RubyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -80,7 +80,7 @@ public IRubyObject classFor(final ThreadContext context, final IRubyObject type)
)
);
}
return klass;
return (RubyClass) klass;
}
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ public final class MemoryReadBatch implements QueueBatch {

private final LinkedHashSet<IRubyObject> events;

public MemoryReadBatch() {
this(new LinkedHashSet<>());
}

public MemoryReadBatch(final LinkedHashSet<IRubyObject> events) {
this.events = events;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@
@JRubyClass(name = "QueueReadClientBase")
public abstract class QueueReadClientBase extends RubyObject implements QueueReadClient {

protected final ConcurrentHashMap<Long, QueueBatch> inflightBatches =
new ConcurrentHashMap<>();
protected final ConcurrentHashMap<Long, Long> 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<Long, QueueBatch> inflightBatches =
new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, Long> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}

0 comments on commit 03dbaf1

Please sign in to comment.