Search in sources :

Example 11 with Path

use of java.nio.file.Path in project storm by apache.

the class DirectoryCleaner method deleteOldestWhileTooLarge.

/**
     * If totalSize of files exceeds the either the per-worker quota or global quota,
     * Logviewer deletes oldest inactive log files in a worker directory or in all worker dirs.
     * We use the parameter for_per_dir to switch between the two deletion modes.
     * @param dirs the list of directories to be scanned for deletion
     * @param quota the per-dir quota or the total quota for the all directories
     * @param for_per_dir if true, deletion happens for a single dir; otherwise, for all directories globally
     * @param active_dirs only for global deletion, we want to skip the active logs in active_dirs
     * @return number of files deleted
     */
public int deleteOldestWhileTooLarge(List<File> dirs, long quota, boolean for_per_dir, Set<String> active_dirs) throws IOException {
    // max number of files to delete for every round
    final int PQ_SIZE = 1024;
    // max rounds of scanning the dirs
    final int MAX_ROUNDS = 512;
    long totalSize = 0;
    int deletedFiles = 0;
    for (File dir : dirs) {
        try (DirectoryStream<Path> stream = getStreamForDirectory(dir)) {
            for (Path path : stream) {
                File file = path.toFile();
                totalSize += file.length();
            }
        }
    }
    long toDeleteSize = totalSize - quota;
    if (toDeleteSize <= 0) {
        return deletedFiles;
    }
    Comparator<File> comparator = new Comparator<File>() {

        public int compare(File f1, File f2) {
            if (f1.lastModified() > f2.lastModified()) {
                return -1;
            } else {
                return 1;
            }
        }
    };
    // the oldest pq_size files in this directory will be placed in PQ, with the newest at the root
    PriorityQueue<File> pq = new PriorityQueue<File>(PQ_SIZE, comparator);
    int round = 0;
    while (toDeleteSize > 0) {
        LOG.debug("To delete size is {}, start a new round of deletion, round: {}", toDeleteSize, round);
        for (File dir : dirs) {
            try (DirectoryStream<Path> stream = getStreamForDirectory(dir)) {
                for (Path path : stream) {
                    File file = path.toFile();
                    if (for_per_dir) {
                        if (ACTIVE_LOG_PATTERN.matcher(file.getName()).matches()) {
                            // skip active log files
                            continue;
                        }
                    } else {
                        // for global cleanup
                        if (active_dirs.contains(dir.getCanonicalPath())) {
                            // for an active worker's dir, make sure for the last "/"
                            if (ACTIVE_LOG_PATTERN.matcher(file.getName()).matches()) {
                                // skip active log files
                                continue;
                            }
                        } else {
                            if (META_LOG_PATTERN.matcher(file.getName()).matches()) {
                                // skip yaml and pid files
                                continue;
                            }
                        }
                    }
                    if (pq.size() < PQ_SIZE) {
                        pq.offer(file);
                    } else {
                        if (file.lastModified() < pq.peek().lastModified()) {
                            pq.poll();
                            pq.offer(file);
                        }
                    }
                }
            }
        }
        // need to reverse the order of elements in PQ to delete files from oldest to newest
        Stack<File> stack = new Stack<File>();
        while (!pq.isEmpty()) {
            File file = pq.poll();
            stack.push(file);
        }
        while (!stack.isEmpty() && toDeleteSize > 0) {
            File file = stack.pop();
            toDeleteSize -= file.length();
            LOG.info("Delete file: {}, size: {}, lastModified: {}", file.getCanonicalPath(), file.length(), file.lastModified());
            file.delete();
            deletedFiles++;
        }
        pq.clear();
        round++;
        if (round >= MAX_ROUNDS) {
            if (for_per_dir) {
                LOG.warn("Reach the MAX_ROUNDS: {} during per-dir deletion, you may have too many files in " + "a single directory : {}, will delete the rest files in next interval.", MAX_ROUNDS, dirs.get(0).getCanonicalPath());
            } else {
                LOG.warn("Reach the MAX_ROUNDS: {} during global deletion, you may have too many files, " + "will delete the rest files in next interval.", MAX_ROUNDS);
            }
            break;
        }
    }
    return deletedFiles;
}
Also used : Path(java.nio.file.Path) PriorityQueue(java.util.PriorityQueue) File(java.io.File) Comparator(java.util.Comparator) Stack(java.util.Stack)

Example 12 with Path

use of java.nio.file.Path in project storm by apache.

the class DirectoryCleaner method getFilesForDir.

// Note that to avoid memory problem, we only return the first 1024 files in a directory
public static List<File> getFilesForDir(File dir) throws IOException {
    List<File> files = new ArrayList<File>();
    final int MAX_NUM = 1024;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath())) {
        for (Path path : stream) {
            files.add(path.toFile());
            if (files.size() >= MAX_NUM) {
                break;
            }
        }
    }
    return files;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) File(java.io.File)

Example 13 with Path

use of java.nio.file.Path in project storm by apache.

the class AdvancedFSOps method deleteIfExists.

/**
     * Delete a file or a directory and all of the children. If it exists.
     * @param path what to delete
     * @throws IOException on any error.
     */
public void deleteIfExists(File path) throws IOException {
    LOG.info("Deleting path {}", path);
    Path p = path.toPath();
    if (Files.exists(p)) {
        try {
            FileUtils.forceDelete(path);
        } catch (FileNotFoundException ignored) {
        }
    }
}
Also used : Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException)

Example 14 with Path

use of java.nio.file.Path in project storm by apache.

the class AdvancedFSOps method createSymlink.

/**
     * Create a symbolic link pointing at target
     * @param link the link to create
     * @param target where it should point to
     * @throws IOException on any error.
     */
public void createSymlink(File link, File target) throws IOException {
    Path plink = link.toPath().toAbsolutePath();
    Path ptarget = target.toPath().toAbsolutePath();
    LOG.debug("Creating symlink [{}] to [{}]", plink, ptarget);
    if (Files.exists(plink)) {
        if (Files.isSameFile(plink, ptarget)) {
            //It already points where we want it to
            return;
        }
        FileUtils.forceDelete(link);
    }
    Files.createSymbolicLink(plink, ptarget);
}
Also used : Path(java.nio.file.Path)

Example 15 with Path

use of java.nio.file.Path in project hbase by apache.

the class TestUpdateConfiguration method testAllCustomOnlineConfigChange.

@Test
public void testAllCustomOnlineConfigChange() throws IOException {
    LOG.debug("Starting the test");
    Path cnfPath = FileSystems.getDefault().getPath("target/test-classes/hbase-site.xml");
    Path cnf2Path = FileSystems.getDefault().getPath("target/test-classes/hbase-site2.xml");
    Path cnf3Path = FileSystems.getDefault().getPath("target/test-classes/hbase-site3.xml");
    // make a backup of hbase-site.xml
    Files.copy(cnfPath, cnf3Path, StandardCopyOption.REPLACE_EXISTING);
    // update hbase-site.xml by overwriting it
    Files.copy(cnf2Path, cnfPath, StandardCopyOption.REPLACE_EXISTING);
    Admin admin = TEST_UTIL.getAdmin();
    admin.updateConfiguration();
    // Check the configuration of the Masters
    Configuration masterConfiguration = TEST_UTIL.getMiniHBaseCluster().getMaster(0).getConfiguration();
    int custom = masterConfiguration.getInt("hbase.custom.config", 0);
    assertEquals(custom, 1000);
    Configuration backupMasterConfiguration = TEST_UTIL.getMiniHBaseCluster().getMaster(1).getConfiguration();
    custom = backupMasterConfiguration.getInt("hbase.custom.config", 0);
    assertEquals(custom, 1000);
    // Check the configuration of the RegionServer
    Configuration regionServerConfiguration = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0).getConfiguration();
    custom = regionServerConfiguration.getInt("hbase.custom.config", 0);
    assertEquals(custom, 1000);
    // restore hbase-site.xml
    Files.copy(cnf3Path, cnfPath, StandardCopyOption.REPLACE_EXISTING);
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.hadoop.conf.Configuration) Test(org.junit.Test)

Aggregations

Path (java.nio.file.Path)4893 Test (org.junit.Test)1960 IOException (java.io.IOException)829 File (java.io.File)445 SourcePath (com.facebook.buck.rules.SourcePath)389 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)334 BuildTarget (com.facebook.buck.model.BuildTarget)320 ArrayList (java.util.ArrayList)313 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)250 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)231 PathSourcePath (com.facebook.buck.rules.PathSourcePath)226 InputStream (java.io.InputStream)210 ImmutableList (com.google.common.collect.ImmutableList)175 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)166 HashMap (java.util.HashMap)159 ImmutableMap (com.google.common.collect.ImmutableMap)157 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)154 Matchers.containsString (org.hamcrest.Matchers.containsString)148 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)147 Map (java.util.Map)146