Skip to content

Commit 80a9de6

Browse files
committed
nodetool shell script should respect management cql port
1 parent 6433c6e commit 80a9de6

2 files changed

Lines changed: 55 additions & 32 deletions

File tree

bin/nodetool

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,51 @@ if [ -z "$CASSANDRA_CONF" -o -z "$CLASSPATH" ]; then
4343
exit 1
4444
fi
4545

46-
JMX_PORT=""
47-
48-
# Try to parse port from configure_jmx method call, when not commented out
49-
if [ -f "$CASSANDRA_CONF/cassandra-env.sh" ]; then
50-
jmx_method_call=$(grep "^configure_jmx \+[0-9]\+$" "$CASSANDRA_CONF/cassandra-env.sh")
51-
if [ ! "x${jmx_method_call}" = "x" ]; then
52-
JMX_PORT=$(echo "${jmx_method_call}" | tr -s " " | cut -d " " -f2)
46+
# Check if protocol is cql (from environment variable)
47+
PROTOCOL_IS_CQL=""
48+
if [ "x$CASSANDRA_CLI_EXECUTION_PROTOCOL" != "x" ]; then
49+
protocol_lower=$(echo "$CASSANDRA_CLI_EXECUTION_PROTOCOL" | tr '[:upper:]' '[:lower:]')
50+
if [ "$protocol_lower" = "cql" ]; then
51+
PROTOCOL_IS_CQL="true"
5352
fi
5453
fi
5554

56-
# In case JMX_PORT is not set (when configure_jmx in cassandra-env.sh is commented out),
57-
# try to parse it from cassandra.yaml.
58-
if [ "x$JMX_PORT" = "x" ]; then
55+
# Check for cql protocol in system properties to determine if we should parse CQL or JMX port
56+
for arg in "$@"; do
57+
case "$arg" in
58+
-Dcassandra.cli.execution.protocol=cql|-Dcassandra.cli.execution.protocol=CQL)
59+
PROTOCOL_IS_CQL="true"
60+
break
61+
;;
62+
esac
63+
done
64+
65+
# Parse port from config files based on protocol
66+
CONNECTION_PORT=""
67+
if [ "$PROTOCOL_IS_CQL" = "true" ]; then
68+
# For CQL, parse native_transport_management_port from cassandra.yaml
5969
if [ -f "$CASSANDRA_CONF/cassandra.yaml" ]; then
60-
JMX_PORT=$(grep jmx_port "$CASSANDRA_CONF/cassandra.yaml" | cut -d ':' -f 2 | tr -d '[[:space:]]')
70+
CONNECTION_PORT=$(grep native_transport_management_port "$CASSANDRA_CONF/cassandra.yaml" | cut -d ':' -f 2 | tr -d '[[:space:]]')
71+
fi
72+
else
73+
# For JMX, parse port from configure_jmx method call, when not commented out
74+
if [ -f "$CASSANDRA_CONF/cassandra-env.sh" ]; then
75+
jmx_method_call=$(grep "^configure_jmx \+[0-9]\+$" "$CASSANDRA_CONF/cassandra-env.sh")
76+
if [ ! "x${jmx_method_call}" = "x" ]; then
77+
CONNECTION_PORT=$(echo "${jmx_method_call}" | tr -s " " | cut -d " " -f2)
78+
fi
6179
fi
62-
fi
6380

64-
# If, by any chance, it is not there either, set it to default.
65-
if [ "x$JMX_PORT" = "x" ]; then
66-
JMX_PORT=7199
81+
# In case CONNECTION_PORT is not set (when configure_jmx in cassandra-env.sh is commented out),
82+
# try to parse it from cassandra.yaml.
83+
if [ "x$CONNECTION_PORT" = "x" ]; then
84+
if [ -f "$CASSANDRA_CONF/cassandra.yaml" ]; then
85+
CONNECTION_PORT=$(grep jmx_port "$CASSANDRA_CONF/cassandra.yaml" | cut -d ':' -f 2 | tr -d '[[:space:]]')
86+
fi
87+
fi
6788
fi
6889

69-
# JMX Port passed via cmd line args (-p 9999 / --port 9999 / --port=9999)
90+
# Connection Port passed via cmd line args (-p 9999 / --port 9999 / --port=9999)
7091
# should override the value from cassandra-env.sh
7192
ARGS=""
7293
JVM_ARGS=""
@@ -76,19 +97,19 @@ do
7697
if [ ! $1 ]; then break; fi
7798
case $1 in
7899
-p)
79-
JMX_PORT=$2
100+
CONNECTION_PORT=$2
80101
shift
81102
;;
82103
--port=*)
83-
JMX_PORT=$(echo $1 | cut -d '=' -f 2)
104+
CONNECTION_PORT=$(echo $1 | cut -d '=' -f 2)
84105
;;
85106
--port)
86-
JMX_PORT=$2
107+
CONNECTION_PORT=$2
87108
shift
88109
;;
89110
--ssl)
90111
if [ -f $SSL_FILE ]
91-
then
112+
then
92113
SSL_ARGS=$(cat $SSL_FILE | tr '\n' ' ')
93114
fi
94115
JVM_ARGS="$JVM_ARGS -Dssl.enable=true $SSL_ARGS"
@@ -108,6 +129,14 @@ do
108129
shift
109130
done
110131

132+
if [ "x$CONNECTION_PORT" = "x" ]; then
133+
if [ "$PROTOCOL_IS_CQL" = "true" ]; then
134+
CONNECTION_PORT=11211
135+
else
136+
CONNECTION_PORT=7199
137+
fi
138+
fi
139+
111140
if [ "x$MAX_HEAP_SIZE" = "x" ]; then
112141
MAX_HEAP_SIZE="128m"
113142
fi
@@ -123,7 +152,7 @@ CMD=$(echo "$JAVA" $JAVA_AGENT -ea -cp "$CLASSPATH" $JVM_OPTS -Xmx$MAX_HEAP_SIZE
123152
-Dcassandra.logdir="$CASSANDRA_LOG_DIR" \
124153
-Dlogback.configurationFile=logback-tools.xml \
125154
$JVM_ARGS \
126-
org.apache.cassandra.tools.NodeTool -p $JMX_PORT $ARGS)
155+
org.apache.cassandra.tools.NodeTool -p $CONNECTION_PORT $ARGS)
127156

128157
if [ "x$ARCHIVE_COMMAND" != "x" ]
129158
then

src/java/org/apache/cassandra/tools/nodetool/CqlConnect.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@
3131
import picocli.CommandLine.Option;
3232
import picocli.CommandLine.Spec;
3333

34-
import static java.lang.Integer.parseInt;
35-
3634
/**
3735
* Command options for NodeTool commands that are executed via CQL.
3836
*/
3937
@Command(name = "cqlconnect", description = "Connect to a Cassandra node via CQL")
4038
public class CqlConnect extends AbstractCommand implements AutoCloseable
4139
{
42-
private static final String DEFAULT_CQL_PORT = "11211";
40+
private static final int DEFAULT_CQL_PORT = 11211;
4341

4442
/** The command specification, used to access command-specific properties. */
4543
@Spec
@@ -48,11 +46,8 @@ public class CqlConnect extends AbstractCommand implements AutoCloseable
4846
@Option(names = { "-h", "--host" }, description = "Node hostname or ip address", arity = "0..1")
4947
private String host = "127.0.0.1";
5048

51-
@Option(names = { "-p", "--port" },
52-
description = "Remote CQL native transport port number",
53-
arity = "0..1",
54-
defaultValue = DEFAULT_CQL_PORT)
55-
private String port;
49+
@Option(names = { "-p", "--port" }, description = "Remote CQL native transport port number", arity = "0..1")
50+
private int port = DEFAULT_CQL_PORT;
5651

5752
private volatile SimpleClient client;
5853

@@ -66,9 +61,8 @@ public void run()
6661

6762
try
6863
{
69-
String portToUse = port == null || port.isEmpty() ? DEFAULT_CQL_PORT : port;
70-
SimpleClient.Builder builder = SimpleClient.builder(host, parseInt(portToUse))
71-
.protocolVersion(ProtocolVersion.V5);
64+
output.printInfo("Connecting to %s:%s via CQL...%n", host, port);
65+
SimpleClient.Builder builder = SimpleClient.builder(host, port).protocolVersion(ProtocolVersion.V5);
7266
client = builder.build();
7367
client.connect(false);
7468
}

0 commit comments

Comments
 (0)