zentol commented on a change in pull request #11592:
URL: https://github.com/apache/flink/pull/11592#discussion_r416581839
##########
File path:
flink-runtime/src/main/java/org/apache/flink/runtime/util/EnvironmentInformation.java
##########
@@ -41,42 +45,143 @@
public static final String UNKNOWN = "<unknown>";
/**
- * Returns the version of the code as String. If version == null, then
the JobManager does not run from a
- * Maven build. An example is a source code checkout, compile, and run
from inside an IDE.
- *
- * @return The version string.
+ * Returns the version of the code as String.
+ * @return The project version string.
*/
public static String getVersion() {
- String version =
EnvironmentInformation.class.getPackage().getImplementationVersion();
- return version != null ? version : UNKNOWN;
+ return getVersionsInstance().projectVersion;
+ }
+
+ /**
+ * Returns the version of the used Scala compiler as String.
+ * @return The scala version string.
+ */
+ public static String getScalaVersion() {
+ return getVersionsInstance().scalaVersion;
+ }
+
+ /**
+ * @return The Instant this version of the software was built.
+ */
+ public static Instant getBuildTime() {
+ return getVersionsInstance().gitBuildTime;
+ }
+
+ /**
+ * @return The Instant this version of the software was built as a
String using the Europe/Berlin timezone.
+ */
+ public static String getBuildTimeString() {
+ return getVersionsInstance().gitBuildTimeStr;
+ }
+
+ /**
+ * @return The last known commit id of this version of the software.
+ */
+ public static String getGitCommitId() {
+ return getVersionsInstance().gitCommitId;
+ }
+
+ /**
+ * @return The last known abbreviated commit id of this version of the
software.
+ */
+ public static String getGitCommitIdAbbrev() {
+ return getVersionsInstance().gitCommitIdAbbrev;
+ }
+
+ /**
+ * @return The Instant of the last commit of this code.
+ */
+ public static Instant getGitCommitTime() {
+ return getVersionsInstance().gitCommitTime;
+ }
+
+ /**
+ * @return The Instant of the last commit of this code as a String
using the Europe/Berlin timezone.
+ */
+ public static String getGitCommitTimeString() {
+ return getVersionsInstance().gitCommitTimeStr;
}
/**
* Returns the code revision (commit and commit date) of Flink, as
generated by the Maven builds.
- *
+ *
* @return The code revision.
*/
public static RevisionInformation getRevisionInformation() {
- String revision = UNKNOWN;
- String commitDate = UNKNOWN;
- try (InputStream propFile =
EnvironmentInformation.class.getClassLoader().getResourceAsStream(".version.properties"))
{
- if (propFile != null) {
- Properties properties = new Properties();
- properties.load(propFile);
- String propRevision =
properties.getProperty("git.commit.id.abbrev");
- String propCommitDate =
properties.getProperty("git.commit.time");
- revision = propRevision != null ? propRevision
: UNKNOWN;
- commitDate = propCommitDate != null ?
propCommitDate : UNKNOWN;
+ return new RevisionInformation(getGitCommitIdAbbrev(),
getGitCommitTimeString());
+ }
+
+ private static final class Versions {
+ private static final Instant DEFAULT_TIME_INSTANT =
Instant.EPOCH;
+ private static final String DEFAULT_TIME_STRING =
"1970-01-01T00:00:00+0000";
+ private static final String UNKNOWN_COMMIT_ID =
"DecafC0ffeeD0d0F00d";
+ private static final String UNKNOWN_COMMIT_ID_ABBREV =
"DeadD0d0";
+ private String projectVersion = UNKNOWN;
+ private String scalaVersion = UNKNOWN;
+ private Instant gitBuildTime = DEFAULT_TIME_INSTANT;
+ private String gitBuildTimeStr = DEFAULT_TIME_STRING;
+ private String gitCommitId = UNKNOWN_COMMIT_ID;
+ private String gitCommitIdAbbrev = UNKNOWN_COMMIT_ID_ABBREV;
+ private Instant gitCommitTime = DEFAULT_TIME_INSTANT;
+ private String gitCommitTimeStr = DEFAULT_TIME_STRING;
+
+ private static final String PROP_FILE =
".flink-runtime.version.properties";
+
+ private static final String FAIL_MESSAGE = "The file " +
PROP_FILE + " has not been generated correctly. You MUST run 'mvn
generate-sources' in the flink-runtime module.";
+
+ private String getProperty(Properties properties, String key,
String defaultValue) {
+ String value = properties.getProperty(key);
+ if (value == null || value.charAt(0) == '$') {
+ return defaultValue;
}
- } catch (Throwable t) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Cannot determine code revision:
Unable to read version property file.", t);
- } else {
- LOG.info("Cannot determine code revision:
Unable to read version property file.");
+ return value;
+ }
+
+ public Versions() {
+ ClassLoader classLoader =
EnvironmentInformation.class.getClassLoader();
+ try (InputStream propFile =
classLoader.getResourceAsStream(PROP_FILE)) {
+ if (propFile != null) {
+ Properties properties = new
Properties();
+ properties.load(propFile);
+
+ projectVersion =
getProperty(properties, "project.version", UNKNOWN);
+ scalaVersion = getProperty(properties,
"scala.binary.version", UNKNOWN);
+
+ gitCommitId = getProperty(properties,
"git.commit.id", UNKNOWN_COMMIT_ID);
+ gitCommitIdAbbrev =
getProperty(properties, "git.commit.id.abbrev", UNKNOWN_COMMIT_ID_ABBREV);
+
+ // This is to reliably parse the
datetime format configured in the git-commit-id-plugin
+ DateTimeFormatter gitDateTimeFormatter
= DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
+
+ // Default format is in Berlin timezone
because that is where Flink originated.
+ DateTimeFormatter BERLIN_DATE_TIME =
DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.of("Europe/Berlin"));
+
+ try {
+ String propGitCommitTime =
getProperty(properties, "git.commit.time", DEFAULT_TIME_STRING);
+ gitCommitTime =
gitDateTimeFormatter.parse(propGitCommitTime, Instant::from);
+ gitCommitTimeStr =
BERLIN_DATE_TIME.format(gitCommitTime);
+
+ String propGitBuildTime =
getProperty(properties, "git.build.time", DEFAULT_TIME_STRING);
+ gitBuildTime =
gitDateTimeFormatter.parse(propGitBuildTime, Instant::from);
+ gitBuildTimeStr =
BERLIN_DATE_TIME.format(gitBuildTime);
+ } catch (DateTimeParseException dtpe) {
+ LOG.error("{} : {}",
FAIL_MESSAGE, dtpe);
+ throw new
IllegalStateException(FAIL_MESSAGE);
+ }
+ }
+ }
+ catch(IOException ioe) {
+ LOG.info("Cannot determine code revision:
Unable to read version property file.: {}", ioe.getMessage());
}
}
-
- return new RevisionInformation(revision, commitDate);
+ }
+
+ private static final class VersionsHolder {
Review comment:
We usually don't encapsulate singletons. What is the goal behind the
holder? (I haven't seen this before)
##########
File path:
flink-runtime/src/main/java/org/apache/flink/runtime/util/EnvironmentInformation.java
##########
@@ -41,42 +45,143 @@
public static final String UNKNOWN = "<unknown>";
/**
- * Returns the version of the code as String. If version == null, then
the JobManager does not run from a
- * Maven build. An example is a source code checkout, compile, and run
from inside an IDE.
- *
- * @return The version string.
+ * Returns the version of the code as String.
+ * @return The project version string.
*/
public static String getVersion() {
- String version =
EnvironmentInformation.class.getPackage().getImplementationVersion();
- return version != null ? version : UNKNOWN;
+ return getVersionsInstance().projectVersion;
+ }
+
+ /**
+ * Returns the version of the used Scala compiler as String.
+ * @return The scala version string.
+ */
+ public static String getScalaVersion() {
+ return getVersionsInstance().scalaVersion;
+ }
+
+ /**
+ * @return The Instant this version of the software was built.
+ */
+ public static Instant getBuildTime() {
+ return getVersionsInstance().gitBuildTime;
+ }
+
+ /**
+ * @return The Instant this version of the software was built as a
String using the Europe/Berlin timezone.
+ */
+ public static String getBuildTimeString() {
+ return getVersionsInstance().gitBuildTimeStr;
+ }
+
+ /**
+ * @return The last known commit id of this version of the software.
+ */
+ public static String getGitCommitId() {
+ return getVersionsInstance().gitCommitId;
+ }
+
+ /**
+ * @return The last known abbreviated commit id of this version of the
software.
+ */
+ public static String getGitCommitIdAbbrev() {
+ return getVersionsInstance().gitCommitIdAbbrev;
+ }
+
+ /**
+ * @return The Instant of the last commit of this code.
+ */
+ public static Instant getGitCommitTime() {
+ return getVersionsInstance().gitCommitTime;
+ }
+
+ /**
+ * @return The Instant of the last commit of this code as a String
using the Europe/Berlin timezone.
+ */
+ public static String getGitCommitTimeString() {
+ return getVersionsInstance().gitCommitTimeStr;
}
/**
* Returns the code revision (commit and commit date) of Flink, as
generated by the Maven builds.
- *
+ *
* @return The code revision.
*/
public static RevisionInformation getRevisionInformation() {
- String revision = UNKNOWN;
- String commitDate = UNKNOWN;
- try (InputStream propFile =
EnvironmentInformation.class.getClassLoader().getResourceAsStream(".version.properties"))
{
- if (propFile != null) {
- Properties properties = new Properties();
- properties.load(propFile);
- String propRevision =
properties.getProperty("git.commit.id.abbrev");
- String propCommitDate =
properties.getProperty("git.commit.time");
- revision = propRevision != null ? propRevision
: UNKNOWN;
- commitDate = propCommitDate != null ?
propCommitDate : UNKNOWN;
+ return new RevisionInformation(getGitCommitIdAbbrev(),
getGitCommitTimeString());
+ }
+
+ private static final class Versions {
+ private static final Instant DEFAULT_TIME_INSTANT =
Instant.EPOCH;
+ private static final String DEFAULT_TIME_STRING =
"1970-01-01T00:00:00+0000";
+ private static final String UNKNOWN_COMMIT_ID =
"DecafC0ffeeD0d0F00d";
+ private static final String UNKNOWN_COMMIT_ID_ABBREV =
"DeadD0d0";
+ private String projectVersion = UNKNOWN;
+ private String scalaVersion = UNKNOWN;
+ private Instant gitBuildTime = DEFAULT_TIME_INSTANT;
+ private String gitBuildTimeStr = DEFAULT_TIME_STRING;
+ private String gitCommitId = UNKNOWN_COMMIT_ID;
+ private String gitCommitIdAbbrev = UNKNOWN_COMMIT_ID_ABBREV;
+ private Instant gitCommitTime = DEFAULT_TIME_INSTANT;
+ private String gitCommitTimeStr = DEFAULT_TIME_STRING;
+
+ private static final String PROP_FILE =
".flink-runtime.version.properties";
+
+ private static final String FAIL_MESSAGE = "The file " +
PROP_FILE + " has not been generated correctly. You MUST run 'mvn
generate-sources' in the flink-runtime module.";
+
+ private String getProperty(Properties properties, String key,
String defaultValue) {
+ String value = properties.getProperty(key);
+ if (value == null || value.charAt(0) == '$') {
+ return defaultValue;
}
- } catch (Throwable t) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Cannot determine code revision:
Unable to read version property file.", t);
- } else {
- LOG.info("Cannot determine code revision:
Unable to read version property file.");
+ return value;
+ }
+
+ public Versions() {
+ ClassLoader classLoader =
EnvironmentInformation.class.getClassLoader();
+ try (InputStream propFile =
classLoader.getResourceAsStream(PROP_FILE)) {
+ if (propFile != null) {
+ Properties properties = new
Properties();
+ properties.load(propFile);
+
+ projectVersion =
getProperty(properties, "project.version", UNKNOWN);
+ scalaVersion = getProperty(properties,
"scala.binary.version", UNKNOWN);
+
+ gitCommitId = getProperty(properties,
"git.commit.id", UNKNOWN_COMMIT_ID);
+ gitCommitIdAbbrev =
getProperty(properties, "git.commit.id.abbrev", UNKNOWN_COMMIT_ID_ABBREV);
+
+ // This is to reliably parse the
datetime format configured in the git-commit-id-plugin
+ DateTimeFormatter gitDateTimeFormatter
= DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
+
+ // Default format is in Berlin timezone
because that is where Flink originated.
+ DateTimeFormatter BERLIN_DATE_TIME =
DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.of("Europe/Berlin"));
+
+ try {
+ String propGitCommitTime =
getProperty(properties, "git.commit.time", DEFAULT_TIME_STRING);
+ gitCommitTime =
gitDateTimeFormatter.parse(propGitCommitTime, Instant::from);
+ gitCommitTimeStr =
BERLIN_DATE_TIME.format(gitCommitTime);
+
+ String propGitBuildTime =
getProperty(properties, "git.build.time", DEFAULT_TIME_STRING);
+ gitBuildTime =
gitDateTimeFormatter.parse(propGitBuildTime, Instant::from);
+ gitBuildTimeStr =
BERLIN_DATE_TIME.format(gitBuildTime);
+ } catch (DateTimeParseException dtpe) {
+ LOG.error("{} : {}",
FAIL_MESSAGE, dtpe);
+ throw new
IllegalStateException(FAIL_MESSAGE);
+ }
+ }
+ }
+ catch(IOException ioe) {
+ LOG.info("Cannot determine code revision:
Unable to read version property file.: {}", ioe.getMessage());
}
}
-
- return new RevisionInformation(revision, commitDate);
+ }
+
+ private static final class VersionsHolder {
+ private static final Versions INSTANCE = new Versions();
+ }
+
+ public static Versions getVersionsInstance() {
Review comment:
private?
##########
File path:
flink-runtime/src/main/java/org/apache/flink/runtime/util/EnvironmentInformation.java
##########
@@ -41,42 +45,143 @@
public static final String UNKNOWN = "<unknown>";
/**
- * Returns the version of the code as String. If version == null, then
the JobManager does not run from a
- * Maven build. An example is a source code checkout, compile, and run
from inside an IDE.
- *
- * @return The version string.
+ * Returns the version of the code as String.
+ * @return The project version string.
*/
public static String getVersion() {
- String version =
EnvironmentInformation.class.getPackage().getImplementationVersion();
- return version != null ? version : UNKNOWN;
+ return getVersionsInstance().projectVersion;
+ }
+
+ /**
+ * Returns the version of the used Scala compiler as String.
+ * @return The scala version string.
+ */
+ public static String getScalaVersion() {
+ return getVersionsInstance().scalaVersion;
+ }
+
+ /**
+ * @return The Instant this version of the software was built.
+ */
+ public static Instant getBuildTime() {
+ return getVersionsInstance().gitBuildTime;
+ }
+
+ /**
+ * @return The Instant this version of the software was built as a
String using the Europe/Berlin timezone.
+ */
+ public static String getBuildTimeString() {
+ return getVersionsInstance().gitBuildTimeStr;
+ }
+
+ /**
+ * @return The last known commit id of this version of the software.
+ */
+ public static String getGitCommitId() {
+ return getVersionsInstance().gitCommitId;
+ }
+
+ /**
+ * @return The last known abbreviated commit id of this version of the
software.
+ */
+ public static String getGitCommitIdAbbrev() {
+ return getVersionsInstance().gitCommitIdAbbrev;
+ }
+
+ /**
+ * @return The Instant of the last commit of this code.
+ */
+ public static Instant getGitCommitTime() {
+ return getVersionsInstance().gitCommitTime;
+ }
+
+ /**
+ * @return The Instant of the last commit of this code as a String
using the Europe/Berlin timezone.
+ */
+ public static String getGitCommitTimeString() {
+ return getVersionsInstance().gitCommitTimeStr;
}
/**
* Returns the code revision (commit and commit date) of Flink, as
generated by the Maven builds.
- *
+ *
* @return The code revision.
*/
public static RevisionInformation getRevisionInformation() {
- String revision = UNKNOWN;
- String commitDate = UNKNOWN;
- try (InputStream propFile =
EnvironmentInformation.class.getClassLoader().getResourceAsStream(".version.properties"))
{
- if (propFile != null) {
- Properties properties = new Properties();
- properties.load(propFile);
- String propRevision =
properties.getProperty("git.commit.id.abbrev");
- String propCommitDate =
properties.getProperty("git.commit.time");
- revision = propRevision != null ? propRevision
: UNKNOWN;
- commitDate = propCommitDate != null ?
propCommitDate : UNKNOWN;
+ return new RevisionInformation(getGitCommitIdAbbrev(),
getGitCommitTimeString());
+ }
+
+ private static final class Versions {
+ private static final Instant DEFAULT_TIME_INSTANT =
Instant.EPOCH;
+ private static final String DEFAULT_TIME_STRING =
"1970-01-01T00:00:00+0000";
+ private static final String UNKNOWN_COMMIT_ID =
"DecafC0ffeeD0d0F00d";
+ private static final String UNKNOWN_COMMIT_ID_ABBREV =
"DeadD0d0";
+ private String projectVersion = UNKNOWN;
+ private String scalaVersion = UNKNOWN;
+ private Instant gitBuildTime = DEFAULT_TIME_INSTANT;
+ private String gitBuildTimeStr = DEFAULT_TIME_STRING;
+ private String gitCommitId = UNKNOWN_COMMIT_ID;
+ private String gitCommitIdAbbrev = UNKNOWN_COMMIT_ID_ABBREV;
+ private Instant gitCommitTime = DEFAULT_TIME_INSTANT;
+ private String gitCommitTimeStr = DEFAULT_TIME_STRING;
Review comment:
```suggestion
private static final Instant DEFAULT_TIME_INSTANT =
Instant.EPOCH;
private static final String DEFAULT_TIME_STRING =
"1970-01-01T00:00:00+0000";
private static final String UNKNOWN_COMMIT_ID =
"DecafC0ffeeD0d0F00d";
private static final String UNKNOWN_COMMIT_ID_ABBREV =
"DeadD0d0";
private String projectVersion = UNKNOWN;
private String scalaVersion = UNKNOWN;
private Instant gitBuildTime = DEFAULT_TIME_INSTANT;
private String gitBuildTimeStr = DEFAULT_TIME_STRING;
private String gitCommitId = UNKNOWN_COMMIT_ID;
private String gitCommitIdAbbrev = UNKNOWN_COMMIT_ID_ABBREV;
private Instant gitCommitTime = DEFAULT_TIME_INSTANT;
private String gitCommitTimeStr = DEFAULT_TIME_STRING;
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]