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

NIFI-5922: Stateless NiFi, an alternative runtime for NiFi flows #3241

Closed
wants to merge 10 commits into from
79 changes: 79 additions & 0 deletions nifi-docker/dockermaven-stateless/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#@IgnoreInspection BashAddShebang
# 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.
#

FROM openjdk:8-jre-alpine
LABEL maintainer="Apache NiFi <dev@nifi.apache.org>"

ARG UID=1000
ARG GID=1000
ARG NIFI_VERSION
ARG STATELESS_LIB_DIR
ARG WORKING_DIR

ENV NIFI_BASE_DIR /opt/nifi
ENV NIFI_HOME ${NIFI_BASE_DIR}/nifi-current


#Use Maven-Ant until Docker squash is stable
#COPY $NIFI_BINARY $NIFI_BASE_DIR
#RUN unzip ${NIFI_BASE_DIR}/nifi-${NIFI_VERSION}-bin.zip -d ${NIFI_BASE_DIR} \
# && rm ${NIFI_BASE_DIR}/nifi-${NIFI_VERSION}-bin.zip \
# && mv ${NIFI_BASE_DIR}/nifi-${NIFI_VERSION} ${NIFI_HOME}
#
#COPY $NIFI_STATELESS_BINARY $NIFI_BASE_DIR
#RUN unzip ${NIFI_BASE_DIR}/nifi-stateless-${NIFI_VERSION}-bin.zip -d ${NIFI_BASE_DIR} \
# && rm ${NIFI_BASE_DIR}/nifi-stateless-${NIFI_VERSION}-bin.zip \
# && mv ${NIFI_BASE_DIR}/nifi-stateless-${NIFI_VERSION}/lib ${NIFI_HOME}/stateless-lib \
# && rm -r ${NIFI_BASE_DIR}/nifi-stateless-${NIFI_VERSION}
#
#RUN java -cp "${NIFI_HOME}/stateless-lib/*" org.apache.nifi.stateless.NiFiStateless ExtractNars
#RUN rm -r ${NIFI_HOME}/lib

# Setup NiFi user
RUN addgroup -g ${GID} nifi && adduser -s /bin/sh -u ${UID} -G nifi -D nifi

RUN mkdir -p $NIFI_HOME && chown nifi:nifi $NIFI_HOME
RUN mkdir -p ${NIFI_HOME}/work/ && chown nifi:nifi ${NIFI_HOME}/work/ && chmod 777 ${NIFI_HOME}/work/

COPY --chown=nifi:nifi $WORKING_DIR ${NIFI_HOME}/work/
COPY --chown=nifi:nifi $STATELESS_LIB_DIR ${NIFI_HOME}/stateless-lib/


#NiFi's HDFS processors require core-site.xml or hdfs-site.xml to exist on disk before they can be started...
RUN echo '<configuration> \n\
<property> \n\
<name>fs.defaultFS</name> \n\
<value>hdfs://localhost:8020</value> \n\
</property> \n\
<property> \n\
<name>fs.hdfs.impl</name> \n\
<value>org.apache.hadoop.hdfs.DistributedFileSystem</value> \n\
</property> \n\
</configuration>' > /tmp/core-site.xml && chown nifi /tmp/core-site.xml && chmod 777 /tmp/core-site.xml

RUN mkdir -p /hadoop/yarn/local && chown nifi /hadoop/yarn/local && chmod 777 /hadoop/yarn/local

USER nifi

EXPOSE 8080

WORKDIR ${NIFI_HOME}

ENTRYPOINT ["/usr/bin/java", "-cp", "stateless-lib/*", "org.apache.nifi.stateless.NiFiStateless"]
CMD ["RunOpenwhiskActionServer", "8080"]
46 changes: 46 additions & 0 deletions nifi-docker/dockermaven-stateless/integration-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# 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.

set -exuo pipefail

TAG=$1
VERSION=$2

trap "{ docker ps -qaf Name=nifi-${TAG}-integration-test | xargs docker rm -f; }" EXIT

echo "Checking that all files are owned by NiFi"
test -z $(docker run --rm --entrypoint /bin/bash apache/nifi-stateless:${TAG} -c "find /opt/nifi ! -user nifi")

echo "Checking environment variables"
test "/opt/nifi/nifi-current" = "$(docker run --rm --entrypoint /bin/bash apache/nifi-stateless:${TAG} -c 'echo -n $NIFI_HOME')"
test "/opt/nifi/nifi-current" = "$(docker run --rm --entrypoint /bin/bash apache/nifi-stateless:${TAG} -c "readlink \${NIFI_BASE_DIR}/nifi-${VERSION}")"
test "/opt/nifi" = "$(docker run --rm --entrypoint /bin/bash apache/nifi-stateless:${TAG} -c 'echo -n $NIFI_BASE_DIR')"

echo "Starting NiFi container..."
docker run -d --name nifi-${TAG}-integration-test apache/nifi-stateless:${TAG}

IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nifi-${TAG}-integration-test)

for i in $(seq 1 10) :; do
if docker exec nifi-${TAG}-integration-test bash -c "ss -ntl | grep 8080"; then
break
fi
sleep 10
done

echo "Stopping NiFi container"
time docker stop nifi-${TAG}-integration-test
136 changes: 136 additions & 0 deletions nifi-docker/dockermaven-stateless/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-docker</artifactId>
<version>1.10.0-SNAPSHOT</version>
</parent>

<artifactId>dockermaven-stateless</artifactId>

<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<!-- Using Ant until Docker squash is stable -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>extract archives</id>
<phase>process-sources</phase>
<configuration>
<tasks>
<unzip src="${project.basedir}/../../nifi-assembly/target/nifi-${nifi.version}-bin.zip" dest="${project.basedir}/target/"/>
<unzip src="${project.basedir}/../../nifi-stateless/nifi-stateless-assembly/target/nifi-stateless-${nifi.version}-bin.zip" dest="${project.basedir}/target/"/>
<move todir="${project.basedir}/target/lib">
<fileset dir="${project.basedir}/target/nifi-${nifi.version}/lib"/>
</move>
<move todir="${project.basedir}/target/stateless-lib">
<fileset dir="${project.basedir}/target/nifi-stateless-${nifi.version}/lib"/>
</move>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>extract NARs</id>
<phase>process-sources</phase>
<configuration>
<target>
<exec dir="${project.basedir}/target" executable="java">
<arg value="-cp" />
<arg value="stateless-lib/*" />
<arg value="org.apache.nifi.stateless.NiFiStateless" />
<arg value="ExtractNars" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>remove framework nar</id>
<phase>process-sources</phase>
<configuration>
<tasks>
<!--Not needed in docker image-->
<delete dir="${project.basedir}/target/lib" />

<!--Remove conflicting JAR. TODO: create custom assembly instead of using ..nifi-assembly/target/nifi-${nifi.version}-bin.zip-->
<delete dir="${project.basedir}/target/work/nifi-framework-nar-1.10.0-SNAPSHOT.nar-unpacked" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
<configuration>
<buildArgs>
<UID>1000</UID>
<GID>1000</GID>
<NIFI_VERSION>${project.version}</NIFI_VERSION>
<STATELESS_LIB_DIR>target/stateless-lib</STATELESS_LIB_DIR>
<WORKING_DIR>target/work</WORKING_DIR>
</buildArgs>
<repository>apache/nifi-stateless</repository>
<tag>${project.version}-dockermaven</tag>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>Docker integration tests</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<arguments>
<argument>${project.version}-dockermaven</argument>
<argument>${project.version}</argument>
</arguments>
<executable>${project.basedir}/integration-test.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
1 change: 1 addition & 0 deletions nifi-docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ language governing permissions and limitations under the License. -->

<modules>
<module>dockermaven</module>
<module>dockermaven-stateless</module>
<module>dockerhub</module>
</modules>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -238,7 +239,7 @@ private static boolean checkControllerServiceReferenceEligibility(final Configur
if (!cobundledApis.isEmpty()) {
logger.warn(String.format(
"Component %s is bundled with its referenced Controller Service APIs %s. The service APIs should not be bundled with component implementations that reference it.",
component.getClass().getName(), StringUtils.join(cobundledApis.stream().map(cls -> cls.getName()).collect(Collectors.toSet()), ", ")));
component.getClass().getName(), StringUtils.join(cobundledApis.stream().map(Class::getName).collect(Collectors.toSet()), ", ")));
}

// the component is eligible when it does not require instance classloading or when the supporting APIs are bundled in a parent NAR
Expand All @@ -265,7 +266,7 @@ private static boolean checkControllerServiceEligibility(Class extensionType) {

if (!cobundledApis.isEmpty()) {
logger.warn(String.format("Controller Service %s is bundled with its supporting APIs %s. The service APIs should not be bundled with the implementations.",
originalExtensionType.getName(), StringUtils.join(cobundledApis.stream().map(cls -> cls.getName()).collect(Collectors.toSet()), ", ")));
originalExtensionType.getName(), StringUtils.join(cobundledApis.stream().map(Class::getName).collect(Collectors.toSet()), ", ")));
}

// the service is eligible when it does not require instance classloading or when the supporting APIs are bundled in a parent NAR
Expand Down Expand Up @@ -364,17 +365,15 @@ public InstanceClassLoader createInstanceClassLoader(final String classType, fin
logger.debug("Including ClassLoader resources from {} for component {}", new Object[] {bundle.getBundleDetails(), instanceIdentifier});

final Set<URL> instanceUrls = new LinkedHashSet<>();
for (final URL url : narBundleClassLoader.getURLs()) {
instanceUrls.add(url);
}
instanceUrls.addAll(Arrays.asList(narBundleClassLoader.getURLs()));

ClassLoader ancestorClassLoader = narBundleClassLoader.getParent();

if (requiresInstanceClassLoading.cloneAncestorResources()) {
final ConfigurableComponent component = getTempComponent(classType, bundle.getBundleDetails().getCoordinate());
final Set<BundleCoordinate> reachableApiBundles = findReachableApiBundles(component);

while (ancestorClassLoader != null && ancestorClassLoader instanceof NarClassLoader) {
while (ancestorClassLoader instanceof NarClassLoader) {
final Bundle ancestorNarBundle = classLoaderBundleLookup.get(ancestorClassLoader);

// stop including ancestor resources when we reach one of the APIs, or when we hit the Jetty NAR
Expand All @@ -384,9 +383,7 @@ public InstanceClassLoader createInstanceClassLoader(final String classType, fin
}

final NarClassLoader ancestorNarClassLoader = (NarClassLoader) ancestorClassLoader;
for (final URL url : ancestorNarClassLoader.getURLs()) {
instanceUrls.add(url);
}
Collections.addAll(instanceUrls, ancestorNarClassLoader.getURLs());
ancestorClassLoader = ancestorNarClassLoader.getParent();
}
}
Expand Down Expand Up @@ -498,7 +495,7 @@ public Set<Class> getExtensions(final Class<?> definition) {
throw new IllegalArgumentException("Class cannot be null");
}
final Set<Class> extensions = definitionMap.get(definition);
return (extensions == null) ? Collections.<Class>emptySet() : extensions;
return (extensions == null) ? Collections.emptySet() : extensions;
}

@Override
Expand Down Expand Up @@ -527,8 +524,7 @@ public void logClassLoaderMapping() {
builder.append("\n\t=== ").append(entry.getKey().getSimpleName()).append(" Type ===");

for (final Class type : entry.getValue()) {
final List<Bundle> bundles = classNameBundleLookup.containsKey(type.getName())
? classNameBundleLookup.get(type.getName()) : Collections.emptyList();
final List<Bundle> bundles = classNameBundleLookup.getOrDefault(type.getName(), Collections.emptyList());

builder.append("\n\t").append(type.getName());

Expand Down
Loading