|
29 | 29 | import java.util.Date; |
30 | 30 | import java.util.Map; |
31 | 31 | import java.util.UUID; |
| 32 | +import java.util.concurrent.BlockingQueue; |
| 33 | +import java.util.concurrent.CompletableFuture; |
| 34 | +import java.util.concurrent.LinkedBlockingQueue; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | +import java.util.concurrent.atomic.AtomicBoolean; |
32 | 37 | import okhttp3.FormBody; |
33 | 38 | import okhttp3.MediaType; |
34 | 39 | import okhttp3.MultipartBody; |
@@ -271,60 +276,192 @@ public Map<String, Object> createVideo(VideoModel video, UUID jobId) |
271 | 276 | private int uploadVideoChunks(VideoModel video, UUID jobId, InputStream inputStream) |
272 | 277 | throws CopyExceptionWithFailureReason, IOException { |
273 | 278 | final int CHUNK_SIZE = 50 * 1024 * 1024; // 50MB |
274 | | - int actualChunkCount = 0; |
| 279 | + // Use a pool of 2 buffers to limit memory usage to ~100MB regardless of video |
| 280 | + BlockingQueue<byte[]> bufferPool = new LinkedBlockingQueue<>(2); |
| 281 | + bufferPool.add(new byte[CHUNK_SIZE]); |
| 282 | + bufferPool.add(new byte[CHUNK_SIZE]); |
| 283 | + |
| 284 | + // Pass video chunks from producer to consumer using a blocking queue |
| 285 | + // the producer will put a VideoChunk with null data to indicate the end of stream or an error. |
| 286 | + BlockingQueue<VideoChunk> chunkQueue = new LinkedBlockingQueue<>(); |
| 287 | + AtomicBoolean consumerAborted = new AtomicBoolean(false); |
| 288 | + |
| 289 | + // Start a producer thread to read the input stream. |
| 290 | + CompletableFuture<Void> producerFuture = |
| 291 | + CompletableFuture.runAsync( |
| 292 | + () -> |
| 293 | + produceVideoChunks( |
| 294 | + video, |
| 295 | + jobId, |
| 296 | + inputStream, |
| 297 | + bufferPool, |
| 298 | + chunkQueue, |
| 299 | + consumerAborted, |
| 300 | + CHUNK_SIZE)); |
| 301 | + |
| 302 | + try { |
| 303 | + return consumeAndUploadVideoChunks(video, jobId, bufferPool, chunkQueue); |
| 304 | + } catch (InterruptedException e) { |
| 305 | + Thread.currentThread().interrupt(); |
| 306 | + throw new IOException("Video upload interrupted", e); |
| 307 | + } finally { |
| 308 | + consumerAborted.set(true); |
| 309 | + producerFuture.cancel(true); |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + private void produceVideoChunks( |
| 314 | + VideoModel video, |
| 315 | + UUID jobId, |
| 316 | + InputStream inputStream, |
| 317 | + BlockingQueue<byte[]> bufferPool, |
| 318 | + BlockingQueue<VideoChunk> chunkQueue, |
| 319 | + AtomicBoolean consumerAborted, |
| 320 | + int chunkSize) { |
| 321 | + try { |
| 322 | + int index = 0; |
| 323 | + while (!consumerAborted.get()) { |
| 324 | + byte[] chunkData = bufferPool.poll(500, TimeUnit.MILLISECONDS); |
| 325 | + if (chunkData == null) { |
| 326 | + continue; |
| 327 | + } |
| 328 | + |
| 329 | + int bytesRead = ByteStreams.read(inputStream, chunkData, 0, chunkSize); |
| 330 | + if (bytesRead == 0) { |
| 331 | + bufferPool.add(chunkData); // Return unused buffer |
| 332 | + chunkQueue.add(new VideoChunk(null, 0, -1)); |
| 333 | + break; |
| 334 | + } |
275 | 335 |
|
276 | | - // reuse the same byte array for each chunk to reduce memory usage, |
277 | | - // since we are uploading in sequential, there is no concurrency issue |
278 | | - byte[] chunkData = new byte[CHUNK_SIZE]; |
| 336 | + final int fIndex = index; |
| 337 | + monitor.info( |
| 338 | + () -> |
| 339 | + String.format( |
| 340 | + "[SynologyImporter] fetched video chunk, video name: [%s], chunk index: [%d]," |
| 341 | + + " chunk size: [%d].", |
| 342 | + video.getName(), fIndex, bytesRead), |
| 343 | + jobId); |
| 344 | + |
| 345 | + chunkQueue.add(new VideoChunk(chunkData, bytesRead, index++)); |
| 346 | + |
| 347 | + monitor.info( |
| 348 | + () -> |
| 349 | + String.format( |
| 350 | + "[SynologyImporter] video chunk put into queue, video name: [%s], chunk index:" |
| 351 | + + " [%d], chunk size: [%d].", |
| 352 | + video.getName(), fIndex, bytesRead), |
| 353 | + jobId); |
| 354 | + } |
| 355 | + } catch (Exception e) { |
| 356 | + chunkQueue.add(new VideoChunk(e)); |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + private int consumeAndUploadVideoChunks( |
| 361 | + VideoModel video, |
| 362 | + UUID jobId, |
| 363 | + BlockingQueue<byte[]> bufferPool, |
| 364 | + BlockingQueue<VideoChunk> chunkQueue) |
| 365 | + throws CopyExceptionWithFailureReason, IOException, InterruptedException { |
| 366 | + int actualChunkCount = 0; |
279 | 367 | while (true) { |
280 | | - int bytesRead = ByteStreams.read(inputStream, chunkData, 0, CHUNK_SIZE); |
281 | | - if (bytesRead == 0) { |
282 | | - break; |
| 368 | + VideoChunk chunk = chunkQueue.take(); |
| 369 | + if (chunk.error != null) { |
| 370 | + throw new IOException("Error reading video stream", chunk.error); |
| 371 | + } |
| 372 | + if (chunk.data == null) { |
| 373 | + final int finalActualChunkCount = actualChunkCount; |
| 374 | + monitor.info( |
| 375 | + () -> |
| 376 | + String.format( |
| 377 | + "[SynologyImporter] finished reading video stream, total chunks: [%d].", |
| 378 | + finalActualChunkCount), |
| 379 | + jobId); |
| 380 | + return actualChunkCount; |
283 | 381 | } |
284 | 382 |
|
285 | | - RequestBody fileBody = |
286 | | - new RequestBody() { |
287 | | - @Override |
288 | | - public MediaType contentType() { |
289 | | - return MediaType.parse(video.getMimeType()); |
290 | | - } |
| 383 | + monitor.info( |
| 384 | + () -> |
| 385 | + String.format( |
| 386 | + "[SynologyImporter] get video chunk, video name: [%s], chunk index: [%d], chunk" |
| 387 | + + " size: [%d].", |
| 388 | + video.getName(), chunk.index, chunk.size), |
| 389 | + jobId); |
291 | 390 |
|
292 | | - @Override |
293 | | - public long contentLength() { |
294 | | - return bytesRead; |
295 | | - } |
| 391 | + try { |
| 392 | + uploadVideoChunk(video, jobId, chunk); |
| 393 | + } finally { |
| 394 | + bufferPool.add(chunk.data); |
| 395 | + } |
| 396 | + actualChunkCount++; |
| 397 | + } |
| 398 | + } |
296 | 399 |
|
297 | | - @Override |
298 | | - public void writeTo(BufferedSink sink) throws IOException { |
299 | | - // write the actual bytes read for the last chunk, which may be smaller than |
300 | | - // CHUNK_SIZE |
301 | | - sink.write(chunkData, 0, bytesRead); |
302 | | - } |
303 | | - }; |
| 400 | + private void uploadVideoChunk(VideoModel video, UUID jobId, VideoChunk chunk) |
| 401 | + throws CopyExceptionWithFailureReason, IOException { |
| 402 | + final int bytesRead = chunk.size; |
| 403 | + final byte[] data = chunk.data; |
| 404 | + |
| 405 | + RequestBodyGenerator bodyGenerator = |
| 406 | + () -> { |
| 407 | + RequestBody fileBody = |
| 408 | + new RequestBody() { |
| 409 | + @Override |
| 410 | + public MediaType contentType() { |
| 411 | + return MediaType.parse(video.getMimeType()); |
| 412 | + } |
304 | 413 |
|
305 | | - MultipartBody.Builder builder = |
306 | | - new MultipartBody.Builder() |
| 414 | + @Override |
| 415 | + public long contentLength() { |
| 416 | + return bytesRead; |
| 417 | + } |
| 418 | + |
| 419 | + @Override |
| 420 | + public void writeTo(BufferedSink sink) throws IOException { |
| 421 | + sink.write(data, 0, bytesRead); |
| 422 | + } |
| 423 | + }; |
| 424 | + |
| 425 | + return new MultipartBody.Builder() |
307 | 426 | .setType(MultipartBody.FORM) |
308 | 427 | .addFormDataPart("item_id", video.getDataId()) |
309 | | - .addFormDataPart("index", String.valueOf(actualChunkCount)) |
| 428 | + .addFormDataPart("index", String.valueOf(chunk.index)) |
310 | 429 | .addFormDataPart("job_id", jobId.toString()) |
311 | 430 | .addFormDataPart("service", exportingService) |
312 | 431 | .addFormDataPart("file_size", String.valueOf(bytesRead)) |
313 | | - .addFormDataPart("file", video.getName(), fileBody); |
| 432 | + .addFormDataPart("file", video.getName(), fileBody) |
| 433 | + .build(); |
| 434 | + }; |
314 | 435 |
|
315 | | - RequestBody requestBody = builder.build(); |
| 436 | + monitor.info( |
| 437 | + () -> |
| 438 | + String.format( |
| 439 | + "[SynologyImporter] uploading video chunk, video name: [%s], chunk index: [%d]," |
| 440 | + + " chunk size: [%d].", |
| 441 | + video.getName(), chunk.index, bytesRead), |
| 442 | + jobId); |
| 443 | + sendPostRequest(c2Api.getChunkUploadItem(), bodyGenerator, jobId); |
| 444 | + } |
316 | 445 |
|
317 | | - final String chunkInfo = |
318 | | - String.format( |
319 | | - "[SynologyImporter] uploading video chunk, video name: [%s], chunk index: [%d], chunk" |
320 | | - + " size: [%d].", |
321 | | - video.getName(), actualChunkCount, bytesRead); |
322 | | - monitor.info(() -> chunkInfo, jobId); |
323 | | - sendPostRequest(c2Api.getChunkUploadItem(), () -> requestBody, jobId); |
| 446 | + private static class VideoChunk { |
| 447 | + final byte[] data; |
| 448 | + final int size; |
| 449 | + final int index; |
| 450 | + final Throwable error; |
| 451 | + |
| 452 | + VideoChunk(byte[] data, int size, int index) { |
| 453 | + this.data = data; |
| 454 | + this.size = size; |
| 455 | + this.index = index; |
| 456 | + this.error = null; |
| 457 | + } |
324 | 458 |
|
325 | | - actualChunkCount++; |
| 459 | + VideoChunk(Throwable error) { |
| 460 | + this.data = null; |
| 461 | + this.size = 0; |
| 462 | + this.index = -1; |
| 463 | + this.error = error; |
326 | 464 | } |
327 | | - return actualChunkCount; |
328 | 465 | } |
329 | 466 |
|
330 | 467 | private Map<String, Object> completeVideoUpload(VideoModel video, UUID jobId, int totalChunks) |
|
0 commit comments