| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationFlowQueryWork.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Threading; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Reports the complete internal lifecycle of one bounded flow query.</summary> |
| | | 14 | | internal enum NavigationFlowQueryStatus : byte |
| | | 15 | | { |
| | | 16 | | Pending = 0, |
| | | 17 | | Success = 1, |
| | | 18 | | Unsupported = 2, |
| | | 19 | | NoMap = 3, |
| | | 20 | | InvalidProfile = 4, |
| | | 21 | | InvalidStart = 5, |
| | | 22 | | InvalidEnd = 6, |
| | | 23 | | NoPath = 7, |
| | | 24 | | BudgetExceeded = 8, |
| | | 25 | | CostOverflow = 9, |
| | | 26 | | CapacityExceeded = 10, |
| | | 27 | | Stale = 11 |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary>Owns one successful flow payload lease and its exact resolved origin.</summary> |
| | | 31 | | internal readonly struct NavigationFlowQueryResult : IDisposable |
| | | 32 | | { |
| | | 33 | | internal NavigationFlowQueryResult( |
| | | 34 | | NavigationCellAddress resolvedOrigin, |
| | | 35 | | NavigationFlowFieldPayloadLease payloadLease) |
| | | 36 | | { |
| | | 37 | | ResolvedOrigin = resolvedOrigin; |
| | | 38 | | PayloadLease = payloadLease; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | internal NavigationCellAddress ResolvedOrigin { get; } |
| | | 42 | | |
| | | 43 | | internal NavigationFlowFieldPayloadLease PayloadLease { get; } |
| | | 44 | | |
| | | 45 | | public void Dispose() => PayloadLease.Dispose(); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary>Owns flow admission, cache reservation, search, and result publication.</summary> |
| | | 49 | | internal sealed class NavigationFlowQueryWork : IDisposable |
| | | 50 | | { |
| | | 51 | | private readonly NavigationWorldGraphStore _store; |
| | | 52 | | private readonly NavigationFlowFieldPayloadCache _cache; |
| | | 53 | | private readonly NavigationFlowFieldWorkspace _workspace; |
| | | 54 | | private readonly NavigationQueryAdmissionWork _admission; |
| | | 55 | | private NavigationFlowFieldWork? _search; |
| | | 56 | | private NavigationFlowFieldPayloadLease _pendingLease; |
| | | 57 | | private NavigationFlowFieldPayload? _pendingProof; |
| | | 58 | | private NavigationFlowQueryResult _result; |
| | | 59 | | private NavigationFlowFieldReservation _payloadReservation; |
| | | 60 | | private NavigationCellAddress _resolvedOrigin; |
| | | 61 | | private NavigationFlowQueryStatus _readyStatus; |
| | | 62 | | private int _status; |
| | | 63 | | private bool _hasPendingLease; |
| | | 64 | | private bool _hasResult; |
| | | 65 | | private bool _readyToPublish; |
| | | 66 | | private bool _started; |
| | | 67 | | private bool _admissionActive; |
| | | 68 | | |
| | 7805 | 69 | | internal NavigationFlowQueryWork( |
| | 7805 | 70 | | NavigationWorldGraphStore store, |
| | 7805 | 71 | | NavigationFlowFieldWorkspace workspace, |
| | 7805 | 72 | | NavigationFlowFieldPayloadCache cache) |
| | | 73 | | { |
| | 7805 | 74 | | SwiftThrowHelper.ThrowIfNull(store, nameof(store)); |
| | 7805 | 75 | | SwiftThrowHelper.ThrowIfNull(workspace, nameof(workspace)); |
| | 7805 | 76 | | SwiftThrowHelper.ThrowIfNull(cache, nameof(cache)); |
| | 7805 | 77 | | _store = store; |
| | 7805 | 78 | | _cache = cache; |
| | 7805 | 79 | | _workspace = workspace; |
| | 7805 | 80 | | _admission = new NavigationQueryAdmissionWork( |
| | 7805 | 81 | | cache.World, |
| | 7805 | 82 | | store, |
| | 7805 | 83 | | workspace.EndpointWorkspace, |
| | 7805 | 84 | | workspace.RayWorkspace, |
| | 7805 | 85 | | PathAlgorithm.FlowField); |
| | 7805 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal NavigationFlowQueryStatus Status |
| | | 89 | | { |
| | 386 | 90 | | get => (NavigationFlowQueryStatus)Volatile.Read(ref _status); |
| | 134 | 91 | | private set => Volatile.Write(ref _status, (int)value); |
| | | 92 | | } |
| | | 93 | | |
| | 209 | 94 | | internal bool IsPrepared => _search != null || IsReadyToPublish; |
| | | 95 | | |
| | 435 | 96 | | internal bool IsReadyToPublish => Volatile.Read(ref _readyToPublish); |
| | | 97 | | |
| | | 98 | | internal bool ReservationRejected { get; private set; } |
| | | 99 | | |
| | | 100 | | internal void Begin(PathQuery query, NavigationWorldGraphLease lease) |
| | | 101 | | { |
| | 75 | 102 | | SwiftThrowHelper.ThrowIfNull(lease, nameof(lease)); |
| | 75 | 103 | | SwiftThrowHelper.ThrowIfTrue( |
| | 75 | 104 | | _started, |
| | 75 | 105 | | message: "The flow query work is already active."); |
| | 74 | 106 | | _started = true; |
| | 74 | 107 | | ReservationRejected = false; |
| | 74 | 108 | | _resolvedOrigin = default; |
| | 74 | 109 | | _readyStatus = NavigationFlowQueryStatus.Pending; |
| | 74 | 110 | | Volatile.Write(ref _readyToPublish, false); |
| | 74 | 111 | | Status = NavigationFlowQueryStatus.Pending; |
| | 74 | 112 | | _admission.Begin( |
| | 74 | 113 | | lease, |
| | 74 | 114 | | query, |
| | 74 | 115 | | query.Traversal.StartMedium, |
| | 74 | 116 | | query.Traversal.TargetMedia); |
| | 74 | 117 | | _admissionActive = true; |
| | 74 | 118 | | if (_admission.Status != NavigationQueryAdmissionStatus.Pending) |
| | 2 | 119 | | MarkReady(MapAdmissionStatus(_admission.Status)); |
| | 74 | 120 | | } |
| | | 121 | | |
| | | 122 | | internal NavigationFlowQueryStatus PrepareSearchOrCheckout( |
| | | 123 | | int lookupStepLimit, |
| | | 124 | | int endpointCandidateStepLimit) |
| | | 125 | | { |
| | 107 | 126 | | SwiftThrowHelper.ThrowIfNegative(lookupStepLimit, nameof(lookupStepLimit)); |
| | 107 | 127 | | SwiftThrowHelper.ThrowIfNegative( |
| | 107 | 128 | | endpointCandidateStepLimit, |
| | 107 | 129 | | nameof(endpointCandidateStepLimit)); |
| | 107 | 130 | | if (!_started || Status != NavigationFlowQueryStatus.Pending || IsPrepared) |
| | 2 | 131 | | return Status; |
| | | 132 | | |
| | 105 | 133 | | NavigationQueryAdmissionStatus admissionStatus = _admission.Advance( |
| | 105 | 134 | | lookupStepLimit, |
| | 105 | 135 | | endpointCandidateStepLimit); |
| | 105 | 136 | | if (admissionStatus == NavigationQueryAdmissionStatus.Pending) |
| | 41 | 137 | | return Status; |
| | 64 | 138 | | if (admissionStatus != NavigationQueryAdmissionStatus.Success) |
| | | 139 | | { |
| | 6 | 140 | | MarkReady(MapAdmissionStatus(admissionStatus)); |
| | 6 | 141 | | return Status; |
| | | 142 | | } |
| | | 143 | | |
| | 58 | 144 | | NavigationResolvedPathQuery resolved = _admission.Result; |
| | 58 | 145 | | _resolvedOrigin = resolved.Start.Address; |
| | 58 | 146 | | var key = new NavigationFlowFieldPayloadKey( |
| | 58 | 147 | | resolved.Query, |
| | 58 | 148 | | resolved.End.Address, |
| | 58 | 149 | | resolved.StartMedium, |
| | 58 | 150 | | resolved.TargetMedia); |
| | 58 | 151 | | NavigationFlowFieldStatus checkout = _cache.TryCheckout( |
| | 58 | 152 | | _store, |
| | 58 | 153 | | resolved.Graph, |
| | 58 | 154 | | key, |
| | 58 | 155 | | _resolvedOrigin, |
| | 58 | 156 | | out _pendingLease, |
| | 58 | 157 | | out _pendingProof); |
| | 58 | 158 | | if (checkout != NavigationFlowFieldStatus.Pending) |
| | | 159 | | { |
| | 16 | 160 | | _hasPendingLease = checkout == NavigationFlowFieldStatus.Success; |
| | 16 | 161 | | resolved.Dispose(); |
| | 16 | 162 | | DisposeAdmission(); |
| | 16 | 163 | | if (checkout == NavigationFlowFieldStatus.CapacityExceeded) |
| | 1 | 164 | | ReservationRejected = true; |
| | 16 | 165 | | MarkReady(MapFlowStatus(checkout)); |
| | 16 | 166 | | return Status; |
| | | 167 | | } |
| | | 168 | | |
| | 42 | 169 | | int maximumNodes = Math.Min( |
| | 42 | 170 | | _workspace.NodeCapacity, |
| | 42 | 171 | | resolved.Query.Budget.MaxExpandedNodes); |
| | 42 | 172 | | int maximumTransitions = resolved.Query.AllowTransitions |
| | 42 | 173 | | ? Math.Max(0, maximumNodes - 1) |
| | 42 | 174 | | : 0; |
| | 42 | 175 | | long maximumBytes = NavigationFlowFieldPayload.GetMaximumRetainedBytes( |
| | 42 | 176 | | maximumNodes, |
| | 42 | 177 | | maximumTransitions, |
| | 42 | 178 | | _workspace.DependencyComponentCapacity, |
| | 42 | 179 | | _workspace.DependencyPageCapacity); |
| | 42 | 180 | | if (!_cache.TryReservePayload(maximumBytes, out _payloadReservation)) |
| | | 181 | | { |
| | 3 | 182 | | ReservationRejected = true; |
| | 3 | 183 | | resolved.Dispose(); |
| | 3 | 184 | | DisposeAdmission(); |
| | 3 | 185 | | MarkReady(NavigationFlowQueryStatus.CapacityExceeded); |
| | 3 | 186 | | return Status; |
| | | 187 | | } |
| | 39 | 188 | | _search = new NavigationFlowFieldWork( |
| | 39 | 189 | | _cache.World, |
| | 39 | 190 | | resolved, |
| | 39 | 191 | | _workspace, |
| | 39 | 192 | | _cache.MaximumSinglePayloadBytes); |
| | 39 | 193 | | DisposeAdmission(); |
| | 39 | 194 | | if (_search.Status != NavigationFlowFieldStatus.Pending) |
| | 1 | 195 | | MarkReady(MapFlowStatus(_search.Status)); |
| | 39 | 196 | | return Status; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | internal NavigationFlowQueryStatus AdvanceSearch( |
| | | 200 | | int lookupStepLimit, |
| | | 201 | | int nodeStepLimit, |
| | | 202 | | int edgeStepLimit, |
| | | 203 | | int connectionStepLimit) |
| | | 204 | | { |
| | 41 | 205 | | SwiftThrowHelper.ThrowIfNegative(lookupStepLimit, nameof(lookupStepLimit)); |
| | 41 | 206 | | SwiftThrowHelper.ThrowIfNegative(nodeStepLimit, nameof(nodeStepLimit)); |
| | 41 | 207 | | SwiftThrowHelper.ThrowIfNegative(edgeStepLimit, nameof(edgeStepLimit)); |
| | 41 | 208 | | SwiftThrowHelper.ThrowIfNegative(connectionStepLimit, nameof(connectionStepLimit)); |
| | 41 | 209 | | if (Status != NavigationFlowQueryStatus.Pending || IsReadyToPublish) |
| | 1 | 210 | | return Status; |
| | 40 | 211 | | if (_search == null) |
| | 1 | 212 | | return Status; |
| | | 213 | | |
| | 39 | 214 | | NavigationFlowFieldStatus searchStatus = _search.Advance( |
| | 39 | 215 | | lookupStepLimit, |
| | 39 | 216 | | nodeStepLimit, |
| | 39 | 217 | | edgeStepLimit, |
| | 39 | 218 | | connectionStepLimit); |
| | 39 | 219 | | if (searchStatus != NavigationFlowFieldStatus.Pending) |
| | 35 | 220 | | MarkReady(MapFlowStatus(searchStatus)); |
| | 39 | 221 | | return Status; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | internal NavigationFlowQueryStatus Publish() |
| | | 225 | | { |
| | 61 | 226 | | if (!IsReadyToPublish) |
| | 1 | 227 | | return Status; |
| | 60 | 228 | | if (_hasPendingLease) |
| | | 229 | | { |
| | 9 | 230 | | NavigationFlowFieldPayloadLease lease = _pendingLease; |
| | 9 | 231 | | _pendingLease = default; |
| | 9 | 232 | | _hasPendingLease = false; |
| | 9 | 233 | | return FinishCached(lease); |
| | | 234 | | } |
| | 51 | 235 | | if (_pendingProof != null) |
| | | 236 | | { |
| | 4 | 237 | | NavigationFlowFieldStatus expected = |
| | 4 | 238 | | _readyStatus == NavigationFlowQueryStatus.NoPath |
| | 4 | 239 | | ? NavigationFlowFieldStatus.NoPath |
| | 4 | 240 | | : NavigationFlowFieldStatus.CostOverflow; |
| | 4 | 241 | | if (!_cache.IsExactProofCurrent( |
| | 4 | 242 | | _store, |
| | 4 | 243 | | _pendingProof, |
| | 4 | 244 | | _resolvedOrigin, |
| | 4 | 245 | | expected)) |
| | | 246 | | { |
| | 3 | 247 | | return Finish(NavigationFlowQueryStatus.Stale); |
| | | 248 | | } |
| | 1 | 249 | | _pendingProof = null; |
| | | 250 | | } |
| | 48 | 251 | | if (_search?.Result == null) |
| | 14 | 252 | | return Finish(_readyStatus); |
| | | 253 | | |
| | 34 | 254 | | NavigationFlowFieldStatus publishedStatus = _cache.TryPublishOrPromote( |
| | 34 | 255 | | _store, |
| | 34 | 256 | | _search.Result, |
| | 34 | 257 | | _resolvedOrigin, |
| | 34 | 258 | | ref _payloadReservation, |
| | 34 | 259 | | out NavigationFlowFieldPayloadLease published); |
| | 34 | 260 | | if (publishedStatus == NavigationFlowFieldStatus.Success) |
| | 32 | 261 | | return FinishSuccess(published); |
| | 2 | 262 | | published.Dispose(); |
| | 2 | 263 | | return Finish(MapFlowStatus(publishedStatus)); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | internal NavigationFlowQueryResult TakeResult() |
| | | 267 | | { |
| | 39 | 268 | | SwiftThrowHelper.ThrowIfTrue( |
| | 39 | 269 | | Status != NavigationFlowQueryStatus.Success || !_hasResult, |
| | 39 | 270 | | message: "The flow query has no successful payload lease."); |
| | 38 | 271 | | NavigationFlowQueryResult result = _result; |
| | 38 | 272 | | _result = default; |
| | 38 | 273 | | _hasResult = false; |
| | 38 | 274 | | return result; |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | public void Dispose() |
| | | 278 | | { |
| | 76 | 279 | | DisposeAdmission(); |
| | 76 | 280 | | _search?.Dispose(); |
| | 76 | 281 | | _search = null; |
| | 76 | 282 | | if (_hasPendingLease) |
| | 1 | 283 | | _pendingLease.Dispose(); |
| | 76 | 284 | | _pendingLease = default; |
| | 76 | 285 | | _hasPendingLease = false; |
| | 76 | 286 | | _pendingProof = null; |
| | 76 | 287 | | if (_hasResult) |
| | 1 | 288 | | _result.Dispose(); |
| | 76 | 289 | | _result = default; |
| | 76 | 290 | | _hasResult = false; |
| | 76 | 291 | | _cache.ReleasePayloadReservation(ref _payloadReservation); |
| | 76 | 292 | | ReservationRejected = false; |
| | 76 | 293 | | Volatile.Write(ref _readyToPublish, false); |
| | 76 | 294 | | _started = false; |
| | 76 | 295 | | } |
| | | 296 | | |
| | | 297 | | private void MarkReady(NavigationFlowQueryStatus status) |
| | | 298 | | { |
| | 63 | 299 | | _readyStatus = status; |
| | 63 | 300 | | Volatile.Write(ref _readyToPublish, true); |
| | 63 | 301 | | } |
| | | 302 | | |
| | | 303 | | private NavigationFlowQueryStatus FinishCached( |
| | | 304 | | NavigationFlowFieldPayloadLease lease) |
| | | 305 | | { |
| | 9 | 306 | | NavigationFlowFieldStatus leaseStatus = lease.TryGetPayload( |
| | 9 | 307 | | out NavigationFlowFieldPayload payload); |
| | 9 | 308 | | if (leaseStatus != NavigationFlowFieldStatus.Success |
| | 9 | 309 | | || (!_store.Current.IsDependencyCurrent(payload.Dependencies) |
| | 9 | 310 | | || !_cache.IsWorldCurrent(payload))) |
| | | 311 | | { |
| | 2 | 312 | | if (leaseStatus == NavigationFlowFieldStatus.Success) |
| | 1 | 313 | | _cache.RemoveExact(payload); |
| | 2 | 314 | | lease.Dispose(); |
| | 2 | 315 | | return Finish(NavigationFlowQueryStatus.Stale); |
| | | 316 | | } |
| | 7 | 317 | | return FinishSuccess(lease); |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | private NavigationFlowQueryStatus FinishSuccess( |
| | | 321 | | NavigationFlowFieldPayloadLease lease) |
| | | 322 | | { |
| | 39 | 323 | | _result = new NavigationFlowQueryResult(_resolvedOrigin, lease); |
| | 39 | 324 | | _hasResult = true; |
| | 39 | 325 | | return Finish(NavigationFlowQueryStatus.Success); |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | private NavigationFlowQueryStatus Finish(NavigationFlowQueryStatus status) |
| | | 329 | | { |
| | 60 | 330 | | _pendingProof = null; |
| | 60 | 331 | | Status = status; |
| | 60 | 332 | | Volatile.Write(ref _readyToPublish, false); |
| | 60 | 333 | | DisposeAdmission(); |
| | 60 | 334 | | _search?.Dispose(); |
| | 60 | 335 | | _search = null; |
| | 60 | 336 | | _cache.ReleasePayloadReservation(ref _payloadReservation); |
| | 60 | 337 | | return status; |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private void DisposeAdmission() |
| | | 341 | | { |
| | 194 | 342 | | if (!_admissionActive) |
| | 120 | 343 | | return; |
| | 74 | 344 | | _admission.Dispose(); |
| | 74 | 345 | | _admissionActive = false; |
| | 74 | 346 | | } |
| | | 347 | | |
| | | 348 | | private static NavigationFlowQueryStatus MapAdmissionStatus( |
| | 8 | 349 | | NavigationQueryAdmissionStatus status) => (NavigationFlowQueryStatus)status; |
| | | 350 | | |
| | | 351 | | private static NavigationFlowQueryStatus MapFlowStatus( |
| | 54 | 352 | | NavigationFlowFieldStatus status) => status <= NavigationFlowFieldStatus.Success |
| | 54 | 353 | | ? (NavigationFlowQueryStatus)status |
| | 54 | 354 | | : (NavigationFlowQueryStatus)((byte)status + 5); |
| | | 355 | | } |