JedisCluster中应用的Apache Commons Pool对象池技术

- Jedis jedis = jedisPool.getResource();
- try {
- while (true) {
- String productCountString = jedis.get("product");
- if (Integer.parseInt(productCountString) > 0) {
- if (acquireLock(jedis, "abc")) {
- int productCount = Integer.parseInt(jedis.get("product"));
- System.out.println(String.format("%tT --- Get product: %s", new Date(), productCount));
- // System.out.println(productCount);
- jedis.decr("product");
- releaseLock(jedis, "abc");
- return "Success";
- }
- Thread.sleep(1000L);
- } else {
- return "Over";
- }
- }
- } finally {
- jedis.close();
- }

- @Override
- public String set(final byte[] key, final byte[] value) {
- return new JedisClusterCommand<String>(connectionHandler, maxRedirections) {
- @Override
- public String execute(Jedis connection) {
- return connection.set(key, value);
- }
- }.runBinary(key);
- }
- @Override
- public Jedis getConnection() {
- // In antirez's redis-rb-cluster implementation,
- // getRandomConnection always return valid connection (able to
- // ping-pong)
- // or exception if all connections are invalid
- List<JedisPool> pools = getShuffledNodesPool();
- for (JedisPool pool : pools) {
- Jedis jedis = null;
- try {
- jedis = pool.getResource();
- if (jedis == null) {
- continue;
- }
- String result = jedis.ping();
- if (result.equalsIgnoreCase("pong")) return jedis;
- pool.returnBrokenResource(jedis);
- } catch (JedisConnectionException ex) {
- if (jedis != null) {
- pool.returnBrokenResource(jedis);
- }
- }
- }
- throw new JedisConnectionException("no reachable node in cluster");
- }
- return runWithRetries(SafeEncoder.encode(keys[0]), this.redirections, false, false);
- @Override
- public Jedis getConnectionFromSlot(int slot) {
- JedisPool connectionPool = cache.getSlotPool(slot);
- if (connectionPool != null) {
- // It can't guaranteed to get valid connection because of node
- // assignment
- return connectionPool.getResource();
- } else {
- return getConnection();
- }
- }
- 5974ed7dd81c112d9a2354a0a985995913b4702c 192.168.1.137:6389 master - 0 1468809898374 26 connected 0-5640
- d08dc883ee4fcb90c4bb47992ee03e6474398324 192.168.1.137:6390 master - 0 1468809898875 25 connected 5641-11040
- ffb4db4e1ced0f91ea66cd2335f7e4eadc29fd56 192.168.1.138:6390 slave 5974ed7dd81c112d9a2354a0a985995913b4702c 0 1468809899376 26 connected
- c69b521a30336caf8bce078047cf9bb5f37363ee 192.168.1.137:6388 master - 0 1468809897873 28 connected 11041-16383
- 532e58842d001f8097fadc325bdb5541b788a360 192.168.1.138:6389 slave c69b521a30336caf8bce078047cf9bb5f37363ee 0 1468809899876 28 connected
- aa52c7810e499d042e94e0aa4bc28c57a1da74e3 192.168.1.138:6388 myself,slave d08dc883ee4fcb90c4bb47992ee03e6474398324 0 0 19 connected
- private T runWithRetries(byte[] key, int redirections, boolean tryRandomNode, boolean asking) {
- if (redirections <= 0) {
- throw new JedisClusterMaxRedirectionsException("Too many Cluster redirections?");
- }
- Jedis connection = null;
- try {
- if (asking) {
- // TODO: Pipeline asking with the original command to make it
- // faster....
- connection = askConnection.get();
- connection.asking();
- // if asking success, reset asking flag
- asking = false;
- } else {
- if (tryRandomNode) {
- connection = connectionHandler.getConnection();
- } else {
- connection = connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
- }
- }
- return execute(connection);
- } catch (JedisConnectionException jce) {
- if (tryRandomNode) {
- // maybe all connection is down
- throw jce;
- }
- // release current connection before recursion
- releaseConnection(connection);
- connection = null;
- // retry with random connection
- return runWithRetries(key, redirections - 1, true, asking);
- } catch (JedisRedirectionException jre) {
- // if MOVED redirection occurred,
- if (jre instanceof JedisMovedDataException) {
- // it rebuilds cluster's slot cache
- // recommended by Redis cluster specification
- this.connectionHandler.renewSlotCache(connection);
- }
- // release current connection before recursion or renewing
- releaseConnection(connection);
- connection = null;
- if (jre instanceof JedisAskDataException) {
- asking = true;
- askConnection.set(this.connectionHandler.getConnectionFromNode(jre.getTargetNode()));
- } else if (jre instanceof JedisMovedDataException) {
- } else {
- throw new JedisClusterException(jre);
- }
- return runWithRetries(key, redirections - 1, false, asking);
- } finally {
- releaseConnection(connection);
- }
- }
- - 2016-06-04 00:02:51,911 [// - - ] ERROR xxx - Too many Cluster redirections?
- redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?
- at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:97)
- at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:131)
- at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:152)
- at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:131)
- redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream.
- at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:198)
- at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40)
- at redis.clients.jedis.Protocol.process(Protocol.java:141)
- at redis.clients.jedis.Protocol.read(Protocol.java:205)
- at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
- at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:216)
- at redis.clients.jedis.Connection.getBulkReply(Connection.java:205)
- at redis.clients.jedis.Jedis.get(Jedis.java:101)
- at redis.clients.jedis.JedisCluster$3.execute(JedisCluster.java:79)
- at redis.clients.jedis.JedisCluster$3.execute(JedisCluster.java:76)
- at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:119)
- at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:30)
- at redis.clients.jedis.JedisCluster.get(JedisCluster.java:81)
- at redis.RedisClusterTest.main(RedisClusterTest.java:30)
- jce = {redis.clients.jedis.exceptions.JedisConnectionException@1014} "redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream."
- detailMessage = "Unexpected end of stream."
- cause = {redis.clients.jedis.exceptions.JedisConnectionException@1014} "redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream."
- stackTrace = {java.lang.StackTraceElement[0]@1017}
- suppressedExceptions = {java.util.Collections$UnmodifiableRandomAccessList@1018} size = 0
- 1) "client-output-buffer-limit"
- 2) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
- jre = {redis.clients.jedis.exceptions.JedisMovedDataException@2008} "redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 8855 192.168.1.137:6390"
- targetNode = {redis.clients.jedis.HostAndPort@2015} "192.168.1.137:6390"
- slot = 8855
- detailMessage = "MOVED 8855 192.168.1.137:6390"
- cause = {redis.clients.jedis.exceptions.JedisMovedDataException@2008} "redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 8855 192.168.1.137:6390"
- stackTrace = {java.lang.StackTraceElement[0]@1978}
- suppressedExceptions = {java.util.Collections$UnmodifiableRandomAccessList@1979} size = 0
- 4851:S 18 Jul 11:05:38.005 * Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without waiting for fsync to complete, this may slow down Redis.
JedisCluster中应用的Apache Commons Pool对象池技术的更多相关文章
- 对象池化技术 org.apache.commons.pool
恰当地使用对象池化技术,可以有效地减少对象生成和初始化时的消耗,提高系统的运行效率.Jakarta Commons Pool组件提供了一整套用于实现对象池化的框架,以及若干种各具特色的对象池实现,可以 ...
- Apache Commons Pool 故事一则
Apache Commons Pool 故事一则 最近工作中遇到一个由于对commons-pool的使用不当而引发的问题,习得正确的使用姿势后,写下这个简单的故事,帮助理解Apache Commons ...
- 池化 - Apache Commons Pool
对于那些创建耗时较长,或者资源占用较多的对象,比如网络连接,线程之类的资源,通常使用池化来管理这些对象,从而达到提高性能的目的.比如数据库连接池(c3p0, dbcp), java的线程池 Execu ...
- apache commons pool
apache commons下的pool 其中的borrowObject函数源代码显示其产生可用对象的过程: 如果stack中有空闲的对象,则pop对象,激活对象(activate函数),验证对象(v ...
- Apache Commons Pool 故事一则 专题
Apache Commons Pool 故事一则 最近工作中遇到一个由于对commons-pool的使用不当而引发的问题,习得正确的使用姿势后,写下这个简单的故事,帮助理解Apache Commons ...
- Tomcat 开发web项目报Illegal access: this web application instance has been stopped already. Could not load [org.apache.commons.pool.impl.CursorableLinkedList$Cursor]. 错误
开发Java web项目,在tomcat运行后报如下错误: Illegal access: this web application instance has been stopped already ...
- NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/pool/impl ...
- Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
错误如下: -- ::,-[TS] INFO http-- org.springframework.beans.factory.support.DefaultListableBeanFactory - ...
- org/apache/commons/pool/impl/GenericObjectPool异常的解决办法
org/apache/commons/pool/impl/GenericObjectPool异常的解决办法 webwork+spring+hibernate框架的集成, 一启动Tomcat服务器就出了 ...
随机推荐
- 2019.1.11 EDVT
Processing Gain and Occupied Bandwidth ESA Basic Setup (11b)Span 110MHzRBW 100kHzVBW 100kHzSweep Tim ...
- koa 框架 介绍 -- 待续
对比 express 更小 更健壮 解决繁琐的回调函数嵌套, 并极大地提升错误处理的效率 Koa 的核心设计思路是为中间件层 提供高级语法糖封装, (其实就是用了 ES6的生成器, 能中断函数的执 ...
- Linux上安装编译工具链
在Linux上安装编译工具链,安装它会依赖dpkg-dev,g++,libc6-dev,make等,所以安装之后这些依赖的工具也都会被安装.ubuntu软件库中这么描述 Informational l ...
- Linux:SSH服务配置文件详解
SSH服务配置文件详解 SSH客户端配置文件 /etc/ssh/ssh——config 配置文件概要 Host * #选项“Host”只对能够匹配后面字串的计算机有效.“*”表示所有的计算机. For ...
- python3:cmd运行python脚本,提示 No module named 'xxx'
问题:cmd窗口运行python脚本,报错 C:\Users\xxx\Documents\GitHub\python3\main>python run_test.pyTraceback (mos ...
- C++基础之 成员变量初 始化赋值
摘要: C++成员变量 初始化赋值 你都清楚吗?还有好多坑,好多细节也不知道... 今天在写一个类的时候,由于该类的一个成员变量是引用,所以初始化出现了问题,查了一下之后,才发现,原来引用的初始化和c ...
- 51Nod 1058: N的阶乘的长度(斯特林公式)
1058 N的阶乘的长度 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. Inp ...
- hdu5230
bc41第三题: 由 1 - n-1 这 n-1 个数组成 l - c 到 r - c 闭区间内的数共有多少种组合方法: 据称本来应该也比较简单吧,xiaoxin说了个五边形数,然后纷纷找了五边形数的 ...
- java泛型学习(1)
java泛型(Generices Type) --->概念:泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和 ...
- LG1801 【黑匣子_NOI导刊2010提高(06)】
看到各路dalao用平衡树的做法,表示本人不才,并不会. 然而我会优先队列_huaji_,并且发现用堆解题的dalao们并没有基于在线的做法 于是我的showtime到了 评测结果:https://w ...