/
metric_searcher.cc
148 lines (118 loc) · 4.05 KB
/
metric_searcher.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "sentinel-core/log/metric/metric_searcher.h"
#include <algorithm>
#include <iostream>
#include "sentinel-core/log/log_base.h"
#include "sentinel-core/log/metric/metric_writer.h"
#include "sentinel-core/utils/file_utils.h"
namespace Sentinel {
namespace Log {
bool MetricSearcher::ValidPosition(int64_t begin_time_ms) {
if (begin_time_ms / 1000 < last_pos_.second) {
return false;
}
if (last_pos_.index_file_name.size() == 0) {
return false;
}
// index file dose not exits
if (!Sentinel::Utils::FileUtils::FileExists(last_pos_.index_file_name)) {
return false;
}
std::ifstream in(last_pos_.index_file_name);
in.seekg(last_pos_.offset_in_index);
int64_t sec;
in >> sec;
in.close();
// timestamp(second) in the specific position == that we cached
return sec == last_pos_.second;
}
int64_t MetricSearcher::FindOffset(int64_t begin_time_ms,
const std::string &metric_file_name,
const std::string &idx_file_name,
int64_t offset_in_index) {
last_pos_.metric_file_name = "";
last_pos_.index_file_name = "";
if (!Sentinel::Utils::FileUtils::FileExists(idx_file_name)) {
return -1;
}
auto begin_second = begin_time_ms / 1000;
std::ifstream in(idx_file_name);
in.seekg(offset_in_index);
int64_t second;
int64_t tmp_offset = -1;
int64_t offset = -1;
while (!in.eof()) {
in >> second >> tmp_offset;
if (second < begin_second) {
last_pos_.offset_in_index = in.tellg();
} else {
offset = tmp_offset;
break;
}
}
in.close();
if (offset != -1) {
last_pos_.metric_file_name = metric_file_name;
last_pos_.index_file_name = idx_file_name;
}
return offset;
}
MetricSearcher::MetricSearcher(const std::string &base_dir,
const std::string &base_file_name)
: base_dir_(base_dir), base_file_name_(base_file_name) {}
std::vector<Stat::MetricItemSharedPtr> MetricSearcher::Find(
int64_t begin_time_ms, int recommend_lines) {
std::lock_guard<std::mutex> guard(lock_);
auto file_names = MetricWriter::ListMetricFiles(base_dir_, base_file_name_);
auto it = file_names.begin();
int64_t offset_in_index = 0;
if (ValidPosition(begin_time_ms)) {
auto lit = std::find(file_names.begin(), file_names.end(),
last_pos_.metric_file_name);
if (lit != file_names.end()) {
it = lit;
offset_in_index = last_pos_.offset_in_index;
}
}
for (; it < file_names.end(); ++it) {
auto file_name = *it;
long offset =
FindOffset(begin_time_ms, file_name,
MetricWriter::FormIndexFileName(file_name), offset_in_index);
offset_in_index = 0;
if (offset != -1) {
return metric_reader_.ReadMetrics(file_names, it - file_names.begin(),
offset, recommend_lines);
}
}
return std::vector<Stat::MetricItemSharedPtr>();
}
std::vector<Stat::MetricItemSharedPtr> MetricSearcher::FindByTimeAndResource(
int64_t begin_time_ms, int64_t end_time_ms, const std::string &identity) {
std::lock_guard<std::mutex> guard(lock_);
auto file_names = MetricWriter::ListMetricFiles(base_dir_, base_file_name_);
auto it = file_names.begin();
int64_t offset_in_index = 0;
if (ValidPosition(begin_time_ms)) {
auto lit = std::find(file_names.begin(), file_names.end(),
last_pos_.metric_file_name);
if (lit != file_names.end()) {
it = lit;
offset_in_index = last_pos_.offset_in_index;
}
}
for (; it < file_names.end(); ++it) {
auto file_name = *it;
long offset =
FindOffset(begin_time_ms, file_name,
MetricWriter::FormIndexFileName(file_name), offset_in_index);
offset_in_index = 0;
if (offset != -1) {
return metric_reader_.ReadMetricsByEndTime(
file_names, it - file_names.begin(), offset, begin_time_ms,
end_time_ms, identity);
}
}
return std::vector<Stat::MetricItemSharedPtr>();
}
} // namespace Log
} // namespace Sentinel