Skip to content

Commit

Permalink
return error for txn condition not succeeded (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 authored Jun 24, 2021
1 parent 59f65e0 commit 02fb1bf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
15 changes: 12 additions & 3 deletions store/etcdv3/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,14 @@ func (m *Mercury) UpdateNodes(ctx context.Context, nodes ...*types.Node) error {
data[fmt.Sprintf(nodePodKey, node.Podname, node.Name)] = d
}

_, err := m.BatchUpdate(ctx, data)
return errors.WithStack(err)
resp, err := m.BatchUpdate(ctx, data)
if err != nil {
return err
}
if !resp.Succeeded {
return types.ErrTxnConditionFailed
}
return nil
}

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

_, err = m.BatchCreate(ctx, data)
resp, err := m.BatchCreate(ctx, data)
if err != nil {
return nil, err
}
if !resp.Succeeded {
return nil, types.ErrTxnConditionFailed
}

go metrics.Client.SendNodeInfo(node.Metrics())
return node, nil
Expand Down
2 changes: 2 additions & 0 deletions store/etcdv3/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ func TestUpdateNode(t *testing.T) {
},
}
assert.Error(t, m.UpdateNodes(ctx, fakeNode))
assert.Error(t, m.UpdateNodes(ctx, node), "ETCD Txn condition failed")
node.Available = false
assert.NoError(t, m.UpdateNodes(ctx, node))
}

Expand Down
13 changes: 10 additions & 3 deletions store/etcdv3/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,22 @@ func (m *Mercury) doOpsWorkload(ctx context.Context, workload *types.Workload, p
filepath.Join(workloadDeployPrefix, appname, entrypoint, workload.Nodename, workload.ID): workloadData,
}

var resp *clientv3.TxnResponse
if create {
if processing != nil {
processingKey := m.getProcessingKey(processing)
err = m.BatchCreateAndDecr(ctx, data, processingKey)
} else {
_, err = m.BatchCreate(ctx, data)
resp, err = m.BatchCreate(ctx, data)
}
} else {
_, err = m.BatchUpdate(ctx, data)
resp, err = m.BatchUpdate(ctx, data)
}
return err
if err != nil {
return err
}
if resp != nil && !resp.Succeeded {
return types.ErrTxnConditionFailed
}
return nil
}
4 changes: 4 additions & 0 deletions store/etcdv3/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func TestAddORUpdateWorkload(t *testing.T) {
assert.NoError(t, err)
// success updat
err = m.UpdateWorkload(ctx, workload)
assert.Error(t, err, "ETCD Txn condition failed")
// success updat
workload.Name = "test_app_2"
err = m.UpdateWorkload(ctx, workload)
assert.NoError(t, err)
}

Expand Down
9 changes: 5 additions & 4 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ var (
ErrRunAndWaitCountOneWithStdin = errors.New("Count must be 1 if OpenStdin is true")
ErrUnknownControlType = errors.New("Unknown control type")

ErrNoETCD = errors.New("ETCD must be set")
ErrKeyNotExists = errors.New("Key not exists")
ErrKeyExists = errors.New("Key exists")
ErrNoOps = errors.New("No txn ops")
ErrNoETCD = errors.New("ETCD must be set")
ErrKeyNotExists = errors.New("Key not exists")
ErrKeyExists = errors.New("Key exists")
ErrNoOps = errors.New("No txn ops")
ErrTxnConditionFailed = errors.New("ETCD Txn condition failed")

ErrNotSupport = errors.New("Not Support")
ErrSCMNotSet = errors.New("SCM not set")
Expand Down

0 comments on commit 02fb1bf

Please sign in to comment.