/** * @module sync-clone-concurrency.respec * * Test Suite 4: Active Concurrent Commits vs Clone Swarm Stress Test. * Verifies that while Alice is actively, concurrently committing new frames to her repo, * multiple independent Bob clients (Bob1, Bob2, Bob3) can concurrently cloneReadOnly * the pinned point-in-time snapshot without lock contention, deadlocks, or corruption. */ import { respecfully, ifWe, iReckon } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { clone, delay, extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../core-constants.mjs'; import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs'; import { getFromSpace } from '../witness/space/space-helper.mjs'; import { Metaspace_Innerspace } from '../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { InnerSpace_V1 } from '../witness/space/inner-space/inner-space-v1.mjs'; import { createTimelineRootTestHelper } from '../test-helpers.mjs'; import { mut8Timeline } from '../timeline/timeline-api.mjs'; import { DEFAULT_INNER_SPACE_DATA_V1 } from '../witness/space/inner-space/inner-space-types.mjs'; import { SyncPeerInnerspace_V1 } from './sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs'; import { SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1 } from './sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs'; import { createReadOnlyMountedSpace } from '../witness/space/readonly-space-wrapper.mjs'; import { fnObs } from '../common/pubsub/observer/observer-helper.mjs'; const logalot = GLOBAL_LOG_A_LOT; const lc = `[sync-clone-concurrency.respec.mts]`; interface ConcurrencyTestData { type?: string; commitN?: number; tag?: string; } await respecfully(lc, `SyncSagaCoordinator Concurrency & Swarm Clone Stress`, async () => { await ifWe(lc, `serves concurrent clone swarms while upstream publisher is actively committing`, async () => { // 1. Setup Metaspace const metaspace = new Metaspace_Innerspace(undefined); await metaspace.initialize({ getFnAlert: () => async () => {}, getFnPrompt: () => async () => '', getFnPromptPassword: () => async () => null, }); while (!metaspace.initialized) { await delay(10); } // 2. Setup Alice's Space const aliceDomainSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `alice_concurrent_domain_${Date.now()}`, uuid: `alice_concurrent_uuid_${Date.now()}`, }); await aliceDomainSpace.initialized; // Seed initial commit const root = await createTimelineRootTestHelper({ ib: 'concurrent_repo_root', data: { type: 'repo_root', commitN: 0, tag: 'v0.0' }, space: aliceDomainSpace, }); const rootAddr = getIbGibAddr({ ibGib: root }); let currentTip = await mut8Timeline({ timeline: root, mut8Opts: { dataToAddOrPatch: { type: 'repo_commit', commitN: 1, tag: 'v1.0-pinned' }, }, metaspace, space: aliceDomainSpace, }); // Pinned target snapshot address for all Bob clones const pinnedSnapshotAddr = getIbGibAddr({ ibGib: currentTip }); // Mount Alice's space as ReadOnlyMountedSpace for the server const aliceReadOnlySpace = createReadOnlyMountedSpace(aliceDomainSpace); await aliceReadOnlySpace.initialized; // 3. Helper to create a Bob client clone worker async function runBobCloneWorker(workerId: string) { const bobClientControlSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `bob_ctrl_${workerId}_${Date.now()}`, uuid: `bob_ctrl_uuid_${workerId}_${Date.now()}`, }); await bobClientControlSpace.initialized; const bobClientDomainSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `bob_dom_${workerId}_${Date.now()}`, uuid: `bob_dom_uuid_${workerId}_${Date.now()}`, }); await bobClientDomainSpace.initialized; const bobServerControlSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `bob_srv_ctrl_${workerId}_${Date.now()}`, uuid: `bob_srv_ctrl_uuid_${workerId}_${Date.now()}`, }); await bobServerControlSpace.initialized; const bobCoordinator = new SyncSagaCoordinator(); const serverCoordinator = new SyncSagaCoordinator(); const peer = new SyncPeerInnerspace_V1(clone(SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1)); await peer.initialized; await peer.initializeOpts({ localMetaspace: metaspace, controlSpace: bobClientControlSpace, domainSpace: bobClientDomainSpace, receiverControlSpace: bobServerControlSpace, receiverDomainSpace: aliceReadOnlySpace, receiverCoordinator: serverCoordinator, receiverMetaspace: metaspace, }); const cloneRes = await bobCoordinator.cloneReadOnly({ peer, targetAddrs: [pinnedSnapshotAddr], controlSpace: bobClientControlSpace, domainSpace: bobClientDomainSpace, metaspace, }); const sub = await cloneRes.updates$.subscribe(fnObs({ next: async () => {}, error: async (err) => { console.error(`[Bob ${workerId}] error: ${extractErrorMsg(err)}`); }, complete: async () => {}, })); await cloneRes.done; await sub.unsubscribe(); // Verify worker received the pinned point-in-time snapshot const pinnedInBob = await getFromSpace({ space: bobClientDomainSpace, addr: pinnedSnapshotAddr }); iReckon(lc, pinnedInBob.success).isGonnaBeTrue(); iReckon(lc, (pinnedInBob.ibGibs?.[0]?.data as any)?.tag).willEqual('v1.0-pinned'); const rootInBob = await getFromSpace({ space: bobClientDomainSpace, addr: rootAddr }); iReckon(lc, rootInBob.success).isGonnaBeTrue(); return { workerId, clientControlStones: Object.keys(bobClientControlSpace.ibGibs || {}).length, serverControlStones: Object.keys(bobServerControlSpace.ibGibs || {}).length, domainStones: Object.keys(bobClientDomainSpace.ibGibs || {}).length, }; } // 4. Background concurrent task: Alice continues active development let aliceCommitCount = 0; async function runAliceConcurrentWriter() { for (let i = 2; i <= 6; i++) { await delay(15); currentTip = await mut8Timeline({ timeline: currentTip, mut8Opts: { dataToAddOrPatch: { type: 'repo_commit', commitN: i, tag: `v${i}.0-concurrent` }, }, metaspace, space: aliceDomainSpace, }); aliceCommitCount++; } } // 5. Execute Alice concurrent writes and 3 concurrent Bob clone swarms in parallel! const [_, bob1Result, bob2Result, bob3Result] = await Promise.all([ runAliceConcurrentWriter(), runBobCloneWorker('bob1'), runBobCloneWorker('bob2'), runBobCloneWorker('bob3'), ]); // 6. Assertions iReckon(lc, aliceCommitCount).willEqual(5); iReckon(lc, bob1Result.domainStones >= 2).isGonnaBeTrue(); iReckon(lc, bob2Result.domainStones >= 2).isGonnaBeTrue(); iReckon(lc, bob3Result.domainStones >= 2).isGonnaBeTrue(); iReckon(lc, bob1Result.clientControlStones > 0).isGonnaBeTrue(); iReckon(lc, bob2Result.clientControlStones > 0).isGonnaBeTrue(); iReckon(lc, bob3Result.clientControlStones > 0).isGonnaBeTrue(); // 7. Verify Alice's space contains only Alice's commits and 0 audit trail frames const aliceAddrs = Object.keys(aliceDomainSpace.ibGibs || {}); for (const addr of aliceAddrs) { iReckon(lc, addr.includes('sync_saga')).isGonnaBeFalse(); iReckon(lc, addr.includes('sync_msg')).isGonnaBeFalse(); } }); });