| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | /* |
| 20 | * MultipartUploadSlicingAlgorithm.java |
| 21 | * |
| 22 | * |
| 23 | * Created by: tibor |
| 24 | * |
| 25 | * History |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | package org.jclouds.aws.s3.blobstore.strategy.internal; |
| 30 | |
| 31 | import javax.annotation.Resource; |
| 32 | import javax.inject.Named; |
| 33 | |
| 34 | import org.jclouds.aws.s3.blobstore.strategy.MultipartUpload; |
| 35 | import org.jclouds.blobstore.reference.BlobStoreConstants; |
| 36 | import org.jclouds.logging.Logger; |
| 37 | |
| 38 | import com.google.common.annotations.VisibleForTesting; |
| 39 | import com.google.inject.Inject; |
| 40 | |
| 41 | public class MultipartUploadSlicingAlgorithm { |
| 42 | |
| 43 | @Resource |
| 44 | @Named(BlobStoreConstants.BLOBSTORE_LOGGER) |
| 45 | protected Logger logger = Logger.NULL; |
| 46 | |
| 47 | @VisibleForTesting |
| 48 | static final long DEFAULT_PART_SIZE = 33554432; // 32MB |
| 49 | |
| 50 | @VisibleForTesting |
| 51 | static final int DEFAULT_MAGNITUDE_BASE = 100; |
| 52 | |
| 53 | @Inject(optional = true) |
| 54 | @Named("jclouds.mpu.parts.size") |
| 55 | @VisibleForTesting |
| 56 | long defaultPartSize = DEFAULT_PART_SIZE; |
| 57 | |
| 58 | @Inject(optional = true) |
| 59 | @Named("jclouds.mpu.parts.magnitude") |
| 60 | @VisibleForTesting |
| 61 | int magnitudeBase = DEFAULT_MAGNITUDE_BASE; |
| 62 | |
| 63 | // calculated only once, but not from the constructor |
| 64 | private volatile int parts; // required number of parts with chunkSize |
| 65 | private volatile long chunkSize; |
| 66 | private volatile long remaining; // number of bytes remained for the last part |
| 67 | |
| 68 | // sequentially updated values |
| 69 | private volatile int part; |
| 70 | private volatile long chunkOffset; |
| 71 | private volatile long copied; |
| 72 | |
| 73 | @VisibleForTesting |
| 74 | protected long calculateChunkSize(long length) { |
| 75 | long unitPartSize = defaultPartSize; // first try with default part size |
| 76 | int parts = (int)(length / unitPartSize); |
| 77 | long partSize = unitPartSize; |
| 78 | int magnitude = (int) (parts / magnitudeBase); |
| 79 | if (magnitude > 0) { |
| 80 | partSize = magnitude * unitPartSize; |
| 81 | if (partSize > MultipartUpload.MAX_PART_SIZE) { |
| 82 | partSize = MultipartUpload.MAX_PART_SIZE; |
| 83 | unitPartSize = MultipartUpload.MAX_PART_SIZE; |
| 84 | } |
| 85 | parts = (int)(length / partSize); |
| 86 | if (parts * partSize < length) { |
| 87 | partSize = (magnitude + 1) * unitPartSize; |
| 88 | if (partSize > MultipartUpload.MAX_PART_SIZE) { |
| 89 | partSize = MultipartUpload.MAX_PART_SIZE; |
| 90 | unitPartSize = MultipartUpload.MAX_PART_SIZE; |
| 91 | } |
| 92 | parts = (int)(length / partSize); |
| 93 | } |
| 94 | } |
| 95 | if (parts > MultipartUpload.MAX_NUMBER_OF_PARTS) { // if splits in too many parts or |
| 96 | // cannot be split |
| 97 | unitPartSize = MultipartUpload.MIN_PART_SIZE; // take the minimum part size |
| 98 | parts = (int)(length / unitPartSize); |
| 99 | } |
| 100 | if (parts > MultipartUpload.MAX_NUMBER_OF_PARTS) { // if still splits in too many parts |
| 101 | parts = MultipartUpload.MAX_NUMBER_OF_PARTS - 1; // limit them. do not care about not |
| 102 | // covering |
| 103 | } |
| 104 | long remainder = length % unitPartSize; |
| 105 | if (remainder == 0 && parts > 0) { |
| 106 | parts -= 1; |
| 107 | } |
| 108 | this.chunkSize = partSize; |
| 109 | this.parts = parts; |
| 110 | this.remaining = length - partSize * parts; |
| 111 | logger.debug(" %d bytes partitioned in %d parts of part size: %d, remaining: %d%s", length, parts, chunkSize, |
| 112 | remaining, (remaining > MultipartUpload.MAX_PART_SIZE ? " overflow!" : "")); |
| 113 | return this.chunkSize; |
| 114 | } |
| 115 | |
| 116 | public long getCopied() { |
| 117 | return copied; |
| 118 | } |
| 119 | |
| 120 | public void setCopied(long copied) { |
| 121 | this.copied = copied; |
| 122 | } |
| 123 | |
| 124 | @VisibleForTesting |
| 125 | protected int getParts() { |
| 126 | return parts; |
| 127 | } |
| 128 | |
| 129 | protected int getNextPart() { |
| 130 | return ++part; |
| 131 | } |
| 132 | |
| 133 | protected void addCopied(long copied) { |
| 134 | this.copied += copied; |
| 135 | } |
| 136 | |
| 137 | protected long getNextChunkOffset() { |
| 138 | long next = chunkOffset; |
| 139 | chunkOffset += getChunkSize(); |
| 140 | return next; |
| 141 | } |
| 142 | |
| 143 | @VisibleForTesting |
| 144 | protected long getChunkSize() { |
| 145 | return chunkSize; |
| 146 | } |
| 147 | |
| 148 | @VisibleForTesting |
| 149 | protected long getRemaining() { |
| 150 | return remaining; |
| 151 | } |
| 152 | |
| 153 | } |