/
cassandraHistoryPersistence.go
213 lines (187 loc) · 6.92 KB
/
cassandraHistoryPersistence.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
// 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 persistence
import (
"fmt"
"github.com/gocql/gocql"
"github.com/uber-common/bark"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
)
const (
templateAppendHistoryEvents = `INSERT INTO events (` +
`domain_id, workflow_id, run_id, first_event_id, range_id, tx_id, data, data_encoding, data_version) ` +
`VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS`
templateOverwriteHistoryEvents = `UPDATE events ` +
`SET range_id = ?, tx_id = ?, data = ?, data_encoding = ?, data_version = ? ` +
`WHERE domain_id = ? AND workflow_id = ? AND run_id = ? AND first_event_id = ? ` +
`IF range_id <= ? AND tx_id < ?`
templateGetWorkflowExecutionHistory = `SELECT first_event_id, data, data_encoding, data_version FROM events ` +
`WHERE domain_id = ? ` +
`AND workflow_id = ? ` +
`AND run_id = ? ` +
`AND first_event_id >= ? ` +
`AND first_event_id < ?`
templateDeleteWorkflowExecutionHistory = `DELETE FROM events ` +
`WHERE domain_id = ? ` +
`AND workflow_id = ? ` +
`AND run_id = ? `
)
type (
cassandraHistoryPersistence struct {
session *gocql.Session
logger bark.Logger
}
)
// NewCassandraHistoryPersistence is used to create an instance of HistoryManager implementation
func NewCassandraHistoryPersistence(hosts string, port int, user, password, dc string, keyspace string,
numConns int, logger bark.Logger) (HistoryManager,
error) {
cluster := common.NewCassandraCluster(hosts, port, user, password, dc)
cluster.Keyspace = keyspace
cluster.ProtoVersion = cassandraProtoVersion
cluster.Consistency = gocql.LocalQuorum
cluster.SerialConsistency = gocql.LocalSerial
cluster.Timeout = defaultSessionTimeout
cluster.NumConns = numConns
session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
return &cassandraHistoryPersistence{session: session, logger: logger}, nil
}
// Close gracefully releases the resources held by this object
func (h *cassandraHistoryPersistence) Close() {
if h.session != nil {
h.session.Close()
}
}
func (h *cassandraHistoryPersistence) AppendHistoryEvents(request *AppendHistoryEventsRequest) error {
var query *gocql.Query
if request.Overwrite {
query = h.session.Query(templateOverwriteHistoryEvents,
request.RangeID,
request.TransactionID,
request.Events.Data,
request.Events.EncodingType,
request.Events.Version,
request.DomainID,
*request.Execution.WorkflowId,
*request.Execution.RunId,
request.FirstEventID,
request.RangeID,
request.TransactionID)
} else {
query = h.session.Query(templateAppendHistoryEvents,
request.DomainID,
*request.Execution.WorkflowId,
*request.Execution.RunId,
request.FirstEventID,
request.RangeID,
request.TransactionID,
request.Events.Data,
request.Events.EncodingType,
request.Events.Version)
}
previous := make(map[string]interface{})
applied, err := query.MapScanCAS(previous)
if err != nil {
if isThrottlingError(err) {
return &workflow.ServiceBusyError{
Message: fmt.Sprintf("AppendHistoryEvents operation failed. Error: %v", err),
}
} else if isTimeoutError(err) {
// Write may have succeeded, but we don't know
// return this info to the caller so they have the option of trying to find out by executing a read
return &TimeoutError{Msg: fmt.Sprintf("AppendHistoryEvents timed out. Error: %v", err)}
}
return &workflow.InternalServiceError{
Message: fmt.Sprintf("AppendHistoryEvents operation failed. Error: %v", err),
}
}
if !applied {
return &ConditionFailedError{
Msg: "Failed to append history events.",
}
}
return nil
}
func (h *cassandraHistoryPersistence) GetWorkflowExecutionHistory(request *GetWorkflowExecutionHistoryRequest) (
*GetWorkflowExecutionHistoryResponse, error) {
execution := request.Execution
query := h.session.Query(templateGetWorkflowExecutionHistory,
request.DomainID,
*execution.WorkflowId,
*execution.RunId,
request.FirstEventID,
request.NextEventID)
iter := query.PageSize(request.PageSize).PageState(request.NextPageToken).Iter()
if iter == nil {
return nil, &workflow.InternalServiceError{
Message: "GetWorkflowExecutionHistory operation failed. Not able to create query iterator.",
}
}
var firstEventID int64
var history SerializedHistoryEventBatch
response := &GetWorkflowExecutionHistoryResponse{}
found := false
for iter.Scan(&firstEventID, &history.Data, &history.EncodingType, &history.Version) {
found = true
response.Events = append(response.Events, history)
history = SerializedHistoryEventBatch{}
}
nextPageToken := iter.PageState()
response.NextPageToken = make([]byte, len(nextPageToken))
copy(response.NextPageToken, nextPageToken)
if err := iter.Close(); err != nil {
return nil, &workflow.InternalServiceError{
Message: fmt.Sprintf("GetWorkflowExecutionHistory operation failed. Error: %v", err),
}
}
if !found && len(request.NextPageToken) == 0 {
// adding the check of request next token being not nil, since
// there can be case when found == false at the very end of pagination.
return nil, &workflow.EntityNotExistsError{
Message: fmt.Sprintf("Workflow execution history not found. WorkflowId: %v, RunId: %v",
*execution.WorkflowId, *execution.RunId),
}
}
return response, nil
}
func (h *cassandraHistoryPersistence) DeleteWorkflowExecutionHistory(
request *DeleteWorkflowExecutionHistoryRequest) error {
execution := request.Execution
query := h.session.Query(templateDeleteWorkflowExecutionHistory,
request.DomainID,
*execution.WorkflowId,
*execution.RunId)
err := query.Exec()
if err != nil {
if isThrottlingError(err) {
return &workflow.ServiceBusyError{
Message: fmt.Sprintf("DeleteWorkflowExecutionHistory operation failed. Error: %v", err),
}
}
return &workflow.InternalServiceError{
Message: fmt.Sprintf("DeleteWorkflowExecutionHistory operation failed. Error: %v", err),
}
}
return nil
}