2020-08-24 13:06:16 +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
|
|
|
|
{
|
|
|
|
static void bootstrap_event_vector(homeserver &);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::homeserver::bootstrap()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(opts);
|
2020-09-15 04:02:37 +02:00
|
|
|
assert(dbs::events);
|
2020-08-24 13:06:16 +02:00
|
|
|
if(opts->bootstrap_vector_path)
|
2020-08-28 11:59:33 +02:00
|
|
|
return bootstrap_event_vector(*this);
|
2020-08-24 13:06:16 +02:00
|
|
|
|
2020-09-15 04:02:37 +02:00
|
|
|
assert(db::sequence(*dbs::events) == 0);
|
2020-08-24 13:06:16 +02:00
|
|
|
assert(this->self);
|
|
|
|
const m::user::id &my_id
|
|
|
|
{
|
|
|
|
this->self
|
|
|
|
};
|
|
|
|
|
2020-08-28 11:59:33 +02:00
|
|
|
const m::user me
|
2020-08-24 13:06:16 +02:00
|
|
|
{
|
|
|
|
my_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::node my_node
|
|
|
|
{
|
|
|
|
origin(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::node::room node_room
|
|
|
|
{
|
|
|
|
my_node
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf my_room_id
|
|
|
|
{
|
|
|
|
"ircd", origin(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room my_room
|
|
|
|
{
|
|
|
|
my_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf conf_room_id
|
|
|
|
{
|
|
|
|
"conf", origin(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room conf_room
|
|
|
|
{
|
|
|
|
conf_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf tokens_room_id
|
|
|
|
{
|
|
|
|
"tokens", origin(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room tokens_room
|
|
|
|
{
|
|
|
|
tokens_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf public_room_id
|
|
|
|
{
|
|
|
|
"public", origin(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room public_room
|
|
|
|
{
|
|
|
|
public_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf alias_room_id
|
|
|
|
{
|
|
|
|
"alias", origin(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room alias_room
|
|
|
|
{
|
|
|
|
alias_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id::buf control_room_id
|
|
|
|
{
|
|
|
|
"control", origin(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room control_room
|
|
|
|
{
|
|
|
|
control_room_id
|
|
|
|
};
|
|
|
|
|
2020-08-28 11:59:33 +02:00
|
|
|
if(my_id.hostname() == "localhost")
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"The server's name is configured to localhost. This is probably not what you want."
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(key);
|
|
|
|
assert(!key->verify_keys.empty());
|
|
|
|
m::keys::cache::set(key->verify_keys);
|
|
|
|
|
|
|
|
create(me);
|
2020-11-03 20:30:15 +01:00
|
|
|
mutable_cast(me).activate();
|
2020-08-28 11:59:33 +02:00
|
|
|
|
|
|
|
create(my_room, me);
|
|
|
|
create(conf_room, me);
|
|
|
|
create(tokens_room, me);
|
|
|
|
create(public_room, me);
|
|
|
|
create(alias_room, me);
|
|
|
|
create(control_room, me);
|
|
|
|
|
2022-06-18 04:58:35 +02:00
|
|
|
send(my_room, me, "m.room.name", "", json::members
|
2020-08-28 11:59:33 +02:00
|
|
|
{
|
|
|
|
{ "name", "IRCd's Room" }
|
|
|
|
});
|
|
|
|
|
2022-06-18 04:58:35 +02:00
|
|
|
send(my_room, me, "m.room.topic", "", json::members
|
2020-08-28 11:59:33 +02:00
|
|
|
{
|
|
|
|
{ "topic", "The daemon's den." }
|
|
|
|
});
|
|
|
|
|
2022-06-18 04:58:35 +02:00
|
|
|
send(conf_room, me, "m.room.name", "", json::members
|
2020-08-28 11:59:33 +02:00
|
|
|
{
|
|
|
|
{ "name", "Server Configuration" }
|
|
|
|
});
|
|
|
|
|
2022-06-18 04:58:35 +02:00
|
|
|
send(tokens_room, me, "m.room.name", "", json::members
|
2020-08-28 11:59:33 +02:00
|
|
|
{
|
|
|
|
{ "name", "User Tokens" }
|
|
|
|
});
|
|
|
|
|
2022-06-18 04:58:35 +02:00
|
|
|
send(control_room, me, "m.room.name", "", json::members
|
2020-08-28 11:59:33 +02:00
|
|
|
{
|
|
|
|
{ "name", "Control Room" }
|
|
|
|
});
|
2020-08-24 13:06:16 +02:00
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "Bootstrap event generation completed nominally."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, log::level::CRITICAL,
|
|
|
|
"Failed to bootstrap server '%s' on network '%s' :%s",
|
|
|
|
server_name(*this),
|
|
|
|
origin(*this),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw ircd::panic
|
|
|
|
{
|
|
|
|
"bootstrap %s error :%s",
|
|
|
|
server_name(*this),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::bootstrap_event_vector(homeserver &homeserver)
|
|
|
|
try
|
|
|
|
{
|
2020-09-15 04:02:37 +02:00
|
|
|
assert(homeserver.opts);
|
2020-09-27 23:54:54 +02:00
|
|
|
const auto &hs_opts
|
|
|
|
{
|
|
|
|
*homeserver.opts
|
|
|
|
};
|
|
|
|
|
2020-08-24 13:06:16 +02:00
|
|
|
const string_view &path
|
|
|
|
{
|
2020-09-27 23:54:54 +02:00
|
|
|
hs_opts.bootstrap_vector_path
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool validate_json_only
|
|
|
|
{
|
|
|
|
has(string_view(ircd::diagnostic), "valid-json")
|
2020-08-24 13:06:16 +02:00
|
|
|
};
|
|
|
|
|
2022-06-21 02:29:13 +02:00
|
|
|
const fs::fd::opts file_opts
|
|
|
|
{
|
|
|
|
.mode = std::ios::in,
|
|
|
|
.sequential = true,
|
|
|
|
};
|
|
|
|
|
2020-08-24 13:06:16 +02:00
|
|
|
const fs::fd file
|
|
|
|
{
|
2022-06-21 02:29:13 +02:00
|
|
|
path, file_opts
|
2020-08-24 13:06:16 +02:00
|
|
|
};
|
|
|
|
|
2022-06-21 02:29:13 +02:00
|
|
|
fs::map::opts map_opts{file_opts};
|
|
|
|
map_opts.huge2mb = true;
|
2020-08-28 11:59:33 +02:00
|
|
|
const fs::map map
|
|
|
|
{
|
2022-06-21 02:29:13 +02:00
|
|
|
file, map_opts
|
2020-08-28 11:59:33 +02:00
|
|
|
};
|
|
|
|
|
2020-09-07 22:15:18 +02:00
|
|
|
// This array is backed by the mmap
|
|
|
|
const json::array events
|
|
|
|
{
|
|
|
|
const_buffer{map}
|
|
|
|
};
|
|
|
|
|
2020-09-12 12:44:39 +02:00
|
|
|
char pbuf[4][48];
|
2020-08-24 13:06:16 +02:00
|
|
|
log::notice
|
|
|
|
{
|
2020-09-07 22:15:18 +02:00
|
|
|
log, "Bootstrapping database from events @ `%s' %s",
|
2020-08-24 13:06:16 +02:00
|
|
|
path,
|
2020-08-28 11:59:33 +02:00
|
|
|
pretty(pbuf[0], iec(size(map))),
|
2020-08-24 13:06:16 +02:00
|
|
|
};
|
|
|
|
|
2020-09-12 12:44:39 +02:00
|
|
|
auto ¤t(ctx::cur());
|
|
|
|
const run::changed handle_quit
|
|
|
|
{
|
|
|
|
run::level::QUIT, [¤t]
|
|
|
|
{
|
|
|
|
ctx::interrupt(current);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-01 01:32:06 +02:00
|
|
|
// Options for eval. This eval disables all phases except a select few.
|
|
|
|
// These may change based on assumptions about the input.
|
2020-08-28 11:59:33 +02:00
|
|
|
vm::opts vmopts;
|
|
|
|
vmopts.phase.reset();
|
2020-09-07 22:15:18 +02:00
|
|
|
|
|
|
|
// Primary interest is to perform the INDEX and WRITE phase which create
|
|
|
|
// a database transaction and commit it respectively.
|
2020-12-05 13:39:49 +01:00
|
|
|
vmopts.phase.set(vm::phase::INDEX, true);
|
|
|
|
vmopts.phase.set(vm::phase::WRITE, true);
|
2020-08-24 13:06:16 +02:00
|
|
|
|
2021-02-15 17:57:21 +01:00
|
|
|
// Perform prefetches over the whole batch;
|
|
|
|
vmopts.mprefetch_refs = true;
|
|
|
|
|
|
|
|
//XXX Consider enable for large batch size.
|
|
|
|
//vmopts.phase.set(vm::phase::PREINDEX, true);
|
|
|
|
|
2022-06-16 05:44:24 +02:00
|
|
|
// Optimize the bootstrap by disabling WAL journaling.
|
2022-07-02 20:37:49 +02:00
|
|
|
vmopts.wopts.sopts.journal = false;
|
2022-06-16 05:44:24 +02:00
|
|
|
|
2020-09-16 11:20:40 +02:00
|
|
|
// Optimize the bootstrap by not updating room heads at every step.
|
2020-12-05 13:39:49 +01:00
|
|
|
vmopts.wopts.appendix.set(dbs::appendix::ROOM_HEAD, false);
|
|
|
|
vmopts.wopts.appendix.set(dbs::appendix::ROOM_HEAD_RESOLVE, false);
|
2020-09-16 11:20:40 +02:00
|
|
|
|
2020-09-07 22:15:18 +02:00
|
|
|
// Perform normal static-conformity checks; there's no reason to accept
|
|
|
|
// inputs that wouldn't normally be accepted. While inputs are supposed
|
|
|
|
// to be trusted and authentic, their correctness should still be checked;
|
|
|
|
// attempting to recover from a catastrophic failure might be the reason
|
|
|
|
// for the rebuild.
|
2020-12-05 13:39:49 +01:00
|
|
|
vmopts.phase.set(vm::phase::CONFORM, true);
|
2020-09-07 22:15:18 +02:00
|
|
|
|
|
|
|
//TODO: XXX
|
|
|
|
// This workaround is required for internal rooms to work, for now.
|
2020-09-01 01:32:06 +02:00
|
|
|
vmopts.non_conform.set(event::conforms::MISMATCH_ORIGIN_SENDER);
|
|
|
|
|
|
|
|
// Indicates the input JSON is canonical (to optimize eval).
|
|
|
|
//vmopts.json_source = true;
|
2020-09-12 12:44:39 +02:00
|
|
|
vmopts.non_conform.set(event::conforms::MISMATCH_HASHES);
|
2020-09-01 01:32:06 +02:00
|
|
|
|
2020-09-13 17:00:58 +02:00
|
|
|
// Optimize eval if we assume there's only one copy of each event in the
|
|
|
|
// input array.
|
2020-09-13 09:15:44 +02:00
|
|
|
vmopts.unique = false;
|
|
|
|
|
|
|
|
// Optimize eval if we guarantee there's only one copy of each event
|
2020-09-13 17:00:58 +02:00
|
|
|
// in the input. This assumption is made when bootstrapping fresh DB.
|
|
|
|
vmopts.replays = sequence(*dbs::events) == 0;
|
|
|
|
|
|
|
|
// Error control
|
|
|
|
//vmopts.nothrows = -1UL;
|
2020-09-13 09:15:44 +02:00
|
|
|
|
2020-09-01 01:32:06 +02:00
|
|
|
// Outputs to infolog for each event; may be noisy;
|
|
|
|
vmopts.infolog_accept = false;
|
|
|
|
|
2021-02-15 17:57:21 +01:00
|
|
|
static const size_t batch_max {64};
|
2020-09-20 15:45:14 +02:00
|
|
|
std::vector<m::event> vec(batch_max);
|
2022-06-14 05:00:51 +02:00
|
|
|
size_t count {0}, ebytes[3] {0, 1, 0}, accept {0};
|
2020-09-14 00:29:28 +02:00
|
|
|
vm::eval eval
|
2020-09-12 12:44:39 +02:00
|
|
|
{
|
|
|
|
vmopts
|
|
|
|
};
|
|
|
|
|
2020-08-28 11:59:33 +02:00
|
|
|
util::timer stopwatch;
|
2020-09-14 01:46:25 +02:00
|
|
|
auto it(begin(events));
|
2020-10-31 10:39:16 +01:00
|
|
|
while(it != end(events)) try
|
2020-08-28 11:59:33 +02:00
|
|
|
{
|
2020-09-20 15:45:14 +02:00
|
|
|
// page in the JSON
|
2020-09-14 01:46:25 +02:00
|
|
|
size_t i(0);
|
|
|
|
for(; i < vec.size() && it != end(events); ++i, ++it)
|
2020-09-12 12:44:39 +02:00
|
|
|
{
|
2020-09-27 23:54:54 +02:00
|
|
|
// Account for the bytes of the value plus one `,` separator
|
2020-09-14 01:46:25 +02:00
|
|
|
const string_view &elem(*it);
|
|
|
|
ebytes[1] += elem.size() + 1;
|
2020-09-27 23:54:54 +02:00
|
|
|
|
|
|
|
// In validate mode there's no need to load up the event tuple
|
|
|
|
if(validate_json_only)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
vec[i] = json::object{elem};
|
2020-09-14 01:46:25 +02:00
|
|
|
}
|
2020-09-12 12:44:39 +02:00
|
|
|
|
2020-09-27 23:54:54 +02:00
|
|
|
const vector_view<const m::event> batch
|
|
|
|
{
|
|
|
|
vec.data(), vec.data() + i
|
|
|
|
};
|
|
|
|
|
|
|
|
// process the event batch; make the batch size 0 for validate
|
2020-12-01 05:47:57 +01:00
|
|
|
if(likely(!validate_json_only))
|
|
|
|
execute(eval, batch);
|
2020-09-12 12:44:39 +02:00
|
|
|
|
2020-09-14 01:46:25 +02:00
|
|
|
count += i;
|
2020-09-12 12:44:39 +02:00
|
|
|
|
2020-09-13 17:00:58 +02:00
|
|
|
const size_t incore
|
2020-09-12 12:44:39 +02:00
|
|
|
{
|
|
|
|
ebytes[1] > ebytes[0]?
|
|
|
|
ebytes[1] - ebytes[0]:
|
|
|
|
0UL
|
|
|
|
};
|
|
|
|
|
2020-09-20 15:45:14 +02:00
|
|
|
// advise dontneed
|
2022-05-03 05:25:57 +02:00
|
|
|
if(likely(!validate_json_only))
|
2022-06-18 21:05:17 +02:00
|
|
|
{
|
|
|
|
const fs::opts opts
|
|
|
|
{
|
|
|
|
.offset = off_t(ebytes[0]),
|
|
|
|
};
|
|
|
|
|
|
|
|
ebytes[0] += evict(map, incore, opts);
|
|
|
|
}
|
2022-05-03 05:25:57 +02:00
|
|
|
|
2021-03-11 22:39:29 +01:00
|
|
|
if(count % (batch_max * 256) != 0)
|
2021-02-15 17:57:21 +01:00
|
|
|
continue;
|
2020-09-27 23:54:54 +02:00
|
|
|
|
2020-09-14 01:46:25 +02:00
|
|
|
const auto db_bytes
|
2020-09-12 12:44:39 +02:00
|
|
|
{
|
2020-09-14 01:46:25 +02:00
|
|
|
db::ticker(*dbs::events, "rocksdb.bytes.written")
|
|
|
|
};
|
2020-09-13 17:00:58 +02:00
|
|
|
|
2020-09-20 15:45:14 +02:00
|
|
|
const auto elapsed
|
|
|
|
{
|
2022-05-03 05:25:57 +02:00
|
|
|
stopwatch.at<milliseconds>().count()
|
2020-09-20 15:45:14 +02:00
|
|
|
};
|
|
|
|
|
2020-09-14 01:46:25 +02:00
|
|
|
log::info
|
|
|
|
{
|
2022-06-14 05:00:51 +02:00
|
|
|
log, "Bootstrap %3.2lf%% %s in %s | seq:%zu accept:%zu fault:%zu | %zu event/s; input %s/s; output %s/s",
|
|
|
|
(ebytes[1] / double(size(string_view(events)))) * 100.0,
|
|
|
|
pretty(pbuf[0], iec(ebytes[1])),
|
|
|
|
stopwatch.pretty(pbuf[1]),
|
2020-09-14 01:46:25 +02:00
|
|
|
vm::sequence::retired,
|
2020-12-01 05:47:57 +01:00
|
|
|
eval.accepted,
|
|
|
|
eval.faulted,
|
2022-06-14 05:00:51 +02:00
|
|
|
size_t(count / double(std::max(elapsed / 1000L, 1000L))),
|
2022-05-03 05:25:57 +02:00
|
|
|
pretty(pbuf[2], iec((ebytes[1] / std::max(elapsed, 1L)) * 1000), 1),
|
|
|
|
pretty(pbuf[3], iec((db_bytes / std::max(elapsed, 1L)) * 1000), 1),
|
2020-09-14 01:46:25 +02:00
|
|
|
};
|
2020-09-12 12:44:39 +02:00
|
|
|
|
2022-05-03 05:25:57 +02:00
|
|
|
if(likely(!validate_json_only))
|
|
|
|
ctx::yield();
|
|
|
|
|
2020-09-14 01:46:25 +02:00
|
|
|
ctx::interruption_point();
|
2020-09-12 12:44:39 +02:00
|
|
|
}
|
2020-10-31 10:39:16 +01:00
|
|
|
catch(const json::parse_error &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "Bootstrap retired:%zu count:%zu accept:%zu offset:%zu :%s",
|
|
|
|
vm::sequence::retired,
|
|
|
|
count,
|
|
|
|
accept,
|
|
|
|
std::distance(begin(events).start, it.start),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2020-08-24 13:06:16 +02:00
|
|
|
|
2020-09-16 09:23:04 +02:00
|
|
|
// Manual flush of the memtables is required in case the user disabled the
|
2022-06-16 05:44:24 +02:00
|
|
|
// WAL. If this isn't run several thousand keys in memory will be dropped
|
|
|
|
// inconsistently between database columns. If WAL is enabled then it
|
|
|
|
// tidies the DB up just as well.
|
2020-09-27 23:54:54 +02:00
|
|
|
if(likely(sequence(*dbs::events) > 0))
|
2020-09-16 09:23:04 +02:00
|
|
|
{
|
|
|
|
const bool blocking(true), allow_stall(true);
|
|
|
|
db::sort(*dbs::events, blocking, allow_stall);
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:59:33 +02:00
|
|
|
log::notice
|
2020-08-24 13:06:16 +02:00
|
|
|
{
|
2020-09-12 12:44:39 +02:00
|
|
|
log, "Bootstrapped count:%zu retired:%zu in %s from `%s' in %s",
|
|
|
|
count,
|
2020-08-28 11:59:33 +02:00
|
|
|
vm::sequence::retired,
|
|
|
|
pretty(pbuf[0], iec(size(string_view(events)))),
|
2020-08-24 13:06:16 +02:00
|
|
|
path,
|
2020-08-28 11:59:33 +02:00
|
|
|
stopwatch.pretty(pbuf[1]),
|
2020-08-24 13:06:16 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-09-12 12:44:39 +02:00
|
|
|
throw ircd::error
|
2020-08-24 13:06:16 +02:00
|
|
|
{
|
2022-06-14 06:02:39 +02:00
|
|
|
"bootstrap %s :terminated without completion :%s",
|
|
|
|
server_name(homeserver),
|
|
|
|
e.what(),
|
2020-08-24 13:06:16 +02:00
|
|
|
};
|
2020-09-12 12:44:39 +02:00
|
|
|
}
|
|
|
|
catch(const ctx::terminated &)
|
|
|
|
{
|
2022-06-14 06:02:39 +02:00
|
|
|
log::critical
|
2020-08-24 13:06:16 +02:00
|
|
|
{
|
2022-06-14 06:02:39 +02:00
|
|
|
log, "bootstrap %s :terminated without completion.",
|
2020-08-24 13:06:16 +02:00
|
|
|
server_name(homeserver),
|
|
|
|
};
|
2022-06-14 06:02:39 +02:00
|
|
|
|
|
|
|
throw;
|
2020-08-24 13:06:16 +02:00
|
|
|
}
|