Skip to content

Add service:DHT:NodeCount to metric web #321

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

Merged
merged 5 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions consistent/consistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ package consistent

import (
"errors"
"expvar"
"sort"
"strconv"
"sync"

mw "github.com/zserge/metric"

"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/proto"
Expand All @@ -63,6 +66,14 @@ var (
ErrKeyNotFound = errors.New("node key not found")
)

const (
mwKeyDHTNodeCount = "service:DHT:NodeCount"
)

func init() {
expvar.Publish(mwKeyDHTNodeCount, mw.NewGauge("1M1h"))
}

// Consistent holds the information about the members of the consistent hash circle.
type Consistent struct {
// TODO(auxten): do not store node info on circle, just put node id as value
Expand All @@ -71,7 +82,6 @@ type Consistent struct {
//members map[proto.NodeID]proto.Node
sortedHashes NodeKeys
NumberOfReplicas int
count int64
persist Persistence
cacheLock sync.RWMutex
sync.RWMutex
Expand Down Expand Up @@ -180,7 +190,6 @@ func (c *Consistent) add(node proto.Node) (err error) {
log.WithField("node", node).WithError(err).Error("set node info failed")
return
}

c.AddCache(node)
return
}
Expand All @@ -194,7 +203,7 @@ func (c *Consistent) AddCache(node proto.Node) {
c.circle[hashKey(c.nodeKey(node.ID, i))] = &node
}
c.updateSortedHashes()
c.count++
expvar.Get(mwKeyDHTNodeCount).(mw.Metric).Add(float64(len(c.circle) / c.NumberOfReplicas))
return
}

Expand All @@ -207,7 +216,6 @@ func (c *Consistent) RemoveCache(nodeID proto.NodeID) {
delete(c.circle, hashKey(c.nodeKey(nodeID, i)))
}
c.updateSortedHashes()
c.count--
}

// ResetCache removes all node from the hash cache.
Expand All @@ -216,7 +224,6 @@ func (c *Consistent) ResetCache() {
defer c.cacheLock.Unlock()

c.circle = make(map[proto.NodeKey]*proto.Node)
c.count = 0
c.sortedHashes = NodeKeys{}
}

Expand Down Expand Up @@ -275,7 +282,7 @@ func (c *Consistent) GetTwoNeighbors(name string) (proto.Node, proto.Node, error
i := c.search(key)
a := *c.circle[c.sortedHashes[i]]

if c.count == 1 {
if len(c.sortedHashes)/c.NumberOfReplicas == 1 {
return a, proto.Node{}, nil
}

Expand Down Expand Up @@ -304,8 +311,9 @@ func (c *Consistent) GetNeighborsEx(name string, n int, roles proto.ServerRoles)
return nil, ErrEmptyCircle
}

if c.count < int64(n) {
n = int(c.count)
count := len(c.sortedHashes) / c.NumberOfReplicas
if count < n {
n = count
}

var (
Expand Down
12 changes: 6 additions & 6 deletions consistent/consistent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@ func TestSet(t *testing.T) {
x.Add(NewNodeFromString("2222"))

x.Set([]Node{NewNodeFromString("3333"), NewNodeFromString("4444")})
if x.count != 2 {
t.Errorf("expected 2 elts, got %d", x.count)
if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
}
a, b, err := x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
if err != nil {
Expand All @@ -722,8 +722,8 @@ func TestSet(t *testing.T) {
t.Errorf("expected a != b, they were both %v", a)
}
x.Set([]Node{NewNodeFromString("5555"), NewNodeFromString("4444")})
if x.count != 2 {
t.Errorf("expected 2 elts, got %d", x.count)
if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
}
a, b, err = x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
if err != nil {
Expand All @@ -739,8 +739,8 @@ func TestSet(t *testing.T) {
t.Errorf("expected a != b, they were both %v", a)
}
x.Set([]Node{NewNodeFromString("5555"), NewNodeFromString("4444")})
if x.count != 2 {
t.Errorf("expected 2 elts, got %d", x.count)
if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
}
a, b, err = x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/zserge/metric/metric.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.