clockworklabs / clockworklabs/SpacetimeDB

endless procedures

Open
#5,220 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request
Dominant language
Rust
Stars
25.2k
Forks
1.1k
Avg merge
2d 7h
Merged PRs (30d)
46

Description

procedures have a very high potential for server side computation that takes longer than a ms or needs data that is heavy to calculate/build from the tables. be it physics, npc path finding and behaviour tree updates or other stuff that needs to be updated often with large base data.

the problem currently is that procedures don't have a way of checking if they should stop for a publish, wasm rebuild or other stuff like moving of the module from one machine to another.
the idea is that we need a st_ table for the procedures that keeps track of all the active procedure instances and that they have a column that the procedure can check with a ctx. function for it to stop and all the data the server requires to forcefully stop the procedure if needed (timeout no response and so on).
an example code of the wanted setup would be:

#[procedure]
pub fn run_physics_loop(ctx: &mut ProcedureContext, _row: PhysicsLaunch) {
    let mut world = ctx.with_tx(|tx| {
        build_world_from_db(tx) // computation/time/size expensive build process
    });
    while not ctx.check_stop(){  // check the st_table if it should stop
        let inputs = ctx.with_tx(read_pending_inputs);  // get input data
        apply_membership(&mut world, /* ... */); 
        run_substeps(&mut world, &inputs, /* ... */); // do math that takes a while
        ctx.with_tx(|tx| {
            write_tick_outputs(tx, /* ... */);
        });
        ctx.sleep_until(procedure_runtime::next_tick_target(ctx.timestamp, PHYSICS_INTERVAL));
    }
}

this setup can speed up physics by over 1000x compared to rebuilding the physics world each tick inside a reducer and the expensive calculations don't block other reducers while it is running.

Requested by @xDovos via the SpacetimeDB site.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by tracing how procedures run, how publish or WASM rebuild operations stop work, and how procedure context accesses database state. Done should include tracked active procedure instances, a stop signal exposed through the context, and a way to forcefully stop unresponsive procedures.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.