void io_looper::add_timer(task* timer) { uint64_t ts_ms = dsn_now_ms() + timer->delay_milliseconds(); timer->set_delay(0); // put into locked queue when it is shared or from remote threads if (is_shared_timer_queue()) { { utils::auto_lock<::dsn::utils::ex_lock_nr_spin> l(_remote_timer_tasks_lock); auto pr = _remote_timer_tasks.insert( std::map<uint64_t, slist<task>>::value_type(ts_ms, slist<task>())); pr.first->second.add(timer); } _remote_timer_tasks_count++; } // put into local queue else { auto pr = _local_timer_tasks.insert( std::map<uint64_t, slist<task>>::value_type(ts_ms, slist<task>())); pr.first->second.add(timer); } }
std::string perf_counters::list_snapshot_by_regexp(const std::vector<std::string> &args) { perf_counter_info info; std::vector<std::regex> regs; regs.reserve(args.size()); for (auto &arg : args) { try { regs.emplace_back(arg, std::regex_constants::basic); } catch (...) { info.result = "ERROR: invalid filter: " + arg; break; } } if (info.result.empty()) { snapshot_iterator visitor = [®s, &info](const dsn::perf_counter_ptr &ptr, double val) { bool matched = false; if (regs.empty()) { matched = true; } else { for (auto ® : regs) { if (std::regex_match(ptr->full_name(), reg)) { matched = true; break; } } } if (matched) { info.counters.emplace_back(ptr->full_name(), ptr->type(), val); } }; iterate_snapshot(visitor); info.result = "OK"; } std::stringstream ss; info.timestamp = dsn_now_ms() / 1000; char buf[20]; utils::time_ms_to_date_time(info.timestamp * 1000, buf, sizeof(buf)); info.timestamp_str = buf; info.encode_json_state(ss); return ss.str(); }
void nfs_service_impl::close_file() // release out-of-date file handle { zauto_lock l(_handles_map_lock); for (auto it = _handles_map.begin(); it != _handles_map.end();) { auto fptr = it->second; // not used and expired if (fptr->file_access_count == 0 && dsn_now_ms() - fptr->last_access_time > _opts.file_close_expire_time_ms) { dinfo("nfs: close file handle %s", it->first.c_str()); it = _handles_map.erase(it); ::dsn::error_code err = dsn_file_close(fptr->file_handle); dassert(err == ERR_OK, "dsn_file_close failed, err = %s", err.to_string()); delete fptr; } else it++; } }
void meta_server_failure_detector::set_primary(rpc_address primary) { /* * we don't do register worker things in set_primary * as only nodes sync from meta_state_service are useful, * but currently, we haven't do sync yet */ bool old = _is_primary; { utils::auto_lock<zlock> l(_primary_address_lock); _primary_address = primary; _is_primary = (primary == primary_address()); if (_is_primary) { _election_moment = dsn_now_ms(); } } if (old && !_is_primary) { clear_workers(); } }
void replica::broadcast_group_check() { dassert (nullptr != _primary_states.group_check_task, ""); ddebug("%s: start to broadcast group check", name()); if (_primary_states.group_check_pending_replies.size() > 0) { dwarn( "%s: %u group check replies are still pending when doing next round check, cancel first", name(), static_cast<int>(_primary_states.group_check_pending_replies.size()) ); for (auto it = _primary_states.group_check_pending_replies.begin(); it != _primary_states.group_check_pending_replies.end(); ++it) { it->second->cancel(true); } _primary_states.group_check_pending_replies.clear(); } for (auto it = _primary_states.statuses.begin(); it != _primary_states.statuses.end(); ++it) { if (it->first == _stub->_primary_address) continue; ::dsn::rpc_address addr = it->first; std::shared_ptr<group_check_request> request(new group_check_request); request->app = _app_info; request->node = addr; _primary_states.get_replica_config(it->second, request->config); request->last_committed_decree = last_committed_decree(); if (request->config.status == partition_status::PS_POTENTIAL_SECONDARY) { auto it = _primary_states.learners.find(addr); dassert(it != _primary_states.learners.end(), "learner %s is missing", addr.to_string()); request->config.learner_signature = it->second.signature; } ddebug( "%s: send group check to %s with state %s", name(), addr.to_string(), enum_to_string(it->second) ); dsn::task_ptr callback_task = rpc::call( addr, RPC_GROUP_CHECK, *request, this, [=](error_code err, group_check_response&& resp) { auto alloc = std::make_shared<group_check_response>(std::move(resp)); on_group_check_reply(err, request, alloc); }, std::chrono::milliseconds(0), gpid_to_thread_hash(get_gpid()) ); _primary_states.group_check_pending_replies[addr] = callback_task; } // send empty prepare when necessary if (!_options->empty_write_disabled && dsn_now_ms() >= _primary_states.last_prepare_ts_ms + _options->group_check_interval_ms) { mutation_ptr mu = new_mutation(invalid_decree); mu->add_client_request(RPC_REPLICATION_WRITE_EMPTY, nullptr); init_prepare(mu); } }
void nfs_service_impl::on_copy(const ::dsn::service::copy_request& request, ::dsn::rpc_replier< ::dsn::service::copy_response>& reply) { //dinfo(">>> on call RPC_COPY end, exec RPC_NFS_COPY"); std::string file_path = dsn::utils::filesystem::path_combine(request.source_dir, request.file_name); dsn_handle_t hfile; { zauto_lock l(_handles_map_lock); auto it = _handles_map.find(file_path); // find file handle cache first if (it == _handles_map.end()) // not found { hfile = dsn_file_open(file_path.c_str(), O_RDONLY | O_BINARY, 0); if (hfile) { file_handle_info_on_server* fh = new file_handle_info_on_server; fh->file_handle = hfile; fh->file_access_count = 1; fh->last_access_time = dsn_now_ms(); _handles_map.insert(std::pair<std::string, file_handle_info_on_server*>(file_path, fh)); } } else // found { hfile = it->second->file_handle; it->second->file_access_count++; it->second->last_access_time = dsn_now_ms(); } } dinfo("nfs: copy file %s [%" PRId64 ", %" PRId64 ")", file_path.c_str(), request.offset, request.offset + request.size ); if (hfile == 0) { derror("file open failed"); ::dsn::service::copy_response resp; resp.error = ERR_OBJECT_NOT_FOUND; reply(resp); return; } callback_para cp(reply); cp.bb = blob( std::shared_ptr<char>(new char[_opts.nfs_copy_block_bytes], std::default_delete<char[]>{}), _opts.nfs_copy_block_bytes); cp.dst_dir = std::move(request.dst_dir); cp.file_path = std::move(file_path); cp.hfile = hfile; cp.offset = request.offset; cp.size = request.size; auto buffer_save = cp.bb.buffer().get(); file::read( hfile, buffer_save, request.size, request.offset, LPC_NFS_READ, this, [this, cp_cap = std::move(cp)] (error_code err, int sz) { internal_read_callback(err, sz, std::move(cp_cap)); } ); }