exceptionfactory commented on code in PR #10053:
URL: https://github.com/apache/nifi/pull/10053#discussion_r2248924987


##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/main/java/org/apache/nifi/processors/aws/kinesis/stream/ConsumeKinesisStream.java:
##########
@@ -319,6 +320,15 @@ public class ConsumeKinesisStream extends 
AbstractAwsAsyncProcessor<KinesisAsync
             .dependsOn(RECORD_WRITER)
             .build();
 
+    public static final PropertyDescriptor 
FLOW_FILE_HANDLING_ON_SCHEMA_CHANGE_STRATEGY = new PropertyDescriptor.Builder()
+            .name("FlowFile Handling On Schema Change Strategy")

Review Comment:
   The name can be adjusted along the lines of the enum.
   ```suggestion
               .name("FlowFile Handling On Schema Difference")
   ```



##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/main/java/org/apache/nifi/processors/aws/kinesis/property/SchemaDifferenceHandlingStrategy.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.aws.kinesis.property;
+
+import org.apache.nifi.components.DescribedValue;
+
+public enum SchemaDifferenceHandlingStrategy implements DescribedValue {
+    ROLL_FLOW_FILES("Roll FlowFile", "Create a new FlowFile for each record 
with a different schema. The previous FlowFile will be completed with the 
records that have the previous schema. Emitted" +

Review Comment:
   Perhaps `CREATE_FLOW_FILE` is clearer based on the description, and the enum 
name should be singular to match the label.
   ```suggestion
       CREATE_FLOW_FILE("Roll FlowFile", "Create a new FlowFile for each record 
with a different schema. The previous FlowFile will be completed with the 
records that have the previous schema. Emitted" +
   ```



##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/main/java/org/apache/nifi/processors/aws/kinesis/property/SchemaDifferenceHandlingStrategy.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.aws.kinesis.property;
+
+import org.apache.nifi.components.DescribedValue;
+
+public enum SchemaDifferenceHandlingStrategy implements DescribedValue {
+    ROLL_FLOW_FILES("Roll FlowFile", "Create a new FlowFile for each record 
with a different schema. The previous FlowFile will be completed with the 
records that have the previous schema. Emitted" +
+            " FlowFiles will contain continuous record sequences."),
+    GROUP_FLOW_FILES("Group Records By Schema", "Group records with the same 
schema into a single FlowFile. If a record with a different schema is 
encountered, a new FlowFile will be created for" +

Review Comment:
   The enum name should align more closely to the label, such as 
`GROUP_RECORDS` or `GROUP_RECORDS_BY_SCHEMA`.
   ```suggestion
       GROUP_RECORDS("Group Records By Schema", "Group records with the same 
schema into a single FlowFile. If a record with a different schema is 
encountered, a new FlowFile will be created for" +
   ```



##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/main/java/org/apache/nifi/processors/aws/kinesis/stream/record/StateHandlerStrategy.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.aws.kinesis.stream.record;
+
+import 
org.apache.nifi.processors.aws.kinesis.property.SchemaDifferenceHandlingStrategy;
+import 
org.apache.nifi.processors.aws.kinesis.stream.record.AbstractKinesisRecordProcessor.BatchProcessingContext;
+import 
org.apache.nifi.processors.aws.kinesis.stream.record.KinesisRecordProcessorRecord.FlowFileCompletionException;
+import 
org.apache.nifi.processors.aws.kinesis.stream.record.KinesisRecordProcessorRecord.FlowFileState;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.serialization.record.Record;
+import org.apache.nifi.serialization.record.RecordSchema;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * StateHandlerStrategy is responsible for managing the state of FlowFiles 
created for Records processed in a single batch. In general this class decides 
what should happen when a new RecordSchema
+ * is encountered — whether previous State should be completed and new state 
created or only a new state should be created.
+ */
+class StateHandlerStrategy {
+
+    private final SchemaDifferenceHandlingStrategy strategy;
+    private final StateInitializerAction stateInitializerAction;
+    private final StateFinalizerAction stateFinalizerAction;
+    private final Map<RecordSchema, FlowFileState> activeStateMap = new 
HashMap<>();
+
+    StateHandlerStrategy(final SchemaDifferenceHandlingStrategy strategy, 
final StateInitializerAction stateInitializerAction, final StateFinalizerAction 
stateFinalizerAction) {
+        this.strategy = strategy;
+        this.stateInitializerAction = stateInitializerAction;
+        this.stateFinalizerAction = stateFinalizerAction;
+    }
+
+    FlowFileState getOrCreate(final Record record, final 
BatchProcessingContext flowFileContext) throws FlowFileCompletionException, 
IOException, SchemaNotFoundException {
+        return switch (strategy) {
+            case ROLL_FLOW_FILES -> getOrFinalizeAndCreateNewState(record, 
flowFileContext);
+            case GROUP_FLOW_FILES -> getOrCreateNewState(record, 
flowFileContext);
+        };
+    }
+
+    private FlowFileState getOrFinalizeAndCreateNewState(final Record record, 
final BatchProcessingContext flowFileContext) throws 
FlowFileCompletionException, IOException, SchemaNotFoundException {
+        final FlowFileState previousState = 
activeStateMap.get(record.getSchema());
+        if (previousState != null) {
+            return previousState;
+        }
+        final FlowFileState previousStateForDifferentSchema = pop();
+        if (previousStateForDifferentSchema != null) {
+            stateFinalizerAction.complete(previousStateForDifferentSchema, 
flowFileContext);
+        }
+        final FlowFileState newState = stateInitializerAction.init(record, 
flowFileContext);
+        activeStateMap.put(record.getSchema(), newState);
+        return newState;
+    }
+
+    private FlowFileState getOrCreateNewState(final Record record, final 
BatchProcessingContext flowFileContext) throws IOException, 
SchemaNotFoundException {
+        final FlowFileState previousState = 
activeStateMap.get(record.getSchema());
+        if (previousState != null) {
+            return previousState;
+        }
+        final FlowFileState newState = stateInitializerAction.init(record, 
flowFileContext);
+        activeStateMap.put(record.getSchema(), newState);
+        return newState;
+    }

Review Comment:
   The two different methods appear to be the same, except for the 
`stateFinalizerAction` call, I recommend collapsing them and just checking for 
the `Roll FlowFiles` strategy, unless there is some other key difference I am 
missing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to