summaryrefslogtreecommitdiff
path: root/cmd/algoh/deadman.go
blob: ffd4d556b3fd944e78197dc61bf79e542cf851f9 (plain)
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
// 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 main

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/logging/telemetryspec"
	"github.com/algorand/go-algorand/rpcs"
)

type deadManWatcher struct {
	timeout       time.Duration
	newBlockChan  chan uint64
	uploadOnError bool
	client        Client
	done          <-chan struct{}
	wg            *sync.WaitGroup
	algodConfig   config.Local
}

func makeDeadManWatcher(timeout int64, client Client, uploadOnError bool, done <-chan struct{}, wg *sync.WaitGroup, algodConfig config.Local) deadManWatcher {
	var deadManTime time.Duration
	if timeout == 0 {
		deadManTime = time.Hour * (10 * 365 * 24) // Don't fire for 10 years
	} else {
		deadManTime = time.Duration(timeout) * time.Second
	}

	return deadManWatcher{
		timeout:       deadManTime,
		newBlockChan:  make(chan uint64),
		client:        client,
		uploadOnError: uploadOnError,
		done:          done,
		wg:            wg,
		algodConfig:   algodConfig,
	}
}

func (w deadManWatcher) init(initBlock uint64) {
	go w.run(initBlock)
}

func (w deadManWatcher) run(initBlock uint64) {
	defer w.wg.Done()
	latestBlock := initBlock

	var deadManTimeout <-chan time.Time

	for {
		select {
		case block := <-w.newBlockChan:
			latestBlock = block
			deadManTimeout = time.After(w.timeout)
		case <-w.done:
			return
		case <-deadManTimeout:
			deadManTimeout = nil // Don't detect deadlock again until after we see another block

			err := w.reportDeadManTimeout(latestBlock)
			// If err is not nil, algod failed to respond to goroutine request
			// This is a critical failure - hopefully telemetry and logging will capture
			// the details, but the best thing we can do is try to shut it down.
			if err != nil {
				nc := getNodeController()
				nc.FullStop()
			}
		}
	}
}

func (w deadManWatcher) onBlock(block rpcs.EncodedBlockCert) {
	w.newBlockChan <- uint64(block.Block.BlockHeader.Round)
}

func (w deadManWatcher) reportDeadManTimeout(curBlock uint64) (err error) {
	var details telemetryspec.DeadManTriggeredEventDetails
	if w.algodConfig.EnableProfiler {
		goRoutines, err := getGoRoutines(w.client)
		if err != nil {
			goRoutines = fmt.Sprintf("Error dumping goroutines: %v", err)
		}
		details = telemetryspec.DeadManTriggeredEventDetails{
			Timeout:      int64(w.timeout.Seconds()),
			CurrentBlock: curBlock,
			GoRoutines:   goRoutines,
		}
	} else {
		healthCheck, err := getHealthCheck(w.client)
		if err != nil {
			healthCheck = fmt.Sprintf("Error performing health check : %v", err)
		}
		details = telemetryspec.DeadManTriggeredEventDetails{
			Timeout:      int64(w.timeout.Seconds()),
			CurrentBlock: curBlock,
			GoRoutines:   healthCheck,
		}
	}
	log.EventWithDetails(telemetryspec.HostApplicationState, telemetryspec.DeadManTriggeredEvent, details)

	if w.uploadOnError {
		sendLogs()
	}
	return
}

func getGoRoutines(client Client) (goRoutines string, err error) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	goRoutines, err = client.GetGoRoutines(ctx)
	if ctx.Err() == context.DeadlineExceeded {
		err = fmt.Errorf("timed out requesting goroutines")
	}
	return
}

func getHealthCheck(client Client) (healthCheck string, err error) {
	err = client.HealthCheck()
	if err == nil {
		healthCheck = "Node is healthy"
	}
	return
}