summaryrefslogtreecommitdiff
path: root/agreement/persistence.go
blob: b8510e446cb5b0174ed6478666027eac90c5536f (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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright (C) 2019-2023 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 agreement

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"sync"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/logging/logspec"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/util/db"
	"github.com/algorand/go-algorand/util/timers"
)

// diskState represents the state required by the agreement protocol to be persistent.
type diskState struct {
	_struct struct{} `codec:","`

	Router []byte
	Player []byte
	Clock  []byte

	ActionTypes []actionType `codec:"ActionTypes,allocbound=-"`
	Actions     [][]byte     `codec:"Actions,allocbound=-"`
}

func persistent(as []action) bool {
	for _, a := range as {
		if a.persistent() {
			return true
		}
	}
	return false
}

// encode serializes the current state into a byte array.
func encode(t timers.Clock[TimeoutType], rr rootRouter, p player, a []action, reflect bool) (raw []byte) {
	var s diskState

	// Don't persist state for old rounds
	// rootRouter.update() may preserve roundRouters from credentialRoundLag rounds ago
	children := make(map[round]*roundRouter)
	for rnd, rndRouter := range rr.Children {
		if rnd >= p.Round {
			children[rnd] = rndRouter
		}
	}
	if len(children) == 0 {
		rr.Children = nil
	} else {
		rr.Children = children
	}

	if reflect {
		s.Router = protocol.EncodeReflect(rr)
		s.Player = protocol.EncodeReflect(p)
	} else {
		s.Router = protocol.Encode(&rr)
		s.Player = protocol.Encode(&p)
	}
	s.Clock = t.Encode()
	s.ActionTypes = make([]actionType, len(a))
	s.Actions = make([][]byte, len(a))
	for i, act := range a {
		s.ActionTypes[i] = act.t()

		// still use reflection for actions since action is an interface and we can't define marshaller methods on it
		s.Actions[i] = protocol.EncodeReflect(act)
	}
	if reflect {
		raw = protocol.EncodeReflect(s)
	} else {
		raw = protocol.Encode(&s)
	}
	return
}

// persist atomically writes state to the crash database.
func persist(log serviceLogger, crash db.Accessor, Round basics.Round, Period period, Step step, raw []byte) (err error) {
	logEvent := logspec.AgreementEvent{
		Type:   logspec.Persisted,
		Round:  uint64(Round),
		Period: uint64(Period),
		Step:   uint64(Step),
	}
	defer func() {
		log.with(logEvent).Info("persisted state to the database")
	}()

	err = crash.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		_, err := tx.Exec("insert or replace into Service (rowid, data) values (1, ?)", raw)
		return err
	})
	if err == nil {
		return
	}

	log.Errorf("persisting failure: %v", err)
	return
}

// reset deletes the existing recovery state from database.
//
// In case it's unable to clear the Service table, an error would get logged.
func reset(log logging.Logger, crash db.Accessor) {
	log.Infof("reset (agreement): resetting crash state")

	err := crash.Atomic(func(ctx context.Context, tx *sql.Tx) (err error) {
		// we could not retrieve our state, so wipe it
		_, err = tx.Exec("delete from Service")
		return
	})

	if err != nil {
		log.Warnf("reset (agreement): failed to clear Service table - %v", err)
	}
}

// errNoCrashStateAvailable returned by restore when the crash recovery state is not available in the crash recovery database table.
var errNoCrashStateAvailable = errors.New("restore (agreement): no crash state available")

// restore reads state from a crash database. It does not attempt to parse the encoded data.
//
// It returns an error if this fails or if crash state does not exist.
func restore(log logging.Logger, crash db.Accessor) (raw []byte, err error) {
	var noCrashState bool
	defer func() {
		if err != nil && !noCrashState {
			log.Warnf("restore (agreement): could not restore crash state from database: %v", err)
		}
	}()

	err = crash.Atomic(func(ctx context.Context, tx *sql.Tx) error {
		return agreeInstallDatabase(tx)
	})

	if err == nil {
		// the above call was completed sucecssfully, which means that we've just created the table ( which wasn't there ! ).
		// in that case, the table is guaranteed to be empty, and therefore we can return right here.
		log.Infof("restore (agreement): crash state table initialized")
		noCrashState = true // this is a normal case (we don't have crash state)
		err = errNoCrashStateAvailable
		return
	}

	err = crash.Atomic(func(ctx context.Context, tx *sql.Tx) (res error) {
		var reset bool
		defer func() {
			if !reset {
				return
			}
			log.Infof("restore (agreement): resetting crash state")

			// we could not retrieve our state, so wipe it
			_, err = tx.Exec("delete from Service")
			if err != nil {
				res = fmt.Errorf("restore (agreement): (in reset) failed to clear Service table")
				return
			}
		}()

		var nrows int
		row := tx.QueryRow("select count(*) from Service")
		err := row.Scan(&nrows)
		if err != nil {
			log.Errorf("restore (agreement): could not query raw state: %v", err)
			reset = true
			return err
		}
		if nrows != 1 {
			log.Infof("restore (agreement): crash state not found (n = %d)", nrows)
			reset = true
			noCrashState = true // this is a normal case (we have leftover crash state from an old round)
			return errNoCrashStateAvailable
		}

		row = tx.QueryRow("select data from Service")
		err = row.Scan(&raw)
		if err != nil {
			log.Errorf("restore (agreement): could not read crash state raw data: %v", err)
			reset = true
			return err
		}

		return nil
	})
	return
}

// decode process the incoming raw bytes array and attempt to reconstruct the agreement state objects.
//
// In all decoding errors, it returns the error code in err
func decode(raw []byte, t0 timers.Clock[TimeoutType], log serviceLogger, reflect bool) (t timers.Clock[TimeoutType], rr rootRouter, p player, a []action, err error) {
	var t2 timers.Clock[TimeoutType]
	var rr2 rootRouter
	var p2 player
	a2 := []action{}
	var s diskState
	if reflect {
		err = protocol.DecodeReflect(raw, &s)
		if err != nil {
			log.Errorf("decode (agreement): error decoding retrieved state (len = %v): %v", len(raw), err)
			return
		}
	} else {
		err = protocol.Decode(raw, &s)
		if err != nil {
			log.Warnf("decode (agreement): error decoding retrieved state using msgp (len = %v): %v. Trying reflection", len(raw), err)
			err = protocol.DecodeReflect(raw, &s)
			if err != nil {
				log.Errorf("decode (agreement): error decoding using either reflection or msgp): %v", err)
				return
			}
		}
	}

	t2, err = t0.Decode(s.Clock)
	if err != nil {
		return
	}

	if reflect {
		err = protocol.DecodeReflect(s.Player, &p2)
		if err != nil {
			return
		}
		p2.lowestCredentialArrivals = makeCredentialArrivalHistory(dynamicFilterCredentialArrivalHistory)
		rr2 = makeRootRouter(p2)
		err = protocol.DecodeReflect(s.Router, &rr2)
		if err != nil {
			return
		}
	} else {
		err = protocol.Decode(s.Player, &p2)
		if err != nil {
			log.Warnf("decode (agreement): failed to decode Player using msgp (len = %v): %v. Trying reflection", len(s.Player), err)
			err = protocol.DecodeReflect(s.Player, &p2)
			if err != nil {
				log.Errorf("decode (agreement): failed to decode Player using either reflection or msgp: %v", err)
				return
			}
		}
		p2.lowestCredentialArrivals = makeCredentialArrivalHistory(dynamicFilterCredentialArrivalHistory)
		if p2.OldDeadline != 0 {
			p2.Deadline = Deadline{Duration: p2.OldDeadline, Type: TimeoutDeadline}
			p2.OldDeadline = 0 // clear old value
		}
		rr2 = makeRootRouter(p2)
		err = protocol.Decode(s.Router, &rr2)
		if err != nil {
			log.Warnf("decode (agreement): failed to decode Router using msgp (len = %v): %v. Trying reflection", len(s.Router), err)
			rr2 = makeRootRouter(p2)
			err = protocol.DecodeReflect(s.Router, &rr2)
			if err != nil {
				log.Errorf("decode (agreement): failed to decode Router using either reflection or msgp: %v", err)
				return
			}
		}
	}

	for i := range s.Actions {
		act := zeroAction(s.ActionTypes[i])
		// always use reflection for actions since action is an interface and we can't define unmarshaller methods on it
		err = protocol.DecodeReflect(s.Actions[i], &act)
		if err != nil {
			return
		}
		a2 = append(a2, act)
	}

	t = t2
	rr = rr2
	p = p2
	a = a2
	return
}

type persistentRequest struct {
	round  basics.Round
	period period
	step   step
	raw    []byte
	done   chan error
	clock  timers.Clock[TimeoutType]
	events chan<- externalEvent
}

type asyncPersistenceLoop struct {
	log     serviceLogger
	crashDb db.Accessor
	ledger  LedgerReader
	wg      sync.WaitGroup // wait for goroutine to abort.
	ctxExit context.CancelFunc
	pending chan persistentRequest
}

func makeAsyncPersistenceLoop(log serviceLogger, crash db.Accessor, ledger LedgerReader) *asyncPersistenceLoop {
	return &asyncPersistenceLoop{
		log:     log,
		crashDb: crash,
		ledger:  ledger,
		pending: make(chan persistentRequest, 1),
	}
}

func (p *asyncPersistenceLoop) Enqueue(clock timers.Clock[TimeoutType], round basics.Round, period period, step step, raw []byte, done chan error) (events <-chan externalEvent) {
	eventsChannel := make(chan externalEvent, 1)
	p.pending <- persistentRequest{
		round:  round,
		period: period,
		step:   step,
		raw:    raw,
		done:   done,
		clock:  clock,
		events: eventsChannel,
	}
	return eventsChannel
}

func (p *asyncPersistenceLoop) Start() {
	p.wg.Add(1)
	ctx, ctxExit := context.WithCancel(context.Background())
	p.ctxExit = ctxExit
	go p.loop(ctx)
}

func (p *asyncPersistenceLoop) Quit() {
	p.ctxExit()
	p.wg.Wait()
}

func (p *asyncPersistenceLoop) loop(ctx context.Context) {
	defer p.wg.Done()
	var s persistentRequest
	for {
		select {
		case <-ctx.Done():
			return
		case s, _ = <-p.pending:
		}

		// make sure that the ledger finished writing the previous round to disk.
		select {
		case <-ctx.Done():
			return
		case <-p.ledger.Wait(s.round.SubSaturate(1)):
		}

		// store the state.
		err := persist(p.log, p.crashDb, s.round, s.period, s.step, s.raw)

		s.events <- checkpointEvent{
			Round:  s.round,
			Period: s.period,
			Step:   s.step,
			Err:    makeSerErr(err),
			done:   s.done,
		}
		close(s.events)

		// sanity check; we check it after the fact, since it's not expected to ever happen.
		// performance-wise, it takes approximitly 300000ns to execute, and we don't want it to
		// block the persist operation.
		_, _, _, _, derr := decode(s.raw, s.clock, p.log, false)
		if derr != nil {
			p.log.Errorf("could not decode own encoded disk state: %v", derr)
		}
	}
}