|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "information_schema/schema_tso_status_scanner.h" |
| 19 | + |
| 20 | +#include <gen_cpp/FrontendService_types.h> |
| 21 | + |
| 22 | +#include "core/block/block.h" |
| 23 | +#include "core/data_type/data_type_factory.hpp" |
| 24 | +#include "runtime/cluster_info.h" |
| 25 | +#include "runtime/exec_env.h" |
| 26 | +#include "runtime/runtime_state.h" |
| 27 | +#include "util/client_cache.h" |
| 28 | +#include "util/thrift_rpc_helper.h" |
| 29 | + |
| 30 | +namespace doris { |
| 31 | + |
| 32 | +std::vector<SchemaScanner::ColumnDesc> SchemaTsoStatusScanner::_s_tso_status_columns = { |
| 33 | + {"WINDOW_END_PHYSICAL_TIME", TYPE_BIGINT, sizeof(int64_t), true}, |
| 34 | + {"CURRENT_TSO", TYPE_BIGINT, sizeof(int64_t), true}, |
| 35 | + {"CURRENT_TSO_PHYSICAL_TIME", TYPE_BIGINT, sizeof(int64_t), true}, |
| 36 | + {"CURRENT_TSO_LOGICAL_COUNTER", TYPE_BIGINT, sizeof(int64_t), true}, |
| 37 | +}; |
| 38 | + |
| 39 | +SchemaTsoStatusScanner::SchemaTsoStatusScanner() |
| 40 | + : SchemaScanner(_s_tso_status_columns, TSchemaTableType::SCH_TSO_STATUS) {} |
| 41 | + |
| 42 | +SchemaTsoStatusScanner::~SchemaTsoStatusScanner() = default; |
| 43 | + |
| 44 | +Status SchemaTsoStatusScanner::start(RuntimeState* state) { |
| 45 | + _block_rows_limit = state->batch_size(); |
| 46 | + _rpc_timeout_ms = state->execution_timeout() * 1000; |
| 47 | + return Status::OK(); |
| 48 | +} |
| 49 | + |
| 50 | +Status SchemaTsoStatusScanner::_get_tso_status_block_from_fe() { |
| 51 | + TNetworkAddress master_addr = ExecEnv::GetInstance()->cluster_info()->master_fe_addr; |
| 52 | + |
| 53 | + TSchemaTableRequestParams schema_table_request_params; |
| 54 | + TFetchSchemaTableDataRequest request; |
| 55 | + request.__set_schema_table_name(TSchemaTableName::TSO_STATUS); |
| 56 | + request.__set_schema_table_params(schema_table_request_params); |
| 57 | + |
| 58 | + TFetchSchemaTableDataResult result; |
| 59 | + RETURN_IF_ERROR(ThriftRpcHelper::rpc<FrontendServiceClient>( |
| 60 | + master_addr.hostname, master_addr.port, |
| 61 | + [&request, &result](FrontendServiceConnection& client) { |
| 62 | + client->fetchSchemaTableData(result, request); |
| 63 | + }, |
| 64 | + _rpc_timeout_ms)); |
| 65 | + |
| 66 | + return _process_tso_status_result(result); |
| 67 | +} |
| 68 | + |
| 69 | +Status SchemaTsoStatusScanner::_process_tso_status_result( |
| 70 | + const TFetchSchemaTableDataResult& result) { |
| 71 | + Status status(Status::create(result.status)); |
| 72 | + if (!status.ok()) { |
| 73 | + LOG(WARNING) << "fetch TSO status from FE failed, errmsg=" << status; |
| 74 | + return status; |
| 75 | + } |
| 76 | + |
| 77 | + _tso_status_block = Block::create_unique(); |
| 78 | + for (int i = 0; i < _s_tso_status_columns.size(); ++i) { |
| 79 | + auto data_type = |
| 80 | + DataTypeFactory::instance().create_data_type(_s_tso_status_columns[i].type, true); |
| 81 | + _tso_status_block->insert(ColumnWithTypeAndName(data_type->create_column(), data_type, |
| 82 | + _s_tso_status_columns[i].name)); |
| 83 | + } |
| 84 | + |
| 85 | + _tso_status_block->reserve(result.data_batch.size()); |
| 86 | + for (const TRow& row : result.data_batch) { |
| 87 | + if (row.column_value.size() != _s_tso_status_columns.size()) { |
| 88 | + return Status::InternalError<false>( |
| 89 | + "TSO status schema does not match between FE and BE"); |
| 90 | + } |
| 91 | + for (int i = 0; i < _s_tso_status_columns.size(); ++i) { |
| 92 | + RETURN_IF_ERROR(insert_block_column(row.column_value[i], i, _tso_status_block.get(), |
| 93 | + _s_tso_status_columns[i].type)); |
| 94 | + } |
| 95 | + } |
| 96 | + _total_rows = static_cast<int>(_tso_status_block->rows()); |
| 97 | + return Status::OK(); |
| 98 | +} |
| 99 | + |
| 100 | +Status SchemaTsoStatusScanner::get_next_block_internal(Block* block, bool* eos) { |
| 101 | + if (!_is_init) { |
| 102 | + return Status::InternalError("Used before initialized."); |
| 103 | + } |
| 104 | + |
| 105 | + if (nullptr == block || nullptr == eos) { |
| 106 | + return Status::InternalError("input pointer is nullptr."); |
| 107 | + } |
| 108 | + |
| 109 | + if (_tso_status_block == nullptr) { |
| 110 | + RETURN_IF_ERROR(_get_tso_status_block_from_fe()); |
| 111 | + } |
| 112 | + |
| 113 | + if (_row_idx == _total_rows) { |
| 114 | + *eos = true; |
| 115 | + return Status::OK(); |
| 116 | + } |
| 117 | + |
| 118 | + int current_batch_rows = std::min(_block_rows_limit, _total_rows - _row_idx); |
| 119 | + ScopedMutableBlock scoped_mblock(block); |
| 120 | + auto& mblock = scoped_mblock.mutable_block(); |
| 121 | + RETURN_IF_ERROR(mblock.add_rows(_tso_status_block.get(), _row_idx, current_batch_rows)); |
| 122 | + _row_idx += current_batch_rows; |
| 123 | + |
| 124 | + *eos = _row_idx == _total_rows; |
| 125 | + return Status::OK(); |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace doris |
0 commit comments