Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class MetricSearcher {
private static final Charset defaultCharset = Charset.forName(SentinelConfig.charset());
private final MetricsReader metricsReader;

private String baseDir;
private String baseFileName;
private final String baseDir;
private final String baseFileName;

private Position lastPosition = new Position();

Expand All @@ -64,10 +64,11 @@ public MetricSearcher(String baseDir, String baseFileName, Charset charset) {
if (charset == null) {
throw new IllegalArgumentException("charset can't be null");
}
this.baseDir = baseDir;
String baseDirContainer = baseDir;
if (!baseDir.endsWith(File.separator)) {
this.baseDir += File.separator;
baseDirContainer += File.separator;
}
this.baseDir = baseDirContainer;
this.baseFileName = baseFileName;
metricsReader = new MetricsReader(charset);
}
Expand Down Expand Up @@ -117,6 +118,7 @@ public synchronized List<MetricNode> findByTimeAndResource(long beginTimeMs, lon
// + "], " + identity + ")");
int i = 0;
long offsetInIndex = 0;
//noinspection StatementWithEmptyBody
if (validPosition(beginTimeMs)) {
i = fileNames.indexOf(lastPosition.metricFileName);
if (i == -1) {
Expand Down Expand Up @@ -171,22 +173,13 @@ private boolean validPosition(long beginTimeMs) {
if (!new File(lastPosition.indexFileName).exists()) {
return false;
}
FileInputStream in = null;
try {
in = new FileInputStream(lastPosition.indexFileName);
try (FileInputStream in = new FileInputStream(lastPosition.indexFileName)) {
in.getChannel().position(lastPosition.offsetInIndex);
DataInputStream indexIn = new DataInputStream(in);
// timestamp(second) in the specific position == that we cached
return indexIn.readLong() == lastPosition.second;
} catch (Exception e) {
return false;
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ignore) {
}
}
}
}

Expand All @@ -200,13 +193,12 @@ private long findOffset(long beginTime, String metricFileName,
long beginSecond = beginTime / 1000;
FileInputStream in = new FileInputStream(idxFileName);
in.getChannel().position(offsetInIndex);
DataInputStream indexIn = new DataInputStream(in);
long offset;
try {
try (DataInputStream indexIn = new DataInputStream(in)) {
long offset;
long second;
lastPosition.offsetInIndex = in.getChannel().position();
while ((second = indexIn.readLong()) < beginSecond) {
offset = indexIn.readLong();
indexIn.readLong();
lastPosition.offsetInIndex = in.getChannel().position();
}
offset = indexIn.readLong();
Expand All @@ -216,8 +208,6 @@ private long findOffset(long beginTime, String metricFileName,
return offset;
} catch (EOFException ignore) {
return -1;
} finally {
indexIn.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ public MetricsReader(Charset charset) {
}

/**
* @return if should continue read, return true, else false.
* @return if it should continue read, return true, else false.
*/
boolean readMetricsInOneFileByEndTime(List<MetricNode> list, String fileName, long offset,
long beginTimeMs, long endTimeMs, String identity) throws Exception {
FileInputStream in = null;
long beginSecond = beginTimeMs / 1000;
long endSecond = endTimeMs / 1000;
try {
in = new FileInputStream(fileName);
try (FileInputStream in = new FileInputStream(fileName)) {
in.getChannel().position(offset);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
String line;
Expand All @@ -72,10 +70,6 @@ boolean readMetricsInOneFileByEndTime(List<MetricNode> list, String fileName, lo
return false;
}
}
} finally {
if (in != null) {
in.close();
}
}
return true;
}
Expand All @@ -89,9 +83,7 @@ void readMetricsInOneFile(List<MetricNode> list, String fileName,
if (list.size() > 0) {
lastSecond = list.get(list.size() - 1).getTimestamp() / 1000;
}
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
try (FileInputStream in = new FileInputStream(fileName)) {
in.getChannel().position(offset);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
String line;
Expand All @@ -108,10 +100,6 @@ void readMetricsInOneFile(List<MetricNode> list, String fileName,
}
lastSecond = currentSecond;
}
} finally {
if (in != null) {
in.close();
}
}
}

Expand All @@ -121,7 +109,7 @@ void readMetricsInOneFile(List<MetricNode> list, String fileName,
*/
List<MetricNode> readMetricsByEndTime(List<String> fileNames, int pos, long offset,
long beginTimeMs, long endTimeMs, String identity) throws Exception {
List<MetricNode> list = new ArrayList<MetricNode>(1024);
List<MetricNode> list = new ArrayList<>(1024);
if (readMetricsInOneFileByEndTime(list, fileNames.get(pos++), offset, beginTimeMs, endTimeMs, identity)) {
while (pos < fileNames.size()
&& readMetricsInOneFileByEndTime(list, fileNames.get(pos++), 0, beginTimeMs, endTimeMs, identity)) {
Expand All @@ -132,7 +120,7 @@ && readMetricsInOneFileByEndTime(list, fileNames.get(pos++), 0, beginTimeMs, end

List<MetricNode> readMetrics(List<String> fileNames, int pos,
long offset, int recommendLines) throws Exception {
List<MetricNode> list = new ArrayList<MetricNode>(recommendLines);
List<MetricNode> list = new ArrayList<>(recommendLines);
readMetricsInOneFile(list, fileNames.get(pos++), offset, recommendLines);
while (list.size() < recommendLines && pos < fileNames.size()) {
readMetricsInOneFile(list, fileNames.get(pos++), 0, recommendLines);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import static org.apache.curator.framework.CuratorFrameworkFactory.newClient;

/**
* Zookeeper config sender for demo
*
Expand All @@ -31,22 +33,21 @@ public static void main(String[] args) throws Exception {
+ " }\n"
+ "]";

CuratorFramework zkClient = CuratorFrameworkFactory.newClient(remoteAddress, new ExponentialBackoffRetry(SLEEP_TIME, RETRY_TIMES));
zkClient.start();
String path = getPath(groupId, dataId);
Stat stat = zkClient.checkExists().forPath(path);
if (stat == null) {
zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null);
}
zkClient.setData().forPath(path, rule.getBytes());
try(CuratorFramework zkClient = newClient(remoteAddress, new ExponentialBackoffRetry(SLEEP_TIME, RETRY_TIMES))) {
zkClient.start();
String path = getPath(groupId, dataId);
Stat stat = zkClient.checkExists().forPath(path);
if (stat == null) {
zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null);
}
zkClient.setData().forPath(path, rule.getBytes());

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

zkClient.close();
}

private static String getPath(String groupId, String dataId) {
Expand Down