.tasks/core/LSYNC-013-sync-protocol-handler.md
Create sync protocol handler supporting the new hybrid model:
No leader/follower distinction - all devices are peers.
Old: Leader/follower with sequence-based sync New: Peer-to-peer with hybrid strategy
Benefits:
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SyncMessage {
// ===== State-Based (Device-Owned Data) =====
/// Broadcast current state of device-owned resource
StateChange {
model_type: String, // "location", "entry", "volume"
record_uuid: Uuid,
device_id: Uuid, // Owner
data: serde_json::Value,
timestamp: DateTime<Utc>,
},
/// Batch state changes (efficiency)
StateBatch {
model_type: String,
device_id: Uuid,
records: Vec<StateRecord>,
},
/// Request full state from peer
StateRequest {
model_types: Vec<String>,
device_id: Option<Uuid>, // Specific device or all
since: Option<DateTime>, // Incremental
},
/// Response with state
StateResponse {
model_type: String,
device_id: Uuid,
records: Vec<StateRecord>,
has_more: bool,
},
// ===== Log-Based (Shared Resources) =====
/// Broadcast shared resource change (with HLC)
SharedChange {
hlc: HLC,
model_type: String, // "tag", "album", "user_metadata"
record_uuid: Uuid,
change_type: ChangeType, // Insert/Update/Delete
data: serde_json::Value,
},
/// Batch shared changes
SharedChangeBatch {
entries: Vec<SharedChangeEntry>,
},
/// Request shared changes since HLC
SharedChangeRequest {
since_hlc: Option<HLC>,
limit: usize,
},
/// Response with shared changes
SharedChangeResponse {
entries: Vec<SharedChangeEntry>,
has_more: bool,
},
/// Acknowledge received shared changes (for pruning)
AckSharedChanges {
from_device: Uuid,
up_to_hlc: HLC,
},
}
core/src/service/network/protocol/sync/messages.rs - New SyncMessage enumsync/handler.rs - Handle both state and log-based messagessync/state.rs - State-based sync logicsync/shared.rs - Log-based sync with HLC/spacedrive/sync/2.0.0 (version bump!)// core/src/service/network/protocol/sync/handler.rs
pub struct SyncProtocolHandler {
library_id: Uuid,
shared_changes_db: Arc<SharedChangesDb>, // My log of shared changes
device_registry: Arc<RwLock<DeviceRegistry>>,
event_bus: Arc<EventBus>,
hlc_generator: Arc<Mutex<HLCGenerator>>,
// No role field!
}
impl ProtocolHandler for SyncProtocolHandler {
const ALPN: &'static [u8] = b"/spacedrive/sync/2.0.0";
async fn handle_stream(
&self,
stream: BiStream,
peer_device_id: Uuid,
) -> Result<(), NetworkingError>;
}
impl SyncProtocolHandler {
/// Broadcast state change to all peers
pub async fn broadcast_state_change(
&self,
change: StateChange,
) -> Result<(), SyncError>;
/// Broadcast shared change to all peers
pub async fn broadcast_shared_change(
&self,
entry: SharedChangeEntry,
) -> Result<(), SyncError>;
/// Request state from peer
pub async fn request_state(
&self,
peer_id: Uuid,
request: StateRequest,
) -> Result<StateResponse, SyncError>;
/// Request shared changes from peer
pub async fn request_shared_changes(
&self,
peer_id: Uuid,
since_hlc: Option<HLC>,
) -> Result<Vec<SharedChangeEntry>, SyncError>;
/// Handle incoming message
async fn handle_message(
&self,
msg: SyncMessage,
stream: &mut BiStream,
from_device: Uuid,
) -> Result<(), SyncError>;
}
Successfully implemented in core/src/service/network/protocol/sync/handler.rs:
Message Handling:
StateChange and StateBatch - Routes to PeerSync for state-based syncSharedChange and SharedChangeBatch - Routes to PeerSync for log-based syncStateRequest / StateResponse - Stub for backfill (TODO)SharedChangeRequest / SharedChangeResponse - Stub for catch-up (TODO)AckSharedChanges - Routes to PeerSync for log pruningHeartbeat - Basic echo responseKey Features:
core/src/infra/sync/NEW_SYNC.md