Skip to content

Commit a6ee5c2

Browse files
author
auxten
committed
Fix Consistent.count
1 parent 17e8785 commit a6ee5c2

2 files changed

Lines changed: 12 additions & 15 deletions

File tree

consistent/consistent.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const (
7171
)
7272

7373
func init() {
74-
expvar.Publish(mwKeyDHTNodeCount, mw.NewGauge("1M1d"))
74+
expvar.Publish(mwKeyDHTNodeCount, mw.NewGauge("1M1h"))
7575
}
7676

7777
// Consistent holds the information about the members of the consistent hash circle.
@@ -82,7 +82,6 @@ type Consistent struct {
8282
//members map[proto.NodeID]proto.Node
8383
sortedHashes NodeKeys
8484
NumberOfReplicas int
85-
count int64
8685
persist Persistence
8786
cacheLock sync.RWMutex
8887
sync.RWMutex
@@ -204,8 +203,7 @@ func (c *Consistent) AddCache(node proto.Node) {
204203
c.circle[hashKey(c.nodeKey(node.ID, i))] = &node
205204
}
206205
c.updateSortedHashes()
207-
c.count++
208-
expvar.Get(mwKeyDHTNodeCount).(mw.Metric).Add(float64(c.count))
206+
expvar.Get(mwKeyDHTNodeCount).(mw.Metric).Add(float64(len(c.circle) / c.NumberOfReplicas))
209207
return
210208
}
211209

@@ -218,7 +216,6 @@ func (c *Consistent) RemoveCache(nodeID proto.NodeID) {
218216
delete(c.circle, hashKey(c.nodeKey(nodeID, i)))
219217
}
220218
c.updateSortedHashes()
221-
c.count--
222219
}
223220

224221
// ResetCache removes all node from the hash cache.
@@ -227,7 +224,6 @@ func (c *Consistent) ResetCache() {
227224
defer c.cacheLock.Unlock()
228225

229226
c.circle = make(map[proto.NodeKey]*proto.Node)
230-
c.count = 0
231227
c.sortedHashes = NodeKeys{}
232228
}
233229

@@ -286,7 +282,7 @@ func (c *Consistent) GetTwoNeighbors(name string) (proto.Node, proto.Node, error
286282
i := c.search(key)
287283
a := *c.circle[c.sortedHashes[i]]
288284

289-
if c.count == 1 {
285+
if len(c.sortedHashes)/c.NumberOfReplicas == 1 {
290286
return a, proto.Node{}, nil
291287
}
292288

@@ -315,8 +311,9 @@ func (c *Consistent) GetNeighborsEx(name string, n int, roles proto.ServerRoles)
315311
return nil, ErrEmptyCircle
316312
}
317313

318-
if c.count < int64(n) {
319-
n = int(c.count)
314+
count := len(c.sortedHashes) / c.NumberOfReplicas
315+
if count < n {
316+
n = count
320317
}
321318

322319
var (

consistent/consistent_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,8 @@ func TestSet(t *testing.T) {
705705
x.Add(NewNodeFromString("2222"))
706706

707707
x.Set([]Node{NewNodeFromString("3333"), NewNodeFromString("4444")})
708-
if x.count != 2 {
709-
t.Errorf("expected 2 elts, got %d", x.count)
708+
if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
709+
t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
710710
}
711711
a, b, err := x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
712712
if err != nil {
@@ -722,8 +722,8 @@ func TestSet(t *testing.T) {
722722
t.Errorf("expected a != b, they were both %v", a)
723723
}
724724
x.Set([]Node{NewNodeFromString("5555"), NewNodeFromString("4444")})
725-
if x.count != 2 {
726-
t.Errorf("expected 2 elts, got %d", x.count)
725+
if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
726+
t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
727727
}
728728
a, b, err = x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
729729
if err != nil {
@@ -739,8 +739,8 @@ func TestSet(t *testing.T) {
739739
t.Errorf("expected a != b, they were both %v", a)
740740
}
741741
x.Set([]Node{NewNodeFromString("5555"), NewNodeFromString("4444")})
742-
if x.count != 2 {
743-
t.Errorf("expected 2 elts, got %d", x.count)
742+
if len(x.sortedHashes)/x.NumberOfReplicas != 2 {
743+
t.Errorf("expected 2 elts, got %d", len(x.sortedHashes)/x.NumberOfReplicas)
744744
}
745745
a, b, err = x.GetTwoNeighbors("33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333wqer")
746746
if err != nil {

0 commit comments

Comments
 (0)