public JaRedisPool(final String host) { URI uri = URI.create(host); if (JedisURIHelper.isValid(uri)) { String h = uri.getHost(); int port = uri.getPort(); String password = JedisURIHelper.getPassword(uri); int database = JedisURIHelper.getDBIndex(uri); this.internalPool = new GenericObjectPool<>(new JaRedisFactory(h, port, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, password, database, null), new GenericObjectPoolConfig()); } else { this.internalPool = new GenericObjectPool<>(new JaRedisFactory(host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null), new GenericObjectPoolConfig()); } }
public byte[][] getByteParams(byte[]... args) { ArrayList<byte[]> byteParams = new ArrayList<byte[]>(); for (byte[] arg : args) { byteParams.add(arg); } if (contains(WITHCOORD)) { byteParams.add(SafeEncoder.encode(WITHCOORD)); } if (contains(WITHDIST)) { byteParams.add(SafeEncoder.encode(WITHDIST)); } if (contains(COUNT)) { byteParams.add(SafeEncoder.encode(COUNT)); byteParams.add(Protocol.toByteArray((Integer) getParam(COUNT))); } if (contains(ASC)) { byteParams.add(SafeEncoder.encode(ASC)); } else if (contains(DESC)) { byteParams.add(SafeEncoder.encode(DESC)); } return byteParams.toArray(new byte[byteParams.size()][]); }
private static long measureInputMulti() throws Exception { long duration = 0; InputStream is = new ByteArrayInputStream( "*4\r\n$3\r\nfoo\r\n$13\r\nbarbarbarfooz\r\n$5\r\nHello\r\n$5\r\nWorld\r\n".getBytes()); RedisInputStream in = new RedisInputStream(is); for (int n = 0; n <= TOTAL_OPERATIONS; n++) { long start = System.nanoTime(); Protocol.read(in); duration += (System.nanoTime() - start); in.reset(); } return duration; }
@Test public void testMasterSlaveShardingConsistency() { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT)); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1)); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2)); Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards, Hashing.MURMUR_HASH); List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT)); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1)); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2)); Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards, Hashing.MURMUR_HASH); for (int i = 0; i < 1000; i++) { JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i)); JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i)); assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2)); } }
@Test public void testMasterSlaveShardingConsistencyWithShardNaming() { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234")); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234")); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234")); Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards, Hashing.MURMUR_HASH); List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234")); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234")); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234")); Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards, Hashing.MURMUR_HASH); for (int i = 0; i < 1000; i++) { JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i)); JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i)); assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName()); } }
@Test public void buildACommand() throws IOException { PipedInputStream pis = new PipedInputStream(); BufferedInputStream bis = new BufferedInputStream(pis); PipedOutputStream pos = new PipedOutputStream(pis); RedisOutputStream ros = new RedisOutputStream(pos); Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET)); ros.flush(); pos.close(); String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n"; int b; StringBuilder sb = new StringBuilder(); while ((b = bis.read()) != -1) { sb.append((char) b); } assertEquals(expectedCommand, sb.toString()); }
private void createJedisPool() { JedisPoolConfig config = new JedisPoolConfig(); // 设置最大连接数 config.setMaxTotal(500); // 设置最大阻塞时间(毫秒) config.setMaxWaitMillis(1000); // 设置空闲连接数 config.setMaxIdle(20); config.setMinIdle(10); // 创建连接池 pool = new JedisPool(config, appConfig.get(CONFIG_KEY_REDIS_HOST, DEFAULT_REDIS_HOST), appConfig.getInt(CONFIG_KEY_REDIS_PORT, DEFAULT_REDIS_PORT), Protocol.DEFAULT_TIMEOUT, appConfig.get(CONFIG_KEY_REDIS_PASS, "")); }
@Override protected EntityAccess createEntityAccess(PersistentEntity persistentEntity, Object obj, Map nativeEntry) { final NativeEntryModifyingEntityAccess ea = new NativeEntryModifyingEntityAccess(persistentEntity, obj) { public void setProperty(String name, Object value) { Class type = getPropertyType(name); if(type.isArray() && byte.class.isAssignableFrom(type.getComponentType()) && value instanceof CharSequence) { try { super.setProperty(name, value.toString().getBytes(Protocol.CHARSET)); } catch (UnsupportedEncodingException e) { // ignore } } else { super.setProperty(name, value); } } }; ea.setConversionService(getMappingContext().getConversionService()); ea.setNativeEntry(nativeEntry); return ea; }
private static JedisPool createPool(GenericObjectPoolConfig redisPoolConfig, String connection, int timeout) { URI redisConnection = URI.create(connection); String host = redisConnection.getHost(); int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort(); String path = redisConnection.getPath(); if (StringUtils.isEmpty(path)) { path = "/" + String.valueOf(Protocol.DEFAULT_DATABASE); } int database = Integer.parseInt(path.split("/", 2)[1]); String password = null; if (redisConnection.getUserInfo() != null) { password = redisConnection.getUserInfo().split(":", 2)[1]; } if (redisPoolConfig == null) { redisPoolConfig = new GenericObjectPoolConfig(); } return new JedisPool(redisPoolConfig, host, port, timeout, password, database, null); }
private boolean slaveOf(final long appId, final String masterHost, final int masterPort, final String slaveHost, final int slavePort) { final Jedis slave = redisCenter.getJedis(appId, slaveHost, slavePort, Protocol.DEFAULT_TIMEOUT * 3, Protocol.DEFAULT_TIMEOUT * 3); try { boolean isSlave = new IdempotentConfirmer() { @Override public boolean execute() { String result = slave.slaveof(masterHost, masterPort); return result != null && result.equalsIgnoreCase("OK"); } }.run(); if (!isSlave) { logger.error(String.format("modifyAppConfig:ip=%s,port=%s failed", slaveHost, slavePort)); return false; } redisCenter.configRewrite(appId, slaveHost, slavePort); } catch (Exception e) { logger.error(e.getMessage(), e); return false; } finally { if (slave != null) slave.close(); } return true; }
public JedisSentinelPoolExt(String masterName, String sentinels, final GenericObjectPoolConfig poolConfig, final String password, int connectionTimeout, int failMax, boolean masterWriteOnly, String clientName) { String[] strings = sentinels.split(","); Collections.addAll(_sentinels, strings); this.poolConfig = poolConfig; this.connectionTimeout = connectionTimeout; if (!StringUtils.isBlank(password)) { this.password = password; } this.database = Protocol.DEFAULT_DATABASE; this.failMax = failMax; this.masterName = masterName; this.masterWriteOnly = masterWriteOnly; if (!StringUtils.isBlank(clientName)) { this.clientName = clientName; } HostAndPort master = initSentinels(_sentinels, masterName); initPool(master); initReadPool(); }
/** * 读池增加新节点 * * @param hostAndPort */ protected synchronized void insertReadPool(HostAndPort hostAndPort) { boolean exists = false; for (JedisPoolExt oldJedisPool : jedisReadPools) { if (oldJedisPool.getHostAndPort().equals(hostAndPort)) { exists = true; break; } } if (!exists) { JedisPoolExt newJedisPool = new JedisPoolExt(poolConfig, hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout, password, Protocol.DEFAULT_DATABASE, clientName); jedisReadPools.add(newJedisPool); // 新来的节点,可能是之前挂掉后恢复的节点,所以要检查其是否存在于失败列表中,要去除 failMap.remove(hostAndPort); log.info("Add JedisReadPool at " + hostAndPort); } }
@Override public Long getNoIncr(String key) { final Jedis resource = this.getResource(); try { final byte[] rawKey = this.serializeKey(key); final byte[] rawValue = resource.get(rawKey); if (ArrayUtils.isEmpty(rawValue)) { return null; } return Long.valueOf(new String(rawValue, Protocol.CHARSET)); } catch (Exception e) { throw new CacheException("redis:get", e); } finally { this.returnResource(resource); } }
@Override public Long getNoIncr(String key) { try { final byte[] rawKey = this.serializeKey(key); final byte[] rawValue = this.jedisCluster.get(rawKey); if (ArrayUtils.isEmpty(rawValue)) { return null; } return Long.valueOf(new String(rawValue, Protocol.CHARSET)); } catch (Exception e) { throw new CacheException("redis:get", e); } }
@Override public void setAttribute(String name, Object value) { super.setAttribute(name, value); if (value == null || name.startsWith("javax.zkoss.zk.ui.Session") || !_manager.isStarted()) { return; } try { byte[] bytesValue = KryoSerializer.write(value); if (_manager.debug) { log.info("id=" + this.id + ",name=" + name + ",strValue=" + new String(bytesValue, Protocol.CHARSET)); } _manager.jedisHset(RedisManager.TOMCAT_SESSION_PREFIX + this.id, name, bytesValue); } catch (Exception ex) { log.error("error:name=" + name + ";value=" + value, ex); } }
@Before public void setUpRedis() throws IOException, SchedulerConfigException { port = getPort(); logger.debug("Attempting to start embedded Redis server on port " + port); redisServer = RedisServer.builder() .port(port) .build(); redisServer.start(); final short database = 1; JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setTestOnBorrow(true); jedisPool = new JedisPool(jedisPoolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, database); jobStore = new RedisJobStore(); jobStore.setHost(host); jobStore.setLockTimeout(2000); jobStore.setPort(port); jobStore.setInstanceId("testJobStore1"); jobStore.setDatabase(database); mockScheduleSignaler = mock(SchedulerSignaler.class); jobStore.initialize(null, mockScheduleSignaler); schema = new RedisJobStoreSchema(); jedis = jedisPool.getResource(); jedis.flushDB(); }
@Override public int getShardIdByKey(String key) { // 1. Get bytes byte[] vinBytes; try { vinBytes = key.getBytes(Protocol.CHARSET); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage()); } // 2. Get VIN hash MurmurHash murmurHash = new MurmurHash(); long vinHash = murmurHash.hash(vinBytes); // 3. Get shard id from tail map of shardsMap by vinHash SortedMap<Long, Integer> tailMap = shardsMap.tailMap(vinHash); if (tailMap.isEmpty()) { return shardsMap.get(shardsMap.firstKey()); } return tailMap.get(tailMap.firstKey()); }
@Override public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) throws SchedulerConfigException { this.loadHelper = loadHelper; this.signaler = signaler; // initializing a connection pool JedisPoolConfig config = new JedisPoolConfig(); if (password != null) pool = new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT, password); else pool = new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT); // initializing a locking connection pool with a longer timeout if (lockTimeout == 0) lockTimeout = 10 * 60 * 1000; // 10 Minutes locking timeout lockPool = new JedisLock(pool.getResource(), "JobLock", lockTimeout); }
@Override public void init(final NamedList args) { final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(getInt(args, MAX_CONNECTIONS_FIELD, DEFAULT_MAX_CONNECTIONS)); final String host = getString(args, HOST_FIELD, HostAndPort.LOCALHOST_STR); final int timeout = getInt(args, TIMEOUT_FIELD, Protocol.DEFAULT_TIMEOUT); final String password = getString(args, PASSWORD_FIELD, null); final int database = getInt(args, DATABASE_FIELD, Protocol.DEFAULT_DATABASE); final int retries = getInt(args, RETRIES_FIELD, DEFAULT_RETRIES); final String[] hostAndPort = host.split(":"); final JedisPool jedisConnectionPool = createPool(poolConfig, hostAndPort[0], hostAndPort.length == 2 ? Integer.parseInt(hostAndPort[1]) : Protocol.DEFAULT_PORT, timeout, password, database); connectionHandler = createCommandHandler(jedisConnectionPool, retries); log.info("Initialized RedisQParserPlugin with host: " + host); }
public void start(JedisPoolConfig config) { LOG.info("JedisServiceImpl starting"); String[] hosts = conf.getGroupIds(); for (String h : hosts) { if ("".equals(h)) continue; try { String ip = conf.getConf(h, "ip"); // if ("127.0.0.1".equals(ip)) { // continue; // } int port = conf.getConfInt(h, "port", "6379"); int timeout = conf.getConfInt(h, "timeout", "2000"); if (port == 0) { _jedis.put(h, new JedisPool(config, ip, Protocol.DEFAULT_PORT, timeout)); } else { _jedis.put(h, new JedisPool(config, ip, port, timeout)); } } catch (Exception e) { LOG.error(e.getMessage()); continue; } } LOG.info("JedisServiceImpl start successfully"); }
/** * Creates a new {@link JedisPool}. * * @param hostAndPort * @param password * @param db * @param timeoutMs * @return */ public static JedisPool newJedisPool(String hostAndPort, String password, int db, long timeoutMs) { final int maxTotal = Runtime.getRuntime().availableProcessors(); final int maxIdle = maxTotal / 2; JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(maxTotal); poolConfig.setMinIdle(1); poolConfig.setMaxIdle(maxIdle > 0 ? maxIdle : 1); poolConfig.setMaxWaitMillis(timeoutMs + 1000); // poolConfig.setTestOnBorrow(true); poolConfig.setTestWhileIdle(true); String[] tokens = hostAndPort.split(":"); String host = tokens.length > 0 ? tokens[0] : Protocol.DEFAULT_HOST; int port = tokens.length > 1 ? Integer.parseInt(tokens[1]) : Protocol.DEFAULT_PORT; JedisPool jedisPool = new JedisPool(poolConfig, host, port, (int) timeoutMs, password, db); return jedisPool; }
/** * Creates a new {@link JedisPool}. * * @param hostAndPort * @param password * @param db * @param timeoutMs * @return * @since 0.5.0 */ public static JedisPool newJedisPool(String hostAndPort, String password, int db, long timeoutMs) { final int maxTotal = Runtime.getRuntime().availableProcessors(); final int maxIdle = maxTotal / 2; JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(maxTotal); poolConfig.setMinIdle(1); poolConfig.setMaxIdle(maxIdle > 0 ? maxIdle : 1); poolConfig.setMaxWaitMillis(timeoutMs); // poolConfig.setTestOnBorrow(true); poolConfig.setTestWhileIdle(true); String[] tokens = hostAndPort.split(":"); String host = tokens.length > 0 ? tokens[0] : Protocol.DEFAULT_HOST; int port = tokens.length > 1 ? Integer.parseInt(tokens[1]) : Protocol.DEFAULT_PORT; JedisPool jedisPool = new JedisPool(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, password, db); return jedisPool; }
public void init() throws DBException { Properties props = getProperties(); int port; String portString = props.getProperty(PORT_PROPERTY); if (portString != null) { port = Integer.parseInt(portString); } else { port = Protocol.DEFAULT_PORT; } String host = props.getProperty(HOST_PROPERTY); jedis = new Jedis(host, port); jedis.connect(); String password = props.getProperty(PASSWORD_PROPERTY); if (password != null) { jedis.auth(password); } }
private RateLimiter buildRateLimiter(double permitsPerSecond) { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(30); JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379, Protocol.DEFAULT_TIMEOUT); RDBI rdbi = new RDBI(jedisPool); // Verify our loading of the LUA script upon initial start. rdbi.withHandle(new Callback<Void>() { @Override public Void run(Handle handle) { handle.jedis().scriptFlush(); return null; } }); return new StrictRateLimiter("d:test:rdbi", rdbi, UUID.randomUUID().toString(), permitsPerSecond); }
private RDBI getRdbi() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(30); JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379, Protocol.DEFAULT_TIMEOUT); RDBI rdbi = new RDBI(jedisPool); // Verify our loading of the LUA script upon initial start. rdbi.withHandle(new Callback<Void>() { @Override public Void run(Handle handle) { handle.jedis().scriptFlush(); return null; } }); return rdbi; }
@Override public GelfSender create(GelfSenderConfiguration configuration) throws IOException { String graylogHost = configuration.getHost(); URI hostUri = URI.create(graylogHost); int port = hostUri.getPort(); if (port <= 0) { port = configuration.getPort(); } if (port <= 0) { port = Protocol.DEFAULT_PORT; } if (hostUri.getFragment() == null || hostUri.getFragment().trim().equals("")) { throw new IllegalArgumentException("Redis URI must specify fragment"); } if (hostUri.getHost() == null) { throw new IllegalArgumentException("Redis URI must specify host"); } Pool<Jedis> pool = RedisSenderPoolProvider.getJedisPool(hostUri, port); return new GelfREDISSender(pool, hostUri.getFragment(), configuration.getErrorReporter()); }
/** * Create a new {@link ShardedJedisPool}. * * @param poolConfig * format {@code host1:port1,host2:port2,...}, default Redis port is used if not * specified * @param password * @param timeoutMs * @return */ public static ShardedJedisPool newShardedJedisPool(JedisPoolConfig poolConfig, String hostsAndPorts, String password, int timeoutMs) { List<JedisShardInfo> shards = new ArrayList<>(); String[] hapList = hostsAndPorts.split("[,;\\s]+"); for (String hostAndPort : hapList) { String[] tokens = hostAndPort.split(":"); String host = tokens.length > 0 ? tokens[0] : Protocol.DEFAULT_HOST; int port = tokens.length > 1 ? Integer.parseInt(tokens[1]) : Protocol.DEFAULT_PORT; JedisShardInfo shardInfo = new JedisShardInfo(host, port, timeoutMs); shardInfo.setPassword(password); shards.add(shardInfo); } ShardedJedisPool jedisPool = new ShardedJedisPool(poolConfig, shards); return jedisPool; }
public RedisPool(final String host) { URI uri = URI.create(host); if (uri.getScheme() != null && uri.getScheme().equals("redis")) { String h = uri.getHost(); int port = uri.getPort(); String password = JedisURIHelper.getPassword(uri); int database = 0; Integer dbIndex = JedisURIHelper.getDBIndex(uri); if (dbIndex != null) { database = dbIndex.intValue(); } this.internalPool = new GenericObjectPool<BinaryJedis>( new BinaryJedisFactory(h, port, Protocol.DEFAULT_TIMEOUT, password, database, null), new GenericObjectPoolConfig()); } else { this.internalPool = new GenericObjectPool<BinaryJedis>(new BinaryJedisFactory( host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null), new GenericObjectPoolConfig()); } }
/** * Verify that setting a timeout in the configuration does not impact the database setting * * @throws EventDeliveryException */ @Test public void timeoutConfiguredTest() throws EventDeliveryException { String host = "localhost"; int timeout = 10; JedisPoolFactory mockJedisPoolFactory = mock(JedisPoolFactory.class); RedisSource redisSource = new RedisSource(mockJedisPoolFactory); Context context = new Context(); context.put(RedisSourceConfigurationConstant.HOST, host); context.put(RedisSourceConfigurationConstant.BATCH_SIZE, "10"); context.put(RedisSourceConfigurationConstant.TIMEOUT, Integer.toString(timeout)); redisSource.configure(context); redisSource.doStart(); verify(mockJedisPoolFactory).create(any(JedisPoolConfig.class), eq(host), eq(Protocol.DEFAULT_PORT), eq(timeout), isNull(String.class), eq(Protocol.DEFAULT_DATABASE)); }