| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationAStarQueryWork.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.Diagnostics; |
| | | 10 | | using System.Threading; |
| | | 11 | | using GridForge.Grids; |
| | | 12 | | |
| | | 13 | | namespace Trailblazer.Pathing; |
| | | 14 | | |
| | | 15 | | /// <summary>Reports the complete internal lifecycle of one bounded A* query.</summary> |
| | | 16 | | internal enum NavigationAStarQueryStatus : byte |
| | | 17 | | { |
| | | 18 | | Pending = 0, |
| | | 19 | | Success = 1, |
| | | 20 | | Unsupported = 2, |
| | | 21 | | NoMap = 3, |
| | | 22 | | InvalidProfile = 4, |
| | | 23 | | InvalidStart = 5, |
| | | 24 | | InvalidEnd = 6, |
| | | 25 | | NoPath = 7, |
| | | 26 | | BudgetExceeded = 8, |
| | | 27 | | CostOverflow = 9, |
| | | 28 | | CapacityExceeded = 10, |
| | | 29 | | Stale = 11 |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Owns admission and search leases until one immutable payload lease can be returned. |
| | | 34 | | /// </summary> |
| | | 35 | | internal sealed class NavigationAStarQueryWork : IDisposable |
| | | 36 | | { |
| | | 37 | | private readonly NavigationWorldGraphStore _store; |
| | | 38 | | private readonly NavigationAStarPayloadCache _cache; |
| | | 39 | | private readonly NavigationAStarWorkspace _workspace; |
| | | 40 | | private readonly NavigationQueryAdmissionWork _admission; |
| | | 41 | | private NavigationSurfaceAStarWork? _search; |
| | | 42 | | private NavigationAStarPayloadLease? _pendingLease; |
| | | 43 | | private NavigationAStarPayloadLease? _result; |
| | | 44 | | private NavigationAStarPayloadReservation _payloadReservation; |
| | | 45 | | private NavigationAStarQueryStatus _readyStatus; |
| | | 46 | | private int _status; |
| | | 47 | | private bool _readyToPublish; |
| | | 48 | | private bool _started; |
| | | 49 | | private bool _admissionActive; |
| | | 50 | | |
| | 7829 | 51 | | internal NavigationAStarQueryWork( |
| | 7829 | 52 | | GridWorld world, |
| | 7829 | 53 | | NavigationWorldGraphStore store, |
| | 7829 | 54 | | NavigationAStarWorkspace workspace, |
| | 7829 | 55 | | NavigationAStarPayloadCache cache) |
| | | 56 | | { |
| | 7829 | 57 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 7829 | 58 | | SwiftThrowHelper.ThrowIfNull(store, nameof(store)); |
| | 7829 | 59 | | SwiftThrowHelper.ThrowIfNull(workspace, nameof(workspace)); |
| | 7829 | 60 | | SwiftThrowHelper.ThrowIfNull(cache, nameof(cache)); |
| | 7829 | 61 | | _store = store; |
| | 7829 | 62 | | _workspace = workspace; |
| | 7829 | 63 | | _cache = cache; |
| | 7829 | 64 | | _admission = new NavigationQueryAdmissionWork( |
| | 7829 | 65 | | world, |
| | 7829 | 66 | | store, |
| | 7829 | 67 | | workspace.EndpointWorkspace, |
| | 7829 | 68 | | workspace.RayWorkspace, |
| | 7829 | 69 | | PathAlgorithm.AStar); |
| | 7829 | 70 | | } |
| | | 71 | | |
| | | 72 | | internal NavigationAStarQueryStatus Status |
| | | 73 | | { |
| | 2925 | 74 | | get => (NavigationAStarQueryStatus)Volatile.Read(ref _status); |
| | 845 | 75 | | private set => Volatile.Write(ref _status, (int)value); |
| | | 76 | | } |
| | | 77 | | |
| | 1579 | 78 | | internal bool IsPrepared => _search != null || _readyToPublish; |
| | | 79 | | |
| | 2062 | 80 | | internal bool IsReadyToPublish => Volatile.Read(ref _readyToPublish); |
| | | 81 | | |
| | | 82 | | internal void BeginReserved( |
| | | 83 | | PathQuery query, |
| | | 84 | | NavigationWorldGraphLease lease, |
| | | 85 | | ref NavigationAStarPayloadReservation reservation) |
| | | 86 | | { |
| | 434 | 87 | | SwiftThrowHelper.ThrowIfNull(lease, nameof(lease)); |
| | 434 | 88 | | SwiftThrowHelper.ThrowIfArgument( |
| | 434 | 89 | | !reservation.HasLeaseSlot, |
| | 434 | 90 | | paramName: nameof(reservation), |
| | 434 | 91 | | message: "A batch query requires one payload reservation."); |
| | 433 | 92 | | SwiftThrowHelper.ThrowIfTrue( |
| | 433 | 93 | | _started, |
| | 433 | 94 | | message: "The A* query work is already active."); |
| | 432 | 95 | | _started = true; |
| | 432 | 96 | | Volatile.Write(ref _readyToPublish, false); |
| | 432 | 97 | | _readyStatus = NavigationAStarQueryStatus.Pending; |
| | 432 | 98 | | Status = NavigationAStarQueryStatus.Pending; |
| | 432 | 99 | | _payloadReservation = reservation; |
| | 432 | 100 | | reservation = default; |
| | 432 | 101 | | _admission.Begin( |
| | 432 | 102 | | lease, |
| | 432 | 103 | | query, |
| | 432 | 104 | | query.Traversal.StartMedium, |
| | 432 | 105 | | query.Traversal.TargetMedia); |
| | 432 | 106 | | _admissionActive = true; |
| | 432 | 107 | | if (_admission.Status != NavigationQueryAdmissionStatus.Pending) |
| | 4 | 108 | | MarkReady(MapAdmissionStatus(_admission.Status)); |
| | 432 | 109 | | } |
| | | 110 | | |
| | | 111 | | internal NavigationAStarQueryStatus PrepareSearchOrCheckout( |
| | | 112 | | int lookupStepLimit, |
| | | 113 | | int endpointCandidateStepLimit) |
| | | 114 | | { |
| | 653 | 115 | | SwiftThrowHelper.ThrowIfNegative(lookupStepLimit, nameof(lookupStepLimit)); |
| | 653 | 116 | | SwiftThrowHelper.ThrowIfNegative( |
| | 653 | 117 | | endpointCandidateStepLimit, |
| | 653 | 118 | | nameof(endpointCandidateStepLimit)); |
| | 653 | 119 | | if (!_started || Status != NavigationAStarQueryStatus.Pending || IsPrepared) |
| | 4 | 120 | | return Status; |
| | | 121 | | |
| | 649 | 122 | | NavigationQueryAdmissionStatus admissionStatus = _admission.Advance( |
| | 649 | 123 | | lookupStepLimit, |
| | 649 | 124 | | endpointCandidateStepLimit); |
| | 649 | 125 | | if (admissionStatus == NavigationQueryAdmissionStatus.Pending) |
| | 238 | 126 | | return Status; |
| | 411 | 127 | | if (admissionStatus != NavigationQueryAdmissionStatus.Success) |
| | | 128 | | { |
| | 7 | 129 | | MarkReady(MapAdmissionStatus(admissionStatus)); |
| | 7 | 130 | | return Status; |
| | | 131 | | } |
| | | 132 | | |
| | 404 | 133 | | NavigationResolvedPathQuery resolved = _admission.Result; |
| | 404 | 134 | | var key = new NavigationAStarPayloadKey( |
| | 404 | 135 | | resolved.Query, |
| | 404 | 136 | | resolved.Start.Address, |
| | 404 | 137 | | resolved.End.Address, |
| | 404 | 138 | | resolved.StartMedium, |
| | 404 | 139 | | resolved.TargetMedia); |
| | 404 | 140 | | bool cacheHit = _cache.TryCheckoutReserved( |
| | 404 | 141 | | key, |
| | 404 | 142 | | resolved.Graph, |
| | 404 | 143 | | ref _payloadReservation, |
| | 404 | 144 | | out _pendingLease); |
| | 404 | 145 | | if (cacheHit) |
| | | 146 | | { |
| | 277 | 147 | | resolved.Dispose(); |
| | 277 | 148 | | DisposeAdmission(); |
| | 277 | 149 | | MarkReady(MapSearchStatus(_pendingLease.Payload.Status)); |
| | 277 | 150 | | return Status; |
| | | 151 | | } |
| | 127 | 152 | | NavigationRayWork rayWork = _admission.RayWork; |
| | 127 | 153 | | DisposeAdmission(); |
| | 127 | 154 | | _search = new NavigationSurfaceAStarWork( |
| | 127 | 155 | | _cache.World, |
| | 127 | 156 | | _store, |
| | 127 | 157 | | resolved, |
| | 127 | 158 | | _workspace, |
| | 127 | 159 | | rayWork, |
| | 127 | 160 | | _payloadReservation.MaximumBytes); |
| | 127 | 161 | | return Status; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | internal NavigationAStarQueryStatus AdvanceSearch( |
| | | 165 | | int lookupStepLimit, |
| | | 166 | | int nodeStepLimit, |
| | | 167 | | int edgeStepLimit, |
| | | 168 | | int connectionStepLimit) |
| | | 169 | | { |
| | 408 | 170 | | SwiftThrowHelper.ThrowIfNegative(lookupStepLimit, nameof(lookupStepLimit)); |
| | 408 | 171 | | SwiftThrowHelper.ThrowIfNegative(nodeStepLimit, nameof(nodeStepLimit)); |
| | 408 | 172 | | SwiftThrowHelper.ThrowIfNegative(edgeStepLimit, nameof(edgeStepLimit)); |
| | 408 | 173 | | SwiftThrowHelper.ThrowIfNegative(connectionStepLimit, nameof(connectionStepLimit)); |
| | 408 | 174 | | if (Status != NavigationAStarQueryStatus.Pending || IsReadyToPublish) |
| | 1 | 175 | | return Status; |
| | 407 | 176 | | if (_search == null) |
| | 1 | 177 | | return Status; |
| | | 178 | | |
| | 406 | 179 | | NavigationSurfaceAStarStatus searchStatus = _search.Advance( |
| | 406 | 180 | | lookupStepLimit, |
| | 406 | 181 | | nodeStepLimit, |
| | 406 | 182 | | edgeStepLimit, |
| | 406 | 183 | | connectionStepLimit); |
| | 406 | 184 | | if (searchStatus != NavigationSurfaceAStarStatus.Pending) |
| | 126 | 185 | | MarkReady(MapSearchStatus(searchStatus)); |
| | 406 | 186 | | return Status; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | internal NavigationAStarQueryStatus Publish() |
| | | 190 | | { |
| | 414 | 191 | | if (!IsReadyToPublish) |
| | 1 | 192 | | return Status; |
| | 413 | 193 | | if (_pendingLease != null) |
| | | 194 | | { |
| | 276 | 195 | | NavigationAStarPayloadLease cached = _pendingLease; |
| | 276 | 196 | | _pendingLease = null; |
| | 276 | 197 | | return FinishCached(cached); |
| | | 198 | | } |
| | 137 | 199 | | if (_readyStatus is not NavigationAStarQueryStatus.Success |
| | 137 | 200 | | and not NavigationAStarQueryStatus.NoPath) |
| | | 201 | | { |
| | 15 | 202 | | return Finish(_readyStatus); |
| | | 203 | | } |
| | 122 | 204 | | if (_search == null) |
| | 1 | 205 | | return Finish(_readyStatus); |
| | 121 | 206 | | NavigationAStarPayload payload = _search!.Result; |
| | 121 | 207 | | if (!_cache.TryPublish( |
| | 121 | 208 | | payload, |
| | 121 | 209 | | _store, |
| | 121 | 210 | | ref _payloadReservation, |
| | 121 | 211 | | out NavigationAStarPayloadLease published)) |
| | | 212 | | { |
| | | 213 | | Debug.Assert( |
| | | 214 | | !_store.Current.IsDependencyCurrent(payload.Dependencies) |
| | | 215 | | || !_cache.IsWorldCurrent(payload), |
| | | 216 | | "A gate-owned reservation must cover its worker's maximum payload."); |
| | 3 | 217 | | return Finish(NavigationAStarQueryStatus.Stale); |
| | | 218 | | } |
| | 118 | 219 | | return _readyStatus == NavigationAStarQueryStatus.Success |
| | 118 | 220 | | ? FinishSuccess(published) |
| | 118 | 221 | | : FinishNegative(published, _readyStatus); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | internal NavigationAStarPayloadLease TakeResult() |
| | | 225 | | { |
| | 380 | 226 | | SwiftThrowHelper.ThrowIfTrue( |
| | 380 | 227 | | Status != NavigationAStarQueryStatus.Success || _result == null, |
| | 380 | 228 | | message: "The A* query has no successful payload lease."); |
| | 377 | 229 | | NavigationAStarPayloadLease result = _result; |
| | 377 | 230 | | _result = null; |
| | 377 | 231 | | return result; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | public void Dispose() |
| | | 235 | | { |
| | 436 | 236 | | DisposeAdmission(); |
| | 436 | 237 | | _search?.Dispose(); |
| | 436 | 238 | | _search = null; |
| | 436 | 239 | | _pendingLease?.Dispose(); |
| | 436 | 240 | | _pendingLease = null; |
| | 436 | 241 | | _result?.Dispose(); |
| | 436 | 242 | | _result = null; |
| | 436 | 243 | | _cache.ReleasePayloadReservation(ref _payloadReservation); |
| | 436 | 244 | | Volatile.Write(ref _readyToPublish, false); |
| | 436 | 245 | | _started = false; |
| | 436 | 246 | | } |
| | | 247 | | |
| | | 248 | | private void MarkReady(NavigationAStarQueryStatus status) |
| | | 249 | | { |
| | 414 | 250 | | _readyStatus = status; |
| | 414 | 251 | | Volatile.Write(ref _readyToPublish, true); |
| | 414 | 252 | | } |
| | | 253 | | |
| | | 254 | | private NavigationAStarQueryStatus FinishCached( |
| | | 255 | | NavigationAStarPayloadLease lease) |
| | | 256 | | { |
| | 276 | 257 | | if (!_store.Current.IsDependencyCurrent(lease.Payload.Dependencies) |
| | 276 | 258 | | || !_cache.IsWorldCurrent(lease.Payload)) |
| | | 259 | | { |
| | 1 | 260 | | _cache.RemoveExact(lease.Payload); |
| | 1 | 261 | | lease.Dispose(); |
| | 1 | 262 | | return Finish(NavigationAStarQueryStatus.Stale); |
| | | 263 | | } |
| | 275 | 264 | | NavigationAStarQueryStatus status = MapSearchStatus(lease.Payload.Status); |
| | 275 | 265 | | return status == NavigationAStarQueryStatus.Success |
| | 275 | 266 | | ? FinishSuccess(lease) |
| | 275 | 267 | | : FinishNegative(lease, status); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | private NavigationAStarQueryStatus FinishSuccess( |
| | | 271 | | NavigationAStarPayloadLease lease) |
| | | 272 | | { |
| | 378 | 273 | | _result = lease; |
| | 378 | 274 | | return Finish(NavigationAStarQueryStatus.Success); |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | private NavigationAStarQueryStatus FinishNegative( |
| | | 278 | | NavigationAStarPayloadLease lease, |
| | | 279 | | NavigationAStarQueryStatus status) |
| | | 280 | | { |
| | 15 | 281 | | lease.Dispose(); |
| | 15 | 282 | | return Finish(status); |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | private NavigationAStarQueryStatus Finish(NavigationAStarQueryStatus status) |
| | | 286 | | { |
| | 413 | 287 | | Status = status; |
| | 413 | 288 | | Volatile.Write(ref _readyToPublish, false); |
| | 413 | 289 | | DisposeAdmission(); |
| | 413 | 290 | | _search?.Dispose(); |
| | 413 | 291 | | _search = null; |
| | 413 | 292 | | _cache.ReleasePayloadReservation(ref _payloadReservation); |
| | 413 | 293 | | return status; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | private void DisposeAdmission() |
| | | 297 | | { |
| | 1253 | 298 | | if (!_admissionActive) |
| | 821 | 299 | | return; |
| | 432 | 300 | | _admission.Dispose(); |
| | 432 | 301 | | _admissionActive = false; |
| | 432 | 302 | | } |
| | | 303 | | |
| | | 304 | | private static NavigationAStarQueryStatus MapAdmissionStatus( |
| | 11 | 305 | | NavigationQueryAdmissionStatus status) => (NavigationAStarQueryStatus)status; |
| | | 306 | | |
| | | 307 | | private static NavigationAStarQueryStatus MapSearchStatus( |
| | 678 | 308 | | NavigationSurfaceAStarStatus status) => status <= NavigationSurfaceAStarStatus.Success |
| | 678 | 309 | | ? (NavigationAStarQueryStatus)status |
| | 678 | 310 | | : (NavigationAStarQueryStatus)((byte)status + 5); |
| | | 311 | | } |