Allow toggling redis compression and disable by default

This commit is contained in:
Ajay
2024-02-08 21:58:10 -05:00
parent db225f8a84
commit bf1fe1ff61
5 changed files with 76 additions and 44 deletions

View File

@@ -167,7 +167,8 @@ addDefaults(config, {
stopWritingAfterResponseTime: 50, stopWritingAfterResponseTime: 50,
responseTimePause: 1000, responseTimePause: 1000,
disableHashCache: false, disableHashCache: false,
clientCacheSize: 2000 clientCacheSize: 2000,
useCompression: false
}, },
redisRead: { redisRead: {
enabled: false, enabled: false,

View File

@@ -12,6 +12,7 @@ interface RedisConfig extends redis.RedisClientOptions {
responseTimePause: number; responseTimePause: number;
disableHashCache: boolean; disableHashCache: boolean;
clientCacheSize: number; clientCacheSize: number;
useCompression: boolean;
} }
interface RedisReadOnlyConfig extends redis.RedisClientOptions { interface RedisReadOnlyConfig extends redis.RedisClientOptions {

View File

@@ -7,7 +7,7 @@ import { config } from "../config";
async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> { async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
try { try {
const reply = await redis.getCompressed(key); const reply = await redis.getWithCache(key);
if (reply) { if (reply) {
Logger.debug(`Got data from redis: ${reply}`); Logger.debug(`Got data from redis: ${reply}`);
@@ -21,7 +21,7 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
const data = await fetchFromDB(); const data = await fetchFromDB();
redis.setExCompressed(key, config.redis?.expiryTime, JSON.stringify(data)).catch((err) => Logger.error(err)); redis.setExWithCache(key, config.redis?.expiryTime, JSON.stringify(data)).catch((err) => Logger.error(err));
return data; return data;
} }
@@ -36,7 +36,7 @@ async function getTraced<T>(fetchFromDB: () => Promise<T>, key: string): Promise
const startTime = Date.now(); const startTime = Date.now();
try { try {
const reply = await redis.getCompressed(key); const reply = await redis.getWithCache(key);
if (reply) { if (reply) {
Logger.debug(`Got data from redis: ${reply}`); Logger.debug(`Got data from redis: ${reply}`);
@@ -55,7 +55,7 @@ async function getTraced<T>(fetchFromDB: () => Promise<T>, key: string): Promise
const dbStartTime = Date.now(); const dbStartTime = Date.now();
const data = await fetchFromDB(); const data = await fetchFromDB();
redis.setExCompressed(key, config.redis?.expiryTime, JSON.stringify(data)).catch((err) => Logger.error(err)); redis.setExWithCache(key, config.redis?.expiryTime, JSON.stringify(data)).catch((err) => Logger.error(err));
return { return {
data, data,

View File

@@ -25,11 +25,11 @@ export interface RedisStats {
interface RedisSB { interface RedisSB {
get(key: RedisCommandArgument, useClientCache?: boolean): Promise<string>; get(key: RedisCommandArgument, useClientCache?: boolean): Promise<string>;
getCompressed(key: RedisCommandArgument): Promise<string>; getWithCache(key: RedisCommandArgument): Promise<string>;
set(key: RedisCommandArgument, value: RedisCommandArgument, options?: SetOptions): Promise<string>; set(key: RedisCommandArgument, value: RedisCommandArgument, options?: SetOptions): Promise<string>;
setCompressed(key: RedisCommandArgument, value: RedisCommandArgument, options?: SetOptions): Promise<string>; setWithCache(key: RedisCommandArgument, value: RedisCommandArgument, options?: SetOptions): Promise<string>;
setEx(key: RedisCommandArgument, seconds: number, value: RedisCommandArgument): Promise<string>; setEx(key: RedisCommandArgument, seconds: number, value: RedisCommandArgument): Promise<string>;
setExCompressed(key: RedisCommandArgument, seconds: number, value: RedisCommandArgument): Promise<string>; setExWithCache(key: RedisCommandArgument, seconds: number, value: RedisCommandArgument): Promise<string>;
del(...keys: [RedisCommandArgument]): Promise<number>; del(...keys: [RedisCommandArgument]): Promise<number>;
increment?(key: RedisCommandArgument): Promise<RedisCommandRawReply[]>; increment?(key: RedisCommandArgument): Promise<RedisCommandRawReply[]>;
sendCommand(args: RedisCommandArguments, options?: RedisClientOptions): Promise<RedisReply>; sendCommand(args: RedisCommandArguments, options?: RedisClientOptions): Promise<RedisReply>;
@@ -39,11 +39,11 @@ interface RedisSB {
let exportClient: RedisSB = { let exportClient: RedisSB = {
get: () => Promise.resolve(null), get: () => Promise.resolve(null),
getCompressed: () => Promise.resolve(null), getWithCache: () => Promise.resolve(null),
set: () => Promise.resolve(null), set: () => Promise.resolve(null),
setCompressed: () => Promise.resolve(null), setWithCache: () => Promise.resolve(null),
setEx: () => Promise.resolve(null), setEx: () => Promise.resolve(null),
setExCompressed: () => Promise.resolve(null), setExWithCache: () => Promise.resolve(null),
del: () => Promise.resolve(null), del: () => Promise.resolve(null),
increment: () => Promise.resolve(null), increment: () => Promise.resolve(null),
sendCommand: () => Promise.resolve(null), sendCommand: () => Promise.resolve(null),
@@ -95,7 +95,9 @@ if (config.redis?.enabled) {
let cacheClient = null as RedisClientType | null; let cacheClient = null as RedisClientType | null;
exportClient.getCompressed = (key) => { const createKeyName = (key: RedisCommandArgument) => (key + (config.redis.useCompression ? ".c" : "")) as RedisCommandArgument;
exportClient.getWithCache = (key) => {
if (cache && cacheClient && cache.has(key)) { if (cache && cacheClient && cache.has(key)) {
memoryCacheHits++; memoryCacheHits++;
return Promise.resolve(cache.get(key)); return Promise.resolve(cache.get(key));
@@ -115,9 +117,10 @@ if (config.redis?.enabled) {
return activeRequestPromises[key as string]; return activeRequestPromises[key as string];
} }
const request = exportClient.get(key).then((reply) => { const request = exportClient.get(createKeyName(key)).then((reply) => {
if (reply === null) return null; if (reply === null) return null;
if (config.redis.useCompression) {
const decompressed = uncompress(Buffer.from(reply, "base64")).then((decompressed) => decompressed.toString("utf-8")); const decompressed = uncompress(Buffer.from(reply, "base64")).then((decompressed) => decompressed.toString("utf-8"));
if (cache && shouldClientCacheKey(key)) { if (cache && shouldClientCacheKey(key)) {
decompressed.then((d) => { decompressed.then((d) => {
@@ -132,6 +135,16 @@ if (config.redis?.enabled) {
} }
return decompressed; return decompressed;
} else {
if (cache && shouldClientCacheKey(key)) {
if (!resetKeys.has(key)) {
cache.set(key, reply);
}
}
resetKeys.delete(key);
return reply;
}
}); });
activeRequestPromises[key as string] = request; activeRequestPromises[key as string] = request;
@@ -140,15 +153,32 @@ if (config.redis?.enabled) {
return request; return request;
}; };
exportClient.setCompressed = (key, value, options) => { exportClient.setWithCache = (key, value, options) => {
if (config.redis.useCompression) {
return compress(Buffer.from(value as string, "utf-8")).then((compressed) => return compress(Buffer.from(value as string, "utf-8")).then((compressed) =>
exportClient.set(key, compressed.toString("base64"), options) exportClient.set(createKeyName(key), compressed.toString("base64"), options)
); );
} else {
return exportClient.set(createKeyName(key), value, options);
}
}; };
exportClient.setExCompressed = (key, seconds, value) => { exportClient.setExWithCache = (key, seconds, value) => {
if (config.redis.useCompression) {
return compress(Buffer.from(value as string, "utf-8")).then((compressed) => return compress(Buffer.from(value as string, "utf-8")).then((compressed) =>
exportClient.setEx(key, seconds, compressed.toString("base64")) exportClient.setEx(createKeyName(key), seconds, compressed.toString("base64"))
); );
} else {
return exportClient.setEx(createKeyName(key), seconds, value);
}
};
const del = client.del.bind(client);
exportClient.del = (...keys) => {
if (config.redis.useCompression) {
return del(...keys.map((key) => createKeyName(key)) as [RedisCommandArgument]);
} else {
return del(...keys);
}
}; };
const get = client.get.bind(client); const get = client.get.bind(client);

View File

@@ -6,43 +6,43 @@ import { BrandingUUID } from "../types/branding.model";
import { RedisCommandArgument } from "@redis/client/dist/lib/commands"; import { RedisCommandArgument } from "@redis/client/dist/lib/commands";
export const skipSegmentsKey = (videoID: VideoID, service: Service): string => export const skipSegmentsKey = (videoID: VideoID, service: Service): string =>
`segments.v6.${service}.videoID.${videoID}`; `segments.v4.${service}.videoID.${videoID}`;
export const skipSegmentGroupsKey = (videoID: VideoID, service: Service): string => export const skipSegmentGroupsKey = (videoID: VideoID, service: Service): string =>
`segments.groups.v5.${service}.videoID.${videoID}`; `segments.groups.v3.${service}.videoID.${videoID}`;
export function skipSegmentsHashKey(hashedVideoIDPrefix: VideoIDHash, service: Service): string { export function skipSegmentsHashKey(hashedVideoIDPrefix: VideoIDHash, service: Service): string {
hashedVideoIDPrefix = hashedVideoIDPrefix.substring(0, 4) as VideoIDHash; hashedVideoIDPrefix = hashedVideoIDPrefix.substring(0, 4) as VideoIDHash;
if (hashedVideoIDPrefix.length !== 4) Logger.warn(`Redis skip segment hash-prefix key is not length 4! ${hashedVideoIDPrefix}`); if (hashedVideoIDPrefix.length !== 4) Logger.warn(`Redis skip segment hash-prefix key is not length 4! ${hashedVideoIDPrefix}`);
return `segments.v6.${service}.${hashedVideoIDPrefix}`; return `segments.v4.${service}.${hashedVideoIDPrefix}`;
} }
export const brandingKey = (videoID: VideoID, service: Service): string => export const brandingKey = (videoID: VideoID, service: Service): string =>
`branding.v4.${service}.videoID.${videoID}`; `branding.v2.${service}.videoID.${videoID}`;
export function brandingHashKey(hashedVideoIDPrefix: VideoIDHash, service: Service): string { export function brandingHashKey(hashedVideoIDPrefix: VideoIDHash, service: Service): string {
hashedVideoIDPrefix = hashedVideoIDPrefix.substring(0, 4) as VideoIDHash; hashedVideoIDPrefix = hashedVideoIDPrefix.substring(0, 4) as VideoIDHash;
if (hashedVideoIDPrefix.length !== 4) Logger.warn(`Redis skip segment hash-prefix key is not length 4! ${hashedVideoIDPrefix}`); if (hashedVideoIDPrefix.length !== 4) Logger.warn(`Redis skip segment hash-prefix key is not length 4! ${hashedVideoIDPrefix}`);
return `branding.v4.${service}.${hashedVideoIDPrefix}`; return `branding.v2.${service}.${hashedVideoIDPrefix}`;
} }
export const brandingIPKey = (uuid: BrandingUUID): string => export const brandingIPKey = (uuid: BrandingUUID): string =>
`branding.v2.shadow.${uuid}`; `branding.v1.shadow.${uuid}`;
export const shadowHiddenIPKey = (videoID: VideoID, timeSubmitted: number, service: Service): string => export const shadowHiddenIPKey = (videoID: VideoID, timeSubmitted: number, service: Service): string =>
`segments.v2.${service}.videoID.${videoID}.shadow.${timeSubmitted}`; `segments.v1.${service}.videoID.${videoID}.shadow.${timeSubmitted}`;
export const reputationKey = (userID: UserID): string => export const reputationKey = (userID: UserID): string =>
`reputation.v2.user.${userID}`; `reputation.v1.user.${userID}`;
export function ratingHashKey(hashPrefix: VideoIDHash, service: Service): string { export function ratingHashKey(hashPrefix: VideoIDHash, service: Service): string {
hashPrefix = hashPrefix.substring(0, 4) as VideoIDHash; hashPrefix = hashPrefix.substring(0, 4) as VideoIDHash;
if (hashPrefix.length !== 4) Logger.warn(`Redis rating hash-prefix key is not length 4! ${hashPrefix}`); if (hashPrefix.length !== 4) Logger.warn(`Redis rating hash-prefix key is not length 4! ${hashPrefix}`);
return `rating.v2.${service}.${hashPrefix}`; return `rating.v1.${service}.${hashPrefix}`;
} }
export function shaHashKey(singleIter: HashedValue): string { export function shaHashKey(singleIter: HashedValue): string {
@@ -55,17 +55,17 @@ export const tempVIPKey = (userID: HashedUserID): string =>
`vip.temp.${userID}`; `vip.temp.${userID}`;
export const videoLabelsKey = (videoID: VideoID, service: Service): string => export const videoLabelsKey = (videoID: VideoID, service: Service): string =>
`labels.v3.${service}.videoID.${videoID}`; `labels.v1.${service}.videoID.${videoID}`;
export function videoLabelsHashKey(hashedVideoIDPrefix: VideoIDHash, service: Service): string { export function videoLabelsHashKey(hashedVideoIDPrefix: VideoIDHash, service: Service): string {
hashedVideoIDPrefix = hashedVideoIDPrefix.substring(0, 3) as VideoIDHash; hashedVideoIDPrefix = hashedVideoIDPrefix.substring(0, 3) as VideoIDHash;
if (hashedVideoIDPrefix.length !== 3) Logger.warn(`Redis video labels hash-prefix key is not length 3! ${hashedVideoIDPrefix}`); if (hashedVideoIDPrefix.length !== 3) Logger.warn(`Redis video labels hash-prefix key is not length 3! ${hashedVideoIDPrefix}`);
return `labels.v3.${service}.${hashedVideoIDPrefix}`; return `labels.v1.${service}.${hashedVideoIDPrefix}`;
} }
export function userFeatureKey (userID: HashedUserID, feature: Feature): string { export function userFeatureKey (userID: HashedUserID, feature: Feature): string {
return `user.v2.${userID}.feature.${feature}`; return `user.v1.${userID}.feature.${feature}`;
} }
export function shouldClientCacheKey(key: RedisCommandArgument): boolean { export function shouldClientCacheKey(key: RedisCommandArgument): boolean {