You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "vaijosh (via GitHub)" <gi...@apache.org> on 2023/03/22 05:55:19 UTC
[GitHub] [calcite-avatica] vaijosh opened a new pull request, #217: CALCITE-5581: Client side LB implementation
vaijosh opened a new pull request, #217:
URL: https://github.com/apache/calcite-avatica/pull/217
CALCITE-5581:Implement Basic client side load balancing in Avatica Driver
Added a basic client side load balancing behavior which allows user specify list of URLs and get client connection to any of the URL using Load balancing strategy. Out of the box we provided RandomSelect and RoundRobin load balancing strategies , but user can implement their own and plug it using lbStrategy parameter.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty closed pull request #217: [CALCITE-5581] Implement Basic client side load balancing in Avatica Driver
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty closed pull request #217: [CALCITE-5581] Implement Basic client side load balancing in Avatica Driver
URL: https://github.com/apache/calcite-avatica/pull/217
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1144363321
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -119,23 +119,25 @@ Service createService(AvaticaConnection connection, ConnectionConfig config) {
final Service service;
if (metaFactory != null) {
service = metaFactory.create(connection);
- } else if (config.url() != null) {
- final AvaticaHttpClient httpClient = getHttpClient(connection, config);
- final Serialization serializationType = getSerialization(config);
-
- LOG.debug("Instantiating {} service", serializationType);
- switch (serializationType) {
- case JSON:
- service = new RemoteService(httpClient);
- break;
- case PROTOBUF:
- service = new RemoteProtobufService(httpClient, new ProtobufTranslationImpl());
- break;
- default:
- throw new IllegalArgumentException("Unhandled serialization type: " + serializationType);
- }
} else {
- service = new MockJsonService(Collections.<String, String>emptyMap());
+ if (config.url() != null) {
Review Comment:
This change seems to be eqivalent to the existing code but nests one more if.
While we want to make sure that we always use curly braces in control statements, else if a special case, which actually improves readability.
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
+
+ public static final RoundRobinLBStrategy INSTANCE = new RoundRobinLBStrategy();
+ private RoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
Review Comment:
We may consider using the config object itself, instead of just the server list.
Both have pros and cons.
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
+
+ public static final RoundRobinLBStrategy INSTANCE = new RoundRobinLBStrategy();
+ private RoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ try {
+ String key = getKey(config);
+ String[] urls = config.getLbURLs().split(URL_SEPERATOR_CHAR);
Review Comment:
We should store the split list, and not re-process on each connection attempt.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1493692128
That's fine.
Commas are not valid in host part anyway, only in the path and query
The idea is that the %2c is passed verbatim to the remote server, and then it decodes it.
https://host1:8765/context%2ccontext,https://host2:8765/context%2ccontext
And then Jetty on the server side converts the latter parts back to
"context,context" before processing further
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1516091254
>
I have added a test for a following scenario.
1. Start few server.
2. Create connection
3. Specify the client side LB properties with RoundRobinLBStrategy.
4. Stop the server which appears first in the list if lbURLs
5. Try to create statement. Here we make sure that connection is failed ( and not attempted with different URL).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1495502591
Or an RR variant that shuffles the list on initialization ?
I think that that would have the advantage being deterministic, while avoiding the uneven load problem.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1495488118
What is your opinion on adding a RR variant that starts on a randomly selected host ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1497143192
> I suggest keeping RoundRobinLBStrategy as is, and adding something like ShuffledRoundRobinLBStrategy as a subclass. We could make ShuffledRoundRobinLBStrategy the default, but users can still choose classic RR if they want to.
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1162310680
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
+ configToIndexServedMap.put(key, nextURlIdx);
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+ return url;
+ }
+
+ private void initialiseCaches(String key, String lbURLs) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ List<String> list = Arrays.asList(urls);
+ Collections.shuffle(list);
+ list.toArray(urls);
Review Comment:
this doesn't do anything.
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
+ configToIndexServedMap.put(key, nextURlIdx);
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+ return url;
+ }
+
+ private void initialiseCaches(String key, String lbURLs) {
Review Comment:
nit:
This isn't really a cache. Better call this something else, like initialiseUrlList
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
Review Comment:
nit:
Use inc + modulo, that's easier to read.
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(RoundRobinLBStrategy.class);
+
+ public static final RoundRobinLBStrategy INSTANCE = new RoundRobinLBStrategy();
+ private RoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ if (!configToIndexServedMap.containsKey(key)) {
+ configToIndexServedMap.put(key, 0);
+ configToUrlListMap.put(key, config.getLbURLs().split(URL_SEPERATOR_CHAR));
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
Review Comment:
nit:
Use inc + modulo, that's easier to read.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh closed pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh closed pull request #217: CALCITE-5581: Client side LB implementation
URL: https://github.com/apache/calcite-avatica/pull/217
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172070997
##########
core/src/main/java/org/apache/calcite/avatica/ha/RandomSelectLBStrategy.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Random;
+
+/**
+ * Random Select strategy for client side load balancing.
+ */
+public class RandomSelectLBStrategy implements LBStrategy {
Review Comment:
Thanks @joshelser for the inputs.
For internal failover retries ( done by driver) we have "lb_connection_failover_retries" to s address this issue. By default we will do only 3 failover retries after connection will be marked as failed.
Not sure if you are pointing about some scenarios related to external retries( implemented by application).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163575749
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
+ configToIndexServedMap.put(key, nextURlIdx);
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+ return url;
+ }
+
+ private void initialiseCaches(String key, String lbURLs) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ List<String> list = Arrays.asList(urls);
+ Collections.shuffle(list);
+ list.toArray(urls);
Review Comment:
You are correct.
However, relying on that side effect hurts readability.
`urls = list.toArray(urls);`
is obviously correct, and does the same thing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1184601821
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -194,10 +196,20 @@ AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig c
} catch (Exception e) {
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
+ LOG.warn("Connection Failed: {}", e.getMessage());
LOG.debug("Failure detected in: {} milliseconds", elapsedTime);
- if (currentRetry < retries) {
+ if (currentRetry < retries - 1) {
Review Comment:
Done.
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -40,18 +40,15 @@ private RoundRobinLBStrategy() { }
Map<String, String[]> configToUrlListMap = new HashMap<>();
@Override
- public String getLbURL(ConnectionConfig config) {
+ public synchronized String getLbURL(ConnectionConfig config) {
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1153063735
##########
core/src/main/java/org/apache/calcite/avatica/ha/RandomSelectLBStrategy.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import java.util.Random;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Random Select strategy for client side load balancing.
+ */
+public class RandomSelectLBStrategy implements LBStrategy {
+ public static final String URL_SEPERATOR_CHAR = ",";
+ Random random = new Random();
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ try {
+ String[] urls = config.getLbURLs().split(URL_SEPERATOR_CHAR);
+ return urls[random.nextInt(urls.length)];
+ } catch (PatternSyntaxException e) {
+ throw new RuntimeException(e);
Review Comment:
We are using separator character as a pattern ( which is currently hardcoded semicolon). So we won't get PatternSyntaxException. So removed it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1491320901
One more feature that Knox has, but this is missing is the ability to sleep before retries.
A simple flat value like Knox has should be enough.
https://docs.cloudera.com/runtime/7.2.9/knox-authentication/topics/security-knox-configure-ha.html
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] joshelser commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "joshelser (via GitHub)" <gi...@apache.org>.
joshelser commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1516545680
> With [CALCITE-5009](https://issues.apache.org/jira/browse/CALCITE-5009), there is a strict one-to-one mapping between JDBC connection and HTTP client connection objects, and there is no way that a new HTTP Client connection object pointing to different URL can get associated with a JDBC connection during its lifetime.
Ah, I'm too far removed! I'll try to re-read today and think through this some more. Thanks for the great explanation. I was quite confused because it seemed like there was something fundamentally missing -- turns out what was missing was my understanding :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1495920863
I suggest keeping RoundRobinLBStrategy as is, and adding something like ShuffledRoundRobinLBStrategy as a subclass.
We could make ShuffledRoundRobinLBStrategy the default, but users can still choose classic RR if they want to.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1159770335
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
Review Comment:
It's not possible. We have these classes as SingleTon ( so that we can persist the state).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1144659331
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
+
+ public static final RoundRobinLBStrategy INSTANCE = new RoundRobinLBStrategy();
+ private RoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
Review Comment:
Config was natural choice. But from callers we don't always get same object (see AvaticaConnection.config). So this was not feasible. toString() method was another option I had considered, but it was a superset of config.getLbURLs().
So config.getLbURLs() was a better option here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1153063928
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -149,8 +150,16 @@ Service createService(AvaticaConnection connection, ConnectionConfig config) {
*/
AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig config) {
URL url;
+ String urlStr;
+ if (config.useClientSideLb()) {
+ urlStr = config.getLBStrategy().getLbURL(config);
+ LOG.info("LB Strategy:{}.Selected URL:{}",
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: [CALCITE-5581] Implement Basic client side load balancing in Avatica Driver
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1545407706
>
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1161110063
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ configToIndexServedMap.put(key, new Random().nextInt(urls.length));
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1495440415
> Do you have a specific reason for choosing random as the default strategy ? Knox uses round Robin (only) , and my impression is that round robin tends to be default for most balancers.
Yes this is intentional. Here are my thoughts.
Knox is centralized (all client send requests to KNOX) mechanism where as we implemented distributed (all client independently do the LB). For centralized mechanism, RoundRobin makes more sense. But for distributed approach Random select makes more sense.
To give an example, if we have some clients C1, C2, C3 interacting with DB at the same time using DB URLs DB1,DB2,DB3. So if the clients are doing transactions concurrently it's likely that they can overload Perticular DB server ( e.g. all C1, C2, C3 pickup first DB URL and connect to i). With Random RandomSelect algorithm there is less probability that all clients connect to same DB and overload it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1493687690
> Functionally,this looks good. Some remaining issues:
>
> * As Julien mentioned, URLs including commas are problematic. I think that specifying the encoded comma %2C in the URLs should work. Can you test and conform this ?
> * Need to add the new features to the docs
> * Need a release note that summarizes the feature (in this JIRA)
> * Invite another round of reviews by replying to your original email on the dev list.
Thanks @stoty.
I tried %2C but I get URL string like "http://vjoshi21-2.vj%2Coshi2.root.hwx.site:8765".
Will it be a make sense to add one more property called "LB_URL_SEPARATOR_CHAR" and fix this issue?
I am little concerned about the number of properties we are exposing with this features. So please advise.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172070997
##########
core/src/main/java/org/apache/calcite/avatica/ha/RandomSelectLBStrategy.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Random;
+
+/**
+ * Random Select strategy for client side load balancing.
+ */
+public class RandomSelectLBStrategy implements LBStrategy {
Review Comment:
Thanks @joshelser for the inputs.
For internal failover retries ( done by driver) we have "lb_connection_failover_retries" to s address this issue. By default we will do only 3 failover retries after connection will be marked as failed.
But yes, sticky session is not implemented as part of this change. So we will loose state, when we will try to create a new connection.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172071327
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
Review Comment:
As I mentioned above "lb_connection_failover_retries" should address this concern. But sticky session is not maintained which might create issues when we rely on some JDBC states.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172355086
##########
site/_docs/client_reference.md:
##########
@@ -193,3 +193,52 @@ on-hover images for the permalink, but oh well.
: _Default_: `false`.
: _Required_: No.
+
+<strong><a name="use_client_side_lb" href="#use_client_side_lb">use_client_side_lb</a></strong>
+
+: _Description_: Enables the client side load-balancing.
+: _Default_: `false`.
+
+: _Required_: No.
+
+<strong><a name="lb_urls" href="#lb_urls">lb_urls</a></strong>
+
+: _Description_: List of URLs in a comma separated format, for example "URL1,URL2...URLn", to be used by the client side
+load balancer. Depending on the load balancing strategy, load balancer selects one of the URLs from the list.
+
+: _Default_: `null`.
+
+: _Required_: No.
+
+<strong><a name="lb_strategy" href="#lb_strategy">lb_strategy</a></strong>
+
+: _Description_: The load balancing strategy to be used by the client side load balancer. It must be a fully qualified
+Java class name which implements `org.apache.calcite.avatica.ha.LBStrategy`. Two implementations are provided
Review Comment:
Thanks @joshelser.
I have changed it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1515750008
The automatic reconnect feature has been disabled by default in CALCITE-5009, @joshelser .
Adding a check so that the LB feature and the automatic reconnect cannot be enabled together is a good idea, though, and we should do that, @vaijosh.
This is how it's supposed to work:
With CALCITE-5009, there is a strict one-to-one mapping between JDBC connection and HTTP client connection objects, and there is no way that a new HTTP Client connection object pointing to different URL can get associated with a JDBC connection during its lifetime.
The URL in the HTTP client connection object for a JDBC connection doesn't change after the JDBC Connection has been initialized. A new JDBC Connection is only created ( in Driver#connect() ) when the user explicitly requests a new Connection object from JDBC. The LB host selection / retery code is guaranteed to only ever run before the user receives a new Connection, and never on an active JDBC Connection.
This ensures stickyness, if the server goes away, then the JDBC Connection simply fails normally, and the application has to create a new one.
Of course, we might have missed some code path where that happens, but this is the general idea.
Having a test specifically for this behaviour would be a good thing. I'm thinking of creating a single connection, and issuing BEGIN, upsert, COMMIT sequence from three different statements created from the same connection would be a good way to demonstrate that they all connect to the same Avatica server.
We could also add a test that checks wich Avatica server an active connection is using, kills that server, and the checks that the connection goes into a fail state without trying to transparently re-connect.
As for using an external LB solution, the main motivation for this patch was Kerberos/SPNEGO, which precludes the use of standard external LBs.
Apart from removing the need for Knox (the only LB that works with Kerberos that I know), adding the LB code into the client also removes the LB as a single point of failure. (Which otherwise requires additional DNS or network level complexity)
We also haven't tested yet whether the new Jetty kerberos code that you've enabled in Avatica fixes all the known perf problems with Knox. I suspect that it fixes well-behaved applications that reuse the JDBC connection object, and hence the HTTP session, but spamming new Connections for each statement would stil overload the identity infra.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172438183
##########
server/src/test/java/org/apache/calcite/avatica/ha/ConnectionPropertiesHATest.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.BuiltInConnectionProperty;
+import org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImpl;
+import org.apache.calcite.avatica.remote.AvaticaHttpClient;
+import org.apache.calcite.avatica.remote.AvaticaServersForTest;
+import org.apache.calcite.avatica.remote.Driver;
+import org.apache.calcite.avatica.remote.RemoteProtobufService;
+import org.apache.calcite.avatica.server.AvaticaProtobufHandler;
+import org.apache.calcite.avatica.server.HttpServer;
+import org.apache.calcite.avatica.server.Main;
+
+import org.apache.hc.client5.http.ConnectTimeoutException;
+import org.apache.hc.client5.http.HttpHostConnectException;
+import org.apache.hc.core5.util.Timeout;
+
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.sql.DriverManager;
+import java.util.Locale;
+import java.util.Properties;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class ConnectionPropertiesHATest {
+ private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest();
+ private static final String[] SERVER_ARGS = {
+ AvaticaServersForTest.FullyRemoteJdbcMetaFactory.class.getName() };
+ public static final int NO_OF_SERVERS = 5;
+ public static final String HTTP_LOCALHOST = "http://localhost:";
+ public static final String COMMA = ",";
+ public static final String OS_NAME_LOWERCASE = System.getProperty("os.name")
+ .toLowerCase(Locale.ROOT);
+ public static final String WINDOWS_OS_PREFIX = "windows";
+ private static String lbURLs = "";
+ private static final int START_PORT = 10000;
+ private static String[] urls;
+
+ @BeforeClass
+ public static void beforeClass() throws ClassNotFoundException, InvocationTargetException,
+ InstantiationException, IllegalAccessException, NoSuchMethodException {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ int port = START_PORT + i;
+ Main.start(SERVER_ARGS, port, AvaticaProtobufHandler::new);
+ sb.append(HTTP_LOCALHOST).append(port).append(COMMA);
+ }
+ lbURLs = sb.substring(0, sb.length() - 1);
+ urls = lbURLs.split(COMMA);
+ }
+
+
+ @Test
+ public void connectionPropertiesNoHATest() throws Exception {
+ Properties properties = new Properties();
+ String url = SERVERS.getJdbcUrl(10000, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn1 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ Assert.assertNotNull(conn1);
+ }
+
+ @Test
+ public void connectionPropertiesHATestRandomSelectLB() {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RandomSelectLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ try {
+ getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ } catch (Exception e) {
+ Assert.fail(); //Verify that exception is not generated.
+ }
+ }
+ }
+
+ @Test
+ public void connectionPropertiesHATestRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String uri1 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[0], uri1);
+
+ String uri2 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[1], uri2);
+
+ String uri3 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[2], uri3);
+ }
+
+ @Test
+ public void connectionPropertiesHATestShuffledRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ ShuffledRoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String firstConnectiondURL = getConnectionURI((AvaticaConnection)
+ DriverManager.getConnection(url, properties));
+
+ Assert.assertNotNull(firstConnectiondURL);
+
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ try {
+ getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ } catch (Exception e) {
+ Assert.fail(); //In System test verify that exception is not generated.
+ }
+ }
+ }
+
+ @Test
+ public void connectionPropertiesHATestInvalidLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(), "com.incorrect.badLb");
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof ClassNotFoundException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestLongURlList() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+ String uri1 = getConnectionURI(conn);
+ Assert.assertEquals(urls[0], uri1);
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestInvalidLBUrl() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://invalid:" + START_PORT);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof UnknownHostException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHALBFailover() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "2");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_SLEEP_TIME.name(), "100");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ //Invalid URL at first position in lb_urls
+ StringBuilder sb = new StringBuilder("http://invalidurl:").append(START_PORT).append(",");
+
+ //Put Valid URL at second position in lb_urls. This should be returned during failover.
+ sb.append(urls[0]).append(",");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ AvaticaConnection connection = (AvaticaConnection) DriverManager.getConnection(url,
+ properties);
+ String uri = getConnectionURI(connection);
+ Assert.assertEquals(urls[0], uri);
+ }
+
+ @Test
+ public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception {
+ //Skip the test for Windows.
+ Assume.assumeFalse(OS_NAME_LOWERCASE.startsWith(WINDOWS_OS_PREFIX));
+ Properties properties = new Properties();
+
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0");
+ //240.0.0.1 is special URL which should result in connection timeout.
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://240.0.0.1:" + 9000);
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ long startTime = System.currentTimeMillis();
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ long endTime = System.currentTimeMillis();
+ long elapsedTime = endTime - startTime;
+ Assert.assertTrue(elapsedTime < Timeout.ofMinutes(3).toMilliseconds());
+ Assert.assertTrue(elapsedTime >= 5000);
+ Assert.assertTrue(re.getCause() instanceof ConnectTimeoutException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesCreateStatementAfterDisconnect() throws Exception {
+
+ //Start a new server at port 100 port from the startport
+ int test_server_port = START_PORT + 100;
+ HttpServer avaticaServer = Main.start(SERVER_ARGS, test_server_port,
+ AvaticaProtobufHandler::new);
+
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "2");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_SLEEP_TIME.name(), "100");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ StringBuilder sb = new StringBuilder();
+ //First URL will be server we started in this test
+ sb.append("http://localhost:").append(test_server_port).append(",");
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+
+ //Create a connection
+ String url = SERVERS.getJdbcUrl(test_server_port, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+
Review Comment:
Actually, to exercise the most code paths, and make sure we do not transparently reconnect:
Create connection1
Create statement1
Execute select
Kill server
Try to execute a query on statement1 (expect to fail)
Try to create statement2 on connection1(expect to fail)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172071327
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
Review Comment:
As I mentioned above "lb_connection_failover_retries" should address this concern.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] joshelser commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "joshelser (via GitHub)" <gi...@apache.org>.
joshelser commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1171645775
##########
site/_docs/client_reference.md:
##########
@@ -193,3 +193,52 @@ on-hover images for the permalink, but oh well.
: _Default_: `false`.
: _Required_: No.
+
+<strong><a name="use_client_side_lb" href="#use_client_side_lb">use_client_side_lb</a></strong>
+
+: _Description_: Enables the client side load-balancing.
+: _Default_: `false`.
+
+: _Required_: No.
+
+<strong><a name="lb_urls" href="#lb_urls">lb_urls</a></strong>
+
+: _Description_: List of URLs in a comma separated format, for example "URL1,URL2...URLn", to be used by the client side
+load balancer. Depending on the load balancing strategy, load balancer selects one of the URLs from the list.
+
+: _Default_: `null`.
+
+: _Required_: No.
+
+<strong><a name="lb_strategy" href="#lb_strategy">lb_strategy</a></strong>
+
+: _Description_: The load balancing strategy to be used by the client side load balancer. It must be a fully qualified
+Java class name which implements `org.apache.calcite.avatica.ha.LBStrategy`. Two implementations are provided
Review Comment:
"Three" implementations appear to be provided.
##########
core/src/main/java/org/apache/calcite/avatica/ha/RandomSelectLBStrategy.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Random;
+
+/**
+ * Random Select strategy for client side load balancing.
+ */
+public class RandomSelectLBStrategy implements LBStrategy {
Review Comment:
If you choose a random Avatica server every time, the client will (effectively) be stuck in a retry loop to re-create new Connection and Statements on that Avatica server, to then be bounced to a different Avatica server.
I think this implementation would be overtly harmful should a user ever try to configure it.
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
Review Comment:
Same with this implementation, if the caller is invoking a different Avatica server every call, the client will be "stuck" constantly trying to create and use the Statement/Connection which were on the n-1th server.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: [CALCITE-5581] Implement Basic client side load balancing in Avatica Driver
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1547217332
> The docs formatting for the new options is off. (defaults are not formatted as for the rest of the options) You can preview it by following the instructions in site/README.md (I had to replace "docker compose" with "docker-compose")
Thank @stoty
I have fixed the formatting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163996778
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(RoundRobinLBStrategy.class);
+
+ public static final RoundRobinLBStrategy INSTANCE = new RoundRobinLBStrategy();
+ private RoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ if (!configToIndexServedMap.containsKey(key)) {
+ configToIndexServedMap.put(key, 0);
+ configToUrlListMap.put(key, config.getLbURLs().split(URL_SEPERATOR_CHAR));
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163685453
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ configToIndexServedMap.put(key, 0);
+ initialiseUrlList(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+
+ urlIndex++;
Review Comment:
I had
urlIndex = (urlIndex + 1) % urls.length
or
urlIndex = ++urlIndex % urls.length
in mind
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1159770335
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
Review Comment:
It's not possible. We have implemented these classes as singleton( so that we can persist the state). The constructor is private so we can't extend the RoundRobinLb class.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163568763
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
+ configToIndexServedMap.put(key, nextURlIdx);
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+ return url;
+ }
+
+ private void initialiseCaches(String key, String lbURLs) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ List<String> list = Arrays.asList(urls);
+ Collections.shuffle(list);
+ list.toArray(urls);
Review Comment:
@stoty
It works as follows
1. Convert from array to list using Arrays.asList()
2. Collection.shuffle shuffles the list passed as an argument ( void return)
3. List.toArray( array ) - Put the shuffled elements in original array. [Check this](https://devdocs.io/openjdk~8/java/util/list). We can ignored the return because we know that passed array will have shuffled elements.
Please let me know if you see any issue in above behavior or other suggestion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1154498701
##########
core/src/main/java/org/apache/calcite/avatica/BuiltInConnectionProperty.java:
##########
@@ -91,7 +95,28 @@ public enum BuiltInConnectionProperty implements ConnectionProperty {
TRANSPARENT_RECONNECTION("transparent_reconnection", Type.BOOLEAN, Boolean.FALSE, false),
/** Number of rows to fetch per call. */
- FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false);
+ FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false),
+
+ /** Avatica connection HA property - use client side load balancing **/
+ USE_CLIENT_SIDE_LB("useClientSideLB", Type.BOOLEAN, Boolean.FALSE, false),
Review Comment:
After changing the property names to under score format the properties are not reaching to the retry logic block ( which is dependent of serialized properties). So I need to investigate this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1155030053
##########
core/src/main/java/org/apache/calcite/avatica/BuiltInConnectionProperty.java:
##########
@@ -91,7 +95,28 @@ public enum BuiltInConnectionProperty implements ConnectionProperty {
TRANSPARENT_RECONNECTION("transparent_reconnection", Type.BOOLEAN, Boolean.FALSE, false),
/** Number of rows to fetch per call. */
- FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false);
+ FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false),
+
+ /** Avatica connection HA property - use client side load balancing **/
+ USE_CLIENT_SIDE_LB("useClientSideLB", Type.BOOLEAN, Boolean.FALSE, false),
Review Comment:
Fixed the issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1505147335
Looks good to me, but it would be nice to get another review before committing.
@joshelser Do you have the time take a look ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1144666381
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
+
+ public static final RoundRobinLBStrategy INSTANCE = new RoundRobinLBStrategy();
+ private RoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ try {
+ String key = getKey(config);
+ String[] urls = config.getLbURLs().split(URL_SEPERATOR_CHAR);
Review Comment:
Good point. I have done the changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1152818002
##########
core/src/main/java/org/apache/calcite/avatica/BuiltInConnectionProperty.java:
##########
@@ -91,7 +95,28 @@ public enum BuiltInConnectionProperty implements ConnectionProperty {
TRANSPARENT_RECONNECTION("transparent_reconnection", Type.BOOLEAN, Boolean.FALSE, false),
/** Number of rows to fetch per call. */
- FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false);
+ FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false),
+
+ /** Avatica connection HA property - use client side load balancing **/
+ USE_CLIENT_SIDE_LB("useClientSideLB", Type.BOOLEAN, Boolean.FALSE, false),
Review Comment:
switch to underscore from CamelCase
##########
core/src/main/java/org/apache/calcite/avatica/ConnectionConfig.java:
##########
@@ -66,6 +67,21 @@ public interface ConnectionConfig {
boolean transparentReconnectionEnabled();
/** @see BuiltInConnectionProperty#FETCH_SIZE */
int fetchSize();
+
Review Comment:
Nit: the other properties are listed without empty lines between them
##########
core/src/main/java/org/apache/calcite/avatica/ha/RandomSelectLBStrategy.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import java.util.Random;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Random Select strategy for client side load balancing.
+ */
+public class RandomSelectLBStrategy implements LBStrategy {
+ public static final String URL_SEPERATOR_CHAR = ",";
+ Random random = new Random();
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ try {
+ String[] urls = config.getLbURLs().split(URL_SEPERATOR_CHAR);
+ return urls[random.nextInt(urls.length)];
+ } catch (PatternSyntaxException e) {
+ throw new RuntimeException(e);
Review Comment:
Use a more specific exception, like IllegalArgumentException
##########
core/src/main/java/org/apache/calcite/avatica/remote/AvaticaCommonsHttpClientImpl.java:
##########
@@ -91,11 +96,19 @@ public AvaticaCommonsHttpClientImpl(URL url) {
this.uri = toURI(Objects.requireNonNull(url));
}
- protected void initializeClient(PoolingHttpClientConnectionManager pool) {
+ protected void initializeClient(PoolingHttpClientConnectionManager pool,
+ ConnectionConfig config) {
this.authCache = new BasicAuthCache();
// A single thread-safe HttpClient, pooling connections via the
// ConnectionManager
- this.client = HttpClients.custom().setConnectionManager(pool).build();
+ RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
+ if (config.useClientSideLb()) {
+ requestConfigBuilder.setConnectTimeout(config.getHttpConnectionTimeout(),
Review Comment:
I wonder if it would be cleaner to process the config in the factory, and pass a RequestConfig object instead.
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -149,8 +150,16 @@ Service createService(AvaticaConnection connection, ConnectionConfig config) {
*/
AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig config) {
URL url;
+ String urlStr;
+ if (config.useClientSideLb()) {
+ urlStr = config.getLBStrategy().getLbURL(config);
+ LOG.info("LB Strategy:{}.Selected URL:{}",
Review Comment:
Nit: Move logging to the strategy ?
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -159,25 +168,43 @@ AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig c
return httpClientFactory.getClient(url, config, connection.getKerberosConnection());
}
-
@Override public Connection connect(String url, Properties info)
throws SQLException {
- AvaticaConnection conn = (AvaticaConnection) super.connect(url, info);
- if (conn == null) {
- // It's not an url for our driver
- return null;
+ ConnectionConfig config = new ConnectionConfigImpl(info);
+ int retries = 0;
+ int currentRetry = 0;
+ if (config.useClientSideLb()) {
+ retries = config.getLBConnectionFailoverRetries();
}
+ do {
+ long startTime = System.currentTimeMillis();
+ AvaticaConnection conn = (AvaticaConnection) super.connect(url, info);
+ if (conn == null) {
+ // It's not an url for our driver
+ return null;
+ }
- Service service = conn.getService();
-
- // super.connect(...) should be creating a service and setting it in the AvaticaConnection
- assert null != service;
-
- service.apply(
- new Service.OpenConnectionRequest(conn.id,
- Service.OpenConnectionRequest.serializeProperties(info)));
+ Service service = conn.getService();
- return conn;
+ // super.connect(...) should be creating a service and setting it in the AvaticaConnection
+ assert null != service;
+ try {
+ service.apply(
+ new Service.OpenConnectionRequest(conn.id,
+ Service.OpenConnectionRequest.serializeProperties(info)));
Review Comment:
We have some logic to filter these properties.
Make sure that we do not send the LB properties to the server.
##########
core/src/main/java/org/apache/calcite/avatica/remote/AvaticaCommonsHttpClientImpl.java:
##########
@@ -91,11 +96,19 @@ public AvaticaCommonsHttpClientImpl(URL url) {
this.uri = toURI(Objects.requireNonNull(url));
}
- protected void initializeClient(PoolingHttpClientConnectionManager pool) {
+ protected void initializeClient(PoolingHttpClientConnectionManager pool,
+ ConnectionConfig config) {
this.authCache = new BasicAuthCache();
// A single thread-safe HttpClient, pooling connections via the
// ConnectionManager
- this.client = HttpClients.custom().setConnectionManager(pool).build();
+ RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
+ if (config.useClientSideLb()) {
Review Comment:
Do not gate this setting on LB only.
Instead, check if the timeout property is set, and if not then leave it at the default.
##########
server/src/test/java/org/apache/calcite/avatica/ha/ConnectionPropertiesHATest.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.BuiltInConnectionProperty;
+import org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImpl;
+import org.apache.calcite.avatica.remote.AvaticaHttpClient;
+import org.apache.calcite.avatica.remote.AvaticaServersForTest;
+import org.apache.calcite.avatica.remote.Driver;
+import org.apache.calcite.avatica.remote.RemoteProtobufService;
+import org.apache.calcite.avatica.server.AvaticaProtobufHandler;
+import org.apache.calcite.avatica.server.Main;
+
+import org.apache.hc.client5.http.ConnectTimeoutException;
+import org.apache.hc.core5.util.Timeout;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.sql.DriverManager;
+import java.util.Properties;
+
+public class ConnectionPropertiesHATest {
+ private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest();
+ private static final String[] SERVER_ARGS = {
+ AvaticaServersForTest.FullyRemoteJdbcMetaFactory.class.getName() };
+ public static final int NO_OF_SERVERS = 5;
+ public static final String HTTP_LOCALHOST = "http://localhost:";
+ public static final String COMMA = ",";
+ private static String lbURLs = "";
+ private static final int START_PORT = 10000;
+ private static String[] urls;
+
+ @BeforeClass
+ public static void beforeClass() throws ClassNotFoundException, InvocationTargetException,
+ InstantiationException, IllegalAccessException, NoSuchMethodException {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ int port = START_PORT + i;
+ Main.start(SERVER_ARGS, port, AvaticaProtobufHandler::new);
+ sb.append(HTTP_LOCALHOST).append(port).append(COMMA);
+ }
+ lbURLs = sb.substring(0, sb.length() - 1);
+ urls = lbURLs.split(COMMA);
+ }
+
+
+ @Test
+ public void connectionPropertiesNoHATest() throws Exception {
+ Properties properties = new Properties();
+ String url = SERVERS.getJdbcUrl(10000, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn1 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ Assert.assertNotNull(conn1);
+ }
+
+ @Test
+ public void connectionPropertiesHATestRandomSelectLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn1 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ AvaticaConnection conn2 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ AvaticaConnection conn3 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+
+ Assert.assertTrue(!getConnectionURI(conn1).equals(urls[0])
+ || !getConnectionURI(conn2).equals(urls[1])
+ || !getConnectionURI(conn3).equals(urls[2]));
+ }
+
+ @Test
+ public void connectionPropertiesHATestRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String uri1 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[0], uri1);
+
+ String uri2 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[1], uri2);
+
+ String uri3 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[2], uri3);
+
+ }
+
+ @Test
+ public void connectionPropertiesHATestInvalidLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(), "com.incorrect.badLb");
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof ClassNotFoundException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestLongURlList() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+ String uri1 = getConnectionURI(conn);
+ Assert.assertEquals(urls[0], uri1);
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestInvalidLBUrl() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 3; i++) {
+ sb.append("http://invalid:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof UnknownHostException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHALBFailover() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "3");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ String lbUrls = "http://localhost:" + START_PORT;
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbUrls);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof UnknownHostException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0");
+ String urls = "http://240.0.0.1:" + START_PORT;
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), urls);
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ long startTime = System.currentTimeMillis();
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ long endTime = System.currentTimeMillis();
+ long elapsedTime = endTime - startTime;
+ Assert.assertTrue(elapsedTime < Timeout.ofMinutes(3).toMicroseconds());
Review Comment:
Are you comparing milli and micro seconds here ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1492764793
> One more feature that Knox has, but this is missing is the ability to sleep before retries. A simple flat value like Knox has should be enough.
>
> https://docs.cloudera.com/runtime/7.2.9/knox-authentication/topics/security-knox-configure-ha.html
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] joshelser commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "joshelser (via GitHub)" <gi...@apache.org>.
joshelser commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1514931737
Sorry, I was out last week. Looking :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1515750291
Oh and thanks for taking the time to look at this, @joshelser .
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1160042299
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ configToIndexServedMap.put(key, new Random().nextInt(urls.length));
Review Comment:
Thanks @stoty
Yes. I got your point. So shuffle will solve repetition problem within JVM, so its better than RandomSelect in this sense. I will make these changes tomorrow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163657255
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
+ configToIndexServedMap.put(key, nextURlIdx);
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+ return url;
+ }
+
+ private void initialiseCaches(String key, String lbURLs) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ List<String> list = Arrays.asList(urls);
+ Collections.shuffle(list);
+ list.toArray(urls);
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1182175588
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -194,10 +196,20 @@ AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig c
} catch (Exception e) {
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
+ LOG.warn("Connection Failed: {}", e.getMessage());
LOG.debug("Failure detected in: {} milliseconds", elapsedTime);
- if (currentRetry < retries) {
+ if (currentRetry < retries - 1) {
Review Comment:
This means that if you set retries to 1, then you won't retry at all.
Technically, retry = 1 mens and initial attempt, and the one retry.
IMO the previous version was correct,
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -40,18 +40,15 @@ private RoundRobinLBStrategy() { }
Map<String, String[]> configToUrlListMap = new HashMap<>();
@Override
- public String getLbURL(ConnectionConfig config) {
+ public synchronized String getLbURL(ConnectionConfig config) {
Review Comment:
Nit double space
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -194,10 +196,20 @@ AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig c
} catch (Exception e) {
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
+ LOG.warn("Connection Failed: {}", e.getMessage());
LOG.debug("Failure detected in: {} milliseconds", elapsedTime);
- if (currentRetry < retries) {
+ if (currentRetry < retries - 1) {
Review Comment:
The docs you wrote also says that this the number of REtries.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: [CALCITE-5581] Implement Basic client side load balancing in Avatica Driver
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1550894343
The commit message doesn't include the JIRA number, but I will fix that on commit.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1515751505
Thinking some more about this, LB doesn't make the transaprent reconnect sitation worse than it is without LB, it doesn't really matter if we re-create an unrelated new connection on the same server, or on a different server, the state is lost just the same.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172580372
##########
server/src/test/java/org/apache/calcite/avatica/ha/ConnectionPropertiesHATest.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.BuiltInConnectionProperty;
+import org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImpl;
+import org.apache.calcite.avatica.remote.AvaticaHttpClient;
+import org.apache.calcite.avatica.remote.AvaticaServersForTest;
+import org.apache.calcite.avatica.remote.Driver;
+import org.apache.calcite.avatica.remote.RemoteProtobufService;
+import org.apache.calcite.avatica.server.AvaticaProtobufHandler;
+import org.apache.calcite.avatica.server.HttpServer;
+import org.apache.calcite.avatica.server.Main;
+
+import org.apache.hc.client5.http.ConnectTimeoutException;
+import org.apache.hc.client5.http.HttpHostConnectException;
+import org.apache.hc.core5.util.Timeout;
+
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.sql.DriverManager;
+import java.util.Locale;
+import java.util.Properties;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class ConnectionPropertiesHATest {
+ private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest();
+ private static final String[] SERVER_ARGS = {
+ AvaticaServersForTest.FullyRemoteJdbcMetaFactory.class.getName() };
+ public static final int NO_OF_SERVERS = 5;
+ public static final String HTTP_LOCALHOST = "http://localhost:";
+ public static final String COMMA = ",";
+ public static final String OS_NAME_LOWERCASE = System.getProperty("os.name")
+ .toLowerCase(Locale.ROOT);
+ public static final String WINDOWS_OS_PREFIX = "windows";
+ private static String lbURLs = "";
+ private static final int START_PORT = 10000;
+ private static String[] urls;
+
+ @BeforeClass
+ public static void beforeClass() throws ClassNotFoundException, InvocationTargetException,
+ InstantiationException, IllegalAccessException, NoSuchMethodException {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ int port = START_PORT + i;
+ Main.start(SERVER_ARGS, port, AvaticaProtobufHandler::new);
+ sb.append(HTTP_LOCALHOST).append(port).append(COMMA);
+ }
+ lbURLs = sb.substring(0, sb.length() - 1);
+ urls = lbURLs.split(COMMA);
+ }
+
+
+ @Test
+ public void connectionPropertiesNoHATest() throws Exception {
+ Properties properties = new Properties();
+ String url = SERVERS.getJdbcUrl(10000, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn1 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ Assert.assertNotNull(conn1);
+ }
+
+ @Test
+ public void connectionPropertiesHATestRandomSelectLB() {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RandomSelectLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ try {
+ getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ } catch (Exception e) {
+ Assert.fail(); //Verify that exception is not generated.
+ }
+ }
+ }
+
+ @Test
+ public void connectionPropertiesHATestRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String uri1 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[0], uri1);
+
+ String uri2 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[1], uri2);
+
+ String uri3 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[2], uri3);
+ }
+
+ @Test
+ public void connectionPropertiesHATestShuffledRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ ShuffledRoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String firstConnectiondURL = getConnectionURI((AvaticaConnection)
+ DriverManager.getConnection(url, properties));
+
+ Assert.assertNotNull(firstConnectiondURL);
+
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ try {
+ getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ } catch (Exception e) {
+ Assert.fail(); //In System test verify that exception is not generated.
+ }
+ }
+ }
+
+ @Test
+ public void connectionPropertiesHATestInvalidLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(), "com.incorrect.badLb");
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof ClassNotFoundException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestLongURlList() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+ String uri1 = getConnectionURI(conn);
+ Assert.assertEquals(urls[0], uri1);
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestInvalidLBUrl() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://invalid:" + START_PORT);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof UnknownHostException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHALBFailover() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "2");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_SLEEP_TIME.name(), "100");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ //Invalid URL at first position in lb_urls
+ StringBuilder sb = new StringBuilder("http://invalidurl:").append(START_PORT).append(",");
+
+ //Put Valid URL at second position in lb_urls. This should be returned during failover.
+ sb.append(urls[0]).append(",");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ AvaticaConnection connection = (AvaticaConnection) DriverManager.getConnection(url,
+ properties);
+ String uri = getConnectionURI(connection);
+ Assert.assertEquals(urls[0], uri);
+ }
+
+ @Test
+ public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception {
+ //Skip the test for Windows.
+ Assume.assumeFalse(OS_NAME_LOWERCASE.startsWith(WINDOWS_OS_PREFIX));
+ Properties properties = new Properties();
+
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0");
+ //240.0.0.1 is special URL which should result in connection timeout.
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://240.0.0.1:" + 9000);
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ long startTime = System.currentTimeMillis();
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ long endTime = System.currentTimeMillis();
+ long elapsedTime = endTime - startTime;
+ Assert.assertTrue(elapsedTime < Timeout.ofMinutes(3).toMilliseconds());
+ Assert.assertTrue(elapsedTime >= 5000);
+ Assert.assertTrue(re.getCause() instanceof ConnectTimeoutException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesCreateStatementAfterDisconnect() throws Exception {
+
+ //Start a new server at port 100 port from the startport
+ int test_server_port = START_PORT + 100;
+ HttpServer avaticaServer = Main.start(SERVER_ARGS, test_server_port,
+ AvaticaProtobufHandler::new);
+
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "2");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_SLEEP_TIME.name(), "100");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ StringBuilder sb = new StringBuilder();
+ //First URL will be server we started in this test
+ sb.append("http://localhost:").append(test_server_port).append(",");
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+
+ //Create a connection
+ String url = SERVERS.getJdbcUrl(test_server_port, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1156809244
##########
site/_docs/client_reference.md:
##########
@@ -193,3 +193,52 @@ on-hover images for the permalink, but oh well.
: _Default_: `false`.
: _Required_: No.
+
+<strong><a name="use_client_side_lb" href="#use_client_side_lb">use_client_side_lb</a></strong>
+
+: _Description_: Enables the client side load-balancing.
+: _Default_: `false`.
+
+: _Required_: No.
+
+<strong><a name="lb_urls" href="#lb_urls">lb_urls</a></strong>
+
+: _Description_: List of URLs in a comma separated format, for example "URL1,URL2...URLn", to be used by the client side
+load balancer. Depending on the load balancing strategy, load balancer selects one of the URLs from the list.
+
+: _Default_: `null`.
+
+: _Required_: No.
+
+<strong><a name="lb_strategy" href="#lb_strategy">lb_strategy</a></strong>
+
+: _Description_: The load balancing strategy to be used by the client side load balancer. It must be a fully qualified
+Java class name which implements `org.apache.calcite.avatica.ha.LBStrategy`. Out of the box we have provided two
+implementations `org.apache.calcite.avatica.ha.RandomSelectLBStrategy` and
+`org.apache.calcite.avatica.ha.RoundRobinLBStrategy` which you can use as load balancing strategies.
Review Comment:
Done. Thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1159771208
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ configToIndexServedMap.put(key, new Random().nextInt(urls.length));
Review Comment:
@stoty
If we shuffle the URLs it will degenerate to RandomSelectLBStrategy. Only difference will be Eager (ShuffledRoundRobin) vs Lazy randomization (RandomSelect)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1159771208
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ configToIndexServedMap.put(key, new Random().nextInt(urls.length));
Review Comment:
@stoty
If we shuffle the URLs it will degenerate to RandomSelectLBStrategy.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163656952
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
Review Comment:
Done. Made it more readably.
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
+ configToIndexServedMap.put(key, nextURlIdx);
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+ return url;
+ }
+
+ private void initialiseCaches(String key, String lbURLs) {
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1144664647
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -119,23 +119,25 @@ Service createService(AvaticaConnection connection, ConnectionConfig config) {
final Service service;
if (metaFactory != null) {
service = metaFactory.create(connection);
- } else if (config.url() != null) {
- final AvaticaHttpClient httpClient = getHttpClient(connection, config);
- final Serialization serializationType = getSerialization(config);
-
- LOG.debug("Instantiating {} service", serializationType);
- switch (serializationType) {
- case JSON:
- service = new RemoteService(httpClient);
- break;
- case PROTOBUF:
- service = new RemoteProtobufService(httpClient, new ProtobufTranslationImpl());
- break;
- default:
- throw new IllegalArgumentException("Unhandled serialization type: " + serializationType);
- }
} else {
- service = new MockJsonService(Collections.<String, String>emptyMap());
+ if (config.url() != null) {
Review Comment:
Thanks @stoty.
While doing checkstyle formatting, I had accidentally done this. But restored the previous "if - else if -else" statement
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1542101644
One more thing, @vaijosh .
Please change the commit message to match the Avatica convention.
i.e. use square brackets instead of colon.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: [CALCITE-5581] Implemented a basic client side load balancing
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1544019278
> One more thing, @vaijosh . Please change the commit message to match the Avatica convention. i.e. use square brackets instead of colon.
Done. Also squashed 2 commits into single.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: [CALCITE-5581] Implement Basic client side load balancing in Avatica Driver
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1550943145
merged manually
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1479408247
> Also use square brackets in the commit message, see the other PRs for formatting.
Done. Thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1153070204
##########
core/src/main/java/org/apache/calcite/avatica/remote/Driver.java:
##########
@@ -159,25 +168,43 @@ AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig c
return httpClientFactory.getClient(url, config, connection.getKerberosConnection());
}
-
@Override public Connection connect(String url, Properties info)
throws SQLException {
- AvaticaConnection conn = (AvaticaConnection) super.connect(url, info);
- if (conn == null) {
- // It's not an url for our driver
- return null;
+ ConnectionConfig config = new ConnectionConfigImpl(info);
+ int retries = 0;
+ int currentRetry = 0;
+ if (config.useClientSideLb()) {
+ retries = config.getLBConnectionFailoverRetries();
}
+ do {
+ long startTime = System.currentTimeMillis();
+ AvaticaConnection conn = (AvaticaConnection) super.connect(url, info);
+ if (conn == null) {
+ // It's not an url for our driver
+ return null;
+ }
- Service service = conn.getService();
-
- // super.connect(...) should be creating a service and setting it in the AvaticaConnection
- assert null != service;
-
- service.apply(
- new Service.OpenConnectionRequest(conn.id,
- Service.OpenConnectionRequest.serializeProperties(info)));
+ Service service = conn.getService();
- return conn;
+ // super.connect(...) should be creating a service and setting it in the AvaticaConnection
+ assert null != service;
+ try {
+ service.apply(
+ new Service.OpenConnectionRequest(conn.id,
+ Service.OpenConnectionRequest.serializeProperties(info)));
Review Comment:
Yes. I have already checked this. BuiltInConnectionProperty.isLocalProperty() decides which properties should not be serialized to the server.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1495746828
> Or an RR variant that shuffles the list on initialization ? I think that that would have the advantage of being deterministic, while avoiding the uneven load problem.
Interesting proposal. It's better than current RoundRobin.In fact I am tempted to modify RoundRobinLBStrategy and have this. For clients we can expect deterministic behavior ( i.e. which host will come next). But for servers, it's still an uneven load(Since clients are dependent on Randomiser).
I feel "client side load balancers" can't solve uneven load balancing unless they have integration with some external Discovery service.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1159715753
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ configToIndexServedMap.put(key, new Random().nextInt(urls.length));
Review Comment:
I think that we could getter behaviour if we shuffled the whole URL list, not just randomized the starting index.
IMO that would help with the clients synchronizing on the same host when there is a problem.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1521134592
>
Hi @joshelser
Did you get a chance to review this?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163575749
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ initialiseCaches(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+ int nextURlIdx = urlIndex < urls.length - 1 ? (urlIndex + 1) : 0;
+ configToIndexServedMap.put(key, nextURlIdx);
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+ return url;
+ }
+
+ private void initialiseCaches(String key, String lbURLs) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ List<String> list = Arrays.asList(urls);
+ Collections.shuffle(list);
+ list.toArray(urls);
Review Comment:
You are correct.
However, relying on that side effect hurts readability.
`list = Collections.shuffle(list);`
is obviously correct, and does the same thing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1163869030
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ configToIndexServedMap.put(key, 0);
+ initialiseUrlList(key, lbURLs);
+ }
+ String[] urls = configToUrlListMap.get(key);
+ int urlIndex = configToIndexServedMap.get(key);
+
+ String url = urls[urlIndex];
+ LOG.info("Selected URL:{}", url);
+
+ urlIndex++;
Review Comment:
Done. Thanks a lot @stoty
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1172435220
##########
server/src/test/java/org/apache/calcite/avatica/ha/ConnectionPropertiesHATest.java:
##########
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.BuiltInConnectionProperty;
+import org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImpl;
+import org.apache.calcite.avatica.remote.AvaticaHttpClient;
+import org.apache.calcite.avatica.remote.AvaticaServersForTest;
+import org.apache.calcite.avatica.remote.Driver;
+import org.apache.calcite.avatica.remote.RemoteProtobufService;
+import org.apache.calcite.avatica.server.AvaticaProtobufHandler;
+import org.apache.calcite.avatica.server.HttpServer;
+import org.apache.calcite.avatica.server.Main;
+
+import org.apache.hc.client5.http.ConnectTimeoutException;
+import org.apache.hc.client5.http.HttpHostConnectException;
+import org.apache.hc.core5.util.Timeout;
+
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.sql.DriverManager;
+import java.util.Locale;
+import java.util.Properties;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class ConnectionPropertiesHATest {
+ private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest();
+ private static final String[] SERVER_ARGS = {
+ AvaticaServersForTest.FullyRemoteJdbcMetaFactory.class.getName() };
+ public static final int NO_OF_SERVERS = 5;
+ public static final String HTTP_LOCALHOST = "http://localhost:";
+ public static final String COMMA = ",";
+ public static final String OS_NAME_LOWERCASE = System.getProperty("os.name")
+ .toLowerCase(Locale.ROOT);
+ public static final String WINDOWS_OS_PREFIX = "windows";
+ private static String lbURLs = "";
+ private static final int START_PORT = 10000;
+ private static String[] urls;
+
+ @BeforeClass
+ public static void beforeClass() throws ClassNotFoundException, InvocationTargetException,
+ InstantiationException, IllegalAccessException, NoSuchMethodException {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ int port = START_PORT + i;
+ Main.start(SERVER_ARGS, port, AvaticaProtobufHandler::new);
+ sb.append(HTTP_LOCALHOST).append(port).append(COMMA);
+ }
+ lbURLs = sb.substring(0, sb.length() - 1);
+ urls = lbURLs.split(COMMA);
+ }
+
+
+ @Test
+ public void connectionPropertiesNoHATest() throws Exception {
+ Properties properties = new Properties();
+ String url = SERVERS.getJdbcUrl(10000, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn1 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ Assert.assertNotNull(conn1);
+ }
+
+ @Test
+ public void connectionPropertiesHATestRandomSelectLB() {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RandomSelectLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ try {
+ getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ } catch (Exception e) {
+ Assert.fail(); //Verify that exception is not generated.
+ }
+ }
+ }
+
+ @Test
+ public void connectionPropertiesHATestRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String uri1 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[0], uri1);
+
+ String uri2 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[1], uri2);
+
+ String uri3 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[2], uri3);
+ }
+
+ @Test
+ public void connectionPropertiesHATestShuffledRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ ShuffledRoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String firstConnectiondURL = getConnectionURI((AvaticaConnection)
+ DriverManager.getConnection(url, properties));
+
+ Assert.assertNotNull(firstConnectiondURL);
+
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ try {
+ getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ } catch (Exception e) {
+ Assert.fail(); //In System test verify that exception is not generated.
+ }
+ }
+ }
+
+ @Test
+ public void connectionPropertiesHATestInvalidLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(), "com.incorrect.badLb");
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof ClassNotFoundException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestLongURlList() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+ String uri1 = getConnectionURI(conn);
+ Assert.assertEquals(urls[0], uri1);
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestInvalidLBUrl() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://invalid:" + START_PORT);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof UnknownHostException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHALBFailover() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "2");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_SLEEP_TIME.name(), "100");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ //Invalid URL at first position in lb_urls
+ StringBuilder sb = new StringBuilder("http://invalidurl:").append(START_PORT).append(",");
+
+ //Put Valid URL at second position in lb_urls. This should be returned during failover.
+ sb.append(urls[0]).append(",");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ AvaticaConnection connection = (AvaticaConnection) DriverManager.getConnection(url,
+ properties);
+ String uri = getConnectionURI(connection);
+ Assert.assertEquals(urls[0], uri);
+ }
+
+ @Test
+ public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception {
+ //Skip the test for Windows.
+ Assume.assumeFalse(OS_NAME_LOWERCASE.startsWith(WINDOWS_OS_PREFIX));
+ Properties properties = new Properties();
+
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0");
+ //240.0.0.1 is special URL which should result in connection timeout.
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), "http://240.0.0.1:" + 9000);
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ long startTime = System.currentTimeMillis();
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ long endTime = System.currentTimeMillis();
+ long elapsedTime = endTime - startTime;
+ Assert.assertTrue(elapsedTime < Timeout.ofMinutes(3).toMilliseconds());
+ Assert.assertTrue(elapsedTime >= 5000);
+ Assert.assertTrue(re.getCause() instanceof ConnectTimeoutException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesCreateStatementAfterDisconnect() throws Exception {
+
+ //Start a new server at port 100 port from the startport
+ int test_server_port = START_PORT + 100;
+ HttpServer avaticaServer = Main.start(SERVER_ARGS, test_server_port,
+ AvaticaProtobufHandler::new);
+
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "2");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_SLEEP_TIME.name(), "100");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ StringBuilder sb = new StringBuilder();
+ //First URL will be server we started in this test
+ sb.append("http://localhost:").append(test_server_port).append(",");
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+
+ //Create a connection
+ String url = SERVERS.getJdbcUrl(test_server_port, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+
Review Comment:
Better have a test here first here, to make sure that the connection works before we shut the server down.
(Just create a connection, and select anything)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: [CALCITE-5581] Implemented a basic client side load balancing
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1545309881
The commit message doesn't match the JIRA description
please use the JIRA description verbatim.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1153064814
##########
server/src/test/java/org/apache/calcite/avatica/ha/ConnectionPropertiesHATest.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.AvaticaConnection;
+import org.apache.calcite.avatica.BuiltInConnectionProperty;
+import org.apache.calcite.avatica.remote.AvaticaCommonsHttpClientImpl;
+import org.apache.calcite.avatica.remote.AvaticaHttpClient;
+import org.apache.calcite.avatica.remote.AvaticaServersForTest;
+import org.apache.calcite.avatica.remote.Driver;
+import org.apache.calcite.avatica.remote.RemoteProtobufService;
+import org.apache.calcite.avatica.server.AvaticaProtobufHandler;
+import org.apache.calcite.avatica.server.Main;
+
+import org.apache.hc.client5.http.ConnectTimeoutException;
+import org.apache.hc.core5.util.Timeout;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.sql.DriverManager;
+import java.util.Properties;
+
+public class ConnectionPropertiesHATest {
+ private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest();
+ private static final String[] SERVER_ARGS = {
+ AvaticaServersForTest.FullyRemoteJdbcMetaFactory.class.getName() };
+ public static final int NO_OF_SERVERS = 5;
+ public static final String HTTP_LOCALHOST = "http://localhost:";
+ public static final String COMMA = ",";
+ private static String lbURLs = "";
+ private static final int START_PORT = 10000;
+ private static String[] urls;
+
+ @BeforeClass
+ public static void beforeClass() throws ClassNotFoundException, InvocationTargetException,
+ InstantiationException, IllegalAccessException, NoSuchMethodException {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < NO_OF_SERVERS; i++) {
+ int port = START_PORT + i;
+ Main.start(SERVER_ARGS, port, AvaticaProtobufHandler::new);
+ sb.append(HTTP_LOCALHOST).append(port).append(COMMA);
+ }
+ lbURLs = sb.substring(0, sb.length() - 1);
+ urls = lbURLs.split(COMMA);
+ }
+
+
+ @Test
+ public void connectionPropertiesNoHATest() throws Exception {
+ Properties properties = new Properties();
+ String url = SERVERS.getJdbcUrl(10000, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn1 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ Assert.assertNotNull(conn1);
+ }
+
+ @Test
+ public void connectionPropertiesHATestRandomSelectLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ AvaticaConnection conn1 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ AvaticaConnection conn2 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+ AvaticaConnection conn3 =
+ (AvaticaConnection) DriverManager.getConnection(url, properties);
+
+ Assert.assertTrue(!getConnectionURI(conn1).equals(urls[0])
+ || !getConnectionURI(conn2).equals(urls[1])
+ || !getConnectionURI(conn3).equals(urls[2]));
+ }
+
+ @Test
+ public void connectionPropertiesHATestRoundRobinLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ String uri1 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[0], uri1);
+
+ String uri2 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[1], uri2);
+
+ String uri3 = getConnectionURI((AvaticaConnection) DriverManager.getConnection(url,
+ properties));
+ Assert.assertEquals(urls[2], uri3);
+
+ }
+
+ @Test
+ public void connectionPropertiesHATestInvalidLB() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbURLs);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(), "com.incorrect.badLb");
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof ClassNotFoundException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestLongURlList() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 1000; i++) {
+ sb.append("http://localhost:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+
+ AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url, properties);
+ String uri1 = getConnectionURI(conn);
+ Assert.assertEquals(urls[0], uri1);
+ }
+
+ @Test
+ public void testConnectionPropertiesHATestInvalidLBUrl() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 3; i++) {
+ sb.append("http://invalid:").append(START_PORT + i).append(",");
+ }
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), sb.substring(0, sb.length() - 1));
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof UnknownHostException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHALBFailover() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "3");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "300");
+ String lbUrls = "http://localhost:" + START_PORT;
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), lbUrls);
+ properties.put(BuiltInConnectionProperty.LB_STRATEGY.name(),
+ RoundRobinLBStrategy.class.getName());
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ Assert.assertTrue(re.getCause() instanceof UnknownHostException);
+ }
+ }
+
+ @Test
+ public void testConnectionPropertiesHAHttpConnectionTimeout5Sec() throws Exception {
+ Properties properties = new Properties();
+ properties.put(BuiltInConnectionProperty.USE_CLIENT_SIDE_LB.name(), "true");
+ properties.put(BuiltInConnectionProperty.HTTP_CONNECTION_TIMEOUT.name(), "5000");
+ properties.put(BuiltInConnectionProperty.LB_CONNECTION_FAILOVER_RETRIES.name(), "0");
+ String urls = "http://240.0.0.1:" + START_PORT;
+ properties.put(BuiltInConnectionProperty.LB_URLS.name(), urls);
+
+ String url = SERVERS.getJdbcUrl(START_PORT, Driver.Serialization.PROTOBUF);
+ long startTime = System.currentTimeMillis();
+ try {
+ DriverManager.getConnection(url, properties);
+ } catch (RuntimeException re) {
+ long endTime = System.currentTimeMillis();
+ long elapsedTime = endTime - startTime;
+ Assert.assertTrue(elapsedTime < Timeout.ofMinutes(3).toMicroseconds());
Review Comment:
Done. Changed it to milliseconds.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1153064169
##########
core/src/main/java/org/apache/calcite/avatica/remote/AvaticaCommonsHttpClientImpl.java:
##########
@@ -91,11 +96,19 @@ public AvaticaCommonsHttpClientImpl(URL url) {
this.uri = toURI(Objects.requireNonNull(url));
}
- protected void initializeClient(PoolingHttpClientConnectionManager pool) {
+ protected void initializeClient(PoolingHttpClientConnectionManager pool,
+ ConnectionConfig config) {
this.authCache = new BasicAuthCache();
// A single thread-safe HttpClient, pooling connections via the
// ConnectionManager
- this.client = HttpClients.custom().setConnectionManager(pool).build();
+ RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
+ if (config.useClientSideLb()) {
Review Comment:
Done.
##########
core/src/main/java/org/apache/calcite/avatica/remote/AvaticaCommonsHttpClientImpl.java:
##########
@@ -91,11 +96,19 @@ public AvaticaCommonsHttpClientImpl(URL url) {
this.uri = toURI(Objects.requireNonNull(url));
}
- protected void initializeClient(PoolingHttpClientConnectionManager pool) {
+ protected void initializeClient(PoolingHttpClientConnectionManager pool,
+ ConnectionConfig config) {
this.authCache = new BasicAuthCache();
// A single thread-safe HttpClient, pooling connections via the
// ConnectionManager
- this.client = HttpClients.custom().setConnectionManager(pool).build();
+ RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
+ if (config.useClientSideLb()) {
+ requestConfigBuilder.setConnectTimeout(config.getHttpConnectionTimeout(),
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1153062050
##########
core/src/main/java/org/apache/calcite/avatica/BuiltInConnectionProperty.java:
##########
@@ -91,7 +95,28 @@ public enum BuiltInConnectionProperty implements ConnectionProperty {
TRANSPARENT_RECONNECTION("transparent_reconnection", Type.BOOLEAN, Boolean.FALSE, false),
/** Number of rows to fetch per call. */
- FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false);
+ FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false),
+
+ /** Avatica connection HA property - use client side load balancing **/
+ USE_CLIENT_SIDE_LB("useClientSideLB", Type.BOOLEAN, Boolean.FALSE, false),
Review Comment:
Done.
##########
core/src/main/java/org/apache/calcite/avatica/ConnectionConfig.java:
##########
@@ -66,6 +67,21 @@ public interface ConnectionConfig {
boolean transparentReconnectionEnabled();
/** @see BuiltInConnectionProperty#FETCH_SIZE */
int fetchSize();
+
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1156769190
##########
site/_docs/client_reference.md:
##########
@@ -193,3 +193,52 @@ on-hover images for the permalink, but oh well.
: _Default_: `false`.
: _Required_: No.
+
+<strong><a name="use_client_side_lb" href="#use_client_side_lb">use_client_side_lb</a></strong>
+
+: _Description_: Enables the client side load-balancing.
+: _Default_: `false`.
+
+: _Required_: No.
+
+<strong><a name="lb_urls" href="#lb_urls">lb_urls</a></strong>
+
+: _Description_: List of URLs in a comma separated format, for example "URL1,URL2...URLn", to be used by the client side
+load balancer. Depending on the load balancing strategy, load balancer selects one of the URLs from the list.
+
+: _Default_: `null`.
+
+: _Required_: No.
+
+<strong><a name="lb_strategy" href="#lb_strategy">lb_strategy</a></strong>
+
+: _Description_: The load balancing strategy to be used by the client side load balancer. It must be a fully qualified
+Java class name which implements `org.apache.calcite.avatica.ha.LBStrategy`. Out of the box we have provided two
+implementations `org.apache.calcite.avatica.ha.RandomSelectLBStrategy` and
+`org.apache.calcite.avatica.ha.RoundRobinLBStrategy` which you can use as load balancing strategies.
Review Comment:
nit:
The load balancing strategy to be used by the client side load balancer. It must be a fully qualified
Java class name which implements `org.apache.calcite.avatica.ha.LBStrategy`.
Two implementations are provided: `org.apache.calcite.avatica.ha.RandomSelectLBStrategy` and
`org.apache.calcite.avatica.ha.RoundRobinLBStrategy` .
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1156795870
##########
site/_docs/client_reference.md:
##########
@@ -193,3 +193,52 @@ on-hover images for the permalink, but oh well.
: _Default_: `false`.
: _Required_: No.
+
+<strong><a name="use_client_side_lb" href="#use_client_side_lb">use_client_side_lb</a></strong>
+
+: _Description_: Enables the client side load-balancing.
+: _Default_: `false`.
+
+: _Required_: No.
+
+<strong><a name="lb_urls" href="#lb_urls">lb_urls</a></strong>
+
+: _Description_: List of URLs in a comma separated format, for example "URL1,URL2...URLn", to be used by the client side
+load balancer. Depending on the load balancing strategy, load balancer selects one of the URLs from the list.
+
+: _Default_: `null`.
+
+: _Required_: No.
+
+<strong><a name="lb_strategy" href="#lb_strategy">lb_strategy</a></strong>
+
+: _Description_: The load balancing strategy to be used by the client side load balancer. It must be a fully qualified
+Java class name which implements `org.apache.calcite.avatica.ha.LBStrategy`. Out of the box we have provided two
+implementations `org.apache.calcite.avatica.ha.RandomSelectLBStrategy` and
+`org.apache.calcite.avatica.ha.RoundRobinLBStrategy` which you can use as load balancing strategies.
Review Comment:
Yes. Here are my thoughts.
Knox is centralized (all client send requests to KNOX) mechanism where as we implemented distributed (all client independently do the LB). For centralized mechanism, RoundRobin makes more sense. But for distributed approach Random select makes more sense.
To give an example, if we have some clients C1, C2, C3 interacting with DB at the same time using DB URLs DB1,DB2,DB3. So if the clients are doing transactions concurrently it's likely that they can overload Perticular DB server ( e.g. all C1, C2, C3 pickup first DB URL and connect to i). With Random RandomSelect algorithm there is less probability that all clients connect to same DB and overload it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] vaijosh commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "vaijosh (via GitHub)" <gi...@apache.org>.
vaijosh commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1156795870
##########
site/_docs/client_reference.md:
##########
@@ -193,3 +193,52 @@ on-hover images for the permalink, but oh well.
: _Default_: `false`.
: _Required_: No.
+
+<strong><a name="use_client_side_lb" href="#use_client_side_lb">use_client_side_lb</a></strong>
+
+: _Description_: Enables the client side load-balancing.
+: _Default_: `false`.
+
+: _Required_: No.
+
+<strong><a name="lb_urls" href="#lb_urls">lb_urls</a></strong>
+
+: _Description_: List of URLs in a comma separated format, for example "URL1,URL2...URLn", to be used by the client side
+load balancer. Depending on the load balancing strategy, load balancer selects one of the URLs from the list.
+
+: _Default_: `null`.
+
+: _Required_: No.
+
+<strong><a name="lb_strategy" href="#lb_strategy">lb_strategy</a></strong>
+
+: _Description_: The load balancing strategy to be used by the client side load balancer. It must be a fully qualified
+Java class name which implements `org.apache.calcite.avatica.ha.LBStrategy`. Out of the box we have provided two
+implementations `org.apache.calcite.avatica.ha.RandomSelectLBStrategy` and
+`org.apache.calcite.avatica.ha.RoundRobinLBStrategy` which you can use as load balancing strategies.
Review Comment:
Yes this is intentional. Here are my thoughts.
Knox is centralized (all client send requests to KNOX) mechanism where as we implemented distributed (all client independently do the LB). For centralized mechanism, RoundRobin makes more sense. But for distributed approach Random select makes more sense.
To give an example, if we have some clients C1, C2, C3 interacting with DB at the same time using DB URLs DB1,DB2,DB3. So if the clients are doing transactions concurrently it's likely that they can overload Perticular DB server ( e.g. all C1, C2, C3 pickup first DB URL and connect to i). With Random RandomSelect algorithm there is less probability that all clients connect to same DB and overload it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1493674848
Functionally,this looks good.
Some remaining issues:
- As Julien mentioned, URLs including commas are problematic. I think that specifying the encoded comma %2C in the URLs should work. Can you test and conform this ?
- Need to add the new features to the docs
- Need a release note that summarizes the feature (in this JIRA)
- Invite another round of reviews by replying to your original email on the dev list.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1159869548
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ configToIndexServedMap.put(key, new Random().nextInt(urls.length));
Review Comment:
I disagree.
If we shuffle the list before storing it into the hash, then each VM locally will still behave deterministically, but each one will still behave differently.
for the URLs ABCD
JVM1:
RR on CBDA
JVM2:
RR on DCBA
JVM3:
RR on ADCB
and so on.
With true random strategy you will get something like ACCDBADD.... where you can re-try a failed host immediately.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on a diff in pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on code in PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#discussion_r1159713187
##########
core/src/main/java/org/apache/calcite/avatica/ha/RoundRobinLBStrategy.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Round Robin strategy for client side load balancing.
+ * Its implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class RoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(RoundRobinLBStrategy.class);
+
+ public static final RoundRobinLBStrategy INSTANCE = new RoundRobinLBStrategy();
+ private RoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
Review Comment:
nit: No need to declare separately.
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
+ String lbURLs = config.getLbURLs();
+ if (!configToIndexServedMap.containsKey(key)) {
+ String[] urls = lbURLs.split(URL_SEPERATOR_CHAR);
+ configToIndexServedMap.put(key, new Random().nextInt(urls.length));
Review Comment:
I think that we could getter behaviour if we shuffled the whole URL list, not just randomized the starting index.
IMO that would help with the client synchronizing on the same host when there is a problem.
##########
core/src/main/java/org/apache/calcite/avatica/ha/ShuffledRoundRobinLBStrategy.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.avatica.ha;
+
+import org.apache.calcite.avatica.ConnectionConfig;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Shuffled Round Robin strategy for client side load balancing.
+ * It starts with some random position in a list of URLs, and then returns subsequent URL
+ * in a RoundRobin manner.
+ * It's implemented it as a singleton so that we can maintain state
+ * i.e. which URL was last used from the list of URLs specified.
+ */
+public class ShuffledRoundRobinLBStrategy implements LBStrategy {
+ private static final Logger LOG = LoggerFactory.getLogger(ShuffledRoundRobinLBStrategy.class);
+
+ public static final ShuffledRoundRobinLBStrategy INSTANCE = new ShuffledRoundRobinLBStrategy();
+ private ShuffledRoundRobinLBStrategy() { }
+ public static final String URL_SEPERATOR_CHAR = ",";
+
+ Map<String, Integer> configToIndexServedMap = new HashMap<>();
+ Map<String, String[]> configToUrlListMap = new HashMap<>();
+
+ @Override
+ public String getLbURL(ConnectionConfig config) {
+ String key = getKey(config);
+ int urlIndex;
Review Comment:
Would it make sense to extend this from RRLBStrategy ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [calcite-avatica] stoty commented on pull request #217: CALCITE-5581: Client side LB implementation
Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #217:
URL: https://github.com/apache/calcite-avatica/pull/217#issuecomment-1479113918
Also use square brackets in the commit message, see the other PRs for formatting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org