In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is to share with you about the common client-side exceptions in the use of Jedis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The connection cannot be obtained from the connection pool
The number of Jedis objects in JedisPool is limited, and the default is 8. In the default configuration assumed here, if 8 Jedis objects are occupied and have not been returned, if the caller still needs to borrow Jedis from JedisPool, it needs to wait (for example, set maxWaitMillis > 0). If the Jedis object is still not available within the maxWaitMillis time, the following exception will be thrown.
Redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool... Caused by: java.util.NoSuchElementException: Timeout waiting for idle object at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject (GenericObjectPool.java:449)
In another case, if blockWhenExhausted=false is set, when the caller finds that there are no resources in the pool, the caller will immediately throw an exception without waiting. The following exception is the effect of blockWhenExhausted=false.
Redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool... Caused by: java.util.NoSuchElementException: Pool exhausted at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject (GenericObjectPool.java:464)
For this problem, we need to focus on why the connection pool has no resources, and there are many possible reasons for the lack of resources.
1. Client: under high concurrency, the connection pool setting is too small, so the supply exceeds the demand, so the above error will occur, but under normal circumstances, it only needs to be a little more than the default maximum number of connections (8), because the processing efficiency of JedisPool and Jedis is high enough.
two。 Client: connection pooling is not used correctly, such as not releasing, as shown in the following code: define JedisPool, using the default connection pooling configuration.
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig (); JedisPool jedisPool = new JedisPool (poolConfig, "127.0.0.1", 6379); / / borrows the connection from JedisPool 8 times, but does not perform the return operation. For (int I = 0; I
< 8; i++) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.ping(); } catch (Exception e) { e.printStackTrace(); }} 当调用者再向连接池借用Jedis时(如下操作),就会抛出异常: jedisPool.getResource().ping(); 3.客户端:存在慢查询操作,这些慢查询持有的Jedis对象归还速度会比较慢,造成池子满了。 4.服务端:客户端是正常的,但是Redis服务端由于一些原因造成了客户端命令执行过程的阻塞,也会使得客户端抛出这种异常。可以看到造成这个异常的原因是多个方面的,不要被异常的表象所迷惑,而且并不存在万能钥匙能解决所有问题,开发和运维只能不断加强对于Redis的理解,顺藤摸瓜逐渐找到问题所在。 二、 客户端读写超时 Jedis在调用Redis时,如果出现了读写超时后,会出现下面的异常: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out 造成该异常的原因也有以下几种: 读写超时设置的过短。 命令本身就比较慢。 客户端与服务端网络不正常。 Redis自身发生阻塞。 三、客户端连接超时 Jedis在调用Redis时,如果出现了读写超时后,会出现下面的异常: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out 造成该异常的原因也有以下几种: 连接超时设置的过短。 Redis发生阻塞,造成tcp-backlog已满,造成新的连接失败。 客户端与服务端网络不正常。 四、客户端缓冲区异常 Jedis在调用Redis时,如果出现客户端数据流异常,会出现下面的异常。 redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream. 造成这个异常原因可能有如下几种: 1.输出缓冲区满。例如将普通客户端的输出缓冲区设置为1M 1M 60: config set client-output-buffer-limit "normal 1048576 1048576 60 slave 268435456 67108864 60 pubsub 33554432 8388608 60" 如果使用get命令获取一个bigkey(例如3M),就会出现这个异常。 2.长时间闲置连接被服务端主动断开,可以查询timeout配置的设置以及自身连接池配置是否需要做空闲检测。 3.不正常并发读写:Jedis对象同时被多个线程并发操作,可能会出现上述异常。 五、Lua脚本正在执行 如果Redis当前正在执行Lua脚本,并且超过了lua-time-limit,此时Jedis调用Redis时,会收到下面的异常。对于如何处理这类问题(Lua lua-time-limit配置之前章节已经介绍了) redis.clients.jedis.exceptions.JedisDataException: BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE. 六、Redis正在加载持久化文件 Jedis调用Redis时,如果Redis正在加载持久化文件,那么会收到下面的异常。 redis.clients.jedis.exceptions.JedisDataException: LOADING Redis is loading the dataset in memory 七、Redis使用的内存超过maxmemory配置 Jedis调用Redis执行写操作时,如果Redis的使用内存大于maxmemory的设置,会收到下面的异常,此时应该调整maxmemory并找到造成内存增长的原因(maxmemory之前章节已经介绍了) redis.clients.jedis.exceptions.JedisDataException: OOM command not allowed when used memory >'maxmemory'.
The number of client connections is too large
If the number of client connections exceeds maxclients, the newly requested connection will have the following exception:
Redis.clients.jedis.exceptions.JedisDataException: ERR max number of clients reached
At this point, the new client connection executes any command, and the return result is as follows:
127.0.0.1 ERR max number of clients reached 6379 > get hello (error)
This problem can be tricky because you can't execute the Redis command at this time, and you can generally start in two ways.
1. Client: if the maxclients parameter is not very small, the number of client connections on the application side will not exceed maxclients, which is usually due to the improper use of Redis clients by the application. At this time, if the application side is a distributed structure, you can use some offline application nodes (for example, nodes that occupy more connections) to reduce the number of Redis connections first. So that most of the nodes can run normally, and then fix the problem by finding the program bug or adjusting the maxclients.
two。 Server: if the client cannot handle it at this time, and the current Redis is in high availability mode (such as Redis Sentinel and Redis Cluster), you can consider failing over the current Redis.
There is no definite solution to this problem, but no matter from which aspect to deal with, the rapid recovery of the fault is extremely important, of course, it is more important to find the location of the problem, otherwise the number of client connections will still exceed maxclients after a period of time.
Important attributes of GenericObjectPoolConfig as a gift
Thank you for reading! This is the end of this article on "what are the common client-side anomalies in the use of Jedis?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.