txn: fix an issue with querying nodes by name instead of ID

This commit is contained in:
Kyle Havlovitz 2018-12-12 12:46:33 -08:00
parent efcdc85e1a
commit 8b1dc6a22c
3 changed files with 11 additions and 24 deletions

View File

@ -127,13 +127,17 @@ func (s *Store) txnNode(tx *memdb.Txn, idx uint64, op *structs.TxnNodeOp) (struc
var entry *structs.Node
var err error
getNode := func() (*structs.Node, error) {
if op.Node.ID != "" {
return getNodeIDTxn(tx, op.Node.ID)
} else {
return getNodeTxn(tx, op.Node.Node)
}
}
switch op.Verb {
case api.NodeGet:
if op.Node.ID != "" {
entry, err = getNodeIDTxn(tx, op.Node.ID)
} else {
entry, err = getNodeTxn(tx, op.Node.Node)
}
entry, err = getNode()
if entry == nil && err == nil {
err = fmt.Errorf("node %q doesn't exist", op.Node.Node)
}
@ -141,7 +145,7 @@ func (s *Store) txnNode(tx *memdb.Txn, idx uint64, op *structs.TxnNodeOp) (struc
case api.NodeSet:
err = s.ensureNodeTxn(tx, idx, &op.Node)
if err == nil {
entry, err = getNodeIDTxn(tx, op.Node.ID)
entry, err = getNode()
}
case api.NodeCAS:
@ -151,7 +155,7 @@ func (s *Store) txnNode(tx *memdb.Txn, idx uint64, op *structs.TxnNodeOp) (struc
err = fmt.Errorf("failed to set node %q, index is stale", op.Node.Node)
break
}
entry, err = getNodeIDTxn(tx, op.Node.ID)
entry, err = getNode()
case api.NodeDelete:
err = s.deleteNodeTxn(tx, idx, op.Node.Node)

View File

@ -119,21 +119,6 @@ func (t *Txn) Apply(args *structs.TxnRequest, reply *structs.TxnResponse) error
return nil
}
str := ""
for _, op := range args.Ops {
switch {
case op.KV != nil:
str += fmt.Sprintf("%#v\n", op.KV)
case op.Node != nil:
str += fmt.Sprintf("%#v\n", op.Node)
case op.Service != nil:
str += fmt.Sprintf("%#v\n", op.Service)
case op.Check != nil:
str += fmt.Sprintf("%#v\n", op.Check)
}
}
//return fmt.Errorf("%s", str)
// Apply the update.
resp, err := t.srv.raftApply(structs.TxnRequestType, args)
if err != nil {

View File

@ -278,7 +278,6 @@ func (s *HTTPServer) Txn(resp http.ResponseWriter, req *http.Request) (interface
// Fast-path a transaction with only writes to the read-only endpoint,
// which bypasses Raft, and allows for staleness.
s.agent.logger.Printf("ops: %d", len(ops))
conflict := false
var ret interface{}
if writes == 0 {
@ -308,7 +307,6 @@ func (s *HTTPServer) Txn(resp http.ResponseWriter, req *http.Request) (interface
return nil, err
}
ret, conflict = reply, len(reply.Errors) > 0
s.agent.logger.Printf("results: %d, errors: %d", len(reply.Results), len(reply.Errors))
}
// If there was a conflict return the response object but set a special