From 86ec72d443bfdc1a9d42defcfb6909a2e930ce22 Mon Sep 17 00:00:00 2001 From: Mengwei Ding Date: Mon, 24 Jun 2019 15:09:10 -0700 Subject: [PATCH] [Code] always enforce HEAD revision for indexing even when repo get updated during index (#39528) --- .../plugins/code/server/git_operations.ts | 4 ++-- .../server/indexer/lsp_incremental_indexer.ts | 10 +++++++--- .../code/server/indexer/lsp_indexer.ts | 20 +++++++++++-------- .../plugins/code/server/queue/index_worker.ts | 4 ++-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/x-pack/legacy/plugins/code/server/git_operations.ts b/x-pack/legacy/plugins/code/server/git_operations.ts index 2d494c33d274..04beec2457f3 100644 --- a/x-pack/legacy/plugins/code/server/git_operations.ts +++ b/x-pack/legacy/plugins/code/server/git_operations.ts @@ -29,7 +29,7 @@ import { FileTree, FileTreeItemType, RepositoryUri, sortFileTree } from '../mode import { CommitInfo, ReferenceInfo, ReferenceType } from '../model/commit'; import { detectLanguage } from './utils/detect_language'; -const HEAD = 'HEAD'; +export const HEAD = 'HEAD'; const REFS_HEADS = 'refs/heads/'; export const DEFAULT_TREE_CHILDREN_LIMIT = 50; @@ -129,7 +129,7 @@ export class GitOperations { public async getCommit(uri: RepositoryUri, revision: string): Promise { const repo = await this.openRepo(uri); - if (revision.toUpperCase() === 'HEAD') { + if (revision.toUpperCase() === HEAD) { return await repo.getHeadCommit(); } // branches and tags diff --git a/x-pack/legacy/plugins/code/server/indexer/lsp_incremental_indexer.ts b/x-pack/legacy/plugins/code/server/indexer/lsp_incremental_indexer.ts index 0ac5af08477a..bd6c03a8ab3e 100644 --- a/x-pack/legacy/plugins/code/server/indexer/lsp_incremental_indexer.ts +++ b/x-pack/legacy/plugins/code/server/indexer/lsp_incremental_indexer.ts @@ -17,7 +17,7 @@ import { LspIncIndexRequest, RepositoryUri, } from '../../model'; -import { GitOperations } from '../git_operations'; +import { GitOperations, HEAD } from '../git_operations'; import { EsClient } from '../lib/esqueue'; import { Logger } from '../log'; import { LspService } from '../lsp/lsp_service'; @@ -126,7 +126,7 @@ export class LspIncrementalIndexer extends LspIndexer { try { const { workspaceRepo } = await this.lspService.workspaceHandler.openWorkspace( this.repoUri, - 'head' + HEAD ); const workspaceDir = workspaceRepo.workdir(); if (this.diff) { @@ -136,7 +136,11 @@ export class LspIncrementalIndexer extends LspIndexer { localRepoPath: workspaceDir, filePath: f.path, originPath: f.originPath, - revision: this.revision, + // Always use HEAD for now until we have multi revision. + // Also, since the workspace might get updated during the index, we always + // want the revision to keep updated so that lsp proxy could pass the revision + // check per discussion here: https://github.com/elastic/code/issues/1317#issuecomment-504615833 + revision: HEAD, kind: f.kind, originRevision: this.originRevision, }; diff --git a/x-pack/legacy/plugins/code/server/indexer/lsp_indexer.ts b/x-pack/legacy/plugins/code/server/indexer/lsp_indexer.ts index a293c896e6c0..a729a81036b0 100644 --- a/x-pack/legacy/plugins/code/server/indexer/lsp_indexer.ts +++ b/x-pack/legacy/plugins/code/server/indexer/lsp_indexer.ts @@ -17,7 +17,7 @@ import { } from '../../common/lsp_error_codes'; import { toCanonicalUrl } from '../../common/uri_util'; import { Document, IndexStats, IndexStatsKey, LspIndexRequest, RepositoryUri } from '../../model'; -import { GitOperations } from '../git_operations'; +import { GitOperations, HEAD } from '../git_operations'; import { EsClient } from '../lib/esqueue'; import { Logger } from '../log'; import { LspService } from '../lsp/lsp_service'; @@ -103,20 +103,24 @@ export class LspIndexer extends AbstractIndexer { protected async *getIndexRequestIterator(): AsyncIterableIterator { let repo; try { - const { - workspaceRepo, - workspaceRevision, - } = await this.lspService.workspaceHandler.openWorkspace(this.repoUri, 'head'); + const { workspaceRepo } = await this.lspService.workspaceHandler.openWorkspace( + this.repoUri, + HEAD + ); repo = workspaceRepo; const workspaceDir = workspaceRepo.workdir(); - const fileIterator = await this.gitOps.iterateRepo(this.repoUri, 'head'); + const fileIterator = await this.gitOps.iterateRepo(this.repoUri, HEAD); for await (const file of fileIterator) { const filePath = file.path!; const req: LspIndexRequest = { repoUri: this.repoUri, localRepoPath: workspaceDir, filePath, - revision: workspaceRevision, + // Always use HEAD for now until we have multi revision. + // Also, since the workspace might get updated during the index, we always + // want the revision to keep updated so that lsp proxy could pass the revision + // check per discussion here: https://github.com/elastic/code/issues/1317#issuecomment-504615833 + revision: HEAD, }; yield req; } @@ -133,7 +137,7 @@ export class LspIndexer extends AbstractIndexer { protected async getIndexRequestCount(): Promise { try { - return await this.gitOps.countRepoFiles(this.repoUri, 'head'); + return await this.gitOps.countRepoFiles(this.repoUri, HEAD); } catch (error) { if (this.isCancelled()) { this.log.debug(`Indexer got cancelled. Skip get index count error.`); diff --git a/x-pack/legacy/plugins/code/server/queue/index_worker.ts b/x-pack/legacy/plugins/code/server/queue/index_worker.ts index 1a9a63b8e60e..c80829be8b49 100644 --- a/x-pack/legacy/plugins/code/server/queue/index_worker.ts +++ b/x-pack/legacy/plugins/code/server/queue/index_worker.ts @@ -16,7 +16,7 @@ import { WorkerProgress, WorkerReservedProgress, } from '../../model'; -import { GitOperations } from '../git_operations'; +import { GitOperations, HEAD } from '../git_operations'; import { IndexerFactory } from '../indexer'; import { EsClient, Esqueue } from '../lib/esqueue'; import { Logger } from '../log'; @@ -171,7 +171,7 @@ export class IndexWorker extends AbstractWorker { protected async getTimeoutMs(payload: any) { try { - const totalCount = await this.gitOps.countRepoFiles(payload.uri, 'head'); + const totalCount = await this.gitOps.countRepoFiles(payload.uri, HEAD); let timeout = moment.duration(1, 'hour').asMilliseconds(); if (totalCount > 0) { // timeout = ln(file_count) in hour