Skip to content

adds garbage collection for repos #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add rest of gc logic
  • Loading branch information
msukkari committed Jan 29, 2025
commit e7f5c0b58af6a40dffe728c63c9a94e3970e0626
23 changes: 21 additions & 2 deletions packages/backend/src/repoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Connection, PrismaClient, Repo, RepoToConnection, RepoIndexingStatus }
import { ConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
import { AppContext, Settings } from "./types.js";
import { captureEvent } from "./posthog.js";
import { getRepoPath, getTokenFromConfig, measure } from "./utils.js";
import { getRepoPath, getTokenFromConfig, measure, getShardPrefix } from "./utils.js";
import { cloneRepository, fetchRepository } from "./git.js";
import { existsSync, rmSync } from 'fs';
import { existsSync, rmSync, readdirSync } from 'fs';
import { indexGitRepository } from "./zoekt.js";
import os from 'os';

Expand Down Expand Up @@ -47,6 +47,7 @@ export class RepoManager implements IRepoManager {
public async blockingPollLoop() {
while(true) {
this.fetchAndScheduleRepoIndexing();
this.garbageCollectRepo();

await new Promise(resolve => setTimeout(resolve, this.settings.reindexRepoPollingInternvalMs));
}
Expand Down Expand Up @@ -108,12 +109,30 @@ export class RepoManager implements IRepoManager {
for (const repo of reposWithNoConnections) {
this.logger.info(`Garbage collecting repo with no connections: ${repo.id}`);

// delete cloned repo
const repoPath = getRepoPath(repo, this.ctx);
if(existsSync(repoPath)) {
this.logger.info(`Deleting repo directory ${repoPath}`);
rmSync(repoPath, { recursive: true, force: true });
}

// delete shards
const shardPrefix = getShardPrefix(repo.orgId, repo.id);
const files = readdirSync(this.ctx.indexPath).filter(file => file.startsWith(shardPrefix));
for (const file of files) {
const filePath = `${this.ctx.indexPath}/${file}`;
this.logger.info(`Deleting shard file ${filePath}`);
rmSync(filePath);
}
}

await this.db.repo.deleteMany({
where: {
id: {
in: reposWithNoConnections.map(repo => repo.id)
}
}
});
}

// TODO: do this better? ex: try using the tokens from all the connections
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,8 @@ export const arraysEqualShallow = <T>(a?: readonly T[], b?: readonly T[]) => {

export const getRepoPath = (repo: Repo, ctx: AppContext) => {
return path.join(ctx.reposPath, repo.id.toString());
}

export const getShardPrefix = (orgId: number, repoId: number) => {
return `${orgId}_${repoId}`;
}
3 changes: 2 additions & 1 deletion packages/backend/src/zoekt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppContext, LocalRepository, Settings } from "./types.js";
import { Repo } from "@sourcebot/db";
import { getRepoPath } from "./utils.js";
import { DEFAULT_SETTINGS } from "./constants.js";
import { getShardPrefix } from "./utils.js";

const ALWAYS_EXCLUDED_DIRS = ['.git', '.hg', '.svn'];

Expand All @@ -11,7 +12,7 @@ export const indexGitRepository = async (repo: Repo, ctx: AppContext) => {
'HEAD'
];

const shardPrefix = `${repo.orgId}_${repo.id}`;
const shardPrefix = getShardPrefix(repo.orgId, repo.id);
const repoPath = getRepoPath(repo, ctx);
const command = `zoekt-git-index -allow_missing_branches -index ${ctx.indexPath} -file_limit ${DEFAULT_SETTINGS.maxFileSize} -branches ${revisions.join(',')} -tenant_id ${repo.orgId} -shard_prefix ${shardPrefix} ${repoPath}`;

Expand Down