Fix broken object storage playlist on file removal

pull/6562/head
Chocobozzz 2024-08-19 16:00:40 +02:00
parent bd60f178af
commit b2bb45cf91
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 193 additions and 165 deletions

View File

@ -1,16 +1,18 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import { expect } from 'chai'
import { HttpStatusCode } from '@peertube/peertube-models' import { HttpStatusCode } from '@peertube/peertube-models'
import { areMockObjectStorageTestsDisabled } from '@peertube/peertube-node-utils'
import { import {
cleanupTests, cleanupTests,
createMultipleServers, createMultipleServers,
doubleFollow, doubleFollow,
makeRawRequest, makeRawRequest,
ObjectStorageCommand,
PeerTubeServer, PeerTubeServer,
setAccessTokensToServers, setAccessTokensToServers,
waitJobs waitJobs
} from '@peertube/peertube-server-commands' } from '@peertube/peertube-server-commands'
import { expect } from 'chai'
describe('Test videos files', function () { describe('Test videos files', function () {
let servers: PeerTubeServer[] let servers: PeerTubeServer[]
@ -28,6 +30,8 @@ describe('Test videos files', function () {
await servers[0].config.enableTranscoding({ hls: true, webVideo: true, resolutions: 'max' }) await servers[0].config.enableTranscoding({ hls: true, webVideo: true, resolutions: 'max' })
}) })
function runTests (objectStorage?: ObjectStorageCommand) {
describe('When deleting all files', function () { describe('When deleting all files', function () {
let validId1: string let validId1: string
let validId2: string let validId2: string
@ -154,10 +158,11 @@ describe('Test videos files', function () {
expect(video.streamingPlaylists[0].files).to.have.lengthOf(files.length - 1) expect(video.streamingPlaylists[0].files).to.have.lengthOf(files.length - 1)
expect(video.streamingPlaylists[0].files.find(f => f.resolution.id === toDelete.resolution.id)).to.not.exist expect(video.streamingPlaylists[0].files.find(f => f.resolution.id === toDelete.resolution.id)).to.not.exist
const { text } = await makeRawRequest({ url: video.streamingPlaylists[0].playlistUrl, expectedStatus: HttpStatusCode.OK_200 }) const m3u8Content = await servers[0].streamingPlaylists.get({ url: video.streamingPlaylists[0].playlistUrl })
await makeRawRequest({ url: video.streamingPlaylists[0].segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
expect(text.includes(`-${toDelete.resolution.id}.m3u8`)).to.be.false expect(m3u8Content.includes(`-${toDelete.resolution.id}.m3u8`)).to.be.false
expect(text.includes(`-${video.streamingPlaylists[0].files[0].resolution.id}.m3u8`)).to.be.true expect(m3u8Content.includes(`-${video.streamingPlaylists[0].files[0].resolution.id}.m3u8`)).to.be.true
} }
}) })
@ -196,6 +201,29 @@ describe('Test videos files', function () {
await servers[0].videos.removeHLSFile({ videoId: hlsOnly.id, fileId: hlsOnly.streamingPlaylists[0].files[4].id, expectedStatus }) await servers[0].videos.removeHLSFile({ videoId: hlsOnly.id, fileId: hlsOnly.streamingPlaylists[0].files[4].id, expectedStatus })
}) })
}) })
}
describe('Using filesystem', function () {
runTests()
})
describe('Using object storage', function () {
if (areMockObjectStorageTestsDisabled()) return
before(async function () {
this.timeout(120000)
const configOverride = objectStorage.getDefaultMockConfig()
await objectStorage.prepareDefaultMockBuckets()
await servers[0].kill()
await servers[0].run(configOverride)
})
const objectStorage = new ObjectStorageCommand()
runTests(objectStorage)
})
after(async function () { after(async function () {
await cleanupTests(servers) await cleanupTests(servers)

View File

@ -17,7 +17,7 @@ import { P2P_MEDIA_LOADER_PEER_VERSION, REQUEST_TIMEOUTS } from '../initializers
import { sequelizeTypescript } from '../initializers/database.js' import { sequelizeTypescript } from '../initializers/database.js'
import { VideoFileModel } from '../models/video/video-file.js' import { VideoFileModel } from '../models/video/video-file.js'
import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist.js' import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist.js'
import { storeHLSFileFromFilename } from './object-storage/index.js' import { storeHLSFileFromContent } from './object-storage/index.js'
import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHlsResolutionPlaylistFilename } from './paths.js' import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHlsResolutionPlaylistFilename } from './paths.js'
import { VideoPathManager } from './video-path-manager.js' import { VideoPathManager } from './video-path-manager.js'
@ -121,14 +121,17 @@ export function updateMasterHLSPlaylist (video: MVideo, playlistArg: MStreamingP
} }
playlist.playlistFilename = generateHLSMasterPlaylistFilename(video.isLive) playlist.playlistFilename = generateHLSMasterPlaylistFilename(video.isLive)
const masterPlaylistPath = VideoPathManager.Instance.getFSHLSOutputPath(video, playlist.playlistFilename) const masterPlaylistContent = masterPlaylists.join('\n') + '\n'
await writeFile(masterPlaylistPath, masterPlaylists.join('\n') + '\n')
logger.info('Updating %s master playlist file of video %s', masterPlaylistPath, video.uuid, lTags(video.uuid))
if (playlist.storage === FileStorage.OBJECT_STORAGE) { if (playlist.storage === FileStorage.OBJECT_STORAGE) {
playlist.playlistUrl = await storeHLSFileFromFilename(playlist, playlist.playlistFilename) playlist.playlistUrl = await storeHLSFileFromContent(playlist, playlist.playlistFilename, masterPlaylistContent)
await remove(masterPlaylistPath)
logger.info(`Updated master playlist file of video ${video.uuid} to object storage ${playlist.playlistUrl}`, lTags(video.uuid))
} else {
const masterPlaylistPath = VideoPathManager.Instance.getFSHLSOutputPath(video, playlist.playlistFilename)
await writeFile(masterPlaylistPath, masterPlaylistContent)
logger.info(`Updated master playlist file ${masterPlaylistPath} of video ${video.uuid}`, lTags(video.uuid))
} }
return playlist.save() return playlist.save()
@ -174,12 +177,11 @@ export function updateSha256VODSegments (video: MVideo, playlistArg: MStreamingP
} }
playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(video.isLive) playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(video.isLive)
if (playlist.storage === FileStorage.OBJECT_STORAGE) {
playlist.segmentsSha256Url = await storeHLSFileFromContent(playlist, playlist.segmentsSha256Filename, JSON.stringify(json))
} else {
const outputPath = VideoPathManager.Instance.getFSHLSOutputPath(video, playlist.segmentsSha256Filename) const outputPath = VideoPathManager.Instance.getFSHLSOutputPath(video, playlist.segmentsSha256Filename)
await outputJSON(outputPath, json) await outputJSON(outputPath, json)
if (playlist.storage === FileStorage.OBJECT_STORAGE) {
playlist.segmentsSha256Url = await storeHLSFileFromFilename(playlist, playlist.segmentsSha256Filename)
await remove(outputPath)
} }
return playlist.save() return playlist.save()

View File

@ -62,14 +62,13 @@ async function storeObject (options: {
async function storeContent (options: { async function storeContent (options: {
content: string content: string
inputPath: string
objectStorageKey: string objectStorageKey: string
bucketInfo: BucketInfo bucketInfo: BucketInfo
isPrivate: boolean isPrivate: boolean
}): Promise<string> { }): Promise<string> {
const { content, objectStorageKey, bucketInfo, inputPath, isPrivate } = options const { content, objectStorageKey, bucketInfo, isPrivate } = options
logger.debug('Uploading %s content to %s%s in bucket %s', inputPath, bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, lTags()) logger.debug('Uploading %s content to %s%s in bucket %s', content, bucketInfo.PREFIX, objectStorageKey, bucketInfo.BUCKET_NAME, lTags())
return uploadToStorage({ objectStorageKey, content, bucketInfo, isPrivate }) return uploadToStorage({ objectStorageKey, content, bucketInfo, isPrivate })
} }

View File

@ -49,11 +49,10 @@ export function storeHLSFileFromPath (playlist: MStreamingPlaylistVideo, path: s
}) })
} }
export function storeHLSFileFromContent (playlist: MStreamingPlaylistVideo, path: string, content: string) { export function storeHLSFileFromContent (playlist: MStreamingPlaylistVideo, pathOrFilename: string, content: string) {
return storeContent({ return storeContent({
content, content,
inputPath: path, objectStorageKey: generateHLSObjectStorageKey(playlist, basename(pathOrFilename)),
objectStorageKey: generateHLSObjectStorageKey(playlist, basename(path)),
bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS, bucketInfo: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS,
isPrivate: playlist.Video.hasPrivateStaticPath() isPrivate: playlist.Video.hasPrivateStaticPath()
}) })