Skip to content
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

Network: Support simple liveness check via http on gossip server port. #5944

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions node/follower_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
}

node.ledger.RegisterBlockListeners(blockListeners)

if cfg.IsGossipServer() {
rpcs.MakeHealthService(node.net)

Check warning on line 132 in node/follower_node.go

View check run for this annotation

Codecov / codecov/patch

node/follower_node.go#L132

Added line #L132 was not covered by tests
}

node.blockService = rpcs.MakeBlockService(node.log, cfg, node.ledger, p2pNode, node.genesisID)
node.catchupBlockAuth = blockAuthenticatorImpl{Ledger: node.ledger, AsyncVoteVerifier: agreement.MakeAsyncVoteVerifier(node.lowPriorityCryptoVerificationPool)}
node.catchupService = catchup.MakeService(node.log, node.config, p2pNode, node.ledger, node.catchupBlockAuth, make(chan catchup.PendingUnmatchedCertificate), node.lowPriorityCryptoVerificationPool)
Expand Down
5 changes: 5 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@
return nil, err
}

// The health service registers itself with the network
if cfg.IsGossipServer() {
rpcs.MakeHealthService(node.net)

Check warning on line 256 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L256

Added line #L256 was not covered by tests
}

node.blockService = rpcs.MakeBlockService(node.log, cfg, node.ledger, p2pNode, node.genesisID)
node.ledgerService = rpcs.MakeLedgerService(cfg, node.ledger, p2pNode, node.genesisID)
rpcs.RegisterTxService(node.transactionPool, p2pNode, node.genesisID, cfg.TxPoolSize, cfg.TxSyncServeResponseSize)
Expand Down
41 changes: 41 additions & 0 deletions rpcs/healthService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.

package rpcs

import (
"github.com/algorand/go-algorand/network"
"net/http"
)

// HealthServiceStatusPath is the path to register HealthService as a handler for when using gorilla/mux
const HealthServiceStatusPath = "/status"

// HealthService is a service that provides health information endpoints for the node
type HealthService struct{}

// MakeHealthService creates a new HealthService and registers it with the provided network if enabled
func MakeHealthService(net network.GossipNode) HealthService {
gmalouf marked this conversation as resolved.
Show resolved Hide resolved
service := HealthService{}

net.RegisterHTTPHandler(HealthServiceStatusPath, service)

return service
}

func (h HealthService) ServeHTTP(writer http.ResponseWriter, _ *http.Request) {
gmalouf marked this conversation as resolved.
Show resolved Hide resolved
writer.WriteHeader(http.StatusOK)
}
52 changes: 52 additions & 0 deletions rpcs/healthService_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.

package rpcs

import (
"github.com/algorand/go-algorand/network"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
"io"
"net/http"
"path"
"testing"
)

func TestHealthService_ServeHTTP(t *testing.T) {
partitiontest.PartitionTest(t)

nodeA := &basicRPCNode{}
nodeA.start()
defer nodeA.stop()

_ = MakeHealthService(nodeA)

parsedURL, err := network.ParseHostOrURL(nodeA.rootURL())
require.NoError(t, err)

client := http.Client{}

parsedURL.Path = path.Join(parsedURL.Path, HealthServiceStatusPath)

response, err := client.Get(parsedURL.String())
require.NoError(t, err)

require.Equal(t, http.StatusOK, response.StatusCode)
bodyData, err := io.ReadAll(response.Body)
require.NoError(t, err)
require.Empty(t, bodyData)
}