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.
|
|
|
|
|
2019-06-27 08:05:18 +02:00
|
|
|
#include "m_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" },
|
2019-04-26 14:28:18 +02:00
|
|
|
{ "default", 5L },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::fetch::auth_timeout)
|
|
|
|
ircd::m::fetch::auth_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.fetch.auth.timeout" },
|
|
|
|
{ "default", 15L },
|
2019-04-15 20:37:13 +02:00
|
|
|
};
|
|
|
|
|
2019-06-06 02:12:02 +02:00
|
|
|
decltype(ircd::m::fetch::requests_max)
|
|
|
|
ircd::m::fetch::requests_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.fetch.requests.max" },
|
|
|
|
{ "default", 256L },
|
|
|
|
};
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
decltype(ircd::m::fetch::hook)
|
|
|
|
ircd::m::fetch::hook
|
|
|
|
{
|
2019-04-30 22:57:12 +02:00
|
|
|
hook_handle,
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
|
|
|
{ "_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-26 13:00:58 +02:00
|
|
|
//
|
|
|
|
// fetch_phase
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2019-04-30 22:57:12 +02:00
|
|
|
ircd::m::fetch::hook_handle(const event &event,
|
|
|
|
vm::eval &eval)
|
2019-04-26 13:00:58 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
|
|
|
assert(eval.opts->fetch);
|
2019-04-30 22:57:12 +02:00
|
|
|
const auto &opts{*eval.opts};
|
2019-04-26 13:00:58 +02:00
|
|
|
|
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
at<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type == "m.room.create")
|
|
|
|
return;
|
|
|
|
|
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
2019-07-06 07:01:34 +02:00
|
|
|
event.event_id
|
2019-04-26 13:00:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// Can't construct m::room with the event_id argument because it
|
|
|
|
// won't be found (we're evaluating that event here!) so we just set
|
|
|
|
// the member manually to make further use of the room struct.
|
2019-04-26 13:00:58 +02:00
|
|
|
m::room room{room_id};
|
|
|
|
room.event_id = event_id;
|
|
|
|
|
2019-04-30 22:57:12 +02:00
|
|
|
evaltab tab;
|
|
|
|
if(opts.fetch_auth_check)
|
|
|
|
hook_handle_auth(event, eval, tab, room);
|
|
|
|
|
|
|
|
if(opts.fetch_prev_check)
|
|
|
|
hook_handle_prev(event, eval, tab, room);
|
|
|
|
|
|
|
|
log::debug
|
2019-04-26 13:00:58 +02:00
|
|
|
{
|
2019-04-30 22:57:12 +02:00
|
|
|
log, "%s %s %s ac:%zu ae:%zu pc:%zu pe:%zu pf:%zu",
|
|
|
|
loghead(eval),
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-04-30 22:57:12 +02:00
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
tab.auth_count,
|
|
|
|
tab.auth_exists,
|
|
|
|
tab.prev_count,
|
|
|
|
tab.prev_exists,
|
|
|
|
tab.prev_fetched,
|
2019-04-26 13:00:58 +02:00
|
|
|
};
|
2019-04-30 22:57:12 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-05-20 00:28:34 +02:00
|
|
|
log::derror
|
2019-04-26 13:00:58 +02:00
|
|
|
{
|
2019-04-30 22:57:12 +02:00
|
|
|
log, "%s %s :%s",
|
|
|
|
loghead(eval),
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-04-30 22:57:12 +02:00
|
|
|
e.what(),
|
2019-04-26 13:00:58 +02:00
|
|
|
};
|
|
|
|
|
2019-04-30 22:57:12 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::hook_handle_auth(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
evaltab &tab,
|
|
|
|
const room &room)
|
|
|
|
|
|
|
|
{
|
2019-06-02 03:19:07 +02:00
|
|
|
// Count how many of the auth_events provided exist locally.
|
2019-04-30 22:57:12 +02:00
|
|
|
const auto &opts{*eval.opts};
|
|
|
|
const event::prev prev{event};
|
2019-07-14 02:23:50 +02:00
|
|
|
tab.auth_count = prev.auth_events_count();
|
2019-04-30 22:57:12 +02:00
|
|
|
for(size_t i(0); i < tab.auth_count; ++i)
|
2019-04-26 13:00:58 +02:00
|
|
|
{
|
|
|
|
const auto &auth_id
|
|
|
|
{
|
|
|
|
prev.auth_event(i)
|
|
|
|
};
|
|
|
|
|
2019-04-30 22:57:12 +02:00
|
|
|
tab.auth_exists += bool(m::exists(auth_id));
|
2019-04-26 13:00:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// We are satisfied at this point if all auth_events for this event exist,
|
|
|
|
// as those events have themselves been successfully evaluated and so forth.
|
2019-04-30 22:57:12 +02:00
|
|
|
assert(tab.auth_exists <= tab.auth_count);
|
|
|
|
if(tab.auth_exists == tab.auth_count)
|
|
|
|
return;
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// At this point we are missing one or more auth_events for this event.
|
2019-04-30 22:57:12 +02:00
|
|
|
log::dwarning
|
2019-04-26 13:00:58 +02:00
|
|
|
{
|
2019-04-30 22:57:12 +02:00
|
|
|
log, "%s %s auth_events:%zu hit:%zu miss:%zu",
|
|
|
|
loghead(eval),
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-04-30 22:57:12 +02:00
|
|
|
tab.auth_count,
|
|
|
|
tab.auth_exists,
|
|
|
|
tab.auth_count - tab.auth_exists,
|
|
|
|
};
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// We need to figure out where best to sling a request to fetch these
|
|
|
|
// missing auth_events. We prefer the remote client conducting this eval
|
|
|
|
// with their /federation/send/ request which we stored in the opts.
|
2019-05-17 17:49:51 +02:00
|
|
|
const string_view &remote
|
2019-04-30 22:57:12 +02:00
|
|
|
{
|
|
|
|
opts.node_id?
|
|
|
|
opts.node_id:
|
|
|
|
!my_host(json::get<"origin"_>(event))?
|
|
|
|
string_view(json::get<"origin"_>(event)):
|
2019-05-17 18:05:32 +02:00
|
|
|
!my_host(room.room_id.host())? //TODO: XXX
|
|
|
|
room.room_id.host():
|
2019-04-30 22:57:12 +02:00
|
|
|
string_view{}
|
|
|
|
};
|
2019-04-29 20:30:12 +02:00
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// Bail out here if we can't or won't attempt fetching auth_events.
|
2019-04-30 22:57:12 +02:00
|
|
|
if(!opts.fetch_auth || !bool(m::fetch::enable) || !remote)
|
|
|
|
throw vm::error
|
2019-04-26 13:00:58 +02:00
|
|
|
{
|
2019-04-30 22:57:12 +02:00
|
|
|
vm::fault::EVENT, "Failed to fetch auth_events for %s in %s",
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-04-30 22:57:12 +02:00
|
|
|
json::get<"room_id"_>(event)
|
2019-04-26 13:00:58 +02:00
|
|
|
};
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// This is a blocking call to recursively fetch and evaluate the auth_chain
|
|
|
|
// for this event. Upon return all of the auth_events for this event will
|
|
|
|
// have themselves been fetched and auth'ed recursively or throws.
|
2019-06-02 03:20:02 +02:00
|
|
|
auth_chain(room, remote);
|
|
|
|
tab.auth_exists = tab.auth_count;
|
2019-04-30 22:57:12 +02:00
|
|
|
}
|
2019-04-26 13:00:58 +02:00
|
|
|
|
2019-04-30 22:57:12 +02:00
|
|
|
void
|
|
|
|
ircd::m::fetch::hook_handle_prev(const event &event,
|
|
|
|
vm::eval &eval,
|
|
|
|
evaltab &tab,
|
|
|
|
const room &room)
|
|
|
|
{
|
|
|
|
const auto &opts{*eval.opts};
|
|
|
|
const event::prev prev{event};
|
2019-07-14 02:23:50 +02:00
|
|
|
tab.prev_count = prev.prev_events_count();
|
2019-04-30 22:57:12 +02:00
|
|
|
for(size_t i(0); i < tab.prev_count; ++i)
|
2019-04-26 13:00:58 +02:00
|
|
|
{
|
|
|
|
const auto &prev_id
|
|
|
|
{
|
|
|
|
prev.prev_event(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(m::exists(prev_id))
|
|
|
|
{
|
2019-04-30 22:57:12 +02:00
|
|
|
++tab.prev_exists;
|
2019-04-26 13:00:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool can_fetch
|
|
|
|
{
|
|
|
|
opts.fetch_prev && bool(m::fetch::enable)
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool fetching
|
|
|
|
{
|
2019-04-30 22:57:12 +02:00
|
|
|
can_fetch && start(room.room_id, prev_id)
|
2019-04-26 13:00:58 +02:00
|
|
|
};
|
|
|
|
|
2019-04-30 22:57:12 +02:00
|
|
|
tab.prev_fetching += fetching;
|
2019-04-26 13:00:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// If we have all of the referenced prev_events we are satisfied here.
|
2019-04-30 22:57:12 +02:00
|
|
|
assert(tab.prev_exists <= tab.prev_count);
|
|
|
|
if(tab.prev_exists == tab.prev_count)
|
|
|
|
return;
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// At this point one or more prev_events are missing; the fetches were
|
|
|
|
// launched asynchronously if the options allowed for it.
|
2019-04-30 22:57:12 +02:00
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "%s %s prev_events:%zu hit:%zu miss:%zu fetching:%zu",
|
|
|
|
loghead(eval),
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-04-30 22:57:12 +02:00
|
|
|
tab.prev_count,
|
|
|
|
tab.prev_exists,
|
|
|
|
tab.prev_count - tab.prev_exists,
|
|
|
|
tab.prev_fetching,
|
|
|
|
};
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// If the options want to wait for the fetch+evals of the prev_events to occur
|
|
|
|
// before we continue processing this event further, we block in here.
|
2019-04-30 22:57:12 +02:00
|
|
|
const bool &prev_wait{opts.fetch_prev_wait};
|
|
|
|
if(prev_wait && tab.prev_fetching) for(size_t i(0); i < tab.prev_count; ++i)
|
2019-04-26 13:00:58 +02:00
|
|
|
{
|
|
|
|
const auto &prev_id
|
|
|
|
{
|
|
|
|
prev.prev_event(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
dock.wait([&prev_id]
|
|
|
|
{
|
|
|
|
return !requests.count(prev_id);
|
|
|
|
});
|
|
|
|
|
2019-04-30 22:57:12 +02:00
|
|
|
tab.prev_fetched += m::exists(prev_id);
|
2019-04-26 13:00:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// Aborts this event if the options want us to guarantee at least one
|
|
|
|
// prev_event was fetched and evaluated for this event. This is generally
|
|
|
|
// used in conjunction with the fetch_prev_wait option to be effective.
|
2019-04-30 22:57:12 +02:00
|
|
|
const bool &prev_any{opts.fetch_prev_any};
|
|
|
|
if(prev_any && tab.prev_exists + tab.prev_fetched == 0)
|
2019-04-26 13:00:58 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
|
|
|
vm::fault::EVENT, "Failed to fetch any prev_events for %s in %s",
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-04-30 22:57:12 +02:00
|
|
|
json::get<"room_id"_>(event)
|
2019-04-26 13:00:58 +02:00
|
|
|
};
|
|
|
|
|
2019-06-02 03:19:07 +02:00
|
|
|
// Aborts this event if the options want us to guarantee ALL of the
|
|
|
|
// prev_events were fetched and evaluated for this event.
|
2019-04-30 22:57:12 +02:00
|
|
|
const bool &prev_all{opts.fetch_prev_all};
|
|
|
|
if(prev_all && tab.prev_exists + tab.prev_fetched < tab.prev_count)
|
2019-04-26 13:00:58 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
|
|
|
vm::fault::EVENT, "Failed to fetch all %zu required prev_events for %s in %s",
|
2019-04-30 22:57:12 +02:00
|
|
|
tab.prev_count,
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-04-30 22:57:12 +02:00
|
|
|
json::get<"room_id"_>(event)
|
2019-04-26 13:00:58 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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)
|
2019-06-02 03:20:27 +02:00
|
|
|
try
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-26 12:40:38 +02:00
|
|
|
thread_local char rembuf[64];
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Fetching auth chain for %s in %s from %s",
|
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{room.room_id},
|
|
|
|
string(rembuf, remote),
|
|
|
|
};
|
|
|
|
|
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-06-02 03:20:27 +02:00
|
|
|
request.wait(seconds(auth_timeout));
|
2018-11-30 21:59:44 +01:00
|
|
|
request.get();
|
2019-06-23 01:12:14 +02:00
|
|
|
const json::array events
|
2019-04-12 13:02:09 +02:00
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-04-26 12:40:38 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Evaluating %zu auth events in chain for %s in %s from %s",
|
|
|
|
events.size(),
|
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{room.room_id},
|
|
|
|
string(rembuf, remote),
|
|
|
|
};
|
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
m::vm::opts vmopts;
|
|
|
|
vmopts.infolog_accept = true;
|
2019-04-26 12:41:11 +02:00
|
|
|
vmopts.fetch_prev_check = false;
|
|
|
|
vmopts.fetch_state_check = false;
|
2019-06-02 03:20:27 +02:00
|
|
|
vmopts.warnlog &= ~vm::fault::EXISTS;
|
2019-04-26 14:29:42 +02:00
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
events, vmopts
|
|
|
|
};
|
2019-04-12 13:02:09 +02:00
|
|
|
}
|
2019-06-02 03:20:27 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
thread_local char rembuf[64];
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Fetching auth chain for %s in %s from %s :%s",
|
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{room.room_id},
|
|
|
|
string(rembuf, remote),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2019-04-12 13:02:09 +02:00
|
|
|
|
|
|
|
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-06-06 02:12:02 +02:00
|
|
|
if(count() > size_t(requests_max))
|
|
|
|
return false;
|
|
|
|
|
2019-04-18 10:20:49 +02:00
|
|
|
return submit(event_id, room_id);
|
2019-04-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
|
2019-07-14 18:42:43 +02:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::cancel(request &request)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-05 03:50:17 +02:00
|
|
|
size_t
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::count()
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each([&ret](const auto &request)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
2019-05-17 17:18:31 +02:00
|
|
|
template<class... args>
|
|
|
|
bool
|
|
|
|
ircd::m::fetch::submit(const m::event::id &event_id,
|
|
|
|
const m::room::id &room_id,
|
|
|
|
const size_t &bufsz,
|
|
|
|
args&&... a)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(room_id && event_id);
|
|
|
|
auto it(requests.lower_bound(string_view(event_id)));
|
|
|
|
if(it != end(requests) && it->event_id == event_id)
|
|
|
|
{
|
|
|
|
assert(it->room_id == room_id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
it = requests.emplace_hint(it, room_id, event_id, bufsz, std::forward<args>(a)...);
|
|
|
|
auto &request(const_cast<fetch::request &>(*it)); try
|
|
|
|
{
|
|
|
|
while(!start(request))
|
|
|
|
request.origin = {};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "Failed to start any fetch for %s in %s :%s",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-05-17 10:46:50 +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-26 14:28:50 +02:00
|
|
|
if(it->finished == -1)
|
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());
|
2019-06-05 05:08:08 +02:00
|
|
|
for(auto it(begin(requests)); it != end(requests); ++it)
|
|
|
|
{
|
|
|
|
auto &request(const_cast<fetch::request &>(*it));
|
|
|
|
if(request.finished < 0 || request.last == std::numeric_limits<time_t>::max())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(request.finished == 0 && timedout(request, now))
|
|
|
|
{
|
|
|
|
retry(request);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(request.finished > 0 && timedout(request, now))
|
|
|
|
{
|
|
|
|
request.finished = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 06:16:00 +02:00
|
|
|
|
2019-06-05 05:08:08 +02:00
|
|
|
request_cleanup();
|
2019-04-11 06:16:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
next.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(requests))
|
|
|
|
return;
|
|
|
|
|
|
|
|
request_handle(it);
|
2019-06-05 05:08:08 +02:00
|
|
|
dock.notify_all();
|
2019-04-11 06:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::request_handle(const decltype(requests)::iterator &it)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &request
|
|
|
|
{
|
|
|
|
const_cast<fetch::request &>(*it)
|
|
|
|
};
|
|
|
|
|
2019-06-05 05:08:08 +02:00
|
|
|
if(!request.started || !request.last || request.finished < 0)
|
2019-04-16 00:26:52 +02:00
|
|
|
return;
|
|
|
|
|
2019-06-05 05:08:08 +02:00
|
|
|
if(!request.finished && !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-26 14:28:50 +02:00
|
|
|
{
|
|
|
|
request.finished = -1;
|
|
|
|
std::rethrow_exception(request.eptr);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2019-04-11 06:16:00 +02:00
|
|
|
|
2019-06-05 05:08:08 +02:00
|
|
|
assert(!request.eptr);
|
|
|
|
request.last = std::numeric_limits<time_t>::max();
|
2019-04-11 06:16:00 +02:00
|
|
|
complete.emplace_back(it);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-04-26 12:40:38 +02:00
|
|
|
log, "request %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-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-26 14:28:50 +02:00
|
|
|
const unwind free{[&request]
|
2019-04-11 08:29:56 +02:00
|
|
|
{
|
2019-04-26 14:28:50 +02:00
|
|
|
request.finished = -1;
|
2019-06-05 05:08:08 +02:00
|
|
|
dock.notify_all();
|
2019-04-11 08:29:56 +02:00
|
|
|
}};
|
|
|
|
|
2019-04-26 14:28:50 +02:00
|
|
|
assert(!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-07-14 04:14:53 +02:00
|
|
|
const m::event event
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
2019-07-14 04:14:53 +02:00
|
|
|
json::object{request}, request.event_id
|
2019-04-11 06:16:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-07-14 04:14:53 +02:00
|
|
|
event, opts
|
2019-04-11 06:16:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
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-07-14 02:18:53 +02:00
|
|
|
request.last = ircd::time(); try
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-07-14 02:18:53 +02:00
|
|
|
*static_cast<m::v1::event *>(&request) =
|
|
|
|
{
|
|
|
|
request.event_id, request.buf, std::move(opts)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2019-07-14 18:42:43 +02:00
|
|
|
server::cancel(request);
|
2019-07-14 02:18:53 +02:00
|
|
|
throw;
|
|
|
|
}
|
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();
|
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}
|
|
|
|
{
|
|
|
|
}
|