Skip to content

Commit

Permalink
[Code] stop persist random path in es (#47923)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yulong authored Oct 18, 2019
1 parent f79292a commit 713993c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
24 changes: 19 additions & 5 deletions x-pack/legacy/plugins/code/server/__tests__/lsp_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('lsp_service tests', () => {
}

const repoUri = 'github.com/test/test_repo';
const mockRndPath = '__random';

// @ts-ignore
before(async () => {
Expand All @@ -101,7 +102,7 @@ describe('lsp_service tests', () => {

function mockLspService() {
const esClient = mockEsClient();
return new LspService(
const service = new LspService(
'127.0.0.1',
serverOptions,
gitOps,
Expand All @@ -110,6 +111,9 @@ describe('lsp_service tests', () => {
new ConsoleLoggerFactory(),
new RepositoryConfigController(esClient)
);
// @ts-ignore
service.workspaceHandler.randomPath = () => 'random';
return service;
}

async function sendHoverRequest(lspservice: LspService, revision: string) {
Expand Down Expand Up @@ -145,13 +149,13 @@ describe('lsp_service tests', () => {
ctrlSpy.restore();

const workspaceFolderExists = fs.existsSync(
path.join(serverOptions.workspacePath, repoUri, revision)
path.join(serverOptions.workspacePath, repoUri, mockRndPath, revision)
);
// workspace is opened
assert.ok(workspaceFolderExists);

const workspacePath = fs.realpathSync(
path.resolve(serverOptions.workspacePath, repoUri, revision)
path.resolve(serverOptions.workspacePath, repoUri, mockRndPath, revision)
);
// workspace handler is working, filled workspacePath
sinon.assert.calledWith(
Expand All @@ -177,7 +181,12 @@ describe('lsp_service tests', () => {
// send a dummy request to open a workspace;
const response = await sendHoverRequest(lspservice, revision);
assert.ok(response);
const workspacePath = path.resolve(serverOptions.workspacePath, repoUri, revision);
const workspacePath = path.resolve(
serverOptions.workspacePath,
repoUri,
mockRndPath,
revision
);
const workspaceFolderExists = fs.existsSync(workspacePath);
// workspace is opened
assert.ok(workspaceFolderExists);
Expand Down Expand Up @@ -216,7 +225,12 @@ describe('lsp_service tests', () => {
// send a dummy request to open a workspace;
const response = await sendHoverRequest(lspservice, revision);
assert.ok(response);
const workspacePath = path.resolve(serverOptions.workspacePath, repoUri, revision);
const workspacePath = path.resolve(
serverOptions.workspacePath,
repoUri,
mockRndPath,
revision
);
const git = simplegit(workspacePath);
const workspaceCommit = await git.revparse(['HEAD']);
// workspace is newest now
Expand Down
6 changes: 0 additions & 6 deletions x-pack/legacy/plugins/code/server/indexer/schema/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ export const RepositoryDeleteStatusReservedField = 'repository_delete_status';
export const RepositoryIndexStatusReservedField = 'repository_index_status';
// The field name of repository config object nested in the Document index.
export const RepositoryConfigReservedField = 'repository_config';
// The field name of repository config object nested in the Document index.
export const RepositoryRandomPathReservedField = 'repository_random_path';

export const ALL_RESERVED = [
RepositoryReservedField,
RepositoryGitStatusReservedField,
RepositoryDeleteStatusReservedField,
RepositoryIndexStatusReservedField,
RepositoryConfigReservedField,
RepositoryRandomPathReservedField,
];

// Correspond to model/search/Document
Expand Down Expand Up @@ -107,9 +104,6 @@ export const DocumentSchema = {
},
},
},
[RepositoryRandomPathReservedField]: {
type: 'keyword',
},
// A single Repository Git Status object resides in this document index.
[RepositoryGitStatusReservedField]: {
properties: {
Expand Down
23 changes: 17 additions & 6 deletions x-pack/legacy/plugins/code/server/lsp/workspace_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import del from 'del';
import fs from 'fs';
import { delay } from 'lodash';
import path from 'path';
import crypto from 'crypto';
import { ResponseMessage } from 'vscode-jsonrpc/lib/messages';
import { Hover, Location, TextDocumentPositionParams } from 'vscode-languageserver';

Expand Down Expand Up @@ -97,7 +98,7 @@ export class WorkspaceHandler {
wt = await this.openWorktree(
git,
workspaceBranch,
await this.revisionDir(repositoryUri, ref),
await this.revisionDir(repositoryUri, ref, this.randomPath()),
targetRevision
);
}
Expand All @@ -110,6 +111,10 @@ export class WorkspaceHandler {
};
}

private randomPath() {
return crypto.randomBytes(4).toString('hex');
}

public async openWorktree(
git: SimpleGit,
workspaceBranch: string,
Expand Down Expand Up @@ -378,14 +383,20 @@ export class WorkspaceHandler {
}
}

public async revisionDir(repositoryUri: string, ref: string) {
return path.join(await this.workspaceDir(repositoryUri), ref);
public async revisionDir(repositoryUri: string, ref: string, randomStr: string = '') {
return path.join(await this.workspaceDir(repositoryUri, randomStr), ref);
}

private async workspaceDir(repoUri: string) {
const randomStr =
this.objectClient && (await this.objectClient.getRepositoryRandomStr(repoUri));
private async workspaceDir(repoUri: string, randomStr: string = '') {
const base = path.join(this.workspacePath, repoUri);
if (randomStr === '') {
const git = await this.gitOps.openGit(repoUri);
const trees = await this.listWorktrees(git);
if (trees.size > 0) {
const wt = trees.values().next().value;
return path.dirname(wt.path);
}
}
if (randomStr) {
return path.join(base, `__${randomStr}`);
} else {
Expand Down
5 changes: 1 addition & 4 deletions x-pack/legacy/plugins/code/server/routes/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ export function repositoryRoute(

// Persist to elasticsearch
await repoObjectClient.setRepository(repo.uri, repo);
const randomStr = Math.random()
.toString(36)
.substring(2, 15);
await repoObjectClient.setRepositoryRandomStr(repo.uri, randomStr);

// Kick off clone job
const payload = {
url: repoUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
RepositoryIndexName,
RepositoryIndexNamePrefix,
RepositoryIndexStatusReservedField,
RepositoryRandomPathReservedField,
RepositoryReservedField,
RepositorySearchIndexWithScope,
} from '../indexer/schema';
Expand Down Expand Up @@ -47,21 +46,17 @@ export class RepositoryObjectClient {
return await this.getRepositoryObject(repoUri, RepositoryConfigReservedField);
}

public async getRepository(repoUri: RepositoryUri): Promise<Repository> {
return await this.getRepositoryObject(repoUri, RepositoryReservedField);
}

public async getRepositoryRandomStr(repoUri: RepositoryUri): Promise<string> {
return await this.getRepositoryObject(repoUri, RepositoryRandomPathReservedField);
}

public async getRepositories(uris: string[]): Promise<Repository[]> {
if (uris.length === 0) {
return [];
}
return this.getRepositoriesInternal(RepositorySearchIndexWithScope(uris));
}

public async getRepository(repoUri: RepositoryUri): Promise<Repository> {
return await this.getRepositoryObject(repoUri, RepositoryReservedField);
}

public async getAllRepositories(): Promise<Repository[]> {
return await this.getRepositoriesInternal(`${RepositoryIndexNamePrefix}*`);
}
Expand Down Expand Up @@ -107,10 +102,6 @@ export class RepositoryObjectClient {
return await this.setRepositoryObject(repoUri, RepositoryConfigReservedField, config);
}

public async setRepositoryRandomStr(repoUri: RepositoryUri, randomStr: string) {
return await this.setRepositoryObject(repoUri, RepositoryRandomPathReservedField, randomStr);
}

public async setRepository(repoUri: RepositoryUri, repo: Repository) {
return await this.setRepositoryObject(repoUri, RepositoryReservedField, repo);
}
Expand Down

0 comments on commit 713993c

Please sign in to comment.