| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {AbsolutePath, RepositoryError, ValidatedRepoInfo} from 'isl/src/types'; |
| b69ab31 | | | 9 | import type {RepositoryContext} from './serverTypes'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {TypedEventEmitter} from 'shared/TypedEventEmitter'; |
| b69ab31 | | | 12 | import {ensureTrailingPathSep} from 'shared/pathUtils'; |
| b69ab31 | | | 13 | import {Repository} from './Repository'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | /** |
| b69ab31 | | | 16 | * Reference-counting access to a {@link Repository}, via a Promise. |
| b69ab31 | | | 17 | * Be sure to `unref()` when no longer needed. |
| b69ab31 | | | 18 | */ |
| b69ab31 | | | 19 | export interface RepositoryReference { |
| b69ab31 | | | 20 | promise: Promise<Repository | RepositoryError>; |
| b69ab31 | | | 21 | unref: () => unknown; |
| b69ab31 | | | 22 | } |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | /** |
| b69ab31 | | | 25 | * We return `RepositoryReference`s synchronously before we have the Repository, |
| b69ab31 | | | 26 | * but reference counts should be associated with the actual async constructed Repository, |
| b69ab31 | | | 27 | * which is why we can't return RefCounted<Repository> directly. |
| b69ab31 | | | 28 | */ |
| b69ab31 | | | 29 | class RepositoryReferenceImpl implements RepositoryReference { |
| b69ab31 | | | 30 | constructor( |
| b69ab31 | | | 31 | public promise: Promise<Repository | RepositoryError>, |
| b69ab31 | | | 32 | private disposeFunc: () => void, |
| b69ab31 | | | 33 | ) {} |
| b69ab31 | | | 34 | public unref() { |
| b69ab31 | | | 35 | if (!this.disposed) { |
| b69ab31 | | | 36 | this.disposed = true; |
| b69ab31 | | | 37 | this.disposeFunc(); |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | internalReference: RefCounted<Repository> | undefined; |
| b69ab31 | | | 42 | disposed = false; |
| b69ab31 | | | 43 | } |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | class RefCounted<T extends {dispose: () => void}> { |
| b69ab31 | | | 46 | constructor(public value: T) {} |
| b69ab31 | | | 47 | private references = 0; |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | public isDisposed = false; |
| b69ab31 | | | 50 | public ref() { |
| b69ab31 | | | 51 | this.references++; |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | public getNumberOfReferences() { |
| b69ab31 | | | 54 | return this.references; |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | public dispose() { |
| b69ab31 | | | 57 | this.references--; |
| b69ab31 | | | 58 | if (!this.isDisposed && this.references === 0) { |
| b69ab31 | | | 59 | this.isDisposed = true; |
| b69ab31 | | | 60 | this.value.dispose(); |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | } |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | class RepoMap { |
| b69ab31 | | | 66 | /** |
| b69ab31 | | | 67 | * Previously distributed RepositoryReferences, keyed by repository root path |
| b69ab31 | | | 68 | * Note that Repositories do not define their own `cwd`, and can be reused across cwds. |
| b69ab31 | | | 69 | */ |
| b69ab31 | | | 70 | private reposByRoot = new Map<AbsolutePath, RefCounted<Repository>>(); |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | /** |
| b69ab31 | | | 73 | * Ordered by length from longest to shortest, for quick longest prefix match. |
| b69ab31 | | | 74 | * Used with reposByRoot to achieve O(n) prefix lookup. |
| b69ab31 | | | 75 | */ |
| b69ab31 | | | 76 | private orderedRoots: string[] = []; |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | private static cmpRoots = (a: string, b: string) => b.length - a.length; |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | public get(repoRoot: AbsolutePath): RefCounted<Repository> | undefined { |
| b69ab31 | | | 81 | return this.reposByRoot.get(repoRoot); |
| b69ab31 | | | 82 | } |
| b69ab31 | | | 83 | |
| b69ab31 | | | 84 | public getLongestPrefixMatch(path: AbsolutePath): RefCounted<Repository> | undefined { |
| b69ab31 | | | 85 | for (const repoRoot of this.orderedRoots) { |
| b69ab31 | | | 86 | if (path === repoRoot || path.startsWith(ensureTrailingPathSep(repoRoot))) { |
| b69ab31 | | | 87 | return this.reposByRoot.get(repoRoot); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | return undefined; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | public set(repoRoot: AbsolutePath, repoRef: RefCounted<Repository>) { |
| b69ab31 | | | 94 | this.reposByRoot.set(repoRoot, repoRef); |
| b69ab31 | | | 95 | this.orderedRoots.push(repoRoot); |
| b69ab31 | | | 96 | this.orderedRoots.sort(RepoMap.cmpRoots); |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | public values(): IterableIterator<RefCounted<Repository>> { |
| b69ab31 | | | 100 | return this.reposByRoot.values(); |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | public forEach(callback: (value: RefCounted<Repository>) => void) { |
| b69ab31 | | | 104 | this.reposByRoot.forEach(callback); |
| b69ab31 | | | 105 | } |
| b69ab31 | | | 106 | } |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | /** |
| b69ab31 | | | 109 | * Allow reusing Repository instances by storing instances by path, |
| b69ab31 | | | 110 | * and controlling how Repositories are created. |
| b69ab31 | | | 111 | * |
| b69ab31 | | | 112 | * Some async work is needed to construct repositories (finding repo root dir), |
| b69ab31 | | | 113 | * so it's possible to duplicate some work if multiple repos are constructed at similar times. |
| b69ab31 | | | 114 | * We still enable Repository reuse in this case by double checking for pre-existing Repos at the last second. |
| b69ab31 | | | 115 | */ |
| b69ab31 | | | 116 | class RepositoryCache { |
| b69ab31 | | | 117 | // allow mocking Repository in tests |
| b69ab31 | | | 118 | constructor(private RepositoryType = Repository) {} |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | private repoMap = new RepoMap(); |
| b69ab31 | | | 121 | private activeReposEmitter = new TypedEventEmitter<'change', undefined>(); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | private lookup(dirGuess: AbsolutePath): RefCounted<Repository> | undefined { |
| b69ab31 | | | 124 | const found = this.repoMap.get(dirGuess); |
| b69ab31 | | | 125 | return found && !found.isDisposed ? found : undefined; |
| b69ab31 | | | 126 | } |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | // Longest match is necessary because repos can be nested (as submodules). |
| b69ab31 | | | 129 | private lookupByPrefix(dirGuess: AbsolutePath): RefCounted<Repository> | undefined { |
| b69ab31 | | | 130 | const found = this.repoMap.getLongestPrefixMatch(dirGuess); |
| b69ab31 | | | 131 | return found && !found.isDisposed ? found : undefined; |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | /** |
| b69ab31 | | | 135 | * Create a new Repository, or reuse if one already exists. |
| b69ab31 | | | 136 | * Repositories are reference-counted to ensure they can be disposed when no longer needed. |
| b69ab31 | | | 137 | */ |
| b69ab31 | | | 138 | getOrCreate(ctx: RepositoryContext): RepositoryReference { |
| b69ab31 | | | 139 | // Fast path: if this cwd is already a known repo root, we can use it directly. |
| b69ab31 | | | 140 | // This only works if the cwd happens to be the repo root. |
| b69ab31 | | | 141 | const found = this.lookup(ctx.cwd); |
| b69ab31 | | | 142 | if (found) { |
| b69ab31 | | | 143 | found.ref(); |
| b69ab31 | | | 144 | return new RepositoryReferenceImpl(Promise.resolve(found.value), () => found.dispose()); |
| b69ab31 | | | 145 | } |
| b69ab31 | | | 146 | |
| b69ab31 | | | 147 | // More typically, we can reuse a Repository among different cwds: |
| b69ab31 | | | 148 | |
| b69ab31 | | | 149 | // eslint-disable-next-line prefer-const |
| b69ab31 | | | 150 | let ref: RepositoryReferenceImpl; |
| b69ab31 | | | 151 | const lookupRepoInfoAndReuseIfPossible = async (): Promise<Repository | RepositoryError> => { |
| b69ab31 | | | 152 | // TODO: we should rate limit how many getRepoInfos we run at a time, and make other callers just wait. |
| b69ab31 | | | 153 | // this would guard against querying lots of redundant paths within the same repo. |
| b69ab31 | | | 154 | // This is probably not necessary right now, but would be useful for a VS Code extension where we need to query |
| b69ab31 | | | 155 | // individual file paths to add diff gutters. |
| b69ab31 | | | 156 | const repoInfo = await this.RepositoryType.getRepoInfo(ctx); |
| b69ab31 | | | 157 | // important: there should be no `await` points after here, to ensure there is no race when reusing Repositories. |
| b69ab31 | | | 158 | if (repoInfo.type !== 'success') { |
| b69ab31 | | | 159 | // No repository found at this root, or some other error prevents the repo from being created |
| b69ab31 | | | 160 | return repoInfo; |
| b69ab31 | | | 161 | } |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | if (ref.disposed) { |
| b69ab31 | | | 164 | // If the reference is disposed, the caller gave up while waiting for the repo to be created. |
| b69ab31 | | | 165 | // make sure we don't create a dangling Repository. |
| b69ab31 | | | 166 | return {type: 'unknownError', error: new Error('Repository already disposed')}; |
| b69ab31 | | | 167 | } |
| b69ab31 | | | 168 | |
| b69ab31 | | | 169 | // Now that we've spent some async time to determine this repo's actual root, |
| b69ab31 | | | 170 | // we can check if we already have a reference to it saved. |
| b69ab31 | | | 171 | // This way, we can still reuse a Repository, and only risk duplicating `getRepoInfo` work. |
| b69ab31 | | | 172 | const newlyFound = this.lookup(repoInfo.repoRoot); |
| b69ab31 | | | 173 | if (newlyFound) { |
| b69ab31 | | | 174 | // if it is found now, it means either the cwd differs from the repo root (lookup fails), or |
| b69ab31 | | | 175 | // another instance was created at the same time and finished faster than this one (lookup failed before, but succeeds now). |
| b69ab31 | | | 176 | |
| b69ab31 | | | 177 | newlyFound.ref(); |
| b69ab31 | | | 178 | ref.internalReference = newlyFound; |
| b69ab31 | | | 179 | return newlyFound.value; |
| b69ab31 | | | 180 | } |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | // This is where we actually start new subscriptions and trigger work, so we should only do this |
| b69ab31 | | | 183 | // once we're sure we don't have a repository to reuse. |
| b69ab31 | | | 184 | const repo = new this.RepositoryType( |
| b69ab31 | | | 185 | repoInfo as ValidatedRepoInfo, // repoInfo is now guaranteed to have these root/dotdir set |
| b69ab31 | | | 186 | ctx, |
| b69ab31 | | | 187 | ); |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | const internalRef = new RefCounted(repo); |
| b69ab31 | | | 190 | internalRef.ref(); |
| b69ab31 | | | 191 | ref.internalReference = internalRef; |
| b69ab31 | | | 192 | this.repoMap.set(repoInfo.repoRoot, internalRef); |
| b69ab31 | | | 193 | this.activeReposEmitter.emit('change'); |
| b69ab31 | | | 194 | return repo; |
| b69ab31 | | | 195 | }; |
| b69ab31 | | | 196 | ref = new RepositoryReferenceImpl(lookupRepoInfoAndReuseIfPossible(), () => { |
| b69ab31 | | | 197 | if (ref.internalReference) { |
| b69ab31 | | | 198 | ref.internalReference.dispose(); |
| b69ab31 | | | 199 | } |
| b69ab31 | | | 200 | ref.unref(); |
| b69ab31 | | | 201 | }); |
| b69ab31 | | | 202 | return ref; |
| b69ab31 | | | 203 | } |
| b69ab31 | | | 204 | |
| b69ab31 | | | 205 | /** |
| b69ab31 | | | 206 | * Lookup a cached repository without creating a new one if it doesn't exist |
| b69ab31 | | | 207 | */ |
| b69ab31 | | | 208 | public cachedRepositoryForPath(path: AbsolutePath): Repository | undefined { |
| b69ab31 | | | 209 | const ref = this.lookupByPrefix(path); |
| b69ab31 | | | 210 | return ref?.value; |
| b69ab31 | | | 211 | } |
| b69ab31 | | | 212 | |
| b69ab31 | | | 213 | public onChangeActiveRepos(cb: (repos: Array<Repository>) => unknown): () => unknown { |
| b69ab31 | | | 214 | const onChange = () => { |
| b69ab31 | | | 215 | cb([...this.repoMap.values()].map(ref => ref.value)); |
| b69ab31 | | | 216 | }; |
| b69ab31 | | | 217 | this.activeReposEmitter.on('change', onChange); |
| b69ab31 | | | 218 | // start with initial repos set |
| b69ab31 | | | 219 | onChange(); |
| b69ab31 | | | 220 | return () => this.activeReposEmitter.off('change', onChange); |
| b69ab31 | | | 221 | } |
| b69ab31 | | | 222 | |
| b69ab31 | | | 223 | /** Clean up all known repos. Mostly useful for testing. */ |
| b69ab31 | | | 224 | clearCache() { |
| b69ab31 | | | 225 | this.repoMap.forEach(repo => repo.dispose()); |
| b69ab31 | | | 226 | this.repoMap = new RepoMap(); |
| b69ab31 | | | 227 | this.activeReposEmitter.removeAllListeners(); |
| b69ab31 | | | 228 | } |
| b69ab31 | | | 229 | |
| b69ab31 | | | 230 | public numberOfActiveServers(): number { |
| b69ab31 | | | 231 | let numActive = 0; |
| b69ab31 | | | 232 | for (const repo of this.repoMap.values()) { |
| b69ab31 | | | 233 | numActive += repo.getNumberOfReferences(); |
| b69ab31 | | | 234 | } |
| b69ab31 | | | 235 | return numActive; |
| b69ab31 | | | 236 | } |
| b69ab31 | | | 237 | } |
| b69ab31 | | | 238 | |
| b69ab31 | | | 239 | export const __TEST__ = {RepositoryCache, RepoMap, RefCounted}; |
| b69ab31 | | | 240 | |
| b69ab31 | | | 241 | export const repositoryCache = new RepositoryCache(); |