|
| 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 | +} |
0 commit comments