Skip to content

Commit 98521f6

Browse files
committed
Reject external versioning when script is configured in OpenSearch sink
OpenSearch does not support external versioning with scripted upserts. Add config-time validation in IndexConfiguration to fail fast with a clear error message instead of getting runtime 400 errors from OpenSearch. Signed-off-by: Dinu John <86094133+dinujoh@users.noreply.github.com>
1 parent 8a14fd7 commit 98521f6

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

data-prepper-plugins/opensearch/src/main/java/org/opensearch/dataprepper/plugins/sink/opensearch/index/IndexConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ public static IndexConfiguration readIndexConfig(final OpenSearchSinkConfig open
302302
builder = builder.withScriptConfiguration(scriptConfiguration);
303303
}
304304

305+
if (scriptConfiguration != null && openSearchSinkConfig.getVersionType() != null
306+
&& VersionType.External.jsonValue().equalsIgnoreCase(openSearchSinkConfig.getVersionType())) {
307+
throw new InvalidPluginConfigurationException(
308+
"document_version_type 'external' is incompatible with script configuration. " +
309+
"OpenSearch does not support external versioning with scripted upserts.");
310+
}
311+
305312
AwsAuthenticationConfiguration awsAuthenticationConfiguration = openSearchSinkConfig.getAwsAuthenticationOptions();
306313
if (awsAuthenticationConfiguration != null) {
307314
builder = builder.withServerless(awsAuthenticationConfiguration.isServerlessCollection());

data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/sink/opensearch/index/IndexConfigurationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,4 +653,26 @@ private String createTemplateContent() {
653653
"\"mappings\":{\"properties\":{\"timestamp\":{\"type\":\"date\",\"format\":\"yyyy-MM-ddHH:mm:ss||yyyy-MM-dd||epoch_millis\"}," +
654654
"\"value\":{\"type\":\"double\"}}}}}";
655655
}
656+
657+
@Test
658+
public void testReadIndexConfig_withExternalVersionAndScript_throws_InvalidPluginConfigurationException() throws JsonProcessingException {
659+
final Map<String, Object> metadata = new HashMap<>();
660+
metadata.put("index_type", "custom");
661+
metadata.put("index", "test-index");
662+
metadata.put("document_version_type", "external");
663+
metadata.put("script", Map.of("source", "ctx._source.counter += 1"));
664+
final OpenSearchSinkConfig openSearchSinkConfig = getOpenSearchSinkConfig(metadata);
665+
assertThrows(InvalidPluginConfigurationException.class, () -> IndexConfiguration.readIndexConfig(openSearchSinkConfig));
666+
}
667+
668+
@Test
669+
public void testReadIndexConfig_withScriptAndNoExternalVersion_doesNotThrow() throws JsonProcessingException {
670+
final Map<String, Object> metadata = new HashMap<>();
671+
metadata.put("index_type", "custom");
672+
metadata.put("index", "test-index");
673+
metadata.put("script", Map.of("source", "ctx._source.counter += 1"));
674+
final OpenSearchSinkConfig openSearchSinkConfig = getOpenSearchSinkConfig(metadata);
675+
final IndexConfiguration config = IndexConfiguration.readIndexConfig(openSearchSinkConfig);
676+
assertThat(config.getScriptConfiguration(), notNullValue());
677+
}
656678
}

0 commit comments

Comments
 (0)