Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.cs.tests.system;

import org.apache.nifi.annotation.behavior.Stateful;
import org.apache.nifi.annotation.lifecycle.OnEnabled;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.state.Scope;
import org.apache.nifi.components.state.StateManager;
import org.apache.nifi.components.state.StateMap;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.controller.ConfigurationContext;
import org.apache.nifi.processor.util.StandardValidators;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Store whose contents live in the component's local state, so that they are subject to the framework's own state
* lifecycle. The store records when it was first created and how many rows it holds.
*
* Removing a Controller Service clears its state, so a service that was torn down and recreated comes back with a
* later creation timestamp and no rows. Comparing the creation timestamp across an operation therefore distinguishes
* a preserved service from one that was replaced, even when the replacement carries the same identifier.
*/
@Stateful(scopes = Scope.LOCAL, description = "Holds the creation timestamp of the store and the number of rows written to it.")
public class StateBackedStoreService extends AbstractControllerService implements StoreService {

static final PropertyDescriptor STORE_NAME = new PropertyDescriptor.Builder()
.name("Store Name")
.required(true)
.defaultValue("store")
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();

static final String CREATED_KEY = "created";
static final String ROW_COUNT_KEY = "rowCount";
static final String LAST_ROW_KEY = "lastRow";

private static final List<PropertyDescriptor> PROPERTIES = List.of(STORE_NAME);

@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return PROPERTIES;
}

/**
* Establishes the store on first enable and leaves it alone afterwards, so that the creation timestamp survives
* the disable and re-enable cycle that a version change performs.
*/
@OnEnabled
public void onEnabled(final ConfigurationContext context) throws IOException {
final StateManager stateManager = getStateManager();
final StateMap stateMap = stateManager.getState(Scope.LOCAL);
if (stateMap.get(CREATED_KEY) != null) {
return;
}

final Map<String, String> state = new HashMap<>();
state.put(CREATED_KEY, String.valueOf(System.currentTimeMillis()));
state.put(ROW_COUNT_KEY, "0");
stateManager.setState(state, Scope.LOCAL);
}

@Override
public void append(final String row) {
try {
final StateManager stateManager = getStateManager();
final Map<String, String> state = new HashMap<>(stateManager.getState(Scope.LOCAL).toMap());
final long rowCount = Long.parseLong(state.getOrDefault(ROW_COUNT_KEY, "0"));
state.put(ROW_COUNT_KEY, String.valueOf(rowCount + 1));
state.put(LAST_ROW_KEY, row);
stateManager.setState(state, Scope.LOCAL);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.cs.tests.system;

import org.apache.nifi.controller.ControllerService;

/**
* A store that accumulates rows and whose contents must survive flow and runtime upgrades.
*/
public interface StoreService extends ControllerService {

void append(String row);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.tests.system;

import org.apache.nifi.annotation.behavior.InputRequirement;
import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.cs.tests.system.StateBackedStoreService;
import org.apache.nifi.cs.tests.system.StoreService;
import org.apache.nifi.migration.PropertyConfiguration;
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.exception.ProcessException;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;

@CapabilityDescription("""
Post-upgrade shape of a processor whose property migration creates the store Controller Service that the pre-upgrade shape did not have.
Each execution appends a row to the store so that tests can observe whether store contents survive flow and runtime upgrades.
""")
@InputRequirement(Requirement.INPUT_FORBIDDEN)
public class MigrateToControllerService extends AbstractProcessor {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Initially I wanted to reuse MigrateProperties processor, but some of its modifications blocked flow upgrade, so I proceeded with a dedicated processor - controller service pair


static final PropertyDescriptor STORE_SERVICE = new PropertyDescriptor.Builder()
.name("Store Service")
.required(false)
.identifiesControllerService(StoreService.class)
.build();

static final Relationship REL_SUCCESS = new Relationship.Builder().name("success").build();

private static final List<PropertyDescriptor> PROPERTIES = List.of(STORE_SERVICE);
private static final Set<Relationship> RELATIONSHIPS = Set.of(REL_SUCCESS);

private final AtomicLong rowCounter = new AtomicLong(0L);

@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return PROPERTIES;
}

@Override
public Set<Relationship> getRelationships() {
return RELATIONSHIPS;
}

@Override
public void migrateProperties(final PropertyConfiguration config) {
final String storeName = config.getPropertyValue("store-name").orElse(null);
config.removeProperty("store-name");

if (storeName != null) {
final String serviceId = config.createControllerService(StateBackedStoreService.class.getName(), Map.of("Store Name", storeName));
config.setProperty(STORE_SERVICE, serviceId);
}
}

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
final StoreService storeService = context.getProperty(STORE_SERVICE).asControllerService(StoreService.class);
if (storeService == null) {
context.yield();
return;
}

storeService.append("row-" + rowCounter.getAndIncrement());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# limitations under the License.

org.apache.nifi.cs.tests.system.MigrationService
org.apache.nifi.cs.tests.system.StateBackedStoreService
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# limitations under the License.

org.apache.nifi.processors.tests.system.MigrateProperties
org.apache.nifi.processors.tests.system.MigrateToControllerService
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
<artifactId>nifi-bin-manager</artifactId>
<version>2.13.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-connector-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.tests.system;

import org.apache.nifi.annotation.behavior.InputRequirement;
import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.util.StandardValidators;

import java.util.List;
import java.util.Set;

@CapabilityDescription("""
Pre-upgrade shape of a processor that keeps its store location in a plain property.
The post-upgrade shape of the same processor, in the alternate-config extensions bundle, migrates that property into a Controller Service.
""")
@InputRequirement(Requirement.INPUT_FORBIDDEN)
public class MigrateToControllerService extends AbstractProcessor {

static final PropertyDescriptor STORE_NAME = new PropertyDescriptor.Builder()
.name("store-name")
.displayName("Store Name")
.required(true)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();

static final Relationship REL_SUCCESS = new Relationship.Builder().name("success").build();

private static final List<PropertyDescriptor> PROPERTIES = List.of(STORE_NAME);
private static final Set<Relationship> RELATIONSHIPS = Set.of(REL_SUCCESS);

@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return PROPERTIES;
}

@Override
public Set<Relationship> getRelationships() {
return RELATIONSHIPS;
}

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
context.yield();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ org.apache.nifi.processors.tests.system.HoldInput
org.apache.nifi.processors.tests.system.IngestFile
org.apache.nifi.processors.tests.system.LoopFlowFile
org.apache.nifi.processors.tests.system.MigrateProperties
org.apache.nifi.processors.tests.system.MigrateToControllerService
org.apache.nifi.processors.tests.system.MultiKeyState
org.apache.nifi.processors.tests.system.MultiKeyStateNotDroppable
org.apache.nifi.processors.tests.system.PartitionText
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-system-test-flow-registry-bundle</artifactId>
<version>2.13.0-SNAPSHOT</version>
</parent>

<artifactId>nifi-system-test-flow-registry-nar</artifactId>
<packaging>nar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-framework-api</artifactId>
<version>2.13.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-system-test-flow-registry</artifactId>
<version>2.13.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Loading
Loading