mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2026-12-01 15:06:07 -06:00
Drops the `redundancy` option from the three `S3Cache` put paths. `S3Bucket.put` already falls back to `STANDARD` when nothing is passed (`lib/s3-handler.ts:77`), so the deletion is the whole fix. One thing beyond what the issue lists: `lib/stats.ts` asks for the same class when it flushes compilation records, and that is the only other caller in the tree. Those records are not reconstructible the way a cache entry is, so the lower durability costs something there and buys nothing. I moved it across in the same commit rather than leaving a single RRS write behind, but say the word and I will pull it back out into its own PR. For the regression guard I added a case per file, both asserting the `StorageClass` that reaches the mocked `PutObjectCommand`: `test/cache-tests.ts` covers `put`, `putWithTTL` and `putWithTTLAndPath`, and `test/stats-test.ts` covers the flush. Reverting either source file turns the matching test red with `REDUCED_REDUNDANCY` instead of `STANDARD`. Verified in a `node:22-bookworm` container, which is where my node_modules live: full suite 2630 passed, 1 skipped across 122 files, plus `npm run ts-check` and `biome check` clean. Committed through the normal pre-commit hook.
116 lines
4.2 KiB
TypeScript
116 lines
4.2 KiB
TypeScript
// Copyright (c) 2018, Compiler Explorer Authors
|
|
// All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
// documentation and/or other materials provided with the distribution.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
import {Buffer} from 'buffer';
|
|
|
|
import type {GetResult} from '../../types/cache.interfaces.js';
|
|
import {logger} from '../logger.js';
|
|
import type {S3HandlerOptions} from '../s3-handler.interfaces.js';
|
|
import {S3Bucket} from '../s3-handler.js';
|
|
import {SentryCapture} from '../sentry.js';
|
|
import {BaseCache} from './base.js';
|
|
|
|
function messageFor(e: any) {
|
|
return e.message || e.toString();
|
|
}
|
|
|
|
export class S3Cache extends BaseCache {
|
|
private readonly s3: S3Bucket;
|
|
readonly path: string;
|
|
readonly region: string;
|
|
private readonly onError: (error: any, msg: string) => void;
|
|
|
|
constructor(
|
|
cacheName: string,
|
|
bucket: string,
|
|
path: string,
|
|
region: string,
|
|
onError?: (error: any, msg: string) => void,
|
|
) {
|
|
super(cacheName, `S3Cache(s3://${bucket}/${path} in ${region})`, 's3');
|
|
this.path = path;
|
|
this.region = region;
|
|
this.s3 = new S3Bucket(bucket, region);
|
|
this.onError =
|
|
onError ||
|
|
((e, op) => {
|
|
SentryCapture(e, 'S3Cache onError');
|
|
logger.error(`Error while trying to ${op} S3 cache: ${messageFor(e)}`);
|
|
});
|
|
}
|
|
|
|
override async getInternal(key: string): Promise<GetResult> {
|
|
try {
|
|
return await this.s3.get(key, this.path);
|
|
} catch (e) {
|
|
this.onError(e, 'read');
|
|
return {hit: false};
|
|
}
|
|
}
|
|
|
|
override async putInternal(key: string, value: Buffer, creator?: string): Promise<void> {
|
|
const options: S3HandlerOptions = {
|
|
metadata: creator ? {CreatedBy: creator} : {},
|
|
};
|
|
try {
|
|
await this.s3.put(key, value, this.path, options);
|
|
} catch (e) {
|
|
this.onError(e, 'write');
|
|
}
|
|
}
|
|
|
|
async putWithTTL(key: string, value: Buffer, ttlDays: number, creator?: string): Promise<void> {
|
|
const expiresDate = new Date(Date.now() + ttlDays * 24 * 60 * 60 * 1000);
|
|
const options: S3HandlerOptions = {
|
|
metadata: creator ? {CreatedBy: creator} : {},
|
|
expires: expiresDate,
|
|
};
|
|
try {
|
|
await this.s3.put(key, value, this.path, options);
|
|
} catch (e) {
|
|
this.onError(e, 'write');
|
|
}
|
|
}
|
|
|
|
async putWithTTLAndPath(
|
|
key: string,
|
|
value: Buffer,
|
|
ttlDays: number,
|
|
pathPrefix: string,
|
|
creator?: string,
|
|
): Promise<void> {
|
|
const expiresDate = new Date(Date.now() + ttlDays * 24 * 60 * 60 * 1000);
|
|
const options: S3HandlerOptions = {
|
|
metadata: creator ? {CreatedBy: creator} : {},
|
|
expires: expiresDate,
|
|
};
|
|
try {
|
|
const customPath = `${this.path}/${pathPrefix}`;
|
|
await this.s3.put(key, value, customPath, options);
|
|
} catch (e) {
|
|
this.onError(e, 'write');
|
|
}
|
|
}
|
|
}
|