|
@@ -120,6 +120,17 @@ type Device struct {
|
|
|
inflightTimeout time.Duration
|
|
inflightTimeout time.Duration
|
|
|
inflightKind jobKind
|
|
inflightKind jobKind
|
|
|
controlWindowUntil time.Time
|
|
controlWindowUntil time.Time
|
|
|
|
|
+ jobSeq uint64
|
|
|
|
|
+ portGen uint64
|
|
|
|
|
+ currentInflight inflightIdent
|
|
|
|
|
+ pendingResume *pendingResumeState
|
|
|
|
|
+ pendingSafeJob *pendingSafeState
|
|
|
|
|
+ cmdSeq uint64
|
|
|
|
|
+ lastHWHeartbeat time.Time
|
|
|
|
|
+ hbDeliveryState hthv1.ActionDispatchState
|
|
|
|
|
+ safetyContract safety.EffectiveContract
|
|
|
|
|
+ safetyErr error
|
|
|
|
|
+ workerCtx context.Context
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type applyJobState struct {
|
|
type applyJobState struct {
|
|
@@ -172,23 +183,32 @@ func New(p Params) *Device {
|
|
|
slewCredit: map[string]float64{},
|
|
slewCredit: map[string]float64{},
|
|
|
controlMode: hthv1.SafetyControlMode_SAFETY_CONTROL_MODE_NORMAL,
|
|
controlMode: hthv1.SafetyControlMode_SAFETY_CONTROL_MODE_NORMAL,
|
|
|
}
|
|
}
|
|
|
|
|
+ if p.Config != nil {
|
|
|
|
|
+ c, err := safety.BuildEffectiveSafetyContract(p.Profile, p.Config.Safety)
|
|
|
|
|
+ d.safetyContract = c
|
|
|
|
|
+ d.safetyErr = err
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ d.caps = overlaySafetyDefaults(d.caps, c)
|
|
|
|
|
+ d.caps.CapabilityHash = caphash.HashCanonicalCapabilities(d.caps)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
d.hub.SetIDAlloc(d.store.NextEventID)
|
|
d.hub.SetIDAlloc(d.store.NextEventID)
|
|
|
d.book.SetHook(d.publishFaultEvent)
|
|
d.book.SetHook(d.publishFaultEvent)
|
|
|
- if p.Config.Safety.Fan != nil || p.Config.Safety.PTC != nil || p.Config.Safety.DoorClosePercent != nil {
|
|
|
|
|
- _ = safety.SafeSetAll(p.Profile, p.Config.Safety.Fan, p.Config.Safety.PTC, p.Config.Safety.DoorClosePercent)
|
|
|
|
|
- }
|
|
|
|
|
return d
|
|
return d
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (d *Device) ID() string { return d.id }
|
|
func (d *Device) ID() string { return d.id }
|
|
|
|
|
|
|
|
func (d *Device) Start(parent context.Context) {
|
|
func (d *Device) Start(parent context.Context) {
|
|
|
- _ = parent
|
|
|
|
|
- actorCtx, actorCancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
+ if parent == nil {
|
|
|
|
|
+ parent = context.Background()
|
|
|
|
|
+ }
|
|
|
|
|
+ actorCtx, actorCancel := context.WithCancel(parent)
|
|
|
workerCtx, workerCancel := context.WithCancel(context.Background())
|
|
workerCtx, workerCancel := context.WithCancel(context.Background())
|
|
|
d.cancel = actorCancel
|
|
d.cancel = actorCancel
|
|
|
d.workerCancel = workerCancel
|
|
d.workerCancel = workerCancel
|
|
|
d.runCtx = actorCtx
|
|
d.runCtx = actorCtx
|
|
|
|
|
+ d.workerCtx = workerCtx
|
|
|
d.wg.Add(2)
|
|
d.wg.Add(2)
|
|
|
go func() {
|
|
go func() {
|
|
|
defer d.wg.Done()
|
|
defer d.wg.Done()
|
|
@@ -205,8 +225,34 @@ func (d *Device) Stop() {
|
|
|
if d.cancel != nil {
|
|
if d.cancel != nil {
|
|
|
d.cancel()
|
|
d.cancel()
|
|
|
}
|
|
}
|
|
|
- d.wg.Wait()
|
|
|
|
|
- _ = d.backend.Close()
|
|
|
|
|
|
|
+ deadline := d.cfg.ShutdownSafeTimeout()
|
|
|
|
|
+ if deadline < 2*time.Second {
|
|
|
|
|
+ deadline = 2 * time.Second
|
|
|
|
|
+ }
|
|
|
|
|
+ done := make(chan struct{})
|
|
|
|
|
+ go func() {
|
|
|
|
|
+ d.wg.Wait()
|
|
|
|
|
+ close(done)
|
|
|
|
|
+ }()
|
|
|
|
|
+ select {
|
|
|
|
|
+ case <-done:
|
|
|
|
|
+ _ = d.backend.Close()
|
|
|
|
|
+ case <-time.After(deadline):
|
|
|
|
|
+ _ = d.backend.Close()
|
|
|
|
|
+ if d.workerCancel != nil {
|
|
|
|
|
+ d.workerCancel()
|
|
|
|
|
+ }
|
|
|
|
|
+ join := deadline
|
|
|
|
|
+ if join > 3*time.Second {
|
|
|
|
|
+ join = 3 * time.Second
|
|
|
|
|
+ }
|
|
|
|
|
+ select {
|
|
|
|
|
+ case <-done:
|
|
|
|
|
+ case <-time.After(join):
|
|
|
|
|
+ d.book.Raise(d.clk.Now(), hthv1.FaultType_FAULT_TYPE_SERIAL_DISCONNECTED, d.id, "",
|
|
|
|
|
+ "worker join exceeded deadline after Close; process should exit for systemd restart", "", nil)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -280,7 +326,7 @@ func (d *Device) loop(ctx context.Context) {
|
|
|
}
|
|
}
|
|
|
case <-temp.C:
|
|
case <-temp.C:
|
|
|
if d.backend.PollMode() == backend.PollUART && d.connected && d.startupDone && !d.serialInflight() {
|
|
if d.backend.PollMode() == backend.PollUART && d.connected && d.startupDone && !d.serialInflight() {
|
|
|
- if d.latchedSafe() || d.pendingSafe || len(d.pendingApply) > 0 || d.controlWindowOpen(d.clk.Now()) {
|
|
|
|
|
|
|
+ if d.pendingSafe || d.pendingSafeJob != nil || d.pendingResume != nil || len(d.pendingApply) > 0 || d.controlWindowOpen(d.clk.Now()) {
|
|
|
d.pendingTemp = true
|
|
d.pendingTemp = true
|
|
|
break
|
|
break
|
|
|
}
|
|
}
|
|
@@ -374,6 +420,7 @@ func (d *Device) onDrop(ctx context.Context, cause error) {
|
|
|
d.haveCont = false
|
|
d.haveCont = false
|
|
|
d.haveDispatch = false
|
|
d.haveDispatch = false
|
|
|
d.baselineReady = false
|
|
d.baselineReady = false
|
|
|
|
|
+ d.portGen++
|
|
|
d.slewCredit = map[string]float64{}
|
|
d.slewCredit = map[string]float64{}
|
|
|
if d.controlMode == hthv1.SafetyControlMode_SAFETY_CONTROL_MODE_SAFE ||
|
|
if d.controlMode == hthv1.SafetyControlMode_SAFETY_CONTROL_MODE_SAFE ||
|
|
|
d.controlMode == hthv1.SafetyControlMode_SAFETY_CONTROL_MODE_RESUME_PENDING {
|
|
d.controlMode == hthv1.SafetyControlMode_SAFETY_CONTROL_MODE_RESUME_PENDING {
|
|
@@ -381,6 +428,7 @@ func (d *Device) onDrop(ctx context.Context, cause error) {
|
|
|
}
|
|
}
|
|
|
d.endInflight()
|
|
d.endInflight()
|
|
|
d.pendingSafe = false
|
|
d.pendingSafe = false
|
|
|
|
|
+ d.pendingSafeJob = nil
|
|
|
select {
|
|
select {
|
|
|
case <-d.jobs:
|
|
case <-d.jobs:
|
|
|
default:
|
|
default:
|
|
@@ -394,9 +442,13 @@ func (d *Device) onDrop(ctx context.Context, cause error) {
|
|
|
snap := d.store.Commit(func(s *hthv1.Snapshot) {
|
|
snap := d.store.Commit(func(s *hthv1.Snapshot) {
|
|
|
s.Measurements = missingTemps(d.prof, now, "SERIAL_DROP_NOT_REUSING_OLD_SNAPSHOT")
|
|
s.Measurements = missingTemps(d.prof, now, "SERIAL_DROP_NOT_REUSING_OLD_SNAPSHOT")
|
|
|
s.ContinuousTargets = nil
|
|
s.ContinuousTargets = nil
|
|
|
|
|
+ s.LastRequested = nil
|
|
|
|
|
+ s.LastAccepted = nil
|
|
|
|
|
+ s.LastDispatched = nil
|
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
|
s.ActiveFaults = d.book.Active()
|
|
s.ActiveFaults = d.book.Active()
|
|
|
s.DeviceConfirmation = hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN
|
|
s.DeviceConfirmation = hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN
|
|
|
|
|
+ d.annotateSnapshot(s)
|
|
|
})
|
|
})
|
|
|
d.hub.PublishSnapshot(snap, now)
|
|
d.hub.PublishSnapshot(snap, now)
|
|
|
d.hub.PublishEvent(&hthv1.Event{
|
|
d.hub.PublishEvent(&hthv1.Event{
|
|
@@ -426,6 +478,10 @@ func (d *Device) failInflight() {
|
|
|
d.pendingApply = nil
|
|
d.pendingApply = nil
|
|
|
d.pendingHB = false
|
|
d.pendingHB = false
|
|
|
d.pendingSafe = false
|
|
d.pendingSafe = false
|
|
|
|
|
+ d.pendingSafeJob = nil
|
|
|
|
|
+ if d.pendingResume != nil {
|
|
|
|
|
+ d.abortPendingResume("serial drop while queued", hthv1.ConfirmationStatus_CONFIRMATION_STATUS_OUTCOME_UNKNOWN, hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_OUTCOME_UNKNOWN)
|
|
|
|
|
+ }
|
|
|
for _, ch := range d.inflightApply {
|
|
for _, ch := range d.inflightApply {
|
|
|
replyNonblock(ch, unknown("serial drop while inflight"))
|
|
replyNonblock(ch, unknown("serial drop while inflight"))
|
|
|
}
|
|
}
|
|
@@ -434,11 +490,19 @@ func (d *Device) failInflight() {
|
|
|
|
|
|
|
|
func (d *Device) handle(ctx context.Context, r req) {
|
|
func (d *Device) handle(ctx context.Context, r req) {
|
|
|
if d.draining && (r.kind == opAcquire || r.kind == opApply || r.kind == opResume) {
|
|
if d.draining && (r.kind == opAcquire || r.kind == opApply || r.kind == opResume) {
|
|
|
|
|
+ reason := hthv1.RejectReason_REJECT_REASON_DEVICE_NOT_READY
|
|
|
|
|
+ st := hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_REJECTED
|
|
|
|
|
+ conf := hthv1.ConfirmationStatus_CONFIRMATION_STATUS_REJECTED
|
|
|
|
|
+ if r.kind == opApply {
|
|
|
|
|
+ reason = hthv1.RejectReason_REJECT_REASON_SUPERSEDED_BY_SHUTDOWN
|
|
|
|
|
+ st = hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_SUPERSEDED
|
|
|
|
|
+ conf = hthv1.ConfirmationStatus_CONFIRMATION_STATUS_NOT_DELIVERED
|
|
|
|
|
+ }
|
|
|
r.reply <- res{action: &hthv1.ActionResult{
|
|
r.reply <- res{action: &hthv1.ActionResult{
|
|
|
CommandId: r.batch.GetCommandId(), Accepted: false,
|
|
CommandId: r.batch.GetCommandId(), Accepted: false,
|
|
|
- RejectReason: hthv1.RejectReason_REJECT_REASON_DEVICE_NOT_READY,
|
|
|
|
|
- Confirmation: hthv1.ConfirmationStatus_CONFIRMATION_STATUS_REJECTED,
|
|
|
|
|
- DispatchState: hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_REJECTED,
|
|
|
|
|
|
|
+ RejectReason: reason,
|
|
|
|
|
+ Confirmation: conf,
|
|
|
|
|
+ DispatchState: st,
|
|
|
Message: "daemon draining", DeviceEpoch: d.store.Epoch(),
|
|
Message: "daemon draining", DeviceEpoch: d.store.Epoch(),
|
|
|
}}
|
|
}}
|
|
|
return
|
|
return
|
|
@@ -562,6 +626,16 @@ func (d *Device) assembleResult(batch *hthv1.ActionBatch, validateOnly, resume b
|
|
|
d.book.Raise(now, hthv1.FaultType_FAULT_TYPE_CONTROL_COMMAND_REJECTED, d.id, batch.CommandId, rr.String(), "", nil)
|
|
d.book.Raise(now, hthv1.FaultType_FAULT_TYPE_CONTROL_COMMAND_REJECTED, d.id, batch.CommandId, rr.String(), "", nil)
|
|
|
return res
|
|
return res
|
|
|
}
|
|
}
|
|
|
|
|
+ if !d.cfg.AllowUnboundActions && !resume {
|
|
|
|
|
+ if batch.BasedOnSnapshotSeq == 0 || batch.ExpectedControlRevision == 0 {
|
|
|
|
|
+ res.RejectReason = hthv1.RejectReason_REJECT_REASON_STALE_SNAPSHOT
|
|
|
|
|
+ res.Accepted = false
|
|
|
|
|
+ res.Confirmation = hthv1.ConfirmationStatus_CONFIRMATION_STATUS_REJECTED
|
|
|
|
|
+ res.DispatchState = hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_REJECTED
|
|
|
|
|
+ res.Message = "ordinary ApplyAction requires based_on_snapshot_seq and expected_control_revision"
|
|
|
|
|
+ return res
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
if !validateOnly && d.latchedSafe() && !resume {
|
|
if !validateOnly && d.latchedSafe() && !resume {
|
|
|
res.RejectReason = hthv1.RejectReason_REJECT_REASON_SAFE_MODE_ACTIVE
|
|
res.RejectReason = hthv1.RejectReason_REJECT_REASON_SAFE_MODE_ACTIVE
|
|
@@ -654,17 +728,7 @@ func (d *Device) beginApply(ctx context.Context, r req) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
if d.serialInflight() {
|
|
if d.serialInflight() {
|
|
|
- if len(d.pendingApply) >= 32 {
|
|
|
|
|
- replyNonblock(r.reply, res{action: &hthv1.ActionResult{
|
|
|
|
|
- CommandId: r.batch.GetCommandId(), Accepted: false,
|
|
|
|
|
- RejectReason: hthv1.RejectReason_REJECT_REASON_QUEUE_FULL,
|
|
|
|
|
- Confirmation: hthv1.ConfirmationStatus_CONFIRMATION_STATUS_REJECTED,
|
|
|
|
|
- Message: "apply queue full while serial transaction in flight",
|
|
|
|
|
- DeviceEpoch: d.store.Epoch(),
|
|
|
|
|
- }})
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- d.pendingApply = append(d.pendingApply, r)
|
|
|
|
|
|
|
+ d.enqueueLatestApply(r)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
assembled := safety.Assemble(d.lastCont, r.batch, d.prof)
|
|
assembled := safety.Assemble(d.lastCont, r.batch, d.prof)
|
|
@@ -810,7 +874,7 @@ func (d *Device) recordAction(res *hthv1.ActionResult) {
|
|
|
if res.Dispatched != nil {
|
|
if res.Dispatched != nil {
|
|
|
s.LastDispatched = res.Dispatched
|
|
s.LastDispatched = res.Dispatched
|
|
|
}
|
|
}
|
|
|
- s.ContinuousTargets = commanded(d.lastCont)
|
|
|
|
|
|
|
+ s.ContinuousTargets = d.commandedTargets()
|
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
|
s.ActiveFaults = d.book.Active()
|
|
s.ActiveFaults = d.book.Active()
|
|
|
s.DeviceConfirmation = res.Confirmation
|
|
s.DeviceConfirmation = res.Confirmation
|
|
@@ -819,7 +883,7 @@ func (d *Device) recordAction(res *hthv1.ActionResult) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (d *Device) heartbeat(ctx context.Context) {
|
|
func (d *Device) heartbeat(ctx context.Context) {
|
|
|
- if !d.connected || d.backend.PollMode() != backend.PollUART {
|
|
|
|
|
|
|
+ if d.draining || !d.connected || d.backend.PollMode() != backend.PollUART {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
if !d.startupDone || !d.baselineReady || !d.haveCont {
|
|
if !d.startupDone || !d.baselineReady || !d.haveCont {
|
|
@@ -840,10 +904,14 @@ func (d *Device) heartbeat(ctx context.Context) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (d *Device) safeFrame() protocol.SetAll {
|
|
func (d *Device) safeFrame() protocol.SetAll {
|
|
|
|
|
+ if d.safetyErr == nil && (d.safetyContract.Frame != protocol.SetAll{} || d.safetyContract.ByID != nil) {
|
|
|
|
|
+ return d.safetyContract.Frame
|
|
|
|
|
+ }
|
|
|
return safety.SafeSetAll(d.prof, d.cfg.Safety.Fan, d.cfg.Safety.PTC, d.cfg.Safety.DoorClosePercent)
|
|
return safety.SafeSetAll(d.prof, d.cfg.Safety.Fan, d.cfg.Safety.PTC, d.cfg.Safety.DoorClosePercent)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (d *Device) checkLease(ctx context.Context) {
|
|
func (d *Device) checkLease(ctx context.Context) {
|
|
|
|
|
+ d.expirePendingResume(ctx)
|
|
|
if expired := d.lease.ExpireIfNeeded(d.clk.Now()); expired != nil {
|
|
if expired := d.lease.ExpireIfNeeded(d.clk.Now()); expired != nil {
|
|
|
d.book.Raise(d.clk.Now(), hthv1.FaultType_FAULT_TYPE_CONTROL_LEASE_EXPIRED, d.id, "", "lease expired", "", nil)
|
|
d.book.Raise(d.clk.Now(), hthv1.FaultType_FAULT_TYPE_CONTROL_LEASE_EXPIRED, d.id, "", "lease expired", "", nil)
|
|
|
d.publishEvent(hthv1.EventKind_EVENT_KIND_LEASE_EXPIRED, "expired")
|
|
d.publishEvent(hthv1.EventKind_EVENT_KIND_LEASE_EXPIRED, "expired")
|
|
@@ -947,7 +1015,7 @@ func (d *Device) applyTempMeas(rep *backend.TempReport) {
|
|
|
d.store.Commit(func(s *hthv1.Snapshot) {
|
|
d.store.Commit(func(s *hthv1.Snapshot) {
|
|
|
s.Measurements = mergeMeas(s.Measurements, meas)
|
|
s.Measurements = mergeMeas(s.Measurements, meas)
|
|
|
s.CaptureWindow = window(rep.Window)
|
|
s.CaptureWindow = window(rep.Window)
|
|
|
- s.ContinuousTargets = commanded(d.lastCont)
|
|
|
|
|
|
|
+ s.ContinuousTargets = d.commandedTargets()
|
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
|
s.ActiveFaults = d.book.Active()
|
|
s.ActiveFaults = d.book.Active()
|
|
|
})
|
|
})
|
|
@@ -959,7 +1027,7 @@ func (d *Device) applyMotorMeas(rep *backend.MotorReport) {
|
|
|
meas := motorMeasurements(rep.Status, now)
|
|
meas := motorMeasurements(rep.Status, now)
|
|
|
d.store.Commit(func(s *hthv1.Snapshot) {
|
|
d.store.Commit(func(s *hthv1.Snapshot) {
|
|
|
s.Measurements = mergeMeas(s.Measurements, meas)
|
|
s.Measurements = mergeMeas(s.Measurements, meas)
|
|
|
- s.ContinuousTargets = commanded(d.lastCont)
|
|
|
|
|
|
|
+ s.ContinuousTargets = d.commandedTargets()
|
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
|
s.ActiveFaults = d.book.Active()
|
|
s.ActiveFaults = d.book.Active()
|
|
|
if !d.startupDone {
|
|
if !d.startupDone {
|
|
@@ -1020,16 +1088,19 @@ func (d *Device) replayAdvance(ctx context.Context, rp backend.Replayer) error {
|
|
|
rec := capture.Record{Snapshot: item.Snapshot, Kind: "snapshot", EventID: item.EventID, SourceEventID: item.EventID}
|
|
rec := capture.Record{Snapshot: item.Snapshot, Kind: "snapshot", EventID: item.EventID, SourceEventID: item.EventID}
|
|
|
if snap, err := capture.DecodeSnapshot(rec); err == nil {
|
|
if snap, err := capture.DecodeSnapshot(rec); err == nil {
|
|
|
src := item.EventID
|
|
src := item.EventID
|
|
|
|
|
+ srcDev := snap.GetDeviceId()
|
|
|
|
|
+ srcDaemon := snap.GetDaemonInstanceId()
|
|
|
out := d.store.ReplaceFromReplay(snap)
|
|
out := d.store.ReplaceFromReplay(snap)
|
|
|
d.lastStepSnap = out
|
|
d.lastStepSnap = out
|
|
|
d.hub.PublishEvent(&hthv1.Event{
|
|
d.hub.PublishEvent(&hthv1.Event{
|
|
|
EventId: out.EventId,
|
|
EventId: out.EventId,
|
|
|
SourceEventId: src,
|
|
SourceEventId: src,
|
|
|
- SourceDeviceId: snap.GetDeviceId(),
|
|
|
|
|
- SourceDaemonInstanceId: snap.GetDaemonInstanceId(),
|
|
|
|
|
|
|
+ SourceDeviceId: srcDev,
|
|
|
|
|
+ SourceDaemonInstanceId: srcDaemon,
|
|
|
|
|
+ SourceCaptureId: d.replayCaptureID(),
|
|
|
Kind: hthv1.EventKind_EVENT_KIND_SNAPSHOT,
|
|
Kind: hthv1.EventKind_EVENT_KIND_SNAPSHOT,
|
|
|
WallTime: timestamppb.New(now),
|
|
WallTime: timestamppb.New(now),
|
|
|
- DeviceId: out.GetDeviceId(),
|
|
|
|
|
|
|
+ DeviceId: d.id,
|
|
|
Snapshot: out,
|
|
Snapshot: out,
|
|
|
})
|
|
})
|
|
|
} else {
|
|
} else {
|
|
@@ -1052,9 +1123,19 @@ func (d *Device) replayAdvance(ctx context.Context, rp backend.Replayer) error {
|
|
|
if ev.SourceEventId != 0 {
|
|
if ev.SourceEventId != 0 {
|
|
|
src = ev.SourceEventId
|
|
src = ev.SourceEventId
|
|
|
}
|
|
}
|
|
|
|
|
+ srcDev := ev.SourceDeviceId
|
|
|
|
|
+ if srcDev == "" {
|
|
|
|
|
+ srcDev = ev.DeviceId
|
|
|
|
|
+ }
|
|
|
|
|
+ srcDaemon := ev.SourceDaemonInstanceId
|
|
|
|
|
+ srcCap := ev.SourceCaptureId
|
|
|
|
|
+ if srcCap == "" {
|
|
|
|
|
+ srcCap = d.replayCaptureID()
|
|
|
|
|
+ }
|
|
|
ev.SourceEventId = src
|
|
ev.SourceEventId = src
|
|
|
- ev.SourceDeviceId = ev.DeviceId
|
|
|
|
|
- ev.SourceDaemonInstanceId = d.daemon
|
|
|
|
|
|
|
+ ev.SourceDeviceId = srcDev
|
|
|
|
|
+ ev.SourceDaemonInstanceId = srcDaemon
|
|
|
|
|
+ ev.SourceCaptureId = srcCap
|
|
|
ev.EventId = d.store.NextEventID()
|
|
ev.EventId = d.store.NextEventID()
|
|
|
if ev.EventId == 0 {
|
|
if ev.EventId == 0 {
|
|
|
ev.EventId = 1
|
|
ev.EventId = 1
|
|
@@ -1067,6 +1148,13 @@ func (d *Device) replayAdvance(ctx context.Context, rp backend.Replayer) error {
|
|
|
return fmt.Errorf("capture: record missing snapshot and event")
|
|
return fmt.Errorf("capture: record missing snapshot and event")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (d *Device) replayCaptureID() string {
|
|
|
|
|
+ if d.cfg != nil && d.cfg.Replay.Path != "" {
|
|
|
|
|
+ return d.cfg.Replay.Path
|
|
|
|
|
+ }
|
|
|
|
|
+ return ""
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func (d *Device) publishNow() {
|
|
func (d *Device) publishNow() {
|
|
|
now := d.clk.Now()
|
|
now := d.clk.Now()
|
|
|
stale := false
|
|
stale := false
|
|
@@ -1077,7 +1165,7 @@ func (d *Device) publishNow() {
|
|
|
d.book.Clear(hthv1.FaultType_FAULT_TYPE_TELEMETRY_STALE, d.id, "", now)
|
|
d.book.Clear(hthv1.FaultType_FAULT_TYPE_TELEMETRY_STALE, d.id, "", now)
|
|
|
}
|
|
}
|
|
|
snap := d.store.Commit(func(s *hthv1.Snapshot) {
|
|
snap := d.store.Commit(func(s *hthv1.Snapshot) {
|
|
|
- s.ContinuousTargets = commanded(d.lastCont)
|
|
|
|
|
|
|
+ s.ContinuousTargets = d.commandedTargets()
|
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
s.Lease = d.lease.Current().Summary(now)
|
|
|
s.ActiveFaults = d.book.Active()
|
|
s.ActiveFaults = d.book.Active()
|
|
|
d.refreshMeasurementAge(s, now, stale)
|
|
d.refreshMeasurementAge(s, now, stale)
|
|
@@ -1408,6 +1496,125 @@ func actionRecord(batch *hthv1.ActionBatch, now time.Time, conf hthv1.Confirmati
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func overlaySafetyDefaults(c *hthv1.Capabilities, contract safety.EffectiveContract) *hthv1.Capabilities {
|
|
|
|
|
+ if c == nil {
|
|
|
|
|
+ return c
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, a := range c.Actuators {
|
|
|
|
|
+ if a == nil {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ if v, ok := contract.ByID[a.Id]; ok {
|
|
|
|
|
+ a.SafetyDefault = v
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return c
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Device) commandedTargets() []*hthv1.CommandedActuator {
|
|
|
|
|
+ if !d.haveCont || !d.baselineReady {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ return commandedWithEpoch(d.lastCont, 0)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func commandedWithEpoch(s protocol.SetAll, epoch uint64) []*hthv1.CommandedActuator {
|
|
|
|
|
+ out := commanded(s)
|
|
|
|
|
+ for _, a := range out {
|
|
|
|
|
+ if a == nil {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ a.TargetEpoch = epoch
|
|
|
|
|
+ a.TargetSource = "commanded"
|
|
|
|
|
+ a.TargetQuality = hthv1.DataQuality_DATA_QUALITY_NOT_SUPPORTED
|
|
|
|
|
+ a.ActualReason = "COMMANDED_NOT_ACTUAL"
|
|
|
|
|
+ }
|
|
|
|
|
+ return out
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Device) publishActionResult(res *hthv1.ActionResult) {
|
|
|
|
|
+ if res == nil {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ now := d.clk.Now()
|
|
|
|
|
+ if res.DeviceEpoch == 0 {
|
|
|
|
|
+ res.DeviceEpoch = d.store.Epoch()
|
|
|
|
|
+ }
|
|
|
|
|
+ if res.PortGeneration == 0 {
|
|
|
|
|
+ res.PortGeneration = d.portGen
|
|
|
|
|
+ }
|
|
|
|
|
+ d.hub.PublishEvent(&hthv1.Event{
|
|
|
|
|
+ EventId: d.store.NextEventID(), Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT,
|
|
|
|
|
+ WallTime: timestamppb.New(now), MonotonicNs: uint64(now.UnixNano()),
|
|
|
|
|
+ DeviceId: d.id, Message: res.Message, ActionResult: proto.Clone(res).(*hthv1.ActionResult),
|
|
|
|
|
+ PortGeneration: d.portGen,
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Device) enqueueLatestApply(r req) {
|
|
|
|
|
+ if d.draining {
|
|
|
|
|
+ d.replySuperseded(r, hthv1.RejectReason_REJECT_REASON_SUPERSEDED_BY_SHUTDOWN, "SUPERSEDED_BY_SHUTDOWN")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ leaseID := r.batch.GetLeaseId()
|
|
|
|
|
+ for _, old := range d.pendingApply {
|
|
|
|
|
+ if old.batch != nil && old.batch.GetLeaseId() != leaseID {
|
|
|
|
|
+ replyNonblock(r.reply, res{action: &hthv1.ActionResult{
|
|
|
|
|
+ CommandId: r.batch.GetCommandId(), Accepted: false,
|
|
|
|
|
+ RejectReason: hthv1.RejectReason_REJECT_REASON_LEASE_MISMATCH,
|
|
|
|
|
+ Confirmation: hthv1.ConfirmationStatus_CONFIRMATION_STATUS_REJECTED,
|
|
|
|
|
+ DispatchState: hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_REJECTED,
|
|
|
|
|
+ Message: "pending action belongs to a different lease",
|
|
|
|
|
+ DeviceEpoch: d.store.Epoch(),
|
|
|
|
|
+ }})
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ d.replySuperseded(old, hthv1.RejectReason_REJECT_REASON_SUPERSEDED, "SUPERSEDED by later apply")
|
|
|
|
|
+ }
|
|
|
|
|
+ d.pendingApply = []req{r}
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Device) replySuperseded(r req, reason hthv1.RejectReason, msg string) {
|
|
|
|
|
+ out := &hthv1.ActionResult{
|
|
|
|
|
+ CommandId: r.batch.GetCommandId(), Accepted: false, RejectReason: reason,
|
|
|
|
|
+ Confirmation: hthv1.ConfirmationStatus_CONFIRMATION_STATUS_NOT_DELIVERED,
|
|
|
|
|
+ DispatchState: hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_SUPERSEDED,
|
|
|
|
|
+ Message: msg, DeviceEpoch: d.store.Epoch(), PortGeneration: d.portGen,
|
|
|
|
|
+ ActionOrigin: "apply",
|
|
|
|
|
+ }
|
|
|
|
|
+ d.publishActionResult(out)
|
|
|
|
|
+ replyNonblock(r.reply, res{action: out})
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Device) supersedePendingApplies(msg string) {
|
|
|
|
|
+ reason := hthv1.RejectReason_REJECT_REASON_SUPERSEDED
|
|
|
|
|
+ if msg == "SUPERSEDED_BY_SHUTDOWN" {
|
|
|
|
|
+ reason = hthv1.RejectReason_REJECT_REASON_SUPERSEDED_BY_SHUTDOWN
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, r := range d.pendingApply {
|
|
|
|
|
+ d.replySuperseded(r, reason, msg)
|
|
|
|
|
+ }
|
|
|
|
|
+ d.pendingApply = nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// TestingInjectStaleSerialResult injects a result that must not clear CurrentInflight.
|
|
|
|
|
+func (d *Device) TestingInjectStaleSerialResult(jobID, portGen, epoch uint64, kind int) {
|
|
|
|
|
+ select {
|
|
|
|
|
+ case d.results <- serialResult{id: jobID, portGen: portGen, epoch: epoch, kind: jobKind(kind)}:
|
|
|
|
|
+ default:
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Device) TestingCurrentInflight() (jobID, portGen, epoch uint64, kind int, ok bool) {
|
|
|
|
|
+ inf := d.currentInflight
|
|
|
|
|
+ if inf.id == 0 {
|
|
|
|
|
+ return 0, 0, 0, 0, false
|
|
|
|
|
+ }
|
|
|
|
|
+ return inf.id, inf.portGen, inf.epoch, int(inf.kind), true
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *Device) TestingPortGeneration() uint64 { return d.portGen }
|
|
|
|
|
+
|
|
|
func commanded(s protocol.SetAll) []*hthv1.CommandedActuator {
|
|
func commanded(s protocol.SetAll) []*hthv1.CommandedActuator {
|
|
|
mk := func(id string, v uint8) *hthv1.CommandedActuator {
|
|
mk := func(id string, v uint8) *hthv1.CommandedActuator {
|
|
|
return &hthv1.CommandedActuator{
|
|
return &hthv1.CommandedActuator{
|
|
@@ -1536,22 +1743,39 @@ func buildCaps(cfg *config.Config, p *profile.Profile) *hthv1.Capabilities {
|
|
|
sort.Slice(c.Actuators, func(i, j int) bool { return c.Actuators[i].Id < c.Actuators[j].Id })
|
|
sort.Slice(c.Actuators, func(i, j int) bool { return c.Actuators[i].Id < c.Actuators[j].Id })
|
|
|
tempMS := uint32(cfg.GetTempTimeout(p) / time.Millisecond)
|
|
tempMS := uint32(cfg.GetTempTimeout(p) / time.Millisecond)
|
|
|
motorMS := uint32(cfg.GetMotorTimeout() / time.Millisecond)
|
|
motorMS := uint32(cfg.GetMotorTimeout() / time.Millisecond)
|
|
|
- ctrlMS := uint32(cfg.SetAllTimeout() / time.Millisecond)
|
|
|
|
|
|
|
+ setMS := uint32(cfg.SetAllTimeout() / time.Millisecond)
|
|
|
if tempMS < 1 {
|
|
if tempMS < 1 {
|
|
|
tempMS = 6500
|
|
tempMS = 6500
|
|
|
}
|
|
}
|
|
|
- recRPC := tempMS + ctrlMS + motorMS + 2000
|
|
|
|
|
|
|
+ unpreempt := tempMS
|
|
|
|
|
+ if motorMS > unpreempt {
|
|
|
|
|
+ unpreempt = motorMS
|
|
|
|
|
+ }
|
|
|
|
|
+ if setMS > unpreempt {
|
|
|
|
|
+ unpreempt = setMS
|
|
|
|
|
+ }
|
|
|
|
|
+ ctrlWindow := setMS + 50
|
|
|
|
|
+ schedMargin := uint32(50)
|
|
|
|
|
+ ctrlWorst := unpreempt + ctrlWindow + schedMargin
|
|
|
|
|
+ recRPC := tempMS + ctrlWorst + motorMS + 2000
|
|
|
if recRPC < tempMS+3000 {
|
|
if recRPC < tempMS+3000 {
|
|
|
recRPC = tempMS + 3000
|
|
recRPC = tempMS + 3000
|
|
|
}
|
|
}
|
|
|
c.Timing = &hthv1.TimingCapabilities{
|
|
c.Timing = &hthv1.TimingCapabilities{
|
|
|
TemperatureTransactionWorstCaseMs: tempMS,
|
|
TemperatureTransactionWorstCaseMs: tempMS,
|
|
|
MotorStatusWorstCaseMs: motorMS,
|
|
MotorStatusWorstCaseMs: motorMS,
|
|
|
- ControlDispatchWorstCaseMs: ctrlMS,
|
|
|
|
|
|
|
+ ControlDispatchWorstCaseMs: ctrlWorst,
|
|
|
RecommendedRpcDeadlineMs: recRPC,
|
|
RecommendedRpcDeadlineMs: recRPC,
|
|
|
RecommendedActionTtlMs: recRPC,
|
|
RecommendedActionTtlMs: recRPC,
|
|
|
- MinimumControlPeriodMs: ctrlMS + 50,
|
|
|
|
|
|
|
+ MinimumControlPeriodMs: ctrlWindow,
|
|
|
RecommendedSamplePeriodMs: uint32(p.RecommendedSamplePeriod() / time.Millisecond),
|
|
RecommendedSamplePeriodMs: uint32(p.RecommendedSamplePeriod() / time.Millisecond),
|
|
|
|
|
+ MaxUnpreemptableTransactionMs: unpreempt,
|
|
|
|
|
+ ControlWindowMs: ctrlWindow,
|
|
|
|
|
+ SchedulerMarginMs: schedMargin,
|
|
|
|
|
+ MaxPendingContinuousActions: 1,
|
|
|
|
|
+ }
|
|
|
|
|
+ if contract, err := safety.BuildEffectiveSafetyContract(p, cfg.Safety); err == nil {
|
|
|
|
|
+ c = overlaySafetyDefaults(c, contract)
|
|
|
}
|
|
}
|
|
|
c.CapabilityHash = caphash.HashCanonicalCapabilities(c)
|
|
c.CapabilityHash = caphash.HashCanonicalCapabilities(c)
|
|
|
return c
|
|
return c
|