Repository navigation
Expand file tree
/
Copy pathdata.rs
More file actions
832 lines (784 loc) · 36.8 KB
/
Copy pathdata.rs
File metadata and controls
832 lines (784 loc) · 36.8 KB
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
//! Query access is independent of whether data is one region or a union of published blocks.
use crate::{
base::{Column, Costs, Graph},
blocks::{self, Ids, Union},
closures::Closures,
landmarks::{self, Guide, Junctions},
model::{Point, Profile, Road},
package::{cell_key, digest, Departure, Endpoint, Manifest, Package, Source, MAX_MANIFEST_BYTES, ROADS_PER_PAGE},
snap::{self, Candidates, Policy},
Error, Result,
};
use std::{
collections::{BTreeMap, BTreeSet, VecDeque},
sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError, Weak},
};
/// What the router reads. `Selection` is its one implementation; the trait keeps the router
/// generic over the storage behind it.
pub trait RoutingData {
fn identity(&self) -> &str;
fn region(&self) -> &str;
fn bounds(&self) -> [f64; 4];
fn attribution(&self) -> &str;
fn warnings(&self) -> &[String];
fn profiles(&self) -> Vec<&str>;
fn profile(&self, name: &str) -> Result<&Profile>;
fn snap(&self, point: Point, metric: &str, policy: Policy) -> Result<Candidates>;
/// Applies `f` to a road without a copy of its shape.
fn with_road<T>(&self, road: u32, f: impl FnOnce(&Road) -> T) -> Result<T>;
fn closures(&self) -> Result<Arc<Closures>>;
fn endpoint(&self, metric: &str, road: u32) -> Result<Endpoint>;
/// A metric's graph, costs and landmark columns, loaded and budgeted together.
fn prepared(&self, metric: &str) -> Result<Arc<Prepared>>;
/// This router's estimated working set with `metric` prepared: its own label blocks and the
/// data the forks hold together.
fn routing_bytes(&self, metric: &str) -> Result<usize>;
/// The working set of `routing_bytes` on an idle service: only `metric` prepared.
fn idle_bytes(&self, metric: &str) -> Result<usize>;
/// Drops the cached metrics other than `metric` that no router holds, so that an idle service
/// has the room of `idle_bytes`. Returns whether it dropped one.
fn release(&self, metric: &str) -> bool;
fn memory_budget(&self) -> usize;
fn set_memory_budget(&mut self, bytes: usize);
/// The budget native hosts give a router: the costliest profile's routing bytes plus room
/// for the search queues and the other cached profiles, at least `MINIMUM_BUDGET`.
fn default_budget(&self) -> usize {
let costliest = self.profiles().into_iter().filter_map(|profile| self.routing_bytes(profile).ok()).max();
MINIMUM_BUDGET.max(costliest.unwrap_or(0).saturating_add(SEARCH_HEAP))
}
}
/// Room above the costliest profile for search labels and queues and for the cached costs of
/// other profiles, which a router drops first when it needs the room.
const SEARCH_HEAP: usize = 256 * 1024 * 1024;
const MINIMUM_BUDGET: usize = 768 * 1024 * 1024;
/// One metric, ready to search.
pub struct Prepared {
pub graph: Arc<Graph>,
pub costs: Costs,
pub guide: Option<Guide>,
}
/// Cached prepared metrics per router sharing the cache.
const CACHED_PROFILES: usize = 3;
/// Room each router keeps for its search queues when the cache decides what to retain.
const QUEUE_HEADROOM: usize = 16 * 1024 * 1024;
/// Geometry, index caches, compressed pages and decode scratch.
const MARGIN: usize = 64 * 1024 * 1024;
/// Where the roads of a fine snap cell are listed.
enum Snap {
/// The manifest's one-degree directories of a complete package.
Directory,
/// The flat cell map of a grid selection.
Cells(BTreeMap<String, String>),
}
/// Loaded once per package and shared by every fork. No load runs while `state` is held, so
/// hot reads never wait for a cold load.
#[derive(Default)]
struct Shared {
union: OnceLock<Arc<Union>>,
junctions: OnceLock<Arc<Junctions>>,
closures: OnceLock<Arc<Closures>>,
/// Cold loads take this lock, so forks that need the same data wait for one copy.
loading: Mutex<()>,
state: Mutex<State>,
}
#[derive(Default)]
struct State {
profiles: VecDeque<(String, Arc<Prepared>)>,
/// Evicted metrics. One a router still holds counts, and comes back when asked for.
retired: Vec<(String, Weak<Prepared>)>,
/// The budget of every live handle. The cache is budgeted like one router: the shared data
/// plus one router's own blocks and queue headroom fit the largest budget.
budgets: Vec<usize>,
}
/// Drops the cached metrics other than `metric` that no router holds. Returns whether it dropped one.
fn drop_unheld(state: &mut State, metric: &str) -> bool {
let cached = state.profiles.len();
state.profiles.retain(|(name, prepared)| name == metric || Arc::strong_count(prepared) > 1);
state.profiles.len() < cached
}
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
// The data behind every lock is complete once inserted, so a panic elsewhere leaves it usable.
mutex.lock().unwrap_or_else(PoisonError::into_inner)
}
/// The roads a router sees: all of a package, or the ranges of a grid selection. Road ids are
/// compact and in source order.
pub struct Selection<S> {
package: Package<S>,
identity: String,
ids: Arc<Ids>,
/// Source arcs that leave the selected roads, before those to absent roads are dropped.
arcs: u32,
snap: Arc<Snap>,
/// Metrics with identical turn tables share one decoded turn column.
turn_groups: Arc<BTreeMap<String, usize>>,
shared: Arc<Shared>,
memory_budget: usize,
}
fn turn_groups(manifest: &Manifest) -> BTreeMap<String, usize> {
let mut tables: Vec<&Column> = Vec::new();
manifest
.metrics
.iter()
.map(|(name, metric)| {
let group = tables.iter().position(|table| **table == metric.weights.turns).unwrap_or_else(|| {
tables.push(&metric.weights.turns);
tables.len() - 1
});
(name.clone(), group)
})
.collect()
}
impl<S: Source> Selection<S> {
fn with(package: Package<S>, identity: String, ids: Ids, arcs: u32, snap: Snap) -> Self {
let state = State { budgets: vec![usize::MAX], ..Default::default() };
Self {
turn_groups: Arc::new(turn_groups(package.manifest())),
package,
identity,
ids: Arc::new(ids),
arcs,
snap: Arc::new(snap),
shared: Arc::new(Shared { state: Mutex::new(state), ..Default::default() }),
memory_budget: usize::MAX,
}
}
/// A complete package as a selection of all of its roads.
pub fn whole(package: Package<S>) -> Self {
let manifest = package.manifest();
let (roads, arcs) = (manifest.roads, manifest.graph.head.len);
let identity = package.identity().to_owned();
Self::with(package, identity, Ids::whole(roads), arcs, Snap::Directory)
}
/// A grid selection from its `blocks.json` bytes.
pub fn open(source: S, bytes: &[u8]) -> Result<Self> {
if bytes.len() > MAX_MANIFEST_BYTES {
return Err(Error::Limit);
}
let manifest: blocks::Manifest =
serde_json::from_slice(bytes).map_err(|e| Error::InvalidData(e.to_string()))?;
Self::new(manifest, digest(bytes), |_| Ok(source))
}
/// A grid selection over the source that `source` opens for its validated pack ids.
pub fn new(
manifest: blocks::Manifest,
identity: String,
source: impl FnOnce(&[String]) -> Result<S>,
) -> Result<Self> {
manifest.validate()?;
let source = source(&manifest.archives)?;
let blocks::Manifest { source: source_id, data, roads, arcs, snap, .. } = manifest;
let ids = Ids::new(roads, data.roads)?;
Ok(Self::with(Package::new(source, data, source_id)?, identity, ids, arcs, Snap::Cells(snap)))
}
/// A handle with its own page caches; graph, junctions, closures and prepared metrics are shared.
pub fn fork(&self) -> Self
where
S: Clone,
{
lock(&self.shared.state).budgets.push(self.memory_budget);
Self {
package: self.package.fork(),
identity: self.identity.clone(),
ids: Arc::clone(&self.ids),
arcs: self.arcs,
snap: Arc::clone(&self.snap),
turn_groups: Arc::clone(&self.turn_groups),
shared: Arc::clone(&self.shared),
memory_budget: self.memory_budget,
}
}
/// The package reader, in source road ids.
pub fn package(&self) -> &Package<S> {
&self.package
}
pub fn local_id(&self, source: u32) -> Option<u32> {
self.ids.local(source)
}
pub fn source_id(&self, local: u32) -> Result<u32> {
self.ids.source(local)
}
/// Every object the selection refers to, in the source's verification order. Decodes the
/// cost dictionary, the per-road cost ids, the access words and the closures on the way.
fn objects(&self) -> Result<Vec<String>> {
let package = &self.package;
let manifest = package.manifest();
let mut keys: BTreeSet<String> = manifest.tables().flat_map(|table| table.blocks.iter().cloned()).collect();
for block in manifest.costs.positions() {
if package.bases.borrow_mut().block(package, &manifest.costs, block)?.iter().any(|cost| !cost.valid()) {
return Err(Error::InvalidData("Invalid cost dictionary".into()));
}
}
for metric in manifest.metrics.values() {
for block in metric.costs.positions() {
package.numbers.borrow_mut().block(package, &metric.costs, block)?;
}
for block in metric.allowed.positions() {
package.words.borrow_mut().block(package, &metric.allowed, block)?;
}
}
if let Some(index) = &manifest.closures {
keys.insert(index.sets.clone());
self.closures()?;
}
let mut page = None;
for id in self.ids.iter() {
if page != Some(id / ROADS_PER_PAGE) {
page = Some(id / ROADS_PER_PAGE);
keys.insert(package.key(&manifest.geometry, id / ROADS_PER_PAGE)?);
}
}
match &*self.snap {
Snap::Directory => {
for key in manifest.spatial.values() {
keys.insert(key.clone());
keys.extend(package.spatial_directory(key)?.into_values());
}
}
Snap::Cells(cells) => keys.extend(cells.values().cloned()),
}
let mut keys: Vec<_> = keys.into_iter().collect();
package.source().order_for_verify(&mut keys)?;
Ok(keys)
}
/// Check the complete object closure before publishing or installing.
pub fn verify(&self) -> Result<()> {
for key in self.objects()? {
self.package.bytes(&key)?;
}
Ok(())
}
/// Bytes every router needs for itself: its label blocks and the decode margin.
pub fn router_bytes(&self) -> usize {
crate::search::label_bytes(self.ids.count as usize).saturating_add(MARGIN)
}
/// Bytes the forks hold together with these metrics prepared: the graph, the junction
/// mapping, the closures, and each metric's cost columns (turn columns once per identical
/// table) and landmark columns.
pub fn shared_bytes<'a>(&self, metrics: impl IntoIterator<Item = &'a str>) -> Result<usize> {
let manifest = self.package.manifest();
let roads = self.ids.count as usize;
let arcs = self.arcs as usize;
let mut bytes = roads
.saturating_add(1)
.saturating_mul(8)
.saturating_add(arcs.saturating_mul(8 + manifest.graph.offsets.bytes()))
.saturating_add(arcs.div_ceil(64).saturating_mul(8))
.saturating_add(self.ids.ranges().saturating_mul(16))
.saturating_add(
manifest.closures.as_ref().map_or(0, |index| roads.saturating_mul(index.roads.width.bytes())),
);
if self.shared.union.get().is_none() {
// Linking scratch: the reverse cursor and the fast id lookup.
bytes = bytes.saturating_add(roads.saturating_add(1).saturating_mul(4));
bytes = bytes.saturating_add(self.ids.lookup_bytes());
}
let mut nodes = 0;
if let Some(index) = &manifest.landmarks {
bytes = bytes.saturating_add(roads.saturating_mul(4));
nodes = match self.shared.junctions.get() {
Some(junctions) => junctions.selected.count as usize,
None => roads.min(index.junctions as usize),
};
}
let mut turns: Vec<usize> = Vec::new();
for name in metrics {
let weights = &manifest.metric(name)?.weights;
bytes = bytes.saturating_add(roads.saturating_mul(weights.road_costs.width.bytes()));
if !turns.contains(&self.turn_groups[name]) {
bytes = bytes.saturating_add(arcs.saturating_mul(weights.turns.width.bytes()));
turns.push(self.turn_groups[name]);
}
if let Some(columns) = manifest.landmarks.as_ref().and_then(|index| index.profiles.get(name)) {
bytes = bytes.saturating_add(columns.tables.len().saturating_mul(nodes).saturating_mul(2));
}
}
Ok(bytes)
}
/// The metrics the cache holds, with `metric` beside them. A retired metric counts while a
/// router still holds it.
fn held<'a>(&self, state: &'a State, metric: &'a str) -> Vec<&'a str> {
let mut names: Vec<&str> = state.profiles.iter().map(|(name, _)| name.as_str()).collect();
names.extend(state.retired.iter().filter(|(_, weak)| weak.strong_count() > 0).map(|(name, _)| name.as_str()));
if !names.contains(&metric) {
names.push(metric);
}
names
}
/// One router's view: its own blocks plus everything the forks hold together.
fn share(&self, state: &State, metric: &str) -> Result<usize> {
Ok(self.router_bytes().saturating_add(self.shared_bytes(self.held(state, metric))?))
}
/// Drops the oldest metrics until the cache fits: at most `CACHED_PROFILES` per router, and
/// the shared data plus one router's own blocks and queue headroom within the largest budget,
/// so each further router adds only what it allocates itself.
fn retain(&self, state: &mut State, metric: &str) -> Result<()> {
state.retired.retain(|(_, weak)| weak.strong_count() > 0);
let largest = state.budgets.iter().copied().max().unwrap_or(usize::MAX);
let room = largest.saturating_sub(self.router_bytes().saturating_add(QUEUE_HEADROOM));
loop {
let slots = state.profiles.len() < CACHED_PROFILES.saturating_mul(state.budgets.len().max(1));
if (slots && self.shared_bytes(self.held(state, metric))? <= room) || state.profiles.is_empty() {
return Ok(());
}
let (name, old) = state.profiles.pop_front().unwrap();
if Arc::strong_count(&old) > 1 {
state.retired.push((name, Arc::downgrade(&old)));
}
}
}
/// A cached metric, or a retired one that a router still holds; either moves to the back.
fn cached(&self, metric: &str) -> Option<Arc<Prepared>> {
let mut state = lock(&self.shared.state);
let entry = match state.profiles.iter().position(|(name, _)| name == metric) {
Some(at) => state.profiles.remove(at).unwrap(),
None => {
let at = state.retired.iter().position(|(name, weak)| name == metric && weak.strong_count() > 0)?;
let (name, weak) = state.retired.remove(at);
(name, weak.upgrade()?)
}
};
let prepared = Arc::clone(&entry.1);
state.profiles.push_back(entry);
Some(prepared)
}
/// Callers hold `loading`, so one fork builds the union.
fn union(&self) -> Result<Arc<Union>> {
if let Some(union) = self.shared.union.get() {
return Ok(Arc::clone(union));
}
let union = Arc::new(Union::open(&self.package, &self.ids, self.arcs)?);
Ok(Arc::clone(self.shared.union.get_or_init(|| union)))
}
/// Callers hold `loading`, so one fork remaps the junctions.
fn junctions(&self, index: &landmarks::Index) -> Result<Arc<Junctions>> {
if let Some(junctions) = self.shared.junctions.get() {
return Ok(Arc::clone(junctions));
}
let junctions = Arc::new(Junctions::read(&self.package, index, &self.ids)?);
Ok(Arc::clone(self.shared.junctions.get_or_init(|| junctions)))
}
/// Callers hold `loading`. Reads through this fork's page caches only.
fn load(&self, metric: &str) -> Result<Prepared> {
let package = &self.package;
let union = self.union()?;
let turns = {
let state = lock(&self.shared.state);
let same = |name: &String| self.turn_groups[name] == self.turn_groups[metric];
let cached = state.profiles.iter().filter(|(name, _)| same(name)).map(|(_, p)| Arc::clone(p)).next();
let retired = || state.retired.iter().filter(|(name, _)| same(name)).find_map(|(_, weak)| weak.upgrade());
cached.or_else(retired).map(|prepared| Arc::clone(&prepared.costs.turns))
};
let costs = union.costs(package, &self.ids, metric, turns)?;
let columns =
package.manifest().landmarks.as_ref().and_then(|index| Some((index, index.profiles.get(metric)?)));
let guide = match columns {
None => None,
Some((index, columns)) => {
let junctions = self.junctions(index)?;
let tables = columns
.tables
.iter()
.map(|table| landmarks::read(package, table, junctions.selected.iter(), u16::MAX as u32))
.collect::<Result<_>>()?;
Some(Guide { mapping: Arc::clone(&junctions.mapping), scale: columns.scale, columns: tables })
}
};
Ok(Prepared { graph: Arc::clone(&union.graph), costs, guide })
}
}
impl<S> Drop for Selection<S> {
fn drop(&mut self) {
let mut state = lock(&self.shared.state);
if let Some(at) = state.budgets.iter().position(|&budget| budget == self.memory_budget) {
state.budgets.remove(at);
}
}
}
fn departures(graph: &Graph, costs: &Costs, road: u32) -> Vec<Departure> {
(graph.reverse_first[road as usize]..graph.reverse_first[road as usize + 1])
.filter_map(|reverse| {
let reverse = reverse as usize;
let penalty = costs.turns.get(graph.reverse_arc(reverse));
(penalty != u64::MAX).then_some(Departure { state: graph.reverse_tail[reverse], penalty })
})
.collect()
}
impl<S: Source> RoutingData for Selection<S> {
fn identity(&self) -> &str {
&self.identity
}
fn region(&self) -> &str {
&self.package.manifest().region
}
fn bounds(&self) -> [f64; 4] {
self.package.manifest().bounds
}
fn attribution(&self) -> &str {
&self.package.manifest().attribution
}
fn warnings(&self) -> &[String] {
&self.package.manifest().warnings
}
fn profiles(&self) -> Vec<&str> {
self.package.manifest().metrics.keys().map(String::as_str).collect()
}
fn profile(&self, name: &str) -> Result<&Profile> {
Ok(&self.package.metric(name)?.profile)
}
fn snap(&self, point: Point, metric: &str, policy: Policy) -> Result<Candidates> {
let package = &self.package;
package.metric(metric)?;
let mut roads = BTreeSet::new();
for cell in snap::cells(point, policy.radius_m).map_err(Error::InvalidRequest)? {
let found = match &*self.snap {
Snap::Directory => package.spatial_roads(cell)?,
Snap::Cells(cells) => match cells.get(&cell_key(cell)) {
Some(key) => package.read(key)?,
None => Vec::new(),
},
};
if found.iter().any(|&id| id >= package.manifest().roads) {
return Err(Error::InvalidData("Snap road outside package".into()));
}
roads.extend(found.into_iter().filter_map(|id| self.ids.local(id)));
if roads.len() > 100_000 {
return Err(Error::Limit);
}
}
snap::candidates(roads, policy, |local| {
let source = self.ids.source(local)?;
if package.allowed(metric, source)? {
package.with_road(source, |road| snap::project(local, road, point, policy.radius_m))
} else {
Ok(None)
}
})
}
fn with_road<T>(&self, road: u32, f: impl FnOnce(&Road) -> T) -> Result<T> {
self.package.with_road(self.ids.source(road)?, f)
}
/// The closures of the selected roads, decoded once from their pages and shared by forks.
fn closures(&self) -> Result<Arc<Closures>> {
if let Some(closures) = self.shared.closures.get() {
return Ok(Arc::clone(closures));
}
let package = &self.package;
let closures = match &package.manifest().closures {
None => Closures::default(),
Some(index) => Closures {
sets: package.read(&index.sets)?,
roads: blocks::numbers(package, &index.roads, self.ids.iter(), self.ids.count as usize)?,
},
};
if !closures.valid() {
return Err(Error::InvalidData("Invalid closures".into()));
}
Ok(Arc::clone(self.shared.closures.get_or_init(|| Arc::new(closures))))
}
fn endpoint(&self, metric: &str, road: u32) -> Result<Endpoint> {
let source = self.ids.source(road)?;
let Some(basis) = self.package.prepared_cost(metric, source)? else {
return Ok(Endpoint { cost: None, departures: Vec::new() });
};
let profile = self.profile(metric)?;
let cost = self.package.with_road(source, |road| basis.compile(road, profile))?.map_err(Error::InvalidData)?;
let prepared = self.prepared(metric)?;
Ok(Endpoint { cost: Some(cost), departures: departures(&prepared.graph, &prepared.costs, road) })
}
fn prepared(&self, metric: &str) -> Result<Arc<Prepared>> {
self.package.metric(metric)?;
if let Some(prepared) = self.cached(metric) {
return Ok(prepared);
}
{
// Make room first, so a metric that cannot fit fails before any page is read.
let mut state = lock(&self.shared.state);
self.retain(&mut state, metric)?;
if self.share(&state, metric)? > self.memory_budget
&& (!drop_unheld(&mut state, metric) || self.share(&state, metric)? > self.memory_budget)
{
return Err(if self.idle_bytes(metric)? > self.memory_budget { Error::Limit } else { Error::Memory });
}
}
let _loading = lock(&self.shared.loading);
if let Some(prepared) = self.cached(metric) {
return Ok(prepared);
}
let prepared = Arc::new(self.load(metric)?);
let mut state = lock(&self.shared.state);
self.retain(&mut state, metric)?;
state.profiles.push_back((metric.into(), Arc::clone(&prepared)));
Ok(prepared)
}
fn routing_bytes(&self, metric: &str) -> Result<usize> {
self.package.metric(metric)?;
let state = lock(&self.shared.state);
self.share(&state, metric)
}
fn idle_bytes(&self, metric: &str) -> Result<usize> {
self.package.metric(metric)?;
Ok(self.router_bytes().saturating_add(self.shared_bytes([metric])?))
}
fn release(&self, metric: &str) -> bool {
drop_unheld(&mut lock(&self.shared.state), metric)
}
fn memory_budget(&self) -> usize {
self.memory_budget
}
fn set_memory_budget(&mut self, bytes: usize) {
let mut state = lock(&self.shared.state);
if let Some(budget) = state.budgets.iter_mut().find(|budget| **budget == self.memory_budget) {
*budget = bytes;
}
self.memory_budget = bytes;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
base::{write_topology, write_weights, Topology, Weights},
model::Profile,
package::{Manifest, Metric, FORMAT},
table::{self, Table},
};
use std::sync::{
atomic::{AtomicUsize, Ordering},
mpsc, Barrier,
};
/// A read of this key waits at the barrier twice: once to report, once to be released.
type Gate = Option<(String, Arc<Barrier>)>;
#[derive(Clone, Default)]
struct Memory {
objects: Arc<Mutex<BTreeMap<String, Vec<u8>>>>,
reads: Arc<AtomicUsize>,
gate: Arc<Mutex<Gate>>,
}
impl Source for Memory {
fn read(&self, key: &str) -> Result<Vec<u8>> {
let gate = lock(&self.gate).clone();
if let Some((gated, barrier)) = gate {
if gated == key {
barrier.wait();
barrier.wait();
}
}
self.reads.fetch_add(1, Ordering::Relaxed);
lock(&self.objects).get(key).cloned().ok_or_else(|| Error::MissingRegion(key.into()))
}
}
impl Memory {
fn write(&self, bytes: &[u8]) -> std::result::Result<String, String> {
let key = digest(bytes);
lock(&self.objects).insert(key.clone(), bytes.to_vec());
Ok(key)
}
fn reads(&self) -> usize {
self.reads.load(Ordering::Relaxed)
}
fn manifest(&self, roads: u32, graph: Topology, weights: Weights) -> Manifest {
let placeholder = |len: u32| Table {
len,
blocks: vec!["0".repeat(64); (len as usize).div_ceil(table::ENTRIES)],
pages: None,
};
let profile = Profile::presets().remove(0);
let metric = Metric {
profile: profile.clone(),
weights,
costs: placeholder(roads),
allowed: placeholder(roads.div_ceil(64)),
};
Manifest {
format: FORMAT,
region: "test".into(),
bounds: [0., 0., 1., 1.],
source_sha256: vec![],
attribution: String::new(),
warnings: vec![],
roads,
graph,
geometry: placeholder(roads.div_ceil(ROADS_PER_PAGE)),
spatial: BTreeMap::new(),
metrics: BTreeMap::from([(profile.name, metric)]),
costs: Table::default(),
landmarks: None,
closures: None,
}
}
/// Adds metrics with the given road costs and turn penalties to a one-metric manifest.
fn metrics(&self, manifest: &mut Manifest, extra: &[(&str, &[u64], &[u64])]) {
for &(name, roads, turns) in extra {
let mut metric = manifest.metrics["touring"].clone();
metric.profile = Profile::presets().into_iter().find(|p| p.name == name).unwrap();
metric.weights =
write_weights(roads.iter().copied(), turns.iter().copied(), |b| self.write(b)).unwrap();
manifest.metrics.insert(name.into(), metric);
}
}
fn selection(&self, manifest: &Manifest) -> Selection<Self> {
Selection::whole(Package::open(self.clone(), &serde_json::to_vec(manifest).unwrap()).unwrap())
}
}
#[test]
fn forks_share_one_graph_one_prepared_metric_and_one_closures_table() {
let memory = Memory::default();
let mut edges: Vec<_> = (0..5000).map(|to| (0, to)).collect();
edges.extend([(4095, 0), (4096, 4999), (4999, 1)]);
let mut first = vec![0u32; 5001];
for &(from, _) in &edges {
first[from as usize + 1] += 1;
}
for road in 0..5000 {
first[road + 1] += first[road];
}
let head: Vec<_> = edges.iter().map(|&(_, to)| to).collect();
let topology = write_topology(&first, &head, |b| memory.write(b)).unwrap();
let weights = write_weights(vec![1; 5000], vec![0; edges.len()], |b| memory.write(b)).unwrap();
let selection = memory.selection(&memory.manifest(5000, topology, weights.clone()));
let fork = selection.fork();
let prepared = selection.prepared("touring").unwrap();
let graph = &prepared.graph;
for to in 0..graph.nodes() {
for index in graph.reverse_first[to]..graph.reverse_first[to + 1] {
let arc = graph.reverse_arc(index as usize);
assert_eq!(edges[arc], (graph.reverse_tail[index as usize], to as u32));
}
}
assert_eq!(prepared.costs.arc(graph, 0), 1);
for maximum in [254, 255, 65534, 65535, u32::MAX as u64 - 1, u32::MAX as u64, u64::MAX - 1] {
let original = [0, 1, maximum, u64::MAX];
let column = write_weights(original, [], |b| memory.write(b)).unwrap().road_costs;
let decoded = blocks::numbers(selection.package(), &column, 0..4, 4).unwrap();
assert_eq!((0..original.len()).map(|i| decoded.get(i)).collect::<Vec<_>>(), original);
}
memory.reads.store(0, Ordering::Relaxed);
let again = fork.prepared("touring").unwrap();
assert!(Arc::ptr_eq(&prepared, &again));
assert!(Arc::ptr_eq(&selection.closures().unwrap(), &fork.closures().unwrap()));
assert_eq!(memory.reads(), 0);
assert_eq!(selection.identity(), fork.identity());
assert!(write_topology(&[0, 1, 1], &[1, 0], |b| memory.write(b)).is_err());
assert!(write_topology(&[0, 2, 2], &[1, 1], |b| memory.write(b)).is_err());
}
#[test]
fn metrics_share_only_identical_turn_columns_and_the_oldest_leaves_first() {
let memory = Memory::default();
let topology = write_topology(&[0, 1, 1], &[1], |b| memory.write(b)).unwrap();
let weights = write_weights([1, 2], [0], |b| memory.write(b)).unwrap();
let mut manifest = memory.manifest(2, topology, weights);
memory.metrics(&mut manifest, &[("gravel", &[4, 5], &[0]), ("hiking", &[6, 7], &[2]), ("mtb", &[8, 9], &[0])]);
let selection = memory.selection(&manifest);
let touring = selection.prepared("touring").unwrap();
memory.reads.store(0, Ordering::Relaxed);
let gravel = selection.prepared("gravel").unwrap();
assert_eq!(memory.reads(), 1);
assert!(Arc::ptr_eq(&touring.costs.turns, &gravel.costs.turns));
let hiking = selection.prepared("hiking").unwrap();
assert!(!Arc::ptr_eq(&touring.costs.turns, &hiking.costs.turns));
let graph = &touring.graph;
assert_eq!([touring.costs.arc(graph, 0), gravel.costs.arc(graph, 0), hiking.costs.arc(graph, 0)], [2, 5, 9]);
// The fourth metric evicts the oldest; held by this test, it comes back without a read.
selection.prepared("mtb").unwrap();
memory.reads.store(0, Ordering::Relaxed);
assert!(Arc::ptr_eq(&selection.prepared("gravel").unwrap(), &gravel));
assert!(Arc::ptr_eq(&selection.prepared("touring").unwrap(), &touring));
assert_eq!(memory.reads(), 0);
}
#[test]
fn the_cache_is_budgeted_like_one_router_and_keeps_evicted_metrics_a_router_still_holds() {
let memory = Memory::default();
let topology = write_topology(&[0, 1, 1], &[1], |b| memory.write(b)).unwrap();
let weights = write_weights([1, 2], [0], |b| memory.write(b)).unwrap();
let mut manifest = memory.manifest(2, topology, weights);
memory.metrics(&mut manifest, &[("gravel", &[4, 5], &[0]), ("hiking", &[6, 7], &[0]), ("mtb", &[8, 9], &[0])]);
let mut selection = memory.selection(&manifest);
// The first load builds the union; the estimate drops its linking scratch after that.
let touring = selection.prepared("touring").unwrap();
let router = selection.router_bytes();
let one = selection.shared_bytes(["touring"]).unwrap();
let two = selection.shared_bytes(["touring", "gravel"]).unwrap();
let three = selection.shared_bytes(["touring", "gravel", "hiking"]).unwrap();
assert!(one < two && two < three);
// Room for one metric beside this router's own blocks and queue headroom.
selection.set_memory_budget(router + QUEUE_HEADROOM + one);
let gravel = selection.prepared("gravel").unwrap();
// The evicted metric counts while a router holds it, and comes back without a read.
assert_eq!(selection.routing_bytes("gravel").unwrap(), router + two);
memory.reads.store(0, Ordering::Relaxed);
assert!(Arc::ptr_eq(&selection.prepared("touring").unwrap(), &touring));
assert_eq!(memory.reads(), 0);
drop(touring);
let hiking = selection.prepared("hiking").unwrap();
let kept = selection.shared_bytes(["gravel", "hiking"]).unwrap();
assert_eq!(selection.routing_bytes("hiking").unwrap(), router + kept);
drop((gravel, hiking));
assert_eq!(selection.routing_bytes("hiking").unwrap(), router + selection.shared_bytes(["hiking"]).unwrap());
// A second router with the same budget adds only its own blocks: the room stays one metric.
let mut fork = selection.fork();
let gravel = fork.prepared("gravel").unwrap();
assert_eq!(fork.routing_bytes("gravel").unwrap(), router + selection.shared_bytes(["gravel"]).unwrap());
drop(gravel);
// The largest budget sets the room, and both routers see one cache.
fork.set_memory_budget(router + QUEUE_HEADROOM + three);
let touring = fork.prepared("touring").unwrap();
let mtb = fork.prepared("mtb").unwrap();
assert!(Arc::ptr_eq(&selection.prepared("touring").unwrap(), &touring));
assert!(Arc::ptr_eq(&selection.prepared("mtb").unwrap(), &mtb));
let all = selection.shared_bytes(["gravel", "touring", "mtb"]).unwrap();
assert_eq!(selection.routing_bytes("mtb").unwrap(), router + all);
drop((fork, touring, mtb));
// Alone again, the next load shrinks the cache back to one metric.
selection.prepared("hiking").unwrap();
assert_eq!(selection.routing_bytes("hiking").unwrap(), router + selection.shared_bytes(["hiking"]).unwrap());
}
#[test]
fn a_cold_load_in_one_fork_does_not_block_the_others() {
let memory = Memory::default();
let topology = write_topology(&[0, 1, 1], &[1], |b| memory.write(b)).unwrap();
let weights = write_weights([1, 2], [0], |b| memory.write(b)).unwrap();
let mut manifest = memory.manifest(2, topology, weights);
memory.metrics(&mut manifest, &[("gravel", &[4, 5], &[0])]);
let selection = memory.selection(&manifest);
let touring = selection.prepared("touring").unwrap();
let barrier = Arc::new(Barrier::new(2));
let gated = manifest.metrics["gravel"].weights.road_costs.values.blocks[0].clone();
*lock(&memory.gate) = Some((gated, Arc::clone(&barrier)));
let loader = selection.fork();
let loader = std::thread::spawn(move || loader.prepared("gravel"));
// The loader now sits in its first page read with the loading lock held.
barrier.wait();
let (done, hot) = mpsc::channel();
let probe = selection.fork();
let probe = std::thread::spawn(move || {
let bytes = probe.routing_bytes("touring").is_ok();
let same = probe.prepared("touring").map(|p| Arc::ptr_eq(&p, &touring)).unwrap_or(false);
let closures = probe.closures().is_ok();
done.send((bytes, same, closures)).unwrap();
});
let hot = hot.recv_timeout(std::time::Duration::from_secs(10)).expect("hot reads waited for a cold load");
assert_eq!(hot, (true, true, true));
barrier.wait();
let gravel = loader.join().unwrap().unwrap();
probe.join().unwrap();
assert!(Arc::ptr_eq(&selection.prepared("gravel").unwrap(), &gravel));
}
#[test]
fn malformed_graph_costs_and_insufficient_memory_fail_before_search() {
let memory = Memory::default();
let topology = write_topology(&[0, 1, 1], &[1], |b| memory.write(b)).unwrap();
let weights = write_weights([1, u64::MAX - 2], [3], |b| memory.write(b)).unwrap();
let manifest = memory.manifest(2, topology.clone(), weights);
let mut selection = memory.selection(&manifest);
selection.set_memory_budget(selection.routing_bytes("touring").unwrap() - 1);
assert!(matches!(selection.prepared("touring"), Err(Error::Limit)));
assert_eq!(memory.reads(), 0);
selection.set_memory_budget(usize::MAX);
assert!(matches!(selection.prepared("touring"), Err(Error::InvalidData(_))));
let mut invalid = topology;
invalid.head.blocks[0] = memory.write(&crate::storage::encode(&Vec::<u32>::new()).unwrap()).unwrap();
let mut broken = manifest;
broken.graph = invalid;
assert!(matches!(memory.selection(&broken).prepared("touring"), Err(Error::InvalidData(_))));
}
}