From 0305db28c98fd6cf43a3c50ba92c76215e99d512 Mon Sep 17 00:00:00 2001 From: Jelle Besseling Date: Tue, 17 Aug 2021 08:26:20 +0200 Subject: Add support for saving video files to object storage (#4290) * Add support for saving video files to object storage * Add support for custom url generation on s3 stored files Uses two config keys to support url generation that doesn't directly go to (compatible s3). Can be used to generate urls to any cache server or CDN. * Upload files to s3 concurrently and delete originals afterwards * Only publish after move to object storage is complete * Use base url instead of url template * Fix mistyped config field * Add rudenmentary way to download before transcode * Implement Chocobozzz suggestions https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478 The remarks in question: Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET) I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs) https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs * Import correct function * Support multipart upload * Remove import of node 15.0 module stream/promises * Extend maximum upload job length Using the same value as for redundancy downloading seems logical * Use dynamic part size for really large uploads Also adds very small part size for local testing * Fix decreasePendingMove query * Resolve various PR comments * Move to object storage after optimize * Make upload size configurable and increase default * Prune webtorrent files that are stored in object storage * Move files after transcoding jobs * Fix federation * Add video path manager * Support move to external storage job in client * Fix live object storage tests Co-authored-by: Chocobozzz --- server/initializers/checker-after-init.ts | 23 +++++++++ server/initializers/config.ts | 20 ++++++++ server/initializers/constants.ts | 14 ++++-- server/initializers/database.ts | 4 +- .../migrations/0065-video-file-size.ts | 28 +---------- .../initializers/migrations/0660-object-storage.ts | 58 ++++++++++++++++++++++ 6 files changed, 114 insertions(+), 33 deletions(-) create mode 100644 server/initializers/migrations/0660-object-storage.ts (limited to 'server/initializers') diff --git a/server/initializers/checker-after-init.ts b/server/initializers/checker-after-init.ts index 911734fa0..09f587274 100644 --- a/server/initializers/checker-after-init.ts +++ b/server/initializers/checker-after-init.ts @@ -153,6 +153,29 @@ function checkConfig () { } } + // Object storage + if (CONFIG.OBJECT_STORAGE.ENABLED === true) { + + if (!CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME) { + return 'videos_bucket should be set when object storage support is enabled.' + } + + if (!CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME) { + return 'streaming_playlists_bucket should be set when object storage support is enabled.' + } + + if ( + CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME && + CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX + ) { + if (CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === '') { + return 'Object storage bucket prefixes should be set when the same bucket is used for both types of video.' + } else { + return 'Object storage bucket prefixes should be set to different values when the same bucket is used for both types of video.' + } + } + } + return null } diff --git a/server/initializers/config.ts b/server/initializers/config.ts index 30a9823b9..0e684eef8 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts @@ -73,6 +73,26 @@ const CONFIG = { PLUGINS_DIR: buildPath(config.get('storage.plugins')), CLIENT_OVERRIDES_DIR: buildPath(config.get('storage.client_overrides')) }, + OBJECT_STORAGE: { + ENABLED: config.get('object_storage.enabled'), + MAX_UPLOAD_PART: bytes.parse(config.get('object_storage.max_upload_part')), + ENDPOINT: config.get('object_storage.endpoint'), + REGION: config.get('object_storage.region'), + CREDENTIALS: { + ACCESS_KEY_ID: config.get('object_storage.credentials.access_key_id'), + SECRET_ACCESS_KEY: config.get('object_storage.credentials.secret_access_key') + }, + VIDEOS: { + BUCKET_NAME: config.get('object_storage.videos.bucket_name'), + PREFIX: config.get('object_storage.videos.prefix'), + BASE_URL: config.get('object_storage.videos.base_url') + }, + STREAMING_PLAYLISTS: { + BUCKET_NAME: config.get('object_storage.streaming_playlists.bucket_name'), + PREFIX: config.get('object_storage.streaming_playlists.prefix'), + BASE_URL: config.get('object_storage.streaming_playlists.base_url') + } + }, WEBSERVER: { SCHEME: config.get('webserver.https') === true ? 'https' : 'http', WS: config.get('webserver.https') === true ? 'wss' : 'ws', diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 5f121d9a4..8a1526ae8 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -24,7 +24,7 @@ import { CONFIG, registerConfigChangedHandler } from './config' // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 655 +const LAST_MIGRATION_VERSION = 660 // --------------------------------------------------------------------------- @@ -147,7 +147,8 @@ const JOB_ATTEMPTS: { [id in JobType]: number } = { 'videos-views': 1, 'activitypub-refresher': 1, 'video-redundancy': 1, - 'video-live-ending': 1 + 'video-live-ending': 1, + 'move-to-object-storage': 3 } // Excluded keys are jobs that can be configured by admins const JOB_CONCURRENCY: { [id in Exclude]: number } = { @@ -162,7 +163,8 @@ const JOB_CONCURRENCY: { [id in Exclude { - return utils.db.Video.listOwnedAndPopulateAuthorAndTags() - .then((videos: VideoModel[]) => { - const tasks: Promise[] = [] - - videos.forEach(video => { - video.VideoFiles.forEach(videoFile => { - const p = new Promise((res, rej) => { - stat(getVideoFilePath(video, videoFile), (err, stats) => { - if (err) return rej(err) - - videoFile.size = stats.size - videoFile.save().then(res).catch(rej) - }) - }) - - tasks.push(p) - }) - }) - - return tasks - }) - .then((tasks: Promise[]) => { - return Promise.all(tasks) - }) + throw new Error('Removed, please upgrade from a previous version first.') } function down (options) { diff --git a/server/initializers/migrations/0660-object-storage.ts b/server/initializers/migrations/0660-object-storage.ts new file mode 100644 index 000000000..c815c71c6 --- /dev/null +++ b/server/initializers/migrations/0660-object-storage.ts @@ -0,0 +1,58 @@ +import * as Sequelize from 'sequelize' +import { VideoStorage } from '@shared/models' + +async function up (utils: { + transaction: Sequelize.Transaction + queryInterface: Sequelize.QueryInterface + sequelize: Sequelize.Sequelize + db: any +}): Promise { + { + const query = ` + CREATE TABLE IF NOT EXISTS "videoJobInfo" ( + "id" serial, + "pendingMove" INTEGER NOT NULL, + "pendingTranscode" INTEGER NOT NULL, + "videoId" serial UNIQUE NOT NULL REFERENCES "video" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + "createdAt" timestamp WITH time zone NOT NULL, + "updatedAt" timestamp WITH time zone NOT NULL, + PRIMARY KEY ("id") + ); + ` + + await utils.sequelize.query(query) + } + + { + await utils.queryInterface.addColumn('videoFile', 'storage', { type: Sequelize.INTEGER, allowNull: true }) + } + { + await utils.sequelize.query( + `UPDATE "videoFile" SET "storage" = ${VideoStorage.FILE_SYSTEM}` + ) + } + { + await utils.queryInterface.changeColumn('videoFile', 'storage', { type: Sequelize.INTEGER, allowNull: false }) + } + + { + await utils.queryInterface.addColumn('videoStreamingPlaylist', 'storage', { type: Sequelize.INTEGER, allowNull: true }) + } + { + await utils.sequelize.query( + `UPDATE "videoStreamingPlaylist" SET "storage" = ${VideoStorage.FILE_SYSTEM}` + ) + } + { + await utils.queryInterface.changeColumn('videoStreamingPlaylist', 'storage', { type: Sequelize.INTEGER, allowNull: false }) + } +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} -- cgit v1.2.3