void RepeatedTimerTask::schedule(std::unique_lock<raft_mutex_t>& lck) { _next_duetime = butil::milliseconds_from_now(adjust_timeout_ms(_timeout_ms)); if (bthread_timer_add(&_timer, _next_duetime, on_timedout, this) != 0) { lck.unlock(); LOG(ERROR) << "Fail to add timer"; return on_timedout(this); } }
void Stream::StartIdleTimer() { if (_options.idle_timeout_ms < 0) { return; } _start_idle_timer_us = butil::gettimeofday_us(); timespec due_time = butil::microseconds_to_timespec( _start_idle_timer_us + _options.idle_timeout_ms * 1000); const int rc = bthread_timer_add(&_idle_timer, due_time, OnIdleTimeout, (void*)(_consumer_queue.value)); LOG_IF(WARNING, rc != 0) << "Fail to add timer"; }
void DownSpeed::_CalculateSpeed(void* T_this) { DownSpeed* cur_this= static_cast<DownSpeed*>(T_this); cur_this->CalculateSpeed(); //10s auto until_time= butil::seconds_from_now(10); auto code= bthread_timer_add(&cur_this->id,until_time,&DownSpeed::_CalculateSpeed,T_this); if (code!= 0) { Csz::ErrMsg("[%s->%d]->failed,bthread timer add failed code=%d",__func__,__LINE__,code); } return ; }
void Stream::Wait(void (*on_writable)(StreamId, void*, int), void* arg, const timespec* due_time, bool new_thread, bthread_id_t *join_id) { WritableMeta *wm = new WritableMeta; wm->on_writable = on_writable; wm->id = id(); wm->arg = arg; wm->new_thread = new_thread; wm->has_timer = false; bthread_id_t wait_id; const int rc = bthread_id_create(&wait_id, wm, TriggerOnWritable); if (rc != 0) { CHECK(false) << "Fail to create bthread_id, " << berror(rc); wm->error_code = rc; RunOnWritable(wm); return; } if (join_id) { *join_id = wait_id; } CHECK_EQ(0, bthread_id_lock(wait_id, NULL)); if (due_time != NULL) { wm->has_timer = true; const int rc = bthread_timer_add(&wm->timer, *due_time, OnTimedOut, reinterpret_cast<void*>(wait_id.value)); if (rc != 0) { LOG(ERROR) << "Fail to add timer, " << berror(rc); CHECK_EQ(0, TriggerOnWritable(wait_id, wm, rc)); } } bthread_mutex_lock(&_congestion_control_mutex); if (_options.max_buf_size <= 0 || _produced < _remote_consumed + (size_t)_options.max_buf_size) { bthread_mutex_unlock(&_congestion_control_mutex); CHECK_EQ(0, TriggerOnWritable(wait_id, wm, 0)); return; } else { bthread_id_list_add(&_writable_wait_list, wait_id); bthread_mutex_unlock(&_congestion_control_mutex); } CHECK_EQ(0, bthread_id_unlock(wait_id)); }
void RemoteFileCopier::Session::send_next_rpc() { _cntl.Reset(); _response.Clear(); // Not clear request as we need some fields of the previouse RPC off_t offset = _request.offset() + _request.count(); const size_t max_count = (!_buf) ? FLAGS_raft_max_byte_count_per_rpc : UINT_MAX; _cntl.set_timeout_ms(_options.timeout_ms); _request.set_offset(offset); // Read partly when throttled _request.set_read_partly(true); std::unique_lock<raft_mutex_t> lck(_mutex); if (_finished) { return; } // throttle size_t new_max_count = max_count; if (_throttle) { new_max_count = _throttle->throttled_by_throughput(max_count); if (new_max_count == 0) { // Reset count to make next rpc retry the previous one _request.set_count(0); AddRef(); if (bthread_timer_add( &_timer, butil::milliseconds_from_now(_options.retry_interval_ms), on_timer, this) != 0) { lck.unlock(); LOG(ERROR) << "Fail to add timer"; return on_timer(this); } return; } } _request.set_count(new_max_count); _rpc_call = _cntl.call_id(); FileService_Stub stub(_channel); AddRef(); // Release in on_rpc_returned return stub.get_file(&_cntl, &_request, &_response, &_done); }
void RemoteFileCopier::Session::on_rpc_returned() { scoped_refptr<Session> ref_gurad; Session* this_ref = this; ref_gurad.swap(&this_ref); std::unique_lock<raft_mutex_t> lck(_mutex); if (_finished) { return; } if (_cntl.Failed()) { // Reset count to make next rpc retry the previous one _request.set_count(0); if (_cntl.ErrorCode() == ECANCELED) { if (_st.ok()) { _st.set_error(_cntl.ErrorCode(), _cntl.ErrorText()); return on_finished(); } } // Throttled reading failure does not increase _retry_times if (_cntl.ErrorCode() != EAGAIN && _retry_times++ >= _options.max_retry) { if (_st.ok()) { _st.set_error(_cntl.ErrorCode(), _cntl.ErrorText()); return on_finished(); } } AddRef(); if (bthread_timer_add( &_timer, butil::milliseconds_from_now(_options.retry_interval_ms), on_timer, this) != 0) { lck.unlock(); LOG(ERROR) << "Fail to add timer"; return on_timer(this); } return; } _retry_times = 0; // Reset count to |real_read_size| to make next rpc get the right offset if (_response.has_read_size() && (_response.read_size() != 0)) { _request.set_count(_response.read_size()); } if (_file) { FileSegData data(_cntl.response_attachment()); uint64_t seg_offset = 0; butil::IOBuf seg_data; while (0 != data.next(&seg_offset, &seg_data)) { ssize_t nwritten = _file->write(seg_data, seg_offset); if (static_cast<size_t>(nwritten) != seg_data.size()) { LOG(WARNING) << "Fail to write into file: " << _dest_path; _st.set_error(EIO, "%s", berror(EIO)); return on_finished(); } seg_data.clear(); } } else { FileSegData data(_cntl.response_attachment()); uint64_t seg_offset = 0; butil::IOBuf seg_data; while (0 != data.next(&seg_offset, &seg_data)) { CHECK_GE((size_t)seg_offset, _buf->length()); _buf->resize(seg_offset); _buf->append(seg_data); } } if (_response.eof()) { on_finished(); return; } lck.unlock(); return send_next_rpc(); }