Skip to content

Commit

Permalink
Improve datasource creation resilience to missing cache
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Jul 27, 2024
1 parent 0349d02 commit 5a1835e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,47 @@ export class WorkspaceDatasourceFactory {
workspaceId: string,
cacheVersion: string | null,
): Promise<WorkspaceDataSource> {
cacheVersion ??=
await this.workspaceCacheVersionService.getVersion(workspaceId);
const desiredCacheVersion =
cacheVersion ??
(await this.workspaceCacheVersionService.getVersion(workspaceId));

if (!cacheVersion) {
if (!desiredCacheVersion) {
throw new Error('Cache version not found');
}

const latestCacheVersion =
await this.workspaceCacheVersionService.getVersion(workspaceId);

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(
`${workspaceId}-${cacheVersion}`,
async () => {
Expand All @@ -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),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 5a1835e

Please sign in to comment.