-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of conditional routing of sinks.
Signed-off-by: David Venable <[email protected]>
- Loading branch information
Showing
19 changed files
with
1,344 additions
and
59 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
data-prepper-core/src/main/java/org/opensearch/dataprepper/parser/DataFlowComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.parser; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* Represents a part of the pipeline through which data flows. This includes | ||
* {@link com.amazon.dataprepper.model.sink.Sink} and {@link com.amazon.dataprepper.model.processor.Processor}. | ||
* | ||
* @param <T> The type of component. | ||
*/ | ||
public class DataFlowComponent<T> { | ||
private final T component; | ||
private final Set<String> routes; | ||
|
||
DataFlowComponent(final T component, final Collection<String> routes) { | ||
this.component = Objects.requireNonNull(component); | ||
this.routes = new HashSet<>(Objects.requireNonNull(routes)); | ||
} | ||
|
||
public T getComponent() { | ||
return component; | ||
} | ||
|
||
public Set<String> getRoutes() { | ||
return routes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ore/src/main/java/org/opensearch/dataprepper/pipeline/router/DataFlowComponentRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.pipeline.router; | ||
|
||
import org.opensearch.dataprepper.model.record.Record; | ||
import org.opensearch.dataprepper.parser.DataFlowComponent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* Package-protected utility to route for a single {@link DataFlowComponent}. This is | ||
* intended to help break apart {@link Router} for better testing. | ||
*/ | ||
class DataFlowComponentRouter { | ||
|
||
<C> void route(final Collection<Record> allRecords, | ||
final DataFlowComponent<C> dataFlowComponent, | ||
final Map<Record, Set<String>> recordsToRoutes, | ||
final BiConsumer<C, Collection<Record>> componentRecordsConsumer) { | ||
|
||
final Collection<Record> recordsForComponent; | ||
if (dataFlowComponent.getRoutes().isEmpty()) { | ||
recordsForComponent = allRecords; | ||
} else { | ||
recordsForComponent = new ArrayList<>(); | ||
for (Record event : allRecords) { | ||
final Set<String> routesForEvent = recordsToRoutes | ||
.getOrDefault(event, Collections.emptySet()); | ||
|
||
boolean routed = false; | ||
for (String route : dataFlowComponent.getRoutes()) { | ||
if (routesForEvent.contains(route)) { | ||
routed = true; | ||
break; | ||
} | ||
} | ||
|
||
if (routed) { | ||
recordsForComponent.add(event); | ||
} | ||
} | ||
|
||
} | ||
componentRecordsConsumer.accept(dataFlowComponent.getComponent(), recordsForComponent); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...er-core/src/main/java/org/opensearch/dataprepper/pipeline/router/RouteEventEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.pipeline.router; | ||
|
||
import org.opensearch.dataprepper.model.configuration.ConditionalRoute; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import org.opensearch.dataprepper.expression.ExpressionEvaluator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
class RouteEventEvaluator { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RouteEventEvaluator.class); | ||
|
||
private final ExpressionEvaluator<Boolean> evaluator; | ||
private final Collection<ConditionalRoute> routes; | ||
|
||
RouteEventEvaluator(final ExpressionEvaluator<Boolean> evaluator, final Collection<ConditionalRoute> routes) { | ||
this.evaluator = evaluator; | ||
this.routes = routes; | ||
} | ||
|
||
Map<Record, Set<String>> evaluateEventRoutes(final Collection<Record> records) { | ||
final Map<Record, Set<String>> recordsToRoutes = new HashMap<>(); | ||
|
||
int nonEventRecords = 0; | ||
|
||
for (Record record : records) { | ||
|
||
final Object data = record.getData(); | ||
|
||
if(data instanceof Event) { | ||
|
||
final Event event = (Event) data; | ||
|
||
recordsToRoutes.put(record, new HashSet<>()); | ||
|
||
for (ConditionalRoute route : routes) { | ||
Boolean routed; | ||
try { | ||
routed = evaluator.evaluate(route.getCondition(), event); | ||
} catch (final Exception ex) { | ||
routed = false; | ||
LOG.error("Failed to evaluate route. This route will not be applied to any events.", ex); | ||
} | ||
if (routed) { | ||
recordsToRoutes | ||
.get(record) | ||
.add(route.getName()); | ||
} | ||
} | ||
} else { | ||
nonEventRecords++; | ||
recordsToRoutes.put(record, Collections.emptySet()); | ||
} | ||
} | ||
|
||
if(nonEventRecords > 0) { | ||
LOG.warn("Received {} records which are not events. These will have no routes applied.", nonEventRecords); | ||
} | ||
|
||
return recordsToRoutes; | ||
} | ||
} |
Oops, something went wrong.