diff --git a/src/java/org/apache/cassandra/tools/NodeProbe.java b/src/java/org/apache/cassandra/tools/NodeProbe.java index 747da8348564..b21b4d1dac0c 100644 --- a/src/java/org/apache/cassandra/tools/NodeProbe.java +++ b/src/java/org/apache/cassandra/tools/NodeProbe.java @@ -1144,6 +1144,14 @@ public void setCacheKeysToSave(int keyCacheKeysToSave, int rowCacheKeysToSave, i cacheMBean.setCounterCacheKeysToSave(counterCacheKeysToSave); } + public void setCachePeriod(int keyCachePeriod, int rowCachePeriod, int counterPeriod) + { + CacheServiceMBean cacheMBean = getCacheServiceMBean(); + cacheMBean.setKeyCacheSavePeriodInSeconds(keyCachePeriod); + cacheMBean.setRowCacheSavePeriodInSeconds(rowCachePeriod); + cacheMBean.setCounterCacheSavePeriodInSeconds(counterPeriod); + } + public void setHintedHandoffThrottleInKB(int throttleInKB) { ssProxy.setHintedHandoffThrottleInKB(throttleInKB); diff --git a/src/java/org/apache/cassandra/tools/NodeTool.java b/src/java/org/apache/cassandra/tools/NodeTool.java index 49be441b89a6..4a90707edc21 100644 --- a/src/java/org/apache/cassandra/tools/NodeTool.java +++ b/src/java/org/apache/cassandra/tools/NodeTool.java @@ -202,6 +202,7 @@ public int execute(String... args) SetBatchlogReplayThrottle.class, SetCacheCapacity.class, SetCacheKeysToSave.class, + SetCachePeriod.class, SetColumnIndexSize.class, SetCompactionThreshold.class, SetCompactionThroughput.class, diff --git a/src/java/org/apache/cassandra/tools/nodetool/SetCachePeriod.java b/src/java/org/apache/cassandra/tools/nodetool/SetCachePeriod.java new file mode 100644 index 000000000000..bbadf277e9e0 --- /dev/null +++ b/src/java/org/apache/cassandra/tools/nodetool/SetCachePeriod.java @@ -0,0 +1,45 @@ +/* + * 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.cassandra.tools.nodetool; + +import java.util.ArrayList; +import java.util.List; + +import io.airlift.airline.Arguments; +import io.airlift.airline.Command; +import org.apache.cassandra.tools.NodeProbe; +import org.apache.cassandra.tools.NodeTool.NodeToolCmd; + +import static com.google.common.base.Preconditions.checkArgument; + +@Command(name = "setcacheperiod", description = "Set global key, row, and counter cache period in second units") +public class SetCachePeriod extends NodeToolCmd +{ + @Arguments(title = " ", + usage = " ", + description = "Key cache, row cache, and counter period in second units", + required = true) + private List args = new ArrayList<>(); + + @Override + public void execute(NodeProbe probe) + { + checkArgument(args.size() == 3, "setcacheperiod requires key-cache-period, row-cache-period, and counter-cache-period args."); + probe.setCachePeriod(args.get(0), args.get(1), args.get(2)); + } +} diff --git a/test/unit/org/apache/cassandra/tools/nodetool/SetCachePeriodTest.java b/test/unit/org/apache/cassandra/tools/nodetool/SetCachePeriodTest.java new file mode 100644 index 000000000000..85e861e738dd --- /dev/null +++ b/test/unit/org/apache/cassandra/tools/nodetool/SetCachePeriodTest.java @@ -0,0 +1,106 @@ +/* + * 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.cassandra.tools.nodetool; + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.service.CacheService; +import org.apache.cassandra.tools.ToolRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SetCachePeriodTest extends CQLTester +{ + private static CacheService cacheService; + + @BeforeClass + public static void setup() throws Exception + { + requireNetwork(); + startJMXServer(); + cacheService = CacheService.instance; + } + + @Test + public void testHelp() + { + ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("help", "setcacheperiod"); + tool.assertOnExitCode(); + + String help = "NAME\n" + + " nodetool setcacheperiod - Set global key, row, and counter cache period\n" + + " in second units\n" + + "\n" + + "SYNOPSIS\n" + + " nodetool [(-h | --host )] [(-p | --port )]\n" + + " [(-pp | --print-port)] [(-pw | --password )]\n" + + " [(-pwf | --password-file )]\n" + + " [(-u | --username )] setcacheperiod [--]\n" + + " \n" + + "\n" + + "OPTIONS\n" + + " -h , --host \n" + + " Node hostname or ip address\n" + + "\n" + + " -p , --port \n" + + " Remote jmx agent port number\n" + + "\n" + + " -pp, --print-port\n" + + " Operate in 4.0 mode with hosts disambiguated by port number\n" + + "\n" + + " -pw , --password \n" + + " Remote jmx agent password\n" + + "\n" + + " -pwf , --password-file \n" + + " Path to the JMX password file\n" + + "\n" + + " -u , --username \n" + + " Remote jmx agent username\n" + + "\n" + + " --\n" + + " This option can be used to separate command-line options from the\n" + + " list of argument, (useful when arguments might be mistaken for\n" + + " command-line options\n" + + "\n" + + " \n" + + " Key cache, row cache, and counter period in second units\n" + + "\n" + + "\n"; + assertThat(tool.getStdout()).isEqualTo(help); + } + + @Test + public void testOptionalParameters() + { + int keyPeriodSetting = 333; + int rowPeriodSetting = 444; + int counterPeriodSetting = 555; + ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("setcacheperiod", + String.valueOf(keyPeriodSetting), + String.valueOf(rowPeriodSetting), + String.valueOf(counterPeriodSetting)); + tool.assertOnCleanExit(); + assertThat(tool.getStdout()).isEmpty(); + + assertThat(cacheService.getKeyCacheSavePeriodInSeconds()).isEqualTo(keyPeriodSetting); + assertThat(cacheService.getRowCacheSavePeriodInSeconds()).isEqualTo(rowPeriodSetting); + assertThat(cacheService.getCounterCacheSavePeriodInSeconds()).isEqualTo(counterPeriodSetting); + } +}