forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAVAFICATION: Cleanup OutputDelegatorExt
Fixes elastic#9740
- Loading branch information
1 parent
30e055b
commit 5c6e3e7
Showing
17 changed files
with
353 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
logstash-core/src/main/java/org/logstash/config/ir/compiler/AbstractOutputDelegatorExt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package org.logstash.config.ir.compiler; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.TimeUnit; | ||
import org.jruby.Ruby; | ||
import org.jruby.RubyArray; | ||
import org.jruby.RubyClass; | ||
import org.jruby.RubyObject; | ||
import org.jruby.RubyString; | ||
import org.jruby.anno.JRubyClass; | ||
import org.jruby.anno.JRubyMethod; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.logstash.RubyUtil; | ||
import org.logstash.ext.JrubyEventExtLibrary; | ||
import org.logstash.instrument.metrics.AbstractMetricExt; | ||
import org.logstash.instrument.metrics.AbstractNamespacedMetricExt; | ||
import org.logstash.instrument.metrics.MetricKeys; | ||
import org.logstash.instrument.metrics.counter.LongCounter; | ||
|
||
@JRubyClass(name = "AbstractOutputDelegator") | ||
public abstract class AbstractOutputDelegatorExt extends RubyObject { | ||
|
||
private AbstractMetricExt metric; | ||
|
||
protected AbstractNamespacedMetricExt namespacedMetric; | ||
|
||
private IRubyObject metricEvents; | ||
|
||
private RubyString id; | ||
|
||
private LongCounter eventMetricOut; | ||
|
||
private LongCounter eventMetricIn; | ||
|
||
private LongCounter eventMetricTime; | ||
|
||
public AbstractOutputDelegatorExt(final Ruby runtime, final RubyClass metaClass) { | ||
super(runtime, metaClass); | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject register(final ThreadContext context) { | ||
doRegister(context); | ||
return context.nil; | ||
} | ||
|
||
@JRubyMethod(name = "do_close") | ||
public IRubyObject doClose(final ThreadContext context) { | ||
close(context); | ||
return context.nil; | ||
} | ||
|
||
@JRubyMethod(name = "reloadable?") | ||
public IRubyObject isReloadable(final ThreadContext context) { | ||
return reloadable(context); | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject concurrency(final ThreadContext context) { | ||
return getConcurrency(context); | ||
} | ||
|
||
@JRubyMethod(name = "config_name") | ||
public IRubyObject configName(final ThreadContext context) { | ||
return getConfigName(context); | ||
} | ||
|
||
@JRubyMethod(name = "id") | ||
public IRubyObject getId() { | ||
return id; | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject metric() { | ||
return metric; | ||
} | ||
|
||
@JRubyMethod(name = "namespaced_metric") | ||
public IRubyObject namespacedMetric() { | ||
return namespacedMetric; | ||
} | ||
|
||
@JRubyMethod(name = "metric_events") | ||
public IRubyObject metricEvents() { | ||
return metricEvents; | ||
} | ||
|
||
@JRubyMethod(name = "multi_receive") | ||
public IRubyObject multiReceive(final IRubyObject events) { | ||
final RubyArray batch = (RubyArray) events; | ||
final int count = batch.size(); | ||
eventMetricIn.increment((long) count); | ||
final long start = System.nanoTime(); | ||
doOutput(batch); | ||
eventMetricTime.increment( | ||
TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS) | ||
); | ||
eventMetricOut.increment((long) count); | ||
return this; | ||
} | ||
|
||
protected void initMetrics(final String id, final AbstractMetricExt metric) { | ||
this.metric = metric; | ||
final ThreadContext context = RubyUtil.RUBY.getCurrentContext(); | ||
this.id = RubyString.newString(context.runtime, id); | ||
namespacedMetric = metric.namespace(context, context.runtime.newSymbol(id)); | ||
metricEvents = namespacedMetric.namespace(context, MetricKeys.EVENTS_KEY); | ||
namespacedMetric.gauge( | ||
context, MetricKeys.NAME_KEY, configName(context) | ||
); | ||
eventMetricOut = LongCounter.fromRubyBase(metricEvents, MetricKeys.OUT_KEY); | ||
eventMetricIn = LongCounter.fromRubyBase(metricEvents, MetricKeys.IN_KEY); | ||
eventMetricTime = LongCounter.fromRubyBase( | ||
metricEvents, MetricKeys.DURATION_IN_MILLIS_KEY | ||
); | ||
} | ||
|
||
protected abstract IRubyObject getConfigName(ThreadContext context); | ||
|
||
protected abstract IRubyObject getConcurrency(ThreadContext context); | ||
|
||
protected abstract void doOutput(Collection<JrubyEventExtLibrary.RubyEvent> batch); | ||
|
||
protected abstract void close(ThreadContext context); | ||
|
||
protected abstract void doRegister(ThreadContext context); | ||
|
||
protected abstract IRubyObject reloadable(ThreadContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
logstash-core/src/main/java/org/logstash/config/ir/compiler/JavaOutputDelegatorExt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.logstash.config.ir.compiler; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Consumer; | ||
import org.jruby.Ruby; | ||
import org.jruby.RubyClass; | ||
import org.jruby.RubyString; | ||
import org.jruby.RubySymbol; | ||
import org.jruby.anno.JRubyClass; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.logstash.RubyUtil; | ||
import org.logstash.ext.JrubyEventExtLibrary; | ||
import org.logstash.instrument.metrics.AbstractMetricExt; | ||
|
||
@JRubyClass(name = "JavaOutputDelegator") | ||
public final class JavaOutputDelegatorExt extends AbstractOutputDelegatorExt { | ||
|
||
private static final RubySymbol CONCURRENCY = RubyUtil.RUBY.newSymbol("java"); | ||
|
||
private RubyString configName; | ||
|
||
private Consumer<Collection<JrubyEventExtLibrary.RubyEvent>> outputFunction; | ||
|
||
private Runnable closeAction; | ||
|
||
private Runnable registerAction; | ||
|
||
public JavaOutputDelegatorExt(final Ruby runtime, final RubyClass metaClass) { | ||
super(runtime, metaClass); | ||
} | ||
|
||
public static JavaOutputDelegatorExt create(final String configName, final String id, | ||
final AbstractMetricExt metric, | ||
final Consumer<Collection<JrubyEventExtLibrary.RubyEvent>> outputFunction, | ||
final Runnable closeAction, final Runnable registerAction) { | ||
final JavaOutputDelegatorExt instance = | ||
new JavaOutputDelegatorExt(RubyUtil.RUBY, RubyUtil.JAVA_OUTPUT_DELEGATOR_CLASS); | ||
instance.initMetrics(id, metric); | ||
instance.configName = RubyUtil.RUBY.newString(configName); | ||
instance.outputFunction = outputFunction; | ||
instance.closeAction = closeAction; | ||
instance.registerAction = registerAction; | ||
return instance; | ||
} | ||
|
||
@Override | ||
protected IRubyObject getConfigName(final ThreadContext context) { | ||
return configName; | ||
} | ||
|
||
@Override | ||
protected IRubyObject getConcurrency(final ThreadContext context) { | ||
return CONCURRENCY; | ||
} | ||
|
||
@Override | ||
protected void doOutput(final Collection<JrubyEventExtLibrary.RubyEvent> batch) { | ||
outputFunction.accept(batch); | ||
} | ||
|
||
@Override | ||
protected void close(final ThreadContext context) { | ||
closeAction.run(); | ||
} | ||
|
||
@Override | ||
protected void doRegister(final ThreadContext context) { | ||
registerAction.run(); | ||
} | ||
|
||
@Override | ||
protected IRubyObject reloadable(final ThreadContext context) { | ||
return context.tru; | ||
} | ||
} |
Oops, something went wrong.