Commit Graph

172 Commits

Author SHA1 Message Date
Tyler Cloutier 19474c9ec7 Remove v8 feature gate 2025-10-15 19:20:04 -04:00
Noa 207e559859 Noa/ts module host changes (#3388)
# Description of Changes

Host-side changes extracted from #3327 

I added AUTO_INC_OVERFLOW even though we don't currently ever return it,
in order to future-proof so it's already there when we start emitting
it.

Prepublish was failing because it was expecting a wasm module
unconditionally, so now it takes ?host_type.

I tweaked JS deser to accept null/undefined when the unit type or an
option type is expected.

I switched to bsatn, because the native sats->js translator wasn't
matching what js was expecting.

I renamed the sys module: my thinking is that `spacetime:` as a scheme
will help disambiguate it, and maybe it could also be used for IMC in
the future or something? And I believe we had discussed wanting this to
be versioned, similar to wasm imports.

Trying to get a borrowed str from deserialize_js doesn't work, because
v8 strings don't store utf8.

# Testing

<!-- Describe any testing you've done, and any testing you'd like your
reviewers to do,
so that you're confident that all the changes work as expected! -->

- [x] All this was done in the course of getting an actual typescript
module to successfully publish.
2025-10-10 18:25:26 +00:00
Mazdak Farrokhzad abc3a1c032 V8: Enable as unstable, add syscalls, flesh out call_reducer, etc. (#3276)
# Description of Changes

Update:

This PR did all of the below but was split. Now it just does:

1. Exposes V8/JS modules via the `unstable` feature flag on the host. To
publish a JS module, `--js-path path/to/module.js`

This PR:

1. Exposes V8/JS modules via the `unstable` feature flag on the host. To
publish a JS module, `--js-path path/to/module.js` needs to be used.
2. Bumps V8 to 140.2.
3. Shares more logic with WASM and makes some minor refactorings to
energy/budget logic.
4. Moves logic from `WasmInstanceEnv` to `InstanceEnv` and friends.
5. Makes JS modules actually work in terms of `create_instance`,
`make_actor`,
6. Fleshes out `call_reducer` with timeouts and long-running logs added
as well.
7. Adds all the syscalls with associated documentation as well.

# API and ABI breaking changes

None

# Expected complexity level and risk

2? It's only available on unstable and mostly touches V8 stuff.

# Testing

Follow up PRs will add unit tests for parts.
We'll also need to add integration tests for whole modules.
2025-10-06 20:03:36 +00:00
Shubham Mishra 54b07d0b87 cli: pre-publish endpoint call. (#3278)
# Description of Changes
PR contains:

* CLI changes for the `pre_publish` endpoint when publishing a module.
* The regular `--yes` flag will not bypasses the *break clients* warning
prompt — an extra confirmation is now required. For CI, a hidden flag
`--break-clients` is added.
* Added smoketest.
* Some trivial naming changes in `client-api-*` crates for consistency
reasons.
* `pre_publish` route to accept similar Body size limit as `publish`
route.



# API and ABI breaking changes
an additive API change, does not break anything.

# Expected complexity level and risk
2

# Testing
- Existing smoketests passing for backward compatibility.
- New smoketest for add columns

---------

Signed-off-by: Shubham Mishra <shivam828787@gmail.com>
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
Co-authored-by: Zeke Foppa <196249+bfops@users.noreply.github.com>
2025-10-01 18:26:03 +00:00
Shubham Mishra bfc9741388 core: disconnect clients (#3275)
# Description of Changes
The `AutoMigrateStep::DisconnectAllUsers` step is implemented as
follows:

1. The `spacetimedb::db::update::update_database` function returns a
response of type
`UpdateDatabaseResult::UpdatePerformedWithClientDisconnect`.

2. Upon receiving this response, the `host_controller::update_module`
proceeds to drop the `watch::Sender<ModuleHost>` field within the
`core::host_controller::Host` and disconnect clients.

# API and ABI breaking changes
NA

# Expected complexity level and risk
3.
Diff code is simple but It depends on the subcription logic to behave
correctly.

# Testing
Manually.

---------

Signed-off-by: Shubham Mishra <shivam828787@gmail.com>
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
2025-10-01 15:15:39 +00:00
Jeffrey Dallatezza 1d08167ebd Store client credentials in a new system table (#2983)
# Description of Changes

This adds a new system table to store the jwt payloads of connected
clients. I'm planning to use this system table to expose client claims
to modules in subsequent PRs.

The new table is called `st_connection_credentials`. It is a **private**
system table which stores a mapping from `connection_id` to
`jwt_payload`. Note that a jwt payload is a json representation of the
clients claims, not a fully signed token.

The times when we need to insert and delete these rows closely mirrors
that of the existing `st_client` table, with 1.5 exceptions:
1. We weren't previously inserting to `st_client` until after the
`OnConnect` reducer ran (even though it was in the same transaction). We
want `st_connection_credentials` to be populated before calling the
reducer, so that the reducer can use it get the credentials, so I made a
change to insert to `st_client` and `st_connection_credentials` before
calling the reducer.
2. This difference has not actualized, but when clients start sending
refresh tokens, we will probably need to update the credentials stored
in this table.

This also enforces uniqueness of connection ids. A duplicate connection
id will now make the on-connect reducer fail (since it will violate
uniqueness when trying to insert to `st_connection_credentials`).

# Expected complexity level and risk

2.5

Adding a system table is a bit risky. This is almost rollback safe, with
one annoying case that is worth calling out:

If a database is created with this system table, opening it with an
older version of spacetimedb will only work if there is a snapshot of
the database. If we try to load a table without a snapshot, replaying
will fail on the first row for that table. This is because we don't
write the table schema information to the commit log when creating a
database. In practice, this is unlikely to be an issue, because new
databases asynchronously trigger a snapshot immediately after creation.

Migrating existing databases will be fine. On startup this will detect
that there is a missing system table, and add it in a way that writes it
to the commit log. Since it is in the commit log, we can open the
database with an older version and still understand the data for that
table.

# Testing

There are unit tests that cover opening a database created with an older
version (which doesn't have this table).

I manually tested opening a migrated database with an older version of
spacetimedb.
2025-09-19 15:39:45 +00:00
Mario Montoya 8adef2b93b Support for the PG wire protocol (#2702)
# Description of Changes

Closes
[#2686](https://github.com/clockworklabs/SpacetimeDB/issues/2686).

Add support for listening using the [PG wire
protocol](https://www.postgresql.org/docs/current/protocol.html) so `pg`
clients could be used against the database.

# API and ABI breaking changes

The output of `duration` is changed to `rfc3339`, instead of the way is
made with `sats` because is what is done in `pg`, see note below.

# Expected complexity level and risk

2

~~There is open questions that are in the [ticket
#2686](https://github.com/clockworklabs/SpacetimeDB/issues/2686). Also
the crate used here require `RustTls`, so it could be good idea to
decide if~~:

* ~~Rewrite a big chunk of code to use `OpenSSL`~~
* ~~Move to `RustTls`
https://github.com/clockworklabs/SpacetimeDB/pull/1700~~
* ~~Pay for the extra compilation cost~~.

I open another port(`5433`) to listen for `pg` connections using `ssl`.
Need to be decided if this is the way or instead try to multi-plex the
current port for both protocols.

# Testing

Only manual testing so far. Solving the above questions allow me to
implement some unit tests. Also, not yet integrated into cloud for the
same reasons.

- [x] Adding some test for the binary encoding of special and primitive
types
- [x] Smoke test using `psql` that connect to the db instance and run
some queries
- [x] Manually inspect using a UI database explorer how infer the types,
some of this tools generate special widgets when displaying `json,
duration, etc`

---------

Co-authored-by: Noa <coolreader18@gmail.com>
2025-09-10 19:58:03 +00:00
Shubham Mishra 2c74f73550 Endpoint for pretty print migration plan (#3137)
# Description of Changes
- Adds endpoint for for pretty printing migration plan.
- It also changes current `publish` endpoint to optionally provide
`MigrationToken` and `MigrationPolicy` to allow migration with breaking
clients.

# API and ABI breaking changes
Backward compatible change to existing API and new Api

# Expected complexity level and risk
2

# Testing
- Existing smoketest should cover changes for `publish` endpoint.
- For pretty print endpoint, smoketests can be written only after cli
changes.

---------

Signed-off-by: Shubham Mishra <shivam828787@gmail.com>
Co-authored-by: James Gilles <jameshgilles@gmail.com>
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
2025-09-09 06:53:37 +00:00
Kim Altintop f394de32d9 Confirmed reads (#3133)
# Description of Changes

Implements [subscribing to durable
commits](https://github.com/clockworklabs/SpacetimeDBPrivate/issues/1594).

The setting works on a per-connection level, and essentially just delays
sending transaction updates until the transaction is reported as durable
by the database.

For connectionless SQL operations, the setting works per-request. No SQL
syntax is provided by this patch to toggle the configuration.

After some deliberation, I opted to obtain the offset when a transaction
commits (as opposed to when it starts). This creates some mild
inconvenience, because we prevent the transaction from committing until
the corresponding subscription updates are enqueued.
The strategy is, however, more correct should we ever support weaker
isolation levels, and it is easier to document.

Follow-ups include:

- Provide SQL syntax (`SET synchronous_commit = ON` or something)
- C# and TypeScript SDKs
- Reference docs?
 

# API and ABI breaking changes

Not breaking, but adds a parameter to the subscribe and sql endpoints.


# Expected complexity level and risk

4

To the author's understanding, ordering of outbound messages is not
changed by this patch, even if there are messages that don't have a
transaction offset (such as error messages). I.e. while waiting for the
transaction offset of a message to become durable, no message enqueued
after that message will be delivered. This may not be desirable in some
cases.

The patch may contain concurrency bugs, e.g. awaiting futures that may
never resolve.


# Testing

- [x] Implemented a new test in the `module_subscription_actor` module
- [x] Added unit tests for the core logic in `ClientConnectionReceiver` 

It would be desirable to also have integration-level tests, but I'm
currently unsure how to write those without being able to control if and
when the database reports an offset as durable.

---------

Signed-off-by: Kim Altintop <kim@eagain.io>
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
2025-09-08 17:51:04 +00:00
joshua-spacetime f9c8e72d8f fix incoming message queue length metric (#3172)
# Description of Changes

<!-- Please describe your change, mention any related tickets, and so on
here. -->

We stopped incrementing the incoming queue length metric. This patch
increments it again and adds a regression test.

# API and ABI breaking changes

<!-- If this is an API or ABI breaking change, please apply the
corresponding GitHub label. -->

None

# Expected complexity level and risk

<!--
How complicated do you think these changes are? Grade on a scale from 1
to 5,
where 1 is a trivial change, and 5 is a deep-reaching and complex
change.

This complexity rating applies not only to the complexity apparent in
the diff,
but also to its interactions with existing and future code.

If you answered more than a 2, explain what is complex about the PR,
and what other components it interacts with in potentially concerning
ways. -->

1

# Testing

<!-- Describe any testing you've done, and any testing you'd like your
reviewers to do,
so that you're confident that all the changes work as expected! -->

- [x] Regression test
2025-08-15 15:00:12 +00:00
Phoebe Goldman 3feed08ced Increase default incoming_queue_length limit, log warning when a client violates it (#3171)
# Description of Changes

Closes #3170 .

Commit messages:

### Increase the default incoming-queue-length limit

2048 turned out to be too low a value for BitCraft, as their world
upload process requests on the order of 6000 reducers very rapidly. We
still feel that having a limit is valuable to prevent malicious or
misguided clients from taking an arbitrarily large amount of host
memory, so we bump the value to give us a wide safety error for
BitCraft's needs but don't remove the limit entirely.

### Add log at `warn` when the host disconnects a client due to too many
requests

# API and ABI breaking changes

N/a

# Expected complexity level and risk

1

# Testing

- [x] @mamcx to run a BitCraft bot test.
2025-08-14 22:18:33 +00:00
Kim Altintop b445620f03 Fix module hotswapping for connected clients (#3159)
The `Clone` impl for `ClientConnection` would create an independent
instance that could not observe module hotswapping. This would result in
methods called on a replaced `ModuleHost` to fail, because that host
exited already.

Fix by reading the `ModuleHost` from the watch channel directly, instead
of maintaining a redundant copy.

Also fix `watch_module_host` to properly mark the current module host as
seen.

# Expected complexity level and risk

2

# Testing

- [x] test suite passes
- [x] ran @bfops repro script
2025-08-13 18:36:06 +00:00
Zeke Foppa f6f0909ea4 Update all licenses (#3002)
# Description of Changes

We recently merged several repos together. This PR clarifies the license
terms for several subdirectories, as well as the relationship between
the licenses.

The licenses in our subdirectories have become symbolic links to
licenses in our toplevel `licenses` directory. For any particular
subdirectory's license file in the diff, you can click `... -> View
file` and then click on the text that says "Symbolic Link" on that page.
This will take you to the license file that it links to.

I have also updated the `tools/upgrade-version` script to update the
change date in the new `licenses/BSL.txt` file.

# API and ABI breaking changes

None.

# Expected complexity level and risk

1

# Testing

None. Only changes to license files.

---------

Co-authored-by: Zeke Foppa <bfops@users.noreply.github.com>
2025-08-12 18:20:58 +00:00
Phoebe Goldman c18b291f12 Add additional logging to subscribe route and simplify calling client_connected (#2998)
# Description of Changes

Out-of-band discussions with the BitCraft team brought up questions
about whether it was possible for a rejected client connection to start
an expensive computation like a subscription before their connection was
killed, e.g. by sending a `Subscribe` message along the WebSocket before
`client_connected` had finished returning `Err`.

I don't believe this was actually possible, as `ClientConnection::spawn`
called and awaited `call_identity_connected` before invoking its `actor`
closure, and it was that `actor` which processed `Subscribe` messages.
But it was somewhat difficult to verify that behavior, and so I
re-organized the code so that the outer layer of the `subscribe` handler
obviously had that property without having to step into
`ClientConnection::spawn`.

I also added some additional logging to the subscribe route, including
the `X-Forwarded-For` header in more messages, as the BitCraft team
complained about having difficulty correlating IP addresses with
connections. The log levels remain the same as before, just with
additional information added:

- Successful connections are at `debug` level,
- Rejected connections are at `info` level (these are the ones BitCraft
cares about in this case).
- Failed connections are at `warn`.

As the levels are unchanged, this should not add undesirable log noise.

# API and ABI breaking changes

N/a

# Expected complexity level and risk

3? The `subscribe` route was complex and remains so. I believe this
change simplifies the code and makes the logic more obvious, but
reviewers should take care to verify that the behavior actually is
equivalent as I believe.

# Testing

- [ ] I would like a test deployment of BitCraft to staging or
something, or to include this patch in the next bot test that we do
anyways, just for sanity's sake.
2025-07-29 18:29:04 +00:00
ResuBaka 7fb4df4275 Update tungstenite to get client read performance improvement (#2966)
Co-authored-by: Kim Altintop <kim@eagain.io>
2025-07-24 17:06:43 +00:00
Kim Altintop 09717e97ad Make websocket configurable via config.toml (#2944) 2025-07-16 19:31:07 +00:00
Noa 742303ca49 Bump rust-toolchain to rust 1.88 (#2749)
Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
2025-07-15 17:39:41 +00:00
Tyler Cloutier 20b087c248 Split datastore into its own crate (#2933) 2025-07-12 21:41:00 +00:00
Kim Altintop b63216aec9 client-api: Rewrite websocket loop (#2906)
Signed-off-by: Kim Altintop <kim@eagain.io>
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
2025-07-10 10:50:06 +00:00
joshua-spacetime e4d5c18f3c Wrap websocket flush in timeout (#2908) 2025-07-01 01:41:59 +00:00
joshua-spacetime 28186d8721 Track disconnects initiated by the client (#2893) 2025-06-25 23:28:29 +00:00
joshua-spacetime 718712a8f7 Record the size of a client's outgoing message queue on drop (#2877) 2025-06-18 17:28:55 +00:00
Mazdak Farrokhzad 1318e7e1f5 messages::serialize: take/put buffers from/into a SerializeBufferPool (#2823)
Co-authored-by: Noa <coolreader18@gmail.com>
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
Co-authored-by: Phoebe Goldman <phoebe@goldman-tribe.org>
2025-06-16 16:44:57 +00:00
Phoebe Goldman 1e3e8db9bc Database-specific Timestamp route (#2864) 2025-06-13 20:23:30 +00:00
Tyler Cloutier 013e268eb5 Fixed rename by using put instead of post (#2764)
Co-authored-by: Zeke Foppa <bfops@users.noreply.github.com>
Co-authored-by: Zeke Foppa <196249+bfops@users.noreply.github.com>
Co-authored-by: John Detter <4099508+jdetter@users.noreply.github.com>
2025-06-12 17:05:02 +00:00
Kim Altintop 4974676ce8 client-api: Don't debug-print dropped outgoing Ws messages at info (#2819) 2025-06-11 09:23:41 +00:00
Phoebe Goldman 0866fd39d5 Add an HTTP route to get the current Timestamp (#2850) 2025-06-09 17:26:25 +00:00
Viktor Szépe f6da9e1f5f Fix typos (#2812)
Signed-off-by: Viktor Szépe <viktor@szepe.net>
2025-06-04 16:33:32 +00:00
Phoebe Goldman ac187906ad Per-database incoming and outgoing queue length metrics (#2773) 2025-05-28 16:17:48 +00:00
Kim Altintop c729c2100d Reintroduce replication factor parameter (#2718) 2025-05-19 06:55:09 +00:00
Phoebe Goldman 03a56e11a2 Check validity of modules before control DB modifications during initial publish (#2580) 2025-04-14 15:56:23 +00:00
Noa 483a9488e2 Update rand (#2568) 2025-04-11 17:39:41 +00:00
Noa 68d23d4c25 Remove spacetimedb-core as a dep of cli (#2244) 2025-04-09 19:37:44 +00:00
Noa 649ce34b01 Update tungstenite to 0.26 (#2539) 2025-04-02 18:09:13 +00:00
Mazdak Farrokhzad eb380e6a18 Repair transactionality of st_client removals after disconnect (#2446)
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io>
2025-04-01 14:04:16 +00:00
Jeffrey Dallatezza 64aef29d3c Add endpoints for heap profiling (#2517)
Co-authored-by: Zeke Foppa <196249+bfops@users.noreply.github.com>
2025-03-31 18:12:31 +00:00
Mario Montoya 98395ca530 Print back the # of rows affected (ins, upd, del) with timings (#2462)
Signed-off-by: Mario Montoya <mamcx@elmalabarista.com>
Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
2025-03-28 16:57:23 +00:00
Mario Montoya f9f38543c8 Add readmes to all implementation crates specifying that they do no offer stable interfaces (#2320) 2025-03-06 19:50:17 +00:00
Kim Altintop e32b00420b set names atomic (#2325)
Co-authored-by: Tyler Cloutier <cloutiertyler@aol.com>
Co-authored-by: Noa <coolreader18@gmail.com>
2025-02-28 21:59:23 +00:00
Jeffrey Dallatezza 909040f716 hacky auth check for database creation. (#2308) 2025-02-26 16:52:30 +00:00
Kim Altintop 55cd1e4587 sdk: Client ping on idle connections (#2309)
Co-authored-by: Phoebe Goldman <phoebe@goldman-tribe.org>
2025-02-26 16:22:42 +00:00
Tyler Cloutier d147dff5b1 Added in a SQL execution time calculation (#2297) 2025-02-25 21:44:21 +00:00
Noa f307d531a5 Fixup comment for SpacetimeCreds (#2250) 2025-02-11 20:14:32 +00:00
Noa e76ba2bd35 Only support bearer auth (#2248) 2025-02-11 18:51:05 +00:00
Noa 6a8375260f Refactor HTTP api to better adhere to REST guidelines (real) (#2243) 2025-02-11 02:59:45 +00:00
Noa cf6822541f Remove janky schema from http API (#2181) 2025-02-11 00:56:07 +00:00
Phoebe Goldman aedc601145 Rename Address to ConnectionId (#2220)
Signed-off-by: Phoebe Goldman <phoebe@goldman-tribe.org>
Co-authored-by: James Gilles <jameshgilles@gmail.com>
Co-authored-by: Tyler Cloutier <cloutiertyler@users.noreply.github.com>
2025-02-10 00:40:16 +00:00
Piotr Sarnacki 00c6aa0baf Add /health endpoint (#1989)
Co-authored-by: Tyler Cloutier <cloutiertyler@users.noreply.github.com>
2025-02-08 19:58:14 +00:00
joshua-spacetime 6aa75bd0eb Track compute metrics for sql dml with new engine (#2190) 2025-01-31 17:59:33 +00:00
Noa 293aebaef9 Bump to Rust 1.84 (#2001) 2025-01-28 23:11:29 +00:00