Skip to content

Commit

Permalink
feat: add map of bootstrapped cluster members
Browse files Browse the repository at this point in the history
  • Loading branch information
buraksezer committed Jan 12, 2021
1 parent 09b41ad commit 91174dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
13 changes: 11 additions & 2 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"runtime"

"github.com/buraksezer/olric/internal/discovery"
"github.com/buraksezer/olric/internal/protocol"
"github.com/buraksezer/olric/stats"
"github.com/vmihailenco/msgpack"
Expand All @@ -38,10 +39,18 @@ func (db *Olric) stats() stats.Stats {
NumGoroutine: runtime.NumGoroutine(),
MemStats: *mem,
},
Partitions: make(map[uint64]stats.Partition),
Backups: make(map[uint64]stats.Partition),
Partitions: make(map[uint64]stats.Partition),
Backups: make(map[uint64]stats.Partition),
ClusterMembers: make(map[uint64]discovery.Member),
}

db.members.mtx.RLock()
for id, member := range db.members.m {
// List of bootstrapped cluster members
s.ClusterMembers[id] = member
}
db.members.mtx.RUnlock()

collect := func(partID uint64, part *partition) stats.Partition {
owners := part.loadOwners()
p := stats.Partition{
Expand Down
1 change: 1 addition & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ type Stats struct {
ClusterCoordinator discovery.Member
Partitions map[uint64]Partition
Backups map[uint64]Partition
ClusterMembers map[uint64]discovery.Member
}
34 changes: 34 additions & 0 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ package olric

import (
"context"
"reflect"
"testing"

"github.com/buraksezer/olric/internal/discovery"
)

func TestStatsStandalone(t *testing.T) {
Expand Down Expand Up @@ -51,6 +54,17 @@ func TestStatsStandalone(t *testing.T) {
t.Fatalf("Expected cluster coordinator: %v. Got: %v", db.this, s.ClusterCoordinator)
}

if len(s.ClusterMembers) != 1 {
t.Fatalf("Expected length of ClusterMembers map: 1. Got: %d", len(s.ClusterMembers))
}
m, ok := s.ClusterMembers[s.ClusterCoordinator.ID]
if !ok {
t.Fatalf("Member could not be found in ClusterMembers")
}
if !reflect.DeepEqual(m, s.ClusterCoordinator) {
t.Fatalf("Different member for the same ID")
}

var total int
for partID, part := range s.Partitions {
total += part.Length
Expand Down Expand Up @@ -140,4 +154,24 @@ func TestStatsCluster(t *testing.T) {
t.Fatalf("Expected cluster coordinator: %v. Got: %v", db1.this, s.ClusterCoordinator)
}
})

t.Run("check ClusterMembers", func(t *testing.T) {
s, err := db2.Stats()
if err != nil {
t.Fatalf("Expected nil. Got: %v", err)
}
if len(s.ClusterMembers) != 2 {
t.Fatalf("Expected length of ClusterMembers map: 2. Got: %d", len(s.ClusterMembers))
}

for _, member := range []discovery.Member{db1.this, db2.this} {
m, ok := s.ClusterMembers[member.ID]
if !ok {
t.Fatalf("Member: %s could not be found in ClusterMembers", member)
}
if !reflect.DeepEqual(member, m) {
t.Fatalf("Different member for the same ID: %s", member)
}
}
})
}

0 comments on commit 91174dc

Please sign in to comment.