-
Notifications
You must be signed in to change notification settings - Fork 672
/
health.go
65 lines (55 loc) · 1.98 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"fmt"
"strings"
"github.com/ava-labs/avalanchego/utils/constants"
)
// MinConnectedStake is the minimum percentage of the Primary Network's that
// this node must be connected to to be considered healthy
const MinConnectedStake = .80
func (vm *VM) HealthCheck() (interface{}, error) {
// Returns nil if this node is connected to > alpha percent of the Primary Network's stake
primaryPercentConnected, err := vm.getPercentConnected(constants.PrimaryNetworkID)
if err != nil {
return nil, fmt.Errorf("couldn't get percent connected: %w", err)
}
vm.metrics.percentConnected.Set(primaryPercentConnected)
details := map[string]float64{
"primary-percentConnected": primaryPercentConnected,
}
// TODO: Use alpha from consensus instead of const
var errorReasons []string
if primaryPercentConnected < MinConnectedStake {
errorReasons = append(errorReasons,
fmt.Sprintf("connected to %f%% of primary network stake; should be connected to at least %f%%",
primaryPercentConnected*100,
MinConnectedStake*100,
),
)
}
for subnetID := range vm.WhitelistedSubnets {
percentConnected, err := vm.getPercentConnected(subnetID)
if err != nil {
return nil, fmt.Errorf("couldn't get percent connected for %q: %w", subnetID, err)
}
subnetIDStr := subnetID.String()
vm.metrics.subnetPercentConnected.WithLabelValues(subnetIDStr).Set(percentConnected)
key := fmt.Sprintf("%s-percentConnected", subnetID)
details[key] = percentConnected
if percentConnected < MinConnectedStake {
errorReasons = append(errorReasons,
fmt.Sprintf("connected to %f%% of %q weight; should be connected to at least %f%%",
percentConnected*100,
subnetIDStr,
MinConnectedStake*100,
),
)
}
}
if len(errorReasons) > 0 {
err = fmt.Errorf("platform layer is unhealthy reason: %s", strings.Join(errorReasons, ", "))
}
return details, err
}