2019-08-14 07:59:27 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 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-10-04 23:34:46 +02:00
|
|
|
namespace ircd::m::init::backfill
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2020-04-23 11:04:07 +02:00
|
|
|
void gossip(const room::id &, const event::id &, const string_view &remote);
|
2019-10-04 23:34:46 +02:00
|
|
|
void handle_room(const room::id &);
|
|
|
|
void worker();
|
|
|
|
|
2020-06-13 04:24:18 +02:00
|
|
|
extern size_t count, complete;
|
2020-06-13 04:00:08 +02:00
|
|
|
extern ircd::run::changed handle_quit;
|
2020-11-19 21:34:47 +01:00
|
|
|
extern ctx::ctx *worker_context;
|
2020-06-13 04:00:08 +02:00
|
|
|
extern ctx::pool *worker_pool;
|
2020-04-26 08:51:24 +02:00
|
|
|
extern conf::item<seconds> delay;
|
2020-04-24 21:05:39 +02:00
|
|
|
extern conf::item<seconds> gossip_timeout;
|
|
|
|
extern conf::item<bool> gossip_enable;
|
2020-03-02 02:23:46 +01:00
|
|
|
extern conf::item<bool> local_joined_only;
|
2020-11-07 14:12:48 +01:00
|
|
|
extern conf::item<size_t> viewports;
|
2020-04-24 21:05:39 +02:00
|
|
|
extern conf::item<size_t> pool_size;
|
|
|
|
extern conf::item<bool> enable;
|
2019-10-04 23:34:46 +02:00
|
|
|
extern log::log log;
|
2019-08-14 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::init::backfill::log)
|
|
|
|
ircd::m::init::backfill::log
|
|
|
|
{
|
|
|
|
"m.init.backfill"
|
|
|
|
};
|
|
|
|
|
2019-08-14 11:45:01 +02:00
|
|
|
decltype(ircd::m::init::backfill::enable)
|
|
|
|
ircd::m::init::backfill::enable
|
|
|
|
{
|
2020-04-21 09:08:59 +02:00
|
|
|
{ "name", "ircd.m.init.backfill.enable" },
|
|
|
|
{ "default", true },
|
2019-08-14 11:45:01 +02:00
|
|
|
};
|
|
|
|
|
2019-09-07 01:27:29 +02:00
|
|
|
decltype(ircd::m::init::backfill::pool_size)
|
|
|
|
ircd::m::init::backfill::pool_size
|
|
|
|
{
|
2020-04-21 09:08:59 +02:00
|
|
|
{ "name", "ircd.m.init.backfill.pool_size" },
|
|
|
|
{ "default", 12L },
|
2019-09-07 01:27:29 +02:00
|
|
|
};
|
|
|
|
|
2020-03-02 02:23:46 +01:00
|
|
|
decltype(ircd::m::init::backfill::local_joined_only)
|
|
|
|
ircd::m::init::backfill::local_joined_only
|
|
|
|
{
|
2020-04-21 09:08:59 +02:00
|
|
|
{ "name", "ircd.m.init.backfill.local_joined_only" },
|
|
|
|
{ "default", true },
|
2020-03-02 02:23:46 +01:00
|
|
|
};
|
|
|
|
|
2020-04-23 11:04:07 +02:00
|
|
|
decltype(ircd::m::init::backfill::gossip_enable)
|
|
|
|
ircd::m::init::backfill::gossip_enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.gossip.enable" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::init::backfill::gossip_timeout)
|
|
|
|
ircd::m::init::backfill::gossip_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.gossip.timeout" },
|
|
|
|
{ "default", 5L },
|
|
|
|
};
|
|
|
|
|
2020-04-26 08:51:24 +02:00
|
|
|
decltype(ircd::m::init::backfill::delay)
|
|
|
|
ircd::m::init::backfill::delay
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.delay" },
|
|
|
|
{ "default", 15L },
|
|
|
|
};
|
|
|
|
|
2020-11-07 14:12:48 +01:00
|
|
|
decltype(ircd::m::init::backfill::viewports)
|
|
|
|
ircd::m::init::backfill::viewports
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.viewports" },
|
|
|
|
{ "default", 4L },
|
|
|
|
};
|
|
|
|
|
2020-06-13 04:24:18 +02:00
|
|
|
decltype(ircd::m::init::backfill::count)
|
|
|
|
ircd::m::init::backfill::count;
|
|
|
|
|
|
|
|
decltype(ircd::m::init::backfill::complete)
|
|
|
|
ircd::m::init::backfill::complete;
|
|
|
|
|
2020-06-13 04:00:08 +02:00
|
|
|
decltype(ircd::m::init::backfill::worker_pool)
|
|
|
|
ircd::m::init::backfill::worker_pool;
|
|
|
|
|
2019-09-07 01:27:29 +02:00
|
|
|
decltype(ircd::m::init::backfill::worker_context)
|
|
|
|
ircd::m::init::backfill::worker_context;
|
|
|
|
|
2020-06-13 04:00:08 +02:00
|
|
|
decltype(ircd::m::init::backfill::handle_quit)
|
|
|
|
ircd::m::init::backfill::handle_quit
|
|
|
|
{
|
|
|
|
run::level::QUIT, []
|
|
|
|
{
|
2020-11-19 21:34:47 +01:00
|
|
|
fini();
|
2020-06-13 04:00:08 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-14 07:59:27 +02:00
|
|
|
void
|
|
|
|
ircd::m::init::backfill::init()
|
|
|
|
{
|
2019-08-14 11:45:01 +02:00
|
|
|
if(!enable)
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
log, "Initial synchronization of rooms from remote servers has"
|
|
|
|
" been disabled by the configuration. Not fetching latest events."
|
|
|
|
};
|
2019-08-25 03:49:27 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:34:47 +01:00
|
|
|
ctx::context context
|
2019-08-14 07:59:27 +02:00
|
|
|
{
|
|
|
|
"m.init.backfill",
|
|
|
|
512_KiB,
|
|
|
|
&worker,
|
|
|
|
context::POST
|
2020-11-19 21:34:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Detach into the worker_context ptr. When the worker finishes it
|
|
|
|
// will free itself.
|
|
|
|
assert(!worker_context);
|
|
|
|
worker_context = context.detach();
|
2019-08-14 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::init::backfill::fini()
|
2019-10-04 23:34:46 +02:00
|
|
|
noexcept
|
2019-08-14 07:59:27 +02:00
|
|
|
{
|
2020-11-19 21:34:47 +01:00
|
|
|
if(worker_context)
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Terminating worker context..."
|
|
|
|
};
|
2019-09-10 20:36:01 +02:00
|
|
|
|
2020-06-13 04:00:08 +02:00
|
|
|
if(worker_pool)
|
|
|
|
worker_pool->terminate();
|
|
|
|
|
2020-11-19 21:34:47 +01:00
|
|
|
if(worker_context)
|
|
|
|
{
|
|
|
|
ctx::terminate(*worker_context);
|
|
|
|
worker_context = nullptr;
|
|
|
|
}
|
2019-08-14 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::init::backfill::worker()
|
|
|
|
try
|
|
|
|
{
|
2020-03-16 21:11:00 +01:00
|
|
|
// Wait for runlevel RUN before proceeding...
|
|
|
|
run::barrier<ctx::interrupted>{};
|
2019-08-14 07:59:27 +02:00
|
|
|
|
2020-01-07 00:05:48 +01:00
|
|
|
// Set a low priority for this context; see related pool_opts
|
|
|
|
ionice(ctx::cur(), 4);
|
|
|
|
nice(ctx::cur(), 4);
|
|
|
|
|
2019-09-07 23:53:44 +02:00
|
|
|
// Prepare to iterate all of the rooms this server is aware of which
|
2019-09-10 21:25:48 +02:00
|
|
|
// contain at least one member from another server in any state, and
|
|
|
|
// one member from our server in a joined state.
|
2019-09-07 23:53:44 +02:00
|
|
|
rooms::opts opts;
|
2019-09-08 01:34:46 +02:00
|
|
|
opts.remote_only = true;
|
2020-03-02 02:23:46 +01:00
|
|
|
opts.local_joined_only = local_joined_only;
|
2019-09-07 23:53:44 +02:00
|
|
|
|
|
|
|
// This is only an estimate because the rooms on the server can change
|
|
|
|
// before this task completes.
|
|
|
|
const auto estimate
|
2019-09-07 01:27:29 +02:00
|
|
|
{
|
2019-09-08 07:11:18 +02:00
|
|
|
1UL //rooms::count(opts)
|
2019-09-07 01:27:29 +02:00
|
|
|
};
|
|
|
|
|
2019-09-07 23:53:44 +02:00
|
|
|
if(!estimate)
|
|
|
|
return;
|
|
|
|
|
2020-04-26 08:51:24 +02:00
|
|
|
// Wait a delay before starting.
|
|
|
|
ctx::sleep(seconds(delay));
|
|
|
|
|
2019-09-07 23:53:44 +02:00
|
|
|
log::notice
|
2019-08-14 07:59:27 +02:00
|
|
|
{
|
2019-09-08 07:11:18 +02:00
|
|
|
log, "Starting initial backfill of rooms from other servers...",
|
2019-09-07 23:53:44 +02:00
|
|
|
estimate,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Prepare a pool of child contexts to process rooms concurrently.
|
|
|
|
// The context pool lives directly in this frame.
|
|
|
|
static const ctx::pool::opts pool_opts
|
|
|
|
{
|
|
|
|
512_KiB, // stack sz
|
|
|
|
size_t(pool_size), // pool sz
|
2020-01-07 00:05:48 +01:00
|
|
|
-1, // queue max hard
|
|
|
|
0, // queue max soft
|
|
|
|
true, // queue max blocking
|
|
|
|
true, // queue max warning
|
|
|
|
3, // ionice
|
|
|
|
3, // nice
|
2019-08-14 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
2019-09-07 01:27:29 +02:00
|
|
|
ctx::pool pool
|
|
|
|
{
|
2020-06-13 04:00:08 +02:00
|
|
|
"m.init.backfill", pool_opts
|
|
|
|
};
|
|
|
|
|
|
|
|
const scope_restore backfill_worker_pool
|
|
|
|
{
|
|
|
|
backfill::worker_pool, std::addressof(pool)
|
2019-09-07 01:27:29 +02:00
|
|
|
};
|
2019-08-14 10:41:17 +02:00
|
|
|
|
2019-09-07 23:53:44 +02:00
|
|
|
// Iterate the room_id's, submitting a copy of each to the next pool
|
|
|
|
// worker; the submission blocks when all pool workers are busy, as per
|
|
|
|
// the pool::opts.
|
2020-04-24 21:05:39 +02:00
|
|
|
ctx::dock dock;
|
2019-09-10 20:36:01 +02:00
|
|
|
const ctx::uninterruptible ui;
|
2020-06-13 04:24:18 +02:00
|
|
|
rooms::for_each(opts, [&pool, &estimate, &dock]
|
2019-09-07 01:27:29 +02:00
|
|
|
(const room::id &room_id)
|
|
|
|
{
|
2019-09-10 20:36:01 +02:00
|
|
|
if(unlikely(ctx::interruption_requested()))
|
|
|
|
return false;
|
|
|
|
|
2019-08-14 10:41:17 +02:00
|
|
|
++count;
|
2020-04-24 21:05:39 +02:00
|
|
|
pool([&, room_id(std::string(room_id))] // asynchronous
|
2019-09-07 01:27:29 +02:00
|
|
|
{
|
2020-06-13 04:24:18 +02:00
|
|
|
const unwind completed{[&dock]
|
2020-04-24 21:05:39 +02:00
|
|
|
{
|
|
|
|
++complete;
|
|
|
|
dock.notify_one();
|
|
|
|
}};
|
|
|
|
|
|
|
|
handle_room(room_id);
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "Initial backfill of %s complete:%zu", //estimate:%zu %02.2lf%%",
|
|
|
|
string_view{room_id},
|
|
|
|
complete,
|
|
|
|
estimate,
|
|
|
|
(complete / double(estimate)) * 100.0,
|
|
|
|
};
|
2019-09-07 01:27:29 +02:00
|
|
|
});
|
|
|
|
|
2019-09-10 20:36:01 +02:00
|
|
|
return true;
|
2019-08-14 07:59:27 +02:00
|
|
|
});
|
2019-08-14 10:41:17 +02:00
|
|
|
|
2019-09-07 01:27:29 +02:00
|
|
|
if(complete < count)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "Waiting for initial resynchronization count:%zu complete:%zu rooms...",
|
|
|
|
count,
|
|
|
|
complete,
|
|
|
|
};
|
|
|
|
|
2019-09-07 23:53:44 +02:00
|
|
|
// All rooms have been submitted to the pool but the pool workers might
|
|
|
|
// still be busy. If we unwind now the pool's dtor will kill the workers
|
|
|
|
// so we synchronize their completion here.
|
2020-06-13 04:24:18 +02:00
|
|
|
dock.wait([]
|
2019-09-07 01:27:29 +02:00
|
|
|
{
|
|
|
|
return complete >= count;
|
|
|
|
});
|
|
|
|
|
2020-06-13 04:24:18 +02:00
|
|
|
if(count)
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
log, "Initial resynchronization of %zu rooms completed.",
|
|
|
|
count,
|
|
|
|
};
|
2019-08-14 07:59:27 +02:00
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
2020-06-13 04:24:18 +02:00
|
|
|
if(count)
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "Worker interrupted without completing resynchronization of all rooms."
|
|
|
|
};
|
2019-08-14 10:41:17 +02:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const ctx::terminated &e)
|
|
|
|
{
|
2020-06-13 04:24:18 +02:00
|
|
|
if(count)
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Worker terminated without completing resynchronization of all rooms."
|
|
|
|
};
|
2019-08-14 07:59:27 +02:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "Worker fatal :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::init::backfill::handle_room(const room::id &room_id)
|
2019-09-07 00:46:22 +02:00
|
|
|
{
|
2020-11-19 21:29:53 +01:00
|
|
|
struct m::acquire::opts opts;
|
2020-11-07 14:12:48 +01:00
|
|
|
opts.room = room_id;
|
|
|
|
opts.viewport_size = ssize_t(m::room::events::viewport_size);
|
|
|
|
opts.viewport_size *= size_t(viewports);
|
2020-11-19 21:29:53 +01:00
|
|
|
m::acquire
|
2019-09-07 00:46:22 +02:00
|
|
|
{
|
2020-11-07 14:12:48 +01:00
|
|
|
opts
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t num_reset
|
|
|
|
{
|
|
|
|
m::room::head::reset(opts.room)
|
2019-08-28 03:27:11 +02:00
|
|
|
};
|
|
|
|
}
|
2019-12-03 23:42:00 +01:00
|
|
|
|
2020-04-23 11:04:07 +02:00
|
|
|
void
|
2019-12-03 23:42:00 +01:00
|
|
|
ircd::m::init::backfill::gossip(const room::id &room_id,
|
|
|
|
const event::id &event_id,
|
|
|
|
const string_view &remote)
|
|
|
|
{
|
2020-04-23 11:04:07 +02:00
|
|
|
m::gossip::opts opts;
|
|
|
|
opts.timeout = seconds(gossip_timeout);
|
2019-12-04 01:08:37 +01:00
|
|
|
opts.remote = remote;
|
2020-04-23 11:04:07 +02:00
|
|
|
opts.event_id = event_id;
|
|
|
|
gossip::gossip
|
2019-12-04 01:08:37 +01:00
|
|
|
{
|
2020-04-23 11:04:07 +02:00
|
|
|
room_id, opts
|
2019-12-04 01:08:37 +01:00
|
|
|
};
|
2019-12-03 23:42:00 +01:00
|
|
|
}
|