Skip to content

Commit

Permalink
Merge pull request #2824 from cloudflare/milan/create-replicas
Browse files Browse the repository at this point in the history
Add experimental method to create DO replicas
  • Loading branch information
MellowYarker authored Nov 12, 2024
2 parents 22adba4 + 437e838 commit 968d761
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/workerd/api/actor-state.c++
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ kj::Promise<void> DurableObjectStorage::waitForBookmark(kj::String bookmark) {
return cache->waitForBookmark(bookmark);
}

void DurableObjectStorage::ensureReplicas() {
return cache->ensureReplicas();
}

ActorCacheOps& DurableObjectTransaction::getCache(OpName op) {
JSG_REQUIRE(!rolledBack, Error, kj::str("Cannot ", op, " on rolled back transaction"));
auto& result = *JSG_REQUIRE_NONNULL(cacheTxn, Error,
Expand Down
10 changes: 10 additions & 0 deletions src/workerd/api/actor-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ class DurableObjectStorage: public jsg::Object, public DurableObjectStorageOpera
// `bookmark`.
kj::Promise<void> waitForBookmark(kj::String bookmark);

// Arrange to create replicas for this Durable Object.
//
// Once a Durable Object instance calls `ensureReplicas`, all subsequent calls will be no-ops,
// thus it is idempotent.
void ensureReplicas();

JSG_RESOURCE_TYPE(DurableObjectStorage, CompatibilityFlags::Reader flags) {
JSG_METHOD(get);
JSG_METHOD(list);
Expand All @@ -258,6 +264,10 @@ class DurableObjectStorage: public jsg::Object, public DurableObjectStorageOpera
JSG_METHOD(waitForBookmark);
}

if (flags.getReplicaRouting()) {
JSG_METHOD(ensureReplicas);
}

JSG_TS_OVERRIDE({
get<T = unknown>(key: string, options?: DurableObjectGetOptions): Promise<T | undefined>;
get<T = unknown>(keys: string[], options?: DurableObjectGetOptions): Promise<Map<string, T>>;
Expand Down
23 changes: 0 additions & 23 deletions src/workerd/io/actor-cache.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3339,27 +3339,4 @@ kj::Maybe<ActorCache::KeyPtr> ActorCache::Transaction::putImpl(
return KeyPtr(slot.entry->key);
}
}

// =======================================================================================

kj::Promise<kj::String> ActorCacheInterface::getCurrentBookmark() {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

kj::Promise<kj::String> ActorCacheInterface::getBookmarkForTime(kj::Date timestamp) {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

kj::Promise<kj::String> ActorCacheInterface::onNextSessionRestoreBookmark(kj::StringPtr bookmark) {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

kj::Promise<void> ActorCacheInterface::waitForBookmark(kj::StringPtr bookmark) {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

} // namespace workerd
30 changes: 25 additions & 5 deletions src/workerd/io/actor-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <workerd/io/actor-storage.capnp.h>
#include <workerd/jsg/exception.h>

#include <kj/async.h>
#include <kj/debug.h>
Expand Down Expand Up @@ -234,11 +235,30 @@ class ActorCacheInterface: public ActorCacheOps {
virtual kj::Maybe<kj::Promise<void>> onNoPendingFlush() = 0;

// Implements the respective PITR API calls. The default implementations throw JSG errors saying
// PITR is not implemented.
virtual kj::Promise<kj::String> getCurrentBookmark();
virtual kj::Promise<kj::String> getBookmarkForTime(kj::Date timestamp);
virtual kj::Promise<kj::String> onNextSessionRestoreBookmark(kj::StringPtr bookmark);
virtual kj::Promise<void> waitForBookmark(kj::StringPtr bookmark);
// PITR is not implemented. These methods are meant to be implemented internally.
virtual kj::Promise<kj::String> getCurrentBookmark() {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

virtual kj::Promise<kj::String> getBookmarkForTime(kj::Date timestamp) {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

virtual kj::Promise<kj::String> onNextSessionRestoreBookmark(kj::StringPtr bookmark) {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

virtual kj::Promise<void> waitForBookmark(kj::StringPtr bookmark) {
JSG_FAIL_REQUIRE(
Error, "This Durable Object's storage back-end does not implement point-in-time recovery.");
}

virtual void ensureReplicas() {
JSG_FAIL_REQUIRE(Error, "This Durable Object's storage back-end does not support replication.");
}
};

// An in-memory caching layer on top of ActorStorage.Stage RPC interface.
Expand Down

0 comments on commit 968d761

Please sign in to comment.