Skip to content

Commit

Permalink
CASSANDRA-17753: Include GitSHA in nodetool version output
Browse files Browse the repository at this point in the history
$ # When Cassandra is built with a clean Git index
$ ./bin/nodetool version
ReleaseVersion: 4.1-alpha2-SNAPSHOT
GitSHA: 5fce07e2f1e2e0b9cf7d82adfd8c0b21993f8672

$ # When Cassandra is built with a dirty Git index
$ ./bin/nodetool version
ReleaseVersion: 4.1-alpha2-SNAPSHOT
GitSHA: 5fce07e2f1e2e0b9cf7d82adfd8c0b21993f8672-dirty
  • Loading branch information
aratno committed Jul 13, 2022
1 parent 5aec5a9 commit d09951f
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -15,6 +15,7 @@
* Add new CQL function maxWritetime (CASSANDRA-17425)
* Add guardrail for ALTER TABLE ADD / DROP / REMOVE column operations (CASSANDRA-17495)
* Rename DisableFlag class to EnableFlag on guardrails (CASSANDRA-17544)
* `nodetool version` now includes GitSHA
Merged from 4.1:
* Remove commons-lang dependency during build runtime (CASSANDRA-17724)
* Relax synchronization on StreamSession#onError() to avoid deadlock (CASSANDRA-17706)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.txt
Expand Up @@ -70,6 +70,8 @@ New features
- Whether ALTER TABLE commands are allowed to mutate columns
- Whether SimpleStrategy is allowed on keyspace creation or alteration
- Maximum replication factor
- `nodetool version` now includes the Git SHA the project was built with, and whether it was built with a dirty Git
index; build now depends on a local Git installation

Upgrading
---------
Expand Down
30 changes: 28 additions & 2 deletions build.xml
Expand Up @@ -881,12 +881,37 @@
<echo message="${base.version}" />
</target>

<target name="get-git-sha">
<exec 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 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>

<!-- 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 @@ -929,7 +954,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 @@ -1126,6 +1151,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
1 change: 1 addition & 0 deletions conf/cassandra.yaml
Expand Up @@ -1843,3 +1843,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

6 changes: 6 additions & 0 deletions src/java/org/apache/cassandra/service/StorageService.java
Expand Up @@ -3550,6 +3550,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 @@ -747,6 +747,11 @@ public String getReleaseVersion()
return ssProxy.getReleaseVersion();
}

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

public int getCurrentGenerationNumber()
{
return ssProxy.getCurrentGenerationNumber();
Expand Down
1 change: 1 addition & 0 deletions src/java/org/apache/cassandra/tools/nodetool/Version.java
Expand Up @@ -29,5 +29,6 @@ public class Version extends NodeToolCmd
public void execute(NodeProbe probe)
{
probe.output().out.println("ReleaseVersion: " + probe.getReleaseVersion());
probe.output().out.println("GitSHA: " + probe.getGitSHA());
}
}
25 changes: 21 additions & 4 deletions src/java/org/apache/cassandra/utils/FBUtilities.java
Expand Up @@ -105,6 +105,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 @@ -419,26 +420,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 @@ -116,4 +116,13 @@ public void testSetCacheCapacityWhenDisabled() throws Throwable
ringResult.asserts().stderrContains("is not permitted as this cache is disabled");
}
}

@Test
public void testInfoIncludesGitSHA() throws Throwable
{
NODE.nodetoolResult("version")
.asserts()
.success()
.stdoutContains("GitSHA:");
}
}

0 comments on commit d09951f

Please sign in to comment.