-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sqs source: json codec support to split sqs message into multiple events #5330
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.sqs; | ||
|
||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.EventMetadata; | ||
import software.amazon.awssdk.services.sqs.model.Message; | ||
import software.amazon.awssdk.services.sqs.model.MessageAttributeValue; | ||
import software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class AttributeHandler { | ||
public static Map<String, String> collectMetadataAttributes(final Message message, final String queueUrl) { | ||
final Map<String, String> metadataMap = new HashMap<>(); | ||
metadataMap.put("queueUrl", queueUrl); | ||
|
||
for (Map.Entry<MessageSystemAttributeName, String> entry : message.attributes().entrySet()) { | ||
String originalKey = entry.getKey().toString(); | ||
String key = originalKey.substring(0, 1).toLowerCase() + originalKey.substring(1); | ||
metadataMap.put(key, entry.getValue()); | ||
} | ||
|
||
for (Map.Entry<String, MessageAttributeValue> entry : message.messageAttributes().entrySet()) { | ||
String originalKey = entry.getKey().toString(); | ||
String key = originalKey.substring(0, 1).toLowerCase() + originalKey.substring(1); | ||
metadataMap.put(key, entry.getValue().stringValue()); | ||
} | ||
return metadataMap; | ||
} | ||
|
||
public static void applyMetadataAttributes(final Event event, final Map<String, String> attributes) { | ||
final EventMetadata metadata = event.getMetadata(); | ||
attributes.forEach(metadata::setAttribute); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.sqs; | ||
|
||
import org.opensearch.dataprepper.model.codec.InputCodec; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import java.io.ByteArrayInputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
public class JsonBulkMessageFieldStrategy implements MessageFieldStrategy { | ||
|
||
private final InputCodec codec; | ||
|
||
public JsonBulkMessageFieldStrategy(final InputCodec codec) { | ||
this.codec = codec; | ||
} | ||
|
||
@Override | ||
public List<Event> parseEvents(final String messageBody) { | ||
final List<Event> events = new ArrayList<>(); | ||
final ByteArrayInputStream inputStream = new ByteArrayInputStream(messageBody.getBytes(StandardCharsets.UTF_8)); | ||
try { | ||
codec.parse(inputStream, (Consumer<Record<Event>>) record -> events.add(record.getData())); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to parse events from SQS body.", e); | ||
} | ||
return events; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.sqs; | ||
|
||
import org.opensearch.dataprepper.model.event.Event; | ||
import java.util.List; | ||
|
||
public interface MessageFieldStrategy { | ||
// Convert the SQS message body into one or more events. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this into a Javadoc comment. |
||
List<Event> parseEvents(String messageBody); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.sqs; | ||
|
@@ -9,6 +14,10 @@ | |
import org.opensearch.dataprepper.common.concurrent.BackgroundThreadFactory; | ||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSetManager; | ||
import org.opensearch.dataprepper.model.codec.InputCodec; | ||
import org.opensearch.dataprepper.model.configuration.PluginModel; | ||
import org.opensearch.dataprepper.model.configuration.PluginSetting; | ||
import org.opensearch.dataprepper.model.plugin.PluginFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
|
@@ -35,9 +44,9 @@ public class SqsService { | |
static final double JITTER_RATE = 0.20; | ||
|
||
private final SqsSourceConfig sqsSourceConfig; | ||
private final SqsEventProcessor sqsEventProcessor; | ||
private final SqsClient sqsClient; | ||
private final PluginMetrics pluginMetrics; | ||
private final PluginFactory pluginFactory; | ||
private final AcknowledgementSetManager acknowledgementSetManager; | ||
private final List<ExecutorService> allSqsUrlExecutorServices; | ||
private final List<SqsWorker> sqsWorkers; | ||
|
@@ -46,13 +55,13 @@ public class SqsService { | |
public SqsService(final Buffer<Record<Event>> buffer, | ||
final AcknowledgementSetManager acknowledgementSetManager, | ||
final SqsSourceConfig sqsSourceConfig, | ||
final SqsEventProcessor sqsEventProcessor, | ||
final PluginMetrics pluginMetrics, | ||
final PluginFactory pluginFactory, | ||
final AwsCredentialsProvider credentialsProvider) { | ||
|
||
this.sqsSourceConfig = sqsSourceConfig; | ||
this.sqsEventProcessor = sqsEventProcessor; | ||
this.pluginMetrics = pluginMetrics; | ||
this.pluginFactory = pluginFactory; | ||
this.acknowledgementSetManager = acknowledgementSetManager; | ||
this.allSqsUrlExecutorServices = new ArrayList<>(); | ||
this.sqsWorkers = new ArrayList<>(); | ||
|
@@ -70,8 +79,18 @@ public void start() { | |
sqsSourceConfig.getQueues().forEach(queueConfig -> { | ||
String queueUrl = queueConfig.getUrl(); | ||
String queueName = queueUrl.substring(queueUrl.lastIndexOf('/') + 1); | ||
|
||
int numWorkers = queueConfig.getNumWorkers(); | ||
SqsEventProcessor sqsEventProcessor; | ||
if (queueConfig.getCodec() != null) { | ||
final PluginModel codecConfiguration = queueConfig.getCodec(); | ||
final PluginSetting codecPluginSettings = new PluginSetting(codecConfiguration.getPluginName(), codecConfiguration.getPluginSettings()); | ||
final InputCodec codec = pluginFactory.loadPlugin(InputCodec.class, codecPluginSettings); | ||
MessageFieldStrategy bulkStrategy = new JsonBulkMessageFieldStrategy(codec); | ||
sqsEventProcessor = new SqsEventProcessor(new RawSqsMessageHandler(bulkStrategy)); | ||
} else { | ||
MessageFieldStrategy standardStrategy = new StandardMessageFieldStrategy(); | ||
sqsEventProcessor = new SqsEventProcessor(new RawSqsMessageHandler(standardStrategy)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line and line 89 are the same. Refactor to use the same line. Just get the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that makes sense, i made the change |
||
} | ||
ExecutorService executorService = Executors.newFixedThreadPool( | ||
numWorkers, BackgroundThreadFactory.defaultExecutorThreadFactory("sqs-source" + queueName)); | ||
allSqsUrlExecutorServices.add(executorService); | ||
|
@@ -80,10 +99,10 @@ public void start() { | |
buffer, | ||
acknowledgementSetManager, | ||
sqsClient, | ||
sqsEventProcessor, | ||
sqsSourceConfig, | ||
queueConfig, | ||
pluginMetrics, | ||
sqsEventProcessor, | ||
backoff)) | ||
.collect(Collectors.toList()); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just rename this to
CodecBulkMessageFieldStrategy
because it is not bound to JSON.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done