Skip to content

Commit d902c59

Browse files
authored
RATIS-2338. Print version info when starting a server. (#1292)
1 parent d411b02 commit d902c59

File tree

9 files changed

+158
-37
lines changed

9 files changed

+158
-37
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,10 @@
789789
<configuration>
790790
<source>
791791
<directory>${project.basedir}</directory>
792+
<includes>
793+
<include>*/src/main/java/**/*.java</include>
794+
<include>*/src/main/proto/*.proto</include>
795+
</includes>
792796
</source>
793797
</configuration>
794798
</execution>
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.ratis.util;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.Collections;
26+
import java.util.EnumMap;
27+
import java.util.LinkedHashMap;
28+
import java.util.Map;
29+
import java.util.Objects;
30+
import java.util.Properties;
31+
import java.util.function.Consumer;
32+
33+
/**
34+
* This class is for the resource generated by hadoop-maven-plugins:version-info.
35+
* <p>
36+
* This class is immutable.
37+
*/
38+
public final class VersionInfo {
39+
static final Logger LOG = LoggerFactory.getLogger(VersionInfo.class);
40+
41+
private static final String RATIS_VERSION_PROPERTIES = "ratis-version.properties";
42+
private static final String UNKNOWN = "<unknown>";
43+
private static final String FORMAT = " %20s: %s";
44+
45+
private enum SoftwareInfo {
46+
// the ordering is the output ordering
47+
NAME, VERSION, URL, REVISION;
48+
49+
static SoftwareInfo parse(String key) {
50+
for (SoftwareInfo info : SoftwareInfo.values()) {
51+
if (info.name().toLowerCase().equals(key)) {
52+
return info;
53+
}
54+
}
55+
return null;
56+
}
57+
}
58+
59+
private enum RuntimeInfo {
60+
// the ordering is the output ordering
61+
JAVA, USER;
62+
63+
static final InfoMap<RuntimeInfo> MAP;
64+
65+
static {
66+
final EnumMap<RuntimeInfo, String> map = new EnumMap<>(RuntimeInfo.class);
67+
final Properties properties = System.getProperties();
68+
map.put(JAVA, properties.getProperty("java.vm.name") + " " + properties.getProperty("java.runtime.version"));
69+
map.put(USER, properties.getProperty("user.name"));
70+
MAP = new InfoMap<>(map);
71+
}
72+
}
73+
74+
private static class InfoMap<INFO extends Enum<INFO>> {
75+
private final Map<INFO, String> map;
76+
77+
InfoMap(EnumMap<INFO, String> map) {
78+
this.map = Collections.unmodifiableMap(map);
79+
}
80+
81+
String getOrDefault(INFO info) {
82+
return map.getOrDefault(info, UNKNOWN);
83+
}
84+
85+
String format(INFO info) {
86+
return String.format(FORMAT, info.name().toLowerCase(), getOrDefault(info));
87+
}
88+
}
89+
90+
public static VersionInfo load(Class<?> clazz) {
91+
final Properties properties = new Properties();
92+
93+
try (InputStream in = clazz.getClassLoader().getResourceAsStream(RATIS_VERSION_PROPERTIES)) {
94+
if (in != null) {
95+
properties.load(in);
96+
} else {
97+
LOG.warn("Resource '{}' not found for {}", RATIS_VERSION_PROPERTIES, clazz);
98+
}
99+
} catch (IOException e) {
100+
LOG.warn("Failed to load resource '{}' for {}", RATIS_VERSION_PROPERTIES, clazz, e);
101+
}
102+
return new VersionInfo(clazz, properties);
103+
}
104+
105+
private final Class<?> clazz;
106+
private final InfoMap<RuntimeInfo> runtimeInfos = RuntimeInfo.MAP;
107+
private final InfoMap<SoftwareInfo> softwareInfos;
108+
private final Map<String, String> otherInfos;
109+
110+
private VersionInfo(Class<?> clazz, Properties properties) {
111+
this.clazz = Objects.requireNonNull(clazz, "clazz == null");
112+
113+
final EnumMap<SoftwareInfo, String> softwareInfoMap = new EnumMap<>(SoftwareInfo.class);
114+
final Map<String, String> others = new LinkedHashMap<>(); // preserve insertion order
115+
for (Map.Entry<Object, Object> e : properties.entrySet()) {
116+
final String key = e.getKey().toString();
117+
final String value = e.getValue().toString();
118+
final SoftwareInfo k = SoftwareInfo.parse(key);
119+
if (k != null) {
120+
softwareInfoMap.put(k, value);
121+
} else {
122+
others.put(key, value);
123+
}
124+
}
125+
126+
this.softwareInfos = new InfoMap<>(softwareInfoMap);
127+
this.otherInfos = Collections.unmodifiableMap(others);
128+
}
129+
130+
public void printStartupMessages(Object name, Consumer<String> log) {
131+
Objects.requireNonNull(name, "name == null");
132+
log.accept(String.format("Starting %s -- %s %s",
133+
softwareInfos.getOrDefault(SoftwareInfo.NAME), clazz.getSimpleName(), name));
134+
final SoftwareInfo[] softwareInfoValues = SoftwareInfo.values();
135+
for(int i = 1; i < softwareInfoValues.length; i++) {
136+
log.accept(softwareInfos.format(softwareInfoValues[i]));
137+
}
138+
for(RuntimeInfo runtimeInfo : RuntimeInfo.values()) {
139+
log.accept(runtimeInfos.format(runtimeInfo));
140+
}
141+
for (Map.Entry<String, String> e : otherInfos.entrySet()) {
142+
log.accept(String.format(FORMAT, e.getKey(), e.getValue()));
143+
}
144+
}
145+
146+
public static void main(String[] args) {
147+
VersionInfo.load(VersionInfo.class).printStartupMessages(":", System.out::println);
148+
}
149+
}

ratis-grpc/pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@
3737
<artifactId>ratis-common</artifactId>
3838
<groupId>org.apache.ratis</groupId>
3939
</dependency>
40-
<dependency>
41-
<artifactId>ratis-common</artifactId>
42-
<groupId>org.apache.ratis</groupId>
43-
<scope>test</scope>
44-
<type>test-jar</type>
45-
</dependency>
4640
<dependency>
4741
<artifactId>ratis-client</artifactId>
4842
<groupId>org.apache.ratis</groupId>
@@ -79,10 +73,5 @@
7973
<artifactId>junit-jupiter-api</artifactId>
8074
<scope>test</scope>
8175
</dependency>
82-
<dependency>
83-
<groupId>org.mockito</groupId>
84-
<artifactId>mockito-core</artifactId>
85-
<scope>test</scope>
86-
</dependency>
8776
</dependencies>
8877
</project>

ratis-metrics-default/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,9 @@
2929
<artifactId>ratis-metrics-api</artifactId>
3030
<groupId>org.apache.ratis</groupId>
3131
</dependency>
32-
<dependency>
33-
<artifactId>ratis-proto</artifactId>
34-
<groupId>org.apache.ratis</groupId>
35-
</dependency>
36-
<dependency>
37-
<artifactId>ratis-common</artifactId>
38-
<groupId>org.apache.ratis</groupId>
39-
</dependency>
4032
<dependency>
4133
<artifactId>ratis-common</artifactId>
4234
<groupId>org.apache.ratis</groupId>
43-
<scope>test</scope>
44-
<type>test-jar</type>
4535
</dependency>
4636

4737
<dependency>

ratis-server/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@
4848
<artifactId>ratis-client</artifactId>
4949
<groupId>org.apache.ratis</groupId>
5050
</dependency>
51-
<dependency>
52-
<artifactId>ratis-client</artifactId>
53-
<groupId>org.apache.ratis</groupId>
54-
<scope>test</scope>
55-
<type>test-jar</type>
56-
</dependency>
5751

5852
<dependency>
5953
<artifactId>ratis-server-api</artifactId>

ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.apache.ratis.util.Preconditions;
5454
import org.apache.ratis.util.ProtoUtils;
5555
import org.apache.ratis.util.TimeDuration;
56+
import org.apache.ratis.util.VersionInfo;
5657

5758
import java.io.Closeable;
5859
import java.io.File;
@@ -207,6 +208,8 @@ String toString(RaftGroupId groupId, CompletableFuture<RaftServerImpl> f) {
207208

208209
RaftServerProxy(RaftPeerId id, StateMachine.Registry stateMachineRegistry,
209210
RaftProperties properties, Parameters parameters, ThreadGroup threadGroup) {
211+
VersionInfo.load(getClass()).printStartupMessages(id, LOG::info);
212+
210213
this.properties = properties;
211214
this.stateMachineRegistry = stateMachineRegistry;
212215

ratis-server/src/main/java/org/apache/ratis/server/impl/ServerImplUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ public static RaftServerProxy newRaftServer(
170170
RaftPeerId id, RaftGroup group, RaftStorage.StartupOption option, StateMachine.Registry stateMachineRegistry,
171171
ThreadGroup threadGroup, RaftProperties properties, Parameters parameters) throws IOException {
172172
RaftServer.LOG.debug("newRaftServer: {}, {}", id, group);
173+
Objects.requireNonNull(id, "id == null");
173174
if (group != null && !group.getPeers().isEmpty()) {
174-
Objects.requireNonNull(id, () -> "RaftPeerId " + id + " is not in RaftGroup " + group);
175175
Objects.requireNonNull(group.getPeer(id), () -> "RaftPeerId " + id + " is not in RaftGroup " + group);
176176
}
177177
final RaftServerProxy proxy = newRaftServer(id, stateMachineRegistry, threadGroup, properties, parameters);

ratis-test/src/test/java/org/apache/ratis/server/ServerBuilderTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,4 @@ public void testPeerIdWithNullRaftGroup() throws Exception {
9090
.build();
9191
server.close();
9292
}
93-
94-
@Test
95-
public void testNullPeerIdWithNullRaftGroup() throws Exception {
96-
RaftServer server = RaftServer.newBuilder()
97-
.setStateMachine(new BaseStateMachine())
98-
.setProperties(new RaftProperties())
99-
.build();
100-
server.close();
101-
}
10293
}

src/main/resources/ratis-version.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
#
1818
name=${project.name}
1919
version=${project.version}
20+
url=${version-info.scm.uri}
2021
revision=${version-info.scm.commit}

0 commit comments

Comments
 (0)