This is an automated email from the ASF dual-hosted git repository.

smiklosovic pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 230fe8e647 Include Git SHA in --verbose flag for nodetool version
230fe8e647 is described below

commit 230fe8e64722ac02dbf8cdafb7d4fef120726dd7
Author: Abe Ratnofsky <[email protected]>
AuthorDate: Fri Feb 4 11:15:42 2022 -0800

    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
---
 .build/build-git.xml                               | 54 ++++++++++++++++++++++
 CHANGES.txt                                        |  1 +
 build.xml                                          |  7 ++-
 conf/cassandra.yaml                                |  1 +
 .../apache/cassandra/service/StorageService.java   |  7 +++
 .../cassandra/service/StorageServiceMBean.java     |  6 +++
 src/java/org/apache/cassandra/tools/NodeProbe.java |  5 ++
 .../apache/cassandra/tools/nodetool/Version.java   |  9 +++-
 .../org/apache/cassandra/utils/FBUtilities.java    | 25 ++++++++--
 .../cassandra/distributed/test/NodeToolTest.java   | 14 ++++++
 10 files changed, 122 insertions(+), 7 deletions(-)

diff --git a/.build/build-git.xml b/.build/build-git.xml
new file mode 100644
index 0000000000..03b5ca7f5b
--- /dev/null
+++ b/.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>
diff --git a/CHANGES.txt b/CHANGES.txt
index a51b81d926..5a4dcba477 100644
--- a/CHANGES.txt
+++ b/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)
diff --git a/build.xml b/build.xml
index 88ffc8836b..ffb107c7b1 100644
--- a/build.xml
+++ b/build.xml
@@ -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>
diff --git a/conf/cassandra.yaml b/conf/cassandra.yaml
index 93a581b058..17c994bbb1 100644
--- a/conf/cassandra.yaml
+++ b/conf/cassandra.yaml
@@ -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
+
diff --git a/src/java/org/apache/cassandra/service/StorageService.java 
b/src/java/org/apache/cassandra/service/StorageService.java
index 1d0ccc0866..b4022ba8a7 100644
--- a/src/java/org/apache/cassandra/service/StorageService.java
+++ b/src/java/org/apache/cassandra/service/StorageService.java
@@ -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();
diff --git a/src/java/org/apache/cassandra/service/StorageServiceMBean.java 
b/src/java/org/apache/cassandra/service/StorageServiceMBean.java
index da77fcf286..101c9a3ebf 100644
--- a/src/java/org/apache/cassandra/service/StorageServiceMBean.java
+++ b/src/java/org/apache/cassandra/service/StorageServiceMBean.java
@@ -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.
diff --git a/src/java/org/apache/cassandra/tools/NodeProbe.java 
b/src/java/org/apache/cassandra/tools/NodeProbe.java
index b1336ddd5d..9da34350f4 100644
--- a/src/java/org/apache/cassandra/tools/NodeProbe.java
+++ b/src/java/org/apache/cassandra/tools/NodeProbe.java
@@ -780,6 +780,11 @@ public class NodeProbe implements AutoCloseable
         return ssProxy.getReleaseVersion();
     }
 
+    public String getGitSHA()
+    {
+        return ssProxy.getGitSHA();
+    }
+
     public int getCurrentGenerationNumber()
     {
         return ssProxy.getCurrentGenerationNumber();
diff --git a/src/java/org/apache/cassandra/tools/nodetool/Version.java 
b/src/java/org/apache/cassandra/tools/nodetool/Version.java
index f95907a5cc..6556a04627 100644
--- a/src/java/org/apache/cassandra/tools/nodetool/Version.java
+++ b/src/java/org/apache/cassandra/tools/nodetool/Version.java
@@ -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());
     }
 }
diff --git a/src/java/org/apache/cassandra/utils/FBUtilities.java 
b/src/java/org/apache/cassandra/utils/FBUtilities.java
index 02666ac6cb..c279e8b54c 100644
--- a/src/java/org/apache/cassandra/utils/FBUtilities.java
+++ b/src/java/org/apache/cassandra/utils/FBUtilities.java
@@ -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();
diff --git 
a/test/distributed/org/apache/cassandra/distributed/test/NodeToolTest.java 
b/test/distributed/org/apache/cassandra/distributed/test/NodeToolTest.java
index 486d9f5618..24a65e3d4e 100644
--- a/test/distributed/org/apache/cassandra/distributed/test/NodeToolTest.java
+++ b/test/distributed/org/apache/cassandra/distributed/test/NodeToolTest.java
@@ -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:");
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to