| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostbox" |
| 7 | "github.com/carlosframework/platform/internal/hostclaim" |
| 8 | execlease "github.com/carlosframework/platform/internal/hostexec" |
| 9 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 10 | "github.com/carlosframework/platform/internal/rootstate" |
| 11 | ) |
| 12 | |
| 13 | func rootExecutionRead(ctx context.Context, c *hostclaim.Claim) (execlease.ExecutionRecord, error) { |
| 14 | return readRootExecution(ctx, nil, execlease.SystemClock{}, c) |
| 15 | } |
| 16 | |
| 17 | func (h *ClaimLaunchRoot) readExecution(ctx context.Context, c *hostclaim.Claim) (execlease.ExecutionRecord, error) { |
| 18 | return readRootExecution(ctx, h.executionState, h.Clock, c) |
| 19 | } |
| 20 | |
| 21 | func readRootExecution(ctx context.Context, store rootstate.Store, clock execlease.Clock, c *hostclaim.Claim) (execlease.ExecutionRecord, error) { |
| 22 | if c == nil || c.Holder == nil { |
| 23 | return execlease.ExecutionRecord{}, execlease.ErrState |
| 24 | } |
| 25 | if store == nil { |
| 26 | var err error |
| 27 | store, err = rootstate.Open(rootparticipation.Namespace, false, false) |
| 28 | if err != nil { |
| 29 | return execlease.ExecutionRecord{}, err |
| 30 | } |
| 31 | defer store.Close() |
| 32 | } |
| 33 | q := execlease.ExecutionRequest{Host: c.Host, Incarnation: c.Holder.Incarnation, Stamp: c.Holder.Stamp, Session: c.Holder.Session, Op: c.Op.ID} |
| 34 | return execlease.ReadRoot(ctx, store, clock, q, c.Op.Kind == hostclaim.KindServing) |
| 35 | } |
| 36 | |
| 37 | func (p *productionClaimLocal) checkRootParticipation(ctx context.Context, actor hostbox.Actor, c *hostclaim.Claim) error { |
| 38 | read := p.executionRead |
| 39 | if read == nil { |
| 40 | read = rootExecutionRead |
| 41 | } |
| 42 | r, err := read(ctx, c) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | if c == nil || c.Holder == nil { |
| 47 | return execlease.ErrState |
| 48 | } |
| 49 | if c.Op.Kind != hostclaim.KindServing && (r.Authority.Box != actor.Box || r.Authority.Enrollment != actor.Enrollment) { |
| 50 | return execlease.ErrLost |
| 51 | } |
| 52 | return nil |
| 53 | } |
| 54 | |