public static String getNodeKey(HostAndPort hnp) { return hnp.getHost() + ":" + hnp.getPort(); }
public void setNodeIfNotExist(HostAndPort node) { w.lock(); try { String nodeKey = getNodeKey(node); if (nodes.containsKey(nodeKey)) return; JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), connectionTimeout, soTimeout, null, 0, null); nodes.put(nodeKey, nodePool); } finally { w.unlock(); } }
public HostAndPort getHostAndPortFromNodeLine(String[] nodeInfoPartArray, HostAndPort current) { String stringHostAndPort = nodeInfoPartArray[HOST_AND_PORT_INDEX]; String[] arrayHostAndPort = stringHostAndPort.split(":"); return new HostAndPort(arrayHostAndPort[0].isEmpty() ? current.getHost() : arrayHostAndPort[0], arrayHostAndPort[1].isEmpty() ? current.getPort() : Integer.valueOf(arrayHostAndPort[1])); }
@Override public boolean validateObject(PooledObject<Jedis> pooledJedis) { final BinaryJedis jedis = pooledJedis.getObject(); try { HostAndPort hostAndPort = this.hostAndPort.get(); String connectionHost = jedis.getClient().getHost(); int connectionPort = jedis.getClient().getPort(); return hostAndPort.getHost().equals(connectionHost) && hostAndPort.getPort() == connectionPort && jedis.isConnected() && jedis.ping().equals("PONG"); } catch (final Exception e) { return false; } } }
private void initPool(HostAndPort master) { if (!master.equals(currentHostMaster)) { currentHostMaster = master; if (factory == null) { factory = new JedisFactory(master.getHost(), master.getPort(), connectionTimeout, soTimeout, password, database, clientName); initPool(poolConfig, factory); } else { factory.setHostAndPort(currentHostMaster); // although we clear the pool, we still have to check the // returned object // in getResource, this call only clears idle instances, not // borrowed instances internalPool.clear(); } log.info("Created JedisPool to master at " + master); } }
private void initializeSlotsCache(Set<HostAndPort> startNodes, GenericObjectPoolConfig poolConfig) { for (HostAndPort hostAndPort : startNodes) { Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort()); try { cache.discoverClusterNodesAndSlots(jedis); break; } catch (JedisConnectionException e) { // try next nodes } finally { if (jedis != null) { jedis.close(); } } } for (HostAndPort node : startNodes) { cache.setNodeIfNotExist(node); } }
return new ClusterRedirectException(re.getSlot(), re.getTargetNode().getHost(), re.getTargetNode().getPort(), ex);
@Override public PooledObject<Jedis> makeObject() throws Exception { final HostAndPort hostAndPort = this.hostAndPort.get(); final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout, soTimeout); try { jedis.connect(); if (null != this.password) { jedis.auth(this.password); } if (database != 0) { jedis.select(database); } if (clientName != null) { jedis.clientSetname(clientName); } } catch (JedisException je) { jedis.close(); throw je; } return new DefaultPooledObject<Jedis>(jedis); }
public JedisShardInfo(HostAndPort hp) { this(hp.getHost(), hp.getPort()); }
public static String getNodeKey(HostAndPort hnp) { return hnp.getHost() + ":" + hnp.getPort(); }
public BinaryJedis(final HostAndPort hp) { this(hp.getHost(), hp.getPort()); }
public JedisPool setupNodeIfNotExist(HostAndPort node) { w.lock(); try { String nodeKey = getNodeKey(node); JedisPool existingPool = nodes.get(nodeKey); if (existingPool != null) return existingPool; JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), connectionTimeout, soTimeout, password, 0, clientName, false, null, null, null); nodes.put(nodeKey, nodePool); return nodePool; } finally { w.unlock(); } }
private List<JedisShardInfo> makeShardInfoList(List<HostAndPort> masters) { List<JedisShardInfo> shardMasters = new ArrayList<JedisShardInfo>(); for (HostAndPort master : masters) { JedisShardInfo jedisShardInfo = new JedisShardInfo(master.getHost(), master.getPort(), timeout); jedisShardInfo.setPassword(password); shardMasters.add(jedisShardInfo); } return shardMasters; }
protected void initSentinelLiseners() { if(currentHostMasterSlaves != null){ logger.info("Starting Sentinel listeners..."); for (String sentinel : sentinels) { try { final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":"))); ShardedMasterSlaveListener shardedMasterSlaveListener = new ShardedMasterSlaveListener(masterNames, hap.getHost(), hap.getPort()); shardedMasterSlaveListeners.add(shardedMasterSlaveListener); shardedMasterSlaveListener.start(); } catch(Exception e) { logger.error("Starting Sentinel listeners caught a exception: " + e.getMessage(), e); } } } }
protected JedisShardInfo toJedisShardInfo(HostAndPort hostAndPort, String masterName) { JedisShardInfo shard = new JedisShardInfo(hostAndPort.getHost(), hostAndPort.getPort(), timeout, masterName); shard.setPassword(password); return shard; }
protected JedisShardInfo toJedisShardInfo(HostAndPort hostAndPort, String name) { JedisShardInfo shard = new JedisShardInfo(hostAndPort.getHost(), hostAndPort.getPort(), timeout, name); shard.setPassword(password); return shard; }
@Override public boolean validateObject(PooledObject<Jedis> p) { BinaryJedis jedis = (BinaryJedis) p.getObject(); try { HostAndPort hostAndPort = (HostAndPort) this.hostAndPort.get(); String connectionHost = jedis.getClient().getHost(); int connectionPort = jedis.getClient().getPort(); return ((hostAndPort.getHost().equals(connectionHost)) && (hostAndPort.getPort() == connectionPort) && (jedis.isConnected()) && (jedis.ping().equals("PONG"))); } catch (Exception e) { } return false; }
@Override public boolean validateObject(PooledObject<BinaryJedis> pooledJedis) { final BinaryJedis jedis = pooledJedis.getObject(); try { HostAndPort hostAndPort = this.hostAndPort.get(); String connectionHost = jedis.getClient().getHost(); int connectionPort = jedis.getClient().getPort(); return hostAndPort.getHost().equals(connectionHost) && hostAndPort.getPort() == connectionPort && jedis.isConnected() && jedis.ping().equals("PONG"); } catch (final Exception e) { return false; } } }
/** * Initialize the internal pool with connection info and pool config. */ protected void initInternalPool(HostAndPort address, ConnectionInfo connectionInfo, JedisPoolConfig config) { this.poolName = poolName; this.address = address; this.connectionInfo = connectionInfo; JedisFactory factory = new JedisFactory(address.getHost(), address.getPort(), connectionInfo.getTimeout(), connectionInfo.getPassword(), connectionInfo.getDatabase()); internalPool = new GenericObjectPool(factory, config); }