This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
/
ElasticsearchNodeClient.java
151 lines (131 loc) Β· 5.69 KB
/
ElasticsearchNodeClient.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/
package com.amazon.opendistroforelasticsearch.sql.elasticsearch.client;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.mapping.IndexMapping;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.request.ElasticsearchRequest;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.response.ElasticsearchResponse;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.ThreadContext;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.threadpool.ThreadPool;
/** Elasticsearch connection by node client. */
@RequiredArgsConstructor
public class ElasticsearchNodeClient implements ElasticsearchClient {
/** Default types and field filter to match all. */
public static final String[] ALL_TYPES = new String[0];
public static final Function<String, Predicate<String>> ALL_FIELDS =
(anyIndex -> (anyField -> true));
/** Current cluster state on local node. */
private final ClusterService clusterService;
/** Node client provided by Elasticsearch container. */
private final NodeClient client;
/** Index name expression resolver to get concrete index name. */
private final IndexNameExpressionResolver resolver = new IndexNameExpressionResolver();
private static final String SQL_WORKER_THREAD_POOL_NAME = "sql-worker";
/**
* Get field mappings of index by an index expression. Majority is copied from legacy
* LocalClusterState.
*
* <p>For simplicity, removed type (deprecated) and field filter in argument list. Also removed
* mapping cache, cluster state listener (mainly for performance and debugging).
*
* @param indexExpression index name expression
* @return index mapping(s) in our class to isolate Elasticsearch API. IndexNotFoundException is
* thrown if no index matched.
*/
@Override
public Map<String, IndexMapping> getIndexMappings(String indexExpression) {
try {
ClusterState state = clusterService.state();
String[] concreteIndices = resolveIndexExpression(state, new String[] {indexExpression});
return populateIndexMappings(
state.metadata().findMappings(concreteIndices, ALL_TYPES, ALL_FIELDS));
} catch (IOException e) {
throw new IllegalStateException(
"Failed to read mapping in cluster state for index pattern [" + indexExpression + "]", e);
}
}
/** TODO: Scroll doesn't work for aggregation. Support aggregation later. */
@Override
public ElasticsearchResponse search(ElasticsearchRequest request) {
SearchResponse esResponse;
if (request.isScrollStarted()) {
esResponse = client.searchScroll(request.scrollRequest()).actionGet();
} else {
esResponse = client.search(request.searchRequest()).actionGet();
}
request.setScrollId(esResponse.getScrollId());
return new ElasticsearchResponse(esResponse);
}
@Override
public void cleanup(ElasticsearchRequest request) {
if (request.isScrollStarted()) {
client.prepareClearScroll().addScrollId(request.getScrollId()).get();
request.reset();
}
}
@Override
public void schedule(Runnable task) {
ThreadPool threadPool = client.threadPool();
threadPool.schedule(
withCurrentContext(task),
new TimeValue(0),
SQL_WORKER_THREAD_POOL_NAME
);
}
private String[] resolveIndexExpression(ClusterState state, String[] indices) {
return resolver.concreteIndexNames(state, IndicesOptions.strictExpandOpen(), indices);
}
private Map<String, IndexMapping> populateIndexMappings(
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> indexMappings) {
ImmutableMap.Builder<String, IndexMapping> result = ImmutableMap.builder();
for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetadata>> cursor :
indexMappings) {
result.put(cursor.key, populateIndexMapping(cursor.value));
}
return result.build();
}
private IndexMapping populateIndexMapping(
ImmutableOpenMap<String, MappingMetadata> indexMapping) {
if (indexMapping.isEmpty()) {
return new IndexMapping(Collections.emptyMap());
}
return new IndexMapping(indexMapping.iterator().next().value);
}
/** Copy from LogUtils. */
private static Runnable withCurrentContext(final Runnable task) {
final Map<String, String> currentContext = ThreadContext.getImmutableContext();
return () -> {
ThreadContext.putAll(currentContext);
task.run();
};
}
}