-
Notifications
You must be signed in to change notification settings - Fork 800
/
historyIterator.go
278 lines (247 loc) · 9.07 KB
/
historyIterator.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
// Copyright (c) 2017 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.
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination historyIterator_mock.go -self_package github.com/uber/cadence/common/archiver
package archiver
import (
"context"
"encoding/json"
"errors"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/persistence"
persistenceutils "github.com/uber/cadence/common/persistence/persistence-utils"
"github.com/uber/cadence/common/types"
)
const (
historyPageSize = 250
)
type (
// HistoryIterator is used to get history batches
HistoryIterator interface {
Next() (*HistoryBlob, error)
HasNext() bool
GetState() ([]byte, error)
}
// HistoryBlobHeader is the header attached to all history blobs
HistoryBlobHeader struct {
DomainName *string `json:"domain_name,omitempty"`
DomainID *string `json:"domain_id,omitempty"`
WorkflowID *string `json:"workflow_id,omitempty"`
RunID *string `json:"run_id,omitempty"`
IsLast *bool `json:"is_last,omitempty"`
FirstFailoverVersion *int64 `json:"first_failover_version,omitempty"`
LastFailoverVersion *int64 `json:"last_failover_version,omitempty"`
FirstEventID *int64 `json:"first_event_id,omitempty"`
LastEventID *int64 `json:"last_event_id,omitempty"`
EventCount *int64 `json:"event_count,omitempty"`
}
// HistoryBlob is the serializable data that forms the body of a blob
HistoryBlob struct {
Header *HistoryBlobHeader `json:"header"`
Body []*types.History `json:"body"`
}
historyIteratorState struct {
NextEventID int64
FinishedIteration bool
}
historyIterator struct {
historyIteratorState
ctx context.Context
request *ArchiveHistoryRequest
historyV2Manager persistence.HistoryManager
sizeEstimator SizeEstimator
historyPageSize int
targetHistoryBlobSize int
}
)
var (
errIteratorDepleted = errors.New("iterator is depleted")
)
// NewHistoryIterator returns a new HistoryIterator
func NewHistoryIterator(
ctx context.Context,
request *ArchiveHistoryRequest,
historyV2Manager persistence.HistoryManager,
targetHistoryBlobSize int,
) HistoryIterator {
return newHistoryIterator(ctx, request, historyV2Manager, targetHistoryBlobSize)
}
// NewHistoryIteratorFromState returns a new HistoryIterator with specified state
func NewHistoryIteratorFromState(
ctx context.Context,
request *ArchiveHistoryRequest,
historyV2Manager persistence.HistoryManager,
targetHistoryBlobSize int,
initialState []byte,
) (HistoryIterator, error) {
it := newHistoryIterator(ctx, request, historyV2Manager, targetHistoryBlobSize)
if initialState == nil {
return it, nil
}
if err := it.reset(initialState); err != nil {
return nil, err
}
return it, nil
}
func newHistoryIterator(
ctx context.Context,
request *ArchiveHistoryRequest,
historyV2Manager persistence.HistoryManager,
targetHistoryBlobSize int,
) *historyIterator {
return &historyIterator{
historyIteratorState: historyIteratorState{
NextEventID: common.FirstEventID,
FinishedIteration: false,
},
ctx: ctx,
request: request,
historyV2Manager: historyV2Manager,
historyPageSize: historyPageSize,
targetHistoryBlobSize: targetHistoryBlobSize,
sizeEstimator: NewJSONSizeEstimator(),
}
}
func (i *historyIterator) Next() (*HistoryBlob, error) {
if !i.HasNext() {
return nil, errIteratorDepleted
}
historyBatches, newIterState, err := i.readHistoryBatches(i.ctx, i.NextEventID)
if err != nil {
return nil, err
}
i.historyIteratorState = newIterState
firstEvent := historyBatches[0].Events[0]
lastBatch := historyBatches[len(historyBatches)-1]
lastEvent := lastBatch.Events[len(lastBatch.Events)-1]
eventCount := int64(0)
for _, batch := range historyBatches {
eventCount += int64(len(batch.Events))
}
header := &HistoryBlobHeader{
DomainName: common.StringPtr(i.request.DomainName),
DomainID: common.StringPtr(i.request.DomainID),
WorkflowID: common.StringPtr(i.request.WorkflowID),
RunID: common.StringPtr(i.request.RunID),
IsLast: common.BoolPtr(i.FinishedIteration),
FirstFailoverVersion: common.Int64Ptr(firstEvent.Version),
LastFailoverVersion: common.Int64Ptr(lastEvent.Version),
FirstEventID: common.Int64Ptr(firstEvent.ID),
LastEventID: common.Int64Ptr(lastEvent.ID),
EventCount: common.Int64Ptr(eventCount),
}
return &HistoryBlob{
Header: header,
Body: historyBatches,
}, nil
}
// HasNext returns true if there are more items to iterate over.
func (i *historyIterator) HasNext() bool {
return !i.FinishedIteration
}
// GetState returns the encoded iterator state
func (i *historyIterator) GetState() ([]byte, error) {
return json.Marshal(i.historyIteratorState)
}
func (i *historyIterator) readHistoryBatches(ctx context.Context, firstEventID int64) ([]*types.History, historyIteratorState, error) {
size := 0
targetSize := i.targetHistoryBlobSize
var historyBatches []*types.History
newIterState := historyIteratorState{}
for size < targetSize {
currHistoryBatches, err := i.readHistory(ctx, firstEventID)
if _, ok := err.(*types.EntityNotExistsError); ok && firstEventID != common.FirstEventID {
newIterState.FinishedIteration = true
return historyBatches, newIterState, nil
}
if err != nil {
return nil, newIterState, err
}
for idx, batch := range currHistoryBatches {
historyBatchSize, err := i.sizeEstimator.EstimateSize(batch)
if err != nil {
return nil, newIterState, err
}
size += historyBatchSize
historyBatches = append(historyBatches, batch)
firstEventID = batch.Events[len(batch.Events)-1].ID + 1
// In case targetSize is satisfied before reaching the end of current set of batches, return immediately.
// Otherwise, we need to look ahead to see if there's more history batches.
if size >= targetSize && idx != len(currHistoryBatches)-1 {
newIterState.FinishedIteration = false
newIterState.NextEventID = firstEventID
return historyBatches, newIterState, nil
}
}
}
// If you are here, it means the target size is met after adding the last batch of read history.
// We need to check if there's more history batches.
_, err := i.readHistory(ctx, firstEventID)
if _, ok := err.(*types.EntityNotExistsError); ok && firstEventID != common.FirstEventID {
newIterState.FinishedIteration = true
return historyBatches, newIterState, nil
}
if err != nil {
return nil, newIterState, err
}
newIterState.FinishedIteration = false
newIterState.NextEventID = firstEventID
return historyBatches, newIterState, nil
}
func (i *historyIterator) readHistory(ctx context.Context, firstEventID int64) ([]*types.History, error) {
req := &persistence.ReadHistoryBranchRequest{
BranchToken: i.request.BranchToken,
MinEventID: firstEventID,
MaxEventID: common.EndEventID,
PageSize: i.historyPageSize,
ShardID: common.IntPtr(i.request.ShardID),
DomainName: i.request.DomainName,
}
historyBatches, _, _, err := persistenceutils.ReadFullPageV2EventsByBatch(ctx, i.historyV2Manager, req)
return historyBatches, err
}
// reset resets iterator to a certain state given its encoded representation
// if it returns an error, the operation will have no effect on the iterator
func (i *historyIterator) reset(stateToken []byte) error {
var iteratorState historyIteratorState
if err := json.Unmarshal(stateToken, &iteratorState); err != nil {
return err
}
i.historyIteratorState = iteratorState
return nil
}
type (
// SizeEstimator is used to estimate the size of any object
SizeEstimator interface {
EstimateSize(v interface{}) (int, error)
}
jsonSizeEstimator struct{}
)
func (e *jsonSizeEstimator) EstimateSize(v interface{}) (int, error) {
data, err := json.Marshal(v)
if err != nil {
return 0, err
}
return len(data), nil
}
// NewJSONSizeEstimator returns a new SizeEstimator which uses json encoding to
// estimate size
func NewJSONSizeEstimator() SizeEstimator {
return &jsonSizeEstimator{}
}