-
Notifications
You must be signed in to change notification settings - Fork 28
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
Auto generate node ID #162
Changes from 2 commits
320616e
e0d85d3
0fdb499
7cb9900
c3216bd
812fb04
c6c6f38
1f965e6
e2456e4
44b6d73
2b248eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,11 +53,6 @@ public String getType() { | |
return "raw-container"; | ||
} | ||
|
||
/** Specifies task name. */ | ||
public String getName() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
return getClass().getName(); | ||
} | ||
|
||
/** Specifies task input type. */ | ||
public SdkType<InputT> getInputType() { | ||
return inputType; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,6 @@ public SdkNode apply(String id, SdkTransform transform) { | |
List<String> upstreamNodeIds = | ||
getOutputs().isEmpty() ? Collections.singletonList(getNodeId()) : Collections.emptyList(); | ||
|
||
return builder.applyInternal(id, transform, upstreamNodeIds, /*metadata=*/ null, getOutputs()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is strictly a breaking change, but I'm not sure why we had |
||
return builder.applyInternal(id, transform, upstreamNodeIds, getOutputs()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,10 +94,22 @@ public SdkTransform withUpstreamNode(SdkNode node) { | |
|
||
@Override | ||
public SdkTransform withNameOverride(String name) { | ||
return withNameOverride(name, true); | ||
} | ||
|
||
@Override | ||
SdkTransform withNameOverride(String name, boolean failOnDuplicate) { | ||
requireNonNull(name, "Name override cannot be null"); | ||
|
||
SdkNodeMetadata newMetadata = SdkNodeMetadata.builder().name(name).build(); | ||
checkForDuplicateMetadata(metadata, newMetadata, SdkNodeMetadata::name, "name"); | ||
try { | ||
checkForDuplicateMetadata(metadata, newMetadata, SdkNodeMetadata::name, "name"); | ||
} catch (IllegalArgumentException e) { | ||
if (failOnDuplicate) { | ||
throw e; | ||
} | ||
return this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is surprising for me, if a name is already set and check is false then we don't override the name (this method becomes no op). My suggestion would be to create a method called public SdkTransform withNameOverrideIfNotSet(String name) {
if (metadata.name() != null) {
return this;
}
return withNameOverride(name);
} Leaving the |
||
} | ||
SdkNodeMetadata mergedMetadata = mergeMetadata(metadata, newMetadata); | ||
|
||
return new SdkPartialTransform(transform, fixedInputs, extraUpstreamNodeIds, mergedMetadata); | ||
|
@@ -114,6 +126,11 @@ public SdkTransform withTimeoutOverride(Duration timeout) { | |
return new SdkPartialTransform(transform, fixedInputs, extraUpstreamNodeIds, mergedMetadata); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return transform.getName(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delegate to |
||
} | ||
|
||
@Override | ||
public SdkNode apply( | ||
SdkWorkflowBuilder builder, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,10 @@ public SdkTransform withUpstreamNode(SdkNode node) { | |
} | ||
|
||
public SdkTransform withNameOverride(String name) { | ||
return withNameOverride(name, true); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is exposed publicly? So now we have 2 ways to set the name? One though the withNameOverride and the other through the buillder.apply(name,...)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
SdkTransform withNameOverride(String name, boolean failOnDuplicate) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentionally not opening this method to avoid exposing unnecessary methods to users. |
||
requireNonNull(name, "Name override cannot be null"); | ||
|
||
SdkNodeMetadata metadata = SdkNodeMetadata.builder().name(name).build(); | ||
|
@@ -84,4 +88,8 @@ public SdkTransform withTimeoutOverride(Duration timeout) { | |
SdkNodeMetadata metadata = SdkNodeMetadata.builder().timeout(timeout).build(); | ||
return SdkPartialTransform.of(this, metadata); | ||
} | ||
|
||
public String getName() { | ||
return getClass().getName(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -27,17 +27,24 @@ | |||||||
import java.util.LinkedHashMap; | ||||||||
import java.util.List; | ||||||||
import java.util.Map; | ||||||||
import java.util.Objects; | ||||||||
import java.util.concurrent.atomic.AtomicInteger; | ||||||||
import java.util.regex.Pattern; | ||||||||
import javax.annotation.Nullable; | ||||||||
import org.flyte.api.v1.LiteralType; | ||||||||
import org.flyte.api.v1.SimpleType; | ||||||||
import org.flyte.api.v1.WorkflowTemplate; | ||||||||
|
||||||||
public class SdkWorkflowBuilder { | ||||||||
|
||||||||
private static final Pattern PATTERN = Pattern.compile("([a-z])([A-Z]+)"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we allow for capitals? i thought that only lower case was allowed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to convert |
||||||||
|
||||||||
private final Map<String, SdkNode> nodes; | ||||||||
private final Map<String, SdkBindingData> inputs; | ||||||||
private final Map<String, SdkBindingData> outputs; | ||||||||
private final Map<String, String> inputDescriptions; | ||||||||
private final Map<String, String> outputDescriptions; | ||||||||
private final AtomicInteger nodeIdSuffix; | ||||||||
|
||||||||
public SdkWorkflowBuilder() { | ||||||||
// Using LinkedHashMap to preserve declaration order | ||||||||
|
@@ -47,34 +54,52 @@ public SdkWorkflowBuilder() { | |||||||
|
||||||||
this.inputDescriptions = new HashMap<>(); | ||||||||
this.outputDescriptions = new HashMap<>(); | ||||||||
|
||||||||
this.nodeIdSuffix = new AtomicInteger(); | ||||||||
} | ||||||||
|
||||||||
public SdkNode apply(String nodeId, SdkTransform transform) { | ||||||||
return apply(nodeId, transform, emptyMap()); | ||||||||
} | ||||||||
|
||||||||
public SdkNode apply(String nodeId, SdkTransform transform, Map<String, SdkBindingData> inputs) { | ||||||||
return applyInternal(nodeId, transform, emptyList(), /*metadata=*/ null, inputs); | ||||||||
return applyInternal(nodeId, transform, emptyList(), inputs); | ||||||||
} | ||||||||
|
||||||||
protected SdkNode applyInternal( | ||||||||
String nodeId, | ||||||||
public SdkNode apply(SdkTransform transform) { | ||||||||
return apply(/*nodeId=*/ null, transform, emptyMap()); | ||||||||
} | ||||||||
|
||||||||
public SdkNode apply(SdkTransform transform, Map<String, SdkBindingData> inputs) { | ||||||||
return applyInternal(/*nodeId=*/ null, transform, emptyList(), inputs); | ||||||||
} | ||||||||
|
||||||||
SdkNode applyInternal( | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed visibility to package only. Note that this is strictly a breaking change. |
||||||||
@Nullable String nodeId, | ||||||||
SdkTransform transform, | ||||||||
List<String> upstreamNodeIds, | ||||||||
@Nullable SdkNodeMetadata metadata, | ||||||||
Map<String, SdkBindingData> inputs) { | ||||||||
|
||||||||
if (nodes.containsKey(nodeId)) { | ||||||||
String actualNodeId = | ||||||||
Objects.requireNonNullElseGet(nodeId, () -> "n" + nodeIdSuffix.getAndIncrement()); | ||||||||
|
||||||||
if (nodes.containsKey(actualNodeId)) { | ||||||||
CompilerError error = | ||||||||
CompilerError.create( | ||||||||
CompilerError.Kind.DUPLICATE_NODE_ID, | ||||||||
nodeId, | ||||||||
actualNodeId, | ||||||||
"Trying to insert two nodes with the same id."); | ||||||||
|
||||||||
throw new CompilerException(error); | ||||||||
} | ||||||||
|
||||||||
SdkNode sdkNode = transform.apply(this, nodeId, upstreamNodeIds, metadata, inputs); | ||||||||
String fallbackNodeName = | ||||||||
narape marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
Objects.requireNonNullElseGet(nodeId, () -> toNodeName(transform.getName())); | ||||||||
|
||||||||
SdkNode sdkNode = | ||||||||
transform | ||||||||
.withNameOverride(fallbackNodeName, false) | ||||||||
.apply(this, actualNodeId, upstreamNodeIds, null, inputs); | ||||||||
nodes.put(sdkNode.getNodeId(), sdkNode); | ||||||||
|
||||||||
return sdkNode; | ||||||||
|
@@ -201,4 +226,9 @@ public void output(String name, SdkBindingData value, String help) { | |||||||
public WorkflowTemplate toIdlTemplate() { | ||||||||
return WorkflowTemplateIdl.ofBuilder(this); | ||||||||
} | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To make it easier to follow |
||||||||
private static String toNodeName(String name) { | ||||||||
String lastPart = name.substring(name.lastIndexOf('.') + 1); | ||||||||
return PATTERN.matcher(lastPart).replaceAll("$1-$2").toLowerCase().replaceAll("\\$", "-"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically camel to kebab, also placing |
||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels unnecessarily strict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to delete these because of calling
SdkTransform withNameOverride(String name, boolean failOnDuplicate)