-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4626 from ElrondNetwork/fix-hb-v2-monitor-after-n…
…odes-restart Heartbeat v2 monitor better filtering
- Loading branch information
Showing
4 changed files
with
259 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package monitor | ||
|
||
import "errors" | ||
|
||
var ( | ||
errEmptyHeartbeatMessagesInstance = errors.New("programming error: empty heartbeatMessages instance") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package monitor | ||
|
||
import ( | ||
"github.com/ElrondNetwork/elrond-go-core/core" | ||
"github.com/ElrondNetwork/elrond-go/heartbeat/data" | ||
) | ||
|
||
type heartbeatMessages struct { | ||
firstActivePid core.PeerID | ||
firstActiveHeartbeat *data.PubKeyHeartbeat | ||
numActivePids uint64 | ||
latestInactivePid core.PeerID | ||
latestInactiveHeartbeat *data.PubKeyHeartbeat | ||
inactivePids []core.PeerID | ||
} | ||
|
||
func newHeartbeatMessages() *heartbeatMessages { | ||
return &heartbeatMessages{} | ||
} | ||
|
||
func (instance *heartbeatMessages) addMessage(pid core.PeerID, message *data.PubKeyHeartbeat) { | ||
if message.IsActive { | ||
instance.handleActiveMessage(pid, message) | ||
return | ||
} | ||
|
||
instance.handleInactiveMessage(pid, message) | ||
} | ||
|
||
func (instance *heartbeatMessages) handleActiveMessage(pid core.PeerID, message *data.PubKeyHeartbeat) { | ||
instance.numActivePids++ | ||
|
||
if len(instance.firstActivePid) == 0 { | ||
instance.firstActivePid = pid | ||
instance.firstActiveHeartbeat = message | ||
return | ||
} | ||
|
||
if string(instance.firstActivePid) > string(pid) { | ||
instance.firstActivePid = pid | ||
instance.firstActiveHeartbeat = message | ||
} | ||
} | ||
|
||
func (instance *heartbeatMessages) handleInactiveMessage(pid core.PeerID, message *data.PubKeyHeartbeat) { | ||
instance.inactivePids = append(instance.inactivePids, pid) | ||
|
||
if instance.latestInactiveHeartbeat == nil { | ||
instance.latestInactivePid = pid | ||
instance.latestInactiveHeartbeat = message | ||
return | ||
} | ||
|
||
if message.TimeStamp.Unix() > instance.latestInactiveHeartbeat.TimeStamp.Unix() { | ||
instance.latestInactivePid = pid | ||
instance.latestInactiveHeartbeat = message | ||
} | ||
} | ||
|
||
func (instance *heartbeatMessages) getHeartbeat() (*data.PubKeyHeartbeat, error) { | ||
if instance.firstActiveHeartbeat != nil { | ||
return instance.firstActiveHeartbeat, nil | ||
} | ||
if instance.latestInactiveHeartbeat != nil { | ||
return instance.latestInactiveHeartbeat, nil | ||
} | ||
return nil, errEmptyHeartbeatMessagesInstance | ||
} | ||
|
||
func (instance *heartbeatMessages) getInactivePids() []core.PeerID { | ||
if instance.firstActiveHeartbeat != nil { | ||
// at least one active heartbeat, return all pids containing inactive messages | ||
return instance.inactivePids | ||
} | ||
|
||
result := make([]core.PeerID, 0, len(instance.inactivePids)) | ||
for _, pid := range instance.inactivePids { | ||
if pid == instance.latestInactivePid { | ||
continue | ||
} | ||
|
||
result = append(result, pid) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.