-
Notifications
You must be signed in to change notification settings - Fork 148
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
safe message passing sample #681
Merged
Quinn-With-Two-Ns
merged 8 commits into
temporalio:main
from
Quinn-With-Two-Ns:SDK-2751
Oct 30, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6467873
Add safe message passing sample
Quinn-With-Two-Ns 73f3dca
fix
Quinn-With-Two-Ns 4f50ce4
Add unit tests
Quinn-With-Two-Ns a66ec92
Fix licenses headers
Quinn-With-Two-Ns bbf3470
run spotless
Quinn-With-Two-Ns 8972e7c
Respond to PR comments
Quinn-With-Two-Ns 52226ac
Use workflow init
Quinn-With-Two-Ns bf6f30b
Update core/src/main/java/io/temporal/samples/safemessagepassing/Clus…
Quinn-With-Two-Ns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
102 changes: 102 additions & 0 deletions
102
core/src/main/java/io/temporal/samples/safemessagepassing/ClusterManagerActivities.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,102 @@ | ||
/* | ||
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
* | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 io.temporal.samples.safemessagepassing; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.temporal.activity.ActivityInterface; | ||
import io.temporal.activity.ActivityMethod; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@ActivityInterface | ||
public interface ClusterManagerActivities { | ||
|
||
class AssignNodesToJobInput { | ||
private final List<String> nodes; | ||
private final String jobName; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public AssignNodesToJobInput( | ||
@JsonProperty("nodes_to_assign") Set<String> nodesToAssign, | ||
@JsonProperty("job_name") String jobName) { | ||
this.nodes = List.copyOf(nodesToAssign); | ||
this.jobName = jobName; | ||
} | ||
|
||
@JsonProperty("nodes_to_assign") | ||
public List<String> getNodes() { | ||
return nodes; | ||
} | ||
|
||
@JsonProperty("job_name") | ||
public String getJobName() { | ||
return jobName; | ||
} | ||
} | ||
|
||
@ActivityMethod | ||
void assignNodesToJob(AssignNodesToJobInput input); | ||
|
||
class UnassignNodesForJobInput { | ||
private final List<String> nodes; | ||
private final String jobName; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public UnassignNodesForJobInput( | ||
@JsonProperty("nodes") Set<String> nodes, @JsonProperty("job_name") String jobName) { | ||
this.nodes = List.copyOf(nodes); | ||
this.jobName = jobName; | ||
} | ||
|
||
@JsonProperty("nodes") | ||
public List<String> getNodes() { | ||
return nodes; | ||
} | ||
|
||
@JsonProperty("job_name") | ||
public String getJobName() { | ||
return jobName; | ||
} | ||
} | ||
|
||
@ActivityMethod | ||
void unassignNodesForJob(UnassignNodesForJobInput input); | ||
|
||
class FindBadNodesInput { | ||
private final Set<String> nodesToCheck; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public FindBadNodesInput(@JsonProperty("assigned_nodes") Set<String> assignedNodes) { | ||
this.nodesToCheck = assignedNodes; | ||
} | ||
|
||
@JsonProperty("assigned_nodes") | ||
public Set<String> getNodesToCheck() { | ||
return nodesToCheck; | ||
} | ||
} | ||
|
||
@ActivityMethod | ||
Set<String> findBadNodes(FindBadNodesInput input); | ||
|
||
@ActivityMethod | ||
void shutdown(); | ||
} |
82 changes: 82 additions & 0 deletions
82
core/src/main/java/io/temporal/samples/safemessagepassing/ClusterManagerActivitiesImpl.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,82 @@ | ||
/* | ||
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
* | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 io.temporal.samples.safemessagepassing; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ClusterManagerActivitiesImpl implements ClusterManagerActivities { | ||
private static final Logger log = LoggerFactory.getLogger(ClusterManagerActivitiesImpl.class); | ||
|
||
@Override | ||
public void assignNodesToJob(AssignNodesToJobInput input) { | ||
for (String node : input.getNodes()) { | ||
log.info("Assigned node " + node + " to job " + input.getJobName()); | ||
} | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void unassignNodesForJob(UnassignNodesForJobInput input) { | ||
for (String node : input.getNodes()) { | ||
log.info("Unassigned node " + node + " from job " + input.getJobName()); | ||
} | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<String> findBadNodes(FindBadNodesInput input) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
Set<String> badNodes = | ||
input.getNodesToCheck().stream() | ||
.filter(n -> Integer.parseInt(n) % 5 == 0) | ||
.collect(Collectors.toSet()); | ||
if (!badNodes.isEmpty()) { | ||
log.info("Found bad nodes: " + badNodes); | ||
} else { | ||
log.info("No bad nodes found"); | ||
} | ||
return badNodes; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
log.info("Shutting down cluster"); | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
171 changes: 171 additions & 0 deletions
171
core/src/main/java/io/temporal/samples/safemessagepassing/ClusterManagerWorkflow.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,171 @@ | ||
/* | ||
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved | ||
* | ||
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License is | ||
* located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 io.temporal.samples.safemessagepassing; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.temporal.workflow.SignalMethod; | ||
import io.temporal.workflow.UpdateMethod; | ||
import io.temporal.workflow.WorkflowInterface; | ||
import io.temporal.workflow.WorkflowMethod; | ||
import java.util.*; | ||
|
||
/** | ||
* ClusterManagerWorkflow keeps track of the assignments of a cluster of nodes. Via signals, the | ||
* cluster can be started and shutdown. Via updates, clients can also assign jobs to nodes and | ||
* delete jobs. These updates must run atomically. | ||
*/ | ||
@WorkflowInterface | ||
public interface ClusterManagerWorkflow { | ||
|
||
enum ClusterState { | ||
NOT_STARTED, | ||
STARTED, | ||
SHUTTING_DOWN | ||
} | ||
// In workflows that continue-as-new, it's convenient to store all your state in one serializable | ||
// structure to make it easier to pass between runs | ||
class ClusterManagerState { | ||
public ClusterState workflowState = ClusterState.NOT_STARTED; | ||
public Map<String, Optional<String>> nodes = new HashMap<>(); | ||
public Set<String> jobAssigned = new HashSet<>(); | ||
} | ||
|
||
class ClusterManagerInput { | ||
private final Optional<ClusterManagerState> state; | ||
private final boolean testContinueAsNew; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public ClusterManagerInput( | ||
@JsonProperty("state") Optional<ClusterManagerState> state, | ||
@JsonProperty("test_continue_as_new") boolean testContinueAsNew) { | ||
this.state = state; | ||
this.testContinueAsNew = testContinueAsNew; | ||
} | ||
|
||
@JsonProperty("state") | ||
public Optional<ClusterManagerState> getState() { | ||
return state; | ||
} | ||
|
||
@JsonProperty("test_continue_as_new") | ||
public boolean isTestContinueAsNew() { | ||
return testContinueAsNew; | ||
} | ||
} | ||
|
||
class ClusterManagerResult { | ||
private final int numCurrentlyAssignedNodes; | ||
private final int numBadNodes; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public ClusterManagerResult( | ||
@JsonProperty("num_currently_assigned_nodes") int numCurrentlyAssignedNodes, | ||
@JsonProperty("num_bad_nodes") int numBadNodes) { | ||
this.numCurrentlyAssignedNodes = numCurrentlyAssignedNodes; | ||
this.numBadNodes = numBadNodes; | ||
} | ||
|
||
@JsonProperty("num_currently_assigned_nodes") | ||
public int getNumCurrentlyAssignedNodes() { | ||
return numCurrentlyAssignedNodes; | ||
} | ||
|
||
@JsonProperty("num_bad_nodes") | ||
public int getNumBadNodes() { | ||
return numBadNodes; | ||
} | ||
} | ||
|
||
// Be in the habit of storing message inputs and outputs in serializable structures. | ||
// This makes it easier to add more overtime in a backward-compatible way. | ||
class ClusterManagerAssignNodesToJobInput { | ||
// If larger or smaller than previous amounts, will resize the job. | ||
private final int totalNumNodes; | ||
private final String jobName; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public ClusterManagerAssignNodesToJobInput( | ||
@JsonProperty("total_num_nodes") int totalNumNodes, | ||
@JsonProperty("job_name") String jobName) { | ||
this.totalNumNodes = totalNumNodes; | ||
this.jobName = jobName; | ||
} | ||
|
||
@JsonProperty("total_num_nodes") | ||
public int getTotalNumNodes() { | ||
return totalNumNodes; | ||
} | ||
|
||
@JsonProperty("job_name") | ||
public String getJobName() { | ||
return jobName; | ||
} | ||
} | ||
|
||
class ClusterManagerDeleteJobInput { | ||
private final String jobName; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public ClusterManagerDeleteJobInput(@JsonProperty("job_name") String jobName) { | ||
this.jobName = jobName; | ||
} | ||
|
||
@JsonProperty("job_name") | ||
public String getJobName() { | ||
return jobName; | ||
} | ||
} | ||
|
||
class ClusterManagerAssignNodesToJobResult { | ||
private final Set<String> nodesAssigned; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
public ClusterManagerAssignNodesToJobResult( | ||
@JsonProperty("assigned_nodes") Set<String> assignedNodes) { | ||
this.nodesAssigned = assignedNodes; | ||
} | ||
|
||
@JsonProperty("assigned_nodes") | ||
public Set<String> getNodesAssigned() { | ||
return nodesAssigned; | ||
} | ||
} | ||
|
||
@WorkflowMethod | ||
ClusterManagerResult run(ClusterManagerInput input); | ||
|
||
@SignalMethod | ||
void startCluster(); | ||
|
||
@UpdateMethod | ||
boolean stopCluster(); | ||
|
||
// This is an update as opposed to a signal because the client may want to wait for nodes to be | ||
// allocated before sending work to those nodes. Returns the list of node names that were | ||
// allocated to the job. | ||
@UpdateMethod | ||
ClusterManagerAssignNodesToJobResult assignNodesToJobs(ClusterManagerAssignNodesToJobInput input); | ||
|
||
// Even though it returns nothing, this is an update because the client may want to track it, for | ||
// example to wait for nodes to be unassigned before reassigning them. | ||
@UpdateMethod | ||
void deleteJob(ClusterManagerDeleteJobInput input); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you educate me on why we convert the one exception into the other?
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.
InterruptedException
is a checked exception so we need to translate it to a unchecked exception