File: wrapper.go

package info (click to toggle)
golang-github-canonicalltd-raft-test 0.0~git20180628.c3345b5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 284 kB
  • sloc: makefile: 2
file content (187 lines) | stat: -rw-r--r-- 4,621 bytes parent folder | download
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
// Copyright 2017 Canonical Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fsms

import (
	"encoding/binary"
	"io"
	"log"
	"sync"

	"github.com/CanonicalLtd/raft-test/internal/event"
	"github.com/hashicorp/raft"
	"github.com/pkg/errors"
)

// Wraps a raft.FSM, adding control on logs, snapshots and restores.
type fsmWrapper struct {
	logger *log.Logger

	// ID of of the raft server associated with this FSM.
	id raft.ServerID

	// Wrapped FSM
	fsm raft.FSM

	// Total number of commands applied by this FSM.
	commands uint64

	// Total number of snapshots performed on this FSM.
	snapshots uint64

	// Total number of restores performed on this FSM.
	restores uint64

	// Events that should be fired when a certain command log is events.
	events map[uint64][]*event.Event

	mu sync.RWMutex
}

func newFSMWrapper(logger *log.Logger, id raft.ServerID, fsm raft.FSM) *fsmWrapper {
	return &fsmWrapper{
		logger: logger,
		id:     id,
		fsm:    fsm,
		events: make(map[uint64][]*event.Event),
	}
}

func (f *fsmWrapper) Apply(log *raft.Log) interface{} {
	result := f.fsm.Apply(log)

	f.mu.Lock()
	f.commands++
	f.mu.Unlock()

	f.logger.Printf("[DEBUG] raft-test: fsm %s: applied %d", f.id, f.commands)
	if events, ok := f.events[f.commands]; ok {
		for _, event := range events {
			event.Fire()
			event.Block()
		}
	}

	return result
}

// Snapshot always return a dummy snapshot and no error without doing
// anything.
func (f *fsmWrapper) Snapshot() (raft.FSMSnapshot, error) {
	snapshot, err := f.fsm.Snapshot()

	if snapshot != nil {
		f.mu.Lock()
		f.snapshots++
		snapshot = &fsmSnapshotWrapper{
			commands: f.commands,
			snapshot: snapshot,
		}
		f.mu.Unlock()
	}

	return snapshot, err
}

// Restore always return a nil error without reading anything from
// the reader.
func (f *fsmWrapper) Restore(reader io.ReadCloser) error {
	if err := binary.Read(reader, binary.LittleEndian, &f.commands); err != nil {
		return errors.Wrap(err, "failed to restore commands count")
	}
	if err := f.fsm.Restore(reader); err != nil {
		return errors.Wrap(err, "failed to perform restore on user's FSM")
	}

	if events, ok := f.events[f.commands]; ok {
		for _, event := range events {
			event.Fire()
			event.Block()
		}
	}

	f.mu.Lock()
	f.restores++
	f.mu.Unlock()

	return nil
}

// This method must be called whenever the server associated with this FSM is
// about to transition to the leader state, and before any new command log is
// applied.
//
// It resets the internal state of the fsm, such as the list of applied command
// logs and the scheduled events.
func (f *fsmWrapper) electing() {
	f.mu.Lock()
	defer f.mu.Unlock()
	for n := range f.events {
		delete(f.events, n)
	}
}

// Return an event that will fire when the n'th command log for the term is
// applied on this FSM. It's assumed that this FSM is associated with the
// current leader.
func (f *fsmWrapper) whenApplied(n uint64) *event.Event {
	e := event.New()
	f.mu.RLock()
	defer f.mu.RUnlock()
	if f.commands >= n {
		// Fire immediately.
		go e.Fire()
	} else {
		_, ok := f.events[n]
		if !ok {
			f.events[n] = make([]*event.Event, 0)
		}
		f.events[n] = append(f.events[n], e)
	}
	return e
}

// Return the total number of command logs applied by this FSM.
func (f *fsmWrapper) Commands() uint64 {
	return f.commands
}

// Return the total number of snapshots performed by this FSM.
func (f *fsmWrapper) Snapshots() uint64 {
	return f.snapshots
}

// Return the total number of restores performed by this FSM.
func (f *fsmWrapper) Restores() uint64 {
	return f.restores
}

type fsmSnapshotWrapper struct {
	commands uint64
	snapshot raft.FSMSnapshot
}

func (s *fsmSnapshotWrapper) Persist(sink raft.SnapshotSink) error {
	// Augment the snapshot with the current command count.
	if err := binary.Write(sink, binary.LittleEndian, s.commands); err != nil {
		return errors.Wrap(err, "failed to augment snapshot with commands count")
	}
	if err := s.snapshot.Persist(sink); err != nil {
		return errors.Wrap(err, "failed to perform snapshot on user's FSM")
	}
	return nil
}

func (s *fsmSnapshotWrapper) Release() {}