/
serve_health.go
174 lines (160 loc) · 7.35 KB
/
serve_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package server
import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/AudiusProject/audius-protocol/mediorum/ethcontracts"
"github.com/AudiusProject/audius-protocol/mediorum/server/signature"
"github.com/gowebpki/jcs"
"github.com/labstack/echo/v4"
)
type healthCheckResponse struct {
Data healthCheckResponseData `json:"data"`
Signer string `json:"signer"`
Signature string `json:"signature"`
Timestamp time.Time `json:"timestamp"`
}
type healthCheckResponseData struct {
Healthy bool `json:"healthy"`
Version string `json:"version"`
Service string `json:"service"` // used by registerWithDelegate()
IsSeeding bool `json:"isSeeding"`
IsAudiusdManaged bool `json:"isAudiusdManaged"`
BuiltAt string `json:"builtAt"`
StartedAt time.Time `json:"startedAt"`
SPID int `json:"spID"`
SPOwnerWallet string `json:"spOwnerWallet"`
Git string `json:"git"`
AudiusDockerCompose string `json:"audiusDockerCompose"`
MediorumPathUsed uint64 `json:"mediorumPathUsed"` // bytes
MediorumPathSize uint64 `json:"mediorumPathSize"` // bytes
DatabaseSize uint64 `json:"databaseSize"` // bytes
DbSizeErr string `json:"dbSizeErr"`
LastSuccessfulRepair RepairTracker `json:"lastSuccessfulRepair"`
LastSuccessfulCleanup RepairTracker `json:"lastSuccessfulCleanup"`
UploadsCount int64 `json:"uploadsCount"`
UploadsCountErr string `json:"uploadsCountErr"`
AutoUpgradeEnabled bool `json:"autoUpgradeEnabled"`
TrustedNotifier *ethcontracts.NotifierInfo `json:"trustedNotifier"`
Env string `json:"env"`
Self Peer `json:"self"`
WalletIsRegistered bool `json:"wallet_is_registered"`
Signers []Peer `json:"signers"`
ReplicationFactor int `json:"replicationFactor"`
Dir string `json:"dir"`
BlobStorePrefix string `json:"blobStorePrefix"`
MoveFromBlobStorePrefix string `json:"moveFromBlobStorePrefix"`
ListenPort string `json:"listenPort"`
TrustedNotifierID int `json:"trustedNotifierId"`
PeerHealths map[string]*PeerHealth `json:"peerHealths"`
UnreachablePeers []string `json:"unreachablePeers"`
FailsPeerReachability bool `json:"failsPeerReachability"`
StoreAll bool `json:"storeAll"`
IsDbLocalhost bool `json:"isDbLocalhost"`
}
func (ss *MediorumServer) serveHealthCheck(c echo.Context) error {
healthy := ss.databaseSize > 0 && ss.dbSizeErr == "" && ss.uploadsCountErr == ""
allowUnregistered, _ := strconv.ParseBool(c.QueryParam("allow_unregistered"))
if !allowUnregistered && !ss.Config.WalletIsRegistered {
healthy = false
}
blobStorePrefix, _, foundBlobStore := strings.Cut(ss.Config.BlobStoreDSN, "://")
if !foundBlobStore {
blobStorePrefix = ""
}
blobStoreMoveFromPrefix, _, foundBlobStoreMoveFrom := strings.Cut(ss.Config.MoveFromBlobStoreDSN, "://")
if !foundBlobStoreMoveFrom {
blobStoreMoveFromPrefix = ""
}
var err error
// since we're using peerHealth
ss.peerHealthsMutex.RLock()
defer ss.peerHealthsMutex.RUnlock()
data := healthCheckResponseData{
Healthy: healthy,
Version: ss.Config.VersionJson.Version,
Service: ss.Config.VersionJson.Service,
IsSeeding: ss.isSeeding,
IsAudiusdManaged: ss.isAudiusdManaged,
BuiltAt: vcsBuildTime,
StartedAt: ss.StartedAt,
SPID: ss.Config.SPID,
SPOwnerWallet: ss.Config.SPOwnerWallet,
Git: ss.Config.GitSHA,
AudiusDockerCompose: ss.Config.AudiusDockerCompose,
MediorumPathUsed: ss.mediorumPathUsed,
MediorumPathSize: ss.mediorumPathSize,
DatabaseSize: ss.databaseSize,
DbSizeErr: ss.dbSizeErr,
LastSuccessfulRepair: ss.lastSuccessfulRepair,
LastSuccessfulCleanup: ss.lastSuccessfulCleanup,
UploadsCount: ss.uploadsCount,
UploadsCountErr: ss.uploadsCountErr,
AutoUpgradeEnabled: ss.Config.AutoUpgradeEnabled,
TrustedNotifier: ss.trustedNotifier,
Dir: ss.Config.Dir,
BlobStorePrefix: blobStorePrefix,
MoveFromBlobStorePrefix: blobStoreMoveFromPrefix,
ListenPort: ss.Config.ListenPort,
ReplicationFactor: ss.Config.ReplicationFactor,
Env: ss.Config.Env,
Self: ss.Config.Self,
WalletIsRegistered: ss.Config.WalletIsRegistered,
TrustedNotifierID: ss.Config.TrustedNotifierID,
PeerHealths: ss.peerHealths,
UnreachablePeers: ss.unreachablePeers,
FailsPeerReachability: ss.failsPeerReachability,
Signers: ss.Config.Signers,
StoreAll: ss.Config.StoreAll,
IsDbLocalhost: ss.Config.PostgresDSN == "postgres://postgres:postgres@db:5432/audius_creator_node" || ss.Config.PostgresDSN == "postgresql://postgres:postgres@db:5432/audius_creator_node" || ss.Config.PostgresDSN == "localhost",
}
dataBytes, err := json.Marshal(data)
if err != nil {
return c.JSON(500, map[string]string{"error": "Failed to marshal health check data: " + err.Error()})
}
dataBytesSorted, err := jcs.Transform(dataBytes)
if err != nil {
return c.JSON(500, map[string]string{"error": "Failed to sort health check data: " + err.Error()})
}
signatureHex := "private key not set (should only happen on local dev)!"
if ss.Config.privateKey != nil {
signature, err := signature.SignBytes(dataBytesSorted, ss.Config.privateKey)
if err != nil {
return c.JSON(500, map[string]string{"error": "Failed to sign health check response: " + err.Error()})
}
signatureHex = fmt.Sprintf("0x%s", hex.EncodeToString(signature))
}
status := 200
if !healthy {
if !allowUnregistered && !ss.Config.WalletIsRegistered {
status = 506
} else {
status = 503
}
}
return c.JSON(status, healthCheckResponse{
Data: data,
Signer: ss.Config.Self.Wallet,
Signature: signatureHex,
Timestamp: time.Now(),
})
}
func (ss *MediorumServer) requireHealthy(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
allowUnhealthy, _ := strconv.ParseBool(c.QueryParam("allow_unhealthy"))
if allowUnhealthy {
return next(c)
}
if !ss.Config.WalletIsRegistered {
return c.JSON(506, "wallet not registered")
}
dbHealthy := ss.databaseSize > 0 && ss.dbSizeErr == "" && ss.uploadsCountErr == ""
if !dbHealthy {
return c.JSON(503, "database not healthy")
}
return next(c)
}
}