Skip to content
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

Allow SQL interface to be enabled without "experimental" compat flag. #2666

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/workerd/api/actor-state.c++
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,34 @@ jsg::Promise<void> DurableObjectStorage::sync(jsg::Lock& js) {
}
}

jsg::Ref<SqlStorage> DurableObjectStorage::getSql(jsg::Lock& js) {
jsg::Optional<jsg::Ref<SqlStorage>> DurableObjectStorage::getSql(
jsg::Lock& js, CompatibilityFlags::Reader flags) {
KJ_IF_SOME(db, cache->getSqliteDatabase()) {
// Actor is SQLite-backed but let's make sure SQL is configured to be enabled.
if (!enableSql) {
// For backwards-compatibility, if the `experimental` compat flag is on, enable SQL. This is
// deprecated, though, so warn in this case.
if (flags.getWorkerdExperimental()) {
// TODO(soon): Uncomment this warning after the D1 simulator has been updated to use
// `enableSql`. Otherwise, people doing local dev against D1 may see the warning
// spuriously.

// IoContext::current().logWarningOnce(
// "Enabling SQL API based on the 'experimental' flag, but this will stop working soon. "
// "Instead, please set `enableSql = true` in your workerd config for the DO namespace. "
// "If using wrangler, under `[[migrations]]` in wrangler.toml, change `new_classes` to "
// "`new_sqlite_classes`.");
} else {
// SQL is not enabled at all.
return kj::none;
}
}

return jsg::alloc<SqlStorage>(db, JSG_THIS);
} else {
JSG_FAIL_REQUIRE(Error, "Durable Object is not backed by SQL.");
// Not SQLite-backed.
KJ_REQUIRE(!enableSql, "Durable Object is not backed by SQL but enableSql was true?");
return kj::none;
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/workerd/api/actor-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ class DurableObjectTransaction;

class DurableObjectStorage: public jsg::Object, public DurableObjectStorageOperations {
public:
DurableObjectStorage(IoPtr<ActorCacheInterface> cache): cache(kj::mv(cache)) {}
DurableObjectStorage(IoPtr<ActorCacheInterface> cache, bool enableSql)
: cache(kj::mv(cache)),
enableSql(enableSql) {}

ActorCacheInterface& getActorCacheInterface() {
return *cache;
Expand All @@ -199,7 +201,7 @@ class DurableObjectStorage: public jsg::Object, public DurableObjectStorageOpera

jsg::Promise<void> sync(jsg::Lock& js);

jsg::Ref<SqlStorage> getSql(jsg::Lock& js);
jsg::Optional<jsg::Ref<SqlStorage>> getSql(jsg::Lock& js, CompatibilityFlags::Reader flags);

// Get a bookmark for the current state of the database. Note that since this is async, the
// bookmark will include any writes in the current atomic batch, including writes that are
Expand Down Expand Up @@ -233,10 +235,10 @@ class DurableObjectStorage: public jsg::Object, public DurableObjectStorageOpera
JSG_METHOD(deleteAlarm);
JSG_METHOD(sync);

if (flags.getWorkerdExperimental()) {
JSG_LAZY_INSTANCE_PROPERTY(sql, getSql);
JSG_METHOD(transactionSync);
JSG_LAZY_INSTANCE_PROPERTY(sql, getSql);
JSG_METHOD(transactionSync);

if (flags.getWorkerdExperimental()) {
JSG_METHOD(getCurrentBookmark);
JSG_METHOD(getBookmarkForTime);
JSG_METHOD(onNextSessionRestoreBookmark);
Expand Down Expand Up @@ -268,6 +270,7 @@ class DurableObjectStorage: public jsg::Object, public DurableObjectStorageOpera

private:
IoPtr<ActorCacheInterface> cache;
bool enableSql;
uint transactionSyncDepth = 0;
};

Expand Down
6 changes: 5 additions & 1 deletion src/workerd/api/sql-test.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ const config :Workerd.Config = (

const mainWorker :Workerd.Worker = (
compatibilityDate = "2024-03-04",

# "experimental" flag is needed to test `sql.ingest()`
compatibilityFlags = ["experimental", "nodejs_compat"],

modules = [
(name = "worker", esModule = embed "sql-test.js"),
],

durableObjectNamespaces = [
(className = "DurableObjectExample", uniqueKey = "210bd0cbd803ef7883a1ee9d86cce06e"),
( className = "DurableObjectExample",
uniqueKey = "210bd0cbd803ef7883a1ee9d86cce06e",
enableSql = true ),
],

durableObjectStorage = (localDisk = "TEST_TMPDIR"),
Expand Down
21 changes: 16 additions & 5 deletions src/workerd/server/server.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1956,11 +1956,21 @@ public:
});
};

bool enableSql = true;
KJ_SWITCH_ONEOF(config) {
KJ_CASE_ONEOF(c, Durable) {
enableSql = c.enableSql;
}
KJ_CASE_ONEOF(c, Ephemeral) {
enableSql = c.enableSql;
}
}

auto makeStorage =
[](jsg::Lock& js, const Worker::Api& api,
[enableSql = enableSql](jsg::Lock& js, const Worker::Api& api,
ActorCacheInterface& actorCache) -> jsg::Ref<api::DurableObjectStorage> {
return jsg::alloc<api::DurableObjectStorage>(
IoContext::current().addObject(actorCache));
IoContext::current().addObject(actorCache), enableSql);
};

TimerChannel& timerChannel = service;
Expand Down Expand Up @@ -3560,7 +3570,8 @@ void Server::startServices(jsg::V8System& v8System,
hadDurable = true;
serviceActorConfigs.insert(kj::str(ns.getClassName()),
Durable{.uniqueKey = kj::str(ns.getUniqueKey()),
.isEvictable = !ns.getPreventEviction()});
.isEvictable = !ns.getPreventEviction(),
.enableSql = ns.getEnableSql()});
continue;
case config::Worker::DurableObjectNamespace::EPHEMERAL_LOCAL:
if (!experimental) {
Expand All @@ -3569,8 +3580,8 @@ void Server::startServices(jsg::V8System& v8System,
"experimental feature which may change or go away in the future. You must run "
"workerd with `--experimental` to use this feature."));
}
serviceActorConfigs.insert(
kj::str(ns.getClassName()), Ephemeral{.isEvictable = !ns.getPreventEviction()});
serviceActorConfigs.insert(kj::str(ns.getClassName()),
Ephemeral{.isEvictable = !ns.getPreventEviction(), .enableSql = ns.getEnableSql()});
continue;
}
reportConfigError(kj::str("Encountered unknown DurableObjectNamespace type in service \"",
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ class Server final: private kj::TaskSet::ErrorHandler {
struct Durable {
kj::String uniqueKey;
bool isEvictable;
bool enableSql;
};
struct Ephemeral {
bool isEvictable;
bool enableSql;
};
using ActorConfig = kj::OneOf<Durable, Ephemeral>;

Expand Down
8 changes: 8 additions & 0 deletions src/workerd/server/workerd.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@ struct Worker {
# pinned to memory forever, so we provide this flag to change the default behavior.
#
# Note that this is only supported in Workerd; production Durable Objects cannot toggle eviction.

enableSql @4 :Bool;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand how this field would be in a union with ephemeralLocal but it does seem like enableSql would be compatible with preventEviction and uniqueKey. Are we modeling the config this way because those combinations haven't been tested?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it appears we are manually copying this file over here(workers-sdk)? 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field isn't a member of a union. The union ended on line 574.

Also, it appears we are manually copying this file over here(workers-sdk)? 😕

Manual copying is a pretty normal way to share schemas. It's not a big deal, you just overwrite the copy with the updated version if and when you want to change workers-sdk to use the new field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad!

# Whether or not Durable Objects in this namespace can use the `storage.sql` API to execute SQL
# queries.
#
# workerd uses SQLite to back all Durable Objects, but the SQL API is hidden by default to
# emulate behavior of traditional DO namespaces on Cloudflare that aren't SQLite-backed. This
# flag should be enabled when testing code that will run on a SQLite-backed namespace.
}

durableObjectUniqueKeyModifier @8 :Text;
Expand Down
3 changes: 2 additions & 1 deletion src/workerd/tests/test-fixture.c++
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ TestFixture::TestFixture(SetupParams&& params)
auto makeStorage =
[](jsg::Lock& js, const Worker::Api& api,
ActorCacheInterface& actorCache) -> jsg::Ref<api::DurableObjectStorage> {
return jsg::alloc<api::DurableObjectStorage>(IoContext::current().addObject(actorCache));
return jsg::alloc<api::DurableObjectStorage>(
IoContext::current().addObject(actorCache), /*enableSql=*/false);
};
actor = kj::refcounted<Worker::Actor>(*worker, /*tracker=*/kj::none, kj::mv(id),
/*hasTransient=*/false, makeActorCache,
Expand Down