Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions mesh/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,23 +390,7 @@ impl MeshController {

if !all_updates.is_empty() {
for (store_type, updates) in all_updates {
let proto_store_type = match store_type {
LocalStoreType::Membership => {
super::service::gossip::StoreType::Membership as i32
}
LocalStoreType::App => {
super::service::gossip::StoreType::App as i32
}
LocalStoreType::Worker => {
super::service::gossip::StoreType::Worker as i32
}
LocalStoreType::Policy => {
super::service::gossip::StoreType::Policy as i32
}
LocalStoreType::RateLimit => {
super::service::gossip::StoreType::RateLimit as i32
}
};
let proto_store_type = store_type.to_proto();

let incremental_update = StreamMessage {
message_type: StreamMessageType::IncrementalUpdate as i32,
Expand Down Expand Up @@ -499,7 +483,6 @@ impl MeshController {
if let Err(err) = stores.app.insert(
app_state.key.clone(),
app_state,
state_update.actor.clone(),
) {
log::warn!(error = %err, "Failed to apply app state update");
}
Expand All @@ -515,7 +498,6 @@ impl MeshController {
if let Err(err) = stores.membership.insert(
membership_state.name.clone(),
membership_state,
state_update.actor.clone(),
) {
log::warn!(error = %err, "Failed to apply membership state update");
}
Expand Down
75 changes: 32 additions & 43 deletions mesh/src/crdt_kv/crdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ use super::{
/// Value metadata for CRDT OR-Map
#[derive(Debug, Clone, PartialEq, Eq)]
struct ValueMetadata {
value: Vec<u8>,
timestamp: u64,
replica_id: ReplicaId,
is_tombstone: bool, // Marks if this version is a tombstone (deletion)
}

impl ValueMetadata {
fn new(value: Vec<u8>, timestamp: u64, replica_id: ReplicaId) -> Self {
fn new(timestamp: u64, replica_id: ReplicaId) -> Self {
Self {
value,
timestamp,
replica_id,
is_tombstone: false,
Expand All @@ -35,7 +33,6 @@ impl ValueMetadata {

fn tombstone(timestamp: u64, replica_id: ReplicaId) -> Self {
Self {
value: Vec::new(),
timestamp,
replica_id,
is_tombstone: true,
Expand Down Expand Up @@ -119,7 +116,7 @@ impl CrdtOrMap {
let key_guard = key_lock.lock();

let timestamp = self.clock.tick();
let result = if self.record_insert_metadata(&key, &value, timestamp, self.replica_id) {
let result = if self.record_insert_metadata(&key, timestamp, self.replica_id) {
let mut prev = None;
let value_for_operation = value.clone();
let _ = self.store.upsert(key.clone(), |current| {
Expand Down Expand Up @@ -158,22 +155,21 @@ impl CrdtOrMap {
let updated_value = updater(current_value.as_deref());
let timestamp = self.clock.tick();

let result =
if self.record_insert_metadata(&key, &updated_value, timestamp, self.replica_id) {
let operation = Operation::insert(
key.clone(),
updated_value.clone(),
timestamp,
self.replica_id,
);
let result = if self.record_insert_metadata(&key, timestamp, self.replica_id) {
let operation = Operation::insert(
key.clone(),
updated_value.clone(),
timestamp,
self.replica_id,
);

self.store.insert(key.clone(), updated_value.clone());
self.operation_log.write().append(operation);
self.store.insert(key.clone(), updated_value.clone());
self.operation_log.write().append(operation);

updated_value
} else {
self.store.get(&key).unwrap_or_default()
};
updated_value
} else {
self.store.get(&key).unwrap_or_default()
};

drop(key_guard);
self.try_cleanup_key_lock(&key, &key_lock);
Expand All @@ -199,22 +195,21 @@ impl CrdtOrMap {
};
let timestamp = self.clock.tick();

let result =
if self.record_insert_metadata(&key, &updated_value, timestamp, self.replica_id) {
let operation = Operation::insert(
key.clone(),
updated_value.clone(),
timestamp,
self.replica_id,
);
let result = if self.record_insert_metadata(&key, timestamp, self.replica_id) {
let operation = Operation::insert(
key.clone(),
updated_value.clone(),
timestamp,
self.replica_id,
);

self.store.insert(key.clone(), updated_value.clone());
self.operation_log.write().append(operation);
self.store.insert(key.clone(), updated_value.clone());
self.operation_log.write().append(operation);

updated_value
} else {
self.store.get(&key).unwrap_or_default()
};
updated_value
} else {
self.store.get(&key).unwrap_or_default()
};

drop(key_guard);
self.try_cleanup_key_lock(&key, &key_lock);
Expand All @@ -241,7 +236,7 @@ impl CrdtOrMap {

let (result, changed) = if let Some(updated_value) = maybe_updated_value {
let timestamp = self.clock.tick();
if self.record_insert_metadata(&key, &updated_value, timestamp, self.replica_id) {
if self.record_insert_metadata(&key, timestamp, self.replica_id) {
let operation = Operation::insert(
key.clone(),
updated_value.clone(),
Expand Down Expand Up @@ -402,7 +397,7 @@ impl CrdtOrMap {
let key_lock = self.key_lock_for(key);
let key_guard = key_lock.lock();

if self.record_insert_metadata(key, &value, timestamp, replica_id) {
if self.record_insert_metadata(key, timestamp, replica_id) {
self.store.insert(key.to_string(), value);
}

Expand All @@ -421,14 +416,8 @@ impl CrdtOrMap {
}
}

fn record_insert_metadata(
&self,
key: &str,
value: &[u8],
timestamp: u64,
replica_id: ReplicaId,
) -> bool {
let new_metadata = ValueMetadata::new(value.to_vec(), timestamp, replica_id);
fn record_insert_metadata(&self, key: &str, timestamp: u64, replica_id: ReplicaId) -> bool {
let new_metadata = ValueMetadata::new(timestamp, replica_id);

match self.metadata.entry(key.to_string()) {
MapEntry::Occupied(mut entry) => {
Expand Down
18 changes: 4 additions & 14 deletions mesh/src/crdt_kv/operation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{HashMap, HashSet};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -111,16 +111,6 @@ impl OperationLog {
&self.operations
}

/// Serialize to JSON
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}

/// Deserialize from JSON
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}

/// Serialize to JSON bytes
pub fn to_bytes(&self) -> Result<Vec<u8>, serde_json::Error> {
serde_json::to_vec(self)
Expand All @@ -141,8 +131,8 @@ impl OperationLog {
self.operations.is_empty()
}

fn latest_operations_by_key(&self) -> BTreeMap<String, Operation> {
let mut latest_by_key: BTreeMap<String, Operation> = BTreeMap::new();
fn latest_operations_by_key(&self) -> HashMap<String, Operation> {
let mut latest_by_key: HashMap<String, Operation> = HashMap::new();

for operation in &self.operations {
let key = operation.key().to_string();
Expand Down Expand Up @@ -182,7 +172,7 @@ impl OperationLog {
}

/// Build a latest-state snapshot and clear the operation log.
pub fn snapshot_and_truncate(&mut self) -> BTreeMap<String, Operation> {
pub fn snapshot_and_truncate(&mut self) -> HashMap<String, Operation> {
let snapshot = self.latest_operations_by_key();
self.operations.clear();
snapshot
Expand Down
14 changes: 7 additions & 7 deletions mesh/src/crdt_kv/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct LamportClock {
impl Clone for LamportClock {
fn clone(&self) -> Self {
Self {
counter: AtomicU64::new(self.counter.load(Ordering::SeqCst)),
counter: AtomicU64::new(self.counter.load(Ordering::Acquire)),
}
}
}
Expand All @@ -63,14 +63,14 @@ impl LamportClock {

/// Increment and return new timestamp
pub fn tick(&self) -> u64 {
let mut current = self.counter.load(Ordering::SeqCst);
let mut current = self.counter.load(Ordering::Acquire);
loop {
let new_value = current.saturating_add(1);
match self.counter.compare_exchange(
current,
new_value,
Ordering::SeqCst,
Ordering::SeqCst,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => return new_value,
Err(actual) => current = actual,
Expand All @@ -80,14 +80,14 @@ impl LamportClock {

/// Update clock to max(local, remote) + 1
pub fn update(&self, remote_timestamp: u64) -> u64 {
let mut current = self.counter.load(Ordering::SeqCst);
let mut current = self.counter.load(Ordering::Acquire);
loop {
let new_value = current.max(remote_timestamp).saturating_add(1);
match self.counter.compare_exchange(
current,
new_value,
Ordering::SeqCst,
Ordering::SeqCst,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => return new_value,
Err(actual) => current = actual,
Expand Down
6 changes: 3 additions & 3 deletions mesh/src/crdt_kv/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ fn test_operation_log_json_serialization() {

let log = map.get_operation_log();

// Serialize to JSON
let json = log.to_json().unwrap();
// Serialize to bytes
let bytes = log.to_bytes().unwrap();

// Deserialize
let deserialized_log = OperationLog::from_json(&json).unwrap();
let deserialized_log = OperationLog::from_bytes(&bytes).unwrap();
assert_eq!(log.len(), deserialized_log.len());
}

Expand Down
Loading
Loading