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 #2
0
int ObMonitor::report_slave_failure(const int64_t timeout, const int64_t retry_times, const ObServer slave, const ObServer master)
{
  int err = OB_ERROR;
  for(int64_t i = 0; (i < retry_times) && (err != OB_SUCCESS); i++)
  {
    int64_t timeu = tbsys::CTimeUtil::getMonotonicTime();
    err = report_slave_failure_(timeout, slave, master);
    timeu = tbsys::CTimeUtil::getMonotonicTime() - timeu;
    if(OB_SUCCESS != err)
    {
      TBSYS_LOG(WARN, "%ldth, report slave failure fail. err =%d", i, err);
    }
    else
    {
      TBSYS_LOG(INFO, "report slave failure to master succ!");
    }      
  }

  if(OB_SUCCESS != err)
  {
    TBSYS_LOG(ERROR, "can't report failure to master node. master addr =%d", master.get_ipv4());
    err = OB_ERROR;
  }
  return err;
}
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;
}
Exemple #4
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 #5
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;
}
Exemple #6
0
int ObMonitor::change_master(const int64_t timeout, const int64_t retry_times, const ObServer &new_master)
{
  int err = OB_ERROR;
  for(int64_t i = 0; (i < retry_times) && (err != OB_SUCCESS); i++)                                                
  { 
    err = change_master_(timeout, new_master);
    if(OB_SUCCESS != err)                                                                                      
    { 
      TBSYS_LOG(WARN, "%ldth, report change master fail. err =%d", i, err);                                     
    }
    else                                                                                                       
    { 
      TBSYS_LOG(INFO, "report change master to master succ!");                                                 
    }                                                                                                          
  }                                                                                                            

  if(OB_SUCCESS != err)                                                                                        
  { 
    TBSYS_LOG(ERROR, "can't report to master node. master addr =%d, err=%d", new_master.get_ipv4(), err);               
  }
  return err;            
}
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
// 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;
}
Exemple #9
0
int ObMonitor::heartbeat(const int64_t timeout, const ObServer &master, const ObServer &node, MMS_Status &status)
{
  int err = OB_SUCCESS;
  ObDataBuffer data_buffer;
  err = get_rpc_buffer(data_buffer);

  if(OB_SUCCESS != err)
  {
    TBSYS_LOG(WARN, "fail to get rpc buffer. err=%d", err);
  }

  //step1:serialize lease
  ObLease lease;
  lease.lease_time = tbsys::CTimeUtil::getTime();
  lease.lease_interval = lease_interval_;
  lease.renew_interval = renew_interval_;
  if(OB_SUCCESS == err)
  {
    err = lease.serialize(data_buffer.get_data(), data_buffer.get_capacity(), data_buffer.get_position());
    if(OB_SUCCESS != err)
    {
      TBSYS_LOG(WARN, "fail to serialize lease.err=%d", err);
    }
  }

  FILL_TRACE_LOG("1.serailize lease, err= %d", err);

  //step2:serialize master server 
  if(OB_SUCCESS == err)
  {
    err = master.serialize(data_buffer.get_data(), data_buffer.get_capacity(), data_buffer.get_position());
    if(err != OB_SUCCESS)
    {
      TBSYS_LOG(WARN, "fail to serialize master");
    }
  }
  FILL_TRACE_LOG("2.serialize master server. err=%d", err);
  //step3:send request to node
  if(OB_SUCCESS == err)
  {
    err = client_manager_->send_request(node, OB_MMS_HEART_BEAT, MY_VERSION, timeout, data_buffer);
    if(err != OB_SUCCESS)
    {
      TBSYS_LOG(WARN, "fail to send request. err=%d", err);
    }
  }
  FILL_TRACE_LOG("3. send request to node. node ip=%d, err=%d", node.get_ipv4(), err);
  //step4:deserizlie the result_code
  ObResultCode result_code;
  int64_t pos = 0;
  if(OB_SUCCESS == err)
  {
    err = result_code.deserialize(data_buffer.get_data(), data_buffer.get_position(), pos);
    if(OB_SUCCESS != err)
    {
      TBSYS_LOG(WARN, "fail to deserialize result_code");
    }
    else
    {
      err = result_code.result_code_;
    }
  }
  FILL_TRACE_LOG("4.deserialize result code. err=%d", err);

  //step5: deserializiton the node status
  if(OB_SUCCESS == err)
  {
    err = serialization::decode_vi32(data_buffer.get_data(), data_buffer.get_position(), pos, reinterpret_cast<int32_t*>(&status));
    if(err != OB_SUCCESS)
    {
      TBSYS_LOG(WARN, "fail to decode status.err=%d", err);
    }
  }
  FILL_TRACE_LOG("5.deserializiton the node status, status=%d, err=%d", status, err);
  return err;  
}
Exemple #10
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 #11
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 #12
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 #13
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 #14
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;
    }
Exemple #15
0
int main(const int argc, char *argv[])
{
  int ret = 0;
  int exit_flag = 0;
  int cmd_type;
  char *token = NULL;

  ObServer root_server;

  ret = parse_obsql_options(argc, argv, root_server);
  if ( OB_SUCCESS != ret)
  {
    usage();
    return ret;
  }

  ret = GFactory::get_instance().initialize(root_server);
  if (OB_SUCCESS != ret)
  {
    fprintf(stderr, "initialize GFactory error, ret=%d\n", ret);
    return ret;
  }

  while ( !exit_flag)
  {
    char cmdbuf[256];
    char *p;

    fprintf(stderr, "obsql>");

    do
    {
      fgets(cmdbuf, sizeof(cmdbuf), stdin);
    }while (strlen(p = trim(cmdbuf)) == 0);

    token = get_token(p);
    cmd_type = get_command_type(token);

    switch(cmd_type)
    {
      case CMD_SHOW:
      {
/*
        const int MAX_SERVER_COUNT = 1000;
        ObServer data_holder[MAX_SERVER_COUNT];
        common::ObArrayHelper<ObServer> servers(MAX_SERVER_COUNT, data_holder);
*/
        ObServer server;
        cmd_type = parse_show_command(p, server);
        switch(cmd_type)
        {
          case CMD_SHOW_SCHEMA:
            ret = show_schema(); 
            break;
          case CMD_SHOW_TABLET:
          {
/*
            ObTabletShower tablet_shower(GFactory::get_instance().get_rpc_stub(), server);
            ret = parse_number_range(p, tablet_shower.get_disks_array());
            if (ret != OB_SUCCESS)
            {
              fprintf(stderr, "parse array numbers error, ret=%d\n", ret);
              return ret;
            }
            
            tablet_shower.output();
*/
            fprintf(stderr, "Cannot support to show tablets now! \n");
            break;
          }
          case CMD_SHOW_STATS_ROOT:
            ret = show_server_stats_info(common::ObStatManager::SERVER_TYPE_ROOT, 
                                         (server.get_ipv4() != 0) ? &server : NULL,
                                         0);
            break;
          case CMD_SHOW_STATS_UPDATE:
            ret = show_server_stats_info(common::ObStatManager::SERVER_TYPE_UPDATE,
                                         (server.get_ipv4() != 0) ? &server : NULL,
                                         0);
            break;
          case CMD_SHOW_STATS_CHUNK:
            ret = show_server_stats_info(common::ObStatManager::SERVER_TYPE_CHUNK, 
                                         (server.get_ipv4() != 0) ? &server : NULL,
                                         0);
            break;
          case CMD_SHOW_STATS_MERGE:
            ret = show_server_stats_info(common::ObStatManager::SERVER_TYPE_MERGE,
                                         (server.get_ipv4() != 0) ? &server : NULL,
                                         0);
            break;
          case CMD_MIN:
          default:
            fprintf(stderr, "wrong command. Try \"help\" for more help\n");
            break;
        }
        break;
      }
      case CMD_HELP:
        cmd_usage();
        break;
      case CMD_EXIT:
        fprintf(stderr, "exit the obsql\n");
        exit_flag = 1;
        break;
      case CMD_SET:
        /* To be done */
        break;
      case CMD_SELECT:
      {
        p = putback_token(token, p);
        oceanbase::obsql::SelectStmt select_stmt(p, strlen(p) + 1, 
                                                 GFactory::get_instance().get_rpc_stub(),
                                                 GFactory::get_instance().get_rowkey_map());
        select_stmt.query();

        break;
      }
      case CMD_INSERT:
      {
        p = putback_token(token, p);
        oceanbase::obsql::InsertStmt insert_stmt(p, strlen(p) + 1, 
                                                 GFactory::get_instance().get_rpc_stub(),
                                                 GFactory::get_instance().get_rowkey_map());

        insert_stmt.query();
        
        break;
      }
      case CMD_UPDATE:
      {
        p = putback_token(token, p);
        oceanbase::obsql::UpdateStmt update_stmt(p, strlen(p) + 1,
                                                 GFactory::get_instance().get_rpc_stub(),
                                                 GFactory::get_instance().get_rowkey_map());
        update_stmt.query();

        break;
      }
      case CMD_DELETE:
      {
        p = putback_token(token, p);
        oceanbase::obsql::DeleteStmt delete_stmt(p, strlen(p) + 1,
                                                 GFactory::get_instance().get_rpc_stub(),
                                                 GFactory::get_instance().get_rowkey_map());
        delete_stmt.query();

        break;
      }
      case CMD_MIN:
      case CMD_MAX:
      default:
        fprintf(stderr, "wrong command. Try \"help\" for more help\n");
        break;
    }
    
  }

  GFactory::get_instance().stop();
  GFactory::get_instance().wait();

  return ret;
}
Exemple #16
0
/* this function will be replace by command parser */
int parse_show_command(char *&cmdstr, ObServer& server)
{
    
  char *token;
  int cmd_type = CMD_MIN;
  
  token = get_token(cmdstr);

  if (token == NULL)
  {
    return cmd_type;
  }
  else if (strcmp(token, "schema") == 0)
  {
    cmd_type = CMD_SHOW_SCHEMA;
  }
  else if (strcmp(token, "tablets") == 0)
  {
    cmd_type = CMD_SHOW_TABLET;
    token = get_token(cmdstr);

    if (token)
    {
      /* not follow by an IP string */
      if (NULL == strstr(token, "."))
      {
        cmdstr = putback_token(token, cmdstr);
        return cmd_type; 
      }
      
      get_cs_or_ms_server(token, true, server);
      if (server.get_ipv4() == 0)
        cmd_type = CMD_MIN;/* wrong IP */
    }
  }  
  else if (strcmp(token, "stats") == 0)
  {
    token = get_token(cmdstr);
    if (token == NULL)
    {
      return cmd_type;
    }
    else if (strcmp(token, "root_server") == 0)
    {
      cmd_type = CMD_SHOW_STATS_ROOT;
      server = GFactory::get_instance().get_rpc_stub().get_root_server();
    }
    else if (strcmp(token, "update_server") == 0)
    {
      cmd_type = CMD_SHOW_STATS_UPDATE;
      server = GFactory::get_instance().get_rpc_stub().get_update_server();
    }
    else if (strcmp(token, "chunk_server") == 0)
    {
      cmd_type = CMD_SHOW_STATS_CHUNK;
      token = get_token(cmdstr);

      if (token)
      {
        get_cs_or_ms_server(token, true, server);
        if (server.get_ipv4() == 0)
          cmd_type = CMD_MIN;/* wrong IP */
      }
    }
    else if (strcmp(token, "merge_server") == 0)
    {
      cmd_type = CMD_SHOW_STATS_MERGE;
      token = get_token(cmdstr);
        
      if (token)
      {
        get_cs_or_ms_server(token, false, server);
        if (server.get_ipv4() == 0)
          cmd_type = CMD_MIN;/* wrong IP */
      }
    }
  }

  return cmd_type;
}
// 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;
}