int ObSchemaServiceMsProvider::get_ms(const ObScanParam &scan_param, const int64_t retry_num, ObServer &ms)
{
  int ret = OB_SUCCESS;
  UNUSED(scan_param);
  if (0 < retry_num
      && 0 != ms.get_port()
      && 0 != ms.get_ipv4())
  {
    update_ms_retry(ms);
  }
  if (did_need_reset())
  {
    if (OB_SUCCESS != (ret = reset()))
    {
      TBSYS_LOG(WARN, "failed to init schema service ms provider, err=%d", ret);
    }
  }
  if (OB_SUCCESS == ret)
  {
    if (retry_num >= count_)
    {
      TBSYS_LOG(DEBUG, "no more ms for scan, retry=%ld count=%ld", retry_num, count_);
      ret = OB_MS_ITER_END;
    }
    else
    {
      tbsys::CRLockGuard guard(rwlock_);
      ms = ms_carray_[retry_num].ms_;
    }
  }
  return ret;
}
int ObScanHelperImpl::mutate(ObMutator& mutator)
{
  int ret = OB_SUCCESS;
  if (!check_inner_stat())
  {
    ret = OB_NOT_INIT;
    TBSYS_LOG(ERROR, "scan help not init");
  }
  else
  {
    ObServer ups;
    if (OB_SUCCESS != (ret = ups_provider_->get_ups(ups)))
    {
      TBSYS_LOG(WARN, "failed to get ups, err=%d", ret);
    }
    else if (0 == ups.get_port() || 0 == ups.get_ipv4())
    {
      ret = OB_INVALID_ARGUMENT;
      TBSYS_LOG(WARN, "invalid update server address");
    }
    else if (OB_SUCCESS != (ret = rpc_stub_->mutate(ups, mutator, mutate_timeout_us_)))
    {
      TBSYS_LOG(WARN, "failed to mutate, mutate_timeout_us_=%ld, ups=%s, err=%d",
          mutate_timeout_us_, to_cstring(ups), ret);
    }
    else
    {
      TBSYS_LOG(DEBUG, "ups mutate succ, ups=%s", to_cstring(ups));
    }
  }
  return ret;
}
Exemple #3
0
MockNodeServer::MockNodeServer(int32_t port, int64_t retry_time, int64_t timeout, ObServer monitor, char* app_name, char* instance_name, char* hostname)
{
  port_ = port;
  retry_times_ = retry_time;
  timeout_ = timeout;
  monitor_.set_ipv4_addr(monitor.get_ipv4(), monitor.get_port());

  app_name_ = app_name;
  instance_name_ = instance_name;
  hostname_ = hostname;
  status_ = UNKNOWN;
}
Exemple #4
0
int ObMergerRpcStub::fetch_update_server(
    const int64_t timeout, const ObServer & root_server,
    ObServer & update_server, bool for_merge) const
{
    int ret = OB_SUCCESS;
    ObDataBuffer data_buff;
    ret = get_rpc_buffer(data_buff);
    // step 1. send get update server info request
    if (OB_SUCCESS == ret)
    {
        ret = rpc_frame_->send_request(root_server,
                                       for_merge ? OB_GET_UPDATE_SERVER_INFO_FOR_MERGE : OB_GET_UPDATE_SERVER_INFO,
                                       DEFAULT_VERSION, timeout, data_buff);
        if (ret != OB_SUCCESS)
        {
            TBSYS_LOG(WARN, "send request to root server for find update server failed:ret[%d]", ret);
        }
    }

    // step 2. deserialize restult 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 3. deserialize update server addr
    if (OB_SUCCESS == ret)
    {
        ret = update_server.deserialize(data_buff.get_data(), data_buff.get_position(), pos);
        if (ret != OB_SUCCESS)
        {
            TBSYS_LOG(WARN, "deserialize server failed:pos[%ld], ret[%d]", pos, ret);
        }
        else
        {
            TBSYS_LOG(DEBUG, "find update server succ:addr[%d], port[%d]", update_server.get_ipv4(),
                      update_server.get_port());
        }
    }
    return ret;
}
int ObInnerTableOperator::update_all_server(ObString & sql, const int64_t cluster_id,
    const char * server_type, const ObServer & server, const uint32_t inner_port, const char * version)
{
  int ret = OB_SUCCESS;
  if ((sql.ptr() == NULL) || (0 == sql.size()))
  {
    TBSYS_LOG(WARN, "Check SQL buffer size failed! ptr[%p], size[%d]", sql.ptr(), sql.size());
    ret = OB_INVALID_ARGUMENT;
  }
  else
  {
    char buf[OB_MAX_SERVER_ADDR_SIZE] = "";
    if (server.ip_to_string(buf, sizeof(buf)) != true)
    {
      ret = OB_CONVERT_ERROR;
    }
    else
    {
      const char * format = "REPLACE INTO %s"
        "(cluster_id, svr_type, svr_ip, svr_port, inner_port, svr_role, svr_version) "
        "values (%d, '%s', '%s', %u, %u, %d, '%s');";
      int size = snprintf(sql.ptr(), sql.size(), format, OB_ALL_SERVER, cluster_id,
          server_type, buf, server.get_port(), inner_port, 0, version);
      if (size >= sql.size())
      {
        TBSYS_LOG(ERROR, "SQL buffer size not enough! size: [%d], need: [%d], sql: [%.*s]",
            sql.size(), size, sql.length(), sql.ptr());
        ret = OB_SIZE_OVERFLOW;
      }
      else
      {
        sql.assign_ptr(sql.ptr(), size);
      }
    }
  }
  return ret;
}
int ObInnerTableOperator::delete_all_server(ObString & sql, const int64_t cluster_id,
    const char * server_type, const ObServer & server)
{
  int ret = OB_SUCCESS;
  if ((sql.ptr() == NULL) || (0 == sql.size()))
  {
    TBSYS_LOG(WARN, "Check SQL buffer size failed! ptr[%p], size[%d]", sql.ptr(), sql.size());
    ret = OB_INVALID_ARGUMENT;
  }
  else
  {
    char buf[OB_MAX_SERVER_ADDR_SIZE] = "";
    if (server.ip_to_string(buf, sizeof (buf)) != true)
    {
      ret = OB_CONVERT_ERROR;
    }
    else
    {
      const char * format = "DELETE FROM %s WHERE cluster_id = %d AND svr_type = '%s' "
        "AND svr_ip = '%s' AND svr_port = %u;";
      int size = snprintf(sql.ptr(), sql.size(), format, OB_ALL_SERVER,
          cluster_id, server_type, buf, server.get_port());
      if (size >= sql.size())
      {
        TBSYS_LOG(ERROR, "SQL buffer size not enough! size: [%d], need: [%d], sql: [%.*s]",
            sql.size(), size, sql.length(), sql.ptr());
        ret = OB_SIZE_OVERFLOW;
      }
      else
      {
        sql.assign_ptr(sql.ptr(), size);
      }
    }
  }
  return ret;
}
int ObScanHelperImpl::scan(const ObScanParam& scan_param, ObScanner &out) const
{
  int ret = OB_SUCCESS;
  if (!check_inner_stat())
  {
    ret = OB_NOT_INIT;
    TBSYS_LOG(ERROR, "scan help not init");
  }
  else
  {
    ObServer ms;
    for (int64_t i = 0; i < scan_retry_times_; ++i)
    {
      if (OB_SUCCESS != (ret = ms_provider_->get_ms(scan_param, i, ms)))
      {
        TBSYS_LOG(WARN, "failed to get one mergeserver, err=%d", ret);
      }
      else if (0 == ms.get_port() || 0 == ms.get_ipv4())
      {
        TBSYS_LOG(WARN, "invalid merge server address, i=%ld", i);
        ret = OB_INVALID_ARGUMENT;
      }
      else if (OB_SUCCESS == (ret = rpc_stub_->scan(ms, scan_param, out, scan_timeout_us_)))
      {
        TBSYS_LOG(DEBUG, "scan from ms=%s", to_cstring(ms));
        break;
      }
      else
      {
        TBSYS_LOG(WARN, "scan ms timeout, scan_timeout_us_=%ld, ms=%s, retry=%ld",
            scan_timeout_us_, to_cstring(ms), i);
      }
    } // end for
  }
  return ret;
}
Exemple #8
0
int ObMonitor::add_register_node(const ObServer &node, const char *app_name, const char *instance_name, const MMS_Status status)
{
  int err = OB_SUCCESS;
  if(app_name == NULL || instance_name == NULL)
  {
    TBSYS_LOG(ERROR, "should not be here.");
    err = OB_INVALID_ARGUMENT;
  }
  //node_list_type * pnodelist = NULL;
  mms_value *pvalue = NULL;
  mms_key key;
  key.app_name = const_cast<char*>(app_name);
  key.instance_name = const_cast<char *>(instance_name);
  if(OB_SUCCESS == err)
  {
    if(HASH_EXIST == node_map_.get(key, pvalue))
    {
      node_list_type *pnodelist = &((*pvalue).node_list);
      node_list_type::iterator it_server = pnodelist->end();
      bool node_exist = if_node_exist(pnodelist, node, it_server);
      if(node_exist == true)
      {
        TBSYS_LOG(DEBUG, "re_register node. reset the node status");
        (*it_server)->is_valid = true;
        (*it_server)->lease.lease_time = tbsys::CTimeUtil::getTime();
        (*it_server)->lease.lease_interval = lease_interval_;
        (*it_server)->lease.renew_interval = renew_interval_;
      }
      else
      {
        TBSYS_LOG(DEBUG, "already have register node in the list, add node_info");
        node_info *node_server = new(std::nothrow) node_info;
        if(node_server == NULL)
        {
          TBSYS_LOG(WARN, "fail to new node_server.");
          err = OB_ALLOCATE_MEMORY_FAILED;
        }
        else
        {
          node_server->node_server.set_ipv4_addr(node.get_ipv4(), node.get_port());
          node_server->status = status;
          node_server->is_valid = true;
          node_server->lease.lease_time = tbsys::CTimeUtil::getTime();                                           
          node_server->lease.lease_interval = lease_interval_;                                             
          node_server->lease.renew_interval = renew_interval_;
          pnodelist->push_back(node_server);
        }
      }
    }
    else
    {
      TBSYS_LOG(DEBUG, "a new app+instance node register. add a map pair");
      key.app_name = new(std::nothrow)char [OB_MAX_APP_NAME_LENGTH];
      key.instance_name = new(std::nothrow)char [OB_MAX_INSTANCE_NAME_LENGTH];
      pvalue = new(std::nothrow) mms_value;
      node_info *node_server = new(std::nothrow)node_info;
      if(key.app_name == NULL || key.instance_name == NULL || node_server == NULL || pvalue == NULL)
      {
        TBSYS_LOG(WARN, "fail to allocate memory");
        err = OB_ALLOCATE_MEMORY_FAILED;
      }
      else
      {
        node_list_type *pnodelist = &(pvalue->node_list);
        memcpy(key.app_name, app_name, strlen(app_name) + 1);
        memcpy(key.instance_name, instance_name, strlen(instance_name) + 1);
        node_server->node_server.set_ipv4_addr(node.get_ipv4(), node.get_port());
        node_server->status = status;
        node_server->is_valid = true;
        node_server->lease.lease_time = tbsys::CTimeUtil::getTime();
        node_server->lease.lease_interval = lease_interval_;
        node_server->lease.renew_interval = renew_interval_;
        pnodelist->push_back(node_server);
        int hash_err = node_map_.set(key, pvalue);
        if(hash_err != HASH_INSERT_SUCC)
        {
          TBSYS_LOG(WARN, "fail to insert to node_map");
          err = OB_ERROR;
        }
        else
        {
          TBSYS_LOG(DEBUG, "add map pair succ~!");
        }
      }
    }
  }
  if(err == OB_SUCCESS && status == MASTER_ACTIVE)
  {
    pvalue->has_master = true;
    pvalue->master.set_ipv4_addr(node.get_ipv4(), node.get_port());
  }
  return err;
}
Exemple #9
0
    int ObServerRpc::fetch_schema(const ObServer& root_server,
                                  const int64_t timestap, 
                                  ObSchemaManagerV2& schema,
                                  const int64_t timeout)
    {
      int ret     = OB_SUCCESS;
      int64_t pos = 0;
      ObResultCode result_code;
      ObDataBuffer data_buff;

      if (root_server.get_ipv4() == 0 || root_server.get_port() == 0
          || timestap < 0 || timeout <= 0)
      {
        TBSYS_LOG(WARN, "invalid param, ip=%d, port=%d, timestap=%ld, "
                        "timeout=%ld",
                  root_server.get_ipv4(), root_server.get_port(),
                  timestap, timeout);
        ret = OB_ERROR;
      }
      else if (NULL == rpc_frame_)
      {
        TBSYS_LOG(WARN, "server rpc doesn't init.");
        ret = OB_ERROR;
      }

      if (OB_SUCCESS == ret)
      {
        ret = get_frame_buffer(data_buff);
      }
    
      // step 1. serialize timestap to data_buff
      if (OB_SUCCESS == ret)
      {
        ret = encode_vi64(data_buff.get_data(), 
                          data_buff.get_capacity(), 
                          data_buff.get_position(), timestap);
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(WARN, "serialize timestap failed, timestap=%ld, ret=%d.", 
                    timestap, 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 for fetch schema failed, "
                          "timestap=%ld, ret=%d.", timestap, ret);
        }
      }
    
      // step 3. deserialize the response code
      if (OB_SUCCESS == ret)
      {
        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_;
          if (OB_SUCCESS != ret)
          {
            TBSYS_LOG(WARN, "get response from root server failed, "
                            "timeout=%ld, ret=%d.", timeout, ret);
          }
        }
      }

      // 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 buffer failed, "
                          "timestap=%ld, pos=%ld, ret=%d", 
                    timestap, pos, ret);
        }
      }
    
      return ret;
    }
Exemple #10
0
    int ObServerRpc::ups_apply(const ObServer& update_server,
                               const ObMutator &mutator, 
                               const int64_t timeout)
    {
      int ret     = OB_SUCCESS;
      int64_t pos = 0;
      ObResultCode result_code;
      ObDataBuffer data_buff;

      if (update_server.get_ipv4() == 0 || update_server.get_port() == 0
          || timeout <= 0)
      {
        TBSYS_LOG(WARN, "invalid param, ip=%d, port=%d, timeout=%ld",
                  update_server.get_ipv4(), update_server.get_port(),
                  timeout);
        ret = OB_ERROR;
      }
      else if (NULL == rpc_frame_)
      {
        TBSYS_LOG(WARN, "server rpc doesn't init.");
        ret = OB_ERROR;
      }

      if (NULL == rpc_frame_)
      {
        TBSYS_LOG(WARN, "server rpc doesn't init.");
        ret = OB_ERROR;
      }

      if (OB_SUCCESS == ret)
      {
        ret = get_frame_buffer(data_buff);
      }

      // step 1. serialize mutator to data_buff
      if (OB_SUCCESS == ret)
      {
        ret = mutator.serialize(data_buff.get_data(), 
                                data_buff.get_capacity(), 
                                data_buff.get_position());
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(WARN, "serialize get_param failed, ret=%d.", ret);
        }
      }

      // step 2. send request for apply
      if (OB_SUCCESS == ret)
      {
        ret = rpc_frame_->send_request(update_server, 
                                       OB_WRITE, DEFAULT_VERSION, 
                                       timeout, data_buff);
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(WARN, "send request to update server for apply failed, "
                          "timeout=%ld, ret=%d.", timeout, ret);
        }
      }

      // step 3. deserialize the response code
      if (OB_SUCCESS == ret)
      {
        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_;
        }
      }

      return ret;
    }
Exemple #11
0
    int ObServerRpc::get(const ObServer& remote_server,
                         const ObGetParam& get_param, 
                         ObScanner& scanner,
                         const int64_t timeout)
    {
      int ret     = OB_SUCCESS;
      int64_t pos = 0;
      ObResultCode result_code;
      ObDataBuffer data_buff;

      if (remote_server.get_ipv4() == 0 || remote_server.get_port() == 0
          || timeout <= 0)
      {
        TBSYS_LOG(WARN, "invalid param, ip=%d, port=%d, timeout=%ld",
                  remote_server.get_ipv4(), remote_server.get_port(),
                  timeout);
        ret = OB_ERROR;
      }
      else if (get_param.get_cell_size() <= 0 || get_param.get_row_size() <= 0)
      {
        TBSYS_LOG(WARN, "invalid param, cell_size=%ld, row_size=%ld", 
                  get_param.get_cell_size(), get_param.get_row_size());
        ret = OB_ERROR;
      }
      else if (NULL == rpc_frame_)
      {
        TBSYS_LOG(WARN, "server rpc doesn't init.");
        ret = OB_ERROR;
      }

      if (OB_SUCCESS == ret)
      {
        ret = get_frame_buffer(data_buff);
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(WARN, "get frame buffer failed, ret=%d.", ret);
        }
      }
      
      // step 1. serialize get param to data_buff
      if (OB_SUCCESS == ret)
      {
        ret = get_param.serialize(data_buff.get_data(), 
                                  data_buff.get_capacity(), 
                                  data_buff.get_position());
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(WARN, "serialize get_param failed, ret=%d.", ret);
        }
      }
    
      // step 2. send request for get
      if (OB_SUCCESS == ret)
      {
        ret = rpc_frame_->send_request(remote_server, 
                                       OB_GET_REQUEST, DEFAULT_VERSION, 
                                       timeout, data_buff);
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(WARN, "send request to remote server for get failed, "
                          "ret=%d.", ret);
        }
      }
    
      // step 3. deserialize the response code
      if (OB_SUCCESS == ret)
      {
        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_;
          if (OB_SUCCESS != ret && OB_DATA_NOT_SERVE != ret)
          {
            TBSYS_LOG(WARN, "get response from remote server failed, "
                            "timeout=%ld, ret=%d.", timeout, ret);
          }
        }
      }

      // 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);
        }
      }
    
      return ret;
    }
Exemple #12
0
    int ObServerRpc::fetch_update_server(const ObServer& root_server,
                                         ObServer& update_server,
                                         const int64_t timeout)
    {
      int ret     = OB_SUCCESS;
      int64_t pos = 0;
      ObResultCode result_code;
      ObDataBuffer data_buff;

      if (root_server.get_ipv4() == 0 || root_server.get_port() == 0
          || timeout <= 0)
      {
        TBSYS_LOG(WARN, "invalid param, ip=%d, port=%d, timeout=%ld",
                  root_server.get_ipv4(), root_server.get_port(),
                  timeout);
        ret = OB_ERROR;
      }
      else if (NULL == rpc_frame_)
      {
        TBSYS_LOG(WARN, "server rpc doesn't init.");
        ret = OB_ERROR;
      }

      if (OB_SUCCESS == ret)
      {
        ret = get_frame_buffer(data_buff);
      }

      // step 1. send get update server info request
      if (OB_SUCCESS == ret)
      {
        ret = rpc_frame_->send_request(root_server, 
                                       OB_GET_UPDATE_SERVER_INFO, 
                                       DEFAULT_VERSION,
                                       timeout, data_buff);
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(WARN, "send request to root server for "
                           "get update server failed, ret=%d", ret);
        }
      }
    
      // step 2. deserialize restult code
      if (OB_SUCCESS == ret)
      {
        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_;
          if (OB_SUCCESS != ret && OB_DATA_NOT_SERVE != ret)
          {
            TBSYS_LOG(WARN, "get response from root server failed, "
                            "timeout=%ld, ret=%d.", timeout, ret);
          }
        }
      }
    
      // step 3. deserialize update server addr
      if (OB_SUCCESS == ret)
      {
        ret = update_server.deserialize(data_buff.get_data(), 
                                        data_buff.get_position(), pos);
        if (OB_SUCCESS != ret)
        {
          TBSYS_LOG(ERROR, "deserialize update server failed, pos=%ld, ret=%d", 
                    pos, 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;
}
int ObInnerTableOperator::update_all_cluster(ObString & sql, const int64_t cluster_id,
    const ObServer & server, const ObiRole role, const int64_t flow_percent)
{
  int ret = OB_SUCCESS;
  if ((sql.ptr() == NULL) || (0 == sql.size()))
  {
    ret = OB_INVALID_ARGUMENT;
    TBSYS_LOG(WARN, "Check SQL buffer size failed! ptr[%p], size[%d]", sql.ptr(), sql.size());
  }
  else
  {
    char buf[OB_MAX_SERVER_ADDR_SIZE];
    memset(buf, 0 , sizeof(buf));
    if (server.ip_to_string(buf, sizeof(buf)) != true)
    {
      ret = OB_CONVERT_ERROR;
      TBSYS_LOG(ERROR, "server ip is invalid, ret=%d", ret);
    }
    else
    {
      const char * format = "REPLACE INTO %s"
        "(cluster_id, cluster_role, cluster_vip, cluster_flow_percent, read_strategy, rootserver_port) "
        "VALUES(%d,%d,'%s',%d,%d,%d);";
      // random read read_strategy
      int size = snprintf(sql.ptr(), sql.size(), format, OB_ALL_CLUSTER, cluster_id,
                          role.get_role() == ObiRole::MASTER ? 1 : 2, buf, flow_percent, 0, server.get_port());
      if (size >= sql.size())
      {
        TBSYS_LOG(ERROR, "SQL buffer size not enough! size: [%d], need: [%d], sql: [%.*s]",
                  sql.size(), size, sql.length(), sql.ptr());
        ret = OB_SIZE_OVERFLOW;
      }
      else
      {
        sql.assign_ptr(sql.ptr(), size);
        TBSYS_LOG(INFO, "XXX=%.*s", sql.length(), sql.ptr());
      }
    }
  }
  return ret;
}
Exemple #15
0
// 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)
  {
    TaskInfo task;

    task.set_table_id(table_id);
    task.set_table_name(table_name);
    // for the first table tablet
    ObString row_key;
    char temp_buffer[1];
    memset(temp_buffer, 0, sizeof(temp_buffer));
    row_key.assign(temp_buffer, sizeof(temp_buffer));
    ObGetParam param;
    ObScanner scanner;
    ObServer server;
    ObString start_key;
    ObString end_key; 
    ObCellInfo * cell = NULL;
    ObScannerIterator iter; 
    bool row_change = false;
    ObString name;
    name.assign(const_cast<char*>(table_name), strlen(table_name));
    ObCellInfo temp_cell;
    temp_cell.table_id_ = table_id;
    temp_cell.column_id_ = 0;
    const uint64_t MAX_LEN = 1024;
    char last_tablet_rowkey[MAX_LEN] = "";
    const int32_t MAX_SERVER_ADDR_SIZE = 128;
    char server_addr[MAX_SERVER_ADDR_SIZE];
    while ((OB_SUCCESS == ret) 
        && (!is_max_rowkey(max_len, row_key.ptr(), row_key.length() - 1)))
    {
      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;
          }
          start_key.assign(cell->row_key_.ptr(), cell->row_key_.length());
          ++iter;
        }
      }
      
      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))
          {
            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;
            end_key.assign(cell->row_key_.ptr(), cell->row_key_.length());
          }
          else
          {
            end_key.assign(cell->row_key_.ptr(), cell->row_key_.length());
            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(ip, 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
                  {
                    server.to_string(server_addr, MAX_SERVER_ADDR_SIZE);
                    TBSYS_LOG(DEBUG, "add addr succ:server[%s], version:%ld", server_addr, 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))
        {
          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)
          {
            // modify last row key for next get root table
            uint64_t len = end_key.length();
            if (MAX_LEN > len + 1)
            {
              memcpy(last_tablet_rowkey, end_key.ptr(), len);
              last_tablet_rowkey[len] = 0;
              row_key.assign(last_tablet_rowkey, len + 1);
            }
            else
            {
              TBSYS_LOG(ERROR, "check end key failed:len[%lu]", len);
            }
          }
        }
        list.clear();
      }
    }
  }
  return ret;
}