Skip to content

Commit

Permalink
pkg/receive: rename host->node
Browse files Browse the repository at this point in the history
This commit renames `host` to `node` in the context of the receive
hashring. This is because more often than not, the hashring will deal
with endpoints rather than simply hosts and node is a more generic
term for the operand of a hashring.
  • Loading branch information
squat committed Jun 11, 2019
1 parent f2356eb commit a51ebdb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
19 changes: 9 additions & 10 deletions pkg/receive/hashring.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (

const sep = '\xff'

// Hashring finds the correct host to handle a given time series
// for a specified tenant.
// It returns the hostname and any error encountered.
// It returns the node and any error encountered.
type Hashring interface {
GetHost(tenant string, timeSeries *prompb.TimeSeries) (string, error)
Get(tenant string, timeSeries *prompb.TimeSeries) (string, error)
}

// Matcher determines whether or tenant matches a hashring.
Expand Down Expand Up @@ -71,8 +70,8 @@ type simpleHashring struct {
targetgroup.Group
}

// GetHost returns a hostname to handle the given tenant and time series.
func (s *simpleHashring) GetHost(tenant string, ts *prompb.TimeSeries) (string, error) {
// Get returns a target to handle the given tenant and time series.
func (s *simpleHashring) Get(tenant string, ts *prompb.TimeSeries) (string, error) {
// Always return nil here to implement the Hashring interface.
return string(s.Targets[hash(tenant, ts)%uint64(len(s.Targets))][model.AddressLabel]), nil
}
Expand All @@ -85,18 +84,18 @@ type matchingHashring struct {
matcher Matcher
}

// GetHost returns a hostname to handle the given tenant and time series.
func (m matchingHashring) GetHost(tenant string, ts *prompb.TimeSeries) (string, error) {
// Get returns a target to handle the given tenant and time series.
func (m matchingHashring) Get(tenant string, ts *prompb.TimeSeries) (string, error) {
if h, ok := m.cache[tenant]; ok {
return h.GetHost(tenant, ts)
return h.Get(tenant, ts)
}
for name := range m.hashrings {
if m.matcher.Match(tenant, name) {
m.cache[tenant] = m.hashrings[name]
return m.hashrings[name].GetHost(tenant, ts)
return m.hashrings[name].Get(tenant, ts)
}
}
return "", errors.New("no matching hosts to handle tenant")
return "", errors.New("no matching hashring to handle tenant")
}

// NewHashring creates a multi-tenant hashring for a given slice of
Expand Down
80 changes: 40 additions & 40 deletions pkg/receive/hashring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestHash(t *testing.T) {
}
}

func TestGetHost(t *testing.T) {
func TestHashringGet(t *testing.T) {
ts := &prompb.TimeSeries{
Labels: []prompb.Label{
{
Expand All @@ -48,7 +48,7 @@ func TestGetHost(t *testing.T) {
for _, tc := range []struct {
name string
cfg []*targetgroup.Group
hosts map[string]struct{}
nodes map[string]struct{}
tenant string
}{
{
Expand All @@ -62,34 +62,34 @@ func TestGetHost(t *testing.T) {
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host1",
model.AddressLabel: "node1",
},
},
},
},
hosts: map[string]struct{}{"host1": struct{}{}},
nodes: map[string]struct{}{"node1": struct{}{}},
},
{
name: "specific",
cfg: []*targetgroup.Group{
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host1",
model.AddressLabel: "node1",
},
},
Source: "",
},
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host2",
model.AddressLabel: "node2",
},
},
Source: "tenant1",
},
},
hosts: map[string]struct{}{"host2": struct{}{}},
nodes: map[string]struct{}{"node2": struct{}{}},
tenant: "tenant1",
},
{
Expand All @@ -98,29 +98,29 @@ func TestGetHost(t *testing.T) {
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host1",
model.AddressLabel: "node1",
},
},
Source: "tenant1",
},
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host2",
model.AddressLabel: "node2",
},
},
Source: "tenant2",
},
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host3",
model.AddressLabel: "node3",
},
},
Source: "tenant3",
},
},
hosts: map[string]struct{}{"host1": struct{}{}},
nodes: map[string]struct{}{"node1": struct{}{}},
tenant: "tenant1",
},
{
Expand All @@ -129,23 +129,23 @@ func TestGetHost(t *testing.T) {
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host1",
model.AddressLabel: "node1",
},
},
Source: "tenant1",
},
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host2",
model.AddressLabel: "node2",
},
},
Source: "tenant2",
},
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host3",
model.AddressLabel: "node3",
},
},
Source: "tenant3",
Expand All @@ -154,91 +154,91 @@ func TestGetHost(t *testing.T) {
tenant: "tenant4",
},
{
name: "many hosts",
name: "many nodes",
cfg: []*targetgroup.Group{
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host1",
model.AddressLabel: "node1",
},
model.LabelSet{
model.AddressLabel: "host2",
model.AddressLabel: "node2",
},
model.LabelSet{
model.AddressLabel: "host3",
model.AddressLabel: "node3",
},
},
Source: "tenant1",
},
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host4",
model.AddressLabel: "node4",
},
model.LabelSet{
model.AddressLabel: "host5",
model.AddressLabel: "node5",
},
model.LabelSet{
model.AddressLabel: "host6",
model.AddressLabel: "node6",
},
},
Source: "",
},
},
hosts: map[string]struct{}{
"host1": struct{}{},
"host2": struct{}{},
"host3": struct{}{},
nodes: map[string]struct{}{
"node1": struct{}{},
"node2": struct{}{},
"node3": struct{}{},
},
tenant: "tenant1",
},
{
name: "many hosts 2",
name: "many nodes 2",
cfg: []*targetgroup.Group{
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host1",
model.AddressLabel: "node1",
},
model.LabelSet{
model.AddressLabel: "host2",
model.AddressLabel: "node2",
},
model.LabelSet{
model.AddressLabel: "host3",
model.AddressLabel: "node3",
},
},
Source: "tenant1",
},
{
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: "host4",
model.AddressLabel: "node4",
},
model.LabelSet{
model.AddressLabel: "host5",
model.AddressLabel: "node5",
},
model.LabelSet{
model.AddressLabel: "host6",
model.AddressLabel: "node6",
},
},
},
},
hosts: map[string]struct{}{
"host4": struct{}{},
"host5": struct{}{},
"host6": struct{}{},
nodes: map[string]struct{}{
"node4": struct{}{},
"node5": struct{}{},
"node6": struct{}{},
},
},
} {
hs := NewHashring(ExactMatcher, tc.cfg)
h, err := hs.GetHost(tc.tenant, ts)
if tc.hosts != nil {
h, err := hs.Get(tc.tenant, ts)
if tc.nodes != nil {
if err != nil {
t.Errorf("case %q: got unexpected error: %v", tc.name, err)
continue
}
if _, ok := tc.hosts[h]; !ok {
t.Errorf("case %q: got unexpected host %q", tc.name, h)
if _, ok := tc.nodes[h]; !ok {
t.Errorf("case %q: got unexpected node %q", tc.name, h)
}
continue
}
Expand Down

0 comments on commit a51ebdb

Please sign in to comment.