diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-01-28 18:32:51 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-01-28 18:32:51 +0100 |
commit | bcd260cd95241cab4ac024abd2b6de49965e34ff (patch) | |
tree | 70b86b6468aeca74d5d1e4b8602f7f460c79ff68 | |
parent | 354b7c36e0f18a63b4b6ca8e4143bf278cba2925 (diff) |
Implement debug handler
7 files changed, 112 insertions, 11 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/component/TaskContext.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/component/TaskContext.java index 9def627e87f..87491367514 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/component/TaskContext.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/component/TaskContext.java @@ -18,4 +18,6 @@ public interface TaskContext { FileSystem fileSystem(); void logSystemModification(Logger logger, String actionDescription); + + default boolean executeSubtask(IdempotentTask task) { return false; } } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/DebugHandlerHelper.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/DebugHandlerHelper.java new file mode 100644 index 00000000000..a1e6ab7c8be --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/DebugHandlerHelper.java @@ -0,0 +1,45 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.vespa.hosted.node.admin.provider; + +import javax.annotation.concurrent.ThreadSafe; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +@ThreadSafe +public class DebugHandlerHelper implements NodeAdminDebugHandler { + private Object monitor = new Object(); + private final ConcurrentMap<String, Supplier<Object>> suppliers = new ConcurrentHashMap<>(); + + public void addThreadSafeSupplier(String name, Supplier<Object> threadSafeSupplier) { + Supplier<Object> previousSupplier = suppliers.putIfAbsent(name, threadSafeSupplier); + if (previousSupplier != null) { + throw new IllegalArgumentException(name + " is already registered"); + } + } + + public void addHandler(String name, NodeAdminDebugHandler handler) { + addThreadSafeSupplier(name, () -> handler.getDebugPage()); + } + + public void addConstant(String name, String value) { + addThreadSafeSupplier(name, () -> value); + } + + public void remove(String name) { + Supplier<Object> supplier = suppliers.remove(name); + if (supplier == null) { + throw new IllegalArgumentException(name + " is not registered"); + } + } + + @Override + public Map<String, Object> getDebugPage() { + return suppliers.entrySet().stream().collect(Collectors.toMap( + Map.Entry::getKey, + entry -> entry.getValue().get())); + } +} diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/NodeAdminDebugHandler.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/NodeAdminDebugHandler.java new file mode 100644 index 00000000000..7cd5386ecbd --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/NodeAdminDebugHandler.java @@ -0,0 +1,15 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.vespa.hosted.node.admin.provider; + +import javax.annotation.concurrent.ThreadSafe; +import java.util.Map; + +@ThreadSafe +public interface NodeAdminDebugHandler { + /** + * The Object in the map values must be serializable with Jackson's ObjectMapper. + * May be called concurrently by different threads. + */ + Map<String, Object> getDebugPage(); +} diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/NodeAdminStateUpdater.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/NodeAdminStateUpdater.java index 755e1301c12..841f464e014 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/NodeAdminStateUpdater.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/provider/NodeAdminStateUpdater.java @@ -1,9 +1,10 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.provider; -import java.util.Map; +import javax.annotation.concurrent.ThreadSafe; -public interface NodeAdminStateUpdater { +@ThreadSafe +public interface NodeAdminStateUpdater extends NodeAdminDebugHandler { enum State { TRANSITIONING, RESUMED, SUSPENDED_NODE_ADMIN, SUSPENDED} /** @@ -12,6 +13,4 @@ public interface NodeAdminStateUpdater { * has converged. */ boolean setResumeStateAndCheckIfResumed(State wantedState); - - Map<String, Object> getDebugPage(); } diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/provider/DebugHandlerHelperTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/provider/DebugHandlerHelperTest.java new file mode 100644 index 00000000000..723b9f0df8a --- /dev/null +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/provider/DebugHandlerHelperTest.java @@ -0,0 +1,35 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.vespa.hosted.node.admin.provider; + +import org.junit.Test; + +import java.util.Collections; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class DebugHandlerHelperTest { + @Test + public void trivial() { + DebugHandlerHelper helper = new DebugHandlerHelper(); + helper.addConstant("constant-key", "constant-value"); + + NodeAdminDebugHandler handler = new NodeAdminDebugHandler() { + @Override + public Map<String, Object> getDebugPage() { + return Collections.singletonMap("handler-value-key", "handler-value-value"); + } + }; + helper.addHandler("handler-key", handler); + + helper.addThreadSafeSupplier("supplier-key", () -> "supplier-value"); + + assertEquals("{" + + "supplier-key=supplier-value, " + + "handler-key={handler-value-key=handler-value-value}, " + + "constant-key=constant-value" + + "}", + helper.getDebugPage().toString()); + } +}
\ No newline at end of file diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileSyncTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileSyncTest.java index 71cd312d5d6..44868e17464 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileSyncTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileSyncTest.java @@ -2,6 +2,7 @@ package com.yahoo.vespa.hosted.node.admin.task.util.file; +import com.yahoo.vespa.test.file.TestFileSystem; import org.junit.Test; import java.nio.file.FileSystem; @@ -18,7 +19,7 @@ import static org.junit.Assert.assertTrue; public class FileSyncTest { private final TestTaskContext taskContext = new TestTaskContext(); - private final FileSystem fileSystem = taskContext.fileSystem(); + private final FileSystem fileSystem = TestFileSystem.create(); private final Path path = fileSystem.getPath("/dir/file.txt"); private final UnixPath unixPath = new UnixPath(path); @@ -80,4 +81,4 @@ public class FileSyncTest { actualMods = taskContext.getSystemModificationLog(); assertEquals(new ArrayList<>(), actualMods); } -}
\ No newline at end of file +} diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TestTaskContext.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TestTaskContext.java index d023a11671e..757f3004683 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TestTaskContext.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/TestTaskContext.java @@ -2,8 +2,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.file; +import com.yahoo.vespa.hosted.node.admin.component.IdempotentTask; import com.yahoo.vespa.hosted.node.admin.component.TaskContext; -import com.yahoo.vespa.test.file.TestFileSystem; import java.nio.file.FileSystem; import java.util.ArrayList; @@ -12,22 +12,21 @@ import java.util.List; import java.util.logging.Logger; public class TestTaskContext implements TaskContext { - private final FileSystem fileSystem = TestFileSystem.create(); private final List<String> logs = new ArrayList<>(); @Override public Cloud cloud() { - return Cloud.YAHOO; + throw new UnsupportedOperationException(); } @Override public EnumSet<Role> roles() { - return EnumSet.of(Role.CONFIG_SERVER_DOCKER_HOST); + throw new UnsupportedOperationException(); } @Override public FileSystem fileSystem() { - return fileSystem; + throw new UnsupportedOperationException(); } @Override @@ -42,4 +41,9 @@ public class TestTaskContext implements TaskContext { public void clearSystemModificationLog() { logs.clear(); } + + @Override + public boolean executeSubtask(IdempotentTask task) { + throw new UnsupportedOperationException(); + } } |