2020-04-24 21:05:39 +02:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 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.
|
|
|
|
|
|
|
|
namespace ircd::m::acquire
|
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
struct result;
|
|
|
|
using list = std::list<result>;
|
|
|
|
|
|
|
|
static void start(const opts &, list &);
|
|
|
|
static bool handle(const opts &, ctx::future<m::fetch::result> &);
|
|
|
|
static bool handle(const opts &, list &);
|
|
|
|
static void fetch_head(const opts &, list &);
|
|
|
|
static bool fetch_missing(const opts &, list &);
|
|
|
|
static void submit(const opts &, list &);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ircd::m::acquire::result
|
|
|
|
{
|
|
|
|
ctx::future<m::fetch::result> future;
|
|
|
|
event::id::buf event_id;
|
|
|
|
|
|
|
|
result(ctx::future<m::fetch::result> &&future, const event::id &event_id)
|
|
|
|
:future{std::move(future)}
|
|
|
|
,event_id{event_id}
|
|
|
|
{}
|
2020-04-24 21:05:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::acquire::log)
|
|
|
|
ircd::m::acquire::log
|
|
|
|
{
|
|
|
|
"m.acquire"
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2020-11-07 14:12:48 +01:00
|
|
|
// execute::execute
|
2020-04-24 21:05:39 +02:00
|
|
|
//
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
ircd::m::acquire::execute::execute(const opts &opts)
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
list fetching;
|
|
|
|
|
|
|
|
// Branch to acquire head
|
2020-05-10 04:01:25 +02:00
|
|
|
if(opts.head)
|
2020-11-07 14:12:48 +01:00
|
|
|
fetch_head(opts, fetching);
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
// Branch to acquire missing
|
2020-05-10 04:01:25 +02:00
|
|
|
if(opts.missing)
|
2020-11-07 14:12:48 +01:00
|
|
|
for(size_t i(0); i < opts.rounds; ++i)
|
|
|
|
if(!fetch_missing(opts, fetching))
|
|
|
|
break;
|
2020-06-13 05:52:32 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
// Complete all work before returning, otherwise everything
|
|
|
|
// will be cancelled on unwind.
|
|
|
|
while(handle(opts, fetching));
|
2020-04-24 21:05:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
bool
|
|
|
|
ircd::m::acquire::fetch_missing(const opts &opts,
|
|
|
|
list &fetching)
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
const auto top
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
m::top(opts.room.room_id)
|
2020-04-24 21:05:39 +02:00
|
|
|
};
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
m::room::events::missing missing
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
opts.room
|
2020-04-24 21:05:39 +02:00
|
|
|
};
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
bool ret(false);
|
2020-11-08 14:56:02 +01:00
|
|
|
missing.for_each(opts.depth, [&opts, &fetching, &top, &ret]
|
2020-11-07 14:12:48 +01:00
|
|
|
(const event::id &event_id, const int64_t &ref_depth, const event::idx &ref_idx)
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
// Bail if interrupted
|
|
|
|
if(ctx::interruption_requested())
|
|
|
|
return false;
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
// Branch if we have to measure the viewportion
|
|
|
|
if(opts.viewport_size)
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
const m::event::idx_range range
|
|
|
|
{
|
|
|
|
std::min(ref_idx, std::get<event::idx>(top)),
|
|
|
|
std::max(ref_idx, std::get<event::idx>(top)),
|
|
|
|
};
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
// Bail if this event sits above the viewport.
|
|
|
|
if(m::room::events::count(opts.room, range) > opts.viewport_size)
|
2020-04-24 21:05:39 +02:00
|
|
|
return false;
|
2020-11-07 14:12:48 +01:00
|
|
|
}
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-13 14:28:03 +01:00
|
|
|
const auto ref_id
|
|
|
|
{
|
|
|
|
m::event_id(ref_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room ref_room
|
|
|
|
{
|
|
|
|
opts.room.room_id, ref_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &[sound_depth, sound_idx]
|
|
|
|
{
|
|
|
|
m::sounding(ref_room)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &[twain_depth, _twain_idx]
|
|
|
|
{
|
|
|
|
sound_idx == ref_idx?
|
|
|
|
m::twain(ref_room):
|
|
|
|
std::make_pair(0L, 0UL)
|
|
|
|
};
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
auto _opts(opts);
|
|
|
|
_opts.room.event_id = event_id;
|
|
|
|
_opts.hint = opts.hint;
|
2020-11-13 14:28:03 +01:00
|
|
|
_opts.viewport_size = twain_depth?
|
|
|
|
std::clamp(sound_depth - twain_depth, 1L, 48L):
|
|
|
|
1UL;
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
submit(_opts, fetching);
|
|
|
|
ret = true;
|
2020-11-14 14:25:56 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Fetch %s miss prev of %s @%lu in %s @%lu sound:%lu twain:%ld fetching:%zu",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{ref_id},
|
|
|
|
ref_depth,
|
|
|
|
string_view{ref_room.room_id},
|
|
|
|
std::get<int64_t>(top),
|
|
|
|
sound_depth,
|
|
|
|
twain_depth,
|
|
|
|
fetching.size(),
|
|
|
|
};
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
return true;
|
2020-04-24 21:05:39 +02:00
|
|
|
});
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
return ret;
|
2020-04-24 21:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-07 14:12:48 +01:00
|
|
|
ircd::m::acquire::fetch_head(const opts &opts,
|
|
|
|
list &fetching)
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
const auto handle_head{[&opts, &fetching]
|
|
|
|
(const m::event &result)
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
// Bail if interrupted
|
|
|
|
if(ctx::interruption_requested())
|
|
|
|
return false;
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
// Bail if the depth is below the window
|
|
|
|
if(json::get<"depth"_>(result) < opts.depth.first)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto _opts(opts);
|
|
|
|
_opts.room.event_id = result.event_id;
|
|
|
|
_opts.hint = json::get<"origin"_>(result);
|
|
|
|
_opts.hint_only = true;
|
2020-11-13 14:28:03 +01:00
|
|
|
_opts.viewport_size = 1; //XXX
|
2020-11-07 14:12:48 +01:00
|
|
|
submit(_opts, fetching);
|
|
|
|
return true;
|
|
|
|
}};
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
const auto top
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
m::top(opts.room.room_id)
|
2020-04-24 21:05:39 +02:00
|
|
|
};
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
m::room::head::fetch::opts hfopts;
|
|
|
|
hfopts.room_id = opts.room.room_id;
|
|
|
|
hfopts.top = top;
|
|
|
|
m::room::head::fetch
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
hfopts, handle_head
|
2020-04-24 21:05:39 +02:00
|
|
|
};
|
2020-11-07 14:12:48 +01:00
|
|
|
}
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
void
|
|
|
|
ircd::m::acquire::submit(const opts &opts,
|
|
|
|
list &fetching)
|
|
|
|
{
|
2020-11-14 14:25:56 +01:00
|
|
|
start(opts, fetching);
|
|
|
|
while(!fetching.empty())
|
2020-11-07 14:12:48 +01:00
|
|
|
if(!handle(opts, fetching))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::acquire::start(const opts &opts,
|
|
|
|
list &fetching)
|
|
|
|
{
|
|
|
|
fetch::opts fopts;
|
2020-11-13 14:28:03 +01:00
|
|
|
fopts.op = fetch::op::backfill;
|
2020-11-07 14:12:48 +01:00
|
|
|
fopts.room_id = opts.room.room_id;
|
|
|
|
fopts.event_id = opts.room.event_id;
|
2020-11-13 14:28:03 +01:00
|
|
|
fopts.backfill_limit = opts.viewport_size;
|
2020-11-07 14:12:48 +01:00
|
|
|
fopts.hint = opts.hint;
|
|
|
|
fopts.attempt_limit = opts.hint_only;
|
|
|
|
fetching.emplace_back(fetch::start(fopts), opts.room.event_id);
|
|
|
|
}
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
bool
|
|
|
|
ircd::m::acquire::handle(const opts &opts,
|
|
|
|
list &fetching)
|
|
|
|
{
|
2020-11-14 14:25:56 +01:00
|
|
|
const bool full
|
2020-11-07 14:12:48 +01:00
|
|
|
{
|
2020-11-14 14:25:56 +01:00
|
|
|
fetching.size() >= opts.fetch_width
|
|
|
|
};
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-14 14:25:56 +01:00
|
|
|
auto next
|
|
|
|
{
|
|
|
|
ctx::when_any(std::begin(fetching), std::end(fetching), []
|
|
|
|
(auto &it) -> ctx::future<m::fetch::result> &
|
2020-11-07 14:12:48 +01:00
|
|
|
{
|
2020-11-14 14:25:56 +01:00
|
|
|
return it->future;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const milliseconds timeout
|
|
|
|
{
|
|
|
|
full? 5000: 50
|
|
|
|
};
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-14 14:25:56 +01:00
|
|
|
if(!next.wait(timeout, std::nothrow))
|
|
|
|
return full;
|
2020-04-24 21:05:39 +02:00
|
|
|
|
2020-11-14 14:25:56 +01:00
|
|
|
const unique_iterator it
|
|
|
|
{
|
|
|
|
fetching, next.get()
|
|
|
|
};
|
2020-11-07 14:12:48 +01:00
|
|
|
|
2020-11-14 14:25:56 +01:00
|
|
|
assert(it.it != std::end(fetching));
|
2020-11-07 14:12:48 +01:00
|
|
|
|
2020-11-14 14:25:56 +01:00
|
|
|
auto _opts(opts);
|
|
|
|
_opts.room.event_id = it.it->event_id;
|
|
|
|
return handle(_opts, it.it->future);
|
2020-04-24 21:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-11-07 14:12:48 +01:00
|
|
|
ircd::m::acquire::handle(const opts &opts,
|
|
|
|
ctx::future<m::fetch::result> &future)
|
2020-04-24 21:05:39 +02:00
|
|
|
try
|
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
auto result
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
|
|
|
future.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object response
|
|
|
|
{
|
|
|
|
result
|
|
|
|
};
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
const json::array pdus
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
|
|
|
response["pdus"]
|
|
|
|
};
|
|
|
|
|
2020-11-14 14:25:56 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Eval %zu for %s in %s",
|
|
|
|
pdus.size(),
|
|
|
|
string_view{opts.room.event_id},
|
|
|
|
string_view{opts.room.room_id},
|
|
|
|
};
|
|
|
|
|
2020-04-24 21:05:39 +02:00
|
|
|
m::vm::opts vmopts;
|
|
|
|
vmopts.infolog_accept = true;
|
|
|
|
vmopts.warnlog &= ~vm::fault::EXISTS;
|
2020-11-14 14:25:56 +01:00
|
|
|
//vmopts.phase.set(m::vm::phase::NOTIFY, false);
|
2020-11-13 14:28:03 +01:00
|
|
|
vmopts.phase.set(m::vm::phase::FETCH_PREV, false);
|
|
|
|
vmopts.phase.set(m::vm::phase::FETCH_STATE, false);
|
|
|
|
vmopts.wopts.appendix.set(dbs::appendix::ROOM_HEAD, false);
|
2020-11-14 14:25:56 +01:00
|
|
|
ctx::interruption_point();
|
2020-11-07 14:12:48 +01:00
|
|
|
m::vm::eval
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-13 14:28:03 +01:00
|
|
|
pdus, vmopts
|
2020-04-24 21:05:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-07 14:12:48 +01:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2020-04-24 21:05:39 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
log::error
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
2020-11-14 14:25:56 +01:00
|
|
|
log, "Eval %s in %s :%s",
|
2020-11-07 14:12:48 +01:00
|
|
|
string_view{opts.room.event_id},
|
|
|
|
string_view{opts.room.room_id},
|
2020-04-24 21:05:39 +02:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
2020-11-14 14:25:56 +01:00
|
|
|
return true;
|
2020-04-24 21:05:39 +02:00
|
|
|
}
|