Skip to content

Commit cfb3434

Browse files
mashhursyaauie
andauthored
[logstash-bridge] Introduce GeoIp interfaces (#132595)
* Use default project resolver instead null which caused NPE when running elastic_integration integration tests. * Introduce BridgeProjectIdResolver to applying null for project ID. * Put a note to the ProjectIdResolverBridge about resolving project ID for multi-project. * GeoIP interfaces for the Logstash bridge where elastic_integration plugin utilizes. * Add a note to maxmind depenedency of the ingest-geoip module that what upgrading the dependency, apply same to the logstash-bridge. * A safeguard in case if bridge becomes null, might happen while cyclic pipeline execution. * Introduce bridges for FailProcessorException, Releasable and RefCountingRunnable ES core components. * hamfisted cleanup * add static GeoIpProcessorBridge#newFactory(IpDatabaseProviderBridge) * provide ProcessorBridge.Factory.AbstractExternal * add ProjectIdBridge * add synchronous ProcessorBridge#execute * migrate RefCountingRunnableBridge to interface pattern * migrate SettingsBridge to interface pattern * migrate PipelineConfigurationBridge to interface pattern * migrate EnvironmentBridge to interface pattern * migrate IngestDocumentBridge to interface pattern * migrate PipelineBridge to interface pattern * reduce unnecessary proxying in ProcessorBridge * migrate MetadataBridge to interface pattern * migrate ScriptServiceBridge to interface pattern * migrate ThreadPoolBridge to interface pattern * visibility cleanup, move AbstractExternal-s * Remove accidental change where ingest-geoip module was opened to maxmind.db pkg --------- Co-authored-by: Ry Biesemeyer <[email protected]>
1 parent 899aae4 commit cfb3434

38 files changed

+1288
-464
lines changed

libs/logstash-bridge/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ other Elasticsearch internals.
66

77
If a change is introduced in a separate Elasticsearch project that causes this project to fail,
88
please consult with members of @elastic/logstash to chart a path forward.
9+
10+
## How to build the module?
11+
```shell
12+
./gradlew :lib:logstash-bridge:build
13+
```

libs/logstash-bridge/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dependencies {
2424
compileOnly project(':x-pack:plugin:redact')
2525
compileOnly project(':x-pack:plugin:spatial')
2626
compileOnly project(':x-pack:plugin:wildcard')
27+
28+
compileOnly('com.maxmind.db:maxmind-db:3.1.1')
2729
}
2830

2931
tasks.named('forbiddenApisMain').configure {

libs/logstash-bridge/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
requires org.elasticsearch.redact;
2424
requires org.elasticsearch.spatial;
2525
requires org.elasticsearch.wildcard;
26+
requires org.elasticsearch.ingest.geoip;
27+
requires com.maxmind.db;
2628

2729
exports org.elasticsearch.logstashbridge;
2830
exports org.elasticsearch.logstashbridge.common;

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/StableBridgeAPI.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ static <T, B extends StableBridgeAPI<T>> B fromInternal(final T delegate, final
5050
* An {@code ProxyInternal<INTERNAL>} is an implementation of {@code StableBridgeAPI<INTERNAL>} that
5151
* proxies calls to a delegate that is an actual {@code INTERNAL}.
5252
*
53+
* <p>
54+
* implementations are intended to be <em>opaque</em> to consumers of this library,
55+
* and should <em>NOT</em> have public constructors.
56+
* </p>
57+
*
5358
* @param <INTERNAL>
5459
*/
5560
abstract class ProxyInternal<INTERNAL> implements StableBridgeAPI<INTERNAL> {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.logstashbridge.common;
11+
12+
import org.elasticsearch.cluster.metadata.ProjectId;
13+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
14+
15+
/**
16+
* A {@link StableBridgeAPI} for {@link ProjectId}
17+
*/
18+
public interface ProjectIdBridge extends StableBridgeAPI<ProjectId> {
19+
String id();
20+
21+
static ProjectIdBridge fromInternal(final ProjectId projectId) {
22+
return new ProxyInternal(projectId);
23+
}
24+
25+
static ProjectIdBridge fromId(final String id) {
26+
final ProjectId internal = ProjectId.fromId(id);
27+
return new ProxyInternal(internal);
28+
}
29+
30+
static ProjectIdBridge getDefault() {
31+
return ProxyInternal.DEFAULT;
32+
}
33+
34+
/**
35+
* An implementation of {@link ProjectIdBridge} that proxies calls to
36+
* an internal {@link ProjectId} instance.
37+
*
38+
* @see StableBridgeAPI.ProxyInternal
39+
*/
40+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<ProjectId> implements ProjectIdBridge {
41+
private static final ProjectIdBridge.ProxyInternal DEFAULT = new ProjectIdBridge.ProxyInternal(ProjectId.DEFAULT);
42+
43+
ProxyInternal(ProjectId internalDelegate) {
44+
super(internalDelegate);
45+
}
46+
47+
@Override
48+
public String id() {
49+
return this.internalDelegate.id();
50+
}
51+
52+
@Override
53+
public ProjectId toInternal() {
54+
return this.internalDelegate;
55+
}
56+
}
57+
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/common/SettingsBridge.java

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,27 @@
1212
import org.elasticsearch.logstashbridge.StableBridgeAPI;
1313

1414
/**
15-
* An external bridge for {@link Settings}
15+
* A {@link StableBridgeAPI} for {@link Settings}
1616
*/
17-
public class SettingsBridge extends StableBridgeAPI.ProxyInternal<Settings> {
17+
public interface SettingsBridge extends StableBridgeAPI<Settings> {
1818

19-
public static SettingsBridge fromInternal(final Settings delegate) {
20-
return new SettingsBridge(delegate);
19+
static SettingsBridge fromInternal(final Settings delegate) {
20+
return new ProxyInternal(delegate);
2121
}
2222

23-
public static Builder builder() {
24-
return Builder.fromInternal(Settings.builder());
25-
}
26-
27-
public SettingsBridge(final Settings delegate) {
28-
super(delegate);
29-
}
30-
31-
@Override
32-
public Settings toInternal() {
33-
return this.internalDelegate;
23+
static SettingsBuilderBridge builder() {
24+
return SettingsBuilderBridge.fromInternal(Settings.builder());
3425
}
3526

3627
/**
37-
* An external bridge for {@link Settings.Builder} that proxies calls to a real {@link Settings.Builder}
28+
* An implementation of {@link SettingsBridge} that proxies calls to
29+
* an internal {@link Settings} instance.
30+
*
31+
* @see StableBridgeAPI.ProxyInternal
3832
*/
39-
public static class Builder extends StableBridgeAPI.ProxyInternal<Settings.Builder> {
40-
static Builder fromInternal(final Settings.Builder delegate) {
41-
return new Builder(delegate);
42-
}
43-
44-
private Builder(final Settings.Builder delegate) {
45-
super(delegate);
46-
}
47-
48-
public Builder put(final String key, final String value) {
49-
this.internalDelegate.put(key, value);
50-
return this;
51-
}
52-
53-
public SettingsBridge build() {
54-
return new SettingsBridge(this.internalDelegate.build());
33+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<Settings> implements SettingsBridge {
34+
ProxyInternal(Settings internalDelegate) {
35+
super(internalDelegate);
5536
}
5637
}
5738
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.logstashbridge.common;
11+
12+
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
14+
15+
/**
16+
* A {@link StableBridgeAPI} for {@link Settings.Builder}.
17+
*/
18+
public interface SettingsBuilderBridge extends StableBridgeAPI<Settings.Builder> {
19+
20+
SettingsBuilderBridge put(String key, String value);
21+
22+
SettingsBridge build();
23+
24+
static SettingsBuilderBridge fromInternal(final Settings.Builder builder) {
25+
return new ProxyInternal(builder);
26+
}
27+
28+
/**
29+
* An implementation of {@link SettingsBuilderBridge} that proxies calls to
30+
* an internal {@link Settings.Builder} instance.
31+
*
32+
* @see StableBridgeAPI.ProxyInternal
33+
*/
34+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<Settings.Builder> implements SettingsBuilderBridge {
35+
ProxyInternal(Settings.Builder internalDelegate) {
36+
super(internalDelegate);
37+
}
38+
39+
@Override
40+
public SettingsBuilderBridge put(String key, String value) {
41+
internalDelegate.put(key, value);
42+
return this;
43+
}
44+
45+
@Override
46+
public SettingsBridge build() {
47+
final Settings delegate = internalDelegate.build();
48+
return SettingsBridge.fromInternal(delegate);
49+
}
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.logstashbridge.core;
11+
12+
import org.elasticsearch.common.CheckedBiFunction;
13+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
14+
15+
/**
16+
* A stable interface on top of {@link CheckedBiFunction}.
17+
* @param <T> type of lhs parameter
18+
* @param <U> type of rhs parameter
19+
* @param <R> type of return value
20+
* @param <E> type of anticipated exception
21+
*/
22+
@FunctionalInterface
23+
public interface CheckedBiFunctionBridge<T, U, R, E extends Exception> extends StableBridgeAPI<CheckedBiFunction<T, U, R, E>> {
24+
R apply(T t, U u) throws E;
25+
26+
@Override
27+
default CheckedBiFunction<T, U, R, E> toInternal() {
28+
return this::apply;
29+
}
30+
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/core/IOUtilsBridge.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* An external bridge for {@link IOUtils}
1717
*/
1818
public class IOUtilsBridge {
19+
private IOUtilsBridge() {}
20+
1921
public static void closeWhileHandlingException(final Iterable<? extends Closeable> objects) {
2022
IOUtils.closeWhileHandlingException(objects);
2123
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.logstashbridge.core;
11+
12+
import org.elasticsearch.action.support.RefCountingRunnable;
13+
import org.elasticsearch.core.Releasable;
14+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
15+
16+
import java.io.Closeable;
17+
18+
/**
19+
* A {@link StableBridgeAPI} for {@link RefCountingRunnable}
20+
*/
21+
public interface RefCountingRunnableBridge extends StableBridgeAPI<RefCountingRunnable>, Closeable {
22+
23+
@Override // only RuntimeException
24+
void close();
25+
26+
ReleasableBridge acquire();
27+
28+
/**
29+
* An API-stable factory method for {@link RefCountingRunnableBridge}
30+
* @param delegate the {@link Runnable} to execute when all refs are closed
31+
* @return a {@link RefCountingRunnableBridge} that will execute the provided
32+
* block when all refs are closed
33+
*/
34+
static RefCountingRunnableBridge create(final Runnable delegate) {
35+
final RefCountingRunnable refCountingRunnable = new RefCountingRunnable(delegate);
36+
return new ProxyInternal(refCountingRunnable);
37+
}
38+
39+
/**
40+
* An implementation of {@link RefCountingRunnableBridge} that proxies calls through
41+
* to an internal {@link RefCountingRunnable}.
42+
* @see StableBridgeAPI.ProxyInternal
43+
*/
44+
final class ProxyInternal extends StableBridgeAPI.ProxyInternal<RefCountingRunnable> implements RefCountingRunnableBridge {
45+
private ProxyInternal(final RefCountingRunnable delegate) {
46+
super(delegate);
47+
}
48+
49+
@Override
50+
public void close() {
51+
toInternal().close();
52+
}
53+
54+
@Override
55+
public ReleasableBridge acquire() {
56+
@SuppressWarnings("resource")
57+
final Releasable releasable = toInternal().acquire();
58+
return ReleasableBridge.fromInternal(releasable);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)