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; ...
随机推荐
- JProfiler 9.1.1部署及使用
软件准备: 官网下载地址:http://www.ej-technologies.com/download/jprofiler/files 软件部署: windows安装双击即可. 注册号: L-Lar ...
- C++11:using 的各种作用
C++11中using关键字的主要作用是:为一个模板库定义一个别名. 文章链接:派生类中使用using别名改变基类成员的访问权限 一.<Effective Modern C++>里有比较 ...
- Robot Framework(九) 执行测试用例——基本用法
3.1基本用法 Robot Framework测试用例从命令行执行,默认情况下,最终结果是XML格式的输出文件和HTML 报告和日志.执行后,可以组合输出文件,然后使用rebot工具进行后处理. 3. ...
- 批量生成随机字符串并保存到excel
需要导入jxl.jar,commons-lang-2.6.jar 链接:https://pan.baidu.com/s/1NPPh24XWxkka68x2JQYlYA 提取码:jvj3 链接:http ...
- Kattis - missinggnomesD Missing Gnomes (思路题)
题目: 题意: 给出已经去除了几个数的一个序列,任务是将去除的数字插回去补全这个序列,输出字典序排在第一的那个补全的序列. 例如: 样例输入: 5 3 1 4 2 样例输出: 1 3 4 2 5 思路 ...
- <MyBatis>入门一 HelloWorld
1.HelloWorld 导入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependen ...
- linux whereis-查找二进制程序、代码等相关文件路径
推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 whereis命令用来定位指令的二进制程序.源代码文件和man手册页等相关文件的路径. whereis命令只能用于程序名的搜索,而且 ...
- CCF201509-2 日期计算 java(100分)
试题编号: 201509-2 试题名称: 日期计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有2 ...
- LINUX-文件的特殊属性 - 使用 "+" 设置权限,使用 "-" 用于取消
chattr +a file1 只允许以追加方式读写文件 chattr +c file1 允许这个文件能被内核自动压缩/解压 chattr +d file1 在进行文件系统备份时,dump程序将忽略这 ...
- day21 01 包的初识
day21 01包的初识 包:把解决一类问题的模块放在同一个文件夹里面-----包(一个包里面通常会含有_init_.py文件(python2里面必须有),但是后面的就没有要求一定要有了) 同样导入的 ...