From ba06958d692a521e3d4bfe8f3a7372202884e3a4 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 23 Apr 2020 03:50:03 -0700 Subject: [PATCH] ircd::m: Start a netburst interface; move cache_warm_origin; invoke from resource. --- include/ircd/m/burst.h | 34 +++++++++++++ include/ircd/m/m.h | 1 + matrix/Makefile.am | 1 + matrix/burst.cc | 108 +++++++++++++++++++++++++++++++++++++++++ matrix/resource.cc | 54 +++------------------ 5 files changed, 152 insertions(+), 46 deletions(-) create mode 100644 include/ircd/m/burst.h create mode 100644 matrix/burst.cc diff --git a/include/ircd/m/burst.h b/include/ircd/m/burst.h new file mode 100644 index 000000000..191d7e62b --- /dev/null +++ b/include/ircd/m/burst.h @@ -0,0 +1,34 @@ +// The Construct +// +// Copyright (C) The Construct Developers, Authors & Contributors +// Copyright (C) 2016-2020 Jason Volk +// +// 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. + +#pragma once +#define HAVE_IRCD_M_BURST_H + +namespace ircd::m::burst +{ + struct opts; + struct burst; + + extern log::log log; +}; + +struct ircd::m::burst::burst +{ + burst(const m::node &, const opts &); +}; + +struct ircd::m::burst::opts +{ + /// Enable gossip on netburst + bool gossip {true}; + + /// Enable cache warming feature on netburst + bool cache_warming {true}; +}; diff --git a/include/ircd/m/m.h b/include/ircd/m/m.h index 41190bb75..cd60f00ed 100644 --- a/include/ircd/m/m.h +++ b/include/ircd/m/m.h @@ -95,6 +95,7 @@ namespace ircd #include "media.h" #include "search.h" #include "gossip.h" +#include "burst.h" #include "resource.h" #include "homeserver.h" diff --git a/matrix/Makefile.am b/matrix/Makefile.am index 69c3ab3a9..126ade7ae 100644 --- a/matrix/Makefile.am +++ b/matrix/Makefile.am @@ -127,6 +127,7 @@ libircd_matrix_la_SOURCES += user_rooms.cc libircd_matrix_la_SOURCES += user_tokens.cc libircd_matrix_la_SOURCES += bridge.cc libircd_matrix_la_SOURCES += breadcrumb_rooms.cc +libircd_matrix_la_SOURCES += burst.cc libircd_matrix_la_SOURCES += display_name.cc libircd_matrix_la_SOURCES += event_append.cc libircd_matrix_la_SOURCES += event_horizon.cc diff --git a/matrix/burst.cc b/matrix/burst.cc new file mode 100644 index 000000000..27a44d5b3 --- /dev/null +++ b/matrix/burst.cc @@ -0,0 +1,108 @@ +// The Construct +// +// Copyright (C) The Construct Developers, Authors & Contributors +// Copyright (C) 2016-2020 Jason Volk +// +// 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::burst +{ + static void cache_warming(const node &, const opts &); + static void gossip(const node &, const opts &); + + extern conf::item cache_warmup_time; +} + +decltype(ircd::m::burst::log) +ircd::m::burst::log +{ + "m.burst" +}; + +decltype(ircd::m::burst::cache_warmup_time) +ircd::m::burst::cache_warmup_time +{ + { "name", "ircd.m.cache_warmup_time" }, + { "default", 3600L }, +}; + +ircd::m::burst::burst::burst(const node &node, + const opts &opts) +try +{ + log::debug + { + log, "Bursting to node %s", + string_view{node.node_id}, + }; + + if(opts.cache_warming) + if(ircd::uptime() < seconds(cache_warmup_time)) + cache_warming(node, opts); + + if(opts.gossip) + gossip(node, opts); +} +catch(const ctx::interrupted &e) +{ + throw; +} +catch(const std::exception &e) +{ + log::derror + { + log, "Burst to '%s' :%s", + string_view{node.node_id}, + e.what() + }; +} + +void +ircd::m::burst::gossip(const node &node, + const opts &opts) +try +{ + + +} +catch(const ctx::interrupted &e) +{ + throw; +} +catch(const std::exception &e) +{ + log::derror + { + log, "Gossip to '%s' :%s", + string_view{node.node_id}, + e.what() + }; +} + +/// We can smoothly warmup some memory caches after daemon startup as the +/// requests trickle in from remote servers. This function is invoked after +/// a remote contacts and reveals its identity with the X-Matrix verification. +void +ircd::m::burst::cache_warming(const node &node, + const opts &opts) +try +{ + // Make a query through SRV and A records. + //net::dns::resolve(origin, net::dns::prefetch_ipport); +} +catch(const ctx::interrupted &e) +{ + throw; +} +catch(const std::exception &e) +{ + log::derror + { + log, "Cache warming for '%s' :%s", + string_view{node.node_id}, + e.what() + }; +} diff --git a/matrix/resource.cc b/matrix/resource.cc index 49e3d43cf..8a664b9a5 100644 --- a/matrix/resource.cc +++ b/matrix/resource.cc @@ -10,24 +10,15 @@ namespace ircd::m { - extern conf::item cache_warmup_time; extern conf::item x_matrix_verify_origin; extern conf::item x_matrix_verify_destination; - static void cache_warm_origin(const resource::request &); static pair parse_version(const resource::request &); static string_view authenticate_bridge(const resource::method &, const client &, resource::request &); static user::id authenticate_user(const resource::method &, const client &, resource::request &); static string_view authenticate_node(const resource::method &, const client &, resource::request &); } -decltype(ircd::m::cache_warmup_time) -ircd::m::cache_warmup_time -{ - { "name", "ircd.m.cache_warmup_time" }, - { "default", 3600L }, -}; - decltype(ircd::m::x_matrix_verify_origin) ircd::m::x_matrix_verify_origin { @@ -79,14 +70,15 @@ try *this, client, request_ }; - if(request.node_id) + // If we have an error cached from previously not being able to + // contact this origin we can clear that now that they're alive. + if(request.node_id && server::errclear(fed::matrix_service(request.node_id))) { - // If we have an error cached from previously not being able to - // contact this origin we can clear that now that they're alive. - server::errclear(fed::matrix_service(request.node_id)); - - // The origin was verified so we can invoke the cache warming now. - cache_warm_origin(request); + m::burst::opts opts; + m::burst::burst + { + request.node_id, opts + }; } return function(client, request); @@ -419,33 +411,3 @@ ircd::m::parse_version(const m::resource::request &request) name, version }; } - -/// We can smoothly warmup some memory caches after daemon startup as the -/// requests trickle in from remote servers. This function is invoked after -/// a remote contacts and reveals its identity with the X-Matrix verification. -/// -/// This process helps us avoid cold caches for the first requests coming from -/// our server. Such requests are often parallel requests, for ex. to hundreds -/// of servers in a Matrix room at the same time. -/// -/// This function does nothing after the cache warmup period has ended. -void -ircd::m::cache_warm_origin(const resource::request &request) -try -{ - assert(request.node_id); - if(ircd::uptime() > seconds(cache_warmup_time)) - return; - - // Make a query through SRV and A records. - //net::dns::resolve(origin, net::dns::prefetch_ipport); -} -catch(const std::exception &e) -{ - log::derror - { - resource::log, "Cache warming for '%s' :%s", - request.node_id, - e.what() - }; -}