Skip to content

Commit

Permalink
Refactor BalancedAllocator.Balancer to LocalShardsBalancer (#4761)
Browse files Browse the repository at this point in the history
* Refactor BalancedAllocator.Balancer to LocalShardsBalancer

Signed-off-by: Kunal Kotwani <[email protected]>

* Deprecate Balancer to maintain BWC

Signed-off-by: Kunal Kotwani <[email protected]>

Signed-off-by: Kunal Kotwani <[email protected]>
  • Loading branch information
kotwanikunal authored Oct 17, 2022
1 parent 6ac0c7c commit 1d65485
Show file tree
Hide file tree
Showing 7 changed files with 1,083 additions and 942 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fix weighted routing metadata deserialization error on process restart ([#4691](https://github.com/opensearch-project/OpenSearch/pull/4691))
- Refactor Base Action class javadocs to OpenSearch.API ([#4732](https://github.com/opensearch-project/OpenSearch/pull/4732))
- Migrate client transports to Apache HttpClient / Core 5.x ([#4459](https://github.com/opensearch-project/OpenSearch/pull/4459))
- Refactored BalancedAllocator.Balancer to LocalShardsBalancer ([#4761](https://github.com/opensearch-project/OpenSearch/pull/4761))
### Deprecated
### Removed
- Remove deprecated code to add node name into log pattern of log4j property file ([#4568](https://github.com/opensearch-project/OpenSearch/pull/4568))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.opensearch.cluster.routing.allocation;

import org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator;
import org.opensearch.cluster.routing.allocation.allocator.ShardsBalancer;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -27,11 +28,11 @@ public AllocationConstraints() {
}

class ConstraintParams {
private BalancedShardsAllocator.Balancer balancer;
private ShardsBalancer balancer;
private BalancedShardsAllocator.ModelNode node;
private String index;

ConstraintParams(BalancedShardsAllocator.Balancer balancer, BalancedShardsAllocator.ModelNode node, String index) {
ConstraintParams(ShardsBalancer balancer, BalancedShardsAllocator.ModelNode node, String index) {
this.balancer = balancer;
this.node = node;
this.index = index;
Expand All @@ -50,7 +51,7 @@ class ConstraintParams {
* This weight function is used only in case of unassigned shards to avoid overloading a newly added node.
* Weight calculation in other scenarios like shard movement and re-balancing remain unaffected by this function.
*/
public long weight(BalancedShardsAllocator.Balancer balancer, BalancedShardsAllocator.ModelNode node, String index) {
public long weight(ShardsBalancer balancer, BalancedShardsAllocator.ModelNode node, String index) {
int constraintsBreached = 0;
ConstraintParams params = new ConstraintParams(balancer, node, index);
for (Predicate<ConstraintParams> predicate : constraintPredicates) {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.cluster.routing.allocation.allocator;

import org.opensearch.cluster.routing.ShardRouting;
import org.opensearch.cluster.routing.allocation.AllocateUnassignedDecision;
import org.opensearch.cluster.routing.allocation.MoveDecision;

/**
* <p>
* A {@link ShardsBalancer} helps the {@link BalancedShardsAllocator} to perform allocation and balancing
* operations on the cluster.
* </p>
*
* @opensearch.internal
*/
public abstract class ShardsBalancer {

/**
* Performs allocation of unassigned shards on nodes within the cluster.
*/
abstract void allocateUnassigned();

/**
* Moves shards that cannot be allocated to a node anymore.
*/
abstract void moveShards();

/**
* Balances the nodes on the cluster model.
*/
abstract void balance();

/**
* Make a decision for allocating an unassigned shard.
* @param shardRouting the shard for which the decision has to be made
* @return the allocation decision
*/
abstract AllocateUnassignedDecision decideAllocateUnassigned(ShardRouting shardRouting);

/**
* Makes a decision on whether to move a started shard to another node.
* @param shardRouting the shard for which the decision has to be made
* @return a move decision for the shard
*/
abstract MoveDecision decideMove(ShardRouting shardRouting);

/**
* Makes a decision about moving a single shard to a different node to form a more
* optimally balanced cluster.
* @param shardRouting the shard for which the move decision has to be made
* @return a move decision for the shard
*/
abstract MoveDecision decideRebalance(ShardRouting shardRouting);

/**
* Returns the average of shards per node for the given index
*/
public float avgShardsPerNode() {
return Float.MAX_VALUE;
}

/**
* Returns the global average of shards per node
*/
public float avgShardsPerNode(String index) {
return Float.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.opensearch.cluster.OpenSearchAllocationTestCase;
import org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator;
import org.opensearch.cluster.routing.allocation.allocator.LocalShardsBalancer;
import org.opensearch.cluster.routing.allocation.allocator.ShardsBalancer;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;

Expand Down Expand Up @@ -45,7 +47,7 @@ public void testSettings() {
* for IndexShardPerNode constraint satisfied and breached.
*/
public void testIndexShardsPerNodeConstraint() {
BalancedShardsAllocator.Balancer balancer = mock(BalancedShardsAllocator.Balancer.class);
ShardsBalancer balancer = mock(LocalShardsBalancer.class);
BalancedShardsAllocator.ModelNode node = mock(BalancedShardsAllocator.ModelNode.class);
AllocationConstraints constraints = new AllocationConstraints();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.opensearch.cluster.routing.ShardRouting;
import org.opensearch.cluster.routing.ShardRoutingState;
import org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator;
import org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.Balancer;
import org.opensearch.cluster.routing.allocation.allocator.ShardsBalancer;
import org.opensearch.cluster.routing.allocation.decider.AllocationDecider;
import org.opensearch.cluster.routing.allocation.decider.AllocationDeciders;
import org.opensearch.cluster.routing.allocation.decider.Decision;
Expand All @@ -65,7 +65,7 @@
import static org.hamcrest.Matchers.startsWith;

/**
* Tests for balancing a single shard, see {@link Balancer#decideRebalance(ShardRouting)}.
* Tests for balancing a single shard, see {@link ShardsBalancer#decideRebalance(ShardRouting)}.
*/
public class BalancedSingleShardTests extends OpenSearchAllocationTestCase {

Expand Down

0 comments on commit 1d65485

Please sign in to comment.