Beispiel #1
0
     /**
     * post_packet is async version of send_packet. donot wait for response packet.
     */
    int ObClientManager::do_post_packet(const ObServer& server, ObPacket* packet,
        tbnet::IPacketHandler* handler, void* args) const
    {
      int rc = OB_SUCCESS;
      if (NULL == packet) 
      {
        rc = OB_INVALID_ARGUMENT;
      }
      else if (!inited_) 
      {
        rc = OB_NOT_INIT;
        TBSYS_LOG(ERROR, "cannot post packet, ClientManager not initialized.");
        packet->free();
      }
      else
      {
        bool send_ok = connmgr_->sendPacket(server.get_ipv4_server_id(), packet, handler, args);
        if (!send_ok)
        {
          rc = OB_PACKET_NOT_SENT;
          TBSYS_LOG(WARN, "cannot post packet, maybe send queue is full or disconnect.dest_server:%s",
              server.to_cstring());
          packet->free();
        }
      } 

      return rc;
    }
Beispiel #2
0
    /*
     * send a packet to server %server and wait response packet
     * @param server send to server
     * @param packet send packet object, must be allocate on heap, 
     * if send_packet failed, packet will be free by send_packet.
     * @param timeout max wait time interval
     * @param [out] response  response packet from remote server, allocated on heap, 
     * must be free by user who call the send_packet. response not NULL when return success.
     * @return OB_SUCCESS on success or other on failure.
     */
    int ObClientManager::do_send_packet(const ObServer & server, 
        ObPacket* packet, const int64_t timeout, ObPacket* &response) const
    {
      response = NULL;
      int rc = OB_SUCCESS;
      if (NULL == packet) 
      {
        rc = OB_INVALID_ARGUMENT;
      }
      else if (!inited_) 
      {
        rc = OB_NOT_INIT;
        TBSYS_LOG(ERROR, "cannot send packet, ClientManager not initialized.");
        packet->free();
        packet = NULL;
      }
      else if (OB_SUCCESS != error_)
      {
        rc = error_;
        packet->free();
        TBSYS_LOG(ERROR, "prev_error=%d", error_);
      }

      common::WaitObject* wait_object = NULL;
      if (OB_SUCCESS == rc)
      {
        wait_object =  waitmgr_->create_wait_object();
        if (NULL == wait_object)
        {
          TBSYS_LOG(ERROR, "cannot send packet, cannot create wait object");
          rc = OB_ERROR;
        }

      }
      if (OB_SUCCESS == rc) 
      {
        if (timeout > max_request_timeout_)
        {
          max_request_timeout_ = timeout;
          connmgr_->setDefaultQueueTimeout(0, static_cast<int32_t>(max_request_timeout_ / 1000));
        }
        // caution! wait_object set no free, it means response packet
        // not be free by wait_object, must be handled by user who call send_packet.
        // MODIFY: wait_object need free the response packet not handled.
        // wait_object->set_no_free();
        int packet_code = packet->get_packet_code();
        bool send_ok = connmgr_->sendPacket(server.get_ipv4_server_id(), packet, NULL, 
            reinterpret_cast<void*>(wait_object->get_id()));
        if (send_ok)
        {
          send_ok = wait_object->wait(timeout);
          if (!send_ok)
          {
            TBSYS_LOG(ERROR, "wait packet(%d) response timeout, timeout=%ld, dest_server=%s", 
                packet_code, timeout, server.to_cstring());
            rc = OB_RESPONSE_TIME_OUT;
          }
          else
          {
            response = dynamic_cast<ObPacket*>(wait_object->get_response());
            // there's two situation on here.
            // 1. connect remote server failed, ControlPacket(timeout) raise up.
            // 2. timeout parameter of this function greater than default timeout
            // of ConnectManager(5000ms), Packet timeout raise up.
            // TODO, maybe need a new error code.
            rc = (NULL !=  response) ? OB_SUCCESS : OB_RESPONSE_TIME_OUT;
          }

        }
        else
        {
          packet->free();
          rc = OB_PACKET_NOT_SENT;
          TBSYS_LOG(WARN, "cannot send packet, maybe send queue is full or disconnect.");
        }

        // do not free the response packet.
        waitmgr_->destroy_wait_object(wait_object);
        wait_object = NULL;

        if (OB_SUCCESS == rc && NULL != response)
        {
          rc = response->deserialize();
          if (OB_SUCCESS != rc)
          {
            TBSYS_LOG(ERROR, "response packet deserialize failed.");
            // cannot free response packet, allocate on thread specific memeory.
            response = NULL;
          }
        }

      } 

      return rc;
    }
Beispiel #3
0
int ObRootRpcStub::table_exist_in_cs(const ObServer &cs, const int64_t timeout_us,
    const uint64_t table_id, bool &is_exist_in_cs)
{
  int ret = OB_SUCCESS;
  ObDataBuffer msgbuf;
  if (NULL == client_mgr_)
  {
    TBSYS_LOG(ERROR, "client_mgr_=NULL");
    ret = OB_ERROR;
  }
  if (OB_SUCCESS == ret)
  {
    if (OB_SUCCESS != (ret = get_thread_buffer_(msgbuf)))
    {
      TBSYS_LOG(ERROR, "failed to get thread buffer, err=%d", ret);
    }
  }
  if (OB_SUCCESS == ret)
  {
    if (OB_SUCCESS != (ret = serialization::encode_vi64(msgbuf.get_data(), msgbuf.get_capacity(),
            msgbuf.get_position(), table_id)))
    {
      TBSYS_LOG(WARN, "fail to encode table_id, table_id=%ld, ret=%d", table_id, ret);
    }
  }
  if (OB_SUCCESS == ret)
  {
    if (OB_SUCCESS != (ret = client_mgr_->send_request(cs, OB_CS_CHECK_TABLET, DEFAULT_VERSION, timeout_us, msgbuf)))
    {
      TBSYS_LOG(WARN, "failed to send request, err=%d", ret);
    }
  }
  ObResultCode result;
  int64_t pos = 0;
  if (OB_SUCCESS == ret)
  {
    if (OB_SUCCESS != (ret = result.deserialize(msgbuf.get_data(), msgbuf.get_position(), pos)))
    {
      TBSYS_LOG(ERROR, "failed to deserialize response, err=%d", ret);
    }
  }
  if (OB_SUCCESS == ret)
  {
    if (OB_CS_TABLET_NOT_EXIST == result.result_code_)
    {
      ret = OB_SUCCESS;
      is_exist_in_cs = false;
    }
    else if (OB_SUCCESS == result.result_code_)
    {
      is_exist_in_cs = true;
    }
    else
    {
      ret = result.result_code_;
      TBSYS_LOG(WARN, "fail to check cs tablet. table_id=%lu, cs_addr=%s, err=%d",
          table_id, cs.to_cstring(), ret);
    }
  }
  return ret;
}
// maybe not find a merge server
int TaskFactory::get_table_tablet(const char * table_name, const uint64_t table_id, uint64_t & count)
{
  int ret = OB_SUCCESS;
  ObScanParam scan_param;
  uint64_t max_len = 0;
  if (NULL == table_name)
  {
    TBSYS_LOG(ERROR, "check table name failed:table[%s], id[%lu]", table_name, table_id);
    ret = OB_ERROR;
  }
  else
  {
    ret = init_scan_param(table_name, table_id, max_len, scan_param);
    if (ret != OB_SUCCESS)
    {
      TBSYS_LOG(ERROR, "init scan param failed:table[%s], ret[%d]", table_name, ret);
    }
  }

  if (OB_SUCCESS == ret)
  {
    // for the first table tablet
    ObRowkey row_key;
    ObObj temp_buffer[common::OB_MAX_ROWKEY_COLUMN_NUMBER];
    row_key.assign(temp_buffer, common::OB_MAX_ROWKEY_COLUMN_NUMBER);
    ObGetParam param;
    ObScanner scanner;
    ObServer server;
    ObRowkey start_key;
    ObRowkey end_key;
    ObCellInfo * cell = NULL;
    ObScannerIterator iter;
    bool row_change = false;
    ObString name;
    name.assign(const_cast<char*>(table_name), (int32_t)strlen(table_name));
    ObCellInfo temp_cell;
    temp_cell.table_id_ = table_id;
    temp_cell.column_id_ = 0;
    common::ModuleArena allocator;
    while ((OB_SUCCESS == ret) && (!row_key.is_max_row()))
    {
      param.reset();
      param.set_is_read_consistency(false);
      temp_cell.row_key_ = row_key;
      ret = param.add_cell(temp_cell);
      if (ret != OB_SUCCESS)
      {
        TBSYS_LOG(ERROR, "add cell failed:ret[%d]", ret);
        break;
      }
      ret = rpc_->get(root_server_, timeout_, param, scanner);
      if (ret != OB_SUCCESS)
      {
        TBSYS_LOG(ERROR, "get root table for tablet failed:table[%lu], ret[%d]", table_id, ret);
        break;
      }
      else
      {
        // skip the first row
        iter = scanner.begin();
        ++iter;
        while ((iter != scanner.end())
            && (OB_SUCCESS == (ret = iter.get_cell(&cell, &row_change))) && !row_change)
        {
          if (NULL == cell)
          {
            TBSYS_LOG(ERROR, "%s", "check cell failed");
            ret = OB_INNER_STAT_ERROR;
            break;
          }
          ret = cell->row_key_.deep_copy(start_key, allocator);
          if (ret != OB_SUCCESS)
          {
            TBSYS_LOG(WARN, "deep copy the rowkey failed:ret[%d]", ret);
          }
          ++iter;
        }
      }
      // scanner iter end
      if (ret == OB_SUCCESS)
      {
        int64_t ip = 0;
        int64_t port = 0;
        int64_t version = 0;
        TabletLocation list;
        for (++iter; iter != scanner.end(); ++iter)
        {
          ret = iter.get_cell(&cell, &row_change);
          if (ret != OB_SUCCESS)
          {
            TBSYS_LOG(ERROR, "get cell from scanner iterator failed:ret[%d]", ret);
            break;
          }
          else if (row_change) // && (iter != last_iter))
          {
            TaskInfo task;
            task.set_table_id(table_id);
            task.set_table_name(table_name);
            ret = init_new_task(name, start_key, end_key, scan_param, task);
            if (ret != OB_SUCCESS)
            {
              TBSYS_LOG(ERROR, "init new task failed:ret[%d]", ret);
              break;
            }
            else
            {
              ret = insert_new_task(list, task);
              if (ret != OB_SUCCESS)
              {
                TBSYS_LOG(ERROR, "insert new task failed:ret[%d]", ret);
                break;
              }
              ++count;
            }
            list.clear();
            start_key = end_key;
          }
          else
          {
            cell->row_key_.deep_copy(end_key, allocator);
            if ((cell->column_name_.compare("1_ms_port") == 0)
                || (cell->column_name_.compare("2_ms_port") == 0)
                || (cell->column_name_.compare("3_ms_port") == 0))
            {
              ret = cell->value_.get_int(port);
            }
            else if ((cell->column_name_.compare("1_ipv4") == 0)
                || (cell->column_name_.compare("2_ipv4") == 0)
                || (cell->column_name_.compare("3_ipv4") == 0))
            {
              ret = cell->value_.get_int(ip);
            }
            else if ((cell->column_name_.compare("1_tablet_version") == 0)
                || (cell->column_name_.compare("2_tablet_version") == 0)
                || (cell->column_name_.compare("3_tablet_version") == 0))
            {
              ret = cell->value_.get_int(version);
              if (OB_SUCCESS == ret)
              {
                if (0 == port || port == 0)
                {
                  TBSYS_LOG(WARN, "%s", "check port or ip failed");
                }
                else
                {
                  server.set_ipv4_addr(int32_t(ip), int32_t(port));
                  ObTabletLocation addr(version, server);
                  if (OB_SUCCESS != (ret = list.add(addr)))
                  {
                    TBSYS_LOG(ERROR, "add addr failed:server[%d], port[%d], ret[%d]",
                        server.get_ipv4(), server.get_port(), ret);
                  }
                  else
                  {
                    TBSYS_LOG(DEBUG, "add addr succ:server[%s], version:%ld", server.to_cstring(), version);
                  }
                }
                ip = port = version = 0;
              }

            }

            if (ret != OB_SUCCESS)
            {
              TBSYS_LOG(ERROR, "check get value failed:ret[%d]", ret);
              break;
            }
          }
        }

        // for the last row
        if ((OB_SUCCESS == ret) && (start_key != end_key))
        {
          TaskInfo task;
          task.set_table_id(table_id);
          task.set_table_name(table_name);
          ret = init_new_task(name, start_key, end_key, scan_param, task);
          if (ret != OB_SUCCESS)
          {
            TBSYS_LOG(ERROR, "init new task failed:ret[%d]", ret);
          }
          else
          {
            ret = insert_new_task(list, task);
            if (ret != OB_SUCCESS)
            {
              TBSYS_LOG(ERROR, "insert new task failed:ret[%d]", ret);
            }
            ++count;
          }

          if (OB_SUCCESS == ret)
          {
            // copy all the end key objs to row_key
            for (int64_t i = 0; i < end_key.length(); ++i)
            {
              const_cast<ObObj *> (row_key.ptr())[i] = end_key.ptr()[i];
              row_key.assign(const_cast<ObObj *> (row_key.ptr()), end_key.length());
            }
            // modify last row key for next get root table
            if (!end_key.is_max_row())
            {
              const_cast<ObObj *>(row_key.ptr())[end_key.length()].set_max_value();
              row_key.assign(const_cast<ObObj *> (row_key.ptr()), end_key.length() + 1);
            }
          }
        }
        list.clear();
      }
    }
  }
  return ret;
}
Beispiel #5
0
int ObMergerRpcStub::scan(
    const int64_t timeout, const ObServer & server,
    const ObScanParam & scan_param, ObScanner & scanner) const
{
    int ret = OB_SUCCESS;
    ObDataBuffer data_buff;
    int64_t start_time = tbsys::CTimeUtil::getTime();
    ret = get_rpc_buffer(data_buff);
    // step 1. serialize ObScanParam to the data_buff
    if (OB_SUCCESS == ret)
    {
        ret = scan_param.serialize(data_buff.get_data(), data_buff.get_capacity(),
                                   data_buff.get_position());
        if (OB_SUCCESS != ret)
        {
            TBSYS_LOG(WARN, "serialize scan param failed:ret[%d]", ret);
        }
    }
    // step 2. send request for scan data
    if (OB_SUCCESS == ret)
    {
        ret = rpc_frame_->send_request(server, OB_SCAN_REQUEST, DEFAULT_VERSION,
                                       timeout, data_buff);
        if (ret != OB_SUCCESS)
        {
            TBSYS_LOG(WARN, "send scan request to server failed:ret[%d], timeout=%ld",
                      ret, timeout);
        }
    }
    // step 3. deserialize the response result
    int64_t pos = 0;
    if (OB_SUCCESS == ret)
    {
        ObResultCode result_code;
        ret = result_code.deserialize(data_buff.get_data(), data_buff.get_position(), pos);
        if (OB_SUCCESS != ret)
        {
            TBSYS_LOG(WARN, "deserialize result failed:pos[%ld], ret[%d]", pos, ret);
        }
        else
        {
            ret = result_code.result_code_;
        }
    }

    // step 4. deserialize the scanner
    if (OB_SUCCESS == ret)
    {
        ret = scanner.deserialize(data_buff.get_data(), data_buff.get_position(), pos);
        if (OB_SUCCESS != ret)
        {
            TBSYS_LOG(WARN, "deserialize scanner from buff failed:pos[%ld], ret[%d]", pos, ret);
        }
    }

    // write debug log
    const int32_t MAX_SERVER_ADDR_SIZE = 128;
    char server_addr[MAX_SERVER_ADDR_SIZE];
    server.to_string(server_addr, MAX_SERVER_ADDR_SIZE);
    bool is_fullfilled = false;
    int64_t fullfilled_item_num = 0;
    scanner.get_is_req_fullfilled(is_fullfilled,fullfilled_item_num);
    if (OB_SUCCESS == ret)
    {
        TBSYS_LOG(DEBUG, "scan data succ from server:addr[%s]", server_addr);
    }
    else
    {
        TBSYS_LOG(WARN, "scan data failed from server:addr[%s], "
                  "version_range=%s, ret[%d]",
                  server_addr, range2str(scan_param.get_version_range()), ret);
    }

    int64_t consume_time = tbsys::CTimeUtil::getTime() - start_time;
    if ((double)consume_time > (double)timeout * 0.8)
    {
        TBSYS_LOG(WARN, "slow ups scan, ups_addr=%s, timeout=%ld, consume=%ld",
                  server.to_cstring(), timeout, consume_time);
    }
    return ret;
}
Beispiel #6
0
int ObMergerRpcStub::fetch_schema(
    const int64_t timeout, const ObServer & root_server,
    const int64_t version, ObSchemaManagerV2 & schema) const
{
    int ret = OB_SUCCESS;
    ObDataBuffer data_buff;
    ret = get_rpc_buffer(data_buff);
    // step 1. serialize timestamp to data_buff
    if (OB_SUCCESS == ret)
    {
        ret = serialization::encode_vi64(data_buff.get_data(),
                                         data_buff.get_capacity(), data_buff.get_position(), version);
        if (OB_SUCCESS != ret)
        {
            TBSYS_LOG(WARN, "serialize timestamp failed:version[%ld], ret[%d]",
                      version, ret);
        }
    }
    // step 2. send request for fetch new schema
    if (OB_SUCCESS == ret)
    {
        ret = rpc_frame_->send_request(root_server, OB_FETCH_SCHEMA, DEFAULT_VERSION,
                                       timeout, data_buff);
        if (ret != OB_SUCCESS)
        {
            TBSYS_LOG(WARN, "send request to root server[%s] for fetch schema failed:"
                      "version[%ld], ret[%d]", root_server.to_cstring(), version, ret);
        }
    }
    // step 3. deserialize the response code
    int64_t pos = 0;
    if (OB_SUCCESS == ret)
    {
        ObResultCode result_code;
        ret = result_code.deserialize(data_buff.get_data(), data_buff.get_position(), pos);
        if (OB_SUCCESS != ret)
        {
            TBSYS_LOG(WARN, "deserialize result_code failed:pos[%ld], ret[%d]", pos, ret);
        }
        else
        {
            ret = result_code.result_code_;
        }
    }
    // step 4. deserialize the table schema
    if (OB_SUCCESS == ret)
    {
        ret = schema.deserialize(data_buff.get_data(), data_buff.get_position(), pos);
        if (OB_SUCCESS != ret)
        {
            TBSYS_LOG(WARN, "deserialize schema from buff failed:"
                      "version[%ld], pos[%ld], ret[%d]", version, pos, ret);
        }
        else
        {
            TBSYS_LOG(DEBUG, "fetch schema succ:version[%ld]", schema.get_version());
            //schema.print_info();
        }
    }
    return ret;
}