/
historySerializer.go
191 lines (160 loc) · 6.43 KB
/
historySerializer.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
// 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 (
"encoding/json"
"fmt"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"sync/atomic"
)
type (
// HistorySerializer is used to serialize/deserialize history
HistorySerializer interface {
Serialize(batch *HistoryEventBatch) (*SerializedHistoryEventBatch, error)
Deserialize(batch *SerializedHistoryEventBatch) (*HistoryEventBatch, error)
}
// HistorySerializerFactory is a factory that vends
// HistorySerializers based on encoding type.
HistorySerializerFactory interface {
// Get returns a history serializer corresponding
// to a given encoding type
Get(encodingType common.EncodingType) (HistorySerializer, error)
}
// HistorySerializationError is an error type that's
// returned on a history serialization failure
HistorySerializationError struct {
msg string
}
// HistoryDeserializationError is an error type that's
// returned on a history deserialization failure
HistoryDeserializationError struct {
msg string
}
// UnknownEncodingTypeError is an error type that's
// returned when the encoding type provided as input
// is unknown or unsupported
UnknownEncodingTypeError struct {
encodingType common.EncodingType
}
// HistoryVersionCompatibilityError is an error type
// that's returned when history serialization or
// deserialization cannot proceed due to version
// incompatibility
HistoryVersionCompatibilityError struct {
requiredVersion int
supportedVersion int
}
jsonHistorySerializer struct{}
serializerFactoryImpl struct {
jsonSerializer HistorySerializer
}
)
const (
// DefaultEncodingType is the default encoding format for persisted history
DefaultEncodingType = common.EncodingTypeJSON
)
var defaultHistoryVersion = int32(1)
var maxSupportedHistoryVersion = int32(1)
// NewJSONHistorySerializer returns a JSON HistorySerializer
func NewJSONHistorySerializer() HistorySerializer {
return &jsonHistorySerializer{}
}
func (j *jsonHistorySerializer) Serialize(batch *HistoryEventBatch) (*SerializedHistoryEventBatch, error) {
if batch.Version > GetMaxSupportedHistoryVersion() {
err := NewHistoryVersionCompatibilityError(batch.Version, GetMaxSupportedHistoryVersion())
return nil, &HistorySerializationError{msg: err.Error()}
}
data, err := json.Marshal(batch.Events)
if err != nil {
return nil, &HistorySerializationError{msg: err.Error()}
}
return NewSerializedHistoryEventBatch(data, common.EncodingTypeJSON, batch.Version), nil
}
func (j *jsonHistorySerializer) Deserialize(batch *SerializedHistoryEventBatch) (*HistoryEventBatch, error) {
if batch.Version > GetMaxSupportedHistoryVersion() {
err := NewHistoryVersionCompatibilityError(batch.Version, GetMaxSupportedHistoryVersion())
return nil, &HistoryDeserializationError{msg: err.Error()}
}
var events []*workflow.HistoryEvent
err := json.Unmarshal(batch.Data, &events)
if err != nil {
return nil, &HistoryDeserializationError{msg: err.Error()}
}
return &HistoryEventBatch{Version: batch.Version, Events: events}, nil
}
// NewHistorySerializerFactory creates and returns an instance
// of HistorySerializerFactory
func NewHistorySerializerFactory() HistorySerializerFactory {
return &serializerFactoryImpl{
jsonSerializer: NewJSONHistorySerializer(),
}
}
// Get returns the serializer corresponding to the given encoding type
func (f *serializerFactoryImpl) Get(encodingType common.EncodingType) (HistorySerializer, error) {
switch encodingType {
case common.EncodingTypeJSON:
return f.jsonSerializer, nil
default:
return nil, NewUnknownEncodingTypeError(encodingType)
}
}
// NewUnknownEncodingTypeError returns a new instance of encoding type error
func NewUnknownEncodingTypeError(encodingType common.EncodingType) error {
return &UnknownEncodingTypeError{encodingType: encodingType}
}
func (e *UnknownEncodingTypeError) Error() string {
return fmt.Sprintf("unknown or unsupported encoding type %v", e.encodingType)
}
// NewHistoryVersionCompatibilityError returns a new instance of compatibility error type
func NewHistoryVersionCompatibilityError(required int, supported int) error {
return &HistoryVersionCompatibilityError{
requiredVersion: required,
supportedVersion: supported,
}
}
func (e *HistoryVersionCompatibilityError) Error() string {
return fmt.Sprintf("incompatible history version;required=%v;maxSupported=%v",
e.requiredVersion, e.supportedVersion)
}
func (e *HistorySerializationError) Error() string {
return fmt.Sprintf("history serialization error: %v", e.msg)
}
func (e *HistoryDeserializationError) Error() string {
return fmt.Sprintf("history deserialization error: %v", e.msg)
}
// SetMaxSupportedHistoryVersion resets the max supported history version
// this method is only intended for integration test
func SetMaxSupportedHistoryVersion(version int) {
atomic.StoreInt32(&maxSupportedHistoryVersion, int32(version))
}
// GetMaxSupportedHistoryVersion returns the max supported version
func GetMaxSupportedHistoryVersion() int {
return int(atomic.LoadInt32(&maxSupportedHistoryVersion))
}
// SetDefaultHistoryVersion resets the default history version
// only intended for integration test
func SetDefaultHistoryVersion(version int) {
atomic.StoreInt32(&defaultHistoryVersion, int32(version))
}
// GetDefaultHistoryVersion returns the default history version
func GetDefaultHistoryVersion() int {
return int(atomic.LoadInt32(&defaultHistoryVersion))
}