Ejemplo n.º 1
0
int TaskManager::finish_task(const bool result, const TaskInfo & task)
{
  int ret = OB_SUCCESS;
  if (task.get_token() != task_token_)
  {
    TBSYS_LOG(ERROR, "check task token failed:token[%ld], task[%ld]", 
        task_token_, task.get_token());
    ret = OB_ERROR;
  }
  else
  {
    map<uint64_t, TaskInfo>::iterator it;
    int64_t timestamp = tbsys::CTimeUtil::getTime(); 
    tbsys::CThreadGuard lock(&lock_);
    int64_t task_count = get_server_task_count(task.get_location()[task.get_index()].chunkserver_);
    if (task_count < 1)
    {
      TBSYS_LOG(WARN, "check server task count failed:task[%lu], count[%ld]", task.get_id(), task_count);
    }
    else
    {
      TBSYS_LOG(DEBUG, "server ip = %ld, task_count = %ld", 
                task.get_location()[task.get_index()].chunkserver_.get_ipv4(),
                task_count);
      // wait timeout for next dispatch
      working_queue_[task.get_location()[task.get_index()].chunkserver_] = --task_count;
      // print_access_server();
    }
      
    it = doing_queue_.find(task.get_id());
    if (it != doing_queue_.end())
    {
      if (true == result)
      {
        ++total_finish_count_;
        total_finish_time_ += timestamp - task.get_timestamp();
        complete_queue_.insert(pair<uint64_t, TaskInfo>(task.get_id(), task));
        doing_queue_.erase(it);
      }
      // WARN: not insert into wait queue for timeout if result != true
    }
    else
    {
      it = complete_queue_.find(task.get_id());
      if (it != complete_queue_.end())
      {
        if (true == result)
        {
          // for compute average finish time
          ++total_finish_count_;
          total_finish_time_ += timestamp - task.get_timestamp();
        }
        TBSYS_LOG(WARN, "find the task already finished:task[%lu]", task.get_id());
      }
      else
      {
        TBSYS_LOG(ERROR, "not find this task in doing and complete queue:task[%lu]", task.get_id());
        ret = OB_ERROR;
      }
    }
  }
  TBSYS_LOG(INFO, "finish monitor task [id=%lu] stat:wait[%lu], doing[%lu], finish[%lu], avg_time[%ld]", task.get_id(),
      wait_queue_.size(), doing_queue_.size(), complete_queue_.size(), total_finish_time_ / total_finish_count_);
  return ret;
}