Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions packages/realm-server/handlers/handle-archive-realm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type Koa from 'koa';
import {
archiveRealm,
cancelAllJobsInConcurrencyGroup,
createResponse,
logger,
SupportedMimeType,
Expand Down Expand Up @@ -32,6 +33,16 @@ export default function handleArchiveRealm({

try {
await archiveRealm(dbAdapter, new URL(realmURL));
// Stop the realm's indexer: cancel any in-flight from-scratch /
// incremental-index job and drop the pending queue for this realm's
// concurrency group. Mirrors the realm-level cancel-jobs endpoint
// (Realm.handleCancelJobsRequest). `cancelAllJobsInConcurrencyGroup`
// marks jobs rejected and emits NOTIFY jobs_finished so peer
// replicas evict job-scoped search-cache rows. The unarchive flow
// rebuilds boxel_index from disk via the full-reindex enqueue, so
// any partial work left behind by an in-flight cancellation is
// discarded on restore.
await cancelAllJobsInConcurrencyGroup(dbAdapter, `indexing:${realmURL}`);

let response = createResponse({
body: JSON.stringify(
Expand Down
9 changes: 8 additions & 1 deletion packages/realm-server/lib/full-reindex-realm-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ type RealmRegistryRow = {
url: string;
};

// The system-wide full-reindex source list. Archived realms are sealed and
// their contents can't drift while archived, so the sweep skips them — a
// realm rejoins this list when unarchive clears archived_at, and the
// unarchive handler separately enqueues the one-time reindex that brings
// boxel_index back up to date.
export async function getFullReindexRealmUrls(dbAdapter: DBAdapter) {
let rows = (await query(dbAdapter, [
`SELECT url FROM realm_registry ORDER BY url`,
`SELECT url FROM realm_registry
WHERE url NOT IN (SELECT url FROM realm_metadata WHERE archived_at IS NOT NULL)
ORDER BY url`,
])) as RealmRegistryRow[];

return rows.map(({ url }) => url);
Expand Down
53 changes: 52 additions & 1 deletion packages/realm-server/tests/full-reindex-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import type {
VirtualNetwork,
} from '@cardstack/runtime-common';
import {
archiveRealm,
fullReindex,
insertPermissions,
logger,
unarchiveRealm,
uuidv4,
} from '@cardstack/runtime-common';

import { upsertPublishedRealmInRegistry } from '../lib/realm-registry-writes.ts';
import { getFullReindexRealmUrls } from '../lib/full-reindex-realm-urls.ts';
import {
insertSourceRealmInRegistry,
upsertPublishedRealmInRegistry,
} from '../lib/realm-registry-writes.ts';
import { setupDB } from './helpers/index.ts';

module(basename(import.meta.filename), function (hooks) {
Expand Down Expand Up @@ -174,4 +180,49 @@ module(basename(import.meta.filename), function (hooks) {
'no jobs are enqueued for bot-owned realms',
);
});

module('getFullReindexRealmUrls', function () {
async function seedSourceRealm(realmURL: string) {
await insertSourceRealmInRegistry(dbAdapter, {
url: realmURL,
diskId: uuidv4(),
ownerUsername: '@owner:localhost',
});
}

test('returns only active realms from realm_registry', async function (assert) {
const activeA = 'http://example.com/active-a/';
const activeB = 'http://example.com/active-b/';
const archived = 'http://example.com/archived/';

await seedSourceRealm(activeA);
await seedSourceRealm(activeB);
await seedSourceRealm(archived);
await archiveRealm(dbAdapter, new URL(archived));

let urls = await getFullReindexRealmUrls(dbAdapter);
assert.deepEqual(
[...urls].sort(),
[activeA, activeB].sort(),
'archived realms are excluded from the sweep source',
);
});

test('an unarchived realm returns to the sweep source', async function (assert) {
const realmURL = 'http://example.com/restored/';

await seedSourceRealm(realmURL);
await archiveRealm(dbAdapter, new URL(realmURL));
assert.notOk(
(await getFullReindexRealmUrls(dbAdapter)).includes(realmURL),
'archived realm is absent',
);

await unarchiveRealm(dbAdapter, new URL(realmURL));
assert.ok(
(await getFullReindexRealmUrls(dbAdapter)).includes(realmURL),
'unarchived realm reappears',
);
});
});
});
40 changes: 40 additions & 0 deletions packages/realm-server/tests/server-endpoints/archive-realm-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { basename } from 'path';
import { v4 as uuidv4 } from 'uuid';
import {
archiveRealm,
FROM_SCRATCH_JOB_TIMEOUT_SEC,
insertPermissions,
isRealmArchived,
param,
query,
systemInitiatedPriority,
type RealmPermissions,
} from '@cardstack/runtime-common';
import { realmSecretSeed } from '../helpers/index.ts';
Expand Down Expand Up @@ -122,6 +124,44 @@ module(`server-endpoints/${basename(import.meta.filename)}`, function () {
);
});

test('POST /_archive-realm cancels pending indexing jobs for the realm', async function (assert) {
const owner = '@archive-owner:localhost';
const realmURL = makeRealmURL();
await seedSourceRealm(realmURL, {
[owner]: ['read', 'write', 'realm-owner'],
});

let pending = await context.publisher.publish<void>({
jobType: 'from-scratch-index',
concurrencyGroup: `indexing:${realmURL}`,
timeout: FROM_SCRATCH_JOB_TIMEOUT_SEC,
priority: systemInitiatedPriority,
args: {
realmURL,
realmUsername: 'archive-owner',
clearLastModified: false,
},
});

let response = await context.request
.post('/_archive-realm')
.set('Accept', 'application/vnd.api+json')
.set('Content-Type', 'application/json')
.set('Authorization', authHeader(owner))
.send(JSON.stringify({ data: { type: 'realm', id: realmURL } }));
assert.strictEqual(response.status, 200, 'HTTP 200 status');

let rows = (await context.dbAdapter.execute(
`SELECT status FROM jobs WHERE id = $1`,
{ bind: [pending.id] },
)) as { status: string }[];
assert.strictEqual(
rows[0]?.status,
'rejected',
'the pending indexing job is marked rejected',
);
});

test('POST /_archive-realm returns 403 for a non-owner', async function (assert) {
const owner = '@archive-owner:localhost';
const intruder = '@intruder:localhost';
Expand Down
Loading