ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akuznet...@apache.org
Subject [6/7] ignite git commit: IGNITE-1952 Visorcmd: add a command for reset (clear) of metrics. Fixes #1029.
Date Mon, 05 Sep 2016 08:11:42 GMT
IGNITE-1952 Visorcmd: add a command for reset (clear) of metrics. Fixes #1029.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/afac3fab
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/afac3fab
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/afac3fab

Branch: refs/heads/master
Commit: afac3fab5f22250b830383a9f10336d396c83ad9
Parents: bdbc5a3
Author: Saikat Maitra <saikat.maitra@gmail.com>
Authored: Mon Sep 5 15:06:39 2016 +0700
Committer: Alexey Kuznetsov <akuznetsov@apache.org>
Committed: Mon Sep 5 15:06:39 2016 +0700

----------------------------------------------------------------------
 .../ignite/visor/commands/VisorConsole.scala    |   1 +
 .../commands/cache/VisorCacheCommand.scala      |  33 ++++-
 .../commands/cache/VisorCacheResetCommand.scala | 129 +++++++++++++++++++
 .../cache/VisorCacheResetCommandSpec.scala      | 114 ++++++++++++++++
 4 files changed, 271 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/afac3fab/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/VisorConsole.scala
----------------------------------------------------------------------
diff --git a/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/VisorConsole.scala
b/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/VisorConsole.scala
index b4d78b5..ad8c2ed 100644
--- a/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/VisorConsole.scala
+++ b/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/VisorConsole.scala
@@ -62,6 +62,7 @@ class VisorConsole {
         org.apache.ignite.visor.commands.ack.VisorAckCommand
         org.apache.ignite.visor.commands.alert.VisorAlertCommand
         org.apache.ignite.visor.commands.cache.VisorCacheClearCommand
+        org.apache.ignite.visor.commands.cache.VisorCacheResetCommand
         org.apache.ignite.visor.commands.cache.VisorCacheCommand
         org.apache.ignite.visor.commands.cache.VisorCacheSwapCommand
         org.apache.ignite.visor.commands.config.VisorConfigurationCommand

http://git-wip-us.apache.org/repos/asf/ignite/blob/afac3fab/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheCommand.scala
----------------------------------------------------------------------
diff --git a/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheCommand.scala
b/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheCommand.scala
index 68465de..1fa185f 100755
--- a/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheCommand.scala
+++ b/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheCommand.scala
@@ -58,6 +58,9 @@ import scala.language.{implicitConversions, reflectiveCalls}
  * +-----------------------------------------------------------------------------------------+
  * | cache -stop    | Stop cache with specified name.                                   
    |
  * +-----------------------------------------------------------------------------------------+
+ * | cache -reset   | Reset metrics for cache with specified name.                      
    |
+ * +-----------------------------------------------------------------------------------------+
+ *
  * }}}
  *
  * ====Specification====
@@ -69,6 +72,7 @@ import scala.language.{implicitConversions, reflectiveCalls}
  *     cache -scan -c=<cache-name> {-id=<node-id>|id8=<node-id8>} {-p=<page
size>} {-system}
  *     cache -swap {-c=<cache-name>} {-id=<node-id>|id8=<node-id8>}
  *     cache -stop -c=<cache-name>
+ *     cache -reset -c=<cache-name>
  * }}}
  *
  * ====Arguments====
@@ -113,6 +117,8 @@ import scala.language.{implicitConversions, reflectiveCalls}
  *          Swaps backup entries in cache.
  *     -stop
  *          Stop cache with specified name.
+ *     -reset
+ *          Reset metrics for cache with specified name.
  *     -p=<page size>
  *         Number of object to fetch from cache at once.
  *         Valid range from 1 to 100.
@@ -153,6 +159,9 @@ import scala.language.{implicitConversions, reflectiveCalls}
  *         Swaps entries in cache with name taken from 'c0' memory variable.
  *     cache -stop -c=cache
  *         Stops cache with name 'cache'.
+ *     cache -reset -c=cache
+ *         Reset metrics for cache with name 'cache'.
+ *
  * }}}
  */
 class VisorCacheCommand {
@@ -210,6 +219,9 @@ class VisorCacheCommand {
      * <br>
      * <ex>cache -stop -c=@c0</ex>
      *     Stop cache with name taken from 'c0' memory variable.
+     * <br>
+     * <ex>cache -reset -c=@c0</ex>
+     *     Reset metrics for cache with name taken from 'c0' memory variable.
      *
      * @param args Command arguments.
      */
@@ -260,9 +272,9 @@ class VisorCacheCommand {
             // Get cache stats data from all nodes.
             val aggrData = cacheData(node, cacheName, showSystem)
 
-            if (hasArgFlagIn("clear", "swap", "scan", "stop")) {
+            if (hasArgFlagIn("clear", "swap", "scan", "stop", "reset")) {
                 if (cacheName.isEmpty)
-                    askForCache("Select cache from:", node, showSystem && !hasArgFlagIn("clear",
"swap", "stop"), aggrData) match {
+                    askForCache("Select cache from:", node, showSystem && !hasArgFlagIn("clear",
"swap", "stop", "reset"), aggrData) match {
                         case Some(name) =>
                             argLst = argLst ++ Seq("c" -> name)
 
@@ -282,6 +294,8 @@ class VisorCacheCommand {
                                 VisorCacheSwapCommand().swap(argLst, node)
                             else if (hasArgFlag("stop", argLst))
                                 VisorCacheStopCommand().stop(argLst, node)
+                            else if (hasArgFlag("reset", argLst))
+                              VisorCacheResetCommand().reset(argLst, node)
                         }
                         else {
                             if (hasArgFlag("clear", argLst))
@@ -290,6 +304,8 @@ class VisorCacheCommand {
                                 warn("Backup swapping of system cache is not allowed: " +
name)
                             else if (hasArgFlag("stop", argLst))
                                 warn("Stopping of system cache is not allowed: " + name)
+                            else if (hasArgFlag("reset", argLst))
+                                warn("Reset metrics of system cache is not allowed: " + name)
                         }
                     }
                 })
@@ -716,8 +732,9 @@ object VisorCacheCommand {
             "cache -clear {-c=<cache-name>} {-id=<node-id>|id8=<node-id8>}",
             "cache -scan -c=<cache-name> {-id=<node-id>|id8=<node-id8>}
{-p=<page size>}",
             "cache -swap {-c=<cache-name>} {-id=<node-id>|id8=<node-id8>}",
-            "cache -stop -c=<cache-name>"
-    ),
+            "cache -stop -c=<cache-name>",
+            "cache -reset -c=<cache-name>"
+  ),
         args = Seq(
             "-id8=<node-id>" -> Seq(
                 "ID8 of the node to get cache statistics from.",
@@ -749,7 +766,10 @@ object VisorCacheCommand {
                 "Swaps backup entries in cache."
             ),
             "-stop" -> Seq(
-                "Stop cache with specified name"
+                "Stop cache with specified name."
+            ),
+            "-reset" -> Seq(
+                "Reset metrics of cache with specified name."
             ),
             "-s=hi|mi|rd|wr|cn" -> Seq(
                 "Defines sorting type. Sorted by:",
@@ -809,7 +829,8 @@ object VisorCacheCommand {
             "cache -swap" -> "Swaps entries in interactively selected cache.",
             "cache -swap -c=cache" -> "Swaps entries in cache with name 'cache'.",
             "cache -swap -c=@c0" -> "Swaps entries in cache with name taken from 'c0'
memory variable.",
-            "cache -stop -c=@c0" -> "Stop cache with name taken from 'c0' memory variable."
+            "cache -stop -c=@c0" -> "Stop cache with name taken from 'c0' memory variable.",
+            "cache -reset -c=@c0" -> "Reset metrics for cache with name taken from 'c0'
memory variable."
         ),
         emptyArgs = cmd.cache,
         withArgs = cmd.cache

http://git-wip-us.apache.org/repos/asf/ignite/blob/afac3fab/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommand.scala
----------------------------------------------------------------------
diff --git a/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommand.scala
b/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommand.scala
new file mode 100644
index 0000000..b59155b
--- /dev/null
+++ b/modules/visor-console/src/main/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommand.scala
@@ -0,0 +1,129 @@
+/*
+ * 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 org.apache.ignite.visor.commands.cache
+
+import org.apache.ignite.cluster.{ClusterGroupEmptyException, ClusterNode}
+import org.apache.ignite.internal.visor.cache.VisorCacheResetMetricsTask
+import org.apache.ignite.internal.visor.util.VisorTaskUtils._
+import org.apache.ignite.visor.visor._
+
+import scala.language.reflectiveCalls
+
+/**
+ * ==Overview==
+ * Visor 'reset' command implementation.
+ *
+ * ====Specification====
+ * {{{
+ *     cache -reset -c=<cache name>
+ * }}}
+ *
+ * ====Arguments====
+ * {{{
+ *     <cache-name>
+ *         Name of the cache.
+ * }}}
+ *
+ * ====Examples====
+ * {{{
+ *    cache -reset -c=@c0
+ *        Reset metrics for cache with name taken from 'c0' memory variable.
+ * }}}
+ */
+class VisorCacheResetCommand {
+    /**
+     * Prints error message and advise.
+     *
+     * @param errMsgs Error messages.
+     */
+    private def scold(errMsgs: Any*) {
+        assert(errMsgs != null)
+
+        warn(errMsgs: _*)
+        warn("Type 'help cache' to see how to use this command.")
+    }
+
+    private def error(e: Exception) {
+        var cause: Throwable = e
+
+        while (cause.getCause != null)
+            cause = cause.getCause
+
+        scold(cause.getMessage)
+    }
+
+    /**
+     * ===Command===
+     * Reset metrics for cache with specified name.
+     *
+     * ===Examples===
+     * <ex>cache -c=cache -reset</ex>
+     * Reset metrics for cache with name 'cache'.
+     *
+     * @param argLst Command arguments.
+     */
+    def reset(argLst: ArgList, node: Option[ClusterNode]) {
+        val cacheArg = argValue("c", argLst)
+
+        val cacheName = cacheArg match {
+            case None => null // default cache.
+
+            case Some(s) if s.startsWith("@") =>
+                warn("Can't find cache variable with specified name: " + s,
+                    "Type 'cache' to see available cache variables."
+                )
+
+                return
+
+            case Some(name) => name
+        }
+
+        val grp = try {
+            groupForDataNode(node, cacheName)
+        }
+        catch {
+            case _: ClusterGroupEmptyException =>
+                scold(messageNodeNotFound(node, cacheName))
+
+                return
+        }
+
+        try {
+            executeRandom(grp, classOf[VisorCacheResetMetricsTask], cacheName)
+
+            println("Visor successfully reset metrics for cache: " + escapeName(cacheName))
+        }
+        catch {
+            case _: ClusterGroupEmptyException => scold(messageNodeNotFound(node, cacheName))
+            case e: Exception => error(e)
+        }
+    }
+}
+
+/**
+ * Companion object that does initialization of the command.
+ */
+object VisorCacheResetCommand {
+    /** Singleton command. */
+    private val cmd = new VisorCacheResetCommand
+
+    /**
+      * Singleton.
+      */
+    def apply() = cmd
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/afac3fab/modules/visor-console/src/test/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommandSpec.scala
----------------------------------------------------------------------
diff --git a/modules/visor-console/src/test/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommandSpec.scala
b/modules/visor-console/src/test/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommandSpec.scala
new file mode 100644
index 0000000..18f728c
--- /dev/null
+++ b/modules/visor-console/src/test/scala/org/apache/ignite/visor/commands/cache/VisorCacheResetCommandSpec.scala
@@ -0,0 +1,114 @@
+/*
+ * 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 org.apache.ignite.visor.commands.cache
+
+import org.apache.ignite.Ignition
+import org.apache.ignite.cache.CacheAtomicityMode._
+import org.apache.ignite.cache.CacheMode._
+import org.apache.ignite.configuration.{CacheConfiguration, IgniteConfiguration}
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder
+import org.apache.ignite.visor.commands.cache.VisorCacheCommand._
+import org.apache.ignite.visor.{VisorRuntimeBaseSpec, visor}
+import org.jetbrains.annotations.Nullable
+
+import scala.collection.JavaConversions._
+
+/**
+  * Unit test for 'reset' command.
+  */
+class VisorCacheResetCommandSpec extends VisorRuntimeBaseSpec(2) {
+    /** IP finder. */
+    val ipFinder = new TcpDiscoveryVmIpFinder(true)
+
+    /**
+     * Creates grid configuration for provided grid host.
+     *
+     * @param name Grid name.
+     * @return Grid configuration.
+     */
+    override def config(name: String): IgniteConfiguration = {
+        val cfg = new IgniteConfiguration
+
+        cfg.setGridName(name)
+        cfg.setLocalHost("127.0.0.1")
+        cfg.setCacheConfiguration(cacheConfig(null), cacheConfig("cache"))
+
+        val discoSpi = new TcpDiscoverySpi()
+
+        discoSpi.setIpFinder(ipFinder)
+
+        cfg.setDiscoverySpi(discoSpi)
+
+        cfg
+    }
+
+    /**
+     * @param name Cache name.
+     * @return Cache Configuration.
+     */
+    def cacheConfig(@Nullable name: String): CacheConfiguration[Object, Object] = {
+        val cfg = new CacheConfiguration[Object, Object]
+
+        cfg.setCacheMode(REPLICATED)
+        cfg.setAtomicityMode(TRANSACTIONAL)
+        cfg.setName(name)
+
+        cfg
+    }
+
+    describe("A 'reset' visor command") {
+        it("should show correct result for default cache") {
+            Ignition.ignite("node-1").cache[Int, Int](null).putAll(Map(1 -> 1, 2 ->
2, 3 -> 3))
+
+            val lock = Ignition.ignite("node-1").cache[Int, Int](null).lock(1)
+
+            lock.lock()
+
+            VisorCacheResetCommand().reset(Nil, None)
+
+            lock.unlock()
+
+            VisorCacheResetCommand().reset(Nil, None)
+        }
+
+        it("should show correct result for named cache") {
+            Ignition.ignite("node-1").cache[Int, Int]("cache").putAll(Map(1 -> 1, 2 ->
2, 3 -> 3))
+
+            val lock = Ignition.ignite("node-1").cache[Int, Int]("cache").lock(1)
+
+            lock.lock()
+
+            visor.cache("-reset -c=cache")
+
+            lock.unlock()
+
+            visor.cache("-reset -c=cache")
+        }
+
+        it("should show correct help") {
+            VisorCacheCommand
+
+            visor.help("cache")
+        }
+
+        it("should show empty projection error message") {
+            visor.cache("-reset -c=wrong")
+        }
+    }
+}


Mime
View raw message