Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored MetricSearcher #103

Merged
Merged
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 @@ -15,14 +15,11 @@
*/
package com.alibaba.csp.sentinel.node.metric;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.csp.sentinel.config.SentinelConfig;
Expand All @@ -37,18 +34,13 @@
public class MetricSearcher {

private static final Charset defaultCharset = Charset.forName(SentinelConfig.charset());
private final MetricsReader metricsReader;

private String baseDir;
private String baseFileName;
private Charset charset;

private Position lastPosition = new Position();

/**
* avoid OOM in any case
*/
private static final int maxLinesReturn = 100000;

/**
* @param baseDir metric文件所在目录
* @param baseFileName metric文件名的关键字,比如 alihot-metrics.log
Expand Down Expand Up @@ -77,7 +69,7 @@ public MetricSearcher(String baseDir, String baseFileName, Charset charset) {
this.baseDir += File.separator;
}
this.baseFileName = baseFileName;
this.charset = charset;
metricsReader = new MetricsReader(charset);
}

/**
Expand Down Expand Up @@ -107,7 +99,7 @@ public synchronized List<MetricNode> find(long beginTimeMs, int recommendLines)
MetricWriter.formIndexFileName(fileName), offsetInIndex);
offsetInIndex = 0;
if (offset != -1) {
return readMetrics(fileNames, i, offset, recommendLines);
return metricsReader.readMetrics(fileNames, i, offset, recommendLines);
}
}
return null;
Expand Down Expand Up @@ -139,10 +131,10 @@ public synchronized List<MetricNode> findByTimeAndResource(long beginTimeMs, lon
for (; i < fileNames.size(); i++) {
String fileName = fileNames.get(i);
long offset = findOffset(beginTimeMs, fileName,
fileName + MetricWriter.METRIC_FILE_INDEX_SUFFIX, offsetInIndex);
MetricWriter.formIndexFileName(fileName), offsetInIndex);
offsetInIndex = 0;
if (offset != -1) {
return readMetricsByEndTime(fileNames, i, offset, endTimeMs, identity);
return metricsReader.readMetricsByEndTime(fileNames, i, offset, endTimeMs, identity);
}
}
return null;
Expand Down Expand Up @@ -198,103 +190,6 @@ private boolean validPosition(long beginTimeMs) {
}
}

/**
* @return if should continue read, return true, else false.
*/
private boolean readMetricsInOneFileByEndTime(List<MetricNode> list, String fileName,
long offset, long endTimeMs, String identity) throws Exception {
FileInputStream in = null;
long endSecond = endTimeMs / 1000;
try {
in = new FileInputStream(fileName);
in.getChannel().position(offset);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
String line;
while ((line = reader.readLine()) != null) {
MetricNode node = MetricNode.fromFatString(line);
long currentSecond = node.getTimestamp() / 1000;
if (currentSecond <= endSecond) {
// read all
if (identity == null) {
list.add(node);
} else if (node.getResource().equals(identity)) {
list.add(node);
}
} else {
return false;
}
if (list.size() >= maxLinesReturn) {
return false;
}
}
} finally {
if (in != null) {
in.close();
}
}
return true;
}

private void readMetricsInOneFile(List<MetricNode> list, String fileName,
long offset, int recommendLines) throws Exception {
//if(list.size() >= recommendLines){
// return;
//}
long lastSecond = -1;
if (list.size() > 0) {
lastSecond = list.get(list.size() - 1).getTimestamp() / 1000;
}
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
in.getChannel().position(offset);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
String line;
while ((line = reader.readLine()) != null) {
MetricNode node = MetricNode.fromFatString(line);
long currentSecond = node.getTimestamp() / 1000;

if (list.size() < recommendLines) {
list.add(node);
} else if (currentSecond == lastSecond) {
list.add(node);
} else {
break;
}
lastSecond = currentSecond;
}
} finally {
if (in != null) {
in.close();
}
}
}

private List<MetricNode> readMetrics(List<String> fileNames, int pos,
long offset, int recommendLines) throws Exception {
List<MetricNode> list = new ArrayList<MetricNode>(recommendLines);
readMetricsInOneFile(list, fileNames.get(pos++), offset, recommendLines);
while (list.size() < recommendLines && pos < fileNames.size()) {
readMetricsInOneFile(list, fileNames.get(pos++), 0, recommendLines);
}
return list;
}

/**
* When identity is null, all metric between the time intervalMs will be read, otherwise, only the specific
* identity will be read.
*/
private List<MetricNode> readMetricsByEndTime(List<String> fileNames, int pos,
long offset, long endTimeMs, String identity) throws Exception {
List<MetricNode> list = new ArrayList<MetricNode>(1024);
if (readMetricsInOneFileByEndTime(list, fileNames.get(pos++), offset, endTimeMs, identity)) {
while (pos < fileNames.size()
&& readMetricsInOneFileByEndTime(list, fileNames.get(pos++), 0, endTimeMs, identity)) {
}
}
return list;
}

private long findOffset(long beginTime, String metricFileName,
String idxFileName, long offsetInIndex) throws Exception {
lastPosition.metricFileName = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.node.metric;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Copyright and License here. Just copy the following:

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed 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.
 */


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

class MetricsReader {
/**
* avoid OOM in any case
*/
private static final int maxLinesReturn = 100000;
private Charset charset;

public MetricsReader(Charset charset) {
this.charset = charset;
}

/**
* @return if should continue read, return true, else false.
*/
boolean readMetricsInOneFileByEndTime(List<MetricNode> list, String fileName,
long offset, long endTimeMs, String identity) throws Exception {
FileInputStream in = null;
long endSecond = endTimeMs / 1000;
try {
in = new FileInputStream(fileName);
in.getChannel().position(offset);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
String line;
while ((line = reader.readLine()) != null) {
MetricNode node = MetricNode.fromFatString(line);
long currentSecond = node.getTimestamp() / 1000;
if (currentSecond <= endSecond) {
// read all
if (identity == null) {
list.add(node);
} else if (node.getResource().equals(identity)) {
list.add(node);
}
} else {
return false;
}
if (list.size() >= maxLinesReturn) {
return false;
}
}
} finally {
if (in != null) {
in.close();
}
}
return true;
}

void readMetricsInOneFile(List<MetricNode> list, String fileName,
long offset, int recommendLines) throws Exception {
//if(list.size() >= recommendLines){
// return;
//}
long lastSecond = -1;
if (list.size() > 0) {
lastSecond = list.get(list.size() - 1).getTimestamp() / 1000;
}
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
in.getChannel().position(offset);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
String line;
while ((line = reader.readLine()) != null) {
MetricNode node = MetricNode.fromFatString(line);
long currentSecond = node.getTimestamp() / 1000;

if (list.size() < recommendLines) {
list.add(node);
} else if (currentSecond == lastSecond) {
list.add(node);
} else {
break;
}
lastSecond = currentSecond;
}
} finally {
if (in != null) {
in.close();
}
}
}

/**
* When identity is null, all metric between the time intervalMs will be read, otherwise, only the specific
* identity will be read.
*/
List<MetricNode> readMetricsByEndTime(List<String> fileNames, int pos,
long offset, long endTimeMs, String identity) throws Exception {
List<MetricNode> list = new ArrayList<MetricNode>(1024);
if (readMetricsInOneFileByEndTime(list, fileNames.get(pos++), offset, endTimeMs, identity)) {
while (pos < fileNames.size()
&& readMetricsInOneFileByEndTime(list, fileNames.get(pos++), 0, endTimeMs, identity)) {
}
}
return list;
}

List<MetricNode> readMetrics(List<String> fileNames, int pos,
long offset, int recommendLines) throws Exception {
List<MetricNode> list = new ArrayList<MetricNode>(recommendLines);
readMetricsInOneFile(list, fileNames.get(pos++), offset, recommendLines);
while (list.size() < recommendLines && pos < fileNames.size()) {
readMetricsInOneFile(list, fileNames.get(pos++), 0, recommendLines);
}
return list;
}
}