|
@@ -24,6 +24,7 @@ import java.net.URI;
|
|
|
|
|
|
import com.aliyun.oss.common.auth.CredentialsProvider;
|
|
import com.aliyun.oss.common.auth.CredentialsProvider;
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.base.Preconditions;
|
|
|
|
+import org.apache.hadoop.fs.Path;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.hadoop.conf.Configuration;
|
|
import org.apache.hadoop.conf.Configuration;
|
|
import org.apache.hadoop.fs.LocalDirAllocator;
|
|
import org.apache.hadoop.fs.LocalDirAllocator;
|
|
@@ -39,7 +40,7 @@ import static org.apache.hadoop.fs.aliyun.oss.Constants.*;
|
|
final public class AliyunOSSUtils {
|
|
final public class AliyunOSSUtils {
|
|
private static final Logger LOG =
|
|
private static final Logger LOG =
|
|
LoggerFactory.getLogger(AliyunOSSUtils.class);
|
|
LoggerFactory.getLogger(AliyunOSSUtils.class);
|
|
- private static LocalDirAllocator directoryAllocator;
|
|
|
|
|
|
+ private static volatile LocalDirAllocator directoryAllocator;
|
|
|
|
|
|
private AliyunOSSUtils() {
|
|
private AliyunOSSUtils() {
|
|
}
|
|
}
|
|
@@ -171,21 +172,33 @@ final public class AliyunOSSUtils {
|
|
|
|
|
|
/**
|
|
/**
|
|
* Demand create the directory allocator, then create a temporary file.
|
|
* Demand create the directory allocator, then create a temporary file.
|
|
- * @param path prefix for the temporary file
|
|
|
|
- * @param size the size of the file that is going to be written
|
|
|
|
- * @param conf the Configuration object
|
|
|
|
- * @return a unique temporary file
|
|
|
|
- * @throws IOException IO problems
|
|
|
|
|
|
+ * This does not mark the file for deletion when a process exits.
|
|
|
|
+ * {@link LocalDirAllocator#createTmpFileForWrite(
|
|
|
|
+ * String, long, Configuration)}.
|
|
|
|
+ * @param pathStr prefix for the temporary file
|
|
|
|
+ * @param size the size of the file that is going to be written
|
|
|
|
+ * @param conf the Configuration object
|
|
|
|
+ * @return a unique temporary file
|
|
|
|
+ * @throws IOException IO problems
|
|
*/
|
|
*/
|
|
- public static File createTmpFileForWrite(String path, long size,
|
|
|
|
|
|
+ public static File createTmpFileForWrite(String pathStr, long size,
|
|
Configuration conf) throws IOException {
|
|
Configuration conf) throws IOException {
|
|
if (conf.get(BUFFER_DIR_KEY) == null) {
|
|
if (conf.get(BUFFER_DIR_KEY) == null) {
|
|
conf.set(BUFFER_DIR_KEY, conf.get("hadoop.tmp.dir") + "/oss");
|
|
conf.set(BUFFER_DIR_KEY, conf.get("hadoop.tmp.dir") + "/oss");
|
|
}
|
|
}
|
|
if (directoryAllocator == null) {
|
|
if (directoryAllocator == null) {
|
|
- directoryAllocator = new LocalDirAllocator(BUFFER_DIR_KEY);
|
|
|
|
|
|
+ synchronized (AliyunOSSUtils.class) {
|
|
|
|
+ if (directoryAllocator == null) {
|
|
|
|
+ directoryAllocator = new LocalDirAllocator(BUFFER_DIR_KEY);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- return directoryAllocator.createTmpFileForWrite(path, size, conf);
|
|
|
|
|
|
+ Path path = directoryAllocator.getLocalPathForWrite(pathStr,
|
|
|
|
+ size, conf);
|
|
|
|
+ File dir = new File(path.getParent().toUri().getPath());
|
|
|
|
+ String prefix = path.getName();
|
|
|
|
+ // create a temp file on this directory
|
|
|
|
+ return File.createTempFile(prefix, null, dir);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|