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 67bac7a815
commit 995e728ea0
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 entry *structs.Node
var err error 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 { switch op.Verb {
case api.NodeGet: case api.NodeGet:
if op.Node.ID != "" { entry, err = getNode()
entry, err = getNodeIDTxn(tx, op.Node.ID)
} else {
entry, err = getNodeTxn(tx, op.Node.Node)
}
if entry == nil && err == nil { if entry == nil && err == nil {
err = fmt.Errorf("node %q doesn't exist", op.Node.Node) 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: case api.NodeSet:
err = s.ensureNodeTxn(tx, idx, &op.Node) err = s.ensureNodeTxn(tx, idx, &op.Node)
if err == nil { if err == nil {
entry, err = getNodeIDTxn(tx, op.Node.ID) entry, err = getNode()
} }
case api.NodeCAS: 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) err = fmt.Errorf("failed to set node %q, index is stale", op.Node.Node)
break break
} }
entry, err = getNodeIDTxn(tx, op.Node.ID) entry, err = getNode()
case api.NodeDelete: case api.NodeDelete:
err = s.deleteNodeTxn(tx, idx, op.Node.Node) 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 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. // Apply the update.
resp, err := t.srv.raftApply(structs.TxnRequestType, args) resp, err := t.srv.raftApply(structs.TxnRequestType, args)
if err != nil { 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, // Fast-path a transaction with only writes to the read-only endpoint,
// which bypasses Raft, and allows for staleness. // which bypasses Raft, and allows for staleness.
s.agent.logger.Printf("ops: %d", len(ops))
conflict := false conflict := false
var ret interface{} var ret interface{}
if writes == 0 { if writes == 0 {
@ -308,7 +307,6 @@ func (s *HTTPServer) Txn(resp http.ResponseWriter, req *http.Request) (interface
return nil, err return nil, err
} }
ret, conflict = reply, len(reply.Errors) > 0 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 // If there was a conflict return the response object but set a special