-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathNettyTransport.java
129 lines (112 loc) · 4.75 KB
/
NettyTransport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.sdk;
import java.nio.file.Path;
import java.util.Collections;
import org.opensearch.Version;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.network.NetworkService;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.PageCacheRecycler;
import org.opensearch.core.indices.breaker.CircuitBreakerService;
import org.opensearch.core.indices.breaker.NoneCircuitBreakerService;
import org.opensearch.sdk.ssl.DefaultSslKeyStore;
import org.opensearch.sdk.ssl.SSLConfigConstants;
import org.opensearch.sdk.ssl.SSLNettyTransport;
import org.opensearch.sdk.ssl.SslKeyStore;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.SharedGroupFactory;
import org.opensearch.transport.TransportInterceptor;
import org.opensearch.transport.TransportService;
import org.opensearch.transport.netty4.Netty4Transport;
import static java.util.Collections.emptySet;
import static org.opensearch.common.UUIDs.randomBase64UUID;
/**
* This class initializes a Netty4Transport object and control communication between the extension and OpenSearch.
*/
public class NettyTransport {
private static final String NODE_NAME_SETTING = "node.name";
private final ExtensionsRunner extensionsRunner;
private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() {
};
/**
* @param extensionsRunner Instantiate this object with a reference to the ExtensionsRunner.
*/
public NettyTransport(ExtensionsRunner extensionsRunner) {
this.extensionsRunner = extensionsRunner;
}
/**
* Initializes a Netty4Transport object. This object will be wrapped in a {@link TransportService} object.
*
* @param settings The transport settings to configure.
* @param threadPool A thread pool to use.
* @return The configured Netty4Transport object.
*/
public Netty4Transport getNetty4Transport(Settings settings, ThreadPool threadPool) {
NetworkService networkService = new NetworkService(Collections.emptyList());
PageCacheRecycler pageCacheRecycler = new PageCacheRecycler(settings);
final CircuitBreakerService circuitBreakerService = new NoneCircuitBreakerService();
boolean transportSSLEnabled = settings.getAsBoolean(
SSLConfigConstants.SSL_TRANSPORT_ENABLED,
SSLConfigConstants.SSL_TRANSPORT_ENABLED_DEFAULT
);
Netty4Transport transport = new Netty4Transport(
settings,
Version.CURRENT,
threadPool,
networkService,
pageCacheRecycler,
extensionsRunner.getNamedWriteableRegistry().getRegistry(),
circuitBreakerService,
new SharedGroupFactory(settings)
);
if (transportSSLEnabled) {
Path configPath = Path.of("").toAbsolutePath().resolve("config");
SslKeyStore sks = new DefaultSslKeyStore(settings, configPath);
transport = new SSLNettyTransport(
settings,
Version.CURRENT,
threadPool,
networkService,
pageCacheRecycler,
extensionsRunner.getNamedWriteableRegistry().getRegistry(),
circuitBreakerService,
sks,
new SharedGroupFactory(settings)
);
}
return transport;
}
/**
* Initializes the TransportService object for this extension. This object will control communication between the extension and OpenSearch.
*
* @param settings The transport settings to configure.
* @param threadPool The thread pool to use to start transport service.
* @return The initialized TransportService object.
*/
public TransportService initializeExtensionTransportService(Settings settings, ThreadPool threadPool) {
Netty4Transport transport = getNetty4Transport(settings, threadPool);
// create transport service
TransportService transportService = new TransportService(
settings,
transport,
threadPool,
NOOP_TRANSPORT_INTERCEPTOR,
boundAddress -> DiscoveryNode.createLocal(
Settings.builder().put(NODE_NAME_SETTING, settings.get(NODE_NAME_SETTING)).build(),
boundAddress.publishAddress(),
randomBase64UUID()
),
null,
emptySet()
);
extensionsRunner.startTransportService(transportService);
return transportService;
}
}