Skip to content

Commit

Permalink
client config load balancer and load balancer class name are mutually…
Browse files Browse the repository at this point in the history
… exclusive

load balancer client config xml and yaml examples updated

(cherry picked from commit dac66e2)
  • Loading branch information
the-thing authored and sancar committed Sep 3, 2020
1 parent 88e76b7 commit cdd290c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static com.hazelcast.internal.config.DeclarativeConfigUtil.SYSPROP_CLIENT_CONFIG;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.validateSuffixInSystemProperty;
import static com.hazelcast.internal.util.Preconditions.checkFalse;
import static com.hazelcast.internal.util.Preconditions.checkHasText;
import static com.hazelcast.internal.util.Preconditions.isNotNull;
import static com.hazelcast.partition.strategy.StringPartitioningStrategy.getBaseName;

Expand Down Expand Up @@ -605,14 +606,17 @@ public LoadBalancer getLoadBalancer() {
}

/**
* Sets the {@link LoadBalancer}
* Sets the {@link LoadBalancer}.
* <p>
* If a load balancer class name was set, it will be removed.
*
* @param loadBalancer {@link LoadBalancer}
* @return configured {@link com.hazelcast.client.config.ClientConfig} for chaining
* @see com.hazelcast.client.LoadBalancer
*/
public ClientConfig setLoadBalancer(LoadBalancer loadBalancer) {
this.loadBalancer = loadBalancer;
this.loadBalancerClassName = null;
return this;
}

Expand All @@ -627,14 +631,17 @@ public String getLoadBalancerClassName() {
}

/**
* Sets load balancer class name
* Sets load balancer class name.
* <p>
* If a load balancer implementation was set, it will be removed.
*
* @param loadBalancerClassName {@link LoadBalancer}
* @return configured {@link com.hazelcast.client.config.ClientConfig} for chaining
* @see com.hazelcast.client.LoadBalancer
*/
public ClientConfig setLoadBalancerClassName(String loadBalancerClassName) {
this.loadBalancerClassName = loadBalancerClassName;
public ClientConfig setLoadBalancerClassName(@Nonnull String loadBalancerClassName) {
this.loadBalancerClassName = checkHasText(loadBalancerClassName, "Load balancer class name must contain text");
this.loadBalancer = null;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@
It has the attribute "type". The valid values for the type of the load balancer are"
* "random": The member the operations to be sent to is chosen randomly.
* "round-robin": The member the operations to be sent to is chosen in a round-robin fashion.
* "custom": The member the operations to be sent to is chosen by provided load balancer implementation.
The implementation class name is specified as text content.
-->
<load-balancer type="random"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ hazelcast-client:
# It has a scalar sub-node called "type". The valid values for the type of the load balancer are:
# * "random": The member the operations to be sent to is chosen randomly.
# * "round-robin": The member the operations to be sent to is chosen in a round-robin fashion.
# * "custom": The member the operations to be sent to is chosen by provided load balancer implementation.
# The implementation class name is specified in additional "class-name" key.
#
load-balancer:
type: random
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,6 @@ public void shouldUseConfigClassLoaderInstanceWhenClassNameNotSpecified() {
assertSame(loadBalancer, actual);
}

@Test
public void shouldUseConfigClassLoaderInstanceWhenClassNameSpecified() {
hazelcastFactory.newHazelcastInstance();

LoadBalancer loadBalancer = new RandomLB();

ClientConfig config = new ClientConfig();
config.setLoadBalancer(loadBalancer);
config.setLoadBalancerClassName("com.hazelcast.client.test.CustomLoadBalancer");

HazelcastInstance instance = hazelcastFactory.newHazelcastClient(config);
HazelcastClientInstanceImpl client = ClientTestUtil.getHazelcastClientInstanceImpl(instance);

LoadBalancer actual = client.getLoadBalancer();
assertSame(loadBalancer, actual);
}

@Test
public void shouldCreateCustomLoadBalancerWhenConfigInstanceNotProvidedAndClassNameSpecified() {
hazelcastFactory.newHazelcastInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.hazelcast.client.config;

import com.hazelcast.client.LoadBalancer;
import com.hazelcast.client.test.Employee;
import com.hazelcast.client.test.PortableFactory;
import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.client.util.RandomLB;
import com.hazelcast.config.Config;
import com.hazelcast.config.SerializationConfig;
import com.hazelcast.core.HazelcastInstance;
Expand All @@ -38,6 +40,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
Expand Down Expand Up @@ -135,4 +139,28 @@ public void testReliableTopic() {

assertEquals(100, newConfig.getReadBatchSize());
}

@Test
public void testSettingLoaderBalancerShouldClearLoadBalancerClassName() {
LoadBalancer loadBalancer = new RandomLB();

ClientConfig clientConfig = new ClientConfig();
clientConfig.setLoadBalancerClassName("com.hazelcast.client.test.CustomLoadBalancer");
clientConfig.setLoadBalancer(loadBalancer);

assertNull(clientConfig.getLoadBalancerClassName());
assertSame(loadBalancer, clientConfig.getLoadBalancer());
}

@Test
public void testSettingLoadBalancerClassNameShouldClearLoadBalancer() {
LoadBalancer loadBalancer = new RandomLB();

ClientConfig clientConfig = new ClientConfig();
clientConfig.setLoadBalancer(loadBalancer);
clientConfig.setLoadBalancerClassName("com.hazelcast.client.test.CustomLoadBalancer");

assertEquals("com.hazelcast.client.test.CustomLoadBalancer", clientConfig.getLoadBalancerClassName());
assertNull(clientConfig.getLoadBalancer());
}
}

0 comments on commit cdd290c

Please sign in to comment.