forked from uber/cadence
/
eventsCache.go
204 lines (176 loc) · 6.99 KB
/
eventsCache.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
// 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.
package history
import (
"time"
"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
)
type (
eventsCache interface {
getEvent(domainID, workflowID, runID string, firstEventID, eventID int64, eventStoreVersion int32,
branchToken []byte) (*shared.HistoryEvent, error)
putEvent(domainID, workflowID, runID string, eventID int64, event *shared.HistoryEvent)
deleteEvent(domainID, workflowID, runID string, eventID int64)
}
eventsCacheImpl struct {
cache.Cache
eventsMgr persistence.HistoryManager
eventsV2Mgr persistence.HistoryV2Manager
disabled bool
logger log.Logger
metricsClient metrics.Client
shardID *int
}
eventKey struct {
domainID string
workflowID string
runID string
eventID int64
}
)
var (
errEventNotFoundInBatch = &shared.InternalServiceError{Message: "History event not found within expected batch"}
)
var _ eventsCache = (*eventsCacheImpl)(nil)
func newEventsCache(shardCtx ShardContext) eventsCache {
config := shardCtx.GetConfig()
shardID := common.IntPtr(shardCtx.GetShardID())
return newEventsCacheWithOptions(config.EventsCacheInitialSize(), config.EventsCacheMaxSize(), config.EventsCacheTTL(),
shardCtx.GetHistoryManager(), shardCtx.GetHistoryV2Manager(), false, shardCtx.GetLogger(), shardCtx.GetMetricsClient(), shardID)
}
func newEventsCacheWithOptions(initialSize, maxSize int, ttl time.Duration, eventsMgr persistence.HistoryManager,
eventsV2Mgr persistence.HistoryV2Manager, disabled bool, logger log.Logger, metrics metrics.Client, shardID *int) *eventsCacheImpl {
opts := &cache.Options{}
opts.InitialCapacity = initialSize
opts.TTL = ttl
return &eventsCacheImpl{
Cache: cache.New(maxSize, opts),
eventsMgr: eventsMgr,
eventsV2Mgr: eventsV2Mgr,
disabled: disabled,
logger: logger.WithTags(tag.ComponentEventsCache),
metricsClient: metrics,
shardID: shardID,
}
}
func newEventKey(domainID, workflowID, runID string, eventID int64) eventKey {
return eventKey{
domainID: domainID,
workflowID: workflowID,
runID: runID,
eventID: eventID,
}
}
func (e *eventsCacheImpl) getEvent(domainID, workflowID, runID string, firstEventID, eventID int64, eventStoreVersion int32,
branchToken []byte) (*shared.HistoryEvent, error) {
e.metricsClient.IncCounter(metrics.EventsCacheGetEventScope, metrics.CacheRequests)
sw := e.metricsClient.StartTimer(metrics.EventsCacheGetEventScope, metrics.CacheLatency)
defer sw.Stop()
key := newEventKey(domainID, workflowID, runID, eventID)
// Test hook for disabling cache
if !e.disabled {
event, cacheHit := e.Cache.Get(key).(*shared.HistoryEvent)
if cacheHit {
return event, nil
}
}
e.metricsClient.IncCounter(metrics.EventsCacheGetEventScope, metrics.CacheMissCounter)
event, err := e.getHistoryEventFromStore(domainID, workflowID, runID, firstEventID, eventID, eventStoreVersion, branchToken)
if err != nil {
e.metricsClient.IncCounter(metrics.EventsCacheGetEventScope, metrics.CacheFailures)
e.logger.Error("EventsCache unable to retrieve event from store",
tag.Error(err),
tag.WorkflowID(workflowID),
tag.WorkflowRunID(runID),
tag.WorkflowDomainID(domainID),
tag.WorkflowEventID(eventID))
return nil, err
}
e.Put(key, event)
return event, nil
}
func (e *eventsCacheImpl) putEvent(domainID, workflowID, runID string, eventID int64, event *shared.HistoryEvent) {
e.metricsClient.IncCounter(metrics.EventsCachePutEventScope, metrics.CacheRequests)
sw := e.metricsClient.StartTimer(metrics.EventsCachePutEventScope, metrics.CacheLatency)
defer sw.Stop()
key := newEventKey(domainID, workflowID, runID, eventID)
e.Put(key, event)
}
func (e *eventsCacheImpl) deleteEvent(domainID, workflowID, runID string, eventID int64) {
e.metricsClient.IncCounter(metrics.EventsCacheDeleteEventScope, metrics.CacheRequests)
sw := e.metricsClient.StartTimer(metrics.EventsCacheDeleteEventScope, metrics.CacheLatency)
defer sw.Stop()
key := newEventKey(domainID, workflowID, runID, eventID)
e.Delete(key)
}
func (e *eventsCacheImpl) getHistoryEventFromStore(domainID, workflowID, runID string, firstEventID, eventID int64,
eventStoreVersion int32, branchToken []byte) (*shared.HistoryEvent, error) {
e.metricsClient.IncCounter(metrics.EventsCacheGetFromStoreScope, metrics.CacheRequests)
sw := e.metricsClient.StartTimer(metrics.EventsCacheGetFromStoreScope, metrics.CacheLatency)
defer sw.Stop()
var historyEvents []*shared.HistoryEvent
if eventStoreVersion == persistence.EventStoreVersionV2 {
response, err := e.eventsV2Mgr.ReadHistoryBranch(&persistence.ReadHistoryBranchRequest{
BranchToken: branchToken,
MinEventID: firstEventID,
MaxEventID: eventID + 1,
PageSize: 1,
NextPageToken: nil,
ShardID: e.shardID,
})
if err != nil {
e.metricsClient.IncCounter(metrics.EventsCacheGetFromStoreScope, metrics.CacheFailures)
return nil, err
}
historyEvents = response.HistoryEvents
} else {
response, err := e.eventsMgr.GetWorkflowExecutionHistory(&persistence.GetWorkflowExecutionHistoryRequest{
DomainID: domainID,
Execution: shared.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: common.StringPtr(runID),
},
FirstEventID: firstEventID,
NextEventID: eventID + 1,
PageSize: 1,
NextPageToken: nil,
})
if err != nil {
e.metricsClient.IncCounter(metrics.EventsCacheGetFromStoreScope, metrics.CacheFailures)
return nil, err
}
if response.History != nil {
historyEvents = response.History.Events
}
}
// find history event from batch and return back single event to caller
for _, e := range historyEvents {
if e.GetEventId() == eventID {
return e, nil
}
}
return nil, errEventNotFoundInBatch
}