This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdata_accessor.go
199 lines (175 loc) · 6.49 KB
/
data_accessor.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
// 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 helix
import (
"github.com/pkg/errors"
"github.com/samuel/go-zookeeper/zk"
"github.com/uber-go/go-helix/model"
uzk "github.com/uber-go/go-helix/zk"
)
var (
// ErrorNilUpdatedData returns when updateFn returns nil *model.ZNRecord without error
ErrorNilUpdatedData = errors.New("data accessor: updated data is nil")
)
// updateFn gets the old data (nil if data does not exist) and return the updated data
// return data is expected to be non-nil
type updateFn func(data *model.ZNRecord) (*model.ZNRecord, error)
// DataAccessor helps to interact with Helix Data Types like IdealState, LiveInstance, Message
// it mirrors org.apache.helix.HelixDataAccessor
type DataAccessor struct {
zkClient *uzk.Client
keyBuilder *KeyBuilder
}
// newDataAccessor creates new DataAccessor with Zookeeper client
func newDataAccessor(zkClient *uzk.Client, keyBuilder *KeyBuilder) *DataAccessor {
return &DataAccessor{zkClient: zkClient, keyBuilder: keyBuilder}
}
// Msg helps get Helix property with type Message
func (a *DataAccessor) Msg(path string) (*model.Message, error) {
record, err := a.zkClient.GetRecordFromPath(path)
if err != nil {
return nil, err
}
return &model.Message{ZNRecord: *record}, nil
}
// InstanceConfig helps get Helix property with type Message
func (a *DataAccessor) InstanceConfig(path string) (*model.InstanceConfig, error) {
record, err := a.zkClient.GetRecordFromPath(path)
if err != nil {
return nil, err
}
return &model.InstanceConfig{ZNRecord: *record}, nil
}
// IdealState helps get Helix property with type IdealState
func (a *DataAccessor) IdealState(resourceName string) (*model.IdealState, error) {
path := a.keyBuilder.idealStateForResource(resourceName)
record, err := a.zkClient.GetRecordFromPath(path)
if err != nil {
return nil, err
}
return &model.IdealState{ZNRecord: *record}, nil
}
// ExternalView helps get Helix property with type ExternalView
func (a *DataAccessor) ExternalView(resourceName string) (*model.ExternalView, error) {
path := a.keyBuilder.externalViewForResource(resourceName)
record, err := a.zkClient.GetRecordFromPath(path)
if err != nil {
return nil, err
}
return &model.ExternalView{ZNRecord: *record}, nil
}
// CurrentState helps get Helix property with type CurrentState
func (a *DataAccessor) CurrentState(instanceName, session, resourceName string,
) (*model.CurrentState, error) {
path := a.keyBuilder.currentStateForResource(instanceName, session, resourceName)
record, err := a.zkClient.GetRecordFromPath(path)
if err != nil {
return nil, err
}
return &model.CurrentState{ZNRecord: *record}, nil
}
// LiveInstance returns Helix property with type LiveInstance
func (a *DataAccessor) LiveInstance(instanceName string) (*model.LiveInstance, error) {
path := a.keyBuilder.liveInstance(instanceName)
record, err := a.zkClient.GetRecordFromPath(path)
if err != nil {
return nil, err
}
return &model.LiveInstance{ZNRecord: *record}, nil
}
// StateModelDef helps get Helix property with type StateModelDef
func (a *DataAccessor) StateModelDef(stateModel string) (*model.StateModelDef, error) {
path := a.keyBuilder.stateModelDef(stateModel)
record, err := a.zkClient.GetRecordFromPath(path)
if err != nil {
return nil, err
}
return &model.StateModelDef{ZNRecord: *record}, nil
}
// updateData would update the data in path with updateFn
// if path does not exist, updateData would create it
// and updateFn would have a nil *model.ZNRecord as input
func (a *DataAccessor) updateData(path string, update updateFn) error {
var err error
var record *model.ZNRecord
for {
nodeExists := true
record, err = a.zkClient.GetRecordFromPath(path)
cause := errors.Cause(err)
if cause == zk.ErrNoNode {
nodeExists = false
} else if cause != nil {
return err
}
record, err = update(record)
if err != nil {
return err
}
if record == nil {
return ErrorNilUpdatedData
}
if nodeExists {
err = a.setData(path, *record, record.Version)
} else {
err = a.createData(path, *record)
}
if cause := errors.Cause(err); cause != nil &&
(cause != zk.ErrBadVersion || cause != zk.ErrNoNode) {
return err
}
// retry for ErrBadVersion or ErrNoNode
if err == nil {
return nil
}
}
}
func (a *DataAccessor) createData(path string, data model.ZNRecord) error {
serialized, err := data.Marshal()
if err != nil {
return err
}
return a.zkClient.CreateDataWithPath(path, serialized)
}
func (a *DataAccessor) setData(path string, data model.ZNRecord, version int32) error {
serialized, err := data.Marshal()
if err != nil {
return err
}
return a.zkClient.SetDataForPath(path, serialized, version)
}
// createMsg creates a new message
func (a *DataAccessor) createMsg(path string, msg *model.Message) error {
return a.createData(path, msg.ZNRecord)
}
// CreateParticipantMsg creates a message for the participant and helps
// testing the participant
func (a *DataAccessor) CreateParticipantMsg(instanceName string, msg *model.Message) error {
path := a.keyBuilder.participantMsg(instanceName, msg.ID)
return a.createData(path, msg.ZNRecord)
}
func (a *DataAccessor) setMsg(path string, msg *model.Message) error {
return a.setData(path, msg.ZNRecord, msg.Version)
}
func (a *DataAccessor) createCurrentState(path string, state *model.CurrentState) error {
return a.createData(path, state.ZNRecord)
}
func (a *DataAccessor) createInstanceConfig(path string, config *model.InstanceConfig) error {
return a.createData(path, config.ZNRecord)
}