Skip to content

Commit

Permalink
Added PID logic to better home in on a needed addjustment to sync min…
Browse files Browse the repository at this point in the history
…utes between leaders
  • Loading branch information
PaulSnow committed Aug 7, 2019
1 parent bf3b6ae commit 8807a7d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
8 changes: 0 additions & 8 deletions engine/timer.go
Expand Up @@ -5,8 +5,6 @@
package engine

import (
"fmt"
"os"
"time"

"github.com/FactomProject/factomd/state"
Expand Down Expand Up @@ -52,14 +50,8 @@ func Timer(si interfaces.IState) {

// Delay some number of milliseconds.
time.Sleep(s.GetTimeOffset())
last := s.SyncTick
s.SyncTick = time.Now()

_, _ = fmt.Fprintf(os.Stderr, "%20s Time : %s diff %10.2f\n",
s.FactomNodeName,
s.SyncTick.String(),
float64(s.SyncTick.Sub(last)/1000000000))

s.TickerQueue() <- i

period = int64(s.GetDirectoryBlockInSeconds()) * billion
Expand Down
4 changes: 4 additions & 0 deletions state/state.go
Expand Up @@ -172,9 +172,13 @@ type State struct {
timerMsgQueue chan interfaces.IMsg
TimeOffset time.Duration
MaxTimeOffset interfaces.Timestamp

SyncError time.Duration
SyncIntegral time.Duration
SyncStart time.Time
SyncEnd time.Time
SyncTick time.Time
SyncEOMIssue time.Time
SyncExtraTick int

networkOutMsgQueue NetOutMsgQueue
Expand Down
17 changes: 15 additions & 2 deletions state/stateConsensus.go
Expand Up @@ -1500,6 +1500,8 @@ func (s *State) LeaderExecuteEOM(m interfaces.IMsg) {
ack := s.NewAck(m, nil).(*messages.Ack) // LeaderExecuteEOM()
eom.SetLocal(false)

s.SyncEOMIssue = time.Now()

if fix {
s.LogMessage("executeMsg", "fixed EOM", eom)
s.LogMessage("executeMsg", "matching ACK", ack)
Expand Down Expand Up @@ -1963,8 +1965,19 @@ func (s *State) ProcessEOM(dbheight uint32, msg interfaces.IMsg) bool {

s.SyncEnd = time.Now()

took := s.SyncTick.Sub(s.SyncStart)
s.TimeOffset = time.Duration(took.Nanoseconds() / 4)
KP := .20
KI := .01
KD := .80

//
SError := -s.SyncEnd.Sub(s.SyncStart)

s.SyncIntegral = s.SyncIntegral + SError
derivative := SError - s.SyncError
output := KP*float64(SError) + KI*float64(s.SyncIntegral) + KD*float64(derivative) + float64(time.Duration(100*time.Millisecond))
s.SyncError = SError

s.TimeOffset = time.Duration(output)

s.SendHeartBeat() // Only do this once per minute
s.LogPrintf("dbsig-eom", "ProcessEOM complete for %d", e.Minute)
Expand Down

0 comments on commit 8807a7d

Please sign in to comment.