shardmap 0.3.0

Sharded embedded in-memory map with optional cache, protocol, and server internals
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
use super::connection::{ConnectionRejector, EngineConnection, HandoffConfig, SnapshotTask};
use super::direct::{DirectConnection, DirectServer, ShardArcConnection};
#[cfg(feature = "embedded")]
use super::transactions::TransactionCoordinator;
#[cfg(all(target_os = "linux", feature = "embedded", feature = "monoio"))]
use super::transport::{MonoioMultiDirectWorker, MonoioWorkerConfig};
#[cfg(feature = "embedded")]
use super::transport::{
    MultiDirectAddress, MultiDirectConnection, MultiDirectWorker, MultiDirectWorkerMessage,
    TokioHybridWorkerConfig, TokioWorkerConfig,
};
use super::*;

impl ShardCacheServer {
    pub fn new(config: ShardCacheConfig, engine: EngineHandle) -> Self {
        Self {
            config,
            engine: Some(engine),
            mode: ServerMode::Auto,
            unix_socket_path: None,
            embedded_store: None,
            shard_arc_store: None,
            thread_local_embedded_store: false,
        }
    }

    pub fn with_mode(config: ShardCacheConfig, engine: EngineHandle, mode: ServerMode) -> Self {
        Self {
            config,
            engine: Some(engine),
            mode,
            unix_socket_path: None,
            embedded_store: None,
            shard_arc_store: None,
            thread_local_embedded_store: false,
        }
    }

    pub fn direct(config: ShardCacheConfig) -> Self {
        Self {
            config,
            engine: None,
            mode: ServerMode::Direct,
            unix_socket_path: None,
            embedded_store: None,
            shard_arc_store: None,
            thread_local_embedded_store: false,
        }
    }

    /// Exposes an existing embedded store over the shardcache RESP/SCNP server.
    ///
    /// The supplied store remains owned by the embedding process. Local
    /// in-process reads and writes and remote protocol requests all observe the
    /// same data. Server configuration controls the listener and connection
    /// behavior; configure memory limits directly on the store before serving
    /// it if the embedding process owns that policy.
    pub fn from_embedded_store(config: ShardCacheConfig, store: Arc<EmbeddedStore>) -> Self {
        Self {
            config,
            engine: None,
            mode: ServerMode::Direct,
            unix_socket_path: None,
            embedded_store: Some(store),
            shard_arc_store: None,
            thread_local_embedded_store: false,
        }
    }

    /// Exposes a shard-shared embedded store over the benchmark GET/SET server.
    ///
    /// This mode is intentionally narrower than `from_embedded_store`: it is a
    /// measurement surface for architectures where the sharing boundary is an
    /// `Arc` per storage shard rather than one `Arc` around the full store.
    #[doc(hidden)]
    pub fn from_benchmark_shard_arc_embedded_store(
        config: ShardCacheConfig,
        store: Arc<ShardArcEmbeddedStore>,
    ) -> Self {
        Self {
            config,
            engine: None,
            mode: ServerMode::Direct,
            unix_socket_path: None,
            embedded_store: None,
            shard_arc_store: Some(store),
            thread_local_embedded_store: false,
        }
    }

    #[doc(hidden)]
    #[deprecated(
        note = "benchmark topology probe; use from_embedded_store for public fanout or server_endpoint_mode=direct_shard for direct shard ports"
    )]
    pub fn from_shard_arc_embedded_store(
        config: ShardCacheConfig,
        store: Arc<ShardArcEmbeddedStore>,
    ) -> Self {
        Self::from_benchmark_shard_arc_embedded_store(config, store)
    }

    /// Serves the [`LocalEmbeddedStore`]
    /// already installed on this thread.
    ///
    /// This mode is for owner-local embedded deployments where the embedding
    /// process and the TCP server must use the exact same shard-owned memory
    /// without falling back to shared `EmbeddedStore` locks. Install a local
    /// store with `LocalEmbeddedStore::install_local()` before calling `run` or
    /// `run_with_shutdown`; the server leaves that store installed when it
    /// stops.
    pub fn from_thread_local_embedded_store(config: ShardCacheConfig) -> Self {
        Self {
            config,
            engine: None,
            mode: ServerMode::Direct,
            unix_socket_path: None,
            embedded_store: None,
            shard_arc_store: None,
            thread_local_embedded_store: true,
        }
    }

    pub fn with_unix_socket(mut self, path: PathBuf) -> Self {
        self.unix_socket_path = Some(path);
        self
    }

    pub async fn run(self) -> Result<()> {
        if self.shard_arc_store.is_some() {
            return self
                .run_shard_arc_with_shutdown(async {
                    let _ = tokio::signal::ctrl_c().await;
                })
                .await;
        }
        if self.should_run_multi_direct() {
            return self
                .run_multi_direct_with_shutdown(async {
                    let _ = tokio::signal::ctrl_c().await;
                })
                .await;
        }
        if self.should_run_direct() {
            return self
                .run_direct_with_shutdown(async {
                    let _ = tokio::signal::ctrl_c().await;
                })
                .await;
        }

        self.run_engine_with_shutdown(async {
            let _ = tokio::signal::ctrl_c().await;
        })
        .await
    }

    pub async fn run_with_shutdown<F>(self, shutdown: F) -> Result<()>
    where
        F: std::future::Future<Output = ()> + Send,
    {
        if self.shard_arc_store.is_some() {
            return self.run_shard_arc_with_shutdown(shutdown).await;
        }
        if self.should_run_multi_direct() {
            return self.run_multi_direct_with_shutdown(shutdown).await;
        }
        if self.should_run_direct() {
            return Err(crate::ShardCacheError::Config(
                "thread-local embedded servers must use run_thread_local_with_shutdown because the owner-local runtime is !Send"
                    .into(),
            ));
        }

        self.run_engine_with_shutdown(shutdown).await
    }

    pub async fn run_thread_local_with_shutdown<F>(self, shutdown: F) -> Result<()>
    where
        F: std::future::Future<Output = ()>,
    {
        if !self.should_run_direct() {
            return Err(crate::ShardCacheError::Config(
                "run_thread_local_with_shutdown requires from_thread_local_embedded_store".into(),
            ));
        }
        self.run_direct_with_shutdown(shutdown).await
    }

    async fn run_shard_arc_with_shutdown<F>(self, shutdown: F) -> Result<()>
    where
        F: std::future::Future<Output = ()> + Send,
    {
        if self.unix_socket_path.is_some() {
            return Err(crate::ShardCacheError::Config(
                "shard-arc embedded server mode does not support unix sockets".into(),
            ));
        }
        let store = self
            .shard_arc_store
            .as_ref()
            .expect("shard-arc server requires store")
            .clone();
        let listener = TcpListener::bind(&self.config.bind_addr).await?;
        tracing::info!(
            "shardcache listening on {} (shard-arc embedded GET/SET mode, {} shards)",
            self.config.bind_addr,
            store.shard_count()
        );

        let limiter = Arc::new(Semaphore::new(self.config.max_connections));
        tokio::pin!(shutdown);

        loop {
            tokio::select! {
                _ = &mut shutdown => {
                    tracing::info!("shutdown requested");
                    break;
                }
                accept_result = listener.accept() => {
                    let (stream, peer_addr) = accept_result?;
                    stream.set_nodelay(true)?;
                    tracing::debug!("accepted connection from {peer_addr}");
                    let permit = match limiter.clone().try_acquire_owned() {
                        Ok(permit) => permit,
                        Err(_) => {
                            ConnectionRejector::reject(stream).await?;
                            continue;
                        }
                    };
                    let store = store.clone();
                    tokio::spawn(async move {
                        if let Err(error) = ShardArcConnection::handle(stream, store, permit).await {
                            tracing::warn!("shard-arc connection closed with error: {error}");
                        }
                    });
                }
            }
        }

        Ok(())
    }

    async fn run_engine_with_shutdown<F>(self, shutdown: F) -> Result<()>
    where
        F: std::future::Future<Output = ()> + Send,
    {
        if let Some(path) = self.unix_socket_path.clone() {
            UnixSocketPath::prepare(&path)?;
            let listener = UnixListener::bind(&path)?;
            tracing::info!("shardcache listening on unix://{}", path.display());

            let limiter = Arc::new(Semaphore::new(self.config.max_connections));
            let snapshot_task = SnapshotTask::spawn(self.engine().clone());
            tokio::pin!(shutdown);

            loop {
                tokio::select! {
                    _ = &mut shutdown => {
                        tracing::info!("shutdown requested");
                        break;
                    }
                    accept_result = listener.accept() => {
                        let (stream, _addr) = accept_result?;
                        let permit = match limiter.clone().try_acquire_owned() {
                            Ok(permit) => permit,
                            Err(_) => {
                                ConnectionRejector::reject(stream).await?;
                                continue;
                            }
                        };
                        let engine = self.engine().clone();
                        let (read_half, write_half) = stream.into_split();
                        let write_handoff = WriteHandoff::spawn(write_half, HandoffConfig::write());
                        tokio::spawn(async move {
                            if let Err(error) =
                                EngineConnection::handle(read_half, write_handoff, engine, permit).await
                            {
                                tracing::warn!("connection closed with error: {error}");
                            }
                        });
                    }
                }
            }

            snapshot_task.abort();
            let _ = snapshot_task.await;
            UnixSocketPath::cleanup(&path);
            return self.engine().shutdown().await;
        }

        let listener = TcpListener::bind(&self.config.bind_addr).await?;
        tracing::info!("shardcache listening on {}", self.config.bind_addr);

        let limiter = Arc::new(Semaphore::new(self.config.max_connections));
        let snapshot_task = SnapshotTask::spawn(self.engine().clone());
        tokio::pin!(shutdown);

        loop {
            tokio::select! {
                _ = &mut shutdown => {
                    tracing::info!("shutdown requested");
                    break;
                }
                accept_result = listener.accept() => {
                    let (stream, peer_addr) = accept_result?;
                    stream.set_nodelay(true)?;
                    tracing::debug!("accepted connection from {peer_addr}");
                    let permit = match limiter.clone().try_acquire_owned() {
                        Ok(permit) => permit,
                        Err(_) => {
                            ConnectionRejector::reject(stream).await?;
                            continue;
                        }
                    };
                    let engine = self.engine().clone();
                    let (read_half, write_half) = stream.into_split();
                    let write_handoff = WriteHandoff::spawn(write_half, HandoffConfig::write());
                    tokio::spawn(async move {
                        if let Err(error) =
                            EngineConnection::handle(read_half, write_handoff, engine, permit).await
                        {
                            tracing::warn!("connection closed with error: {error}");
                        }
                    });
                }
            }
        }

        snapshot_task.abort();
        let _ = snapshot_task.await;
        self.engine().shutdown().await
    }
}

trait ServerModeRouting {
    fn should_run_direct(&self) -> bool;
    fn should_run_multi_direct(&self) -> bool;
    fn engine(&self) -> &EngineHandle;
}

impl ServerModeRouting for ShardCacheServer {
    fn should_run_direct(&self) -> bool {
        self.thread_local_embedded_store
    }

    fn should_run_multi_direct(&self) -> bool {
        // Direct mode now always uses multi-direct (with at least 1 worker).
        !self.thread_local_embedded_store
            && (matches!(self.mode, ServerMode::Direct)
                || (matches!(self.mode, ServerMode::Auto)
                    && !self.config.persistence.enabled
                    && self.config.shard_count >= 1))
    }

    fn engine(&self) -> &EngineHandle {
        self.engine
            .as_ref()
            .expect("engine-backed server requires an engine handle")
    }
}

impl ShardCacheServer {
    async fn run_direct_with_shutdown<F>(self, shutdown: F) -> Result<()>
    where
        F: std::future::Future<Output = ()>,
    {
        if self.thread_local_embedded_store {
            DirectServer::initialize_thread_local(&self.config)?;
        } else {
            DirectServer::initialize(&self.config)?;
        }

        let result = if let Some(path) = self.unix_socket_path.clone() {
            UnixSocketPath::prepare(&path)?;
            let listener = UnixListener::bind(&path)?;
            tracing::info!(
                "shardcache listening on unix://{} (direct local mode)",
                path.display()
            );

            let limiter = Arc::new(Semaphore::new(self.config.max_connections));
            let local = LocalSet::new();
            let config = self.config.clone();
            let result = local.run_until(async move {
                let mut maintenance = interval(config.ttl_sweep_interval());
                maintenance.set_missed_tick_behavior(MissedTickBehavior::Delay);
                tokio::pin!(shutdown);

                loop {
                    tokio::select! {
                        _ = &mut shutdown => {
                            tracing::info!("shutdown requested");
                            break;
                        }
                        _ = maintenance.tick() => {
                            DirectServer::process_maintenance();
                        }
                        accept_result = listener.accept() => {
                            let (stream, _addr) = accept_result?;
                            let permit = match limiter.clone().try_acquire_owned() {
                                Ok(permit) => permit,
                                Err(_) => {
                                    ConnectionRejector::reject(stream).await?;
                                    continue;
                                }
                            };
                            spawn_local(async move {
                                if let Err(error) = DirectConnection::handle(stream, permit).await {
                                    tracing::warn!("connection closed with error: {error}");
                                }
                            });
                        }
                    }
                }

                Ok(())
            })
            .await;
            UnixSocketPath::cleanup(&path);
            result
        } else {
            let listener = TcpListener::bind(&self.config.bind_addr).await?;
            tracing::info!(
                "shardcache listening on {} (direct local mode)",
                self.config.bind_addr
            );

            let limiter = Arc::new(Semaphore::new(self.config.max_connections));
            let local = LocalSet::new();
            let config = self.config.clone();
            local.run_until(async move {
                let mut maintenance = interval(config.ttl_sweep_interval());
                maintenance.set_missed_tick_behavior(MissedTickBehavior::Delay);
                tokio::pin!(shutdown);

                loop {
                    tokio::select! {
                        _ = &mut shutdown => {
                            tracing::info!("shutdown requested");
                            break;
                        }
                        _ = maintenance.tick() => {
                            DirectServer::process_maintenance();
                        }
                        accept_result = listener.accept() => {
                            let (stream, peer_addr) = accept_result?;
                            stream.set_nodelay(true)?;
                            tracing::debug!("accepted connection from {peer_addr}");
                            let permit = match limiter.clone().try_acquire_owned() {
                                Ok(permit) => permit,
                                Err(_) => {
                                    ConnectionRejector::reject(stream).await?;
                                    continue;
                                }
                            };
                            spawn_local(async move {
                                if let Err(error) = DirectConnection::handle(stream, permit).await {
                                    tracing::warn!("connection closed with error: {error}");
                                }
                            });
                        }
                    }
                }

                Ok(())
            })
            .await
        };

        DirectServer::clear();
        result
    }

    async fn run_multi_direct_with_shutdown<F>(self, shutdown: F) -> Result<()>
    where
        F: std::future::Future<Output = ()>,
    {
        if self.unix_socket_path.is_some() {
            return Err(crate::ShardCacheError::Config(
                "multi-direct mode does not support unix sockets yet; use --shard-count 1".into(),
            ));
        }

        let bind_addr: SocketAddr = self.config.bind_addr.parse().map_err(|error| {
            crate::ShardCacheError::Config(format!(
                "invalid bind addr {}: {error}",
                self.config.bind_addr
            ))
        })?;

        let max_connections = self.config.max_connections;
        let caller_owned_embedded_store = self.embedded_store.is_some();
        let shared_store = self.multi_direct_store()?;
        let shard_count = shared_store.shard_count();
        let fanout_routes_to_owner = caller_owned_embedded_store;
        let limiter = Arc::new(Semaphore::new(max_connections));
        let transaction_coordinator =
            TransactionCoordinator::new(shard_count, self.config.transaction_mode).map(Arc::new);

        #[cfg(all(target_os = "linux", feature = "monoio"))]
        let use_monoio = std::env::var("SHARDCACHE_USE_MONOIO").is_ok_and(|v| v != "0");
        #[cfg(any(not(target_os = "linux"), not(feature = "monoio")))]
        let use_monoio = false;
        let direct_shard_ports = matches!(
            self.config.server_endpoint_mode,
            ServerEndpointMode::DirectShard
        ) || std::env::var("SHARDCACHE_DIRECT_SHARD_PORTS")
            .is_ok_and(|v| v != "0");
        if use_monoio && fanout_routes_to_owner {
            return Err(crate::ShardCacheError::Config(
                "owner-routed fanout is not supported with SHARDCACHE_USE_MONOIO=1 yet".into(),
            ));
        }
        let available_workers = std::thread::available_parallelism()
            .map(|available| available.get())
            .unwrap_or(shard_count)
            .max(1);
        let worker_count = if direct_shard_ports {
            shard_count
        } else {
            std::env::var("SHARDCACHE_WORKER_COUNT")
                .ok()
                .and_then(|value| value.parse::<usize>().ok())
                .filter(|value| *value > 0)
                .unwrap_or(available_workers)
        };
        let direct_base_port = direct_shard_ports
            .then(|| MultiDirectAddress::direct_base_port(bind_addr, shard_count))
            .transpose()?;
        if let Some(direct_base_port) = direct_base_port {
            tracing::info!(
                "multi-direct: exposing shard-owned native ports {}-{}",
                direct_base_port,
                direct_base_port.saturating_add(shard_count.saturating_sub(1) as u16)
            );
        }

        if fanout_routes_to_owner {
            tracing::info!(
                "multi-direct: public fanout routes each request to the owning shard worker"
            );
        } else {
            tracing::info!(
                "multi-direct: public fanout assigns whole connections to worker threads"
            );
        }

        let mut worker_txs: Vec<flume::Sender<MultiDirectWorkerMessage>> =
            Vec::with_capacity(worker_count);
        let mut handles = Vec::with_capacity(worker_count);

        // Resolve available CPU cores for multi-worker pinning. A single worker
        // inherits the process affinity so taskset/cpuset one-vCPU launches stay
        // strict.
        let core_ids: Vec<core_affinity::CoreId> = if worker_count == 1 {
            Vec::new()
        } else {
            core_affinity::get_core_ids().unwrap_or_default()
        };
        match (worker_count, core_ids.is_empty()) {
            (1, _) => tracing::info!("multi-direct: leaving single worker affinity unchanged"),
            (_, true) => {
                tracing::warn!("multi-direct: no core ids available, workers will not be pinned");
            }
            (_, false) => {
                tracing::info!(
                    "multi-direct: pinning {} workers across {} available cores",
                    worker_count,
                    core_ids.len()
                );
            }
        }

        let single_threaded = worker_count == 1 && cfg!(feature = "unsafe");
        let started_at = Instant::now();
        for worker_id in 0..worker_count {
            // Public fanout uses this channel either for legacy whole-socket
            // handoff or for strict per-request shard routing. Shard-port
            // workers bind and accept inside their own pinned worker thread.
            let (tx, rx) = flume::bounded::<MultiDirectWorkerMessage>(256);
            worker_txs.push(tx);
            let store = shared_store.clone();
            let limiter = limiter.clone();
            let transaction_coordinator = transaction_coordinator.clone();
            let core_id = if core_ids.is_empty() {
                None
            } else {
                Some(core_ids[worker_id % core_ids.len()])
            };
            let direct_bind_addr = match direct_base_port {
                Some(port) => Some(MultiDirectAddress::direct_worker_bind_addr(
                    bind_addr, port, worker_id,
                )?),
                None => None,
            };
            let handle = std::thread::Builder::new()
                .name(format!("fc-multi-direct-{worker_id}"))
                .spawn(move || {
                    #[cfg(all(target_os = "linux", feature = "monoio"))]
                    if use_monoio {
                        drop(rx);
                        MonoioMultiDirectWorker::run(
                            MonoioWorkerConfig {
                                worker_id,
                                worker_count,
                                fanout_bind_addr: bind_addr,
                                direct_bind_addr,
                                core_id,
                                single_threaded,
                                started_at,
                                transaction_coordinator,
                            },
                            store,
                            limiter,
                        );
                        return;
                    }
                    if direct_shard_ports {
                        match direct_bind_addr {
                            Some(direct_bind_addr) => MultiDirectWorker::run_hybrid(
                                TokioHybridWorkerConfig {
                                    worker_id,
                                    direct_bind_addr,
                                    core_id,
                                    single_threaded,
                                    owned_shard_id: worker_id,
                                    started_at,
                                    transaction_coordinator,
                                },
                                store,
                                limiter,
                                rx,
                            ),
                            None => tracing::error!(
                                "worker {worker_id} missing direct bind addr despite direct shard ports"
                            ),
                        }
                        return;
                    }
                    MultiDirectWorker::run(
                        TokioWorkerConfig {
                            worker_id,
                            core_id,
                            single_threaded,
                            started_at,
                            transaction_coordinator,
                        },
                        store,
                        limiter,
                        rx,
                    )
                })
                .map_err(|error| {
                    crate::ShardCacheError::Config(format!(
                        "failed to spawn worker thread {worker_id}: {error}"
                    ))
                })?;
            handles.push(handle);
        }

        // Monoio workers accept directly on a shared SO_REUSEPORT listener and,
        // when enabled, shard-owned direct ports. In that case the main runtime
        // only keeps the process alive until ctrl-c. Tokio direct-shard workers
        // still receive fanout connections from the main accept loop below.
        if use_monoio {
            tracing::info!(
                "shardcache main: workers handle accept directly on {}{} ({} workers)",
                bind_addr,
                if direct_shard_ports {
                    " and shard-owned direct ports"
                } else {
                    ""
                },
                worker_count
            );
            shutdown.await;
            tracing::info!("shutdown requested");
            drop(worker_txs);
            for handle in handles {
                let _ = handle.join();
            }
            return Ok(());
        }

        let listener = TcpListener::bind(&bind_addr).await?;
        tracing::info!(
            "shardcache listening on {} (multi-direct, {} workers)",
            bind_addr,
            worker_count
        );

        tokio::pin!(shutdown);

        let worker_txs = Arc::new(worker_txs);
        let mut public_tasks = tokio::task::JoinSet::new();
        let mut next_worker = 0usize;
        loop {
            tokio::select! {
                _ = &mut shutdown => {
                    tracing::info!("shutdown requested");
                    break;
                }
                finished = public_tasks.join_next(), if !public_tasks.is_empty() => {
                    if let Some(Err(error)) = finished {
                        tracing::warn!("public routed connection task failed: {error}");
                    }
                }
                accept = listener.accept() => {
                    let (stream, _addr) = accept?;
                    let _ = stream.set_nodelay(true);
                    if fanout_routes_to_owner {
                        let permit = match limiter.clone().try_acquire_owned() {
                            Ok(permit) => permit,
                            Err(_) => {
                                let _ = ConnectionRejector::reject(stream).await;
                                continue;
                            }
                        };
                        let store = shared_store.clone();
                        let worker_txs = Arc::clone(&worker_txs);
                        let transaction_coordinator = transaction_coordinator.clone();
                        public_tasks.spawn(async move {
                            if let Err(error) = MultiDirectConnection::handle_public_routed(
                                stream,
                                store,
                                permit,
                                worker_txs,
                                transaction_coordinator,
                            )
                            .await
                            {
                                tracing::warn!("public routed connection closed with error: {error}");
                            }
                        });
                        continue;
                    }
                    let std_stream = match stream.into_std() {
                        Ok(s) => s,
                        Err(error) => {
                            tracing::warn!("into_std failed: {error}");
                            continue;
                        }
                    };
                    let target = next_worker % worker_txs.len();
                    next_worker = next_worker.wrapping_add(1);
                    if worker_txs[target]
                        .send_async(MultiDirectWorkerMessage::Stream(std_stream))
                        .await
                        .is_err()
                    {
                        tracing::warn!("worker {target} channel closed");
                        break;
                    }
                }
            }
        }

        public_tasks.abort_all();
        while public_tasks.join_next().await.is_some() {}

        // Drop senders to signal workers to exit, then join.
        drop(worker_txs);
        for handle in handles {
            let _ = handle.join();
        }
        Ok(())
    }

    fn multi_direct_store(&self) -> Result<Arc<EmbeddedStore>> {
        if let Some(store) = &self.embedded_store {
            tracing::info!(
                "multi-direct: serving caller-owned embedded store with {} shards ({:?} routing)",
                store.shard_count(),
                store.route_mode()
            );
            return Ok(Arc::clone(store));
        }

        let route_mode = MultiDirectRouteMode::configured()?;
        let store = EmbeddedStore::with_route_mode(self.config.shard_count, route_mode);
        store.configure_memory_policy(
            self.config.per_shard_memory_limit_bytes(),
            self.config.eviction_policy,
        );
        #[cfg(feature = "redis")]
        store.configure_vector_memory_policy(
            self.config.total_memory_limit_bytes(),
            self.config.eviction_policy,
        );
        Ok(Arc::new(store))
    }
}

struct MultiDirectRouteMode;

impl MultiDirectRouteMode {
    fn configured() -> Result<EmbeddedRouteMode> {
        match std::env::var("SHARDCACHE_ROUTE_MODE") {
            Ok(value)
                if value.eq_ignore_ascii_case("session_prefix")
                    || value.eq_ignore_ascii_case("session-prefix")
                    || value.eq_ignore_ascii_case("session") =>
            {
                Ok(EmbeddedRouteMode::SessionPrefix)
            }
            Ok(value)
                if value.eq_ignore_ascii_case("full_key")
                    || value.eq_ignore_ascii_case("full-key")
                    || value.eq_ignore_ascii_case("point") =>
            {
                Ok(EmbeddedRouteMode::FullKey)
            }
            Ok(value) => Err(crate::ShardCacheError::Config(format!(
                "unknown SHARDCACHE_ROUTE_MODE={value}; use full_key or session_prefix"
            ))),
            Err(_) => Ok(EmbeddedRouteMode::FullKey),
        }
    }
}

struct UnixSocketPath;

impl UnixSocketPath {
    fn prepare(path: &Path) -> Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        if path.exists() {
            std::fs::remove_file(path)?;
        }
        Ok(())
    }

    fn cleanup(path: &Path) {
        let _ = std::fs::remove_file(path);
    }
}

/// Process-level helpers for launching the server runtime.
pub struct ServerRuntime;

impl ServerRuntime {
    pub async fn launch(config: ShardCacheConfig) -> Result<()> {
        let engine = EngineHandle::open(config.clone())?;
        ShardCacheServer::new(config, engine).run().await
    }

    pub fn initialize_tracing() {
        let _ = tracing_subscriber::fmt()
            .with_env_filter(
                tracing_subscriber::EnvFilter::try_from_default_env()
                    .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
            )
            .try_init();
    }
}