Improve datasource creation resilience to missing cache

This commit is contained in:
Charles Bochet
2024-07-27 11:35:47 +02:00
parent 0349d02ede
commit 5a1835e9e0
3 changed files with 49 additions and 35 deletions

View File

@ -28,11 +28,45 @@ export class WorkspaceDatasourceFactory {
workspaceId: string,
cacheVersion: string | null,
): Promise<WorkspaceDataSource> {
cacheVersion ??=
const desiredCacheVersion =
cacheVersion ??
(await this.workspaceCacheVersionService.getVersion(workspaceId));
if (!desiredCacheVersion) {
throw new Error('Cache version not found');
}
const latestCacheVersion =
await this.workspaceCacheVersionService.getVersion(workspaceId);
if (!cacheVersion) {
throw new Error('Cache version not found');
if (latestCacheVersion !== desiredCacheVersion) {
throw new Error('Cache version mismatch');
}
let cachedObjectMetadataCollection =
await this.workspaceCacheStorageService.getObjectMetadataCollection(
workspaceId,
);
if (!cachedObjectMetadataCollection) {
const freshObjectMetadataCollection =
await this.objectMetadataRepository.find({
where: { workspaceId },
relations: [
'fields.object',
'fields',
'fields.fromRelationMetadata',
'fields.toRelationMetadata',
'fields.fromRelationMetadata.toObjectMetadata',
],
});
await this.workspaceCacheStorageService.setObjectMetadataCollection(
workspaceId,
freshObjectMetadataCollection,
);
cachedObjectMetadataCollection = freshObjectMetadataCollection;
}
const workspaceDataSource = await workspaceDataSourceCacheInstance.execute(
@ -47,38 +81,12 @@ export class WorkspaceDatasourceFactory {
throw new Error('Data source metadata not found');
}
const latestCacheVersion =
await this.workspaceCacheVersionService.getVersion(workspaceId);
if (latestCacheVersion !== cacheVersion) {
throw new Error('Cache version mismatch');
}
let objectMetadataCollection =
await this.workspaceCacheStorageService.getObjectMetadataCollection(
workspaceId,
);
if (!objectMetadataCollection) {
objectMetadataCollection = await this.objectMetadataRepository.find({
where: { workspaceId },
relations: [
'fields.object',
'fields',
'fields.fromRelationMetadata',
'fields.toRelationMetadata',
'fields.fromRelationMetadata.toObjectMetadata',
],
});
await this.workspaceCacheStorageService.setObjectMetadataCollection(
workspaceId,
objectMetadataCollection,
);
if (!cachedObjectMetadataCollection) {
throw new Error('Object metadata collection not found');
}
const entities = await Promise.all(
objectMetadataCollection.map((objectMetadata) =>
cachedObjectMetadataCollection.map((objectMetadata) =>
this.entitySchemaFactory.create(workspaceId, objectMetadata),
),
);

View File

@ -634,7 +634,7 @@ export class WorkspaceRepository<
);
throw new Error(
`Object metadata for object "${objectMetadataName}" is missing` +
`Object metadata for object "${objectMetadataName}" is missing ` +
`in workspace "${this.internalContext.workspaceId}" ` +
`with object metadata collection length: ${objectMetadataCollection?.length}`,
);

View File

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
@ -8,6 +8,8 @@ import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/worksp
@Injectable()
export class WorkspaceCacheStorageService {
private readonly logger = new Logger(WorkspaceCacheStorageService.name);
constructor(
@InjectCacheStorage(CacheStorageNamespace.WorkspaceSchema)
private readonly workspaceSchemaCache: CacheStorageService,
@ -24,7 +26,11 @@ export class WorkspaceCacheStorageService {
await this.workspaceCacheVersionService.getVersion(workspaceId);
if (!latestVersion || currentVersion !== latestVersion) {
// Invalidate cache if version mismatch is detected
// Invalidate cache if version mismatch is detected"
this.logger.log(
`Cache version mismatch detected for workspace ${workspaceId}. Current version: ${currentVersion}. Latest version: ${latestVersion}. Invalidating cache...`,
);
await this.invalidateCache(workspaceId);
// If the latest version is not found, increment the version