Skip to content
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

🐐 Experimental repository cache #731

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
10 changes: 10 additions & 0 deletions tdrive/backend/node/config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
"password": "DB_POSTGRES_PASSWORD",
"port": "DB_POSTGRES_PORT",
"ssl": "DB_POSTGRES_USE_SSL"
},
"localMemCache": {
"ttlS": "DB_LCM_TTL_S",
"maxKeyCount": "DB_LCM_MAX_KEY_COUNT",
"printPeriodMs": "DB_LCM_PRINT_PERIOD_MS",
"printPeriodIdleMs": "DB_LCM_PRINT_PERIOD_IDLE_MS",
"extraNodeCacheConfig": {
"__name": "DB_LCM_NODE_CACHE_EXTRA_CONFIG",
"__format": "json"
}
}
},
"search": {
Expand Down
6 changes: 6 additions & 0 deletions tdrive/backend/node/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
"mongodb": {
"uri": "mongodb://mongo:27017",
"database": "tdrive"
},
"localMemCache": {
"ttlS": 5,
"maxKeyCount": 10000,
"printPeriodMs": 180000,
"printPeriodIdleMs": 180000
}
},
"message-queue": {
Expand Down
2 changes: 1 addition & 1 deletion tdrive/backend/node/src/core/platform/framework/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const logger = pino({
name: "TdriveApp",
level: config.get("level", "info") || "info",
mixin() {
return executionStorage.getStore() ? executionStorage.getStore() : {};
return executionStorage.getStore() ? { ...executionStorage.getStore() } : {};
},
formatters: {
level(label: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,13 @@ export class MongoConnector extends AbstractConnector<MongoConnectionOptions> {
: {};
}

logger.debug(`services.database.orm.mongodb.find - Query: ${JSON.stringify(query)}`);
logger.debug(
{
entity: entityDefinition.name,
query,
},
"services.database.orm.mongodb.find",
);

const cursor = collection
.find(query)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import Repository, { AtomicCompareAndSetResult, FindFilter, FindOptions } from "./repository";
import { logger } from "../../../../../../../core/platform/framework";
import { ExecutionContext } from "../../../../../framework/api/crud-service";
import { Connector } from "../connectors";
import { EntityTarget } from "../types";
import NodeCache from "node-cache";
import config from "../../../../../../../core/config";

/** Configuration structure in `database.localMemCache` */
interface ICachingRepositoryConfig {
/** Maximum time in seconds to keep a given key */
ttlS: number;
/** Maximum total number of keys */
maxKeyCount: number;
/** Consider logging the statistics at this period, if active will log immediately */
printPeriodMs: number;
/** If during a print period, this time elapsed and it is still unused, print anyway */
printPeriodIdleMs: number;
/**
* Object with additional configuration for node-cache (overrides the other configuration fields here).
* https://www.npmjs.com/package/node-cache#initialize-init
*/
extraNodeCacheConfig?: ConstructorParameters<typeof NodeCache>[0];
}

const loadConfig = () => config.get("database.localMemCache") as ICachingRepositoryConfig;

const emptyStats = () => ({ hits: 0, misses: 0, wrongIndex: 0, start: new Date() });
/**
* This is a passthrough for {@link Repository} that caches requests by a provided
* `keys` list of fields that must be globally unique.
* Only {@link Repository.findOne} returns from cache.
*/
export default class CachingRepository<EntityType> extends Repository<EntityType> {
private readonly cache;
private cacheStats = emptyStats();

private startPrintingStats(configuration: ICachingRepositoryConfig) {
setInterval(() => {
const stats = this.cacheStats;
const ageMs = new Date().getTime() - stats.start.getTime();
const cacheName = `CachingRepository<${this.table}>(${this.keys.join(", ")})`;
if (stats.hits + stats.misses + stats.wrongIndex === 0) {
if (ageMs < configuration.printPeriodIdleMs) return;
logger.info(`${cacheName} - unused since ${ageMs / 1000}s`);
return;
}
const libCacheStats = this.cache.getStats();
this.cacheStats = emptyStats();
logger.info(
{
cacheName,
stats: {
keyCount: libCacheStats.keys,
valueSize: libCacheStats.vsize,
...stats,
},
ageMs,
},
`${cacheName} had ${stats.hits} hits and ${stats.misses} misses (${
stats.wrongIndex
} mismatched keys) in ${ageMs / 1000}s`,
);
}, configuration.printPeriodMs);
}

constructor(
connector: Connector,
table: string,
entityType: EntityTarget<EntityType>,
private readonly keys: string[],
) {
super(connector, table, entityType);
const configuration = loadConfig();
this.cache = new NodeCache({
stdTTL: configuration.ttlS,
maxKeys: configuration.maxKeyCount,
...(configuration.extraNodeCacheConfig ?? {}),
});
this.keys.sort();
this.startPrintingStats(configuration);
}

private cacheGetEntityKey(entity: EntityType | FindFilter | undefined): string | undefined {
if (!entity) return undefined;
const indices = this.keys.map(k => entity[k] && encodeURIComponent(entity[k]));
if (indices.some(x => !x)) return undefined;
return indices.join("&");
}

private cacheInvalidateEntity(entity: EntityType | undefined) {
const key = this.cacheGetEntityKey(entity);
if (key) this.cache.del(key);
}

private cacheGet(keys: FindFilter): EntityType | undefined {
const key = this.cacheGetEntityKey(keys);
if (!key) {
this.cacheStats.wrongIndex++;
return undefined;
}
const cached = this.cache.get(key);
if (cached) {
this.cacheStats.hits++;
return cached;
}
this.cacheStats.misses++;
return undefined;
}

private cacheSave(entity: EntityType) {
const key = entity && this.cacheGetEntityKey(entity);
if (!key) return;
this.cache.set(key, entity);
}

override async atomicCompareAndSet<FieldValueType>(
entity: EntityType,
fieldName: keyof EntityType,
previousValue: FieldValueType | null,
newValue: FieldValueType | null,
): Promise<AtomicCompareAndSetResult<FieldValueType>> {
this.cacheInvalidateEntity(entity);
return await super.atomicCompareAndSet(entity, fieldName, previousValue, newValue);
}

override async save(entity: EntityType, _context?: ExecutionContext): Promise<void> {
this.cacheInvalidateEntity(entity);
await super.save(entity, _context);
}

override async saveAll(entities: EntityType[] = [], _context?: ExecutionContext): Promise<void> {
entities.forEach(entity => this.cacheInvalidateEntity(entity));
await super.saveAll(entities, _context);
}

override async remove(entity: EntityType, _context?: ExecutionContext): Promise<void> {
this.cacheInvalidateEntity(entity);
await super.remove(entity, _context);
}

async findOne(
filters: FindFilter,
options: FindOptions = {},
context?: ExecutionContext,
): Promise<EntityType> {
const cachedValue = this.cacheGet(filters);
if (cachedValue) return cachedValue;
const result = (await this.find(filters, options, context)).getEntities()[0] || null;
this.cacheSave(result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { logger } from "../../../../../../../core/platform/framework";
import DatabaseService from "../..";
import Repository from "./repository";
import CachingRepository from "./caching-repository";
import { EntityTarget } from "../types";

export class RepositoryManager {
private static toCacheEntities = new Map<string, string[]>();
/** When an entity is called with a key, registry instances from `getRepository` will be {@link CachingRepository} */
public static registerEntityToCacheRegistryBy(table: string, keys: string[]) {
this.toCacheEntities.set(table, keys);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private repositories: Map<string, Repository<any>> = new Map<string, Repository<any>>();

Expand All @@ -14,7 +20,15 @@ export class RepositoryManager {
entity: EntityTarget<Entity>,
): Promise<Repository<Entity>> {
if (!this.repositories.has(table)) {
const repository = new Repository<Entity>(this.databaseService.getConnector(), table, entity);
const cacheKeys = RepositoryManager.toCacheEntities.get(table);
const repository = cacheKeys
? new CachingRepository<Entity>(
this.databaseService.getConnector(),
table,
entity,
cacheKeys,
)
: new Repository<Entity>(this.databaseService.getConnector(), table, entity);

try {
await repository.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import { DriveFileAccessLevel, publicAccessLevel } from "../types";
import { FileVersion } from "./file-version";
import search from "./drive-file.search";
import * as UUIDTools from "../../../utils/uuid";
import { RepositoryManager } from "../../../core/platform/services/database/services/orm/repository/manager";

export const TYPE = "drive_files";
export type DriveScope = "personal" | "shared";

RepositoryManager.registerEntityToCacheRegistryBy(TYPE, ["id"]);

/**
* This represents an item in the file hierarchy.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { merge } from "lodash";
import { Column, Entity } from "../../../core/platform/services/database/services/orm/decorators";
import { CompanyUserRole } from "../web/types";
import { RepositoryManager } from "../../../core/platform/services/database/services/orm/repository/manager";

export const TYPE = "group_user";

RepositoryManager.registerEntityToCacheRegistryBy(TYPE, ["group_id", "user_id"]);

/**
* Link between a company and a user
*/
Expand Down
3 changes: 3 additions & 0 deletions tdrive/backend/node/src/services/user/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { isNumber, merge } from "lodash";
import { Column, Entity } from "../../../core/platform/services/database/services/orm/decorators";
import search from "./user.search";
import { uuid } from "../../../utils/types";
import { RepositoryManager } from "../../../core/platform/services/database/services/orm/repository/manager";

export const TYPE = "user";
export type UserType = "anonymous" | "tech" | "regular";

RepositoryManager.registerEntityToCacheRegistryBy(TYPE, ["id"]);

@Entity(TYPE, {
primaryKey: [["id"]],
globalIndexes: [["email_canonical"], ["username_canonical"]],
Expand Down