Include Git SHA in --verbose flag for nodetool version

Patch by Abe Ratnofsky; review by Brandon Williams, Caleb Rackliffe, Michael Semb Wever and Stefan Miklosovic for CASSANDRA-17753
This commit is contained in:
Abe Ratnofsky 2022-02-04 11:15:42 -08:00 committed by Stefan Miklosovic
parent afdf567d53
commit 230fe8e647
10 changed files with 122 additions and 7 deletions

54
.build/build-git.xml Normal file
View File

@ -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>

View File

@ -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)

View File

@ -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>
@ -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! -->
@ -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>
@ -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>

View File

@ -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

View File

@ -788,6 +788,7 @@ public class StorageService extends NotificationBroadcasterSupport implements IE
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);
@ -3659,6 +3660,12 @@ public class StorageService extends NotificationBroadcasterSupport implements IE
return FBUtilities.getReleaseVersionString();
}
@Override
public String getGitSHA()
{
return FBUtilities.getGitSHA();
}
public String getSchemaVersion()
{
return Schema.instance.getVersion().toString();

View File

@ -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.

View File

@ -780,6 +780,11 @@ public class NodeProbe implements AutoCloseable
return ssProxy.getReleaseVersion();
}
public String getGitSHA()
{
return ssProxy.getGitSHA();
}
public int getCurrentGenerationNumber()
{
return ssProxy.getCurrentGenerationNumber();

View File

@ -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());
}
}

View File

@ -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";
@ -422,26 +423,42 @@ public class FBUtilities
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();

View File

@ -133,4 +133,18 @@ public class NodeToolTest extends TestBaseImpl
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:");
}
}