• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uber / cadence / 018dfbb1-48ef-4faa-92f4-b269d8ca713f

01 Mar 2024 08:25PM UTC coverage: 62.889% (-0.004%) from 62.893%
018dfbb1-48ef-4faa-92f4-b269d8ca713f

push

buildkite

web-flow
Addition of tests for ArchivalConfigStateMachine in common/domain  (#5698)

* Addition of tests for ArchivalConfigStateMachine in common/domain

92957 of 147811 relevant lines covered (62.89%)

2344.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/common/persistence/sql/common.go
1
// Copyright (c) 2018 Uber Technologies, Inc.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a copy
4
// of this software and associated documentation files (the "Software"), to deal
5
// in the Software without restriction, including without limitation the rights
6
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
// copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in
11
// all copies or substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
// THE SOFTWARE.
20

21
package sql
22

23
import (
24
        "bytes"
25
        "context"
26
        "encoding/binary"
27
        "encoding/gob"
28
        "fmt"
29

30
        "github.com/uber/cadence/common/log"
31
        "github.com/uber/cadence/common/log/tag"
32
        "github.com/uber/cadence/common/persistence"
33
        p "github.com/uber/cadence/common/persistence"
34
        "github.com/uber/cadence/common/persistence/serialization"
35
        "github.com/uber/cadence/common/persistence/sql/sqlplugin"
36
        "github.com/uber/cadence/common/types"
37
)
38

39
// TODO: Rename all SQL Managers to Stores
40
type sqlStore struct {
41
        db     sqlplugin.DB
42
        logger log.Logger
43
        parser serialization.Parser
44
        dc     *p.DynamicConfiguration
45
}
46

47
func (m *sqlStore) GetName() string {
×
48
        return m.db.PluginName()
×
49
}
×
50

51
func (m *sqlStore) Close() {
194✔
52
        if m.db != nil {
388✔
53
                m.db.Close()
194✔
54
        }
194✔
55
}
56

57
func (m *sqlStore) useAsyncTransaction() bool {
2,913✔
58
        return m.db.SupportsAsyncTransaction() && m.dc != nil && m.dc.EnableSQLAsyncTransaction()
2,913✔
59
}
2,913✔
60

61
func (m *sqlStore) txExecute(ctx context.Context, dbShardID int, operation string, f func(tx sqlplugin.Tx) error) error {
8,363✔
62
        tx, err := m.db.BeginTx(ctx, dbShardID)
8,363✔
63
        if err != nil {
8,363✔
64
                return convertCommonErrors(m.db, operation, "Failed to start transaction.", err)
×
65
        }
×
66
        err = f(tx)
8,363✔
67
        if err != nil {
8,423✔
68
                rollBackErr := tx.Rollback()
60✔
69
                if rollBackErr != nil {
60✔
70
                        m.logger.Error("transaction rollback error", tag.Error(rollBackErr))
×
71
                }
×
72
                return convertCommonErrors(m.db, operation, "", err)
60✔
73
        }
74
        if err := tx.Commit(); err != nil {
8,305✔
75
                return convertCommonErrors(m.db, operation, "Failed to commit transaction.", err)
×
76
        }
×
77
        return nil
8,305✔
78
}
79

80
func gobSerialize(x interface{}) ([]byte, error) {
4✔
81
        b := bytes.Buffer{}
4✔
82
        e := gob.NewEncoder(&b)
4✔
83
        err := e.Encode(x)
4✔
84
        if err != nil {
4✔
85
                return nil, &types.InternalServiceError{
×
86
                        Message: fmt.Sprintf("Error in serialization: %v", err),
×
87
                }
×
88
        }
×
89
        return b.Bytes(), nil
4✔
90
}
91

92
func gobDeserialize(a []byte, x interface{}) error {
2✔
93
        b := bytes.NewBuffer(a)
2✔
94
        d := gob.NewDecoder(b)
2✔
95
        err := d.Decode(x)
2✔
96

2✔
97
        if err != nil {
2✔
98
                return &types.InternalServiceError{
×
99
                        Message: fmt.Sprintf("Error in deserialization: %v", err),
×
100
                }
×
101
        }
×
102
        return nil
2✔
103
}
104

105
func serializePageToken(offset int64) []byte {
74✔
106
        b := make([]byte, 8)
74✔
107
        binary.LittleEndian.PutUint64(b, uint64(offset))
74✔
108
        return b
74✔
109
}
74✔
110

111
func deserializePageToken(payload []byte) (int64, error) {
50✔
112
        if len(payload) != 8 {
51✔
113
                return 0, fmt.Errorf("invalid token of %v length", len(payload))
1✔
114
        }
1✔
115
        return int64(binary.LittleEndian.Uint64(payload)), nil
49✔
116
}
117

118
func convertCommonErrors(
119
        errChecker sqlplugin.ErrorChecker,
120
        operation, message string,
121
        err error,
122
) error {
909✔
123
        switch err.(type) {
909✔
124
        case *persistence.ConditionFailedError,
125
                *persistence.CurrentWorkflowConditionFailedError,
126
                *persistence.WorkflowExecutionAlreadyStartedError,
127
                *persistence.ShardOwnershipLostError,
128
                *persistence.TimeoutError,
129
                *types.DomainAlreadyExistsError,
130
                *types.EntityNotExistsError,
131
                *types.ServiceBusyError,
132
                *types.InternalServiceError:
39✔
133
                return err
39✔
134
        }
135
        if errChecker.IsNotFoundError(err) {
1,014✔
136
                return &types.EntityNotExistsError{
142✔
137
                        Message: fmt.Sprintf("%v failed. %s Error: %v ", operation, message, err),
142✔
138
                }
142✔
139
        }
142✔
140

141
        if errChecker.IsTimeoutError(err) {
734✔
142
                return &persistence.TimeoutError{Msg: fmt.Sprintf("%v timed out. %s Error: %v", operation, message, err)}
2✔
143
        }
2✔
144

145
        if errChecker.IsThrottlingError(err) {
730✔
146
                return &types.ServiceBusyError{
×
147
                        Message: fmt.Sprintf("%v operation failed. %s Error: %v", operation, message, err),
×
148
                }
×
149
        }
×
150

151
        return &types.InternalServiceError{
730✔
152
                Message: fmt.Sprintf("%v operation failed. %s Error: %v", operation, message, err),
730✔
153
        }
730✔
154
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc