@Override public Parser<String, Object> makeParser() { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); parser = parser == null ? new JavaScriptParser(function) : parser; return parser; }
@JsonCreator public JavaScriptTieredBrokerSelectorStrategy( @JsonProperty("function") String fn, @JacksonInject JavaScriptConfig config ) { Preconditions.checkNotNull(fn, "function must not be null"); Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); this.function = fn; }
@JsonCreator public JavaScriptWorkerSelectStrategy( @JsonProperty("function") String fn, @JacksonInject JavaScriptConfig config ) { Preconditions.checkNotNull(fn, "function must not be null"); Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); this.function = fn; }
/** * {@link #compute} can be called by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ @EnsuresNonNull("fn") private Function getCompiledScript() { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); Function syncedFn = fn; if (syncedFn == null) { synchronized (config) { syncedFn = fn; if (syncedFn == null) { syncedFn = compile(function); fn = syncedFn; } } } return syncedFn; }
/** * This class can be used by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ @EnsuresNonNull("compiledScript") private JavaScriptAggregator.ScriptAggregator getCompiledScript() { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); JavaScriptAggregator.ScriptAggregator syncedCompiledScript = compiledScript; if (syncedCompiledScript == null) { synchronized (config) { syncedCompiledScript = compiledScript; if (syncedCompiledScript == null) { syncedCompiledScript = compileScript(fnAggregate, fnReset, fnCombine); compiledScript = syncedCompiledScript; } } } return syncedCompiledScript; }
/** * {@link #apply(Object)} can be called by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ @EnsuresNonNull("fn") private Function<Object, String> getCompiledScript() { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); Function<Object, String> syncedFn = fn; if (syncedFn == null) { synchronized (config) { syncedFn = fn; if (syncedFn == null) { syncedFn = compile(function); fn = syncedFn; } } } return syncedFn; }
/** * This class can be used by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ @EnsuresNonNull("predicateFactory") private JavaScriptPredicateFactory getPredicateFactory() { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); JavaScriptPredicateFactory syncedFnPredicateFactory = predicateFactory; if (syncedFnPredicateFactory == null) { synchronized (config) { syncedFnPredicateFactory = predicateFactory; if (syncedFnPredicateFactory == null) { syncedFnPredicateFactory = new JavaScriptPredicateFactory(function, extractionFn); predicateFactory = syncedFnPredicateFactory; } } } return syncedFnPredicateFactory; }
@Test public void testSerdeWithDefaults() throws Exception { String json = "{}"; JavaScriptConfig config = mapper.readValue( mapper.writeValueAsString( mapper.readValue( json, JavaScriptConfig.class ) ), JavaScriptConfig.class ); Assert.assertFalse(config.isEnabled()); } }
@Test public void testSerde() throws Exception { String json = "{\"enabled\":true}"; JavaScriptConfig config = mapper.readValue( mapper.writeValueAsString( mapper.readValue( json, JavaScriptConfig.class ) ), JavaScriptConfig.class ); Assert.assertTrue(config.isEnabled()); }
@Test public void testInjectionEnabled() { final Properties props = new Properties(); props.setProperty("druid.javascript.enabled", "true"); JavaScriptConfig config = makeInjectorWithProperties(props).getInstance(JavaScriptConfig.class); Assert.assertTrue(config.isEnabled()); }
@Test public void testInjectionDefault() { JavaScriptConfig config = makeInjectorWithProperties(new Properties()).getInstance(JavaScriptConfig.class); Assert.assertFalse(config.isEnabled()); }
@JsonCreator public JavaScriptTieredBrokerSelectorStrategy( @JsonProperty("function") String fn, @JacksonInject JavaScriptConfig config ) { Preconditions.checkNotNull(fn, "function must not be null"); Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); this.function = fn; }
@Override public Parser<String, Object> makeParser() { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); parser = parser == null ? new JavaScriptParser(function) : parser; return parser; }
@JsonCreator public JavaScriptWorkerSelectStrategy( @JsonProperty("function") String fn, @JacksonInject JavaScriptConfig config ) { Preconditions.checkNotNull(fn, "function must not be null"); Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); this.function = fn; }
/** * {@link #apply(Object)} can be called by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ private void checkAndCompileScript() { if (fn == null) { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); synchronized (config) { if (fn == null) { fn = compile(function); } } } }
/** * This class can be used by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ private void checkAndCompileScript() { if (compiledScript == null) { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); synchronized (config) { if (compiledScript == null) { compiledScript = compileScript(fnAggregate, fnReset, fnCombine); } } } }
/** * This class can be used by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ private void checkAndCreatePredicateFactory() { if (predicateFactory == null) { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); synchronized (config) { if (predicateFactory == null) { predicateFactory = new JavaScriptPredicateFactory(function, extractionFn); } } } }
/** * {@link #compute} can be called by multiple threads, so this function should be thread-safe to avoid extra * script compilation. */ private void checkAndCompileScript() { if (fn == null) { // JavaScript configuration should be checked when it's actually used because someone might still want Druid // nodes to be able to deserialize JavaScript-based objects even though JavaScript is disabled. Preconditions.checkState(config.isEnabled(), "JavaScript is disabled"); // Synchronizing here can degrade the performance significantly because this method is called per input row. // However, early compilation of JavaScript functions can occur some memory issues due to unnecessary compilation // involving Java class generation each time, and thus this will be better. synchronized (config) { if (fn == null) { fn = compile(function); } } } }