forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#2 from lukecwik/pr6328
[BEAM-4176] Add the ability to allow for runners to register native transforms.
- Loading branch information
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...uction-java/src/main/java/org/apache/beam/runners/core/construction/NativeTransforms.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,63 @@ | ||
/* | ||
* 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.beam.runners.core.construction; | ||
|
||
import com.google.auto.service.AutoService; | ||
import java.util.Iterator; | ||
import java.util.ServiceLoader; | ||
import java.util.function.Predicate; | ||
import org.apache.beam.model.pipeline.v1.RunnerApi; | ||
|
||
/** | ||
* An extension point for users to define their own native transforms for usage with specific | ||
* runners. This extension point enables shared libraries within the Apache Beam codebase to treat | ||
* the native transform as a primitive transforms that the runner implicitly understands. | ||
* | ||
* <p><b>Warning:</b>Usage of native transforms within pipelines will prevent users from migrating | ||
* between runners as there is no expectation that the transform will be understood by all runners. | ||
* Note that for some use cases this can be a way to test out a new type of transform on a limited | ||
* set of runners and promote its adoption as a primitive within the Apache Beam model. | ||
* | ||
* <p>Note that users are required to ensure that translation and execution for the native transform | ||
* is supported by their runner. | ||
* | ||
* <p>Automatic registration occurs by creating a {@link ServiceLoader} entry and a concrete | ||
* implementation of the {@link IsNativeTransform} interface. It is optional but recommended to use | ||
* one of the many build time tools such as {@link AutoService} to generate the necessary META-INF | ||
* files automatically. | ||
*/ | ||
public class NativeTransforms { | ||
/** | ||
* Returns true if an only if the Runner understands this transform and can handle it directly. | ||
*/ | ||
public static boolean isNative(RunnerApi.PTransform pTransform) { | ||
Iterator<IsNativeTransform> matchers = ServiceLoader.load(IsNativeTransform.class).iterator(); | ||
while (matchers.hasNext()) { | ||
if (matchers.next().test(pTransform)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** A predicate which returns true if and only if the transform is a native transform. */ | ||
public interface IsNativeTransform extends Predicate<RunnerApi.PTransform> { | ||
@Override | ||
boolean test(RunnerApi.PTransform pTransform); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...on-java/src/test/java/org/apache/beam/runners/core/construction/NativeTransformsTest.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,52 @@ | ||
/* | ||
* 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.beam.runners.core.construction; | ||
|
||
import com.google.auto.service.AutoService; | ||
import org.apache.beam.model.pipeline.v1.RunnerApi; | ||
import org.apache.beam.runners.core.construction.NativeTransforms.IsNativeTransform; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for {@link NativeTransforms}. */ | ||
@RunWith(JUnit4.class) | ||
public class NativeTransformsTest { | ||
/** A test implementation of a {@link IsNativeTransform}. */ | ||
@AutoService(IsNativeTransform.class) | ||
public static class TestNativeTransform implements IsNativeTransform { | ||
|
||
@Override | ||
public boolean test(RunnerApi.PTransform pTransform) { | ||
return "test".equals(PTransformTranslation.urnForTransformOrNull(pTransform)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMatch() { | ||
NativeTransforms.isNative( | ||
RunnerApi.PTransform.newBuilder() | ||
.setSpec(RunnerApi.FunctionSpec.newBuilder().setUrn("test").build()) | ||
.build()); | ||
} | ||
|
||
@Test | ||
public void testNoMatch() { | ||
NativeTransforms.isNative(RunnerApi.PTransform.getDefaultInstance()); | ||
} | ||
} |