/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import { createEmitter } from "@fluid-internal/client-utils"; import type { Listenable } from "@fluidframework/core-interfaces"; import { assert, fail } from "@fluidframework/core-utils/internal"; import type { SessionId } from "@fluidframework/id-compressor"; import { TelemetryEventBatcher, UsageError, type TelemetryLoggerExt, } from "@fluidframework/telemetry-utils/internal"; import { BTree } from "@tylerbu/sorted-btree-es6"; import { type ChangeFamily, type ChangeFamilyEditor, CommitKind, type GraphCommit, type RevisionTag, findAncestor, findCommonAncestor, mintCommit, type RebaseStatsWithDuration, tagChange, } from "../core/index.js"; import { type Mutable, brand, getOrCreate, mapIterable } from "../util/index.js"; import { SharedTreeBranch, type BranchTrimState, type BranchTrimmingEvents, onForkTransitive, type BranchId, } from "./branch.js"; import type { Commit, SeqNumber, SequenceId, SequencedCommit, SummarySessionBranch, } from "./editManagerFormatCommons.js"; import { getUpperBoundOfPreviousSequenceId, equalSequenceIds, maxSequenceId, minSequenceId, sequenceIdComparator, } from "./sequenceIdUtils.js"; export const minimumPossibleSequenceNumber: SeqNumber = brand(Number.MIN_SAFE_INTEGER); const minimumPossibleSequenceId: SequenceId = { sequenceNumber: minimumPossibleSequenceNumber, }; /** * A special revision tag for the initial {@link EditManager.trunkBase} commit. * @remarks This tag is used to supply the _initial_ trunk base with a known revision. * The trunk base may advance over time, after which point the trunk base will have a different revision. * When {@link EditManager.getSummaryData | serializing} and deserializing, peer branches that include the trunk base commit in their history will always use this tag. */ const rootRevision = "root" as const satisfies RevisionTag; /** * Max number of telemetry log call that may be aggregated before being sent. */ const maxRebaseStatsAggregationCount = 1000; /** * Represents a local branch of a document and interprets the effect on the document of adding sequenced changes, * which were based on a given session's branch, to the document history */ // TODO: Try to reduce this to a single type parameter export class EditManager< TEditor extends ChangeFamilyEditor, TChangeset, TChangeProcessingContext = never, > { private readonly trimmer = createEmitter(); private readonly trimState = { trimmedCommitCount: 0 }; private readonly sharedBranches = new Map< BranchId, SharedBranch >(); /** * Tracks where on the trunk of the main branch all registered branches are based. * Each key is the sequence id of a commit on the trunk, * and the value is the set of all branches who have that commit as their common ancestor with the trunk. * * @remarks * This does not include the local branch. */ private readonly trunkBranches = new BTree< SequenceId, Set> >(undefined, sequenceIdComparator); /** * The sequence number of the newest commit on the trunk that has been received by all peers. * Defaults to {@link minimumPossibleSequenceNumber} if no commits have been received. * * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch. */ private minimumSequenceNumber = minimumPossibleSequenceNumber; /** * Local retention policy, always a boolean or a nonnegative safe integer. * `false` or zero disables extra retention; `true` retains all available history. * A positive safe integer retains the newest specified number of available sequenced main-branch commits. * Collaboration and registered branches can require retaining additional history. */ private historyRetention: boolean | number = false; /** * A special commit that is a "base" (tail) of the trunk, though not part of the trunk itself. * This makes it possible to model the trunk in the same way as any other branch (it branches off of a base commit) * which allows it to use branching APIs to interact with the other branches. * Each time trunk eviction occurs, the most recent evicted commit becomes the new `trunkBase`. */ private trunkBase: GraphCommit; private readonly telemetryEventBatcher: | TelemetryEventBatcher | undefined; /** * @param changeFamily - the change family of changes on the trunk and local branch * @param localSessionId - the id of the local session that will be used for local commits * @param mintRevisionTag - a function which generates globally unique revision tags * @param onSharedBranchCreated - called when a new shared branch is created. This is not called for the main branch. */ public constructor( public readonly changeFamily: ChangeFamily, public readonly localSessionId: SessionId, private readonly mintRevisionTag: () => RevisionTag, private readonly onSharedBranchCreated?: (branchId: BranchId) => void, logger?: TelemetryLoggerExt, ) { this.trunkBase = { revision: rootRevision, change: changeFamily.rebaser.compose([]), customMetadata: undefined, wasTrimmed: true, }; if (logger !== undefined) { this.telemetryEventBatcher = new TelemetryEventBatcher( { eventName: "rebaseProcessing", category: "performance", }, logger, maxRebaseStatsAggregationCount, ); } const mainTrunk = new SharedTreeBranch( this.trunkBase, changeFamily, mintRevisionTag, this.trimState, this.telemetryEventBatcher, ); mainTrunk.trimmer = this.trimmer; this.createAndAddSharedBranch("main", "main", undefined, undefined, mainTrunk); } /** * Gets this manager's current local history retention policy. * @returns The configured boolean or nonnegative safe integer, defaulting to `false`. */ public getHistoryRetention(): boolean | number { return this.historyRetention; } /** * Sets this manager's local retention policy for sequenced main-branch commits. * * @param value - `true` to retain all available history, `false` or zero to disable extra retention, * or a positive safe integer to retain the newest specified number of available commits. * * @remarks * If this method is never called, extra retention is disabled (`false`). * Each call replaces the current policy; there is no separate fallback policy. * * Collaboration and local branches can require retaining more commits than the policy specifies. * Commits that are sequenced in batches are individually counted toward the retention limit. * Unsequenced local commits do not count toward the retention limit. * * Calling this method immediately checks for history that can be trimmed. * Increasing retention preserves available history but does not recover trimmed commits. */ public setHistoryRetention(value: boolean | number): void { assert( typeof value === "boolean" || (Number.isSafeInteger(value) && value >= 0), 0xd79 /* historyRetention must be a boolean or a nonnegative safe integer */, ); this.historyRetention = value; this.trimHistory(); } /** * Returns an inclusive upper bound for commits to be removed by trimming based on the retention policy, * @remarks * The bound need not identify an existing commit. */ private getUpperTrimmingBoundForHistory(): SequenceId { if (this.historyRetention === true) { return minimumPossibleSequenceId; } const sequenceIdToCommit = this.getSharedBranch("main").sequenceIdToCommit; if (this.historyRetention === false || this.historyRetention === 0) { return sequenceIdToCommit.maxKey() ?? minimumPossibleSequenceId; } // The map includes the trimmed base, which does not count toward retention. if (this.historyRetention < sequenceIdToCommit.size - 1) { let remaining = this.historyRetention; for (const [sequenceId] of sequenceIdToCommit.entriesReversed()) { remaining--; if (remaining === 0) { return getUpperBoundOfPreviousSequenceId(sequenceId); } } } return minimumPossibleSequenceId; } /** * Returns an inclusive upper bound for commits to be removed by trimming based on local forks (excluding peer branches maintained by EditManager), * @remarks * The bound need not identify an existing commit. */ private getUpperTrimmingBoundForLocalForks(): SequenceId { const minimumBranchBaseSequenceId = this.trunkBranches.minKey(); return minimumBranchBaseSequenceId === undefined ? (this.getSharedBranch("main").sequenceIdToCommit.maxKey() ?? minimumPossibleSequenceId) : getUpperBoundOfPreviousSequenceId(minimumBranchBaseSequenceId); } /** * Returns an inclusive upper bound for commits to be removed by trimming based on the minimum sequence number, * @remarks * The bound need not identify an existing commit. */ private getUpperTrimmingBoundForMinimumSequenceNumber(): SequenceId { return { sequenceNumber: this.minimumSequenceNumber, indexInBatch: Number.POSITIVE_INFINITY, }; } public getLocalBranch( branchId: BranchId, ): SharedTreeBranch { return this.getSharedBranch(branchId).localBranch; } public getSharedBranchName(branchId: BranchId): string | undefined { return this.getSharedBranch(branchId).branchName; } private getSharedBranch( branchId: BranchId, ): SharedBranch { const branch = this.sharedBranches.get(branchId); if (branch === undefined) { throw new UsageError("No shared branch with such ID"); } return branch; } /** * Make the given branch known to the `EditManager`. The `EditManager` will ensure that all registered * branches remain usable even as the minimum sequence number advances. * * TODO#AB6926: Refactor local branch management into a separate class that encapsulates `trunkBranches` and everything * that touches it. * TODO#AB6925: Maintain the divergence point between each branch and the trunk so that we don't have to recompute * it so often. */ private registerBranch( branch: SharedTreeBranch, ): void { this.trackBranch(branch); // Whenever the branch is rebased, update our record of its base trunk commit const offBeforeRebase = branch.events.on("beforeChange", (args) => { if (args.type === "rebase") { this.untrackBranch(branch); } }); const offAfterRebase = branch.events.on("afterChange", (args) => { if (args.type === "rebase") { this.trackBranch(branch); this.trimHistory(); } }); // When the branch is disposed, update our branch set and trim the trunk const offDispose = branch.events.on("dispose", () => { this.untrackBranch(branch); this.trimHistory(); offBeforeRebase(); offAfterRebase(); offDispose(); }); } private trackBranch( b: SharedTreeBranch, ): void { const main = this.getSharedBranch("main"); const trunkCommit = findCommonAncestor(main.trunk.getHead(), b.getHead()) ?? fail(0xad2 /* Expected branch to be related to main */); const sequenceId = main.getCommitSequenceId(trunkCommit.revision); const branches = getOrCreate(this.trunkBranches, sequenceId, () => new Set()); assert(!branches.has(b), 0x670 /* Branch was registered more than once */); branches.add(b); } private untrackBranch( b: SharedTreeBranch, ): void { const main = this.getSharedBranch("main"); const trunkCommit = findCommonAncestor(main.trunk.getHead(), b.getHead()) ?? fail(0xad3 /* Expected branch to be related to main */); const sequenceId = main.getCommitSequenceId(trunkCommit.revision); const branches = this.trunkBranches.get(sequenceId) ?? fail(0xad4 /* Expected branch to be tracked */); assert(branches.delete(b), 0x671 /* Expected branch to be tracked */); if (branches.size === 0) { this.trunkBranches.delete(sequenceId); } } /** * Return the sequenced number of the latest sequenced change. */ public getLatestSequenceNumber(): SeqNumber | undefined { let maxSequenceNumber: SeqNumber | undefined; for (const branch of this.sharedBranches.values()) { const branchMax = branch.getCommitSequenceId( branch.trunk.getHead().revision, ).sequenceNumber; if (maxSequenceNumber === undefined || maxSequenceNumber < branchMax) { maxSequenceNumber = branchMax; } } return maxSequenceNumber; } /** * Advances the minimum sequence number, and removes all commits from the trunk which lie outside the collaboration window, * if they are not retained by revertibles or local branches. * @param minimumSequenceNumber - the sequence number of the newest commit that all peers (including this one) have received and applied to their trunks. * @param allowHistoryTrimming - Whether this call may trim history according to the retention policy. * * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch. */ public advanceMinimumSequenceNumber( minimumSequenceNumber: SeqNumber, allowHistoryTrimming = true, ): void { if (minimumSequenceNumber === this.minimumSequenceNumber) { return; } assert( minimumSequenceNumber > this.minimumSequenceNumber, 0x476 /* number must be larger or equal to current minimumSequenceNumber. */, ); this.minimumSequenceNumber = minimumSequenceNumber; if (allowHistoryTrimming) { this.trimHistory(); } } /** * Uses the retention policy, latest known minimum sequence number, and registered branch bases * to evict trunk commits that are no longer needed. */ private trimHistory(): void { const msnBound = this.getUpperTrimmingBoundForMinimumSequenceNumber(); const forkBound = this.getUpperTrimmingBoundForLocalForks(); const historyBound = this.getUpperTrimmingBoundForHistory(); const trunkTailSequenceId = minSequenceId( msnBound, minSequenceId(forkBound, historyBound), ); const mainBranch = this.getSharedBranch("main"); const [sequenceId, latestEvicted] = mainBranch.getClosestTrunkCommit( maxSequenceId( trunkTailSequenceId, mainBranch.sequenceIdToCommit.minKey() ?? minimumPossibleSequenceId, ), ); // Don't do any work if the commit found by the search is already the tail of the trunk if (latestEvicted === this.trunkBase) { return; } // This mutation is a performance hack. If commits are truly immutable, then changing the trunk's tail requires // regenerating the entire commit graph. Instead, we can simply chop off the tail like this if we're certain // that there are no outstanding references to any of the commits being removed (other than the references via // the trunk). The peer branches have been rebased to the head of the trunk, the local branch is already rebased // to the head of the trunk, and all other branches are tracked by `trunkBranches` and known to be ahead of or at // `newTrunkBase`. Therefore, no branches should have unique references to any of the commits being evicted here. // We mutate the most recent of the evicted commits to become the new trunk base. That way, any other commits that // have parent pointers to the latest evicted commit will stay linked, even though that it is no longer part of the trunk. const newTrunkBase = latestEvicted as Mutable>; // collect the revisions that will be trimmed to send as part of the branch trimmed event const trimmedCommits = getPathFromBase(newTrunkBase, this.trunkBase) as Mutable< GraphCommit >[]; const trimmedRevisions = trimmedCommits.map((c) => c.revision); // The minimum sequence number informs us that all peer branches are at least caught up to the tail commit, // so rebase them accordingly. This is necessary to prevent peer branches from referencing any evicted commits. mainBranch.trimHistory(latestEvicted, sequenceId); // Custom metadata shares the lifetime of its commit, so it must be dropped when the commit is // trimmed. The new trunk base stays reachable as the sentinel for the remaining history, so its // metadata is cleared rather than trapped; fully evicted commits get the throwing trap below. newTrunkBase.customMetadata = undefined; // Access to the intrinsic properties of trimmed commits indicates a bug in SharedTree. // Throwing lowers the risk of such a bug causing data loss or corruption. for (const commit of trimmedCommits.slice(0, -1)) { Reflect.defineProperty(commit, "change", { get: () => fail(0xa5e /* Should not access 'change' property of an evicted commit */), }); Reflect.defineProperty(commit, "revision", { get: () => assert( false, 0xa5f /* Should not access 'revision' property of an evicted commit */, ), }); Reflect.defineProperty(commit, "customMetadata", { get: () => fail(0xd36 /* Should not access 'customMetadata' property of an evicted commit */), }); Reflect.defineProperty(commit, "parent", { get: () => assert(false, 0xa60 /* Should not access 'parent' property of an evicted commit */), }); commit.wasTrimmed = true; } // Dropping the parent field (transitively) removes references to the evicted commits so they can be garbage collected. // Allowing the `parent` property to be read from the trunk base (despite it being a trimmed commit) spares readers from having to check `wasTrimmed` in order to safely find the root commit. delete newTrunkBase.parent; newTrunkBase.wasTrimmed = true; this.trunkBase = newTrunkBase; this.trimState.trimmedCommitCount += trimmedRevisions.length; this.trimmer.emit("ancestryTrimmed", trimmedRevisions); } public isEmpty(): boolean { for (const branch of this.sharedBranches.values()) { if (!branch.isEmpty(this.trunkBase)) { return false; } } return this.minimumSequenceNumber === minimumPossibleSequenceNumber; } public getSummaryData(): SummaryData { // Trimming the trunk before serializing ensures that the trunk data in the summary is as minimal as possible. this.trimHistory(); const mainBranch = this.getSharedBranch("main"); const minSeqNumberToSummarize: SequenceId = { sequenceNumber: brand(this.minimumSequenceNumber + 1), }; const historyBound = this.getUpperTrimmingBoundForHistory(); let minBaseSeqId = minSequenceId( minSeqNumberToSummarize, mainBranch.sequenceIdToCommit.nextHigherKey(historyBound) ?? minSeqNumberToSummarize, ); const branches = new Map>(); for (const [branchId, branch] of this.sharedBranches) { if (branchId !== "main") { const branchSummary = branch.getSummaryData( minSeqNumberToSummarize, this.trunkBase.revision, ); branches.set(branchId, branchSummary); assert(branchSummary.base !== undefined, 0xc57 /* Branch summary must have a base */); const baseSequenceId = mainBranch.getCommitSequenceId(branchSummary.base); minBaseSeqId = minSequenceId(minBaseSeqId, baseSequenceId); } } const mainSummary = mainBranch.getSummaryData(minBaseSeqId, this.trunkBase.revision); return { main: mainSummary, branches, originator: this.localSessionId }; } public loadSummaryData(data: SummaryData): void { assert( this.isEmpty(), 0x68a /* Attempted to load from summary after edit manager was already mutated */, ); // Record the tags of each trunk commit as we generate the trunk so they can be looked up quickly // when hydrating the peer branches below const trunkRevisionCache = new Map>(); trunkRevisionCache.set(this.trunkBase.revision, this.trunkBase); const mainBranch = this.sharedBranches.get("main") ?? fail(0xc58 /* Main branch must exist */); mainBranch.loadSummaryData(data.main, trunkRevisionCache); if (data.branches !== undefined) { for (const [branchId, branchData] of data.branches) { const baseCommit = trunkRevisionCache.get(branchData.base ?? rootRevision) ?? fail(0xd87 /* Expected summary branch base to be in trunk cache */); const branch = this.createSharedBranch( branchId, branchData.name, branchData.session, mainBranch, mainBranch.trunk.fork(baseCommit), ); branch.loadSummaryData(branchData, trunkRevisionCache); this.addSharedBranch(branchId, branch); } } // Summaries do not currently include a minimum sequence number,so the loader must conservatively retain all revisions from the summary. // This prevents the trimming of superfluous commits when the summarizer's history retention policy is more generous than the loader's policy. // If this changes, uncomment the next line to trim superfluous commits. // this.trimHistory(); } public getTrunkHead(branchId: BranchId): GraphCommit { return this.getSharedBranch(branchId).trunk.getHead(); } public getTrunkChanges(branchId: BranchId): TChangeset[] { return this.getTrunkCommits(branchId).map((c) => c.change); } public getTrunkCommits(branchId: BranchId): GraphCommit[] { return getPathFromBase(this.getTrunkHead(branchId), this.trunkBase); } public getLocalCommits(branchId: BranchId): readonly GraphCommit[] { const branch = this.getSharedBranch(branchId); return branch.getLocalCommits(); } /** * Gets the length of the longest branch maintained by this `EditManager`. * This may be the length of a peer branch or the local branch. * * @remarks * The length is counted from the lowest common ancestor with the trunk such that a fully sequenced branch would * have length zero. */ public getLongestBranchLength(): number { let max = 0; for (const branch of this.sharedBranches.values()) { max = Math.max(max, branch.getLongestBranchLength()); } return max; } public sequenceBranchCreation( sessionId: SessionId, referenceSequenceNumber: SeqNumber, branchId: BranchId, branchName?: string, ): void { if (sessionId === this.localSessionId) { assert(this.sharedBranches.has(branchId), 0xc59 /* Expected branch to already exist */); return; } const mainBranch = this.getSharedBranch("main"); const branchTrunk = mainBranch.rebasePeer(sessionId, referenceSequenceNumber).fork(); this.createAndAddSharedBranch(branchId, branchName, sessionId, mainBranch, branchTrunk); } public addNewBranch(branchId: BranchId, branchName?: string): void { const main = this.getSharedBranch("main") ?? fail(0xc5a /* Main branch must exist */); this.createAndAddSharedBranch( branchId, branchName, this.localSessionId, main, this.getLocalBranch("main").fork(), ); } public getSharedBranchIds(): BranchId[] { return [...this.sharedBranches.keys()]; } public removeBranch(branchId: BranchId): void { assert(branchId !== "main", 0xc5b /* Cannot remove main branch */); const hadBranch = this.sharedBranches.delete(branchId); assert(hadBranch, 0xc5c /* Expected branch to exist */); } private createAndAddSharedBranch( branchId: BranchId, branchName: string | undefined, sessionId: SessionId | undefined, parent: SharedBranch | undefined, branch: SharedTreeBranch, ): SharedBranch { const sharedBranch = this.createSharedBranch( branchId, branchName, sessionId, parent, branch, ); this.addSharedBranch(branchId, sharedBranch); return sharedBranch; } private addSharedBranch( branchId: BranchId, branch: SharedBranch, ): void { assert( !this.sharedBranches.has(branchId), 0xc5d /* A branch with this ID already exists */, ); this.sharedBranches.set(branchId, branch); // Track all forks of the local branch for purposes of trunk eviction. Unlike the local branch, they have // an unknown lifetime and rebase frequency, so we can not make any assumptions about which trunk commits // they require and therefore we monitor them explicitly. onForkTransitive(branch.localBranch, (fork) => this.registerBranch(fork)); if (branchId !== "main") { this.registerBranch(branch.localBranch); this.onSharedBranchCreated?.(branchId); } } private createSharedBranch( branchId: BranchId, branchName: string | undefined, sessionId: SessionId | undefined, parent: SharedBranch | undefined, branch: SharedTreeBranch, ): SharedBranch { const sharedBranch = new SharedBranch( parent, branch, branchId, branchName, sessionId, minimumPossibleSequenceId, this.changeFamily, this.mintRevisionTag, this.trimmer, this.trimState, this.telemetryEventBatcher, ); return sharedBranch; } /* eslint-disable jsdoc/check-indentation */ /** * Add a bunch of sequenced changes. A bunch is a group of sequenced commits that have the following properties: * - They are not interleaved with messages from other DDSes in the container. * - They are all part of the same batch, which entails: * - They are contiguous in sequencing order. * - They are all from the same client. * - They are all based on the same reference sequence number. * - They are not interleaved with messages from other clients. * * @param allowHistoryTrimming - Whether this call may trim history according to the retention policy after sequencing the entire bunch. * Set to false for detached edits to defer trimming. */ /* eslint-enable jsdoc/check-indentation */ public addSequencedChanges( newCommits: readonly GraphCommit[], sessionId: SessionId, sequenceNumber: SeqNumber, referenceSequenceNumber: SeqNumber, branchId: BranchId, allowHistoryTrimming = true, ): void { assert(newCommits.length > 0, 0xad8 /* Expected at least one sequenced change */); assert( sequenceNumber > this.minimumSequenceNumber, 0x713 /* Expected change sequence number to exceed the last known minimum sequence number */, ); const branch = this.getSharedBranch(branchId); const onSequenceLocalCommit = ( commit: GraphCommit, sequenceId: SequenceId, previousSequenceId: SequenceId, ): void => { branch.localBranch.commitWasSequenced(commit); // Next, we need to update the sequence IDs that our local branches (user's branches, not peer branches) are associated with. // In particular, if a local branch is based on the previous trunk head (the branch's first ancestor in the trunk is the commit that was the head before we pushed the new commit) // and also branches off of the local branch (it has an ancestor that is part of the local branch), it needs to have its sequence number advanced to be that of the new trunk head. // Intuitively, this makes sense because: // 1. The trunk's head just advanced forward by some (sequence) amount. // 2. The local branch is always rebased to be branching off of the head of the trunk (not literally in this case, because of the optimization, but in effect). // 3. Therefore, the entire local branch just advanced forward by some (sequence) amount, and any commits downstream of it which track the sequence numbers of their base commits on the trunk should also advance. // This update is not necessarily required for all local branches, since some may have fallen behind the local branch and are based on older trunk commits (such branches do not need updating). const currentBranches = this.trunkBranches.get(previousSequenceId); if (currentBranches !== undefined) { const newBranches = getOrCreate(this.trunkBranches, sequenceId, () => new Set()); for (const forkedBranch of currentBranches) { // Check every branch associated with the old sequence ID and advance it if it is based on the local branch (specifically, on the local branch as it was before we pushed its first commit to the trunk). // We validate this by checking if the branch's head is a descendant of the local commit that we just pushed. if (findAncestor(forkedBranch.getHead(), (c) => c === commit) !== undefined) { newBranches.add(forkedBranch); currentBranches.delete(forkedBranch); forkedBranch.commitWasSequenced(commit); } } // Clean up our trunk branches map by removing any empty sets. if (currentBranches.size === 0) { this.trunkBranches.delete(previousSequenceId); } if (newBranches.size === 0) { this.trunkBranches.delete(sequenceId); } } }; const areLocalCommits = sessionId === this.localSessionId; branch.addSequencedChanges( newCommits, sessionId, sequenceNumber, areLocalCommits, referenceSequenceNumber, onSequenceLocalCommit, ); if (allowHistoryTrimming) { this.trimHistory(); } } public findLocalCommit( branchId: BranchId, revision: RevisionTag, ): [commit: GraphCommit, commitsAfter: GraphCommit[]] { const commits: GraphCommit[] = []; const commit = findAncestor( [this.getSharedBranch(branchId).localBranch.getHead(), commits], (c) => c.revision === revision, ); assert(commit !== undefined, 0x599 /* Expected local branch to contain revision */); return [commit, commits]; } } /** * The in-memory data that summaries contain */ export interface SummaryData { readonly originator?: SessionId; readonly main: SharedBranchSummaryData; readonly branches?: ReadonlyMap>; } export interface SharedBranchSummaryData { readonly id?: BranchId; readonly name?: string; readonly session?: SessionId; readonly author?: string; readonly base?: RevisionTag; readonly trunk: readonly SequencedCommit[]; readonly peerLocalBranches: ReadonlyMap>; } /** * Gets the path from the base of a branch to its head. */ function getPathFromBase( branchHead: TCommit, baseBranchHead: TCommit, ): TCommit[] { const path: TCommit[] = []; assert( findCommonAncestor([branchHead, path], baseBranchHead) !== undefined, 0x573 /* Expected branches to be related */, ); return path; } class SharedBranch { /** * This branch holds the changes made by this client which have not yet been confirmed as sequenced changes. */ public readonly localBranch: SharedTreeBranch; /** * Branches are maintained to represent the local change list that the issuing client had * at the time of submitting the latest known edit on the branch. * This means the head commit of each branch is always in its original (non-rebased) form. */ private readonly peerLocalBranches: Map< SessionId, SharedTreeBranch > = new Map(); /** * A map from a sequence id to the commit which has that sequence id. * This also includes an entry for the {@link trunkBase} which always has the lowest key in the map. */ public readonly sequenceIdToCommit = new BTree>( undefined, sequenceIdComparator, ); /** * The list of commits (from oldest to most recent) that are on the local branch but not on the trunk. * When a local commit is sequenced, the first commit in this list shifted onto the tip of the trunk. */ private readonly localCommits: GraphCommit[] = []; /** * Records extra data associated with sequenced commits. * This does not include an entry for the {@link trunkBase}. */ private readonly commitMetadata = new Map< RevisionTag, { sequenceId: SequenceId; sessionId: SessionId } >(); public constructor( public readonly parentBranch: | SharedBranch | undefined, public readonly trunk: SharedTreeBranch, private readonly id: BranchId, public readonly branchName: string | undefined, private readonly sessionId: SessionId | undefined, baseCommitSequenceId: SequenceId, private readonly changeFamily: ChangeFamily, private readonly mintRevisionTag: () => RevisionTag, branchTrimmer: Listenable, private readonly trimState: BranchTrimState, telemetryEventBatcher: TelemetryEventBatcher | undefined, ) { this.localBranch = new SharedTreeBranch( this.trunk.getHead(), changeFamily, mintRevisionTag, trimState, telemetryEventBatcher, ); this.localBranch.trimmer = branchTrimmer; this.sequenceIdToCommit.set(baseCommitSequenceId, this.trunk.getHead()); this.localBranch.events.on("afterChange", (event) => { if (event.type === "append") { for (const commit of event.newCommits) { this.localCommits.push(commit); } } else { this.localCommits.length = 0; findCommonAncestor( [this.localBranch.getHead(), this.localCommits], this.trunk.getHead(), ); } }); } public addSequencedChanges( newCommits: readonly GraphCommit[], sessionId: SessionId, sequenceNumber: SeqNumber, areLocalCommits: boolean, referenceSequenceNumber: SeqNumber, onSequenceLocalCommit: OnSequenceCommit, ): void { assert( sequenceNumber >= // This is ">=", not ">" because changes in the same batch will have the same sequence number (this.sequenceIdToCommit.maxKey()?.sequenceNumber ?? minimumPossibleSequenceNumber), 0xa64 /* Attempted to sequence change with an outdated sequence number */, ); const batchSize = this.getBatchSize(sequenceNumber); // The sequence id for the next commit to be processed in the bunch. let nextSequenceId = batchSize === 0 ? { sequenceNumber, } : { sequenceNumber, indexInBatch: batchSize, }; // Local changes, i.e., changes from this client are applied by fast forwarding the local branch commit onto // the trunk. if (areLocalCommits) { for (const { revision } of newCommits) { this.sequenceLocalCommit(revision, nextSequenceId, sessionId, onSequenceLocalCommit); nextSequenceId = getNextSequenceId(nextSequenceId); } return; } // Remote changes, i.e., changes from remote clients are applied in three steps. // Step 1 - Recreate the peer remote client's local environment. // Get the revision that the remote change is based on and rebase that peer local branch over the part of the // trunk up to the base revision. This will be a no-op if the sending client has not advanced since the last // time we received an edit from it const peerLocalBranch = this.rebasePeer(sessionId, referenceSequenceNumber); // Step 2 - Append the changes to the peer branch and rebase the changes to the tip of the trunk. if (peerLocalBranch.getHead() === this.trunk.getHead()) { // If the peer local branch is fully caught up and empty (no changes relative to the trunk) after being // rebased, then push changes to the trunk directly and update the peer branch to the trunk's head. for (const newCommit of newCommits) { this.pushCommitToTrunk(nextSequenceId, { ...newCommit, sessionId }); nextSequenceId = getNextSequenceId(nextSequenceId); } peerLocalBranch.setHead(this.trunk.getHead(), newCommits.length); } else { // Otherwise, push the changes to the peer local branch and merge the branch into the trunk. for (const newCommit of newCommits) { peerLocalBranch.apply( tagChange(newCommit.change, newCommit.revision), CommitKind.Default, newCommit.customMetadata, ); } const result = this.trunk.merge(peerLocalBranch); if (result !== undefined) { // If the merge resulted in any changes to the trunk, update the sequence map and trunk metadata // with the rebased commits. for (const sourceCommit of result.sourceCommits) { this.sequenceIdToCommit.set(nextSequenceId, sourceCommit); this.commitMetadata.set(sourceCommit.revision, { sequenceId: nextSequenceId, sessionId, }); nextSequenceId = getNextSequenceId(nextSequenceId); } } } // Step 3 - Rebase the local branch over the updated trunk. this.localBranch.rebaseOnto(this.trunk); } public isEmpty(baseCommit: GraphCommit): boolean { return ( this.trunk.getHead() === baseCommit && this.peerLocalBranches.size === 0 && this.localBranch.getHead() === this.trunk.getHead() ); } public rebasePeer( sessionId: SessionId, referenceSequenceNumber: SeqNumber, ): SharedTreeBranch { const [, baseRevisionInTrunk] = this.getClosestTrunkCommit(referenceSequenceNumber); const peerLocalBranch = getOrCreate( this.peerLocalBranches, sessionId, () => new SharedTreeBranch( baseRevisionInTrunk, this.changeFamily, this.mintRevisionTag, this.trimState, ), ); peerLocalBranch.rebaseOnto(this.trunk, baseRevisionInTrunk); return peerLocalBranch; } public getPeerBranchOrTrunk( sessionId: SessionId, ): SharedTreeBranch { return this.peerLocalBranches.get(sessionId) ?? this.trunk; } /** * Gets the length of the longest branch maintained by this `SharedBranch`. * This may be the length of a peer branch or the local branch. * * @remarks * The length is counted from the lowest common ancestor with the trunk such that a fully sequenced branch would * have length zero. */ public getLongestBranchLength(): number { let max = 0; const trunkHead = this.trunk.getHead(); for (const branch of this.peerLocalBranches.values()) { const branchPath = getPathFromBase(branch.getHead(), trunkHead); if (branchPath.length > max) { max = branchPath.length; } } const localPath = getPathFromBase(this.localBranch.getHead(), trunkHead); return Math.max(max, localPath.length); } public trimHistory(newBase: GraphCommit, sequenceId: SequenceId): void { this.rebasePeers(newBase); this.sequenceIdToCommit.editRange( minimumPossibleSequenceId, sequenceId, true, (s, commit) => { // Cleanup look-aside data for each evicted commit this.commitMetadata.delete(commit.revision); // Delete all evicted commits from `sequenceMap` except for the latest one, which is the new `trunkBase` if (equalSequenceIds(s, sequenceId)) { assert( commit === newBase, 0x729 /* Expected last evicted commit to be new trunk base */, ); } else { return { delete: true }; } }, ); const trunkSize = getPathFromBase(this.trunk.getHead(), newBase).length; assert( this.sequenceIdToCommit.size === trunkSize + 1, 0x744 /* The size of the sequenceMap must have one element more than the trunk */, ); assert( this.commitMetadata.size === trunkSize, 0x745 /* The size of the trunkMetadata must be the same as the trunk */, ); } private rebasePeers(commit: GraphCommit): void { for (const [, branch] of this.peerLocalBranches) { branch.rebaseOnto(this.trunk, commit); } // The metadata for new trunk base revision needs to be deleted before modifying it. this.commitMetadata.delete(commit.revision); } public getLocalCommits(): readonly GraphCommit[] { return this.localCommits; } /** * Promote the oldest un-sequenced commit on the local branch to the head of the trunk. * @param sequenceId - The sequence id of the new trunk commit * @remarks This method is a performance optimization for the scenario where this client receives its own change back after sequencing. * The normal (not optimized) process in this case would be to apply the new sequenced commit to the trunk and then rebase the local branch over the trunk. * The first commit will be "the same" (as in, it will have the same revision) as the commit that was just sequenced, so the rebase will be a no-op. * Because the rebase is a no-op, we can skip it entirely and simply remove the commit from the local branch and append it to the trunk. * Avoiding the overhead of the rebase process, even when it's a no-op, has real measured performance benefits and is worth the added complexity here. */ private sequenceLocalCommit( revision: RevisionTag, sequenceId: SequenceId, sessionId: SessionId, onSequenceLocalCommit: OnSequenceCommit, ): void { if (this.commitMetadata.has(revision)) { // This can happen if the commit came from a shared branch and was concurrently merged by another client. // In this case, the newly sequenced commit is redundant and should therefore be ignored. return; } // First, push the local commit to the trunk. // We are mutating our `localCommits` cache here,but there is no need to actually change the `localBranch` itself because it will simply catch up later if/when it next rebases. const firstLocalCommit = this.localCommits.shift(); assert( firstLocalCommit !== undefined, 0x6b5 /* Received a sequenced change from the local session despite having no local changes */, ); const prevSequenceId = this.getCommitSequenceId(this.trunk.getHead().revision); this.pushGraphCommitToTrunk(sequenceId, firstLocalCommit, sessionId); onSequenceLocalCommit(firstLocalCommit, sequenceId, prevSequenceId); } /** * Finds the most recent trunk commit that was sequenced at or before the given point. * @param searchBy - the sequence number or the sequence id to search for * @remarks Fails if there is no eligible commit. * @returns the closest commit and its sequence id */ public getClosestTrunkCommit(searchBy: SeqNumber): [SequenceId, GraphCommit]; public getClosestTrunkCommit(searchBy: SequenceId): [SequenceId, GraphCommit]; public getClosestTrunkCommit( searchBy: SeqNumber | SequenceId, ): [SequenceId, GraphCommit] { const sequenceId: SequenceId = typeof searchBy === "number" ? { // This is to make sure that the correct commit is selected in this 2 scenarios: // 1) The commit is unique for that sequence number // 2) There are more than one commit for the same sequence number, in this case we need to select the last one. sequenceNumber: searchBy, indexInBatch: Number.POSITIVE_INFINITY, } : searchBy; const commit = this.sequenceIdToCommit.getPairOrNextLower(sequenceId); assert(commit !== undefined, 0x746 /* sequence id has been evicted */); return commit; } private pushGraphCommitToTrunk( sequenceId: SequenceId, graphCommit: GraphCommit, sessionId: SessionId, ): void { this.trunk.setHead(graphCommit, 1); this.registerSequencedCommit(sequenceId, sessionId, graphCommit); } private pushCommitToTrunk(sequenceId: SequenceId, commit: Commit): void { const mintedCommit = mintCommit(this.trunk.getHead(), commit); this.pushGraphCommitToTrunk(sequenceId, mintedCommit, commit.sessionId); } private registerSequencedCommit( sequenceId: SequenceId, sessionId: SessionId, commit: GraphCommit, ): void { this.sequenceIdToCommit.set(sequenceId, commit); this.commitMetadata.set(commit.revision, { sequenceId, sessionId }); } public getCommitSequenceId(commitRevision: RevisionTag): SequenceId { const id = this.commitMetadata.get(commitRevision)?.sequenceId; if (id === undefined) { return minimumPossibleSequenceId; } return id; } // TODO: Document that this is to handle receiving separate commits with the same sequence ID, // as a batch of changes are not guaranteed to be processed as one bunch. private getBatchSize(sequenceNumber: SeqNumber): number { const startSequenceId: SequenceId = { sequenceNumber, }; const endSequenceId: SequenceId = { sequenceNumber: brand((sequenceNumber as number) + 1), }; return this.sequenceIdToCommit.getRange(startSequenceId, endSequenceId, false).length; } public getSummaryData( minSeqNumberToSummarize: SequenceId, trunkBaseRevision: RevisionTag, ): SharedBranchSummaryData { // The assert below is acceptable at present because summarization only ever occurs on a client with no // local/in-flight changes. // In the future we may wish to relax this constraint. For that to work, the current implementation of // `EditManager` would have to be amended in one of two ways: // A) Changes made by the local session should be represented by a branch in `EditManager.branches`. // B) The contents of such a branch should be computed on demand based on the trunk. // Note that option (A) would be a simple change to `addSequencedChanges` whereas (B) would likely require // rebasing trunk changes over the inverse of trunk changes. assert( this.localBranch.getHead() === this.trunk.getHead(), 0xc5e /* Clients with local changes cannot be used to generate summaries */, ); let parentHead: GraphCommit; if (this.parentBranch === undefined) { const oldestCommitInCollabWindow = this.getClosestTrunkCommit(minSeqNumberToSummarize)[1]; // Path construction is exclusive, so we need to use the parent of the oldest commit in the window if it exists parentHead = oldestCommitInCollabWindow.parent ?? oldestCommitInCollabWindow; } else { parentHead = this.parentBranch.trunk.getHead(); } const childBranchTrunkCommits: GraphCommit[] = []; const forkPointFromMainTrunk = findCommonAncestor( [this.trunk.getHead(), childBranchTrunkCommits], parentHead, ); assert( forkPointFromMainTrunk !== undefined, 0xc5f /* Expected child branch to be based on main branch */, ); const trunk = childBranchTrunkCommits.map((c) => { const metadata = this.commitMetadata.get(c.revision) ?? fail(0xad5 /* Expected metadata for trunk commit */); const commit: SequencedCommit = { change: c.change, revision: c.revision, sequenceNumber: metadata.sequenceId.sequenceNumber, sessionId: metadata.sessionId, customMetadata: c.customMetadata, }; if (metadata.sequenceId.indexInBatch !== undefined) { commit.indexInBatch = metadata.sequenceId.indexInBatch; } return commit; }); const peerLocalBranches = new Map>( mapIterable(this.peerLocalBranches.entries(), ([sessionId, branch]) => { const branchPath: GraphCommit[] = []; const ancestor = findCommonAncestor([branch.getHead(), branchPath], this.trunk.getHead()) ?? fail(0xad6 /* Expected branch to be based on trunk */); const base = ancestor.revision === trunkBaseRevision ? rootRevision : ancestor.revision; return [ sessionId, { base, commits: branchPath.map((c) => { const commit: Commit = { change: c.change, revision: c.revision, sessionId, customMetadata: c.customMetadata, }; return commit; }), }, ]; }), ); const trunkBase = this.parentBranch === undefined ? undefined : forkPointFromMainTrunk.revision; return { trunk, peerLocalBranches, base: trunkBase, id: this.id, name: this.branchName, session: this.sessionId, }; } public loadSummaryData( data: SharedBranchSummaryData, trunkRevisionCache: Map>, ): void { assert( (this.parentBranch === undefined) === (data.base === undefined), 0xc60 /* Expected branch base to match presence of parent branch */, ); let trunkHead = this.trunk.getHead(); assert( trunkHead.revision === (data.base ?? rootRevision) && this.localBranch.getHead() === trunkHead, 0xc61 /* Expected base revision to be in trunk cache */, ); for (const c of data.trunk) { const sequenceId: SequenceId = c.indexInBatch === undefined ? { sequenceNumber: c.sequenceNumber, } : { sequenceNumber: c.sequenceNumber, indexInBatch: c.indexInBatch, }; const commit = mintCommit(trunkHead, c); this.sequenceIdToCommit.set(sequenceId, commit); this.commitMetadata.set(c.revision, { sequenceId, sessionId: c.sessionId, }); trunkRevisionCache.set(c.revision, commit); trunkHead = commit; } this.trunk.setHead(trunkHead, data.trunk.length); this.localBranch.setHead(this.trunk.getHead(), data.trunk.length); for (const [sessionId, branch] of data.peerLocalBranches) { const commit = trunkRevisionCache.get(branch.base) ?? fail(0xad7 /* Expected summary branch to be based off of a revision in the trunk */); let branchHead = commit; for (const c of branch.commits) { branchHead = mintCommit(branchHead, c); } this.peerLocalBranches.set( sessionId, new SharedTreeBranch( branchHead, this.changeFamily, this.mintRevisionTag, this.trimState, ), ); } } } // Returns the sequence id for the next commit to be processed in the bunch. Since all the commits have the // same sequence number, only the index in the batch needs to be incremented. function getNextSequenceId(sequenceId: SequenceId): SequenceId { return { sequenceNumber: sequenceId.sequenceNumber, indexInBatch: (sequenceId.indexInBatch ?? 0) + 1, }; } type OnSequenceCommit = ( commit: GraphCommit, sequenceId: SequenceId, prevSequenceId: SequenceId, ) => void;