Skip to content

Commit

Permalink
fixes after first review
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-rosianu committed Sep 25, 2023
1 parent 730fb75 commit 35b3223
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion observer/circularQueueNodesProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// balancing of them
type circularQueueNodesProvider struct {
*baseNodeProvider
positionsHolder *mapCounters.MapCountersHolder
positionsHolder CounterMapsHolder
}

// NewCircularQueueNodesProvider returns a new instance of circularQueueNodesProvider
Expand Down
7 changes: 7 additions & 0 deletions observer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ type NodesHolder interface {
Count() int
IsInterfaceNil() bool
}

// CounterMapsHolder defines the actions to be implemented by a component that can hold multiple counter maps
type CounterMapsHolder interface {
ComputeShardPosition(availability data.ObserverDataAvailabilityType, shardID uint32, numNodes uint32) (uint32, error)
ComputeAllNodesPosition(availability data.ObserverDataAvailabilityType, numNodes uint32) (uint32, error)
IsInterfaceNil() bool
}
21 changes: 13 additions & 8 deletions observer/mapCounters/mapCountersHolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ var (
errNumNodesMustBeGreaterThanZero = errors.New("the number of nodes must be greater than 0")
)

// MapCountersHolder handles multiple counters map based on the data availability
type MapCountersHolder struct {
// mapCountersHolder handles multiple counters map based on the data availability
type mapCountersHolder struct {
countersMap map[data.ObserverDataAvailabilityType]*mapCounter
}

// NewMapCountersHolder populates the initial map and returns a new instance of MapCountersHolder
func NewMapCountersHolder() *MapCountersHolder {
// NewMapCountersHolder populates the initial map and returns a new instance of mapCountersHolder
func NewMapCountersHolder() *mapCountersHolder {
availabilityProvider := availabilityCommon.AvailabilityProvider{}
dataAvailabilityTypes := availabilityProvider.GetAllAvailabilityTypes()

countersMap := make(map[data.ObserverDataAvailabilityType]*mapCounter)
countersMap := make(map[data.ObserverDataAvailabilityType]*mapCounter, len(dataAvailabilityTypes))
for _, availability := range dataAvailabilityTypes {
countersMap[availability] = newMapCounter()
}

return &MapCountersHolder{
return &mapCountersHolder{
countersMap: countersMap,
}
}

// ComputeShardPosition returns the shard position based on the availability and the shard
func (mch *MapCountersHolder) ComputeShardPosition(availability data.ObserverDataAvailabilityType, shardID uint32, numNodes uint32) (uint32, error) {
func (mch *mapCountersHolder) ComputeShardPosition(availability data.ObserverDataAvailabilityType, shardID uint32, numNodes uint32) (uint32, error) {
counterMap, exists := mch.countersMap[availability]
if !exists {
return 0, errInvalidAvailability
Expand All @@ -48,7 +48,7 @@ func (mch *MapCountersHolder) ComputeShardPosition(availability data.ObserverDat
}

// ComputeAllNodesPosition returns the all nodes position based on the availability
func (mch *MapCountersHolder) ComputeAllNodesPosition(availability data.ObserverDataAvailabilityType, numNodes uint32) (uint32, error) {
func (mch *mapCountersHolder) ComputeAllNodesPosition(availability data.ObserverDataAvailabilityType, numNodes uint32) (uint32, error) {
counterMap, exists := mch.countersMap[availability]
if !exists {
return 0, errInvalidAvailability
Expand All @@ -61,3 +61,8 @@ func (mch *MapCountersHolder) ComputeAllNodesPosition(availability data.Observer
position := counterMap.computePositionForAllNodes(numNodes)
return position, nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (mch *mapCountersHolder) IsInterfaceNil() bool {
return mch == nil
}
16 changes: 13 additions & 3 deletions observer/mapCounters/mapCountersHolder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestMapCountersHolder_ComputeShardPositionShouldWorkForMultipleAvailabiliti

func calculatePosAndAssertForShard(
t *testing.T,
mch *MapCountersHolder,
mch *mapCountersHolder,
availability data.ObserverDataAvailabilityType,
shardID uint32,
numNodes uint32,
Expand All @@ -102,7 +102,7 @@ func calculatePosAndAssertForShard(
require.Equal(t, expectedPos, pos)
}

func calculatePosAndAssert(t *testing.T, mch *MapCountersHolder, shardID uint32, numNodes uint32, expectedPos uint32) {
func calculatePosAndAssert(t *testing.T, mch *mapCountersHolder, shardID uint32, numNodes uint32, expectedPos uint32) {
calculatePosAndAssertForShard(t, mch, data.AvailabilityRecent, shardID, numNodes, expectedPos)
}

Expand Down Expand Up @@ -146,7 +146,7 @@ func TestMapCountersHolder_ComputeAllNodesPositionShouldWork(t *testing.T) {
calculateAllNodesPosAndAssert(t, mch, 2, 0)
}

func calculateAllNodesPosAndAssert(t *testing.T, mch *MapCountersHolder, numNodes uint32, expectedPos uint32) {
func calculateAllNodesPosAndAssert(t *testing.T, mch *mapCountersHolder, numNodes uint32, expectedPos uint32) {
pos, err := mch.ComputeAllNodesPosition(data.AvailabilityRecent, numNodes)
require.NoError(t, err)
require.Equal(t, expectedPos, pos)
Expand Down Expand Up @@ -176,3 +176,13 @@ func TestMapCountersHolder_ConcurrentOperations(t *testing.T) {
}(i % 2)
}
}

func TestMapCountersHolder_IsInterfaceNil(t *testing.T) {
t.Parallel()

var mch *mapCountersHolder
require.True(t, mch.IsInterfaceNil())

mch = NewMapCountersHolder()
require.False(t, mch.IsInterfaceNil())
}

0 comments on commit 35b3223

Please sign in to comment.