-
Notifications
You must be signed in to change notification settings - Fork 0
feat(java): add mesh scheduling worker #359
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
276f6e1
feat(java): mesh scheduling JNI bridge (mesh feature)
pratyush618 d8b674a
feat(java): Worker.mesh + MeshOptions/cluster info
pratyush618 b018371
test(java): cover mesh worker + cluster info
pratyush618 a897df6
ci: build Java native with mesh feature
pratyush618 d33f1d6
fix(java): log mesh config parse and task failures
pratyush618 c1c688c
fix(java): saturate mesh capacity cast to u16
pratyush618 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,120 @@ | ||
| //! Mesh scheduling bridge (feature `mesh`). | ||
| //! | ||
| //! When a worker is started with a `meshConfig`, [`spawn_mesh`] interposes a | ||
| //! [`taskito_mesh::MeshNode`] between the scheduler and the dispatcher: the | ||
| //! scheduler emits ready jobs into an intermediate channel, the bridge sorts | ||
| //! them into the node's affinity-aware local deque (and steals from busy peers), | ||
| //! then drains the deque to the real dispatcher channel. The DB stays the source | ||
| //! of truth, so the scheduler and dispatcher remain mesh-unaware — identical to | ||
| //! the Python and Node shells. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use taskito_core::job::Job; | ||
| use taskito_core::scheduler::Scheduler; | ||
| use taskito_mesh::{MeshConfig, MeshNode}; | ||
| use tokio::sync::mpsc::Sender; | ||
|
|
||
| /// Build the mesh node, start its gossip + steal-server tasks and the bridge | ||
| /// loop on `runtime`, and return the node so the worker can request shutdown and | ||
| /// read cluster info. `threads` is advertised to peers as this worker's capacity. | ||
| pub fn spawn_mesh( | ||
| runtime: &tokio::runtime::Runtime, | ||
| scheduler: Arc<Scheduler>, | ||
| job_tx: Sender<Job>, | ||
| config_json: &str, | ||
| worker_id: String, | ||
| queues: Vec<String>, | ||
| threads: u16, | ||
| ) -> Arc<MeshNode> { | ||
| let config: MeshConfig = serde_json::from_str(config_json).unwrap_or_else(|e| { | ||
| log::error!("[taskito-java] invalid mesh config, falling back to defaults: {e}"); | ||
| MeshConfig::default() | ||
| }); | ||
| log::info!( | ||
| "[taskito-java] mesh scheduling enabled (gossip_port={}, local_buffer={})", | ||
| config.gossip_port, | ||
| config.local_buffer_capacity, | ||
| ); | ||
| let node = Arc::new(MeshNode::new(worker_id, config)); | ||
|
|
||
| // `spawn_gossip`/`spawn_steal_server` call the free `tokio::spawn` internally, | ||
| // so they must run inside the runtime context (we are on the JNI thread here). | ||
| let (gossip, steal_server) = { | ||
| let _guard = runtime.enter(); | ||
| ( | ||
| node.spawn_gossip(queues, threads), | ||
| node.spawn_steal_server(), | ||
| ) | ||
| }; | ||
|
|
||
| let bridge_node = node.clone(); | ||
| runtime.spawn(async move { | ||
| run_mesh_bridge(scheduler, bridge_node, job_tx).await; | ||
| }); | ||
| // Keep the gossip + steal-server tasks alive for the worker's lifetime; they | ||
| // stop when the node's shutdown is signalled (see `request_shutdown`). | ||
| runtime.spawn(async move { | ||
| let (gossip_res, steal_res) = tokio::join!(gossip, steal_server); | ||
| if let Err(e) = gossip_res { | ||
| log::error!("[taskito-java] mesh gossip task failed: {e}"); | ||
| } | ||
| if let Err(e) = steal_res { | ||
| log::error!("[taskito-java] mesh steal-server task failed: {e}"); | ||
| } | ||
| }); | ||
|
|
||
| node | ||
| } | ||
|
|
||
| /// Pump jobs from the scheduler through the mesh local deque to the dispatcher. | ||
| async fn run_mesh_bridge(scheduler: Arc<Scheduler>, mesh_node: Arc<MeshNode>, job_tx: Sender<Job>) { | ||
| let (mesh_tx, mut mesh_rx) = tokio::sync::mpsc::channel::<Job>(64); | ||
|
|
||
| let sched = scheduler.clone(); | ||
| let sched_task = tokio::spawn(async move { | ||
| sched.run(mesh_tx).await; | ||
| }); | ||
|
|
||
| loop { | ||
| // Drain the local deque to the dispatcher first. | ||
| while let Some(job) = mesh_node.pop_local() { | ||
| if job_tx.send(job).await.is_err() { | ||
| let _ = sched_task.await; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Steal from a busier peer when our deque is low. | ||
| if mesh_node.should_steal() { | ||
| mesh_node.try_steal().await; | ||
| while let Some(job) = mesh_node.pop_local() { | ||
| if job_tx.send(job).await.is_err() { | ||
| let _ = sched_task.await; | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Wait for the scheduler to produce jobs, then prefetch the batch. | ||
| match mesh_rx.recv().await { | ||
| Some(job) => { | ||
| let mut batch = vec![job]; | ||
| while let Ok(extra) = mesh_rx.try_recv() { | ||
| batch.push(extra); | ||
| } | ||
| mesh_node.prefetch(batch); | ||
| } | ||
| None => break, | ||
| } | ||
| } | ||
|
|
||
| // Scheduler done: drain whatever remains in the deque. | ||
| while let Some(job) = mesh_node.pop_local() { | ||
| if job_tx.send(job).await.is_err() { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| let _ = sched_task.await; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
23 changes: 23 additions & 0 deletions
23
sdks/java/src/main/java/org/byteveda/taskito/worker/MeshClusterInfo.java
This file contains hidden or 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,23 @@ | ||
| package org.byteveda.taskito.worker; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| /** | ||
| * A snapshot of a mesh worker's view of its cluster, from | ||
| * {@link Worker#meshClusterInfo()}. Capacities and loads are summed across alive | ||
| * peers (excluding this node); buffered counts are job-deque lengths. | ||
| * | ||
| * @param peerCount alive peers discovered via gossip (excludes self) | ||
| * @param totalCapacity summed advertised capacity of those peers | ||
| * @param totalLoad summed in-flight jobs across those peers | ||
| * @param totalBuffered summed local-deque length across those peers | ||
| * @param localBufferLen this worker's local-deque length | ||
| * @param adaptivePrefetch this worker's current prefetch budget | ||
| */ | ||
| public record MeshClusterInfo( | ||
| @JsonProperty("peer_count") int peerCount, | ||
| @JsonProperty("total_capacity") int totalCapacity, | ||
| @JsonProperty("total_load") int totalLoad, | ||
| @JsonProperty("total_buffered") int totalBuffered, | ||
| @JsonProperty("local_buffer_len") int localBufferLen, | ||
| @JsonProperty("adaptive_prefetch") int adaptivePrefetch) {} |
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.
Uh oh!
There was an error while loading. Please reload this page.