|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { inject, injectable } from "inversify"; |
| 8 | +import { |
| 9 | + CommitContext, |
| 10 | + PrebuiltWorkspace, |
| 11 | + TaskConfig, |
| 12 | + User, |
| 13 | + Workspace, |
| 14 | + WorkspaceConfig, |
| 15 | + WorkspaceImageSource, |
| 16 | +} from "@gitpod/gitpod-protocol"; |
| 17 | +import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; |
| 18 | +import { WithCommitHistories } from "@gitpod/gitpod-protocol/src/protocol"; |
| 19 | +import { WorkspaceDB } from "@gitpod/gitpod-db/lib"; |
| 20 | +import { Config } from "../../../src/config"; |
| 21 | +import { ConfigProvider } from "../../../src/workspace/config-provider"; |
| 22 | +import { HostContextProvider } from "../../../src/auth/host-context-provider"; |
| 23 | +import { ImageSourceProvider } from "../../../src/workspace/image-source-provider"; |
| 24 | + |
| 25 | +@injectable() |
| 26 | +export class IncrementalPrebuildsService { |
| 27 | + @inject(Config) protected readonly config: Config; |
| 28 | + @inject(ConfigProvider) protected readonly configProvider: ConfigProvider; |
| 29 | + @inject(HostContextProvider) protected readonly hostContextProvider: HostContextProvider; |
| 30 | + @inject(ImageSourceProvider) protected readonly imageSourceProvider: ImageSourceProvider; |
| 31 | + @inject(WorkspaceDB) protected readonly workspaceDB: WorkspaceDB; |
| 32 | + |
| 33 | + public async getCommitHistories(context: CommitContext, user: User): Promise<WithCommitHistories> { |
| 34 | + const maxDepth = this.config.incrementalPrebuilds.commitHistory; |
| 35 | + const hostContext = this.hostContextProvider.get(context.repository.host); |
| 36 | + const repoProvider = hostContext?.services?.repositoryProvider; |
| 37 | + if (!repoProvider) { |
| 38 | + return {}; |
| 39 | + } |
| 40 | + const result: WithCommitHistories = {}; |
| 41 | + result.commitHistory = await repoProvider.getCommitHistory( |
| 42 | + user, |
| 43 | + context.repository.owner, |
| 44 | + context.repository.name, |
| 45 | + context.revision, |
| 46 | + maxDepth, |
| 47 | + ); |
| 48 | + if (context.additionalRepositoryCheckoutInfo && context.additionalRepositoryCheckoutInfo.length > 0) { |
| 49 | + const histories = context.additionalRepositoryCheckoutInfo.map(async (info) => { |
| 50 | + const commitHistory = await repoProvider.getCommitHistory( |
| 51 | + user, |
| 52 | + info.repository.owner, |
| 53 | + info.repository.name, |
| 54 | + info.revision, |
| 55 | + maxDepth, |
| 56 | + ); |
| 57 | + return { |
| 58 | + cloneUrl: info.repository.cloneUrl, |
| 59 | + commitHistory, |
| 60 | + }; |
| 61 | + }); |
| 62 | + result.additionalRepositoryCommitHistories = await Promise.all(histories); |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + public async findGoodBaseForIncrementalBuild( |
| 68 | + context: WithCommitHistories, |
| 69 | + commitContext: CommitContext, |
| 70 | + user: User, |
| 71 | + ): Promise<PrebuiltWorkspace | undefined> { |
| 72 | + if (!context.commitHistory || context.commitHistory.length < 1) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const { config } = await this.configProvider.fetchConfig({}, user, commitContext); |
| 77 | + const imageSource = await this.imageSourceProvider.getImageSource({}, user, commitContext, config); |
| 78 | + |
| 79 | + // Note: This query returns only not-garbage-collected prebuilds in order to reduce cardinality |
| 80 | + // (e.g., at the time of writing, the Gitpod repository has 16K+ prebuilds, but only ~300 not-garbage-collected) |
| 81 | + const recentPrebuilds = await this.workspaceDB.findPrebuildsWithWorkpace(commitContext.repository.cloneUrl); |
| 82 | + for (const recentPrebuild of recentPrebuilds) { |
| 83 | + if ( |
| 84 | + await this.isGoodBaseforIncrementalBuild( |
| 85 | + context, |
| 86 | + config, |
| 87 | + imageSource, |
| 88 | + recentPrebuild.prebuild, |
| 89 | + recentPrebuild.workspace, |
| 90 | + ) |
| 91 | + ) { |
| 92 | + return recentPrebuild.prebuild; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + protected async isGoodBaseforIncrementalBuild( |
| 98 | + context: WithCommitHistories, |
| 99 | + config: WorkspaceConfig, |
| 100 | + imageSource: WorkspaceImageSource, |
| 101 | + candidatePrebuild: PrebuiltWorkspace, |
| 102 | + candidateWorkspace: Workspace, |
| 103 | + ): Promise<boolean> { |
| 104 | + if (!context.commitHistory || context.commitHistory.length === 0) { |
| 105 | + log.info("Disqualified: no commitHistory"); |
| 106 | + return false; |
| 107 | + } |
| 108 | + if (!CommitContext.is(candidateWorkspace.context)) { |
| 109 | + log.info("Disqualified: candiate workspace context not a commit context"); |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + // we are only considering available prebuilds |
| 114 | + if (candidatePrebuild.state !== "available") { |
| 115 | + log.info("Disqualified: candidate prebuild not available"); |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + // we are only considering full prebuilds |
| 120 | + if (!!candidateWorkspace.basedOnPrebuildId) { |
| 121 | + log.info("Disqualified: candidate is based on another prebuild"); |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + if ( |
| 126 | + candidateWorkspace.context.additionalRepositoryCheckoutInfo?.length !== |
| 127 | + context.additionalRepositoryCommitHistories?.length |
| 128 | + ) { |
| 129 | + // different number of repos |
| 130 | + log.info( |
| 131 | + "Disqualified: candidate context additional repository checkout info !== context additional repo histories", |
| 132 | + ); |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + const candidateCtx = candidateWorkspace.context; |
| 137 | + if (!context.commitHistory.some((sha) => sha === candidateCtx.revision)) { |
| 138 | + log.info("Disqualified: candidate revision not in commit history"); |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + // check the commits are included in the commit history |
| 143 | + for (const subRepo of candidateWorkspace.context.additionalRepositoryCheckoutInfo || []) { |
| 144 | + const matchIngRepo = context.additionalRepositoryCommitHistories?.find( |
| 145 | + (repo) => repo.cloneUrl === subRepo.repository.cloneUrl, |
| 146 | + ); |
| 147 | + if (!matchIngRepo || !matchIngRepo.commitHistory.some((sha) => sha === subRepo.revision)) { |
| 148 | + log.info("Disqualified: something about matchIngRepo"); |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // ensure the image source hasn't changed (skips older images) |
| 154 | + if (JSON.stringify(imageSource) !== JSON.stringify(candidateWorkspace.imageSource)) { |
| 155 | + log.debug(`Skipping parent prebuild: Outdated image`, { |
| 156 | + imageSource, |
| 157 | + parentImageSource: candidateWorkspace.imageSource, |
| 158 | + }); |
| 159 | + log.info("Disqualified: image source has changed"); |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + // ensure the tasks haven't changed |
| 164 | + const filterPrebuildTasks = (tasks: TaskConfig[] = []) => |
| 165 | + tasks |
| 166 | + .map((task) => |
| 167 | + Object.keys(task) |
| 168 | + .filter((key) => ["before", "init", "prebuild"].includes(key)) |
| 169 | + // @ts-ignore |
| 170 | + .reduce((obj, key) => ({ ...obj, [key]: task[key] }), {}), |
| 171 | + ) |
| 172 | + .filter((task) => Object.keys(task).length > 0); |
| 173 | + const prebuildTasks = filterPrebuildTasks(config.tasks); |
| 174 | + const parentPrebuildTasks = filterPrebuildTasks(candidateWorkspace.config.tasks); |
| 175 | + if (JSON.stringify(prebuildTasks) !== JSON.stringify(parentPrebuildTasks)) { |
| 176 | + log.debug(`Skipping parent prebuild: Outdated prebuild tasks`, { |
| 177 | + prebuildTasks, |
| 178 | + parentPrebuildTasks, |
| 179 | + }); |
| 180 | + log.info("Disqualified: prebuild tasks have changed"); |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + return true; |
| 185 | + } |
| 186 | +} |
0 commit comments