Skip to content

Commit 4778182

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

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-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/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.Succeeded {
296+
return types.ErrTxnConditionFailed
297+
}
298+
return nil
292299
}

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)