Compare commits

..
42 changed files with 689 additions and 1239 deletions

No files matched your search

+1 -1
View File
@@ -51,7 +51,7 @@ repos:
hooks:
- id: cargo-clippy
name: cargo clippy
entry: cargo clippy --
entry: cargo clippy -- -D warnings
language: system
pass_filenames: false
types: [ rust ]
Generated
+27 -61
View File
@@ -485,20 +485,19 @@ dependencies = [
[[package]]
name = "bindgen"
version = "0.72.1"
version = "0.73.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895"
checksum = "787ef8ef523575546b106a58213d6e6b06198a05c2f757258c68a74273670cfa"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"itertools 0.13.0",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex 1.3.0",
"syn 2.0.119",
"shlex",
"syn 3.0.10",
]
[[package]]
@@ -684,7 +683,7 @@ dependencies = [
"find-msvc-tools",
"jobserver",
"libc",
"shlex 2.0.1",
"shlex",
]
[[package]]
@@ -874,13 +873,9 @@ dependencies = [
name = "conduwuit"
version = "26.11.0-alpha.2"
dependencies = [
"aws-lc-rs",
"clap",
"conduwuit_admin",
"conduwuit_api",
"conduwuit_build_metadata",
"conduwuit_core",
"conduwuit_database",
"conduwuit_macros",
"conduwuit_router",
"conduwuit_service",
@@ -888,8 +883,6 @@ dependencies = [
"const-str",
"ctor",
"dtor",
"hardened_malloc-rs",
"log",
"opentelemetry",
"opentelemetry-otlp",
"opentelemetry-prometheus",
@@ -897,13 +890,10 @@ dependencies = [
"opentelemetry_sdk",
"parking_lot",
"prometheus",
"reqwest 0.13.5",
"rustls",
"sentry",
"sentry-tower",
"sentry-tracing",
"tokio",
"tokio-metrics",
"tracing",
"tracing-flame",
"tracing-journald",
@@ -1143,6 +1133,7 @@ dependencies = [
"p256 0.14.0",
"rand 0.10.4",
"regex",
"reqwest 0.12.28",
"reqwest 0.13.5",
"resolvematrix",
"ruma",
@@ -2720,10 +2711,10 @@ dependencies = [
"hyper",
"hyper-util",
"rustls",
"rustls-native-certs",
"tokio",
"tokio-rustls",
"tower-service",
"webpki-roots",
]
[[package]]
@@ -2997,15 +2988,6 @@ dependencies = [
"serde",
]
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.14.0"
@@ -3276,9 +3258,9 @@ checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "libz-sys"
version = "1.1.29"
version = "1.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85bc9657773828b90eeb625adff10eeac83cc21bbfd8e23a03eaa8a33c9e28d9"
checksum = "69965d2409bff8c8cea3829009fff66a86f05a0570656552ef70f616e8e4bd85"
dependencies = [
"cc",
"pkg-config",
@@ -4189,12 +4171,6 @@ dependencies = [
"phc",
]
[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pear"
version = "0.2.9"
@@ -4858,8 +4834,8 @@ dependencies = [
"log",
"percent-encoding",
"pin-project-lite",
"quinn",
"rustls",
"rustls-native-certs",
"rustls-pki-types",
"serde",
"serde_json",
@@ -4874,7 +4850,6 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"webpki-roots",
]
[[package]]
@@ -5213,8 +5188,8 @@ dependencies = [
[[package]]
name = "rust-librocksdb-sys"
version = "0.45.1+11.1.1"
source = "git+https://forgejo.ellis.link/continuwuation/rust-rocksdb-zaidoon1?rev=0a25ff92f7c09b55eec496b9c192c7d5136ab2b8#0a25ff92f7c09b55eec496b9c192c7d5136ab2b8"
version = "0.49.0+11.12.0"
source = "git+https://forgejo.ellis.link/continuwuation/rust-rocksdb-zaidoon1?rev=85ded20348e3dab902c239967075795e9c6d101b#85ded20348e3dab902c239967075795e9c6d101b"
dependencies = [
"bindgen",
"bzip2-sys",
@@ -5224,14 +5199,15 @@ dependencies = [
"libz-sys",
"lz4-sys",
"pkg-config",
"rustflags",
"tikv-jemalloc-sys",
"zstd-sys",
]
[[package]]
name = "rust-rocksdb"
version = "0.49.1"
source = "git+https://forgejo.ellis.link/continuwuation/rust-rocksdb-zaidoon1?rev=0a25ff92f7c09b55eec496b9c192c7d5136ab2b8#0a25ff92f7c09b55eec496b9c192c7d5136ab2b8"
version = "0.54.0"
source = "git+https://forgejo.ellis.link/continuwuation/rust-rocksdb-zaidoon1?rev=85ded20348e3dab902c239967075795e9c6d101b#85ded20348e3dab902c239967075795e9c6d101b"
dependencies = [
"libc",
"parking_lot",
@@ -5259,6 +5235,12 @@ dependencies = [
"semver",
]
[[package]]
name = "rustflags"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a39e0e9135d7a7208ee80aa4e3e4b88f0f5ad7be92153ed70686c38a03db2e63"
[[package]]
name = "rustix"
version = "1.1.6"
@@ -5886,12 +5868,6 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "shlex"
version = "2.0.1"
@@ -6251,18 +6227,17 @@ dependencies = [
[[package]]
name = "tikv-jemalloc-ctl"
version = "0.6.1"
source = "git+https://forgejo.ellis.link/continuwuation/jemallocator?rev=df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554#df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554"
version = "0.7.0"
source = "git+https://forgejo.ellis.link/continuwuation/jemallocator?rev=46e7821917b06636cdb5b203d312799970eaea92#46e7821917b06636cdb5b203d312799970eaea92"
dependencies = [
"libc",
"paste",
"tikv-jemalloc-sys",
]
[[package]]
name = "tikv-jemalloc-sys"
version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
source = "git+https://forgejo.ellis.link/continuwuation/jemallocator?rev=df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554#df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554"
version = "0.7.0+5.4.0-0-g7a34f18502e7b222724097cdcd499b437d189acc"
source = "git+https://forgejo.ellis.link/continuwuation/jemallocator?rev=46e7821917b06636cdb5b203d312799970eaea92#46e7821917b06636cdb5b203d312799970eaea92"
dependencies = [
"cc",
"libc",
@@ -6270,8 +6245,8 @@ dependencies = [
[[package]]
name = "tikv-jemallocator"
version = "0.6.1"
source = "git+https://forgejo.ellis.link/continuwuation/jemallocator?rev=df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554#df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554"
version = "0.7.0"
source = "git+https://forgejo.ellis.link/continuwuation/jemallocator?rev=46e7821917b06636cdb5b203d312799970eaea92#46e7821917b06636cdb5b203d312799970eaea92"
dependencies = [
"libc",
"tikv-jemalloc-sys",
@@ -7129,15 +7104,6 @@ dependencies = [
"rustls-pki-types",
]
[[package]]
name = "webpki-roots"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5f280bbb2b287bc8e7de7e3852945a0ff50d1bbcb5ed42b6c7a568c546df171"
dependencies = [
"rustls-pki-types",
]
[[package]]
name = "weezl"
version = "0.1.12"
+15 -4
View File
@@ -166,6 +166,15 @@ features = [
"form",
]
# Needed for as long as openidconnect is on an outdated reqwest
[workspace.dependencies.reqwest_0_12]
package = "reqwest"
version = "0.12"
default-features = false
features = [
"rustls-tls-native-roots-no-provider",
]
[workspace.dependencies.serde]
version = "1.0.229"
default-features = false
@@ -398,7 +407,7 @@ features = [
[workspace.dependencies.rust-rocksdb]
git = "https://forgejo.ellis.link/continuwuation/rust-rocksdb-zaidoon1"
rev = "0a25ff92f7c09b55eec496b9c192c7d5136ab2b8"
rev = "85ded20348e3dab902c239967075795e9c6d101b"
default-features = false
features = [
"multi-threaded-cf",
@@ -418,6 +427,8 @@ default-features = false
[workspace.dependencies.openidconnect]
version = "4.1.0"
default-features = false
features = ["reqwest"]
# optional opentelemetry, performance measurements, flamegraphs, etc for performance measurements and monitoring
[workspace.dependencies.opentelemetry]
@@ -479,7 +490,7 @@ version = "0.49.5"
# jemalloc usage
[workspace.dependencies.tikv-jemalloc-sys]
git = "https://forgejo.ellis.link/continuwuation/jemallocator"
rev = "df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554"
rev = "46e7821917b06636cdb5b203d312799970eaea92"
default-features = false
features = [
"background_threads_runtime_support",
@@ -487,7 +498,7 @@ features = [
]
[workspace.dependencies.tikv-jemallocator]
git = "https://forgejo.ellis.link/continuwuation/jemallocator"
rev = "df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554"
rev = "46e7821917b06636cdb5b203d312799970eaea92"
default-features = false
features = [
"background_threads_runtime_support",
@@ -495,7 +506,7 @@ features = [
]
[workspace.dependencies.tikv-jemalloc-ctl]
git = "https://forgejo.ellis.link/continuwuation/jemallocator"
rev = "df86ff89d4b1e223b9f7d2dd2fbb7f202da7f554"
rev = "46e7821917b06636cdb5b203d312799970eaea92"
default-features = false
features = ["use_std"]
@@ -1 +0,0 @@
Added the `GET /_matrix/client/v1/rooms/{roomId}/timestamp_to_event` endpoint as per MSC3030, letting clients find the event closest to a given timestamp. Contributed by @eleboucher.
-1
View File
@@ -1 +0,0 @@
URL previews no longer completely fail if downloading auxiliary content (images/videos/audio) fails. Contributed by @nex.
-1
View File
@@ -1 +0,0 @@
The former configuration alias `allow_check_for_updates` is now explicitly deprecated in favor of `allow_announcements_check`. Additionally, `request_ip_source` is now deprecated in favor of `accepted_ip_sources`.
-1
View File
@@ -1 +0,0 @@
Fix outgoing federation stalling after a sender worker panics. Contributed by @eleboucher.
+15 -4
View File
@@ -412,7 +412,7 @@
# env: CONTINUWUITY_IP_LOOKUP_STRATEGY
#ip_lookup_strategy = 5
# DEPRECATED: The preferred source to use for getting the real client IP.
# The preferred source to use for getting the real client IP.
#
# Takes any of the following options:
#
@@ -431,10 +431,9 @@
# to set this header" check; if the header selected is present, it is
# used.
#
# Ignored if `accepted_ip_sources` is set. `accepted_ip_sources` is
# the preferred alternative.
# Ignored if `accepted_ip_sources` is set.
#
# If not set, `direct` is used. Additionally, `direct` is used
# If not set set, `direct` is used. Additionally, `direct` is used
# as a fallback if the chosen header is not found.
#
# env: CONTINUWUITY_REQUEST_IP_SOURCE
@@ -813,6 +812,18 @@
# env: CONTINUWUITY_FORGET_FORCED_UPON_LEAVE
#forget_forced_upon_leave = false
# Set this to true to require authentication when looking up user profiles
# through this server.
#
# By default this is false, allowing tools such as matrix.to to access
# profiles of users on this server and on other servers.
# Setting this to true will block those tools, and may block automated
# crawlers from scraping users' profiles, although it will not block
# other homeservers from accessing users' profiles.
#
# env: CONTINUWUITY_REQUIRE_AUTH_FOR_PROFILE_REQUESTS
#require_auth_for_profile_requests = false
# Set this to true to require authentication to create a rendezvous
# session. Rendezvous sessions are used for QR code login.
#
+3 -2
View File
@@ -84,8 +84,9 @@ All forked dependencies are maintained under the
- [rocksdb][continuwuation-rocksdb] - Fork of [facebook/rocksdb][rocksdb] via
[`@zaidoon1`][8] with liburing build fixes and GCC debug build fixes
- [jemallocator][continuwuation-jemallocator] - Fork of
[tikv/jemallocator][jemallocator] fixing musl builds, suspicious code, and
adding support for redzones in Valgrind
[tikv/jemallocator][jemallocator] fixing musl builds and using jemalloc's
`*x` APIs consistently in `GlobalAlloc` to avoid interposer/Valgrind
mismatches
- [rustyline-async][continuwuation-rustyline-async] - Fork of
[zyansheep/rustyline-async][rustyline-async] with tab completion callback and
`CTRL+\` signal quit event for Continuwuity console CLI
-3
View File
@@ -71,9 +71,6 @@ Names below are as they appear in Prometheus. OTLP uses the dotted OpenTelemetry
| `continuwuity_sender_events_total` | counter | `destination`, `kind`, `outcome` | PDUs and EDUs sent in outgoing transactions |
| `continuwuity_sender_events_queued_total` | counter | `destination`, `kind` | PDUs and EDUs queued for sending |
| `continuwuity_sender_queue_depth` | gauge | | Messages waiting for the sender workers |
| `continuwuity_sender_requests` | gauge | `state` | Estimated outgoing requests stored as `queued` or `active` (in flight) |
| `continuwuity_sender_workers` | gauge | | Running sender workers; below the configured count means a worker is restarting |
| `continuwuity_sender_panics_total` | counter | `scope` | Panics caught in a single outgoing `transaction` or a whole sender `worker` |
| `continuwuity_users` | gauge | `origin` | Known `local` users, including deactivated, and `remote` users |
| `continuwuity_invites_total` | counter | `direction`, `destination`, `outcome` | Invites sent by local users or received over federation |
| `continuwuity_media_write_size_bytes` | histogram | `origin` | Media files written, from local uploads or cached remote media |
-46
View File
@@ -1,46 +0,0 @@
#-------------------------------------------------------------------------------#
# Qodana analysis is configured by qodana.yaml file #
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
#-------------------------------------------------------------------------------#
#################################################################################
# WARNING: Do not store sensitive information in this file, #
# as its contents will be included in the Qodana report. #
#################################################################################
version: "1.0"
#Specify inspection profile for code analysis
profile:
name: qodana.starter
#Enable inspections
#include:
# - name: <SomeEnabledInspectionId>
#Disable inspections
#exclude:
# - name: <SomeDisabledInspectionId>
# paths:
# - <path/where/not/run/inspection>
#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
#bootstrap: sh ./prepare-qodana.sh
#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
#plugins:
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)
# Quality gate. Will fail the CI/CD pipeline if any condition is not met
# severityThresholds - configures maximum thresholds for different problem severities
# testCoverageThresholds - configures minimum code coverage on a whole project and newly added code
# Code Coverage is available in Ultimate and Ultimate Plus plans
#failureConditions:
# severityThresholds:
# any: 15
# critical: 5
# testCoverageThresholds:
# fresh: 70
# total: 50
#Specify Qodana linter for analysis (Applied in CI/CD pipeline)
linter: jetbrains/qodana-<linter>:2026.2
+1 -6
View File
@@ -2,7 +2,7 @@ use std::time::Duration;
use axum::extract::State;
use conduwuit::{
Err, Result, err, info,
Err, Result, err,
utils::{self, content_disposition::make_content_disposition, math::ruma_from_usize},
};
use conduwuit_core::error;
@@ -219,15 +219,10 @@ pub(crate) async fn get_media_preview_route(
)));
}
info!("Fetching a URL preview for {url} on behalf of {sender_user}");
let preview = services
.media
.get_url_preview(&url)
.await
.inspect(|_| info!(user=%sender_user, %url, "Finished fetching a URL preview"))
.inspect_err(
|e| info!(user=%sender_user, %url, error=?e, "Failed to fetch a URL preview"),
)
.map_err(|error| {
err!(Request(Unknown(
debug_error!(%sender_user, %url, "Failed to fetch URL preview: {error}")
+2 -2
View File
@@ -29,7 +29,7 @@ pub(crate) async fn get_profile_route(
State(services): State<crate::State>,
body: Ruma<get_profile::v3::Request>,
) -> Result<get_profile::v3::Response> {
if body.identity.is_none() {
if services.config.require_auth_for_profile_requests && body.identity.is_none() {
return Err!(Request(Unauthorized(
"This server requires authentication to access user profiles."
)));
@@ -46,7 +46,7 @@ pub(crate) async fn get_profile_field_route(
State(services): State<crate::State>,
body: Ruma<get_profile_field::v3::Request>,
) -> Result<get_profile_field::v3::Response> {
if body.identity.is_none() {
if services.config.require_auth_for_profile_requests && body.identity.is_none() {
return Err!(Request(Unauthorized(
"This server requires authentication to access user profiles."
)));
+1 -3
View File
@@ -4,12 +4,10 @@ mod event;
mod initial_sync;
mod stripped_state;
mod summary;
mod timestamp_to_event;
mod upgrade;
pub(crate) use self::{
aliases::get_room_aliases_route, create::create_room_route, event::get_room_event_route,
initial_sync::room_initial_sync_route, stripped_state::get_stripped_state_route,
summary::get_room_summary, timestamp_to_event::get_event_by_timestamp_route,
upgrade::upgrade_room_route,
summary::get_room_summary, upgrade::upgrade_room_route,
};
-61
View File
@@ -1,61 +0,0 @@
use axum::extract::State;
use conduwuit::{Err, Event, Result, utils::stream::TryIgnore};
use futures::{StreamExt, pin_mut};
use ruma::api::client::room::get_event_by_timestamp;
use crate::Ruma;
const MAX_CANDIDATES: usize = 20;
/// # `GET /_matrix/client/v1/rooms/{roomId}/timestamp_to_event`
///
/// Gets the ID of the event closest to the given timestamp, in the direction
/// specified by `dir`.
pub(crate) async fn get_event_by_timestamp_route(
State(ref services): State<crate::State>,
body: Ruma<get_event_by_timestamp::v1::Request>,
) -> Result<get_event_by_timestamp::v1::Response> {
let sender_user = body.identity.expect_sender_user()?;
let room_id = &body.room_id;
if !services
.rooms
.state_accessor
.user_can_see_state_events(sender_user, room_id)
.await
{
return Err!(Request(Forbidden("You don't have permission to view this room.")));
}
if !services.rooms.timeline.timestamp_index_ready().await {
return Err!(FeatureDisabled("timestamp_to_event"));
}
let candidates = services
.rooms
.timeline
.pdus_by_timestamp(room_id, body.ts, body.dir)
.ignore_err()
.take(MAX_CANDIDATES);
pin_mut!(candidates);
while let Some((_, pdu)) = candidates.next().await {
if services
.rooms
.state_accessor
.user_can_see_event(sender_user, room_id, pdu.event_id())
.await
{
return Ok(get_event_by_timestamp::v1::Response::new(
pdu.event_id().to_owned(),
pdu.origin_server_ts(),
));
}
}
Err!(Request(NotFound(
"Unable to find event from {} in direction {:?}",
body.ts.get(),
body.dir
)))
}
-1
View File
@@ -52,7 +52,6 @@ pub fn build(router: Router<State>, state: State) -> Router<State> {
.ruma_route(&client::set_pushrule_actions_route)
.ruma_route(&client::delete_pushrule_route)
.ruma_route(&client::get_room_event_route)
.ruma_route(&client::get_event_by_timestamp_route)
.ruma_route(&client::get_room_aliases_route)
.ruma_route(&client::get_filter_route)
.ruma_route(&client::create_filter_route)
+428 -137
View File
@@ -1,9 +1,12 @@
//! jemalloc allocator
use std::{
cell::OnceCell,
ffi::{CStr, c_char, c_void},
any::Any,
ffi::{CStr, CString, c_char, c_uint, c_void},
fmt::Debug,
panic::{self, AssertUnwindSafe},
ptr,
sync::OnceLock,
};
use arrayvec::ArrayVec;
@@ -12,26 +15,61 @@ use tikv_jemalloc_sys as ffi;
use tikv_jemallocator as jemalloc;
use crate::{
Result, SyncRwLock, err, is_equal_to, is_nonzero,
Result, err, is_equal_to, is_nonzero,
utils::{math, math::Tried},
};
#[cfg(feature = "jemalloc_conf")]
#[unsafe(no_mangle)]
pub static malloc_conf: &[u8] = const_str::concat_bytes!(
#[repr(transparent)]
struct MallocConf(*const c_char);
// SAFETY: MallocConf points to static immutable NUL-terminated bytes.
#[cfg(feature = "jemalloc_conf")]
unsafe impl Sync for MallocConf {}
#[cfg(feature = "jemalloc_conf")]
const MALLOC_CONF_BYTES: &[u8] = const_str::concat_bytes!(
"lg_extent_max_active_fit:4",
",oversize_threshold:16777216",
",tcache_max:2097152",
",dirty_decay_ms:16000",
",muzzy_decay_ms:144000",
",percpu_arena:percpu",
",metadata_thp:always",
",background_thread:true",
",max_background_threads:-1",
MALLOC_CONF_PLATFORM,
MALLOC_CONF_PROF,
0
);
#[cfg(all(feature = "jemalloc_conf", target_os = "linux"))]
const MALLOC_CONF_PLATFORM: &str =
",percpu_arena:percpu,metadata_thp:always,background_thread:true";
#[cfg(all(feature = "jemalloc_conf", not(target_os = "linux")))]
const MALLOC_CONF_PLATFORM: &str = "";
// Targets match `NO_UNPREFIXED_MALLOC_TARGETS` in `tikv-jemalloc-sys` when
// `unprefixed_malloc_on_supported_platforms` is enabled.
#[cfg(feature = "jemalloc_conf")]
#[used]
#[allow(non_upper_case_globals)]
#[cfg_attr(
any(
target_vendor = "apple",
target_env = "musl",
target_os = "android",
target_os = "dragonfly",
),
unsafe(export_name = "_rjem_malloc_conf")
)]
#[cfg_attr(
not(any(
target_vendor = "apple",
target_env = "musl",
target_os = "android",
target_os = "dragonfly",
)),
unsafe(no_mangle)
)]
static malloc_conf: MallocConf = MallocConf(MALLOC_CONF_BYTES.as_ptr().cast());
#[cfg(all(feature = "jemalloc_conf", feature = "jemalloc_prof"))]
const MALLOC_CONF_PROF: &str = ",prof_active:false";
#[cfg(all(feature = "jemalloc_conf", not(feature = "jemalloc_prof")))]
@@ -39,28 +77,39 @@ const MALLOC_CONF_PROF: &str = "";
#[global_allocator]
static JEMALLOC: jemalloc::Jemalloc = jemalloc::Jemalloc;
static CONTROL: SyncRwLock<()> = SyncRwLock::new(());
type Name = ArrayVec<u8, NAME_MAX>;
type Key = ArrayVec<usize, KEY_SEGS>;
const NAME_MAX: usize = 128;
const KEY_SEGS: usize = 8;
const ARENA_LIMIT: usize = 4095;
const ARENAS_ALL: usize = 4096;
#[ctor::ctor(unsafe)]
fn _static_initialization() {
acq_epoch().expect("pre-initialization of jemalloc failed");
acq_epoch().expect("pre-initialization of jemalloc failed");
let mut epoch: u64 = 0;
let mut len = size_of::<u64>();
// SAFETY: Pre-initialize jemalloc before main() without touching Rust TLS
// or unwinding across the constructor boundary.
unsafe {
ffi::mallctl(
c"epoch".as_ptr(),
ptr::from_mut(&mut epoch).cast::<c_void>(),
&raw mut len,
ptr::null_mut(),
0,
);
}
}
#[must_use]
#[cfg(feature = "jemalloc_stats")]
pub fn memory_usage() -> Option<String> {
let mibs = |input: usize| {
let kibs = input / 1024;
let kibs = u32::try_from(kibs).unwrap_or_default();
let kibs = f64::from(kibs);
kibs / 1024.0
let whole = u32::try_from(input / (1024 * 1024)).unwrap_or(u32::MAX);
let rem_kibs = u32::try_from((input / 1024) % 1024).unwrap_or_default();
f64::from(whole) + f64::from(rem_kibs) / 1024.0
};
let [allocated, active, mapped, metadata, resident, retained] =
@@ -81,8 +130,8 @@ pub fn memory_usage() -> Option<String> { None }
pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> {
use mallctl::stats;
// Acquire the epoch; ensure latest stats are pulled in
acq_epoch().ok()?;
// Advance the epoch; ensure latest stats are pulled in
inc_epoch().ok()?;
Some([
("allocated", stats::allocated::read().unwrap_or_default()),
@@ -98,164 +147,239 @@ pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> {
#[cfg(not(feature = "jemalloc_stats"))]
pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { None }
struct StatsPrintState {
buf: String,
max_len: usize,
panic: Option<Box<dyn Any + Send>>,
}
pub fn memory_stats(opts: &str) -> Option<String> {
const MAX_LENGTH: usize = 1_048_576;
let mut str = String::new();
let opaque = std::ptr::from_mut(&mut str).cast::<c_void>();
let opts_p: *const c_char = std::ffi::CString::new(opts)
.expect("cstring")
.into_raw()
.cast_const();
let opts = CString::new(opts).ok()?;
// Acquire the epoch; ensure latest stats are pulled in
acq_epoch().ok()?;
let mut state = StatsPrintState {
buf: String::new(),
max_len: MAX_LENGTH,
panic: None,
};
let opaque = ptr::from_mut(&mut state).cast::<c_void>();
// SAFETY: calls malloc_stats_print() with our string instance which must
// remain in this frame. https://docs.rs/tikv-jemalloc-sys/latest/tikv_jemalloc_sys/fn.malloc_stats_print.html
unsafe { ffi::malloc_stats_print(Some(malloc_stats_cb), opaque, opts_p) };
// SAFETY: `state` and `opts` remain live in this frame for the duration of
// `malloc_stats_print()`, which invokes `malloc_stats_cb` synchronously.
unsafe { ffi::malloc_stats_print(Some(malloc_stats_cb), opaque, opts.as_ptr()) };
str.truncate(MAX_LENGTH);
if let Some(payload) = state.panic {
panic::resume_unwind(payload);
}
Some(str)
Some(state.buf)
}
unsafe extern "C" fn malloc_stats_cb(opaque: *mut c_void, msg: *const c_char) {
// SAFETY: we have to trust the opaque points to our String
let res: &mut String = unsafe {
opaque
.cast::<String>()
.as_mut()
.expect("failed to cast void* to &mut String")
// SAFETY: `opaque` points to `StatsPrintState` on the caller's stack.
let Some(state) = (unsafe { opaque.cast::<StatsPrintState>().as_mut() }) else {
return;
};
// SAFETY: we have to trust the string is null terminated.
if state.panic.is_some() || msg.is_null() || state.buf.len() >= state.max_len {
return;
}
// SAFETY: jemalloc guarantees `msg` is a valid NUL-terminated C string.
let msg = unsafe { CStr::from_ptr(msg) };
let msg = String::from_utf8_lossy(msg.to_bytes());
res.push_str(msg.as_ref());
if let Err(err) = panic::catch_unwind(AssertUnwindSafe(|| {
let msg = String::from_utf8_lossy(msg.to_bytes());
let remaining = state.max_len.saturating_sub(state.buf.len());
if msg.len() <= remaining {
state.buf.push_str(msg.as_ref());
} else {
if let Some(prefix) = msg.get(..msg.floor_char_boundary(remaining)) {
state.buf.push_str(prefix);
}
state.max_len = state.buf.len();
}
})) {
state.panic = Some(err);
}
}
macro_rules! mallctl {
($name:expr_2021) => {{
thread_local! {
static KEY: OnceCell<Key> = OnceCell::default();
};
($name:literal) => {{
static KEY: OnceLock<Key> = OnceLock::new();
KEY.with(|once| {
once.get_or_init(move || key($name).expect("failed to translate name into mib key"))
.clone()
})
KEY.get_or_init(|| key($name).expect("failed to translate name into mib key"))
}};
}
pub mod this_thread {
use super::{Debug, Key, OnceCell, Result, is_nonzero, key, math};
thread_local! {
static ALLOCATED_BYTES: OnceCell<&'static u64> = const { OnceCell::new() };
static DEALLOCATED_BYTES: OnceCell<&'static u64> = const { OnceCell::new() };
}
use super::{Debug, Key, OnceLock, Result, c_uint, is_nonzero, key, math};
pub fn trim() -> Result { decay().and_then(|()| purge()) }
pub fn purge() -> Result { notify(mallctl!("arena.0.purge")) }
pub fn purge() -> Result {
// SAFETY: `arena.<i>.purge` is an `arena.<i>.*` notification ctl.
unsafe { notify(mallctl!("arena.0.purge")) }
}
pub fn decay() -> Result { notify(mallctl!("arena.0.decay")) }
pub fn decay() -> Result {
// SAFETY: `arena.<i>.decay` is an `arena.<i>.*` notification ctl.
unsafe { notify(mallctl!("arena.0.decay")) }
}
pub fn idle() -> Result { super::notify(&mallctl!("thread.idle")) }
pub fn idle() -> Result {
// SAFETY: `thread.idle` is a non-indexed notification ctl.
unsafe { super::notify(mallctl!("thread.idle")) }
}
pub fn flush() -> Result { super::notify(&mallctl!("thread.tcache.flush")) }
pub fn flush() -> Result {
// SAFETY: `thread.tcache.flush` is a non-indexed notification ctl.
unsafe { super::notify(mallctl!("thread.tcache.flush")) }
}
pub fn set_muzzy_decay(decay_ms: isize) -> Result<isize> {
set(mallctl!("arena.0.muzzy_decay_ms"), decay_ms)
// SAFETY: `arena.<i>.muzzy_decay_ms` has C type `ssize_t`.
unsafe { set(mallctl!("arena.0.muzzy_decay_ms"), decay_ms) }
}
pub fn get_muzzy_decay() -> Result<isize> { get(mallctl!("arena.0.muzzy_decay_ms")) }
pub fn get_muzzy_decay() -> Result<isize> {
// SAFETY: `arena.<i>.muzzy_decay_ms` has C type `ssize_t`.
unsafe { get(mallctl!("arena.0.muzzy_decay_ms")) }
}
pub fn set_dirty_decay(decay_ms: isize) -> Result<isize> {
set(mallctl!("arena.0.dirty_decay_ms"), decay_ms)
// SAFETY: `arena.<i>.dirty_decay_ms` has C type `ssize_t`.
unsafe { set(mallctl!("arena.0.dirty_decay_ms"), decay_ms) }
}
pub fn get_dirty_decay() -> Result<isize> { get(mallctl!("arena.0.dirty_decay_ms")) }
pub fn get_dirty_decay() -> Result<isize> {
// SAFETY: `arena.<i>.dirty_decay_ms` has C type `ssize_t`.
unsafe { get(mallctl!("arena.0.dirty_decay_ms")) }
}
pub fn cache_enable(enable: bool) -> Result<bool> {
super::set::<u8>(&mallctl!("thread.tcache.enabled"), enable.into()).map(is_nonzero!())
// SAFETY: `thread.tcache.enabled` has C type `bool` (1 byte on
// non-MSVC).
unsafe { super::set::<u8>(mallctl!("thread.tcache.enabled"), enable.into()) }
.map(is_nonzero!())
}
pub fn is_cache_enabled() -> Result<bool> {
super::get::<u8>(&mallctl!("thread.tcache.enabled")).map(is_nonzero!())
// SAFETY: `thread.tcache.enabled` has C type `bool` (1 byte on
// non-MSVC).
unsafe { super::get::<u8>(mallctl!("thread.tcache.enabled")) }.map(is_nonzero!())
}
pub fn set_arena(id: usize) -> Result<usize> {
super::set::<u32>(&mallctl!("thread.arena"), id.try_into()?).and_then(math::try_into)
// SAFETY: `thread.arena` has C type `unsigned`.
unsafe { super::set::<c_uint>(mallctl!("thread.arena"), id.try_into()?) }
.and_then(math::try_into)
}
pub fn arena_id() -> Result<usize> {
super::get::<u32>(&mallctl!("thread.arena")).and_then(math::try_into)
// SAFETY: `thread.arena` has C type `unsigned`.
unsafe { super::get::<c_uint>(mallctl!("thread.arena")) }.and_then(math::try_into)
}
pub fn prof_enable(enable: bool) -> Result<bool> {
super::set::<u8>(&mallctl!("thread.prof.active"), enable.into()).map(is_nonzero!())
// SAFETY: `thread.prof.active` has C type `bool` (1 byte on non-MSVC).
unsafe { super::set::<u8>(mallctl!("thread.prof.active"), enable.into()) }
.map(is_nonzero!())
}
pub fn is_prof_enabled() -> Result<bool> {
super::get::<u8>(&mallctl!("thread.prof.active")).map(is_nonzero!())
// SAFETY: `thread.prof.active` has C type `bool` (1 byte on non-MSVC).
unsafe { super::get::<u8>(mallctl!("thread.prof.active")) }.map(is_nonzero!())
}
pub fn reset_peak() -> Result { super::notify(&mallctl!("thread.peak.reset")) }
pub fn reset_peak() -> Result {
// SAFETY: `thread.peak.reset` is a non-indexed notification ctl.
unsafe { super::notify(mallctl!("thread.peak.reset")) }
}
pub fn peak() -> Result<u64> { super::get(&mallctl!("thread.peak.read")) }
pub fn peak() -> Result<u64> {
// SAFETY: `thread.peak.read` has C type `uint64_t`.
unsafe { super::get(mallctl!("thread.peak.read")) }
}
#[inline]
#[must_use]
pub fn allocated() -> u64 {
*ALLOCATED_BYTES.with(|once| init_tls_cell(once, "thread.allocatedp"))
// SAFETY: `thread.allocated` has C type `uint64_t`.
unsafe { super::get(mallctl!("thread.allocated")) }.unwrap_or_default()
}
#[inline]
#[must_use]
pub fn deallocated() -> u64 {
*DEALLOCATED_BYTES.with(|once| init_tls_cell(once, "thread.deallocatedp"))
// SAFETY: `thread.deallocated` has C type `uint64_t`.
unsafe { super::get(mallctl!("thread.deallocated")) }.unwrap_or_default()
}
fn notify(key: Key) -> Result { super::notify_by_arena(Some(arena_id()?), key) }
/// # Safety
/// Caller must ensure `key` is an `arena.<i>.*` notification MIB
/// (`key.len() >= 2`).
unsafe fn notify(key: &Key) -> Result {
let mut key = key.clone();
// `arena_id()` returns this thread's own valid arena (< narenas <
// ARENA_LIMIT), so we skip `validate_arena_id()` to avoid locking
// `ctl_mtx` on hot paths like `decay()`.
key[1] = arena_id()?;
// SAFETY: Caller guarantees `key` is an `arena.<i>.*` notification MIB
// and `arena_id()` returns a valid arena index.
unsafe { super::notify(&key) }
}
fn set<T>(key: Key, val: T) -> Result<T>
/// # Safety
/// Caller must ensure `key` is an `arena.<i>.*` MIB and `T` matches its
/// exact C type.
unsafe fn set<T>(key: &Key, val: T) -> Result<T>
where
T: Copy + Debug,
{
super::set_by_arena(Some(arena_id()?), key, val)
let mut key = key.clone();
// `arena_id()` is already a valid arena index; skip
// `validate_arena_id()`.
key[1] = arena_id()?;
// SAFETY: Caller guarantees `T` matches `key`.
unsafe { super::set(&key, val) }
}
fn get<T>(key: Key) -> Result<T>
/// # Safety
/// Caller must ensure `key` is an `arena.<i>.*` MIB and `T` matches its
/// exact C type.
unsafe fn get<T>(key: &Key) -> Result<T>
where
T: Copy + Debug,
{
super::get_by_arena(Some(arena_id()?), key)
}
fn init_tls_cell(cell: &OnceCell<&'static u64>, name: &str) -> &'static u64 {
cell.get_or_init(|| {
let ptr: *const u64 = super::get(&mallctl!(name)).expect("failed to obtain pointer");
// SAFETY: ptr points directly to the internal state of jemalloc for
// this thread
unsafe { ptr.as_ref() }.expect("pointer must not be null")
})
let mut key = key.clone();
// `arena_id()` is already a valid arena index; skip
// `validate_arena_id()`.
key[1] = arena_id()?;
// SAFETY: Caller guarantees `T` matches `key`.
unsafe { super::get(&key) }
}
}
pub fn stats_reset() -> Result { notify(&mallctl!("stats.mutexes.reset")) }
pub fn stats_reset() -> Result {
// SAFETY: `stats.mutexes.reset` is a non-indexed notification ctl.
unsafe { notify(mallctl!("stats.mutexes.reset")) }
}
pub fn prof_reset() -> Result { notify(&mallctl!("prof.reset")) }
pub fn prof_reset() -> Result {
// SAFETY: `prof.reset` is a non-indexed notification ctl.
unsafe { notify(mallctl!("prof.reset")) }
}
pub fn prof_enable(enable: bool) -> Result<bool> {
set::<u8>(&mallctl!("prof.active"), enable.into()).map(is_nonzero!())
// SAFETY: `prof.active` has C type `bool` (1 byte on non-MSVC).
unsafe { set::<u8>(mallctl!("prof.active"), enable.into()) }.map(is_nonzero!())
}
pub fn is_prof_enabled() -> Result<bool> {
get::<u8>(&mallctl!("prof.active")).map(is_nonzero!())
// SAFETY: `prof.active` has C type `bool` (1 byte on non-MSVC).
unsafe { get::<u8>(mallctl!("prof.active")) }.map(is_nonzero!())
}
pub fn trim<I: Into<Option<usize>> + Copy>(arena: I) -> Result {
@@ -263,31 +387,40 @@ pub fn trim<I: Into<Option<usize>> + Copy>(arena: I) -> Result {
}
pub fn purge<I: Into<Option<usize>>>(arena: I) -> Result {
notify_by_arena(arena.into(), mallctl!("arena.4096.purge"))
// SAFETY: `arena.<i>.purge` is an `arena.<i>.*` notification ctl.
unsafe { notify_by_arena(arena.into(), mallctl!("arena.4096.purge")) }
}
pub fn decay<I: Into<Option<usize>>>(arena: I) -> Result {
notify_by_arena(arena.into(), mallctl!("arena.4096.decay"))
// SAFETY: `arena.<i>.decay` is an `arena.<i>.*` notification ctl.
unsafe { notify_by_arena(arena.into(), mallctl!("arena.4096.decay")) }
}
pub fn set_muzzy_decay<I: Into<Option<usize>>>(arena: I, decay_ms: isize) -> Result<isize> {
match arena.into() {
| Some(arena) =>
set_by_arena(Some(arena), mallctl!("arena.4096.muzzy_decay_ms"), decay_ms),
| _ => set(&mallctl!("arenas.muzzy_decay_ms"), decay_ms),
// SAFETY: `arena.<i>.muzzy_decay_ms` has C type `ssize_t`.
| Some(arena) => unsafe {
set_by_arena(arena, mallctl!("arena.0.muzzy_decay_ms"), decay_ms)
},
// SAFETY: `arenas.muzzy_decay_ms` has C type `ssize_t`.
| _ => unsafe { set(mallctl!("arenas.muzzy_decay_ms"), decay_ms) },
}
}
pub fn set_dirty_decay<I: Into<Option<usize>>>(arena: I, decay_ms: isize) -> Result<isize> {
match arena.into() {
| Some(arena) =>
set_by_arena(Some(arena), mallctl!("arena.4096.dirty_decay_ms"), decay_ms),
| _ => set(&mallctl!("arenas.dirty_decay_ms"), decay_ms),
// SAFETY: `arena.<i>.dirty_decay_ms` has C type `ssize_t`.
| Some(arena) => unsafe {
set_by_arena(arena, mallctl!("arena.0.dirty_decay_ms"), decay_ms)
},
// SAFETY: `arenas.dirty_decay_ms` has C type `ssize_t`.
| _ => unsafe { set(mallctl!("arenas.dirty_decay_ms"), decay_ms) },
}
}
pub fn background_thread_enable(enable: bool) -> Result<bool> {
set::<u8>(&mallctl!("background_thread"), enable.into()).map(is_nonzero!())
// SAFETY: `background_thread` has C type `bool` (1 byte on non-MSVC).
unsafe { set::<u8>(mallctl!("background_thread"), enable.into()) }.map(is_nonzero!())
}
#[inline]
@@ -303,83 +436,138 @@ pub fn is_percpu_arena() -> bool { percpu_arenas().is_ok_and(is_equal_to!("percp
pub fn is_phycpu_arena() -> bool { percpu_arenas().is_ok_and(is_equal_to!("phycpu")) }
pub fn percpu_arenas() -> Result<&'static str> {
let ptr = get::<*const c_char>(&mallctl!("opt.percpu_arena"))?;
//SAFETY: ptr points to a null-terminated string returned for
// opt.percpu_arena.
// SAFETY: `opt.percpu_arena` has C type `const char *`.
let ptr = unsafe { get::<*const c_char>(mallctl!("opt.percpu_arena")) }?;
if ptr.is_null() {
return Err(err!("mallctl: opt.percpu_arena returned NULL"));
}
// SAFETY: `ptr` is non-null and points to a static NUL-terminated string
// returned by jemalloc for `opt.percpu_arena`.
let cstr = unsafe { CStr::from_ptr(ptr) };
cstr.to_str().map_err(Into::into)
}
pub fn arenas() -> Result<usize> {
get::<u32>(&mallctl!("arenas.narenas")).and_then(math::try_into)
// SAFETY: `arenas.narenas` has C type `unsigned`.
unsafe { get::<c_uint>(mallctl!("arenas.narenas")) }.and_then(math::try_into)
}
pub fn inc_epoch() -> Result<u64> { xchg(&mallctl!("epoch"), 1_u64) }
pub fn acq_epoch() -> Result<u64> { xchg(&mallctl!("epoch"), 0_u64) }
fn notify_by_arena(id: Option<usize>, mut key: Key) -> Result {
key[1] = id.unwrap_or(4096);
notify(&key)
pub fn inc_epoch() -> Result<u64> {
// SAFETY: `epoch` has C type `uint64_t`.
unsafe { xchg(mallctl!("epoch"), 1_u64) }
}
fn set_by_arena<T>(id: Option<usize>, mut key: Key, val: T) -> Result<T>
pub fn acq_epoch() -> Result<u64> {
// SAFETY: `epoch` has C type `uint64_t`.
unsafe { get(mallctl!("epoch")) }
}
fn validate_arena_id(id: usize, allow_all: bool) -> Result<usize> {
if allow_all && id == ARENAS_ALL {
return Ok(id);
}
let narenas = arenas()?.min(ARENA_LIMIT);
if id < narenas {
Ok(id)
} else {
Err(err!("mallctl: invalid arena index {id} (narenas: {narenas})"))
}
}
/// # Safety
/// Caller must ensure `key` is an `arena.<i>.*` notification MIB
/// (`key.len() >= 2`).
unsafe fn notify_by_arena(id: Option<usize>, key: &Key) -> Result {
let mut key = key.clone();
key[1] = match id {
| Some(id) => validate_arena_id(id, true)?,
| None => ARENAS_ALL,
};
// SAFETY: Caller guarantees `key` is an `arena.<i>.*` notification MIB and
// `key[1]` has been validated above.
unsafe { notify(&key) }
}
/// # Safety
/// Caller must ensure `key` is an `arena.<i>.*` MIB (`key.len() >= 2`) and `T`
/// matches its exact C type.
unsafe fn set_by_arena<T>(id: usize, key: &Key, val: T) -> Result<T>
where
T: Copy + Debug,
{
key[1] = id.unwrap_or(4096);
set(&key, val)
let mut key = key.clone();
key[1] = validate_arena_id(id, false)?;
// SAFETY: Caller guarantees `T` matches `key`.
unsafe { set(&key, val) }
}
fn get_by_arena<T>(id: Option<usize>, mut key: Key) -> Result<T>
/// # Safety
/// Caller must ensure `key` is a valid notification MIB and any indexed arena
/// component is within bounds.
unsafe fn notify(key: &Key) -> Result {
// SAFETY: jemalloc notification ctls require `oldp`, `oldlenp`, and `newp`
// to be NULL and `newlen` to be 0 (otherwise `EPERM` is returned).
let ret = unsafe {
ffi::mallctlbymib(
key.as_ptr(),
key.len(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
0,
)
};
if ret == 0 {
Ok(())
} else {
Err(err!("mallctl: {}", std::io::Error::from_raw_os_error(ret)))
}
}
/// Does not advance `epoch`; callers reading cached `stats.*` must call
/// `inc_epoch()` explicitly.
///
/// # Safety
/// Caller must ensure `T` matches the exact C type expected by `key`.
unsafe fn set<T>(key: &Key, val: T) -> Result<T>
where
T: Copy + Debug,
{
key[1] = id.unwrap_or(4096);
get(&key)
}
fn notify(key: &Key) -> Result { xchg(key, ()) }
fn set<T>(key: &Key, val: T) -> Result<T>
where
T: Copy + Debug,
{
let _lock = CONTROL.write();
let res = xchg(key, val)?;
inc_epoch()?;
Ok(res)
// SAFETY: Caller guarantees `T` matches `key`.
unsafe { xchg(key, val) }
}
/// # Safety
/// Caller must ensure `T` matches the exact C type expected by `key`.
#[tracing::instrument(
name = "get",
level = "trace",
skip_all,
fields(?key)
)]
fn get<T>(key: &Key) -> Result<T>
unsafe fn get<T>(key: &Key) -> Result<T>
where
T: Copy + Debug,
{
acq_epoch()?;
acq_epoch()?;
// SAFETY: T must be perfectly valid to receive value.
// SAFETY: Caller guarantees `T` matches the C type returned for `key`.
unsafe { mallctl::raw::read_mib(key.as_slice()) }.map_err(map_err)
}
/// # Safety
/// Caller must ensure `T` matches the exact C type expected by `key`.
#[tracing::instrument(
name = "xchg",
level = "trace",
skip_all,
fields(?key, ?val)
)]
fn xchg<T>(key: &Key, val: T) -> Result<T>
unsafe fn xchg<T>(key: &Key, val: T) -> Result<T>
where
T: Copy + Debug,
{
// SAFETY: T must be the exact expected type.
// SAFETY: Caller guarantees `T` matches the C type expected by `key`.
unsafe { mallctl::raw::update_mib(key.as_slice(), val) }.map_err(map_err)
}
@@ -387,6 +575,9 @@ fn key(name: &str) -> Result<Key> {
// tikv asserts the output buffer length is tight to the number of required
// mibs so we slice that down here.
let segs = name.chars().filter(is_equal_to!(&'.')).count().try_add(1)?;
if segs > KEY_SEGS {
return Err(err!("mallctl: key has too many segments ({segs} > {KEY_SEGS})"));
}
let name = self::name(name)?;
let mut buf = [0_usize; KEY_SEGS];
@@ -396,6 +587,10 @@ fn key(name: &str) -> Result<Key> {
}
fn name(name: &str) -> Result<Name> {
if name.as_bytes().contains(&0) {
return Err(err!("mallctl: key contains interior NUL byte"));
}
let mut buf = Name::new();
buf.try_extend_from_slice(name.as_bytes())?;
buf.try_extend_from_slice(b"\0")?;
@@ -406,3 +601,99 @@ fn name(name: &str) -> Result<Name> {
fn map_err(error: tikv_jemalloc_ctl::Error) -> crate::Error {
err!("mallctl: {}", error.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn notify_ctls_succeed() {
this_thread::flush().expect("thread.tcache.flush");
this_thread::decay().expect("arena.<i>.decay");
this_thread::purge().expect("arena.<i>.purge");
this_thread::trim().expect("this_thread::trim");
trim(None).expect("trim(None)");
}
#[test]
#[cfg(feature = "jemalloc_stats")]
fn allocated_and_deallocated_do_not_alias() {
let a0 = this_thread::allocated();
let d0 = this_thread::deallocated();
let buf = std::hint::black_box(vec![0_u8; 64 * 1024]);
let a1 = this_thread::allocated();
assert!(a1 > a0, "expected allocated to increase: {a0} -> {a1}");
drop(buf);
let d1 = this_thread::deallocated();
assert!(d1 > d0, "expected deallocated to increase: {d0} -> {d1}");
}
#[test]
fn memory_stats_handles_valid_and_nul_opts() {
assert!(memory_stats("invalid\0opts").is_none());
let stats = memory_stats("gmal").expect("memory_stats");
assert!(!stats.is_empty());
}
#[test]
fn key_too_many_segments_returns_err() { key("a.b.c.d.e.f.g.h.i").unwrap_err(); }
#[test]
fn key_interior_nul_returns_err() { key("a.b\0.c").unwrap_err(); }
#[test]
fn arena_index_bounds_rejected() {
purge(Some(ARENAS_ALL)).expect("purge(MALLCTL_ARENAS_ALL)");
decay(Some(ARENAS_ALL)).expect("decay(MALLCTL_ARENAS_ALL)");
let narenas = arenas().expect("arenas.narenas");
purge(Some(narenas)).unwrap_err();
decay(Some(narenas)).unwrap_err();
set_dirty_decay(Some(narenas), -1).unwrap_err();
set_muzzy_decay(Some(narenas), -1).unwrap_err();
purge(Some(4097)).unwrap_err();
decay(Some(4097)).unwrap_err();
trim(Some(4097)).unwrap_err();
purge(Some(usize::MAX)).unwrap_err();
set_dirty_decay(Some(ARENAS_ALL), -1).unwrap_err();
set_muzzy_decay(Some(ARENAS_ALL), -1).unwrap_err();
set_dirty_decay(Some(4097), -1).unwrap_err();
set_muzzy_decay(Some(4097), -1).unwrap_err();
}
#[test]
#[cfg(feature = "jemalloc_conf")]
fn malloc_conf_is_applied() {
if std::env::var_os("MALLOC_CONF").is_some()
|| std::env::var_os("_RJEM_MALLOC_CONF").is_some()
{
return;
}
// SAFETY: `opt.dirty_decay_ms` and `opt.muzzy_decay_ms` have C type
// `ssize_t`.
let dirty =
unsafe { get::<isize>(mallctl!("opt.dirty_decay_ms")) }.expect("opt.dirty_decay_ms");
let muzzy =
unsafe { get::<isize>(mallctl!("opt.muzzy_decay_ms")) }.expect("opt.muzzy_decay_ms");
assert_eq!(dirty, 16_000, "expected malloc_conf dirty_decay_ms:16000");
assert_eq!(muzzy, 144_000, "expected malloc_conf muzzy_decay_ms:144000");
}
#[test]
fn percpu_arenas_returns_valid_mode() {
let mode = percpu_arenas().expect("opt.percpu_arena");
assert!(matches!(mode, "disabled" | "percpu" | "phycpu"), "unexpected mode: {mode}");
}
#[test]
fn epoch_read_and_advance() {
let e0 = acq_epoch().expect("acq_epoch");
let e1 = inc_epoch().expect("inc_epoch");
assert!(e1 > e0, "expected inc_epoch to advance epoch: {e0} -> {e1}");
}
}
+35 -31
View File
@@ -195,7 +195,7 @@ pub struct Config {
/// announcements or major updates. This is not an update check endpoint.
///
/// default: true
#[serde(default = "true_fn")]
#[serde(alias = "allow_check_for_updates", default = "true_fn")]
pub allow_announcements_check: bool,
/// Set this to any float value to multiply continuwuity's in-memory LRU
@@ -490,7 +490,7 @@ pub struct Config {
#[serde(default = "default_ip_lookup_strategy")]
pub ip_lookup_strategy: u8,
/// DEPRECATED: The preferred source to use for getting the real client IP.
/// The preferred source to use for getting the real client IP.
///
/// Takes any of the following options:
///
@@ -509,10 +509,9 @@ pub struct Config {
/// to set this header" check; if the header selected is present, it is
/// used.
///
/// Ignored if `accepted_ip_sources` is set. `accepted_ip_sources` is
/// the preferred alternative.
/// Ignored if `accepted_ip_sources` is set.
///
/// If not set, `direct` is used. Additionally, `direct` is used
/// If not set set, `direct` is used. Additionally, `direct` is used
/// as a fallback if the chosen header is not found.
#[serde(default)]
pub request_ip_source: Option<String>,
@@ -920,6 +919,19 @@ pub struct Config {
#[serde(default)]
pub forget_forced_upon_leave: bool,
/// Set this to true to require authentication when looking up user profiles
/// through this server.
///
/// By default this is false, allowing tools such as matrix.to to access
/// profiles of users on this server and on other servers.
/// Setting this to true will block those tools, and may block automated
/// crawlers from scraping users' profiles, although it will not block
/// other homeservers from accessing users' profiles.
///
/// default: false
#[serde(default)]
pub require_auth_for_profile_requests: bool,
/// Set this to true to require authentication to create a rendezvous
/// session. Rendezvous sessions are used for QR code login.
///
@@ -2981,17 +2993,11 @@ impl<'de, K: Hash + Eq + Clone> Deserialize<'de> for PerMinuteRatelimiter<K> {
}
const DEPRECATED_KEYS: &[&str] = &[
"allow_check_for_updates",
"allow_moderators_to_view_redacted_content",
"allow_sticky_events",
"cache_capacity",
"conduit_cache_capacity_modifier",
"max_concurrent_requests",
"otlp_protocol",
"request_ip_source",
"require_auth_for_profile_requests",
"roomid_spacehierarchy_cache_capacity",
"unredacted_content_max_age_secs",
"well_known_client",
"well_known_server",
"well_known_support_page",
@@ -2999,6 +3005,9 @@ const DEPRECATED_KEYS: &[&str] = &[
"well_known_support_email",
"well_known_support_mxid",
"well_known.rtc_focus_server_urls",
"allow_sticky_events",
"allow_moderators_to_view_redacted_content",
"unredacted_content_max_age_secs",
];
impl Config {
@@ -3129,41 +3138,41 @@ fn default_database_backups_to_keep() -> i16 { 1 }
fn default_db_write_buffer_capacity_mb() -> f64 { 48.0 + parallelism_scaled_f64(4.0) }
fn default_db_cache_capacity_mb() -> f64 { 512.0 + parallelism_scaled_f64(512.0) }
fn default_db_cache_capacity_mb() -> f64 { 128.0 + parallelism_scaled_f64(64.0) }
fn default_pdu_cache_capacity() -> u32 { parallelism_scaled_u32(50_000).saturating_add(100_000) }
fn default_pdu_cache_capacity() -> u32 { parallelism_scaled_u32(10_000).saturating_add(100_000) }
fn default_cache_capacity_modifier() -> f64 { 1.0 }
fn default_auth_chain_cache_capacity() -> u32 {
parallelism_scaled_u32(50_000).saturating_add(100_000)
parallelism_scaled_u32(10_000).saturating_add(100_000)
}
fn default_shorteventid_cache_capacity() -> u32 {
parallelism_scaled_u32(100_000).saturating_add(100_000)
parallelism_scaled_u32(50_000).saturating_add(100_000)
}
fn default_eventidshort_cache_capacity() -> u32 {
parallelism_scaled_u32(50_000).saturating_add(100_000)
parallelism_scaled_u32(25_000).saturating_add(100_000)
}
fn default_eventid_pdu_cache_capacity() -> u32 {
parallelism_scaled_u32(50_000).saturating_add(100_000)
parallelism_scaled_u32(25_000).saturating_add(100_000)
}
fn default_shortstatekey_cache_capacity() -> u32 {
parallelism_scaled_u32(100_000).saturating_add(100_000)
parallelism_scaled_u32(10_000).saturating_add(100_000)
}
fn default_statekeyshort_cache_capacity() -> u32 {
parallelism_scaled_u32(50_000).saturating_add(100_000)
parallelism_scaled_u32(10_000).saturating_add(100_000)
}
fn default_servernameevent_data_cache_capacity() -> u32 {
parallelism_scaled_u32(100_000).saturating_add(100_000)
parallelism_scaled_u32(100_000).saturating_add(500_000)
}
fn default_stateinfo_cache_capacity() -> u32 { parallelism_scaled_u32(500).clamp(100, 12000) }
fn default_stateinfo_cache_capacity() -> u32 { parallelism_scaled_u32(100) }
fn default_dns_cache_entries() -> u32 { 32768 }
@@ -3386,20 +3395,15 @@ fn default_admin_log_capture() -> String {
fn default_admin_room_tag() -> String { "m.server_notice".to_owned() }
#[must_use]
#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
pub fn parallelism_scaled_f64(val: f64) -> f64 { val * (sys::available_parallelism() as f64) }
fn parallelism_scaled_f64(val: f64) -> f64 { val * (sys::available_parallelism() as f64) }
#[must_use]
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
pub fn parallelism_scaled_u32(val: u32) -> u32 {
val.saturating_mul(sys::available_parallelism() as u32)
fn parallelism_scaled_u32(val: u32) -> u32 {
let val = val.try_into().expect("failed to cast u32 to usize");
parallelism_scaled(val).try_into().unwrap_or(u32::MAX)
}
#[must_use]
pub fn parallelism_scaled(val: usize) -> usize {
val.saturating_mul(sys::available_parallelism())
}
fn parallelism_scaled(val: usize) -> usize { val.saturating_mul(sys::available_parallelism()) }
fn default_db_pool_workers() -> usize {
sys::available_parallelism()
-6
View File
@@ -21,7 +21,6 @@ pub struct Instruments {
pub sender_transaction_duration: Histogram<f64>,
pub sender_events: Counter<u64>,
pub sender_events_queued: Counter<u64>,
pub sender_panics: Counter<u64>,
pub media_write_size: Histogram<u64>,
pub media_read_size: Histogram<u64>,
@@ -107,11 +106,6 @@ impl Instruments {
.with_description("PDUs and EDUs queued for sending")
.with_unit("{event}")
.build(),
sender_panics: meter
.u64_counter("continuwuity.sender.panics")
.with_description("Panics caught in outgoing transactions and sender workers")
.with_unit("{panic}")
.build(),
media_write_size: size(
"continuwuity.media.write.size",
-3
View File
@@ -5,7 +5,6 @@ pub type DigestOut = [u8; 256 / 8];
/// Sha256 hash (input gather joined by 0xFF bytes)
#[must_use]
#[tracing::instrument(skip(inputs), level = "trace")]
#[allow(clippy::unnecessary_fallible_conversions)]
pub fn delimited<'a, T, I>(mut inputs: I) -> DigestOut
where
I: Iterator<Item = T> + 'a,
@@ -26,7 +25,6 @@ where
/// Sha256 hash (input gather)
#[must_use]
#[tracing::instrument(skip(inputs), level = "trace")]
#[allow(clippy::unnecessary_fallible_conversions)]
pub fn concat<'a, T, I>(inputs: I) -> DigestOut
where
I: Iterator<Item = T> + 'a,
@@ -45,7 +43,6 @@ where
#[inline]
#[must_use]
#[tracing::instrument(skip(input), level = "trace")]
#[allow(clippy::unnecessary_fallible_conversions)]
pub fn hash<T>(input: T) -> DigestOut
where
T: AsRef<[u8]>,
+1 -1
View File
@@ -29,7 +29,7 @@ fn descriptor_cf_options(
let mut opts = set_table_options(opts, &desc, cache)?;
opts.set_min_write_buffer_number(1);
opts.set_max_write_buffer_number(3);
opts.set_max_write_buffer_number(2);
opts.set_write_buffer_size(desc.write_size);
opts.set_target_file_size_base(desc.file_size);
-4
View File
@@ -232,10 +232,6 @@ pub(super) static MAPS: &[Descriptor] = &[
name: "roomsynctoken_shortstatehash",
..descriptor::DROPPED
},
Descriptor {
name: "roomts_pduid",
..descriptor::SEQUENTIAL_SMALL
},
Descriptor {
name: "roomuserdataid_accountdata",
..descriptor::RANDOM_SMALL
+8 -33
View File
@@ -77,7 +77,6 @@ full = [
]
brotli_compression = [
"conduwuit-api/brotli_compression",
"conduwuit-core/brotli_compression",
"conduwuit-router/brotli_compression",
"conduwuit-service/brotli_compression",
@@ -89,11 +88,10 @@ direct_tls = [
"conduwuit-router/direct_tls"
]
element_hacks = [
"conduwuit-api/element_hacks",
"conduwuit-service/element_hacks",
"conduwuit-router/element_hacks",
]
gzip_compression = [
"conduwuit-api/gzip_compression",
"conduwuit-core/gzip_compression",
"conduwuit-router/gzip_compression",
"conduwuit-service/gzip_compression",
@@ -102,21 +100,21 @@ hardened_malloc = [
"conduwuit-core/hardened_malloc",
]
http3 = [
"reqwest/http3"
"conduwuit-service/http3"
]
ring = [
"rustls/ring"
"rustls/ring",
]
aws_lc_rs = [
"rustls/aws_lc_rs",
"dep:aws-lc-rs"
]
io_uring = [
"conduwuit-database/io_uring",
"conduwuit-service/io_uring",
]
jemalloc = [
"conduwuit-core/jemalloc",
"conduwuit-database/jemalloc",
"conduwuit-service/jemalloc",
]
jemalloc_prof = [
"conduwuit-core/jemalloc_prof",
@@ -145,19 +143,13 @@ perf_measurements = [
release_max_log_level = [
"tracing/max_level_trace",
"tracing/release_max_level_info",
"log/max_level_trace",
"log/release_max_level_info",
"conduwuit-admin/release_max_log_level",
"conduwuit-api/release_max_log_level",
"conduwuit-core/release_max_log_level",
"conduwuit-database/release_max_log_level",
"conduwuit-router/release_max_log_level",
"conduwuit-service/release_max_log_level",
]
sentry_telemetry = [
"dep:sentry",
"dep:sentry-tracing",
"dep:sentry-tower",
"conduwuit-core/sentry_telemetry",
"conduwuit-router/sentry_telemetry",
]
@@ -180,9 +172,7 @@ url_preview = [
"conduwuit-service/url_preview",
]
zstd_compression = [
"conduwuit-api/zstd_compression",
"conduwuit-core/zstd_compression",
"conduwuit-database/zstd_compression",
"conduwuit-router/zstd_compression",
"conduwuit-service/zstd_compression",
]
@@ -192,10 +182,10 @@ conduwuit_mods = [
bindgen-static = [
# "bindgen/static"
# "clang-sys/static"
"conduwuit-database/bindgen-static"
"conduwuit-service/bindgen-static"
]
bindgen-runtime = [
"conduwuit-database/bindgen-runtime"
"conduwuit-service/bindgen-runtime"
]
[build-dependencies]
@@ -203,10 +193,7 @@ bindgen-runtime = [
# clang-sys = {version = "1", default-features = false}
[dependencies]
conduwuit-admin.workspace = true
conduwuit-api.workspace = true
conduwuit-core.workspace = true
conduwuit-database.workspace = true
conduwuit-router.workspace = true
conduwuit-service.workspace = true
conduwuit-build-metadata.workspace = true
@@ -218,7 +205,6 @@ dtor.workspace = true
console-subscriber.optional = true
console-subscriber.workspace = true
const-str.workspace = true
log.workspace = true
opentelemetry.workspace = true
opentelemetry-otlp.optional = true
opentelemetry-otlp.workspace = true
@@ -226,14 +212,10 @@ opentelemetry-prometheus.workspace = true
opentelemetry-semantic-conventions.workspace = true
opentelemetry_sdk.workspace = true
prometheus.workspace = true
sentry-tower.optional = true
sentry-tower.workspace = true
sentry-tracing.optional = true
sentry-tracing.workspace = true
sentry.optional = true
sentry.workspace = true
tokio-metrics.optional = true
tokio-metrics.workspace = true
tokio.workspace = true
tracing-flame.optional = true
tracing-flame.workspace = true
@@ -243,14 +225,7 @@ tracing-subscriber.workspace = true
tracing.workspace = true
tracing-journald = { workspace = true, optional = true }
parking_lot.workspace = true
reqwest = { workspace = true, default-features = false }
rustls = { workspace = true, default-features = false }
aws-lc-rs = { version = "1.19.0", default-features = false, optional = true }
[target.'cfg(all(not(target_env = "msvc"), target_os = "linux"))'.dependencies]
hardened_malloc-rs.workspace = true
hardened_malloc-rs.optional = true
[lints]
workspace = true
+4 -4
View File
@@ -8,7 +8,7 @@ use axum::{
extract::State,
response::{IntoResponse, Response},
};
use conduwuit::{Result, debug_warn, err, error, info, trace};
use conduwuit::{Result, debug, debug_error, debug_warn, err, error, trace};
use conduwuit_service::Services;
use futures::FutureExt;
use http::{Method, StatusCode, Uri};
@@ -102,11 +102,11 @@ fn handle_result(method: &Method, uri: &Uri, result: Response) -> Result<Respons
let reason = status.canonical_reason().unwrap_or("Unknown Reason");
if status.is_server_error() {
info!(%method, %uri, "{code} {reason}");
error!(%method, %uri, "{code} {reason}");
} else if status.is_client_error() {
info!(%method, %uri, "{code} {reason}");
debug_error!(%method, %uri, "{code} {reason}");
} else if status.is_redirection() {
trace!(%method, %uri, "{code} {reason}");
debug!(%method, %uri, "{code} {reason}");
} else {
trace!(%method, %uri, "{code} {reason}");
}
+12
View File
@@ -16,6 +16,12 @@ crate-type = [
]
[features]
bindgen-runtime = [
"conduwuit-database/bindgen-runtime"
]
bindgen-static = [
"conduwuit-database/bindgen-static"
]
brotli_compression = [
"conduwuit-core/brotli_compression",
"reqwest/brotli",
@@ -29,6 +35,9 @@ gzip_compression = [
"conduwuit-core/gzip_compression",
"reqwest/gzip",
]
http3 = [
"reqwest/http3"
]
io_uring = [
"conduwuit-database/io_uring",
]
@@ -122,6 +131,9 @@ lettre.workspace = true
resolvematrix.workspace = true
serde_urlencoded.workspace = true
openidconnect.workspace = true
# We use this to set TLS backend features on the outdated reqwest bundled by openidconnect,
# which is necessary to call `danger_accept_invalid_certs`.
reqwest_0_12.workspace = true
rand.workspace = true
[target.'cfg(all(unix, target_os = "linux"))'.dependencies]
+1 -1
View File
@@ -111,7 +111,7 @@ impl crate::Service for Service {
.local_address(url_preview_bind_addr)
.timeout(Duration::from_secs(config.url_preview_timeout))
.redirect(redirect::Policy::limited(3))
.user_agent(url_preview_user_agent.replace("<version>", conduwuit::version_ua()))
.user_agent(url_preview_user_agent)
.build()?,
external_resource: base(config, reqwest_dns_resolver.clone())?
-7
View File
@@ -223,8 +223,6 @@ impl Data {
x => x,
};*/
// TODO(nex): Oh my god this is awful when can we kill it
let title = match values
.next()
.and_then(|b| String::from_utf8(b.to_vec()).ok())
@@ -323,11 +321,6 @@ impl Data {
video_height,
audio,
audio_size,
..Default::default() /* TODO(nex,silversys): This weird manual field
* de/serialisation just needs replacing with JSON storage to
* avoid this mess. This is consequently intentionally
* dropping information added later (like image alt text).
* I'll do it later. */
})
}
}
+46 -231
View File
@@ -7,9 +7,8 @@
use std::time::SystemTime;
use conduwuit::{Err, Result, debug, err, info, utils::response::LimitReadExt};
use conduwuit::{Err, Result, debug, err, utils::response::LimitReadExt};
use ipaddress::IPAddress;
use lettre::message::header::ContentType;
use ruma::OwnedMxcUri;
use serde::Serialize;
use url::Url;
@@ -17,54 +16,20 @@ use url::Url;
use super::Service;
use crate::media::mxc::Mxc;
#[derive(Serialize, Default, Debug)]
#[derive(Serialize, Default)]
pub struct UrlPreviewData {
// From https://ogp.me/
/// The title of your object as it should appear within the graph
/// "The Rock".
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:title"))]
pub title: Option<String>,
/// The type of your object, e.g., "video.movie".
#[serde(skip_serializing_if = "String::is_empty", rename(serialize = "og:type"))]
pub site_type: String,
/// The canonical URL of your object that will be used as its permanent ID
/// in the graph.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:url"))]
pub url: Option<String>,
/// A one to two sentence description of your object.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:description"))]
pub description: Option<String>,
/// The word that appears before this object's title in a sentence.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:determiner"))]
pub determiner: Option<String>,
/// The locale these tags are marked up in
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:locale"))]
pub locale: Option<String>,
/// If your object is part of a larger web site, the name which should be
/// displayed for the overall site.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:site_name"))]
pub site_name: Option<String>,
/// An image URL which should represent your object within the graph.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image"))]
pub image: Option<String>,
/// A MIME type for this image.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:type"))]
pub image_type: Option<ContentType>,
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "matrix:image:size"))]
pub image_size: Option<usize>,
/// The number of pixels wide.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:width"))]
pub image_width: Option<u32>,
/// The number of pixels high.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:height"))]
pub image_height: Option<u32>,
/// A description of what is in the image (not a caption).
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:alt"))]
pub image_alt: Option<String>,
/// A URL to a video file that complements this object.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video"))]
pub video: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "matrix:video:size"))]
@@ -73,11 +38,6 @@ pub struct UrlPreviewData {
pub video_width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:height"))]
pub video_height: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:type"))]
pub video_type: Option<ContentType>,
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:alt"))]
pub video_alt: Option<String>,
/// A URL to an audio file to accompany this object.
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:audio"))]
pub audio: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "matrix:audio:size"))]
@@ -140,9 +100,7 @@ impl Service {
}
let Some(content_type) = response.headers().get(reqwest::header::CONTENT_TYPE) else {
return Err!(Request(Unknown(
"Response contains unknown, invalid, or missing Content-Type header"
)));
return Err!(Request(Unknown("Unknown or invalid Content-Type header")));
};
let content_type = content_type
@@ -151,10 +109,12 @@ impl Service {
let data = match content_type {
| html if html.starts_with("text/html") => self.download_html(url.as_str()).await?,
| img if img.starts_with("image/") => self.download_image(url.as_str()).await?,
| video if video.starts_with("video/") => self.download_video(url.as_str()).await?,
| audio if audio.starts_with("audio/") => self.download_audio(url.as_str()).await?,
| other => return Err!(Request(Unknown("Unsupported Content-Type: {other}"))),
| img if img.starts_with("image/") => self.download_image(url.as_str(), None).await?,
| video if video.starts_with("video/") =>
self.download_video(url.as_str(), None).await?,
| audio if audio.starts_with("audio/") =>
self.download_audio(url.as_str(), None).await?,
| _ => return Err!(Request(Unknown("Unsupported Content-Type"))),
};
self.set_url_preview(url.as_str(), &data).await?;
@@ -163,13 +123,17 @@ impl Service {
}
#[cfg(feature = "url_preview")]
pub async fn download_image(&self, url: &str) -> Result<UrlPreviewData> {
pub async fn download_image(
&self,
url: &str,
preview_data: Option<UrlPreviewData>,
) -> Result<UrlPreviewData> {
use conduwuit::utils::random_string;
use image::ImageReader;
use crate::media::mxc::Mxc;
let mut preview_data = UrlPreviewData::default();
let mut preview_data = preview_data.unwrap_or_default();
let response = self.services.client.url_preview.get(url).send().await?;
if !response.status().is_success() {
@@ -193,12 +157,13 @@ impl Service {
.ok_or_else(|| err!(Request(Unknown("Downloaded file is not a supported image"))))?
.to_mime_type();
let (width, height) = reader.into_dimensions().map_err(|error| {
err!(Request(Unknown("Failed to read preview image dimensions: {error}")))
})?;
preview_data.image_width.get_or_insert(width);
preview_data.image_height.get_or_insert(height);
preview_data.image_type = content_type.parse().ok();
if preview_data.image_height.is_none() || preview_data.image_width.is_none() {
let (width, height) = reader.into_dimensions().map_err(|error| {
err!(Request(Unknown("Failed to read preview image dimensions: {error}")))
})?;
preview_data.image_width.get_or_insert(width);
preview_data.image_height.get_or_insert(height);
}
let mxc = Mxc {
server_name: self.services.globals.server_name(),
@@ -214,9 +179,12 @@ impl Service {
}
#[cfg(feature = "url_preview")]
#[tracing::instrument(skip(self))]
pub async fn download_video(&self, url: &str) -> Result<UrlPreviewData> {
let mut preview_data = UrlPreviewData::default();
pub async fn download_video(
&self,
url: &str,
preview_data: Option<UrlPreviewData>,
) -> Result<UrlPreviewData> {
let mut preview_data = preview_data.unwrap_or_default();
if self.services.globals.url_preview_allow_audio_video() {
let (url, size) = self.download_media(url).await?;
@@ -228,9 +196,12 @@ impl Service {
}
#[cfg(feature = "url_preview")]
#[tracing::instrument(skip(self))]
pub async fn download_audio(&self, url: &str) -> Result<UrlPreviewData> {
let mut preview_data = UrlPreviewData::default();
pub async fn download_audio(
&self,
url: &str,
preview_data: Option<UrlPreviewData>,
) -> Result<UrlPreviewData> {
let mut preview_data = preview_data.unwrap_or_default();
if self.services.globals.url_preview_allow_audio_video() {
let (url, size) = self.download_media(url).await?;
@@ -241,7 +212,6 @@ impl Service {
Ok(preview_data)
}
#[tracing::instrument(skip(self))]
pub async fn download_media(&self, url: &str) -> Result<(OwnedMxcUri, usize)> {
use conduwuit::utils::random_string;
use http::header::CONTENT_TYPE;
@@ -299,7 +269,6 @@ impl Service {
}
#[cfg(feature = "url_preview")]
#[tracing::instrument(skip(self))]
async fn download_html(&self, url: &str) -> Result<UrlPreviewData> {
use webpage::HTML;
@@ -317,75 +286,32 @@ impl Service {
.expect("u64 should fit in usize"),
)
.await?;
let Ok(html) = HTML::from_string(body.clone(), Some(url.to_owned()))
.inspect_err(|e| debug!(error=?e, "Failed to parse HTML data"))
else {
let Ok(html) = HTML::from_string(body.clone(), Some(url.to_owned())) else {
return Err!(Request(Unknown("Failed to parse HTML")));
};
let mut preview_data = UrlPreviewData::from(&html.opengraph);
debug!(parsed=?preview_data, deserialised=?html.opengraph, "Parsed opengraph data");
let mut preview_data = UrlPreviewData::default();
if let Some(obj) = html.opengraph.images.first() {
info!(asset_url=%obj.url, "Downloading preview image");
if let Ok(image_data) = self.download_image(&obj.url).await.inspect_err(
|e| info!(error=?e, asset_url=%obj.url, "Could not download URL preview image"),
) {
preview_data.image = image_data.image;
preview_data.image_size = image_data.image_size;
// Prioritise what the top-level site specified, falling back to
// detected params.
preview_data.image_width = preview_data.image_width.or(image_data.image_width);
preview_data.image_height = preview_data.image_height.or(image_data.image_height);
preview_data.image_type = preview_data.image_type.or(image_data.image_type);
}
preview_data = self.download_image(&obj.url, Some(preview_data)).await?;
}
if let Some(obj) = html.opengraph.videos.first() {
info!(asset_url=%obj.url, "Downloading preview video");
if let Ok(video_data) = self.download_video(&obj.url).await.inspect_err(
|e| info!(error=?e, asset_url=%obj.url, "Could not download URL preview video"),
) {
preview_data.video = video_data.video;
preview_data.video_size = video_data.video_size;
}
preview_data = self.download_video(&obj.url, Some(preview_data)).await?;
preview_data.video_width = obj.properties.get("width").and_then(|v| v.parse().ok());
preview_data.video_height = obj.properties.get("height").and_then(|v| v.parse().ok());
}
if let Some(obj) = html.opengraph.audios.first() {
info!(asset_url=%obj.url, "Downloading URL preview audio");
if let Ok(audio_data) = self.download_audio(&obj.url).await.inspect_err(
|e| info!(error=?e, asset_url=%obj.url, "Could not download URL preview audio"),
) {
preview_data.audio = audio_data.audio;
preview_data.audio_size = audio_data.audio_size;
}
preview_data = self.download_audio(&obj.url, Some(preview_data)).await?;
}
// Provide defaults for some params
preview_data.title = preview_data.title.or(html.title);
preview_data.description = preview_data.description.or(html.description);
preview_data.url = preview_data.url.or_else(|| Some(url.to_owned()));
let props = html.opengraph.properties;
// We remove og:image:*, og:video:*, and og:audio:* if their respective
// prior-level properties are not present.
// We do this here instead of in the From<> impl because we can't know
// if we'll be able to fetch the media to convert it to an mxc that
// point.
if preview_data.image.is_none() {
preview_data.image_type = None;
preview_data.image_alt = None;
preview_data.image_height = None;
preview_data.image_width = None;
}
if preview_data.video.is_none() {
preview_data.video_type = None;
preview_data.video_alt = None;
preview_data.video_height = None;
preview_data.video_width = None;
}
if preview_data.audio.is_none() {
preview_data.audio_size = None;
}
/* use OpenGraph title/description, but fall back to HTML if not
* available */
preview_data.title = props.get("title").cloned().or(html.title);
preview_data.description = props.get("description").cloned().or(html.description);
Ok(preview_data)
}
@@ -517,114 +443,3 @@ impl Service {
false
}
}
impl From<&webpage::Opengraph> for UrlPreviewData {
fn from(og: &webpage::Opengraph) -> Self {
Self {
title: og.properties.get("title").cloned(),
description: og.properties.get("description").cloned(),
site_type: og.og_type.clone(),
url: og.properties.get("url").cloned(),
determiner: og.properties.get("determiner").cloned(),
locale: og.properties.get("locale").cloned(),
site_name: og.properties.get("site_name").cloned(),
image_type: og
.images
.first()
.and_then(|img| img.properties.get("type").and_then(|t| t.parse().ok())),
image_width: og
.images
.first()
.and_then(|img| img.properties.get("width").and_then(|w| w.parse().ok())),
image_height: og
.images
.first()
.and_then(|img| img.properties.get("height").and_then(|h| h.parse().ok())),
image_alt: og
.images
.first()
.and_then(|img| img.properties.get("alt").cloned()),
video_type: og
.videos
.first()
.and_then(|vid| vid.properties.get("type"))
.and_then(|t| t.parse().ok()),
video_width: og
.videos
.first()
.and_then(|vid| vid.properties.get("width"))
.and_then(|w| w.parse().ok()),
video_height: og
.videos
.first()
.and_then(|vid| vid.properties.get("height"))
.and_then(|h| h.parse().ok()),
video_alt: og
.videos
.first()
.and_then(|vid| vid.properties.get("alt").cloned()),
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use webpage::HTML;
use super::UrlPreviewData;
#[test]
fn test_urlpreviewdata_parses_correctly() {
let html = r#"
<html>
<head>
<meta property="og:title" content="Test Title" />
<meta property="og:description" content="Test Description" />
<meta property="og:image" content="https://example.com/image.png" />
<meta property="og:url" content="https://example.com/index.html" />
<meta property="og:type" content="org.continuwuity.other" />
<meta property="og:site_name" content="Example Site" />
<meta property="og:image:alt" content="An example image" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:height" content="69" />
<meta property="og:image:width" content="420" />
<meta property="og:video" content="https://example.com/video.mp4" />
<meta property="og:audio" content="https://example.com/audio.ogg" />
</head>
<body></body>
</html>
"#;
let parsed_html =
HTML::from_string(html.to_owned(), Some("https://example.com".to_owned()))
.expect("Failed to parse HTML");
let data = UrlPreviewData::from(&parsed_html.opengraph);
assert_eq!(data.title, Some("Test Title".to_owned()));
assert_eq!(data.description, Some("Test Description".to_owned()));
assert_eq!(
data.image, None,
"UrlPreviewData should not inherit HTTP/S URL from URL preview"
);
assert_eq!(
data.video, None,
"UrlPreviewData should not inherit HTTP/S URL from URL preview"
);
assert_eq!(
data.audio, None,
"UrlPreviewData should not inherit HTTP/S URL from URL preview"
);
assert_eq!(data.url, Some("https://example.com/index.html".to_owned()));
assert_eq!(data.site_type, "org.continuwuity.other".to_owned());
assert_eq!(data.site_name, Some("Example Site".to_owned()));
assert_eq!(data.image_alt, Some("An example image".to_owned()));
assert_eq!(data.image_type, Some("image/png".parse().unwrap()));
assert_eq!(data.image_height, Some(69));
assert_eq!(data.image_width, Some(420));
assert_eq!(data.image_size, None, "*_size must be None when pulling from opengraph");
assert_eq!(data.video_size, None, "*_size must be None when pulling from opengraph");
assert_eq!(data.audio_size, None, "*_size must be None when pulling from opengraph");
}
}
-22
View File
@@ -18,28 +18,6 @@ impl Services {
},
);
self.gauge(
"continuwuity.sender.requests",
"Estimated outgoing requests stored as queued or in flight",
"{request}",
|services, observer| {
for (state, count) in services.sending.db.request_counts() {
if let Some(count) = count {
observer.observe(count, &[KeyValue::new("state", state)]);
}
}
},
);
self.gauge(
"continuwuity.sender.workers",
"Running sender workers",
"{worker}",
|services, observer| {
observe(observer, services.sending.workers_alive(), &[]);
},
);
self.gauge(
"continuwuity.federation.incoming.transactions.active",
"Incoming federation transactions currently being processed",
+3 -69
View File
@@ -18,11 +18,11 @@ use conduwuit::{
},
warn,
};
use database::{Database, Deserialized, Ignore, Json};
use database::{Deserialized, Ignore, Json};
use futures::{FutureExt, StreamExt, TryStreamExt};
use itertools::Itertools;
use ruma::{
MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedUserId, UserId,
OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedUserId, UserId,
api::OAuthClientScope,
events::{
AnyStrippedStateEvent, GlobalAccountDataEventType, StateEventType,
@@ -32,13 +32,9 @@ use ruma::{
push::Ruleset,
serde::{Raw, from_raw_json_value},
};
use serde::Deserialize;
use serde_json::{Value, value::to_raw_value};
use crate::{
Services, media, rooms,
rooms::{short::ShortStateHash, timeline::RawPduId},
};
use crate::{Services, media, rooms, rooms::short::ShortStateHash};
type MigrationFn =
for<'a> fn(&'a Services) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
@@ -180,36 +176,6 @@ migration!(stringify!($name) => $name)
// This is backwards compatible because old versions never read the
// account data entries it removes.
migration!(remove_replaced_account_data),
// This is backwards compatible because old versions ignore the
// `roomts_pduid` keyspace.
Migration(rooms::timeline::TIMESTAMP_INDEX_MIGRATION, |services| {
Box::pin(async {
let global = &services.db["global"];
if global
.exists(rooms::timeline::TIMESTAMP_INDEX_MIGRATION)
.await
.is_ok()
{
return Ok(());
}
let until = match global.get(ROOMTS_UNTIL).await.deserialized::<u64>() {
| Ok(until) => until,
| Err(_) => {
let until = services.globals.current_count()?;
global.insert(ROOMTS_UNTIL, until.to_be_bytes());
until
},
};
services
.server
.runtime()
.spawn(populate_roomts_pduid(services.db.clone(), until));
Ok(())
})
}),
]
};
@@ -995,38 +961,6 @@ async fn drop_server_signingkeys(services: &Services) -> Result<()> {
Ok(())
}
const ROOMTS_UNTIL: &str = "populate_roomts_pduid_until";
async fn populate_roomts_pduid(db: Arc<Database>, until: u64) {
#[derive(Deserialize)]
struct ExtractTs {
origin_server_ts: MilliSecondsSinceUnixEpoch,
}
info!("Starting background migration to index PDUs by timestamp.");
let start = Instant::now();
let roomts_pduid = db["roomts_pduid"].clone();
db["pduid_pdu"]
.raw_stream()
.ignore_err()
.ready_for_each(|(key, val)| {
let pdu_id = RawPduId::from(key);
if matches!(pdu_id.pdu_count(), PduCount::Normal(count) if count > until) {
return;
}
if let Ok(pdu) = serde_json::from_slice::<ExtractTs>(val) {
roomts_pduid
.insert(&rooms::timeline::roomts_key(&pdu_id, pdu.origin_server_ts), []);
}
})
.await;
db["global"].insert(rooms::timeline::TIMESTAMP_INDEX_MIGRATION, []);
db["global"].remove(ROOMTS_UNTIL);
info!(elapsed = ?start.elapsed(), "Finished indexing PDUs by timestamp.");
}
async fn fix_oauth_scope_names(services: &Services) -> Result {
let db = &services.db;
let userdeviceid_oauthsessioninfo = db["userdeviceid_oauthsessioninfo"].clone();
-2
View File
@@ -13,8 +13,6 @@ mod migrations;
mod service;
pub mod services;
pub mod state;
#[cfg(test)]
mod test_utils;
pub mod account_data;
pub mod admin;
@@ -1,10 +1,14 @@
use conduwuit::PduEvent;
use std::sync::Arc;
use conduwuit::{
Config, PduEvent, Server,
log::{Log, LogLevelReloadHandles},
metrics::Metrics,
};
use futures::StreamExt;
use ruma::{RoomId, RoomVersionId, events::StateEventType};
use serde_json::json;
use crate::test_utils::test_services;
#[tokio::test]
async fn admitted_joins_do_not_disappear_after_a_merge() {
use conduwuit::matrix::PartialPdu;
@@ -16,7 +20,7 @@ async fn admitted_joins_do_not_disappear_after_a_merge() {
},
};
let (services, path, _database) = test_services().await;
let (services, path) = test_services().await;
let alice = UserId::parse("@alice:test.example").unwrap();
let bob = UserId::parse("@bob:bob.example").unwrap();
let charlie = UserId::parse("@charlie:charlie.example").unwrap();
@@ -208,6 +212,35 @@ async fn admitted_joins_do_not_disappear_after_a_merge() {
assert_eq!(results, [true, true], "both joined users must be able to send messages");
}
async fn test_services() -> (Arc<crate::Services>, std::path::PathBuf) {
let _ = rustls::crypto::ring::default_provider().install_default();
let path = std::env::temp_dir()
.join(format!("continuwuity-shared-state-{:016x}", rand::random::<u64>()));
std::fs::create_dir(&path).unwrap();
let config: Config = serde_json::from_value(json!({
"server_name": "test.example",
"database_path": path,
"db_cache_capacity_mb": 16.0,
"rocksdb_write_buffer_capacity_mb": 4.0,
"allow_local_presence": false,
"dns_servers": ["127.0.0.1"],
"allow_invalid_tls_certificates_yes_i_know_what_the_fuck_i_am_doing_with_this_and_i_know_this_is_insecure": true
}))
.unwrap();
let runtime = tokio::runtime::Handle::current();
let server = Arc::new(Server::new(
config,
Some(runtime.clone()),
Log {
reload: LogLevelReloadHandles::default(),
capture: Arc::default(),
},
Metrics::new(Some(runtime), opentelemetry::global::meter("test"), None),
));
let services = crate::Services::build(server).await.unwrap();
(services, path)
}
fn cache_key(
services: &crate::Services,
server: &ruma::ServerName,
+1 -1
View File
@@ -349,7 +349,7 @@ impl super::Service {
// Insert pdu
self.db
.append_pdu(&pdu_id, pdu, room_id, &pdu_json, count2, pdu.origin_server_ts())
.append_pdu(&pdu_id, pdu, room_id, &pdu_json, count2)
.await;
self.progress_extremities(pdu)
.await
+1 -2
View File
@@ -225,8 +225,7 @@ impl super::Service {
.into();
// Insert pdu
self.db
.prepend_backfill_pdu(&pdu_id, &event_id, &value, pdu.origin_server_ts());
self.db.prepend_backfill_pdu(&pdu_id, &event_id, &value);
drop(insert_lock);
if pdu.kind == TimelineEventType::RoomMessage {
+1 -95
View File
@@ -12,9 +12,7 @@ use database::{Database, Deserialized, Json, KeyVal, Map};
use futures::{
FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt, future::select_ok, pin_mut,
};
use ruma::{
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedUserId, RoomId, api::Direction,
};
use ruma::{CanonicalJsonObject, EventId, OwnedUserId, RoomId, api::Direction};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
@@ -27,7 +25,6 @@ pub(super) struct Data {
pduid_pdu: Arc<Map>,
pduid_unredactedcontent: Arc<Map>,
roomid_lasttimelinecount: Arc<Map>,
roomts_pduid: Arc<Map>,
userroomid_highlightcount: Arc<Map>,
userroomid_notificationcount: Arc<Map>,
pub(super) db: Arc<Database>,
@@ -55,7 +52,6 @@ impl Data {
pduid_pdu: db["pduid_pdu"].clone(),
pduid_unredactedcontent: db["pduid_unredactedcontent"].clone(),
roomid_lasttimelinecount: db["roomid_lasttimelinecount"].clone(),
roomts_pduid: db["roomts_pduid"].clone(),
userroomid_highlightcount: db["userroomid_highlightcount"].clone(),
userroomid_notificationcount: db["userroomid_notificationcount"].clone(),
db: args.db.clone(),
@@ -217,12 +213,10 @@ impl Data {
room_id: &RoomId,
json: &CanonicalJsonObject,
count: PduCount,
index_ts: MilliSecondsSinceUnixEpoch,
) {
debug_assert!(matches!(count, PduCount::Normal(_)), "PduCount not Normal");
self.pduid_pdu.raw_put(pdu_id, Json(json));
self.roomts_pduid.insert(&roomts_key(pdu_id, index_ts), []);
self.eventid_pduid.insert(pdu.event_id.as_bytes(), pdu_id);
self.eventid_outlierpdu.remove(pdu.event_id.as_bytes());
self.roomid_lasttimelinecount
@@ -234,10 +228,8 @@ impl Data {
pdu_id: &RawPduId,
event_id: &EventId,
json: &CanonicalJsonObject,
index_ts: MilliSecondsSinceUnixEpoch,
) {
self.pduid_pdu.raw_put(pdu_id, Json(json));
self.roomts_pduid.insert(&roomts_key(pdu_id, index_ts), []);
self.eventid_pduid.insert(event_id, pdu_id);
self.eventid_outlierpdu.remove(event_id);
}
@@ -330,49 +322,6 @@ impl Data {
.try_flatten_stream()
}
pub(super) fn pdus_by_timestamp<'a>(
&'a self,
room_id: &'a RoomId,
timestamp: MilliSecondsSinceUnixEpoch,
dir: Direction,
) -> impl Stream<Item = Result<PdusIterItem>> + Send + 'a {
self.services
.short
.get_shortroomid(room_id)
.map_err(move |e| err!(Request(NotFound("Room {room_id:?} not found: {e:?}"))))
.map_ok(move |shortroomid| {
let seek = |shorteventid| {
roomts_key(&PduId { shortroomid, shorteventid }.into(), timestamp)
};
let keys = match dir {
| Direction::Forward => self
.roomts_pduid
.raw_keys_from(&seek(PduCount::min()))
.left_stream(),
| Direction::Backward => self
.roomts_pduid
.rev_raw_keys_from(&seek(PduCount::max()))
.right_stream(),
};
keys.ready_try_take_while(move |key| {
Ok(key.starts_with(&shortroomid.to_be_bytes()))
})
.map_ok(roomts_key_to_pdu_id)
.and_then(async move |pdu_id| {
Ok((pdu_id.pdu_count(), self.get_pdu_from_id(&pdu_id).await?))
})
})
.try_flatten_stream()
}
pub(super) async fn timestamp_index_ready(&self) -> bool {
self.db["global"]
.exists(TIMESTAMP_INDEX_MIGRATION)
.await
.is_ok()
}
fn from_json_slice((pdu_id, pdu): KeyVal<'_>) -> Result<PdusIterItem> {
let pdu_id: RawPduId = pdu_id.into();
@@ -427,52 +376,9 @@ impl Data {
}
}
pub const TIMESTAMP_INDEX_MIGRATION: &str = "populate_roomts_pduid";
#[must_use]
pub fn roomts_key(pdu_id: &RawPduId, ts: MilliSecondsSinceUnixEpoch) -> Vec<u8> {
let count = pdu_id.pdu_count().into_signed().cast_unsigned() ^ (1 << 63);
[pdu_id.shortroomid(), u64::from(ts.get()).to_be_bytes(), count.to_be_bytes()].concat()
}
fn roomts_key_to_pdu_id(key: &[u8]) -> RawPduId {
let shortroomid = u64::from_be_bytes(key[0..8].try_into().expect("roomts key shortroomid"));
let count = u64::from_be_bytes(key[16..24].try_into().expect("roomts key count")) ^ (1 << 63);
PduId {
shortroomid,
shorteventid: PduCount::from_signed(count.cast_signed()),
}
.into()
}
//TODO: this is an ABA
fn increment(db: &Arc<Map>, key: &[u8]) {
let old = db.get_blocking(key);
let new = utils::increment(old.ok().as_deref());
db.insert(key, new);
}
#[cfg(test)]
mod tests {
use ruma::uint;
use super::*;
#[test]
fn roomts_key_orders_by_ts_then_count() {
let ids = [
(PduCount::Normal(9), uint!(1)),
(PduCount::Backfilled(-5), uint!(2)),
(PduCount::Normal(3), uint!(2)),
(PduCount::Normal(1), uint!(3)),
]
.map(|(count, ts)| (RawPduId::from(PduId { shortroomid: 7, shorteventid: count }), ts));
let keys = ids.map(|(id, ts)| roomts_key(&id, MilliSecondsSinceUnixEpoch(ts)));
assert!(keys.is_sorted());
for ((id, _), key) in ids.iter().zip(&keys) {
assert_eq!(roomts_key_to_pdu_id(key), *id);
}
}
}
+3 -17
View File
@@ -21,8 +21,8 @@ use conduwuit_core::{
};
use futures::{Future, Stream, TryStreamExt, pin_mut};
use ruma::{
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, RoomId,
api::Direction, events::room::encrypted::Relation,
CanonicalJsonObject, EventId, OwnedEventId, OwnedRoomId, RoomId,
events::room::encrypted::Relation,
};
use serde::Deserialize;
use serde_json::value::RawValue as RawJsonValue;
@@ -32,10 +32,7 @@ use tokio::{
};
use self::data::Data;
pub use self::{
create::pdu_fits,
data::{PdusIterItem, TIMESTAMP_INDEX_MIGRATION, roomts_key},
};
pub use self::{create::pdu_fits, data::PdusIterItem};
use crate::{
Dep, account_data, admin, appservice, config, globals, pusher, rooms, sending, server_keys,
sync, users,
@@ -310,17 +307,6 @@ impl Service {
self.pdus(room_id, None).ignore_err()
}
pub fn pdus_by_timestamp<'a>(
&'a self,
room_id: &'a RoomId,
timestamp: MilliSecondsSinceUnixEpoch,
dir: Direction,
) -> impl Stream<Item = Result<PdusIterItem>> + Send + 'a {
self.db.pdus_by_timestamp(room_id, timestamp, dir)
}
pub async fn timestamp_index_ready(&self) -> bool { self.db.timestamp_index_ready().await }
/// Reverse iteration starting after `until`.
#[tracing::instrument(skip(self), level = "debug")]
pub fn pdus_rev<'a>(
-28
View File
@@ -3,7 +3,6 @@ use std::{fmt::Debug, sync::Arc};
use conduwuit::{
Error, Result, at, utils,
utils::{ReadyExt, stream::TryIgnore},
warn,
};
use database::{Database, Deserialized, Map};
use futures::{Stream, StreamExt};
@@ -170,33 +169,6 @@ impl Data {
})
}
/// The oldest queued request of each destination, not yet in flight.
pub(super) async fn queued_heads(&self) -> Vec<(Destination, QueueItem)> {
let mut heads: Vec<(Destination, QueueItem)> = Vec::new();
self.servernameevent_data
.raw_stream()
.ignore_err()
.ready_for_each(|(key, val)| match parse_servercurrentevent(key, val) {
| Ok((dest, event)) =>
if heads.last().is_none_or(|(last, _)| *last != dest) {
heads.push((dest, (key.to_vec(), event)));
},
| Err(e) => warn!(?key, "invalid queued request key: {e}"),
})
.await;
heads
}
/// Estimated number of requests waiting to be sent and in flight.
pub(crate) fn request_counts(&self) -> [(&'static str, Option<u64>); 2] {
[
("queued", &self.servernameevent_data),
("active", &self.servercurrentevent_data),
]
.map(|(state, map)| (state, map.property_integer(c"rocksdb.estimate-num-keys").ok()))
}
pub(super) fn set_latest_educount(&self, server_name: &ServerName, last_count: u64) {
self.servername_educount.raw_put(server_name, last_count);
}
+25 -107
View File
@@ -3,19 +3,12 @@ mod appservice;
mod data;
mod dest;
mod sender;
#[cfg(test)]
mod tests;
use std::{
collections::HashMap,
fmt::Debug,
hash::{DefaultHasher, Hash, Hasher},
iter::once,
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
time::{Duration, Instant},
sync::Arc,
};
use async_trait::async_trait;
@@ -26,7 +19,7 @@ use conduwuit::{
utils::{ReadyExt, TryReadyExt, available_parallelism, math::usize_from_u64_truncated},
warn,
};
use futures::{FutureExt, Stream, StreamExt, stream::FuturesUnordered};
use futures::{FutureExt, Stream, StreamExt};
use ruma::{
OwnedServerName, RoomId, ServerName, UserId,
api::{
@@ -34,7 +27,7 @@ use ruma::{
federation::authentication::ServerSignatures, path_builder::PathBuilder,
},
};
use tokio::{task, task::JoinSet, time::sleep};
use tokio::{task, task::JoinSet};
use self::data::Data;
pub use self::{
@@ -56,7 +49,6 @@ pub struct Service {
server: Arc<Server>,
services: Services,
channels: Vec<(loole::Sender<Msg>, loole::Receiver<Msg>)>,
workers_alive: AtomicUsize,
}
struct Services {
@@ -95,8 +87,6 @@ pub type EduVec = SmallVec<[EduBuf; EDU_VEC_CAP]>;
const EDU_BUF_CAP: usize = 128;
const EDU_VEC_CAP: usize = 1;
const WORKER_RESTART_DELAY: Duration = Duration::from_millis(2500);
const WORKER_RESTART_DELAY_MAX: Duration = Duration::from_secs(30);
#[async_trait]
impl crate::Service for Service {
@@ -121,67 +111,36 @@ impl crate::Service for Service {
federation: args.depend::<federation::Service>("federation"),
},
channels: (0..num_senders).map(|_| loole::unbounded()).collect(),
workers_alive: AtomicUsize::new(0),
}))
}
async fn worker(self: Arc<Self>) -> Result {
if self.server.config.startup_netburst {
self.dispatch_queued(None).await;
}
let num_senders = self.channels.len();
let mut senders = JoinSet::new();
let mut shards = HashMap::new();
let mut started = vec![Instant::now(); num_senders];
let mut failures = vec![0_u32; num_senders];
let mut restarts = FuturesUnordered::new();
for id in 0..num_senders {
self.spawn_sender(&mut senders, &mut shards, id, false);
}
loop {
tokio::select! {
Some(ret) = senders.join_next_with_id() => {
self.workers_alive.fetch_sub(1, Ordering::Relaxed);
let (task_id, panicked) = match &ret {
| Ok((task_id, _)) => (*task_id, false),
| Err(error) => (error.id(), error.is_panic()),
};
let shard = shards.remove(&task_id).expect("sender worker has a shard");
if panicked {
self.server
.metrics
.instruments
.sender_panics
.add(1, &[KeyValue::new("scope", "worker")]);
}
if self.channels[shard].1.is_closed() || !self.server.running() {
debug!(shard, ?ret, "sender worker finished");
continue;
}
// A worker that ran a while before failing starts over from
// the shortest delay.
failures[shard] = if started[shard].elapsed() > WORKER_RESTART_DELAY_MAX {
1
let mut senders =
self.channels
.iter()
.enumerate()
.fold(JoinSet::new(), |mut joinset, (id, _)| {
let self_ = self.clone();
let worker = self_.sender(id);
let worker = if self.unconstrained() {
task::unconstrained(worker).boxed()
} else {
failures[shard].saturating_add(1)
worker.boxed()
};
let delay = restart_delay(failures[shard]);
error!(shard, ?ret, ?delay, "sender worker unexpectedly finished, restarting");
restarts.push(sleep(delay).map(move |()| shard));
let runtime = self.server.runtime();
let _abort = joinset.spawn_on(worker, runtime);
joinset
});
while let Some(ret) = senders.join_next_with_id().await {
match ret {
| Ok((id, error)) => {
debug!(?id, ?error, "sender worker finished");
},
Some(shard) = restarts.next() => {
if !self.channels[shard].1.is_closed() {
started[shard] = Instant::now();
self.spawn_sender(&mut senders, &mut shards, shard, true);
}
| Err(error) => {
error!(id = ?error.id(), ?error, "sender worker failed");
},
() = self.server.until_shutdown(), if !restarts.is_empty() => restarts.clear(),
else => break,
}
}
@@ -202,25 +161,6 @@ impl crate::Service for Service {
}
impl Service {
fn spawn_sender(
self: &Arc<Self>,
senders: &mut JoinSet<Result>,
shards: &mut HashMap<task::Id, usize>,
id: usize,
restarted: bool,
) {
let worker = self.clone().sender(id, restarted);
let worker = if crate::Service::unconstrained(&**self) {
task::unconstrained(worker).boxed()
} else {
worker.boxed()
};
let handle = senders.spawn_on(worker, self.server.runtime());
shards.insert(handle.id(), id);
self.workers_alive.fetch_add(1, Ordering::Relaxed);
}
#[tracing::instrument(skip(self, pdu_id, user, pushkey), level = "debug")]
pub fn send_pdu_push(&self, pdu_id: &RawPduId, user: &UserId, pushkey: String) -> Result {
let dest = Destination::Push(user.to_owned(), pushkey);
@@ -540,24 +480,10 @@ impl Service {
sender.send(msg).map_err(|e| err!("{e}"))
}
/// Hands the oldest unsent request of each destination to its worker, so
/// a backlog with nothing in flight is sent without waiting for a new
/// event. Bypasses `dispatch` as these requests were already counted.
async fn dispatch_queued(&self, shard: Option<usize>) {
for (dest, (queue_id, event)) in self.db.queued_heads().await {
let id = self.shard_id(&dest);
if shard.is_none_or(|shard| shard == id) {
self.channels[id].0.send(Msg { dest, event, queue_id }).ok();
}
}
}
pub fn queue_depth(&self) -> usize {
self.channels.iter().map(|(sender, _)| sender.len()).sum()
}
pub fn workers_alive(&self) -> usize { self.workers_alive.load(Ordering::Relaxed) }
pub(super) fn shard_id(&self, dest: &Destination) -> usize {
if self.channels.len() <= 1 {
return 0;
@@ -593,11 +519,3 @@ fn num_senders(args: &crate::Args<'_>) -> usize {
worker_count.clamp(MIN_SENDERS, max_senders)
}
}
/// Delay before restarting a worker after its `failures`-th consecutive
/// failure, doubling each time.
fn restart_delay(failures: u32) -> Duration {
WORKER_RESTART_DELAY
.saturating_mul(2_u32.saturating_pow(failures.saturating_sub(1)))
.min(WORKER_RESTART_DELAY_MAX)
}
+18 -56
View File
@@ -1,7 +1,6 @@
use std::{
collections::{BTreeMap, HashMap, HashSet},
fmt::Debug,
panic::AssertUnwindSafe,
sync::{
Arc,
atomic::{AtomicU64, AtomicUsize, Ordering},
@@ -56,9 +55,6 @@ use serde_json::value::{RawValue as RawJsonValue, to_raw_value};
use super::{Destination, EduBuf, EduVec, Msg, SendingEvent, Service, data::QueueItem};
#[cfg(test)]
mod tests;
#[derive(Debug)]
enum TransactionStatus {
Running,
@@ -82,18 +78,12 @@ pub const EDU_LIMIT: usize = 100;
impl Service {
#[tracing::instrument(skip(self), level = "debug")]
pub(super) async fn sender(self: Arc<Self>, id: usize, restarted: bool) -> Result {
pub(super) async fn sender(self: Arc<Self>, id: usize) -> Result {
let mut statuses: CurTransactionStatus = CurTransactionStatus::new();
let mut futures: SendingFutures<'_> = FuturesUnordered::new();
// The previous worker of this shard may have dropped requests it had
// taken from the channel.
if restarted {
self.dispatch_queued(Some(id)).await;
}
info!(shard = id, "Running startup netburst");
self.startup_netburst(id, restarted, &mut futures, &mut statuses)
self.startup_netburst(id, &mut futures, &mut statuses)
.boxed()
.await;
info!(shard = id, "Finished startup netburst");
@@ -152,9 +142,6 @@ impl Service {
) {
match response {
| Ok(dest) => self.handle_response_ok(&dest, futures, statuses).await,
// A bug on our side, not the remote's fault: retry with backoff
// without marking the remote unhealthy.
| Err((dest, e)) if e.is_panic() => Self::handle_response_err(dest, statuses, &e),
| Err((dest, e)) => {
if let Destination::Federation(dest) = &dest {
if self.services.federation.should_mark_stale(&e)
@@ -287,8 +274,7 @@ impl Service {
}
/// Performs startup netburst, which sends any previously queued up but not
/// yet sent requests immediately. A restarted worker always resends its
/// shard's requests in flight.
/// yet sent requests immediately.
#[tracing::instrument(
name = "netburst",
level = "debug",
@@ -299,7 +285,6 @@ impl Service {
async fn startup_netburst<'a>(
&'a self,
id: usize,
restarted: bool,
futures: &mut SendingFutures<'a>,
statuses: &mut CurTransactionStatus,
) {
@@ -330,12 +315,8 @@ impl Service {
}
}
if !restarted && !self.server.config.startup_netburst {
return;
}
for (dest, events) in txns {
if !events.is_empty() {
if self.server.config.startup_netburst && !events.is_empty() {
statuses.insert(dest.clone(), TransactionStatus::Running);
futures.push(self.send_events(dest, events));
}
@@ -791,7 +772,6 @@ impl Service {
});
let start = Instant::now();
let panic_dest = dest.clone();
let send = match dest {
| Destination::Federation(server) =>
self.send_events_dest_federation(server, events).boxed(),
@@ -800,42 +780,24 @@ impl Service {
self.send_events_dest_push(user_id, pushkey, events).boxed(),
};
AssertUnwindSafe(send)
.catch_unwind()
.map(move |result| {
result.unwrap_or_else(|panic| {
let error = Error::from_panic(panic);
error!(dest = ?panic_dest, ?error, "Panicked while sending transaction");
self.server
.metrics
.instruments
.sender_panics
.add(1, &[KeyValue::new("scope", "transaction")]);
Err((panic_dest, Box::new(error)))
})
})
.inspect(move |result| {
let instruments = &self.server.metrics.instruments;
let outcome =
KeyValue::new("outcome", if result.is_ok() { "success" } else { "failure" });
instruments
.sender_transaction_duration
.record(start.elapsed().as_secs_f64(), &[
send.inspect(move |result| {
let instruments = &self.server.metrics.instruments;
let outcome =
KeyValue::new("outcome", if result.is_ok() { "success" } else { "failure" });
instruments
.sender_transaction_duration
.record(start.elapsed().as_secs_f64(), &[destination.clone(), outcome.clone()]);
for (kind, count) in [("pdu", pdus), ("edu", edus)] {
if count > 0 {
instruments.sender_events.add(count, &[
destination.clone(),
KeyValue::new("kind", kind),
outcome.clone(),
]);
for (kind, count) in [("pdu", pdus), ("edu", edus)] {
if count > 0 {
instruments.sender_events.add(count, &[
destination.clone(),
KeyValue::new("kind", kind),
outcome.clone(),
]);
}
}
})
.boxed()
}
})
.boxed()
}
#[tracing::instrument(
-86
View File
@@ -1,86 +0,0 @@
use std::iter::once;
use conduwuit::Error;
use futures::{StreamExt, stream::FuturesUnordered};
use ruma::server_name;
use serde_json::json;
use super::{CurTransactionStatus, TransactionStatus};
use crate::{
sending::{Destination, EduBuf, SendingEvent},
test_utils::{test_services, test_services_with},
};
#[tokio::test]
async fn panicked_transaction_is_kept_for_retry_without_blaming_the_remote() {
let (services, path, _database) = test_services().await;
let sending = &services.sending;
let server = server_name!("remote.example");
let dest = Destination::Federation(server.to_owned());
let event = SendingEvent::Edu(EduBuf::from_slice(br#"{"edu_type":"m.typing"}"#));
sending.db.queue_requests(once((&event, &dest)));
let in_flight: Vec<_> = sending.db.queued_requests(&dest).collect().await;
sending.db.mark_as_active(in_flight.iter());
let mut futures = FuturesUnordered::new();
let mut statuses = CurTransactionStatus::from([(dest.clone(), TransactionStatus::Running)]);
let panic = Error::from_panic(Box::new("boom"));
sending
.handle_response(Err((dest.clone(), Box::new(panic))), &mut futures, &mut statuses)
.await;
let active: Vec<_> = sending
.db
.active_requests_for(&dest)
.map(|(key, _)| key)
.collect()
.await;
let retry_after = sending.services.federation.retry_after(server);
let sending_next = futures.len();
let failed = matches!(statuses.get(&dest), Some(TransactionStatus::Failed(1, _)));
drop(futures);
drop(services);
std::fs::remove_dir_all(path).unwrap();
assert_eq!(
active,
in_flight
.into_iter()
.map(|(key, _)| key)
.collect::<Vec<_>>()
);
assert_eq!(sending_next, 0);
assert!(failed);
assert_eq!(retry_after, None);
}
#[tokio::test]
async fn restarted_worker_resends_trimmed_in_flight_requests() {
let (services, path, _database) =
test_services_with(json!({ "startup_netburst_keep": 1 })).await;
let sending = &services.sending;
let dest = Destination::Federation(server_name!("remote.example").to_owned());
let event = SendingEvent::Edu(EduBuf::from_slice(br#"{"edu_type":"m.typing"}"#));
for _ in 0..3 {
sending.db.queue_requests(once((&event, &dest)));
}
let queued: Vec<_> = sending.db.queued_requests(&dest).collect().await;
sending.db.mark_as_active(queued.iter());
let id = sending.shard_id(&dest);
let mut futures = FuturesUnordered::new();
let mut statuses = CurTransactionStatus::new();
sending
.startup_netburst(id, true, &mut futures, &mut statuses)
.await;
let active = sending.db.active_requests_for(&dest).count().await;
let sending_now = futures.len();
drop(futures);
drop(services);
std::fs::remove_dir_all(path).unwrap();
assert_eq!(active, 1);
assert_eq!(sending_now, 1);
}
-42
View File
@@ -1,42 +0,0 @@
use std::{iter::once, time::Duration};
use futures::StreamExt;
use ruma::server_name;
use super::{Destination, EduBuf, SendingEvent, restart_delay};
use crate::test_utils::test_services;
#[tokio::test]
async fn queued_heads_lists_the_oldest_unsent_request_per_destination() {
let (services, path, _database) = test_services().await;
let db = &services.sending.db;
let queued = Destination::Federation(server_name!("queued.example").to_owned());
let active = Destination::Federation(server_name!("active.example").to_owned());
let event = SendingEvent::Edu(EduBuf::from_slice(br#"{"edu_type":"m.typing"}"#));
let first = db.queue_requests(once((&event, &queued)));
db.queue_requests(once((&event, &queued)));
db.queue_requests(once((&event, &active)));
let in_flight: Vec<_> = db.queued_requests(&active).collect().await;
db.mark_as_active(in_flight.iter());
let heads: Vec<_> = db
.queued_heads()
.await
.into_iter()
.map(|(dest, (key, _))| (dest, key))
.collect();
drop(services);
std::fs::remove_dir_all(path).unwrap();
assert_eq!(heads, [(queued, first[0].clone())]);
}
#[test]
fn restart_delay_doubles_up_to_the_cap() {
assert_eq!(restart_delay(1), Duration::from_millis(2500));
assert_eq!(restart_delay(2), Duration::from_secs(5));
assert_eq!(restart_delay(4), Duration::from_secs(20));
assert_eq!(restart_delay(5), Duration::from_secs(30));
assert_eq!(restart_delay(u32::MAX), Duration::from_secs(30));
}
-52
View File
@@ -1,52 +0,0 @@
use std::{path::PathBuf, sync::Arc};
use conduwuit::{
Config, Server,
log::{Log, LogLevelReloadHandles},
metrics::Metrics,
};
use serde_json::json;
use tokio::sync::{Mutex, MutexGuard};
/// RocksDB's default `Env` is shared by the whole process and closing a
/// database stops its background threads, so tests must not keep databases
/// open in parallel. Hold the returned guard until the services are dropped.
static DATABASE: Mutex<()> = Mutex::const_new(());
type TestServices = (Arc<crate::Services>, PathBuf, MutexGuard<'static, ()>);
pub(crate) async fn test_services() -> TestServices { test_services_with(json!({})).await }
pub(crate) async fn test_services_with(overrides: serde_json::Value) -> TestServices {
let guard = DATABASE.lock().await;
let _ = rustls::crypto::ring::default_provider().install_default();
let path = std::env::temp_dir()
.join(format!("continuwuity-shared-state-{:016x}", rand::random::<u64>()));
std::fs::create_dir(&path).unwrap();
let mut config = json!({
"server_name": "test.example",
"database_path": path,
"db_cache_capacity_mb": 16.0,
"rocksdb_write_buffer_capacity_mb": 4.0,
"allow_local_presence": false,
"dns_servers": ["127.0.0.1"],
"allow_invalid_tls_certificates_yes_i_know_what_the_fuck_i_am_doing_with_this_and_i_know_this_is_insecure": true
});
config
.as_object_mut()
.unwrap()
.extend(overrides.as_object().unwrap().clone());
let config: Config = serde_json::from_value(config).unwrap();
let runtime = tokio::runtime::Handle::current();
let server = Arc::new(Server::new(
config,
Some(runtime.clone()),
Log {
reload: LogLevelReloadHandles::default(),
capture: Arc::default(),
},
Metrics::new(Some(runtime), opentelemetry::global::meter("test"), None),
));
let services = crate::Services::build(server).await.unwrap();
(services, path, guard)
}