Skip to content

Commit

Permalink
Include Git SHA in --verbose flag for nodetool version
Browse files Browse the repository at this point in the history
Patch by Abe Ratnofsky; review by Brandon Williams, Caleb Rackliffe, Michael Semb Wever and Stefan Miklosovic for CASSANDRA-17753
  • Loading branch information
aratno authored and smiklosovic committed Oct 25, 2022
1 parent afdf567 commit 230fe8e
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 7 deletions.
54 changes: 54 additions & 0 deletions .build/build-git.xml
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
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.
-->
<project basedir="." name="apache-cassandra-git-tasks"
xmlns:if="ant:if">
<target name="get-git-sha">
<exec executable="git" osfamily="unix" dir="${basedir}" logError="false" failonerror="false" failifexecutionfails="false" resultproperty="git.is-available.exit-code">
<arg value="rev-parse"/>
<arg value="--is-inside-work-tree"/>
<redirector outputproperty="git.is-available.output"/>
</exec>
<condition property="git.is-available" else="false">
<equals arg1="${git.is-available.exit-code}" arg2="0"/>
</condition>
<echo message="git.is-available=${git.is-available}"/>

<exec if:true="${git.is-available}" executable="git" osfamily="unix" dir="${basedir}" logError="true" failonerror="false" failifexecutionfails="false">
<arg value="describe"/>
<arg value="--match=''"/>
<arg value="--always"/>
<arg value="--abbrev=0"/>
<arg value="--dirty"/>
<redirector outputproperty="git.sha"/>
</exec>
<property name="git.sha" value="Unknown"/>
<echo level="info">git.sha=${git.sha}</echo>

<exec if:true="${git.is-available}" executable="git" osfamily="unix" dir="${basedir}" logError="true" failonerror="false" failifexecutionfails="false">
<arg value="diff"/>
<arg value="--stat"/>
<redirector outputproperty="git.diffstat"/>
</exec>
<condition property="is-dirty">
<contains string="${git.sha}" substring="-dirty"/>
</condition>
<echo level="warning" if:true="${is-dirty}">Repository state is dirty</echo>
<echo level="warning" if:true="${is-dirty}">${git.diffstat}</echo>
</target>
</project>
1 change: 1 addition & 0 deletions CHANGES.txt
@@ -1,4 +1,5 @@
4.2
* Include Git SHA in --verbose flag for nodetool version (CASSANDRA-17753)
* Update Byteman to 4.0.20 and Jacoco to 0.8.8 (CASSANDRA-16413)
* Add memtable option among possible tab completions for a table (CASSANDRA-17982)
* Adds a trie-based memtable implementation (CASSANDRA-17240)
Expand Down
7 changes: 5 additions & 2 deletions build.xml
Expand Up @@ -438,11 +438,12 @@
</target>

<!-- create properties file with C version -->
<target name="createVersionPropFile">
<target name="createVersionPropFile" depends="get-git-sha">
<taskdef name="propertyfile" classname="org.apache.tools.ant.taskdefs.optional.PropertyFile"/>
<mkdir dir="${version.properties.dir}"/>
<propertyfile file="${version.properties.dir}/version.properties">
<entry key="CassandraVersion" value="${version}"/>
<entry key="GitSHA" value="${git.sha}"/>
</propertyfile>
</target>

Expand Down Expand Up @@ -485,7 +486,7 @@
</javac>
</target>

<target depends="init,gen-cql3-grammar,generate-cql-html,generate-jflex-java,rat-check"
<target depends="init,gen-cql3-grammar,generate-cql-html,generate-jflex-java,rat-check,get-git-sha"
name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<!-- Order matters! -->
Expand Down Expand Up @@ -695,6 +696,7 @@
<attribute name="Implementation-Title" value="Cassandra"/>
<attribute name="Implementation-Version" value="${version}"/>
<attribute name="Implementation-Vendor" value="Apache"/>
<attribute name="Implementation-Git-SHA" value="${git.sha}"/>
<!-- </section> -->
</manifest>
</jar>
Expand Down Expand Up @@ -1972,4 +1974,5 @@
<import file="${basedir}/.build/build-resolver.xml"/>
<import file="${basedir}/.build/build-rat.xml"/>
<import file="${basedir}/.build/build-owasp.xml"/>
<import file="${basedir}/.build/build-git.xml"/>
</project>
1 change: 1 addition & 0 deletions conf/cassandra.yaml
Expand Up @@ -1881,3 +1881,4 @@ drop_compact_storage_enabled: false
# heartbeat_file: /var/lib/cassandra/data/cassandra-heartbeat
# excluded_keyspaces: # comma separated list of keyspaces to exclude from the check
# excluded_tables: # comma separated list of keyspace.table pairs to exclude from the check

7 changes: 7 additions & 0 deletions src/java/org/apache/cassandra/service/StorageService.java
Expand Up @@ -788,6 +788,7 @@ public synchronized void initServer(int schemaAndRingDelayMillis) throws Configu
public synchronized void initServer(int schemaTimeoutMillis, int ringTimeoutMillis) throws ConfigurationException
{
logger.info("Cassandra version: {}", FBUtilities.getReleaseVersionString());
logger.info("Git SHA: {}", FBUtilities.getGitSHA());
logger.info("CQL version: {}", QueryProcessor.CQL_VERSION);
logger.info("Native protocol supported versions: {} (default: {})",
StringUtils.join(ProtocolVersion.supportedVersions(), ", "), ProtocolVersion.CURRENT);
Expand Down Expand Up @@ -3659,6 +3660,12 @@ public String getReleaseVersion()
return FBUtilities.getReleaseVersionString();
}

@Override
public String getGitSHA()
{
return FBUtilities.getGitSHA();
}

public String getSchemaVersion()
{
return Schema.instance.getVersion().toString();
Expand Down
Expand Up @@ -102,6 +102,12 @@ public interface StorageServiceMBean extends NotificationEmitter
*/
public String getReleaseVersion();

/**
* Fetch a string representation of the Cassandra git SHA.
* @return A string representation of the Cassandra git SHA.
*/
public String getGitSHA();

/**
* Fetch a string representation of the current Schema version.
* @return A string representation of the Schema version.
Expand Down
5 changes: 5 additions & 0 deletions src/java/org/apache/cassandra/tools/NodeProbe.java
Expand Up @@ -780,6 +780,11 @@ public String getReleaseVersion()
return ssProxy.getReleaseVersion();
}

public String getGitSHA()
{
return ssProxy.getGitSHA();
}

public int getCurrentGenerationNumber()
{
return ssProxy.getCurrentGenerationNumber();
Expand Down
9 changes: 8 additions & 1 deletion src/java/org/apache/cassandra/tools/nodetool/Version.java
Expand Up @@ -18,16 +18,23 @@
package org.apache.cassandra.tools.nodetool;

import io.airlift.airline.Command;

import io.airlift.airline.Option;
import org.apache.cassandra.tools.NodeProbe;
import org.apache.cassandra.tools.NodeTool.NodeToolCmd;

@Command(name = "version", description = "Print cassandra version")
public class Version extends NodeToolCmd
{
@Option(title = "verbose",
name = {"-v", "--verbose"},
description = "Include additional information")
private boolean verbose = false;

@Override
public void execute(NodeProbe probe)
{
probe.output().out.println("ReleaseVersion: " + probe.getReleaseVersion());
if (verbose)
probe.output().out.println("GitSHA: " + probe.getGitSHA());
}
}
25 changes: 21 additions & 4 deletions src/java/org/apache/cassandra/utils/FBUtilities.java
Expand Up @@ -106,6 +106,7 @@ public class FBUtilities

private static final Logger logger = LoggerFactory.getLogger(FBUtilities.class);
public static final String UNKNOWN_RELEASE_VERSION = "Unknown";
public static final String UNKNOWN_GIT_SHA = "Unknown";

public static final BigInteger TWO = new BigInteger("2");
private static final String DEFAULT_TRIGGER_DIR = "triggers";
Expand Down Expand Up @@ -422,26 +423,42 @@ public static String getPreviousReleaseVersionString()
return previousReleaseVersionString;
}

public static String getReleaseVersionString()
private static Properties getVersionProperties()
{
try (InputStream in = FBUtilities.class.getClassLoader().getResourceAsStream("org/apache/cassandra/config/version.properties"))
{
if (in == null)
{
return System.getProperty("cassandra.releaseVersion", UNKNOWN_RELEASE_VERSION);
return null;
}
Properties props = new Properties();
props.load(in);
return props.getProperty("CassandraVersion");
return props;
}
catch (Exception e)
{
JVMStabilityInspector.inspectThrowable(e);
logger.warn("Unable to load version.properties", e);
return "debug version";
return null;
}
}

public static String getReleaseVersionString()
{
Properties props = getVersionProperties();
if (props == null)
return System.getProperty("cassandra.releaseVersion", UNKNOWN_RELEASE_VERSION);
return props.getProperty("CassandraVersion");
}

public static String getGitSHA()
{
Properties props = getVersionProperties();
if (props == null)
return System.getProperty("cassandra.gitSHA", UNKNOWN_GIT_SHA);
return props.getProperty("GitSHA", UNKNOWN_GIT_SHA);
}

public static String getReleaseVersionMajor()
{
String releaseVersion = FBUtilities.getReleaseVersionString();
Expand Down
Expand Up @@ -133,4 +133,18 @@ public void testInfoOutput() throws Throwable
ringResult.asserts().stdoutContains("Heap Memory");
}
}

@Test
public void testVersionIncludesGitSHAWhenVerbose() throws Throwable
{
NODE.nodetoolResult("version")
.asserts()
.success()
.stdoutNotContains("GitSHA:");

NODE.nodetoolResult("version", "--verbose")
.asserts()
.success()
.stdoutContains("GitSHA:");
}
}

0 comments on commit 230fe8e

Please sign in to comment.