forked from y-scope/clp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputHandlerImpl.cpp
More file actions
216 lines (198 loc) · 7.48 KB
/
OutputHandlerImpl.cpp
File metadata and controls
216 lines (198 loc) · 7.48 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "OutputHandlerImpl.hpp"
#include <sstream>
#include <string>
#include <string_view>
#include <mongocxx/client.hpp>
#include <mongocxx/collection.hpp>
#include <mongocxx/exception/exception.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <msgpack.hpp>
#include <spdlog/spdlog.h>
#include "../clp/networking/socket_utils.hpp"
#include "../reducer/CountOperator.hpp"
#include "../reducer/network_utils.hpp"
#include "../reducer/Record.hpp"
#include "archive_constants.hpp"
#include "search/OutputHandler.hpp"
using std::string;
using std::string_view;
namespace clp_s {
void FileOutputHandler::write(
string_view message,
epochtime_t timestamp,
string_view archive_id,
int64_t log_event_idx
) {
static constexpr string_view cOrigFilePathPlaceholder{""};
msgpack::type::tuple<epochtime_t, string, string, string, int64_t> const
src(timestamp, message, cOrigFilePathPlaceholder, archive_id, log_event_idx);
msgpack::pack(m_file_writer, src);
}
NetworkOutputHandler::NetworkOutputHandler(
string const& host,
int port,
bool should_output_timestamp
)
: ::clp_s::search::OutputHandler(should_output_timestamp, true) {
m_socket_fd = clp::networking::connect_to_server(host, std::to_string(port));
if (-1 == m_socket_fd) {
SPDLOG_ERROR("Failed to connect to the server, errno={}", errno);
throw OperationFailed(ErrorCode::ErrorCodeFailureNetwork, __FILE__, __LINE__);
}
}
void NetworkOutputHandler::write(
string_view message,
epochtime_t timestamp,
string_view archive_id,
int64_t log_event_idx
) {
static constexpr string_view cOrigFilePathPlaceholder{""};
msgpack::type::tuple<epochtime_t, string, string, string, int64_t> const
src(timestamp, message, cOrigFilePathPlaceholder, archive_id, log_event_idx);
msgpack::sbuffer m;
msgpack::pack(m, src);
if (-1 == send(m_socket_fd, m.data(), m.size(), 0)) {
throw OperationFailed(ErrorCode::ErrorCodeFailureNetwork, __FILE__, __LINE__);
}
}
ResultsCacheOutputHandler::ResultsCacheOutputHandler(
string const& uri,
string const& collection,
uint64_t batch_size,
uint64_t max_num_results,
string dataset,
bool should_output_timestamp
)
: ::clp_s::search::OutputHandler(should_output_timestamp, true),
m_batch_size(batch_size),
m_max_num_results(max_num_results),
m_dataset(std::move(dataset)) {
try {
auto mongo_uri = mongocxx::uri(uri);
m_client = mongocxx::client(mongo_uri);
m_collection = m_client[mongo_uri.database()][collection];
m_results.reserve(m_batch_size);
} catch (mongocxx::exception const& e) {
throw OperationFailed(ErrorCode::ErrorCodeBadParamDbUri, __FILENAME__, __LINE__);
}
}
ErrorCode ResultsCacheOutputHandler::flush() {
size_t count = 0;
while (false == m_latest_results.empty()) {
auto result = std::move(*m_latest_results.top());
m_latest_results.pop();
try {
m_results.emplace_back(
std::move(
bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp(
constants::results_cache::search::cOrigFilePath,
std::move(result.original_path)
),
bsoncxx::builder::basic::kvp(
constants::results_cache::search::cMessage,
std::move(result.message)
),
bsoncxx::builder::basic::kvp(
constants::results_cache::search::cTimestamp,
result.timestamp
),
bsoncxx::builder::basic::kvp(
constants::results_cache::search::cArchiveId,
std::move(result.archive_id)
),
bsoncxx::builder::basic::kvp(
constants::results_cache::search::cLogEventIx,
result.log_event_idx
),
bsoncxx::builder::basic::kvp(
constants::results_cache::search::cDataset,
std::move(result.dataset)
)
)
)
);
count++;
if (count == m_batch_size) {
m_collection.insert_many(m_results);
m_results.clear();
count = 0;
}
} catch (mongocxx::exception const& e) {
return ErrorCode::ErrorCodeFailureDbBulkWrite;
}
}
try {
if (false == m_results.empty()) {
m_collection.insert_many(m_results);
m_results.clear();
}
} catch (mongocxx::exception const& e) {
return ErrorCode::ErrorCodeFailureDbBulkWrite;
}
return ErrorCode::ErrorCodeSuccess;
}
void ResultsCacheOutputHandler::write(
string_view message,
epochtime_t timestamp,
string_view archive_id,
int64_t log_event_idx
) {
if (m_latest_results.size() < m_max_num_results) {
m_latest_results.emplace(
std::make_unique<QueryResult>(
string_view{},
message,
timestamp,
archive_id,
log_event_idx,
m_dataset
)
);
} else if (m_latest_results.top()->timestamp < timestamp) {
m_latest_results.pop();
m_latest_results.emplace(
std::make_unique<QueryResult>(
string_view{},
message,
timestamp,
archive_id,
log_event_idx,
m_dataset
)
);
}
}
CountOutputHandler::CountOutputHandler(int reducer_socket_fd)
: ::clp_s::search::OutputHandler(false, false),
m_reducer_socket_fd(reducer_socket_fd),
m_pipeline(reducer::PipelineInputMode::InterStage) {
m_pipeline.add_pipeline_stage(std::make_shared<reducer::CountOperator>());
}
void CountOutputHandler::write(string_view message) {
m_pipeline.push_record(reducer::EmptyRecord{});
}
ErrorCode CountOutputHandler::finish() {
if (false
== reducer::send_pipeline_results(m_reducer_socket_fd, std::move(m_pipeline.finish())))
{
return ErrorCode::ErrorCodeFailureNetwork;
}
return ErrorCode::ErrorCodeSuccess;
}
ErrorCode CountByTimeOutputHandler::finish() {
if (false
== reducer::send_pipeline_results(
m_reducer_socket_fd,
std::make_unique<reducer::Int64Int64MapRecordGroupIterator>(
m_bucket_counts,
reducer::CountOperator::cRecordElementKey
)
))
{
return ErrorCode::ErrorCodeFailureNetwork;
}
return ErrorCode::ErrorCodeSuccess;
}
} // namespace clp_s