ob_px_sqc_async_proxy.cpp 8.2 KB
Newer Older
O
oceanbase-admin 已提交
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
/**
 * 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 SQL_ENG

#include "sql/engine/px/ob_px_sqc_async_proxy.h"

namespace oceanbase {
using namespace common;
namespace sql {
/* ObSqcAsyncCB */
int ObSqcAsyncCB::process()
{
  ObThreadCondGuard guard(cond_);
  int ret = OB_SUCCESS;
  is_processed_ = true;
  ret = cond_.broadcast();
  return ret;
}

void ObSqcAsyncCB::on_invalid()
{
  ObThreadCondGuard guard(cond_);
  int ret = OB_SUCCESS;
  is_invalid_ = true;
  ret = cond_.broadcast();
  LOG_WARN("ObSqcAsyncCB invalid, check object serialization impl or oom", K(trace_id_), K(ret));
}

void ObSqcAsyncCB::on_timeout()
{
  ObThreadCondGuard guard(cond_);
  int ret = OB_SUCCESS;
  is_timeout_ = true;
  ret = cond_.broadcast();
  LOG_WARN("ObSqcAsyncCB timeout, check timeout value, peer cpu load, network "
           "packet drop rate",
      K(trace_id_),
      K(ret));
}

rpc::frame::ObReqTransport::AsyncCB* ObSqcAsyncCB::clone(const rpc::frame::SPAlloc& alloc) const
{
  UNUSED(alloc);
  return const_cast<rpc::frame::ObReqTransport::AsyncCB*>(
      static_cast<const rpc::frame::ObReqTransport::AsyncCB* const>(this));
}

/* ObPxSqcAsyncProxy */
int ObPxSqcAsyncProxy::launch_all_rpc_request()
{
  int ret = OB_SUCCESS;
  // prepare allocate the results_ array
  if (OB_FAIL(results_.prepare_allocate(sqcs_.count()))) {
    LOG_WARN("fail to prepare allocate result array");
  }

  ARRAY_FOREACH_X(sqcs_, idx, count, OB_SUCC(ret))
  {
    ret = launch_one_rpc_request(idx, NULL);
  }
  if (OB_FAIL(ret)) {
    LOG_WARN("fail to launch all sqc rpc request", K(ret));
    fail_process();
  }
  return ret;
}

int ObPxSqcAsyncProxy::launch_one_rpc_request(int64_t idx, ObSqcAsyncCB* cb)
{
  int ret = OB_SUCCESS;
  ObCurTraceId::TraceId* trace_id = NULL;
  ObPxSqcMeta& sqc = *sqcs_.at(idx);
  const ObAddr& addr = sqc.get_exec_addr();
  ObPxRpcInitSqcArgs args;
  int64_t timeout_us = phy_plan_ctx_->get_timeout_timestamp() - ObTimeUtility::current_time();
  if (phy_plan_->is_new_engine()) {
    if (OB_FAIL(sqc.split_values(exec_ctx_))) {
      LOG_WARN("fail to split values", K(ret));
    } else {
      args.set_serialize_param(exec_ctx_, const_cast<ObOpSpec&>(*dfo_.get_root_op_spec()), *phy_plan_);
    }
  } else {
    args.set_serialize_param(exec_ctx_, const_cast<ObPhyOperator&>(*dfo_.get_root_op()), *phy_plan_);
  }
  if (OB_FAIL(ret)) {
  } else if (timeout_us < 0) {
    ret = OB_TIMEOUT;
  } else if (OB_FAIL(args.sqc_.assign(sqc))) {
    LOG_WARN("fail assign sqc", K(ret));
  } else if (OB_ISNULL(trace_id = ObCurTraceId::get_trace_id())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("fail to get trace id");
  } else {
    // allocate SqcAsync callback
    if (cb == NULL) {
      void* mem = NULL;
      if (NULL == (mem = allocator_.alloc(sizeof(ObSqcAsyncCB)))) {
        ret = OB_ALLOCATE_MEMORY_FAILED;
        LOG_WARN("alloc memory failed", "size", sizeof(ObSqcAsyncCB), K(ret));
      } else {
        cb = new (mem) ObSqcAsyncCB(cond_, *trace_id);
        if (OB_FAIL(callbacks_.push_back(cb))) {
          // free the callback
          LOG_WARN("callback obarray push back failed.");
          cb->~ObSqcAsyncCB();
          allocator_.free(cb);
          cb = NULL;
        }
      }
    }
    if (cb != NULL) {
      if (OB_SUCC(ret)) {
        if (OB_FAIL(proxy_.to(addr)
                        .by(THIS_WORKER.get_rpc_tenant() ?: session_->get_effective_tenant_id())
                        .as(OB_SYS_TENANT_ID)
                        .timeout(timeout_us)
                        .async_init_sqc(args, cb))) {
          LOG_WARN("fail to call asynchronous sqc rpc", K(sqc), K(timeout_us), K(ret));
          // error_index_ = idx;
        } else {
          LOG_DEBUG("send the sqc request successfully.", K(idx), K(sqc), K(args), K(cb));
        }
      }
      if (OB_FAIL(ret) && cb != NULL) {
        int temp_ret = callbacks_.remove(idx);
        if (temp_ret != OB_SUCCESS) {
          // set callback invalid, which processed by fail_process()
          cb->set_invalid(true);
          LOG_WARN("callback obarray remove element failed", K(ret));
        } else {
          cb->~ObSqcAsyncCB();
          allocator_.free(cb);
          cb = NULL;
        }
      }
    }
  }
  return ret;
}

int ObPxSqcAsyncProxy::wait_all()
{
  int ret = OB_SUCCESS;
  LOG_TRACE("wail all async sqc rpc to end", K(dfo_));
  // break while:
  // 1. get enough callback result
  // 2. timeout , ret = OB_TIMEOUT
  // 3. retry rpc fail
  while (return_cb_count_ < sqcs_.count() && OB_SUCC(ret)) {

    ObThreadCondGuard guard(cond_);
    // wait for timeout or until notified.
    cond_.wait_us(500);

    if ((phy_plan_ctx_->get_timeout_timestamp() - ObTimeUtility::current_time()) < 0) {
      ret = OB_TIMEOUT;
    }

    ARRAY_FOREACH_X(callbacks_, idx, count, OB_SUCC(ret))
    {
      ObSqcAsyncCB& callback = *callbacks_.at(idx);
      if (!callback.is_visited() && callback.is_timeout()) {
        // callback timeout, no need retry
        return_cb_count_++;
        if (phy_plan_ctx_->get_timeout_timestamp() - ObTimeUtility::current_time() > 0) {
          error_index_ = idx;
          ret = OB_RPC_CONNECT_ERROR;
        } else {
          ret = OB_TIMEOUT;
        }
        callback.set_visited(true);
      } else if (!callback.is_visited() && callback.is_invalid()) {
        // rpc decode fail, on_invalid() will be called, need no retry
        return_cb_count_++;
        ret = OB_RPC_PACKET_INVALID;
        callback.set_visited(true);
      } else if (!callback.is_visited() && callback.is_processed()) {
        return_cb_count_++;
        callback.set_visited(true);
        if (OB_SUCC(callback.get_ret_code().rcode_)) {
          const ObPxRpcInitSqcResponse& cb_result = callback.get_result();
          if (cb_result.rc_ == OB_ERR_INSUFFICIENT_PX_WORKER) {
            // can not acquire enough px worker, no need SQC retry, stmt retry is needed
            LOG_INFO("can't get enough worker resource, and not retry", K(cb_result.rc_), K(*sqcs_.at(idx)));
          }
          if (OB_FAIL(cb_result.rc_)) {
            if (is_data_not_readable_err(ret)) {
              error_index_ = idx;
            }
          } else {
            results_.at(idx) = &cb_result;
          }
        } else {
          // RPC framework error, need no retry
          ret = callback.get_ret_code().rcode_;
          LOG_WARN("call rpc failed", K(ret), K(callback.get_ret_code()));
        }
      }
    }
  }

  if (return_cb_count_ < callbacks_.count()) {
    // hash unfinished callback, need wait all callback finish
    fail_process();
  }
  return ret;
}

void ObPxSqcAsyncProxy::destroy()
{
  int ret = OB_SUCCESS;
  LOG_DEBUG("async sqc proxy deconstruct, the callbacklist is ", K(callbacks_));
  ARRAY_FOREACH(callbacks_, idx)
  {
    ObSqcAsyncCB* callback = callbacks_.at(idx);
    LOG_DEBUG("async sqc proxy deconstruct, the callback status is ", K(idx), K(*callback));
    callback->~ObSqcAsyncCB();
  }
  allocator_.reuse();
  callbacks_.reuse();
  results_.reuse();
}

void ObPxSqcAsyncProxy::fail_process()
{
  LOG_WARN("async sqc fails, process the callbacks that have not yet got results",
      K(return_cb_count_),
      K(callbacks_.count()));
  while (return_cb_count_ < callbacks_.count()) {
    ObThreadCondGuard guard(cond_);
    ARRAY_FOREACH_X(callbacks_, idx, count, true)
    {
      ObSqcAsyncCB& callback = *callbacks_.at(idx);
      if (!callback.is_visited()) {
        if (callback.is_processed() || callback.is_timeout() || callback.is_invalid()) {
          return_cb_count_++;
          LOG_DEBUG("async sql fails, wait all callbacks", K(return_cb_count_), K(callbacks_.count()));
          callback.set_visited(true);
        }
      }
    }
    cond_.wait_us(500);
  }
  LOG_WARN("async sqc fails, all callbacks have been processed");
}

}  // namespace sql
}  // namespace oceanbase