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
|
|
|
{
|
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;
|
2021-02-16 20:41:51 +01:00
|
|
|
extern conf::item<bool> reset_head;
|
|
|
|
extern conf::item<bool> reset_state;
|
|
|
|
extern conf::item<bool> reset_state_space;
|
2020-11-07 14:12:48 +01:00
|
|
|
extern conf::item<size_t> viewports;
|
2020-11-21 05:11:16 +01:00
|
|
|
extern conf::item<size_t> attempt_max;
|
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" },
|
2021-01-12 18:15:53 +01:00
|
|
|
{ "default", 32L },
|
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
|
|
|
};
|
|
|
|
|
2021-02-16 20:41:51 +01:00
|
|
|
decltype(ircd::m::init::backfill::reset_head)
|
|
|
|
ircd::m::init::backfill::reset_head
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.reset.head" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::init::backfill::reset_state)
|
|
|
|
ircd::m::init::backfill::reset_state
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.reset.state" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::init::backfill::reset_state_space)
|
|
|
|
ircd::m::init::backfill::reset_state_space
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.reset.state_space" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
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-11-21 05:11:16 +01:00
|
|
|
decltype(ircd::m::init::backfill::attempt_max)
|
|
|
|
ircd::m::init::backfill::attempt_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.init.backfill.attempt_max" },
|
|
|
|
{ "default", 8L },
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{
|
2022-07-04 22:15:53 +02:00
|
|
|
run::level::QUIT, []() noexcept
|
2020-06-13 04:00:08 +02:00
|
|
|
{
|
2022-07-03 04:18:20 +02:00
|
|
|
term();
|
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
|
2022-07-03 04:18:20 +02:00
|
|
|
ircd::m::init::backfill::term()
|
2019-10-04 23:34:46 +02:00
|
|
|
noexcept
|
2019-08-14 07:59:27 +02:00
|
|
|
{
|
2022-07-03 04:18:20 +02:00
|
|
|
assert(!worker_pool || worker_context);
|
|
|
|
|
|
|
|
if(worker_pool)
|
|
|
|
{
|
2020-11-19 21:34:47 +01:00
|
|
|
log::debug
|
|
|
|
{
|
2022-07-03 04:18:20 +02:00
|
|
|
log, "Terminating worker pool...",
|
2020-11-19 21:34:47 +01:00
|
|
|
};
|
2019-09-10 20:36:01 +02:00
|
|
|
|
2020-06-13 04:00:08 +02:00
|
|
|
worker_pool->terminate();
|
2022-07-03 04:18:20 +02:00
|
|
|
}
|
2020-06-13 04:00:08 +02:00
|
|
|
|
2020-11-19 21:34:47 +01:00
|
|
|
if(worker_context)
|
2022-07-03 04:18:20 +02:00
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Terminating worker context...",
|
|
|
|
};
|
|
|
|
|
2020-11-19 21:34:47 +01:00
|
|
|
ctx::terminate(*worker_context);
|
2022-07-03 04:18:20 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 10:34:26 +01:00
|
|
|
|
2022-07-03 04:18:20 +02:00
|
|
|
void
|
|
|
|
ircd::m::init::backfill::fini()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
term();
|
|
|
|
if(worker_context)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "Waiting for worker context...",
|
|
|
|
};
|
|
|
|
|
|
|
|
while(worker_context)
|
|
|
|
ctx::sleep(333ms);
|
|
|
|
|
|
|
|
assert(!worker_pool);
|
|
|
|
assert(!worker_context);
|
2019-08-14 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::init::backfill::worker()
|
|
|
|
try
|
|
|
|
{
|
2022-07-03 04:18:20 +02:00
|
|
|
const unwind nullify{[]
|
|
|
|
{
|
|
|
|
worker_context = nullptr;
|
|
|
|
}};
|
|
|
|
|
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
|
|
|
|
{
|
2023-02-20 03:42:11 +01:00
|
|
|
.stack_size = 512_KiB,
|
|
|
|
.initial_ctxs = size_t(pool_size),
|
|
|
|
.ionice = 3,
|
|
|
|
.nice = 3,
|
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.
|
2022-06-24 04:18:05 +02:00
|
|
|
dock.wait([]() noexcept
|
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
|
|
|
{
|
2022-07-04 05:49:12 +02:00
|
|
|
m::acquire
|
|
|
|
{{
|
|
|
|
.room = room_id,
|
|
|
|
.viewport_size = ssize_t(m::room::events::viewport_size) * size_t(viewports),
|
|
|
|
.attempt_max = size_t(attempt_max),
|
|
|
|
.vmopts = vm::opts
|
2020-12-05 06:38:56 +01:00
|
|
|
{
|
2022-07-04 05:49:12 +02:00
|
|
|
.warnlog = 0,
|
|
|
|
.infolog_accept = true,
|
|
|
|
},
|
|
|
|
}};
|
2020-11-07 14:12:48 +01:00
|
|
|
|
2021-02-16 20:41:51 +01:00
|
|
|
if(reset_head)
|
|
|
|
room::head::reset(room(room_id));
|
|
|
|
|
|
|
|
if(reset_state && reset_state_space)
|
|
|
|
room::state::space::rebuild
|
2020-12-05 06:38:56 +01:00
|
|
|
{
|
2021-02-16 20:41:51 +01:00
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(reset_state)
|
|
|
|
room::state::rebuild
|
|
|
|
{
|
|
|
|
room_id
|
2020-12-05 06:38:56 +01:00
|
|
|
};
|
2019-12-03 23:42:00 +01:00
|
|
|
|
2020-12-05 06:38:56 +01:00
|
|
|
if((false))
|
2019-12-04 01:08:37 +01:00
|
|
|
{
|
2020-12-05 06:38:56 +01:00
|
|
|
struct m::gossip::opts opts;
|
|
|
|
opts.room = room_id;
|
|
|
|
m::gossip
|
|
|
|
{
|
|
|
|
opts
|
|
|
|
};
|
|
|
|
}
|
2019-12-03 23:42:00 +01:00
|
|
|
}
|