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
|
// Copyright 2018 The dexon-consensus-core Authors
// This file is part of the dexon-consensus-core library.
//
// The dexon-consensus-core library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus-core library 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 Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus-core library. If not, see
// <http://www.gnu.org/licenses/>.
package test
import (
"container/heap"
"context"
"fmt"
"sync"
"time"
"github.com/dexon-foundation/dexon-consensus-core/core/types"
)
var (
// ErrSchedulerAlreadyStarted means callers attempt to insert some
// seed events after calling 'Run'.
ErrSchedulerAlreadyStarted = fmt.Errorf("scheduler already started")
// errNilEventWhenNotified is an internal error which means a worker routine
// can't get an event when notified.
errNilEventWhenNotified = fmt.Errorf("nil event when notified")
)
type schedulerHandlerRecord struct {
handler EventHandler
lock sync.Mutex
}
// Scheduler is an event scheduler.
type Scheduler struct {
events eventQueue
eventsLock sync.Mutex
history []*Event
historyLock sync.RWMutex
isStarted bool
handlers map[types.ValidatorID]*schedulerHandlerRecord
handlersLock sync.RWMutex
eventNotification chan struct{}
ctx context.Context
cancelFunc context.CancelFunc
stopper Stopper
}
// NewScheduler constructs an Scheduler instance.
func NewScheduler(stopper Stopper) *Scheduler {
ctx, cancel := context.WithCancel(context.Background())
return &Scheduler{
events: eventQueue{},
history: []*Event{},
handlers: make(map[types.ValidatorID]*schedulerHandlerRecord),
eventNotification: make(chan struct{}, 100000),
ctx: ctx,
cancelFunc: cancel,
stopper: stopper,
}
}
// Run would run the scheduler. If you need strict incrememtal execution order
// of events based on their 'Time' field, assign 'numWorkers' as 1. If you need
// faster execution, assign 'numWorkers' a larger number.
func (sch *Scheduler) Run(numWorkers int) {
var wg sync.WaitGroup
sch.isStarted = true
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go sch.workerRoutine(&wg)
}
// Blocks until all routines are finished.
wg.Wait()
}
// Seed is used to provide the scheduler some seed events.
func (sch *Scheduler) Seed(e *Event) error {
sch.eventsLock.Lock()
defer sch.eventsLock.Unlock()
if sch.isStarted {
return ErrSchedulerAlreadyStarted
}
sch.addEvent(e)
return nil
}
// RegisterEventHandler register an event handler by providing ID of
// corresponding validator.
func (sch *Scheduler) RegisterEventHandler(
vID types.ValidatorID,
handler EventHandler) {
sch.handlersLock.Lock()
defer sch.handlersLock.Unlock()
sch.handlers[vID] = &schedulerHandlerRecord{handler: handler}
}
// nextTick would pick the oldest event from eventQueue.
func (sch *Scheduler) nextTick() (e *Event) {
sch.eventsLock.Lock()
defer sch.eventsLock.Unlock()
if len(sch.events) == 0 {
return nil
}
return heap.Pop(&sch.events).(*Event)
}
// addEvent is an helper function to add events into eventQueue sorted by
// their 'Time' field.
func (sch *Scheduler) addEvent(e *Event) {
// Perform sorted insertion.
heap.Push(&sch.events, e)
sch.eventNotification <- struct{}{}
}
// CloneExecutionHistory returns a cloned event execution history.
func (sch *Scheduler) CloneExecutionHistory() (cloned []*Event) {
sch.historyLock.RLock()
defer sch.historyLock.RUnlock()
cloned = make([]*Event, len(sch.history))
copy(cloned, sch.history)
return
}
// workerRoutine is the mainloop when handling events.
func (sch *Scheduler) workerRoutine(wg *sync.WaitGroup) {
defer wg.Done()
handleEvent := func(e *Event) {
// Find correspond handler record.
hRec := func(vID types.ValidatorID) *schedulerHandlerRecord {
sch.handlersLock.RLock()
defer sch.handlersLock.RUnlock()
return sch.handlers[vID]
}(e.ValidatorID)
newEvents := func() []*Event {
// This lock makes sure there would be no concurrent access
// against each handler.
hRec.lock.Lock()
defer hRec.lock.Unlock()
// Handle incoming event, and record its execution time.
beforeExecution := time.Now().UTC()
newEvents := hRec.handler.Handle(e)
e.ExecInterval = time.Now().UTC().Sub(beforeExecution)
// It's safe to check status of that validator under 'hRec.lock'.
if sch.stopper.ShouldStop(e.ValidatorID) {
sch.cancelFunc()
}
// Include the execution interval of parent event to the expected time
// to execute child events.
for _, newEvent := range newEvents {
newEvent.ParentTime = e.Time
newEvent.Time = newEvent.Time.Add(e.ExecInterval)
}
return newEvents
}()
// Record executed events as history.
func() {
sch.historyLock.Lock()
defer sch.historyLock.Unlock()
sch.history = append(sch.history, e)
}()
// Add derivated events back to event queue.
func() {
sch.eventsLock.Lock()
defer sch.eventsLock.Unlock()
for _, newEvent := range newEvents {
sch.addEvent(newEvent)
}
}()
}
Done:
for {
// We favor scheduler-shutdown signal than other events.
select {
case <-sch.ctx.Done():
break Done
default:
}
// Block until new event arrival or scheduler shutdown.
select {
case <-sch.eventNotification:
e := sch.nextTick()
if e == nil {
panic(errNilEventWhenNotified)
}
handleEvent(e)
case <-sch.ctx.Done():
break Done
}
}
}
|