/
scheduler.go
135 lines (117 loc) · 3.53 KB
/
scheduler.go
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
/*
* Copyright 2019 Marco Helmich
*
* 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 scheduler
import (
"io"
"sync"
"github.com/mhelmich/calvin/pb"
"github.com/mhelmich/calvin/ulid"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
type Scheduler struct {
sequencerChan <-chan *pb.TransactionBatch
readyTxnsChan chan<- *pb.Transaction
doneTxnChan <-chan *pb.Transaction
lockMgr *lockManager
lowIsolationReads *sync.Map
logger *log.Entry
}
func NewScheduler(sequencerChan chan *pb.TransactionBatch, readyTxnsChan chan<- *pb.Transaction, doneTxnChan <-chan *pb.Transaction, srvr *grpc.Server, logger *log.Entry) *Scheduler {
lowIsolationReads := &sync.Map{}
s := &Scheduler{
sequencerChan: sequencerChan,
readyTxnsChan: readyTxnsChan,
doneTxnChan: doneTxnChan,
lockMgr: newLockManager(),
lowIsolationReads: lowIsolationReads,
logger: logger,
}
ss := newServer(sequencerChan, lowIsolationReads, logger)
pb.RegisterLowIsolationReadServer(srvr, ss)
go s.runLocker()
go s.runReleaser()
return s
}
func (s *Scheduler) runLocker() {
for {
batch, ok := <-s.sequencerChan
if !ok {
c := s.readyTxnsChan
s.readyTxnsChan = nil
close(c)
s.logger.Warningf("Stopping lock locker")
return
} else if batch == nil {
s.logger.Warningf("Received nil txn batch")
}
for idx := range batch.Transactions {
txn := batch.Transactions[idx]
if log.GetLevel() == log.DebugLevel {
id, _ := ulid.ParseIdFromProto(txn.Id)
s.logger.Debugf("getting locks for txn [%s]", id.String())
}
numLocksNotAcquired := s.lockMgr.lock(txn)
if numLocksNotAcquired == 0 {
if log.GetLevel() == log.DebugLevel {
id, _ := ulid.ParseIdFromProto(txn.Id)
s.logger.Debugf("txn [%s] became ready\n", id.String())
}
s.readyTxnsChan <- txn
}
}
}
}
func (s *Scheduler) runReleaser() {
for {
txn, ok := <-s.doneTxnChan
if !ok {
s.logger.Warningf("Stopping lock releaser")
return
}
if log.GetLevel() == log.DebugLevel {
id, _ := ulid.ParseIdFromProto(txn.Id)
s.logger.Debugf("txn [%s] became done\n", id.String())
}
// in addition to the regular stuff, low iso reads need
// the response out of the txn object to be sent on the response channel
if txn.IsLowIsolationRead {
id, _ := ulid.ParseIdFromProto(txn.Id)
txnID := id.String()
v, ok := s.lowIsolationReads.Load(txnID)
if !ok {
s.logger.Panicf("can't find low isolation read channel for txn [%s]", txnID)
}
c := v.(chan *pb.LowIsolationReadResponse)
c <- txn.LowIsolationReadResponse
close(c)
s.lowIsolationReads.Delete(txnID)
}
newOwners := s.lockMgr.release(txn)
for idx := range newOwners {
if log.GetLevel() == log.DebugLevel {
id, _ := ulid.ParseIdFromProto(newOwners[idx].Id)
s.logger.Debugf("txn [%s] became ready\n", id.String())
}
if s.readyTxnsChan != nil {
s.readyTxnsChan <- newOwners[idx]
}
}
}
}
func (s *Scheduler) LockChainToASCII(out io.Writer) {
s.lockMgr.lockChainToASCII(out)
}