@milaboratories/pl-drivers 1.11.30 → 1.11.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"upload_task.d.ts","sourceRoot":"","sources":["../../src/drivers/upload_task.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAG1D,OAAO,KAAK,KAAK,GAAG,MAAM,iCAAiC,CAAC;AAC5D,OAAO,KAAK,EAAuB,QAAQ,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAExF,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,qBAAqB,CAAC;AAC1E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACnG,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAC;AAErD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEvD;;gCAEgC;AAChC,qBAAa,UAAU;IAmBnB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,yBAAyB;aAE1B,GAAG,EAAE,sBAAsB;IAvB7C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoC;IAC3D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwC;IAChE,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,yBAAyB,CAAK;IACtC,OAAO,CAAC,sBAAsB,CAAM;IAEpC,4DAA4D;IAC5D,OAAO,CAAC,UAAU,CAAC,CAA6B;IACzC,QAAQ,EAAE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MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAOpF;AA2BD,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,GAAG,WAU3C;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,GAAG,0EAKzC"}
@@ -1,5 +1,5 @@
1
1
  import { ChangeSource } from '@milaboratories/computable';
2
- import { resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';
2
+ import { resourceIdToString, isTimeoutError, stringifyWithResourceId } from '@milaboratories/pl-client';
3
3
  import { CallersCounter, asyncPool } from '@milaboratories/ts-helpers';
4
4
  import { MTimeError, UnexpectedEOF, NoFileForUploading, BadRequestError } from '../clients/upload.js';
5
5
  import { ImportFileHandleUploadData } from './types.js';
@@ -61,10 +61,9 @@ class UploadTask {
61
61
  this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} finished`);
62
62
  }
63
63
  catch (e) {
64
- if (isResourceWasDeletedError(e)) {
65
- this.setRetriableError(e);
66
- this.logger.warn(`resource was deleted while uploading a blob: ${e}`);
67
- this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} aborted, resource was deleted`);
64
+ if (isTerminalUploadError(e)) {
65
+ this.logger.warn(`terminal error while uploading a blob: ${e}`);
66
+ this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} aborted: ${e.code}`);
68
67
  this.setDone(true);
69
68
  return;
70
69
  }
@@ -96,13 +95,13 @@ class UploadTask {
96
95
  }
97
96
  catch (e) {
98
97
  this.setRetriableError(e);
99
- if ((e.name == 'RpcError' && e.code == 'DEADLINE_EXCEEDED') || e?.message?.includes('DEADLINE_EXCEEDED')) {
98
+ if (isTimeoutError(e)) {
100
99
  this.logger.warn(`deadline exceeded while getting a status of BlobImport: ${e.message}`);
101
100
  return;
102
101
  }
103
- if (isResourceWasDeletedError(e)) {
104
- this.logger.warn(`resource was not found while updating a status of BlobImport: ${e}, ${stringifyWithResourceId(this.res)}`);
105
- this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} changed, resource not found`);
102
+ if (isTerminalUploadError(e)) {
103
+ this.logger.warn(`terminal error while updating BlobImport status: ${e}, ${stringifyWithResourceId(this.res)}`);
104
+ this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} aborted: ${e.code}`);
106
105
  this.setDone(true);
107
106
  return;
108
107
  }
@@ -256,9 +255,17 @@ function doneProgressIfExisted(alreadyExisted, status) {
256
255
  }
257
256
  return status;
258
257
  }
259
- function isResourceWasDeletedError(e) {
260
- return (e.name == 'RpcError'
261
- && (e.code == 'NOT_FOUND' || e.code == 'ABORTED' || e.code == 'ALREADY_EXISTS'));
258
+ function isTerminalUploadError(e) {
259
+ if (e?.name !== 'RpcError')
260
+ return false;
261
+ switch (e.code) {
262
+ case 'NOT_FOUND':
263
+ case 'ABORTED':
264
+ case 'ALREADY_EXISTS':
265
+ return true;
266
+ default:
267
+ return false;
268
+ }
262
269
  }
263
270
  function nonRecoverableError(e) {
264
271
  return e instanceof MTimeError ||
@@ -285,5 +292,5 @@ function decreaseConcurrency(logger, current, min) {
285
292
  return newConcurrency;
286
293
  }
287
294
 
288
- export { UploadTask, isMyUpload, isResourceWasDeletedError, isSignMatch, isUpload, nonRecoverableError, uploadBlob };
295
+ export { UploadTask, isMyUpload, isSignMatch, isTerminalUploadError, isUpload, nonRecoverableError, uploadBlob };
289
296
  //# sourceMappingURL=upload_task.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"upload_task.js","sources":["../../src/drivers/upload_task.ts"],"sourcesContent":["import type { Watcher } from '@milaboratories/computable';\nimport { ChangeSource } from '@milaboratories/computable';\nimport { resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';\nimport type * as sdk from '@milaboratories/pl-model-common';\nimport type { AsyncPoolController, MiLogger, Signer } from '@milaboratories/ts-helpers';\nimport { asyncPool, CallersCounter } from '@milaboratories/ts-helpers';\nimport type { ClientProgress, ProgressStatus } from '../clients/progress';\nimport type { ClientUpload } from '../clients/upload';\nimport { BadRequestError, MTimeError, NoFileForUploading, UnexpectedEOF } from '../clients/upload';\nimport type { ImportResourceSnapshot } from './types';\nimport { ImportFileHandleUploadData } from './types';\nimport assert from 'node:assert';\nimport { ResourceInfo } from '@milaboratories/pl-tree';\n\n/** Holds all info needed to upload a file and a status of uploading\n * and indexing. Also, has a method to update a status of the progress.\n * And holds a change source. */\nexport class UploadTask {\n private readonly change: ChangeSource = new ChangeSource();\n private readonly counter: CallersCounter = new CallersCounter();\n private nMaxUploads: number;\n private nPartsWithThisUploadSpeed = 0;\n private nPartsToIncreaseUpload = 10; // how many parts we have to wait to increase concurrency, 50 mb, 10 parts by 5 mb each.\n\n /** If this is upload progress this field will be defined */\n private uploadData?: ImportFileHandleUploadData;\n public progress: sdk.ImportProgress;\n\n /** If failed, then getting a progress is terminally failed. */\n public failed?: boolean;\n\n /** True if the blob was existed.\n * At this case, the task will show progress == 1.0. */\n private alreadyExisted = false;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly clientBlob: ClientUpload,\n private readonly clientProgress: ClientProgress,\n private readonly maxNConcurrentPartsUpload: number,\n signer: Signer,\n public readonly res: ImportResourceSnapshot,\n ) {\n this.nMaxUploads = this.maxNConcurrentPartsUpload;\n const { uploadData, progress } = newProgress(res, signer);\n this.uploadData = uploadData;\n this.progress = progress;\n }\n\n public getProgress(w: Watcher, callerId: string) {\n this.incCounter(w, callerId);\n\n if (this.failed) {\n this.logger.error(`Uploading terminally failed: ${this.progress.lastError}`);\n throw new Error(this.progress.lastError);\n }\n\n return cloneProgress(this.progress);\n }\n\n public shouldScheduleUpload(): boolean {\n return isMyUpload(this.progress);\n }\n\n /** Uploads a blob if it's not BlobIndex. */\n public async uploadBlobTask() {\n try {\n await uploadBlob(\n this.logger,\n this.clientBlob,\n this.res,\n this.uploadData!,\n this.isComputableDone.bind(this),\n {\n nPartsWithThisUploadSpeed: this.nPartsWithThisUploadSpeed,\n nPartsToIncreaseUpload: this.nPartsToIncreaseUpload,\n currentSpeed: this.nMaxUploads,\n maxSpeed: this.maxNConcurrentPartsUpload,\n },\n );\n this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} finished`);\n } catch (e: any) {\n\n if (isResourceWasDeletedError(e)) {\n this.setRetriableError(e);\n this.logger.warn(`resource was deleted while uploading a blob: ${e}`);\n this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} aborted, resource was deleted`);\n this.setDone(true);\n\n return;\n }\n\n this.logger.error(`error while uploading a blob: ${e}`);\n this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} failed`);\n\n if (nonRecoverableError(e)) {\n this.setTerminalError(e);\n return;\n }\n\n this.setRetriableError(e);\n\n if (isHeadersTimeoutError(e)) {\n // we probably have a slow internet, we need to slow things a bit.\n this.nMaxUploads = decreaseConcurrency(this.logger, this.nMaxUploads, 1);\n }\n\n throw e;\n }\n }\n\n public async updateStatus() {\n try {\n // we do it with timeout in case we have slow internet.\n const status = await this.clientProgress.getStatus(this.res, { timeout: 10000 });\n\n const oldStatus = this.progress.status;\n const newStatus = doneProgressIfExisted(this.alreadyExisted, protoToStatus(status));\n this.progress.status = newStatus;\n this.setDone(status.done);\n\n if (status.done || this.progress.status.progress != oldStatus?.progress) {\n this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} changed`);\n }\n } catch (e: any) {\n this.setRetriableError(e);\n\n if ((e.name == 'RpcError' && e.code == 'DEADLINE_EXCEEDED') || e?.message?.includes('DEADLINE_EXCEEDED')) {\n this.logger.warn(`deadline exceeded while getting a status of BlobImport: ${e.message}`);\n return;\n }\n\n if (isResourceWasDeletedError(e)) {\n this.logger.warn(\n `resource was not found while updating a status of BlobImport: ${e}, ${stringifyWithResourceId(this.res)}`,\n );\n this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} changed, resource not found`);\n this.setDone(true);\n return;\n }\n\n this.logger.error(`retryable error while updating a status of BlobImport: ${e}`);\n // It was a terminal error, but when a connection drops,\n // this will stop the whole task, so we make it retryable.\n // It was like that:\n // this.change.markChanged();\n // this.setTerminalError(e);\n }\n }\n\n /** Set non-terminal error, that task can be retried. */\n private setRetriableError(e: unknown) {\n this.progress.lastError = String(e);\n }\n\n /** Set a terminal error, the task will throw a error instead of a progress. */\n private setTerminalError(e: unknown) {\n this.progress.lastError = String(e);\n this.progress.done = false;\n this.failed = true;\n }\n\n public setDoneIfOutputSet(res: ImportResourceSnapshot) {\n if (isImportResourceOutputSet(res)) {\n this.setDone(true);\n this.alreadyExisted = true;\n }\n }\n\n private setDone(done: boolean) {\n this.progress.done = done;\n if (done) this.progress.lastError = undefined;\n }\n\n public incCounter(w: Watcher, callerId: string) {\n this.change.attachWatcher(w);\n this.counter.inc(callerId);\n }\n\n public decCounter(callerId: string) {\n return this.counter.dec(callerId);\n }\n\n private isComputableDone() {\n return this.counter.isZero();\n }\n}\n\n/** Uploads a blob if it's not BlobIndex. */\nexport async function uploadBlob(\n logger: MiLogger,\n clientBlob: ClientUpload,\n res: ResourceInfo,\n uploadData: ImportFileHandleUploadData,\n isDoneFn: () => boolean,\n speed: {\n nPartsWithThisUploadSpeed: number,\n nPartsToIncreaseUpload: number,\n currentSpeed: number,\n maxSpeed: number,\n },\n) {\n assert(isUpload(res), 'the upload operation can be done only for BlobUploads');\n const timeout = 10000; // 10 sec instead of standard 5 sec, things might be slow with a slow connection.\n\n if (isDoneFn()) return;\n const parts = await clientBlob.initUpload(res, { timeout });\n logger.info(\n `started to upload blob ${res.id},`\n + ` parts overall: ${parts.overall}, parts remained: ${parts.toUpload.length},`\n + ` number of concurrent uploads: ${speed.currentSpeed}`,\n );\n\n const partUploadFn = (part: bigint) => async (controller: AsyncPoolController) => {\n if (isDoneFn()) return;\n await clientBlob.partUpload(\n res,\n uploadData.localPath,\n BigInt(uploadData.modificationTime),\n part,\n parts.checksumAlgorithm,\n parts.checksumHeader,\n { timeout }\n );\n logger.info(`uploaded chunk ${part}/${parts.overall} of resource: ${res.id}`);\n\n // if we had a network freeze, it will be increased slowly.\n speed.nPartsWithThisUploadSpeed++;\n if (speed.nPartsWithThisUploadSpeed >= speed.nPartsToIncreaseUpload) {\n speed.nPartsWithThisUploadSpeed = 0;\n speed.currentSpeed = increaseConcurrency(logger, speed.currentSpeed, speed.maxSpeed);\n controller.setConcurrency(speed.currentSpeed);\n }\n };\n\n await asyncPool(speed.currentSpeed, parts.toUpload.map(partUploadFn));\n\n if (isDoneFn()) return;\n await clientBlob.finalize(res, { timeout });\n\n logger.info(`uploading of resource ${res.id} finished.`);\n}\n\nfunction newProgress(res: ImportResourceSnapshot, signer: Signer) {\n let isUploadSignMatch: boolean | undefined;\n let uploadData: ImportFileHandleUploadData | undefined;\n if (isUpload(res)) {\n uploadData = ImportFileHandleUploadData.parse(res.data);\n isUploadSignMatch = isSignMatch(signer, uploadData.localPath, uploadData.pathSignature);\n }\n\n return {\n uploadData: uploadData,\n progress: {\n done: false,\n status: undefined,\n isUpload: isUpload(res),\n isUploadSignMatch: isUploadSignMatch,\n lastError: undefined,\n } satisfies sdk.ImportProgress,\n };\n}\n\n/** Returns true if we need to upload the blob that got from this progress. */\nexport function isMyUpload(p: sdk.ImportProgress): boolean {\n return p.isUpload && (p.isUploadSignMatch ?? false);\n}\n\n/** Creates a deep copy of progress,\n * since we do not want to pass a mutable object\n * to API, it led to bugs before.\n * We do not use '...' cloning syntax\n * for the compiler to fail here if we change API. */\nfunction cloneProgress(progress: sdk.ImportProgress): sdk.ImportProgress {\n const cloned: sdk.ImportProgress = {\n done: progress.done,\n isUpload: progress.isUpload,\n isUploadSignMatch: progress.isUploadSignMatch,\n lastError: progress.lastError,\n };\n\n if (progress.status)\n cloned.status = {\n progress: progress.status.progress,\n bytesProcessed: progress.status.bytesProcessed,\n bytesTotal: progress.status.bytesTotal,\n };\n\n return progress;\n}\n\nfunction isImportResourceOutputSet(res: ImportResourceSnapshot) {\n return 'blob' in res.fields\n ? res.fields.blob !== undefined\n : res.fields.incarnation !== undefined;\n}\n\nexport function isUpload(res: ResourceInfo) {\n return res.type.name.startsWith('BlobUpload');\n}\n\nexport function isSignMatch(signer: Signer, path: string, signature: string): boolean {\n try {\n signer.verify(path, signature);\n return true;\n } catch (e) {\n return false;\n }\n}\n\nfunction protoToStatus(proto: ProgressStatus): sdk.ImportStatus {\n return {\n progress: proto.progress ?? 0,\n bytesProcessed: Number(proto.bytesProcessed),\n bytesTotal: Number(proto.bytesTotal),\n };\n}\n\n/** Special hack: if we didn't even start to upload the blob\n * to backend because the result was already there,\n * the backend does show us a status that nothing were uploaded,\n * but we need to show the client that everything is OK.\n * Thus, here we set progress to be 1.0 and all bytes are become processed. */\nfunction doneProgressIfExisted(alreadyExisted: boolean, status: sdk.ImportStatus) {\n if (alreadyExisted && status.bytesTotal != 0 && status.bytesProcessed == 0) {\n return {\n progress: 1.0,\n bytesProcessed: Number(status.bytesTotal),\n bytesTotal: Number(status.bytesTotal),\n };\n }\n\n return status;\n}\n\nexport function isResourceWasDeletedError(e: any) {\n return (\n e.name == 'RpcError'\n && (e.code == 'NOT_FOUND' || e.code == 'ABORTED' || e.code == 'ALREADY_EXISTS')\n );\n}\n\nexport function nonRecoverableError(e: any) {\n return e instanceof MTimeError ||\n e instanceof UnexpectedEOF ||\n e instanceof NoFileForUploading ||\n e instanceof BadRequestError;\n}\n\nfunction isHeadersTimeoutError(e: any) {\n return (e as Error)?.message.includes(`UND_ERR_HEADERS_TIMEOUT`);\n}\n\n/** It's called for every upload success so if everyone is succeeded, we'll double the concurrency. */\nfunction increaseConcurrency(logger: MiLogger, current: number, max: number): number {\n const newConcurrency = Math.min(current + 2, max);\n if (newConcurrency != current)\n logger.info(`uploadTask.increaseConcurrency: increased from ${current} to ${newConcurrency}`)\n\n return newConcurrency;\n}\n\n/** When a error happens, this will half the concurrency level, so the next time\n * we'll try to upload blobs slower. */\nfunction decreaseConcurrency(logger: MiLogger, current: number, min: number): number {\n const newConcurrency = Math.max(Math.round(current / 2), min);\n if (newConcurrency != current)\n logger.info(`uploadTask.decreaseConcurrency: decreased from ${current} to ${newCon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AAU,mBAAmB,CAAC,CAAM,EAAA;IACxC,OAAO,CAAC,YAAY,UAAU;AAC5B,QAAA,CAAC,YAAY,aAAa;AAC1B,QAAA,CAAC,YAAY,kBAAkB;QAC/B,CAAC,YAAY,eAAe;AAChC;AAEA,SAAS,qBAAqB,CAAC,CAAM,EAAA;IACnC,OAAQ,CAAW,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA,uBAAA,CAAyB,CAAC;AAClE;AAEA;AACA,SAAS,mBAAmB,CAAC,MAAgB,EAAE,OAAe,EAAE,GAAW,EAAA;AACzE,IAAA,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,CAAC,EAAE,GAAG,CAAC;IACjD,IAAI,cAAc,IAAI,OAAO;QAC3B,MAAM,CAAC,IAAI,CAAC,CAAA,+CAAA,EAAkD,OAAO,CAAA,IAAA,EAAO,cAAc,CAAA,CAAE,CAAC;AAE/F,IAAA,OAAO,cAAc;AACvB;AAEA;AACuC;AACvC,SAAS,mBAAmB,CAAC,MAAgB,EAAE,OAAe,EAAE,GAAW,EAAA;AACzE,IAAA,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC;IAC7D,IAAI,cAAc,IAAI,OAAO;QAC3B,MAAM,CAAC,IAAI,CAAC,CAAA,+CAAA,EAAkD,OAAO,CAAA,IAAA,EAAO,cAAc,CAAA,CAAE,CAAC;AAE/F,IAAA,OAAO,cAAc;AACvB;;;;"}
1
+ {"version":3,"file":"upload_task.js","sources":["../../src/drivers/upload_task.ts"],"sourcesContent":["import type { Watcher } from '@milaboratories/computable';\nimport { ChangeSource } from '@milaboratories/computable';\nimport { isTimeoutError, resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';\nimport type * as sdk from '@milaboratories/pl-model-common';\nimport type { AsyncPoolController, MiLogger, Signer } from '@milaboratories/ts-helpers';\nimport { asyncPool, CallersCounter } from '@milaboratories/ts-helpers';\nimport type { ClientProgress, ProgressStatus } from '../clients/progress';\nimport type { ClientUpload } from '../clients/upload';\nimport { BadRequestError, MTimeError, NoFileForUploading, UnexpectedEOF } from '../clients/upload';\nimport type { ImportResourceSnapshot } from './types';\nimport { ImportFileHandleUploadData } from './types';\nimport assert from 'node:assert';\nimport { ResourceInfo } from '@milaboratories/pl-tree';\n\n/** Holds all info needed to upload a file and a status of uploading\n * and indexing. Also, has a method to update a status of the progress.\n * And holds a change source. */\nexport class UploadTask {\n private readonly change: ChangeSource = new ChangeSource();\n private readonly counter: CallersCounter = new CallersCounter();\n private nMaxUploads: number;\n private nPartsWithThisUploadSpeed = 0;\n private nPartsToIncreaseUpload = 10; // how many parts we have to wait to increase concurrency, 50 mb, 10 parts by 5 mb each.\n\n /** If this is upload progress this field will be defined */\n private uploadData?: ImportFileHandleUploadData;\n public progress: sdk.ImportProgress;\n\n /** If failed, then getting a progress is terminally failed. */\n public failed?: boolean;\n\n /** True if the blob was existed.\n * At this case, the task will show progress == 1.0. */\n private alreadyExisted = false;\n\n constructor(\n private readonly logger: MiLogger,\n private readonly clientBlob: ClientUpload,\n private readonly clientProgress: ClientProgress,\n private readonly maxNConcurrentPartsUpload: number,\n signer: Signer,\n public readonly res: ImportResourceSnapshot,\n ) {\n this.nMaxUploads = this.maxNConcurrentPartsUpload;\n const { uploadData, progress } = newProgress(res, signer);\n this.uploadData = uploadData;\n this.progress = progress;\n }\n\n public getProgress(w: Watcher, callerId: string) {\n this.incCounter(w, callerId);\n\n if (this.failed) {\n this.logger.error(`Uploading terminally failed: ${this.progress.lastError}`);\n throw new Error(this.progress.lastError);\n }\n\n return cloneProgress(this.progress);\n }\n\n public shouldScheduleUpload(): boolean {\n return isMyUpload(this.progress);\n }\n\n /** Uploads a blob if it's not BlobIndex. */\n public async uploadBlobTask() {\n try {\n await uploadBlob(\n this.logger,\n this.clientBlob,\n this.res,\n this.uploadData!,\n this.isComputableDone.bind(this),\n {\n nPartsWithThisUploadSpeed: this.nPartsWithThisUploadSpeed,\n nPartsToIncreaseUpload: this.nPartsToIncreaseUpload,\n currentSpeed: this.nMaxUploads,\n maxSpeed: this.maxNConcurrentPartsUpload,\n },\n );\n this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} finished`);\n } catch (e: any) {\n\n if (isTerminalUploadError(e)) {\n this.logger.warn(`terminal error while uploading a blob: ${e}`);\n this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} aborted: ${e.code}`);\n this.setDone(true);\n\n return;\n }\n\n this.logger.error(`error while uploading a blob: ${e}`);\n this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} failed`);\n\n if (nonRecoverableError(e)) {\n this.setTerminalError(e);\n return;\n }\n\n this.setRetriableError(e);\n\n if (isHeadersTimeoutError(e)) {\n // we probably have a slow internet, we need to slow things a bit.\n this.nMaxUploads = decreaseConcurrency(this.logger, this.nMaxUploads, 1);\n }\n\n throw e;\n }\n }\n\n public async updateStatus() {\n try {\n // we do it with timeout in case we have slow internet.\n const status = await this.clientProgress.getStatus(this.res, { timeout: 10000 });\n\n const oldStatus = this.progress.status;\n const newStatus = doneProgressIfExisted(this.alreadyExisted, protoToStatus(status));\n this.progress.status = newStatus;\n this.setDone(status.done);\n\n if (status.done || this.progress.status.progress != oldStatus?.progress) {\n this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} changed`);\n }\n } catch (e: any) {\n this.setRetriableError(e);\n\n if (isTimeoutError(e)) {\n this.logger.warn(`deadline exceeded while getting a status of BlobImport: ${e.message}`);\n return;\n }\n\n if (isTerminalUploadError(e)) {\n this.logger.warn(\n `terminal error while updating BlobImport status: ${e}, ${stringifyWithResourceId(this.res)}`,\n );\n this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} aborted: ${e.code}`);\n this.setDone(true);\n return;\n }\n\n this.logger.error(`retryable error while updating a status of BlobImport: ${e}`);\n // It was a terminal error, but when a connection drops,\n // this will stop the whole task, so we make it retryable.\n // It was like that:\n // this.change.markChanged();\n // this.setTerminalError(e);\n }\n }\n\n /** Set non-terminal error, that task can be retried. */\n private setRetriableError(e: unknown) {\n this.progress.lastError = String(e);\n }\n\n /** Set a terminal error, the task will throw a error instead of a progress. */\n private setTerminalError(e: unknown) {\n this.progress.lastError = String(e);\n this.progress.done = false;\n this.failed = true;\n }\n\n public setDoneIfOutputSet(res: ImportResourceSnapshot) {\n if (isImportResourceOutputSet(res)) {\n this.setDone(true);\n this.alreadyExisted = true;\n }\n }\n\n private setDone(done: boolean) {\n this.progress.done = done;\n if (done) this.progress.lastError = undefined;\n }\n\n public incCounter(w: Watcher, callerId: string) {\n this.change.attachWatcher(w);\n this.counter.inc(callerId);\n }\n\n public decCounter(callerId: string) {\n return this.counter.dec(callerId);\n }\n\n private isComputableDone() {\n return this.counter.isZero();\n }\n}\n\n/** Uploads a blob if it's not BlobIndex. */\nexport async function uploadBlob(\n logger: MiLogger,\n clientBlob: ClientUpload,\n res: ResourceInfo,\n uploadData: ImportFileHandleUploadData,\n isDoneFn: () => boolean,\n speed: {\n nPartsWithThisUploadSpeed: number,\n nPartsToIncreaseUpload: number,\n currentSpeed: number,\n maxSpeed: number,\n },\n) {\n assert(isUpload(res), 'the upload operation can be done only for BlobUploads');\n const timeout = 10000; // 10 sec instead of standard 5 sec, things might be slow with a slow connection.\n\n if (isDoneFn()) return;\n const parts = await clientBlob.initUpload(res, { timeout });\n logger.info(\n `started to upload blob ${res.id},`\n + ` parts overall: ${parts.overall}, parts remained: ${parts.toUpload.length},`\n + ` number of concurrent uploads: ${speed.currentSpeed}`,\n );\n\n const partUploadFn = (part: bigint) => async (controller: AsyncPoolController) => {\n if (isDoneFn()) return;\n await clientBlob.partUpload(\n res,\n uploadData.localPath,\n BigInt(uploadData.modificationTime),\n part,\n parts.checksumAlgorithm,\n parts.checksumHeader,\n { timeout }\n );\n logger.info(`uploaded chunk ${part}/${parts.overall} of resource: ${res.id}`);\n\n // if we had a network freeze, it will be increased slowly.\n speed.nPartsWithThisUploadSpeed++;\n if (speed.nPartsWithThisUploadSpeed >= speed.nPartsToIncreaseUpload) {\n speed.nPartsWithThisUploadSpeed = 0;\n speed.currentSpeed = increaseConcurrency(logger, speed.currentSpeed, speed.maxSpeed);\n controller.setConcurrency(speed.currentSpeed);\n }\n };\n\n await asyncPool(speed.currentSpeed, parts.toUpload.map(partUploadFn));\n\n if (isDoneFn()) return;\n await clientBlob.finalize(res, { timeout });\n\n logger.info(`uploading of resource ${res.id} finished.`);\n}\n\nfunction newProgress(res: ImportResourceSnapshot, signer: Signer) {\n let isUploadSignMatch: boolean | undefined;\n let uploadData: ImportFileHandleUploadData | undefined;\n if (isUpload(res)) {\n uploadData = ImportFileHandleUploadData.parse(res.data);\n isUploadSignMatch = isSignMatch(signer, uploadData.localPath, uploadData.pathSignature);\n }\n\n return {\n uploadData: uploadData,\n progress: {\n done: false,\n status: undefined,\n isUpload: isUpload(res),\n isUploadSignMatch: isUploadSignMatch,\n lastError: undefined,\n } satisfies sdk.ImportProgress,\n };\n}\n\n/** Returns true if we need to upload the blob that got from this progress. */\nexport function isMyUpload(p: sdk.ImportProgress): boolean {\n return p.isUpload && (p.isUploadSignMatch ?? false);\n}\n\n/** Creates a deep copy of progress,\n * since we do not want to pass a mutable object\n * to API, it led to bugs before.\n * We do not use '...' cloning syntax\n * for the compiler to fail here if we change API. */\nfunction cloneProgress(progress: sdk.ImportProgress): sdk.ImportProgress {\n const cloned: sdk.ImportProgress = {\n done: progress.done,\n isUpload: progress.isUpload,\n isUploadSignMatch: progress.isUploadSignMatch,\n lastError: progress.lastError,\n };\n\n if (progress.status)\n cloned.status = {\n progress: progress.status.progress,\n bytesProcessed: progress.status.bytesProcessed,\n bytesTotal: progress.status.bytesTotal,\n };\n\n return progress;\n}\n\nfunction isImportResourceOutputSet(res: ImportResourceSnapshot) {\n return 'blob' in res.fields\n ? res.fields.blob !== undefined\n : res.fields.incarnation !== undefined;\n}\n\nexport function isUpload(res: ResourceInfo) {\n return res.type.name.startsWith('BlobUpload');\n}\n\nexport function isSignMatch(signer: Signer, path: string, signature: string): boolean {\n try {\n signer.verify(path, signature);\n return true;\n } catch (e) {\n return false;\n }\n}\n\nfunction protoToStatus(proto: ProgressStatus): sdk.ImportStatus {\n return {\n progress: proto.progress ?? 0,\n bytesProcessed: Number(proto.bytesProcessed),\n bytesTotal: Number(proto.bytesTotal),\n };\n}\n\n/** Special hack: if we didn't even start to upload the blob\n * to backend because the result was already there,\n * the backend does show us a status that nothing were uploaded,\n * but we need to show the client that everything is OK.\n * Thus, here we set progress to be 1.0 and all bytes are become processed. */\nfunction doneProgressIfExisted(alreadyExisted: boolean, status: sdk.ImportStatus) {\n if (alreadyExisted && status.bytesTotal != 0 && status.bytesProcessed == 0) {\n return {\n progress: 1.0,\n bytesProcessed: Number(status.bytesTotal),\n bytesTotal: Number(status.bytesTotal),\n };\n }\n\n return status;\n}\n\nexport function isTerminalUploadError(e: any) {\n if (e?.name !== 'RpcError') return false;\n switch (e.code) {\n case 'NOT_FOUND':\n case 'ABORTED':\n case 'ALREADY_EXISTS':\n return true;\n default:\n return false;\n }\n}\n\nexport function nonRecoverableError(e: any) {\n return e instanceof MTimeError ||\n e instanceof UnexpectedEOF ||\n e instanceof NoFileForUploading ||\n e instanceof BadRequestError;\n}\n\nfunction isHeadersTimeoutError(e: any) {\n return (e as Error)?.message.includes(`UND_ERR_HEADERS_TIMEOUT`);\n}\n\n/** It's called for every upload success so if everyone is succeeded, we'll double the concurrency. */\nfunction increaseConcurrency(logger: MiLogger, current: number, max: number): number {\n const newConcurrency = Math.min(current + 2, max);\n if (newConcurrency != current)\n logger.info(`uploadTask.increaseConcurrency: increased from ${current} to ${newConcurrency}`)\n\n return newConcurrency;\n}\n\n/** When a error happens, this will half the concurrency level, so the next time\n * we'll try to upload blobs slower. */\nfunction decreaseConcurrency(logger: MiLogger, current: number, min: number): number {\n const newConcurrency = Math.max(Math.round(current / 2), min);\n if (newConcurrency != current)\n logger.info(`uploadTask.decrease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;;AAElB;AAEM,SAAU,mBAAmB,CAAC,CAAM,EAAA;IACxC,OAAO,CAAC,YAAY,UAAU;AAC5B,QAAA,CAAC,YAAY,aAAa;AAC1B,QAAA,CAAC,YAAY,kBAAkB;QAC/B,CAAC,YAAY,eAAe;AAChC;AAEA,SAAS,qBAAqB,CAAC,CAAM,EAAA;IACnC,OAAQ,CAAW,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA,uBAAA,CAAyB,CAAC;AAClE;AAEA;AACA,SAAS,mBAAmB,CAAC,MAAgB,EAAE,OAAe,EAAE,GAAW,EAAA;AACzE,IAAA,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,CAAC,EAAE,GAAG,CAAC;IACjD,IAAI,cAAc,IAAI,OAAO;QAC3B,MAAM,CAAC,IAAI,CAAC,CAAA,+CAAA,EAAkD,OAAO,CAAA,IAAA,EAAO,cAAc,CAAA,CAAE,CAAC;AAE/F,IAAA,OAAO,cAAc;AACvB;AAEA;AACuC;AACvC,SAAS,mBAAmB,CAAC,MAAgB,EAAE,OAAe,EAAE,GAAW,EAAA;AACzE,IAAA,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC;IAC7D,IAAI,cAAc,IAAI,OAAO;QAC3B,MAAM,CAAC,IAAI,CAAC,CAAA,+CAAA,EAAkD,OAAO,CAAA,IAAA,EAAO,cAAc,CAAA,CAAE,CAAC;AAE/F,IAAA,OAAO,cAAc;AACvB;;;;"}
package/dist/index.cjs CHANGED
@@ -51,8 +51,8 @@ exports.UploadDriver = upload$1.UploadDriver;
51
51
  exports.makeBlobImportSnapshot = upload$1.makeBlobImportSnapshot;
52
52
  exports.UploadTask = upload_task.UploadTask;
53
53
  exports.isMyUpload = upload_task.isMyUpload;
54
- exports.isResourceWasDeletedError = upload_task.isResourceWasDeletedError;
55
54
  exports.isSignMatch = upload_task.isSignMatch;
55
+ exports.isTerminalUploadError = upload_task.isTerminalUploadError;
56
56
  exports.isUpload = upload_task.isUpload;
57
57
  exports.nonRecoverableError = upload_task.nonRecoverableError;
58
58
  exports.uploadBlob = upload_task.uploadBlob;
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ export { DownloadDriver } from './drivers/download_blob/download_blob.js';
8
8
  export { DownloadBlobToURLDriver } from './drivers/download_blob_url/driver.js';
9
9
  export { DownloadableBlobSnapshot, makeDownloadableBlobSnapshot } from './drivers/download_blob_url/snapshot.js';
10
10
  export { UploadDriver, makeBlobImportSnapshot } from './drivers/upload.js';
11
- export { UploadTask, isMyUpload, isResourceWasDeletedError, isSignMatch, isUpload, nonRecoverableError, uploadBlob } from './drivers/upload_task.js';
11
+ export { UploadTask, isMyUpload, isSignMatch, isTerminalUploadError, isUpload, nonRecoverableError, uploadBlob } from './drivers/upload_task.js';
12
12
  export { LogsStreamDriver } from './drivers/logs_stream.js';
13
13
  export { LogsDriver } from './drivers/logs.js';
14
14
  export { DownloadUrlDriver } from './drivers/download_url/driver.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/pl-drivers",
3
- "version": "1.11.30",
3
+ "version": "1.11.32",
4
4
  "engines": {
5
5
  "node": ">=22"
6
6
  },
@@ -33,26 +33,26 @@
33
33
  "upath": "^2.0.1",
34
34
  "zod": "~3.23.8",
35
35
  "@milaboratories/ts-helpers": "1.5.4",
36
- "@milaboratories/computable": "2.7.5",
37
- "@milaboratories/pl-client": "2.16.14",
38
- "@milaboratories/pl-model-common": "1.21.10",
36
+ "@milaboratories/pl-tree": "1.8.23",
39
37
  "@milaboratories/helpers": "1.12.1",
40
- "@milaboratories/pl-tree": "1.8.22"
38
+ "@milaboratories/pl-model-common": "1.21.10",
39
+ "@milaboratories/computable": "2.7.5",
40
+ "@milaboratories/pl-client": "2.16.15"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/decompress": "^4.2.7",
44
44
  "openapi-typescript": "^7.10.0",
45
45
  "@types/node": "~24.5.2",
46
46
  "@types/tar-fs": "^2.0.4",
47
- "@vitest/coverage-v8": "^4.0.7",
47
+ "@vitest/coverage-istanbul": "^4.0.16",
48
48
  "eslint": "^9.25.1",
49
49
  "typescript": "~5.6.3",
50
- "vitest": "^4.0.7",
50
+ "vitest": "^4.0.16",
51
51
  "@milaboratories/eslint-config": "1.0.5",
52
- "@milaboratories/ts-configs": "1.2.0",
53
52
  "@milaboratories/build-configs": "1.2.1",
54
53
  "@milaboratories/ts-builder": "1.2.1",
55
- "@milaboratories/test-helpers": "1.1.2"
54
+ "@milaboratories/test-helpers": "1.1.2",
55
+ "@milaboratories/ts-configs": "1.2.0"
56
56
  },
57
57
  "scripts": {
58
58
  "type-check": "ts-builder types --target node",
@@ -66,7 +66,7 @@ export class ClientLogs {
66
66
  })).data!;
67
67
 
68
68
  return {
69
- data: new Uint8Array(Buffer.from(resp.data)),
69
+ data: Buffer.from(resp.data, 'base64'),
70
70
  size: BigInt(resp.size),
71
71
  newOffset: BigInt(resp.newOffset),
72
72
  };
@@ -109,7 +109,7 @@ export class ClientLogs {
109
109
  })).data!;
110
110
 
111
111
  return {
112
- data: new Uint8Array(Buffer.from(resp.data)),
112
+ data: Buffer.from(resp.data, 'base64'),
113
113
  size: BigInt(resp.size),
114
114
  newOffset: BigInt(resp.newOffset),
115
115
  };
@@ -8,7 +8,7 @@ import {
8
8
  Computable,
9
9
  } from '@milaboratories/computable';
10
10
  import type { ResourceId, ResourceType } from '@milaboratories/pl-client';
11
- import { resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';
11
+ import { isNotFoundError, resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';
12
12
  import type {
13
13
  AnyLogHandle,
14
14
  BlobDriver,
@@ -710,7 +710,7 @@ class LastLinesGetter {
710
710
  if (this.log != newLogs) this.change.markChanged(`logs for ${this.path} updated`);
711
711
  this.log = newLogs;
712
712
  } catch (e: any) {
713
- if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {
713
+ if (isNotFoundError(e)) {
714
714
  // No resource
715
715
  this.log = '';
716
716
  this.error = e;
@@ -6,6 +6,7 @@ import type * as sdk from '@milaboratories/pl-model-common';
6
6
  import type { MiLogger } from '@milaboratories/ts-helpers';
7
7
  import type { DownloadDriver } from './download_blob/download_blob';
8
8
  import { isLiveLogHandle } from './helpers/logs_handle';
9
+ import { isNotFoundError } from '@milaboratories/pl-client';
9
10
 
10
11
  export class LogsDriver implements sdk.LogsDriver {
11
12
  constructor(
@@ -36,7 +37,7 @@ export class LogsDriver implements sdk.LogsDriver {
36
37
  try {
37
38
  return this.logsStreamDriver.getLastLogs(stream, lines, ctx);
38
39
  } catch (e: any) {
39
- if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {
40
+ if (isNotFoundError(e)) {
40
41
  ctx.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${e}`);
41
42
  return undefined;
42
43
  }
@@ -67,7 +68,7 @@ export class LogsDriver implements sdk.LogsDriver {
67
68
  try {
68
69
  return this.logsStreamDriver.getProgressLog(stream, patternToSearch, ctx);
69
70
  } catch (e: any) {
70
- if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {
71
+ if (isNotFoundError(e)) {
71
72
  ctx.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${e}`);
72
73
  return undefined;
73
74
  }
@@ -110,7 +111,7 @@ export class LogsDriver implements sdk.LogsDriver {
110
111
  live: true,
111
112
  }
112
113
  } catch (e: any) {
113
- if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {
114
+ if (isNotFoundError(e)) {
114
115
  ctx.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log with info: ${e}`);
115
116
  return undefined;
116
117
  }
@@ -11,6 +11,7 @@ import type {
11
11
  ResourceId,
12
12
  ResourceType } from '@milaboratories/pl-client';
13
13
  import {
14
+ isNotFoundError,
14
15
  resourceIdToString,
15
16
  stringifyWithResourceId,
16
17
  } from '@milaboratories/pl-client';
@@ -240,7 +241,7 @@ export class LogsStreamDriver implements sdk.LogsDriver {
240
241
  newOffset: Number(resp.newOffset),
241
242
  };
242
243
  } catch (e: any) {
243
- if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {
244
+ if (isNotFoundError(e)) {
244
245
  return { shouldUpdateHandle: true };
245
246
  }
246
247
 
@@ -365,8 +366,7 @@ class LogGetter {
365
366
 
366
367
  return;
367
368
  } catch (e: any) {
368
- e as RpcError;
369
- if (e.name == 'RpcError' && e.code == 'NOT_FOUND') {
369
+ if (isNotFoundError(e)) {
370
370
  // No resource
371
371
  this.logs = '';
372
372
  this.error = e;
@@ -1,6 +1,6 @@
1
1
  import type { Watcher } from '@milaboratories/computable';
2
2
  import { ChangeSource } from '@milaboratories/computable';
3
- import { resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';
3
+ import { isTimeoutError, resourceIdToString, stringifyWithResourceId } from '@milaboratories/pl-client';
4
4
  import type * as sdk from '@milaboratories/pl-model-common';
5
5
  import type { AsyncPoolController, MiLogger, Signer } from '@milaboratories/ts-helpers';
6
6
  import { asyncPool, CallersCounter } from '@milaboratories/ts-helpers';
@@ -81,10 +81,9 @@ export class UploadTask {
81
81
  this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} finished`);
82
82
  } catch (e: any) {
83
83
 
84
- if (isResourceWasDeletedError(e)) {
85
- this.setRetriableError(e);
86
- this.logger.warn(`resource was deleted while uploading a blob: ${e}`);
87
- this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} aborted, resource was deleted`);
84
+ if (isTerminalUploadError(e)) {
85
+ this.logger.warn(`terminal error while uploading a blob: ${e}`);
86
+ this.change.markChanged(`blob upload for ${resourceIdToString(this.res.id)} aborted: ${e.code}`);
88
87
  this.setDone(true);
89
88
 
90
89
  return;
@@ -125,16 +124,16 @@ export class UploadTask {
125
124
  } catch (e: any) {
126
125
  this.setRetriableError(e);
127
126
 
128
- if ((e.name == 'RpcError' && e.code == 'DEADLINE_EXCEEDED') || e?.message?.includes('DEADLINE_EXCEEDED')) {
127
+ if (isTimeoutError(e)) {
129
128
  this.logger.warn(`deadline exceeded while getting a status of BlobImport: ${e.message}`);
130
129
  return;
131
130
  }
132
131
 
133
- if (isResourceWasDeletedError(e)) {
132
+ if (isTerminalUploadError(e)) {
134
133
  this.logger.warn(
135
- `resource was not found while updating a status of BlobImport: ${e}, ${stringifyWithResourceId(this.res)}`,
134
+ `terminal error while updating BlobImport status: ${e}, ${stringifyWithResourceId(this.res)}`,
136
135
  );
137
- this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} changed, resource not found`);
136
+ this.change.markChanged(`upload status for ${resourceIdToString(this.res.id)} aborted: ${e.code}`);
138
137
  this.setDone(true);
139
138
  return;
140
139
  }
@@ -333,11 +332,16 @@ function doneProgressIfExisted(alreadyExisted: boolean, status: sdk.ImportStatus
333
332
  return status;
334
333
  }
335
334
 
336
- export function isResourceWasDeletedError(e: any) {
337
- return (
338
- e.name == 'RpcError'
339
- && (e.code == 'NOT_FOUND' || e.code == 'ABORTED' || e.code == 'ALREADY_EXISTS')
340
- );
335
+ export function isTerminalUploadError(e: any) {
336
+ if (e?.name !== 'RpcError') return false;
337
+ switch (e.code) {
338
+ case 'NOT_FOUND':
339
+ case 'ABORTED':
340
+ case 'ALREADY_EXISTS':
341
+ return true;
342
+ default:
343
+ return false;
344
+ }
341
345
  }
342
346
 
343
347
  export function nonRecoverableError(e: any) {