Skip to content

Commit

Permalink
[FLINK-16871][Runtime] Make more build time information available at …
Browse files Browse the repository at this point in the history
…runtime
  • Loading branch information
nielsbasjes committed Apr 8, 2020
1 parent e789bb7 commit 47099f6
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 65 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -5,7 +5,6 @@ scalastyle-output.xml
.metadata
.settings
.project
.version.properties
filter.properties
logs.zip
target
Expand Down
23 changes: 0 additions & 23 deletions flink-dist/pom.xml
Expand Up @@ -841,29 +841,6 @@ under the License.
</configuration>
</plugin>

<plugin>
<!-- Description: https://github.com/ktoso/maven-git-commit-id-plugin -->
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<skipPoms>false</skipPoms>
<generateGitPropertiesFilename>src/main/flink-bin/.version.properties</generateGitPropertiesFilename>
<gitDescribe>
<!-- don't generate the describe property -->
<skip>true</skip>
</gitDescribe>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
23 changes: 18 additions & 5 deletions flink-runtime/pom.xml
Expand Up @@ -316,6 +316,18 @@ under the License.
</dependencyManagement>

<build>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -454,25 +466,26 @@ under the License.
</configuration>
</plugin>
<plugin>
<!-- Description: https://github.com/ktoso/maven-git-commit-id-plugin
<!-- Description: https://github.com/git-commit-id/git-commit-id-maven-plugin
Used to show the git ref when starting the jobManager. -->
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<id>get-the-git-infos</id>
<phase>validate</phase>
<goals>
<goal>revision</goal>
</goals>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<skipPoms>false</skipPoms>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<generateGitPropertiesFilename>src/main/resources/.version.properties</generateGitPropertiesFilename>
<gitDescribe>
<!-- don't generate the describe property -->
<!-- Don't generate the describe property -->
<!-- It is useless due to the way Flink does branches and tags -->
<skip>true</skip>
</gitDescribe>
</configuration>
Expand Down
Expand Up @@ -18,18 +18,22 @@

package org.apache.flink.runtime.util;

import org.apache.flink.util.OperatingSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Method;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Properties;

import org.apache.flink.util.OperatingSystem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utility class that gives access to the execution environment of the JVM, like
* the executing user, startup options, or the JVM version.
Expand All @@ -41,42 +45,136 @@ public class EnvironmentInformation {
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:00Z";


private String projectVersion = UNKNOWN;
private String scalaVersion = UNKNOWN;
private Instant gitBuildTime = DEFAULT_TIME_INSTANT;
private String gitBuildTimeStr;
private String gitCommitId = UNKNOWN;
private String gitCommitIdAbbrev = UNKNOWN;
private Instant gitCommitTime = DEFAULT_TIME_INSTANT;
private String gitCommitTimeStr;

private static final String PROP_FILE = ".flink-runtime.version.properties";

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 = properties.getProperty("project.version", UNKNOWN);
scalaVersion = properties.getProperty("scala.binary.version", UNKNOWN);

gitCommitId = properties.getProperty("git.commit.id", UNKNOWN);
gitCommitIdAbbrev = properties.getProperty("git.commit.id.abbrev", UNKNOWN);

// 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");

try {
String propGitCommitTime = properties.getProperty("git.commit.time", DEFAULT_TIME_STRING);
gitCommitTime = gitDateTimeFormatter.parse(propGitCommitTime, Instant::from);

String propGitBuildTime = properties.getProperty("git.build.time", DEFAULT_TIME_STRING);
gitBuildTime = gitDateTimeFormatter.parse(propGitBuildTime, Instant::from);
} catch (DateTimeParseException dtpe) {
LOG.error("The file {} has not been generated correctly: {}", PROP_FILE, dtpe);
}
}
}
} 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.");
catch(IOException ioe) {
if (LOG.isDebugEnabled()) {
LOG.debug("Cannot determine code revision: Unable to read version property file.: {}", ioe.getMessage());
} else {
LOG.info("Cannot determine code revision: Unable to read version property file.: {}", ioe.getMessage());
}
}

// 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"));
gitBuildTimeStr = BERLIN_DATE_TIME.format(gitBuildTime);
gitCommitTimeStr = BERLIN_DATE_TIME.format(gitCommitTime);
}

return new RevisionInformation(revision, commitDate);
}

private static final class VersionsHolder {
private static final Versions INSTANCE = new Versions();
}

public static Versions getVersionsInstance() {
return VersionsHolder.INSTANCE;
}

/**
Expand Down Expand Up @@ -270,6 +368,7 @@ public static void logEnvironmentInfo(Logger log, String componentName, String[]
if (log.isInfoEnabled()) {
RevisionInformation rev = getRevisionInformation();
String version = getVersion();
String scalaVersion = getScalaVersion();

String jvmVersion = getJvmVersion();
String[] options = getJvmStartupOptionsArray();
Expand All @@ -287,7 +386,7 @@ public static void logEnvironmentInfo(Logger log, String componentName, String[]
}

log.info("--------------------------------------------------------------------------------");
log.info(" Starting " + componentName + " (Version: " + version + ", "
log.info(" Starting " + componentName + " (Version: " + version + ", Scala: " + scalaVersion + ", "
+ "Rev:" + rev.commitId + ", " + "Date:" + rev.commitDate + ")");
log.info(" OS current user: " + System.getProperty("user.name"));
log.info(" Current Hadoop/Kerberos user: " + getHadoopUser());
Expand Down Expand Up @@ -367,5 +466,13 @@ public RevisionInformation(String commitId, String commitDate) {
this.commitId = commitId;
this.commitDate = commitDate;
}

@Override
public String toString() {
return "RevisionInformation{" +
"commitId='" + commitId + '\'' +
", commitDate='" + commitDate + '\'' +
'}';
}
}
}
@@ -0,0 +1,22 @@
# 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.version=${project.version}
scala.binary.version=${scala.binary.version}

git.commit.id=${git.commit.id}
git.commit.id.abbrev=${git.commit.id.abbrev}
git.commit.time=${git.commit.time}
git.build.time=${git.build.time}

0 comments on commit 47099f6

Please sign in to comment.