ob_table_query_processor.cpp 8.2 KB
Newer Older
X
xj0 已提交
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
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#define USING_LOG_PREFIX SERVER
#include "ob_table_query_processor.h"
#include "ob_table_rpc_processor_util.h"
#include "observer/ob_service.h"
#include "storage/ob_partition_service.h"
#include "ob_table_end_trans_cb.h"
#include "sql/optimizer/ob_table_location.h"  // ObTableLocation
#include "lib/stat/ob_diagnose_info.h"
#include "lib/stat/ob_session_stat.h"

using namespace oceanbase::observer;
using namespace oceanbase::common;
using namespace oceanbase::table;
using namespace oceanbase::share;
using namespace oceanbase::sql;

ObTableQueryP::ObTableQueryP(const ObGlobalContext &gctx)
    :ObTableRpcProcessor(gctx),
     allocator_(ObModIds::TABLE_PROC),
     table_service_ctx_(allocator_),
     result_row_count_(0)
{
  // the streaming interface may return multi packet. The memory may be freed after the first packet has been sended.
  // the deserialization of arg_ is shallow copy, so we need deep copy data to processor
  set_preserve_recv_data();
}

int ObTableQueryP::deserialize()
{
  arg_.query_.set_deserialize_allocator(&allocator_);
  return ParentType::deserialize();
}

int ObTableQueryP::check_arg()
{
  int ret = OB_SUCCESS;
  if (!arg_.query_.is_valid()) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid table query request", K(ret), "query", arg_.query_);
X
xj0 已提交
52 53
  } else if (!(arg_.consistency_level_ == ObTableConsistencyLevel::STRONG ||
                arg_.consistency_level_ == ObTableConsistencyLevel::EVENTUAL)) {
X
xj0 已提交
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
    ret = OB_NOT_SUPPORTED;
    LOG_WARN("some options not supported yet", K(ret),
             "consistency_level", arg_.consistency_level_);
  }
  return ret;
}

void ObTableQueryP::audit_on_finish()
{
  audit_record_.consistency_level_ = ObTableConsistencyLevel::STRONG == arg_.consistency_level_ ?
      ObConsistencyLevel::STRONG : ObConsistencyLevel::WEAK;
  audit_record_.return_rows_ = result_.get_row_count();
  audit_record_.table_scan_ = true; // todo: exact judgement
  audit_record_.affected_rows_ = result_.get_row_count();
  audit_record_.try_cnt_ = retry_count_ + 1;
}

uint64_t ObTableQueryP::get_request_checksum()
{
  uint64_t checksum = 0;
  checksum = ob_crc64(checksum, arg_.table_name_.ptr(), arg_.table_name_.length());
  checksum = ob_crc64(checksum, &arg_.consistency_level_, sizeof(arg_.consistency_level_));
  const uint64_t op_checksum = arg_.query_.get_checksum();
  checksum = ob_crc64(checksum, &op_checksum, sizeof(op_checksum));
  return checksum;
}

void ObTableQueryP::reset_ctx()
{
  table_service_ctx_.reset_query_ctx(part_service_);
  need_retry_in_queue_ = false;
  result_row_count_ = 0;
  ObTableApiProcessorBase::reset_ctx();
}

ObTableAPITransCb *ObTableQueryP::new_callback(rpc::ObRequest *req)
{
  UNUSED(req);
  return nullptr;
}

int ObTableQueryP::get_partition_ids(uint64_t table_id, ObIArray<int64_t> &part_ids)
{
  int ret = OB_SUCCESS;
  uint64_t partition_id = arg_.partition_id_;
  if (OB_INVALID_ID == partition_id) {
    ret = OB_NOT_SUPPORTED;
    LOG_WARN("partitioned table not supported", K(ret), K(table_id));
  } else {
    if (OB_FAIL(part_ids.push_back(partition_id))) {
      LOG_WARN("failed to push back", K(ret));
    }
  }
  return ret;
}

O
obdev 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123
void ObTableQueryP::set_htable_compressor()
{
  int ret = OB_SUCCESS;
  // hbase model, compress the result packet
  ObCompressorType compressor_type = INVALID_COMPRESSOR;
  if (OB_FAIL(ObCompressorPool::get_instance().get_compressor_type(
                  GCONF.tableapi_transport_compress_func, compressor_type))) {
    compressor_type = INVALID_COMPRESSOR;
  } else if (NONE_COMPRESSOR == compressor_type) {
    compressor_type = INVALID_COMPRESSOR;
  }
  this->set_result_compress_type(compressor_type);
}

X
xj0 已提交
124 125 126 127 128 129 130 131 132
int ObTableQueryP::try_process()
{
  int ret = OB_SUCCESS;
  int64_t rpc_timeout = 0;
  if (NULL != rpc_pkt_) {
    rpc_timeout = rpc_pkt_->get_timeout();
  }
  const int64_t timeout_ts = get_timeout_ts();
  uint64_t &table_id = table_service_ctx_.param_table_id();
O
obdev 已提交
133
  table_service_ctx_.init_param(timeout_ts, this->get_trans_desc(), &allocator_,
X
xj0 已提交
134 135 136 137 138
                                false/*ignored*/,
                                arg_.entity_type_,
                                table::ObBinlogRowImageType::MINIMAL/*ignored*/);
  ObSEArray<int64_t, 1> part_ids;
  const bool is_readonly = true;
X
xj0 已提交
139
  const ObTableConsistencyLevel consistency_level = arg_.consistency_level_;
X
xj0 已提交
140 141 142 143 144 145 146 147 148 149
  ObTableQueryResultIterator *result_iterator = nullptr;
  int32_t result_count = 0;
  if (OB_FAIL(get_table_id(arg_.table_name_, arg_.table_id_, table_id))) {
    LOG_WARN("failed to get table id", K(ret));
  } else if (OB_FAIL(get_partition_ids(table_id, part_ids))) {
    LOG_WARN("failed to get part id", K(ret));
  } else if (1 != part_ids.count()) {
    ret = OB_NOT_SUPPORTED;
    LOG_WARN("should have one partition", K(ret), K(part_ids));
  } else if (FALSE_IT(table_service_ctx_.param_partition_id() = part_ids.at(0))) {
X
xj0 已提交
150
  } else if (OB_FAIL(start_trans(is_readonly, sql::stmt::T_SELECT, consistency_level, table_id, part_ids, timeout_ts))) {
X
xj0 已提交
151 152 153 154 155 156 157
    LOG_WARN("failed to start readonly transaction", K(ret));
  } else if (OB_FAIL(table_service_->execute_query(table_service_ctx_, arg_.query_,
                                                   result_, result_iterator))) {
    if (OB_TRY_LOCK_ROW_CONFLICT != ret) {
      LOG_WARN("failed to execute query", K(ret), K(table_id));
    }
  } else {
X
xj0 已提交
158 159
    if (arg_.query_.get_htable_filter().is_valid()) {
      // hbase model, compress the result packet
O
obdev 已提交
160
      set_htable_compressor();
X
xj0 已提交
161
    }
X
xj0 已提交
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
    // one_result references to result_
    ObTableQueryResult *one_result = nullptr;
    while (OB_SUCC(ret)) {
      ++result_count;
      // the last result_ does not need flush, it will be send automatically
      if (ObTimeUtility::current_time() > timeout_ts) {
        ret = OB_TRANS_TIMEOUT;
        LOG_WARN("exceed operatiton timeout", K(ret));
      } else if (OB_FAIL(result_iterator->get_next_result(one_result))) {
        if (OB_ITER_END != ret) {
          LOG_WARN("fail to get next result", K(ret));
        }
      } else if (result_iterator->has_more_result()) {
        if (OB_FAIL(this->flush())) {
          if (OB_ITER_END != ret) {
            LOG_WARN("failed to flush result packet", K(ret));
          } else {
            LOG_TRACE("user abort the stream rpc", K(ret));
          }
        } else {
          LOG_DEBUG("[yzfdebug] flush one result", K(ret), "row_count", result_.get_row_count());
          result_row_count_ += result_.get_row_count();
          result_.reset_except_property();
        }
      } else {
        // no more result
        result_row_count_ += result_.get_row_count();
        break;
      }
    }
    if (OB_ITER_END == ret) {
      ret = OB_SUCCESS;
    }
    LOG_DEBUG("[yzfdebug] last result", K(ret), "row_count", result_.get_row_count());
    NG_TRACE_EXT(tag1, OB_ID(return_rows), result_count, OB_ID(arg2), result_row_count_);
  }
  table_service_ctx_.destroy_result_iterator(part_service_);
  bool need_rollback_trans = (OB_SUCCESS != ret);
  int tmp_ret = ret;
  if (OB_FAIL(end_trans(need_rollback_trans, req_, timeout_ts))) {
    LOG_WARN("failed to end trans", K(ret), "rollback", need_rollback_trans);
  }
  ret = (OB_SUCCESS == tmp_ret) ? ret : tmp_ret;
  // record events
X
xj0 已提交
206 207 208 209 210
  if (arg_.query_.get_htable_filter().is_valid()) {
    stat_event_type_ = ObTableProccessType::TABLE_API_HBASE_QUERY; // hbase query
  } else {
    stat_event_type_ = ObTableProccessType::TABLE_API_TABLE_QUERY;// table query
  }
X
xj0 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
  audit_row_count_ = result_row_count_;

#ifndef NDEBUG
  // debug mode
  LOG_INFO("[TABLE] execute query", K(ret), K_(arg), K(rpc_timeout),
           K_(retry_count), K(result_count), K_(result_row_count));
#else
  // release mode
  LOG_TRACE("[TABLE] execute query", K(ret), K_(arg), K(rpc_timeout), K_(retry_count),
            "receive_ts", get_receive_timestamp(), K(result_count), K_(result_row_count));
#endif
  return ret;
}