-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Auto generate node ID Signed-off-by: Hongxin Liang <[email protected]> * Do not expose Signed-off-by: Hongxin Liang <[email protected]> * OK Signed-off-by: Hongxin Liang <[email protected]> * Use remote name Signed-off-by: Hongxin Liang <[email protected]> * Use node id prefix Signed-off-by: Hongxin Liang <[email protected]> * Give it a proper prefix Signed-off-by: Hongxin Liang <[email protected]> * Use alphabet for prefix Signed-off-by: Hongxin Liang <[email protected]> * Node id related methods in own class Signed-off-by: Nelson Arapé <[email protected]> * withNameOverride(String, boolean) -> withNameOverrideIfNotSet Signed-off-by: Nelson Arapé <[email protected]> * Rename NamePolicy and add docs Signed-off-by: Nelson Arapé <[email protected]> * Minor refactor test Signed-off-by: Nelson Arapé <[email protected]> Signed-off-by: Hongxin Liang <[email protected]> Signed-off-by: Nelson Arapé <[email protected]> Co-authored-by: Nelson Arapé <[email protected]> Signed-off-by: Andres Gomez Ferrer <[email protected]>
- Loading branch information
1 parent
d0a0b83
commit 5a01149
Showing
17 changed files
with
311 additions
and
47 deletions.
There are no files selected for viewing
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
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
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
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
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
85 changes: 85 additions & 0 deletions
85
flytekit-java/src/main/java/org/flyte/flytekit/SdkNodeNamePolicy.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,85 @@ | ||
/* | ||
* Copyright 2021 Flyte Authors | ||
* | ||
* Licensed 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.flyte.flytekit; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Controls the default node id and node name policy when applying {@link SdkTransform} to {@link | ||
* SdkWorkflowBuilder}. When using {@link SdkWorkflowBuilder#apply(SdkTransform)} or {@link | ||
* SdkWorkflowBuilder#apply(SdkTransform, Map)} then the node id used would be the one returned by | ||
* {@link #nextNodeId()}. Also, if a node name haven't been set by the user, then {@link | ||
* #toNodeName(String)} would be used. | ||
*/ | ||
class SdkNodeNamePolicy { | ||
private static final Pattern UPPER_AFTER_LOWER_PATTERN = Pattern.compile("([a-z])([A-Z]+)"); | ||
private static final int RND_PREFIX_SIZE = 4; | ||
|
||
private final String nodeIdPrefix; | ||
private final AtomicInteger nodeIdSuffix; | ||
|
||
SdkNodeNamePolicy() { | ||
this.nodeIdPrefix = randomPrefix(); | ||
this.nodeIdSuffix = new AtomicInteger(); | ||
} | ||
|
||
/** | ||
* Returns a unique node ids in the format {@code <prefix>-n<consecutive-number>}, where prefix is | ||
* a random, but shared among all ids for this object, set of character in the format {@code | ||
* wRRRR} and {@code R} is a random letter in {@code a-z} range. | ||
* | ||
* @return next unique node id for this policy. | ||
*/ | ||
String nextNodeId() { | ||
return nodeIdPrefix + "n" + nodeIdSuffix.getAndIncrement(); | ||
} | ||
|
||
/** | ||
* Returns a node appropriate name for a given transformation name. The transformation done are | ||
* | ||
* <ul> | ||
* <li>Package name is removed | ||
* <li>CamelCase is transformed to kebab-case | ||
* <li>$ is transformed to - | ||
* </ul> | ||
* | ||
* <p>For example {@code com.example.Outer$InnerTask} get translated to {@code outer-inner-task}. | ||
* | ||
* @return node name. | ||
*/ | ||
String toNodeName(String name) { | ||
String lastPart = name.substring(name.lastIndexOf('.') + 1); | ||
return UPPER_AFTER_LOWER_PATTERN | ||
.matcher(lastPart) | ||
.replaceAll("$1-$2") | ||
.toLowerCase(Locale.ROOT) | ||
.replaceAll("\\$", "-"); | ||
} | ||
|
||
// Returns random prefix in the following format "wqjoz-" | ||
private static String randomPrefix() { | ||
return "w" | ||
+ ThreadLocalRandom.current() | ||
.ints(RND_PREFIX_SIZE, 'a', 'z' + 1) | ||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) | ||
.append('-'); | ||
} | ||
} |
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
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
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
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
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
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
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
Oops, something went wrong.