Skip to content

Commit 311208c

Browse files
committed
return error for txn condition not succeeded
1 parent d33f2fc commit 311208c

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

store/etcdv3/node.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,14 @@ func (m *Mercury) UpdateNodes(ctx context.Context, nodes ...*types.Node) error {
155155
data[fmt.Sprintf(nodePodKey, node.Podname, node.Name)] = d
156156
}
157157

158-
_, err := m.BatchUpdate(ctx, data)
159-
return errors.WithStack(err)
158+
resp, err := m.BatchUpdate(ctx, data)
159+
if err != nil {
160+
return err
161+
}
162+
if !resp.Succeeded {
163+
return types.ErrTxnConditionFailed
164+
}
165+
return nil
160166
}
161167

162168
// UpdateNodeResource update cpu and memory on a node, either add or subtract
@@ -246,10 +252,13 @@ func (m *Mercury) doAddNode(ctx context.Context, name, endpoint, podname, ca, ce
246252
data[fmt.Sprintf(nodeInfoKey, name)] = d
247253
data[fmt.Sprintf(nodePodKey, podname, name)] = d
248254

249-
_, err = m.BatchCreate(ctx, data)
255+
resp, err := m.BatchCreate(ctx, data)
250256
if err != nil {
251257
return nil, err
252258
}
259+
if !resp.Succeeded {
260+
return nil, types.ErrTxnConditionFailed
261+
}
253262

254263
go metrics.Client.SendNodeInfo(node.Metrics())
255264
return node, nil

store/etcdv3/node_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ func TestUpdateNode(t *testing.T) {
205205
},
206206
}
207207
assert.Error(t, m.UpdateNodes(ctx, fakeNode))
208+
assert.Error(t, m.UpdateNodes(ctx, node), "ETCD Txn condition failed")
209+
node.Available = false
208210
assert.NoError(t, m.UpdateNodes(ctx, node))
209211
}
210212

store/etcdv3/workload.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,22 @@ func (m *Mercury) doOpsWorkload(ctx context.Context, workload *types.Workload, p
278278
filepath.Join(workloadDeployPrefix, appname, entrypoint, workload.Nodename, workload.ID): workloadData,
279279
}
280280

281+
var resp *clientv3.TxnResponse
281282
if create {
282283
if processing != nil {
283284
processingKey := m.getProcessingKey(processing)
284285
err = m.BatchCreateAndDecr(ctx, data, processingKey)
285286
} else {
286-
_, err = m.BatchCreate(ctx, data)
287+
resp, err = m.BatchCreate(ctx, data)
287288
}
288289
} else {
289-
_, err = m.BatchUpdate(ctx, data)
290+
resp, err = m.BatchUpdate(ctx, data)
290291
}
291-
return err
292+
if err != nil {
293+
return err
294+
}
295+
if resp != nil && !resp.Succeeded {
296+
return types.ErrTxnConditionFailed
297+
}
298+
return nil
292299
}

store/etcdv3/workload_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func TestAddORUpdateWorkload(t *testing.T) {
3636
assert.NoError(t, err)
3737
// success updat
3838
err = m.UpdateWorkload(ctx, workload)
39+
assert.Error(t, err, "ETCD Txn condition failed")
40+
// success updat
41+
workload.Name = "test_app_2"
42+
err = m.UpdateWorkload(ctx, workload)
3943
assert.NoError(t, err)
4044
}
4145

types/errors.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ var (
6666
ErrRunAndWaitCountOneWithStdin = errors.New("Count must be 1 if OpenStdin is true")
6767
ErrUnknownControlType = errors.New("Unknown control type")
6868

69-
ErrNoETCD = errors.New("ETCD must be set")
70-
ErrKeyNotExists = errors.New("Key not exists")
71-
ErrKeyExists = errors.New("Key exists")
72-
ErrNoOps = errors.New("No txn ops")
69+
ErrNoETCD = errors.New("ETCD must be set")
70+
ErrKeyNotExists = errors.New("Key not exists")
71+
ErrKeyExists = errors.New("Key exists")
72+
ErrNoOps = errors.New("No txn ops")
73+
ErrTxnConditionFailed = errors.New("ETCD Txn condition failed")
7374

7475
ErrNotSupport = errors.New("Not Support")
7576
ErrSCMNotSet = errors.New("SCM not set")

0 commit comments

Comments
 (0)