-
Notifications
You must be signed in to change notification settings - Fork 800
/
sqlShardStore.go
304 lines (271 loc) · 10.8 KB
/
sqlShardStore.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
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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package sql
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/uber/cadence/common/persistence/serialization"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/persistence/sql/sqlplugin"
"github.com/uber/cadence/common/types"
)
type sqlShardStore struct {
sqlStore
currentClusterName string
}
// NewShardPersistence creates an instance of ShardStore
func NewShardPersistence(
db sqlplugin.DB,
currentClusterName string,
log log.Logger,
parser serialization.Parser,
) (persistence.ShardStore, error) {
return &sqlShardStore{
sqlStore: sqlStore{
db: db,
logger: log,
parser: parser,
},
currentClusterName: currentClusterName,
}, nil
}
func (m *sqlShardStore) CreateShard(
ctx context.Context,
request *persistence.InternalCreateShardRequest,
) error {
if _, err := m.GetShard(ctx, &persistence.InternalGetShardRequest{
ShardID: request.ShardInfo.ShardID,
}); err == nil {
return &persistence.ShardAlreadyExistError{
Msg: fmt.Sprintf("CreateShard operation failed. Shard with ID %v already exists.", request.ShardInfo.ShardID),
}
}
row, err := shardInfoToShardsRow(*request.ShardInfo, m.parser)
if err != nil {
return &types.InternalServiceError{
Message: fmt.Sprintf("CreateShard operation failed. Error: %v", err),
}
}
if _, err := m.db.InsertIntoShards(ctx, row); err != nil {
return convertCommonErrors(m.db, "CreateShard", "Failed to insert into shards table.", err)
}
return nil
}
func (m *sqlShardStore) GetShard(
ctx context.Context,
request *persistence.InternalGetShardRequest,
) (*persistence.InternalGetShardResponse, error) {
row, err := m.db.SelectFromShards(ctx, &sqlplugin.ShardsFilter{ShardID: int64(request.ShardID)})
if err != nil {
return nil, convertCommonErrors(m.db, "GetShard", fmt.Sprintf("Failed to get shard, ShardId: %v.", request.ShardID), err)
}
shardInfo, err := m.parser.ShardInfoFromBlob(row.Data, row.DataEncoding)
if err != nil {
return nil, err
}
if len(shardInfo.ClusterTransferAckLevel) == 0 {
shardInfo.ClusterTransferAckLevel = map[string]int64{
m.currentClusterName: shardInfo.GetTransferAckLevel(),
}
}
timerAckLevel := make(map[string]time.Time, len(shardInfo.ClusterTimerAckLevel))
for k, v := range shardInfo.ClusterTimerAckLevel {
timerAckLevel[k] = v
}
if len(timerAckLevel) == 0 {
timerAckLevel = map[string]time.Time{
m.currentClusterName: shardInfo.GetTimerAckLevel(),
}
}
if shardInfo.ClusterReplicationLevel == nil {
shardInfo.ClusterReplicationLevel = make(map[string]int64)
}
if shardInfo.ReplicationDlqAckLevel == nil {
shardInfo.ReplicationDlqAckLevel = make(map[string]int64)
}
var transferPQS *persistence.DataBlob
if shardInfo.GetTransferProcessingQueueStates() != nil {
transferPQS = &persistence.DataBlob{
Encoding: common.EncodingType(shardInfo.GetTransferProcessingQueueStatesEncoding()),
Data: shardInfo.GetTransferProcessingQueueStates(),
}
}
var crossClusterPQS *persistence.DataBlob
if shardInfo.GetCrossClusterProcessingQueueStates() != nil {
crossClusterPQS = &persistence.DataBlob{
Encoding: common.EncodingType(shardInfo.GetCrossClusterProcessingQueueStatesEncoding()),
Data: shardInfo.GetCrossClusterProcessingQueueStates(),
}
}
var timerPQS *persistence.DataBlob
if shardInfo.GetTimerProcessingQueueStates() != nil {
timerPQS = &persistence.DataBlob{
Encoding: common.EncodingType(shardInfo.GetTimerProcessingQueueStatesEncoding()),
Data: shardInfo.GetTimerProcessingQueueStates(),
}
}
resp := &persistence.InternalGetShardResponse{ShardInfo: &persistence.InternalShardInfo{
ShardID: int(row.ShardID),
RangeID: row.RangeID,
Owner: shardInfo.GetOwner(),
StolenSinceRenew: int(shardInfo.GetStolenSinceRenew()),
UpdatedAt: shardInfo.GetUpdatedAt(),
ReplicationAckLevel: shardInfo.GetReplicationAckLevel(),
TransferAckLevel: shardInfo.GetTransferAckLevel(),
TimerAckLevel: shardInfo.GetTimerAckLevel(),
ClusterTransferAckLevel: shardInfo.ClusterTransferAckLevel,
ClusterTimerAckLevel: timerAckLevel,
TransferProcessingQueueStates: transferPQS,
CrossClusterProcessingQueueStates: crossClusterPQS,
TimerProcessingQueueStates: timerPQS,
DomainNotificationVersion: shardInfo.GetDomainNotificationVersion(),
ClusterReplicationLevel: shardInfo.ClusterReplicationLevel,
ReplicationDLQAckLevel: shardInfo.ReplicationDlqAckLevel,
}}
return resp, nil
}
func (m *sqlShardStore) UpdateShard(
ctx context.Context,
request *persistence.InternalUpdateShardRequest,
) error {
row, err := shardInfoToShardsRow(*request.ShardInfo, m.parser)
if err != nil {
return &types.InternalServiceError{
Message: fmt.Sprintf("UpdateShard operation failed. Error: %v", err),
}
}
dbShardID := sqlplugin.GetDBShardIDFromHistoryShardID(request.ShardInfo.ShardID, m.db.GetTotalNumDBShards())
return m.txExecute(ctx, dbShardID, "UpdateShard", func(tx sqlplugin.Tx) error {
if err := lockShard(ctx, tx, request.ShardInfo.ShardID, request.PreviousRangeID); err != nil {
return err
}
result, err := tx.UpdateShards(ctx, row)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("rowsAffected returned error for shardID %v: %v", request.ShardInfo.ShardID, err)
}
if rowsAffected != 1 {
return fmt.Errorf("rowsAffected returned %v shards instead of one", rowsAffected)
}
return nil
})
}
// initiated by the owning shard
func lockShard(ctx context.Context, tx sqlplugin.Tx, shardID int, oldRangeID int64) error {
rangeID, err := tx.WriteLockShards(ctx, &sqlplugin.ShardsFilter{ShardID: int64(shardID)})
if err != nil {
if err == sql.ErrNoRows {
return &types.InternalServiceError{
Message: fmt.Sprintf("Failed to lock shard with ID %v that does not exist.", shardID),
}
}
return convertCommonErrors(tx, "lockShard", fmt.Sprintf("Failed to lock shard with ID: %v.", shardID), err)
}
if int64(rangeID) != oldRangeID {
return &persistence.ShardOwnershipLostError{
ShardID: shardID,
Msg: fmt.Sprintf("Failed to update shard. Previous range ID: %v; new range ID: %v", oldRangeID, rangeID),
}
}
return nil
}
// initiated by the owning shard
func readLockShard(ctx context.Context, tx sqlplugin.Tx, shardID int, oldRangeID int64) error {
rangeID, err := tx.ReadLockShards(ctx, &sqlplugin.ShardsFilter{ShardID: int64(shardID)})
if err != nil {
if err == sql.ErrNoRows {
return &types.InternalServiceError{
Message: fmt.Sprintf("Failed to lock shard with ID %v that does not exist.", shardID),
}
}
return convertCommonErrors(tx, "readLockShard", fmt.Sprintf("Failed to lock shard with ID: %v.", shardID), err)
}
if int64(rangeID) != oldRangeID {
return &persistence.ShardOwnershipLostError{
ShardID: shardID,
Msg: fmt.Sprintf("Failed to lock shard. Previous range ID: %v; new range ID: %v", oldRangeID, rangeID),
}
}
return nil
}
func shardInfoToShardsRow(s persistence.InternalShardInfo, parser serialization.Parser) (*sqlplugin.ShardsRow, error) {
var markerData []byte
markerEncoding := string(common.EncodingTypeEmpty)
if s.PendingFailoverMarkers != nil {
markerData = s.PendingFailoverMarkers.Data
markerEncoding = string(s.PendingFailoverMarkers.Encoding)
}
var transferPQSData []byte
transferPQSEncoding := string(common.EncodingTypeEmpty)
if s.TransferProcessingQueueStates != nil {
transferPQSData = s.TransferProcessingQueueStates.Data
transferPQSEncoding = string(s.TransferProcessingQueueStates.Encoding)
}
var crossClusterPQS []byte
crossClusterPQSEncoding := string(common.EncodingTypeEmpty)
if s.CrossClusterProcessingQueueStates != nil {
crossClusterPQS = s.CrossClusterProcessingQueueStates.Data
crossClusterPQSEncoding = string(s.CrossClusterProcessingQueueStates.Encoding)
}
var timerPQSData []byte
timerPQSEncoding := string(common.EncodingTypeEmpty)
if s.TimerProcessingQueueStates != nil {
timerPQSData = s.TimerProcessingQueueStates.Data
timerPQSEncoding = string(s.TimerProcessingQueueStates.Encoding)
}
shardInfo := &serialization.ShardInfo{
StolenSinceRenew: int32(s.StolenSinceRenew),
UpdatedAt: s.UpdatedAt,
ReplicationAckLevel: s.ReplicationAckLevel,
TransferAckLevel: s.TransferAckLevel,
TimerAckLevel: s.TimerAckLevel,
ClusterTransferAckLevel: s.ClusterTransferAckLevel,
ClusterTimerAckLevel: s.ClusterTimerAckLevel,
TransferProcessingQueueStates: transferPQSData,
TransferProcessingQueueStatesEncoding: transferPQSEncoding,
CrossClusterProcessingQueueStates: crossClusterPQS,
CrossClusterProcessingQueueStatesEncoding: crossClusterPQSEncoding,
TimerProcessingQueueStates: timerPQSData,
TimerProcessingQueueStatesEncoding: timerPQSEncoding,
DomainNotificationVersion: s.DomainNotificationVersion,
Owner: s.Owner,
ClusterReplicationLevel: s.ClusterReplicationLevel,
ReplicationDlqAckLevel: s.ReplicationDLQAckLevel,
PendingFailoverMarkers: markerData,
PendingFailoverMarkersEncoding: markerEncoding,
}
blob, err := parser.ShardInfoToBlob(shardInfo)
if err != nil {
return nil, err
}
return &sqlplugin.ShardsRow{
ShardID: int64(s.ShardID),
RangeID: s.RangeID,
Data: blob.Data,
DataEncoding: string(blob.Encoding),
}, nil
}