| 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 {PageVisibility, ValidatedRepoInfo} from 'isl/src/types'; |
| b69ab31 | | | 9 | import type {PageFocusTracker} from './PageFocusTracker'; |
| b69ab31 | | | 10 | import type {Logger} from './logger'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import fs from 'node:fs/promises'; |
| b69ab31 | | | 13 | import path from 'node:path'; |
| b69ab31 | | | 14 | import {debounce} from 'shared/debounce'; |
| b69ab31 | | | 15 | import {Internal} from './Internal'; |
| b69ab31 | | | 16 | import {stagedThrottler} from './StagedThrottler'; |
| b69ab31 | | | 17 | import type {SubscriptionCallback} from './__generated__/node-edenfs-notifications-client'; |
| b69ab31 | | | 18 | import {EdenFSUtils} from './__generated__/node-edenfs-notifications-client'; |
| b69ab31 | | | 19 | import {type ServerSideTracker} from './analytics/serverSideTracker'; |
| b69ab31 | | | 20 | import {ONE_MINUTE_MS} from './constants'; |
| b69ab31 | | | 21 | import {EdenFSNotifications} from './edenFsNotifications'; |
| b69ab31 | | | 22 | import type {RepositoryContext} from './serverTypes'; |
| b69ab31 | | | 23 | import {Watchman} from './watchman'; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | const DEFAULT_POLL_INTERVAL = 15 * ONE_MINUTE_MS; |
| b69ab31 | | | 26 | // When the page is hidden, aggressively reduce polling. |
| b69ab31 | | | 27 | const HIDDEN_POLL_INTERVAL = 3 * 60 * ONE_MINUTE_MS; |
| b69ab31 | | | 28 | // When visible or focused, poll frequently |
| b69ab31 | | | 29 | const VISIBLE_POLL_INTERVAL = 2 * ONE_MINUTE_MS; |
| b69ab31 | | | 30 | const FOCUSED_POLL_INTERVAL = 0.5 * ONE_MINUTE_MS; |
| b69ab31 | | | 31 | const ON_FOCUS_REFETCH_THROTTLE = 15_000; |
| b69ab31 | | | 32 | const ON_VISIBLE_REFETCH_THROTTLE = 30_000; |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | export type KindOfChange = 'uncommitted changes' | 'commits' | 'merge conflicts' | 'everything'; |
| b69ab31 | | | 35 | export type PollKind = PageVisibility | 'force'; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | /** |
| b69ab31 | | | 38 | * Handles watching for changes to files on disk which should trigger refetching data, |
| b69ab31 | | | 39 | * and polling for changes when watching is not reliable. |
| b69ab31 | | | 40 | */ |
| b69ab31 | | | 41 | export class WatchForChanges { |
| b69ab31 | | | 42 | static WATCHMAN_DEFER = `hg.update`; // TODO: update to sl |
| b69ab31 | | | 43 | static WATCHMAN_DEFER_TRANSACTION = `hg.transaction`; // TODO: update to sl |
| b69ab31 | | | 44 | public watchman: Watchman; |
| b69ab31 | | | 45 | public edenfs: EdenFSNotifications; |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | private dirstateDisposables: Array<() => unknown> = []; |
| b69ab31 | | | 48 | private watchmanDisposables: Array<() => unknown> = []; |
| b69ab31 | | | 49 | private edenfsDisposables: Array<() => unknown> = []; |
| b69ab31 | | | 50 | private logger: Logger; |
| b69ab31 | | | 51 | private tracker: ServerSideTracker; |
| b69ab31 | | | 52 | private dirstateSubscriptionPromise: Promise<void>; |
| b69ab31 | | | 53 | |
| 4fe1f34 | | | 54 | public onWatchmanStatusChange: ((status: Watchman['status']) => void) | undefined; |
| 4fe1f34 | | | 55 | |
| b69ab31 | | | 56 | constructor( |
| b69ab31 | | | 57 | private repoInfo: ValidatedRepoInfo, |
| b69ab31 | | | 58 | private pageFocusTracker: PageFocusTracker, |
| b69ab31 | | | 59 | private changeCallback: (kind: KindOfChange, pollKind?: PollKind) => unknown, |
| b69ab31 | | | 60 | ctx: RepositoryContext, |
| b69ab31 | | | 61 | watchman?: Watchman | undefined, |
| b69ab31 | | | 62 | edenfs?: EdenFSNotifications | undefined, |
| b69ab31 | | | 63 | ) { |
| b69ab31 | | | 64 | this.logger = ctx.logger; |
| b69ab31 | | | 65 | this.tracker = ctx.tracker; |
| b69ab31 | | | 66 | this.watchman = watchman ?? new Watchman(ctx.logger, ctx.tracker); |
| 4fe1f34 | | | 67 | this.watchman.onStatusChange = status => this.onWatchmanStatusChange?.(status); |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | const {repoRoot} = this.repoInfo; |
| b69ab31 | | | 70 | this.edenfs = edenfs ?? new EdenFSNotifications(ctx.logger, repoRoot); |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | // Watch dirstate right away for commit changes |
| b69ab31 | | | 73 | this.dirstateSubscriptionPromise = this.setupDirstateSubscriptions(ctx); |
| b69ab31 | | | 74 | this.setupPolling(); |
| b69ab31 | | | 75 | this.pageFocusTracker.onChange(this.poll.bind(this)); |
| b69ab31 | | | 76 | // poll right away so we get data immediately, without waiting for timeout on startup |
| b69ab31 | | | 77 | this.poll('force'); |
| b69ab31 | | | 78 | } |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | private timeout: NodeJS.Timeout | undefined; |
| b69ab31 | | | 81 | private lastFetch = new Date().valueOf(); |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | /** |
| b69ab31 | | | 84 | * Waits for the dirstate subscription to be set up |
| b69ab31 | | | 85 | * since we can't await in the constructor |
| b69ab31 | | | 86 | * Resolves when dirstateSubscriptionPromise is fulfilled. |
| b69ab31 | | | 87 | */ |
| b69ab31 | | | 88 | public async waitForDirstateSubscriptionReady(): Promise<void> { |
| b69ab31 | | | 89 | await this.dirstateSubscriptionPromise; |
| b69ab31 | | | 90 | } |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | /** |
| b69ab31 | | | 93 | * Combine different signals to determine what interval to poll for information |
| b69ab31 | | | 94 | */ |
| b69ab31 | | | 95 | private setupPolling() { |
| b69ab31 | | | 96 | this.timeout = setTimeout(this.poll, DEFAULT_POLL_INTERVAL); |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | /** |
| b69ab31 | | | 100 | * Re-trigger fetching data from the repository, |
| b69ab31 | | | 101 | * depending on how recently that data was last fetched, |
| b69ab31 | | | 102 | * and whether any ISL windows are focused or visible. |
| b69ab31 | | | 103 | * |
| b69ab31 | | | 104 | * This function calls itself on an interval to check whether we should fetch changes, |
| b69ab31 | | | 105 | * but it can also be called in response to events like focus being gained. |
| b69ab31 | | | 106 | */ |
| b69ab31 | | | 107 | public poll = (kind?: PollKind) => { |
| b69ab31 | | | 108 | // calculate how long we'd like to be waiting from what we know of the windows. |
| b69ab31 | | | 109 | let desiredNextTickTime = DEFAULT_POLL_INTERVAL; |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | if (this.repoInfo.isEdenFs !== true) { |
| b69ab31 | | | 112 | if (this.watchman.status !== 'healthy') { |
| b69ab31 | | | 113 | if (this.pageFocusTracker.hasPageWithFocus()) { |
| b69ab31 | | | 114 | desiredNextTickTime = FOCUSED_POLL_INTERVAL; |
| b69ab31 | | | 115 | } else if (this.pageFocusTracker.hasVisiblePage()) { |
| b69ab31 | | | 116 | desiredNextTickTime = VISIBLE_POLL_INTERVAL; |
| b69ab31 | | | 117 | } |
| b69ab31 | | | 118 | } else { |
| b69ab31 | | | 119 | // if watchman is working normally, and we're not visible, don't poll nearly as often |
| b69ab31 | | | 120 | if (!this.pageFocusTracker.hasPageWithFocus() && !this.pageFocusTracker.hasVisiblePage()) { |
| b69ab31 | | | 121 | desiredNextTickTime = HIDDEN_POLL_INTERVAL; |
| b69ab31 | | | 122 | } |
| b69ab31 | | | 123 | } |
| b69ab31 | | | 124 | } else { |
| b69ab31 | | | 125 | // if using eden and we're not visible, don't poll nearly as often |
| b69ab31 | | | 126 | if (!this.pageFocusTracker.hasPageWithFocus() && !this.pageFocusTracker.hasVisiblePage()) { |
| b69ab31 | | | 127 | desiredNextTickTime = HIDDEN_POLL_INTERVAL; |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | } |
| b69ab31 | | | 130 | |
| b69ab31 | | | 131 | const now = Date.now(); |
| b69ab31 | | | 132 | const elapsedTickTime = now - this.lastFetch; |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | if ( |
| b69ab31 | | | 135 | kind === 'force' || |
| b69ab31 | | | 136 | // we've been waiting longer than desired |
| b69ab31 | | | 137 | elapsedTickTime >= desiredNextTickTime || |
| b69ab31 | | | 138 | // the moment a window gains focus or visibility, consider polling immediately |
| b69ab31 | | | 139 | (kind === 'focused' && elapsedTickTime >= ON_FOCUS_REFETCH_THROTTLE) || |
| b69ab31 | | | 140 | (kind === 'visible' && elapsedTickTime >= ON_VISIBLE_REFETCH_THROTTLE) |
| b69ab31 | | | 141 | ) { |
| b69ab31 | | | 142 | // it's time to fetch |
| b69ab31 | | | 143 | this.changeCallback('everything', kind); |
| b69ab31 | | | 144 | this.lastFetch = Date.now(); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | clearTimeout(this.timeout); |
| b69ab31 | | | 147 | this.timeout = setTimeout(this.poll, desiredNextTickTime); |
| b69ab31 | | | 148 | } else { |
| b69ab31 | | | 149 | // we have some time left before we we would expect to need to poll, schedule next poll |
| b69ab31 | | | 150 | clearTimeout(this.timeout); |
| b69ab31 | | | 151 | this.timeout = setTimeout(this.poll, desiredNextTickTime - elapsedTickTime); |
| b69ab31 | | | 152 | } |
| b69ab31 | | | 153 | }; |
| b69ab31 | | | 154 | |
| b69ab31 | | | 155 | private async setupDirstateSubscriptions(ctx: RepositoryContext) { |
| b69ab31 | | | 156 | const enabled = await Internal.fetchFeatureFlag?.(ctx, 'isl_use_edenfs_notifications'); |
| b69ab31 | | | 157 | this.logger.info('dirstate edenfs notifications flag state: ', enabled); |
| b69ab31 | | | 158 | if (enabled) { |
| b69ab31 | | | 159 | if (this.repoInfo.isEdenFs === true) { |
| b69ab31 | | | 160 | this.logger.info('Valid eden repo'); // For testing, remove when implemented |
| b69ab31 | | | 161 | await this.setupEdenDirstateSubscriptions(); |
| b69ab31 | | | 162 | return; |
| b69ab31 | | | 163 | } else { |
| b69ab31 | | | 164 | this.logger.info('Non-eden repo'); |
| b69ab31 | | | 165 | await this.setupWatchmanDirstateSubscriptions(); |
| b69ab31 | | | 166 | } |
| b69ab31 | | | 167 | } else { |
| b69ab31 | | | 168 | await this.setupWatchmanDirstateSubscriptions(); |
| b69ab31 | | | 169 | } |
| b69ab31 | | | 170 | } |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | private async setupWatchmanDirstateSubscriptions() { |
| b69ab31 | | | 173 | const {repoRoot, dotdir} = this.repoInfo; |
| b69ab31 | | | 174 | |
| b69ab31 | | | 175 | if (repoRoot == null || dotdir == null) { |
| b69ab31 | | | 176 | this.logger.error(`skipping dirstate subscription since ${repoRoot} is not a repository`); |
| b69ab31 | | | 177 | return; |
| b69ab31 | | | 178 | } |
| b69ab31 | | | 179 | |
| b69ab31 | | | 180 | // Resolve the repo dot dir in case it is a symlink. Watchman doesn't follow symlinks, |
| b69ab31 | | | 181 | // so we must follow it and watch the target. |
| b69ab31 | | | 182 | const realDotdir = await fs.realpath(dotdir); |
| b69ab31 | | | 183 | |
| b69ab31 | | | 184 | if (realDotdir != dotdir) { |
| b69ab31 | | | 185 | this.logger.info(`resolved dotdir ${dotdir} to ${realDotdir}`); |
| b69ab31 | | | 186 | |
| b69ab31 | | | 187 | // Write out ".watchmanconfig" so realDotdir passes muster as a watchman "root dir" |
| b69ab31 | | | 188 | // (otherwise watchman will refuse to watch it). |
| b69ab31 | | | 189 | await fs.writeFile(path.join(realDotdir, '.watchmanconfig'), '{}'); |
| b69ab31 | | | 190 | } |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | const DIRSTATE_WATCHMAN_SUBSCRIPTION = 'sapling-smartlog-dirstate-change'; |
| b69ab31 | | | 193 | try { |
| b69ab31 | | | 194 | const handleRepositoryStateChange = debounce(() => { |
| b69ab31 | | | 195 | // if the repo changes, also recheck files. E.g. if you commit, your uncommitted changes will also change. |
| b69ab31 | | | 196 | this.changeCallback('everything'); |
| b69ab31 | | | 197 | |
| b69ab31 | | | 198 | // reset timer for polling |
| b69ab31 | | | 199 | this.lastFetch = new Date().valueOf(); |
| b69ab31 | | | 200 | }, 100); // debounce so that multiple quick changes don't trigger multiple fetches for no reason |
| b69ab31 | | | 201 | |
| b69ab31 | | | 202 | this.logger.info('setting up dirstate subscription', realDotdir); |
| b69ab31 | | | 203 | |
| b69ab31 | | | 204 | const dirstateSubscription = await this.watchman.watchDirectoryRecursive( |
| b69ab31 | | | 205 | realDotdir, |
| b69ab31 | | | 206 | DIRSTATE_WATCHMAN_SUBSCRIPTION, |
| b69ab31 | | | 207 | { |
| b69ab31 | | | 208 | fields: ['name'], |
| b69ab31 | | | 209 | expression: [ |
| b69ab31 | | | 210 | 'name', |
| b69ab31 | | | 211 | ['bookmarks.current', 'bookmarks', 'dirstate', 'merge'], |
| b69ab31 | | | 212 | 'wholename', |
| b69ab31 | | | 213 | ], |
| b69ab31 | | | 214 | defer: [WatchForChanges.WATCHMAN_DEFER], |
| b69ab31 | | | 215 | empty_on_fresh_instance: true, |
| b69ab31 | | | 216 | }, |
| b69ab31 | | | 217 | ); |
| b69ab31 | | | 218 | dirstateSubscription.emitter.on('change', changes => { |
| b69ab31 | | | 219 | if (changes.includes('merge')) { |
| b69ab31 | | | 220 | this.changeCallback('merge conflicts'); |
| b69ab31 | | | 221 | } |
| b69ab31 | | | 222 | if (changes.includes('dirstate')) { |
| b69ab31 | | | 223 | handleRepositoryStateChange(); |
| b69ab31 | | | 224 | } |
| b69ab31 | | | 225 | }); |
| b69ab31 | | | 226 | dirstateSubscription.emitter.on('fresh-instance', handleRepositoryStateChange); |
| b69ab31 | | | 227 | |
| b69ab31 | | | 228 | this.dirstateDisposables.push(() => { |
| b69ab31 | | | 229 | this.logger.info('unsubscribe dirstate watcher'); |
| b69ab31 | | | 230 | this.watchman.unwatch(realDotdir, DIRSTATE_WATCHMAN_SUBSCRIPTION); |
| b69ab31 | | | 231 | }); |
| b69ab31 | | | 232 | } catch (err) { |
| b69ab31 | | | 233 | this.logger.error('failed to setup dirstate subscriptions', err); |
| b69ab31 | | | 234 | this.tracker.error( |
| b69ab31 | | | 235 | 'WatchmanEvent', |
| b69ab31 | | | 236 | 'WatchmanError', |
| b69ab31 | | | 237 | `failed to setup watchman dirstate subscriptions ${err}`, |
| b69ab31 | | | 238 | ); |
| b69ab31 | | | 239 | } |
| b69ab31 | | | 240 | } |
| b69ab31 | | | 241 | |
| b69ab31 | | | 242 | private async setupEdenDirstateSubscriptions() { |
| b69ab31 | | | 243 | const {repoRoot, dotdir} = this.repoInfo; |
| b69ab31 | | | 244 | |
| b69ab31 | | | 245 | if (repoRoot == null || dotdir == null) { |
| b69ab31 | | | 246 | this.logger.error(`skipping dirstate subscription since ${repoRoot} is not a repository`); |
| b69ab31 | | | 247 | return; |
| b69ab31 | | | 248 | } |
| b69ab31 | | | 249 | |
| b69ab31 | | | 250 | const relativeRoot = path.relative(repoRoot, dotdir); |
| b69ab31 | | | 251 | |
| b69ab31 | | | 252 | const DIRSTATE_EDENFS_SUBSCRIPTION = 'sapling-smartlog-dirstate-change-edenfs'; |
| b69ab31 | | | 253 | try { |
| b69ab31 | | | 254 | const handleRepositoryStateChange = debounce(() => { |
| b69ab31 | | | 255 | // if the repo changes, also recheck files. E.g. if you commit, your uncommitted changes will also change. |
| b69ab31 | | | 256 | this.changeCallback('everything'); |
| b69ab31 | | | 257 | |
| b69ab31 | | | 258 | // reset timer for polling |
| b69ab31 | | | 259 | this.lastFetch = new Date().valueOf(); |
| b69ab31 | | | 260 | }, 100); // debounce so that multiple quick changes don't trigger multiple fetches for no reason |
| b69ab31 | | | 261 | |
| b69ab31 | | | 262 | this.logger.info( |
| b69ab31 | | | 263 | 'setting up dirstate edenfs subscription in root', |
| b69ab31 | | | 264 | repoRoot, |
| b69ab31 | | | 265 | 'at', |
| b69ab31 | | | 266 | relativeRoot, |
| b69ab31 | | | 267 | ); |
| b69ab31 | | | 268 | |
| b69ab31 | | | 269 | const subscriptionCallback: SubscriptionCallback = (error, resp) => { |
| b69ab31 | | | 270 | if (error) { |
| b69ab31 | | | 271 | this.logger.error('EdenFS dirstate subscription error:', error.message); |
| b69ab31 | | | 272 | this.tracker.error('EdenWatcherEvent', 'EdenWatcherError', error); |
| b69ab31 | | | 273 | return; |
| b69ab31 | | | 274 | } else if (resp === null) { |
| b69ab31 | | | 275 | // EdenFS subscription closed |
| b69ab31 | | | 276 | return; |
| b69ab31 | | | 277 | } else { |
| b69ab31 | | | 278 | if (resp.changes && resp.changes.length > 0) { |
| b69ab31 | | | 279 | resp.changes.forEach(change => { |
| b69ab31 | | | 280 | if (change.SmallChange) { |
| b69ab31 | | | 281 | const paths = EdenFSUtils.extractPaths([change]); |
| b69ab31 | | | 282 | if (paths.includes('merge')) { |
| b69ab31 | | | 283 | this.changeCallback('merge conflicts'); |
| b69ab31 | | | 284 | return; |
| b69ab31 | | | 285 | } |
| b69ab31 | | | 286 | if (paths.includes('dirstate')) { |
| b69ab31 | | | 287 | handleRepositoryStateChange(); |
| b69ab31 | | | 288 | return; |
| b69ab31 | | | 289 | } |
| b69ab31 | | | 290 | } else if (change.LargeChange) { |
| b69ab31 | | | 291 | handleRepositoryStateChange(); |
| b69ab31 | | | 292 | return; |
| b69ab31 | | | 293 | } |
| b69ab31 | | | 294 | }); |
| b69ab31 | | | 295 | } |
| b69ab31 | | | 296 | return; |
| b69ab31 | | | 297 | } |
| b69ab31 | | | 298 | }; |
| b69ab31 | | | 299 | |
| b69ab31 | | | 300 | await this.edenfs.watchDirectoryRecursive( |
| b69ab31 | | | 301 | repoRoot, |
| b69ab31 | | | 302 | DIRSTATE_EDENFS_SUBSCRIPTION, |
| b69ab31 | | | 303 | { |
| b69ab31 | | | 304 | useCase: 'isl-server-node', |
| b69ab31 | | | 305 | mountPoint: repoRoot, |
| b69ab31 | | | 306 | throttle: 100, |
| b69ab31 | | | 307 | relativeRoot, |
| b69ab31 | | | 308 | deferredStates: [ |
| b69ab31 | | | 309 | WatchForChanges.WATCHMAN_DEFER, |
| b69ab31 | | | 310 | WatchForChanges.WATCHMAN_DEFER_TRANSACTION, |
| b69ab31 | | | 311 | ], |
| b69ab31 | | | 312 | includeVcsRoots: true, |
| b69ab31 | | | 313 | }, |
| b69ab31 | | | 314 | subscriptionCallback, |
| b69ab31 | | | 315 | ); |
| b69ab31 | | | 316 | this.dirstateDisposables.push(() => { |
| b69ab31 | | | 317 | this.logger.info('unsubscribe dirstate edenfs watcher'); |
| b69ab31 | | | 318 | this.edenfs.unwatch(repoRoot, DIRSTATE_EDENFS_SUBSCRIPTION); |
| b69ab31 | | | 319 | }); |
| b69ab31 | | | 320 | } catch (err) { |
| b69ab31 | | | 321 | this.logger.error('failed to setup dirstate edenfs subscriptions', err); |
| b69ab31 | | | 322 | this.tracker.error( |
| b69ab31 | | | 323 | 'EdenWatcherEvent', |
| b69ab31 | | | 324 | 'EdenWatcherError', |
| b69ab31 | | | 325 | `failed to setup dirstate edenfs subscriptions ${err}`, |
| b69ab31 | | | 326 | ); |
| b69ab31 | | | 327 | } |
| b69ab31 | | | 328 | } |
| b69ab31 | | | 329 | |
| b69ab31 | | | 330 | public async setupSubscriptions(ctx: RepositoryContext) { |
| b69ab31 | | | 331 | await this.waitForDirstateSubscriptionReady(); |
| b69ab31 | | | 332 | const enabled = await Internal.fetchFeatureFlag?.(ctx, 'isl_use_edenfs_notifications'); |
| b69ab31 | | | 333 | this.logger.info('subscription edenfs notifications flag state: ', enabled); |
| b69ab31 | | | 334 | if (enabled) { |
| b69ab31 | | | 335 | if (this.repoInfo.isEdenFs === true) { |
| b69ab31 | | | 336 | await this.setupEdenSubscriptions(); |
| b69ab31 | | | 337 | return; |
| b69ab31 | | | 338 | } |
| b69ab31 | | | 339 | } else { |
| b69ab31 | | | 340 | // TODO: move watchman here after implementing eden |
| b69ab31 | | | 341 | } |
| b69ab31 | | | 342 | await this.setupWatchmanSubscriptions(); |
| b69ab31 | | | 343 | } |
| b69ab31 | | | 344 | |
| b69ab31 | | | 345 | /** |
| b69ab31 | | | 346 | * Some Watchmans subscriptions should only activate when ISL is actually opened. |
| b69ab31 | | | 347 | * On platforms like vscode, it's possible to create a Repository without actually opening ISL. |
| b69ab31 | | | 348 | * In those cases, we only want the minimum set of subscriptions to be active. |
| b69ab31 | | | 349 | * We care about the dirstate watcher, but not the watchman subscriptions in that case. |
| b69ab31 | | | 350 | */ |
| b69ab31 | | | 351 | public async setupWatchmanSubscriptions() { |
| b69ab31 | | | 352 | const {repoRoot, dotdir} = this.repoInfo; |
| b69ab31 | | | 353 | |
| b69ab31 | | | 354 | if (repoRoot == null || dotdir == null) { |
| b69ab31 | | | 355 | this.logger.error(`skipping watchman subscription since ${repoRoot} is not a repository`); |
| b69ab31 | | | 356 | return; |
| b69ab31 | | | 357 | } |
| b69ab31 | | | 358 | const relativeDotdir = path.relative(repoRoot, dotdir); |
| b69ab31 | | | 359 | // if working from a git clone, the dotdir lives in .git/sl, |
| b69ab31 | | | 360 | // but we need to ignore changes in .git in our watchman subscriptions |
| b69ab31 | | | 361 | const outerDotDir = |
| b69ab31 | | | 362 | relativeDotdir.indexOf(path.sep) >= 0 ? path.dirname(relativeDotdir) : relativeDotdir; |
| b69ab31 | | | 363 | |
| b69ab31 | | | 364 | await this.maybeModifyGitignore(repoRoot, outerDotDir); |
| b69ab31 | | | 365 | |
| b69ab31 | | | 366 | const FILE_CHANGE_WATCHMAN_SUBSCRIPTION = 'sapling-smartlog-file-change'; |
| b69ab31 | | | 367 | try { |
| b69ab31 | | | 368 | // In some bad cases, a file may not be getting ignored by watchman properly, |
| b69ab31 | | | 369 | // and ends up constantly triggering the watchman subscription. |
| b69ab31 | | | 370 | // Incrementally increase the throttling of events to avoid spamming `status`. |
| b69ab31 | | | 371 | // This does mean "legit" changes will start being missed. |
| b69ab31 | | | 372 | // TODO: can we scan the list of changes and build a list of files that are overfiring, then send those to the UI as a warning? |
| b69ab31 | | | 373 | // This would allow a user to know it's happening and possibly fix it for their repo by adding it to a .watchmanconfig. |
| b69ab31 | | | 374 | const handleUncommittedChanges = stagedThrottler( |
| b69ab31 | | | 375 | [ |
| b69ab31 | | | 376 | { |
| b69ab31 | | | 377 | throttleMs: 0, |
| b69ab31 | | | 378 | numToNextStage: 5, |
| b69ab31 | | | 379 | resetAfterMs: 5_000, |
| b69ab31 | | | 380 | onEnter: () => { |
| b69ab31 | | | 381 | this.logger.info('no longer throttling uncommitted changes'); |
| b69ab31 | | | 382 | }, |
| b69ab31 | | | 383 | }, |
| b69ab31 | | | 384 | { |
| b69ab31 | | | 385 | throttleMs: 5_000, |
| b69ab31 | | | 386 | numToNextStage: 10, |
| b69ab31 | | | 387 | resetAfterMs: 20_000, |
| b69ab31 | | | 388 | onEnter: () => { |
| b69ab31 | | | 389 | this.logger.info('slightly throttling uncommitted changes'); |
| b69ab31 | | | 390 | }, |
| b69ab31 | | | 391 | }, |
| b69ab31 | | | 392 | { |
| b69ab31 | | | 393 | throttleMs: 30_000, |
| b69ab31 | | | 394 | resetAfterMs: 30_000, |
| b69ab31 | | | 395 | onEnter: () => { |
| b69ab31 | | | 396 | this.logger.info('aggressively throttling uncommitted changes'); |
| b69ab31 | | | 397 | }, |
| b69ab31 | | | 398 | }, |
| b69ab31 | | | 399 | ], |
| b69ab31 | | | 400 | () => { |
| b69ab31 | | | 401 | this.changeCallback('uncommitted changes'); |
| b69ab31 | | | 402 | |
| b69ab31 | | | 403 | // reset timer for polling |
| b69ab31 | | | 404 | this.lastFetch = new Date().valueOf(); |
| b69ab31 | | | 405 | }, |
| b69ab31 | | | 406 | ); |
| b69ab31 | | | 407 | const uncommittedChangesSubscription = await this.watchman.watchDirectoryRecursive( |
| b69ab31 | | | 408 | repoRoot, |
| b69ab31 | | | 409 | FILE_CHANGE_WATCHMAN_SUBSCRIPTION, |
| b69ab31 | | | 410 | { |
| b69ab31 | | | 411 | // We only need to know that a change happened (not the list of files) so that we can trigger `status` |
| b69ab31 | | | 412 | fields: ['name'], |
| b69ab31 | | | 413 | expression: [ |
| b69ab31 | | | 414 | 'allof', |
| b69ab31 | | | 415 | // This watchman subscription is used to determine when and which |
| b69ab31 | | | 416 | // files to fetch new statuses for. There is no reason to include |
| b69ab31 | | | 417 | // directories in these updates, and in fact they may make us overfetch |
| b69ab31 | | | 418 | // statuses. |
| b69ab31 | | | 419 | // This line restricts this subscription to only return files. |
| b69ab31 | | | 420 | ['type', 'f'], |
| b69ab31 | | | 421 | ['not', ['dirname', outerDotDir]], |
| b69ab31 | | | 422 | // Even though we tell it not to match .sl, modifying a file inside .sl |
| b69ab31 | | | 423 | // will emit an event for the folder itself, which we want to ignore. |
| b69ab31 | | | 424 | ['not', ['match', outerDotDir, 'basename']], |
| b69ab31 | | | 425 | // Exclude edenfs notifications directory - EdenFS notifications can modify hidden files |
| b69ab31 | | | 426 | // in this directory which would otherwise trigger watchman events. |
| b69ab31 | | | 427 | ['not', ['dirname', '.edenfs-notifications-state']], |
| b69ab31 | | | 428 | ['not', ['match', '.edenfs-notifications-state', 'basename']], |
| b69ab31 | | | 429 | ], |
| b69ab31 | | | 430 | defer: [WatchForChanges.WATCHMAN_DEFER], |
| b69ab31 | | | 431 | empty_on_fresh_instance: true, |
| b69ab31 | | | 432 | }, |
| b69ab31 | | | 433 | ); |
| b69ab31 | | | 434 | uncommittedChangesSubscription.emitter.on('change', handleUncommittedChanges); |
| b69ab31 | | | 435 | uncommittedChangesSubscription.emitter.on('fresh-instance', handleUncommittedChanges); |
| b69ab31 | | | 436 | |
| b69ab31 | | | 437 | this.watchmanDisposables.push(() => { |
| b69ab31 | | | 438 | this.logger.info('unsubscribe watchman'); |
| b69ab31 | | | 439 | this.watchman.unwatch(repoRoot, FILE_CHANGE_WATCHMAN_SUBSCRIPTION); |
| b69ab31 | | | 440 | }); |
| b69ab31 | | | 441 | } catch (err) { |
| b69ab31 | | | 442 | this.logger.error('failed to setup watchman subscriptions', err); |
| b69ab31 | | | 443 | this.tracker.error( |
| b69ab31 | | | 444 | 'WatchmanEvent', |
| b69ab31 | | | 445 | 'WatchmanError', |
| b69ab31 | | | 446 | `failed to setup watchman subscriptions ${err}`, |
| b69ab31 | | | 447 | ); |
| b69ab31 | | | 448 | } |
| b69ab31 | | | 449 | } |
| b69ab31 | | | 450 | |
| b69ab31 | | | 451 | public async setupEdenSubscriptions() { |
| b69ab31 | | | 452 | const {repoRoot, dotdir} = this.repoInfo; |
| b69ab31 | | | 453 | |
| b69ab31 | | | 454 | if (repoRoot == null || dotdir == null) { |
| b69ab31 | | | 455 | this.logger.error(`skipping edenfs subscription since ${repoRoot} is not a repository`); |
| b69ab31 | | | 456 | return; |
| b69ab31 | | | 457 | } |
| b69ab31 | | | 458 | const relativeDotdir = path.relative(repoRoot, dotdir); |
| b69ab31 | | | 459 | // if working from a git clone, the dotdir lives in .git/sl, |
| b69ab31 | | | 460 | // but we need to ignore changes in .git in our watchman subscriptions |
| b69ab31 | | | 461 | const outerDotDir = |
| b69ab31 | | | 462 | relativeDotdir.indexOf(path.sep) >= 0 ? path.dirname(relativeDotdir) : relativeDotdir; |
| b69ab31 | | | 463 | |
| b69ab31 | | | 464 | this.logger.info( |
| b69ab31 | | | 465 | 'setting up edenfs subscription in', |
| b69ab31 | | | 466 | repoRoot, |
| b69ab31 | | | 467 | 'at', |
| b69ab31 | | | 468 | outerDotDir, |
| b69ab31 | | | 469 | 'relativeDotdir', |
| b69ab31 | | | 470 | relativeDotdir, |
| b69ab31 | | | 471 | ); |
| b69ab31 | | | 472 | |
| b69ab31 | | | 473 | const FILE_CHANGE_EDENFS_SUBSCRIPTION = 'sapling-smartlog-file-change-edenfs'; |
| b69ab31 | | | 474 | try { |
| b69ab31 | | | 475 | // In some bad cases, a file that has a lot of activity can constantly trigger the subscription. |
| b69ab31 | | | 476 | // Incrementally increase the throttling of events to avoid spamming `status`. |
| b69ab31 | | | 477 | // This does mean "legit" changes will start being missed. |
| b69ab31 | | | 478 | // TODO: can we scan the list of changes and build a list of files that are overfiring, then send those to the UI as a warning? |
| b69ab31 | | | 479 | // This would allow a user to know it's happening and possibly fix it for their repo. |
| b69ab31 | | | 480 | const handleUncommittedChanges = stagedThrottler( |
| b69ab31 | | | 481 | [ |
| b69ab31 | | | 482 | { |
| b69ab31 | | | 483 | throttleMs: 0, |
| b69ab31 | | | 484 | numToNextStage: 5, |
| b69ab31 | | | 485 | resetAfterMs: 5_000, |
| b69ab31 | | | 486 | onEnter: () => { |
| b69ab31 | | | 487 | this.logger.info('no longer throttling uncommitted changes'); |
| b69ab31 | | | 488 | }, |
| b69ab31 | | | 489 | }, |
| b69ab31 | | | 490 | { |
| b69ab31 | | | 491 | throttleMs: 5_000, |
| b69ab31 | | | 492 | numToNextStage: 10, |
| b69ab31 | | | 493 | resetAfterMs: 20_000, |
| b69ab31 | | | 494 | onEnter: () => { |
| b69ab31 | | | 495 | this.logger.info('slightly throttling uncommitted changes'); |
| b69ab31 | | | 496 | }, |
| b69ab31 | | | 497 | }, |
| b69ab31 | | | 498 | { |
| b69ab31 | | | 499 | throttleMs: 30_000, |
| b69ab31 | | | 500 | resetAfterMs: 30_000, |
| b69ab31 | | | 501 | onEnter: () => { |
| b69ab31 | | | 502 | this.logger.info('aggressively throttling uncommitted changes'); |
| b69ab31 | | | 503 | }, |
| b69ab31 | | | 504 | }, |
| b69ab31 | | | 505 | ], |
| b69ab31 | | | 506 | () => { |
| b69ab31 | | | 507 | this.changeCallback('uncommitted changes'); |
| b69ab31 | | | 508 | |
| b69ab31 | | | 509 | // reset timer for polling |
| b69ab31 | | | 510 | this.lastFetch = new Date().valueOf(); |
| b69ab31 | | | 511 | }, |
| b69ab31 | | | 512 | ); |
| b69ab31 | | | 513 | const subscriptionCallback: SubscriptionCallback = (error, resp) => { |
| b69ab31 | | | 514 | if (error) { |
| b69ab31 | | | 515 | this.logger.error('EdenFS subscription error:', error.message); |
| b69ab31 | | | 516 | this.tracker.error('EdenWatcherEvent', 'EdenWatcherError', error); |
| b69ab31 | | | 517 | return; |
| b69ab31 | | | 518 | } else if (resp === null) { |
| b69ab31 | | | 519 | // EdenFS subscription closed |
| b69ab31 | | | 520 | return; |
| b69ab31 | | | 521 | } else { |
| b69ab31 | | | 522 | if (resp.changes && resp.changes.length > 0) { |
| b69ab31 | | | 523 | handleUncommittedChanges(); |
| b69ab31 | | | 524 | } |
| b69ab31 | | | 525 | } |
| b69ab31 | | | 526 | }; |
| b69ab31 | | | 527 | await this.edenfs.watchDirectoryRecursive( |
| b69ab31 | | | 528 | repoRoot, |
| b69ab31 | | | 529 | FILE_CHANGE_EDENFS_SUBSCRIPTION, |
| b69ab31 | | | 530 | { |
| b69ab31 | | | 531 | useCase: 'isl-server-node', |
| b69ab31 | | | 532 | mountPoint: repoRoot, |
| b69ab31 | | | 533 | throttle: 100, |
| b69ab31 | | | 534 | deferredStates: [ |
| b69ab31 | | | 535 | WatchForChanges.WATCHMAN_DEFER, |
| b69ab31 | | | 536 | WatchForChanges.WATCHMAN_DEFER_TRANSACTION, |
| b69ab31 | | | 537 | ], |
| b69ab31 | | | 538 | excludedRoots: [outerDotDir, relativeDotdir], |
| b69ab31 | | | 539 | }, |
| b69ab31 | | | 540 | subscriptionCallback, |
| b69ab31 | | | 541 | ); |
| b69ab31 | | | 542 | |
| b69ab31 | | | 543 | this.edenfsDisposables.push(() => { |
| b69ab31 | | | 544 | this.logger.info('unsubscribe edenfs'); |
| b69ab31 | | | 545 | this.edenfs.unwatch(repoRoot, FILE_CHANGE_EDENFS_SUBSCRIPTION); |
| b69ab31 | | | 546 | }); |
| b69ab31 | | | 547 | } catch (err) { |
| b69ab31 | | | 548 | this.logger.error('failed to setup edenfs subscriptions', err); |
| b69ab31 | | | 549 | this.tracker.error( |
| b69ab31 | | | 550 | 'EdenWatcherEvent', |
| b69ab31 | | | 551 | 'EdenWatcherError', |
| b69ab31 | | | 552 | `failed to setup edenfs subscriptions ${err}`, |
| b69ab31 | | | 553 | ); |
| b69ab31 | | | 554 | } |
| b69ab31 | | | 555 | } |
| b69ab31 | | | 556 | |
| b69ab31 | | | 557 | /** |
| b69ab31 | | | 558 | * Modify gitignore to ignore watchman cookie files. This is needed when using ISL |
| b69ab31 | | | 559 | * with git repos. `git status` does not exclude watchman cookie files by default. |
| b69ab31 | | | 560 | * `sl` does not use watchman in dotgit mode. |
| b69ab31 | | | 561 | */ |
| b69ab31 | | | 562 | private async maybeModifyGitignore(repoRoot: string, outerDotDir: string) { |
| b69ab31 | | | 563 | if (outerDotDir !== '.git') { |
| b69ab31 | | | 564 | return; |
| b69ab31 | | | 565 | } |
| b69ab31 | | | 566 | const gitIgnorePath = path.join(repoRoot, outerDotDir, 'info', 'exclude'); |
| b69ab31 | | | 567 | // https://github.com/facebook/watchman/blob/76bd924b1169dae9cb9f5371845ab44ea1f836bf/watchman/Cookie.h#L15 |
| b69ab31 | | | 568 | const rule = '/.watchman-cookie-*'; |
| b69ab31 | | | 569 | try { |
| b69ab31 | | | 570 | const gitIgnoreContent = await fs.readFile(gitIgnorePath, 'utf8'); |
| b69ab31 | | | 571 | if (!gitIgnoreContent.includes(rule)) { |
| b69ab31 | | | 572 | await fs.appendFile(gitIgnorePath, `\n${rule}\n`, 'utf8'); |
| b69ab31 | | | 573 | } |
| b69ab31 | | | 574 | } catch (err) { |
| b69ab31 | | | 575 | this.logger.error(`failed to read or write ${gitIgnorePath}`, err); |
| b69ab31 | | | 576 | } |
| b69ab31 | | | 577 | } |
| b69ab31 | | | 578 | |
| b69ab31 | | | 579 | public disposeWatchmanSubscriptions() { |
| b69ab31 | | | 580 | this.watchmanDisposables.forEach(dispose => dispose()); |
| b69ab31 | | | 581 | } |
| b69ab31 | | | 582 | |
| b69ab31 | | | 583 | public disposeEdenFSSubscriptions() { |
| b69ab31 | | | 584 | this.edenfsDisposables.forEach(dispose => dispose()); |
| b69ab31 | | | 585 | } |
| b69ab31 | | | 586 | |
| b69ab31 | | | 587 | public dispose() { |
| b69ab31 | | | 588 | this.dirstateDisposables.forEach(dispose => dispose()); |
| b69ab31 | | | 589 | this.disposeWatchmanSubscriptions(); |
| b69ab31 | | | 590 | this.disposeEdenFSSubscriptions(); |
| b69ab31 | | | 591 | if (this.timeout) { |
| b69ab31 | | | 592 | clearTimeout(this.timeout); |
| b69ab31 | | | 593 | this.timeout = undefined; |
| b69ab31 | | | 594 | } |
| b69ab31 | | | 595 | } |
| b69ab31 | | | 596 | } |