FrankChen021 commented on a change in pull request #10240: URL: https://github.com/apache/druid/pull/10240#discussion_r469652894
########## File path: extensions-contrib/redis-cache/src/main/java/org/apache/druid/client/cache/RedisCacheFactory.java ########## @@ -0,0 +1,113 @@ +/* + * 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.druid.client.cache; + +import org.apache.commons.lang.StringUtils; +import org.apache.druid.java.util.common.IAE; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +public class RedisCacheFactory +{ + public static Cache create(final RedisCacheConfig config) + { + if (config.getCluster() != null && StringUtils.isNotBlank(config.getCluster().getNodes())) { + + Set<HostAndPort> nodes = Arrays.stream(config.getCluster().getNodes().split(",")) + .map(String::trim) + .filter(StringUtils::isNotBlank) + .map(host -> { + int index = host.indexOf(':'); + if (index <= 0 || index == host.length()) { + throw new IAE("Invalid redis cluster configuration: %s", host); + } + + int port; + try { + port = Integer.parseInt(host.substring(index + 1)); + } + catch (NumberFormatException e) { + throw new IAE("Invalid redis cluster configuration: invalid port %s", host); + } + if (port <= 0 || port > 65535) { + throw new IAE("Invalid redis cluster configuration: invalid port %s", host); + } + + return new HostAndPort(host.substring(0, index), port); + }).collect(Collectors.toSet()); + + JedisPoolConfig poolConfig = new JedisPoolConfig(); + poolConfig.setMaxTotal(config.getMaxTotalConnections()); + poolConfig.setMaxIdle(config.getMaxIdleConnections()); + poolConfig.setMinIdle(config.getMinIdleConnections()); + + JedisCluster cluster; + if (StringUtils.isNotBlank(config.getPassword())) { + cluster = new JedisCluster( + nodes, + config.getTimeout().getMillisecondsAsInt(), + config.getTimeout().getMillisecondsAsInt(), Review comment: Yes, connection timeout and read timeout are different. But 1. the internal implementation of Jedis takes `timeout` for both connection timeout and read timeout. 2. the widely used Spring Redis also provides only one configurable timeout parameter for both connection timeout and read timeout. So, currently I think it's acceptable to use one properties for both two parameters. I will update the doc to clarify that `timeout` property is for both connecting and reading. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
