/
lock_table_keeper.go
216 lines (192 loc) · 5.38 KB
/
lock_table_keeper.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
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
// Copyright 2023 Matrix Origin
//
// 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 lockservice
import (
"context"
"time"
"github.com/matrixorigin/matrixone/pkg/common/morpc"
"github.com/matrixorigin/matrixone/pkg/common/stopper"
pb "github.com/matrixorigin/matrixone/pkg/pb/lock"
)
type lockTableKeeper struct {
serviceID string
client Client
stopper *stopper.Stopper
keepLockTableBindInterval time.Duration
keepRemoteLockInterval time.Duration
tables *lockTableHolder
canDoKeep bool
}
// NewLockTableKeeper create a locktable keeper, an internal timer is started
// to send a keepalive request to the lockTableAllocator every interval, so this
// interval needs to be much smaller than the real lockTableAllocator's timeout.
func NewLockTableKeeper(
serviceID string,
client Client,
keepLockTableBindInterval time.Duration,
keepRemoteLockInterval time.Duration,
tables *lockTableHolder) LockTableKeeper {
s := &lockTableKeeper{
serviceID: serviceID,
client: client,
tables: tables,
keepLockTableBindInterval: keepLockTableBindInterval,
keepRemoteLockInterval: keepRemoteLockInterval,
stopper: stopper.NewStopper("lock-table-keeper",
stopper.WithLogger(getLogger().RawLogger())),
}
if err := s.stopper.RunTask(s.keepLockTableBind); err != nil {
panic(err)
}
if err := s.stopper.RunTask(s.keepRemoteLock); err != nil {
panic(err)
}
return s
}
func (k *lockTableKeeper) Close() error {
k.stopper.Stop()
return nil
}
func (k *lockTableKeeper) keepLockTableBind(ctx context.Context) {
defer getLogger().InfoAction("keep lock table bind task")()
timer := time.NewTimer(k.keepLockTableBindInterval)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return
case <-timer.C:
k.doKeepLockTableBind(ctx)
timer.Reset(k.keepLockTableBindInterval)
}
}
}
func (k *lockTableKeeper) keepRemoteLock(ctx context.Context) {
defer getLogger().InfoAction("keep remote locks task")()
timer := time.NewTimer(k.keepRemoteLockInterval)
defer timer.Stop()
services := make(map[string]pb.LockTable)
var futures []*morpc.Future
var binds []pb.LockTable
for {
select {
case <-ctx.Done():
return
case <-timer.C:
futures, binds = k.doKeepRemoteLock(
ctx,
futures,
services,
binds)
timer.Reset(k.keepRemoteLockInterval)
}
}
}
func (k *lockTableKeeper) doKeepRemoteLock(
ctx context.Context,
futures []*morpc.Future,
services map[string]pb.LockTable,
binds []pb.LockTable) ([]*morpc.Future, []pb.LockTable) {
for k := range services {
delete(services, k)
}
binds = binds[:0]
futures = futures[:0]
k.tables.iter(func(key uint64, value lockTable) bool {
bind := value.getBind()
if bind.ServiceID != k.serviceID {
services[bind.ServiceID] = bind
}
return true
})
if len(services) == 0 {
return futures[:0], binds[:0]
}
ctx, cancel := context.WithTimeout(ctx, defaultRPCTimeout)
defer cancel()
for _, bind := range services {
req := acquireRequest()
defer releaseRequest(req)
req.Method = pb.Method_KeepRemoteLock
req.LockTable = bind
req.KeepRemoteLock.ServiceID = k.serviceID
f, err := k.client.AsyncSend(ctx, req)
if err == nil {
futures = append(futures, f)
binds = append(binds, bind)
continue
}
logKeepRemoteLocksFailed(bind, err)
if !isRetryError(err) {
k.tables.removeWithFilter(func(_ uint64, v lockTable) bool {
return !v.getBind().Changed(bind)
})
}
}
for idx, f := range futures {
v, err := f.Get()
if err == nil {
releaseResponse(v.(*pb.Response))
} else {
logKeepRemoteLocksFailed(binds[idx], err)
}
f.Close()
futures[idx] = nil // gc
}
return futures[:0], binds[:0]
}
func (k *lockTableKeeper) doKeepLockTableBind(ctx context.Context) {
if !k.canDoKeep {
k.tables.iter(func(key uint64, value lockTable) bool {
bind := value.getBind()
if bind.ServiceID == k.serviceID {
k.canDoKeep = true
}
return true
})
}
if !k.canDoKeep {
return
}
req := acquireRequest()
defer releaseRequest(req)
req.Method = pb.Method_KeepLockTableBind
req.KeepLockTableBind.ServiceID = k.serviceID
ctx, cancel := context.WithTimeout(ctx, k.keepLockTableBindInterval)
defer cancel()
resp, err := k.client.Send(ctx, req)
if err != nil {
logKeepBindFailed(k.serviceID, err)
return
}
defer releaseResponse(resp)
if resp.KeepLockTableBind.OK {
return
}
n := 0
k.tables.removeWithFilter(func(key uint64, value lockTable) bool {
bind := value.getBind()
if bind.ServiceID == k.serviceID {
return true
}
n++
return false
})
if n > 0 {
// Keep bind receiving an explicit failure means that all the binds of the local
// locktable are invalid. We just need to remove it from the map, and the next
// time we access it, we will automatically get the latest bind from allocate.
logLocalBindsInvalid()
}
}