You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by kr...@apache.org on 2026/08/18 01:49:07 UTC
(iggy) branch fix-max-meta updated (511df4aa6 -> 4a73327e9)
This is an automated email from the ASF dual-hosted git repository.
krishvishal pushed a change to branch fix-max-meta
in repository https://gitbox.apache.org/repos/asf/iggy.git
discard 511df4aa6 fix(metadata): enforce namespace caps at admission to stop aliasing
new 4a73327e9 fix(metadata): enforce namespace caps at admission to stop aliasing
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (511df4aa6)
\
N -- N -- N refs/heads/fix-max-meta (4a73327e9)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
core/common/src/error/iggy_error.rs | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
(iggy) 01/01: fix(metadata): enforce namespace caps at admission to stop aliasing
Posted by kr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
krishvishal pushed a commit to branch fix-max-meta
in repository https://gitbox.apache.org/repos/asf/iggy.git
commit 4a73327e9699a2de5ab7954a2848e05617e4fe60
Author: Krishna Vishal <kr...@gmail.com>
AuthorDate: Tue Aug 18 06:39:52 2026 +0530
fix(metadata): enforce namespace caps at admission to stop aliasing
---
Cargo.lock | 2 -
Cargo.toml | 1 -
core/binary_protocol/src/namespace.rs | 18 +-
core/common/src/error/iggy_error.rs | 15 +
core/configs/src/server_config/defaults.rs | 3 +-
core/configs/src/server_config/displays.rs | 21 +-
core/configs/src/server_config/server.rs | 30 +-
core/configs/src/server_config/validators.rs | 26 +-
.../tests/cluster/metadata_checkpoint_restart.rs | 2 +-
core/metadata/Cargo.toml | 1 -
core/metadata/src/stm/consumer_group.rs | 8 +-
core/metadata/src/stm/id_slab.rs | 353 +++++++++++++++
core/metadata/src/stm/mod.rs | 1 +
core/metadata/src/stm/result.rs | 41 +-
core/metadata/src/stm/stream.rs | 490 ++++++++++++++++++++-
core/metadata/src/stm/user.rs | 44 +-
core/partitions/src/iggy_partition.rs | 4 +-
core/server/Cargo.toml | 1 -
core/server/config.toml | 5 -
core/server/src/bootstrap.rs | 3 +-
core/server/src/partition_helpers.rs | 53 +--
core/server/src/server_error.rs | 11 -
core/server_common/src/sharding/mod.rs | 6 +-
core/server_common/src/sharding/namespace.rs | 222 +++++-----
.../src/workload/ops/create_partitions.rs | 8 +-
core/simulator/src/workload/ops/create_stream.rs | 11 +-
core/simulator/src/workload/ops/create_topic.rs | 11 +-
27 files changed, 1108 insertions(+), 283 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 7f9abf32d..e5964e999 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8491,7 +8491,6 @@ dependencies = [
"rmp-serde",
"serde",
"server_common",
- "slab",
"tempfile",
"tracing",
]
@@ -11906,7 +11905,6 @@ dependencies = [
"server_common",
"shard",
"shard_allocator",
- "slab",
"socket2 0.6.5",
"strum 0.28.0",
"sysinfo 0.39.6",
diff --git a/Cargo.toml b/Cargo.toml
index 7adc072b3..784e3ac54 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -298,7 +298,6 @@ server_common = { path = "core/server_common" }
shard = { path = "core/shard" }
shard_allocator = { path = "core/shard_allocator" }
simd-json = { version = "0.17.3", features = ["serde_impl"] }
-slab = "0.4.12"
smallvec = "1.15"
socket2 = "0.6.5"
sqlx = { version = "0.9.0", features = [
diff --git a/core/binary_protocol/src/namespace.rs b/core/binary_protocol/src/namespace.rs
index 88dbdfa93..88f827f93 100644
--- a/core/binary_protocol/src/namespace.rs
+++ b/core/binary_protocol/src/namespace.rs
@@ -26,7 +26,13 @@
//! sharding layer; the single source of truth lives here in the wire-format
//! crate both already depend on.
-pub const MAX_STREAMS: usize = 4096;
+/// Only the HIGHEST field widens for free.
+///
+/// `STREAM_SHIFT` derives from `TOPIC_SHIFT + TOPIC_BITS`, so growing
+/// `MAX_STREAMS` leaves packed values bit-identical. Growing the others shifts
+/// the fields above them and rewrites every group id on the wire, needing a
+/// version fence and a re-shard.
+pub const MAX_STREAMS: usize = 1 << 20;
pub const MAX_TOPICS: usize = 4096;
pub const MAX_PARTITIONS: usize = 1_000_000;
@@ -85,4 +91,14 @@ const _: () = {
assert!(METADATA_GROUP > PACKED_NAMESPACE_MAX);
assert!(PACKED_NAMESPACE_BITS == STREAM_BITS + TOPIC_BITS + PARTITION_BITS);
assert!(PACKED_NAMESPACE_MAX == (1u64 << PACKED_NAMESPACE_BITS) - 1);
+ // Guards the ENCODING: any width change re-encodes every group id on the wire.
+ assert!(STREAM_BITS == 20 && TOPIC_BITS == 12 && PARTITION_BITS == 20);
+ // Guards the ADMITTED SET. `bits_required` rounds up, so narrowing a maximum
+ // inside its current width keeps the widths (and the assert above) intact
+ // while turning already-committed ids above the new ceiling into a panic.
+ assert!(MAX_STREAMS == 1 << 20 && MAX_TOPICS == 4096 && MAX_PARTITIONS == 1_000_000);
};
+
+// `PARTITION_MASK` admits 48,576 ids `MAX_PARTITIONS` forbids, since only the
+// former rounds to a power of two. Bound checks compare against the `MAX_*`
+// constants: a mask is the looser bound and re-opens the aliasing.
diff --git a/core/common/src/error/iggy_error.rs b/core/common/src/error/iggy_error.rs
index d80006ade..52981fba4 100644
--- a/core/common/src/error/iggy_error.rs
+++ b/core/common/src/error/iggy_error.rs
@@ -22,6 +22,11 @@ use std::sync::Arc;
use strum::{EnumDiscriminants, FromRepr, IntoStaticStr};
use thiserror::Error;
+// A gap in the discriminants is a RETIRED code, not free space. Shipped SDKs
+// keep their own code tables (foreign/go/errors/errors.yaml,
+// foreign/node/src/wire/error.code.ts) that still map the old meaning, and
+// Go's is a typed error matched by errors.Is, so refilling a gap reroutes
+// caller control flow. Allocate above the highest code in its range.
#[derive(Clone, Debug, Error, EnumDiscriminants, IntoStaticStr, FromRepr, Default)]
#[repr(u32)]
#[strum(serialize_all = "snake_case")]
@@ -236,6 +241,8 @@ pub enum IggyError {
"Max topic size cannot be lower than segment size. Max topic size: {0} < segment size: {1}."
)]
InvalidTopicSize(MaxTopicSize, IggyByteSize) = 1019,
+ #[error("Too many streams")]
+ TooManyStreams = 1020,
#[error("Cannot create topics directory for stream with ID: {0}, Path: {1}")]
CannotCreateTopicsDirectory(Identifier, String) = 2000,
#[error("Failed to create directory for topic with ID: {0} for stream with ID: {1}, Path: {2}")]
@@ -276,6 +283,8 @@ pub enum IggyError {
InvalidPartitionsCount = 2019,
#[error("Topic directory: {0} not found")]
TopicDirectoryNotFound(String) = 2020,
+ #[error("Too many topics")]
+ TooManyTopics = 2021,
#[error("Cannot create partition with ID: {0} for stream with ID: {1} and topic with ID: {2}")]
CannotCreatePartition(usize, usize, usize) = 3000,
#[error(
@@ -310,6 +319,12 @@ pub enum IggyError {
CannotDeleteConsumerOffsetFile(String) = 3011,
#[error("Failed to create consumer offsets directory for path: {0}")]
CannotCreateConsumerOffsetsDirectory(String) = 3012,
+ /// Distinct from [`Self::TooManyPartitions`] by remedy: that one is cleared
+ /// by a smaller batch, this one by deleting partitions. Ids are minted one
+ /// past the highest LIVE id, and deletion truncates from the tail, so the
+ /// range frees again.
+ #[error("Partition id space exhausted for this topic")]
+ PartitionIdSpaceExhausted = 3013,
#[error("Failed to read consumers offsets from path: {0}")]
CannotReadConsumerOffsets(String) = 3020,
#[error("Consumer offset for consumer with ID: {0} was not found.")]
diff --git a/core/configs/src/server_config/defaults.rs b/core/configs/src/server_config/defaults.rs
index c9659d317..7238769b5 100644
--- a/core/configs/src/server_config/defaults.rs
+++ b/core/configs/src/server_config/defaults.rs
@@ -30,8 +30,8 @@ use super::message_bus::MessageBusConfig;
use super::metadata::MetadataConfig;
use super::partition::PartitionConfig;
use super::quic::{QuicCertificateConfig, QuicConfig};
+use super::server::ServerConfig;
use super::server::ServerSystemConfig;
-use super::server::{ExtraConfig, ServerConfig};
use super::tcp::{TcpConfig, TcpTlsConfig};
use super::websocket::{WebSocketConfig, WebSocketTlsConfig};
use crate::common::http::HttpConfig;
@@ -50,7 +50,6 @@ impl Default for ServerConfig {
ServerConfig {
consumer_group: ConsumerGroupConfig::default(),
data_maintenance: DataMaintenanceConfig::default(),
- extra: ExtraConfig::default(),
heartbeat: HeartbeatConfig::default(),
personal_access_token: PersonalAccessTokenConfig::default(),
system: Arc::new(ServerSystemConfig::default()),
diff --git a/core/configs/src/server_config/displays.rs b/core/configs/src/server_config/displays.rs
index 642d96970..aa4f79f98 100644
--- a/core/configs/src/server_config/displays.rs
+++ b/core/configs/src/server_config/displays.rs
@@ -26,7 +26,7 @@ use super::message_bus::MessageBusConfig;
use super::metadata::MetadataConfig;
use super::partition::PartitionConfig;
use super::quic::{QuicCertificateConfig, QuicConfig};
-use super::server::{ExtraConfig, NamespaceConfig, ServerConfig};
+use super::server::ServerConfig;
use super::tcp::{TcpConfig, TcpTlsConfig};
use std::fmt::{Display, Formatter};
@@ -34,12 +34,11 @@ impl Display for ServerConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
- "{{ consumer_group: {}, data_maintenance: {}, extra: {}, \
+ "{{ consumer_group: {}, data_maintenance: {}, \
heartbeat: {}, system: {}, quic: {}, tcp: {}, http: {}, telemetry: {}, \
metadata: {}, message_bus: {}, partition: {} }}",
self.consumer_group,
self.data_maintenance,
- self.extra,
self.heartbeat,
self.system,
self.quic,
@@ -97,22 +96,6 @@ impl Display for MessageBusConfig {
}
}
-impl Display for ExtraConfig {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- write!(f, "{{ namespace: {} }}", self.namespace)
- }
-}
-
-impl Display for NamespaceConfig {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- write!(
- f,
- "{{ max_streams: {}, max_topics: {}, max_partitions: {} }}",
- self.max_streams, self.max_topics, self.max_partitions
- )
- }
-}
-
impl Display for TcpConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
diff --git a/core/configs/src/server_config/server.rs b/core/configs/src/server_config/server.rs
index f97b4b903..bd7d168b3 100644
--- a/core/configs/src/server_config/server.rs
+++ b/core/configs/src/server_config/server.rs
@@ -36,7 +36,6 @@ use figment::value::Dict;
use figment::{Metadata, Profile, Provider};
use iggy_common::Validatable;
use serde::{Deserialize, Serialize};
-use server_common::sharding::{MAX_PARTITIONS, MAX_STREAMS, MAX_TOPICS};
use std::env;
use std::sync::Arc;
@@ -88,6 +87,12 @@ const RELOCATED_CONFIG_KEYS: &[RelocatedKey] = &[
path: "system.message_deduplication",
replacement: None,
},
+ // The whole table, not just its leaves. Caps are compile-time constants
+ // enforced at admission, so a per-node value could only diverge from them.
+ RelocatedKey {
+ path: "extra",
+ replacement: None,
+ },
];
/// [`SystemConfig`] bound to this crate's own
@@ -106,7 +111,6 @@ pub struct ServerConfig {
pub consumer_group: ConsumerGroupConfig,
pub data_maintenance: DataMaintenanceConfig,
#[serde(default)]
- pub extra: ExtraConfig,
pub personal_access_token: PersonalAccessTokenConfig,
pub heartbeat: HeartbeatConfig,
pub system: Arc<ServerSystemConfig>,
@@ -121,28 +125,6 @@ pub struct ServerConfig {
pub message_bus: MessageBusConfig,
}
-#[derive(Debug, Default, Deserialize, Serialize, Clone, ConfigEnv)]
-pub struct ExtraConfig {
- pub namespace: NamespaceConfig,
-}
-
-#[derive(Debug, Deserialize, Serialize, Clone, ConfigEnv)]
-pub struct NamespaceConfig {
- pub max_streams: usize,
- pub max_topics: usize,
- pub max_partitions: usize,
-}
-
-impl Default for NamespaceConfig {
- fn default() -> Self {
- Self {
- max_streams: MAX_STREAMS,
- max_topics: MAX_TOPICS,
- max_partitions: MAX_PARTITIONS,
- }
- }
-}
-
impl ServerConfig {
/// Load server configuration from file and environment variables.
///
diff --git a/core/configs/src/server_config/validators.rs b/core/configs/src/server_config/validators.rs
index e3c6b0d99..f8ff7b48a 100644
--- a/core/configs/src/server_config/validators.rs
+++ b/core/configs/src/server_config/validators.rs
@@ -25,13 +25,12 @@
use super::COMPONENT;
use super::cluster::STATE_CHUNK_HEADER_LEN;
use super::partition::{CONCURRENT_SERVED_SEGMENTS, SEGMENT_SIZE_OVERSHOOT_BYTES};
-use super::server::{ExtraConfig, NamespaceConfig, ServerConfig};
+use super::server::ServerConfig;
use crate::ConfigurationError;
use crate::common::http::HMAC_JWT_ALGORITHMS;
use crate::common::validators::SEGMENT_MAX_SIZE_BYTES;
use err_trail::ErrContext;
use iggy_common::{IggyExpiry, Validatable};
-use server_common::sharding::IggyNamespace;
/// compio-ws (tungstenite 0.29) `write_buffer_size` default. Used to
/// evaluate the `max_write_buffer_size > write_buffer_size` invariant
@@ -59,9 +58,6 @@ impl Validatable<ConfigurationError> for ServerConfig {
"{COMPONENT} (error: {e}) - failed to validate personal access token config"
)
})?;
- self.extra.validate().error(|e: &ConfigurationError| {
- format!("{COMPONENT} (error: {e}) - failed to validate extra config")
- })?;
self.system
.segment
.validate()
@@ -401,26 +397,6 @@ fn reject_unsupported(config: &ServerConfig) -> Result<(), ConfigurationError> {
Ok(())
}
-impl Validatable<ConfigurationError> for ExtraConfig {
- fn validate(&self) -> Result<(), ConfigurationError> {
- self.namespace.validate().error(|e: &ConfigurationError| {
- format!("{COMPONENT} (error: {e}) - failed to validate namespace config")
- })?;
- Ok(())
- }
-}
-
-impl Validatable<ConfigurationError> for NamespaceConfig {
- fn validate(&self) -> Result<(), ConfigurationError> {
- IggyNamespace::validate_capacity(self.max_streams, self.max_topics, self.max_partitions)
- .map_err(|error| {
- eprintln!("extra.namespace is invalid: {error}");
- ConfigurationError::InvalidConfigurationValue
- })?;
- Ok(())
- }
-}
-
#[cfg(test)]
mod tests {
use super::super::cluster::{ClusterNodeConfig, TransportPorts};
diff --git a/core/integration/tests/cluster/metadata_checkpoint_restart.rs b/core/integration/tests/cluster/metadata_checkpoint_restart.rs
index 499b66bae..4da083844 100644
--- a/core/integration/tests/cluster/metadata_checkpoint_restart.rs
+++ b/core/integration/tests/cluster/metadata_checkpoint_restart.rs
@@ -284,7 +284,7 @@ async fn given_installed_snapshot_when_node_restarts_again_should_transfer_again
/// The prepare WAL has `SLOT_COUNT = 1024` slots and the coordinator checkpoints at
/// `<= CHECKPOINT_MARGIN` (64) remaining, so after ~960 uncheckpointed ops. 1024
/// stream creates clears that with room for a WAL suffix above the checkpoint, and
-/// stays under the 4096 stream namespace cap.
+/// stays under `MAX_STREAMS`.
const STREAMS: u32 = 1024;
const RECOVER_TIMEOUT: Duration = Duration::from_secs(60);
diff --git a/core/metadata/Cargo.toml b/core/metadata/Cargo.toml
index bfb94458e..e3328af8a 100644
--- a/core/metadata/Cargo.toml
+++ b/core/metadata/Cargo.toml
@@ -52,7 +52,6 @@ rmp = { workspace = true }
rmp-serde = { workspace = true }
serde = { workspace = true, features = ["derive"] }
server_common = { workspace = true }
-slab = { workspace = true }
tracing = { workspace = true }
[dev-dependencies]
diff --git a/core/metadata/src/stm/consumer_group.rs b/core/metadata/src/stm/consumer_group.rs
index fd757c2d4..22a66c7d4 100644
--- a/core/metadata/src/stm/consumer_group.rs
+++ b/core/metadata/src/stm/consumer_group.rs
@@ -27,6 +27,7 @@
//! offset -- making a metadata->offset purge unnecessary for correctness.
use crate::stm::StateHandler;
+use crate::stm::id_slab::IdSlab;
use crate::stm::result::{
ApplyReply, CreateConsumerGroupResult, DeleteConsumerGroupResult, JoinConsumerGroupResult,
LeaveConsumerGroupResult,
@@ -45,7 +46,6 @@ use iggy_binary_protocol::requests::consumer_groups::{
use iggy_binary_protocol::responses::consumer_groups::consumer_group_response::ConsumerGroupResponse;
use iggy_binary_protocol::responses::consumer_groups::get_consumer_group::ConsumerGroupDetailsResponse;
use serde::{Deserialize, Serialize};
-use slab::Slab;
use std::sync::Arc;
/// A partition being cooperatively handed off from this member to `target`.
@@ -120,7 +120,7 @@ pub struct ConsumerGroup {
/// when it advances.
pub generation: u64,
pub name: Arc<str>,
- pub members: Slab<ConsumerGroupMember>,
+ pub members: IdSlab<ConsumerGroupMember>,
}
impl ConsumerGroup {
@@ -130,7 +130,7 @@ impl ConsumerGroup {
id,
generation: 0,
name,
- members: Slab::new(),
+ members: IdSlab::new(),
}
}
@@ -910,7 +910,7 @@ impl ConsumerGroupSnapshot {
#[must_use]
pub fn into_group(self) -> ConsumerGroup {
- let members: Slab<ConsumerGroupMember> = self
+ let members: IdSlab<ConsumerGroupMember> = self
.members
.into_iter()
.map(|(member_key, member_snap)| {
diff --git a/core/metadata/src/stm/id_slab.rs b/core/metadata/src/stm/id_slab.rs
new file mode 100644
index 000000000..e7f4c1af2
--- /dev/null
+++ b/core/metadata/src/stm/id_slab.rs
@@ -0,0 +1,353 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Key-stable arena with deterministic key allocation. Replaces `slab::Slab`
+//! for the catalogs whose keys are committed state.
+//!
+//! `Slab` recycles keys LIFO from a free list no snapshot carries and it
+//! exposes no way to restore, rebuilding it by ascending scan. The next key
+//! therefore depended on local delete order, so a replica restored from a
+//! snapshot assigned a different key than a live one for the same log entry.
+//! Stream and topic keys pack into the consensus group id, making that a
+//! different shard owner, consensus group, directory and authorization scope:
+//! a silent permanent fork.
+//!
+//! [`IdSlab::vacant_key`] is the lowest unoccupied key, a pure function of the
+//! occupied set, which is exactly what a snapshot carries. Restored and live
+//! arenas agree by construction.
+
+use std::collections::BTreeSet;
+use std::ops::{Index, IndexMut};
+
+/// Arena addressed by stable `usize` keys, allocating the lowest free key.
+#[derive(Debug, Clone)]
+pub struct IdSlab<T> {
+ /// Dense by key; `None` is an unoccupied slot below the high-water mark.
+ entries: Vec<Option<T>>,
+ len: usize,
+ /// Unoccupied keys below `entries.len()`, ascending. A memo so allocation
+ /// stays O(log n) instead of scanning. Always derived from `entries`, never
+ /// serialized: that is what keeps a restored arena identical to a live one.
+ free: BTreeSet<usize>,
+}
+
+impl<T> IdSlab<T> {
+ #[must_use]
+ pub const fn new() -> Self {
+ Self {
+ entries: Vec::new(),
+ len: 0,
+ free: BTreeSet::new(),
+ }
+ }
+
+ #[must_use]
+ pub const fn len(&self) -> usize {
+ self.len
+ }
+
+ #[must_use]
+ pub const fn is_empty(&self) -> bool {
+ self.len == 0
+ }
+
+ /// Key the next [`Self::insert`] will use: the lowest unoccupied one.
+ /// Depends only on the occupied set, so it survives a snapshot round trip.
+ /// Callers stamp it into committed state before inserting, which is only
+ /// sound because of that.
+ #[must_use]
+ pub fn vacant_key(&self) -> usize {
+ self.free.first().copied().unwrap_or(self.entries.len())
+ }
+
+ /// Inserts at [`Self::vacant_key`] and returns that key.
+ pub fn insert(&mut self, value: T) -> usize {
+ self.len += 1;
+ if let Some(key) = self.free.pop_first() {
+ self.entries[key] = Some(value);
+ return key;
+ }
+ self.entries.push(Some(value));
+ self.entries.len() - 1
+ }
+
+ #[must_use]
+ pub fn contains(&self, key: usize) -> bool {
+ self.get(key).is_some()
+ }
+
+ #[must_use]
+ pub fn get(&self, key: usize) -> Option<&T> {
+ self.entries.get(key)?.as_ref()
+ }
+
+ pub fn get_mut(&mut self, key: usize) -> Option<&mut T> {
+ self.entries.get_mut(key)?.as_mut()
+ }
+
+ /// Removes `key`, returning its value, or `None` if it was unoccupied.
+ ///
+ /// A trailing removal is not truncated: the slot stays in `free` for a later
+ /// insert. Truncating would make the high-water mark depend on removal
+ /// order, the divergence this type exists to remove.
+ pub fn try_remove(&mut self, key: usize) -> Option<T> {
+ let value = self.entries.get_mut(key)?.take()?;
+ self.len -= 1;
+ self.free.insert(key);
+ Some(value)
+ }
+
+ /// # Panics
+ /// If `key` is unoccupied. Mirrors `Slab::remove`.
+ pub fn remove(&mut self, key: usize) -> T {
+ self.try_remove(key)
+ .unwrap_or_else(|| panic!("no entry at key {key}"))
+ }
+
+ /// Ascending by key.
+ #[must_use]
+ pub fn iter(&self) -> Iter<'_, T> {
+ Iter {
+ inner: self.entries.iter().enumerate(),
+ }
+ }
+
+ /// Ascending by key.
+ pub fn iter_mut(&mut self) -> IterMut<'_, T> {
+ IterMut {
+ inner: self.entries.iter_mut().enumerate(),
+ }
+ }
+}
+
+/// Named rather than `impl Iterator` so `IntoIterator` can name it without
+/// boxing. These run on per-request metadata reads.
+pub struct Iter<'a, T> {
+ inner: std::iter::Enumerate<std::slice::Iter<'a, Option<T>>>,
+}
+
+impl<'a, T> Iterator for Iter<'a, T> {
+ type Item = (usize, &'a T);
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ let (key, slot) = self.inner.next()?;
+ if let Some(value) = slot {
+ return Some((key, value));
+ }
+ }
+ }
+}
+
+pub struct IterMut<'a, T> {
+ inner: std::iter::Enumerate<std::slice::IterMut<'a, Option<T>>>,
+}
+
+impl<'a, T> Iterator for IterMut<'a, T> {
+ type Item = (usize, &'a mut T);
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ let (key, slot) = self.inner.next()?;
+ if let Some(value) = slot {
+ return Some((key, value));
+ }
+ }
+ }
+}
+
+pub struct IntoIter<T> {
+ inner: std::iter::Enumerate<std::vec::IntoIter<Option<T>>>,
+}
+
+impl<T> Iterator for IntoIter<T> {
+ type Item = (usize, T);
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ let (key, slot) = self.inner.next()?;
+ if let Some(value) = slot {
+ return Some((key, value));
+ }
+ }
+ }
+}
+
+impl<T> Default for IdSlab<T> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl<T> Index<usize> for IdSlab<T> {
+ type Output = T;
+
+ fn index(&self, key: usize) -> &Self::Output {
+ self.get(key)
+ .unwrap_or_else(|| panic!("no entry at key {key}"))
+ }
+}
+
+impl<T> IndexMut<usize> for IdSlab<T> {
+ fn index_mut(&mut self, key: usize) -> &mut Self::Output {
+ self.get_mut(key)
+ .unwrap_or_else(|| panic!("no entry at key {key}"))
+ }
+}
+
+/// Rebuilds from `(key, value)` pairs, the shape every snapshot stores.
+///
+/// The high-water mark becomes one past the largest key, and every gap below it
+/// becomes free. Both derive from the pairs alone, so the same pairs always
+/// yield the same [`IdSlab::vacant_key`]. Duplicate keys keep the last value.
+impl<T> FromIterator<(usize, T)> for IdSlab<T> {
+ fn from_iter<I: IntoIterator<Item = (usize, T)>>(iter: I) -> Self {
+ let mut slab = Self::new();
+ for (key, value) in iter {
+ if key >= slab.entries.len() {
+ slab.entries.resize_with(key + 1, || None);
+ }
+ if slab.entries[key].replace(value).is_none() {
+ slab.len += 1;
+ }
+ }
+ slab.free = (0..slab.entries.len())
+ .filter(|&key| slab.entries[key].is_none())
+ .collect();
+ slab
+ }
+}
+
+impl<'a, T> IntoIterator for &'a IdSlab<T> {
+ type Item = (usize, &'a T);
+ type IntoIter = Iter<'a, T>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.iter()
+ }
+}
+
+impl<'a, T> IntoIterator for &'a mut IdSlab<T> {
+ type Item = (usize, &'a mut T);
+ type IntoIter = IterMut<'a, T>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.iter_mut()
+ }
+}
+
+impl<T> IntoIterator for IdSlab<T> {
+ type Item = (usize, T);
+ type IntoIter = IntoIter<T>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ IntoIter {
+ inner: self.entries.into_iter().enumerate(),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::IdSlab;
+
+ /// The property the type exists for. `Slab` fails it: LIFO reuse makes the
+ /// head depend on delete order while a rebuild scans ascending, so
+ /// descending deletes diverge.
+ #[test]
+ fn given_any_delete_order_when_rebuilding_should_keep_vacant_key() {
+ for order in [[1, 0], [0, 1]] {
+ let mut slab: IdSlab<char> = IdSlab::new();
+ for value in ['a', 'b', 'c'] {
+ slab.insert(value);
+ }
+ for key in order {
+ slab.remove(key);
+ }
+ let before = slab.vacant_key();
+
+ let rebuilt: IdSlab<char> = slab
+ .iter()
+ .map(|(key, value)| (key, *value))
+ .collect::<Vec<_>>()
+ .into_iter()
+ .collect();
+
+ assert_eq!(
+ rebuilt.vacant_key(),
+ before,
+ "delete order {order:?} must not survive a rebuild"
+ );
+ }
+ }
+
+ #[test]
+ fn given_a_hole_when_inserting_should_take_the_lowest_key() {
+ let mut slab: IdSlab<char> = IdSlab::new();
+ for value in ['a', 'b', 'c'] {
+ slab.insert(value);
+ }
+ slab.remove(2);
+ slab.remove(0);
+
+ assert_eq!(slab.vacant_key(), 0);
+ assert_eq!(slab.insert('d'), 0);
+ assert_eq!(slab.insert('e'), 2);
+ assert_eq!(slab.insert('f'), 3, "then extends past the high-water mark");
+ assert_eq!(slab.len(), 4);
+ }
+
+ /// A trailing removal must not shrink the high-water mark, or it would
+ /// again depend on removal order.
+ #[test]
+ fn given_a_trailing_removal_when_inserting_should_refill_the_slot() {
+ let mut slab: IdSlab<char> = IdSlab::new();
+ for value in ['a', 'b'] {
+ slab.insert(value);
+ }
+ slab.remove(1);
+
+ assert_eq!(slab.vacant_key(), 1);
+ assert_eq!(slab.insert('c'), 1);
+ }
+
+ #[test]
+ fn given_removed_keys_when_reading_should_report_absent() {
+ let mut slab: IdSlab<char> = IdSlab::new();
+ let key = slab.insert('a');
+ assert_eq!(slab.get(key), Some(&'a'));
+
+ assert_eq!(slab.remove(key), 'a');
+ assert!(!slab.contains(key));
+ assert_eq!(slab.get(key), None);
+ assert_eq!(slab.try_remove(key), None);
+ assert!(slab.is_empty());
+ }
+
+ #[test]
+ fn given_holes_when_iterating_should_yield_ascending_occupied_keys() {
+ let mut slab: IdSlab<char> = IdSlab::new();
+ for value in ['a', 'b', 'c', 'd'] {
+ slab.insert(value);
+ }
+ slab.remove(2);
+ slab.remove(0);
+
+ let seen: Vec<(usize, char)> = slab.iter().map(|(key, value)| (key, *value)).collect();
+ assert_eq!(seen, vec![(1, 'b'), (3, 'd')]);
+ }
+}
diff --git a/core/metadata/src/stm/mod.rs b/core/metadata/src/stm/mod.rs
index cd363cb24..8dd3ae9c4 100644
--- a/core/metadata/src/stm/mod.rs
+++ b/core/metadata/src/stm/mod.rs
@@ -17,6 +17,7 @@
pub mod authz;
pub mod consumer_group;
+pub mod id_slab;
pub mod mux;
pub mod result;
pub mod snapshot;
diff --git a/core/metadata/src/stm/result.rs b/core/metadata/src/stm/result.rs
index a1be91ff6..57dbbce0b 100644
--- a/core/metadata/src/stm/result.rs
+++ b/core/metadata/src/stm/result.rs
@@ -148,6 +148,7 @@ macro_rules! result_enum {
// Streams.
result_enum!(CreateStreamResult {
NameAlreadyExists = 1012,
+ TooManyStreams = 1020,
InvalidOptionValue = 4042,
});
result_enum!(UpdateStreamResult {
@@ -162,6 +163,9 @@ result_enum!(PurgeStreamResult { StreamNotFound = 1009 });
result_enum!(CreateTopicResult {
StreamNotFound = 1009,
NameAlreadyExists = 2013,
+ InvalidPartitionsCount = 2019,
+ TooManyTopics = 2021,
+ PartitionIdSpaceExhausted = 3013,
InvalidOptionValue = 4042,
});
result_enum!(UpdateTopicResult {
@@ -179,12 +183,15 @@ result_enum!(PurgeTopicResult {
TopicNotFound = 2010,
});
-// Partitions. `InvalidPartitionsCount` also covers the partition-id overflow
-// guards in the apply handler.
+// `InvalidPartitionsCount` covers the `u32` overflow guards and non-distinct
+// ids. `PartitionIdSpaceExhausted` covers the packed-layout ceiling. Neither is
+// `TooManyPartitions`, which dispatch owns for an oversized single request: the
+// split is by REMEDY (smaller batch vs delete partitions), not by terminality.
result_enum!(CreatePartitionsResult {
StreamNotFound = 1009,
TopicNotFound = 2010,
InvalidPartitionsCount = 2019,
+ PartitionIdSpaceExhausted = 3013,
});
result_enum!(DeletePartitionsResult {
StreamNotFound = 1009,
@@ -514,6 +521,10 @@ mod tests {
u32::from(CreatePartitionsResult::InvalidPartitionsCount),
IggyError::InvalidPartitionsCount.as_code(),
);
+ assert_eq!(
+ u32::from(CreateTopicResult::InvalidPartitionsCount),
+ IggyError::InvalidPartitionsCount.as_code(),
+ );
// UserAlreadyExists (46) - also stands in for username-already-exists.
let user_exists = IggyError::UserAlreadyExists.as_code();
@@ -640,6 +651,32 @@ mod tests {
IggyError::ConsumerGroupMemberNotFound(0, id(), id()).as_code(),
);
+ // Namespace ceilings. 1020 and 2021 sit above gaps left by retired
+ // variants that shipped SDK tables still map, so a renumber here is the
+ // drift this test exists to catch.
+ assert_eq!(
+ u32::from(CreateStreamResult::TooManyStreams),
+ IggyError::TooManyStreams.as_code(),
+ );
+ assert_eq!(
+ u32::from(CreateTopicResult::TooManyTopics),
+ IggyError::TooManyTopics.as_code(),
+ );
+ let partition_id_space_exhausted = IggyError::PartitionIdSpaceExhausted.as_code();
+ assert_eq!(
+ u32::from(CreateTopicResult::PartitionIdSpaceExhausted),
+ partition_id_space_exhausted,
+ );
+ assert_eq!(
+ u32::from(CreatePartitionsResult::PartitionIdSpaceExhausted),
+ partition_id_space_exhausted,
+ );
+ // The per-topic ceiling must not collapse onto dispatch's per-request cap.
+ assert_ne!(
+ partition_id_space_exhausted,
+ IggyError::TooManyPartitions.as_code(),
+ );
+
// Unauthorized (41) - the global in-apply RBAC denial code.
assert_eq!(UNAUTHORIZED_CODE, IggyError::Unauthorized.as_code());
}
diff --git a/core/metadata/src/stm/stream.rs b/core/metadata/src/stm/stream.rs
index eb99ca652..f7ba28fa3 100644
--- a/core/metadata/src/stm/stream.rs
+++ b/core/metadata/src/stm/stream.rs
@@ -20,6 +20,7 @@ use crate::stm::consumer_group::{
CompleteConsumerGroupRevocationRequest, ConsumerGroup, ConsumerGroupSnapshot,
JoinConsumerGroupRequest, LeaveConsumerGroupRequest, RemoveConsumerGroupMemberRequest,
};
+use crate::stm::id_slab::IdSlab;
use crate::stm::result::{
ApplyReply, CreatePartitionsResult, CreateStreamResult, CreateTopicResult,
DeletePartitionsResult, DeleteStreamResult, DeleteTopicResult, PurgeStreamResult,
@@ -30,12 +31,10 @@ use crate::{collect_handlers, define_state, impl_fill_restore};
use ahash::{AHashMap, AHashSet};
use bytes::{BufMut, Bytes, BytesMut};
use iggy_binary_protocol::codec::{WireDecode, WireEncode};
-// Only `seed_namespace` (below, sim/test-gated) uses these at module scope;
-// keep the imports under the same gate so a production build does not see them
-// as unused. The test module re-imports them independently.
+// Only `seed_namespace` (sim/test-gated) uses this at module scope, so keep the
+// import under the same gate. The test module re-imports it independently.
#[cfg(any(test, feature = "simulator"))]
use iggy_binary_protocol::primitives::options::WireOptions;
-#[cfg(any(test, feature = "simulator"))]
use iggy_binary_protocol::primitives::partition_assignment::CreatedPartitionAssignment;
use iggy_binary_protocol::requests::consumer_groups::{
CreateConsumerGroupRequest, DeleteConsumerGroupRequest,
@@ -70,8 +69,7 @@ use iggy_common::{
ResourceOptions, StreamStats, TopicCreateOptions, TopicRuntimeOptions, TopicStats,
};
use serde::{Deserialize, Serialize};
-use server_common::sharding::IggyNamespace;
-use slab::Slab;
+use server_common::sharding::{IggyNamespace, MAX_PARTITIONS, MAX_STREAMS, MAX_TOPICS};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -297,7 +295,7 @@ pub struct Stream {
pub options: ResourceOptions,
pub stats: Arc<StreamStats>,
- pub topics: Slab<Topic>,
+ pub topics: IdSlab<Topic>,
pub topic_index: AHashMap<Arc<str>, usize>,
}
@@ -309,7 +307,7 @@ impl Default for Stream {
created_at: IggyTimestamp::default(),
options: ResourceOptions::default(),
stats: Arc::new(StreamStats::default()),
- topics: Slab::new(),
+ topics: IdSlab::new(),
topic_index: AHashMap::default(),
}
}
@@ -338,7 +336,7 @@ impl Stream {
created_at,
options: ResourceOptions::new(),
stats: Arc::new(StreamStats::default()),
- topics: Slab::new(),
+ topics: IdSlab::new(),
topic_index: AHashMap::default(),
}
}
@@ -351,7 +349,7 @@ impl Stream {
created_at,
options: ResourceOptions::new(),
stats,
- topics: Slab::new(),
+ topics: IdSlab::new(),
topic_index: AHashMap::default(),
}
}
@@ -597,7 +595,7 @@ impl StatsRegistry {
define_state! {
Streams {
index: AHashMap<Arc<str>, usize>,
- items: Slab<Stream>,
+ items: IdSlab<Stream>,
// Monotonic counter bumped on every partition-shaping commit
// (create/delete topic, create/delete partitions, delete stream).
// Reconciler uses it for a fast-skip when nothing changed and stamps
@@ -1552,6 +1550,45 @@ impl Streams {
}
}
+/// Whether the next insert's slab key still fits the packed namespace layout.
+///
+/// Takes `vacant_key()` rather than `len()` because the KEY is what gets packed
+/// and keys are recycled on delete. The two agree at the ceiling, but the key
+/// is the value whose width matters.
+///
+/// `ceiling` is always a compile-time `MAX_*` constant, never node config: an
+/// apply that branches on local config forks the state machine the moment two
+/// replicas disagree about their TOML.
+const fn admits_slab_key(vacant_key: usize, ceiling: usize) -> bool {
+ vacant_key < ceiling
+}
+
+/// Range and distinctness for the ABSOLUTE partition ids on a topic create,
+/// `None` when the vector is well formed.
+///
+/// Range is the weaker half: two in-range entries sharing an id pack to ONE
+/// namespace, landing both partitions on a single consensus group, shard,
+/// directory and authorization scope. That is the aliasing the range bound
+/// prevents, reached without exceeding it.
+///
+/// Unreachable from a well-behaved primary, which mints dense 0-based ids under
+/// `MAX_PARTITIONS_PER_REQUEST`. Apply is defensive because the prepare body is
+/// primary-minted, not client-minted.
+fn absolute_partition_ids_fault(
+ partitions: &[CreatedPartitionAssignment],
+) -> Option<CreateTopicResult> {
+ let mut seen = AHashSet::with_capacity(partitions.len());
+ for partition in partitions {
+ if partition.partition_id as usize >= MAX_PARTITIONS {
+ return Some(CreateTopicResult::PartitionIdSpaceExhausted);
+ }
+ if !seen.insert(partition.partition_id) {
+ return Some(CreateTopicResult::InvalidPartitionsCount);
+ }
+ }
+ None
+}
+
impl StateHandler for CreateStreamRequest {
type State = StreamsInner;
#[allow(clippy::cast_possible_truncation)]
@@ -1568,6 +1605,9 @@ impl StateHandler for CreateStreamRequest {
// registry (see `StatsRegistry`). The id the next insert will use is
// deterministic, so both buffers resolve the same registry key.
let id = state.items.vacant_key();
+ if !admits_slab_key(id, MAX_STREAMS) {
+ return ApplyReply::err(CreateStreamResult::TooManyStreams);
+ }
let stream_stats = state.stats_registry.stream(id);
let stream = Stream {
id,
@@ -1575,7 +1615,7 @@ impl StateHandler for CreateStreamRequest {
created_at: timestamp,
options,
stats: stream_stats,
- topics: Slab::new(),
+ topics: IdSlab::new(),
topic_index: AHashMap::default(),
};
let inserted = state.items.insert(stream);
@@ -1712,6 +1752,15 @@ impl StateHandler for CreateTopicWithAssignmentsRequest {
if stream.topic_index.contains_key(&name_arc) {
return ApplyReply::err(CreateTopicResult::NameAlreadyExists);
}
+ // Topic slabs are per stream, so this caps topics WITHIN one stream.
+ if !admits_slab_key(stream.topics.vacant_key(), MAX_TOPICS) {
+ return ApplyReply::err(CreateTopicResult::TooManyTopics);
+ }
+ // Checked before any mutation, so a rejected batch leaves no
+ // half-built topic.
+ if let Some(fault) = absolute_partition_ids_fault(&self.partitions) {
+ return ApplyReply::err(fault);
+ }
}
// Both blocks were validated and resolved at admission, so apply reads
@@ -2049,6 +2098,7 @@ impl StateHandler for CreatePartitionsWithAssignmentsRequest {
};
let mut resolved: Vec<usize> = Vec::with_capacity(self.partitions.len());
+ let mut seen = AHashSet::with_capacity(self.partitions.len());
for partition in &self.partitions {
let Some(resolved_id_u32) = partition.partition_id.checked_add(base_partition_id)
else {
@@ -2057,6 +2107,19 @@ impl StateHandler for CreatePartitionsWithAssignmentsRequest {
let Ok(resolved_id_usize) = usize::try_from(resolved_id_u32) else {
return ApplyReply::err(CreatePartitionsResult::InvalidPartitionsCount);
};
+ // The `u32` guards above only catch overflow.
+ // `MAX_PARTITIONS_PER_REQUEST` bounds one call, not the running
+ // total, so without this a topic accumulates past the packed
+ // field across calls and its ids alias. Against `MAX_PARTITIONS`,
+ // never `PARTITION_MASK`, which is the looser bound.
+ if resolved_id_usize >= MAX_PARTITIONS {
+ return ApplyReply::err(CreatePartitionsResult::PartitionIdSpaceExhausted);
+ }
+ // Two offsets resolving to one id alias onto a single namespace.
+ // Existing partitions cannot collide: `base` is one past the max.
+ if !seen.insert(resolved_id_usize) {
+ return ApplyReply::err(CreatePartitionsResult::InvalidPartitionsCount);
+ }
resolved.push(resolved_id_usize);
}
resolved
@@ -2134,7 +2197,7 @@ impl StateHandler for DeletePartitionsRequest {
///
/// Serialized-form invariant (see [`crate::stm::snapshot::MetadataSnapshot`]):
/// `items` and the nested `topics` / `consumer_groups` / `partitions` stay ordered
-/// `Vec`s even though the runtime holds them in `AHashMap`s and a `Slab`. Swapping
+/// `Vec`s even though the runtime holds them in `AHashMap`s and an [`IdSlab`]. Swapping
/// any back to an unordered map reorders on a decode and re-encode, breaking the
/// checkpoint checksum cross-check recovery relies on.
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -2331,7 +2394,7 @@ impl StreamsInner {
topic_entries.push((topic_slab_key, topic));
}
- let topics: Slab<Topic> = topic_entries.into_iter().collect();
+ let topics: IdSlab<Topic> = topic_entries.into_iter().collect();
let stream_name: Arc<str> = Arc::from(stream_snap.name.as_str());
let stream = Stream {
@@ -2348,7 +2411,7 @@ impl StreamsInner {
stream_entries.push((slab_key, stream));
}
- let items: Slab<Stream> = stream_entries.into_iter().collect();
+ let items: IdSlab<Stream> = stream_entries.into_iter().collect();
let mut inner = Self {
index,
items,
@@ -3317,4 +3380,401 @@ mod tests {
"a partition the snapshot dropped must not keep its registry entry"
);
}
+
+ /// Admission is all that stands between a client and a namespace collision.
+ /// `IggyNamespace::new` used to mask, so slab key `MAX_TOPICS` packed
+ /// byte-identically to key 0: same shard, consensus group, directory and
+ /// authorization scope, no error.
+ ///
+ /// Topics are the affordable ceiling to drive end to end. Streams and
+ /// partitions use the same guard but sit at `1 << 20` and 1,000,000, so
+ /// materialising either costs hundreds of megabytes for no extra coverage.
+ #[test]
+ fn given_the_topic_slab_ceiling_when_creating_one_more_should_reject() {
+ let mut inner = StreamsInner::new();
+ create_stream(&mut inner, "s");
+
+ for index in 0..MAX_TOPICS {
+ let request = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 0, &format!("t{index}")),
+ derived_options: WireOptions::empty(),
+ partitions: Vec::new(),
+ };
+ let reply = StateHandler::apply(&request, &mut inner, IggyTimestamp::now());
+ assert_eq!(reply.code, 0, "topic {index} must be admitted");
+ }
+
+ let overflow = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 0, "one-too-many"),
+ derived_options: WireOptions::empty(),
+ partitions: Vec::new(),
+ };
+ let reply = StateHandler::apply(&overflow, &mut inner, IggyTimestamp::now());
+ assert_eq!(reply.code, u32::from(CreateTopicResult::TooManyTopics));
+
+ let stream = inner.items.get(0).expect("stream survives the rejection");
+ assert_eq!(
+ stream.topics.len(),
+ MAX_TOPICS,
+ "a rejected create must not mutate the slab"
+ );
+ assert!(
+ !stream.topic_index.contains_key("one-too-many"),
+ "a rejected create must not leave a name-index entry"
+ );
+ }
+
+ /// `MAX_PARTITIONS_PER_REQUEST` bounds one call, not the running total, so
+ /// the additive path is where a topic walks past the packed field. Seeded at
+ /// the boundary rather than driven there, since the guard is on the resolved
+ /// absolute id.
+ #[test]
+ fn given_partition_ids_at_the_ceiling_when_adding_more_should_reject() {
+ let mut inner = StreamsInner::new();
+ create_stream(&mut inner, "s");
+
+ let seed = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 1, "t"),
+ derived_options: WireOptions::empty(),
+ partitions: vec![CreatedPartitionAssignment {
+ partition_id: u32::try_from(MAX_PARTITIONS - 2).unwrap(),
+ consensus_group_id: 1,
+ }],
+ };
+ assert_eq!(
+ StateHandler::apply(&seed, &mut inner, IggyTimestamp::now()).code,
+ 0
+ );
+
+ // Resolves to MAX_PARTITIONS - 1: the last legal id.
+ let last = CreatePartitionsWithAssignmentsRequest {
+ request: WireCreatePartitionsRequest {
+ stream_id: WireIdentifier::numeric(0),
+ topic_id: WireIdentifier::numeric(0),
+ partitions_count: 1,
+ },
+ partitions: vec![CreatedPartitionAssignment {
+ partition_id: 0,
+ consensus_group_id: 2,
+ }],
+ };
+ assert_eq!(
+ StateHandler::apply(&last, &mut inner, IggyTimestamp::now()).code,
+ 0,
+ "the final in-range id must still be admitted"
+ );
+
+ let overflow = CreatePartitionsWithAssignmentsRequest {
+ request: WireCreatePartitionsRequest {
+ stream_id: WireIdentifier::numeric(0),
+ topic_id: WireIdentifier::numeric(0),
+ partitions_count: 1,
+ },
+ partitions: vec![CreatedPartitionAssignment {
+ partition_id: 0,
+ consensus_group_id: 3,
+ }],
+ };
+ let reply = StateHandler::apply(&overflow, &mut inner, IggyTimestamp::now());
+ assert_eq!(
+ reply.code,
+ u32::from(CreatePartitionsResult::PartitionIdSpaceExhausted)
+ );
+ let topic = inner.items.get(0).unwrap().topics.get(0).unwrap();
+ assert_eq!(
+ topic.partitions.len(),
+ 2,
+ "a rejected batch must not append"
+ );
+ }
+
+ /// Topic create carries ABSOLUTE partition ids, so it needs its own bound.
+ /// The additive guard above never sees them.
+ #[test]
+ fn given_an_out_of_range_partition_id_on_topic_create_should_reject() {
+ let mut inner = StreamsInner::new();
+ create_stream(&mut inner, "s");
+
+ let request = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 1, "t"),
+ derived_options: WireOptions::empty(),
+ partitions: vec![CreatedPartitionAssignment {
+ partition_id: u32::try_from(MAX_PARTITIONS).unwrap(),
+ consensus_group_id: 1,
+ }],
+ };
+ let reply = StateHandler::apply(&request, &mut inner, IggyTimestamp::now());
+ assert_eq!(
+ reply.code,
+ u32::from(CreateTopicResult::PartitionIdSpaceExhausted),
+ "a partition-id violation must not report a topic-count verdict"
+ );
+ assert_eq!(
+ inner.items.get(0).unwrap().topics.len(),
+ 0,
+ "a rejected create must not leave a half-built topic"
+ );
+ }
+
+ fn delete_stream(inner: &mut StreamsInner, id: usize) {
+ let request = DeleteStreamRequest {
+ stream_id: WireIdentifier::numeric(u32::try_from(id).unwrap()),
+ };
+ let reply = StateHandler::apply(&request, inner, IggyTimestamp::now());
+ assert_eq!(reply.code, 0, "stream {id} must delete");
+ }
+
+ /// `vacant_key()` is both the id handed to the next `CreateStream` and the
+ /// input to the `MAX_STREAMS` guard, so it must depend only on committed
+ /// state. `StreamsSnapshot` carries `items` and `revision` and nothing else,
+ /// which is why [`IdSlab`] derives the key from the occupied set instead of
+ /// from a free list: a divergence here forks the state machine, since a
+ /// restored replica would assign a different stream id for the same log
+ /// entry, hence a different packed namespace, shard owner, consensus group,
+ /// on-disk directory and authorization scope.
+ ///
+ /// Descending deletes are the case that catches it. `slab::Slab` recycled
+ /// keys LIFO and rebuilt that head by ascending scan, so this asserted
+ /// `1 == 0` before the swap.
+ #[test]
+ fn given_descending_deletes_when_round_tripping_a_snapshot_should_keep_vacant_key() {
+ let mut inner = StreamsInner::new();
+ for name in ["a", "b", "c"] {
+ create_stream(&mut inner, name);
+ }
+ delete_stream(&mut inner, 1);
+ delete_stream(&mut inner, 0);
+ let before = inner.items.vacant_key();
+
+ let snapshot = Streams::from(inner.clone()).to_snapshot();
+ let restored =
+ StreamsInner::inner_from_snapshot(snapshot, Arc::new(StatsRegistry::default()));
+
+ assert_eq!(
+ restored.items.vacant_key(),
+ before,
+ "the id the next CreateStream is assigned must not depend on whether \
+ a snapshot was restored in between"
+ );
+ }
+
+ /// Topic ids come from the per-stream `topics` arena by the same
+ /// `vacant_key()` route, so they forked identically. Held separate from the
+ /// stream case because the two arenas are restored by different code paths.
+ #[test]
+ fn given_descending_topic_deletes_when_round_tripping_a_snapshot_should_keep_vacant_key() {
+ let mut inner = StreamsInner::new();
+ create_stream(&mut inner, "s");
+ for name in ["a", "b", "c"] {
+ let request = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 0, name),
+ derived_options: WireOptions::empty(),
+ partitions: Vec::new(),
+ };
+ assert_eq!(
+ StateHandler::apply(&request, &mut inner, IggyTimestamp::now()).code,
+ 0
+ );
+ }
+ for topic_id in [1, 0] {
+ let request = DeleteTopicRequest {
+ stream_id: WireIdentifier::numeric(0),
+ topic_id: WireIdentifier::numeric(topic_id),
+ };
+ assert_eq!(
+ StateHandler::apply(&request, &mut inner, IggyTimestamp::now()).code,
+ 0,
+ "topic {topic_id} must delete"
+ );
+ }
+ let before = inner.items.get(0).unwrap().topics.vacant_key();
+
+ let snapshot = Streams::from(inner.clone()).to_snapshot();
+ let restored =
+ StreamsInner::inner_from_snapshot(snapshot, Arc::new(StatsRegistry::default()));
+
+ assert_eq!(
+ restored.items.get(0).unwrap().topics.vacant_key(),
+ before,
+ "the id the next CreateTopic is assigned must not depend on whether \
+ a snapshot was restored in between"
+ );
+ }
+
+ /// The `CreateStream` ceiling cannot be driven end to end: `MAX_STREAMS`
+ /// live slab entries would cost hundreds of megabytes, and `slab` offers no
+ /// way to seed a high key (no `insert_at`, no key setter, and `FromIterator`
+ /// leaves `vacant_key()` on a hole rather than past the end). Testing the
+ /// comparison directly is what is available, so it is factored out.
+ #[test]
+ fn given_a_slab_key_at_the_ceiling_when_admitting_should_refuse() {
+ assert!(admits_slab_key(0, MAX_STREAMS));
+ assert!(admits_slab_key(MAX_STREAMS - 1, MAX_STREAMS));
+ assert!(!admits_slab_key(MAX_STREAMS, MAX_STREAMS));
+ assert!(!admits_slab_key(MAX_STREAMS + 1, MAX_STREAMS));
+
+ assert!(admits_slab_key(MAX_TOPICS - 1, MAX_TOPICS));
+ assert!(!admits_slab_key(MAX_TOPICS, MAX_TOPICS));
+ }
+
+ /// Two in-range entries sharing an id pack to ONE namespace, so the range
+ /// guard alone still admits the aliasing this module exists to prevent.
+ #[test]
+ fn given_duplicate_partition_ids_on_topic_create_should_reject() {
+ let mut inner = StreamsInner::new();
+ create_stream(&mut inner, "s");
+
+ let request = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 2, "t"),
+ derived_options: WireOptions::empty(),
+ partitions: vec![
+ CreatedPartitionAssignment {
+ partition_id: 7,
+ consensus_group_id: 1,
+ },
+ CreatedPartitionAssignment {
+ partition_id: 7,
+ consensus_group_id: 2,
+ },
+ ],
+ };
+ let reply = StateHandler::apply(&request, &mut inner, IggyTimestamp::now());
+ assert_eq!(
+ reply.code,
+ u32::from(CreateTopicResult::InvalidPartitionsCount),
+ "a duplicate id is a malformed batch, not a capacity verdict"
+ );
+ assert_eq!(
+ inner.items.get(0).unwrap().topics.len(),
+ 0,
+ "a rejected create must not leave a half-built topic"
+ );
+ }
+
+ /// Same hole on the additive path: distinct offsets are what keep resolved
+ /// ids distinct, and nothing upstream guarantees it.
+ #[test]
+ fn given_duplicate_partition_offsets_when_adding_should_reject() {
+ let mut inner = StreamsInner::new();
+ create_stream(&mut inner, "s");
+
+ let seed = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 1, "t"),
+ derived_options: WireOptions::empty(),
+ partitions: vec![CreatedPartitionAssignment {
+ partition_id: 0,
+ consensus_group_id: 1,
+ }],
+ };
+ assert_eq!(
+ StateHandler::apply(&seed, &mut inner, IggyTimestamp::now()).code,
+ 0
+ );
+
+ let duplicate = CreatePartitionsWithAssignmentsRequest {
+ request: WireCreatePartitionsRequest {
+ stream_id: WireIdentifier::numeric(0),
+ topic_id: WireIdentifier::numeric(0),
+ partitions_count: 2,
+ },
+ partitions: vec![
+ CreatedPartitionAssignment {
+ partition_id: 0,
+ consensus_group_id: 2,
+ },
+ CreatedPartitionAssignment {
+ partition_id: 0,
+ consensus_group_id: 3,
+ },
+ ],
+ };
+ let reply = StateHandler::apply(&duplicate, &mut inner, IggyTimestamp::now());
+ assert_eq!(
+ reply.code,
+ u32::from(CreatePartitionsResult::InvalidPartitionsCount)
+ );
+
+ let topic = inner.items.get(0).unwrap().topics.get(0).unwrap();
+ assert_eq!(
+ topic.partitions.len(),
+ 1,
+ "a rejected batch must not append"
+ );
+ }
+
+ /// Distinct offsets in the same batch must still be admitted, so the
+ /// uniqueness guard above cannot be passing for the wrong reason.
+ #[test]
+ fn given_distinct_partition_offsets_when_adding_should_admit() {
+ let mut inner = StreamsInner::new();
+ create_stream(&mut inner, "s");
+
+ let seed = CreateTopicWithAssignmentsRequest {
+ request: make_topic_request(0, 1, "t"),
+ derived_options: WireOptions::empty(),
+ partitions: vec![CreatedPartitionAssignment {
+ partition_id: 0,
+ consensus_group_id: 1,
+ }],
+ };
+ assert_eq!(
+ StateHandler::apply(&seed, &mut inner, IggyTimestamp::now()).code,
+ 0
+ );
+
+ let distinct = CreatePartitionsWithAssignmentsRequest {
+ request: WireCreatePartitionsRequest {
+ stream_id: WireIdentifier::numeric(0),
+ topic_id: WireIdentifier::numeric(0),
+ partitions_count: 2,
+ },
+ partitions: vec![
+ CreatedPartitionAssignment {
+ partition_id: 0,
+ consensus_group_id: 2,
+ },
+ CreatedPartitionAssignment {
+ partition_id: 1,
+ consensus_group_id: 3,
+ },
+ ],
+ };
+ assert_eq!(
+ StateHandler::apply(&distinct, &mut inner, IggyTimestamp::now()).code,
+ 0
+ );
+
+ let topic = inner.items.get(0).unwrap().topics.get(0).unwrap();
+ let ids: Vec<usize> = topic
+ .partitions
+ .iter()
+ .map(|partition| partition.id)
+ .collect();
+ assert_eq!(
+ ids,
+ vec![0, 1, 2],
+ "resolved ids must stay dense and distinct"
+ );
+ }
+
+ /// Pins the asymmetry that hid the fork: ascending deletes leave a free-list
+ /// head the ascending rebuild happens to reproduce, so only the descending
+ /// order diverges. Without this, a fix that accidentally broke the agreeing
+ /// case would still look correct.
+ #[test]
+ fn given_ascending_deletes_when_round_tripping_a_snapshot_should_keep_vacant_key() {
+ let mut inner = StreamsInner::new();
+ for name in ["a", "b", "c"] {
+ create_stream(&mut inner, name);
+ }
+ delete_stream(&mut inner, 0);
+ delete_stream(&mut inner, 1);
+ let before = inner.items.vacant_key();
+
+ let snapshot = Streams::from(inner.clone()).to_snapshot();
+ let restored =
+ StreamsInner::inner_from_snapshot(snapshot, Arc::new(StatsRegistry::default()));
+
+ assert_eq!(restored.items.vacant_key(), before);
+ }
}
diff --git a/core/metadata/src/stm/user.rs b/core/metadata/src/stm/user.rs
index 011fefbee..1e79d7853 100644
--- a/core/metadata/src/stm/user.rs
+++ b/core/metadata/src/stm/user.rs
@@ -17,6 +17,7 @@
use crate::permissioner::Permissioner;
use crate::stm::StateHandler;
+use crate::stm::id_slab::IdSlab;
use crate::stm::result::{
ApplyReply, ChangePasswordResult, CreatePersonalAccessTokenResult, CreateUserResult,
DeletePersonalAccessTokenResult, DeleteUserResult, UpdatePermissionsResult, UpdateUserResult,
@@ -42,7 +43,6 @@ use iggy_common::{
ResourceOptions, StreamPermissions, UserId, UserStatus,
};
use serde::{Deserialize, Serialize};
-use slab::Slab;
use std::collections::BTreeSet;
use std::sync::Arc;
@@ -99,7 +99,7 @@ impl User {
define_state! {
Users {
index: AHashMap<Arc<str>, UserId>,
- items: Slab<User>,
+ items: IdSlab<User>,
personal_access_tokens: AHashMap<UserId, AHashMap<Arc<str>, PersonalAccessToken>>,
// SAFETY: deterministic-apply invariant. `AHashMap` iteration order
// differs across replicas (random seed), so this map MUST only be
@@ -900,7 +900,7 @@ impl UsersInner {
user_entries.push((slab_key, user));
}
- let items: Slab<User> = user_entries.into_iter().collect();
+ let items: IdSlab<User> = user_entries.into_iter().collect();
let mut personal_access_tokens: AHashMap<UserId, AHashMap<Arc<str>, PersonalAccessToken>> =
AHashMap::new();
@@ -1174,6 +1174,44 @@ mod tests {
);
}
+ /// User ids come from `items.insert`, and the free list is not part of
+ /// `UsersSnapshot`, so the next `CreateUser` id depended on local delete
+ /// order rather than committed state. A restored replica would hand a
+ /// different id to the same log entry, forking the users table and every
+ /// permission keyed off it.
+ #[test]
+ fn given_descending_deletes_when_round_tripping_a_snapshot_should_keep_the_next_user_id() {
+ let mut users = UsersInner::new();
+ for username in ["alpha", "bravo", "charlie", "delta"] {
+ create_user(&mut users, username);
+ }
+ // Key 0 is the protected root user, so holes go above it. The highest
+ // key stays occupied, or the holes are trailing and a rebuild agrees.
+ let keys: Vec<usize> = users.items.iter().map(|(key, _)| key).collect();
+ let last = keys.len() - 1;
+ for user_id in [keys[last - 1], keys[last - 2]] {
+ let request = DeleteUserRequest {
+ user_id: WireIdentifier::numeric(u32::try_from(user_id).unwrap()),
+ };
+ assert_eq!(
+ StateHandler::apply(&request, &mut users, IggyTimestamp::now()).code,
+ 0,
+ "user {user_id} must delete"
+ );
+ }
+ let before = users.items.vacant_key();
+
+ let snapshot = Users::from(users.clone()).to_snapshot();
+ let restored = UsersInner::inner_from_snapshot(snapshot);
+
+ assert_eq!(
+ restored.items.vacant_key(),
+ before,
+ "the id the next CreateUser is assigned must not depend on whether \
+ a snapshot was restored in between"
+ );
+ }
+
fn create_user(users: &mut UsersInner, username: &str) {
let request = CreateUserRequest {
username: WireName::new(username).unwrap(),
diff --git a/core/partitions/src/iggy_partition.rs b/core/partitions/src/iggy_partition.rs
index c3bd2a698..2aec16964 100644
--- a/core/partitions/src/iggy_partition.rs
+++ b/core/partitions/src/iggy_partition.rs
@@ -4874,8 +4874,8 @@ fn send_messages_reply_body(
let namespace = IggyNamespace::from_raw(namespace);
SendMessagesResponse {
confirmations: vec![SendMessagesConfirmationResponse {
- // `IggyNamespace` packs the ids into 12/12/20 bits, so each
- // component fits a `u32` by construction.
+ // Every field is narrower than a `u32` (widths compile-asserted in
+ // `iggy_binary_protocol`), so each component fits by construction.
stream_id: namespace.stream_id() as u32,
topic_id: namespace.topic_id() as u32,
partition_id: namespace.partition_id() as u32,
diff --git a/core/server/Cargo.toml b/core/server/Cargo.toml
index db597e52f..b67164964 100644
--- a/core/server/Cargo.toml
+++ b/core/server/Cargo.toml
@@ -153,7 +153,6 @@ serde_json = { workspace = true }
server_common = { workspace = true }
shard = { workspace = true }
shard_allocator = { workspace = true }
-slab = { workspace = true }
socket2 = { workspace = true }
strum = { workspace = true }
sysinfo = { workspace = true }
diff --git a/core/server/config.toml b/core/server/config.toml
index 9168ccf26..f7a004197 100644
--- a/core/server/config.toml
+++ b/core/server/config.toml
@@ -937,8 +937,3 @@ close_grace = "2 s"
# connecting.await + accept_bi.await) so a slowloris peer cannot pin
# per-conn channels + registry slot + spawned task indefinitely.
handshake_grace = "10 s"
-
-[extra.namespace]
-max_streams = 4096
-max_topics = 4096
-max_partitions = 1_000_000
diff --git a/core/server/src/bootstrap.rs b/core/server/src/bootstrap.rs
index 8b1613577..03817cd9b 100644
--- a/core/server/src/bootstrap.rs
+++ b/core/server/src/bootstrap.rs
@@ -26,7 +26,7 @@ use crate::dispatch::{
use crate::http;
use crate::partition_helpers::{
build_partition_fresh, configure_consumer_offsets, ensure_initial_segment,
- open_partition_superblock, restore_partition_view, validate_namespace_bounds,
+ open_partition_superblock, restore_partition_view,
};
use crate::segment_recovery::{RecoveredSegment, load_persisted_segments};
use crate::server_error::{ServerError, ShardJoinFailure, ShardJoinFailureKind};
@@ -1839,7 +1839,6 @@ async fn build_shard_for_thread(
// here only add their per-partition deltas, so the shared
// `Arc<TopicStats>` atomics race only against other atomic adds.
for (stream_id, topic_id, partition_stats, partition_metadata, topic_runtime) in owned {
- validate_namespace_bounds(config, stream_id, topic_id, partition_metadata.id)?;
let namespace = IggyNamespace::new(stream_id, topic_id, partition_metadata.id);
let partition = match load_partition(
config,
diff --git a/core/server/src/partition_helpers.rs b/core/server/src/partition_helpers.rs
index e3117fdd3..76ddab41b 100644
--- a/core/server/src/partition_helpers.rs
+++ b/core/server/src/partition_helpers.rs
@@ -46,42 +46,6 @@ use std::sync::Arc;
use std::sync::atomic::Ordering;
use tracing::{error, info, warn};
-/// Validate that a namespace fits within the static caps declared in
-/// `config.extra.namespace`.
-///
-/// Bootstrap calls this for every recovered namespace; the reconciler
-/// calls this before materialising a freshly committed partition. Same
-/// error variant either way so operators see one root cause label.
-///
-/// # Errors
-///
-/// Returns [`ServerError::RecoveredNamespaceOutOfBounds`] if any of
-/// `stream_id`, `topic_id`, or `partition_id` exceed the configured
-/// maxima.
-pub const fn validate_namespace_bounds(
- config: &ServerConfig,
- stream_id: usize,
- topic_id: usize,
- partition_id: usize,
-) -> Result<(), ServerError> {
- let namespace = &config.extra.namespace;
- if stream_id < namespace.max_streams
- && topic_id < namespace.max_topics
- && partition_id < namespace.max_partitions
- {
- return Ok(());
- }
-
- Err(ServerError::RecoveredNamespaceOutOfBounds {
- stream_id,
- topic_id,
- partition_id,
- max_streams: namespace.max_streams,
- max_topics: namespace.max_topics,
- max_partitions: namespace.max_partitions,
- })
-}
-
/// Create the on-disk directory hierarchy for a partition.
///
/// Builds the partition root, offsets, consumer offsets, and consumer
@@ -574,11 +538,13 @@ pub(crate) fn restore_partition_view(
/// the local shard has not yet materialised.
///
/// Steps performed (all idempotent on retry after a partial failure):
-/// 1. Validate namespace fits within the configured caps.
-/// 2. Create directory hierarchy on disk.
-/// 3. Build per-partition VSR consensus group, resuming any superblock-recorded view.
-/// 4. Configure empty consumer-offset storage with the on-disk paths set.
-/// 5. Provision the initial segment + writers (offset 0).
+/// 1. Create directory hierarchy on disk.
+/// 2. Build per-partition VSR consensus group, resuming any superblock-recorded view.
+/// 3. Configure empty consumer-offset storage with the on-disk paths set.
+/// 4. Provision the initial segment + writers (offset 0).
+///
+/// The namespace arrives packed, so its components are in range by
+/// construction. Metadata admission is what bounds them.
///
/// The returned partition's `offset` / `dirty_offset` are `0` and
/// `should_increment_offset` is `false`, mirroring a clean append starting
@@ -586,8 +552,8 @@ pub(crate) fn restore_partition_view(
///
/// # Errors
///
-/// Returns [`ServerError`] when bounds validation, directory creation,
-/// superblock recovery, or segment provisioning fails.
+/// Returns [`ServerError`] when directory creation, superblock recovery, or
+/// segment provisioning fails.
#[allow(clippy::too_many_arguments)]
pub async fn build_partition_fresh(
config: &ServerConfig,
@@ -604,7 +570,6 @@ pub async fn build_partition_fresh(
let topic_id = namespace.topic_id();
let partition_id = namespace.partition_id();
- validate_namespace_bounds(config, stream_id, topic_id, partition_id)?;
// Sampled BEFORE the hierarchy create: a pre-existing partition directory
// is the marker of a prior life (the .log inside may legitimately be
// empty -- committed-but-unflushed data dies with the journal), while a
diff --git a/core/server/src/server_error.rs b/core/server/src/server_error.rs
index ae8b238a3..06c8ec250 100644
--- a/core/server/src/server_error.rs
+++ b/core/server/src/server_error.rs
@@ -265,17 +265,6 @@ pub enum ServerError {
#[source]
source: Box<iggy_common::IggyError>,
},
- #[error(
- "recovered namespace stream {stream_id}, topic {topic_id}, partition {partition_id} exceeds configured limits (max_streams={max_streams}, max_topics={max_topics}, max_partitions={max_partitions})"
- )]
- RecoveredNamespaceOutOfBounds {
- stream_id: usize,
- topic_id: usize,
- partition_id: usize,
- max_streams: usize,
- max_topics: usize,
- max_partitions: usize,
- },
#[error("failed to load {transport} listener credentials")]
ListenerCredentials {
transport: &'static str,
diff --git a/core/server_common/src/sharding/mod.rs b/core/server_common/src/sharding/mod.rs
index b73169f52..58974159a 100644
--- a/core/server_common/src/sharding/mod.rs
+++ b/core/server_common/src/sharding/mod.rs
@@ -22,9 +22,9 @@ mod shard_id;
pub use local_idx::LocalIdx;
pub use namespace::{
- IggyNamespace, MAX_PARTITIONS, MAX_STREAMS, MAX_TOPICS, METADATA_GROUP, NamespaceCapacityError,
- PACKED_NAMESPACE_BITS, PACKED_NAMESPACE_MAX, PARTITION_BITS, PARTITION_MASK, PARTITION_SHIFT,
- STREAM_BITS, STREAM_MASK, STREAM_SHIFT, TOPIC_BITS, TOPIC_MASK, TOPIC_SHIFT,
+ IggyNamespace, MAX_PARTITIONS, MAX_STREAMS, MAX_TOPICS, METADATA_GROUP, PACKED_NAMESPACE_BITS,
+ PACKED_NAMESPACE_MAX, PARTITION_BITS, PARTITION_MASK, PARTITION_SHIFT, STREAM_BITS,
+ STREAM_MASK, STREAM_SHIFT, TOPIC_BITS, TOPIC_MASK, TOPIC_SHIFT,
};
pub use partition_location::PartitionLocation;
pub use shard_id::ShardId;
diff --git a/core/server_common/src/sharding/namespace.rs b/core/server_common/src/sharding/namespace.rs
index 435b076c4..cf2691cb8 100644
--- a/core/server_common/src/sharding/namespace.rs
+++ b/core/server_common/src/sharding/namespace.rs
@@ -15,11 +15,17 @@
// specific language governing permissions and limitations
// under the License.
-// Packed namespace layout
-// +----------------+----------------+----------------+----------------+
-// | stream_id | topic_id | partition_id | unused |
-// | STREAM_BITS | TOPIC_BITS | PARTITION_BITS | (64 - total) |
-// +----------------+----------------+----------------+----------------+
+// Packed namespace layout, drawn most-significant bit first.
+//
+// bit 63 62 ......... 52 51 ....... 32 31 ..... 20 19 ......... 0
+// +----------+-----------------+--------------+-------------+--------------+
+// | METADATA | unused | stream_id | topic_id | partition_id |
+// | _GROUP | (63 - total) | STREAM_BITS | TOPIC_BITS | PARTITION_.. |
+// +----------+-----------------+--------------+-------------+--------------+
+//
+// Slack sits between the highest field and the sentinel, not below partition.
+// Stream is highest, so it is the only field that widens without re-encoding
+// every value already on the wire.
//
// The layout constants live in `iggy_binary_protocol::namespace` because
// both the SDK encoder and this server-side router depend on them and they
@@ -29,34 +35,13 @@
pub use iggy_binary_protocol::namespace::{
MAX_PARTITIONS, MAX_STREAMS, MAX_TOPICS, METADATA_GROUP, PACKED_NAMESPACE_BITS,
PACKED_NAMESPACE_MAX, PARTITION_BITS, PARTITION_MASK, PARTITION_SHIFT, STREAM_BITS,
- STREAM_MASK, STREAM_SHIFT, TOPIC_BITS, TOPIC_MASK, TOPIC_SHIFT, bits_required,
+ STREAM_MASK, STREAM_SHIFT, TOPIC_BITS, TOPIC_MASK, TOPIC_SHIFT,
};
-use thiserror::Error;
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
-pub enum NamespaceCapacityError {
- #[error("max_streams must be greater than 0")]
- ZeroStreams,
- #[error("max_topics must be greater than 0")]
- ZeroTopics,
- #[error("max_partitions must be greater than 0")]
- ZeroPartitions,
- #[error("namespace capacity requires {required_bits} bits, which does not fit in u64")]
- ExceedsU64 { required_bits: u32 },
- #[error(
- "namespace capacity requires stream/topic/partition bits of {stream_bits}/{topic_bits}/{partition_bits}, but IggyNamespace supports {STREAM_BITS}/{TOPIC_BITS}/{PARTITION_BITS}"
- )]
- ExceedsLayout {
- stream_bits: u32,
- topic_bits: u32,
- partition_bits: u32,
- },
-}
-
/// Packed namespace identifier for shard assignment.
///
-/// Encodes stream_id (12 bits), topic_id (12 bits), and partition_id (20 bits)
-/// into a single u64 for efficient hashing and routing.
+/// Packs stream, topic and partition ids into one u64 for hashing and routing.
+/// Widths are compile-asserted in `iggy_binary_protocol`. Do not restate them
+/// here, where they go stale unnoticed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct IggyNamespace(u64);
@@ -86,63 +71,53 @@ impl IggyNamespace {
((self.0 >> PARTITION_SHIFT) & PARTITION_MASK) as usize
}
+ /// Packs a committed `(stream, topic, partition)` slab address.
+ ///
+ /// # Panics
+ /// If any component is at or above its declared maximum. Metadata admission
+ /// rejects such a create before it commits, so reaching here means an
+ /// invariant is already broken. Masking, which this used to do, aliases two
+ /// partitions onto one consensus group, shard, directory and authorization
+ /// scope, silently and unrecoverably. Untrusted or recovered ids want
+ /// [`Self::try_new`].
#[inline]
+ #[must_use]
pub fn new(stream: usize, topic: usize, partition: usize) -> Self {
- let value = ((stream as u64) & STREAM_MASK) << STREAM_SHIFT
- | ((topic as u64) & TOPIC_MASK) << TOPIC_SHIFT
- | ((partition as u64) & PARTITION_MASK) << PARTITION_SHIFT;
- Self(value)
+ Self::try_new(stream, topic, partition).unwrap_or_else(|| {
+ panic!(
+ "namespace ({stream}, {topic}, {partition}) exceeds the packed layout \
+ ({MAX_STREAMS}, {MAX_TOPICS}, {MAX_PARTITIONS})"
+ )
+ })
}
- /// Returns `true` when `value` could have been produced by [`Self::new`].
- /// False for the metadata sentinel and for any `u64` with bits set above
- /// the packed range.
+ /// Fallible [`Self::new`]. `None` when any component is at or above its
+ /// declared maximum. Bounds compare against the `MAX_*` constants, not the
+ /// field masks: `MAX_PARTITIONS` is not a power of two, so its mask admits
+ /// ids the maximum forbids.
#[inline]
#[must_use]
- pub const fn is_packable(value: u64) -> bool {
- value <= PACKED_NAMESPACE_MAX
- }
-
- pub fn validate_capacity(
- max_streams: usize,
- max_topics: usize,
- max_partitions: usize,
- ) -> Result<(), NamespaceCapacityError> {
- let stream_bits = if max_streams == 0 {
- return Err(NamespaceCapacityError::ZeroStreams);
- } else {
- bits_required((max_streams - 1) as u64)
- };
- let topic_bits = if max_topics == 0 {
- return Err(NamespaceCapacityError::ZeroTopics);
- } else {
- bits_required((max_topics - 1) as u64)
- };
- let partition_bits = if max_partitions == 0 {
- return Err(NamespaceCapacityError::ZeroPartitions);
- } else {
- bits_required((max_partitions - 1) as u64)
- };
-
- let required_bits = stream_bits
- .checked_add(topic_bits)
- .and_then(|bits| bits.checked_add(partition_bits))
- .ok_or(NamespaceCapacityError::ExceedsU64 {
- required_bits: u32::MAX,
- })?;
- if required_bits > u64::BITS {
- return Err(NamespaceCapacityError::ExceedsU64 { required_bits });
- }
-
- if stream_bits > STREAM_BITS || topic_bits > TOPIC_BITS || partition_bits > PARTITION_BITS {
- return Err(NamespaceCapacityError::ExceedsLayout {
- stream_bits,
- topic_bits,
- partition_bits,
- });
+ pub fn try_new(stream: usize, topic: usize, partition: usize) -> Option<Self> {
+ if stream >= MAX_STREAMS || topic >= MAX_TOPICS || partition >= MAX_PARTITIONS {
+ return None;
}
+ Some(Self(
+ (stream as u64) << STREAM_SHIFT
+ | (topic as u64) << TOPIC_SHIFT
+ | (partition as u64) << PARTITION_SHIFT,
+ ))
+ }
- Ok(())
+ /// `true` when `value` has no bits set above the packed range, so false for
+ /// the metadata sentinel.
+ ///
+ /// Strictly LOOSER than [`Self::new`], which also rejects any component at
+ /// or above its maximum: 48,576 partition-field values per `(stream, topic)`
+ /// pass here and can never come out of `new`. Not an admission gate.
+ #[inline]
+ #[must_use]
+ pub const fn is_packable(value: u64) -> bool {
+ value <= PACKED_NAMESPACE_MAX
}
}
@@ -150,19 +125,47 @@ impl IggyNamespace {
mod tests {
use super::{
IggyNamespace, MAX_PARTITIONS, MAX_STREAMS, MAX_TOPICS, METADATA_GROUP,
- NamespaceCapacityError, PACKED_NAMESPACE_BITS, PACKED_NAMESPACE_MAX,
+ PACKED_NAMESPACE_BITS, PACKED_NAMESPACE_MAX,
};
- // Both static facts (sentinel above the packed range; bit layout matches
- // module constants) are checked at compile time so any future tweak to
- // STREAM_BITS / TOPIC_BITS / PARTITION_BITS that closes the gap between
- // the packed range and the sentinel will fail to build.
+ /// Byte-exact packings that MUST NOT drift. The first four are reproduced
+ /// from the old 12/12/20 layout, so altering one re-encodes every group id
+ /// on the wire. The last two are only representable since the widening.
+ const GOLDEN: &[(usize, usize, usize, u64)] = &[
+ (0, 0, 0, 0x0),
+ (5, 2, 7, 0x5_0020_0007),
+ (1, 0, 0, 0x1_0000_0000),
+ (4095, 4095, 999_999, 0xFFF_FFFF_423F),
+ (4096, 0, 0, 0x1000_0000_0000),
+ (
+ MAX_STREAMS - 1,
+ MAX_TOPICS - 1,
+ MAX_PARTITIONS - 1,
+ 0xF_FFFF_FFFF_423F,
+ ),
+ ];
+
const _: () = {
assert!(METADATA_GROUP > PACKED_NAMESPACE_MAX);
- assert!(PACKED_NAMESPACE_BITS == 12 + 12 + 20);
assert!(PACKED_NAMESPACE_MAX == (1u64 << PACKED_NAMESPACE_BITS) - 1);
};
+ #[test]
+ fn given_known_triples_when_packing_should_match_the_golden_vectors() {
+ for &(stream, topic, partition, expected) in GOLDEN {
+ let namespace = IggyNamespace::new(stream, topic, partition);
+ assert_eq!(
+ namespace.inner(),
+ expected,
+ "({stream}, {topic}, {partition}) packed to {:#x}, expected {expected:#x}",
+ namespace.inner()
+ );
+ assert_eq!(namespace.stream_id(), stream);
+ assert_eq!(namespace.topic_id(), topic);
+ assert_eq!(namespace.partition_id(), partition);
+ }
+ }
+
#[test]
fn metadata_sentinel_cannot_collide_with_any_packable_namespace() {
assert!(!IggyNamespace::is_packable(METADATA_GROUP));
@@ -180,33 +183,40 @@ mod tests {
assert_ne!(max.inner(), METADATA_GROUP);
}
+ /// The regression this change exists for. The previous `new` masked, so the
+ /// first out-of-range slab key packed byte-identically to slab 0: same
+ /// shard, consensus group, directory and authorization scope, no error.
#[test]
- fn validates_default_namespace_capacity() {
- assert!(IggyNamespace::validate_capacity(MAX_STREAMS, MAX_TOPICS, MAX_PARTITIONS).is_ok());
+ fn given_out_of_range_components_when_packing_should_refuse_rather_than_alias() {
+ for (stream, topic, partition) in [
+ (MAX_STREAMS, 2, 7),
+ (0, MAX_TOPICS, 7),
+ (0, 2, MAX_PARTITIONS),
+ ] {
+ assert_eq!(
+ IggyNamespace::try_new(stream, topic, partition),
+ None,
+ "({stream}, {topic}, {partition}) must not pack"
+ );
+ }
+
+ let in_range = IggyNamespace::new(0, 2, 7);
+ assert!(IggyNamespace::try_new(MAX_STREAMS, 2, 7).is_none());
+ assert_eq!(in_range.stream_id(), 0);
}
+ /// The mask admits 48,576 ids the maximum forbids, so bound checks compare
+ /// against the constant.
#[test]
- fn rejects_zero_capacity_values() {
- assert_eq!(
- IggyNamespace::validate_capacity(0, MAX_TOPICS, MAX_PARTITIONS),
- Err(NamespaceCapacityError::ZeroStreams)
- );
- assert_eq!(
- IggyNamespace::validate_capacity(MAX_STREAMS, 0, MAX_PARTITIONS),
- Err(NamespaceCapacityError::ZeroTopics)
- );
- assert_eq!(
- IggyNamespace::validate_capacity(MAX_STREAMS, MAX_TOPICS, 0),
- Err(NamespaceCapacityError::ZeroPartitions)
- );
+ fn given_a_partition_id_between_the_maximum_and_the_mask_when_packing_should_refuse() {
+ assert!(IggyNamespace::try_new(0, 0, MAX_PARTITIONS - 1).is_some());
+ assert!(IggyNamespace::try_new(0, 0, MAX_PARTITIONS).is_none());
+ assert!(IggyNamespace::try_new(0, 0, super::PARTITION_MASK as usize).is_none());
}
#[test]
- fn rejects_capacity_that_exceeds_current_layout() {
- let err = IggyNamespace::validate_capacity(MAX_STREAMS + 1, MAX_TOPICS, MAX_PARTITIONS);
- assert!(matches!(
- err,
- Err(NamespaceCapacityError::ExceedsLayout { .. })
- ));
+ #[should_panic(expected = "exceeds the packed layout")]
+ fn given_an_out_of_range_stream_when_calling_new_should_panic() {
+ let _ = IggyNamespace::new(MAX_STREAMS, 0, 0);
}
}
diff --git a/core/simulator/src/workload/ops/create_partitions.rs b/core/simulator/src/workload/ops/create_partitions.rs
index 2fdc6a2c4..2ad2d3ed2 100644
--- a/core/simulator/src/workload/ops/create_partitions.rs
+++ b/core/simulator/src/workload/ops/create_partitions.rs
@@ -19,8 +19,9 @@
//!
//! Targets `Ok` (live topic), `StreamNotFound` (fabricated parent stream), or
//! `TopicNotFound` (live stream, fabricated topic). `InvalidPartitionsCount`
-//! not targeted (only reachable through partition-id overflow). A committed
-//! `Ok` grows the shadow's per-topic partition count, which
+//! not targeted (only reachable through partition-id overflow), and
+//! `PartitionIdSpaceExhausted` needs `MAX_PARTITIONS` ids consumed under one
+//! topic. A committed `Ok` grows the shadow's per-topic partition count, which
//! `delete_partitions` samples against.
use iggy_binary_protocol::RoutedRequestHeader;
@@ -83,6 +84,9 @@ pub fn sample(
Outcome::InvalidPartitionsCount => {
unreachable!("create_partitions does not target InvalidPartitionsCount")
}
+ Outcome::PartitionIdSpaceExhausted => {
+ unreachable!("create_partitions does not target PartitionIdSpaceExhausted")
+ }
}
}
diff --git a/core/simulator/src/workload/ops/create_stream.rs b/core/simulator/src/workload/ops/create_stream.rs
index 685301d5d..390069c36 100644
--- a/core/simulator/src/workload/ops/create_stream.rs
+++ b/core/simulator/src/workload/ops/create_stream.rs
@@ -49,9 +49,10 @@ pub fn sample(
Outcome::NameAlreadyExists => Some(Input {
name: shadow.pick_stream_name(prng)?,
}),
- // Not a targeted outcome (absent from `OUTCOMES`); the sim client
- // never sends an options block.
- Outcome::InvalidOptionValue => None,
+ // Not targeted (absent from `OUTCOMES`): the sim client sends no options
+ // block, and the slab ceiling needs `MAX_STREAMS` surviving creates in
+ // one run, far past any workload length.
+ Outcome::InvalidOptionValue | Outcome::TooManyStreams => None,
}
}
@@ -75,6 +76,8 @@ pub fn predicted_effect(input: &Input, outcome: Outcome) -> Effect {
Outcome::Ok => Effect::AddStream {
name: input.name.clone(),
},
- Outcome::NameAlreadyExists | Outcome::InvalidOptionValue => Effect::None,
+ Outcome::NameAlreadyExists | Outcome::InvalidOptionValue | Outcome::TooManyStreams => {
+ Effect::None
+ }
}
}
diff --git a/core/simulator/src/workload/ops/create_topic.rs b/core/simulator/src/workload/ops/create_topic.rs
index 8eff46fd6..3e9a12916 100644
--- a/core/simulator/src/workload/ops/create_topic.rs
+++ b/core/simulator/src/workload/ops/create_topic.rs
@@ -80,9 +80,14 @@ pub fn sample(
partitions_count,
})
}
- // Not a targeted outcome (absent from `OUTCOMES`); the sim client
- // only sends catalog keys with valid values.
- Outcome::InvalidOptionValue => None,
+ // Not targeted (absent from `OUTCOMES`): the sim client sends only valid
+ // catalog keys, the slab ceiling needs `MAX_TOPICS` creates under one
+ // stream, and the primary mints dense 0-based partition ids, so neither
+ // partition guard is reachable from the wire.
+ Outcome::InvalidOptionValue
+ | Outcome::TooManyTopics
+ | Outcome::InvalidPartitionsCount
+ | Outcome::PartitionIdSpaceExhausted => None,
}
}