当前位置: 首页 > news >正文

jedis+redis pipeline诡异的链接损坏、数据读取异常问题解决

文章目录

    • 问题现象
      • 栈溢出(不断的重连)
      • 读取超时
      • 未知响应
      • 尝试读取损坏的链接
      • 读取到的数据和自己要读的无关,导致空指针、类型转换错误,数据读取错乱
    • 问题写法
    • 问题分析
    • 修复
    • 注意点

问题现象

栈溢出(不断的重连)

		at redis.clients.jedis.Connection.sendCommand(Connection.java:163)at redis.clients.jedis.Connection.sendCommand(Connection.java:154)at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:815)at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:145)at redis.clients.jedis.Connection.sendCommand(Connection.java:163)at redis.clients.jedis.Connection.sendCommand(Connection.java:154)at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:815)at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:145)

在这里插入图片描述

读取超时

在这里插入图片描述

未知响应

在这里插入图片描述

尝试读取损坏的链接

在这里插入图片描述

读取到的数据和自己要读的无关,导致空指针、类型转换错误,数据读取错乱

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

问题写法

对Redis操作封装了一个Redis类
在这里插入图片描述
同事对pipeline加了这个方法
在这里插入图片描述
使用的时候

   public Map<String, AnswerModel> batchGetAnswersPipeline(List<String> answerIdList) {Map<String, AnswerModel> resultMap = new HashMap<>();try (Pipeline pipelined = redis.pipelined()) {List<Response<Map<String, String>>> responses = new ArrayList<>();for (String answerId : answerIdList) {String key = String.format(MOMENT_ANSWER_INFO, answerId);responses.add(pipelined.hgetAll(key));}pipelined.sync();for (Response<Map<String, String>> response : responses) {Map<String, String> map = response.get();AnswerModel answers = BeanUtil.mapToBean(map, AnswerModel.class);if (answers != null){resultMap.put(answers.getAnswerId(), answers);}}} catch (Exception e) {log.error("执行batchGetAnswersPipeline发生异常, redis pipeline error", e);throw new CatVillageException();}return resultMap;}

问题分析

在redis这个工具类中调pipelined时获取一个Pinelined对象,而获取这个方法里用了try with ,with了一个jedis链接,当try结束时链接会被归还jedispool连接池。而返回的这个pipeline仍然在用这个链接,当其他线程去拿链接的时候可能拿到的正好是这个链接,导致多个线程共用一个链接,一个线程在执行pipeline多条命令,另一个线程也在用这个链接。而jedis底层执行命令的时候是使用的OutputStream流式去执行命令,使用二进制流读取结果。当出现这种线程不安全问题的时候,读取写入就会有问题。

修复

删除Redis工具类中的pipelined方法,使用getJedis方法获取链接对象,再获取管道Pineline对象,自行close管道,最后执行完释放管道,链接。

public Map<String, QuestionModel> batchGetQuestionPipeline(List<String> questionIdList) {if (CollectionUtils.isEmpty(questionIdList)){return new HashMap<>();}Map<String, QuestionModel> resultMap = new HashMap<>();try (Jedis jedis = redis.getJedis()) {Pipeline pipelined = jedis.pipelined();List<Response<Map<String, String>>> responses = new ArrayList<>();for (String questionId : questionIdList) {String key = String.format(MOMENT_QUESTION_INFO, questionId);responses.add(pipelined.hgetAll(key));}pipelined.sync();for (Response<Map<String, String>> response : responses) {Map<String, String> map = response.get();QuestionModel question = convertFromMap(map);if (question != null) {resultMap.put(question.getPostId(), question);}}pipelined.close();} catch (Exception e) {log.error("执行batchGetQuestionPipeline发生异常, redis pipeline error", e);throw new CatVillageException();}return resultMap;}

注意点

若jedis在3.7版本以下的版本也会有管道二进制读取异常问题,请升级到3.7.0+版本

http://www.xdnf.cn/news/468631.html

相关文章:

  • psycopg_pool.PoolTimeout: couldn‘t get a connection after 120.00 sec异常
  • 《软件测试架构实践与精准测试》| 合乎发展的灰度管理
  • springboot+vue实现在线书店(图书商城)系统
  • CertiK荣获以太坊基金会两项资助,领跑zkEVM形式化验证
  • SGLang、Ollama、vLLM和LLaMA.cpp推理框架的对比及选型建议
  • Java集合详解:HashMap
  • cnn卷积神经网络
  • 关于词向量的思考
  • mvc-service引入
  • 数据结构中链表的含义与link
  • uniapp-vue3项目中引入高德地图的天气展示
  • QMK键盘固件旋钮编码器(Encoder)配置详解(实操部分)
  • 盒带自编教材《软件工程》目录
  • 火山RTC 8 SDK集成进项目中
  • TLV格式
  • linux备份与同步工具rsync
  • [数据结构]8. 树-Tree
  • ES8.x基础知识学习提纲
  • 阿里开源通义万相Wan2.1-VACE-14B:用于视频创建和编辑的一体化模型
  • 【hot100-动态规划-139.单词拆分】
  • 第九讲 | 模板进阶
  • 每周靶点:TIGIT、ICAM1及文献分享
  • 2025ICPC陕西省赛题解一
  • 开机自启动python程序_ubuntu22.04
  • 图片爬虫通过模板及使用说明
  • 轻量级Web画板Paint Board如何本地部署与随时随地在线绘画分享
  • 开启智能未来:DeepSeek赋能行业变革之路
  • 软件测试之测试计划主要包涵哪些内容?
  • 什么是Agentic AI(代理型人工智能)?
  • [特殊字符]川翔云电脑:重新定义云端算力新纪元