Skip to content

Commit cee7b40

Browse files
committed
add comment version
1 parent 73d38df commit cee7b40

File tree

5 files changed

+166
-164
lines changed

5 files changed

+166
-164
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.hugegraph.store.node.metrics;
18+
19+
import static java.util.Locale.ENGLISH;
20+
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.Collections;
26+
import java.util.HashMap;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Objects;
30+
31+
import lombok.Getter;
32+
33+
class ProcFileHandler {
34+
35+
// Cache duration in milliseconds
36+
private static final long CACHE_DURATION_MS = 100;
37+
// Singleton instances of ProcFileHandler
38+
private static final Map<String, ProcFileHandler> handlerInstances = new HashMap<>();
39+
private static final Object handlerLock = new Object();
40+
// Cached data
41+
private static final Map<Path, List<String>> cachedContent = new HashMap<>();
42+
private static final Object contentLock = new Object();
43+
// Base path for proc filesystem
44+
private static final Path PROC_BASE_PATH = Paths.get("/proc", "self");
45+
private final Path filePath;
46+
private final boolean isOSSupported;
47+
private long lastReadTimestamp = -1;
48+
49+
// Private constructor to initialize the handler
50+
private ProcFileHandler(String entry) {
51+
this(PROC_BASE_PATH, entry, false);
52+
}
53+
54+
// Package-private constructor for testing
55+
ProcFileHandler(Path base, String entry) {
56+
this(base, entry, true);
57+
}
58+
59+
// Private constructor with an OS support flag
60+
private ProcFileHandler(Path base, String entry, boolean forceOSSupport) {
61+
Objects.requireNonNull(base);
62+
Objects.requireNonNull(entry);
63+
64+
this.filePath = base.resolve(entry);
65+
this.isOSSupported = forceOSSupport ||
66+
System.getProperty("os.name").toLowerCase(ENGLISH).startsWith("linux");
67+
}
68+
69+
// Get an instance of ProcFileHandler
70+
static ProcFileHandler getInstance(String key) {
71+
Objects.requireNonNull(key);
72+
73+
synchronized (handlerLock) {
74+
ProcFileHandler handler = handlerInstances.get(key);
75+
if (handler == null) {
76+
handler = new ProcFileHandler(key);
77+
handlerInstances.put(key, handler);
78+
}
79+
return handler;
80+
}
81+
}
82+
83+
// Get the file path
84+
Path getFilePath() {
85+
return filePath;
86+
}
87+
88+
// Read the proc file
89+
ReadResult readFile() throws IOException {
90+
return readFile(currentTimeMillis());
91+
}
92+
93+
// Read the proc file with a specific time
94+
ReadResult readFile(long currentTimeMillis) throws IOException {
95+
synchronized (contentLock) {
96+
final Path key = getFilePath().getFileName();
97+
98+
final ReadResult readResult;
99+
if (lastReadTimestamp == -1 ||
100+
lastReadTimestamp + CACHE_DURATION_MS < currentTimeMillis) {
101+
final List<String> lines = readFilePath(filePath);
102+
cacheContent(key, lines);
103+
lastReadTimestamp = currentTimeMillis();
104+
readResult = new ReadResult(lines, lastReadTimestamp);
105+
} else {
106+
readResult = new ReadResult(cachedContent.get(key), lastReadTimestamp);
107+
}
108+
return readResult;
109+
}
110+
}
111+
112+
// Read the content of the path
113+
List<String> readFilePath(Path path) throws IOException {
114+
Objects.requireNonNull(path);
115+
116+
if (!isOSSupported) {
117+
return Collections.emptyList();
118+
}
119+
return Files.readAllLines(path);
120+
}
121+
122+
// Cache the result
123+
void cacheContent(Path key, List<String> lines) {
124+
Objects.requireNonNull(key);
125+
Objects.requireNonNull(lines);
126+
127+
cachedContent.put(key, lines);
128+
}
129+
130+
// Get the current time in milliseconds
131+
long currentTimeMillis() {
132+
return System.currentTimeMillis();
133+
}
134+
135+
// Result of reading the proc file
136+
@Getter
137+
static class ReadResult {
138+
139+
private final List<String> lines;
140+
private final long readTime;
141+
142+
ReadResult(List<String> lines, long readTime) {
143+
this.lines = Objects.requireNonNull(lines);
144+
this.readTime = readTime;
145+
}
146+
}
147+
}

hugegraph-store/hg-store-node/src/main/java/org/apache/hugegraph/store/node/metrics/ProcfsEntry.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package org.apache.hugegraph.store.node.metrics;
1818

19-
import static org.apache.hugegraph.store.node.metrics.ProcfsReader.ReadResult;
19+
import static org.apache.hugegraph.store.node.metrics.ProcFileHandler.ReadResult;
2020

2121
import java.io.IOException;
2222
import java.util.Collection;
@@ -31,26 +31,27 @@ abstract class ProcfsRecord {
3131

3232
private final Object syncLock = new Object();
3333

34-
private final ProcfsReader fileReader;
34+
private final ProcFileHandler fileReader;
3535

3636
private long lastProcessedTime = -1;
3737

38-
protected ProcfsRecord(ProcfsReader fileReader) {
38+
protected ProcfsRecord(ProcFileHandler fileReader) {
3939
this.fileReader = Objects.requireNonNull(fileReader);
4040
}
4141

4242
protected final void gatherData() {
4343
synchronized (syncLock) {
4444
try {
45-
final ReadResult readResult = fileReader.read();
46-
if (readResult != null && (lastProcessedTime == -1 || lastProcessedTime != readResult.getReadTime())) {
45+
final ReadResult readResult = fileReader.readFile();
46+
if (readResult != null &&
47+
(lastProcessedTime == -1 || lastProcessedTime != readResult.getReadTime())) {
4748
clear();
4849
process(readResult.getLines());
4950
lastProcessedTime = readResult.getReadTime();
5051
}
5152
} catch (IOException e) {
5253
clear();
53-
logger.warn("Failed reading '" + fileReader.getEntryPath() + "'!", e);
54+
logger.warn("Failed reading '" + fileReader.getFilePath() + "'!", e);
5455
}
5556
}
5657
}

hugegraph-store/hg-store-node/src/main/java/org/apache/hugegraph/store/node/metrics/ProcfsMetrics.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
import io.micrometer.core.instrument.Gauge;
2121
import io.micrometer.core.instrument.MeterRegistry;
2222

23-
/**
24-
* 2022/3/1
25-
*
26-
* @version 0.1.0
27-
*/
2823
public class ProcfsMetrics {
2924

3025
public final static String PREFIX = "process_memory";
@@ -47,19 +42,20 @@ private static void registerMeters() {
4742
}
4843

4944
private static void registerProcessGauge() {
50-
Gauge.builder(PREFIX + ".rss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.RSS))
51-
.register(registry);
45+
Gauge.builder(PREFIX + ".rss.bytes",
46+
() -> smaps.getMetric(SystemMemoryStats.MetricKey.RSS)).register(registry);
5247

53-
Gauge.builder(PREFIX + ".pss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.PSS))
54-
.register(registry);
48+
Gauge.builder(PREFIX + ".pss.bytes",
49+
() -> smaps.getMetric(SystemMemoryStats.MetricKey.PSS)).register(registry);
5550

56-
Gauge.builder(PREFIX + ".vss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.VSS))
57-
.register(registry);
51+
Gauge.builder(PREFIX + ".vss.bytes",
52+
() -> smaps.getMetric(SystemMemoryStats.MetricKey.VSS)).register(registry);
5853

59-
Gauge.builder(PREFIX + ".swap.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.SWAP))
60-
.register(registry);
54+
Gauge.builder(PREFIX + ".swap.bytes",
55+
() -> smaps.getMetric(SystemMemoryStats.MetricKey.SWAP)).register(registry);
6156

62-
Gauge.builder(PREFIX + ".swappss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.SWAPPSS))
57+
Gauge.builder(PREFIX + ".swappss.bytes",
58+
() -> smaps.getMetric(SystemMemoryStats.MetricKey.SWAPPSS))
6359
.register(registry);
6460
}
6561

hugegraph-store/hg-store-node/src/main/java/org/apache/hugegraph/store/node/metrics/ProcfsReader.java

-142
This file was deleted.

hugegraph-store/hg-store-node/src/main/java/org/apache/hugegraph/store/node/metrics/SystemMemoryStats.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public class SystemMemoryStats extends ProcfsRecord {
2929
private final Map<MetricKey, AtomicLong> metrics = new HashMap<>();
3030

3131
public SystemMemoryStats() {
32-
super(ProcfsReader.getInstance("smaps"));
32+
super(ProcFileHandler.getInstance("smaps"));
3333
}
3434

35-
/* default */ SystemMemoryStats(ProcfsReader reader) {
35+
/* default */ SystemMemoryStats(ProcFileHandler reader) {
3636
super(reader);
3737
}
3838

0 commit comments

Comments
 (0)