rmqtt 0.23.1

MQTT Server for v3.1, v3.1.1 and v5.0 protocols
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
//! MQTT Session Management & Message Routing Core
//!
//! Provides a distributed session management system with asynchronous message routing capabilities
//! for MQTT brokers. Key components implement thread-safe client state management and efficient
//! publish/subscribe message distribution across cluster nodes.
//!
//! ## Core Functionality
//! 1. **Session Lifecycle Management**:
//!    - Connection state tracking with atomic operations
//!    - Graceful session cleanup through `kick` mechanism
//!    - Subscription persistence across reconnects
//!
//! 2. **Message Routing**:
//!    - Topic-based message forwarding with QoS handling
//!    - Shared subscription support with load balancing
//!    - Cross-node message propagation using gRPC integration
//!
//! 3. **Cluster Coordination**:
//!    - Distributed client state tracking
//!    - Health checking and node status monitoring
//!    - Subscription synchronization across nodes
//!
//! ## Key Components
//! - `Entry` Trait: Atomic session operations including:
//!   - Subscription management (add/remove)
//!   - Message publishing with backpressure
//!   - Connection state transitions
//!   
//! - `Shared` Trait: Cluster-wide coordination implementing:
//!   - Concurrent client state storage using `DashMap`
//!   - Message routing with error handling
//!   - Subscription query interface
//!
//! - `DefaultShared`: Production-ready implementation featuring:
//!   - Async mutexes for state protection
//!   - gRPC client management for cluster communication
//!   - Message persistence integration
//!
//! ## Design Characteristics
//! 1. **Concurrency Model**:
//!    - Lock-free reads with copy-on-write patterns
//!    - Fine-grained locking using `tokio::sync::Mutex`
//!    - MPSC channels for inter-task communication
//!
//! 2. **Failure Handling**:
//!    - Timeout-based operation guards
//!    - Atomic transaction rollbacks
//!    - Graceful degradation during node failures
//!
//! 3. **Extensibility**:
//!    - Pluggable storage backends via `msgstore` feature
//!    - Router abstraction for custom subscription logic
//!    - Health monitoring hooks
//!
//! Typical usage includes:
//! - Maintaining client sessions with clean start/restore
//! - Distributing QoS 1/2 messages across cluster nodes
//! - Handling shared subscriptions with group coordination
//! - Performing cluster-wide subscription queries
//!
//! The implementation leverages Rust's ownership system and async/await patterns to achieve:
//! - Zero-copy message forwarding in common cases
//! - Linear scalability with connection count
//! - Sub-millisecond latency for core operations

use std::convert::From as _f;
use std::sync::Arc;
use std::time::Duration;

use anyhow::anyhow;
use async_trait::async_trait;
#[allow(unused_imports)]
use bitflags::Flags;
use tokio::sync::oneshot;
use tokio::sync::{Mutex, OwnedMutexGuard};

use crate::context::ServerContext;
#[cfg(feature = "grpc")]
use crate::grpc::GrpcClients;
use crate::session::Session;
use crate::types::*;
use crate::Result;

/// Represents a client session entry in the shared state.
///
/// Provides atomic operations for session lifecycle management,
/// including connection state transitions, subscription changes,
/// and message publishing. Each entry is identified by a unique
/// [`Id`] and can be locked for exclusive access.
#[async_trait]
pub trait Entry: Sync + Send {
    /// Attempt to acquire an exclusive lock on this entry.
    async fn try_lock(&self) -> Result<Box<dyn Entry>>;
    /// Return the unique identifier for this entry.
    fn id(&self) -> Id;
    /// Check whether the stored session ID matches this entry's ID.
    fn id_same(&self) -> Option<bool>;
    /// Replace the session and its associated sender channel.
    async fn set(&mut self, session: Session, tx: Tx) -> Result<()>;
    /// Remove the session entry and return the previous session and sender.
    async fn remove(&mut self) -> Result<Option<(Session, Tx)>>;
    /// Remove the session entry matching the given [`Id`].
    async fn remove_with(&mut self, id: &Id) -> Result<Option<(Session, Tx)>>;
    /// Forcefully disconnect the client session.
    ///
    /// # Arguments
    /// * `clean_start` - Whether to treat this as a clean start disconnect.
    /// * `clear_subscriptions` - Whether to remove all subscriptions.
    /// * `is_admin` - Whether the kick originates from an admin action.
    async fn kick(
        &mut self,
        clean_start: bool,
        clear_subscriptions: bool,
        is_admin: IsAdmin,
    ) -> Result<OfflineSession>;
    /// Check whether the client is currently online.
    async fn online(&self) -> bool;
    /// Check whether the client connection is active.
    async fn is_connected(&self) -> bool;
    /// Retrieve an optional reference to the stored [`Session`].
    fn session(&self) -> Option<Session>;
    /// Check whether an entry exists in the shared state.
    fn exist(&self) -> bool;
    /// Retrieve the sender channel for delivering messages to this client.
    fn tx(&self) -> Option<Tx>;
    /// Subscribe the client to a topic filter.
    async fn subscribe(&self, subscribe: &Subscribe) -> Result<SubscribeReturn>;
    /// Unsubscribe the client from a topic filter.
    async fn unsubscribe(&self, unsubscribe: &Unsubscribe) -> Result<bool>;
    /// Publish a message to this client.
    async fn publish(&self, from: From, p: Publish) -> std::result::Result<(), (From, Publish, Reason)>;
    /// List all current subscriptions for this client.
    async fn subscriptions(&self) -> Option<Vec<SubsSearchResult>>;
}

/// Cluster-wide shared session state and message routing.
///
/// Provides the core interface for distributed session management,
/// subscription tracking, and publish message forwarding across
/// cluster nodes. Implementations manage client state storage,
/// gRPC-based inter-node communication, and message delivery.
///
/// Async callback trait for processing messages loaded by `message_load_with`.
///
/// Implementations receive batches of loaded messages and can use `await`
/// internally — unlike the sync `Arc<dyn Fn(...)>` approach.
#[cfg(feature = "msgstore")]
#[async_trait]
pub trait MessageLoadCallback: Send + Sync + 'static {
    /// Called for each batch of loaded messages.
    ///
    /// # Arguments
    /// * `msgs` - A batch of messages from local storage or a remote peer.
    async fn on_messages(&self, msgs: Vec<(MsgID, From, Publish)>) -> Result<()>;
}

#[cfg(feature = "retain")]
#[async_trait]
pub trait RetainLoadCallback: Send + Sync + 'static {
    /// Called with the loaded batch of retain messages (topic + retain data).
    /// Returns the extracted (NodeId, MsgID) pairs for caller-side tracking.
    async fn on_retains(&self, retains: Vec<(TopicName, Retain)>) -> Result<Vec<(NodeId, MsgID)>>;
}

#[async_trait]
pub trait Shared: Sync + Send {
    /// Retrieve the session entry associated with the given client [`Id`].
    fn entry(&self, id: Id) -> Box<dyn Entry>;

    /// Check whether a session for the given `client_id` exists in the shared state.
    fn exist(&self, client_id: &str) -> bool;

    /// Route a publish message to all matching subscribers on this node.
    ///
    /// Returns the number of subscribers that received the message,
    /// or a tuple of (subscriber_count, delivery_failures).
    /// When `msg_id` is provided, records subscriber IDs via `mark_forwarded`
    /// after successful forwarding, ensuring subscriber tracking is tied to
    /// the forwarding result rather than deferred to a later `store()` call.
    async fn forwards(
        &self,
        msg_id: Option<MsgID>,
        from: From,
        publish: Publish,
    ) -> std::result::Result<ForwardedCount, (ForwardedCount, Vec<(To, From, Publish, Reason)>)>;

    /// Route a publish message and return both the subscription relation map and subscriber IDs.
    ///
    /// Unlike [`forwards`](Self::forwards), this also separates out shared subscription groups
    /// in the returned [`SubRelationsMap`] for caller-side processing.
    async fn forwards_and_get_shareds(
        &self,
        from: From,
        publish: Publish,
    ) -> std::result::Result<
        (SubRelationsMap, ForwardedRecipients),
        (ForwardedRecipients, Vec<(To, From, Publish, Reason)>),
    >;

    /// Forward a publish message to a specific set of subscription relations.
    ///
    /// Returns the list of subscriber sessions that received the message,
    /// or a tuple of (successful_sessions, delivery_failures).
    async fn forwards_to(
        &self,
        from: From,
        publish: &Publish,
        relations: SubRelations,
        msg_id: Option<MsgID>,
    ) -> std::result::Result<ForwardedRecipients, (ForwardedRecipients, Vec<(To, From, Publish, Reason)>)>;

    /// Return an iterator over all client session entries.
    fn iter(&self) -> Box<dyn Iterator<Item = Box<dyn Entry>> + Sync + Send + '_>;

    /// Pick a random connected session, if any exist.
    fn random_session(&self) -> Option<Session>;

    /// Retrieve the session status for a specific client ID.
    async fn session_status(&self, client_id: &str) -> Option<SessionStatus>;

    /// Return the total number of client states currently tracked.
    async fn client_states_count(&self) -> usize;

    /// Return the number of active sessions.
    fn sessions_count(&self) -> usize;

    /// Query subscriptions matching the given search parameters.
    async fn query_subscriptions(&self, q: &SubsSearchParams) -> Vec<SubsSearchResult>;

    /// Return the total number of subscriptions across all sessions.
    async fn subscriptions_count(&self) -> usize;

    ///This node is not included
    #[inline]
    #[cfg(feature = "grpc")]
    fn get_grpc_clients(&self) -> GrpcClients {
        Arc::new(HashMap::default())
    }

    #[inline]
    fn grpc_enable(&self) -> bool {
        #[cfg(feature = "grpc")]
        {
            !self.get_grpc_clients().is_empty()
        }
        #[cfg(not(feature = "grpc"))]
        false
    }

    #[inline]
    fn node_name(&self, id: NodeId) -> String {
        format!("{id}@127.0.0.1")
    }

    #[inline]
    async fn check_health(&self) -> Result<HealthInfo> {
        Ok(HealthInfo::default())
    }

    #[inline]
    async fn health_status(&self) -> Result<NodeHealthStatus> {
        Ok(NodeHealthStatus::default())
    }

    #[inline]
    fn operation_is_busy(&self) -> bool {
        false
    }

    #[cfg(feature = "msgstore")]
    async fn message_load(
        &self,
        client_id: &str,
        topic_filter: &str,
        group: Option<&SharedGroup>,
    ) -> Result<Vec<(MsgID, From, Publish)>>;

    #[cfg(feature = "msgstore")]
    async fn message_load_with(
        &self,
        client_id: &str,
        topic_filter: &str,
        group: Option<&SharedGroup>,
        cb: Arc<dyn MessageLoadCallback>,
    ) -> Result<()> {
        let msgs = self.message_load(client_id, topic_filter, group).await?;
        cb.on_messages(msgs).await
    }

    #[cfg(feature = "retain")]
    async fn retain_load_with(
        &self,
        topic_filter: &TopicFilter,
        cb: Arc<dyn RetainLoadCallback>,
    ) -> Result<Vec<(NodeId, MsgID)>>;

    /// Broadcast a retained message update to all cluster peers.
    ///
    /// Called after the local node has written a retain: propagates the new
    /// value to remote nodes so they can keep their local stores up to date.
    /// Default implementation is a no-op (single-node mode).
    #[cfg(feature = "retain")]
    async fn retain_set_broadcast(
        &self,
        topic: &TopicName,
        retain: &Retain,
        expiry_interval: Option<Duration>,
    ) -> Result<()> {
        let _ = (topic, retain, expiry_interval);
        Ok(())
    }

    /// Mark a message as successfully forwarded to the given subscribers.
    /// Delegates to [`MessageManager::mark_forwarded`] so that forwarding
    /// results are persisted and subsequent `get()` calls will not redeliver
    /// the same message to these clients.
    ///
    /// # Arguments
    /// * `msg_id` - Unique identifier of the forwarded message.
    /// * `subscribers` - Subscribers (with optional shared group info) that
    ///   have received the message, typically collected from shared subscription
    ///   forwarding.
    #[cfg(feature = "msgstore")]
    async fn message_mark_forwarded(
        &self,
        from_node_id: NodeId,
        msg_id: MsgID,
        recipients: ForwardedRecipients,
    ) -> Result<()>;
}

/// A locked reference to a client session entry.
///
/// Wraps an [`Entry`] with an optional mutex guard to ensure
/// exclusive access during session operations. Used internally
/// by [`DefaultShared`] to serialize access to a specific client's state.
pub struct LockEntry {
    id: Id,
    shared: DefaultShared,
    _locker: Option<OwnedMutexGuard<()>>,
}

impl Drop for LockEntry {
    #[inline]
    fn drop(&mut self) {
        if self._locker.is_some() {
            let _t = self.shared.lockers.remove(&self.id.client_id);
            log::debug!("{:?} LockEntry Drop ..., {}", self.id, _t.is_some());
        }
    }
}

impl LockEntry {
    /// Create a new `LockEntry` wrapping the given session [`Id`].
    ///
    /// # Arguments
    /// * `id` - The unique session identifier.
    /// * `shared` - Reference to the shared state manager.
    /// * `_locker` - Optional mutex guard for exclusive access.
    #[inline]
    pub fn new(id: Id, shared: DefaultShared, _locker: Option<OwnedMutexGuard<()>>) -> Self {
        Self { id, shared, _locker }
    }

    #[inline]
    async fn _unsubscribe(&self, id: Id, topic_filter: &str) -> Result<()> {
        self.shared.context().extends.router().await.remove(topic_filter, id).await?;
        if let Some(s) = self.session() {
            s.subscriptions_remove(topic_filter).await?;
        }
        Ok(())
    }

    /// Remove a peer entry identified by `with_id`, optionally clearing its subscriptions.
    #[inline]
    pub async fn _remove_with(&mut self, clear_subscriptions: bool, with_id: &Id) -> Option<(Session, Tx)> {
        if let Some((_, peer)) =
            { self.shared.peers.remove_if(&self.id.client_id, |_, entry| &entry.s.id == with_id) }
        {
            if clear_subscriptions {
                let topic_filters = peer.s.subscriptions().await.ok()?.to_topic_filters().await;
                for topic_filter in topic_filters {
                    if let Err(e) = self._unsubscribe(peer.s.id.clone(), &topic_filter).await {
                        log::warn!(
                            "{:?} remove._unsubscribe, topic_filter: {}, {:?}",
                            self.id,
                            topic_filter,
                            e
                        );
                    }
                }
            }
            log::debug!("{:?} remove ...", self.id);
            Some((peer.s, peer.tx))
        } else {
            None
        }
    }

    #[inline]
    pub async fn _remove(&mut self, clear_subscriptions: bool) -> Option<(Session, Tx)> {
        if let Some((_, peer)) = { self.shared.peers.remove(&self.id.client_id) } {
            if clear_subscriptions {
                let topic_filters = peer.s.subscriptions().await.ok()?.to_topic_filters().await;
                for topic_filter in topic_filters {
                    if let Err(e) = self._unsubscribe(peer.s.id.clone(), &topic_filter).await {
                        log::warn!(
                            "{:?} remove._unsubscribe, topic_filter: {}, {:?}",
                            self.id,
                            topic_filter,
                            e
                        );
                    }
                }
            }
            log::debug!("{:?} remove ...", self.id);
            Some((peer.s, peer.tx))
        } else {
            None
        }
    }
}

#[async_trait]
impl Entry for LockEntry {
    #[inline]
    async fn try_lock(&self) -> Result<Box<dyn Entry>> {
        log::debug!("{:?} LockEntry.try_lock", self.id);
        let locker = self
            .shared
            .lockers
            .entry(self.id.client_id.clone())
            .or_insert(Arc::new(Mutex::new(())))
            .clone()
            .try_lock_owned()?;
        Ok(Box::new(LockEntry::new(self.id.clone(), self.shared.clone(), Some(locker))))
    }

    #[inline]
    fn id(&self) -> Id {
        self.id.clone()
    }

    #[inline]
    fn id_same(&self) -> Option<bool> {
        self.shared.peers.get(&self.id.client_id).map(|peer| peer.s.id == self.id)
    }

    #[inline]
    async fn set(&mut self, s: Session, tx: Tx) -> Result<()> {
        self.shared.peers.insert(self.id.client_id.clone(), EntryItem { s, tx });
        Ok(())
    }

    #[inline]
    async fn remove(&mut self) -> Result<Option<(Session, Tx)>> {
        Ok(self._remove(true).await)
    }

    #[inline]
    async fn remove_with(&mut self, id: &Id) -> Result<Option<(Session, Tx)>> {
        Ok(self._remove_with(true, id).await)
    }

    #[inline]
    async fn kick(
        &mut self,
        clean_start: bool,
        clear_subscriptions: bool,
        is_admin: IsAdmin,
    ) -> Result<OfflineSession> {
        log::debug!(
            "{:?} LockEntry kick ..., clean_start: {}, clear_subscriptions: {}, is_admin: {}",
            self.session().map(|s| s.id.clone()),
            clean_start,
            clear_subscriptions,
            is_admin
        );

        if let Some(peer_tx) = self.tx().filter(|tx| !tx.is_closed()) {
            let (tx, rx) = oneshot::channel();
            if let Ok(()) = peer_tx.unbounded_send(Message::Kick(tx, self.id.clone(), clean_start, is_admin))
            {
                match tokio::time::timeout(Duration::from_secs(5), rx).await {
                    Ok(Ok(())) => {
                        log::debug!("{:?} kicked, from {:?}", self.id, self.session().map(|s| s.id.clone()));
                    }
                    Ok(Err(e)) => {
                        log::warn!(
                            "{:?} kick, recv result is {:?}, from {:?}",
                            self.id,
                            e,
                            self.session().map(|s| s.id.clone())
                        );
                        return Err(anyhow!(format!("recv kick result is {:?}", e)));
                    }
                    Err(_) => {
                        log::warn!(
                            "{:?} kick, recv result is Timeout, from {:?}",
                            self.id,
                            self.session().map(|s| s.id.clone())
                        );
                    }
                }
            }
        }

        if let Some((s, _)) = self._remove(clear_subscriptions).await {
            if clean_start {
                Ok(OfflineSession::Exist(None))
            } else {
                match s.to_offline_info().await {
                    Ok(offline_info) => Ok(OfflineSession::Exist(Some(offline_info))),
                    Err(e) => {
                        log::error!("get offline info error, {e}");
                        Ok(OfflineSession::Exist(None))
                    }
                }
            }
        } else {
            Ok(OfflineSession::NotExist)
        }
    }

    #[inline]
    async fn online(&self) -> bool {
        self.is_connected().await
    }

    #[inline]
    async fn is_connected(&self) -> bool {
        if let Some(entry) = self.shared.peers.get(&self.id.client_id) {
            entry.s.connected().await.unwrap_or_default()
        } else {
            false
        }
    }

    #[inline]
    fn session(&self) -> Option<Session> {
        self.shared.peers.get(&self.id.client_id).map(|peer| peer.s.clone())
    }

    #[inline]
    fn exist(&self) -> bool {
        self.shared.peers.contains_key(&self.id.client_id)
    }

    #[inline]
    fn tx(&self) -> Option<Tx> {
        self.shared.peers.get(&self.id.client_id).map(|peer| peer.tx.clone())
    }

    #[inline]
    async fn subscribe(&self, sub: &Subscribe) -> Result<SubscribeReturn> {
        let scx = self.shared.context();
        let peer = self
            .shared
            .peers
            .get(&self.id.client_id)
            .map(|peer| peer.value().clone())
            .ok_or_else(|| anyhow!("session is not exist"))?;

        let this_node_id = scx.node.id();
        let node_id = peer.s.id.node_id;
        assert_eq!(
            node_id, this_node_id,
            "session node exception, session node id is {node_id}, this node id is {this_node_id}"
        );

        scx.extends.router().await.add(&sub.topic_filter, self.id.clone(), sub.opts.clone()).await?;
        let prev_opts = peer.s.subscriptions_add(sub.topic_filter.clone(), sub.opts.clone()).await?;
        Ok(SubscribeReturn::new_success(sub.opts.qos(), prev_opts))
    }

    #[inline]
    async fn unsubscribe(&self, unsubscribe: &Unsubscribe) -> Result<bool> {
        let peer = self
            .shared
            .peers
            .get(&self.id.client_id)
            .map(|peer| peer.value().clone())
            .ok_or_else(|| anyhow!("session is not exist"))?;

        if let Err(e) =
            self.shared.context().extends.router().await.remove(&unsubscribe.topic_filter, self.id()).await
        {
            log::warn!("{:?} unsubscribe, error:{:?}", self.id, e);
        }
        let remove_ok = peer.s.subscriptions_remove(&unsubscribe.topic_filter).await?.is_some();
        Ok(remove_ok)
    }

    #[inline]
    async fn publish(&self, from: From, p: Publish) -> std::result::Result<(), (From, Publish, Reason)> {
        let tx = if let Some(tx) = self.tx() {
            tx
        } else {
            log::warn!("{:?} forward, from:{:?}, error: Tx is None", self.id, from);
            return Err((from, p, Reason::from_static("Tx is None")));
        };
        if let Err(e) = tx.unbounded_send(Message::Forward(from, p)) {
            log::warn!("{:?} forward, error: {:?}", self.id, e);
            if let Message::Forward(from, p) = e.into_inner() {
                return Err((from, p, Reason::from_static("Tx is closed")));
            }
        }
        Ok(())
    }

    #[inline]
    async fn subscriptions(&self) -> Option<Vec<SubsSearchResult>> {
        if let Some(s) = self.session() {
            let subs = s
                .subscriptions()
                .await
                .ok()?
                .read()
                .await
                .iter()
                .map(|(topic_filter, opts)| SubsSearchResult {
                    node_id: s.id.node_id,
                    clientid: s.id.client_id.clone(),
                    client_addr: s.id.remote_addr,
                    topic: TopicFilter::from(topic_filter.as_ref()),
                    opts: opts.clone(),
                })
                .collect::<Vec<_>>();
            Some(subs)
        } else {
            None
        }
    }
}

#[derive(Clone)]
struct EntryItem {
    s: Session,
    tx: Tx,
}

/// Default production implementation of the [`Shared`] trait for cluster-wide session management.
///
/// Uses a `DashMap` for lock-free concurrent client state storage,
/// per-client mutexes for serialized access, and optional gRPC integration
/// for inter-node message forwarding and subscription synchronization.
#[derive(Clone)]
pub struct DefaultShared {
    scx: Option<ServerContext>,
    lockers: Arc<DashMap<ClientId, Arc<Mutex<()>>>>,
    peers: Arc<DashMap<ClientId, EntryItem>>,
}

impl DefaultShared {
    /// Create a new `DefaultShared` instance.
    #[inline]
    pub fn new(scx: Option<ServerContext>) -> DefaultShared {
        Self { scx, lockers: Arc::new(DashMap::default()), peers: Arc::new(DashMap::default()) }
    }

    #[inline]
    pub(crate) fn context(&self) -> &ServerContext {
        if let Some(scx) = &self.scx {
            scx
        } else {
            unreachable!()
        }
    }

    /// Retrieve the sender channel and target [`To`] address for a given `client_id`.
    #[inline]
    pub fn tx(&self, client_id: &str) -> Option<(Tx, To)> {
        self.peers.get(client_id).map(|peer| (peer.tx.clone(), peer.s.id.clone()))
    }

    /// Collect subscription client IDs from a [`SubRelationsMap`], resolving shared groups.
    #[inline]
    pub fn _collect_subscription_client_ids(&self, relations_map: &SubRelationsMap) -> ForwardedRecipients {
        relations_map
            .values()
            .flat_map(|subs| {
                subs.iter().flat_map(|(tf, cid, _, _, group_shared)| {
                    log::debug!(
                        "_collect_subscription_client_ids, tf: {tf:?}, cid {cid:?}, group_shared: {group_shared:?}"
                    );
                    if let Some((group, _, cids)) = group_shared {
                        cids.iter()
                            .map(|g_cid| {
                                if g_cid == cid {
                                    log::debug!(
                                        "_collect_subscription_client_ids is group_shared {g_cid:?}"
                                    );
                                    (g_cid.clone(), Some((tf.clone(), group.clone())))
                                } else {
                                    (g_cid.clone(), None)
                                }
                            })
                            .collect()
                    } else {
                        vec![(cid.clone(), None)]
                    }
                })
            })
            .collect()
    }

    /// Merge two [`ForwardedRecipients`] collections into one.
    #[inline]
    pub fn _merge_subscription_client_ids(
        &self,
        mut forwardeds: ForwardedRecipients,
        o_forwardeds: ForwardedRecipients,
    ) -> ForwardedRecipients {
        if !o_forwardeds.is_empty() {
            forwardeds.extend(o_forwardeds);
        }
        forwardeds
    }
}

#[async_trait]
impl Shared for DefaultShared {
    #[inline]
    fn entry(&self, id: Id) -> Box<dyn Entry> {
        Box::new(LockEntry::new(id, self.clone(), None))
    }

    #[inline]
    fn exist(&self, client_id: &str) -> bool {
        self.peers.contains_key(client_id)
    }

    #[inline]
    #[allow(unused_variables)]
    async fn forwards(
        &self,
        msg_id: Option<MsgID>,
        from: From,
        publish: Publish,
    ) -> std::result::Result<ForwardedCount, (ForwardedCount, Vec<(To, From, Publish, Reason)>)> {
        let scx = self.context();
        let from_node_id = from.node_id;
        let recipients_count = if let Some(target_clientid) = &publish.target_clientid {
            let mut opts = SubscriptionOptions::default();
            opts.set_qos(publish.qos);
            let relations = vec![(publish.topic.clone(), target_clientid.clone(), opts, None, None)];
            let recipients =
                self.forwards_to(from, &publish, relations, None).await.map_err(|(_, errs)| (0, errs))?;

            if from_node_id == scx.node.id() {
                #[cfg(feature = "msgstore")]
                if let Some(msg_id) = msg_id {
                    if !recipients.is_empty() {
                        if let Err(e) =
                            scx.extends.message_mgr().await.mark_forwarded(msg_id, recipients).await
                        {
                            log::warn!("forwards: mark_forwarded error, msg_id: {:?}, {e}", msg_id);
                        }
                    }
                }
                1
            } else {
                log::warn!(
                    "Received message from remote node, msg_id: {:?}, from node: {:?}",
                    msg_id,
                    from_node_id
                );
                0
            }
        } else {
            let mut relations_map =
                match scx.extends.router().await.matches(from.id.clone(), &publish.topic).await {
                    Ok(relations_map) => relations_map,
                    Err(e) => {
                        log::warn!("forwards, from:{:?}, topic:{:?}, error: {:?}", from, publish.topic, e);
                        SubRelationsMap::default()
                    }
                };

            let this_node_id = scx.node.id();
            let recipients_count = if let Some(relations) = relations_map.remove(&this_node_id) {
                let (recipients, errs) = match self.forwards_to(from, &publish, relations, None).await {
                    Ok(recipients) => (recipients, None),
                    Err((recipients, errs)) => (recipients, Some(errs)),
                };

                let recipients_count = recipients.len();

                #[cfg(feature = "msgstore")]
                if let Some(msg_id) = msg_id {
                    if !recipients.is_empty() {
                        if let Err(e) =
                            scx.extends.message_mgr().await.mark_forwarded(msg_id, recipients).await
                        {
                            log::warn!("forwards: mark_forwarded error, msg_id: {:?}, {e}", msg_id);
                        }
                    }
                }

                if let Some(errs) = errs {
                    return Err((recipients_count, errs));
                }

                recipients_count
            } else {
                0
            };
            if !relations_map.is_empty() {
                log::warn!(
                    "Received message from remote node, msg_id: {:?}, from node: {:?}, relations_map:{relations_map:?}",
                    msg_id,
                    from_node_id
                );
            }

            recipients_count
        };

        Ok(recipients_count)
    }

    #[inline]
    async fn forwards_and_get_shareds(
        &self,
        from: From,
        publish: Publish,
    ) -> std::result::Result<
        (SubRelationsMap, ForwardedRecipients),
        (ForwardedRecipients, Vec<(To, From, Publish, Reason)>),
    > {
        if let Some(target_clientid) = &publish.target_clientid {
            let mut opts = SubscriptionOptions::default();
            opts.set_qos(publish.qos);
            let relations = vec![(publish.topic.clone(), target_clientid.clone(), opts, None, None)];
            let recipients = self.forwards_to(from, &publish, relations, None).await?;
            Ok((SubRelationsMap::default(), recipients))
        } else {
            let relations_map =
                match self.context().extends.router().await.matches(from.id.clone(), &publish.topic).await {
                    Ok(relations_map) => relations_map,
                    Err(e) => {
                        log::warn!("forwards, from:{:?}, topic:{:?}, error: {:?}", from, publish.topic, e);
                        SubRelationsMap::default()
                    }
                };

            let mut relations = SubRelations::new();
            let mut sub_relations_map = SubRelationsMap::default();
            for (node_id, rels) in relations_map {
                for (topic_filter, client_id, opts, sub_ids, group) in rels {
                    if let Some(group) = group {
                        sub_relations_map.entry(node_id).or_default().push((
                            topic_filter,
                            client_id,
                            opts,
                            sub_ids,
                            Some(group),
                        ));
                    } else {
                        relations.push((topic_filter, client_id, opts, sub_ids, None));
                    }
                }
            }

            let recipients = if !relations.is_empty() {
                self.forwards_to(from, &publish, relations, None).await?
            } else {
                ForwardedRecipients::default()
            };

            Ok((sub_relations_map, recipients))
        }
    }

    #[inline]
    async fn forwards_to(
        &self,
        from: From,
        publish: &Publish,
        mut relations: SubRelations,
        _msg_id: Option<MsgID>,
    ) -> std::result::Result<ForwardedRecipients, (ForwardedRecipients, Vec<(To, From, Publish, Reason)>)>
    {
        let mut errs = Vec::new();
        let mut ok_recipients: ForwardedRecipients = Vec::new();

        for (topic_filter, client_id, opts, sub_ids, group_shared) in relations.drain(..) {
            let retain = if let Some(retain_as_published) = opts.retain_as_published() {
                //MQTT V5: Retain As Publish
                if retain_as_published {
                    publish.retain
                } else {
                    false
                }
            } else {
                false
            };

            let mut p = publish.clone();
            p.dup = false;
            p.retain = retain;
            p.qos = p.qos.less_value(opts.qos());
            p.packet_id = None;
            if let Some(sub_ids) = sub_ids {
                if let Some(p) = &mut p.properties {
                    p.subscription_ids = sub_ids;
                }
            }
            // p.properties.subscription_ids = sub_ids;

            let (tx, to) = if let Some((tx, to)) = self.tx(&client_id) {
                (tx, to)
            } else {
                log::debug!(
                    "forwards_to failed, from:{:?}, to:{:?}, topic_filter:{:?}, topic:{:?}, reason: the client has disconnected.",
                    from,
                    client_id,
                    topic_filter,
                    publish.topic
                );
                errs.push((
                    To::from(0, client_id),
                    from.clone(),
                    p,
                    Reason::from_static("the client has disconnected"),
                ));
                continue;
            };

            if let Err(e) = tx.unbounded_send(Message::Forward(from.clone(), p)) {
                log::warn!(
                    "forwards_to failed, from:{:?}, to:{:?}, topic_filter:{:?}, topic:{:?}, reason:{:?}",
                    from,
                    client_id,
                    topic_filter,
                    publish.topic,
                    e
                );
                if let Message::Forward(from, p) = e.into_inner() {
                    errs.push((to, from, p, Reason::from_static("Connection Tx is closed")));
                }
            } else {
                // Collect successful session info with shared group expansion
                if let Some((group, _, cids)) = &group_shared {
                    for g_cid in cids {
                        if g_cid == &client_id {
                            ok_recipients.push((g_cid.clone(), Some((topic_filter.clone(), group.clone()))));
                        } else {
                            ok_recipients.push((g_cid.clone(), None));
                        }
                    }
                } else {
                    ok_recipients.push((client_id, None));
                }
            }
        }

        if errs.is_empty() {
            Ok(ok_recipients)
        } else {
            Err((ok_recipients, errs))
        }
    }

    #[inline]
    fn iter(&self) -> Box<dyn Iterator<Item = Box<dyn Entry>> + Sync + Send + '_> {
        Box::new(DefaultIter { shared: self.clone(), ptr: self.peers.iter() })
    }

    #[inline]
    fn random_session(&self) -> Option<Session> {
        let mut sessions = self.peers.iter().map(|p| p.s.clone()).collect::<Vec<Session>>();
        let len = self.peers.len();
        if len > 0 {
            let idx = rand::random::<u64>() as usize % len;
            Some(sessions.remove(idx))
        } else {
            None
        }
    }

    #[inline]
    async fn session_status(&self, client_id: &str) -> Option<SessionStatus> {
        if let Some(entry) = self.peers.get(client_id) {
            Some(SessionStatus {
                id: entry.s.id.clone(),
                online: entry.s.connected().await.unwrap_or_default(),
                handshaking: false,
            })
        } else {
            None
        }
    }

    #[inline]
    async fn client_states_count(&self) -> usize {
        self.peers.len()
    }

    #[inline]
    fn sessions_count(&self) -> usize {
        self.peers.len()
    }

    #[inline]
    async fn query_subscriptions(&self, q: &SubsSearchParams) -> Vec<SubsSearchResult> {
        self.context().extends.router().await.query_subscriptions(q).await
    }

    #[inline]
    async fn subscriptions_count(&self) -> usize {
        futures::future::join_all(self.peers.iter().map(|entry| async move {
            if let Ok(subs) = entry.s.subscriptions().await {
                subs.len().await
            } else {
                0
            }
        }))
        .await
        .iter()
        .sum()
    }

    #[inline]
    #[cfg(feature = "msgstore")]
    async fn message_load(
        &self,
        client_id: &str,
        topic_filter: &str,
        group: Option<&SharedGroup>,
    ) -> Result<Vec<(MsgID, From, Publish)>> {
        let scx = self.context();
        let message_mgr = scx.extends.message_mgr().await;
        message_mgr.get(client_id, topic_filter, group).await
    }

    #[cfg(feature = "msgstore")]
    async fn message_load_with(
        &self,
        client_id: &str,
        topic_filter: &str,
        group: Option<&SharedGroup>,
        cb: Arc<dyn MessageLoadCallback>,
    ) -> Result<()> {
        let scx = self.context();
        let message_mgr = scx.extends.message_mgr().await;
        let msgs = message_mgr.get(client_id, topic_filter, group).await?;
        cb.on_messages(msgs).await
    }

    #[cfg(feature = "retain")]
    async fn retain_load_with(
        &self,
        topic_filter: &TopicFilter,
        cb: Arc<dyn RetainLoadCallback>,
    ) -> Result<Vec<(NodeId, MsgID)>> {
        let scx = self.context();
        let retain_mgr = scx.extends.retain().await;
        let retains = retain_mgr.get(topic_filter).await?;
        cb.on_retains(retains).await
    }

    #[inline]
    #[cfg(feature = "msgstore")]
    async fn message_mark_forwarded(
        &self,
        from_node_id: NodeId,
        msg_id: MsgID,
        recipients: ForwardedRecipients,
    ) -> Result<()> {
        let scx = self.context();
        if from_node_id == scx.node.id() {
            scx.extends.message_mgr().await.mark_forwarded(msg_id, recipients).await
        } else {
            log::warn!("forward mark flag to node {}", from_node_id);
            Ok(())
        }
    }
}

/// Iterator over all client session entries managed by [`DefaultShared`].
///
/// Wraps a `DashMap` iterator, yielding each entry as a `Box<dyn Entry>`.
pub struct DefaultIter<'a> {
    shared: DefaultShared,
    ptr: dashmap::iter::Iter<'a, ClientId, EntryItem, ahash::RandomState>,
}

impl Iterator for DefaultIter<'_> {
    type Item = Box<dyn Entry>;

    /// Advance the iterator and return the next session entry.
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(item) = self.ptr.next() {
            Some(Box::new(LockEntry::new(item.s.id.clone(), self.shared.clone(), None)))
        } else {
            None
        }
    }
}

/// Deduplicate retained messages by topic, keeping the one with the latest `create_time`.
///
/// When clustering with node-local retain storage (ram/sled), the same topic may be
/// present on multiple nodes. This helper collapses those duplicates so a subscriber
/// receives only the newest retained message per topic.
#[cfg(feature = "retain")]
pub fn dedup_retains_by_topic(retains: Vec<(TopicName, Retain)>) -> Vec<(TopicName, Retain)> {
    let mut latest_idx: HashMap<TopicName, usize> = HashMap::default();
    for (idx, (topic, retain)) in retains.iter().enumerate() {
        let incoming_ts = retain.publish.create_time.unwrap_or(0);
        let keep = match latest_idx.get(topic) {
            Some(&existing_idx) => {
                let existing_ts = retains[existing_idx].1.publish.create_time.unwrap_or(0);
                incoming_ts > existing_ts
            }
            None => true,
        };
        if keep {
            latest_idx.insert(topic.clone(), idx);
        }
    }
    let mut indices: Vec<usize> = latest_idx.into_values().collect();
    indices.sort_unstable();
    indices.into_iter().map(|idx| retains[idx].clone()).collect()
}

#[cfg(all(test, feature = "retain"))]
mod tests {
    use super::*;
    use crate::codec::types::{Publish as CodecPublish, QoS};
    use crate::types::{ClientId, From, Id, Publish, Retain};
    use bytes::Bytes;
    use std::collections::HashMap as StdHashMap;

    fn make_retain(topic: &str, payload: &str, create_time: i64) -> (TopicName, Retain) {
        let topic = TopicName::from(topic);
        let codec_publish = CodecPublish {
            dup: false,
            retain: true,
            qos: QoS::AtMostOnce,
            topic: topic.clone(),
            packet_id: None,
            payload: Bytes::from(payload.to_string()),
            properties: None,
        };
        let publish = Publish::from(codec_publish).create_time(create_time);
        let retain =
            Retain { msg_id: None, from: From::from_custom(Id::from(0, ClientId::from_static(""))), publish };
        (topic, retain)
    }

    #[test]
    fn test_dedup_retains_by_topic_keeps_latest() {
        let retains = vec![
            make_retain("a/b", "old", 100),
            make_retain("a/b", "new", 200),
            make_retain("a/c", "other", 150),
        ];
        let deduped = dedup_retains_by_topic(retains);
        assert_eq!(deduped.len(), 2);
        let map: StdHashMap<String, String> = deduped
            .into_iter()
            .map(|(t, r)| (t.to_string(), String::from_utf8(r.publish.payload.to_vec()).unwrap()))
            .collect();
        assert_eq!(map.get("a/b").unwrap(), "new");
        assert_eq!(map.get("a/c").unwrap(), "other");
    }

    #[test]
    fn test_dedup_retains_by_topic_first_wins_on_equal_time() {
        let retains = vec![make_retain("a/b", "first", 100), make_retain("a/b", "second", 100)];
        let deduped = dedup_retains_by_topic(retains);
        assert_eq!(deduped.len(), 1);
        assert_eq!(deduped[0].1.publish.payload.as_ref(), b"first");
    }
}