Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS lookup by Consul node ID #2702

Merged
merged 8 commits into from
Feb 2, 2017
Merged

DNS lookup by Consul node ID #2702

merged 8 commits into from
Feb 2, 2017

Conversation

sean-
Copy link
Contributor

@sean- sean- commented Feb 1, 2017

Assuming the following output from a consul agent:

==> Consul agent running!
           Version: 'v0.7.3-43-gc5e140c-dev (c5e140c+CHANGES)'
           Node ID: '40e4a748-2192-161a-0510-9bf59fe950b5'
         Node name: 'myhost'

it is now possible to lookup nodes by their Node Name or Node ID, or a
prefix match of the Node ID, with the following caveats re: the prefix
match:

  1. first eight digits of the Node ID are a required minimum (eight was
    chosen as an arbitrary number)
  2. the length of the Node ID must be an even number or no result will be
    returned.
% dig @127.0.0.1 -p 8600 myhost.node.dc1.consul.
myhost.node.dc1.consul.	0	IN	A	127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a748-2192-161a-0510-9bf59fe950b5.node.dc1.consul.
40e4a748-2192-161a-0510-9bf59fe950b5.node.dc1.consul. 0	IN A 127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a748.node.dc1.consul.
40e4a748.node.dc1.consul. 0	IN	A	127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a74821.node.dc1.consul.
40e4a74821.node.dc1.consul. 0	IN	A	127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a748-21.node.dc1.consul.
40e4a748-21.node.dc1.consul. 0	IN	A	127.0.0.1

If the length isn't `36`, return `false` immediately before firing up
the regexp engine.
Assuming the following output from a consul agent:

```
==> Consul agent running!
           Version: 'v0.7.3-43-gc5e140c-dev (c5e140c+CHANGES)'
           Node ID: '40e4a748-2192-161a-0510-9bf59fe950b5'
         Node name: 'myhost'
```

it is now possible to lookup nodes by their Node Name or Node ID, or a
prefix match of the Node ID, with the following caveats re: the prefix
match:

1) first eight digits of the Node ID are a required minimum (eight was
   chosen as an arbitrary number)
2) the length of the Node ID must be an even number or no result will be
   returned.

```
% dig @127.0.0.1 -p 8600 myhost.node.dc1.consul.
myhost.node.dc1.consul.	0	IN	A	127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a748-2192-161a-0510-9bf59fe950b5.node.dc1.consul.
40e4a748-2192-161a-0510-9bf59fe950b5.node.dc1.consul. 0	IN A 127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a748.node.dc1.consul.
40e4a748.node.dc1.consul. 0	IN	A	127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a74821.node.dc1.consul.
40e4a74821.node.dc1.consul. 0	IN	A	127.0.0.1
% dig @127.0.0.1 -p 8600 40e4a748-21.node.dc1.consul.
40e4a748-21.node.dc1.consul. 0	IN	A	127.0.0.1
```
@sean- sean- self-assigned this Feb 1, 2017
@sean- sean- requested a review from slackpad February 1, 2017 22:47
Copy link
Contributor

@slackpad slackpad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Left some feedback.

@@ -78,6 +78,14 @@ func nodesTableSchema() *memdb.TableSchema {
Lowercase: true,
},
},
"uuid": &memdb.IndexSchema{
Name: "uuid",
AllowMissing: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be true since old nodes won't have this yet.

}
return types.NodeID(id)
}

func TestStateStore_EnsureRegistration(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth it to add another test, or a clause here that calls NodeServices() with a UUID.

if len(nodeName) >= minUUIDLookupLen {
// Attempt to lookup the node by it's node ID
var idWatchCh <-chan struct{}
idWatchCh, n, err = tx.FirstWatch("nodes", "uuid_prefix", nodeName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you wan't to use the iterator form here to make sure the lookup isn't ambiguous:

iter, err := tx.Get("nodes", "uuid_prefix", nodeName)
n = iter.Next()
if n != nil {
  if iter.Next() == nil {
    // got it
  } else {
    // it was ambiguous, return nil
  }
}

@@ -662,15 +688,34 @@ func (s *StateStore) NodeServices(ws memdb.WatchSet, nodeName string) (uint64, *
// Get the table index.
idx := maxIndexTxn(tx, "nodes", "services")

// Query the node
// Query the node by node name
watchCh, n, err := tx.FirstWatch("nodes", "id", nodeName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename nodeName to nodeNameOrID in here.

}

idWatchCh := iter.WatchCh()
if iter.Next() != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably worth a comment about trying to make sure there's not another node that matches the prefix. It's pretty obscure logic :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also wrt the comment below - it's not that we didn't find anything - there were too many things.

return 0, nil, nil
}

// Attempt to lookup the node by it's node ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"its"

// Attempt to lookup the node by it's node ID
iter, err := tx.Get("nodes", "uuid_prefix", nodeNameOrID)
if err != nil {
return 0, nil, fmt.Errorf("node ID lookup failed: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is probably a soft error (maybe debug log it) where we should just add the watch channel and return 0, nil, nil. If they type in a bad name they could hit this and get a confusing error.

…ookup returned nil.

Add a TODO to note where a future point of logging should occur once a
logger is present.
lookup returned nil.

Add a TODO to note where a future point of logging should occur once a
logger is present and a few additional comments to explain the program
flow.
@sean- sean- merged commit b764606 into master Feb 2, 2017
@sean- sean- deleted the f-dns-nodeid branch February 2, 2017 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants