You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by pi...@apache.org on 2025/11/19 16:43:16 UTC

(iggy) branch master updated: fix(server): fix inconsistent current_position of active segment (#2380)

This is an automated email from the ASF dual-hosted git repository.

piotr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iggy.git


The following commit(s) were added to refs/heads/master by this push:
     new a8e2341a4 fix(server): fix inconsistent current_position of active segment (#2380)
a8e2341a4 is described below

commit a8e2341a40832767975c5edcc9b8b7b15e487876
Author: Grzegorz Koszyk <11...@users.noreply.github.com>
AuthorDate: Wed Nov 19 17:43:12 2025 +0100

    fix(server): fix inconsistent current_position of active segment (#2380)
    
    This PR addresses concurrency issue - when committing journal
    `current_position` was in inconsistent state, which lead to incorrect
    index generation for pipelined batch.
    
    Also `Murmur3` hash does not deal well with generating hash for small
    integer values, leading to skewed `shard_id` assignment for partitions
    that are part of the same namespace. Fixed by discarding the lower bits
    of the generated hash, when calculating the shard assignment.
---
 core/server/src/bootstrap.rs                    |  2 ++
 core/server/src/shard/communication.rs          | 13 +++----------
 core/server/src/shard/system/partitions.rs      |  6 ++++--
 core/server/src/slab/streams.rs                 |  3 +--
 core/server/src/streaming/partitions/helpers.rs |  1 +
 core/server/src/streaming/segments/segment.rs   |  2 ++
 6 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/core/server/src/bootstrap.rs b/core/server/src/bootstrap.rs
index f7cec4668..5f7f771ac 100644
--- a/core/server/src/bootstrap.rs
+++ b/core/server/src/bootstrap.rs
@@ -539,6 +539,8 @@ pub async fn load_segments(
         segment.end_timestamp = end_timestamp;
         segment.end_offset = end_offset;
         segment.size = IggyByteSize::from(messages_size as u64);
+        // At segment load, set the current position to the size of the segment (No data is buffered yet).
+        segment.current_position = segment.size.as_bytes_u32();
         segment.sealed = true; // Persisted segments are assumed to be sealed
 
         if config.partition.validate_checksum {
diff --git a/core/server/src/shard/communication.rs b/core/server/src/shard/communication.rs
index c8daa0733..d7e73c5e4 100644
--- a/core/server/src/shard/communication.rs
+++ b/core/server/src/shard/communication.rs
@@ -187,20 +187,13 @@ impl IggyShard {
             })
             .collect()
     }
-
-    pub fn insert_shard_table_records(
-        &self,
-        records: impl IntoIterator<Item = (IggyNamespace, ShardId)>,
-    ) {
-        for (ns, shard_id) in records {
-            self.shards_table.insert(ns, shard_id);
-        }
-    }
 }
 
 // Utility function for shard assignment calculation
 pub fn calculate_shard_assignment(ns: &IggyNamespace, upperbound: u32) -> u16 {
     let mut hasher = Murmur3Hasher::default();
     hasher.write_u64(ns.inner());
-    (hasher.finish32() % upperbound) as u16
+    let hash = hasher.finish32();
+    // Murmur3 has problems with weak lower bits for small integer inputs, so we use bits from the middle.
+    ((hash >> 16) % upperbound) as u16
 }
diff --git a/core/server/src/shard/system/partitions.rs b/core/server/src/shard/system/partitions.rs
index d561581d7..b95847303 100644
--- a/core/server/src/shard/system/partitions.rs
+++ b/core/server/src/shard/system/partitions.rs
@@ -204,8 +204,10 @@ impl IggyShard {
                 "create_partitions_bypass_auth: partition mismatch ID, wrong creation order ?!"
             );
             let ns = IggyNamespace::new(numeric_stream_id, numeric_topic_id, id);
-            let shard_id = ShardId::new(calculate_shard_assignment(&ns, shards_count));
-            self.insert_shard_table_record(ns, shard_id);
+            let shard_id = self.find_shard_table_record(&ns).unwrap_or_else(|| {
+                tracing::warn!("WARNING: missing shard table record for namespace: {:?}, in the event handler for `CreatedPartitions` event.", ns);
+                ShardId::new(calculate_shard_assignment(&ns, shards_count))
+            });
             if self.id == *shard_id {
                 self.init_log(stream_id, topic_id, id).await?;
             }
diff --git a/core/server/src/slab/streams.rs b/core/server/src/slab/streams.rs
index 12176bd1c..222b9f0e4 100644
--- a/core/server/src/slab/streams.rs
+++ b/core/server/src/slab/streams.rs
@@ -200,8 +200,7 @@ impl MainOps for Streams {
 
         let current_position =
             self.with_partition_by_id(stream_id, topic_id, partition_id, |(.., log)| {
-                (log.journal().inner().size.as_bytes_u64()
-                    + log.active_segment().size.as_bytes_u64()) as u32
+                log.active_segment().current_position
             });
         let (segment_start_offset, message_deduplicator) = self.with_partition_by_id(
             stream_id,
diff --git a/core/server/src/streaming/partitions/helpers.rs b/core/server/src/streaming/partitions/helpers.rs
index 1f2c85633..54cfda89a 100644
--- a/core/server/src/streaming/partitions/helpers.rs
+++ b/core/server/src/streaming/partitions/helpers.rs
@@ -425,6 +425,7 @@ pub fn append_to_journal(
             root.set_should_increment_offset(true);
             offset.store(last_offset, Ordering::Relaxed);
         }
+        log.active_segment_mut().current_position += batch_messages_size;
 
         Ok((journal_messages_count, journal_size))
     }
diff --git a/core/server/src/streaming/segments/segment.rs b/core/server/src/streaming/segments/segment.rs
index 14bd8e008..aa72ac37d 100644
--- a/core/server/src/streaming/segments/segment.rs
+++ b/core/server/src/streaming/segments/segment.rs
@@ -24,6 +24,7 @@ pub struct Segment {
     pub message_expiry: IggyExpiry,
     pub start_timestamp: u64,
     pub end_timestamp: u64,
+    pub current_position: u32,
     pub start_offset: u64,
     pub end_offset: u64,
     pub size: IggyByteSize,     // u64
@@ -63,6 +64,7 @@ impl Segment {
             start_offset,
             end_offset: start_offset,
             size: IggyByteSize::default(),
+            current_position: 0,
         }
     }