This is an automated email from the ASF dual-hosted git repository.
ptupitsyn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new da02f3556eb IGNITE-19221 Java thin: Add
ClientConfiguration.clusterDiscoveryEnabled (#10623)
da02f3556eb is described below
commit da02f3556ebeb80f4aa87335fa41932c41f61c0a
Author: Pavel Tupitsyn <[email protected]>
AuthorDate: Wed Apr 5 18:28:09 2023 +0300
IGNITE-19221 Java thin: Add ClientConfiguration.clusterDiscoveryEnabled
(#10623)
Cluster discovery is always enabled currently, which can be undesirable in
some use cases. Add a config property to disable discovery.
---
.../ignite/configuration/ClientConfiguration.java | 34 ++++++++++++++++++++++
.../client/thin/ClientDiscoveryContext.java | 8 +++--
.../org/apache/ignite/client/ReliabilityTest.java | 27 +++++++----------
.../client/thin/AbstractThinClientTest.java | 26 ++---------------
.../thin/ThinClientEnpointsDiscoveryTest.java | 15 ++++++++++
5 files changed, 68 insertions(+), 42 deletions(-)
diff --git
a/modules/core/src/main/java/org/apache/ignite/configuration/ClientConfiguration.java
b/modules/core/src/main/java/org/apache/ignite/configuration/ClientConfiguration.java
index 59a8b1c6ac7..ed1f0319fb0 100644
---
a/modules/core/src/main/java/org/apache/ignite/configuration/ClientConfiguration.java
+++
b/modules/core/src/main/java/org/apache/ignite/configuration/ClientConfiguration.java
@@ -126,6 +126,11 @@ public final class ClientConfiguration implements
Serializable {
*/
private ClientPartitionAwarenessMapperFactory
partitionAwarenessMapperFactory;
+ /**
+ * Whether cluster discovery should be enabled.
+ */
+ private boolean clusterDiscoveryEnabled = true;
+
/**
* Reconnect throttling period (in milliseconds). There are no more than
{@code reconnectThrottlingRetries}
* attempts to reconnect will be made within {@code
reconnectThrottlingPeriod} in case of connection loss.
@@ -575,6 +580,35 @@ public final class ClientConfiguration implements
Serializable {
return this;
}
+ /**
+ * Gets a value indicating whether cluster discovery should be enabled.
+ * <p>
+ * Default is {@code true}: client get addresses of server nodes from the
cluster and connects to all of them.
+ * <p>
+ * When {@code false}, client only connects to the addresses provided in
{@link #setAddresses(String...)} and
+ * {@link #setAddressesFinder(ClientAddressFinder)}.
+ * @return A value indicating whether cluster discovery should be enabled.
+ */
+ public boolean isClusterDiscoveryEnabled() {
+ return clusterDiscoveryEnabled;
+ }
+
+ /**
+ * Sets a value indicating whether cluster discovery should be enabled.
+ * <p>
+ * Default is {@code true}: client get addresses of server nodes from the
cluster and connects to all of them.
+ * <p>
+ * When {@code false}, client only connects to the addresses provided in
{@link #setAddresses(String...)} and
+ * {@link #setAddressesFinder(ClientAddressFinder)}.
+ * @param clusterDiscoveryEnabled Value indicating whether cluster
discovery should be enabled.
+ * @return {@code this} for chaining.
+ */
+ public ClientConfiguration setClusterDiscoveryEnabled(boolean
clusterDiscoveryEnabled) {
+ this.clusterDiscoveryEnabled = clusterDiscoveryEnabled;
+
+ return this;
+ }
+
/**
* @return reconnect throttling period.
*/
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientDiscoveryContext.java
b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientDiscoveryContext.java
index 38040dd862c..c1262d76dc9 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientDiscoveryContext.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientDiscoveryContext.java
@@ -63,6 +63,9 @@ public class ClientDiscoveryContext {
/** Configured address finder. */
@Nullable private final ClientAddressFinder addrFinder;
+ /** */
+ private final boolean enabled;
+
/** */
private volatile TopologyInfo topInfo;
@@ -77,6 +80,7 @@ public class ClientDiscoveryContext {
log = NullLogger.whenNull(clientCfg.getLogger());
addresses = clientCfg.getAddresses();
addrFinder = clientCfg.getAddressesFinder();
+ enabled = clientCfg.isClusterDiscoveryEnabled();
reset();
}
@@ -94,8 +98,8 @@ public class ClientDiscoveryContext {
* @return {@code True} if updated.
*/
boolean refresh(ClientChannel ch) {
- if (addrFinder != null)
- return false; // Used custom address finder.
+ if (addrFinder != null || !enabled)
+ return false; // Disabled or custom finder is used.
if
(!ch.protocolCtx().isFeatureSupported(ProtocolBitmaskFeature.CLUSTER_GROUP_GET_NODES_ENDPOINTS))
return false;
diff --git
a/modules/core/src/test/java/org/apache/ignite/client/ReliabilityTest.java
b/modules/core/src/test/java/org/apache/ignite/client/ReliabilityTest.java
index 14c4dc44eb3..5a014365a15 100644
--- a/modules/core/src/test/java/org/apache/ignite/client/ReliabilityTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/client/ReliabilityTest.java
@@ -111,7 +111,8 @@ public class ReliabilityTest extends AbstractThinClientTest
{
.setReconnectThrottlingRetries(0) // Disable throttling.
// Disable endpoints discovery, since in this test it reduces
attempts count and sometimes one extra
// attempt is required to complete operation without failure.
- .setAddressesFinder(new
StaticAddressFinder(cluster.clientAddresses().toArray(new
String[CLUSTER_SIZE])))
+ .setClusterDiscoveryEnabled(false)
+ .setAddresses(cluster.clientAddresses().toArray(new
String[CLUSTER_SIZE]))
)
) {
final Random rnd = new Random();
@@ -218,10 +219,8 @@ public class ReliabilityTest extends
AbstractThinClientTest {
public void testSingleServerDuplicatedFailover() throws Exception {
try (LocalIgniteCluster cluster = LocalIgniteCluster.start(1);
IgniteClient client =
Ignition.startClient(getClientConfiguration()
- .setAddressesFinder(new StaticAddressFinder(
- F.first(cluster.clientAddresses()),
- F.first(cluster.clientAddresses())
- )))
+ .setAddresses(F.first(cluster.clientAddresses()),
F.first(cluster.clientAddresses()))
+ .setClusterDiscoveryEnabled(false))
) {
ClientCache<Integer, Integer> cache = client.createCache("cache");
@@ -244,10 +243,8 @@ public class ReliabilityTest extends
AbstractThinClientTest {
try (LocalIgniteCluster cluster = LocalIgniteCluster.start(1);
IgniteClient client =
Ignition.startClient(getClientConfiguration()
.setRetryPolicy(new ClientRetryReadPolicy())
- .setAddressesFinder(new StaticAddressFinder(
- F.first(cluster.clientAddresses()),
- F.first(cluster.clientAddresses())
- )))
+ .setAddresses(F.first(cluster.clientAddresses()),
F.first(cluster.clientAddresses()))
+ .setClusterDiscoveryEnabled(false))
) {
ClientCache<Integer, Integer> cache = client.createCache("cache");
@@ -272,10 +269,8 @@ public class ReliabilityTest extends
AbstractThinClientTest {
try (LocalIgniteCluster cluster = LocalIgniteCluster.start(1);
IgniteClient client =
Ignition.startClient(getClientConfiguration()
.setRetryPolicy(new ExceptionRetryPolicy())
- .setAddressesFinder(new StaticAddressFinder(
- F.first(cluster.clientAddresses()),
- F.first(cluster.clientAddresses())
- )))
+ .setAddresses(F.first(cluster.clientAddresses()),
F.first(cluster.clientAddresses()))
+ .setClusterDiscoveryEnabled(false))
) {
ClientCache<Integer, Integer> cache = client.createCache("cache");
dropAllThinClientConnections(Ignition.allGrids().get(0));
@@ -303,10 +298,8 @@ public class ReliabilityTest extends
AbstractThinClientTest {
try (LocalIgniteCluster cluster = LocalIgniteCluster.start(1);
IgniteClient client =
Ignition.startClient(getClientConfiguration()
.setRetryLimit(1)
- .setAddressesFinder(new StaticAddressFinder(
- F.first(cluster.clientAddresses()),
- F.first(cluster.clientAddresses())
- )))
+ .setAddresses(F.first(cluster.clientAddresses()),
F.first(cluster.clientAddresses()))
+ .setClusterDiscoveryEnabled(false))
) {
ClientCache<Integer, Integer> cache = client.createCache("cache");
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/client/thin/AbstractThinClientTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/client/thin/AbstractThinClientTest.java
index f3cc4f01949..15f458fee05 100644
---
a/modules/core/src/test/java/org/apache/ignite/internal/client/thin/AbstractThinClientTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/internal/client/thin/AbstractThinClientTest.java
@@ -20,7 +20,6 @@ package org.apache.ignite.internal.client.thin;
import java.util.Arrays;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
-import org.apache.ignite.client.ClientAddressFinder;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.configuration.ClientConfiguration;
@@ -62,10 +61,9 @@ public abstract class AbstractThinClientTest extends
GridCommonAbstractTest {
addrs[i] = clientHost(node) + ":" + clientPort(node);
}
- if (isClientEndpointsDiscoveryEnabled())
- return getClientConfiguration().setAddresses(addrs);
- else
- return getClientConfiguration().setAddressesFinder(new
StaticAddressFinder(addrs));
+ return getClientConfiguration()
+ .setAddresses(addrs)
+
.setClusterDiscoveryEnabled(isClientEndpointsDiscoveryEnabled());
}
/**
@@ -163,22 +161,4 @@ public abstract class AbstractThinClientTest extends
GridCommonAbstractTest {
protected boolean isClientPartitionAwarenessEnabled() {
return true;
}
-
- /**
- * Address finder with static set of addresses, used to disable endpoints
discovery.
- */
- public static class StaticAddressFinder implements ClientAddressFinder {
- /** */
- private final String[] addrs;
-
- /** */
- public StaticAddressFinder(String... addrs) {
- this.addrs = addrs.clone();
- }
-
- /** {@inheritDoc} */
- @Override public String[] getAddresses() {
- return addrs.clone();
- }
- }
}
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientEnpointsDiscoveryTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientEnpointsDiscoveryTest.java
index d0849134e0a..6c91e3014f1 100644
---
a/modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientEnpointsDiscoveryTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientEnpointsDiscoveryTest.java
@@ -59,6 +59,21 @@ public class ThinClientEnpointsDiscoveryTest extends
ThinClientAbstractPartition
awaitChannelsInit(0, 3);
}
+ /** */
+ @Test
+ public void testEndpointsDiscoveryDisabled() throws Exception {
+ startGrids(2);
+
+ // Set only subset of nodes to connect, but wait for init of all nodes
channels (other nodes should be discovered).
+
initClient(getClientConfiguration(0).setClusterDiscoveryEnabled(false), 0);
+
+ Thread.sleep(300);
+
+ assertNull(channels[1]);
+ assertNull(channels[2]);
+ assertNull(channels[3]);
+ }
+
/** */
@Test
public void testDiscoveryAfterAllNodesFailed() throws Exception {