Skip to content

Commit

Permalink
Adds support for composable index templates (opensearch-project#2808)
Browse files Browse the repository at this point in the history
Adds support for composable index templates. Resolves opensearch-project#1275. Update the OpenSearch sink integration test to skip the composable index template tests on older versions of OpenDistro. Updated the README.md with the new template_type feature.

Signed-off-by: David Venable <[email protected]>
  • Loading branch information
dlvenable authored Jun 5, 2023
1 parent e15aa1f commit a86c0d7
Show file tree
Hide file tree
Showing 19 changed files with 1,914 additions and 873 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Run Open Distro tests
run: |
./gradlew :data-prepper-plugins:opensearch:integrationTest --tests "org.opensearch.dataprepper.plugins.sink.opensearch.OpenSearchIT" -Dtests.opensearch.host=localhost:9200 -Dtests.opensearch.user=admin -Dtests.opensearch.password=admin
./gradlew :data-prepper-plugins:opensearch:integrationTest -Dtests.opensearch.host=localhost:9200 -Dtests.opensearch.user=admin -Dtests.opensearch.password=admin -Dtests.opensearch.bundle=true
./gradlew :data-prepper-plugins:opensearch:integrationTest -Dtests.opensearch.host=localhost:9200 -Dtests.opensearch.user=admin -Dtests.opensearch.password=admin -Dtests.opensearch.bundle=true -Dtests.opensearch.version=opendistro:${{ matrix.opendistro }}
- name: Upload Unit Test Results
if: always()
uses: actions/upload-artifact@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Run OpenSearch tests
run: |
./gradlew :data-prepper-plugins:opensearch:integrationTest --tests "org.opensearch.dataprepper.plugins.sink.opensearch.OpenSearchIT" -Dtests.opensearch.host=localhost:9200 -Dtests.opensearch.user=admin -Dtests.opensearch.password=admin
./gradlew :data-prepper-plugins:opensearch:integrationTest -Dtests.opensearch.host=localhost:9200 -Dtests.opensearch.user=admin -Dtests.opensearch.password=admin -Dtests.opensearch.bundle=true
./gradlew :data-prepper-plugins:opensearch:integrationTest -Dtests.opensearch.host=localhost:9200 -Dtests.opensearch.user=admin -Dtests.opensearch.password=admin -Dtests.opensearch.bundle=true -Dtests.opensearch.version=opensearch:${{ matrix.opensearch }}
- name: Upload Unit Test Results
if: always()
uses: actions/upload-artifact@v3
Expand Down
2 changes: 2 additions & 0 deletions data-prepper-plugins/opensearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ Default is null.
* This index name can also be a plain string plus a date-time pattern as a suffix, such as `application-%{yyyy.MM.dd}`, `my-index-name-%{yyyy.MM.dd.HH}`. When OpenSearch Sink is sending data to OpenSearch, the date-time pattern will be replaced by actual UTC time. The pattern supports all the symbols that represent one hour or above and are listed in [Java DateTimeFormatter](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html). For example, with an index pattern like `my-index-name-%{yyyy.MM.dd}`, a new index is created for each day such as `my-index-name-2022.01.25`. For another example, with an index pattern like `my-index-name-%{yyyy.MM.dd.HH}`, a new index is created for each hour such as `my-index-name-2022.01.25.13`.
* This index name can also be a formatted string (with or without date-time pattern suffix), such as `my-${index}-name`. When OpenSearchSink is sending data to OpenSearch, the format portion "${index}" will be replaced by it's value in the event that is being processed. The format may also be like "${index1/index2/index3}" in which case the field "index1/index2/index3" is searched in the event and replaced by its value.

- <a name="template_type"></a>`template_type`(optional): Defines what type of OpenSearch template to use. The available options are `v1` and `index-template`. The default value is `v1`, which uses the original OpenSearch templates available at the `_template` API endpoints. Select `index-template` to use composable index templates which are available at OpenSearch's `_index_template` endpoint.

- <a name="template_file"></a>`template_file`(optional): A json file path or AWS S3 URI to be read as index template for custom data ingestion. The json file content should be the json value of
`"template"` key in the json content of OpenSearch [Index templates API](https://opensearch.org/docs/latest/opensearch/index-templates/),
e.g. [otel-v1-apm-span-index-template.json](https://github.com/opensearch-project/data-prepper/blob/main/data-prepper-plugins/opensearch/src/main/resources/otel-v1-apm-span-index-template.json)
Expand Down
2 changes: 2 additions & 0 deletions data-prepper-plugins/opensearch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ task integrationTest(type: Test) {
systemProperty 'tests.opensearch.bundle', System.getProperty('tests.opensearch.bundle')
systemProperty 'tests.opensearch.user', System.getProperty('tests.opensearch.user')
systemProperty 'tests.opensearch.password', System.getProperty('tests.opensearch.password')
systemProperty 'tests.opensearch.version', System.getProperty('tests.opensearch.version')

filter {
includeTestsMatching '*IT'
includeTestsMatching '*Test'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.plugins.sink.opensearch;

import org.opensearch.Version;

/**
* An interface for letting the test declare what OpenSearch version is being
* tested against.
*/
class DeclaredOpenSearchVersion implements Comparable<DeclaredOpenSearchVersion> {
private static final DeclaredOpenSearchVersion DEFAULT = new DeclaredOpenSearchVersion(Distribution.OPENSEARCH, "1.0.0");
public static final DeclaredOpenSearchVersion OPENDISTRO_1_9 = new DeclaredOpenSearchVersion(Distribution.OPENDISTRO, "1.9.0");

enum Distribution {
OPENDISTRO,
OPENSEARCH
}

private final Distribution distribution;
private final String version;
private final Version internalVersion;

private DeclaredOpenSearchVersion(final Distribution distribution, final String version) {
this.distribution = distribution;
this.version = version;

internalVersion = Version.fromString(version);
}

static DeclaredOpenSearchVersion parse(final String versionString) {
if(versionString == null) {
return DEFAULT;
}

final String[] parts = versionString.split(":");

if(parts.length != 2) {
throw new IllegalArgumentException("Invalid version string provided.");
}

final Distribution distribution = Distribution.valueOf(parts[0].toUpperCase());

return new DeclaredOpenSearchVersion(distribution, parts[1]);
}

@Override
public int compareTo(final DeclaredOpenSearchVersion other) {
final int distributionCompareTo = distribution.compareTo(other.distribution);

if(distributionCompareTo != 0) {
return distributionCompareTo;
}

return internalVersion.compareTo(other.internalVersion);
}

Distribution getDistribution() {
return distribution;
}

String getVersion() {
return version;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.plugins.sink.opensearch;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

class DeclaredOpenSearchVersionTest {
@ParameterizedTest
@CsvSource({
"opensearch:1.2.4,OPENSEARCH,1.2.4",
"opensearch:1.3.9,OPENSEARCH,1.3.9",
"opensearch:2.2.1,OPENSEARCH,2.2.1",
"opensearch:2.6.0,OPENSEARCH,2.6.0",
"opendistro:1.3.0,OPENDISTRO,1.3.0",
"opendistro:1.13.3,OPENDISTRO,1.13.3"
})
void parse_should_return_expected(final String versionString, final DeclaredOpenSearchVersion.Distribution expectedDistribution, final String expectedVersion) {
final DeclaredOpenSearchVersion version = DeclaredOpenSearchVersion.parse(versionString);

assertThat(version, notNullValue());
assertThat(version.getDistribution(), equalTo(expectedDistribution));
assertThat(version.getVersion(), equalTo(expectedVersion));
}

@Test
void parse_with_null_should_return_minimum_version() {
final DeclaredOpenSearchVersion version = DeclaredOpenSearchVersion.parse(null);

assertThat(version, notNullValue());
assertThat(version.getDistribution(), equalTo(DeclaredOpenSearchVersion.Distribution.OPENSEARCH));
assertThat(version.getVersion(), equalTo("1.0.0"));
}

@ParameterizedTest
@CsvSource({
"opensearch:1.2.4,opensearch:1.2.4,0",
"opendistro:1.13.3,opendistro:1.13.3,0",
"opensearch:1.2.4,opensearch:1.2.3,1",
"opensearch:1.2.3,opensearch:1.2.4,-1",
"opensearch:2.6.0,opensearch:1.2.3,1",
"opensearch:1.2.3,opensearch:2.6.0,-1",
"opensearch:1.3.9,opendistro:1.13.3,1",
"opendistro:1.13.3,opensearch:1.3.9,-1"
})
void compareTo_returns_correct_value(final String versionStringToTest, final String otherVersionString, final int expectedCompareTo) {
final DeclaredOpenSearchVersion objectUnderTest = DeclaredOpenSearchVersion.parse(versionStringToTest);
final DeclaredOpenSearchVersion otherVersion = DeclaredOpenSearchVersion.parse(otherVersionString);

assertThat(objectUnderTest.compareTo(otherVersion), equalTo(expectedCompareTo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ static List<String> getHosts() {
.map(ip -> String.format("https://%s", ip)).collect(Collectors.toList());
}

static DeclaredOpenSearchVersion getVersion() {
return DeclaredOpenSearchVersion.parse(System.getProperty("tests.opensearch.version"));
}

/**
* Copied from OpenSearch test framework
* TODO: Consolidate in OpenSearch
Expand Down
Loading

0 comments on commit a86c0d7

Please sign in to comment.