|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + * |
| 9 | + */ |
| 10 | +package org.opensearch.dataprepper.plugins.source.s3; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 13 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 | +import org.opensearch.dataprepper.expression.ExpressionEvaluator; |
| 15 | +import org.opensearch.dataprepper.model.codec.InputCodec; |
| 16 | +import org.opensearch.dataprepper.model.configuration.PluginModel; |
| 17 | +import org.opensearch.dataprepper.model.configuration.PluginSetting; |
| 18 | +import org.opensearch.dataprepper.model.event.Event; |
| 19 | +import org.opensearch.dataprepper.model.event.JacksonEvent; |
| 20 | +import org.opensearch.dataprepper.model.plugin.PluginFactory; |
| 21 | +import org.opensearch.dataprepper.plugins.source.s3.configuration.S3ScanProcessingCondition; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | +import software.amazon.awssdk.core.ResponseInputStream; |
| 25 | +import software.amazon.awssdk.services.s3.S3Client; |
| 26 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 27 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 28 | +import software.amazon.awssdk.services.s3.model.NoSuchKeyException; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.util.Collection; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.IdentityHashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Optional; |
| 38 | +import java.util.concurrent.atomic.AtomicReference; |
| 39 | + |
| 40 | +/** |
| 41 | + * Evaluates {@link S3ScanProcessingCondition} entries for a given S3 object before |
| 42 | + * the object is processed. For each applicable condition the evaluator downloads the |
| 43 | + * condition object co-located with the S3 object and evaluates the {@code when} expression |
| 44 | + * against its content. When a {@code codec} is configured on the condition the object is |
| 45 | + * parsed with that codec; otherwise the object is parsed as a JSON document. |
| 46 | + */ |
| 47 | +public class S3ScanProcessingConditionEvaluator { |
| 48 | + |
| 49 | + private static final Logger LOG = LoggerFactory.getLogger(S3ScanProcessingConditionEvaluator.class); |
| 50 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 51 | + |
| 52 | + private final S3Client s3Client; |
| 53 | + private final ExpressionEvaluator expressionEvaluator; |
| 54 | + private final Map<S3ScanProcessingCondition, InputCodec> codecCache; |
| 55 | + |
| 56 | + public S3ScanProcessingConditionEvaluator(final S3Client s3Client, |
| 57 | + final ExpressionEvaluator expressionEvaluator, |
| 58 | + final PluginFactory pluginFactory, |
| 59 | + final Collection<S3ScanProcessingCondition> allConditions) { |
| 60 | + this.s3Client = s3Client; |
| 61 | + this.expressionEvaluator = expressionEvaluator; |
| 62 | + final IdentityHashMap<S3ScanProcessingCondition, InputCodec> cache = new IdentityHashMap<>(); |
| 63 | + for (final S3ScanProcessingCondition condition : allConditions) { |
| 64 | + if (condition.getCodec() != null) { |
| 65 | + final PluginModel codecModel = condition.getCodec(); |
| 66 | + final PluginSetting pluginSetting = new PluginSetting( |
| 67 | + codecModel.getPluginName(), codecModel.getPluginSettings()); |
| 68 | + cache.put(condition, pluginFactory.loadPlugin(InputCodec.class, pluginSetting)); |
| 69 | + } |
| 70 | + } |
| 71 | + this.codecCache = Collections.unmodifiableMap(cache); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the first condition in {@code conditions} that is not yet satisfied for the given |
| 76 | + * object, or {@link Optional#empty()} if all applicable conditions are met. A condition is |
| 77 | + * applicable when {@code applicable_prefix} is absent or the object key starts with at least |
| 78 | + * one of the listed applicable prefixes. |
| 79 | + */ |
| 80 | + public Optional<S3ScanProcessingCondition> firstUnmetCondition(final String bucket, |
| 81 | + final String objectKey, |
| 82 | + final List<S3ScanProcessingCondition> conditions) { |
| 83 | + if (conditions == null || conditions.isEmpty()) { |
| 84 | + return Optional.empty(); |
| 85 | + } |
| 86 | + for (final S3ScanProcessingCondition condition : conditions) { |
| 87 | + if (!isApplicable(objectKey, condition)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + final String conditionObjectKey = resolveConditionObjectKey(objectKey, condition.getObjectName()); |
| 91 | + try { |
| 92 | + final Event event; |
| 93 | + final InputCodec codec = codecCache.get(condition); |
| 94 | + if (codec != null) { |
| 95 | + event = parseFirstEventWithCodec(bucket, conditionObjectKey, codec); |
| 96 | + } else { |
| 97 | + final String content = readS3ObjectAsString(bucket, conditionObjectKey); |
| 98 | + final Map<String, Object> data = OBJECT_MAPPER.readValue( |
| 99 | + content, new TypeReference<Map<String, Object>>() {}); |
| 100 | + event = JacksonEvent.builder() |
| 101 | + .withEventType("event") |
| 102 | + .withData(data) |
| 103 | + .build(); |
| 104 | + } |
| 105 | + if (event == null || !expressionEvaluator.evaluateConditional(condition.getWhen(), event)) { |
| 106 | + LOG.debug("Processing condition '{}' not satisfied for {}/{} using condition object {}", |
| 107 | + condition.getWhen(), bucket, objectKey, conditionObjectKey); |
| 108 | + return Optional.of(condition); |
| 109 | + } |
| 110 | + } catch (final NoSuchKeyException e) { |
| 111 | + LOG.debug("Object for condition {}/{} not found yet, condition not met", bucket, conditionObjectKey); |
| 112 | + return Optional.of(condition); |
| 113 | + } catch (final Exception e) { |
| 114 | + LOG.warn("Error reading or evaluating processing condition for {}/{}, processing object as-is", |
| 115 | + bucket, objectKey, e); |
| 116 | + return Optional.empty(); |
| 117 | + } |
| 118 | + } |
| 119 | + return Optional.empty(); |
| 120 | + } |
| 121 | + |
| 122 | + private boolean isApplicable(final String objectKey, final S3ScanProcessingCondition condition) { |
| 123 | + final List<String> includePrefix = condition.getApplicablePrefix(); |
| 124 | + return includePrefix == null || includePrefix.isEmpty() || |
| 125 | + includePrefix.stream().anyMatch(objectKey::startsWith); |
| 126 | + } |
| 127 | + |
| 128 | + private String resolveConditionObjectKey(final String objectKey, final String fileName) { |
| 129 | + final String directory = objectKey.contains("/") |
| 130 | + ? objectKey.substring(0, objectKey.lastIndexOf('/') + 1) |
| 131 | + : ""; |
| 132 | + return directory + fileName; |
| 133 | + } |
| 134 | + |
| 135 | + private Event parseFirstEventWithCodec(final String bucket, |
| 136 | + final String key, |
| 137 | + final InputCodec codec) throws IOException { |
| 138 | + final AtomicReference<Event> firstEvent = new AtomicReference<>(); |
| 139 | + final GetObjectRequest request = GetObjectRequest.builder().bucket(bucket).key(key).build(); |
| 140 | + try (final ResponseInputStream<GetObjectResponse> response = s3Client.getObject(request)) { |
| 141 | + codec.parse(response, record -> firstEvent.compareAndSet(null, record.getData())); |
| 142 | + } |
| 143 | + return firstEvent.get(); |
| 144 | + } |
| 145 | + |
| 146 | + private String readS3ObjectAsString(final String bucket, final String key) throws IOException { |
| 147 | + final GetObjectRequest request = GetObjectRequest.builder().bucket(bucket).key(key).build(); |
| 148 | + try (final ResponseInputStream<GetObjectResponse> response = s3Client.getObject(request)) { |
| 149 | + return new String(response.readAllBytes(), StandardCharsets.UTF_8); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments