Skip to content
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

Aggregator processor should evaluate aggregate_when condition before forwarding events to remote peer #4004

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/

package org.opensearch.dataprepper.model.peerforwarder;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.record.Record;

import java.util.Collection;

Expand All @@ -18,4 +20,15 @@ public interface RequiresPeerForwarding {
* @return A set of keys
*/
Collection<String> getIdentificationKeys();

/**
* Returns events that are applicable for peer forwarding.
*
* @param records collection of input records
*
* @return a collection of output records
*/
default Collection<Record<Event>> applicableEventsForPeerForwarding(Collection<Record<Event>> records) {
return records;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.model.peerforwarder;

import org.junit.jupiter.api.Test;
import org.opensearch.dataprepper.model.record.Record;
import org.opensearch.dataprepper.model.event.Event;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;

import java.util.Collection;

class RequiresPeerForwardingTest {

public class SimpleRequiresPeerForwarding implements RequiresPeerForwarding {
@Override
public Collection<String> getIdentificationKeys() {
return null;
}
}

@Test
void testRequiresPeerForwardingTest() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding this test!

Collection<Record<Event>> records = mock(Collection.class);
RequiresPeerForwarding requiresPeerForwarding = new SimpleRequiresPeerForwarding();
assertThat(requiresPeerForwarding.applicableEventsForPeerForwarding(records), equalTo(records));
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -79,7 +80,9 @@ private PeerForwardingProcessorDecorator(final PeerForwarder peerForwarder, fina

@Override
public Collection<Record<Event>> execute(final Collection<Record<Event>> records) {
final Collection<Record<Event>> recordsToProcessOnLocalPeer = peerForwarder.forwardRecords(records);
final Collection<Record<Event>> recordsToProcess = ((RequiresPeerForwarding)innerProcessor).applicableEventsForPeerForwarding(records);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end of this method, we return:

return innerProcessor.execute(recordsToProcessLocally);

The collection returned by the processor will be the input into the next processor. With this PR, the returned collection is going to be inaccurate when aggregate_when is present. The pipeline author still wants to process those events in downstream processors.

We need something like the following. I'll use + for set union and - for set difference. This should convey the idea, but these are not valid set operators in Java, so you'll need a little modification. Also, Collection is not necessarily a Set.

return innerProcessor.execute(recordsToProcessLocally) + (records - recordsToProcess);

We'll definitely need a unit test here.

It would be ideal to also have a core integration test.

final Collection<Record<Event>> recordsToProcessOnLocalPeer = peerForwarder.forwardRecords(recordsToProcess);

final Collection<Record<Event>> receivedRecordsFromBuffer = peerForwarder.receiveRecords();

final Collection<Record<Event>> recordsToProcessLocally = CollectionUtils.union(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public void run() {
}
}

private void processAcknowledgements(List<Event> inputEvents, Collection outputRecords) {
Set<Event> outputEventsSet = ((ArrayList<Record<Event>>)outputRecords).stream().map(Record::getData).collect(Collectors.toSet());
private void processAcknowledgements(List<Event> inputEvents, Collection<Record<Event>> outputRecords) {
Set<Event> outputEventsSet = outputRecords.stream().map(Record::getData).collect(Collectors.toSet());
// For each event in the input events list that is not present in the output events, send positive acknowledgement, if acknowledgements are enabled for it
inputEvents.forEach(event -> {
EventHandle eventHandle = event.getEventHandle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micrometer.core.instrument.Counter;
import org.opensearch.dataprepper.plugins.hasher.IdentificationKeysHasher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -148,6 +149,21 @@ public void shutdown() {

}

@Override
public Collection<Record<Event>> applicableEventsForPeerForwarding(Collection<Record<Event>> records) {
if (whenCondition == null) {
return records;
}
final Collection<Record<Event>> recordsOut = new ArrayList<>();
for (Record<Event> record: records) {
Event event = record.getData();
if (expressionEvaluator.evaluateConditional(whenCondition, event)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's share the same logic here as we use in the execute method to ensure they remain consistent.

Maybe you could make a method that evaluates the conditional in both.

private boolean isEventApplicable(Record<Event> record)

recordsOut.add(record);
}
}
return recordsOut;
}

@Override
public Collection<String> getIdentificationKeys() {
return aggregateProcessorConfig.getIdentificationKeys();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ void handleEvent_returning_with_condition_eliminates_one_record() {
recordsIn.add(new Record<Event>(secondEvent));
recordsIn.add(new Record<Event>(event));
Collection<Record<Event>> c = recordsIn;
Collection<Record<Event>> applicableRecords = objectUnderTest.applicableEventsForPeerForwarding(recordsIn);
assertThat(applicableRecords.size(), equalTo(2));
final List<Record<Event>> recordsOut = (List<Record<Event>>) objectUnderTest.doExecute(c);

assertThat(recordsOut.size(), equalTo(2));
Expand Down
Loading