Search in sources :

Example 6 with FileStore

use of java.nio.file.FileStore in project lucene-solr by apache.

the class TestIOUtils method testRotatingPlatters.

public void testRotatingPlatters() throws Exception {
    assumeFalse("windows is not supported", Constants.WINDOWS);
    Path dir = createTempDir();
    dir = FilterPath.unwrap(dir).toRealPath();
    // fake ssd
    FileStore root = new MockFileStore(dir.toString() + " (/dev/zzz1)", "reiser4", "/dev/zzz1");
    // make a fake /dev/zzz1 for it
    Path devdir = dir.resolve("dev");
    Files.createDirectories(devdir);
    Files.createFile(devdir.resolve("zzz1"));
    // make a fake /sys/block/zzz/queue/rotational file for it
    Path sysdir = dir.resolve("sys").resolve("block").resolve("zzz").resolve("queue");
    Files.createDirectories(sysdir);
    try (OutputStream o = Files.newOutputStream(sysdir.resolve("rotational"))) {
        o.write("1\n".getBytes(StandardCharsets.US_ASCII));
    }
    Map<String, FileStore> mappings = Collections.singletonMap(dir.toString(), root);
    FileSystem mockLinux = new MockLinuxFileSystemProvider(dir.getFileSystem(), mappings, dir).getFileSystem(null);
    Path mockPath = mockLinux.getPath(dir.toString());
    assertTrue(IOUtils.spinsLinux(mockPath));
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) FileStore(java.nio.file.FileStore) OutputStream(java.io.OutputStream) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem)

Example 7 with FileStore

use of java.nio.file.FileStore in project lucene-solr by apache.

the class IOUtils method spinsLinux.

// following methods are package-private for testing ONLY
// note: requires a real or fake linux filesystem!
static boolean spinsLinux(Path path) throws IOException {
    FileStore store = getFileStore(path);
    // this won't have a corresponding block device
    if ("tmpfs".equals(store.type())) {
        return false;
    }
    // get block device name
    String devName = store.name();
    // not a device (e.g. NFS server)
    if (!devName.startsWith("/")) {
        return true;
    }
    // resolve any symlinks to real block device (e.g. LVM)
    // /dev/sda0 -> sda0
    // /devices/XXX -> sda0
    devName = path.getRoot().resolve(devName).toRealPath().getFileName().toString();
    // now try to find the longest matching device folder in /sys/block
    // (that starts with our dev name):
    Path sysinfo = path.getRoot().resolve("sys").resolve("block");
    Path devsysinfo = null;
    int matchlen = 0;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(sysinfo)) {
        for (Path device : stream) {
            String name = device.getFileName().toString();
            if (name.length() > matchlen && devName.startsWith(name)) {
                devsysinfo = device;
                matchlen = name.length();
            }
        }
    }
    if (devsysinfo == null) {
        // give up
        return true;
    }
    // read first byte from rotational, it's a 1 if it spins.
    Path rotational = devsysinfo.resolve("queue").resolve("rotational");
    try (InputStream stream = Files.newInputStream(rotational)) {
        return stream.read() == '1';
    }
}
Also used : Path(java.nio.file.Path) FileStore(java.nio.file.FileStore) InputStream(java.io.InputStream)

Example 8 with FileStore

use of java.nio.file.FileStore in project elasticsearch by elastic.

the class ShardPath method selectNewPathForShard.

public static ShardPath selectNewPathForShard(NodeEnvironment env, ShardId shardId, IndexSettings indexSettings, long avgShardSizeInBytes, Map<Path, Integer> dataPathToShardCount) throws IOException {
    final Path dataPath;
    final Path statePath;
    if (indexSettings.hasCustomDataPath()) {
        dataPath = env.resolveCustomLocation(indexSettings, shardId);
        statePath = env.nodePaths()[0].resolve(shardId);
    } else {
        BigInteger totFreeSpace = BigInteger.ZERO;
        for (NodeEnvironment.NodePath nodePath : env.nodePaths()) {
            totFreeSpace = totFreeSpace.add(BigInteger.valueOf(nodePath.fileStore.getUsableSpace()));
        }
        // TODO: this is a hack!!  We should instead keep track of incoming (relocated) shards since we know
        // how large they will be once they're done copying, instead of a silly guess for such cases:
        // Very rough heuristic of how much disk space we expect the shard will use over its lifetime, the max of current average
        // shard size across the cluster and 5% of the total available free space on this node:
        BigInteger estShardSizeInBytes = BigInteger.valueOf(avgShardSizeInBytes).max(totFreeSpace.divide(BigInteger.valueOf(20)));
        // TODO - do we need something more extensible? Yet, this does the job for now...
        final NodeEnvironment.NodePath[] paths = env.nodePaths();
        NodeEnvironment.NodePath bestPath = null;
        BigInteger maxUsableBytes = BigInteger.valueOf(Long.MIN_VALUE);
        for (NodeEnvironment.NodePath nodePath : paths) {
            FileStore fileStore = nodePath.fileStore;
            BigInteger usableBytes = BigInteger.valueOf(fileStore.getUsableSpace());
            assert usableBytes.compareTo(BigInteger.ZERO) >= 0;
            // Deduct estimated reserved bytes from usable space:
            Integer count = dataPathToShardCount.get(nodePath.path);
            if (count != null) {
                usableBytes = usableBytes.subtract(estShardSizeInBytes.multiply(BigInteger.valueOf(count)));
            }
            if (bestPath == null || usableBytes.compareTo(maxUsableBytes) > 0) {
                maxUsableBytes = usableBytes;
                bestPath = nodePath;
            }
        }
        statePath = bestPath.resolve(shardId);
        dataPath = statePath;
    }
    return new ShardPath(indexSettings.hasCustomDataPath(), dataPath, statePath, shardId);
}
Also used : Path(java.nio.file.Path) BigInteger(java.math.BigInteger) FileStore(java.nio.file.FileStore) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) BigInteger(java.math.BigInteger)

Example 9 with FileStore

use of java.nio.file.FileStore in project elasticsearch by elastic.

the class ESFileStore method getFileStoreWindows.

/** 
     * remove this code and just use getFileStore for windows on java 9
     * works around https://bugs.openjdk.java.net/browse/JDK-8034057
     */
@SuppressForbidden(reason = "works around https://bugs.openjdk.java.net/browse/JDK-8034057")
static FileStore getFileStoreWindows(Path path, FileStore[] fileStores) throws IOException {
    assert Constants.WINDOWS;
    try {
        return Files.getFileStore(path);
    } catch (FileSystemException possibleBug) {
        final char driveLetter;
        // if something goes wrong, we just deliver the original exception
        try {
            String root = path.toRealPath().getRoot().toString();
            if (root.length() < 2) {
                throw new RuntimeException("root isn't a drive letter: " + root);
            }
            driveLetter = Character.toLowerCase(root.charAt(0));
            if (Character.isAlphabetic(driveLetter) == false || root.charAt(1) != ':') {
                throw new RuntimeException("root isn't a drive letter: " + root);
            }
        } catch (Exception checkFailed) {
            // something went wrong, 
            possibleBug.addSuppressed(checkFailed);
            throw possibleBug;
        }
        // we have a drive letter: the hack begins!!!!!!!!
        try {
            // we have no choice but to parse toString of all stores and find the matching drive letter
            for (FileStore store : fileStores) {
                String toString = store.toString();
                int length = toString.length();
                if (length > 3 && toString.endsWith(":)") && toString.charAt(length - 4) == '(') {
                    if (Character.toLowerCase(toString.charAt(length - 3)) == driveLetter) {
                        return store;
                    }
                }
            }
            throw new RuntimeException("no filestores matched");
        } catch (Exception weTried) {
            IOException newException = new IOException("Unable to retrieve filestore for '" + path + "', tried matching against " + Arrays.toString(fileStores), weTried);
            newException.addSuppressed(possibleBug);
            throw newException;
        }
    }
}
Also used : FileStore(java.nio.file.FileStore) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) SuppressForbidden(org.elasticsearch.common.SuppressForbidden)

Example 10 with FileStore

use of java.nio.file.FileStore in project elasticsearch by elastic.

the class ESFileStore method getMatchingFileStore.

/** 
     * Files.getFileStore(Path) useless here!  Don't complain, just try it yourself. 
     */
@SuppressForbidden(reason = "works around the bugs")
static FileStore getMatchingFileStore(Path path, FileStore[] fileStores) throws IOException {
    if (Constants.WINDOWS) {
        return getFileStoreWindows(path, fileStores);
    }
    final FileStore store;
    try {
        store = Files.getFileStore(path);
    } catch (IOException unexpected) {
        // give a better error message if a filestore cannot be retrieved from inside a FreeBSD jail.
        if (Constants.FREE_BSD) {
            throw new IOException("Unable to retrieve mount point data for " + path + ". If you are running within a jail, set enforce_statfs=1. See jail(8)", unexpected);
        } else {
            throw unexpected;
        }
    }
    try {
        String mount = getMountPointLinux(store);
        FileStore sameMountPoint = null;
        for (FileStore fs : fileStores) {
            if (mount.equals(getMountPointLinux(fs))) {
                if (sameMountPoint == null) {
                    sameMountPoint = fs;
                } else {
                    // fall back to crappy one we got from Files.getFileStore
                    return store;
                }
            }
        }
        if (sameMountPoint != null) {
            // ok, we found only one, use it:
            return sameMountPoint;
        } else {
            // fall back to crappy one we got from Files.getFileStore
            return store;
        }
    } catch (Exception e) {
    // ignore
    }
    // fall back to crappy one we got from Files.getFileStore
    return store;
}
Also used : FileStore(java.nio.file.FileStore) IOException(java.io.IOException) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) SuppressForbidden(org.elasticsearch.common.SuppressForbidden)

Aggregations

FileStore (java.nio.file.FileStore)17 Path (java.nio.file.Path)11 FileSystem (java.nio.file.FileSystem)8 FilterFileSystem (org.apache.lucene.mockfile.FilterFileSystem)8 FilterPath (org.apache.lucene.mockfile.FilterPath)8 OutputStream (java.io.OutputStream)5 IOException (java.io.IOException)4 FileSystemException (java.nio.file.FileSystemException)2 SuppressForbidden (org.elasticsearch.common.SuppressForbidden)2 NSDictionary (com.dd.plist.NSDictionary)1 NSNumber (com.dd.plist.NSNumber)1 NSObject (com.dd.plist.NSObject)1 FileSystemException (io.vertx.core.file.FileSystemException)1 File (java.io.File)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 UUID (java.util.UUID)1 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)1