mirror of
https://github.com/hcengineering/platform.git
synced 2024-11-22 03:14:40 +03:00
UBERF-8578: Fix extra stat call for storage adapter (#7132)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
parent
a05e2a31d9
commit
04c8c2ffa0
@ -28,17 +28,20 @@ export async function yDocFromStorage (
|
||||
ydoc?: YDoc
|
||||
): Promise<YDoc | undefined> {
|
||||
// stat the object to ensure it exists, because read will throw an error in this case
|
||||
const blob = await storageAdapter.stat(ctx, workspace, documentId)
|
||||
if (blob === undefined) {
|
||||
return undefined
|
||||
}
|
||||
try {
|
||||
const buffer = await storageAdapter.read(ctx, workspace, documentId)
|
||||
|
||||
// no need to apply gc because we load existing document
|
||||
// it is either already gc-ed, or gc not needed and it is disabled
|
||||
ydoc ??= new YDoc({ guid: generateId(), gc: false })
|
||||
|
||||
const buffer = await storageAdapter.read(ctx, workspace, documentId)
|
||||
return yDocFromBuffer(Buffer.concat(buffer as any), ydoc)
|
||||
} catch (err: any) {
|
||||
if (err.code === 'NoSuchKey') {
|
||||
return undefined
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
/** @public */
|
||||
|
@ -48,8 +48,8 @@ import type { Request, Response } from '@hcengineering/rpc'
|
||||
import type { Token } from '@hcengineering/server-token'
|
||||
import { type Readable } from 'stream'
|
||||
import type { DbAdapter, DomainHelper } from './adapter'
|
||||
import { type StorageAdapter } from './storage'
|
||||
import type { StatisticsElement } from './stats'
|
||||
import { type StorageAdapter } from './storage'
|
||||
|
||||
export interface ServerFindOptions<T extends Doc> extends FindOptions<T> {
|
||||
domain?: Domain // Allow to find for Doc's in specified domain only.
|
||||
@ -519,6 +519,17 @@ export interface StorageConfig {
|
||||
port?: number
|
||||
}
|
||||
|
||||
export class NoSuchKeyError extends Error {
|
||||
code: string
|
||||
constructor (
|
||||
msg: string,
|
||||
readonly cause?: any
|
||||
) {
|
||||
super(msg)
|
||||
this.code = 'NoSuchKey'
|
||||
}
|
||||
}
|
||||
|
||||
export interface StorageConfiguration {
|
||||
default: string
|
||||
storages: StorageConfig[]
|
||||
|
@ -401,7 +401,7 @@ export function start (
|
||||
}
|
||||
|
||||
let blobInfo = await ctx.with(
|
||||
'notoken-stat',
|
||||
'stat',
|
||||
{ workspace: payload.workspace.name },
|
||||
async (ctx) => await config.storageAdapter.stat(ctx, payload.workspace, uuid)
|
||||
)
|
||||
|
@ -29,6 +29,7 @@ import core, {
|
||||
} from '@hcengineering/core'
|
||||
import { getMetadata } from '@hcengineering/platform'
|
||||
import serverCore, {
|
||||
NoSuchKeyError,
|
||||
type BlobStorageIterator,
|
||||
type ListBlobResult,
|
||||
type StorageAdapter,
|
||||
@ -316,6 +317,7 @@ export class S3Service implements StorageAdapter {
|
||||
}
|
||||
|
||||
async doGet (ctx: MeasureContext, workspaceId: WorkspaceId, objectName: string, range?: string): Promise<Readable> {
|
||||
try {
|
||||
const res = await this.client.getObject({
|
||||
Bucket: this.getBucketId(workspaceId),
|
||||
Key: this.getDocumentKey(workspaceId, objectName),
|
||||
@ -332,6 +334,10 @@ export class S3Service implements StorageAdapter {
|
||||
readable.push(null)
|
||||
return readable
|
||||
}
|
||||
} catch (err: any) {
|
||||
// In case of error return undefined
|
||||
throw new NoSuchKeyError(`${workspaceId.name} missing ${objectName}`, err)
|
||||
}
|
||||
}
|
||||
|
||||
@withContext('put')
|
||||
|
@ -144,7 +144,7 @@ export class FallbackStorageAdapter implements StorageAdapter, StorageAdapterEx
|
||||
return result
|
||||
}
|
||||
|
||||
@withContext('aggregator-delete', {})
|
||||
@withContext('fallback-delete', {})
|
||||
async delete (ctx: MeasureContext, workspaceId: WorkspaceId): Promise<void> {
|
||||
for (const { adapter } of this.adapters) {
|
||||
if (await adapter.exists(ctx, workspaceId)) {
|
||||
@ -153,7 +153,7 @@ export class FallbackStorageAdapter implements StorageAdapter, StorageAdapterEx
|
||||
}
|
||||
}
|
||||
|
||||
@withContext('aggregator-remove', {})
|
||||
@withContext('fallback-remove', {})
|
||||
async remove (ctx: MeasureContext, workspaceId: WorkspaceId, objectNames: string[]): Promise<void> {
|
||||
// Group by provider and delegate into it.
|
||||
for (const { adapter } of this.adapters) {
|
||||
@ -173,40 +173,30 @@ export class FallbackStorageAdapter implements StorageAdapter, StorageAdapterEx
|
||||
}
|
||||
}
|
||||
|
||||
@withContext('aggregator-stat', {})
|
||||
async stat (ctx: MeasureContext, workspaceId: WorkspaceId, name: string): Promise<Blob | undefined> {
|
||||
const result = await this.findProvider(ctx, workspaceId, name)
|
||||
if (result !== undefined) {
|
||||
result.stat.provider = result.name
|
||||
}
|
||||
return result?.stat
|
||||
}
|
||||
|
||||
@withContext('aggregator-get', {})
|
||||
async get (ctx: MeasureContext, workspaceId: WorkspaceId, name: string): Promise<Readable> {
|
||||
const result = await this.findProvider(ctx, workspaceId, name)
|
||||
if (result === undefined) {
|
||||
throw new NoSuchKeyError(`${workspaceId.name} missing ${name}`)
|
||||
}
|
||||
return await result.adapter.get(ctx, workspaceId, result.stat._id)
|
||||
}
|
||||
|
||||
@withContext('find-provider', {})
|
||||
private async findProvider (
|
||||
ctx: MeasureContext,
|
||||
workspaceId: WorkspaceId,
|
||||
objectName: string
|
||||
): Promise<{ name: string, adapter: StorageAdapter, stat: Blob } | undefined> {
|
||||
// Group by provider and delegate into it.
|
||||
@withContext('fallback-stat', {})
|
||||
async stat (ctx: MeasureContext, workspaceId: WorkspaceId, objectName: string): Promise<Blob | undefined> {
|
||||
for (const { name, adapter } of this.adapters) {
|
||||
const stat = await adapter.stat(ctx, workspaceId, objectName)
|
||||
if (stat !== undefined) {
|
||||
return { name, adapter, stat }
|
||||
stat.provider = name
|
||||
return stat
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@withContext('aggregator-partial', {})
|
||||
@withContext('fallback-get', {})
|
||||
async get (ctx: MeasureContext, workspaceId: WorkspaceId, objectName: string): Promise<Readable> {
|
||||
for (const { adapter } of this.adapters) {
|
||||
try {
|
||||
return await adapter.get(ctx, workspaceId, objectName)
|
||||
} catch (err: any) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
throw new NoSuchKeyError(`${workspaceId.name} missing ${objectName}`)
|
||||
}
|
||||
|
||||
@withContext('fallback-partial', {})
|
||||
async partial (
|
||||
ctx: MeasureContext,
|
||||
workspaceId: WorkspaceId,
|
||||
@ -214,20 +204,26 @@ export class FallbackStorageAdapter implements StorageAdapter, StorageAdapterEx
|
||||
offset: number,
|
||||
length?: number | undefined
|
||||
): Promise<Readable> {
|
||||
const result = await this.findProvider(ctx, workspaceId, objectName)
|
||||
if (result === undefined) {
|
||||
throw new NoSuchKeyError(`${workspaceId.name} missing ${objectName}`)
|
||||
for (const { adapter } of this.adapters) {
|
||||
try {
|
||||
return await adapter.partial(ctx, workspaceId, objectName, offset, length)
|
||||
} catch (err: any) {
|
||||
// ignore
|
||||
}
|
||||
return await result.adapter.partial(ctx, workspaceId, result.stat._id, offset, length)
|
||||
}
|
||||
throw new NoSuchKeyError(`${workspaceId.name} missing ${objectName}`)
|
||||
}
|
||||
|
||||
@withContext('aggregator-read', {})
|
||||
@withContext('fallback-read', {})
|
||||
async read (ctx: MeasureContext, workspaceId: WorkspaceId, objectName: string): Promise<Buffer[]> {
|
||||
const result = await this.findProvider(ctx, workspaceId, objectName)
|
||||
if (result === undefined) {
|
||||
throw new NoSuchKeyError(`${workspaceId.name} missing ${objectName}`)
|
||||
for (const { adapter } of this.adapters) {
|
||||
try {
|
||||
return await adapter.read(ctx, workspaceId, objectName)
|
||||
} catch (err: any) {
|
||||
// Ignore
|
||||
}
|
||||
return await result.adapter.read(ctx, workspaceId, result.stat._id)
|
||||
}
|
||||
throw new NoSuchKeyError(`${workspaceId.name} missing ${objectName}`)
|
||||
}
|
||||
|
||||
@withContext('aggregator-put', {})
|
||||
|
Loading…
Reference in New Issue
Block a user