(core) Add AzureExternalStorage

Summary:
Adds a new implementation of the interface ExternalStorage that works for Azure Blob Storage as an alternative to S3, for a specific self-hosting case.

Tweaks HostedStorageManager and ICreate to allow configuring different core implementations of ExternalStorage.

Followup tasks:

- Make this code available to self hosters, possibly by making it open source.
- Add an env var or other config option to specify the preferred type of storage. Currently using the var `AZURE_STORAGE_CONNECTION_STRING` to know how to connect to Azure when requested, but that choice still only lives in test code.

Test Plan: Generalized HostedStorageManager and ExternalStorage tests to test the new AzureExternalStorage alongside S3ExternalStorage. The HostedStorageManager tests also now test the 'cached' in-memory test storage in a way that's closer to the real storage methods.

Reviewers: paulfitz

Reviewed By: paulfitz

Differential Revision: https://phab.getgrist.com/D3413
This commit is contained in:
Alex Hall
2022-05-09 21:05:19 +02:00
parent 52eb5325c2
commit 4408315f2e
4 changed files with 74 additions and 7 deletions

View File

@@ -45,12 +45,15 @@ function checkValidDocId(docId: string): void {
}
}
interface HostedStorageOptions {
export interface HostedStorageOptions {
secondsBeforePush: number;
secondsBeforeFirstRetry: number;
pushDocUpdateTimes: boolean;
testExternalStorageDoc?: ExternalStorage;
testExternalStorageMeta?: ExternalStorage;
// A function returning the core ExternalStorage implementation,
// which may then be wrapped in additional layer(s) of ExternalStorage.
// See ICreate.ExternalStorage.
// Uses S3 by default in hosted Grist.
innerExternalStorageCreate?: (bucket: string) => ExternalStorage;
}
const defaultOptions: HostedStorageOptions = {
@@ -132,8 +135,8 @@ export class HostedStorageManager implements IDocStorageManager {
) {
// We store documents either in a test store, or in an s3 store
// at s3://<s3Bucket>/<s3Prefix><docId>.grist
const externalStoreDoc = options.testExternalStorageDoc ||
(this._disableS3 ? undefined : create.ExternalStorage('doc', extraS3Prefix));
const externalStoreDoc = this._disableS3 ? undefined :
create.ExternalStorage('doc', extraS3Prefix, options.innerExternalStorageCreate);
if (!externalStoreDoc) { this._disableS3 = true; }
const secondsBeforePush = options.secondsBeforePush;
if (options.pushDocUpdateTimes) {
@@ -154,7 +157,7 @@ export class HostedStorageManager implements IDocStorageManager {
this._ext = this._getChecksummedExternalStorage('doc', this._baseStore,
this._latestVersions, options);
const baseStoreMeta = options.testExternalStorageMeta || create.ExternalStorage('meta', extraS3Prefix);
const baseStoreMeta = create.ExternalStorage('meta', extraS3Prefix, options.innerExternalStorageCreate);
if (!baseStoreMeta) {
throw new Error('bug: external storage should be created for "meta" if it is created for "doc"');
}

View File

@@ -20,7 +20,14 @@ export interface ICreate {
// - meta. This store need not be versioned, and can be eventually consistent.
// For test purposes an extra prefix may be supplied. Stores with different prefixes
// should not interfere with each other.
ExternalStorage(purpose: 'doc' | 'meta', testExtraPrefix: string): ExternalStorage|undefined;
// innerCreate should be a function returning the core ExternalStorage implementation,
// which this method may wrap in additional layer(s) of ExternalStorage.
// Uses S3 by default in hosted Grist.
ExternalStorage(
purpose: 'doc' | 'meta',
testExtraPrefix: string,
innerCreate?: (bucket: string) => ExternalStorage
): ExternalStorage | undefined;
ActiveDoc(docManager: DocManager, docName: string, options: ICreateActiveDocOptions): ActiveDoc;
NSandbox(options: ISandboxCreationOptions): ISandbox;