/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aliyun.fs.oss.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Utils {
    private static int idx = Math.abs(new Random(System.currentTimeMillis()).nextInt());
    public static synchronized File getTempBufferDir(Configuration conf) {
        boolean confirmExists = conf.getBoolean("fs.oss.buffer.dirs.exists", false);
        String[] bufferDirs = conf.get("fs.oss.buffer.dirs", "file:///tmp/").split(",");
        List<String> bufferPaths = new ArrayList<String>();
        for(int i = 0; i < bufferDirs.length; i++) {
            URI uri = new Path(bufferDirs[i]).toUri();
            String path = uri.getPath();
            Boolean fileExists = new File(path).exists();
            if (confirmExists && !fileExists) {
                continue;
            }
            bufferPaths.add(path);
        }
        if (bufferPaths.size() == 0) {
            bufferPaths.add("/tmp/");
        }
        idx = (idx + 1) % bufferPaths.size();
        return new File(bufferPaths.get(Math.abs(idx)), "oss");
    }

    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.set("dfs.datanode.data.dir", "file:///mnt/disk1,file:///mnt/disk4");
        System.out.println(Utils.getTempBufferDir(conf));
    }
}

@@ -856,162 +856,154 @@
 che.
-hadoop.conf.Configuration;%0Aimport org.apache.hadoop.fs.Path;%0A%0Aimport java.io.File;%0Aimport java.net.URI;%0Aimport java.util.ArrayList;%0Aimport java.util.List;
+commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0Aimport org.apache.hadoop.conf.Configuration;%0Aimport org.apache.hadoop.fs.Path;%0A
 %0Aimp
@@ -1015,19 +1015,15 @@
 ava.
-util.Random
+io.File
 ;%0A%0Ap
@@ -1065,75 +1065,47 @@
 tic 
-int idx = Math.abs(new Random(System.currentTimeMillis()).nextInt()
+Log LOG = LogFactory.getLog(Utils.class
 );%0A 
@@ -1182,93 +1182,8 @@
 ) %7B%0A
-        boolean confirmExists = conf.getBoolean(%22fs.oss.buffer.dirs.exists%22, false);%0A
     
@@ -1195,22 +1195,20 @@
 tring%5B%5D 
-buffer
+data
 Dirs = c
@@ -1220,26 +1220,29 @@
 et(%22
+d
 fs.
-oss.buffer
+datanode.data
 .dir
-s
 %22, %22
@@ -1280,58 +1280,61 @@
     
-List%3CString%3E bufferPaths = new ArrayList%3CString%3E()
+double minUsage = Double.MAX_VALUE;%0A        int n = 0
 ;%0A  
@@ -1352,24 +1352,18 @@
 nt i
- = 
+=
 0; i
- %3C buffer
+%3Cdata
 Dirs
@@ -1394,18 +1394,29 @@
     
-URI uri = 
+File file = new File(
 new 
@@ -1420,22 +1420,20 @@
 ew Path(
-buffer
+data
 Dirs%5Bi%5D)
@@ -1444,39 +1444,8 @@
 ri()
-;%0A            String path = uri
 .get
@@ -1450,16 +1450,17 @@
 etPath()
+)
 ;%0A      
@@ -1469,98 +1469,138 @@
     
-Boolean fileExists = new File(path).exists();%0A            if (confirmExists && !fileExists
+double diskUsage = 1.0 * (file.getTotalSpace() - file.getFreeSpace()) / file.getTotalSpace();%0A            if (diskUsage %3C minUsage
 ) %7B%0A
@@ -1619,16 +1619,13 @@
     
-continue
+n = i
 ;%0A  
@@ -1638,44 +1638,47 @@
     
-%7D%0A            bufferPaths.add(path);
+    minUsage = diskUsage;%0A            %7D
 %0A   
@@ -1686,200 +1686,228 @@
     
-%7D%0A
     
-    if (bufferPaths.size() == 0) %7B%0A            bufferPaths.add(%22/tmp/%22);%0A        %7D%0A        idx = (idx + 1) %25 bufferPaths.size();%0A        return new File(bufferPaths.get(Math.abs(idx)), %22
+i++;%0A        %7D%0A%0A        String diskPath = new Path(dataDirs%5Bn%5D).toUri().getPath();%0A        LOG.debug(%22choose oss buffer dir: %22+diskPath+%22, and this disk usage is: %22+minUsage);%0A        return new File(diskPath, %22data/
 oss%22

fix disk usage imbalance