| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationGraphRuntime.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 GridForge.Grids; |
| | | 10 | | using GridForge.Grids.Topology; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | |
| | | 13 | | namespace Trailblazer.Pathing; |
| | | 14 | | |
| | | 15 | | /// <summary>Owns map operations, GridForge ingress, and immutable root publication for one context.</summary> |
| | | 16 | | internal sealed class NavigationGraphRuntime : IDisposable |
| | | 17 | | { |
| | | 18 | | internal enum OperationTerminalDisposition |
| | | 19 | | { |
| | | 20 | | None, |
| | | 21 | | Release, |
| | | 22 | | ReleaseAndBeginRollback, |
| | | 23 | | ReleaseAndTryReopen |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | internal enum PolicyFrameDisposition |
| | | 27 | | { |
| | | 28 | | None, |
| | | 29 | | Complete, |
| | | 30 | | RejectCapacity |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | internal enum AutomaticSeamAdvanceDisposition |
| | | 34 | | { |
| | | 35 | | Continue, |
| | | 36 | | Retain, |
| | | 37 | | Restart, |
| | | 38 | | Complete |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | internal enum MaterializedAdvanceDisposition |
| | | 42 | | { |
| | | 43 | | Restart, |
| | | 44 | | RejectCapacity, |
| | | 45 | | Defer, |
| | | 46 | | Ready, |
| | | 47 | | Publish |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | internal enum PublishedCandidateEffect |
| | | 51 | | { |
| | | 52 | | InstallMaterialized, |
| | | 53 | | CompleteMaterialized, |
| | | 54 | | CompleteMaterializedWithinStructuralPublication, |
| | | 55 | | CompleteAutomaticSeam, |
| | | 56 | | CompleteComposition, |
| | | 57 | | ReopenStructuralScopes, |
| | | 58 | | StartAutomaticSeam |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | private readonly GridWorld _world; |
| | | 62 | | private readonly NavigationOperationProcessor _operations; |
| | | 63 | | private readonly NavigationGridChangeIngress _ingress; |
| | | 64 | | private readonly NavigationWorldGraphStore _store; |
| | | 65 | | private readonly NavigationAreaCatalogProcessor _areaPolicies; |
| | | 66 | | private readonly MaintenanceWorkBudget _maintenanceBudget; |
| | | 67 | | private readonly MaintenanceWorkMeter _maintenanceMeter; |
| | | 68 | | private readonly int _maxDynamicSlotsPerMap; |
| | | 69 | | private readonly int _maxDynamicSlots; |
| | | 70 | | private readonly long _maxActiveSnapshotBytes; |
| | | 71 | | private readonly int _maxPersistentGraphPages; |
| | | 72 | | private readonly NavigationCandidatePublisher _publishCandidate; |
| | | 73 | | private readonly NavigationRetainedWorkGuard _retainedWorkGuard; |
| | | 74 | | private readonly Action _maintainSnapshot; |
| | | 75 | | private readonly GridEventInfo[] _maintenanceEvents; |
| | | 76 | | private readonly NavigationGridChangeScope[] _blockedScopes; |
| | | 77 | | private readonly NavigationGridChangeScope[] _resnapshotScopes; |
| | | 78 | | private readonly NavigationGridChangeScope[] _deferredScopes; |
| | | 79 | | private readonly NavigationGridChangeScope[] _priorBlockedScopes; |
| | | 80 | | private readonly NavigationGridBaselineCapture[] _baselineCaptures; |
| | 1197 | 81 | | private PersistentStringMap<NavigationBaselineRebuild> _baselineRebuilds = |
| | 1197 | 82 | | PersistentStringMap<NavigationBaselineRebuild>.Empty; |
| | | 83 | | private NavigationStructuralCompositionWork? _compositionWork; |
| | | 84 | | private NavigationMaterializedComponentWork? _materializedComponentWork; |
| | | 85 | | private bool _materializedComponentWorkCompletesOperation; |
| | | 86 | | private bool _materializedStartsAutomaticSeamLifecycle; |
| | | 87 | | private NavigationAutomaticSeamLifecycleWork? _lifecycleWork; |
| | | 88 | | private readonly VoxelIndex[] _baselineAddressScratch; |
| | | 89 | | private readonly GridCoveredAddress[] _baselineCoveredAddressScratch; |
| | | 90 | | private int _publicationEventCount; |
| | | 91 | | private int _lifecycleEventCount; |
| | | 92 | | private int _publicationBlockedScopeCount; |
| | | 93 | | private int _publicationResnapshotScopeCount; |
| | 1197 | 94 | | private NavigationAreaCatalog _publicationAreaCatalog = NavigationAreaCatalog.Empty; |
| | | 95 | | private bool _publicationResnapshotAll; |
| | | 96 | | private bool _publicationBlockAll; |
| | 1197 | 97 | | private NavigationWorldGraph _snapshotGraph = NavigationWorldGraph.Empty; |
| | 1197 | 98 | | private NavigationWorldGraph _snapshotPreviousGraph = NavigationWorldGraph.Empty; |
| | 1197 | 99 | | private NavigationOperationFrameChange[] _snapshotChanges = Array.Empty<NavigationOperationFrameChange>(); |
| | | 100 | | private int _snapshotChangeCount; |
| | | 101 | | private bool _snapshotStartsAutomaticSeamLifecycle; |
| | | 102 | | private int _snapshotAffectedCount; |
| | | 103 | | private int _snapshotDeferredScopeCount; |
| | | 104 | | private long _snapshotWorldSpawnToken; |
| | | 105 | | private bool _snapshotDeferAll; |
| | | 106 | | private int _priorBlockedScopeCount; |
| | | 107 | | private bool _priorBlockAll; |
| | | 108 | | private readonly int[] _affectedMapOrdinals; |
| | | 109 | | private readonly int[] _affectedMapStamps; |
| | | 110 | | private int _affectedMapStamp; |
| | | 111 | | private int _lastAffectedMapCollectionCount; |
| | | 112 | | private bool _publishedThisMaintenance; |
| | | 113 | | private bool _automaticSeamFullRebuildPending; |
| | | 114 | | private bool _operationClosureRollbackPending; |
| | | 115 | | private NavigationSurfaceComponentKeySet? _ownedStructuralClosureBaseline; |
| | | 116 | | private bool _ownedStructuralClosureBaselineAll; |
| | | 117 | | private bool _resetPending; |
| | | 118 | | private bool _disposed; |
| | | 119 | | |
| | 1197 | 120 | | internal NavigationGraphRuntime(GridWorld world, TrailblazerWorldContextSettings settings) |
| | | 121 | | { |
| | 1197 | 122 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 1197 | 123 | | SwiftThrowHelper.ThrowIfNull(settings, nameof(settings)); |
| | 1197 | 124 | | _world = world; |
| | 1197 | 125 | | _operations = new NavigationOperationProcessor( |
| | 1197 | 126 | | settings.OperationLimits, |
| | 1197 | 127 | | int.MaxValue, |
| | 1197 | 128 | | settings.NavigationAreaCount); |
| | 1197 | 129 | | int ingressCapacity = NavigationGridChangeIngress.GetMaximumCapacity( |
| | 1197 | 130 | | settings.MaxIngressEntries, |
| | 1197 | 131 | | settings.MaxIngressBytes, |
| | 1197 | 132 | | settings.OperationLimits.MaxMaps); |
| | 1197 | 133 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1197 | 134 | | ingressCapacity <= 0, |
| | 1197 | 135 | | nameof(settings), |
| | 1197 | 136 | | "Ingress byte capacity must fit at least one final-state event."); |
| | 1197 | 137 | | _ingress = new NavigationGridChangeIngress( |
| | 1197 | 138 | | ingressCapacity, |
| | 1197 | 139 | | settings.OperationLimits.MaxMaps); |
| | 1197 | 140 | | _store = new NavigationWorldGraphStore( |
| | 1197 | 141 | | settings.MaxActiveSnapshots, |
| | 1197 | 142 | | settings.MaxRetiredSnapshots, |
| | 1197 | 143 | | settings.MaxRetiredSnapshotBytes, |
| | 1197 | 144 | | settings.MaxActiveSnapshotBytes, |
| | 1197 | 145 | | settings.MaxPersistentGraphPages, |
| | 1197 | 146 | | settings.MaxConcurrentSnapshotLeases); |
| | 1197 | 147 | | _areaPolicies = new NavigationAreaCatalogProcessor(settings); |
| | 1197 | 148 | | _maintenanceBudget = settings.MaintenanceBudget; |
| | 1197 | 149 | | _maintenanceMeter = new MaintenanceWorkMeter(_maintenanceBudget); |
| | 1197 | 150 | | _maxDynamicSlotsPerMap = settings.MaxDynamicCellSlotsPerMap; |
| | 1197 | 151 | | _maxDynamicSlots = settings.MaxDynamicCellSlots; |
| | 1197 | 152 | | _maxActiveSnapshotBytes = settings.MaxActiveSnapshotBytes; |
| | 1197 | 153 | | _maxPersistentGraphPages = settings.MaxPersistentGraphPages; |
| | 1197 | 154 | | _maintenanceEvents = new GridEventInfo[_maintenanceBudget.MaxConsumedEnvelopes]; |
| | 1197 | 155 | | _blockedScopes = new NavigationGridChangeScope[settings.OperationLimits.MaxMaps]; |
| | 1197 | 156 | | _resnapshotScopes = new NavigationGridChangeScope[settings.OperationLimits.MaxMaps]; |
| | 1197 | 157 | | _deferredScopes = new NavigationGridChangeScope[settings.OperationLimits.MaxMaps]; |
| | 1197 | 158 | | _priorBlockedScopes = new NavigationGridChangeScope[settings.OperationLimits.MaxMaps]; |
| | 1197 | 159 | | _baselineCaptures = new NavigationGridBaselineCapture[settings.OperationLimits.MaxMaps]; |
| | 1197 | 160 | | _baselineAddressScratch = new VoxelIndex[_maintenanceBudget.MaxBaselineAddresses]; |
| | 1197 | 161 | | _baselineCoveredAddressScratch = |
| | 1197 | 162 | | new GridCoveredAddress[_maintenanceBudget.MaxBaselineAddresses]; |
| | 1197 | 163 | | _affectedMapOrdinals = new int[settings.OperationLimits.MaxMaps]; |
| | 1197 | 164 | | _affectedMapStamps = new int[settings.OperationLimits.MaxMaps]; |
| | 1197 | 165 | | _publishCandidate = PublishPendingCandidate; |
| | 1197 | 166 | | _retainedWorkGuard = IsOperationWithinRetainedWorkCapacity; |
| | 1197 | 167 | | _maintainSnapshot = MaintainSnapshot; |
| | 1197 | 168 | | } |
| | | 169 | | |
| | 466 | 170 | | internal NavigationWorldGraph Current => _store.Current; |
| | | 171 | | |
| | 3043 | 172 | | internal NavigationWorldGraphStore Store => _store; |
| | | 173 | | |
| | | 174 | | internal int RetainedBaselineCaptureCount |
| | | 175 | | { |
| | | 176 | | get |
| | | 177 | | { |
| | 24 | 178 | | int count = 0; |
| | 816 | 179 | | for (int i = 0; i < _baselineCaptures.Length; i++) |
| | | 180 | | { |
| | 384 | 181 | | if (_baselineCaptures[i].IsRequested) |
| | 1 | 182 | | count++; |
| | | 183 | | } |
| | 24 | 184 | | return count; |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | 552 | 188 | | internal int RetainedCompositionWorkCount => (_compositionWork == null ? 0 : 1) |
| | 552 | 189 | | + (_materializedComponentWork == null ? 0 : 1) |
| | 552 | 190 | | + (_lifecycleWork == null ? 0 : 1); |
| | | 191 | | |
| | 2232 | 192 | | internal long RetainedCompositionWorkBytes => checked( |
| | 2232 | 193 | | (_compositionWork?.RetainedBytes ?? 0L) |
| | 2232 | 194 | | + (_materializedComponentWork?.RetainedBytes ?? 0L) |
| | 2232 | 195 | | + (_lifecycleWork?.RetainedBytes ?? 0L) |
| | 2232 | 196 | | + GetOwnedClosureBaselineAdditionalRetainedBytes(_store.Current)); |
| | | 197 | | |
| | 31 | 198 | | internal long RetainedOperationWorkBytes => _operations.RetainedOperationWorkBytes; |
| | | 199 | | |
| | 48 | 200 | | internal int RetainedOperationWorkCount => _operations.RetainedOperationWorkCount; |
| | | 201 | | |
| | | 202 | | internal int RetainedCompositionWorkPageCount => |
| | 2572 | 203 | | checked( |
| | 2572 | 204 | | (_compositionWork?.PersistentPageCount ?? 0) |
| | 2572 | 205 | | + (_materializedComponentWork?.PersistentPageCount ?? 0) |
| | 2572 | 206 | | + (_lifecycleWork?.PersistentPageCount ?? 0) |
| | 2572 | 207 | | + GetOwnedClosureBaselineAdditionalPersistentPages(_store.Current)); |
| | | 208 | | |
| | 373 | 209 | | internal int RetainedOperationWorkPageCount => _operations.RetainedOperationWorkPageCount; |
| | | 210 | | |
| | 321 | 211 | | internal MaintenanceWorkMeter MaintenanceMeter => _maintenanceMeter; |
| | | 212 | | |
| | 2 | 213 | | internal int LastAffectedMapCollectionCount => _lastAffectedMapCollectionCount; |
| | | 214 | | |
| | | 215 | | internal bool Admit(NavigationMapCommitOperation operation) |
| | | 216 | | { |
| | 1136 | 217 | | EnsureUsable(); |
| | 1136 | 218 | | return _operations.Admit(operation); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | internal bool Admit(NavigationMapRemoveOperation operation) |
| | | 222 | | { |
| | 9 | 223 | | EnsureUsable(); |
| | 9 | 224 | | return _operations.Admit(operation); |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | internal bool Admit(NavigationOverlayCommitOperation operation) |
| | | 228 | | { |
| | 191 | 229 | | EnsureUsable(); |
| | 191 | 230 | | return _operations.Admit(operation); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | internal bool Admit(NavigationAreaPolicyCommitOperation operation) |
| | | 234 | | { |
| | 172 | 235 | | EnsureUsable(); |
| | 172 | 236 | | return _areaPolicies.Admit(operation); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | internal NavigationWorldGraphLease? TryAcquire() |
| | | 240 | | { |
| | 1550 | 241 | | EnsureUsable(); |
| | 1550 | 242 | | return _store.TryAcquire(); |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | internal void Maintain(int frame) |
| | | 246 | | { |
| | 20559 | 247 | | EnsureUsable(); |
| | 20559 | 248 | | _maintenanceMeter.Reset(); |
| | 20559 | 249 | | _publicationEventCount = 0; |
| | 20559 | 250 | | _publishedThisMaintenance = false; |
| | 20559 | 251 | | if (_resetPending) |
| | | 252 | | { |
| | 2 | 253 | | NavigationCandidatePublication resetPublication = _store.TryPublish( |
| | 2 | 254 | | NavigationWorldGraph.CreateEmpty(_store.Current.GraphVersion + 1)); |
| | 2 | 255 | | if (resetPublication != NavigationCandidatePublication.Published) |
| | 1 | 256 | | return; |
| | 1 | 257 | | _resetPending = false; |
| | 1 | 258 | | _store.ClearSafetyPending(); |
| | | 259 | | } |
| | 20558 | 260 | | if (_operationClosureRollbackPending |
| | 20558 | 261 | | && !_automaticSeamFullRebuildPending |
| | 20558 | 262 | | && !_ingress.HasPendingWork |
| | 20558 | 263 | | && _materializedComponentWork == null |
| | 20558 | 264 | | && _lifecycleWork == null) |
| | | 265 | | { |
| | 2 | 266 | | ApplyOperationTerminalRollback( |
| | 2 | 267 | | OperationTerminalDisposition.ReleaseAndTryReopen); |
| | 2 | 268 | | return; |
| | | 269 | | } |
| | 20556 | 270 | | if (_lifecycleWork != null) |
| | | 271 | | { |
| | | 272 | | // Lifecycle work owns the detached prefix in _maintenanceEvents. Its published |
| | | 273 | | // source is already all-closed, so leave later ingress queued while leases block |
| | | 274 | | // publication rather than overwriting the retained prefix with a pressure drain. |
| | 220 | 275 | | if (!_store.CanPublish) |
| | 2 | 276 | | return; |
| | 218 | 277 | | MaintainAutomaticSeamLifecycle(); |
| | 218 | 278 | | return; |
| | | 279 | | } |
| | | 280 | | |
| | 20336 | 281 | | if (!_store.CanPublish) |
| | | 282 | | { |
| | 17 | 283 | | DrainSafetyPrefixUnderPressure(); |
| | 17 | 284 | | return; |
| | | 285 | | } |
| | | 286 | | |
| | 20319 | 287 | | if (_materializedComponentWork != null) |
| | | 288 | | { |
| | 3518 | 289 | | if (!_materializedComponentWorkCompletesOperation |
| | 3518 | 290 | | || _operations.RetainedOperationWorkCount == 0) |
| | | 291 | | { |
| | 188 | 292 | | MaintainMaterializedComponentWork(); |
| | 188 | 293 | | return; |
| | | 294 | | } |
| | 3330 | 295 | | if (MaintainMaterializedComponentWork(publish: false) |
| | 3330 | 296 | | == NavigationCandidatePublication.Deferred) |
| | | 297 | | { |
| | 3125 | 298 | | return; |
| | | 299 | | } |
| | | 300 | | } |
| | | 301 | | |
| | 17006 | 302 | | if (_operations.RetainedOperationWorkCount == 0 |
| | 17006 | 303 | | && _compositionWork == null |
| | 17006 | 304 | | && _materializedComponentWork == null) |
| | | 305 | | { |
| | 3600 | 306 | | NavigationWorldGraph ingressCurrent = _store.Current; |
| | 3600 | 307 | | NavigationCandidatePublication ingressPublication = ReconcileAndPublish( |
| | 3600 | 308 | | ingressCurrent, |
| | 3600 | 309 | | Array.Empty<NavigationOperationFrameChange>(), |
| | 3600 | 310 | | 0, |
| | 3600 | 311 | | ingressCurrent.GraphVersion + 1, |
| | 3600 | 312 | | startAutomaticSeamLifecycle: true); |
| | 3600 | 313 | | if (_lifecycleWork != null |
| | 3600 | 314 | | || _publishedThisMaintenance |
| | 3600 | 315 | | || ingressPublication != NavigationCandidatePublication.Published) |
| | | 316 | | { |
| | 136 | 317 | | return; |
| | | 318 | | } |
| | | 319 | | } |
| | | 320 | | |
| | 16870 | 321 | | NavigationWorldGraph before = _store.Current; |
| | 16870 | 322 | | bool graphWorkWasPending = _operations.RetainedOperationWorkCount != 0 |
| | 16870 | 323 | | || _compositionWork != null |
| | 16870 | 324 | | || _materializedComponentWork != null; |
| | 16870 | 325 | | NavigationAreaCatalogProcessor.PreparedFrame policyFrame = default; |
| | 16870 | 326 | | bool policyPrepared = !graphWorkWasPending; |
| | 16870 | 327 | | _publicationAreaCatalog = before.AreaCatalog; |
| | 16870 | 328 | | if (policyPrepared) |
| | | 329 | | { |
| | 3464 | 330 | | policyFrame = _areaPolicies.Prepare( |
| | 3464 | 331 | | frame, |
| | 3464 | 332 | | before.AreaCatalog, |
| | 3464 | 333 | | _maintenanceMeter, |
| | 3464 | 334 | | checked(_maxActiveSnapshotBytes - before.RetainedBytes + before.AreaCatalog.RetainedBytes), |
| | 3464 | 335 | | checked(_maxPersistentGraphPages - before.PersistentPageCount + before.AreaCatalog.PersistentPageCount)) |
| | 3464 | 336 | | _publicationAreaCatalog = policyFrame.Candidate; |
| | | 337 | | } |
| | 16870 | 338 | | NavigationOperationFrameResult operationResult = _operations.ProcessFrame( |
| | 16870 | 339 | | frame, |
| | 16870 | 340 | | _publishCandidate, |
| | 16870 | 341 | | _maintenanceMeter, |
| | 16870 | 342 | | _retainedWorkGuard); |
| | 16870 | 343 | | OperationTerminalDisposition terminalDisposition = |
| | 16870 | 344 | | ClassifyOperationTerminalDisposition( |
| | 16870 | 345 | | operationResult, |
| | 16870 | 346 | | _operations.RetainedOperationWorkCount, |
| | 16870 | 347 | | _compositionWork != null, |
| | 16870 | 348 | | _materializedComponentWorkCompletesOperation, |
| | 16870 | 349 | | _store.Current.HasClosedStructuralScope, |
| | 16870 | 350 | | _automaticSeamFullRebuildPending); |
| | 16870 | 351 | | if (terminalDisposition != OperationTerminalDisposition.None) |
| | | 352 | | { |
| | 176 | 353 | | _compositionWork = null; |
| | 176 | 354 | | AbandonMaterializedSnapshot(_materializedComponentWorkCompletesOperation); |
| | 176 | 355 | | bool returnsAfterCleanup = terminalDisposition is |
| | 176 | 356 | | OperationTerminalDisposition.ReleaseAndBeginRollback or |
| | 176 | 357 | | OperationTerminalDisposition.ReleaseAndTryReopen; |
| | 176 | 358 | | ApplyOperationTerminalRollback(terminalDisposition); |
| | 176 | 359 | | if (returnsAfterCleanup) |
| | 3 | 360 | | return; |
| | | 361 | | } |
| | 16867 | 362 | | if (operationResult == NavigationOperationFrameResult.Published) |
| | | 363 | | { |
| | 945 | 364 | | if (policyPrepared) |
| | 764 | 365 | | policyFrame.Complete(frame); |
| | 945 | 366 | | return; |
| | | 367 | | } |
| | 15922 | 368 | | if (operationResult == NavigationOperationFrameResult.Deferred) |
| | | 369 | | { |
| | 13414 | 370 | | _publicationAreaCatalog = before.AreaCatalog; |
| | 13414 | 371 | | long retainedWorkBytes = checked( |
| | 13414 | 372 | | _operations.RetainedOperationWorkBytes |
| | 13414 | 373 | | + (_compositionWork?.RetainedBytes ?? 0L)); |
| | 13414 | 374 | | int retainedWorkPages = checked( |
| | 13414 | 375 | | _operations.RetainedOperationWorkPageCount |
| | 13414 | 376 | | + (_compositionWork?.PersistentPageCount ?? 0)); |
| | 13415 | 377 | | if (!IsWithinRetainedWorkCapacity(retainedWorkBytes, retainedWorkPages)) RejectDeferredOperationForCapacity( |
| | 13413 | 378 | | else if (_compositionWork != null) |
| | | 379 | | { |
| | 10859 | 380 | | if (!_publishedThisMaintenance) |
| | | 381 | | { |
| | 10368 | 382 | | NavigationWorldGraph compositionCurrent = _store.Current; |
| | 10368 | 383 | | ReconcileAndPublish( |
| | 10368 | 384 | | compositionCurrent, |
| | 10368 | 385 | | Array.Empty<NavigationOperationFrameChange>(), |
| | 10368 | 386 | | 0, |
| | 10368 | 387 | | compositionCurrent.GraphVersion + 1); |
| | | 388 | | } |
| | | 389 | | } |
| | 2554 | 390 | | else if (!_publishedThisMaintenance) |
| | | 391 | | { |
| | 2553 | 392 | | NavigationWorldGraph publishedGraph = _store.Current; |
| | 2553 | 393 | | long safetyVersion = publishedGraph.GraphVersion + 1; |
| | 2553 | 394 | | CaptureOwnedStructuralClosureBaseline(publishedGraph); |
| | 2553 | 395 | | NavigationWorldGraph closed = publishedGraph.WithClosedStructuralComponents( |
| | 2553 | 396 | | NavigationSurfaceComponentKeySet.Empty, |
| | 2553 | 397 | | true, |
| | 2553 | 398 | | safetyVersion); |
| | 2553 | 399 | | NavigationCandidatePublication closurePublication = ReconcileAndPublish( |
| | 2553 | 400 | | closed, |
| | 2553 | 401 | | Array.Empty<NavigationOperationFrameChange>(), |
| | 2553 | 402 | | 0, |
| | 2553 | 403 | | safetyVersion); |
| | 2553 | 404 | | if (closurePublication == NavigationCandidatePublication.PermanentCapacity) |
| | 1 | 405 | | RejectDeferredOperationForCapacity(); |
| | | 406 | | } |
| | 13414 | 407 | | if (_compositionWork != null) |
| | 10859 | 408 | | RequeuePublicationEvents(); |
| | 13414 | 409 | | return; |
| | | 410 | | } |
| | | 411 | | |
| | 2508 | 412 | | NavigationWorldGraph current = _store.Current; |
| | 2508 | 413 | | long nextVersion = current.GraphVersion + 1; |
| | 2508 | 414 | | NavigationAreaCatalog fallbackCatalog = policyPrepared |
| | 2508 | 415 | | ? policyFrame.Candidate |
| | 2508 | 416 | | : current.AreaCatalog; |
| | 2508 | 417 | | NavigationCandidatePublication fallbackPublication = ReconcileAndPublish( |
| | 2508 | 418 | | current.WithAreaCatalog(fallbackCatalog, nextVersion), |
| | 2508 | 419 | | Array.Empty<NavigationOperationFrameChange>(), |
| | 2508 | 420 | | 0, |
| | 2508 | 421 | | nextVersion); |
| | 2508 | 422 | | CompletePolicyFrame(policyFrame, frame, policyPrepared, fallbackPublication); |
| | 2508 | 423 | | _publicationAreaCatalog = _store.Current.AreaCatalog; |
| | 2508 | 424 | | } |
| | | 425 | | |
| | | 426 | | internal static void CompletePolicyFrame( |
| | | 427 | | NavigationAreaCatalogProcessor.PreparedFrame policyFrame, |
| | | 428 | | int frame, |
| | | 429 | | bool policyPrepared, |
| | | 430 | | NavigationCandidatePublication publication) |
| | | 431 | | { |
| | 2512 | 432 | | switch (ClassifyPolicyFrameDisposition(policyPrepared, publication)) |
| | | 433 | | { |
| | | 434 | | case PolicyFrameDisposition.Complete: |
| | 2506 | 435 | | policyFrame.Complete(frame); |
| | 2506 | 436 | | break; |
| | | 437 | | case PolicyFrameDisposition.RejectCapacity: |
| | 1 | 438 | | policyFrame.CompleteCapacityRejected(); |
| | | 439 | | break; |
| | | 440 | | } |
| | 1 | 441 | | } |
| | | 442 | | |
| | | 443 | | internal static PolicyFrameDisposition ClassifyPolicyFrameDisposition( |
| | | 444 | | bool policyPrepared, |
| | | 445 | | NavigationCandidatePublication publication) |
| | | 446 | | { |
| | 2512 | 447 | | if (!policyPrepared) |
| | 4 | 448 | | return PolicyFrameDisposition.None; |
| | 2508 | 449 | | return publication switch |
| | 2508 | 450 | | { |
| | 2506 | 451 | | NavigationCandidatePublication.Published => PolicyFrameDisposition.Complete, |
| | 1 | 452 | | NavigationCandidatePublication.PermanentCapacity => PolicyFrameDisposition.RejectCapacity, |
| | 1 | 453 | | _ => PolicyFrameDisposition.None |
| | 2508 | 454 | | }; |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | internal static NavigationCandidatePublication ContinueAfterRetainedClosure( |
| | | 458 | | NavigationCandidatePublication publication) => |
| | 415 | 459 | | publication == NavigationCandidatePublication.Published |
| | 415 | 460 | | ? NavigationCandidatePublication.Deferred |
| | 415 | 461 | | : publication; |
| | | 462 | | |
| | | 463 | | internal static AutomaticSeamAdvanceDisposition ClassifyAutomaticSeamAdvance( |
| | | 464 | | NavigationAutomaticSeamLifecycleWork.AdvanceStatus status) |
| | | 465 | | { |
| | 1327 | 466 | | if (status == NavigationAutomaticSeamLifecycleWork.AdvanceStatus.Progressed) |
| | 1105 | 467 | | return AutomaticSeamAdvanceDisposition.Continue; |
| | 222 | 468 | | if (status == NavigationAutomaticSeamLifecycleWork.AdvanceStatus.Blocked) |
| | 184 | 469 | | return AutomaticSeamAdvanceDisposition.Retain; |
| | 38 | 470 | | return status == NavigationAutomaticSeamLifecycleWork.AdvanceStatus.Complete |
| | 38 | 471 | | ? AutomaticSeamAdvanceDisposition.Complete |
| | 38 | 472 | | : AutomaticSeamAdvanceDisposition.Restart; |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | internal static MaterializedAdvanceDisposition ClassifyMaterializedAdvance( |
| | | 476 | | bool requiresRestart, |
| | | 477 | | bool withinCapacity, |
| | | 478 | | bool complete, |
| | | 479 | | bool publish, |
| | | 480 | | out NavigationCandidatePublication? abandonmentPublication) |
| | | 481 | | { |
| | 3728 | 482 | | if (requiresRestart) |
| | | 483 | | { |
| | 2 | 484 | | abandonmentPublication = NavigationCandidatePublication.Deferred; |
| | 2 | 485 | | return MaterializedAdvanceDisposition.Restart; |
| | | 486 | | } |
| | 3726 | 487 | | if (!withinCapacity) |
| | | 488 | | { |
| | 1 | 489 | | abandonmentPublication = NavigationCandidatePublication.PermanentCapacity; |
| | 1 | 490 | | return MaterializedAdvanceDisposition.RejectCapacity; |
| | | 491 | | } |
| | 3725 | 492 | | abandonmentPublication = null; |
| | 3725 | 493 | | if (!complete) |
| | 3277 | 494 | | return MaterializedAdvanceDisposition.Defer; |
| | 448 | 495 | | return publish |
| | 448 | 496 | | ? MaterializedAdvanceDisposition.Publish |
| | 448 | 497 | | : MaterializedAdvanceDisposition.Ready; |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | internal static OperationTerminalDisposition ClassifyOperationTerminalDisposition( |
| | | 501 | | NavigationOperationFrameResult result, |
| | | 502 | | int retainedOperationWorkCount, |
| | | 503 | | bool hasCompositionWork, |
| | | 504 | | bool materializedCompletesOperation, |
| | | 505 | | bool hasClosedStructuralScope, |
| | | 506 | | bool automaticSeamFullRebuildPending) |
| | | 507 | | { |
| | 16876 | 508 | | if (result == NavigationOperationFrameResult.Published |
| | 16876 | 509 | | && retainedOperationWorkCount == 0 |
| | 16876 | 510 | | && (hasCompositionWork || materializedCompletesOperation)) |
| | | 511 | | { |
| | 3 | 512 | | return OperationTerminalDisposition.ReleaseAndBeginRollback; |
| | | 513 | | } |
| | 16873 | 514 | | if (result != NavigationOperationFrameResult.Rejected) |
| | 16695 | 515 | | return OperationTerminalDisposition.None; |
| | 178 | 516 | | if (!hasClosedStructuralScope) |
| | 174 | 517 | | return OperationTerminalDisposition.Release; |
| | 4 | 518 | | return automaticSeamFullRebuildPending |
| | 4 | 519 | | ? OperationTerminalDisposition.ReleaseAndBeginRollback |
| | 4 | 520 | | : OperationTerminalDisposition.ReleaseAndTryReopen; |
| | | 521 | | } |
| | | 522 | | |
| | | 523 | | internal static void GetOwnedClosureBaselineAdditionalCapacity( |
| | | 524 | | NavigationSurfaceComponentKeySet? baseline, |
| | | 525 | | NavigationWorldGraph ownerGraph, |
| | | 526 | | out long retainedBytes, |
| | | 527 | | out int persistentPages) |
| | | 528 | | { |
| | 40215 | 529 | | if (baseline == null |
| | 40215 | 530 | | || ReferenceEquals(baseline, NavigationSurfaceComponentKeySet.Empty) |
| | 40215 | 531 | | || ownerGraph.RetainsClosedComponentRoot(baseline)) |
| | | 532 | | { |
| | 40214 | 533 | | retainedBytes = 0; |
| | 40214 | 534 | | persistentPages = 0; |
| | 40214 | 535 | | return; |
| | | 536 | | } |
| | 1 | 537 | | retainedBytes = baseline.RetainedBytes; |
| | 1 | 538 | | persistentPages = baseline.PersistentPageCount; |
| | 1 | 539 | | } |
| | | 540 | | |
| | | 541 | | internal static bool IsRetainedWorkWithinCapacity( |
| | | 542 | | long rootBytes, |
| | | 543 | | int rootPages, |
| | | 544 | | long workBytes, |
| | | 545 | | int workPages, |
| | | 546 | | long rebuildBytes, |
| | | 547 | | int rebuildPages, |
| | | 548 | | long maximumBytes, |
| | | 549 | | int maximumPages) |
| | | 550 | | { |
| | | 551 | | System.Diagnostics.Debug.Assert(workPages >= 0); |
| | | 552 | | System.Diagnostics.Debug.Assert(rebuildPages >= 0); |
| | 40001 | 553 | | long retainedPages = (long)rootPages + workPages + rebuildPages; |
| | 40001 | 554 | | return rootBytes <= maximumBytes |
| | 40001 | 555 | | && workBytes <= maximumBytes - rootBytes |
| | 40001 | 556 | | && rebuildBytes <= maximumBytes - rootBytes - workBytes |
| | 40001 | 557 | | && retainedPages <= maximumPages; |
| | | 558 | | } |
| | | 559 | | |
| | | 560 | | private void MaintainSnapshot() |
| | | 561 | | { |
| | 20249 | 562 | | _publicationEventCount = _ingress.DetachInto( |
| | 20249 | 563 | | _maintenanceEvents.AsSpan(0, _maintenanceMeter.RemainingEnvelopes), |
| | 20249 | 564 | | _blockedScopes, |
| | 20249 | 565 | | out _publicationBlockedScopeCount, |
| | 20249 | 566 | | out _publicationBlockAll, |
| | 20249 | 567 | | out bool topologyLifecycleCoverageLost); |
| | 20249 | 568 | | if (topologyLifecycleCoverageLost |
| | 20249 | 569 | | || (!_snapshotStartsAutomaticSeamLifecycle |
| | 20249 | 570 | | && ContainsTopologyLifecycle(_maintenanceEvents, _publicationEventCount))) |
| | | 571 | | { |
| | 31 | 572 | | _automaticSeamFullRebuildPending = true; |
| | | 573 | | } |
| | 20249 | 574 | | _maintenanceMeter.TryConsumeEnvelopes(_publicationEventCount); |
| | 20249 | 575 | | _publicationResnapshotScopeCount = PrepareResnapshotScopes( |
| | 20249 | 576 | | _publicationBlockedScopeCount, |
| | 20249 | 577 | | _publicationBlockAll); |
| | 20249 | 578 | | _publicationResnapshotAll = _priorBlockAll && !_publicationBlockAll; |
| | 20249 | 579 | | _snapshotWorldSpawnToken = _world.SpawnToken; |
| | 20249 | 580 | | long retainedWorkBytes = checked( |
| | 20249 | 581 | | _operations.RetainedOperationWorkBytes |
| | 20249 | 582 | | + (_compositionWork?.RetainedBytes ?? 0L)); |
| | 20249 | 583 | | int retainedWorkPages = checked( |
| | 20249 | 584 | | _operations.RetainedOperationWorkPageCount |
| | 20249 | 585 | | + (_compositionWork?.PersistentPageCount ?? 0)); |
| | 20249 | 586 | | long maximumGraphBytes = Math.Max(0L, _maxActiveSnapshotBytes - retainedWorkBytes); |
| | 20249 | 587 | | int maximumGraphPages = Math.Max(0, _maxPersistentGraphPages - retainedWorkPages); |
| | 20249 | 588 | | _snapshotAffectedCount = _snapshotGraph.CaptureMaintenanceSnapshot( |
| | 20249 | 589 | | _world, |
| | 20249 | 590 | | _snapshotPreviousGraph, |
| | 20249 | 591 | | _maintenanceEvents.AsSpan(0, _publicationEventCount), |
| | 20249 | 592 | | _resnapshotScopes.AsSpan(0, _publicationResnapshotScopeCount), |
| | 20249 | 593 | | _publicationResnapshotAll, |
| | 20249 | 594 | | _blockedScopes.AsSpan(0, _publicationBlockedScopeCount), |
| | 20249 | 595 | | _publicationBlockAll, |
| | 20249 | 596 | | _snapshotChanges, |
| | 20249 | 597 | | _snapshotChangeCount, |
| | 20249 | 598 | | _maintenanceMeter, |
| | 20249 | 599 | | maximumGraphBytes, |
| | 20249 | 600 | | maximumGraphPages, |
| | 20249 | 601 | | _baselineCaptures, |
| | 20249 | 602 | | ref _baselineRebuilds, |
| | 20249 | 603 | | _baselineAddressScratch, |
| | 20249 | 604 | | _baselineCoveredAddressScratch, |
| | 20249 | 605 | | _deferredScopes, |
| | 20249 | 606 | | out _snapshotDeferredScopeCount, |
| | 20249 | 607 | | out _snapshotDeferAll, |
| | 20249 | 608 | | _affectedMapOrdinals, |
| | 20249 | 609 | | _affectedMapStamps, |
| | 20249 | 610 | | ref _affectedMapStamp, |
| | 20249 | 611 | | out _lastAffectedMapCollectionCount); |
| | 20249 | 612 | | } |
| | | 613 | | |
| | | 614 | | private NavigationCandidatePublication ReconcileAndPublish( |
| | | 615 | | NavigationWorldGraph graph, |
| | | 616 | | NavigationOperationFrameChange[] changes, |
| | | 617 | | int changeCount, |
| | | 618 | | long graphVersion, |
| | | 619 | | bool startAutomaticSeamLifecycle = false) |
| | | 620 | | { |
| | 20454 | 621 | | if (_materializedComponentWork != null) |
| | 205 | 622 | | return MaintainMaterializedComponentWork(structuralClosure: graph); |
| | 20249 | 623 | | AdvanceCoveredBaselineRebuilds(graph); |
| | 20249 | 624 | | _snapshotGraph = graph; |
| | 20249 | 625 | | _snapshotPreviousGraph = _store.Current; |
| | 20249 | 626 | | _snapshotChanges = changes; |
| | 20249 | 627 | | _snapshotChangeCount = changeCount; |
| | 20249 | 628 | | _snapshotStartsAutomaticSeamLifecycle = startAutomaticSeamLifecycle; |
| | 20249 | 629 | | _world.ExecuteNavigationMaintenanceSnapshot(_maintainSnapshot); |
| | | 630 | | |
| | | 631 | | NavigationCandidatePublication publication; |
| | | 632 | | try |
| | | 633 | | { |
| | 20249 | 634 | | NavigationWorldGraph next = graph.ApplyMaintenanceSnapshot( |
| | 20249 | 635 | | _snapshotWorldSpawnToken, |
| | 20249 | 636 | | _snapshotPreviousGraph, |
| | 20249 | 637 | | _maintenanceEvents.AsSpan(0, _publicationEventCount), |
| | 20249 | 638 | | _resnapshotScopes.AsSpan(0, _publicationResnapshotScopeCount), |
| | 20249 | 639 | | _publicationResnapshotAll, |
| | 20249 | 640 | | _blockedScopes.AsSpan(0, _publicationBlockedScopeCount), |
| | 20249 | 641 | | _publicationBlockAll, |
| | 20249 | 642 | | changes, |
| | 20249 | 643 | | changeCount, |
| | 20249 | 644 | | _baselineCaptures, |
| | 20249 | 645 | | _affectedMapOrdinals, |
| | 20249 | 646 | | _snapshotAffectedCount, |
| | 20249 | 647 | | graphVersion); |
| | 20249 | 648 | | bool hasAutomaticSeamLifecycle = startAutomaticSeamLifecycle |
| | 20249 | 649 | | && HasRelevantAutomaticSeamLifecycleEvent(graph, next); |
| | 20249 | 650 | | NavigationCandidatePublication? componentHold = |
| | 20249 | 651 | | !next.IsWithinDynamicSlotCapacity( |
| | 20249 | 652 | | _maxDynamicSlotsPerMap, |
| | 20249 | 653 | | _maxDynamicSlots) |
| | 20249 | 654 | | ? NavigationCandidatePublication.PermanentCapacity |
| | 20249 | 655 | | : TryPrepareMaterializedComponents( |
| | 20249 | 656 | | ref next, |
| | 20249 | 657 | | hasAutomaticSeamLifecycle); |
| | 20249 | 658 | | if (_automaticSeamFullRebuildPending && !startAutomaticSeamLifecycle) |
| | | 659 | | { |
| | 61 | 660 | | next = next.WithClosedStructuralComponents( |
| | 61 | 661 | | NavigationSurfaceComponentKeySet.Empty, |
| | 61 | 662 | | true, |
| | 61 | 663 | | graphVersion); |
| | | 664 | | } |
| | 20249 | 665 | | NavigationWorldGraph current = _store.Current; |
| | 20249 | 666 | | publication = componentHold.HasValue |
| | 20249 | 667 | | ? componentHold.Value |
| | 20249 | 668 | | : hasAutomaticSeamLifecycle |
| | 20249 | 669 | | ? StartAutomaticSeamLifecycle( |
| | 20249 | 670 | | current, |
| | 20249 | 671 | | next, |
| | 20249 | 672 | | graphVersion) |
| | 20249 | 673 | | : ReferenceEquals(current, next) |
| | 20249 | 674 | | ? NavigationCandidatePublication.Published |
| | 20249 | 675 | | : _store.TryPublish(next); |
| | 20249 | 676 | | if (!ReferenceEquals(current, next) |
| | 20249 | 677 | | && publication == NavigationCandidatePublication.Published) |
| | | 678 | | { |
| | 3787 | 679 | | _publishedThisMaintenance = true; |
| | | 680 | | } |
| | 20249 | 681 | | if (publication == NavigationCandidatePublication.Published |
| | 20249 | 682 | | && _automaticSeamFullRebuildPending |
| | 20249 | 683 | | && next.HasClosedStructuralScope) |
| | | 684 | | { |
| | 50 | 685 | | _store.ClearSafetyPending(); |
| | | 686 | | } |
| | 20249 | 687 | | } |
| | | 688 | | finally |
| | | 689 | | { |
| | 20249 | 690 | | if (_materializedComponentWork == null) |
| | 20006 | 691 | | ClearBaselineCaptures(); |
| | 20249 | 692 | | } |
| | 20249 | 693 | | if (publication == NavigationCandidatePublication.Published) |
| | | 694 | | { |
| | 19981 | 695 | | RemoveCompletedBaselineRebuilds(); |
| | 19981 | 696 | | CommitBlockedScopes( |
| | 19981 | 697 | | _publicationBlockedScopeCount, |
| | 19981 | 698 | | _publicationBlockAll, |
| | 19981 | 699 | | _snapshotDeferredScopeCount, |
| | 19981 | 700 | | _snapshotDeferAll); |
| | 19981 | 701 | | ClearSafetyPendingAfterExactResnapshot(); |
| | | 702 | | } |
| | | 703 | | else |
| | | 704 | | { |
| | 268 | 705 | | bool hasMaterializedWork = _materializedComponentWork != null; |
| | 268 | 706 | | bool materializedWorkOwnsAllClosedSource = hasMaterializedWork |
| | 268 | 707 | | && _store.Current.AreAllStructuralComponentsClosed; |
| | 268 | 708 | | if (_automaticSeamFullRebuildPending |
| | 268 | 709 | | && !materializedWorkOwnsAllClosedSource) |
| | | 710 | | { |
| | 1 | 711 | | _store.MarkSafetyPending(); |
| | | 712 | | } |
| | 268 | 713 | | if (!hasMaterializedWork) |
| | 25 | 714 | | MarkResnapshotRequired(); |
| | | 715 | | } |
| | 20249 | 716 | | return publication; |
| | | 717 | | } |
| | | 718 | | |
| | | 719 | | private NavigationCandidatePublication? TryPrepareMaterializedComponents( |
| | | 720 | | ref NavigationWorldGraph candidate, |
| | | 721 | | bool startsAutomaticSeamLifecycle) |
| | | 722 | | { |
| | 20236 | 723 | | if (_snapshotAffectedCount == 0 && _publicationEventCount == 0) |
| | 18392 | 724 | | return null; |
| | 1844 | 725 | | var work = new NavigationMaterializedComponentWork( |
| | 1844 | 726 | | candidate, |
| | 1844 | 727 | | NavigationSurfaceComponentKeySet.Empty, |
| | 1844 | 728 | | NavigationSurfaceComponentKeySet.Empty, |
| | 1844 | 729 | | NavigationCellAddressSet.Empty, |
| | 1844 | 730 | | affectedMemberCount: 0, |
| | 1844 | 731 | | _world, |
| | 1844 | 732 | | _baselineCaptures, |
| | 1844 | 733 | | _affectedMapOrdinals, |
| | 1844 | 734 | | _snapshotAffectedCount, |
| | 1844 | 735 | | _maintenanceEvents, |
| | 1844 | 736 | | _publicationEventCount, |
| | 1844 | 737 | | _operations.Candidate); |
| | 1844 | 738 | | bool complete = work.Advance(_maintenanceMeter); |
| | 1844 | 739 | | if (!IsWithinRetainedWorkCapacity( |
| | 1844 | 740 | | GetCombinedMaterializedWorkBytes(work), |
| | 1844 | 741 | | GetCombinedMaterializedWorkPages(work))) |
| | 12 | 742 | | return NavigationCandidatePublication.PermanentCapacity; |
| | 1832 | 743 | | if (complete && work.RevalidateForPublication()) |
| | | 744 | | { |
| | 1589 | 745 | | candidate = work.Result; |
| | 1589 | 746 | | return null; |
| | | 747 | | } |
| | | 748 | | |
| | 243 | 749 | | NavigationWorldGraph current = _store.Current; |
| | 243 | 750 | | CaptureOwnedStructuralClosureBaseline(current); |
| | 243 | 751 | | NavigationWorldGraph closed = CreateOwnedStructuralClosure( |
| | 243 | 752 | | current, |
| | 243 | 753 | | NavigationSurfaceComponentKeySet.Empty, |
| | 243 | 754 | | closeAll: true, |
| | 243 | 755 | | current.GraphVersion + 1); |
| | | 756 | | System.Diagnostics.Debug.Assert(!ReferenceEquals(current, closed), |
| | | 757 | | "Materialized work closes a newly versioned maintenance candidate."); |
| | 243 | 758 | | NavigationCandidatePublication publication = _store.TryPublish(closed); |
| | 243 | 759 | | ApplyPublishedCandidate( |
| | 243 | 760 | | publication, |
| | 243 | 761 | | PublishedCandidateEffect.InstallMaterialized, |
| | 243 | 762 | | work, |
| | 243 | 763 | | startsAutomaticSeamLifecycle); |
| | 243 | 764 | | return ContinueAfterRetainedClosure(publication); |
| | | 765 | | } |
| | | 766 | | |
| | | 767 | | private NavigationCandidatePublication MaintainMaterializedComponentWork( |
| | | 768 | | bool publish = true, |
| | | 769 | | NavigationWorldGraph? structuralClosure = null) |
| | | 770 | | { |
| | 3723 | 771 | | NavigationMaterializedComponentWork work = _materializedComponentWork!; |
| | 3723 | 772 | | bool complete = work.Advance(_maintenanceMeter); |
| | 3723 | 773 | | bool requiresRestart = work.RequiresSnapshotRestart; |
| | 3723 | 774 | | bool revalidated = !requiresRestart |
| | 3723 | 775 | | && complete |
| | 3723 | 776 | | && work.RevalidateForPublication(); |
| | 3723 | 777 | | requiresRestart |= work.RequiresSnapshotRestart; |
| | 3723 | 778 | | bool withinCapacity = requiresRestart |
| | 3723 | 779 | | || IsWithinRetainedWorkCapacity( |
| | 3723 | 780 | | GetCombinedMaterializedWorkBytes(work), |
| | 3723 | 781 | | GetCombinedMaterializedWorkPages(work)); |
| | 3723 | 782 | | MaterializedAdvanceDisposition disposition = ClassifyMaterializedAdvance( |
| | 3723 | 783 | | requiresRestart, |
| | 3723 | 784 | | withinCapacity, |
| | 3723 | 785 | | complete, |
| | 3723 | 786 | | publish, |
| | 3723 | 787 | | out NavigationCandidatePublication? abandonmentPublication); |
| | 3723 | 788 | | if (abandonmentPublication.HasValue) |
| | 1 | 789 | | return RestartMaterializedSnapshot(abandonmentPublication.Value); |
| | 3722 | 790 | | if (disposition == MaterializedAdvanceDisposition.Defer) |
| | 3276 | 791 | | return NavigationCandidatePublication.Deferred; |
| | | 792 | | System.Diagnostics.Debug.Assert(revalidated, |
| | | 793 | | "Completed failed revalidation always requests a snapshot restart above."); |
| | 446 | 794 | | if (disposition == MaterializedAdvanceDisposition.Ready) |
| | 205 | 795 | | return NavigationCandidatePublication.Published; |
| | | 796 | | |
| | 241 | 797 | | NavigationWorldGraph current = _store.Current; |
| | | 798 | | System.Diagnostics.Debug.Assert(_ownedStructuralClosureBaseline != null, |
| | | 799 | | "Retained materialized work owns the structural-closure baseline captured at installation."); |
| | 241 | 800 | | NavigationWorldGraph completed = structuralClosure == null |
| | 241 | 801 | | ? work.Result.WithClosedStructuralComponents( |
| | 241 | 802 | | _ownedStructuralClosureBaseline!, |
| | 241 | 803 | | _ownedStructuralClosureBaselineAll, |
| | 241 | 804 | | current.GraphVersion + 1) |
| | 241 | 805 | | : work.Result.WithStructuralClosureFrom( |
| | 241 | 806 | | structuralClosure, |
| | 241 | 807 | | current.GraphVersion + 1); |
| | | 808 | | NavigationCandidatePublication publication; |
| | 241 | 809 | | if (_materializedStartsAutomaticSeamLifecycle) |
| | | 810 | | { |
| | 12 | 811 | | _publicationEventCount = work.RetainedEventCount; |
| | 12 | 812 | | publication = StartAutomaticSeamLifecycle( |
| | 12 | 813 | | current, |
| | 12 | 814 | | completed, |
| | 12 | 815 | | completed.GraphVersion); |
| | | 816 | | } |
| | | 817 | | else |
| | | 818 | | { |
| | 229 | 819 | | publication = _store.TryPublish(completed); |
| | | 820 | | } |
| | 241 | 821 | | ApplyPublishedCandidate( |
| | 241 | 822 | | publication, |
| | 241 | 823 | | structuralClosure == null |
| | 241 | 824 | | ? PublishedCandidateEffect.CompleteMaterialized |
| | 241 | 825 | | : PublishedCandidateEffect.CompleteMaterializedWithinStructuralPublication); |
| | 241 | 826 | | return publication; |
| | | 827 | | } |
| | | 828 | | |
| | | 829 | | private NavigationCandidatePublication RestartMaterializedSnapshot( |
| | | 830 | | NavigationCandidatePublication publication = NavigationCandidatePublication.Deferred) |
| | | 831 | | { |
| | 1 | 832 | | AbandonMaterializedSnapshot(); |
| | 1 | 833 | | return publication; |
| | | 834 | | } |
| | | 835 | | |
| | | 836 | | private void AbandonMaterializedSnapshot(bool required = true) |
| | | 837 | | { |
| | 177 | 838 | | if (!required) |
| | 176 | 839 | | return; |
| | 1 | 840 | | _publicationEventCount = _materializedComponentWork!.RetainedEventCount; |
| | 1 | 841 | | _materializedComponentWork = null; |
| | 1 | 842 | | _materializedComponentWorkCompletesOperation = false; |
| | 1 | 843 | | _materializedStartsAutomaticSeamLifecycle = false; |
| | 1 | 844 | | ClearBaselineCaptures(); |
| | 1 | 845 | | RemoveCompletedBaselineRebuilds(); |
| | 1 | 846 | | RequeuePublicationEvents(); |
| | 1 | 847 | | MarkResnapshotRequired(); |
| | 1 | 848 | | } |
| | | 849 | | |
| | | 850 | | private void AdvanceCoveredBaselineRebuilds(NavigationWorldGraph graph) |
| | | 851 | | { |
| | 20249 | 852 | | int remainingAddresses = _maintenanceMeter.RemainingBaselineAddresses; |
| | 20249 | 853 | | if (remainingAddresses == 0 || _baselineRebuilds.Count == 0) |
| | 20146 | 854 | | return; |
| | 103 | 855 | | NavigationBaselineRebuild.GetRetainedTotals( |
| | 103 | 856 | | _baselineRebuilds, |
| | 103 | 857 | | out long totalBytes, |
| | 103 | 858 | | out int totalPages); |
| | 420 | 859 | | for (int i = 0; i < _baselineRebuilds.Count && remainingAddresses > 0; i++) |
| | | 860 | | { |
| | 107 | 861 | | NavigationBaselineRebuild rebuild = _baselineRebuilds.GetValueAt(i); |
| | 107 | 862 | | if (!rebuild.RequiresCoveredDiscovery |
| | 107 | 863 | | || rebuild.IsComplete) |
| | | 864 | | { |
| | | 865 | | continue; |
| | | 866 | | } |
| | 51 | 867 | | bool mapFound = graph.TryGetMap( |
| | 51 | 868 | | rebuild.MapId, |
| | 51 | 869 | | out NavigationMapInstance instance); |
| | | 870 | | System.Diagnostics.Debug.Assert( |
| | | 871 | | mapFound && rebuild.Matches(instance), |
| | | 872 | | "An inspectable covered-baseline rebuild retains the exact live map generation that owns it."); |
| | 51 | 873 | | long beforeBytes = rebuild.RetainedBytes; |
| | 51 | 874 | | int beforePages = rebuild.PersistentPageCount; |
| | 51 | 875 | | long maximumBytes = Math.Max( |
| | 51 | 876 | | 0L, |
| | 51 | 877 | | _maxActiveSnapshotBytes - graph.RetainedBytes - (totalBytes - beforeBytes)); |
| | 51 | 878 | | int maximumPages = Math.Max( |
| | 51 | 879 | | 0, |
| | 51 | 880 | | _maxPersistentGraphPages - graph.PersistentPageCount - (totalPages - beforePages)); |
| | 51 | 881 | | int consumed = rebuild.Advance( |
| | 51 | 882 | | _world, |
| | 51 | 883 | | instance, |
| | 51 | 884 | | remainingAddresses, |
| | 51 | 885 | | maximumBytes, |
| | 51 | 886 | | maximumPages, |
| | 51 | 887 | | _baselineAddressScratch, |
| | 51 | 888 | | _baselineCoveredAddressScratch, |
| | 51 | 889 | | out _, |
| | 51 | 890 | | out _); |
| | 51 | 891 | | totalBytes = checked(totalBytes - beforeBytes + rebuild.RetainedBytes); |
| | 51 | 892 | | totalPages = checked(totalPages - beforePages + rebuild.PersistentPageCount); |
| | 51 | 893 | | _maintenanceMeter.TryConsumeBaselineAddresses(consumed); |
| | 51 | 894 | | remainingAddresses -= consumed; |
| | | 895 | | } |
| | 103 | 896 | | } |
| | | 897 | | |
| | | 898 | | private bool HasRelevantAutomaticSeamLifecycleEvent( |
| | | 899 | | NavigationWorldGraph source, |
| | | 900 | | NavigationWorldGraph prepared) |
| | | 901 | | { |
| | 3600 | 902 | | if (_automaticSeamFullRebuildPending) |
| | 12 | 903 | | return true; |
| | 8992 | 904 | | for (int i = 0; i < _publicationEventCount; i++) |
| | | 905 | | { |
| | 932 | 906 | | GridEventInfo eventInfo = _maintenanceEvents[i]; |
| | 932 | 907 | | if (eventInfo.ChangeKind == GridEventKind.WorldReset) |
| | | 908 | | { |
| | 1 | 909 | | if (source.MapCount != 0) |
| | 1 | 910 | | return true; |
| | | 911 | | continue; |
| | | 912 | | } |
| | 931 | 913 | | GridForge.Configuration.GridConfigurationKey key = |
| | 931 | 914 | | eventInfo.Configuration.ToGridKey(); |
| | 931 | 915 | | if (eventInfo.ChangeKind == GridEventKind.GridRemoved |
| | 931 | 916 | | && source.TryGetMapId(key, out _)) |
| | | 917 | | { |
| | 11 | 918 | | return true; |
| | | 919 | | } |
| | 920 | 920 | | if (eventInfo.ChangeKind == GridEventKind.GridAdded |
| | 920 | 921 | | && prepared.TryGetMapId(key, out _)) |
| | | 922 | | { |
| | 12 | 923 | | return true; |
| | | 924 | | } |
| | | 925 | | } |
| | 3564 | 926 | | return false; |
| | | 927 | | } |
| | | 928 | | |
| | | 929 | | private NavigationCandidatePublication StartAutomaticSeamLifecycle( |
| | | 930 | | NavigationWorldGraph current, |
| | | 931 | | NavigationWorldGraph prepared, |
| | | 932 | | long graphVersion) |
| | | 933 | | { |
| | 36 | 934 | | NavigationWorldGraph closed = prepared.WithClosedStructuralComponents( |
| | 36 | 935 | | NavigationSurfaceComponentKeySet.Empty, |
| | 36 | 936 | | true, |
| | 36 | 937 | | graphVersion); |
| | | 938 | | System.Diagnostics.Debug.Assert(IsWithinRetainedWorkCapacity( |
| | | 939 | | checked( |
| | | 940 | | NavigationAutomaticSeamLifecycleWork.BaseRetainedBytes |
| | | 941 | | + NavigationAutomaticSeamRefreshWork.FixedRetainedBytes), |
| | | 942 | | 5, |
| | | 943 | | closed), |
| | | 944 | | "Successful materialized preparation strictly dominates the automatic-seam minimum retained-work envelope.") |
| | | 945 | | System.Diagnostics.Debug.Assert(!ReferenceEquals(current, closed), |
| | | 946 | | "Automatic seam lifecycle starts from a newly versioned closed candidate."); |
| | 36 | 947 | | NavigationCandidatePublication publication = _store.TryPublish(closed); |
| | | 948 | | System.Diagnostics.Debug.Assert( |
| | | 949 | | publication == NavigationCandidatePublication.Published, |
| | | 950 | | "Maintenance preflight and retained-work bounds guarantee automatic-seam closure publication."); |
| | 36 | 951 | | _store.SetSafetyPending(false); |
| | 36 | 952 | | ApplyPublishedCandidate( |
| | 36 | 953 | | publication, |
| | 36 | 954 | | PublishedCandidateEffect.StartAutomaticSeam, |
| | 36 | 955 | | automaticSeamGraph: closed); |
| | 36 | 956 | | return publication; |
| | | 957 | | } |
| | | 958 | | |
| | | 959 | | private void MaintainAutomaticSeamLifecycle() |
| | | 960 | | { |
| | 218 | 961 | | NavigationAutomaticSeamLifecycleWork work = _lifecycleWork!; |
| | 218 | 962 | | GetLifecycleWorkAllowance(out long bytes, out int pages); |
| | | 963 | | while (true) |
| | | 964 | | { |
| | 1322 | 965 | | NavigationAutomaticSeamLifecycleWork.AdvanceStatus status = work.AdvanceOne( |
| | 1322 | 966 | | _maintenanceMeter, |
| | 1322 | 967 | | bytes, |
| | 1322 | 968 | | pages); |
| | 1322 | 969 | | AutomaticSeamAdvanceDisposition disposition = |
| | 1322 | 970 | | ClassifyAutomaticSeamAdvance(status); |
| | 1322 | 971 | | if (disposition == AutomaticSeamAdvanceDisposition.Complete) |
| | | 972 | | { |
| | 33 | 973 | | bool revalidated = work.RevalidateForPublication(); |
| | 33 | 974 | | bool withinCapacity = IsWithinRetainedWorkCapacity(0, 0, work.Result); |
| | | 975 | | System.Diagnostics.Debug.Assert(revalidated, |
| | | 976 | | "A completed seam cursor is revalidated on the same deterministic maintenance boundary."); |
| | | 977 | | System.Diagnostics.Debug.Assert(withinCapacity, |
| | | 978 | | "Completed automatic-seam work remains inside its exact retained-work allowance."); |
| | 33 | 979 | | disposition = AutomaticSeamAdvanceDisposition.Complete; |
| | | 980 | | } |
| | | 981 | | switch (disposition) |
| | | 982 | | { |
| | | 983 | | case AutomaticSeamAdvanceDisposition.Continue: |
| | | 984 | | continue; |
| | | 985 | | case AutomaticSeamAdvanceDisposition.Retain: |
| | 183 | 986 | | return; |
| | | 987 | | case AutomaticSeamAdvanceDisposition.Restart: |
| | 2 | 988 | | RestartAutomaticSeamLifecycle(); |
| | 2 | 989 | | return; |
| | | 990 | | case AutomaticSeamAdvanceDisposition.Complete: |
| | 33 | 991 | | NavigationWorldGraph next = work.Result; |
| | 33 | 992 | | NavigationCandidatePublication publication = _store.TryPublish(next); |
| | 33 | 993 | | ApplyPublishedCandidate( |
| | 33 | 994 | | publication, |
| | 33 | 995 | | PublishedCandidateEffect.CompleteAutomaticSeam); |
| | 33 | 996 | | return; |
| | | 997 | | } |
| | | 998 | | } |
| | | 999 | | } |
| | | 1000 | | |
| | | 1001 | | private void GetLifecycleWorkAllowance(out long bytes, out int pages) |
| | | 1002 | | { |
| | | 1003 | | System.Diagnostics.Debug.Assert(_compositionWork == null, |
| | | 1004 | | "Automatic-seam lifecycle work starts only from ingress reconciliation without operation composition ownersh |
| | 218 | 1005 | | NavigationBaselineRebuild.GetRetainedTotals( |
| | 218 | 1006 | | _baselineRebuilds, |
| | 218 | 1007 | | out long rebuildBytes, |
| | 218 | 1008 | | out int rebuildPages); |
| | 218 | 1009 | | NavigationWorldGraph current = _store.Current; |
| | 218 | 1010 | | bytes = _maxActiveSnapshotBytes |
| | 218 | 1011 | | - current.RetainedBytes |
| | 218 | 1012 | | - rebuildBytes |
| | 218 | 1013 | | - _operations.RetainedOperationWorkBytes; |
| | 218 | 1014 | | pages = _maxPersistentGraphPages |
| | 218 | 1015 | | - current.PersistentPageCount |
| | 218 | 1016 | | - rebuildPages |
| | 218 | 1017 | | - _operations.RetainedOperationWorkPageCount; |
| | 218 | 1018 | | } |
| | | 1019 | | |
| | | 1020 | | private void RequeueAutomaticSeamLifecyclePrefix() |
| | | 1021 | | { |
| | 2 | 1022 | | _ingress.RequeuePrefix(_maintenanceEvents.AsSpan(0, _lifecycleEventCount)); |
| | 2 | 1023 | | _lifecycleEventCount = 0; |
| | 2 | 1024 | | } |
| | | 1025 | | |
| | | 1026 | | private void RestartAutomaticSeamLifecycle() |
| | | 1027 | | { |
| | 2 | 1028 | | RequeueAutomaticSeamLifecyclePrefix(); |
| | 2 | 1029 | | _lifecycleWork = null; |
| | 2 | 1030 | | } |
| | | 1031 | | |
| | | 1032 | | internal bool TryGetCellState( |
| | | 1033 | | string mapId, |
| | | 1034 | | VoxelIndex index, |
| | | 1035 | | out NavigationGraphCellState state) |
| | | 1036 | | { |
| | 551 | 1037 | | EnsureUsable(); |
| | 551 | 1038 | | NavigationWorldGraph graph = _store.Current; |
| | 551 | 1039 | | if (!graph.TryGetMap(mapId, out NavigationMapInstance instance) |
| | 551 | 1040 | | || !instance.TryGetSlot(index, out int slot)) |
| | | 1041 | | { |
| | 192 | 1042 | | state = default; |
| | 192 | 1043 | | return false; |
| | | 1044 | | } |
| | | 1045 | | |
| | 359 | 1046 | | bool hasCell = instance.TryGetEffectiveCell(slot, out NavigationCell cell); |
| | 359 | 1047 | | instance.TryGetPhysicalState(slot, out bool isPresent, out byte obstacleCount); |
| | 359 | 1048 | | state = new NavigationGraphCellState( |
| | 359 | 1049 | | mapId, |
| | 359 | 1050 | | index, |
| | 359 | 1051 | | slot, |
| | 359 | 1052 | | slot >= instance.BakedSlotCount, |
| | 359 | 1053 | | hasCell, |
| | 359 | 1054 | | cell, |
| | 359 | 1055 | | instance.IsMaterialized |
| | 359 | 1056 | | && !graph.IsSurfaceAddressClosed( |
| | 359 | 1057 | | new NavigationCellAddress(mapId, index), |
| | 359 | 1058 | | TraversalMedium.Solid), |
| | 359 | 1059 | | isPresent, |
| | 359 | 1060 | | obstacleCount, |
| | 359 | 1061 | | instance.GridIdentity.GridSpawnToken); |
| | 359 | 1062 | | return true; |
| | | 1063 | | } |
| | | 1064 | | |
| | | 1065 | | internal bool TryResolveAreaPolicy( |
| | | 1066 | | NavigationAreaPolicyKey key, |
| | | 1067 | | out NavigationAreaPolicy? policy) |
| | | 1068 | | { |
| | 7 | 1069 | | EnsureUsable(); |
| | 7 | 1070 | | return _store.Current.AreaCatalog.TryGet(key, out policy); |
| | | 1071 | | } |
| | | 1072 | | |
| | | 1073 | | internal NavigationGraphDiagnosticsSnapshot GetDiagnostics(int maximumCells) |
| | | 1074 | | { |
| | 2200 | 1075 | | EnsureUsable(); |
| | 2200 | 1076 | | NavigationWorldGraph graph = _store.Current; |
| | 2200 | 1077 | | NavigationBaselineRebuild.GetRetainedTotals( |
| | 2200 | 1078 | | _baselineRebuilds, |
| | 2200 | 1079 | | out long baselineRebuildBytes, |
| | 2200 | 1080 | | out int baselineRebuildPages); |
| | 2200 | 1081 | | int baselineCapacityBlockedCount = |
| | 2200 | 1082 | | NavigationBaselineRebuild.CountCapacityBlocked(_baselineRebuilds); |
| | 2200 | 1083 | | long compositionWorkBytes = RetainedCompositionWorkBytes; |
| | 2200 | 1084 | | int compositionWorkPages = RetainedCompositionWorkPageCount; |
| | 2200 | 1085 | | long operationWorkBytes = _operations.RetainedOperationWorkBytes; |
| | 2200 | 1086 | | int operationWorkPages = _operations.RetainedOperationWorkPageCount; |
| | 2200 | 1087 | | var maps = new NavigationGraphMapDiagnostic[graph.MapCount]; |
| | 2200 | 1088 | | int remaining = maximumCells; |
| | 2200 | 1089 | | bool truncated = false; |
| | 33670 | 1090 | | for (int i = 0; i < graph.MapCount; i++) |
| | | 1091 | | { |
| | 14635 | 1092 | | NavigationMapInstance instance = graph.GetInstance(i); |
| | 14635 | 1093 | | GetSurfaceComponentDiagnostic( |
| | 14635 | 1094 | | graph, |
| | 14635 | 1095 | | instance, |
| | 14635 | 1096 | | out int componentId, |
| | 14635 | 1097 | | out long componentVersion); |
| | 14635 | 1098 | | maps[i] = instance.CreateDiagnostic( |
| | 14635 | 1099 | | remaining, |
| | 14635 | 1100 | | componentId, |
| | 14635 | 1101 | | componentVersion, |
| | 14635 | 1102 | | graph.ExplicitConnections.GetActiveIncidentEdgeCount( |
| | 14635 | 1103 | | instance.MapId), |
| | 14635 | 1104 | | out bool mapTruncated); |
| | 14635 | 1105 | | remaining -= maps[i].Cells.Count; |
| | 14635 | 1106 | | truncated |= mapTruncated; |
| | | 1107 | | } |
| | 2200 | 1108 | | return new NavigationGraphDiagnosticsSnapshot( |
| | 2200 | 1109 | | graph.GraphVersion, |
| | 2200 | 1110 | | graph.AreaCatalog.Version, |
| | 2200 | 1111 | | checked( |
| | 2200 | 1112 | | graph.RetainedBytes |
| | 2200 | 1113 | | + baselineRebuildBytes |
| | 2200 | 1114 | | + compositionWorkBytes |
| | 2200 | 1115 | | + operationWorkBytes |
| | 2200 | 1116 | | + _areaPolicies.PendingRetainedBytes), |
| | 2200 | 1117 | | _store.ActiveGenerationCount, |
| | 2200 | 1118 | | _store.ActiveLeaseCount, |
| | 2200 | 1119 | | checked( |
| | 2200 | 1120 | | graph.PersistentPageCount |
| | 2200 | 1121 | | + baselineRebuildPages |
| | 2200 | 1122 | | + compositionWorkPages |
| | 2200 | 1123 | | + operationWorkPages |
| | 2200 | 1124 | | + _areaPolicies.PendingCount), |
| | 2200 | 1125 | | _store.RetiredGenerationCount, |
| | 2200 | 1126 | | _store.RetiredBytes, |
| | 2200 | 1127 | | _areaPolicies.PendingCount, |
| | 2200 | 1128 | | _areaPolicies.PendingRuleCount, |
| | 2200 | 1129 | | _areaPolicies.PendingRetainedBytes, |
| | 2200 | 1130 | | _baselineRebuilds.Count, |
| | 2200 | 1131 | | baselineCapacityBlockedCount, |
| | 2200 | 1132 | | baselineRebuildBytes, |
| | 2200 | 1133 | | baselineRebuildPages, |
| | 2200 | 1134 | | truncated, |
| | 2200 | 1135 | | maps); |
| | | 1136 | | } |
| | | 1137 | | |
| | | 1138 | | private static void GetSurfaceComponentDiagnostic( |
| | | 1139 | | NavigationWorldGraph graph, |
| | | 1140 | | NavigationMapInstance instance, |
| | | 1141 | | out int componentId, |
| | | 1142 | | out long componentVersion) |
| | | 1143 | | { |
| | 14635 | 1144 | | componentId = 0; |
| | 14635 | 1145 | | componentVersion = 0; |
| | 14635 | 1146 | | NavigationSurfaceComponentKey selected = default; |
| | 14635 | 1147 | | bool found = false; |
| | 14635 | 1148 | | int bakedCursor = 0; |
| | 14635 | 1149 | | int dynamicCursor = 0; |
| | 14635 | 1150 | | Span<GridForge.Spatial.VoxelIndex> address = |
| | 14635 | 1151 | | stackalloc GridForge.Spatial.VoxelIndex[1]; |
| | 4320242 | 1152 | | for (int ordinal = 0; ordinal < instance.AddressCount; ordinal++) |
| | | 1153 | | { |
| | 2145493 | 1154 | | instance.CopyCanonicalAddressChunk( |
| | 2145493 | 1155 | | ref bakedCursor, |
| | 2145493 | 1156 | | ref dynamicCursor, |
| | 2145493 | 1157 | | address); |
| | 2145493 | 1158 | | var exact = new NavigationCellAddress(instance.MapId, address[0]); |
| | 2145493 | 1159 | | if (!graph.HasEffectiveCell(exact) |
| | 2145493 | 1160 | | || !graph.TryGetSurfaceComponent( |
| | 2145493 | 1161 | | exact, |
| | 2145493 | 1162 | | TraversalMedium.Solid, |
| | 2145493 | 1163 | | out NavigationSurfaceComponentKey key, |
| | 2145493 | 1164 | | out long version)) |
| | | 1165 | | { |
| | | 1166 | | continue; |
| | | 1167 | | } |
| | 2145490 | 1168 | | if (!found) |
| | | 1169 | | { |
| | 14634 | 1170 | | selected = key; |
| | 14634 | 1171 | | componentId = key.GetHashCode(); |
| | 14634 | 1172 | | componentVersion = version; |
| | 14634 | 1173 | | found = true; |
| | | 1174 | | } |
| | 2130856 | 1175 | | else if (!key.Equals(selected)) |
| | | 1176 | | { |
| | 7 | 1177 | | componentId = -1; |
| | 7 | 1178 | | componentVersion = 0; |
| | 7 | 1179 | | return; |
| | | 1180 | | } |
| | | 1181 | | } |
| | 14628 | 1182 | | } |
| | | 1183 | | |
| | | 1184 | | internal void Reset() |
| | | 1185 | | { |
| | 23 | 1186 | | EnsureUsable(); |
| | 23 | 1187 | | _operations.Reset(); |
| | 23 | 1188 | | _areaPolicies.Reset(); |
| | 23 | 1189 | | _ingress.Reset(); |
| | 23 | 1190 | | _priorBlockedScopeCount = 0; |
| | 23 | 1191 | | _priorBlockAll = false; |
| | 23 | 1192 | | _baselineRebuilds = PersistentStringMap<NavigationBaselineRebuild>.Empty; |
| | 23 | 1193 | | _compositionWork = null; |
| | 23 | 1194 | | _materializedComponentWork = null; |
| | 23 | 1195 | | _materializedComponentWorkCompletesOperation = false; |
| | 23 | 1196 | | _materializedStartsAutomaticSeamLifecycle = false; |
| | 23 | 1197 | | _lifecycleWork = null; |
| | 23 | 1198 | | _lifecycleEventCount = 0; |
| | 23 | 1199 | | _automaticSeamFullRebuildPending = false; |
| | 23 | 1200 | | _operationClosureRollbackPending = false; |
| | 23 | 1201 | | ClearOwnedStructuralClosureBaseline(); |
| | 23 | 1202 | | Array.Clear(_baselineCaptures, 0, _baselineCaptures.Length); |
| | 23 | 1203 | | _resetPending = _store.TryPublish( |
| | 23 | 1204 | | NavigationWorldGraph.CreateEmpty(_store.Current.GraphVersion + 1)) |
| | 23 | 1205 | | != NavigationCandidatePublication.Published; |
| | 23 | 1206 | | MarkSafetyPendingIf(_resetPending); |
| | 23 | 1207 | | if (!_resetPending) |
| | 22 | 1208 | | _store.ClearSafetyPending(); |
| | 23 | 1209 | | } |
| | | 1210 | | |
| | | 1211 | | public void Dispose() |
| | | 1212 | | { |
| | 1197 | 1213 | | if (_disposed) |
| | 1 | 1214 | | return; |
| | 1196 | 1215 | | _disposed = true; |
| | 1196 | 1216 | | _baselineRebuilds = PersistentStringMap<NavigationBaselineRebuild>.Empty; |
| | 1196 | 1217 | | _compositionWork = null; |
| | 1196 | 1218 | | _materializedComponentWork = null; |
| | 1196 | 1219 | | _materializedComponentWorkCompletesOperation = false; |
| | 1196 | 1220 | | _materializedStartsAutomaticSeamLifecycle = false; |
| | 1196 | 1221 | | _lifecycleWork = null; |
| | 1196 | 1222 | | _lifecycleEventCount = 0; |
| | 1196 | 1223 | | _automaticSeamFullRebuildPending = false; |
| | 1196 | 1224 | | Array.Clear(_baselineCaptures, 0, _baselineCaptures.Length); |
| | 1196 | 1225 | | _ingress.Dispose(); |
| | 1196 | 1226 | | _store.Dispose(); |
| | 1196 | 1227 | | } |
| | | 1228 | | |
| | | 1229 | | private NavigationCandidatePublication PublishCandidate( |
| | | 1230 | | NavigationOperationCandidate candidate, |
| | | 1231 | | NavigationAreaCatalog areaCatalog, |
| | | 1232 | | NavigationOperationFrameChange[] changes, |
| | | 1233 | | int changeCount) |
| | | 1234 | | { |
| | 11967 | 1235 | | if (_compositionWork != null) |
| | | 1236 | | { |
| | 10854 | 1237 | | if (_compositionWork.RequiresAllClosePublication) return PublishAllCompositionClosureAndDefer(_compositionWo |
| | 10852 | 1238 | | if (_compositionWork.RequiresAffectedClosurePublication) |
| | | 1239 | | { |
| | 169 | 1240 | | if (!_compositionWork.RevalidateAutomaticSeamsForPublication()) return NavigationCandidatePublication.De |
| | 167 | 1241 | | return PublishAffectedCompositionClosureAndDefer(_compositionWork); |
| | | 1242 | | } |
| | 10684 | 1243 | | GetCompositionWorkAllowance(out long resumedBytes, out int resumedPages); |
| | 10684 | 1244 | | if (!_compositionWork.Advance( |
| | 10684 | 1245 | | _maintenanceMeter, |
| | 10684 | 1246 | | resumedBytes, |
| | 10684 | 1247 | | resumedPages, |
| | 10684 | 1248 | | out bool resumedCapacityExceeded)) |
| | | 1249 | | { |
| | | 1250 | | System.Diagnostics.Debug.Assert( |
| | | 1251 | | !_compositionWork.RequiresAllClosePublication, |
| | | 1252 | | "Affected closure publication follows completed seam capture; incomplete advancement cannot invalida |
| | 10370 | 1253 | | if (resumedCapacityExceeded) return RollbackCompositionForCapacityRejection(); |
| | | 1254 | | System.Diagnostics.Debug.Assert(IsWithinRetainedWorkCapacity( |
| | | 1255 | | GetCombinedCompositionWorkBytes(_compositionWork, _store.Current), |
| | | 1256 | | GetCombinedCompositionWorkPages(_compositionWork, _store.Current)), |
| | | 1257 | | "A non-capacity blocked advance remains inside its exact unchanged-root allowance."); |
| | 10368 | 1258 | | return NavigationCandidatePublication.Deferred; |
| | | 1259 | | } |
| | 315 | 1260 | | if (!_compositionWork.RevalidateAutomaticSeamsForPublication()) |
| | | 1261 | | { |
| | | 1262 | | System.Diagnostics.Debug.Assert( |
| | | 1263 | | _compositionWork.RequiresAllClosePublication, |
| | | 1264 | | "Failed completed-work revalidation invalidates an affected-closure owner and requires all-close rep |
| | 1 | 1265 | | return PublishAllCompositionClosureAndDefer(_compositionWork); |
| | | 1266 | | } |
| | | 1267 | | System.Diagnostics.Debug.Assert(IsWithinRetainedWorkCapacity( |
| | | 1268 | | GetCombinedCompositionWorkBytes(_compositionWork, _store.Current), |
| | | 1269 | | GetCombinedCompositionWorkPages(_compositionWork, _store.Current)), |
| | | 1270 | | "Completed revalidated work has not grown since its exact advance allowance check."); |
| | 314 | 1271 | | long completedVersion = checked(_store.Current.GraphVersion + 1); |
| | 314 | 1272 | | NavigationWorldGraph completed = _compositionWork.Result.WithAreaCatalog( |
| | 314 | 1273 | | areaCatalog, |
| | 314 | 1274 | | completedVersion).WithGraphVersion(completedVersion); |
| | 314 | 1275 | | completed = PreservePriorStructuralClosures(completed); |
| | 316 | 1276 | | if (!completed.IsWithinDynamicSlotCapacity(_maxDynamicSlotsPerMap, _maxDynamicSlots)) return RollbackComposi |
| | 312 | 1277 | | NavigationCandidatePublication completedPublication = ReconcileAndPublish( |
| | 312 | 1278 | | completed, |
| | 312 | 1279 | | changes, |
| | 312 | 1280 | | changeCount, |
| | 312 | 1281 | | completedVersion); |
| | 314 | 1282 | | if (completedPublication == NavigationCandidatePublication.PermanentCapacity) return RollbackCompositionForC |
| | 310 | 1283 | | ApplyPublishedCandidate( |
| | 310 | 1284 | | completedPublication, |
| | 310 | 1285 | | PublishedCandidateEffect.CompleteComposition); |
| | 310 | 1286 | | return completedPublication; |
| | | 1287 | | } |
| | | 1288 | | |
| | 1114 | 1289 | | NavigationWorldGraph current = _store.Current; |
| | 1114 | 1290 | | long nextVersion = current.GraphVersion + 1; |
| | 1114 | 1291 | | NavigationWorldGraph? completedStructural = null; |
| | 1114 | 1292 | | if (changeCount > 0) |
| | | 1293 | | { |
| | 1110 | 1294 | | long minimumScratchBytes = NavigationStructuralCompositionWork.GetMinimumScratchBytes( |
| | 1110 | 1295 | | current.MapCount, |
| | 1110 | 1296 | | candidate.MapCount, |
| | 1110 | 1297 | | changeCount, |
| | 1110 | 1298 | | candidate.OverlayCellCount); |
| | 1110 | 1299 | | int minimumScratchPages = NavigationStructuralCompositionWork.GetMinimumScratchPages( |
| | 1110 | 1300 | | candidate.MapCount, |
| | 1110 | 1301 | | candidate.OverlayCellCount); |
| | 1110 | 1302 | | long candidateGrowthBytes = Math.Max( |
| | 1110 | 1303 | | 0L, |
| | 1110 | 1304 | | candidate.RetainedBytes - current.RetainedBytes); |
| | 1110 | 1305 | | int candidateGrowthPages = Math.Max( |
| | 1110 | 1306 | | 0, |
| | 1110 | 1307 | | candidate.PersistentPageCount - current.PersistentPageCount); |
| | 1110 | 1308 | | if (!IsWithinRetainedWorkCapacity( |
| | 1110 | 1309 | | checked(candidateGrowthBytes + minimumScratchBytes), |
| | 1111 | 1310 | | checked(candidateGrowthPages + minimumScratchPages))) return RejectInitialCompositionForCapacity(); |
| | 1109 | 1311 | | var work = new NavigationStructuralCompositionWork( |
| | 1109 | 1312 | | _world, |
| | 1109 | 1313 | | current, |
| | 1109 | 1314 | | candidate, |
| | 1109 | 1315 | | changes, |
| | 1109 | 1316 | | changeCount); |
| | 1109 | 1317 | | GetCompositionWorkAllowance(out long initialBytes, out int initialPages); |
| | 1109 | 1318 | | if (!work.Advance( |
| | 1109 | 1319 | | _maintenanceMeter, |
| | 1109 | 1320 | | initialBytes, |
| | 1109 | 1321 | | initialPages, |
| | 1109 | 1322 | | out _)) |
| | | 1323 | | { |
| | 344 | 1324 | | NavigationSurfaceComponentKeySet closureBaseline = |
| | 344 | 1325 | | _ownedStructuralClosureBaseline ?? current.ClosedStructuralComponents; |
| | 344 | 1326 | | bool closureBaselineAll = _ownedStructuralClosureBaseline == null |
| | 344 | 1327 | | ? current.AreAllStructuralComponentsClosed |
| | 344 | 1328 | | : _ownedStructuralClosureBaselineAll; |
| | 344 | 1329 | | NavigationWorldGraph closed = current.WithOwnedStructuralClosure( |
| | 344 | 1330 | | closureBaseline, |
| | 344 | 1331 | | work.AffectedComponents, |
| | 344 | 1332 | | !work.IsChangedMapCaptureComplete || closureBaselineAll, |
| | 344 | 1333 | | current.GraphVersion + 1); |
| | 344 | 1334 | | if (!IsWithinRetainedWorkCapacity( |
| | 344 | 1335 | | GetCombinedCompositionWorkBytes(work, closed), |
| | 344 | 1336 | | GetCombinedCompositionWorkPages(work, closed), |
| | 512 | 1337 | | closed)) return RejectInitialCompositionForCapacity(); |
| | 176 | 1338 | | CaptureOwnedStructuralClosureBaseline(current); |
| | | 1339 | | System.Diagnostics.Debug.Assert(!ReferenceEquals(closed, current), |
| | | 1340 | | "An operation closure always advances the graph version."); |
| | 176 | 1341 | | NavigationCandidatePublication closedPublication = ReconcileAndPublish( |
| | 176 | 1342 | | closed, |
| | 176 | 1343 | | Array.Empty<NavigationOperationFrameChange>(), |
| | 176 | 1344 | | 0, |
| | 176 | 1345 | | closed.GraphVersion); |
| | 177 | 1346 | | if (closedPublication != NavigationCandidatePublication.Published) return closedPublication; |
| | 175 | 1347 | | work.MarkInitialClosurePublished(); |
| | 175 | 1348 | | _compositionWork = work; |
| | 175 | 1349 | | return NavigationCandidatePublication.Deferred; |
| | | 1350 | | } |
| | 765 | 1351 | | completedStructural = PreservePriorStructuralClosures(work.Result); |
| | | 1352 | | } |
| | 769 | 1353 | | NavigationWorldGraph next = changeCount == 0 |
| | 769 | 1354 | | ? current.ReopenStructuralScopes(nextVersion) |
| | 769 | 1355 | | : completedStructural!; |
| | 770 | 1356 | | if (!next.IsWithinDynamicSlotCapacity(_maxDynamicSlotsPerMap, _maxDynamicSlots)) return RejectInitialComposition |
| | 768 | 1357 | | NavigationCandidatePublication publication = ReconcileAndPublish( |
| | 768 | 1358 | | next.WithAreaCatalog(areaCatalog, nextVersion), |
| | 768 | 1359 | | changes, |
| | 768 | 1360 | | changeCount, |
| | 768 | 1361 | | nextVersion); |
| | 768 | 1362 | | ApplyPublishedCandidate( |
| | 768 | 1363 | | publication, |
| | 768 | 1364 | | PublishedCandidateEffect.CompleteComposition); |
| | 768 | 1365 | | return publication; |
| | | 1366 | | } |
| | | 1367 | | |
| | | 1368 | | private bool IsWithinRetainedWorkCapacity( |
| | | 1369 | | long workBytes, |
| | | 1370 | | int workPages, |
| | | 1371 | | NavigationWorldGraph? graph = null) |
| | | 1372 | | { |
| | 39996 | 1373 | | NavigationBaselineRebuild.GetRetainedTotals( |
| | 39996 | 1374 | | _baselineRebuilds, |
| | 39996 | 1375 | | out long rebuildBytes, |
| | 39996 | 1376 | | out int rebuildPages); |
| | 39996 | 1377 | | NavigationWorldGraph root = graph ?? _store.Current; |
| | | 1378 | | // Conservative sum: persistent roots share pages, but double-counting is safer than |
| | | 1379 | | // allowing aggregate active state to escape its configured ceiling. |
| | 39996 | 1380 | | return IsRetainedWorkWithinCapacity( |
| | 39996 | 1381 | | root.RetainedBytes, |
| | 39996 | 1382 | | root.PersistentPageCount, |
| | 39996 | 1383 | | workBytes, |
| | 39996 | 1384 | | workPages, |
| | 39996 | 1385 | | rebuildBytes, |
| | 39996 | 1386 | | rebuildPages, |
| | 39996 | 1387 | | _maxActiveSnapshotBytes, |
| | 39996 | 1388 | | _maxPersistentGraphPages); |
| | | 1389 | | } |
| | | 1390 | | |
| | | 1391 | | private bool TryRollbackCompositionClosure() |
| | 5 | 1392 | | => _automaticSeamFullRebuildPending || TryPublishReopenedStructuralScopes(); |
| | | 1393 | | |
| | | 1394 | | private NavigationCandidatePublication RollbackCompositionForCapacityRejection() |
| | | 1395 | | { |
| | 5 | 1396 | | bool rollbackCompleted = TryRollbackCompositionClosure(); |
| | | 1397 | | System.Diagnostics.Debug.Assert(rollbackCompleted, |
| | | 1398 | | "A retained composition rolls back only after the maintenance writer preflight succeeds."); |
| | 5 | 1399 | | _compositionWork = null; |
| | 5 | 1400 | | return NavigationCandidatePublication.PermanentCapacity; |
| | | 1401 | | } |
| | | 1402 | | |
| | | 1403 | | private NavigationCandidatePublication RejectInitialCompositionForCapacity() |
| | | 1404 | | { |
| | | 1405 | | System.Diagnostics.Debug.Assert(_compositionWork == null, |
| | | 1406 | | "Initial composition capacity rejection cannot own retained composition work."); |
| | 170 | 1407 | | return NavigationCandidatePublication.PermanentCapacity; |
| | | 1408 | | } |
| | | 1409 | | |
| | | 1410 | | private void CompleteCompositionPublication() |
| | | 1411 | | { |
| | 931 | 1412 | | _compositionWork = null; |
| | 931 | 1413 | | ClearOwnedStructuralClosureBaseline(); |
| | 931 | 1414 | | } |
| | | 1415 | | |
| | | 1416 | | private NavigationCandidatePublication PublishAffectedCompositionClosureAndDefer( |
| | | 1417 | | NavigationStructuralCompositionWork work) |
| | | 1418 | | { |
| | 167 | 1419 | | NavigationWorldGraph current = _store.Current; |
| | 167 | 1420 | | CaptureOwnedStructuralClosureBaseline(current); |
| | 167 | 1421 | | NavigationWorldGraph narrowed = CreateOwnedStructuralClosure( |
| | 167 | 1422 | | current, |
| | 167 | 1423 | | work.AffectedComponents, |
| | 167 | 1424 | | false, |
| | 167 | 1425 | | current.GraphVersion + 1); |
| | | 1426 | | System.Diagnostics.Debug.Assert(!ReferenceEquals(current, narrowed), |
| | | 1427 | | "Narrowing an owned all-close publication changes its closure root."); |
| | 167 | 1428 | | NavigationCandidatePublication publication = ReconcileAndPublish( |
| | 167 | 1429 | | narrowed, |
| | 167 | 1430 | | Array.Empty<NavigationOperationFrameChange>(), |
| | 167 | 1431 | | 0, |
| | 167 | 1432 | | narrowed.GraphVersion); |
| | 167 | 1433 | | work.RecordAffectedClosurePublication(publication); |
| | 167 | 1434 | | return ContinueAfterRetainedClosure(publication); |
| | | 1435 | | } |
| | | 1436 | | |
| | | 1437 | | private NavigationCandidatePublication PublishAllCompositionClosureAndDefer( |
| | | 1438 | | NavigationStructuralCompositionWork work) |
| | | 1439 | | { |
| | 2 | 1440 | | NavigationWorldGraph current = _store.Current; |
| | 2 | 1441 | | CaptureOwnedStructuralClosureBaseline(current); |
| | 2 | 1442 | | NavigationWorldGraph closed = current.WithClosedStructuralComponents( |
| | 2 | 1443 | | NavigationSurfaceComponentKeySet.Empty, |
| | 2 | 1444 | | true, |
| | 2 | 1445 | | current.GraphVersion + 1); |
| | | 1446 | | System.Diagnostics.Debug.Assert(!ReferenceEquals(current, closed), |
| | | 1447 | | "A stale affected closure must broaden before seam work restarts."); |
| | 2 | 1448 | | NavigationCandidatePublication publication = ReconcileAndPublish( |
| | 2 | 1449 | | closed, |
| | 2 | 1450 | | Array.Empty<NavigationOperationFrameChange>(), |
| | 2 | 1451 | | 0, |
| | 2 | 1452 | | closed.GraphVersion); |
| | 2 | 1453 | | work.RecordAllClosePublication(publication); |
| | 2 | 1454 | | return ContinueAfterRetainedClosure(publication); |
| | | 1455 | | } |
| | | 1456 | | |
| | | 1457 | | private void BeginOperationClosureRollback() |
| | | 1458 | | { |
| | | 1459 | | System.Diagnostics.Debug.Assert(_materializedComponentWork == null); |
| | | 1460 | | System.Diagnostics.Debug.Assert(_lifecycleWork == null); |
| | | 1461 | | System.Diagnostics.Debug.Assert( |
| | | 1462 | | _automaticSeamFullRebuildPending |
| | | 1463 | | || _publishedThisMaintenance |
| | | 1464 | | || _ingress.HasPendingWork, |
| | | 1465 | | "Terminal work begins rollback only while this maintenance pass still owns publication or ingress state."); |
| | 5 | 1466 | | _operationClosureRollbackPending = true; |
| | 5 | 1467 | | } |
| | | 1468 | | |
| | | 1469 | | private void ApplyOperationTerminalRollback( |
| | | 1470 | | OperationTerminalDisposition disposition) |
| | | 1471 | | { |
| | | 1472 | | System.Diagnostics.Debug.Assert(disposition != OperationTerminalDisposition.None); |
| | 178 | 1473 | | if (disposition == OperationTerminalDisposition.ReleaseAndBeginRollback) |
| | | 1474 | | { |
| | 3 | 1475 | | BeginOperationClosureRollback(); |
| | 3 | 1476 | | return; |
| | | 1477 | | } |
| | 175 | 1478 | | if (disposition == OperationTerminalDisposition.ReleaseAndTryReopen) |
| | 2 | 1479 | | TryCompleteOperationClosureRollback(); |
| | 175 | 1480 | | } |
| | | 1481 | | |
| | | 1482 | | private void RejectDeferredOperationForCapacity() |
| | | 1483 | | { |
| | 2 | 1484 | | _operations.RejectDeferredCapacity(); |
| | 2 | 1485 | | _compositionWork = null; |
| | 2 | 1486 | | BeginOperationClosureRollback(); |
| | 2 | 1487 | | } |
| | | 1488 | | |
| | | 1489 | | private void TryCompleteOperationClosureRollback() |
| | | 1490 | | { |
| | 2 | 1491 | | _operationClosureRollbackPending = !TryPublishReopenedStructuralScopes(); |
| | 2 | 1492 | | } |
| | | 1493 | | |
| | | 1494 | | private bool TryPublishReopenedStructuralScopes() |
| | | 1495 | | { |
| | | 1496 | | System.Diagnostics.Debug.Assert(!_automaticSeamFullRebuildPending); |
| | | 1497 | | System.Diagnostics.Debug.Assert(!_publishedThisMaintenance); |
| | | 1498 | | System.Diagnostics.Debug.Assert( |
| | | 1499 | | _ownedStructuralClosureBaseline != null, |
| | | 1500 | | "Only an operation-owned structural closure enters rollback."); |
| | 19 | 1501 | | NavigationWorldGraph current = _store.Current; |
| | 19 | 1502 | | NavigationWorldGraph reopened = current.WithClosedStructuralComponents( |
| | 19 | 1503 | | _ownedStructuralClosureBaseline!, |
| | 19 | 1504 | | _ownedStructuralClosureBaselineAll, |
| | 19 | 1505 | | current.GraphVersion + 1); |
| | | 1506 | | System.Diagnostics.Debug.Assert(!ReferenceEquals(current, reopened), |
| | | 1507 | | "An owned closure differs from the baseline it must restore."); |
| | 19 | 1508 | | NavigationCandidatePublication publication = _store.TryPublish(reopened); |
| | 19 | 1509 | | ApplyPublishedCandidate( |
| | 19 | 1510 | | publication, |
| | 19 | 1511 | | PublishedCandidateEffect.ReopenStructuralScopes); |
| | 19 | 1512 | | bool published = publication == NavigationCandidatePublication.Published; |
| | 19 | 1513 | | return published; |
| | | 1514 | | } |
| | | 1515 | | |
| | | 1516 | | private void ApplyPublishedCandidate( |
| | | 1517 | | NavigationCandidatePublication publication, |
| | | 1518 | | PublishedCandidateEffect effect, |
| | | 1519 | | NavigationMaterializedComponentWork? materializedWork = null, |
| | | 1520 | | bool startsAutomaticSeamLifecycle = false, |
| | | 1521 | | NavigationWorldGraph? automaticSeamGraph = null) |
| | | 1522 | | { |
| | 1650 | 1523 | | if (publication != NavigationCandidatePublication.Published) |
| | 147 | 1524 | | return; |
| | 1503 | 1525 | | _publishedThisMaintenance = true; |
| | 1503 | 1526 | | if (effect == PublishedCandidateEffect.InstallMaterialized) |
| | | 1527 | | { |
| | | 1528 | | System.Diagnostics.Debug.Assert(materializedWork != null); |
| | 243 | 1529 | | _materializedComponentWork = materializedWork; |
| | 243 | 1530 | | _materializedComponentWorkCompletesOperation = |
| | 243 | 1531 | | _operations.RetainedOperationWorkCount != 0; |
| | 243 | 1532 | | _materializedStartsAutomaticSeamLifecycle = startsAutomaticSeamLifecycle; |
| | 243 | 1533 | | return; |
| | | 1534 | | } |
| | 1260 | 1535 | | if (effect == PublishedCandidateEffect.CompleteMaterialized |
| | 1260 | 1536 | | || effect == PublishedCandidateEffect.CompleteMaterializedWithinStructuralPublication) |
| | | 1537 | | { |
| | 241 | 1538 | | _materializedComponentWork = null; |
| | 241 | 1539 | | _materializedComponentWorkCompletesOperation = false; |
| | 241 | 1540 | | _materializedStartsAutomaticSeamLifecycle = false; |
| | 241 | 1541 | | ClearBaselineCaptures(); |
| | 241 | 1542 | | RemoveCompletedBaselineRebuilds(); |
| | 241 | 1543 | | CommitBlockedScopes( |
| | 241 | 1544 | | _publicationBlockedScopeCount, |
| | 241 | 1545 | | _publicationBlockAll, |
| | 241 | 1546 | | _snapshotDeferredScopeCount, |
| | 241 | 1547 | | _snapshotDeferAll); |
| | 241 | 1548 | | ClearSafetyPendingAfterExactResnapshot(); |
| | 241 | 1549 | | if (effect == PublishedCandidateEffect.CompleteMaterialized) |
| | 36 | 1550 | | ClearOwnedStructuralClosureBaseline(); |
| | 241 | 1551 | | return; |
| | | 1552 | | } |
| | 1019 | 1553 | | if (effect == PublishedCandidateEffect.CompleteAutomaticSeam) |
| | | 1554 | | { |
| | 33 | 1555 | | _lifecycleWork = null; |
| | 33 | 1556 | | _lifecycleEventCount = 0; |
| | 33 | 1557 | | _automaticSeamFullRebuildPending = false; |
| | 33 | 1558 | | _store.ClearSafetyPending(); |
| | 33 | 1559 | | return; |
| | | 1560 | | } |
| | 986 | 1561 | | if (effect == PublishedCandidateEffect.CompleteComposition) |
| | | 1562 | | { |
| | 931 | 1563 | | CompleteCompositionPublication(); |
| | 931 | 1564 | | return; |
| | | 1565 | | } |
| | 55 | 1566 | | if (effect == PublishedCandidateEffect.ReopenStructuralScopes) |
| | | 1567 | | { |
| | 19 | 1568 | | ClearOwnedStructuralClosureBaseline(); |
| | 19 | 1569 | | return; |
| | | 1570 | | } |
| | | 1571 | | System.Diagnostics.Debug.Assert( |
| | | 1572 | | effect == PublishedCandidateEffect.StartAutomaticSeam, |
| | | 1573 | | "Every non-terminal published-candidate effect starts automatic-seam work."); |
| | | 1574 | | System.Diagnostics.Debug.Assert(automaticSeamGraph != null); |
| | 36 | 1575 | | _lifecycleEventCount = _publicationEventCount; |
| | 36 | 1576 | | _lifecycleWork = new NavigationAutomaticSeamLifecycleWork( |
| | 36 | 1577 | | _world, |
| | 36 | 1578 | | automaticSeamGraph!, |
| | 36 | 1579 | | _maintenanceEvents, |
| | 36 | 1580 | | _lifecycleEventCount, |
| | 36 | 1581 | | _automaticSeamFullRebuildPending); |
| | 36 | 1582 | | } |
| | | 1583 | | |
| | | 1584 | | private void CaptureOwnedStructuralClosureBaseline(NavigationWorldGraph graph) |
| | | 1585 | | { |
| | 3141 | 1586 | | if (_ownedStructuralClosureBaseline != null) |
| | 2910 | 1587 | | return; |
| | 231 | 1588 | | _ownedStructuralClosureBaseline = graph.ClosedStructuralComponents; |
| | 231 | 1589 | | _ownedStructuralClosureBaselineAll = graph.AreAllStructuralComponentsClosed; |
| | 231 | 1590 | | } |
| | | 1591 | | |
| | | 1592 | | private NavigationWorldGraph CreateOwnedStructuralClosure( |
| | | 1593 | | NavigationWorldGraph graph, |
| | | 1594 | | NavigationSurfaceComponentKeySet affected, |
| | | 1595 | | bool closeAll, |
| | | 1596 | | long graphVersion) |
| | | 1597 | | { |
| | 410 | 1598 | | return graph.WithOwnedStructuralClosure( |
| | 410 | 1599 | | _ownedStructuralClosureBaseline!, |
| | 410 | 1600 | | affected, |
| | 410 | 1601 | | closeAll || _ownedStructuralClosureBaselineAll, |
| | 410 | 1602 | | graphVersion); |
| | | 1603 | | } |
| | | 1604 | | |
| | | 1605 | | private NavigationWorldGraph PreservePriorStructuralClosures( |
| | 1079 | 1606 | | NavigationWorldGraph graph) => _ownedStructuralClosureBaseline == null |
| | 1079 | 1607 | | ? graph |
| | 1079 | 1608 | | : graph.WithClosedStructuralComponents( |
| | 1079 | 1609 | | _ownedStructuralClosureBaseline, |
| | 1079 | 1610 | | _ownedStructuralClosureBaselineAll, |
| | 1079 | 1611 | | graph.GraphVersion); |
| | | 1612 | | |
| | | 1613 | | private void ClearOwnedStructuralClosureBaseline() |
| | | 1614 | | { |
| | 1009 | 1615 | | _ownedStructuralClosureBaseline = null; |
| | 1009 | 1616 | | _ownedStructuralClosureBaselineAll = false; |
| | 1009 | 1617 | | } |
| | | 1618 | | |
| | | 1619 | | private bool IsOperationWithinRetainedWorkCapacity(long workBytes, int workPages) => |
| | 19529 | 1620 | | IsWithinRetainedWorkCapacity(workBytes, workPages); |
| | | 1621 | | |
| | | 1622 | | private long GetCombinedCompositionWorkBytes( |
| | | 1623 | | NavigationStructuralCompositionWork work, |
| | | 1624 | | NavigationWorldGraph ownerGraph) => |
| | 344 | 1625 | | checked( |
| | 344 | 1626 | | work.RetainedBytes |
| | 344 | 1627 | | + _operations.RetainedOperationWorkBytes |
| | 344 | 1628 | | + GetOwnedClosureBaselineAdditionalRetainedBytes(ownerGraph)); |
| | | 1629 | | |
| | | 1630 | | private int GetCombinedCompositionWorkPages( |
| | | 1631 | | NavigationStructuralCompositionWork work, |
| | | 1632 | | NavigationWorldGraph ownerGraph) => |
| | 344 | 1633 | | checked( |
| | 344 | 1634 | | work.PersistentPageCount |
| | 344 | 1635 | | + _operations.RetainedOperationWorkPageCount |
| | 344 | 1636 | | + GetOwnedClosureBaselineAdditionalPersistentPages(ownerGraph)); |
| | | 1637 | | |
| | | 1638 | | private long GetCombinedMaterializedWorkBytes( |
| | | 1639 | | NavigationMaterializedComponentWork work) |
| | | 1640 | | { |
| | | 1641 | | System.Diagnostics.Debug.Assert(_lifecycleWork == null, |
| | | 1642 | | "Materialized and automatic-seam lifecycle work are mutually exclusive."); |
| | 5566 | 1643 | | return checked( |
| | 5566 | 1644 | | work.RetainedBytes |
| | 5566 | 1645 | | + _operations.RetainedOperationWorkBytes |
| | 5566 | 1646 | | + (_compositionWork?.RetainedBytes ?? 0L) |
| | 5566 | 1647 | | + GetOwnedClosureBaselineAdditionalRetainedBytes(_store.Current)); |
| | | 1648 | | } |
| | | 1649 | | |
| | | 1650 | | private int GetCombinedMaterializedWorkPages( |
| | | 1651 | | NavigationMaterializedComponentWork work) |
| | | 1652 | | { |
| | | 1653 | | System.Diagnostics.Debug.Assert(_lifecycleWork == null, |
| | | 1654 | | "Materialized and automatic-seam lifecycle work are mutually exclusive."); |
| | 5566 | 1655 | | return checked( |
| | 5566 | 1656 | | work.PersistentPageCount |
| | 5566 | 1657 | | + _operations.RetainedOperationWorkPageCount |
| | 5566 | 1658 | | + (_compositionWork?.PersistentPageCount ?? 0) |
| | 5566 | 1659 | | + GetOwnedClosureBaselineAdditionalPersistentPages(_store.Current)); |
| | | 1660 | | } |
| | | 1661 | | |
| | | 1662 | | private long GetOwnedClosureBaselineAdditionalRetainedBytes( |
| | | 1663 | | NavigationWorldGraph ownerGraph) |
| | | 1664 | | { |
| | 19935 | 1665 | | GetOwnedClosureBaselineAdditionalCapacity( |
| | 19935 | 1666 | | _ownedStructuralClosureBaseline, |
| | 19935 | 1667 | | ownerGraph, |
| | 19935 | 1668 | | out long retainedBytes, |
| | 19935 | 1669 | | out _); |
| | 19935 | 1670 | | return retainedBytes; |
| | | 1671 | | } |
| | | 1672 | | |
| | | 1673 | | private int GetOwnedClosureBaselineAdditionalPersistentPages( |
| | | 1674 | | NavigationWorldGraph ownerGraph) |
| | | 1675 | | { |
| | 20275 | 1676 | | GetOwnedClosureBaselineAdditionalCapacity( |
| | 20275 | 1677 | | _ownedStructuralClosureBaseline, |
| | 20275 | 1678 | | ownerGraph, |
| | 20275 | 1679 | | out _, |
| | 20275 | 1680 | | out int persistentPages); |
| | 20275 | 1681 | | return persistentPages; |
| | | 1682 | | } |
| | | 1683 | | |
| | | 1684 | | private void GetCompositionWorkAllowance(out long bytes, out int pages) |
| | | 1685 | | { |
| | 11793 | 1686 | | NavigationBaselineRebuild.GetRetainedTotals( |
| | 11793 | 1687 | | _baselineRebuilds, |
| | 11793 | 1688 | | out long rebuildBytes, |
| | 11793 | 1689 | | out int rebuildPages); |
| | 11793 | 1690 | | NavigationWorldGraph current = _store.Current; |
| | 11793 | 1691 | | bytes = _maxActiveSnapshotBytes |
| | 11793 | 1692 | | - current.RetainedBytes |
| | 11793 | 1693 | | - rebuildBytes |
| | 11793 | 1694 | | - _operations.RetainedOperationWorkBytes |
| | 11793 | 1695 | | - GetOwnedClosureBaselineAdditionalRetainedBytes(current); |
| | 11793 | 1696 | | pages = _maxPersistentGraphPages |
| | 11793 | 1697 | | - current.PersistentPageCount |
| | 11793 | 1698 | | - rebuildPages |
| | 11793 | 1699 | | - _operations.RetainedOperationWorkPageCount |
| | 11793 | 1700 | | - GetOwnedClosureBaselineAdditionalPersistentPages(current); |
| | 11793 | 1701 | | } |
| | | 1702 | | |
| | | 1703 | | private NavigationCandidatePublication PublishPendingCandidate( |
| | | 1704 | | NavigationOperationCandidate candidate, |
| | | 1705 | | int frame, |
| | | 1706 | | NavigationOperationFrameChange[] changes, |
| | | 1707 | | int changeCount) |
| | | 1708 | | { |
| | 11982 | 1709 | | if (changeCount == 0 && _store.Current.HasClosedStructuralScope) |
| | | 1710 | | { |
| | 15 | 1711 | | bool available = _automaticSeamFullRebuildPending |
| | 15 | 1712 | | || TryPublishReopenedStructuralScopes(); |
| | | 1713 | | System.Diagnostics.Debug.Assert(available, |
| | | 1714 | | "A no-change operation reopens its owned closure only after writer preflight succeeds."); |
| | 15 | 1715 | | return NavigationCandidatePublication.Published; |
| | | 1716 | | } |
| | 11967 | 1717 | | return PublishCandidate( |
| | 11967 | 1718 | | candidate, |
| | 11967 | 1719 | | _publicationAreaCatalog, |
| | 11967 | 1720 | | changes, |
| | 11967 | 1721 | | changeCount); |
| | | 1722 | | } |
| | | 1723 | | |
| | | 1724 | | private int PrepareResnapshotScopes(int blockedScopeCount, bool blockAll) |
| | | 1725 | | { |
| | 20249 | 1726 | | if (_priorBlockAll || blockAll) |
| | 57 | 1727 | | return 0; |
| | | 1728 | | |
| | 20192 | 1729 | | int count = 0; |
| | 40606 | 1730 | | for (int i = 0; i < _priorBlockedScopeCount; i++) |
| | | 1731 | | { |
| | 111 | 1732 | | NavigationGridChangeScope prior = _priorBlockedScopes[i]; |
| | 111 | 1733 | | if (!ContainsScope(_blockedScopes, blockedScopeCount, prior.ConfigurationKey)) |
| | 110 | 1734 | | _resnapshotScopes[count++] = prior; |
| | | 1735 | | } |
| | 20192 | 1736 | | return count; |
| | | 1737 | | } |
| | | 1738 | | |
| | | 1739 | | private void CommitBlockedScopes( |
| | | 1740 | | int blockedScopeCount, |
| | | 1741 | | bool blockAll, |
| | | 1742 | | int deferredScopeCount, |
| | | 1743 | | bool deferAll) |
| | | 1744 | | { |
| | 20222 | 1745 | | if (blockAll || deferAll) |
| | | 1746 | | { |
| | 30 | 1747 | | _priorBlockedScopeCount = 0; |
| | 30 | 1748 | | _priorBlockAll = true; |
| | 30 | 1749 | | return; |
| | | 1750 | | } |
| | | 1751 | | |
| | 20192 | 1752 | | _priorBlockedScopeCount = MergeBlockedScopes( |
| | 20192 | 1753 | | _blockedScopes, |
| | 20192 | 1754 | | blockedScopeCount, |
| | 20192 | 1755 | | _deferredScopes, |
| | 20192 | 1756 | | deferredScopeCount, |
| | 20192 | 1757 | | _priorBlockedScopes, |
| | 20192 | 1758 | | out _priorBlockAll); |
| | 20192 | 1759 | | } |
| | | 1760 | | |
| | | 1761 | | internal static int MergeBlockedScopes( |
| | | 1762 | | NavigationGridChangeScope[] blockedScopes, |
| | | 1763 | | int blockedScopeCount, |
| | | 1764 | | NavigationGridChangeScope[] deferredScopes, |
| | | 1765 | | int deferredScopeCount, |
| | | 1766 | | NavigationGridChangeScope[] destination, |
| | | 1767 | | out bool blockAll) |
| | | 1768 | | { |
| | | 1769 | | System.Diagnostics.Debug.Assert(blockedScopeCount <= destination.Length, |
| | | 1770 | | "The runtime sizes blocked-scope capture to its committed-scope capacity."); |
| | 20194 | 1771 | | int count = 0; |
| | 40414 | 1772 | | for (int i = 0; i < blockedScopeCount; i++) |
| | 13 | 1773 | | destination[count++] = blockedScopes[i]; |
| | 40608 | 1774 | | for (int i = 0; i < deferredScopeCount; i++) |
| | | 1775 | | { |
| | 111 | 1776 | | NavigationGridChangeScope deferred = deferredScopes[i]; |
| | 111 | 1777 | | if (ContainsScope(destination, count, deferred.ConfigurationKey)) |
| | | 1778 | | continue; |
| | 110 | 1779 | | if (count == destination.Length) |
| | | 1780 | | { |
| | 1 | 1781 | | blockAll = true; |
| | 1 | 1782 | | return 0; |
| | | 1783 | | } |
| | 109 | 1784 | | destination[count++] = deferred; |
| | | 1785 | | } |
| | 20193 | 1786 | | blockAll = false; |
| | 20193 | 1787 | | return count; |
| | | 1788 | | } |
| | | 1789 | | |
| | | 1790 | | private static bool ContainsScope( |
| | | 1791 | | NavigationGridChangeScope[] scopes, |
| | | 1792 | | int count, |
| | | 1793 | | GridForge.Configuration.GridConfigurationKey key) |
| | | 1794 | | { |
| | 456 | 1795 | | for (int i = 0; i < count; i++) |
| | | 1796 | | { |
| | 8 | 1797 | | if (scopes[i].ConfigurationKey.Equals(key)) |
| | 2 | 1798 | | return true; |
| | | 1799 | | } |
| | 220 | 1800 | | return false; |
| | | 1801 | | } |
| | | 1802 | | |
| | | 1803 | | internal void EnqueueCommittedChange(GridEventInfo eventInfo) |
| | | 1804 | | { |
| | 1189 | 1805 | | if (_disposed || eventInfo.WorldSpawnToken != _world.SpawnToken) |
| | 2 | 1806 | | return; |
| | 1187 | 1807 | | _ingress.Enqueue(eventInfo); |
| | 1187 | 1808 | | } |
| | | 1809 | | |
| | | 1810 | | private void MarkResnapshotRequired() |
| | | 1811 | | { |
| | 26 | 1812 | | _ingress.MarkResnapshotRequired(); |
| | 26 | 1813 | | } |
| | | 1814 | | |
| | | 1815 | | private void MarkSafetyPendingIf(bool required) |
| | | 1816 | | { |
| | 40 | 1817 | | if (required) |
| | 6 | 1818 | | _store.MarkSafetyPending(); |
| | 40 | 1819 | | } |
| | | 1820 | | |
| | | 1821 | | private void ClearSafetyPendingAfterExactResnapshot() |
| | | 1822 | | { |
| | | 1823 | | System.Diagnostics.Debug.Assert(!_publicationResnapshotAll || !_publicationBlockAll); |
| | 20222 | 1824 | | if (CanClearSafetyPendingAfterSnapshot( |
| | 20222 | 1825 | | _store.IsSafetyPending, |
| | 20222 | 1826 | | _publicationResnapshotAll, |
| | 20222 | 1827 | | _publicationBlockedScopeCount, |
| | 20222 | 1828 | | _snapshotDeferAll, |
| | 20222 | 1829 | | _snapshotDeferredScopeCount)) |
| | | 1830 | | { |
| | 1 | 1831 | | _store.ClearSafetyPending(); |
| | | 1832 | | } |
| | 20222 | 1833 | | } |
| | | 1834 | | |
| | | 1835 | | internal static bool CanClearSafetyPendingAfterSnapshot( |
| | | 1836 | | bool safetyPending, |
| | | 1837 | | bool resnapshotAll, |
| | | 1838 | | int blockedScopeCount, |
| | | 1839 | | bool deferAll, |
| | 20228 | 1840 | | int deferredScopeCount) => safetyPending |
| | 20228 | 1841 | | && resnapshotAll |
| | 20228 | 1842 | | && blockedScopeCount == 0 |
| | 20228 | 1843 | | && !deferAll |
| | 20228 | 1844 | | && deferredScopeCount == 0; |
| | | 1845 | | |
| | | 1846 | | private void RequeuePublicationEvents() |
| | | 1847 | | { |
| | 10860 | 1848 | | _ingress.RequeuePrefix(_maintenanceEvents.AsSpan(0, _publicationEventCount)); |
| | 10860 | 1849 | | } |
| | | 1850 | | |
| | | 1851 | | private void DrainSafetyPrefixUnderPressure() |
| | | 1852 | | { |
| | 17 | 1853 | | int count = _ingress.DetachInto( |
| | 17 | 1854 | | _maintenanceEvents, |
| | 17 | 1855 | | _blockedScopes, |
| | 17 | 1856 | | out int blockedScopeCount, |
| | 17 | 1857 | | out bool blockAll, |
| | 17 | 1858 | | out bool topologyLifecycleCoverageLost); |
| | 17 | 1859 | | _maintenanceMeter.TryConsumeEnvelopes(count); |
| | 17 | 1860 | | if (topologyLifecycleCoverageLost |
| | 17 | 1861 | | || ContainsTopologyLifecycle(_maintenanceEvents, count)) |
| | | 1862 | | { |
| | 4 | 1863 | | _automaticSeamFullRebuildPending = true; |
| | | 1864 | | } |
| | 17 | 1865 | | bool requiresResnapshot = count != 0 || blockedScopeCount != 0 || blockAll; |
| | 17 | 1866 | | MarkSafetyPendingIf(requiresResnapshot); |
| | 17 | 1867 | | if (requiresResnapshot) |
| | | 1868 | | { |
| | | 1869 | | // Snapshot admission is already closed by the store while bounded leased generations |
| | | 1870 | | // prevent publication. Drain a bounded final-state prefix now, then require an exact |
| | | 1871 | | // baseline before any affected scope can reopen once publication pressure clears. |
| | 5 | 1872 | | _ingress.MarkResnapshotRequired(); |
| | | 1873 | | } |
| | 17 | 1874 | | } |
| | | 1875 | | |
| | | 1876 | | private static bool ContainsTopologyLifecycle(GridEventInfo[] events, int count) |
| | | 1877 | | { |
| | 33506 | 1878 | | for (int i = 0; i < count; i++) |
| | | 1879 | | { |
| | 118 | 1880 | | GridEventKind kind = events[i].ChangeKind; |
| | 118 | 1881 | | if (kind == GridEventKind.GridAdded |
| | 118 | 1882 | | || kind == GridEventKind.GridRemoved |
| | 118 | 1883 | | || kind == GridEventKind.WorldReset) |
| | | 1884 | | { |
| | 30 | 1885 | | return true; |
| | | 1886 | | } |
| | | 1887 | | } |
| | 16635 | 1888 | | return false; |
| | | 1889 | | } |
| | | 1890 | | |
| | | 1891 | | private void ClearBaselineCaptures() |
| | | 1892 | | { |
| | 43304 | 1893 | | for (int i = 0; i < _snapshotAffectedCount; i++) |
| | 1404 | 1894 | | _baselineCaptures[_affectedMapOrdinals[i]] = default; |
| | 20248 | 1895 | | } |
| | | 1896 | | |
| | | 1897 | | private void RemoveCompletedBaselineRebuilds() |
| | | 1898 | | { |
| | 40768 | 1899 | | for (int i = _baselineRebuilds.Count - 1; i >= 0; i--) |
| | | 1900 | | { |
| | 161 | 1901 | | NavigationBaselineRebuild rebuild = _baselineRebuilds.GetValueAt(i); |
| | 161 | 1902 | | if (rebuild.IsComplete) |
| | 53 | 1903 | | _baselineRebuilds = _baselineRebuilds.Remove(rebuild.MapId, out _); |
| | | 1904 | | } |
| | 20223 | 1905 | | } |
| | | 1906 | | |
| | | 1907 | | private void EnsureUsable() |
| | | 1908 | | { |
| | 26398 | 1909 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(NavigationGraphRuntime)); |
| | 26398 | 1910 | | } |
| | | 1911 | | } |