imay commented on a change in pull request #450: Add EsScanNode URL: https://github.com/apache/incubator-doris/pull/450#discussion_r243933930
########## File path: be/src/exec/es_scan_node.cpp ########## @@ -0,0 +1,667 @@ +// 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 "es_scan_node.h" + +#include <string> +#include <boost/algorithm/string.hpp> +#include <gutil/strings/substitute.h> + +#include "gen_cpp/PlanNodes_types.h" +#include "gen_cpp/Exprs_types.h" +#include "runtime/runtime_state.h" +#include "runtime/row_batch.h" +#include "runtime/string_value.h" +#include "runtime/tuple_row.h" +#include "runtime/client_cache.h" +#include "util/runtime_profile.h" +#include "util/debug_util.h" +#include "service/backend_options.h" +#include "olap/olap_common.h" +#include "olap/utils.h" +#include "exprs/expr_context.h" +#include "exprs/expr.h" +#include "exprs/slot_ref.h" + +namespace doris { + +// $0 = column type (e.g. INT) +const string ERROR_INVALID_COL_DATA = "Data source returned inconsistent column data. " + "Expected value of type $0 based on column metadata. This likely indicates a " + "problem with the data source library."; +const string ERROR_MEM_LIMIT_EXCEEDED = "DataSourceScanNode::$0() failed to allocate " + "$1 bytes for $2."; + +EsScanNode::EsScanNode( + ObjectPool* pool, + const TPlanNode& tnode, + const DescriptorTbl& descs) : + ScanNode(pool, tnode, descs), + _tuple_id(tnode.es_scan_node.tuple_id), + _scan_range_idx(0) { + if (tnode.es_scan_node.__isset.properties) { + _properties = tnode.es_scan_node.properties; + } +} + +EsScanNode::~EsScanNode() { +} + +Status EsScanNode::prepare(RuntimeState* state) { + VLOG(1) << "EsScanNode::Prepare"; + + RETURN_IF_ERROR(ScanNode::prepare(state)); + _tuple_desc = state->desc_tbl().get_tuple_descriptor(_tuple_id); + if (_tuple_desc == nullptr) { + std::stringstream ss; + ss << "es tuple descriptor is null, _tuple_id=" << _tuple_id; + LOG(WARNING) << ss.str(); + return Status(ss.str()); + } + _env = state->exec_env(); + + return Status::OK; +} + +Status EsScanNode::open(RuntimeState* state) { + VLOG(1) << "EsScanNode::Open"; + + RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::OPEN)); + RETURN_IF_CANCELLED(state); + SCOPED_TIMER(_runtime_profile->total_time_counter()); + RETURN_IF_ERROR(ExecNode::open(state)); + + // TExtOpenParams.row_schema + vector<TExtColumnDesc> cols; + for (const SlotDescriptor* slot : _tuple_desc->slots()) { + TExtColumnDesc col; + col.__set_name(slot->col_name()); + col.__set_type(slot->type().to_thrift()); + cols.emplace_back(std::move(col)); + } + TExtTableSchema row_schema; + row_schema.cols = std::move(cols); + row_schema.__isset.cols = true; + + // TExtOpenParams.predicates + vector<vector<TExtPredicate> > predicates; + vector<int> conjunct_idxes; + for (int i = 0; i < _conjunct_ctxs.size(); ++i) { + VLOG(1) << "conjunct: " << _conjunct_ctxs[i]->root()->debug_string(); + vector<TExtPredicate> disjuncts; + if (get_disjuncts(_conjunct_ctxs[i], _conjunct_ctxs[i]->root(), disjuncts)) { + predicates.push_back(std::move(disjuncts)); + conjunct_idxes.push_back(i); + } + } + + // open every scan range + int conjunct_accepted_times[_conjunct_ctxs.size()]; + for (int i = 0; i < _scan_ranges.size(); ++i) { + TEsScanRange es_scan_range = _scan_ranges[i]; + + // TExtOpenParams + TExtOpenParams params; + params.__set_query_id(state->query_id()); + _properties["index"] = es_scan_range.index; + if (es_scan_range.__isset.type) { + _properties["type"] = es_scan_range.type; + } + _properties["shard_id"] = std::to_string(es_scan_range.shard_id); + params.__set_properties(_properties); + params.__set_row_schema(row_schema); + params.__set_batch_size(state->batch_size()); + params.__set_predicates(predicates); + TExtOpenResult result; + + // check es host + if (es_scan_range.es_hosts.empty()) { + std::stringstream ss; + ss << "es fail to open: hosts empty"; + LOG(ERROR) << ss.str(); + return Status(ss.str()); + } + + // choose an es node, local is better + TNetworkAddress es_host_selected = es_scan_range.es_hosts[0]; + int selected_idx = 0; + for (int j = 0; j < es_scan_range.es_hosts.size(); j++) { + TNetworkAddress& es_host = es_scan_range.es_hosts[j]; + if (es_host.hostname == BackendOptions::get_localhost()) { + es_host_selected = es_host; + selected_idx = j; + break; + } + } + + // if shard not found, try other nodes + Status status = open_es(es_host_selected, result, params); + if (status.code() == TStatusCode::ES_SHARD_NOT_FOUND) { + for (int j = 0; j < es_scan_range.es_hosts.size(); j++) { + if (j == selected_idx) continue; + es_host_selected = es_scan_range.es_hosts[j]; + status = open_es(es_host_selected, result, params); + if (status.code() == TStatusCode::ES_SHARD_NOT_FOUND) { + continue; + } else { + break; + } + } + } Review comment: you can use ``` for (int i = 0; i < 2; ++i) { for (auto& host : es_scan_range.es_host) { if (i == 0 && host != localhost) { continue; } else (i == 1 && j == selected_idx) { continue; } } } ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@doris.apache.org For additional commands, e-mail: dev-h...@doris.apache.org