2018-06-03 19:02:19 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2018-10-23 18:13:48 +02:00
|
|
|
#include "s_fetch.h"
|
2018-06-03 19:02:19 +02:00
|
|
|
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Event Fetch Unit", ircd::m::fetch::init, ircd::m::fetch::fini
|
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-04-12 05:53:57 +02:00
|
|
|
decltype(ircd::m::fetch::enable)
|
|
|
|
ircd::m::fetch::enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.fetch.enable" },
|
2019-04-23 22:27:53 +02:00
|
|
|
{ "default", true },
|
2019-04-12 05:53:57 +02:00
|
|
|
};
|
|
|
|
|
2019-04-15 20:37:13 +02:00
|
|
|
decltype(ircd::m::fetch::timeout)
|
|
|
|
ircd::m::fetch::timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.fetch.timeout" },
|
|
|
|
{ "default", 10L },
|
|
|
|
};
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
decltype(ircd::m::fetch::hook)
|
|
|
|
ircd::m::fetch::hook
|
|
|
|
{
|
|
|
|
hook_handler,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.fetch" }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::fetch::request_context)
|
|
|
|
ircd::m::fetch::request_context
|
|
|
|
{
|
|
|
|
"m::fetch req", 512_KiB, &request_worker, context::POST
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::fetch::eval_context)
|
|
|
|
ircd::m::fetch::eval_context
|
|
|
|
{
|
|
|
|
"m::fetch eval", 512_KiB, &eval_worker, context::POST
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::fetch::complete)
|
|
|
|
ircd::m::fetch::complete;
|
|
|
|
|
|
|
|
decltype(ircd::m::fetch::rooms)
|
|
|
|
ircd::m::fetch::rooms;
|
|
|
|
|
|
|
|
decltype(ircd::m::fetch::requests)
|
|
|
|
ircd::m::fetch::requests;
|
|
|
|
|
|
|
|
decltype(ircd::m::fetch::dock)
|
|
|
|
ircd::m::fetch::dock;
|
|
|
|
|
2019-04-22 21:32:54 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::fini()
|
|
|
|
{
|
|
|
|
request_context.terminate();
|
|
|
|
eval_context.terminate();
|
|
|
|
request_context.join();
|
|
|
|
eval_context.join();
|
|
|
|
requests.clear();
|
|
|
|
complete.clear();
|
|
|
|
|
|
|
|
assert(requests.empty());
|
|
|
|
assert(complete.empty());
|
|
|
|
}
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/fetch.h
|
|
|
|
//
|
|
|
|
|
2019-04-18 14:17:49 +02:00
|
|
|
namespace ircd::m::fetch
|
|
|
|
{
|
|
|
|
static m::event::id::buf _head(const m::feds::opts &);
|
|
|
|
static std::map<std::string, size_t> _heads(const m::feds::opts &);
|
2019-04-12 13:02:09 +02:00
|
|
|
static void handle_state_ids(const m::room &, const m::feds::result &);
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:32:54 +02:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::synchronize(const m::room &room)
|
|
|
|
{
|
|
|
|
m::feds::opts opts;
|
|
|
|
opts.op = m::feds::op::head;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.event_id = room.event_id;
|
|
|
|
opts.nothrow_closure = true;
|
|
|
|
opts.closure_errors = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::state_ids(const room &room)
|
|
|
|
{
|
|
|
|
m::feds::opts opts;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.event_id = room.event_id;
|
2019-04-23 03:21:16 +02:00
|
|
|
opts.timeout = seconds(10); //TODO: conf
|
2019-04-18 14:17:49 +02:00
|
|
|
|
|
|
|
m::event::id::buf event_id_buf;
|
|
|
|
if(!opts.event_id)
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "No event_id supplied; fetching heads for %s...",
|
|
|
|
string_view{room.room_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
event_id_buf = _head(opts);
|
|
|
|
opts.event_id = event_id_buf;
|
|
|
|
}
|
2019-04-11 14:29:59 +02:00
|
|
|
|
2019-04-18 14:17:49 +02:00
|
|
|
opts.arg[0] = "ids";
|
|
|
|
opts.op = m::feds::op::state;
|
2019-04-23 03:21:16 +02:00
|
|
|
opts.timeout = seconds(20); //TODO: conf
|
2019-04-18 14:17:49 +02:00
|
|
|
m::feds::acquire(opts, [&room]
|
2019-04-12 13:02:09 +02:00
|
|
|
(const auto &result)
|
|
|
|
{
|
|
|
|
handle_state_ids(room, result);
|
2019-04-11 14:29:59 +02:00
|
|
|
return true;
|
2019-04-11 06:16:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-18 14:17:49 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::fetch::_head(const m::feds::opts &opts)
|
|
|
|
{
|
|
|
|
const auto heads
|
|
|
|
{
|
|
|
|
_heads(opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::max_element(begin(heads), end(heads), []
|
|
|
|
(const auto &a, const auto &b)
|
|
|
|
{
|
|
|
|
return a.second < b.second;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
return it != end(heads)?
|
|
|
|
event::id::buf{it->first}:
|
|
|
|
event::id::buf{};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, size_t>
|
|
|
|
ircd::m::fetch::_heads(const m::feds::opts &opts_)
|
|
|
|
{
|
|
|
|
auto opts(opts_);
|
|
|
|
opts.op = m::feds::op::head;
|
2019-04-22 21:32:54 +02:00
|
|
|
|
2019-04-18 14:17:49 +02:00
|
|
|
std::map<std::string, size_t> heads;
|
|
|
|
m::feds::acquire(opts, [&heads]
|
|
|
|
(const auto &result)
|
|
|
|
{
|
|
|
|
if(result.eptr)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const json::object &event{result.object["event"]};
|
|
|
|
const m::event::prev prev{event};
|
|
|
|
for(size_t i(0); i < prev.prev_events_count(); ++i)
|
|
|
|
{
|
|
|
|
// check for dups to prevent result bias.
|
|
|
|
const auto &prev_event_id(prev.prev_event(i));
|
|
|
|
for(size_t j(0); j < prev.prev_events_count(); ++j)
|
|
|
|
if(i != j && prev.prev_event(j) == prev_event_id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
++heads[prev_event_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return heads;
|
|
|
|
}
|
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
void
|
|
|
|
ircd::m::fetch::handle_state_ids(const m::room &room,
|
|
|
|
const m::feds::result &result)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(result.eptr)
|
|
|
|
std::rethrow_exception(result.eptr);
|
|
|
|
|
|
|
|
const json::array &ids
|
|
|
|
{
|
|
|
|
result.object["pdu_ids"]
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Got %zu state_ids for %s from '%s'",
|
|
|
|
ids.size(),
|
|
|
|
string_view{room.room_id},
|
|
|
|
string_view{result.origin},
|
|
|
|
};
|
|
|
|
|
2019-04-18 10:20:49 +02:00
|
|
|
size_t count(0);
|
2019-04-12 13:02:09 +02:00
|
|
|
for(const json::string &event_id : ids)
|
2019-04-18 10:20:49 +02:00
|
|
|
count += fetch::prefetch(room.room_id, event_id);
|
|
|
|
|
|
|
|
if(count)
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Prefetched %zu of %zu state_ids for %s from '%s'",
|
|
|
|
count,
|
|
|
|
ids.size(),
|
|
|
|
string_view{room.room_id},
|
|
|
|
string_view{result.origin},
|
|
|
|
};
|
2019-04-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Requesting state_ids for %s from '%s' :%s",
|
|
|
|
string_view{room.room_id},
|
|
|
|
result.origin,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-27 21:44:33 +01:00
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-04-12 13:02:09 +02:00
|
|
|
ircd::m::fetch::auth_chain(const room &room,
|
|
|
|
const net::hostport &remote)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2018-11-30 21:59:44 +01:00
|
|
|
m::v1::event_auth::opts opts;
|
|
|
|
opts.remote = remote;
|
|
|
|
opts.dynamic = true;
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-12 13:02:09 +02:00
|
|
|
8_KiB
|
2018-09-05 08:27:01 +02:00
|
|
|
};
|
|
|
|
|
2018-11-30 21:59:44 +01:00
|
|
|
m::v1::event_auth request
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-12 13:02:09 +02:00
|
|
|
room.room_id, room.event_id, buf, std::move(opts)
|
2018-09-05 08:27:01 +02:00
|
|
|
};
|
|
|
|
|
2019-03-27 21:44:33 +01:00
|
|
|
request.wait(seconds(20)); //TODO: conf
|
2018-11-30 21:59:44 +01:00
|
|
|
request.get();
|
2019-04-12 13:02:09 +02:00
|
|
|
const json::array &array
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
std::vector<json::object> events(array.count());
|
|
|
|
std::copy(begin(array), end(array), begin(events));
|
2019-03-27 21:44:33 +01:00
|
|
|
std::sort(begin(events), end(events), []
|
|
|
|
(const json::object &a, const json::object &b)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-03-27 21:44:33 +01:00
|
|
|
return a.at<uint64_t>("depth") < b.at<uint64_t>("depth");
|
2018-11-30 21:59:44 +01:00
|
|
|
});
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
m::vm::opts vmopts;
|
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
|
|
|
vmopts.infolog_accept = true;
|
2019-04-23 02:16:16 +02:00
|
|
|
vmopts.fetch = false;
|
2018-11-30 21:59:44 +01:00
|
|
|
for(const auto &event : events)
|
2019-04-12 13:02:09 +02:00
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
m::event{event}, vmopts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::prefetch(const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
{
|
|
|
|
if(m::exists(event_id))
|
|
|
|
return false;
|
|
|
|
|
2019-04-18 10:20:49 +02:00
|
|
|
return start(room_id, event_id);
|
2019-04-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 10:20:49 +02:00
|
|
|
bool
|
2019-04-12 13:02:09 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::start(const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
{
|
2019-04-12 23:15:06 +02:00
|
|
|
ircd::run::changed::dock.wait([]
|
|
|
|
{
|
|
|
|
return run::level == run::level::RUN ||
|
|
|
|
run::level == run::level::QUIT;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(unlikely(run::level != run::level::RUN))
|
|
|
|
throw m::UNAVAILABLE
|
|
|
|
{
|
|
|
|
"Cannot fetch %s in %s in runlevel '%s'",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
reflect(run::level)
|
|
|
|
};
|
|
|
|
|
2019-04-18 10:20:49 +02:00
|
|
|
return submit(event_id, room_id);
|
2019-04-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::for_each(const std::function<bool (request &)> &closure)
|
|
|
|
{
|
|
|
|
for(auto &request : requests)
|
|
|
|
if(!closure(const_cast<fetch::request &>(request)))
|
2019-03-27 21:44:33 +01:00
|
|
|
return false;
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-03-27 21:44:33 +01:00
|
|
|
return true;
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 04:12:37 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// s_fetch.h
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// fetch_phase
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::hook_handler(const event &event,
|
|
|
|
vm::eval &eval)
|
2019-04-23 03:34:51 +02:00
|
|
|
try
|
2019-04-19 04:12:37 +02:00
|
|
|
{
|
|
|
|
assert(eval.opts);
|
2019-04-22 21:23:37 +02:00
|
|
|
assert(eval.opts->fetch);
|
|
|
|
const auto &opts
|
|
|
|
{
|
|
|
|
*eval.opts
|
|
|
|
};
|
|
|
|
|
2019-04-19 04:12:37 +02:00
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
at<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type == "m.room.create")
|
|
|
|
return;
|
|
|
|
|
2019-04-23 02:16:16 +02:00
|
|
|
if(eval.copts && my(event))
|
|
|
|
return;
|
|
|
|
|
2019-04-19 04:12:37 +02:00
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
|
|
|
at<"event_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-04-24 04:17:30 +02:00
|
|
|
m::room room{room_id};
|
|
|
|
room.event_id = event_id;
|
2019-04-19 04:12:37 +02:00
|
|
|
|
|
|
|
const event::prev prev
|
|
|
|
{
|
|
|
|
*eval.event_
|
|
|
|
};
|
|
|
|
|
2019-04-22 21:28:45 +02:00
|
|
|
const size_t auth_count
|
|
|
|
{
|
|
|
|
size(json::get<"auth_events"_>(prev))
|
|
|
|
};
|
|
|
|
|
2019-04-24 04:17:30 +02:00
|
|
|
size_t auth_exists(0);
|
2019-04-24 02:57:11 +02:00
|
|
|
if(opts.fetch_auth_check) for(size_t i(0); i < auth_count; ++i)
|
2019-04-22 21:28:45 +02:00
|
|
|
{
|
|
|
|
const auto &auth_id
|
|
|
|
{
|
|
|
|
prev.auth_event(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(m::exists(auth_id))
|
|
|
|
++auth_exists;
|
2019-04-24 04:17:30 +02:00
|
|
|
}
|
2019-04-22 21:28:45 +02:00
|
|
|
|
2019-04-24 04:17:30 +02:00
|
|
|
if(opts.fetch_auth_check && auth_exists < auth_count)
|
|
|
|
{
|
2019-04-25 06:30:05 +02:00
|
|
|
const net::hostport remote
|
|
|
|
{
|
|
|
|
eval.opts->node_id?
|
|
|
|
eval.opts->node_id:
|
|
|
|
json::get<"origin"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!opts.fetch_auth || !bool(m::fetch::enable) || !remote || my_host(host(remote)))
|
2019-04-24 04:17:30 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
|
|
|
vm::fault::EVENT, "Failed to fetch auth_events for %s in %s",
|
|
|
|
json::get<"event_id"_>(*eval.event_),
|
|
|
|
json::get<"room_id"_>(*eval.event_)
|
|
|
|
};
|
2019-04-22 21:28:45 +02:00
|
|
|
|
2019-04-25 06:30:05 +02:00
|
|
|
auth_chain(room, remote);
|
2019-04-22 21:28:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 04:12:37 +02:00
|
|
|
const size_t prev_count
|
|
|
|
{
|
|
|
|
size(json::get<"prev_events"_>(prev))
|
|
|
|
};
|
|
|
|
|
2019-04-22 21:28:45 +02:00
|
|
|
size_t prev_exists(0), prev_fetching(0);
|
2019-04-24 02:57:11 +02:00
|
|
|
if(opts.fetch_prev_check) for(size_t i(0); i < prev_count; ++i)
|
2019-04-19 04:12:37 +02:00
|
|
|
{
|
|
|
|
const auto &prev_id
|
|
|
|
{
|
|
|
|
prev.prev_event(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(m::exists(prev_id))
|
|
|
|
{
|
|
|
|
++prev_exists;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool can_fetch
|
|
|
|
{
|
2019-04-22 21:28:45 +02:00
|
|
|
opts.fetch_prev && bool(m::fetch::enable)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool fetching
|
|
|
|
{
|
|
|
|
can_fetch && start(room_id, prev_id)
|
2019-04-19 04:12:37 +02:00
|
|
|
};
|
|
|
|
|
2019-04-22 21:28:45 +02:00
|
|
|
prev_fetching += fetching;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t prev_fetched(0);
|
|
|
|
if(prev_fetching && opts.fetch_prev_wait) for(size_t i(0); i < prev_count; ++i)
|
|
|
|
{
|
|
|
|
const auto &prev_id
|
|
|
|
{
|
|
|
|
prev.prev_event(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
dock.wait([&prev_id]
|
|
|
|
{
|
|
|
|
return !requests.count(prev_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
prev_fetched += m::exists(prev_id);
|
|
|
|
}
|
|
|
|
|
2019-04-24 02:57:11 +02:00
|
|
|
if(opts.fetch_prev_check && opts.fetch_prev_wait && prev_exists + prev_fetched == 0)
|
2019-04-22 21:28:45 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
|
|
|
vm::fault::EVENT, "Failed to fetch any prev_events for %s in %s",
|
|
|
|
json::get<"event_id"_>(*eval.event_),
|
|
|
|
json::get<"room_id"_>(*eval.event_)
|
|
|
|
};
|
|
|
|
|
2019-04-24 02:57:11 +02:00
|
|
|
if(opts.fetch_prev_check && opts.fetch_prev_wait && opts.fetch_prev_all && prev_exists + prev_fetched < prev_count)
|
2019-04-22 21:28:45 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
2019-04-24 02:57:11 +02:00
|
|
|
vm::fault::EVENT, "Failed to fetch all %zu required prev_events for %s in %s",
|
|
|
|
prev_count,
|
2019-04-22 21:28:45 +02:00
|
|
|
json::get<"event_id"_>(*eval.event_),
|
|
|
|
json::get<"room_id"_>(*eval.event_)
|
|
|
|
};
|
2019-04-19 04:12:37 +02:00
|
|
|
}
|
2019-04-23 03:34:51 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-04-24 02:44:18 +02:00
|
|
|
log, "%s %s :%s",
|
2019-04-23 03:34:51 +02:00
|
|
|
loghead(eval),
|
|
|
|
json::get<"event_id"_>(event),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2019-04-19 04:12:37 +02:00
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
//
|
|
|
|
// request worker
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::request_worker()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
dock.wait([]
|
|
|
|
{
|
|
|
|
return std::any_of(begin(requests), end(requests), []
|
|
|
|
(const request &r)
|
|
|
|
{
|
2019-04-25 09:36:38 +02:00
|
|
|
return r.finished == 0;
|
2019-04-11 06:16:00 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-12 22:03:08 +02:00
|
|
|
if(request_cleanup())
|
|
|
|
continue;
|
|
|
|
|
2019-04-16 00:26:52 +02:00
|
|
|
if(requests.empty())
|
|
|
|
continue;
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
request_handle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "fetch request worker :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-04-12 22:03:08 +02:00
|
|
|
size_t
|
2019-04-11 08:29:56 +02:00
|
|
|
ircd::m::fetch::request_cleanup()
|
|
|
|
{
|
2019-04-25 09:36:38 +02:00
|
|
|
// assert that there is no race starting from here.
|
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
|
2019-04-12 22:03:08 +02:00
|
|
|
size_t ret(0);
|
2019-04-11 08:29:56 +02:00
|
|
|
auto it(begin(requests));
|
|
|
|
while(it != end(requests))
|
|
|
|
{
|
2019-04-25 09:36:38 +02:00
|
|
|
if(it->finished && it->eptr)
|
2019-04-12 22:03:08 +02:00
|
|
|
{
|
2019-04-11 08:29:56 +02:00
|
|
|
it = requests.erase(it);
|
2019-04-12 22:03:08 +02:00
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
else ++it;
|
2019-04-11 08:29:56 +02:00
|
|
|
}
|
2019-04-12 22:03:08 +02:00
|
|
|
|
|
|
|
return ret;
|
2019-04-11 08:29:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
void
|
|
|
|
ircd::m::fetch::request_handle()
|
|
|
|
{
|
|
|
|
auto next
|
|
|
|
{
|
|
|
|
ctx::when_any(requests.begin(), requests.end())
|
|
|
|
};
|
|
|
|
|
2019-04-15 20:37:13 +02:00
|
|
|
if(!next.wait(seconds(timeout), std::nothrow))
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
2019-04-15 20:37:13 +02:00
|
|
|
const auto now(ircd::time());
|
|
|
|
for(const auto &request : requests)
|
2019-04-25 09:36:38 +02:00
|
|
|
if(!request.finished && timedout(request, now))
|
2019-04-15 20:37:13 +02:00
|
|
|
retry(const_cast<fetch::request &>(request));
|
2019-04-11 06:16:00 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
next.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(requests))
|
|
|
|
return;
|
|
|
|
|
|
|
|
request_handle(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::request_handle(const decltype(requests)::iterator &it)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &request
|
|
|
|
{
|
|
|
|
const_cast<fetch::request &>(*it)
|
|
|
|
};
|
|
|
|
|
2019-04-25 09:36:38 +02:00
|
|
|
if(!request.started || !request.last || request.finished)
|
2019-04-16 00:26:52 +02:00
|
|
|
return;
|
|
|
|
|
2019-04-25 09:36:38 +02:00
|
|
|
if(!handle(request))
|
2019-04-11 06:16:00 +02:00
|
|
|
return;
|
|
|
|
|
2019-04-25 09:36:38 +02:00
|
|
|
assert(request.finished);
|
|
|
|
if(request.eptr)
|
2019-04-11 06:16:00 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
complete.emplace_back(it);
|
|
|
|
dock.notify_all();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-04-24 02:44:18 +02:00
|
|
|
log, "eval %s in %s :%s",
|
2019-04-11 06:16:00 +02:00
|
|
|
string_view{it->event_id},
|
|
|
|
string_view{it->room_id},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// eval worker
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::eval_worker()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
dock.wait([]
|
|
|
|
{
|
|
|
|
return !complete.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
eval_handle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "fetch eval worker :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::eval_handle()
|
|
|
|
{
|
2019-04-11 08:29:56 +02:00
|
|
|
assert(!complete.empty());
|
2019-04-11 06:16:00 +02:00
|
|
|
const unwind pop{[]
|
|
|
|
{
|
2019-04-11 08:29:56 +02:00
|
|
|
assert(!complete.empty());
|
2019-04-11 06:16:00 +02:00
|
|
|
complete.pop_front();
|
2019-04-12 12:36:03 +02:00
|
|
|
dock.notify_all();
|
2019-04-11 06:16:00 +02:00
|
|
|
}};
|
|
|
|
|
2019-04-11 08:29:56 +02:00
|
|
|
const auto it
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
|
|
|
complete.front()
|
|
|
|
};
|
|
|
|
|
|
|
|
eval_handle(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::eval_handle(const decltype(requests)::iterator &it)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &request
|
|
|
|
{
|
|
|
|
const_cast<fetch::request &>(*it)
|
|
|
|
};
|
|
|
|
|
2019-04-25 09:36:38 +02:00
|
|
|
const unwind free{[&it]
|
2019-04-11 08:29:56 +02:00
|
|
|
{
|
2019-04-25 09:36:38 +02:00
|
|
|
requests.erase(it);
|
2019-04-11 08:29:56 +02:00
|
|
|
}};
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
if(request.eptr)
|
|
|
|
std::rethrow_exception(request.eptr);
|
|
|
|
|
2019-04-11 08:29:56 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "eval handling %s in %s (r:%zu c:%zu)",
|
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
|
|
|
requests.size(),
|
|
|
|
complete.size(),
|
|
|
|
};
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
const json::object &event
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
m::vm::opts opts;
|
|
|
|
opts.infolog_accept = true;
|
2019-04-23 03:21:16 +02:00
|
|
|
opts.fetch_prev = false;
|
2019-04-22 21:27:43 +02:00
|
|
|
opts.fetch_state_wait = false;
|
|
|
|
opts.fetch_auth_wait = false;
|
|
|
|
opts.fetch_prev_wait = false;
|
2019-04-11 06:16:00 +02:00
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
m::event{event}, opts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-04-12 12:36:03 +02:00
|
|
|
auto &request
|
|
|
|
{
|
|
|
|
const_cast<fetch::request &>(*it)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!request.eptr)
|
|
|
|
request.eptr = std::current_exception();
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "fetch eval %s in %s :%s",
|
2019-04-12 12:36:03 +02:00
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
2019-04-11 06:16:00 +02:00
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
//
|
|
|
|
// fetch::request
|
|
|
|
//
|
|
|
|
|
2019-04-12 14:29:12 +02:00
|
|
|
bool
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::start(request &request)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
m::v1::event::opts opts;
|
2019-04-11 06:16:00 +02:00
|
|
|
opts.dynamic = true;
|
2019-04-16 02:18:31 +02:00
|
|
|
if(!request.origin)
|
|
|
|
select_random_origin(request);
|
|
|
|
|
|
|
|
opts.remote = request.origin;
|
2019-04-12 14:29:12 +02:00
|
|
|
return start(request, opts);
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 14:29:12 +02:00
|
|
|
bool
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::start(request &request,
|
|
|
|
m::v1::event::opts &opts)
|
2019-04-12 14:29:12 +02:00
|
|
|
try
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
assert(request.finished == 0);
|
|
|
|
if(!request.started)
|
|
|
|
request.started = ircd::time();
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-04-12 12:13:40 +02:00
|
|
|
request.last = ircd::time();
|
2019-04-15 21:54:35 +02:00
|
|
|
*static_cast<m::v1::event *>(&request) =
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.event_id, request.buf, std::move(opts)
|
2018-09-05 08:27:01 +02:00
|
|
|
};
|
2019-04-11 06:16:00 +02:00
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Started request for %s in %s from '%s'",
|
2019-04-12 12:13:40 +02:00
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
|
|
|
string_view{request.origin},
|
2019-04-11 06:16:00 +02:00
|
|
|
};
|
2019-04-12 12:13:40 +02:00
|
|
|
|
|
|
|
dock.notify_all();
|
2019-04-12 14:29:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-24 04:08:12 +02:00
|
|
|
catch(const http::error &e)
|
2019-04-12 14:29:12 +02:00
|
|
|
{
|
2019-04-24 04:08:12 +02:00
|
|
|
log::logf
|
2019-04-12 23:26:10 +02:00
|
|
|
{
|
2019-04-24 04:08:12 +02:00
|
|
|
log, run::level == run::level::QUIT? log::DERROR: log::ERROR,
|
|
|
|
"Failed to start request for %s in %s to '%s' :%s %s",
|
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
|
|
|
string_view{request.origin},
|
|
|
|
e.what(),
|
|
|
|
e.content,
|
2019-04-12 23:26:10 +02:00
|
|
|
};
|
|
|
|
|
2019-04-24 04:08:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-04-12 23:26:10 +02:00
|
|
|
log::logf
|
2019-04-12 14:29:12 +02:00
|
|
|
{
|
2019-04-24 04:08:12 +02:00
|
|
|
log, run::level == run::level::QUIT? log::DERROR: log::ERROR,
|
|
|
|
"Failed to start request for %s in %s to '%s' :%s",
|
2019-04-12 14:29:12 +02:00
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
|
|
|
string_view{request.origin},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::select_random_origin(request &request)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
const m::room::origins origins
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.room_id
|
2018-09-05 08:27:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// copies randomly selected origin into the attempted set.
|
2019-04-12 12:13:40 +02:00
|
|
|
const auto closure{[&request]
|
2018-09-05 08:27:01 +02:00
|
|
|
(const string_view &origin)
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
select_origin(request, origin);
|
2018-09-05 08:27:01 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
// Tests if origin is potentially viable
|
2019-04-12 12:13:40 +02:00
|
|
|
const auto proffer{[&request]
|
2018-09-05 08:27:01 +02:00
|
|
|
(const string_view &origin)
|
|
|
|
{
|
2018-10-01 22:59:17 +02:00
|
|
|
// Don't want to request from myself.
|
|
|
|
if(my_host(origin))
|
|
|
|
return false;
|
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
// Don't want to use a peer we already tried and failed with.
|
2019-04-12 12:13:40 +02:00
|
|
|
if(request.attempted.count(origin))
|
2018-09-05 08:27:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't want to use a peer marked with an error by ircd::server
|
|
|
|
if(ircd::server::errmsg(origin))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}};
|
|
|
|
|
2019-04-25 15:45:03 +02:00
|
|
|
request.origin = {};
|
2019-04-12 14:29:12 +02:00
|
|
|
if(!origins.random(closure, proffer) || !request.origin)
|
2018-09-05 08:27:01 +02:00
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Cannot find any server to fetch %s in %s",
|
2019-04-12 12:13:40 +02:00
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
2018-09-05 08:27:01 +02:00
|
|
|
};
|
|
|
|
|
2019-04-12 12:13:40 +02:00
|
|
|
return request.origin;
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::select_origin(request &request,
|
|
|
|
const string_view &origin)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
const auto iit
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.attempted.emplace(std::string{origin})
|
2018-09-05 08:27:01 +02:00
|
|
|
};
|
|
|
|
|
2019-04-12 12:13:40 +02:00
|
|
|
request.origin = *iit.first;
|
|
|
|
return request.origin;
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 18:13:48 +02:00
|
|
|
bool
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::handle(request &request)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.wait(); try
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-11 06:16:00 +02:00
|
|
|
const auto code
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.get()
|
2019-04-11 06:16:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%u %s for %s in %s from '%s'",
|
|
|
|
uint(code),
|
|
|
|
status(code),
|
2019-04-12 12:13:40 +02:00
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
|
|
|
string_view{request.origin}
|
2019-04-11 06:16:00 +02:00
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.eptr = std::current_exception();
|
2019-04-11 06:16:00 +02:00
|
|
|
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "Failure for %s in %s from '%s' :%s",
|
2019-04-12 12:13:40 +02:00
|
|
|
string_view{request.event_id},
|
|
|
|
string_view{request.room_id},
|
|
|
|
string_view{request.origin},
|
|
|
|
what(request.eptr),
|
2019-04-11 06:16:00 +02:00
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 12:13:40 +02:00
|
|
|
if(!request.eptr)
|
|
|
|
finish(request);
|
2018-09-05 08:27:01 +02:00
|
|
|
else
|
2019-04-12 12:13:40 +02:00
|
|
|
retry(request);
|
2018-10-23 18:13:48 +02:00
|
|
|
|
2019-04-12 12:13:40 +02:00
|
|
|
return request.finished;
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::retry(request &request)
|
2018-09-05 08:27:01 +02:00
|
|
|
try
|
|
|
|
{
|
2019-04-16 00:26:52 +02:00
|
|
|
assert(!request.finished);
|
|
|
|
assert(request.started && request.last);
|
2019-04-12 12:13:40 +02:00
|
|
|
server::cancel(request);
|
|
|
|
request.eptr = std::exception_ptr{};
|
|
|
|
request.origin = {};
|
|
|
|
start(request);
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.eptr = std::current_exception();
|
|
|
|
finish(request);
|
2018-06-03 19:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::finish(request &request)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-25 09:36:38 +02:00
|
|
|
assert(request.started);
|
2019-04-12 12:13:40 +02:00
|
|
|
request.finished = ircd::time();
|
2019-04-11 06:16:00 +02:00
|
|
|
dock.notify_all();
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
2019-04-12 12:13:40 +02:00
|
|
|
|
2019-04-15 20:37:13 +02:00
|
|
|
bool
|
|
|
|
ircd::m::fetch::timedout(const request &request,
|
|
|
|
const time_t &now)
|
|
|
|
{
|
2019-04-25 09:36:38 +02:00
|
|
|
assert(request.started && request.finished == 0 && request.last != 0);
|
2019-04-15 20:37:13 +02:00
|
|
|
return request.last + seconds(timeout).count() < now;
|
|
|
|
}
|
|
|
|
|
2019-04-12 12:13:40 +02:00
|
|
|
bool
|
|
|
|
ircd::m::fetch::operator<(const request &a,
|
|
|
|
const request &b)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return a.event_id < b.event_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::fetch::operator<(const request &a,
|
|
|
|
const string_view &b)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return a.event_id < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::fetch::operator<(const string_view &a,
|
|
|
|
const request &b)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return a < b.event_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// request::request
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::fetch::request::request(const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
|
|
|
const size_t &bufsz)
|
|
|
|
:room_id{room_id}
|
|
|
|
,event_id{event_id}
|
|
|
|
,buf{bufsz}
|
|
|
|
{
|
|
|
|
}
|