This is an automated email from the ASF dual-hosted git repository.
szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git
The following commit(s) were added to refs/heads/master by this push:
new d902c593e RATIS-2338. Print version info when starting a server.
(#1292)
d902c593e is described below
commit d902c593e11de03b157527cf3ee9a12cdd9ad451
Author: Tsz-Wo Nicholas Sze <[email protected]>
AuthorDate: Sun Sep 28 23:58:26 2025 -0700
RATIS-2338. Print version info when starting a server. (#1292)
---
pom.xml | 4 +
.../java/org/apache/ratis/util/VersionInfo.java | 149 +++++++++++++++++++++
ratis-grpc/pom.xml | 11 --
ratis-metrics-default/pom.xml | 10 --
ratis-server/pom.xml | 6 -
.../apache/ratis/server/impl/RaftServerProxy.java | 3 +
.../apache/ratis/server/impl/ServerImplUtils.java | 2 +-
.../org/apache/ratis/server/ServerBuilderTest.java | 9 --
src/main/resources/ratis-version.properties | 1 +
9 files changed, 158 insertions(+), 37 deletions(-)
diff --git a/pom.xml b/pom.xml
index b32c7ac1d..9ea737977 100644
--- a/pom.xml
+++ b/pom.xml
@@ -789,6 +789,10 @@
<configuration>
<source>
<directory>${project.basedir}</directory>
+ <includes>
+ <include>*/src/main/java/**/*.java</include>
+ <include>*/src/main/proto/*.proto</include>
+ </includes>
</source>
</configuration>
</execution>
diff --git a/ratis-common/src/main/java/org/apache/ratis/util/VersionInfo.java
b/ratis-common/src/main/java/org/apache/ratis/util/VersionInfo.java
new file mode 100644
index 000000000..98f662f37
--- /dev/null
+++ b/ratis-common/src/main/java/org/apache/ratis/util/VersionInfo.java
@@ -0,0 +1,149 @@
+/*
+ * 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.
+ */
+package org.apache.ratis.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.EnumMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.function.Consumer;
+
+/**
+ * This class is for the resource generated by
hadoop-maven-plugins:version-info.
+ * <p>
+ * This class is immutable.
+ */
+public final class VersionInfo {
+ static final Logger LOG = LoggerFactory.getLogger(VersionInfo.class);
+
+ private static final String RATIS_VERSION_PROPERTIES =
"ratis-version.properties";
+ private static final String UNKNOWN = "<unknown>";
+ private static final String FORMAT = " %20s: %s";
+
+ private enum SoftwareInfo {
+ // the ordering is the output ordering
+ NAME, VERSION, URL, REVISION;
+
+ static SoftwareInfo parse(String key) {
+ for (SoftwareInfo info : SoftwareInfo.values()) {
+ if (info.name().toLowerCase().equals(key)) {
+ return info;
+ }
+ }
+ return null;
+ }
+ }
+
+ private enum RuntimeInfo {
+ // the ordering is the output ordering
+ JAVA, USER;
+
+ static final InfoMap<RuntimeInfo> MAP;
+
+ static {
+ final EnumMap<RuntimeInfo, String> map = new
EnumMap<>(RuntimeInfo.class);
+ final Properties properties = System.getProperties();
+ map.put(JAVA, properties.getProperty("java.vm.name") + " " +
properties.getProperty("java.runtime.version"));
+ map.put(USER, properties.getProperty("user.name"));
+ MAP = new InfoMap<>(map);
+ }
+ }
+
+ private static class InfoMap<INFO extends Enum<INFO>> {
+ private final Map<INFO, String> map;
+
+ InfoMap(EnumMap<INFO, String> map) {
+ this.map = Collections.unmodifiableMap(map);
+ }
+
+ String getOrDefault(INFO info) {
+ return map.getOrDefault(info, UNKNOWN);
+ }
+
+ String format(INFO info) {
+ return String.format(FORMAT, info.name().toLowerCase(),
getOrDefault(info));
+ }
+ }
+
+ public static VersionInfo load(Class<?> clazz) {
+ final Properties properties = new Properties();
+
+ try (InputStream in =
clazz.getClassLoader().getResourceAsStream(RATIS_VERSION_PROPERTIES)) {
+ if (in != null) {
+ properties.load(in);
+ } else {
+ LOG.warn("Resource '{}' not found for {}", RATIS_VERSION_PROPERTIES,
clazz);
+ }
+ } catch (IOException e) {
+ LOG.warn("Failed to load resource '{}' for {}",
RATIS_VERSION_PROPERTIES, clazz, e);
+ }
+ return new VersionInfo(clazz, properties);
+ }
+
+ private final Class<?> clazz;
+ private final InfoMap<RuntimeInfo> runtimeInfos = RuntimeInfo.MAP;
+ private final InfoMap<SoftwareInfo> softwareInfos;
+ private final Map<String, String> otherInfos;
+
+ private VersionInfo(Class<?> clazz, Properties properties) {
+ this.clazz = Objects.requireNonNull(clazz, "clazz == null");
+
+ final EnumMap<SoftwareInfo, String> softwareInfoMap = new
EnumMap<>(SoftwareInfo.class);
+ final Map<String, String> others = new LinkedHashMap<>(); // preserve
insertion order
+ for (Map.Entry<Object, Object> e : properties.entrySet()) {
+ final String key = e.getKey().toString();
+ final String value = e.getValue().toString();
+ final SoftwareInfo k = SoftwareInfo.parse(key);
+ if (k != null) {
+ softwareInfoMap.put(k, value);
+ } else {
+ others.put(key, value);
+ }
+ }
+
+ this.softwareInfos = new InfoMap<>(softwareInfoMap);
+ this.otherInfos = Collections.unmodifiableMap(others);
+ }
+
+ public void printStartupMessages(Object name, Consumer<String> log) {
+ Objects.requireNonNull(name, "name == null");
+ log.accept(String.format("Starting %s -- %s %s",
+ softwareInfos.getOrDefault(SoftwareInfo.NAME), clazz.getSimpleName(),
name));
+ final SoftwareInfo[] softwareInfoValues = SoftwareInfo.values();
+ for(int i = 1; i < softwareInfoValues.length; i++) {
+ log.accept(softwareInfos.format(softwareInfoValues[i]));
+ }
+ for(RuntimeInfo runtimeInfo : RuntimeInfo.values()) {
+ log.accept(runtimeInfos.format(runtimeInfo));
+ }
+ for (Map.Entry<String, String> e : otherInfos.entrySet()) {
+ log.accept(String.format(FORMAT, e.getKey(), e.getValue()));
+ }
+ }
+
+ public static void main(String[] args) {
+ VersionInfo.load(VersionInfo.class).printStartupMessages(":",
System.out::println);
+ }
+}
diff --git a/ratis-grpc/pom.xml b/ratis-grpc/pom.xml
index 554a1a763..1ae667b18 100644
--- a/ratis-grpc/pom.xml
+++ b/ratis-grpc/pom.xml
@@ -37,12 +37,6 @@
<artifactId>ratis-common</artifactId>
<groupId>org.apache.ratis</groupId>
</dependency>
- <dependency>
- <artifactId>ratis-common</artifactId>
- <groupId>org.apache.ratis</groupId>
- <scope>test</scope>
- <type>test-jar</type>
- </dependency>
<dependency>
<artifactId>ratis-client</artifactId>
<groupId>org.apache.ratis</groupId>
@@ -79,10 +73,5 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- <scope>test</scope>
- </dependency>
</dependencies>
</project>
diff --git a/ratis-metrics-default/pom.xml b/ratis-metrics-default/pom.xml
index 1f9e36862..cbdb2dfc6 100644
--- a/ratis-metrics-default/pom.xml
+++ b/ratis-metrics-default/pom.xml
@@ -29,19 +29,9 @@
<artifactId>ratis-metrics-api</artifactId>
<groupId>org.apache.ratis</groupId>
</dependency>
- <dependency>
- <artifactId>ratis-proto</artifactId>
- <groupId>org.apache.ratis</groupId>
- </dependency>
- <dependency>
- <artifactId>ratis-common</artifactId>
- <groupId>org.apache.ratis</groupId>
- </dependency>
<dependency>
<artifactId>ratis-common</artifactId>
<groupId>org.apache.ratis</groupId>
- <scope>test</scope>
- <type>test-jar</type>
</dependency>
<dependency>
diff --git a/ratis-server/pom.xml b/ratis-server/pom.xml
index 54ea8e1de..7a4f92995 100644
--- a/ratis-server/pom.xml
+++ b/ratis-server/pom.xml
@@ -48,12 +48,6 @@
<artifactId>ratis-client</artifactId>
<groupId>org.apache.ratis</groupId>
</dependency>
- <dependency>
- <artifactId>ratis-client</artifactId>
- <groupId>org.apache.ratis</groupId>
- <scope>test</scope>
- <type>test-jar</type>
- </dependency>
<dependency>
<artifactId>ratis-server-api</artifactId>
diff --git
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java
index 2d265cf95..8539fa99e 100644
---
a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java
+++
b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java
@@ -53,6 +53,7 @@ import org.apache.ratis.util.MemoizedSupplier;
import org.apache.ratis.util.Preconditions;
import org.apache.ratis.util.ProtoUtils;
import org.apache.ratis.util.TimeDuration;
+import org.apache.ratis.util.VersionInfo;
import java.io.Closeable;
import java.io.File;
@@ -207,6 +208,8 @@ class RaftServerProxy implements RaftServer {
RaftServerProxy(RaftPeerId id, StateMachine.Registry stateMachineRegistry,
RaftProperties properties, Parameters parameters, ThreadGroup
threadGroup) {
+ VersionInfo.load(getClass()).printStartupMessages(id, LOG::info);
+
this.properties = properties;
this.stateMachineRegistry = stateMachineRegistry;
diff --git
a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerImplUtils.java
b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerImplUtils.java
index 864b402a2..1a5fcfc85 100644
---
a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerImplUtils.java
+++
b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerImplUtils.java
@@ -170,8 +170,8 @@ public final class ServerImplUtils {
RaftPeerId id, RaftGroup group, RaftStorage.StartupOption option,
StateMachine.Registry stateMachineRegistry,
ThreadGroup threadGroup, RaftProperties properties, Parameters
parameters) throws IOException {
RaftServer.LOG.debug("newRaftServer: {}, {}", id, group);
+ Objects.requireNonNull(id, "id == null");
if (group != null && !group.getPeers().isEmpty()) {
- Objects.requireNonNull(id, () -> "RaftPeerId " + id + " is not in
RaftGroup " + group);
Objects.requireNonNull(group.getPeer(id), () -> "RaftPeerId " + id + "
is not in RaftGroup " + group);
}
final RaftServerProxy proxy = newRaftServer(id, stateMachineRegistry,
threadGroup, properties, parameters);
diff --git
a/ratis-test/src/test/java/org/apache/ratis/server/ServerBuilderTest.java
b/ratis-test/src/test/java/org/apache/ratis/server/ServerBuilderTest.java
index 58d553367..15040d3d2 100644
--- a/ratis-test/src/test/java/org/apache/ratis/server/ServerBuilderTest.java
+++ b/ratis-test/src/test/java/org/apache/ratis/server/ServerBuilderTest.java
@@ -90,13 +90,4 @@ public class ServerBuilderTest extends BaseTest {
.build();
server.close();
}
-
- @Test
- public void testNullPeerIdWithNullRaftGroup() throws Exception {
- RaftServer server = RaftServer.newBuilder()
- .setStateMachine(new BaseStateMachine())
- .setProperties(new RaftProperties())
- .build();
- server.close();
- }
}
diff --git a/src/main/resources/ratis-version.properties
b/src/main/resources/ratis-version.properties
index f34dc73dc..7413ff994 100644
--- a/src/main/resources/ratis-version.properties
+++ b/src/main/resources/ratis-version.properties
@@ -17,4 +17,5 @@
#
name=${project.name}
version=${project.version}
+url=${version-info.scm.uri}
revision=${version-info.scm.commit}