| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Itinero.Profiles; |
| | | 8 | | |
| | | 9 | | namespace Itinero.Network.Search.Islands; |
| | | 10 | | |
| | | 11 | | internal class RoutingNetworkIslandManager |
| | | 12 | | { |
| | | 13 | | // Concurrent: every snap enters BuildForTileAsync, and a ReaderWriterLockSlim |
| | | 14 | | // admits only one upgradeable reader at a time, so guarding this dictionary |
| | | 15 | | // with one serialised every snapping thread against every other — including |
| | | 16 | | // threads asking about entirely different tiles. |
| | 620 | 17 | | private readonly ConcurrentDictionary<(string profile, uint tile), Lazy<Task>> _tilesInProgress = new(); |
| | | 18 | | |
| | | 19 | | // Looked up once per edge relaxation (IsMainN); concurrent so that lookup |
| | | 20 | | // takes no lock. |
| | | 21 | | private readonly ConcurrentDictionary<string, Islands> _islands; |
| | | 22 | | |
| | 620 | 23 | | private readonly Dictionary<(string profile, IslandKind kind), IslandDirectedGraph> _directedGraphs = new(); |
| | 620 | 24 | | private readonly ReaderWriterLockSlim _directedGraphsLock = new(); |
| | | 25 | | |
| | | 26 | | // Per-profile semaphore that serialises IslandClassifier.BuildForTileAsync |
| | | 27 | | // calls against each other for the same profile. The shared Full+NonLocal |
| | | 28 | | // dgs are reset to their initial state at the end of each call (per spec); |
| | | 29 | | // running two classifications for the same profile concurrently would let |
| | | 30 | | // one wipe the other's working state mid-flight. Different profiles still |
| | | 31 | | // run in parallel — each has its own dg pair and its own semaphore. |
| | 620 | 32 | | private readonly ConcurrentDictionary<string, SemaphoreSlim> _buildSerialisers = new(); |
| | | 33 | | |
| | | 34 | | internal SemaphoreSlim GetBuildSerialiser(string profileName) => |
| | 0 | 35 | | _buildSerialisers.GetOrAdd(profileName, _ => new SemaphoreSlim(1, 1)); |
| | | 36 | | |
| | 355 | 37 | | internal RoutingNetworkIslandManager(int maxIslandSize) |
| | 355 | 38 | | { |
| | 355 | 39 | | this.MaxIslandSize = maxIslandSize; |
| | 355 | 40 | | _islands = new ConcurrentDictionary<string, Islands>(); |
| | 355 | 41 | | } |
| | | 42 | | |
| | 265 | 43 | | private RoutingNetworkIslandManager(int maxIslandSize, ConcurrentDictionary<string, Islands> islands) |
| | 265 | 44 | | { |
| | 265 | 45 | | this.MaxIslandSize = maxIslandSize; |
| | 265 | 46 | | _islands = islands; |
| | 265 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Checks if an edge is on an island using the directed graph. |
| | | 51 | | /// Returns true if island, false if not island, null if inconclusive. |
| | | 52 | | /// </summary> |
| | | 53 | | internal bool? IsEdgeOnIsland(Profile profile, EdgeId edgeId) => |
| | 0 | 54 | | this.IsEdgeOnIsland(profile.Name, edgeId); |
| | | 55 | | |
| | | 56 | | internal bool? IsEdgeOnIsland(string profileName, EdgeId edgeId) |
| | 0 | 57 | | { |
| | | 58 | | // Snapping is a Full-classification concern, so the existing |
| | | 59 | | // single-DG semantics route through the Full DG. |
| | | 60 | | IslandDirectedGraph? dg; |
| | | 61 | | try |
| | 0 | 62 | | { |
| | 0 | 63 | | _directedGraphsLock.EnterReadLock(); |
| | | 64 | | |
| | 0 | 65 | | if (!_directedGraphs.TryGetValue((profileName, IslandKind.Full), out dg)) |
| | 0 | 66 | | return null; |
| | 0 | 67 | | } |
| | | 68 | | finally |
| | 0 | 69 | | { |
| | 0 | 70 | | _directedGraphsLock.ExitReadLock(); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | // The lock covers the dictionary lookup only; dg's own reads and |
| | | 74 | | // _islands need no lock. |
| | 0 | 75 | | if (!_islands.TryGetValue(profileName, out var profileIslands)) |
| | 0 | 76 | | return null; |
| | 0 | 77 | | if (profileIslands.IsEdgeOnIsland(edgeId)) |
| | 0 | 78 | | return true; |
| | | 79 | | |
| | 0 | 80 | | if (dg.IsNotIsland(edgeId)) |
| | 0 | 81 | | return false; |
| | | 82 | | |
| | 0 | 83 | | return null; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | internal IslandDirectedGraph GetOrCreateDirectedGraph(Profile profile, IslandKind kind = IslandKind.Full) |
| | 5 | 87 | | { |
| | 5 | 88 | | var key = (profile.Name, kind); |
| | | 89 | | try |
| | 5 | 90 | | { |
| | 5 | 91 | | _directedGraphsLock.EnterUpgradeableReadLock(); |
| | | 92 | | |
| | 6 | 93 | | if (_directedGraphs.TryGetValue(key, out var dg)) return dg; |
| | | 94 | | |
| | | 95 | | try |
| | 4 | 96 | | { |
| | 4 | 97 | | _directedGraphsLock.EnterWriteLock(); |
| | | 98 | | |
| | 4 | 99 | | dg = new IslandDirectedGraph(); |
| | 4 | 100 | | _directedGraphs[key] = dg; |
| | 4 | 101 | | return dg; |
| | | 102 | | } |
| | | 103 | | finally |
| | 4 | 104 | | { |
| | 4 | 105 | | _directedGraphsLock.ExitWriteLock(); |
| | 4 | 106 | | } |
| | | 107 | | } |
| | | 108 | | finally |
| | 5 | 109 | | { |
| | 5 | 110 | | _directedGraphsLock.ExitUpgradeableReadLock(); |
| | 5 | 111 | | } |
| | 5 | 112 | | } |
| | | 113 | | |
| | 2782 | 114 | | internal int MaxIslandSize { get; } |
| | | 115 | | |
| | | 116 | | internal bool TryGetIslandsFor(string profileName, out Islands islands) |
| | 0 | 117 | | { |
| | 0 | 118 | | return _islands.TryGetValue(profileName, out islands); |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | internal Islands GetIslandsFor(Profile profile) |
| | 2433 | 122 | | { |
| | | 123 | | // The factory can run more than once under a race, but only one instance |
| | | 124 | | // is published and every caller gets that one. |
| | 2477 | 125 | | return _islands.GetOrAdd(profile.Name, _ => new Islands()); |
| | 2433 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Returns whether the edge is in the profile's main-N component — the |
| | | 130 | | /// dominant SCC of the N-only subgraph, i.e. the "mainland" without |
| | | 131 | | /// L-edges. |
| | | 132 | | /// |
| | | 133 | | /// <list type="bullet"> |
| | | 134 | | /// <item><c>true</c>: edge is in main-N. Default for any edge in a done tile |
| | | 135 | | /// that is neither L-tagged, on an island, nor a non-main-N pocket member.</item> |
| | | 136 | | /// <item><c>false</c>: edge is not in main-N. Either L-tagged (passed in via |
| | | 137 | | /// <paramref name="isLocalAccess"/>), on an island (unreachable in Full), or |
| | | 138 | | /// recorded as a local edge (non-main-N pocket).</item> |
| | | 139 | | /// <item><c>null</c>: classification has not yet produced a verdict for this |
| | | 140 | | /// tile.</item> |
| | | 141 | | /// </list> |
| | | 142 | | /// |
| | | 143 | | /// The L-tag check is tag-driven and resolved by the caller (typically via |
| | | 144 | | /// the cost function's <c>localAccess</c> field on the result of <c>Get</c>), |
| | | 145 | | /// then passed in. The manager itself does not consult any tag storage. |
| | | 146 | | /// </summary> |
| | | 147 | | internal bool? IsMainN(Profile profile, EdgeId edgeId, bool isLocalAccess) |
| | 788377 | 148 | | { |
| | | 149 | | // L-tagged edge — never main-N, no storage lookup needed. |
| | 788379 | 150 | | if (isLocalAccess) return false; |
| | | 151 | | |
| | 1568830 | 152 | | if (!_islands.TryGetValue(profile.Name, out var islands)) return null; |
| | | 153 | | |
| | | 154 | | // Read the tile flag before the edge sets: the classifier marks a tile |
| | | 155 | | // done only after writing its island and local edges, so a tile seen as |
| | | 156 | | // done guarantees the edge reads below see those writes. Sampling the |
| | | 157 | | // edge sets first would let a not-yet-written island edge pair with a |
| | | 158 | | // tile marked done since, reporting main-N for an edge on an island. |
| | 7920 | 159 | | var tileDone = islands.GetTileDone(edgeId.TileId); |
| | | 160 | | |
| | | 161 | | // Unreachable in the Full classification → not in main-N. |
| | 7921 | 162 | | if (islands.IsEdgeOnIsland(edgeId)) return false; |
| | | 163 | | |
| | | 164 | | // Non-main-N pocket → not in main-N. |
| | 7920 | 165 | | if (islands.IsEdgeLocal(edgeId)) return false; |
| | | 166 | | |
| | | 167 | | // Tile finished classifying and the edge is in neither set → main-N. |
| | | 168 | | // Otherwise we don't yet know. |
| | 7918 | 169 | | return tileDone ? true : null; |
| | 788377 | 170 | | } |
| | | 171 | | |
| | | 172 | | internal async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId, |
| | | 173 | | IslandDirectedGraph dgFull, IslandDirectedGraph dgNonLocal, |
| | | 174 | | CancellationToken cancellationToken) |
| | 7 | 175 | | { |
| | | 176 | | // Already classified: nothing to queue and nothing to await. Checked |
| | | 177 | | // before touching the queue because a classified tile is the common case |
| | | 178 | | // once a region is warm, and everything below costs more than this does. |
| | | 179 | | // IslandClassifier.BuildForTileAsync makes the same check first, so this |
| | | 180 | | // only moves it earlier. GetTileDone is a concurrent-set lookup. |
| | 7 | 181 | | if (this.GetIslandsFor(profile).GetTileDone(tileId)) return; |
| | | 182 | | |
| | 7 | 183 | | var key = (profile.Name, tileId); |
| | | 184 | | |
| | 7 | 185 | | if (!_tilesInProgress.TryGetValue(key, out var pending)) |
| | 7 | 186 | | { |
| | | 187 | | // Lazy, not a bare Task: GetOrAdd may invoke a factory more than |
| | | 188 | | // once under a race, and starting a tile's classification twice |
| | | 189 | | // would put a second run behind the per-profile serialiser for work |
| | | 190 | | // already being done. Only the Lazy that wins publication ever has |
| | | 191 | | // Value read, so the classification starts exactly once. |
| | 7 | 192 | | Lazy<Task>? mine = null; |
| | 7 | 193 | | mine = new Lazy<Task>(() => |
| | 7 | 194 | | { |
| | 7 | 195 | | // CancellationToken.None, deliberately: this task is shared with every later |
| | 7 | 196 | | // caller for the same tile, so it must not carry the token of whoever happened |
| | 7 | 197 | | // to ask first. Passing that token let one caller going away cancel the work |
| | 7 | 198 | | // everyone else was waiting on — and, because the entry was only removed |
| | 7 | 199 | | // after a successful await, the cancelled task stayed in the dictionary and was |
| | 7 | 200 | | // handed to every subsequent caller, each of which got an |
| | 7 | 201 | | // OperationCanceledException for a request it never cancelled. That poisoned |
| | 7 | 202 | | // the tile for the lifetime of the network. Callers stay cancellable through |
| | 7 | 203 | | // their own WaitAsync below. |
| | 7 | 204 | | // The graphs belong to whichever request wins publication. A caller that |
| | 7 | 205 | | // merely awaits this task does not get them populated — but the tile is |
| | 7 | 206 | | // done by then, so its durable Islands entry answers instead. |
| | 7 | 207 | | var started = IslandClassifier.BuildForTileAsync(network, profile, tileId, |
| | 7 | 208 | | dgFull, dgNonLocal, CancellationToken.None); |
| | 7 | 209 | | |
| | 7 | 210 | | // Remove on completion whatever the outcome, so a task that failed is retried by |
| | 7 | 211 | | // the next caller rather than replayed at it forever. Removal is matched on this |
| | 7 | 212 | | // exact Lazy: if a later caller has already published a replacement, a stale |
| | 7 | 213 | | // continuation must not evict it and let a third caller start a duplicate run. |
| | 7 | 214 | | _ = started.ContinueWith( |
| | 7 | 215 | | _ => _tilesInProgress.TryRemove( |
| | 7 | 216 | | new KeyValuePair<(string profile, uint tile), Lazy<Task>>(key, mine!)), |
| | 7 | 217 | | TaskContinuationOptions.ExecuteSynchronously); |
| | 7 | 218 | | |
| | 7 | 219 | | return started; |
| | 14 | 220 | | }); |
| | | 221 | | |
| | 7 | 222 | | pending = _tilesInProgress.GetOrAdd(key, mine); |
| | 7 | 223 | | } |
| | | 224 | | |
| | | 225 | | // Await the shared task, but only for as long as this caller is still interested. Giving up |
| | | 226 | | // here does not stop the classification for anyone else. |
| | 7 | 227 | | await pending.Value.WaitAsync(cancellationToken); |
| | 7 | 228 | | } |
| | | 229 | | |
| | | 230 | | internal RoutingNetworkIslandManager Clone() |
| | 265 | 231 | | { |
| | | 232 | | // A profile added while iterating may or may not make it into the clone; |
| | | 233 | | // either is correct, it was not part of the network being cloned. |
| | 265 | 234 | | var islands = new ConcurrentDictionary<string, Islands>(); |
| | 795 | 235 | | foreach (var (profileName, profileIslands) in _islands) |
| | 0 | 236 | | { |
| | 0 | 237 | | islands[profileName] = profileIslands.Clone(); |
| | 0 | 238 | | } |
| | | 239 | | |
| | 265 | 240 | | return new RoutingNetworkIslandManager(this.MaxIslandSize, islands); |
| | 265 | 241 | | } |
| | | 242 | | } |