Skip to content

Commit

Permalink
remove unnecessary fields from balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
pysel committed Nov 29, 2023
1 parent bf94155 commit 3b0cd7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
24 changes: 8 additions & 16 deletions balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ type Balancer struct {
// Multiple partitions can be mapped to the same range.
clients map[*partition.Range][]pbpartition.PartitionServiceClient

// goalReplicaRanges is the number of different sets of replicas that should be created
goalReplicaRanges int
// activePartitions is the number of currently registered partitions
activePartitions int

// coverage is used for tracking the tracked ranges
coverage *coverage
}
Expand All @@ -38,14 +33,12 @@ func NewBalancer(goalReplicaRanges int) *Balancer {
}

b := &Balancer{
DB: db,
clients: make(map[*partition.Range][]pbpartition.PartitionServiceClient),
goalReplicaRanges: goalReplicaRanges,
activePartitions: 0,
coverage: GetCoverage(),
DB: db,
clients: make(map[*partition.Range][]pbpartition.PartitionServiceClient),
coverage: GetCoverage(),
}

b.setupCoverage()
b.setupCoverage(goalReplicaRanges)

return b
}
Expand All @@ -61,7 +54,6 @@ func (b *Balancer) RegisterPartition(ctx context.Context, addr string) error {
}

b.clients[partitionRange] = append(b.clients[partitionRange], client)
b.activePartitions++

// on sucess, inrease min and max values of ticks
b.coverage.bumpTicks(lowerTick, upperTick)
Expand Down Expand Up @@ -121,17 +113,17 @@ func (b *Balancer) Get(ctx context.Context, key string) (*prototypes.GetResponse
}

// setupCoverage creates necessary ticks for coverage based on goalReplicaRanges
func (b *Balancer) setupCoverage() {
if b.goalReplicaRanges == 0 {
func (b *Balancer) setupCoverage(goalReplicaRanges int) {
if goalReplicaRanges == 0 {
b.coverage.addTick(newTick(big.NewInt(0)), false, false)
b.coverage.addTick(newTick(partition.MaxInt), false, false)
return
}

// Create a tick for each partition
for i := 0; i <= b.goalReplicaRanges; i++ {
for i := 0; i <= goalReplicaRanges; i++ {
numerator := new(big.Int).Mul(big.NewInt(int64(i)), partition.MaxInt)
value := new(big.Int).Div(numerator, big.NewInt(int64(b.goalReplicaRanges)))
value := new(big.Int).Div(numerator, big.NewInt(int64(goalReplicaRanges)))
b.coverage.addTick(newTick(value), false, false)
}
}
Expand Down
10 changes: 4 additions & 6 deletions balancer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ func NewBalancerTest(t *testing.T, goalReplicaRanges int) *Balancer {
require.NoError(t, err)

b := &Balancer{
DB: db,
clients: make(map[*partition.Range][]pbpartition.PartitionServiceClient),
goalReplicaRanges: goalReplicaRanges,
activePartitions: 0,
coverage: &coverage{},
DB: db,
clients: make(map[*partition.Range][]pbpartition.PartitionServiceClient),
coverage: &coverage{},
}

b.setupCoverage()
b.setupCoverage(goalReplicaRanges)

return b
}

0 comments on commit 3b0cd7f

Please sign in to comment.