forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes_scan_reader.cpp
More file actions
269 lines (250 loc) · 11.2 KB
/
Copy pathes_scan_reader.cpp
File metadata and controls
269 lines (250 loc) · 11.2 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include "exec/es/es_scan_reader.h"
#include <stdlib.h>
#include <map>
#include <sstream>
#include <string>
#include "common/config.h"
#include "common/logging.h"
#include "common/status.h"
#include "exec/es/es_scroll_parser.h"
#include "exec/es/es_scroll_query.h"
#include "http/http_method.h"
namespace doris {
// hits.hits._id used for obtain ES document `_id`
const std::string SOURCE_SCROLL_SEARCH_FILTER_PATH =
"filter_path=_scroll_id,hits.hits._source,hits.total,hits.hits._id";
// hits.hits._score used for processing field not exists in one batch
const std::string DOCVALUE_SCROLL_SEARCH_FILTER_PATH =
"filter_path=_scroll_id,hits.total,hits.hits._score,hits.hits.fields";
const std::string REQUEST_PREFERENCE_PREFIX = "&preference=_shards:";
const std::string REQUEST_SEARCH_SCROLL_PATH = "/_search/scroll";
const std::string REQUEST_SEPARATOR = "/";
ESScanReader::ESScanReader(const std::string& target,
const std::map<std::string, std::string>& props, bool doc_value_mode)
: _scroll_keep_alive(config::es_scroll_keepalive),
_http_timeout_ms(config::es_http_timeout_ms),
_doc_value_mode(doc_value_mode) {
_target = target;
_index = props.at(KEY_INDEX);
if (props.find(KEY_TYPE) != props.end()) {
_type = props.at(KEY_TYPE);
}
if (props.find(KEY_USER_NAME) != props.end()) {
_user_name = props.at(KEY_USER_NAME);
}
if (props.find(KEY_PASS_WORD) != props.end()) {
_passwd = props.at(KEY_PASS_WORD);
}
if (props.find(KEY_SHARD) != props.end()) {
_shards = props.at(KEY_SHARD);
}
if (props.find(KEY_QUERY) != props.end()) {
_query = props.at(KEY_QUERY);
}
if (props.find(KEY_HTTP_SSL_ENABLED) != props.end()) {
std::istringstream(props.at(KEY_HTTP_SSL_ENABLED)) >> std::boolalpha >> _use_ssl_client;
}
std::string batch_size_str = props.at(KEY_BATCH_SIZE);
_batch_size = atoi(batch_size_str.c_str());
std::string filter_path =
_doc_value_mode ? DOCVALUE_SCROLL_SEARCH_FILTER_PATH : SOURCE_SCROLL_SEARCH_FILTER_PATH;
// When shard_id is negative(-1), the request will be sent to ES without shard preference.
int32 shard_id = std::stoi(_shards);
if (props.find(KEY_TERMINATE_AFTER) != props.end()) {
_exactly_once = true;
std::stringstream scratch;
// just send a normal search against the elasticsearch with additional terminate_after param to achieve terminate early effect when limit take effect
if (_type.empty()) {
if (shard_id < 0) {
scratch << _target << REQUEST_SEPARATOR << _index << "/_search?" << filter_path;
} else {
// `terminate_after` and `size` can not be used together in scroll request of ES 8.x
scratch << _target << REQUEST_SEPARATOR << _index << "/_search?"
<< REQUEST_PREFERENCE_PREFIX << _shards << "&" << filter_path;
}
} else {
if (shard_id < 0) {
scratch << _target << REQUEST_SEPARATOR << _index << REQUEST_SEPARATOR << _type
<< "/_search?"
<< "terminate_after=" << props.at(KEY_TERMINATE_AFTER) << "&"
<< filter_path;
} else {
scratch << _target << REQUEST_SEPARATOR << _index << REQUEST_SEPARATOR << _type
<< "/_search?"
<< "terminate_after=" << props.at(KEY_TERMINATE_AFTER)
<< REQUEST_PREFERENCE_PREFIX << _shards << "&" << filter_path;
}
}
_search_url = scratch.str();
} else {
_exactly_once = false;
std::stringstream scratch;
// scroll request for scanning
// add terminate_after for the first scroll to avoid decompress all postings list
if (_type.empty()) {
if (shard_id < 0) {
scratch << _target << REQUEST_SEPARATOR << _index << "/_search?"
<< "scroll=" << _scroll_keep_alive << "&" << filter_path;
} else {
// `terminate_after` and `size` can not be used together in scroll request of ES 8.x
scratch << _target << REQUEST_SEPARATOR << _index << "/_search?"
<< "scroll=" << _scroll_keep_alive << REQUEST_PREFERENCE_PREFIX << _shards
<< "&" << filter_path;
}
} else {
if (shard_id < 0) {
scratch << _target << REQUEST_SEPARATOR << _index << REQUEST_SEPARATOR << _type
<< "/_search?"
<< "scroll=" << _scroll_keep_alive << "&" << filter_path
<< "&terminate_after=" << batch_size_str;
} else {
scratch << _target << REQUEST_SEPARATOR << _index << REQUEST_SEPARATOR << _type
<< "/_search?"
<< "scroll=" << _scroll_keep_alive << REQUEST_PREFERENCE_PREFIX << _shards
<< "&" << filter_path << "&terminate_after=" << batch_size_str;
}
}
_init_scroll_url = scratch.str();
_next_scroll_url = _target + REQUEST_SEARCH_SCROLL_PATH + "?" + filter_path;
}
_eos = false;
}
ESScanReader::~ESScanReader() {}
Status ESScanReader::open() {
_is_first = true;
// we do not enable set_fail_on_error for ES http request to get more detail error messages
bool set_fail_on_error = false;
if (_exactly_once) {
RETURN_IF_ERROR(_network_client.init(_search_url, set_fail_on_error));
LOG(INFO) << "search request URL: " << _search_url;
} else {
RETURN_IF_ERROR(_network_client.init(_init_scroll_url, set_fail_on_error));
LOG(INFO) << "First scroll request URL: " << _init_scroll_url;
}
_network_client.set_basic_auth(_user_name, _passwd);
_network_client.set_content_type("application/json");
_network_client.set_timeout_ms(_http_timeout_ms);
if (_use_ssl_client) {
_network_client.use_untrusted_ssl();
}
// phase open, we cached the first response for `get_next` phase
Status status = _network_client.execute_post_request(_query, &_cached_response);
if (!status.ok() || _network_client.get_http_status() != 200) {
std::stringstream ss;
ss << "Failed to connect to ES server, errmsg is: " << status
<< ", response: " << _cached_response;
LOG(WARNING) << ss.str();
return Status::InternalError(ss.str());
}
VLOG_CRITICAL << "open _cached response: " << _cached_response;
return Status::OK();
}
Status ESScanReader::get_next(bool* scan_eos, std::unique_ptr<ScrollParser>& scroll_parser) {
std::string response;
// if is first scroll request, should return the cached response
*scan_eos = true;
if (_eos) {
return Status::OK();
}
if (_is_first) {
response = _cached_response;
_is_first = false;
} else {
if (_exactly_once) {
return Status::OK();
}
// we do not enable set_fail_on_error for ES http request to get more detail error messages
bool set_fail_on_error = false;
RETURN_IF_ERROR(_network_client.init(_next_scroll_url, set_fail_on_error));
_network_client.set_basic_auth(_user_name, _passwd);
_network_client.set_content_type("application/json");
_network_client.set_timeout_ms(_http_timeout_ms);
if (_use_ssl_client) {
_network_client.use_untrusted_ssl();
}
RETURN_IF_ERROR(_network_client.execute_post_request(
ESScrollQueryBuilder::build_next_scroll_body(_scroll_id, _scroll_keep_alive),
&response));
long status = _network_client.get_http_status();
if (status == 404) {
LOG(WARNING) << "request scroll search failure 404["
<< ", response: " << (response.empty() ? "empty response" : response)
<< "]";
return Status::InternalError("No search context found for {}", _scroll_id);
}
if (status != 200) {
LOG(WARNING) << "request scroll search failure["
<< "http status: " << status
<< ", response: " << (response.empty() ? "empty response" : response)
<< "]";
return Status::InternalError("request scroll search failure: {}",
(response.empty() ? "empty response" : response));
}
}
scroll_parser.reset(new ScrollParser(_doc_value_mode));
VLOG_CRITICAL << "get_next request ES, returned response: " << response;
Status status = scroll_parser->parse(response, _exactly_once);
if (!status.ok()) {
_eos = true;
LOG(WARNING) << status;
return status;
}
// request ES just only once
if (_exactly_once) {
_eos = true;
} else {
_scroll_id = scroll_parser->get_scroll_id();
if (scroll_parser->get_size() == 0) {
_eos = true;
return Status::OK();
}
_eos = scroll_parser->get_size() < _batch_size;
}
*scan_eos = false;
return Status::OK();
}
Status ESScanReader::close() {
if (_scroll_id.empty()) {
return Status::OK();
}
std::string scratch_target = _target + REQUEST_SEARCH_SCROLL_PATH;
// we do not enable set_fail_on_error for ES http request to get more detail error messages
bool set_fail_on_error = false;
RETURN_IF_ERROR(_network_client.init(scratch_target, set_fail_on_error));
_network_client.set_basic_auth(_user_name, _passwd);
_network_client.set_method(DELETE);
_network_client.set_content_type("application/json");
_network_client.set_timeout_ms(_http_timeout_ms);
if (_use_ssl_client) {
_network_client.use_untrusted_ssl();
}
std::string response;
RETURN_IF_ERROR(_network_client.execute_delete_request(
ESScrollQueryBuilder::build_clear_scroll_body(_scroll_id), &response));
long status = _network_client.get_http_status();
if (status == 200) {
return Status::OK();
} else {
LOG(WARNING) << "es_scan_reader delete scroll context failure["
<< "http status: " << status
<< ", response: " << (response.empty() ? "empty response" : response) << "]";
return Status::InternalError("es_scan_reader delete scroll context failure");
}
}
} // namespace doris