| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "log" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "reflect" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/carlosframework/platform/internal/hostbox" |
| 17 | "github.com/carlosframework/platform/internal/hostclaim" |
| 18 | "github.com/carlosframework/platform/internal/hostguard" |
| 19 | "github.com/carlosframework/platform/internal/registry" |
| 20 | "github.com/carlosframework/platform/internal/release" |
| 21 | ) |
| 22 | |
| 23 | func (h ClaimRootHTTP) commitRootInventory(ctx context.Context, q hostbox.JournalRequest) (hostbox.JournalResponse, error) { |
| 24 | return h.withRootInventoryHistory(ctx, q, false) |
| 25 | } |
| 26 | func (h ClaimRootHTTP) withRootInventoryHistory(ctx context.Context, q hostbox.JournalRequest, pending bool) (hostbox.JournalResponse, error) { |
| 27 | if h.history == nil { |
| 28 | return h.commitRootInventoryPending(ctx, q, pending, nil) |
| 29 | } |
| 30 | var out hostbox.JournalResponse |
| 31 | err := h.history(ctx, func(history *release.RootHistory) error { |
| 32 | var err error |
| 33 | out, err = h.commitRootInventoryPending(ctx, q, pending, history) |
| 34 | return err |
| 35 | }) |
| 36 | return out, err |
| 37 | } |
| 38 | |
| 39 | func (h ClaimRootHTTP) commitRootInventoryPending(ctx context.Context, q hostbox.JournalRequest, pending bool, history *release.RootHistory) (hostbox.JournalResponse, error) { |
| 40 | var out hostbox.JournalResponse |
| 41 | if !pending && (q.V == 2 || q.V == 3) { |
| 42 | now := time.Now() |
| 43 | if h.Journals != nil && h.Journals.Now != nil { |
| 44 | now = h.Journals.Now() |
| 45 | } |
| 46 | age := now.Sub(time.Unix(0, q.IssuedAt)) |
| 47 | if age > hostbox.JournalRequestWindow || age < -hostbox.JournalRequestWindow { |
| 48 | return out, hostbox.ErrJournalUncertain |
| 49 | } |
| 50 | } |
| 51 | guard := h.Stop.Coordinator.Guard |
| 52 | if guard.Root == nil || h.Journals == nil || h.InventoryActor == nil || h.Reconcile.Registry == nil || h.Reconcile.Census == nil { |
| 53 | return out, hostbox.ErrUnsupported |
| 54 | } |
| 55 | if h.RequireNewWork != nil { |
| 56 | if err := h.RequireNewWork(ctx); err != nil { |
| 57 | return out, err |
| 58 | } |
| 59 | } |
| 60 | actor, err := h.InventoryActor(ctx) |
| 61 | if err != nil { |
| 62 | return out, err |
| 63 | } |
| 64 | in, t := q.Inventory, q.Target |
| 65 | in.Entries = append([]hostbox.StopInventoryEntry(nil), in.Entries...) |
| 66 | // The inventory's stamp is validated against the fresh binding; a target |
| 67 | // stamp, when the agent sends one, must name that same binding before |
| 68 | // any intent is persisted (fix round 1, finding 2). |
| 69 | if q.Op != "inventory-write" || !in.Complete || in.Box != actor.Box || in.Enrollment != actor.Enrollment || in.DirectoryRevision != actor.Binding || (t.DirectoryRevision != "" && t.DirectoryRevision != in.DirectoryRevision) || t.Host != in.Host || t.Incarnation != in.Incarnation || t.Stamp != in.InstallationStamp || t.Box != actor.Box || t.Enrollment != actor.Enrollment || t.ClaimETag == "" || q.RecordKey == "" || q.RecordETag == "" { |
| 70 | return out, hostbox.ErrChanged |
| 71 | } |
| 72 | observed, err := hostclaim.Read(ctx, h.Stop.Coordinator.Evidence, in.Host) |
| 73 | if err != nil { |
| 74 | return out, err |
| 75 | } |
| 76 | if observed.Absent || observed.Claim == nil || observed.Claim.Holder == nil { |
| 77 | return out, hostbox.ErrChanged |
| 78 | } |
| 79 | applied := false |
| 80 | if pending { |
| 81 | saved, e := h.Journals.Inventory.SnapshotRequest(ctx, t) |
| 82 | if e != nil { |
| 83 | return out, e |
| 84 | } |
| 85 | applied = saved.Revision == q.Revision+1 && saved.LastRequest == q.ID |
| 86 | } |
| 87 | holder := observed.Claim.Holder |
| 88 | if (!applied && observed.ETag != t.ClaimETag) || holder.Incarnation != t.Incarnation || holder.Stamp != t.Stamp || holder.Session != t.Session || holder.Key != q.RecordKey || observed.Claim.Op.ID != t.Op || observed.Claim.Op.Phase != t.Phase { |
| 89 | return out, hostbox.ErrChanged |
| 90 | } |
| 91 | firstLanding := q.Revision == 0 && observed.Claim.Op.Kind == hostclaim.KindLanding |
| 92 | if firstLanding { |
| 93 | if err := rootFirstInstallation(observed.Claim, actor); err != nil { |
| 94 | return out, err |
| 95 | } |
| 96 | entry := rootLandingEntry(q, observed.Claim, actor) |
| 97 | if len(in.Entries) != 0 { |
| 98 | return out, hostbox.ErrTypedInventory |
| 99 | } |
| 100 | in.Entries = []hostbox.StopInventoryEntry{entry} |
| 101 | } |
| 102 | var canonical registry.Route |
| 103 | var currentRow *registry.Route |
| 104 | installation := hostguard.RootInstallation{Empty: len(in.Entries) == 0, Host: in.Host, Incarnation: in.Incarnation, Stamp: in.InstallationStamp, Migration: 1, InventoryRevision: q.Revision + 1, Account: holder.Account, App: holder.App, Record: holder.Key, Box: actor.Box, Enrollment: actor.Enrollment} |
| 105 | if q.Revision == 0 { |
| 106 | installation.Registration = hostguard.InstallationRegistration{Op: observed.Claim.Op.ID, Kind: observed.Claim.Op.Kind, Phase: observed.Claim.Op.Phase, Session: holder.Session} |
| 107 | if firstLanding { |
| 108 | installation.Registration.RowID = in.Entries[0].RowIdentity |
| 109 | } |
| 110 | } |
| 111 | raw, err := json.Marshal(q) |
| 112 | if err != nil { |
| 113 | return out, err |
| 114 | } |
| 115 | replayed := false |
| 116 | err = guard.CommitRootInventoryArtifacts(ctx, installation, raw, func(c *hostclaim.Claim) error { |
| 117 | if !reflect.DeepEqual(c, observed.Claim) { |
| 118 | return hostbox.ErrChanged |
| 119 | } |
| 120 | fresh, err := hostclaim.Read(ctx, h.Stop.Coordinator.Evidence, in.Host) |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | if !applied && fresh.ETag != t.ClaimETag { |
| 125 | return hostbox.ErrChanged |
| 126 | } |
| 127 | var etag string |
| 128 | canonical, etag, err = rootClaimRecord(ctx, h.Stop.Coordinator.Evidence, c, h.Roots["data"]) |
| 129 | if err != nil { |
| 130 | return err |
| 131 | } |
| 132 | if etag != q.RecordETag { |
| 133 | return hostbox.ErrChanged |
| 134 | } |
| 135 | if c.Op.Kind == hostclaim.KindRelocating && q.Revision == 0 { |
| 136 | rec, e := relocationRecord(ctx, h.Roots["data"], h.Stop.Coordinator.Evidence, observed) |
| 137 | if e != nil { |
| 138 | return e |
| 139 | } |
| 140 | canonical = rec.Route |
| 141 | } |
| 142 | if c.Op.Kind == hostclaim.KindLanding { |
| 143 | canonical, err = h.rootLandingRoute(ctx, c, actor) |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | } |
| 148 | owner := sha256.Sum256([]byte(fmt.Sprintf("%s\x00%s\x00%d\x00%s", in.Host, in.Incarnation, in.InstallationStamp, actor.Box))) |
| 149 | canonical.ClaimIncarnation, canonical.ClaimStamp, canonical.ClaimOwner = in.Incarnation, in.InstallationStamp, hex.EncodeToString(owner[:]) |
| 150 | for _, entry := range in.Entries { |
| 151 | if entry.Kind == "route" { |
| 152 | if canonical.ClaimRowID != "" { |
| 153 | return hostbox.ErrTypedInventory |
| 154 | } |
| 155 | canonical.ClaimRowID = entry.RowIdentity |
| 156 | } |
| 157 | } |
| 158 | row, err := h.Reconcile.Registry.Get(in.Host) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | currentRow = row |
| 163 | if row != nil && (row.Host != canonical.Host || row.Account != canonical.Account || row.App != canonical.App || row.DBPath != canonical.DBPath || row.ClaimOwnership != canonical.ClaimOwnership) { |
| 164 | return hostbox.ErrTypedInventory |
| 165 | } |
| 166 | if row == nil { |
| 167 | for _, e := range in.Entries { |
| 168 | if e.Kind != "route" || (e.Materialization != "intended" && !firstLanding) { |
| 169 | return hostbox.ErrTypedInventory |
| 170 | } |
| 171 | } |
| 172 | } else if err := hostbox.ValidateAgentInventory(in, &canonical, h.Roots["data"]); err != nil { |
| 173 | return err |
| 174 | } |
| 175 | if err := h.verifyInventoryTimers(in); err != nil { |
| 176 | return err |
| 177 | } |
| 178 | if err := h.bindInventoryPaths(ctx, &in); err != nil { |
| 179 | return err |
| 180 | } |
| 181 | for _, e := range in.Entries { |
| 182 | if e.Ownership != canonical.ClaimOwner { |
| 183 | return hostbox.ErrTypedInventory |
| 184 | } |
| 185 | if e.Kind == "route" { |
| 186 | if e.ManagedRoot != "registry" || e.RelativePath != in.Host || e.RowIdentity == "" { |
| 187 | return hostbox.ErrTypedInventory |
| 188 | } |
| 189 | continue |
| 190 | } |
| 191 | if e.Kind == "timer" { |
| 192 | if err := h.verifyTimerMutationRow(in.Host, canonical.ClaimOwnership, e, q.Timer); err != nil { |
| 193 | return err |
| 194 | } |
| 195 | continue |
| 196 | } |
| 197 | if err := h.verifyInventoryPath(e); err != nil { |
| 198 | return err |
| 199 | } |
| 200 | } |
| 201 | return nil |
| 202 | }, func(priorRoot hostguard.RootInstallation) error { |
| 203 | if firstLanding && currentRow != nil && priorRoot.Registration != installation.Registration { |
| 204 | return hostbox.ErrTypedInventory |
| 205 | } |
| 206 | if firstLanding && currentRow == nil && priorRoot.InventoryRevision != 0 { |
| 207 | return hostbox.ErrTypedInventory |
| 208 | } |
| 209 | if observed.Claim.Op.Kind == hostclaim.KindLanding && !firstLanding && !landingRegistrationMatches(priorRoot, observed.Claim, currentRow) { |
| 210 | return hostbox.ErrTypedInventory |
| 211 | } |
| 212 | |
| 213 | snapshot, err := h.Journals.Inventory.SnapshotRequest(ctx, hostguard.FenceTarget{Host: in.Host, Incarnation: in.Incarnation, Stamp: in.InstallationStamp}) |
| 214 | prior, revision := snapshot.Inventory, snapshot.Revision |
| 215 | if err != nil { |
| 216 | return err |
| 217 | } |
| 218 | replay, found, err := h.Journals.Replay(ctx, actor.Box+"/"+actor.Enrollment, q) |
| 219 | if err != nil { |
| 220 | return err |
| 221 | } |
| 222 | if found { |
| 223 | if revision != q.Revision+1 || !reflect.DeepEqual(prior, in) { |
| 224 | return hostbox.ErrChanged |
| 225 | } |
| 226 | out = replay |
| 227 | replayed = true |
| 228 | return nil |
| 229 | } |
| 230 | if revision == q.Revision+1 && reflect.DeepEqual(prior, in) && (priorRoot.InventoryRevision == revision || pending && snapshot.LastRequest == q.ID) { |
| 231 | out = hostbox.JournalResponse{V: 1, ID: q.ID, Revision: revision} |
| 232 | replayed = true |
| 233 | return nil |
| 234 | } |
| 235 | if revision != q.Revision { |
| 236 | return hostbox.ErrChanged |
| 237 | } |
| 238 | if err := h.verifyNewInventoryIdentities(prior, in); err != nil { |
| 239 | return err |
| 240 | } |
| 241 | if err := h.verifyTimerMutation(prior, in, q.Timer); err != nil { |
| 242 | return err |
| 243 | } |
| 244 | if revision == 0 { |
| 245 | if (!firstLanding && len(in.Entries) != 0) || rootFirstInstallation(observed.Claim, actor) != nil { |
| 246 | return hostbox.ErrChanged |
| 247 | } |
| 248 | census := h.rootEmptyCensus() |
| 249 | selected := hostbox.RecordSelection{Host: in.Host, Key: holder.Key, ETag: q.RecordETag, Incarnation: in.Incarnation, Family: holder.Family} |
| 250 | if firstLanding && currentRow != nil && priorRoot.Registration == installation.Registration { |
| 251 | if err := census.CheckRootRegisteredRow(ctx, observed, selected, currentRow.ClaimOwnership); err != nil { |
| 252 | return err |
| 253 | } |
| 254 | } else if err := census.CheckLocal(ctx, observed, selected); err != nil { |
| 255 | return err |
| 256 | } |
| 257 | } else { |
| 258 | if priorRoot.InventoryRevision != revision { |
| 259 | return hostbox.ErrChanged |
| 260 | } |
| 261 | for _, old := range prior.Entries { |
| 262 | found := false |
| 263 | for _, next := range in.Entries { |
| 264 | if old.ArtifactID == next.ArtifactID { |
| 265 | if q.Timer != nil && old.Kind == "timer" && old.RelativePath == q.Timer.Timer.Host+"/"+q.Timer.Timer.Name { |
| 266 | old.RowIdentity, old.Materialization, old.TimerRetired = next.RowIdentity, next.Materialization, next.TimerRetired |
| 267 | } |
| 268 | if old.Materialization == "intended" && next.Materialization == "materialized" { |
| 269 | old.Materialization = next.Materialization |
| 270 | old.Identity = next.Identity |
| 271 | old.Device = next.Device |
| 272 | old.Inode = next.Inode |
| 273 | old.Type = next.Type |
| 274 | } |
| 275 | if old != next { |
| 276 | return hostbox.ErrChanged |
| 277 | } |
| 278 | found = true |
| 279 | } |
| 280 | } |
| 281 | if !found { |
| 282 | return hostbox.ErrChanged |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | return nil |
| 287 | }, func(prior hostguard.RootInstallation) error { |
| 288 | if history != nil && q.Revision == 0 && prior.InventoryRevision == 0 && observed.Claim.Op.Kind == hostclaim.KindServing && holder.Session == 0 && holder.WriterBox == "" { |
| 289 | if err := history.RecordCreation(release.HistoryBinding{Host: in.Host, Account: holder.Account, App: holder.App}, rootHistoryInstallation(installation)); err != nil { |
| 290 | return err |
| 291 | } |
| 292 | } |
| 293 | if firstLanding && currentRow == nil { |
| 294 | if err := h.Reconcile.Registry.Upsert(canonical); err != nil { |
| 295 | return err |
| 296 | } |
| 297 | } |
| 298 | if replayed { |
| 299 | return nil |
| 300 | } |
| 301 | if q.Timer != nil { |
| 302 | if err := h.applyTimerMutation(*q.Timer); err != nil { |
| 303 | return err |
| 304 | } |
| 305 | } |
| 306 | if firstLanding || h.inventoryIdentity != nil { |
| 307 | out, err = h.Journals.ExecuteRootInventory(ctx, actor.Box+"/"+actor.Enrollment, q, in, pending) |
| 308 | } else if pending { |
| 309 | out, err = h.Journals.CompletePendingInventory(ctx, actor.Box+"/"+actor.Enrollment, q) |
| 310 | } else { |
| 311 | out, err = h.Journals.Execute(ctx, actor.Box+"/"+actor.Enrollment, q) |
| 312 | } |
| 313 | return err |
| 314 | }, func(prior hostguard.RootInstallation) (hostguard.InstallationArtifacts, error) { |
| 315 | return rootInventoryArtifacts(prior, canonical, in, q.Revision) |
| 316 | }) |
| 317 | return out, err |
| 318 | } |
| 319 | |
| 320 | func (h ClaimRootHTTP) verifyInventoryPath(e hostbox.StopInventoryEntry) error { |
| 321 | if e.Kind != "path" || e.ManagedRoot != "data" || !filepath.IsLocal(e.RelativePath) { |
| 322 | return hostbox.ErrTypedInventory |
| 323 | } |
| 324 | path := filepath.Join(h.Roots["data"], e.RelativePath) |
| 325 | if e.Materialization == "intended" { |
| 326 | if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) { |
| 327 | return hostbox.ErrChanged |
| 328 | } |
| 329 | return nil |
| 330 | } |
| 331 | if e.Materialization != "materialized" { |
| 332 | return hostbox.ErrTypedInventory |
| 333 | } |
| 334 | return hostbox.VerifyInventoryIdentity(h.Roots["data"], e) |
| 335 | } |
| 336 | |
| 337 | func (h ClaimRootHTTP) verifyNewInventoryIdentities(prior, next hostbox.StopInventory) error { |
| 338 | for _, e := range next.Entries { |
| 339 | if e.Kind != "path" || e.Materialization != "materialized" { |
| 340 | continue |
| 341 | } |
| 342 | retained := false |
| 343 | for _, old := range prior.Entries { |
| 344 | if old.ArtifactID == e.ArtifactID && old.Materialization == "materialized" { |
| 345 | retained = true |
| 346 | } |
| 347 | } |
| 348 | if retained { |
| 349 | continue |
| 350 | } |
| 351 | current, err := hostbox.CaptureInventoryIdentity(h.Roots[e.ManagedRoot], e.RelativePath) |
| 352 | if err != nil { |
| 353 | return err |
| 354 | } |
| 355 | if e.Identity.Boot != current.Boot { |
| 356 | return hostbox.ErrChanged |
| 357 | } |
| 358 | if err := h.verifyInventoryPath(e); err != nil { |
| 359 | return err |
| 360 | } |
| 361 | } |
| 362 | return nil |
| 363 | } |
| 364 | |
| 365 | // obsoleteIntent reports whether a pending intent was stamped under a binding |
| 366 | // other than the startup binding. Its stamp is validated binding evidence |
| 367 | // only: an inventory's own stamp (checked against the fresh binding before |
| 368 | // the intent was persisted) or a rebind's successor stamp (likewise). An |
| 369 | // agent-sent target stamp is never consulted (fix round 1, finding 2). |
| 370 | // Replaying an obsolete intent would compare the old binding with today's |
| 371 | // authority; it is settled instead (N3). |
| 372 | func (h ClaimRootHTTP) obsoleteIntent(stamp string) bool { |
| 373 | return h.startupBinding != "" && stamp != h.startupBinding |
| 374 | } |
| 375 | |
| 376 | // settleObsoleteIntent settles an obsolete pending intent through the |
| 377 | // guard's SettleObsoleteRootInventory (same host lock, exact intent bytes), |
| 378 | // whatever the claim says now: the binding difference alone supersedes it. |
| 379 | // The installation is quarantined and marked settled: it stays refused for |
| 380 | // execution and new work, because every fresh comparison needs the new |
| 381 | // binding, and the migration census at root's next start still inspects it, |
| 382 | // so cleanup and migration startup are not blocked (fix round 1, finding 1). |
| 383 | // applied says whether root's interrupted commit had already landed the |
| 384 | // intent in the inventory; the guard then records the installation that |
| 385 | // commit would have recorded, so guard and inventory agree. A settlement |
| 386 | // failure is logged and leaves the intent pending, as any recovery failure |
| 387 | // does. It never consults InventoryActor, so a quarantined box settles its |
| 388 | // own leftovers. |
| 389 | // |
| 390 | // Cold-handover barriers (roster binding B3) are not pending installations |
| 391 | // and are not settled here: RecoverPending's cold handover recovery settles |
| 392 | // them (hostbox ColdHandover.Obsolete, hostguard |
| 393 | // SettleObsoleteColdHandover) with the same startup binding. |
| 394 | func (h ClaimRootHTTP) settleObsoleteIntent(ctx context.Context, host string, raw []byte, kind, stamped string, applied func(context.Context, hostguard.RootInstallation) (bool, error)) { |
| 395 | err := h.Stop.Coordinator.Guard.SettleObsoleteRootInventory(ctx, host, raw, applied) |
| 396 | log.Printf("instance-control: %s: obsolete pending %s intent: stamped %s current %s", host, kind, stamped, h.startupBinding) |
| 397 | if err != nil { |
| 398 | log.Printf("instance-control: %s: obsolete pending %s intent not settled: %v", host, kind, err) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // inventoryIntentApplied reports whether q, an interrupted inventory write, |
| 403 | // already landed in the inventory: the pending installation's inventory is |
| 404 | // at q's revision and names q as its last request, the test the pending |
| 405 | // replay makes (commitRootInventoryPending's applied). |
| 406 | func (h ClaimRootHTTP) inventoryIntentApplied(q hostbox.JournalRequest) func(context.Context, hostguard.RootInstallation) (bool, error) { |
| 407 | return func(ctx context.Context, pending hostguard.RootInstallation) (bool, error) { |
| 408 | if h.Journals == nil || h.Journals.Inventory == nil { |
| 409 | return false, hostbox.ErrUnsupported |
| 410 | } |
| 411 | saved, err := h.Journals.Inventory.SnapshotRequest(ctx, hostguard.FenceTarget{Host: pending.Host, Incarnation: pending.Incarnation, Stamp: pending.Stamp}) |
| 412 | if err != nil { |
| 413 | return false, err |
| 414 | } |
| 415 | return saved.Revision == q.Revision+1 && saved.LastRequest == q.ID && saved.Revision == pending.InventoryRevision, nil |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // rebindIntentUnapplied: a recovery rebind's commit writes only the |
| 420 | // successor's inventory, so the predecessor the record keeps still agrees |
| 421 | // with its own inventory; the record keeps it. |
| 422 | func rebindIntentUnapplied(context.Context, hostguard.RootInstallation) (bool, error) { |
| 423 | return false, nil |
| 424 | } |
| 425 | |
| 426 | func (h ClaimRootHTTP) recoverRootInventory(ctx context.Context) error { |
| 427 | authority := h.Stop.Coordinator.Guard.Root |
| 428 | if authority == nil { |
| 429 | return nil |
| 430 | } |
| 431 | pending, err := authority.PendingInstallations() |
| 432 | if err != nil { |
| 433 | return err |
| 434 | } |
| 435 | for _, raw := range pending { |
| 436 | var recovery recoveryRebindIntent |
| 437 | if err = json.Unmarshal(raw, &recovery); err != nil { |
| 438 | return err |
| 439 | } |
| 440 | if recovery.Recovery != nil { |
| 441 | if next := recovery.Recovery.Next; h.obsoleteIntent(next.DirectoryRevision) { |
| 442 | h.settleObsoleteIntent(ctx, next.Host, raw, "rebind", next.DirectoryRevision, rebindIntentUnapplied) |
| 443 | continue |
| 444 | } |
| 445 | if err = h.rebindRecoveryInventory(ctx, *recovery.Recovery); err != nil { |
| 446 | n := recovery.Recovery.Next |
| 447 | if errors.Is(err, hostbox.ErrChanged) { |
| 448 | err = errors.Join(err, h.Stop.Coordinator.Guard.AbandonRootInventory(ctx, n.Host, raw, func(c *hostclaim.Claim) bool { |
| 449 | fresh, e := hostclaim.Read(ctx, h.Stop.Coordinator.Evidence, n.Host) |
| 450 | return e == nil && fresh.Claim != nil && reflect.DeepEqual(c, fresh.Claim) && fresh.ETag != n.ETag |
| 451 | }, rebindIntentUnapplied)) |
| 452 | } |
| 453 | log.Printf("instance-control: %s: %v", n.Host, err) |
| 454 | } |
| 455 | continue |
| 456 | } |
| 457 | var q hostbox.JournalRequest |
| 458 | if err = json.Unmarshal(raw, &q); err != nil { |
| 459 | return err |
| 460 | } |
| 461 | if settled, settleErr := h.settleTimerInventory(ctx, q, raw); settled { |
| 462 | continue |
| 463 | } else if settleErr != nil { |
| 464 | return settleErr |
| 465 | } |
| 466 | if h.obsoleteIntent(q.Inventory.DirectoryRevision) { |
| 467 | h.settleObsoleteIntent(ctx, q.Target.Host, raw, "inventory", q.Inventory.DirectoryRevision, h.inventoryIntentApplied(q)) |
| 468 | continue |
| 469 | } |
| 470 | if _, err = h.withRootInventoryHistory(ctx, q, true); err != nil { |
| 471 | if errors.Is(err, hostbox.ErrChanged) || errors.Is(err, hostbox.ErrJournalUncertain) { |
| 472 | err = errors.Join(err, h.Stop.Coordinator.Guard.AbandonRootInventory(ctx, q.Target.Host, raw, func(c *hostclaim.Claim) bool { |
| 473 | fresh, readErr := hostclaim.Read(ctx, h.Stop.Coordinator.Evidence, q.Target.Host) |
| 474 | if readErr != nil || fresh.Claim == nil || !reflect.DeepEqual(c, fresh.Claim) { |
| 475 | return false |
| 476 | } |
| 477 | _, recordETag, recordErr := h.Stop.Coordinator.Evidence.Get(ctx, q.RecordKey) |
| 478 | return recordErr == nil && recordETag != q.RecordETag || fresh.ETag != q.Target.ClaimETag || c.Holder == nil || c.Holder.Incarnation != q.Target.Incarnation || c.Holder.Stamp != q.Target.Stamp || c.Holder.Session != q.Target.Session || c.Op.ID != q.Target.Op || c.Op.Phase != q.Target.Phase || errors.Is(err, hostbox.ErrJournalUncertain) |
| 479 | }, h.inventoryIntentApplied(q))) |
| 480 | } |
| 481 | log.Printf("instance-control: %s: %v", q.Target.Host, err) |
| 482 | } |
| 483 | } |
| 484 | return nil |
| 485 | } |
| 486 | |