首頁 > 軟體

使用Jedis面臨的非執行緒安全問題詳解

2022-12-23 14:01:31

網上都說jedis範例是非執行緒安全的,常常通過JedisPool連線池去管理範例,在多執行緒情況下讓每個執行緒有自己獨立的jedis範例,但都沒有具體說明為啥jedis範例時非執行緒安全的,下面詳細看一下非執行緒安全主要從哪個角度來看。

1. jedis類圖

2. 為什麼jedis不是執行緒安全的

由上述類圖可知,Jedis類中有RedisInputStream和RedisOutputStream兩個屬性,而傳送命令和獲取返回值都是使用這兩個成員變數,顯然,這很容易引發多執行緒問題。測試程式碼如

public class BadConcurrentJedisTest {
 
    private static final ExecutorService pool = Executors.newFixedThreadPool(20);
 
    private static final Jedis jedis = new Jedis("192.168.58.99", 6379);
 
 
    public static void main(String[] args) {
        for(int i=0;i<20;i++){
            pool.execute(new RedisSet());
        }
    }
 
    static class RedisSet implements Runnable{
 
        @Override
        public void run() {
            while(true){
                jedis.set("hello", "world");
            }
        }
 
    }

報錯:

Exception in thread "pool-1-thread-4" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Socket Closed
    at redis.clients.jedis.Connection.connect(Connection.java:164)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
    at redis.clients.jedis.BinaryClient.set(BinaryClient.java:97)
    at redis.clients.jedis.Client.set(Client.java:32)
    at redis.clients.jedis.Jedis.set(Jedis.java:68)
    at threadsafe.BadConcurrentJedisTest$RedisSet.run(BadConcurrentJedisTest.java:26)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Socket Closed
    at java.net.AbstractPlainSocketImpl.setOption(AbstractPlainSocketImpl.java:212)
    at java.net.Socket.setKeepAlive(Socket.java:1310)
    at redis.clients.jedis.Connection.connect(Connection.java:149)
    ... 9 more
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Socket Closed
    at redis.clients.jedis.Connection.connect(Connection.java:164)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
    at redis.clients.jedis.BinaryClient.set(BinaryClient.java:97)
    at redis.clients.jedis.Client.set(Client.java:32)
    at redis.clients.jedis.Jedis.set(Jedis.java:68)
    at threadsafe.BadConcurrentJedisTest$RedisSet.run(BadConcurrentJedisTest.java:26)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
.......

主要錯誤:

ava.net.SocketException: Socket closed
java.net.SocketException: Socket is not connected  

2.1 共用socket引起的異常

為什麼會出現這2個錯誤呢? 我們可以很容易的通過堆疊資訊定位到redis.clients.jedis.Connection的connect方法

public void connect() {
    if (!isConnected()) {
      try {
        socket = new Socket();
        // ->@wjw_add
        socket.setReuseAddress(true);
        socket.setKeepAlive(true); // Will monitor the TCP connection is
        // valid
        socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
        // ensure timely delivery of data
        socket.setSoLinger(true, 0); // Control calls close () method,
        // the underlying socket is closed
        // immediately
        // <-@wjw_add
 
        socket.connect(new InetSocketAddress(host, port), connectionTimeout);
        socket.setSoTimeout(soTimeout);
 
        if (ssl) {
          if (null == sslSocketFactory) {
            sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
          }
          socket = (SSLSocket) sslSocketFactory.createSocket(socket, host, port, true);
          if (null != sslParameters) {
            ((SSLSocket) socket).setSSLParameters(sslParameters);
          }
          if ((null != hostnameVerifier) &&
              (!hostnameVerifier.verify(host, ((SSLSocket) socket).getSession()))) {
            String message = String.format(
                "The connection to '%s' failed ssl/tls hostname verification.", host);
            throw new JedisConnectionException(message);
          }
        }
 
        outputStream = new RedisOutputStream(socket.getOutputStream());
        inputStream = new RedisInputStream(socket.getInputStream());
      } catch (IOException ex) {
        broken = true;
        throw new JedisConnectionException(ex);
      }
    }
  }

 jedis在執行每一個命令之前都會先執行connect方法,socket是一個共用變數,在多執行緒的情況下可能存在:執行緒1執行到了

outputStream = new RedisOutputStream(socket.getOutputStream());
inputStream = new RedisInputStream(socket.getInputStream());  

執行緒2執行到了:

socket = new Socket();
//執行緒2
socket.connect(new InetSocketAddress(host, port), connectionTimeout);  

因為執行緒2重新初始化了socket但是還沒有執行connect,所以執行緒1執行socket.getOutputStream()或者socket.getInputStream()就會丟擲java.net.SocketException: Socket is not connected。java.net.SocketException: Socket closed是因為socket異常導致共用變數socket關閉了引起的。

2.2 共用資料流引起的異常

上面是因為多個執行緒共用jedis引起的socket異常。除了socket連線引起的異常之外,還有共用資料流引起的異常。下面就看一下,因為共用jedis範例引起的共用資料流錯誤問題。

為了避免多執行緒連線的時候引起的錯誤,我們在初始化的時候就先執行一下connect操作:

public class BadConcurrentJedisTest1 {
 
    private static final ExecutorService pool = Executors.newCachedThreadPool();
 
    private static final Jedis jedis = new Jedis("192.168.58.99", 6379);
 
    static{
        jedis.connect();
    }
 
    public static void main(String[] args) {
        for(int i=0;i<20;i++){
            pool.execute(new RedisTest());
        }
 
    }
 
    static class RedisTest implements Runnable{
 
        @Override
        public void run() {
            while(true){
                jedis.set("hello", "world");
            }
        }
 
    }
 
} 

報錯:(每次報的錯可能不完全一樣)

Exception in thread "pool-1-thread-7" Exception in thread "pool-1-thread-1" redis.clients.jedis.exceptions.JedisDataException: ERR Protocol error: invalid multibulk length
    at redis.clients.jedis.Protocol.processError(Protocol.java:123)
    at redis.clients.jedis.Protocol.process(Protocol.java:157)
    at redis.clients.jedis.Protocol.read(Protocol.java:211)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:196)
    at redis.clients.jedis.Jedis.set(Jedis.java:69)
    at threadsafe.BadConcurrentJedisTest1$RedisTest.run(BadConcurrentJedisTest1.java:30)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Exception in thread "pool-1-thread-4" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
    at redis.clients.jedis.Connection.flush(Connection.java:291)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:194)
    at redis.clients.jedis.Jedis.set(Jedis.java:69)
    at threadsafe.BadConcurrentJedisTest1$RedisTest.run(BadConcurrentJedisTest1.java:30)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Broken pipe (Write failed)

Protocol error: invalid multibulk lengt是因為多執行緒通過RedisInputStream和RedisOutputStream讀寫緩衝區的時候引起的問題造成的資料問題不滿足RESP協定引起的。舉個簡單的例子,例如多個執行緒執行命令,執行緒1執行 set hello world命令。本來應該傳送:

*3rn$3rnSETrn$5rnhellorn$5rnworldrn

但是執行緒執行寫到

*3rn$3rnSETrn$5rnhellorn

然後被掛起了,執行緒2執行了寫操作寫入了' ',然後執行緒1繼續執行,最後傳送到redis伺服器端的資料可能就是:

*3rn$3rnSETrn$5rnhellorn' '$5rnworldrn

至於java.net.SocketException: Connection reset或ReadTimeout錯誤,是因為redis伺服器接受到錯誤的命令,執行了socket.close這樣的操作,關閉了連線。伺服器會返回復位標誌"RST",但是使用者端還在繼續執行讀寫資料操作。

3、jedis多執行緒操作

jedis本身不是多執行緒安全的,這並不是jedis的bug,而是jedis的設計與redis本身就是單執行緒相關,jedis範例抽象的是傳送命令相關,一個jedis範例使用一個執行緒與使用100個執行緒去傳送命令沒有本質上的區別,所以沒必要設定為執行緒安全的。但是如果需要用多執行緒方式存取redis伺服器怎麼做呢?那就使用多個jedis範例,每個執行緒對應一個jedis範例,而不是一個jedis範例多個執行緒共用。一個jedis關聯一個Client,相當於一個使用者端,Client繼承了Connection,Connection維護了Socket連線,對於Socket這種昂貴的連線,一般都會做池化,jedis提供了JedisPool。

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
 
public class JedisPoolTest {
 
    private static final ExecutorService pool = Executors.newCachedThreadPool();
 
    private static final CountDownLatch latch = new CountDownLatch(20);
 
    private static final JedisPool jPool = new JedisPool("192.168.58.99", 6379);
 
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for(int i=0;i<20;i++){
            pool.execute(new RedisTest());
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis() - start);
        pool.shutdownNow();
 
    }
 
    static class RedisTest implements Runnable{
 
        @Override
        public void run() {
            Jedis jedis = jPool.getResource();
            int i = 1000;
            try{
                while(i-->0){
                        jedis.set("hello", "world");
                }
            }finally{
                jedis.close();
                latch.countDown();
            }
        }
 
    }
 
}

到此這篇關於使用Jedis面臨的非執行緒安全問題詳解的文章就介紹到這了,更多相關Jedis非執行緒安全問題內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com