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" },
|
|
|
|
{ "default", false },
|
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::init()
|
2018-06-03 19:02:19 +02:00
|
|
|
{
|
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
2018-06-03 19:02:19 +02:00
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
void
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::fini()
|
2018-06-03 19:02:19 +02:00
|
|
|
{
|
2019-04-11 06:16:00 +02:00
|
|
|
request_context.terminate();
|
|
|
|
eval_context.terminate();
|
|
|
|
request_context.join();
|
|
|
|
eval_context.join();
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// fetch_phase
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2018-11-30 21:59:44 +01:00
|
|
|
ircd::m::fetch::hook_handler(const event &event,
|
|
|
|
vm::eval &eval)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
assert(eval.opts);
|
2018-09-06 10:23:48 +02:00
|
|
|
const auto &opts{*eval.opts};
|
2018-11-30 21:59:44 +01:00
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
at<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type == "m.room.create")
|
|
|
|
return;
|
2018-09-06 10:23:48 +02:00
|
|
|
|
2019-03-27 21:45:21 +01:00
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
|
|
|
at<"event_id"_>(event)
|
|
|
|
};
|
|
|
|
|
2018-11-30 21:59:44 +01:00
|
|
|
const m::room::id &room_id
|
2018-09-06 10:23:48 +02:00
|
|
|
{
|
2018-11-30 21:59:44 +01:00
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
if(opts.state_check_exists && !m::exists(room_id))
|
2019-03-27 21:45:21 +01:00
|
|
|
{
|
2019-04-11 06:16:00 +02:00
|
|
|
// Don't pass event_id in ctor here or m::NOT_FOUND.
|
|
|
|
m::room room{room_id};
|
|
|
|
room.event_id = event_id;
|
|
|
|
|
|
|
|
if(opts.state_must_exist && !opts.state_wait && !opts.state_fetch)
|
2018-10-23 18:13:48 +02:00
|
|
|
throw vm::error
|
2018-09-06 10:23:48 +02:00
|
|
|
{
|
2018-11-30 21:59:44 +01:00
|
|
|
vm::fault::STATE, "Missing state for room %s",
|
2018-10-23 18:13:48 +02:00
|
|
|
string_view{room_id}
|
2018-09-06 10:23:48 +02:00
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
if(opts.auth_chain_fetch)
|
2019-04-12 13:02:09 +02:00
|
|
|
fetch::auth_chain(room, room_id.host()); //TODO: XXX
|
2019-03-27 21:45:21 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
const event::prev prev
|
|
|
|
{
|
|
|
|
*eval.event_
|
|
|
|
};
|
|
|
|
|
2018-11-30 21:59:44 +01:00
|
|
|
const size_t prev_count
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2018-11-30 21:59:44 +01:00
|
|
|
size(json::get<"prev_events"_>(prev))
|
2018-09-05 08:27:01 +02:00
|
|
|
};
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
size_t prev_exists(0);
|
2018-09-05 08:27:01 +02:00
|
|
|
for(size_t i(0); i < prev_count; ++i)
|
|
|
|
{
|
2018-11-30 21:59:44 +01:00
|
|
|
const auto &prev_id
|
2018-10-23 18:13:48 +02:00
|
|
|
{
|
2018-11-30 21:59:44 +01:00
|
|
|
prev.prev_event(i)
|
2018-10-23 18:13:48 +02:00
|
|
|
};
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
if(!opts.prev_check_exists)
|
2018-11-30 21:59:44 +01:00
|
|
|
continue;
|
2018-10-23 18:13:48 +02:00
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
if(m::exists(prev_id))
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
|
|
|
++prev_exists;
|
2018-11-30 21:59:44 +01:00
|
|
|
continue;
|
2019-04-11 06:16:00 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 05:53:57 +02:00
|
|
|
const bool can_fetch
|
|
|
|
{
|
|
|
|
opts.prev_fetch && bool(m::fetch::enable)
|
|
|
|
};
|
|
|
|
|
|
|
|
if((!can_fetch || !opts.prev_wait) && opts.prev_must_all_exist)
|
2019-04-11 17:08:47 +02:00
|
|
|
throw vm::error
|
|
|
|
{
|
|
|
|
vm::fault::EVENT, "Missing prev %s in %s in %s",
|
|
|
|
string_view{prev_id},
|
|
|
|
json::get<"event_id"_>(*eval.event_),
|
|
|
|
json::get<"room_id"_>(*eval.event_)
|
|
|
|
};
|
2019-04-11 15:59:09 +02:00
|
|
|
|
2019-04-12 05:53:57 +02:00
|
|
|
if(can_fetch)
|
2019-04-12 13:02:09 +02:00
|
|
|
start(room_id, prev_id);
|
2019-04-11 15:59:09 +02:00
|
|
|
|
2019-04-12 05:53:57 +02:00
|
|
|
if(can_fetch && opts.prev_wait)
|
2019-04-11 15:59:09 +02:00
|
|
|
dock.wait([&prev_id]
|
|
|
|
{
|
|
|
|
return !requests.count(prev_id);
|
|
|
|
});
|
2019-04-11 06:16:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 18:13:48 +02:00
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/fetch.h
|
|
|
|
//
|
|
|
|
|
2019-04-12 14:09:38 +02:00
|
|
|
namespace ircd::m::fetch
|
|
|
|
{
|
|
|
|
static void handle_headfill(const m::room &, const m::feds::result &);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::headfill(const room &room)
|
|
|
|
{
|
|
|
|
m::feds::opts opts;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
|
|
|
|
m::feds::head(opts, [&room]
|
|
|
|
(const auto &result)
|
|
|
|
{
|
|
|
|
handle_headfill(room, result);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::handle_headfill(const m::room &room,
|
|
|
|
const m::feds::result &result)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(result.eptr)
|
|
|
|
std::rethrow_exception(result.eptr);
|
|
|
|
|
|
|
|
const json::array &prev_events
|
|
|
|
{
|
|
|
|
json::object(result.object["event"]).get("prev_events")
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Got %zu heads for %s from '%s'",
|
|
|
|
prev_events.size(),
|
|
|
|
string_view{room.room_id},
|
|
|
|
string_view{result.origin},
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::string &event_id_ : prev_events)
|
|
|
|
{
|
|
|
|
const m::event::id &event_id{event_id_};
|
|
|
|
if(!m::exists(event_id))
|
|
|
|
start(room.room_id, event_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Requesting head events for %s from '%s' :%s",
|
|
|
|
string_view{room.room_id},
|
|
|
|
result.origin,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
namespace ircd::m::fetch
|
|
|
|
{
|
|
|
|
static void handle_backfill(const m::room &, const m::feds::result &);
|
|
|
|
}
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-04-12 13:02:09 +02:00
|
|
|
ircd::m::fetch::backfill(const room &room)
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
2019-04-11 14:29:59 +02:00
|
|
|
m::feds::opts opts;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.event_id = room.event_id;
|
2019-04-12 13:02:09 +02:00
|
|
|
opts.argi[0] = 4;
|
|
|
|
|
|
|
|
m::feds::backfill(opts, [&room]
|
|
|
|
(const auto &result)
|
|
|
|
{
|
|
|
|
handle_backfill(room, result);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::fetch::handle_backfill(const m::room &room,
|
|
|
|
const m::feds::result &result)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(result.eptr)
|
|
|
|
std::rethrow_exception(result.eptr);
|
|
|
|
|
|
|
|
const json::array &pdus
|
|
|
|
{
|
|
|
|
result.object["pdus"]
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Got %zu events for %s off %s from '%s'",
|
|
|
|
pdus.size(),
|
|
|
|
string_view{room.room_id},
|
|
|
|
string_view{room.event_id},
|
|
|
|
string_view{result.origin},
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::object &event : pdus)
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
2019-04-12 13:02:09 +02:00
|
|
|
const m::event::id &event_id
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
2019-04-12 13:02:09 +02:00
|
|
|
unquote(event.get("event_id"))
|
|
|
|
};
|
2019-04-11 14:29:59 +02:00
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
if(m::exists(event_id))
|
|
|
|
continue;
|
2019-04-11 14:29:59 +02:00
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
m::vm::opts vmopts;
|
|
|
|
vmopts.nothrows = -1;
|
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
m::event{event}, vmopts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Requesting backfill for %s off %s from '%s' :%s",
|
|
|
|
string_view{room.room_id},
|
|
|
|
string_view{room.event_id},
|
|
|
|
result.origin,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2019-04-11 14:29:59 +02:00
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::backfill(const room &room,
|
|
|
|
const net::hostport &remote)
|
|
|
|
{
|
|
|
|
m::event::id::buf event_id;
|
|
|
|
if(!room.event_id)
|
|
|
|
event_id = m::v1::fetch_head(room.room_id, remote, room.any_user(my_host(), "join"));
|
|
|
|
|
|
|
|
m::v1::backfill::opts opts;
|
|
|
|
opts.remote = remote;
|
|
|
|
opts.event_id = room.event_id?: event_id;
|
|
|
|
opts.dynamic = true;
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
8_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::backfill request
|
|
|
|
{
|
|
|
|
room.room_id, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
request.wait(seconds(20)); //TODO: conf
|
|
|
|
request.get();
|
|
|
|
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Got %zu events for %s off %s from '%s'",
|
|
|
|
json::array{response["pdus"]}.size(),
|
|
|
|
string_view{room.room_id},
|
|
|
|
string_view{room.event_id},
|
|
|
|
host(remote),
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::object &event : json::array(response["pdus"]))
|
|
|
|
{
|
|
|
|
const m::event::id &event_id
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
2019-04-12 13:02:09 +02:00
|
|
|
unquote(event.get("event_id"))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(m::exists(event_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
m::vm::opts vmopts;
|
|
|
|
vmopts.nothrows = -1;
|
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
m::event{event}, vmopts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ircd::m::fetch
|
|
|
|
{
|
|
|
|
static void handle_state_ids(const m::room &, const m::feds::result &);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
opts.arg[0] = "ids";
|
2019-04-11 14:29:59 +02:00
|
|
|
|
2019-04-12 13:02:09 +02:00
|
|
|
m::feds::state(opts, [&room]
|
|
|
|
(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-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},
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::string &event_id : ids)
|
|
|
|
if(!m::exists(m::event::id(event_id)))
|
|
|
|
start(room.room_id, event_id);
|
|
|
|
}
|
|
|
|
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-04-11 06:16:00 +02:00
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::state_ids(const room &room,
|
|
|
|
const net::hostport &remote)
|
|
|
|
{
|
|
|
|
m::v1::state::opts opts;
|
|
|
|
opts.remote = remote;
|
|
|
|
opts.event_id = room.event_id;
|
|
|
|
opts.ids_only = true;
|
|
|
|
opts.dynamic = true;
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
8_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::state request
|
|
|
|
{
|
|
|
|
room.room_id, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
request.wait(seconds(20)); //TODO: conf
|
|
|
|
request.get();
|
|
|
|
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &auth_chain_ids
|
|
|
|
{
|
|
|
|
response["auth_chain_ids"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &pdu_ids
|
|
|
|
{
|
|
|
|
response["pdu_ids"]
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::string &event_id : auth_chain_ids)
|
2019-04-12 13:02:09 +02:00
|
|
|
if(!m::exists(m::event::id(event_id)))
|
|
|
|
start(room.room_id, event_id);
|
2019-04-11 06:16:00 +02:00
|
|
|
|
|
|
|
for(const json::string &event_id : pdu_ids)
|
2019-04-12 13:02:09 +02:00
|
|
|
if(!m::exists(m::event::id(event_id)))
|
|
|
|
start(room.room_id, event_id);
|
2019-04-12 12:13:40 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
vmopts.prev_check_exists = false;
|
|
|
|
vmopts.warnlog |= m::vm::fault::STATE;
|
|
|
|
vmopts.warnlog &= ~m::vm::fault::EXISTS;
|
|
|
|
vmopts.errorlog &= ~m::vm::fault::STATE;
|
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;
|
|
|
|
|
|
|
|
start(room_id, event_id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::start(const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
{
|
|
|
|
submit(event_id, room_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-12 12:36:03 +02:00
|
|
|
if(r.finished == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(r.finished && empty(r.buf))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2019-04-11 06:16:00 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-11 08:29:56 +02:00
|
|
|
request_cleanup();
|
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-11 08:29:56 +02:00
|
|
|
void
|
|
|
|
ircd::m::fetch::request_cleanup()
|
|
|
|
{
|
|
|
|
auto it(begin(requests));
|
|
|
|
while(it != end(requests))
|
|
|
|
{
|
|
|
|
if(it->finished && empty(it->buf))
|
|
|
|
it = requests.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
void
|
|
|
|
ircd::m::fetch::request_handle()
|
|
|
|
{
|
|
|
|
auto next
|
|
|
|
{
|
|
|
|
ctx::when_any(requests.begin(), requests.end())
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!next.wait(seconds(5), std::nothrow))
|
|
|
|
{
|
|
|
|
for(const auto &request_ : requests)
|
|
|
|
{
|
|
|
|
auto &request(const_cast<fetch::request &>(request_));
|
|
|
|
if(!request.finished)
|
2019-04-12 12:13:40 +02:00
|
|
|
retry(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)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.finished)
|
|
|
|
return;
|
|
|
|
|
2019-04-12 12:13:40 +02:00
|
|
|
if(!handle(request))
|
2019-04-11 06:16:00 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
complete.emplace_back(it);
|
|
|
|
dock.notify_all();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "fetch eval %s in %s :%s",
|
|
|
|
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-11 08:29:56 +02:00
|
|
|
const unwind free{[&request]
|
|
|
|
{
|
2019-04-12 12:13:40 +02:00
|
|
|
request.buf = {};
|
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;
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
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-12 12:13:40 +02:00
|
|
|
opts.remote = request.origin?: select_random_origin(request);
|
|
|
|
start(request, opts);
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-12 12:13:40 +02:00
|
|
|
ircd::m::fetch::start(request &request,
|
|
|
|
m::v1::event::opts &opts)
|
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();
|
|
|
|
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();
|
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;
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(!origins.random(closure, proffer))
|
|
|
|
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-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-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
|
|
|
|
|
|
|
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}
|
|
|
|
{
|
|
|
|
}
|