Skip to content

Commit b18a6f5

Browse files
committedMar 17, 2025
CASSANDRA-20362: add setcacheperiod to nodetool
1 parent ca04d67 commit b18a6f5

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
 

‎src/java/org/apache/cassandra/tools/NodeProbe.java

+8
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,14 @@ public void setCacheKeysToSave(int keyCacheKeysToSave, int rowCacheKeysToSave, i
11441144
cacheMBean.setCounterCacheKeysToSave(counterCacheKeysToSave);
11451145
}
11461146

1147+
public void setCachePeriod(int keyCachePeriod, int rowCachePeriod, int counterPeriod)
1148+
{
1149+
CacheServiceMBean cacheMBean = getCacheServiceMBean();
1150+
cacheMBean.setKeyCacheSavePeriodInSeconds(keyCachePeriod);
1151+
cacheMBean.setRowCacheSavePeriodInSeconds(rowCachePeriod);
1152+
cacheMBean.setCounterCacheSavePeriodInSeconds(counterPeriod);
1153+
}
1154+
11471155
public void setHintedHandoffThrottleInKB(int throttleInKB)
11481156
{
11491157
ssProxy.setHintedHandoffThrottleInKB(throttleInKB);

‎src/java/org/apache/cassandra/tools/NodeTool.java

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public int execute(String... args)
202202
SetBatchlogReplayThrottle.class,
203203
SetCacheCapacity.class,
204204
SetCacheKeysToSave.class,
205+
SetCachePeriod.class,
205206
SetColumnIndexSize.class,
206207
SetCompactionThreshold.class,
207208
SetCompactionThroughput.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.cassandra.tools.nodetool;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import io.airlift.airline.Arguments;
24+
import io.airlift.airline.Command;
25+
import org.apache.cassandra.tools.NodeProbe;
26+
import org.apache.cassandra.tools.NodeTool.NodeToolCmd;
27+
28+
import static com.google.common.base.Preconditions.checkArgument;
29+
30+
@Command(name = "setcacheperiod", description = "Set global key, row, and counter cache period in second units")
31+
public class SetCachePeriod extends NodeToolCmd
32+
{
33+
@Arguments(title = "<key-cache-period> <row-cache-period> <counter-cache-period>",
34+
usage = "<key-cache-period> <row-cache-period> <counter-cache-period>",
35+
description = "Key cache, row cache, and counter period in second units",
36+
required = true)
37+
private List<Integer> args = new ArrayList<>();
38+
39+
@Override
40+
public void execute(NodeProbe probe)
41+
{
42+
checkArgument(args.size() == 3, "setcacheperiod requires key-cache-period, row-cache-period, and counter-cache-period args.");
43+
probe.setCachePeriod(args.get(0), args.get(1), args.get(2));
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.cassandra.tools.nodetool;
19+
20+
import org.junit.BeforeClass;
21+
import org.junit.Test;
22+
23+
import org.apache.cassandra.cql3.CQLTester;
24+
import org.apache.cassandra.service.CacheService;
25+
import org.apache.cassandra.tools.ToolRunner;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
public class SetCachePeriodTest extends CQLTester
30+
{
31+
private static CacheService cacheService;
32+
33+
@BeforeClass
34+
public static void setup() throws Exception
35+
{
36+
requireNetwork();
37+
startJMXServer();
38+
cacheService = CacheService.instance;
39+
}
40+
41+
@Test
42+
public void testHelp()
43+
{
44+
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("help", "setcacheperiod");
45+
tool.assertOnExitCode();
46+
47+
String help = "NAME\n" +
48+
" nodetool setcacheperiod - Set global key, row, and counter cache period\n" +
49+
" in second units\n" +
50+
"\n" +
51+
"SYNOPSIS\n" +
52+
" nodetool [(-h <host> | --host <host>)] [(-p <port> | --port <port>)]\n" +
53+
" [(-pp | --print-port)] [(-pw <password> | --password <password>)]\n" +
54+
" [(-pwf <passwordFilePath> | --password-file <passwordFilePath>)]\n" +
55+
" [(-u <username> | --username <username>)] setcacheperiod [--]\n" +
56+
" <key-cache-period> <row-cache-period> <counter-cache-period>\n" +
57+
"\n" +
58+
"OPTIONS\n" +
59+
" -h <host>, --host <host>\n" +
60+
" Node hostname or ip address\n" +
61+
"\n" +
62+
" -p <port>, --port <port>\n" +
63+
" Remote jmx agent port number\n" +
64+
"\n" +
65+
" -pp, --print-port\n" +
66+
" Operate in 4.0 mode with hosts disambiguated by port number\n" +
67+
"\n" +
68+
" -pw <password>, --password <password>\n" +
69+
" Remote jmx agent password\n" +
70+
"\n" +
71+
" -pwf <passwordFilePath>, --password-file <passwordFilePath>\n" +
72+
" Path to the JMX password file\n" +
73+
"\n" +
74+
" -u <username>, --username <username>\n" +
75+
" Remote jmx agent username\n" +
76+
"\n" +
77+
" --\n" +
78+
" This option can be used to separate command-line options from the\n" +
79+
" list of argument, (useful when arguments might be mistaken for\n" +
80+
" command-line options\n" +
81+
"\n" +
82+
" <key-cache-period> <row-cache-period> <counter-cache-period>\n" +
83+
" Key cache, row cache, and counter period in second units\n" +
84+
"\n" +
85+
"\n";
86+
assertThat(tool.getStdout()).isEqualTo(help);
87+
}
88+
89+
@Test
90+
public void testOptionalParameters()
91+
{
92+
int keyPeriodSetting = 333;
93+
int rowPeriodSetting = 444;
94+
int counterPeriodSetting = 555;
95+
ToolRunner.ToolResult tool = ToolRunner.invokeNodetool("setcacheperiod",
96+
String.valueOf(keyPeriodSetting),
97+
String.valueOf(rowPeriodSetting),
98+
String.valueOf(counterPeriodSetting));
99+
tool.assertOnCleanExit();
100+
assertThat(tool.getStdout()).isEmpty();
101+
102+
assertThat(cacheService.getKeyCacheSavePeriodInSeconds()).isEqualTo(keyPeriodSetting);
103+
assertThat(cacheService.getRowCacheSavePeriodInSeconds()).isEqualTo(rowPeriodSetting);
104+
assertThat(cacheService.getCounterCacheSavePeriodInSeconds()).isEqualTo(counterPeriodSetting);
105+
}
106+
}

0 commit comments

Comments
 (0)