| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationFlowAdmissionGate.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 | | using GridForge.Grids; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Pathing; |
| | | 13 | | |
| | | 14 | | /// <summary>Serializes context-owned flow resource admission.</summary> |
| | | 15 | | internal sealed class NavigationFlowAdmissionGate : IDisposable |
| | | 16 | | { |
| | 999 | 17 | | private readonly object _sync = new(); |
| | | 18 | | private readonly NavigationWorldGraphStore _store; |
| | | 19 | | private readonly NavigationQueryLimits _limits; |
| | | 20 | | private readonly NavigationQueryAdmissionCoordinator _coordinator; |
| | | 21 | | private readonly NavigationFlowFieldPayloadCache _cache; |
| | | 22 | | private readonly NavigationFlowQueryWork[] _queries; |
| | | 23 | | private readonly NavigationWorldGraphLease?[] _leases; |
| | | 24 | | private readonly BatchDescriptor[] _descriptors; |
| | | 25 | | private NavigationQueryCapacityReservation _capacityReservation; |
| | | 26 | | private ulong _generation; |
| | | 27 | | private ulong _activeGeneration; |
| | | 28 | | private int _activeCount; |
| | | 29 | | private int _activeAdmittedCount; |
| | | 30 | | private int _nextAdmission; |
| | | 31 | | private int _nextPublication; |
| | | 32 | | private bool _active; |
| | | 33 | | private bool _disposed; |
| | | 34 | | |
| | 999 | 35 | | internal NavigationFlowAdmissionGate( |
| | 999 | 36 | | GridWorld world, |
| | 999 | 37 | | NavigationWorldGraphStore store, |
| | 999 | 38 | | NavigationQueryLimits limits, |
| | 999 | 39 | | NavigationQueryAdmissionCoordinator coordinator, |
| | 999 | 40 | | NavigationImmediateRayWorkspace immediateRayWorkspace) |
| | | 41 | | { |
| | 999 | 42 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 999 | 43 | | SwiftThrowHelper.ThrowIfNull(store, nameof(store)); |
| | 999 | 44 | | SwiftThrowHelper.ThrowIfNull(coordinator, nameof(coordinator)); |
| | 999 | 45 | | SwiftThrowHelper.ThrowIfArgument( |
| | 999 | 46 | | limits.MaxConcurrentNavigationQueries <= 0, |
| | 999 | 47 | | nameof(limits), |
| | 999 | 48 | | "Query limits must be explicitly initialized."); |
| | 999 | 49 | | _store = store; |
| | 999 | 50 | | _limits = limits; |
| | 999 | 51 | | _coordinator = coordinator; |
| | 999 | 52 | | _cache = new NavigationFlowFieldPayloadCache( |
| | 999 | 53 | | world, |
| | 999 | 54 | | limits.MaxFlowCacheEntries, |
| | 999 | 55 | | limits.MaxFlowReusablePayloadBytes, |
| | 999 | 56 | | limits.MaxFlowSinglePayloadBytes, |
| | 999 | 57 | | limits.MaxFlowActivePayloadBytes, |
| | 999 | 58 | | limits.MaxFlowActivePayloadLeases, |
| | 999 | 59 | | limits.FlowWorkspaceMapCapacity, |
| | 999 | 60 | | immediateRayWorkspace); |
| | 999 | 61 | | _queries = new NavigationFlowQueryWork[limits.MaxConcurrentNavigationQueries]; |
| | 999 | 62 | | _leases = new NavigationWorldGraphLease[limits.MaxConcurrentNavigationQueries]; |
| | 17576 | 63 | | for (int i = 0; i < _queries.Length; i++) |
| | | 64 | | { |
| | 7789 | 65 | | var workspace = new NavigationFlowFieldWorkspace( |
| | 7789 | 66 | | limits.FlowWorkspaceMapCapacity, |
| | 7789 | 67 | | limits.FlowWorkspaceEndpointPageCapacity, |
| | 7789 | 68 | | limits.FlowWorkspaceComponentCapacity, |
| | 7789 | 69 | | limits.FlowWorkspaceNodeCapacity, |
| | 7789 | 70 | | limits.RayWorkspaceCoveredAddressCapacity, |
| | 7789 | 71 | | limits.RayWorkspaceTraceIntervalCapacity); |
| | 7789 | 72 | | _queries[i] = new NavigationFlowQueryWork(store, workspace, _cache); |
| | | 73 | | } |
| | 999 | 74 | | _descriptors = new BatchDescriptor[limits.MaxBatchItems]; |
| | 999 | 75 | | } |
| | | 76 | | |
| | 64 | 77 | | internal NavigationFlowFieldPayloadCache PayloadCache => _cache; |
| | | 78 | | |
| | | 79 | | internal NavigationFlowQueryStatus Begin( |
| | | 80 | | PathQuery query, |
| | | 81 | | out NavigationFlowBatchWork work) |
| | | 82 | | { |
| | 54 | 83 | | lock (_sync) |
| | | 84 | | { |
| | 54 | 85 | | if (!CanBegin() || !FitsSingleEnvelope(query)) |
| | | 86 | | { |
| | 3 | 87 | | work = default; |
| | 3 | 88 | | return NavigationFlowQueryStatus.CapacityExceeded; |
| | | 89 | | } |
| | 51 | 90 | | _descriptors[0] = new BatchDescriptor(0, 0, query); |
| | 51 | 91 | | return BeginCore(1, out work); |
| | | 92 | | } |
| | 54 | 93 | | } |
| | | 94 | | |
| | | 95 | | internal NavigationFlowQueryStatus Begin( |
| | | 96 | | PathQueryBatch batch, |
| | | 97 | | out NavigationFlowBatchWork work) |
| | | 98 | | { |
| | 5 | 99 | | lock (_sync) |
| | | 100 | | { |
| | 5 | 101 | | if (!CanBegin() || !FitsBatchEnvelope(batch)) |
| | | 102 | | { |
| | 1 | 103 | | work = default; |
| | 1 | 104 | | return NavigationFlowQueryStatus.CapacityExceeded; |
| | | 105 | | } |
| | 28 | 106 | | for (int i = 0; i < batch.Count; i++) |
| | | 107 | | { |
| | 10 | 108 | | PathQueryBatchItem item = batch.Items[i]; |
| | 10 | 109 | | _descriptors[i] = new BatchDescriptor(item.StableOrdinal, i, item.Query); |
| | | 110 | | } |
| | 4 | 111 | | Array.Sort(_descriptors, 0, batch.Count); |
| | 4 | 112 | | return BeginCore(batch.Count, out work); |
| | | 113 | | } |
| | 5 | 114 | | } |
| | | 115 | | |
| | | 116 | | public void Dispose() |
| | | 117 | | { |
| | 999 | 118 | | NavigationFlowBatchWork active = default; |
| | 999 | 119 | | lock (_sync) |
| | | 120 | | { |
| | 999 | 121 | | if (_disposed) |
| | 1 | 122 | | return; |
| | 998 | 123 | | _disposed = true; |
| | 998 | 124 | | if (_active) |
| | 1 | 125 | | active = new NavigationFlowBatchWork(this, _activeGeneration); |
| | 998 | 126 | | } |
| | 998 | 127 | | active.Dispose(); |
| | 998 | 128 | | _cache.Dispose(); |
| | 999 | 129 | | } |
| | | 130 | | |
| | | 131 | | internal void CancelActive() |
| | | 132 | | { |
| | 23 | 133 | | NavigationFlowBatchWork active = default; |
| | 23 | 134 | | lock (_sync) |
| | | 135 | | { |
| | 23 | 136 | | if (_active) |
| | 1 | 137 | | active = new NavigationFlowBatchWork(this, _activeGeneration); |
| | 23 | 138 | | } |
| | 23 | 139 | | active.Dispose(); |
| | 23 | 140 | | } |
| | | 141 | | |
| | | 142 | | private bool CanBegin() => |
| | 59 | 143 | | !_disposed |
| | 59 | 144 | | && !_active; |
| | | 145 | | |
| | | 146 | | private bool FitsBatchEnvelope(PathQueryBatch batch) => |
| | 5 | 147 | | batch.Count > 0 |
| | 5 | 148 | | && batch.Count <= _limits.MaxBatchItems |
| | 5 | 149 | | && batch.GetLogicalRetainedBytes() <= _limits.MaxBatchDescriptorBytes; |
| | | 150 | | |
| | | 151 | | private bool FitsSingleEnvelope(PathQuery query) => |
| | 52 | 152 | | PathQueryBatchItem.GetLogicalRetainedBytes(query) |
| | 52 | 153 | | <= _limits.MaxBatchDescriptorBytes; |
| | | 154 | | |
| | | 155 | | private NavigationFlowQueryStatus BeginCore( |
| | | 156 | | int count, |
| | | 157 | | out NavigationFlowBatchWork work) |
| | | 158 | | { |
| | 55 | 159 | | int candidateCount = _coordinator.TryReservePrefix( |
| | 55 | 160 | | PathAlgorithm.FlowField, |
| | 55 | 161 | | Math.Min(count, _queries.Length), |
| | 55 | 162 | | out _capacityReservation); |
| | 55 | 163 | | int leased = _store.TryAcquirePrefix(_leases.AsSpan(0, candidateCount)); |
| | 55 | 164 | | _capacityReservation = _coordinator.Trim(_capacityReservation, leased); |
| | 228 | 165 | | for (int i = 0; i < leased; i++) |
| | | 166 | | { |
| | 59 | 167 | | _descriptors[i].SlotIndex = i; |
| | 59 | 168 | | _queries[i].Begin(_descriptors[i].Query, _leases[i]!); |
| | 59 | 169 | | _leases[i] = null; |
| | | 170 | | } |
| | 55 | 171 | | _generation = unchecked(_generation + 1UL); |
| | 55 | 172 | | _activeGeneration = _generation; |
| | 55 | 173 | | _activeCount = count; |
| | 55 | 174 | | _activeAdmittedCount = leased; |
| | 55 | 175 | | _nextAdmission = 0; |
| | 55 | 176 | | _nextPublication = 0; |
| | 55 | 177 | | _active = true; |
| | 55 | 178 | | work = new NavigationFlowBatchWork(this, _activeGeneration); |
| | 55 | 179 | | return NavigationFlowQueryStatus.Pending; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | private BatchDescriptor GetDescriptor(int inputIndex) |
| | | 183 | | { |
| | 220 | 184 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 220 | 185 | | inputIndex < 0 || inputIndex >= _activeCount, |
| | 220 | 186 | | inputIndex, |
| | 220 | 187 | | nameof(inputIndex)); |
| | 218 | 188 | | int descriptorOrdinal = 0; |
| | 233 | 189 | | while (_descriptors[descriptorOrdinal].InputIndex != inputIndex) |
| | 15 | 190 | | descriptorOrdinal++; |
| | 218 | 191 | | return _descriptors[descriptorOrdinal]; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | internal int GetAdmittedCount(NavigationFlowBatchWork work) |
| | | 195 | | { |
| | 6 | 196 | | lock (_sync) |
| | | 197 | | { |
| | 6 | 198 | | EnsureActive(work); |
| | 6 | 199 | | return _activeAdmittedCount; |
| | | 200 | | } |
| | 6 | 201 | | } |
| | | 202 | | |
| | | 203 | | internal bool IsAdmissionComplete(NavigationFlowBatchWork work) |
| | | 204 | | { |
| | 140 | 205 | | lock (_sync) |
| | | 206 | | { |
| | 140 | 207 | | EnsureActive(work); |
| | 140 | 208 | | return _nextAdmission >= _activeAdmittedCount; |
| | | 209 | | } |
| | 140 | 210 | | } |
| | | 211 | | |
| | | 212 | | internal NavigationFlowQueryStatus GetStatus( |
| | | 213 | | NavigationFlowBatchWork work, |
| | | 214 | | int inputIndex) |
| | | 215 | | { |
| | 61 | 216 | | lock (_sync) |
| | | 217 | | { |
| | 61 | 218 | | EnsureActive(work); |
| | 58 | 219 | | BatchDescriptor descriptor = GetDescriptor(inputIndex); |
| | 56 | 220 | | if (descriptor.SlotIndex < 0) |
| | 6 | 221 | | return NavigationFlowQueryStatus.CapacityExceeded; |
| | 50 | 222 | | NavigationFlowQueryWork query = _queries[descriptor.SlotIndex]; |
| | 50 | 223 | | lock (query) |
| | 50 | 224 | | return query.Status; |
| | | 225 | | } |
| | 56 | 226 | | } |
| | | 227 | | |
| | | 228 | | internal bool IsReadyToPublish( |
| | | 229 | | NavigationFlowBatchWork work, |
| | | 230 | | int inputIndex) |
| | | 231 | | { |
| | 87 | 232 | | lock (_sync) |
| | | 233 | | { |
| | 87 | 234 | | EnsureActive(work); |
| | 87 | 235 | | BatchDescriptor descriptor = GetDescriptor(inputIndex); |
| | 87 | 236 | | return descriptor.SlotIndex < 0 |
| | 87 | 237 | | || _queries[descriptor.SlotIndex].IsReadyToPublish; |
| | | 238 | | } |
| | 87 | 239 | | } |
| | | 240 | | |
| | | 241 | | internal void AdvanceAdmission( |
| | | 242 | | NavigationFlowBatchWork work, |
| | | 243 | | int lookupStepLimit, |
| | | 244 | | int endpointCandidateStepLimit) |
| | | 245 | | { |
| | 85 | 246 | | lock (_sync) |
| | | 247 | | { |
| | 85 | 248 | | EnsureActive(work); |
| | 134 | 249 | | while (_nextAdmission < _activeAdmittedCount) |
| | | 250 | | { |
| | 88 | 251 | | NavigationFlowQueryWork query = _queries[_nextAdmission]; |
| | 88 | 252 | | query.PrepareSearchOrCheckout( |
| | 88 | 253 | | lookupStepLimit, |
| | 88 | 254 | | endpointCandidateStepLimit); |
| | 88 | 255 | | if (!query.IsPrepared) |
| | 37 | 256 | | return; |
| | 51 | 257 | | if (query.ReservationRejected) |
| | | 258 | | { |
| | 2 | 259 | | CutCapacitySuffix(_nextAdmission); |
| | 2 | 260 | | return; |
| | | 261 | | } |
| | 49 | 262 | | _nextAdmission++; |
| | | 263 | | } |
| | 46 | 264 | | } |
| | 85 | 265 | | } |
| | | 266 | | |
| | | 267 | | internal NavigationFlowQueryStatus AdvanceSearch( |
| | | 268 | | NavigationFlowBatchWork work, |
| | | 269 | | int inputIndex, |
| | | 270 | | int lookupStepLimit, |
| | | 271 | | int nodeStepLimit, |
| | | 272 | | int edgeStepLimit, |
| | | 273 | | int connectionStepLimit) |
| | | 274 | | { |
| | | 275 | | BatchDescriptor descriptor; |
| | | 276 | | NavigationFlowQueryWork query; |
| | 40 | 277 | | lock (_sync) |
| | | 278 | | { |
| | 40 | 279 | | EnsureActive(work); |
| | 40 | 280 | | SwiftThrowHelper.ThrowIfTrue( |
| | 40 | 281 | | _nextAdmission < _activeAdmittedCount, |
| | 40 | 282 | | message: "The flow batch has not completed its sequential preparation barrier."); |
| | 38 | 283 | | descriptor = GetDescriptor(inputIndex); |
| | 38 | 284 | | if (descriptor.SlotIndex < 0) |
| | 1 | 285 | | return NavigationFlowQueryStatus.CapacityExceeded; |
| | 37 | 286 | | query = _queries[descriptor.SlotIndex]; |
| | 37 | 287 | | Monitor.Enter(query); |
| | 37 | 288 | | } |
| | | 289 | | try |
| | | 290 | | { |
| | 37 | 291 | | return query.AdvanceSearch( |
| | 37 | 292 | | lookupStepLimit, |
| | 37 | 293 | | nodeStepLimit, |
| | 37 | 294 | | edgeStepLimit, |
| | 37 | 295 | | connectionStepLimit); |
| | | 296 | | } |
| | | 297 | | finally |
| | | 298 | | { |
| | 37 | 299 | | Monitor.Exit(query); |
| | 37 | 300 | | } |
| | 38 | 301 | | } |
| | | 302 | | |
| | | 303 | | internal int PublishReadyPrefix( |
| | | 304 | | NavigationFlowBatchWork work, |
| | | 305 | | int maximumCount) |
| | | 306 | | { |
| | 49 | 307 | | SwiftThrowHelper.ThrowIfNegative(maximumCount, nameof(maximumCount)); |
| | 49 | 308 | | lock (_sync) |
| | | 309 | | { |
| | 49 | 310 | | EnsureActive(work); |
| | 49 | 311 | | SwiftThrowHelper.ThrowIfTrue( |
| | 49 | 312 | | _nextAdmission < _activeAdmittedCount, |
| | 49 | 313 | | message: "The flow batch has not completed its sequential preparation barrier."); |
| | 47 | 314 | | int published = 0; |
| | 94 | 315 | | while (published < maximumCount |
| | 94 | 316 | | && _nextPublication < _activeAdmittedCount) |
| | | 317 | | { |
| | 48 | 318 | | NavigationFlowQueryWork query = _queries[_nextPublication]; |
| | 48 | 319 | | lock (query) |
| | | 320 | | { |
| | 48 | 321 | | if (!query.IsReadyToPublish) |
| | 1 | 322 | | break; |
| | 47 | 323 | | query.Publish(); |
| | 47 | 324 | | } |
| | 47 | 325 | | _nextPublication++; |
| | 47 | 326 | | published++; |
| | | 327 | | } |
| | 47 | 328 | | return published; |
| | | 329 | | } |
| | 47 | 330 | | } |
| | | 331 | | |
| | | 332 | | internal NavigationFlowQueryResult TakeResult( |
| | | 333 | | NavigationFlowBatchWork work, |
| | | 334 | | int inputIndex) |
| | | 335 | | { |
| | 37 | 336 | | lock (_sync) |
| | | 337 | | { |
| | 37 | 338 | | EnsureActive(work); |
| | 37 | 339 | | BatchDescriptor descriptor = GetDescriptor(inputIndex); |
| | 37 | 340 | | SwiftThrowHelper.ThrowIfTrue( |
| | 37 | 341 | | descriptor.SlotIndex < 0, |
| | 37 | 342 | | message: "The flow query was not admitted."); |
| | 36 | 343 | | return _queries[descriptor.SlotIndex].TakeResult(); |
| | | 344 | | } |
| | 36 | 345 | | } |
| | | 346 | | |
| | | 347 | | internal void Release(NavigationFlowBatchWork work) |
| | | 348 | | { |
| | 56 | 349 | | lock (_sync) |
| | | 350 | | { |
| | 56 | 351 | | if (!IsActive(work)) |
| | 1 | 352 | | return; |
| | 220 | 353 | | for (int i = 0; i < _activeAdmittedCount; i++) |
| | | 354 | | { |
| | 55 | 355 | | lock (_queries[i]) |
| | 55 | 356 | | _queries[i].Dispose(); |
| | | 357 | | } |
| | 55 | 358 | | _coordinator.Release(_capacityReservation); |
| | 55 | 359 | | _capacityReservation = default; |
| | 55 | 360 | | if (_activeCount > 0) |
| | 55 | 361 | | Array.Clear(_descriptors, 0, _activeCount); |
| | 55 | 362 | | _active = false; |
| | 55 | 363 | | _activeGeneration = 0; |
| | 55 | 364 | | _activeCount = 0; |
| | 55 | 365 | | _activeAdmittedCount = 0; |
| | 55 | 366 | | _nextAdmission = 0; |
| | 55 | 367 | | _nextPublication = 0; |
| | 55 | 368 | | } |
| | 56 | 369 | | } |
| | | 370 | | |
| | | 371 | | private void CutCapacitySuffix(int firstRejected) |
| | | 372 | | { |
| | 2 | 373 | | int previousCount = _activeAdmittedCount; |
| | 12 | 374 | | for (int i = firstRejected; i < previousCount; i++) |
| | | 375 | | { |
| | 4 | 376 | | _queries[i].Dispose(); |
| | 4 | 377 | | _descriptors[i].SlotIndex = -1; |
| | | 378 | | } |
| | 2 | 379 | | _activeAdmittedCount = firstRejected; |
| | 2 | 380 | | _nextAdmission = firstRejected; |
| | 2 | 381 | | _capacityReservation = _coordinator.Trim( |
| | 2 | 382 | | _capacityReservation, |
| | 2 | 383 | | firstRejected); |
| | 2 | 384 | | } |
| | | 385 | | |
| | | 386 | | private void EnsureActive(NavigationFlowBatchWork work) |
| | | 387 | | { |
| | 505 | 388 | | if (!IsActive(work)) |
| | 3 | 389 | | throw new ObjectDisposedException(nameof(NavigationFlowBatchWork)); |
| | 502 | 390 | | } |
| | | 391 | | |
| | | 392 | | private bool IsActive(NavigationFlowBatchWork work) => |
| | 561 | 393 | | _active |
| | 561 | 394 | | && work.IsOwnedBy(this) |
| | 561 | 395 | | && work.Generation == _activeGeneration; |
| | | 396 | | |
| | | 397 | | private struct BatchDescriptor : IComparable<BatchDescriptor> |
| | | 398 | | { |
| | | 399 | | internal BatchDescriptor(long stableOrdinal, int inputIndex, PathQuery query) |
| | | 400 | | { |
| | 61 | 401 | | StableOrdinal = stableOrdinal; |
| | 61 | 402 | | InputIndex = inputIndex; |
| | 61 | 403 | | Query = query; |
| | 61 | 404 | | SlotIndex = -1; |
| | 61 | 405 | | } |
| | | 406 | | |
| | | 407 | | internal long StableOrdinal; |
| | | 408 | | internal int InputIndex; |
| | | 409 | | internal PathQuery Query; |
| | | 410 | | internal int SlotIndex; |
| | | 411 | | |
| | | 412 | | public readonly int CompareTo(BatchDescriptor other) |
| | | 413 | | { |
| | 8 | 414 | | int ordinalComparison = StableOrdinal.CompareTo(other.StableOrdinal); |
| | 8 | 415 | | return ordinalComparison != 0 |
| | 8 | 416 | | ? ordinalComparison |
| | 8 | 417 | | : InputIndex.CompareTo(other.InputIndex); |
| | | 418 | | } |
| | | 419 | | } |
| | | 420 | | } |