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-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-11 06:16:00 +02:00
|
|
|
if(opts.state_check_exists && !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)
|
|
|
|
{
|
|
|
|
const m::room::auth auth{room};
|
|
|
|
auth.chain_eval(auth, event_id.host());
|
|
|
|
}
|
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
|
|
|
|
2018-11-30 21:59:44 +01:00
|
|
|
if(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
|
|
|
}
|
|
|
|
|
|
|
|
if(!opts.prev_fetch || !opts.prev_wait)
|
|
|
|
if(opts.prev_must_all_exist)
|
|
|
|
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_)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 18:13:48 +02:00
|
|
|
|
2019-04-11 06:16:00 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/fetch.h
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::fetch::state_ids(const room &room)
|
|
|
|
{
|
2019-04-11 14:29:59 +02:00
|
|
|
m::feds::opts opts;
|
|
|
|
opts.room_id = room.room_id;
|
|
|
|
opts.event_id = room.event_id;
|
|
|
|
opts.ids = true;
|
|
|
|
m::feds::state(opts, [&room](const auto &result)
|
2019-04-11 06:16:00 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-04-11 14:29:59 +02:00
|
|
|
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(!exists(m::event::id(event_id)))
|
|
|
|
start(event_id, room.room_id);
|
2019-04-11 06:16:00 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Requesting state_ids for %s from '%s' :%s",
|
|
|
|
string_view{room.room_id},
|
2019-04-11 14:29:59 +02:00
|
|
|
result.origin,
|
2019-04-11 06:16:00 +02:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2019-04-11 14:29:59 +02:00
|
|
|
|
|
|
|
return true;
|
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)
|
|
|
|
if(!exists(m::event::id(event_id)))
|
|
|
|
start(event_id, room.room_id);
|
|
|
|
|
|
|
|
for(const json::string &event_id : pdu_ids)
|
|
|
|
if(!exists(m::event::id(event_id)))
|
|
|
|
start(event_id, room.room_id);
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
2018-06-03 19:02:19 +02:00
|
|
|
//
|
2019-03-27 21:44:33 +01:00
|
|
|
// auth chain fetch
|
2018-06-03 19:02:19 +02:00
|
|
|
//
|
|
|
|
|
2019-03-27 21:44:33 +01:00
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::auth::chain_eval(const auth &auth,
|
|
|
|
const net::hostport &remote)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-03-27 21:44:33 +01:00
|
|
|
m::vm::opts opts;
|
|
|
|
opts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
|
|
|
opts.infolog_accept = true;
|
2019-03-27 21:45:21 +01:00
|
|
|
opts.prev_check_exists = false;
|
2019-03-27 21:44:33 +01:00
|
|
|
opts.warnlog |= m::vm::fault::STATE;
|
|
|
|
opts.warnlog &= ~m::vm::fault::EXISTS;
|
|
|
|
opts.errorlog &= ~m::vm::fault::STATE;
|
|
|
|
|
|
|
|
chain_fetch(auth, remote, [&opts]
|
|
|
|
(const json::object &event)
|
|
|
|
{
|
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
m::event{event}, opts
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 21:44:33 +01:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::auth::chain_fetch(const auth &auth,
|
2018-11-30 21:59:44 +01:00
|
|
|
const net::hostport &remote,
|
2019-03-27 21:44:33 +01:00
|
|
|
const fetch_closure &closure)
|
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
|
|
|
{
|
2018-11-30 21:59:44 +01:00
|
|
|
16_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-03-27 21:44:33 +01:00
|
|
|
auth.room.room_id, auth.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();
|
|
|
|
const json::array &auth_chain
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
|
2019-03-27 21:44:33 +01:00
|
|
|
std::vector<json::object> events(auth_chain.count());
|
|
|
|
std::copy(begin(auth_chain), end(auth_chain), begin(events));
|
|
|
|
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
|
|
|
|
2018-11-30 21:59:44 +01:00
|
|
|
for(const auto &event : events)
|
|
|
|
if(!closure(event))
|
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)
|
|
|
|
{
|
|
|
|
return r.finished == 0;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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)
|
|
|
|
request.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(!request.handle())
|
|
|
|
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-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]
|
|
|
|
{
|
|
|
|
request._buf = {};
|
|
|
|
request.buf = request._buf;
|
|
|
|
}};
|
|
|
|
|
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.prev_check_exists = false;
|
|
|
|
opts.infolog_accept = true;
|
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
m::event{event}, opts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
//
|
|
|
|
// fetch::request
|
|
|
|
//
|
|
|
|
|
2019-04-11 06:16:00 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// fetch::request::request
|
|
|
|
//
|
|
|
|
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::request::request(const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id,
|
|
|
|
const mutable_buffer &buf)
|
2018-09-05 08:27:01 +02:00
|
|
|
:room_id{room_id}
|
|
|
|
,event_id{event_id}
|
|
|
|
,_buf
|
|
|
|
{
|
|
|
|
!buf?
|
2019-04-11 06:16:00 +02:00
|
|
|
unique_buffer<mutable_buffer>{8_KiB}:
|
2018-09-05 08:27:01 +02:00
|
|
|
unique_buffer<mutable_buffer>{}
|
|
|
|
}
|
|
|
|
,buf
|
|
|
|
{
|
|
|
|
empty(buf)? _buf: buf
|
|
|
|
}
|
|
|
|
{
|
2019-04-11 06:16:00 +02:00
|
|
|
//assert(size(this->buf) >= 64_KiB + 8_KiB + 8_KiB);
|
|
|
|
assert(size(this->buf) >= 8_KiB);
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::request::start()
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
m::v1::event::opts opts;
|
2019-04-11 06:16:00 +02:00
|
|
|
opts.dynamic = true;
|
2018-09-05 08:27:01 +02:00
|
|
|
opts.remote = origin?: select_random_origin();
|
2019-04-11 06:16:00 +02:00
|
|
|
start(opts);
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-11 06:16:00 +02:00
|
|
|
ircd::m::fetch::request::start(m::v1::event::opts &opts)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
2019-04-11 08:29:56 +02:00
|
|
|
assert(finished == 0);
|
2018-09-05 08:27:01 +02:00
|
|
|
if(!started)
|
|
|
|
started = ircd::time();
|
|
|
|
|
|
|
|
last = ircd::time();
|
|
|
|
static_cast<m::v1::event &>(*this) =
|
|
|
|
{
|
|
|
|
this->event_id, this->buf, std::move(opts)
|
|
|
|
};
|
2019-04-11 06:16:00 +02:00
|
|
|
|
|
|
|
dock.notify_all();
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Started request for %s in %s from '%s'",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{origin},
|
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::request::select_random_origin()
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
const m::room::origins origins
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
// copies randomly selected origin into the attempted set.
|
|
|
|
const auto closure{[this]
|
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
this->select_origin(origin);
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Tests if origin is potentially viable
|
|
|
|
const auto proffer{[this]
|
|
|
|
(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.
|
|
|
|
if(attempted.count(origin))
|
|
|
|
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",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
return this->origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::request::select_origin(const string_view &origin)
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
const auto iit
|
|
|
|
{
|
|
|
|
attempted.emplace(std::string{origin})
|
|
|
|
};
|
|
|
|
|
|
|
|
this->origin = *iit.first;
|
|
|
|
return this->origin;
|
|
|
|
}
|
|
|
|
|
2018-10-23 18:13:48 +02:00
|
|
|
bool
|
|
|
|
ircd::m::fetch::request::handle()
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
auto &future
|
|
|
|
{
|
|
|
|
static_cast<m::v1::event &>(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
future.wait(); try
|
|
|
|
{
|
2019-04-11 06:16:00 +02:00
|
|
|
const auto code
|
|
|
|
{
|
|
|
|
future.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "%u %s for %s in %s from '%s'",
|
|
|
|
uint(code),
|
|
|
|
status(code),
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{origin}
|
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
eptr = std::current_exception();
|
2019-04-11 06:16:00 +02:00
|
|
|
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "Failure for %s in %s from '%s' :%s",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{origin},
|
|
|
|
what(eptr),
|
|
|
|
};
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!eptr)
|
|
|
|
finish();
|
|
|
|
else
|
|
|
|
retry();
|
2018-10-23 18:13:48 +02:00
|
|
|
|
|
|
|
return finished;
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::request::retry()
|
2018-09-05 08:27:01 +02:00
|
|
|
try
|
|
|
|
{
|
2019-04-11 06:16:00 +02:00
|
|
|
server::cancel(*this);
|
2018-09-05 08:27:01 +02:00
|
|
|
eptr = std::exception_ptr{};
|
|
|
|
origin = {};
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
eptr = std::current_exception();
|
|
|
|
finish();
|
2018-06-03 19:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-10-23 18:13:48 +02:00
|
|
|
ircd::m::fetch::request::finish()
|
2018-09-05 08:27:01 +02:00
|
|
|
{
|
|
|
|
finished = ircd::time();
|
2019-04-11 06:16:00 +02:00
|
|
|
dock.notify_all();
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|