shell 例程 —— 解决redis读取稳定性
问题背景: php读取线上redis数据,常常不稳定,数据响应时有时无。
解决方法:多次读取。每次读取全部上一次没读出的数据,直到全部获取。
本文实现用shell进行多次redis数据读取, 每次取出当中的有效值(对于我们的样例中,就是给key,能在redis上取得其value的为有效值。其它无效),并将无效值重跑一遍,以此迭代,直到全部redis数据被取出。PS:redis数据能够由php或C读出,给定接口的话很easy,详细能够參考phpredis。。因为可能涉密,本文中不给出这块的实现。
方法概述:
- 将source中的key分为N个文件。丢入redis并行get value
- 实时统计N个文件get value输出结果的总行数,以及当中有效的行数
- N个文件统计结束后, 将其全部结果合并,放入result/step/stepx.res
- 删除原先并行的的source文件,结果文件
- 将result中未获取到的key放入source/step2/contsigns。作为下一轮的输入。再次将其分为N个文件,运行(这里的contsign就是redis的key)
- 最后将各个step所得结果都写入final.res文件。cat result/steps/step*.res >> final.res
项目结构:
- getredis.php: 实现获取redis数据
- all.sh: 主程序,并行运行getredis.php;
- analyze_result.sh: 实时分析redis获取数据运行情况(第2步), 加參数后实现上面的第3-5步(详细见下一节凝视);
- source/:存储输入数据,当中all/下为原始全部redis的输入。 step2/为这一轮中未获取到的key,将作为下一轮获取redis的输入, 其余(如xaa)为当前这一轮中key分成的N个文件;
- result/: 存储结果。当中source/包括当前一轮source下全部N个文件的输出;steps/包括各轮输出后的合并结果
详细实现:
all.sh :
#Author:Rachel Zhang
#Email: zrqjennifer@sina.com
for file in source/*
do
{
echo "processing source $file"
if test -f $file
then
php getredis.php $file > result/$file
fi
echo "$file done..."
}&
done
analyze_result.sh:
#Author:Rachel Zhang
#Email: zrqjennifer@sina.com
Filefolder=result/source/*
#Filefolder=source/*
echo "##################################"
echo " In Folder $Filefolder"
echo "##################################"
nl=0
hv=0
for file in $Filefolder
do
if test -f $file
then
fline=`wc -l $file | awk '{print $1}'`
nl=$(($nl+$fline))
fvalue=`cat $file |awk 'BEGIN{x=0;}{if($2) x=x+1;}END{print x;}'`
hv=$(($hv+$fvalue))
fi
done
echo "totally $nl lines"
echo "$hv lines have tag value"
##################################
#combine results into one file
if [ "$#" -gt 0 ]
then
if test -e "result/all.result"
then
mv result/all.result result/all.result.bak
fi
for file in $Filefolder
do
if test -f $file
then
cat $file >> result/all.result
fi
done
echo "all the output are write in result/all.result"
# put null-value keys into source/step2/contsigns
if test -e source/step2
then
mkdir source/step2
fi
cat result/all.result| awk '{if($2==null) print}' > source/step2/contsigns
Nnull_value=`wc -l source/step2/contsigns | awk '{print $1}'`
echo "remaining ${Nnull_value} keys to be processed"
# put has-value key-value into result/steps/step${step_id}.res
step_id=1
while test -e result/steps/step${step_id}.res
do
step_id=$(($step_id+1))
done
cat result/all.result | awk '{if($2) print}' > result/steps/step${step_id}.res
echo "current valid results are writen in result/steps/step${step_id}.res"
# remove the current source files, generate new source files and re-run all.sh
echo "remove current source and result files?
(y/n)"
read answer
if [ $answer == "y" ]; then
if [ $Nnull_value -gt 10 ]; then
rm source/*
rm result/source/*
cd source && split -l 5000 step2/contsigns && cd ../
echo "now re-run sh ./all.sh?
(y/n)"
read answer
if [ $answer == "y" ]; then
sh all.sh
fi
fi
fi
fi
PS: 以上analyze_result.sh 还能够去掉analyze_result.sh中的interactive部分(无需用户输入)。通过crontab进行定时,进一步自己主动化运行。
shell 例程 —— 解决redis读取稳定性的更多相关文章
- 解决shiro多次从redis读取session的问题
/** * 重写sessonManager * 解决shiro多次从redis读取session的问题 */ public class CustomSessionManager extends Def ...
- APP-FND-00676: 弹性域例程 FDFGDC 无法读取为此说明性弹性域指定的默认引用字段
路径: AR: 设置- 财务系统 - 弹性域- 说明性 -注册 手工增加: RECEIPT_METHOD_ID 路径: AR: 设置- 财务系统 - 弹性域- 说明性 -段 路径:收款 - 收款 点 ...
- 解决Redis/Codis Connection with master lost(复制超时)问题
今天在线上环境中遇到了codis-server报警,按照常规处理流程进行处理,报错步骤如下: 首先将codis-slave的rdb文件移除,并重启codis-slave 在codis-dashbord ...
- 解决 python 读取文件乱码问题(UnicodeDecodeError)
解决 python 读取文件乱码问题(UnicodeDecodeError) 确定你的文件的编码,下面的代码将以'utf-8'为例,否则会忽略编码错误导致输出乱码 解决方案一 with open(r' ...
- lua入门demo(HelloWorld+redis读取)
1. lua入门demo 1.1. 入门之Hello World!! 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上 openresty是nginx+lua的各种模 ...
- 解决 Redis 只读不可写的问题
本文转载:https://blog.csdn.net/han_cui/article/details/54767208?tdsourcetag=s_pcqq_aiomsg 解决 Redis 只读不可写 ...
- 解决Redis之MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist o...
解决Redis之MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist o... ...
- 解决redis远程连接不上的问题
解决redis远程连接不上的问题 redis现在的版本开启redis-server后,redis-cli只能访问到127.0.0.1,因为在配置文件中固定了ip,因此需要修改redis.conf(有的 ...
- 关于解决java读取excel文件遇空行抛空指针的问题 !
关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...
随机推荐
- 调度kettle使用taskctl我该怎么部署
转载自: http://www.taskctl.com/forum/detail_133.html 最近在QQ群看到有小伙伴在问用taskctl调度kettle,都要安装些什么呢?都支持哪些平台上的k ...
- Codeforces_768_D_(概率dp)
D. Jon and Orbs time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Windows提高_1.4进程通信
进程通信 使用 WM_COPYDATA 客户端(发送端) // 1. 找到窗口程序 HWND hWnd = FindWindow(NULL, L"Window1"); // 2 ...
- 梦想CAD控件网页版扩展数据
随着基于CAD的应用软件飞速发展,经常需要保存一些与图形可视性无关的数据,即非图形参数.例如在绘制化验样图中包含品位数据.MxCAD定义一类新的参数——实体扩展数据.扩展数据与实体的可视性无关,而是用 ...
- vuex状态管理demo
vuex状态管理主要包含四个概念 mapState,mapMutations,mapGetters,mapActions. 编写vuex文件夹下面的store.js import Vue from ...
- 小程序接口越过域名和https限制方法
都知道小程序上线接口需要域名,还需要https,就算是体验版的都是需要的,这样就筛选掉一大批开发者,像我这样只有学生轻量级服务器的学生要开发自己的小程序就很为难,但今天确惊奇的在小程序社区里面找到了用 ...
- [Algorithm] 9. Two Sum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- 集合:Iterator
why ? when ? how ? what ? 为什么需要集合呢? 在数据结构中链表.树.堆等一些操作都是由我们自己写的,这些操作是不是可以提取出来,以后要用就直接拿来用就好,这样非常方便. Ja ...
- linux性能优化cpu-02平均负载
每次我们系统变慢时,我们通常做的第一件事就是top命令或者uptime命令,看一下系统的负载情况,比如下面: 我在命令行中输入uptime 22:15:51 表示当前系统时间 up 13 min ...
- Redis多实例配置以及主从同步
一.多实例配置 1.准备俩配置文件,开两个就准备两个 redis-6380.conf redis-6381.conf 2.分别写入配置信息(这里简化了配置) # 运行在6380端口 bind 172. ...