Skip to content

Commit

Permalink
prepare showing client status details on clients/forks page
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Oct 23, 2023
1 parent 6523645 commit 5809f20
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
16 changes: 9 additions & 7 deletions handlers/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ func buildClientsPageData() (*models.ClientsPageData, time.Duration) {
cacheTime := time.Duration(utils.Config.Chain.Config.SecondsPerSlot) * time.Second

for _, client := range services.GlobalBeaconService.GetClients() {
lastHeadSlot, lastHeadRoot := client.GetLastHead()
lastHeadSlot, lastHeadRoot, clientRefresh := client.GetLastHead()
if lastHeadSlot < 0 {
lastHeadSlot = 0
}
resClient := &models.ClientsPageDataClient{
Index: int(client.GetIndex()) + 1,
Name: client.GetName(),
Version: client.GetVersion(),
HeadSlot: uint64(lastHeadSlot),
HeadRoot: lastHeadRoot,
Status: client.GetStatus(),
Index: int(client.GetIndex()) + 1,
Name: client.GetName(),
Version: client.GetVersion(),
HeadSlot: uint64(lastHeadSlot),
HeadRoot: lastHeadRoot,
Status: client.GetStatus(),
LastRefresh: clientRefresh,
LastError: client.GetLastClientError(),
}
pageData.Clients = append(pageData.Clients, resClient)
}
Expand Down
12 changes: 7 additions & 5 deletions handlers/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ func buildForksPageData() (*models.ForksPageData, time.Duration) {
pageData.Forks = append(pageData.Forks, forkData)

for _, client := range fork.AllClients {
clientHeadSlot, _ := client.GetLastHead()
clientHeadSlot, _, clientRefresh := client.GetLastHead()
forkClient := &models.ForksPageDataClient{
Index: int(client.GetIndex()) + 1,
Name: client.GetName(),
Version: client.GetVersion(),
Status: client.GetStatus(),
Index: int(client.GetIndex()) + 1,
Name: client.GetName(),
Version: client.GetVersion(),
Status: client.GetStatus(),
LastRefresh: clientRefresh,
LastError: client.GetLastClientError(),
}
if clientHeadSlot >= 0 {
forkClient.HeadSlot = uint64(clientHeadSlot)
Expand Down
17 changes: 15 additions & 2 deletions indexer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type IndexerClient struct {
versionStr string
indexerCache *indexerCache
cacheMutex sync.RWMutex
lastClientError error
lastHeadRefresh time.Time
lastStreamEvent time.Time
isSynchronizing bool
isOptimistic bool
Expand Down Expand Up @@ -72,10 +74,10 @@ func (client *IndexerClient) GetRpcClient() *rpc.BeaconClient {
return client.rpcClient
}

func (client *IndexerClient) GetLastHead() (int64, []byte) {
func (client *IndexerClient) GetLastHead() (int64, []byte, time.Time) {
client.cacheMutex.RLock()
defer client.cacheMutex.RUnlock()
return client.lastHeadSlot, client.lastHeadRoot
return client.lastHeadSlot, client.lastHeadRoot, client.lastHeadRefresh
}

func (client *IndexerClient) GetStatus() string {
Expand All @@ -90,6 +92,15 @@ func (client *IndexerClient) GetStatus() string {
}
}

func (client *IndexerClient) GetLastClientError() string {
client.cacheMutex.RLock()
defer client.cacheMutex.RUnlock()
if client.lastClientError == nil {
return ""
}
return client.lastClientError.Error()
}

func (client *IndexerClient) runIndexerClientLoop() {
defer utils.HandleSubroutinePanic("runIndexerClientLoop")

Expand Down Expand Up @@ -123,6 +134,7 @@ func (client *IndexerClient) runIndexerClientLoop() {
return
}

client.lastClientError = err
client.retryCounter++
waitTime := 10
skipLog := false
Expand Down Expand Up @@ -507,6 +519,7 @@ func (client *IndexerClient) ensureParentBlocks(currentBlock *CacheBlock) error

func (client *IndexerClient) setHeadBlock(root []byte, slot uint64) error {
client.cacheMutex.Lock()
client.lastHeadRefresh = time.Now()
if bytes.Equal(client.lastHeadRoot, root) {
client.cacheMutex.Unlock()
return nil
Expand Down
4 changes: 2 additions & 2 deletions indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (indexer *Indexer) GetHeadForks(readyOnly bool) []*HeadFork {
if readyOnly && (!client.isConnected || client.isSynchronizing || client.isOptimistic) {
continue
}
cHeadSlot, cHeadRoot := client.GetLastHead()
cHeadSlot, cHeadRoot, _ := client.GetLastHead()
var matchingFork *HeadFork
for _, fork := range headForks {
if bytes.Equal(fork.Root, cHeadRoot) || indexer.indexerCache.isCanonicalBlock(cHeadRoot, fork.Root) {
Expand Down Expand Up @@ -222,7 +222,7 @@ func (indexer *Indexer) GetHeadForks(readyOnly bool) []*HeadFork {
})
for _, client := range fork.AllClients {
var headDistance uint64 = 0
_, cHeadRoot := client.GetLastHead()
_, cHeadRoot, _ := client.GetLastHead()
if !bytes.Equal(fork.Root, cHeadRoot) {
_, headDistance = indexer.indexerCache.getCanonicalDistance(cHeadRoot, fork.Root)
}
Expand Down
16 changes: 10 additions & 6 deletions types/models/clients.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package models

import "time"

// ClientsPageData is a struct to hold info for the clients page
type ClientsPageData struct {
Clients []*ClientsPageDataClient `json:"clients"`
ClientCount uint64 `json:"client_count"`
}

type ClientsPageDataClient struct {
Index int `json:"index"`
Name string `json:"name"`
Version string `json:"version"`
HeadSlot uint64 `json:"head_slot"`
HeadRoot []byte `json:"head_root"`
Status string `json:"status"`
Index int `json:"index"`
Name string `json:"name"`
Version string `json:"version"`
HeadSlot uint64 `json:"head_slot"`
HeadRoot []byte `json:"head_root"`
Status string `json:"status"`
LastRefresh time.Time `json:"refresh"`
LastError string `json:"error"`
}
16 changes: 10 additions & 6 deletions types/models/forks.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "time"

// ForksPageData is a struct to hold info for the forks page
type ForksPageData struct {
Forks []*ForksPageDataFork `json:"forks"`
Expand All @@ -14,10 +16,12 @@ type ForksPageDataFork struct {
}

type ForksPageDataClient struct {
Index int `json:"index"`
Name string `json:"name"`
Version string `json:"version"`
Status string `json:"status"`
HeadSlot uint64 `json:"head_slot"`
Distance uint64 `json:"distance"`
Index int `json:"index"`
Name string `json:"name"`
Version string `json:"version"`
Status string `json:"status"`
HeadSlot uint64 `json:"head_slot"`
Distance uint64 `json:"distance"`
LastRefresh time.Time `json:"refresh"`
LastError string `json:"error"`
}

0 comments on commit 5809f20

Please sign in to comment.