diff options
author | Valerij Fredriksen <valerijf@yahooinc.com> | 2021-10-14 15:29:42 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@yahooinc.com> | 2021-10-14 15:30:43 +0200 |
commit | e023017c068741d1ca1e4ea5171c80d7a700df09 (patch) | |
tree | 45e5d04f3483aa285280fa3e04ecea14d0bc2825 /node-admin | |
parent | 7f64a2f8087aedc57becf466b126bda4d24692ac (diff) |
Simplify with UnixPath
Diffstat (limited to 'node-admin')
8 files changed, 61 insertions, 64 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java index c7616abd508..1d87415b78e 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/Cgroup.java @@ -5,12 +5,7 @@ package com.yahoo.vespa.hosted.node.admin.container; import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContext; import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixPath; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.OptionalInt; import java.util.logging.Logger; @@ -85,17 +80,9 @@ public class Cgroup { } private OptionalInt readCgroupsCpuInt(UnixPath unixPath) { - final byte[] currentContentBytes; - try { - currentContentBytes = Files.readAllBytes(unixPath.toPath()); - } catch (NoSuchFileException e) { - return OptionalInt.empty(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - - String currentContent = new String(currentContentBytes, StandardCharsets.UTF_8).strip(); - return OptionalInt.of(Integer.parseInt(currentContent)); + return unixPath.readUtf8FileIfExists() + .map(s -> OptionalInt.of(Integer.parseInt(s.strip()))) + .orElseGet(OptionalInt::empty); } private boolean writeCgroupsCpuInt(NodeAgentContext context, UnixPath unixPath, int value) { diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java index bd01b320666..23fbf01e14b 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/identity/AthenzCredentialsMaintainer.java @@ -266,11 +266,9 @@ public class AthenzCredentialsMaintainer implements CredentialsMaintainer { } private static void writeFile(Path path, int vespaUidOnHost, String utf8Content) { - new UnixPath(path.toString() + ".tmp") - .deleteIfExists() - .createNewFile("r--------") + new UnixPath(path.resolveSibling(path.getFileName() + ".tmp")) + .writeUtf8File(utf8Content, "r--------") .setOwnerId(vespaUidOnHost) - .writeUtf8File(utf8Content) .atomicMove(path); } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java index 23a6ed2aa8c..27b3467163c 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/VespaServiceDumperImpl.java @@ -100,8 +100,7 @@ public class VespaServiceDumperImpl implements VespaServiceDumper { directoryOnHost.deleteRecursively(); } context.log(log, Level.INFO, "Creating '" + directoryOnHost +"'."); - directoryOnHost.createDirectory(); - directoryOnHost.setPermissions("rwxrwxrwx"); + directoryOnHost.createDirectory("rwxrwxrwx"); URI destination = serviceDumpDestination(nodeSpec, createDumpId(request)); ProducerContext producerCtx = new ProducerContext(context, directoryInNode.toPath(), request); List<Artifact> producedArtifacts = new ArrayList<>(); diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java index 020a8b6c7f6..5b53879ceec 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java @@ -1,3 +1,4 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.nodeagent; /** diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java index 2ce49ae383a..6949f648865 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java @@ -3,6 +3,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.file; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.nio.channels.SeekableByteChannel; import java.nio.charset.StandardCharsets; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; @@ -12,6 +14,7 @@ import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.FileTime; import java.nio.file.attribute.GroupPrincipal; @@ -35,6 +38,10 @@ import static com.yahoo.yolean.Exceptions.uncheck; */ // @Immutable public class UnixPath { + + private static final Set<OpenOption> DEFAULT_OPEN_OPTIONS = + Set.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); + private final Path path; public UnixPath(Path path) { this.path = path; } @@ -97,13 +104,29 @@ public class UnixPath { return writeBytes(content.getBytes(StandardCharsets.UTF_8), options); } + public UnixPath writeUtf8File(String content, String permissions, OpenOption... options) { + return writeBytes(content.getBytes(StandardCharsets.UTF_8), permissions, options); + } + public UnixPath writeBytes(byte[] content, OpenOption... options) { - uncheck(() -> Files.write(path, content, options)); - return this; + return writeBytes(content, null, options); } - public UnixPath atomicWriteUt8(String content) { - return atomicWriteBytes(content.getBytes(StandardCharsets.UTF_8)); + public UnixPath writeBytes(byte[] content, String permissions, OpenOption... options) { + FileAttribute<?>[] attributes = Optional.ofNullable(permissions) + .map(this::getPosixFilePermissionsFromString) + .map(PosixFilePermissions::asFileAttribute) + .map(attribute -> new FileAttribute<?>[]{attribute}) + .orElseGet(() -> new FileAttribute<?>[0]); + + Set<OpenOption> optionsSet = options.length == 0 ? DEFAULT_OPEN_OPTIONS : Set.of(options); + + try (SeekableByteChannel channel = Files.newByteChannel(path, optionsSet, attributes)) { + channel.write(ByteBuffer.wrap(content)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return this; } /** Write a file to the same dir as this, and then atomically move it to this' path. */ @@ -184,18 +207,8 @@ public class UnixPath { return this; } - public UnixPath createNewFile(String permissions) { - FileAttribute<?> attribute = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(permissions)); - uncheck(() -> Files.createFile(path, attribute)); - return this; - } - public UnixPath createParents() { - Path parent = path.getParent(); - if (!Files.isDirectory(parent)) { - uncheck(() -> Files.createDirectories(parent)); - } - + uncheck(() -> Files.createDirectories(path.getParent())); return this; } @@ -275,7 +288,7 @@ public class UnixPath { } catch (NoSuchFileException ignored) { return Stream.empty(); } catch (IOException e) { - throw new RuntimeException("Failed to list contents of directory " + path.toAbsolutePath(), e); + throw new UncheckedIOException("Failed to list contents of directory " + path.toAbsolutePath(), e); } } diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/container/ContainerStatsCollectorTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/container/ContainerStatsCollectorTest.java index 80dbbfb9d13..82fb63b0036 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/container/ContainerStatsCollectorTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/container/ContainerStatsCollectorTest.java @@ -1,13 +1,11 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.container; +import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixPath; import com.yahoo.vespa.test.file.TestFileSystem; import org.junit.Test; -import java.io.IOException; import java.nio.file.FileSystem; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Map; import java.util.Optional; @@ -22,7 +20,7 @@ public class ContainerStatsCollectorTest { private final FileSystem fileSystem = TestFileSystem.create(); @Test - public void collect() throws Exception { + public void collect() { ContainerStatsCollector collector = new ContainerStatsCollector(fileSystem); ContainerId containerId = new ContainerId("id1"); int containerPid = 42; @@ -44,22 +42,21 @@ public class ContainerStatsCollectorTest { stats.get().getNetworks()); } - private void mockNetworkStats(int pid) throws IOException { - Path dev = fileSystem.getPath("/proc/" + pid + "/net/dev"); - Files.createDirectories(dev.getParent()); - Files.writeString(dev, "Inter-| Receive | Transmit\n" + + private void mockNetworkStats(int pid) { + UnixPath dev = new UnixPath(fileSystem.getPath("/proc/" + pid + "/net/dev")); + dev.createParents().writeUtf8File("Inter-| Receive | Transmit\n" + " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n" + " lo: 36289258 149700 0 0 0 0 0 0 36289258 149700 0 0 0 0 0 0\n" + " eth0: 22280813 118083 3 4 0 0 0 0 19859383 115415 5 6 0 0 0 0\n"); } - private void mockMemoryStats(ContainerId containerId) throws IOException { - Path root = fileSystem.getPath("/sys/fs/cgroup/memory/machine.slice/libpod-" + containerId + ".scope"); - Files.createDirectories(root); + private void mockMemoryStats(ContainerId containerId) { + UnixPath root = new UnixPath(fileSystem.getPath("/sys/fs/cgroup/memory/machine.slice/libpod-" + containerId + ".scope")); + root.createDirectories(); - Files.writeString(root.resolve("memory.limit_in_bytes"), "2147483648\n"); - Files.writeString(root.resolve("memory.usage_in_bytes"), "1228017664\n"); - Files.writeString(root.resolve("memory.stat"), "cache 470790144\n" + + root.resolve("memory.limit_in_bytes").writeUtf8File("2147483648\n"); + root.resolve("memory.usage_in_bytes").writeUtf8File("1228017664\n"); + root.resolve("memory.stat").writeUtf8File("cache 470790144\n" + "rss 698699776\n" + "rss_huge 526385152\n" + "shmem 0\n" + @@ -97,24 +94,25 @@ public class ContainerStatsCollectorTest { "total_unevictable 0\n"); } - private void mockCpuStats(ContainerId containerId) throws IOException { - Path root = fileSystem.getPath("/sys/fs/cgroup/cpuacct/machine.slice/libpod-" + containerId + ".scope"); - Path proc = fileSystem.getPath("/proc"); - Files.createDirectories(root); - Files.createDirectories(proc); - Files.writeString(root.resolve("cpu.stat"), "nr_periods 1\n" + + private void mockCpuStats(ContainerId containerId) { + UnixPath root = new UnixPath(fileSystem.getPath("/sys/fs/cgroup/cpuacct/machine.slice/libpod-" + containerId + ".scope")); + UnixPath proc = new UnixPath(fileSystem.getPath("/proc")); + root.createDirectories(); + proc.createDirectories(); + + root.resolve("cpu.stat").writeUtf8File("nr_periods 1\n" + "nr_throttled 2\n" + "throttled_time 3\n"); - Files.writeString(root.resolve("cpuacct.usage_percpu"), "25801608855 22529436415 25293652376 26212081533 " + + root.resolve("cpuacct.usage_percpu").writeUtf8File("25801608855 22529436415 25293652376 26212081533 " + "27545883290 25357818592 33464821448 32568003867 " + "28916742231 31771772292 34418037242 38417072233 " + "26069101127 24568838237 23683334366 26824607997 " + "24289870206 22249389818 32683986446 32444831154 " + "30488394217 26840956322 31633747261 30838696584\n"); - Files.writeString(root.resolve("cpuacct.usage"), "691675615472\n"); - Files.writeString(root.resolve("cpuacct.stat"), "user 40900\n" + + root.resolve("cpuacct.usage").writeUtf8File("691675615472\n"); + root.resolve("cpuacct.stat").writeUtf8File("user 40900\n" + "system 26219\n"); - Files.writeString(proc.resolve("stat"), "cpu 7991366 978222 2346238 565556517 1935450 25514479 615206 0 0 0\n" + + proc.resolve("stat").writeUtf8File("cpu 7991366 978222 2346238 565556517 1935450 25514479 615206 0 0 0\n" + "cpu0 387906 61529 99088 23516506 42258 1063359 29882 0 0 0\n" + "cpu1 271253 49383 86149 23655234 41703 1061416 31885 0 0 0\n" + "cpu2 349420 50987 93560 23571695 59437 1051977 24461 0 0 0\n" + diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinderTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinderTest.java index 80e88e302d3..0bb820bbfdc 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinderTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileFinderTest.java @@ -26,7 +26,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; -import static com.google.common.collect.ImmutableSet.of; +import static java.util.Set.of; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java index 952bf04620f..01c7e9265ac 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java @@ -6,6 +6,7 @@ import com.yahoo.vespa.test.file.TestFileSystem; import org.junit.ComparisonFailure; import org.junit.Test; +import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; @@ -147,7 +148,7 @@ public class UnixPathTest { var path = new UnixPath(fs.getPath("/dir/foo")); path.createParents(); path.writeUtf8File("bar"); - path.atomicWriteUt8("bar v2"); + path.atomicWriteBytes("bar v2".getBytes(StandardCharsets.UTF_8)); assertEquals("bar v2", path.readUtf8File()); } |