Skip to content

Commit 04c4fed

Browse files
authored
Adapt ElasticSearch 8.1+, migrate from removed APIs to recommended APIs (#9603)
1 parent 15dab2e commit 04c4fed

File tree

19 files changed

+195
-22
lines changed

19 files changed

+195
-22
lines changed

Diff for: docs/en/changes/changes.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
#### OAP Server
77

8-
* Add component ID(133) for impala JDBC Java agent plugin and component ID(134) for impala server
8+
* Add component ID(133) for impala JDBC Java agent plugin and component ID(134) for impala server.
99
* Use prepareStatement in H2SQLExecutor#getByIDs.(No function change).
10-
* Bump up snakeyaml to 1.31 for fixing CVE-2022-25857
10+
* Bump up snakeyaml to 1.31 for fixing CVE-2022-25857.
1111
* Fix `DurationUtils.convertToTimeBucket` missed verify date format.
1212
* [**Breaking Change**] Change the LAL script format(Add layer property).
13+
* Adapt ElasticSearch 8.1+, migrate from removed APIs to recommended APIs.
1314

1415
#### UI
1516

Diff for: oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/elasticsearch/ElasticSearchClient.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ public boolean existDoc(String indexName, String id) {
303303
return es.get().documents().exists(indexName, TYPE, id);
304304
}
305305

306-
307306
/**
308307
* Provide to get documents from multi indices by IDs.
309308
* @param indexIds key: indexName, value: ids list
@@ -319,7 +318,7 @@ public Optional<Documents> ids(Map<String, List<String>> indexIds) {
319318
}
320319

321320
/**
322-
* Search by ids with index alias, when can not locate the physical index.
321+
* Search by ids with index alias, when can not locate the physical index.
323322
* Otherwise, recommend use method {@link #ids}
324323
* @param indexName Index alias name or physical name
325324
* @param ids ID list

Diff for: oap-server/server-library/library-elasticsearch-client/src/main/java/org/apache/skywalking/library/elasticsearch/ElasticSearchVersion.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import org.apache.skywalking.library.elasticsearch.requests.factory.RequestFactory;
2424
import org.apache.skywalking.library.elasticsearch.requests.factory.v6.V6RequestFactory;
2525
import org.apache.skywalking.library.elasticsearch.requests.factory.v6.codec.V6Codec;
26-
import org.apache.skywalking.library.elasticsearch.requests.factory.v7.V78RequestFactory;
27-
import org.apache.skywalking.library.elasticsearch.requests.factory.v7.V7RequestFactory;
28-
import org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec.V78Codec;
29-
import org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec.V7Codec;
26+
import org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.V78RequestFactory;
27+
import org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.V7RequestFactory;
28+
import org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.V81RequestFactory;
29+
import org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec.V78Codec;
30+
import org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec.V7Codec;
3031

3132
public final class ElasticSearchVersion {
3233
private final String distribution;
@@ -64,7 +65,7 @@ private ElasticSearchVersion(final String distribution, final int major, final i
6465
return;
6566
}
6667
if (major == 8) {
67-
requestFactory = new V78RequestFactory(this);
68+
requestFactory = new V81RequestFactory(this);
6869
codec = V78Codec.INSTANCE;
6970
return;
7071
}
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
1919

2020
import lombok.Getter;
2121
import lombok.experimental.Accessors;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
1919

2020
import com.google.common.collect.ImmutableMap;
2121
import com.linecorp.armeria.common.HttpRequest;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
1919

2020
import com.google.common.collect.ImmutableMap;
2121
import com.linecorp.armeria.common.HttpRequest;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
1919

2020
import com.google.common.base.Strings;
2121
import com.google.common.collect.ImmutableMap;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
1919

2020
import lombok.Getter;
2121
import lombok.experimental.Accessors;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
1919

2020
import com.google.common.collect.ImmutableMap;
2121
import com.linecorp.armeria.common.HttpRequest;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
21+
22+
import static com.google.common.base.Preconditions.checkArgument;
23+
import static com.google.common.base.Strings.isNullOrEmpty;
24+
import static com.google.common.collect.Iterables.isEmpty;
25+
import static java.util.Objects.requireNonNull;
26+
import java.util.Map;
27+
import org.apache.skywalking.library.elasticsearch.ElasticSearchVersion;
28+
import org.apache.skywalking.library.elasticsearch.requests.UpdateRequest;
29+
import org.apache.skywalking.library.elasticsearch.requests.factory.DocumentFactory;
30+
import com.google.common.collect.ImmutableMap;
31+
import com.linecorp.armeria.common.HttpRequest;
32+
import com.linecorp.armeria.common.HttpRequestBuilder;
33+
import com.linecorp.armeria.common.MediaType;
34+
import lombok.SneakyThrows;
35+
import lombok.experimental.Delegate;
36+
37+
public class V81DocumentFactory implements DocumentFactory {
38+
private final ElasticSearchVersion version;
39+
40+
@Delegate // Delegate all compatible methods to V7DocumentFactory and just override the incompatible ones.
41+
private final V7DocumentFactory v7DocumentFactory;
42+
43+
public V81DocumentFactory(ElasticSearchVersion version) {
44+
this.version = version;
45+
this.v7DocumentFactory = new V7DocumentFactory(version);
46+
}
47+
48+
@SneakyThrows
49+
@Override
50+
public HttpRequest update(UpdateRequest request, Map<String, ?> params) {
51+
requireNonNull(request, "request");
52+
53+
final String index = request.getIndex();
54+
final String type = request.getType();
55+
final String id = request.getId();
56+
final Map<String, Object> doc = request.getDoc();
57+
58+
checkArgument(!isNullOrEmpty(index), "index cannot be null or empty");
59+
checkArgument(!isNullOrEmpty(type), "type cannot be null or empty");
60+
checkArgument(!isNullOrEmpty(id), "id cannot be null or empty");
61+
checkArgument(doc != null && !isEmpty(doc.entrySet()), "doc cannot be null or empty");
62+
63+
final HttpRequestBuilder builder = HttpRequest.builder();
64+
if (params != null) {
65+
params.forEach(builder::queryParam);
66+
}
67+
final byte[] content = version.codec().encode(ImmutableMap.of("doc", doc));
68+
69+
builder.post("/{index}/_update/{id}")
70+
.pathParam("index", index)
71+
.pathParam("id", id)
72+
.content(MediaType.JSON, content);
73+
74+
return builder.build();
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus;
19+
20+
import lombok.Getter;
21+
import lombok.experimental.Accessors;
22+
import org.apache.skywalking.library.elasticsearch.ElasticSearchVersion;
23+
import org.apache.skywalking.library.elasticsearch.requests.factory.AliasFactory;
24+
import org.apache.skywalking.library.elasticsearch.requests.factory.BulkFactory;
25+
import org.apache.skywalking.library.elasticsearch.requests.factory.DocumentFactory;
26+
import org.apache.skywalking.library.elasticsearch.requests.factory.IndexFactory;
27+
import org.apache.skywalking.library.elasticsearch.requests.factory.RequestFactory;
28+
import org.apache.skywalking.library.elasticsearch.requests.factory.SearchFactory;
29+
import org.apache.skywalking.library.elasticsearch.requests.factory.TemplateFactory;
30+
import org.apache.skywalking.library.elasticsearch.requests.factory.common.CommonAliasFactory;
31+
import org.apache.skywalking.library.elasticsearch.requests.factory.common.CommonBulkFactory;
32+
import org.apache.skywalking.library.elasticsearch.requests.factory.common.CommonSearchFactory;
33+
34+
@Getter
35+
@Accessors(fluent = true)
36+
public final class V81RequestFactory implements RequestFactory {
37+
private final TemplateFactory template;
38+
private final IndexFactory index;
39+
private final AliasFactory alias;
40+
private final DocumentFactory document;
41+
private final SearchFactory search;
42+
private final BulkFactory bulk;
43+
44+
public V81RequestFactory(final ElasticSearchVersion version) {
45+
template = new V78TemplateFactory(version);
46+
index = new V7IndexFactory(version);
47+
alias = new CommonAliasFactory(version);
48+
document = new V81DocumentFactory(version);
49+
search = new CommonSearchFactory(version);
50+
bulk = new CommonBulkFactory(version);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec;
1919

2020
import com.fasterxml.jackson.annotation.JsonInclude;
2121
import com.fasterxml.jackson.core.type.TypeReference;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec;
1919

2020
import com.fasterxml.jackson.annotation.JsonProperty;
2121
import com.fasterxml.jackson.core.JsonParser;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec;
1919

2020
import com.fasterxml.jackson.annotation.JsonInclude;
2121
import com.fasterxml.jackson.core.type.TypeReference;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec;
1919

2020
import com.fasterxml.jackson.core.JsonGenerator;
2121
import com.fasterxml.jackson.core.io.SerializedString;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec;
1919

2020
import com.fasterxml.jackson.core.JsonParser;
2121
import com.fasterxml.jackson.core.type.TypeReference;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec;
1919

2020
import com.fasterxml.jackson.core.JsonParser;
2121
import com.fasterxml.jackson.core.type.TypeReference;
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.skywalking.library.elasticsearch.requests.factory.v7.codec;
18+
package org.apache.skywalking.library.elasticsearch.requests.factory.v7plus.codec;
1919

2020
import com.fasterxml.jackson.core.JsonGenerator;
2121
import com.fasterxml.jackson.core.io.SerializedString;

Diff for: oap-server/server-library/library-elasticsearch-client/src/test/java/org/apache/skywalking/library/elasticsearch/ITElasticSearchTest.java

+44
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import lombok.RequiredArgsConstructor;
2727
import org.apache.skywalking.library.elasticsearch.client.TemplateClient;
2828
import org.apache.skywalking.library.elasticsearch.requests.IndexRequest;
29+
import org.apache.skywalking.library.elasticsearch.requests.UpdateRequest;
2930
import org.apache.skywalking.library.elasticsearch.requests.search.Query;
3031
import org.apache.skywalking.library.elasticsearch.requests.search.Search;
3132
import org.apache.skywalking.library.elasticsearch.requests.search.aggregation.Aggregation;
@@ -85,6 +86,15 @@ public static Collection<Object[]> es() {
8586
.asCompatibleSubstituteFor(
8687
"docker.elastic.co/elasticsearch/elasticsearch-oss"))
8788
},
89+
{
90+
"ElasticSearch 8.1.0",
91+
new ElasticsearchContainer(
92+
DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch")
93+
.withTag("8.1.0")
94+
.asCompatibleSubstituteFor(
95+
"docker.elastic.co/elasticsearch/elasticsearch-oss"))
96+
.withEnv("xpack.security.enabled", "false")
97+
},
8898
{
8999
"OpenSearch 1.0.0",
90100
new ElasticsearchContainer(
@@ -190,6 +200,40 @@ public void testDoc() {
190200
assertEquals(client.documents().get(index, type, idWithSpace).get().getSource(), doc);
191201
}
192202

203+
@Test
204+
public void testDocUpdate() {
205+
final String index = "test-index-update";
206+
assertTrue(client.index().create(index, null, null));
207+
208+
final ImmutableMap<String, Object> doc = ImmutableMap.of("key", "val");
209+
final String idWithSpace = "an id"; // UI management templates' IDs contains spaces
210+
final String type = "type";
211+
212+
client.documents().index(
213+
IndexRequest.builder()
214+
.index(index)
215+
.type(type)
216+
.id(idWithSpace)
217+
.doc(doc)
218+
.build(), null);
219+
220+
assertTrue(client.documents().get(index, type, idWithSpace).isPresent());
221+
assertEquals(client.documents().get(index, type, idWithSpace).get().getId(), idWithSpace);
222+
assertEquals(client.documents().get(index, type, idWithSpace).get().getSource(), doc);
223+
224+
final Map<String, Object> updatedDoc = ImmutableMap.of("key", "new-val");
225+
client.documents().update(
226+
UpdateRequest
227+
.builder()
228+
.index(index)
229+
.type(type)
230+
.id(idWithSpace)
231+
.doc(updatedDoc)
232+
.build(),
233+
null);
234+
assertEquals(client.documents().get(index, type, idWithSpace).get().getSource(), updatedDoc);
235+
}
236+
193237
@SuppressWarnings("unchecked")
194238
@Test
195239
public void testSearch() {

0 commit comments

Comments
 (0)