Skip to content

Commit e5829f4

Browse files
committed
Deprecated ClusterService and Using NodeClient to fetch meta data (#774)
Signed-off-by: penghuo <[email protected]>
1 parent eeb90cf commit e5829f4

File tree

4 files changed

+113
-208
lines changed

4 files changed

+113
-208
lines changed

legacy/src/main/java/org/opensearch/sql/legacy/plugin/OpenSearchSQLPluginConfig.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.opensearch.sql.legacy.plugin;
88

99
import org.opensearch.client.node.NodeClient;
10-
import org.opensearch.cluster.service.ClusterService;
1110
import org.opensearch.sql.common.setting.Settings;
1211
import org.opensearch.sql.executor.ExecutionEngine;
1312
import org.opensearch.sql.expression.config.ExpressionConfig;
@@ -34,8 +33,6 @@
3433
@Configuration
3534
@Import({ExpressionConfig.class})
3635
public class OpenSearchSQLPluginConfig {
37-
@Autowired
38-
private ClusterService clusterService;
3936

4037
@Autowired
4138
private NodeClient nodeClient;
@@ -48,7 +45,7 @@ public class OpenSearchSQLPluginConfig {
4845

4946
@Bean
5047
public OpenSearchClient client() {
51-
return new OpenSearchNodeClient(clusterService, nodeClient);
48+
return new OpenSearchNodeClient(nodeClient);
5249
}
5350

5451
@Bean

opensearch/src/main/java/org/opensearch/sql/opensearch/client/OpenSearchNodeClient.java

Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
1010
import com.google.common.collect.ImmutableList;
1111
import com.google.common.collect.ImmutableMap;
12-
import java.io.IOException;
12+
import com.google.common.collect.Streams;
1313
import java.util.Arrays;
1414
import java.util.Collection;
1515
import java.util.List;
@@ -18,48 +18,34 @@
1818
import java.util.function.Predicate;
1919
import java.util.stream.Collectors;
2020
import java.util.stream.Stream;
21-
import org.apache.logging.log4j.ThreadContext;
2221
import org.opensearch.action.admin.indices.get.GetIndexResponse;
23-
import org.opensearch.action.support.IndicesOptions;
22+
import org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse;
23+
import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse;
2424
import org.opensearch.client.node.NodeClient;
25-
import org.opensearch.cluster.ClusterState;
2625
import org.opensearch.cluster.metadata.AliasMetadata;
27-
import org.opensearch.cluster.metadata.IndexMetadata;
2826
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
29-
import org.opensearch.cluster.metadata.MappingMetadata;
30-
import org.opensearch.cluster.service.ClusterService;
31-
import org.opensearch.common.collect.ImmutableOpenMap;
3227
import org.opensearch.common.settings.Settings;
33-
import org.opensearch.common.unit.TimeValue;
3428
import org.opensearch.index.IndexSettings;
3529
import org.opensearch.sql.opensearch.mapping.IndexMapping;
3630
import org.opensearch.sql.opensearch.request.OpenSearchRequest;
3731
import org.opensearch.sql.opensearch.response.OpenSearchResponse;
38-
import org.opensearch.threadpool.ThreadPool;
3932

4033
/** OpenSearch connection by node client. */
4134
public class OpenSearchNodeClient implements OpenSearchClient {
4235

4336
public static final Function<String, Predicate<String>> ALL_FIELDS =
4437
(anyIndex -> (anyField -> true));
4538

46-
/** Current cluster state on local node. */
47-
private final ClusterService clusterService;
48-
4939
/** Node client provided by OpenSearch container. */
5040
private final NodeClient client;
5141

5242
/** Index name expression resolver to get concrete index name. */
5343
private final IndexNameExpressionResolver resolver;
5444

55-
private static final String SQL_WORKER_THREAD_POOL_NAME = "sql-worker";
56-
5745
/**
5846
* Constructor of ElasticsearchNodeClient.
5947
*/
60-
public OpenSearchNodeClient(ClusterService clusterService,
61-
NodeClient client) {
62-
this.clusterService = clusterService;
48+
public OpenSearchNodeClient(NodeClient client) {
6349
this.client = client;
6450
this.resolver = new IndexNameExpressionResolver(client.threadPool().getThreadContext());
6551
}
@@ -78,14 +64,16 @@ public OpenSearchNodeClient(ClusterService clusterService,
7864
@Override
7965
public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {
8066
try {
81-
ClusterState state = clusterService.state();
82-
String[] concreteIndices = resolveIndexExpression(state, indexExpression);
83-
84-
return populateIndexMappings(
85-
state.metadata().findMappings(concreteIndices, ALL_FIELDS));
86-
} catch (IOException e) {
67+
GetMappingsResponse mappingsResponse = client.admin().indices()
68+
.prepareGetMappings(indexExpression)
69+
.setLocal(true)
70+
.get();
71+
return Streams.stream(mappingsResponse.mappings().iterator())
72+
.collect(Collectors.toMap(cursor -> cursor.key,
73+
cursor -> new IndexMapping(cursor.value)));
74+
} catch (Exception e) {
8775
throw new IllegalStateException(
88-
"Failed to read mapping in cluster state for index pattern [" + indexExpression + "]", e);
76+
"Failed to read mapping for index pattern [" + indexExpression + "]", e);
8977
}
9078
}
9179

@@ -97,19 +85,24 @@ public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {
9785
*/
9886
@Override
9987
public Map<String, Integer> getIndexMaxResultWindows(String... indexExpression) {
100-
ClusterState state = clusterService.state();
101-
ImmutableOpenMap<String, IndexMetadata> indicesMetadata = state.metadata().getIndices();
102-
String[] concreteIndices = resolveIndexExpression(state, indexExpression);
103-
104-
ImmutableMap.Builder<String, Integer> result = ImmutableMap.builder();
105-
for (String index : concreteIndices) {
106-
Settings settings = indicesMetadata.get(index).getSettings();
107-
Integer maxResultWindow = settings.getAsInt("index.max_result_window",
108-
IndexSettings.MAX_RESULT_WINDOW_SETTING.getDefault(settings));
109-
result.put(index, maxResultWindow);
88+
try {
89+
GetSettingsResponse settingsResponse =
90+
client.admin().indices().prepareGetSettings(indexExpression).setLocal(true).get();
91+
ImmutableMap.Builder<String, Integer> result = ImmutableMap.builder();
92+
for (ObjectObjectCursor<String, Settings> indexToSetting :
93+
settingsResponse.getIndexToSettings()) {
94+
Settings settings = indexToSetting.value;
95+
result.put(
96+
indexToSetting.key,
97+
settings.getAsInt(
98+
IndexSettings.MAX_RESULT_WINDOW_SETTING.getKey(),
99+
IndexSettings.MAX_RESULT_WINDOW_SETTING.getDefault(settings)));
100+
}
101+
return result.build();
102+
} catch (Exception e) {
103+
throw new IllegalStateException(
104+
"Failed to read setting for index pattern [" + indexExpression + "]", e);
110105
}
111-
112-
return result.build();
113106
}
114107

115108
/**
@@ -149,9 +142,8 @@ public List<String> indices() {
149142
*/
150143
@Override
151144
public Map<String, String> meta() {
152-
final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
153-
builder.put(META_CLUSTER_NAME, clusterService.getClusterName().value());
154-
return builder.build();
145+
return ImmutableMap.of(META_CLUSTER_NAME,
146+
client.settings().get("cluster.name", "opensearch"));
155147
}
156148

157149
@Override
@@ -161,40 +153,12 @@ public void cleanup(OpenSearchRequest request) {
161153

162154
@Override
163155
public void schedule(Runnable task) {
164-
ThreadPool threadPool = client.threadPool();
165-
threadPool.schedule(
166-
withCurrentContext(task),
167-
new TimeValue(0),
168-
SQL_WORKER_THREAD_POOL_NAME
169-
);
156+
// at that time, task already running the sql-worker ThreadPool.
157+
task.run();
170158
}
171159

172160
@Override
173161
public NodeClient getNodeClient() {
174162
return client;
175163
}
176-
177-
private String[] resolveIndexExpression(ClusterState state, String[] indices) {
178-
return resolver.concreteIndexNames(state, IndicesOptions.strictExpandOpen(), true, indices);
179-
}
180-
181-
private Map<String, IndexMapping> populateIndexMappings(
182-
ImmutableOpenMap<String, MappingMetadata> indexMappings) {
183-
184-
ImmutableMap.Builder<String, IndexMapping> result = ImmutableMap.builder();
185-
for (ObjectObjectCursor<String, MappingMetadata> cursor:
186-
indexMappings) {
187-
result.put(cursor.key, new IndexMapping(cursor.value));
188-
}
189-
return result.build();
190-
}
191-
192-
/** Copy from LogUtils. */
193-
private static Runnable withCurrentContext(final Runnable task) {
194-
final Map<String, String> currentContext = ThreadContext.getImmutableContext();
195-
return () -> {
196-
ThreadContext.putAll(currentContext);
197-
task.run();
198-
};
199-
}
200164
}

0 commit comments

Comments
 (0)