Skip to content

Commit f1182d6

Browse files
committed
Addressed review comments
Signed-off-by: Krishna Kondaka <krishkdk@amazon.com>
1 parent 3e22ac0 commit f1182d6

9 files changed

Lines changed: 49 additions & 23 deletions

File tree

data-prepper-plugins/aws-plugin-api/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dependencies {
44
implementation 'software.amazon.awssdk:apache-client'
55
implementation 'org.apache.httpcomponents.client5:httpclient5:5.3.1'
66
implementation 'com.fasterxml.jackson.core:jackson-annotations'
7+
testImplementation libs.commons.lang3
78
testImplementation 'org.hibernate.validator:hibernate-validator:8.0.2.Final'
89
}
910

data-prepper-plugins/aws-common/src/test/java/org/opensearch/dataprepper/plugins/common/aws/AwsConfigTest.java renamed to data-prepper-plugins/aws-plugin-api/src/test/java/org/opensearch/dataprepper/aws/api/AwsConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package org.opensearch.dataprepper.plugins.common.aws;
6+
package org.opensearch.dataprepper.aws.api;
77

88
import org.junit.jupiter.api.BeforeEach;
99
import org.junit.jupiter.api.Test;

data-prepper-plugins/parse-json-processor/src/main/java/org/opensearch/dataprepper/plugins/codec/json/NdjsonOutputCodec.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,24 @@ public void start(final OutputStream outputStream, Event event, final OutputCode
3939
this.codecContext = codecContext;
4040
}
4141

42+
private String getFullEventJson(final Event event, final OutputCodecContext context) {
43+
return event.jsonBuilder()
44+
.includeKeys(context.getIncludeKeys())
45+
.excludeKeys(context.getExcludeKeys())
46+
.includeTags(context.getTagsTargetKey())
47+
.toJsonString();
48+
}
49+
50+
@Override
51+
public long getEstimatedSize(final Event event, final OutputCodecContext context) throws IOException {
52+
return getFullEventJson(event, context).length();
53+
}
54+
4255
@Override
4356
public void writeEvent(final Event event, final OutputStream outputStream) throws IOException {
4457
Objects.requireNonNull(event);
4558

46-
String json = event.jsonBuilder()
47-
.includeKeys(codecContext.getIncludeKeys())
48-
.excludeKeys(codecContext.getExcludeKeys())
49-
.includeTags(codecContext.getTagsTargetKey())
50-
.toJsonString();
59+
String json = getFullEventJson(event, codecContext);
5160
outputStream.write(json.getBytes());
5261
outputStream.write(System.lineSeparator().getBytes());
5362
}

data-prepper-plugins/parse-json-processor/src/test/java/org/opensearch/dataprepper/plugins/codec/json/NewlineDelimitedOutputCodecTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void testGetEstimatedSize() throws Exception {
9999
ndjsonOutputCodec.start(outputStream, null, codecContext);
100100

101101
Record<Event> record = getRecord(0);
102-
String expectedEventString = "{\"name\":\"Person0\",\"age\":0}\n";
102+
String expectedEventString = "{\"name\":\"Person0\",\"age\":0}";
103103
assertThat(ndjsonOutputCodec.getEstimatedSize(record.getData(), codecContext), equalTo((long)expectedEventString.length()));
104104
}
105105
}

data-prepper-plugins/sqs-sink/src/integrationTest/java/org/opensearch/dataprepper/plugins/sink/sqs/SqsSinkIT.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.junit.jupiter.api.extension.ExtendWith;
1818
import org.mockito.Mock;
1919
import org.mockito.junit.jupiter.MockitoExtension;
20-
import static org.mockito.Mockito.doAnswer;
2120
import static org.junit.jupiter.api.Assertions.assertNotNull;
2221
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
2322
import org.opensearch.dataprepper.metrics.PluginMetrics;
@@ -574,12 +573,6 @@ void TestSinkOperationWithQueuesAsExpression() throws Exception {
574573
when(thresholdConfig.getMaxEventsPerMessage()).thenReturn(1);
575574
when(sqsSinkConfig.getDlq()).thenReturn(dlqConfig);
576575
when(expressionEvaluator.isValidFormatExpression(anyString())).thenReturn(true);
577-
doAnswer( (a) -> {
578-
String qUrl = (String)a.getArgument(0);
579-
Event event = (Event)a.getArgument(1);
580-
int idx = qUrl.indexOf("${");
581-
return qUrl.substring(0, idx)+event.get("id", String.class);
582-
}).when(expressionEvaluator).evaluate(anyString(), any(Event.class));
583576

584577
when(sqsSinkConfig.getQueueUrl()).thenReturn(queueUrl+"${/id}");
585578

data-prepper-plugins/sqs-sink/src/main/java/org/opensearch/dataprepper/plugins/sink/sqs/SqsSink.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.slf4j.LoggerFactory;
3636

3737
import java.time.Duration;
38+
import java.util.Map;
3839
import java.util.Collection;
3940

4041
@Experimental
@@ -59,8 +60,18 @@ public SqsSink(final PluginSetting pluginSetting,
5960
this.sqsSinkConfig = sqsSinkConfig;
6061
sinkInitialized = false;
6162
final PluginModel codecConfiguration = sqsSinkConfig.getCodec();
62-
final PluginSetting codecPluginSettings = new PluginSetting(codecConfiguration.getPluginName(),
63+
final PluginSetting codecPluginSettings;
64+
if (codecConfiguration != null) {
65+
String codecPluginName = codecConfiguration.getPluginName();
66+
if (!codecPluginName.equals("json") && !codecPluginName.equals("ndjson")) {
67+
throw new RuntimeException(String.format("Codec {} not supported.", codecPluginName));
68+
}
69+
codecPluginSettings = new PluginSetting(codecConfiguration.getPluginName(),
6370
codecConfiguration.getPluginSettings());
71+
} else {
72+
codecPluginSettings = new PluginSetting("ndjson", Map.of());
73+
}
74+
6475
final OutputCodec outputCodec = pluginFactory.loadPlugin(OutputCodec.class, codecPluginSettings);
6576
AwsConfig awsConfig = sqsSinkConfig.getAwsConfig();
6677
if (awsConfig == null && awsCredentialsSupplier == null) {

data-prepper-plugins/sqs-sink/src/main/java/org/opensearch/dataprepper/plugins/sink/sqs/SqsSinkConfig.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class SqsSinkConfig {
2727
private String queueUrl;
2828

2929
@JsonProperty("codec")
30-
@NotNull
3130
private PluginModel codec;
3231

3332
@JsonProperty("threshold")
@@ -45,21 +44,33 @@ public class SqsSinkConfig {
4544
@JsonProperty("dlq")
4645
private PluginModel dlq;
4746

48-
@AssertTrue(message = "FIFO queues wth dynamic group id or dynamic deduplication id and more than one events per message is not valid")
47+
@AssertTrue(message = "FIFO queues wth dynamic group id or dynamic deduplication id and more than one events per message is not valid OR standard queues do not support groupId or deduplication configuration")
4948
boolean isValidConfig() {
5049
String deDupId = getDeDuplicationId();
5150
String groupId = getGroupId();
51+
String queueUrl = getQueueUrl();
5252
boolean isDynamicDeDupId = deDupId != null && deDupId.contains("${");
5353
boolean isDynamicGroupId = groupId != null && groupId.contains("${");
54+
boolean isDynamicQueueUrl = queueUrl != null && queueUrl.contains("${");
55+
if (isDynamicQueueUrl) {
56+
return true;
57+
}
5458
if (getQueueUrl().endsWith(".fifo")) {
5559
if ((isDynamicGroupId || isDynamicDeDupId) && thresholdConfig.getMaxEventsPerMessage() > 1) {
5660
return false;
5761
} else{
5862
return true;
5963
}
64+
} else {
65+
return (groupId == null && deDupId == null);
6066
}
61-
return true;
6267
}
6368

69+
@AssertTrue(message = "ndjson codec (default codec) doesn't support max events per message greater than 1")
70+
boolean isValidCondecConfig() {
71+
if ((codec == null || codec.getPluginName().equals("ndjson")) && thresholdConfig.getMaxEventsPerMessage() > 1)
72+
return false;
73+
return true;
74+
}
6475
}
6576

data-prepper-plugins/sqs-sink/src/main/java/org/opensearch/dataprepper/plugins/sink/sqs/SqsSinkService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public SqsSinkService(final SqsSinkConfig sqsSinkConfig,
9797
isDynamicDeDupId = deDupId != null && deDupId.contains("${");
9898
if (isDynamicDeDupId) {
9999
if (!expressionEvaluator.isValidFormatExpression(deDupId)) {
100-
throw new IllegalArgumentException("Invalid groupId expression");
100+
throw new IllegalArgumentException("Invalid deduplicationId expression");
101101
}
102102
}
103103

@@ -199,7 +199,7 @@ private String getQueueUrl(final Event event, boolean logError) {
199199
String qUrl = queueUrl;
200200
if (isDynamicQueueUrl) {
201201
try {
202-
Object obj = expressionEvaluator.evaluate(queueUrl, event);
202+
Object obj = event.formatString(queueUrl, expressionEvaluator);
203203
if (obj instanceof String) {
204204
qUrl = (String) obj;
205205
} else {
@@ -220,7 +220,7 @@ private String getGroupId(final Event event) {
220220
String gId = groupId;
221221
if (isDynamicGroupId) {
222222
try {
223-
Object obj = expressionEvaluator.evaluate(groupId, event);
223+
Object obj = event.formatString(groupId, expressionEvaluator);
224224
if (obj instanceof String) {
225225
gId = (String) obj;
226226
} else {
@@ -237,7 +237,7 @@ private String getDeDupId(final Event event) {
237237
String ddId = deDupId;
238238
if (isDynamicDeDupId) {
239239
try {
240-
Object obj = expressionEvaluator.evaluate(deDupId, event);
240+
Object obj = event.formatString(deDupId, expressionEvaluator);
241241
if (obj instanceof String) {
242242
ddId = (String) obj;
243243
} else {

data-prepper-plugins/sqs-sink/src/test/java/org/opensearch/dataprepper/plugins/sink/sqs/SqsSinkTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.UUID;
5050

5151
public class SqsSinkTest {
52+
private static final String TEST_CODEC_PLUGIN_NAME = "json";
5253
private static final String TEST_PLUGIN_NAME = "testPluginName";
5354
private static final String TEST_PIPELINE_NAME = "testPipelineName";
5455
@Mock
@@ -90,7 +91,7 @@ void setup() {
9091
awsCredentialsProvider = mock(AwsCredentialsProvider.class);
9192
when(sqsSinkConfig.getDlq()).thenReturn(null);
9293
codecConfig = mock(PluginModel.class);
93-
when(codecConfig.getPluginName()).thenReturn(TEST_PLUGIN_NAME);
94+
when(codecConfig.getPluginName()).thenReturn(TEST_CODEC_PLUGIN_NAME);
9495
when(codecConfig.getPluginSettings()).thenReturn(Map.of());
9596
when(sqsSinkConfig.getCodec()).thenReturn(codecConfig);
9697
queueUrl = UUID.randomUUID().toString();

0 commit comments

Comments
 (0)