-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Health endpoint #836
Open
otherview
wants to merge
19
commits into
master
Choose a base branch
from
pedro/health_endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
19db51a
Adding Health endpoint
otherview 22bc2c2
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview df30451
pr comments + 503 if not healthy
otherview ed0097c
refactored admin server and api + health endpoint tests
otherview 552629c
fix health condition
otherview 6fa89c1
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 91ceb58
fix admin routing
otherview 0206c1f
added comments + changed from ChainSync to ChainBootstrapStatus
otherview 17c2ce9
Adding healthcheck for solo mode
otherview 2f461c8
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 1797c22
adding solo + tests
otherview dbea45a
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 15a18fc
fix log_level handler funcs
otherview bda5f97
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 6b22089
refactor health package + add p2p count
otherview 1fdc48b
remove solo methods
otherview 430c458
moving health service to api pkg
otherview f1b0000
added defaults + api health query
otherview c0aee23
pr comments
otherview File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package admin | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
healthAPI "github.com/vechain/thor/v2/api/admin/health" | ||
"github.com/vechain/thor/v2/api/admin/loglevel" | ||
) | ||
|
||
func New(logLevel *slog.LevelVar, health *healthAPI.Health) http.HandlerFunc { | ||
router := mux.NewRouter() | ||
subRouter := router.PathPrefix("/admin").Subrouter() | ||
|
||
loglevel.New(logLevel).Mount(subRouter, "/loglevel") | ||
healthAPI.NewAPI(health).Mount(subRouter, "/health") | ||
|
||
handler := handlers.CompressHandler(router) | ||
|
||
return handler.ServeHTTP | ||
} |
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,110 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/vechain/thor/v2/chain" | ||
"github.com/vechain/thor/v2/comm" | ||
"github.com/vechain/thor/v2/thor" | ||
) | ||
|
||
type BlockIngestion struct { | ||
ID *thor.Bytes32 `json:"id"` | ||
Timestamp *time.Time `json:"timestamp"` | ||
} | ||
|
||
type Status struct { | ||
Healthy bool `json:"healthy"` | ||
BlockIngestion *BlockIngestion `json:"blockIngestion"` | ||
ChainBootstrapped bool `json:"chainBootstrapped"` | ||
PeerCount int `json:"peerCount"` | ||
} | ||
|
||
type Health struct { | ||
lock sync.RWMutex | ||
repo *chain.Repository | ||
p2p *comm.Communicator | ||
isNodeBootstrapped bool | ||
} | ||
|
||
const ( | ||
defaultBlockTolerance = time.Duration(2*thor.BlockInterval) * time.Second // 2 blocks tolerance | ||
defaultMinPeerCount = 2 | ||
) | ||
|
||
func New(repo *chain.Repository, p2p *comm.Communicator) *Health { | ||
return &Health{ | ||
repo: repo, | ||
p2p: p2p, | ||
} | ||
} | ||
|
||
// isNetworkProgressing checks if the network is producing new blocks within the allowed interval. | ||
func (h *Health) isNetworkProgressing(now time.Time, bestBlockTimestamp time.Time, blockTolerance time.Duration) bool { | ||
return now.Sub(bestBlockTimestamp) <= blockTolerance | ||
} | ||
|
||
// hasNodeBootstrapped checks if the node has bootstrapped by comparing the block interval. | ||
// Once it's marked as done, it never reverts. | ||
func (h *Health) hasNodeBootstrapped(now time.Time, bestBlockTimestamp time.Time) bool { | ||
if h.isNodeBootstrapped { | ||
return true | ||
} | ||
|
||
blockInterval := time.Duration(thor.BlockInterval) * time.Second | ||
if bestBlockTimestamp.Add(blockInterval).After(now) { | ||
h.isNodeBootstrapped = true | ||
} | ||
|
||
return h.isNodeBootstrapped | ||
} | ||
|
||
// isNodeConnectedP2P checks if the node is connected to peers | ||
func (h *Health) isNodeConnectedP2P(peerCount int, minPeerCount int) bool { | ||
return peerCount >= minPeerCount | ||
} | ||
|
||
func (h *Health) Status(blockTolerance time.Duration, minPeerCount int) (*Status, error) { | ||
h.lock.RLock() | ||
defer h.lock.RUnlock() | ||
|
||
// Fetch the best block details | ||
bestBlock := h.repo.BestBlockSummary() | ||
bestBlockID := bestBlock.Header.ID() | ||
bestBlockTimestamp := time.Unix(int64(bestBlock.Header.Timestamp()), 0) | ||
|
||
// Fetch the current connected peers | ||
var connectedPeerCount int | ||
if h.p2p == nil { | ||
connectedPeerCount = minPeerCount // ignore peers in solo mode | ||
} else { | ||
connectedPeerCount = h.p2p.PeerCount() | ||
} | ||
|
||
now := time.Now() | ||
|
||
// Perform the checks | ||
networkProgressing := h.isNetworkProgressing(now, bestBlockTimestamp, blockTolerance) | ||
nodeBootstrapped := h.hasNodeBootstrapped(now, bestBlockTimestamp) | ||
nodeConnected := h.isNodeConnectedP2P(connectedPeerCount, minPeerCount) | ||
|
||
// Calculate overall health status | ||
healthy := networkProgressing && nodeBootstrapped && nodeConnected | ||
|
||
// Return the current status | ||
return &Status{ | ||
Healthy: healthy, | ||
BlockIngestion: &BlockIngestion{ | ||
ID: &bestBlockID, | ||
Timestamp: &bestBlockTimestamp, | ||
}, | ||
ChainBootstrapped: nodeBootstrapped, | ||
PeerCount: connectedPeerCount, | ||
}, nil | ||
} |
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,68 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/api/utils" | ||
) | ||
|
||
type API struct { | ||
healthStatus *Health | ||
} | ||
|
||
func NewAPI(healthStatus *Health) *API { | ||
return &API{ | ||
healthStatus: healthStatus, | ||
} | ||
} | ||
|
||
func (h *API) handleGetHealth(w http.ResponseWriter, r *http.Request) error { | ||
// Parse query parameters | ||
query := r.URL.Query() | ||
|
||
// Default to constants if query parameters are not provided | ||
blockTolerance := defaultBlockTolerance | ||
minPeerCount := defaultMinPeerCount | ||
|
||
// Override with query parameters if they exist | ||
if queryBlockTolerance := query.Get("blockTolerance"); queryBlockTolerance != "" { | ||
if parsed, err := time.ParseDuration(queryBlockTolerance); err == nil { | ||
blockTolerance = parsed | ||
} | ||
} | ||
|
||
if queryMinPeerCount := query.Get("minPeerCount"); queryMinPeerCount != "" { | ||
if parsed, err := strconv.Atoi(queryMinPeerCount); err == nil { | ||
minPeerCount = parsed | ||
} | ||
} | ||
|
||
acc, err := h.healthStatus.Status(blockTolerance, minPeerCount) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !acc.Healthy { | ||
w.WriteHeader(http.StatusServiceUnavailable) // Set the status to 503 | ||
} else { | ||
w.WriteHeader(http.StatusOK) // Set the status to 200 | ||
} | ||
return utils.WriteJSON(w, acc) | ||
} | ||
|
||
func (h *API) Mount(root *mux.Router, pathPrefix string) { | ||
sub := root.PathPrefix(pathPrefix).Subrouter() | ||
|
||
sub.Path(""). | ||
Methods(http.MethodGet). | ||
Name("health"). | ||
HandlerFunc(utils.WrapHandlerFunc(h.handleGetHealth)) | ||
} |
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,59 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/vechain/thor/v2/comm" | ||
"github.com/vechain/thor/v2/test/testchain" | ||
"github.com/vechain/thor/v2/txpool" | ||
) | ||
|
||
var ts *httptest.Server | ||
|
||
func TestHealth(t *testing.T) { | ||
initAPIServer(t) | ||
|
||
var healthStatus Status | ||
respBody, statusCode := httpGet(t, ts.URL+"/health") | ||
require.NoError(t, json.Unmarshal(respBody, &healthStatus)) | ||
assert.False(t, healthStatus.Healthy) | ||
assert.Equal(t, http.StatusServiceUnavailable, statusCode) | ||
} | ||
|
||
func initAPIServer(t *testing.T) { | ||
thorChain, err := testchain.NewIntegrationTestChain() | ||
require.NoError(t, err) | ||
|
||
router := mux.NewRouter() | ||
NewAPI( | ||
New(thorChain.Repo(), comm.New(thorChain.Repo(), txpool.New(thorChain.Repo(), nil, txpool.Options{}))), | ||
).Mount(router, "/health") | ||
|
||
ts = httptest.NewServer(router) | ||
} | ||
|
||
func httpGet(t *testing.T, url string) ([]byte, int) { | ||
res, err := http.Get(url) //#nosec G107 | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer res.Body.Close() | ||
|
||
r, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return r, res.StatusCode | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ChainBootstrapped
can be removed consideringBlockIngestion
?