bash sleep – 永夜 https://www.shuijingwanwq.com 没有不值得去解决的问题,也没有不值得去学习的技术! Mon, 09 Mar 2020 02:10:11 +0000 zh-Hans hourly 1 https://wordpress.org/?v=7.0 在 Rancher 中实现集群的部署与测试 ( 负载均衡 与 命令行下的 Redis 锁定、性能提升 ) https://www.shuijingwanwq.com/2020/03/09/3998/ https://www.shuijingwanwq.com/2020/03/09/3998/#respond Mon, 09 Mar 2020 02:10:11 +0000 https://www.shuijingwanwq.com/?p=3998 浏览量: 91 1、在 Rancher 中,克隆已经存在的 Docker 容器 channel-pub-api-prev 为 channel-pub-api-prev-1、channel-pub-api-prev-2,如图1
在 Rancher 中,克隆已经存在的 Docker 容器 channel-pub-api-prev 为 channel-pub-api-prev-1、channel-pub-api-prev-2

图1

2、最终 3 个容器组成了一个集群,如图2
最终 3 个容器组成了一个集群

图2

3、在负载均衡中,添加服务规则,设置域名与目标容器的映射关系,如图3
在负载均衡中,添加服务规则,设置域名与目标容器的映射关系

图3

4、在 Postman 中执行了 2 次请求,第 1 次为 GET,第 2 次为 POST,最终在容器 channel-pub-api-prev 中有一条 GET 请求日志,在容器 channel-pub-api-prev-1 中有一条 POST 请求日志,在容器 channel-pub-api-prev-2 中无请求日志,负载均衡设置符合预期,如图4
在 Postman 中执行了 2 次请求,第 1 次为 GET,第 2 次为 POST,最终在容器 channel-pub-api-prev 中有一条 GET 请求日志,在容器 channel-pub-api-prev-1 中有一条 POST 请求日志,在容器 channel-pub-api-prev-2 中无请求日志,负载均衡设置符合预期

图4

5、在 3 个容器中,均在基于 Supervisor 监听队列,在队列所调用的服务中,输出日志至文件中,最终在容器 channel-pub-api-prev 中有一条服务日志,表明队列作业在容器 channel-pub-api-prev 中执行的。如图5
在 3 个容器中,均在基于 Supervisor 监听队列,在队列所调用的服务中,输出日志至文件中,最终在容器 channel-pub-api-prev 中有一条服务日志,表明队列作业在容器 channel-pub-api-prev 中执行的。

图5

6、查看 命令行下的 Redis 锁定日志,不仅存在 判断Redis模型的锁定是否存在(已存在),而且存在 Redis模型的锁定实现时失败的 日志。时间:14:58 – 14:50 = 9 分钟。锁定次数:29 + 31 + 10 = 70 。锁定频率:70 / 9 = 8,即每分钟 8 次。如图6
查看 命令行下的 Redis 锁定日志,不仅存在 判断Redis模型的锁定是否存在(已存在),而且存在 Redis模型的锁定实现时失败的 日志。时间:14:58 - 14:50 = 9 分钟。锁定次数:29 + 31 + 10 = 70 。锁定频率:70 / 9 = 8,即每分钟 8 次

图6



[root@7edbf272f55f logs]# ls -ltr
total 136
-rw-r--r-- 1 root root 16556 Mar  4 14:50 app.log
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-lock-2-1583304715.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304842.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304846.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304851.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304855.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304859.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304863.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304868.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304872.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304876.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304880.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304885.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304889.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304893.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304897.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304902.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304906.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304910.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304914.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304919.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304923.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304927.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305042.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305046.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305051.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305063.txt
-rw-r--r-- 1 root root     1 Mar  4 14:58 qq-cw-transaction-article-sync-is-lock-exist-2-1583305084.txt
-rw-r--r-- 1 root root     1 Mar  4 14:58 qq-cw-transaction-article-sync-is-lock-exist-2-1583305093.txt
-rw-r--r-- 1 root root     1 Mar  4 14:58 qq-cw-transaction-article-sync-is-lock-exist-2-1583305097.txt

[root@22f7ada2d88d logs]# ls -ltr
total 144
-rw-r--r-- 1 root root 16547 Mar  4 14:50 app.log
-rw-r--r-- 1 root root     1 Mar  4 14:50 qq-cw-transaction-article-sync-is-lock-exist-2-1583304652.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304665.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304669.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304673.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304677.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304682.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304686.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304690.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304694.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304699.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304703.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304707.txt
-rw-r--r-- 1 root root     1 Mar  4 14:51 qq-cw-transaction-article-sync-is-lock-exist-2-1583304711.txt
-rw-r--r-- 1 root root     1 Mar  4 14:52 qq-cw-transaction-article-sync-is-lock-exist-2-1583304720.txt
-rw-r--r-- 1 root root     1 Mar  4 14:53 qq-cw-transaction-article-sync-is-lock-exist-2-1583304816.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304846.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304851.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304855.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304859.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304864.txt
-rw-r--r-- 1 root root     1 Mar  4 14:54 qq-cw-transaction-article-sync-is-lock-exist-2-1583304868.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304949.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304954.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304958.txt
-rw-r--r-- 1 root root     1 Mar  4 14:56 qq-cw-transaction-article-sync-is-lock-exist-2-1583304962.txt
-rw-r--r-- 1 root root     1 Mar  4 14:56 qq-cw-transaction-article-sync-is-lock-exist-2-1583304988.txt
-rw-r--r-- 1 root root     1 Mar  4 14:56 qq-cw-transaction-article-sync-is-lock-exist-2-1583304996.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305055.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305060.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305072.txt
-rw-r--r-- 1 root root     1 Mar  4 14:58 qq-cw-transaction-article-sync-is-lock-exist-2-1583305081.txt

[root@54d07ee806e6 logs]# ls -ltr
total 60
-rw-r--r-- 1 root root 16556 Mar  4 14:53 app.log
-rw-r--r-- 1 root root     1 Mar  4 14:53 qq-cw-transaction-article-sync-is-lock-exist-2-1583304824.txt
-rw-r--r-- 1 root root     1 Mar  4 14:53 qq-cw-transaction-article-sync-is-lock-exist-2-1583304829.txt
-rw-r--r-- 1 root root     1 Mar  4 14:53 qq-cw-transaction-article-sync-is-lock-exist-2-1583304833.txt
-rw-r--r-- 1 root root     1 Mar  4 14:53 qq-cw-transaction-article-sync-is-lock-exist-2-1583304837.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304932.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304936.txt
-rw-r--r-- 1 root root     1 Mar  4 14:55 qq-cw-transaction-article-sync-is-lock-exist-2-1583304941.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305029.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305033.txt
-rw-r--r-- 1 root root     1 Mar  4 14:57 qq-cw-transaction-article-sync-is-lock-exist-2-1583305038.txt


7、锁定频率过高的根源应该在于命令行中仅有同一条任务在执行。决定模拟出 6 条任务,锁定频率应该会有所下降。时间:15:52 – 15:47 = 6 分钟。锁定次数:47 + 34 + 45 = 126 。锁定频率:126 / 6 = 9,即每分钟 20 次。不符合预期。


[root@7edbf272f55f logs]# ls -ltr
total 188
-rw-r--r-- 1 root root 1 Mar  4 15:47 qq-cw-transaction-video-sync-is-lock-exist-4-1583308059.txt
-rw-r--r-- 1 root root 1 Mar  4 15:47 qq-cw-transaction-video-sync-lock-5-1583308059.txt
-rw-r--r-- 1 root root 1 Mar  4 15:47 qq-cw-transaction-article-sync-is-lock-exist-6-1583308069.txt
-rw-r--r-- 1 root root 1 Mar  4 15:47 qq-cw-transaction-article-sync-is-lock-exist-8-1583308069.txt
-rw-r--r-- 1 root root 1 Mar  4 15:48 qq-cw-transaction-video-sync-is-lock-exist-9-1583308096.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-7-1583308161.txt
-rw-r--r-- 1 root root 2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-11-1583308161.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-6-1583308166.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-8-1583308166.txt
-rw-r--r-- 1 root root 2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-12-1583308167.txt
-rw-r--r-- 1 root root 2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-11-1583308187.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308222.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308222.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308222.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308226.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308226.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308226.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308230.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308230.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308230.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308230.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308234.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308234.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308239.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308239.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308239.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308239.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308243.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308243.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308243.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308247.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308248.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308251.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308256.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-11-1583308260.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-11-1583308264.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-11-1583308268.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-11-1583308272.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308277.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308281.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308290.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308294.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308303.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308307.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308311.txt
-rw-r--r-- 1 root root 2 Mar  4 15:52 qq-cw-transaction-article-sync-is-lock-exist-12-1583308332.txt
-rw-r--r-- 1 root root 2 Mar  4 15:52 qq-cw-transaction-article-sync-is-lock-exist-12-1583308344.txt

[root@22f7ada2d88d logs]# ls -ltr
total 136
-rw-r--r-- 1 root root 1 Mar  4 15:47 qq-cw-transaction-video-sync-is-lock-exist-5-1583308059.txt
-rw-r--r-- 1 root root 2 Mar  4 15:48 qq-cw-transaction-video-sync-is-lock-exist-10-1583308096.txt
-rw-r--r-- 1 root root 1 Mar  4 15:48 qq-cw-transaction-article-sync-is-lock-exist-8-1583308126.txt
-rw-r--r-- 1 root root 2 Mar  4 15:48 qq-cw-transaction-article-sync-is-lock-exist-11-1583308126.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-6-1583308177.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-6-1583308182.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-8-1583308183.txt
-rw-r--r-- 1 root root 2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-12-1583308183.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-6-1583308187.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-6-1583308196.txt
-rw-r--r-- 1 root root 1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-8-1583308196.txt
-rw-r--r-- 1 root root 2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-12-1583308196.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308206.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308222.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308222.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308226.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308226.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308226.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308230.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308230.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308230.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308235.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308235.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308235.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308239.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308239.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308239.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308243.txt
-rw-r--r-- 1 root root 1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308243.txt
-rw-r--r-- 1 root root 2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308244.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308286.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308298.txt
-rw-r--r-- 1 root root 2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308315.txt
-rw-r--r-- 1 root root 2 Mar  4 15:52 qq-cw-transaction-article-sync-is-lock-exist-12-1583308323.txt

[root@54d07ee806e6 logs]# ls -ltr
total 196
-rw-r--r-- 1 root root     1 Mar  4 15:47 qq-cw-transaction-video-sync-is-lock-exist-4-1583308059.txt
-rw-r--r-- 1 root root     1 Mar  4 15:47 qq-cw-transaction-article-sync-is-lock-exist-7-1583308069.txt
-rw-r--r-- 1 root root     1 Mar  4 15:48 qq-cw-transaction-article-sync-is-lock-exist-8-1583308087.txt
-rw-r--r-- 1 root root 17361 Mar  4 15:48 app.log
-rw-r--r-- 1 root root     2 Mar  4 15:48 qq-cw-transaction-video-sync-is-lock-exist-10-1583308096.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-6-1583308160.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-8-1583308161.txt
-rw-r--r-- 1 root root     2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-12-1583308161.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-7-1583308166.txt
-rw-r--r-- 1 root root     2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-11-1583308167.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-7-1583308177.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-7-1583308182.txt
-rw-r--r-- 1 root root     2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-11-1583308183.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-7-1583308187.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-8-1583308187.txt
-rw-r--r-- 1 root root     2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-12-1583308187.txt
-rw-r--r-- 1 root root     1 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-7-1583308196.txt
-rw-r--r-- 1 root root     2 Mar  4 15:49 qq-cw-transaction-article-sync-is-lock-exist-11-1583308196.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308205.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308206.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308226.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-lock-8-1583308226.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-7-1583308230.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308230.txt
-rw-r--r-- 1 root root     2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308230.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308234.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308235.txt
-rw-r--r-- 1 root root     2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308235.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308239.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308239.txt
-rw-r--r-- 1 root root     2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308239.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-6-1583308243.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-lock-7-1583308243.txt
-rw-r--r-- 1 root root     2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-11-1583308243.txt
-rw-r--r-- 1 root root     2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308243.txt
-rw-r--r-- 1 root root     1 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-8-1583308248.txt
-rw-r--r-- 1 root root     2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308251.txt
-rw-r--r-- 1 root root     2 Mar  4 15:50 qq-cw-transaction-article-sync-is-lock-exist-12-1583308256.txt
-rw-r--r-- 1 root root     2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308260.txt
-rw-r--r-- 1 root root     2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308264.txt
-rw-r--r-- 1 root root     2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308269.txt
-rw-r--r-- 1 root root     2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-12-1583308273.txt
-rw-r--r-- 1 root root     2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-11-1583308277.txt
-rw-r--r-- 1 root root     2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-11-1583308281.txt
-rw-r--r-- 1 root root     2 Mar  4 15:51 qq-cw-transaction-article-sync-is-lock-exist-11-1583308285.txt


8、决定调整 bash sleep 3 为 bash sleep 60,即命令行运行一次后,间隔 60 秒后再次运行,而不是 3 秒。重复第 7 步骤,不存在 Redis 锁定日志。符合预期。 9、基于同步时间、ID顺序排列(企鹅号的事务)。某一任务开始执行时,更新企鹅号的事务的同步时间为当前时间 ,以提升执行性能,且再次降低锁定频率。


        // 查询企鹅号的应用类型,cw:内容网站应用 && 类型,1:文章 && 状态,3:处理中 && 文章发布状态:审核中的5条记录,基于同步时间、ID顺序排列(企鹅号的事务)
        $qqTransactionArticleItems = QqTransaction::find()->where(['qq_app_type' => QqArticle::QQ_APP_TYPE_CW, 'type' => QqTransaction::TYPE_ARTICLE, 'article_pub_flag' => HttpQqApiTransaction::ARTICLE_PUB_FLAG_REVIEW])->isDeletedNo()->processing()->orderBy(['synced_at' => SORT_ASC, 'id' => SORT_ASC])->limit(5)->all();

        if ($qqTransactionArticleItems) {
            foreach ($qqTransactionArticleItems as $qqTransactionArticleItem) {
                /* 判断Redis模型的锁定是否存在 */
                $redisLockKeyName = 'qq_cw_transaction_article:sync:' . implode(':', ['id' => $qqTransactionArticleItem->id]);
                $redisLock = new RedisLock();
                $isLockExist = $redisLock->isLockExist($redisLockKeyName);
                // 返回 true,表示锁定存在,即已经被其他客户端锁定
                if ($isLockExist === true) {
                    continue;
                }

                /* Redis模型的锁定实现 */
                $lock = $redisLock->lock($redisLockKeyName, 600);
                // 返回 false,表示已经被其他客户端锁定
                if ($lock === false) {
                    continue;
                }

                // 基于ID查找单个数据模型(企鹅号的事务)
                $qqTransactionArticleItem = QqTransaction::findOne($qqTransactionArticleItem->id);

                // 更新企鹅号的事务的同步时间
                $qqTransactionArticleItem->synced_at = time();
                $qqTransactionArticleItem->update();
                file_put_contents('/mcloud/www/channel-pub-api/console/runtime/logs/qq-cw-transaction-article-sync-' . $qqTransactionArticleItem->id . '-' . time() . '.txt', $qqTransactionArticleItem->id);
            }
        }


10、模拟出 3 条任务。事务ID为 41、42、43 的同步时间(分钟)分别为:9、9、8 。事务ID为 41、42、43 的同步次数分别为:22、20、17 。事务ID为 41、42、43 的同步频率分别为:9 * 60 / 22 = 25、9 * 60 / 20 = 27、8 * 60 / 17 = 28。得出平均值:(25 + 27 + 28) / 3 = 27。理论上的计算公式,进一法取整(事务数量 / 5) / 容器数量 * 60。符合预期。如图7
模拟出 3 条任务。事务ID为 41、42、43 的同步时间(分钟)分别为:9、9、8 。事务ID为 41、42、43 的同步次数分别为:22、20、17 。事务ID为 41、42、43 的同步频率分别为:9 * 60 / 22 = 25、9 * 60 / 20 = 27、8 * 60 / 17 = 28。得出平均值:(25 + 27 + 28) / 3 = 27。理论上的计算公式,进一法取整(事务数量 / 5) / 容器数量 * 60。符合预期

图7



[root@21325d776096 logs]# ls -ltr
total 64
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-41-1583399050.txt
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-43-1583399050.txt
-rw-r--r-- 1 root root 2 Mar  5 17:05 qq-cw-transaction-article-sync-41-1583399112.txt
-rw-r--r-- 1 root root 2 Mar  5 17:05 qq-cw-transaction-article-sync-42-1583399112.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-41-1583399173.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-42-1583399174.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-41-1583399235.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-42-1583399235.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-43-1583399236.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-41-1583399297.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-43-1583399298.txt
-rw-r--r-- 1 root root 2 Mar  5 17:09 qq-cw-transaction-article-sync-41-1583399359.txt
-rw-r--r-- 1 root root 2 Mar  5 17:09 qq-cw-transaction-article-sync-42-1583399359.txt
-rw-r--r-- 1 root root 2 Mar  5 17:10 qq-cw-transaction-article-sync-41-1583399421.txt
-rw-r--r-- 1 root root 2 Mar  5 17:10 qq-cw-transaction-article-sync-42-1583399421.txt
-rw-r--r-- 1 root root 2 Mar  5 17:11 qq-cw-transaction-article-sync-42-1583399483.txt

[root@e9565f71310d logs]# ls -ltr
total 100
-rw-r--r-- 1 root root 2 Mar  5 17:03 qq-cw-transaction-article-sync-41-1583399012.txt
-rw-r--r-- 1 root root 2 Mar  5 17:03 qq-cw-transaction-article-sync-42-1583399013.txt
-rw-r--r-- 1 root root 2 Mar  5 17:03 qq-cw-transaction-article-sync-43-1583399013.txt
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-41-1583399074.txt
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-42-1583399075.txt
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-43-1583399076.txt
-rw-r--r-- 1 root root 2 Mar  5 17:05 qq-cw-transaction-article-sync-41-1583399137.txt
-rw-r--r-- 1 root root 2 Mar  5 17:05 qq-cw-transaction-article-sync-42-1583399138.txt
-rw-r--r-- 1 root root 2 Mar  5 17:05 qq-cw-transaction-article-sync-43-1583399139.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-41-1583399200.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-42-1583399201.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-43-1583399201.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-41-1583399263.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-42-1583399264.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-43-1583399264.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-41-1583399325.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-42-1583399326.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-43-1583399326.txt
-rw-r--r-- 1 root root 2 Mar  5 17:09 qq-cw-transaction-article-sync-42-1583399388.txt
-rw-r--r-- 1 root root 2 Mar  5 17:09 qq-cw-transaction-article-sync-43-1583399388.txt
-rw-r--r-- 1 root root 2 Mar  5 17:09 qq-cw-transaction-article-sync-41-1583399388.txt
-rw-r--r-- 1 root root 2 Mar  5 17:10 qq-cw-transaction-article-sync-42-1583399450.txt
-rw-r--r-- 1 root root 2 Mar  5 17:10 qq-cw-transaction-article-sync-43-1583399451.txt
-rw-r--r-- 1 root root 2 Mar  5 17:10 qq-cw-transaction-article-sync-41-1583399451.txt
-rw-r--r-- 1 root root 2 Mar  5 17:11 qq-cw-transaction-article-sync-42-1583399512.txt

[root@7c7a4390ba3f logs]# ls -ltr
total 72
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-42-1583399050.txt
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-43-1583399050.txt
-rw-r--r-- 1 root root 2 Mar  5 17:04 qq-cw-transaction-article-sync-41-1583399050.txt
-rw-r--r-- 1 root root 2 Mar  5 17:05 qq-cw-transaction-article-sync-43-1583399112.txt
-rw-r--r-- 1 root root 2 Mar  5 17:05 qq-cw-transaction-article-sync-41-1583399112.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-43-1583399174.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-41-1583399174.txt
-rw-r--r-- 1 root root 2 Mar  5 17:06 qq-cw-transaction-article-sync-42-1583399174.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-41-1583399236.txt
-rw-r--r-- 1 root root 2 Mar  5 17:07 qq-cw-transaction-article-sync-42-1583399236.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-42-1583399298.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-43-1583399298.txt
-rw-r--r-- 1 root root 2 Mar  5 17:08 qq-cw-transaction-article-sync-41-1583399298.txt
-rw-r--r-- 1 root root 2 Mar  5 17:09 qq-cw-transaction-article-sync-43-1583399360.txt
-rw-r--r-- 1 root root 2 Mar  5 17:09 qq-cw-transaction-article-sync-41-1583399360.txt
-rw-r--r-- 1 root root 2 Mar  5 17:10 qq-cw-transaction-article-sync-43-1583399421.txt
-rw-r--r-- 1 root root 2 Mar  5 17:10 qq-cw-transaction-article-sync-41-1583399422.txt
-rw-r--r-- 1 root root 2 Mar  5 17:11 qq-cw-transaction-article-sync-42-1583399483.txt


11、模拟出 5 条任务。事务ID为 47、48、49、52、53 的同步时间(分钟)分别为:4、11、4、5、5 。事务ID为 47、48、49、52、53 的同步次数分别为:10、20、10、9、10 。事务ID为 47、48、49、52、53 的同步频率分别为:4 * 60 / 10 = 24、11 * 60 / 20 = 33、4 * 60 / 10 = 24、5 * 60 / 9 = 33、5 * 60 / 10 = 30。得出平均值:(24 + 33 + 24 + 33 + 30) / 5 = 29。理论上的计算公式,进一法取整(事务数量 / 5) / 容器数量 * 60。符合预期。如图8
模拟出 5 条任务。事务ID为 47、48、49、52、53 的同步时间(分钟)分别为:4、11、4、5、5 。事务ID为 47、48、49、52、53 的同步次数分别为:10、20、10、9、10 。事务ID为 47、48、49、52、53 的同步频率分别为:4 * 60 / 10 = 24、11 * 60 / 20 = 33、4 * 60 / 10 = 24、5 * 60 / 9 = 33、5 * 60 / 10 = 30。得出平均值:(24 + 33 + 24 + 33 + 30) / 5 = 29。理论上的计算公式,进一法取整(事务数量 / 5) / 容器数量 * 60。符合预期

图8



[root@21325d776096 logs]# ls -ltr
total 88
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-47-1583459743.txt
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-48-1583459744.txt
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-49-1583459744.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-52-1583459806.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-47-1583459807.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-48-1583459807.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-49-1583459807.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-53-1583459807.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-47-1583459869.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-48-1583459869.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-52-1583459870.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-49-1583459870.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-53-1583459870.txt
-rw-r--r-- 1 root root 2 Mar  6 09:58 qq-cw-transaction-article-sync-48-1583459932.txt
-rw-r--r-- 1 root root 2 Mar  6 09:58 qq-cw-transaction-article-sync-52-1583459932.txt
-rw-r--r-- 1 root root 2 Mar  6 09:58 qq-cw-transaction-article-sync-53-1583459932.txt
-rw-r--r-- 1 root root 2 Mar  6 09:59 qq-cw-transaction-article-sync-48-1583459994.txt
-rw-r--r-- 1 root root 2 Mar  6 10:00 qq-cw-transaction-article-sync-48-1583460055.txt
-rw-r--r-- 1 root root 2 Mar  6 10:01 qq-cw-transaction-article-sync-48-1583460117.txt
-rw-r--r-- 1 root root 2 Mar  6 10:02 qq-cw-transaction-article-sync-48-1583460179.txt
-rw-r--r-- 1 root root 2 Mar  6 10:04 qq-cw-transaction-article-sync-48-1583460240.txt
-rw-r--r-- 1 root root 2 Mar  6 10:05 qq-cw-transaction-article-sync-48-1583460302.txt

[root@e9565f71310d logs]# ls -ltr
total 96
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-52-1583459751.txt
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-53-1583459752.txt
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-47-1583459752.txt
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-48-1583459752.txt
-rw-r--r-- 1 root root 2 Mar  6 09:55 qq-cw-transaction-article-sync-49-1583459752.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-52-1583459813.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-47-1583459813.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-48-1583459814.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-49-1583459814.txt
-rw-r--r-- 1 root root 2 Mar  6 09:56 qq-cw-transaction-article-sync-53-1583459814.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-47-1583459876.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-48-1583459877.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-49-1583459877.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-52-1583459878.txt
-rw-r--r-- 1 root root 2 Mar  6 09:57 qq-cw-transaction-article-sync-53-1583459878.txt
-rw-r--r-- 1 root root 2 Mar  6 09:59 qq-cw-transaction-article-sync-48-1583459940.txt
-rw-r--r-- 1 root root 2 Mar  6 09:59 qq-cw-transaction-article-sync-52-1583459940.txt
-rw-r--r-- 1 root root 2 Mar  6 09:59 qq-cw-transaction-article-sync-53-1583459941.txt
-rw-r--r-- 1 root root 2 Mar  6 10:00 qq-cw-transaction-article-sync-48-1583460002.txt
-rw-r--r-- 1 root root 2 Mar  6 10:01 qq-cw-transaction-article-sync-48-1583460063.txt
-rw-r--r-- 1 root root 2 Mar  6 10:02 qq-cw-transaction-article-sync-48-1583460125.txt
-rw-r--r-- 1 root root 2 Mar  6 10:03 qq-cw-transaction-article-sync-48-1583460187.txt
-rw-r--r-- 1 root root 2 Mar  6 10:04 qq-cw-transaction-article-sync-48-1583460248.txt
-rw-r--r-- 1 root root 2 Mar  6 10:05 qq-cw-transaction-article-sync-48-1583460310.txt

[root@7c7a4390ba3f logs]# ls -ltr
total 120
-rw-r--r-- 1 root root 17367 Mar  6 09:55 app.log
-rw-r--r-- 1 root root     2 Mar  6 09:55 qq-cw-transaction-article-sync-47-1583459731.txt
-rw-r--r-- 1 root root     2 Mar  6 09:55 qq-cw-transaction-article-sync-48-1583459732.txt
-rw-r--r-- 1 root root     2 Mar  6 09:55 qq-cw-transaction-article-sync-49-1583459732.txt
-rw-r--r-- 1 root root     2 Mar  6 09:56 qq-cw-transaction-article-sync-52-1583459793.txt
-rw-r--r-- 1 root root     2 Mar  6 09:56 qq-cw-transaction-article-sync-47-1583459794.txt
-rw-r--r-- 1 root root     2 Mar  6 09:56 qq-cw-transaction-article-sync-48-1583459794.txt
-rw-r--r-- 1 root root     2 Mar  6 09:56 qq-cw-transaction-article-sync-49-1583459794.txt
-rw-r--r-- 1 root root     2 Mar  6 09:56 qq-cw-transaction-article-sync-53-1583459795.txt
-rw-r--r-- 1 root root     2 Mar  6 09:57 qq-cw-transaction-article-sync-47-1583459856.txt
-rw-r--r-- 1 root root     2 Mar  6 09:57 qq-cw-transaction-article-sync-52-1583459857.txt
-rw-r--r-- 1 root root     2 Mar  6 09:57 qq-cw-transaction-article-sync-48-1583459857.txt
-rw-r--r-- 1 root root     2 Mar  6 09:57 qq-cw-transaction-article-sync-49-1583459858.txt
-rw-r--r-- 1 root root     2 Mar  6 09:57 qq-cw-transaction-article-sync-53-1583459859.txt
-rw-r--r-- 1 root root     2 Mar  6 09:58 qq-cw-transaction-article-sync-47-1583459920.txt
-rw-r--r-- 1 root root     2 Mar  6 09:58 qq-cw-transaction-article-sync-48-1583459921.txt
-rw-r--r-- 1 root root     2 Mar  6 09:58 qq-cw-transaction-article-sync-49-1583459922.txt
-rw-r--r-- 1 root root     2 Mar  6 09:58 qq-cw-transaction-article-sync-52-1583459922.txt
-rw-r--r-- 1 root root     2 Mar  6 09:58 qq-cw-transaction-article-sync-53-1583459922.txt
-rw-r--r-- 1 root root     2 Mar  6 09:59 qq-cw-transaction-article-sync-48-1583459984.txt
-rw-r--r-- 1 root root     2 Mar  6 10:00 qq-cw-transaction-article-sync-48-1583460045.txt
-rw-r--r-- 1 root root     2 Mar  6 10:01 qq-cw-transaction-article-sync-48-1583460107.txt
-rw-r--r-- 1 root root     2 Mar  6 10:02 qq-cw-transaction-article-sync-48-1583460169.txt
-rw-r--r-- 1 root root     2 Mar  6 10:03 qq-cw-transaction-article-sync-48-1583460231.txt
-rw-r--r-- 1 root root     2 Mar  6 10:04 qq-cw-transaction-article-sync-48-1583460293.txt
-rw-r--r-- 1 root root     2 Mar  6 10:05 qq-cw-transaction-article-sync-48-1583460355.txt


12、模拟出 8 条任务。事务ID为 57、58、59、63、64、65、68、69 的同步时间(分钟)分别为:11、12、13、12、11、12、11、12 。事务ID为 57、58、59、63、64、65、68、69 的同步次数分别为:21、22、22、19、17、5、17、17 。事务ID为 57、58、59、63、64、65、68、69 的同步频率分别为:11 * 60 / 21 = 31、12 * 60 / 22 = 33、13 * 60 / 22 = 35、12 * 60 / 19 = 38、11 * 60 / 17 = 39、12 * 60 / 5 = 144、11 * 60 / 17 = 39、12 * 60 / 17 = 42。得出平均值:(31 + 33 + 35 + 38 + 39 + 144 + 39 + 42) / 8 = 50。理论上的计算公式,进一法取整(事务数量 / 5) / 容器数量 * 60。符合预期。如图9
模拟出 8 条任务。事务ID为 57、58、59、63、64、65、68、69 的同步时间(分钟)分别为:11、12、13、12、11、12、11、12 。事务ID为 57、58、59、63、64、65、68、69 的同步次数分别为:21、22、22、19、17、5、17、17 。事务ID为 57、58、59、63、64、65、68、69 的同步频率分别为:11 * 60 / 21 = 31、12 * 60 / 22 = 33、13 * 60 / 22 = 35、12 * 60 / 19 = 38、11 * 60 / 17 = 39、12 * 60 / 5 = 144、11 * 60 / 17 = 39、12 * 60 / 17 = 42。得出平均值:(31 + 33 + 35 + 38 + 39 + 144 + 39 + 42) / 8 = 50。理论上的计算公式,进一法取整(事务数量 / 5) / 容器数量 * 60。符合预期

图9



[root@21325d776096 logs]# ls -ltr
total 220
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-57-1583461834.txt
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-58-1583461834.txt
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-59-1583461834.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-68-1583461896.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-69-1583461896.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-57-1583461897.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-58-1583461897.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-63-1583461959.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-64-1583461959.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-68-1583461959.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-69-1583461959.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-58-1583462021.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-59-1583462022.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-63-1583462022.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-57-1583462022.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-68-1583462084.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-58-1583462084.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-69-1583462085.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-59-1583462085.txt
-rw-r--r-- 1 root root 34566 Mar  6 10:35 app.log
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-63-1583462146.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-64-1583462147.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-68-1583462147.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-57-1583462147.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-59-1583462209.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-63-1583462209.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-69-1583462210.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-57-1583462210.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-64-1583462271.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-68-1583462272.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-59-1583462272.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-57-1583462272.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-63-1583462334.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-64-1583462334.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-69-1583462335.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-57-1583462335.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-59-1583462396.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-63-1583462397.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-68-1583462397.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-57-1583462397.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-59-1583462459.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-64-1583462459.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-69-1583462460.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-65-1583462460.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-58-1583462460.txt
-rw-r--r-- 1 root root     2 Mar  6 10:42 qq-cw-transaction-article-sync-69-1583462522.txt
-rw-r--r-- 1 root root     2 Mar  6 10:42 qq-cw-transaction-article-sync-59-1583462523.txt

[root@e9565f71310d logs]# ls -ltr
total 212
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-63-1583461842.txt
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-64-1583461842.txt
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-65-1583461843.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-59-1583461905.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-63-1583461905.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-64-1583461906.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-68-1583461906.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-57-1583461968.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-58-1583461968.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-59-1583461969.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-63-1583461969.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-64-1583462030.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-68-1583462031.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-69-1583462032.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-58-1583462032.txt
-rw-r--r-- 1 root root 33937 Mar  6 10:34 app.log
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-57-1583462093.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-63-1583462094.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-64-1583462094.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-68-1583462094.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-58-1583462155.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-59-1583462156.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-69-1583462156.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-63-1583462156.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-58-1583462218.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-64-1583462218.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-68-1583462218.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-59-1583462219.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-58-1583462280.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-63-1583462281.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-69-1583462281.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-64-1583462281.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-58-1583462343.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-59-1583462344.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-68-1583462344.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-63-1583462344.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-58-1583462406.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-64-1583462406.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-69-1583462406.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-59-1583462406.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-63-1583462468.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-68-1583462468.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-59-1583462469.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-58-1583462469.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-65-1583462470.txt

[root@7c7a4390ba3f logs]# ls -ltr
total 220
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-57-1583461824.txt
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-58-1583461825.txt
-rw-r--r-- 1 root root     2 Mar  6 10:30 qq-cw-transaction-article-sync-59-1583461825.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-57-1583461886.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-58-1583461886.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-59-1583461886.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-63-1583461887.txt
-rw-r--r-- 1 root root     2 Mar  6 10:31 qq-cw-transaction-article-sync-64-1583461887.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-69-1583461948.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-57-1583461949.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-58-1583461949.txt
-rw-r--r-- 1 root root     2 Mar  6 10:32 qq-cw-transaction-article-sync-59-1583461949.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-64-1583462011.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-68-1583462011.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-69-1583462011.txt
-rw-r--r-- 1 root root     2 Mar  6 10:33 qq-cw-transaction-article-sync-57-1583462011.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-59-1583462073.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-57-1583462074.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-63-1583462074.txt
-rw-r--r-- 1 root root     2 Mar  6 10:34 qq-cw-transaction-article-sync-64-1583462074.txt
-rw-r--r-- 1 root root 17200 Mar  6 10:35 app.log
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-58-1583462136.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-59-1583462136.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-69-1583462136.txt
-rw-r--r-- 1 root root     2 Mar  6 10:35 qq-cw-transaction-article-sync-57-1583462136.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-57-1583462198.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-64-1583462199.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-68-1583462199.txt
-rw-r--r-- 1 root root     2 Mar  6 10:36 qq-cw-transaction-article-sync-58-1583462199.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-63-1583462261.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-57-1583462261.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-69-1583462261.txt
-rw-r--r-- 1 root root     2 Mar  6 10:37 qq-cw-transaction-article-sync-58-1583462261.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-57-1583462323.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-59-1583462324.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-68-1583462324.txt
-rw-r--r-- 1 root root     2 Mar  6 10:38 qq-cw-transaction-article-sync-58-1583462324.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-64-1583462386.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-57-1583462386.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-69-1583462386.txt
-rw-r--r-- 1 root root     2 Mar  6 10:39 qq-cw-transaction-article-sync-58-1583462386.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-65-1583462448.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-57-1583462448.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-63-1583462449.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-68-1583462449.txt
-rw-r--r-- 1 root root     2 Mar  6 10:40 qq-cw-transaction-article-sync-58-1583462449.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-69-1583462511.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-63-1583462511.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-68-1583462512.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-59-1583462512.txt
-rw-r--r-- 1 root root     2 Mar  6 10:41 qq-cw-transaction-article-sync-65-1583462512.txt


 ]]>
https://www.shuijingwanwq.com/2020/03/09/3998/feed/ 0
在 Yii 2.0 中,控制台命令的集群实现,Redis模型的锁定实现(以保证同一时间段内,即使多台服务器皆在运行命令行,但是每台服务器运行的任务是不重复的,以提升命令行的总体处理性能) https://www.shuijingwanwq.com/2020/03/02/3964/ https://www.shuijingwanwq.com/2020/03/02/3964/#respond Mon, 02 Mar 2020 05:54:25 +0000 https://www.shuijingwanwq.com/?p=3964 浏览量: 70 1、Docker 部署,基于 Supervisor 的 crontab (bash sleep) 的实现,以降低内存占用,参考:https://www.shuijingwanwq.com/2019/10/12/3555/ 。/console/controllers/CmcConsoleUserController.php
<pre class="wp-block-syntaxhighlighter-code">

<?php
/**
 * Created by PhpStorm.
 * User: Qiang Wang
 * Date: 2019/01/09
 * Time: 13:55
 */

namespace console\controllers;

use Yii;
use console\models\ConfigColumnUser;
use console\models\redis\cmc_console\User as RedisCmcConsoleUser;
use console\services\CmcApiGroupService;
use console\services\CmcConsoleUserService;
use yii\base\InvalidArgumentException;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\helpers\ArrayHelper;
use yii\web\HttpException;
use yii\web\ServerErrorHttpException;

/**
 * 框架服务控制台的用户
 *
 * @author Qiang Wang <shuijingwanwq@163.com>
 * @since  1.0
 */
class CmcConsoleUserController extends Controller
{
    /**
     * 同步框架服务控制台的用户模型(Http)至框架服务控制台的用户模型(Redis)、栏目人员配置模型(MySQL)
     *
     * @throws ServerErrorHttpException
     * @throws HttpException 如果登录超时
     * @throws InvalidArgumentException if the $direction or $sortFlag parameters do not have
     */
    public function actionSync()
    {
        // 设置同步标识的缓存键
        $redisCache = Yii::$app->redisCache;
        $redisCacheIdentityKey = 'cmc_console_user_sync';

        // 从缓存中取回同步标识
        $redisCacheIdentityData = $redisCache[$redisCacheIdentityKey];

        if ($redisCacheIdentityData === false) {
            // HTTP 请求,获取开通有效服务的租户ID列表
            $httpCmcApiGroupIds = CmcApiGroupService::httpGetGroupIds();

            /* 判断 $httpCmcApiGroupIds 是否为空,如果为空,则成功退出 */
            if (empty($httpCmcApiGroupIds)) {
                // 延缓执行 60 * 60 秒
                // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyYesSleepTime']);

                return ExitCode::OK;
            }

            // 设置租户ID列表的缓存键
            $redisCacheGroupIdsKey = 'cmc_api_group_ids';

            // 从缓存中取回租户ID列表
            $redisCacheGroupIdsData = $redisCache[$redisCacheGroupIdsKey];

            // 是否设置租户ID列表的缓存,默认:否
            $isSetRedisCacheGroupIds = false;

            if ($redisCacheGroupIdsData === false) {
                $cmcApiGroupIds = [];
                foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                    $cmcApiGroupIds[] = [
                        'group_id' => $httpCmcApiGroupId,
                        'cmc_console_user_last_synced_at' => 0, //上次同步时间
                    ];
                }
                // 是否设置租户ID列表的缓存:是
                $isSetRedisCacheGroupIds = true;
            } else {
                // 获取 group_id 值列表
                $redisCacheGroupIds = ArrayHelper::getColumn($redisCacheGroupIdsData, 'group_id');
                $cmcApiGroupIds = $redisCacheGroupIdsData;
                foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                    if (!in_array($httpCmcApiGroupId, $redisCacheGroupIds)) {
                        $cmcApiGroupIds[] = [
                            'group_id' => $httpCmcApiGroupId,
                            'cmc_console_user_last_synced_at' => 0, //上次同步时间
                        ];
                        // 是否设置租户ID列表的缓存:是
                        $isSetRedisCacheGroupIds = true;
                    }
                }
            }

            // 判断是否设置租户ID列表的缓存
            if ($isSetRedisCacheGroupIds) {
                // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
                $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);
            }


            // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds
            $sortCmcApiGroupIds = $cmcApiGroupIds;
            ArrayHelper::multisort($sortCmcApiGroupIds, 'cmc_console_user_last_synced_at', SORT_ASC);

            foreach ($sortCmcApiGroupIds as $sortCmcApiGroupId) {
                $isLockExist = RedisCmcConsoleUser::isLockExist($sortCmcApiGroupId['group_id']);
                // 返回 true,表示锁定存在,即已经被其他客户端锁定
                if ($isLockExist === true) {
                    continue;
                }

                // HTTP请求,获取租户ID下的用户列表
                $httpGetUserListData = [
                    'groupId' => $sortCmcApiGroupId['group_id'],
                    'loginId' => '',
                    'loginTid' => '',
                ];
                $getUserListData = CmcConsoleUserService::httpGetUserList($httpGetUserListData);

                // 框架服务控制台的用户模型(Http),重建数组索引(以 ID 索引结果集)
                $httpCmcConsoleUserItems = ArrayHelper::index($getUserListData['list'], 'id');

                // 销毁变量
                unset($getUserListData['list']);

                // 基于租户ID查询框架服务控制台的用户模型(Redis)(以 ID 索引结果集)
                $redisCmcConsoleUserItems = RedisCmcConsoleUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->indexBy('id')->asArray()->all();

                // 使用键名比较计算数组的差集,如果不为空,则删除出现在 Redis 中但是未出现在 Http 中的记录
                $redisArrayDiffItems = array_diff_key($redisCmcConsoleUserItems, $httpCmcConsoleUserItems);
                // 销毁变量
                unset($redisCmcConsoleUserItems);
                if (!empty($redisArrayDiffItems)) {
                    $redisArrayDiffIds = array_keys($redisArrayDiffItems);
                    // 销毁变量
                    unset($redisArrayDiffItems);
                    if (RedisCmcConsoleUser::deleteAllByIds($sortCmcApiGroupId['group_id'], $redisArrayDiffIds) === false) {
                        continue;
                    }
                }

                // 基于租户ID查询栏目人员配置模型(MySQL)(以 用户ID 索引结果集)
                $configColumnUserItems = ConfigColumnUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->isDeletedNo()->indexBy('user_id')->asArray()->all();

                // 使用键名比较计算数组的差集,如果不为空,则删除 (软删除) 出现在 栏目人员配置模型(MySQL) 中但是未出现在 框架服务控制台的用户模型(Http) 中的记录
                $diffItems = array_diff_key($configColumnUserItems, $httpCmcConsoleUserItems);

                // 销毁变量
                unset($configColumnUserItems);
                if (!empty($diffItems)) {
                    // 基于租户ID、用户ID查询栏目人员配置模型(MySQL)(待删除)
                    $toBeDeletedConfigColumnUserItems = ConfigColumnUser::find()->where([
                        'and',
                        ['group_id' => $sortCmcApiGroupId['group_id']],
                        ['in', 'user_id', $diffItems],
                    ])->isDeletedNo()->all();
                    foreach ($toBeDeletedConfigColumnUserItems as $toBeDeletedConfigColumnUserItem) {
                        /* @var $toBeDeletedConfigColumnUserItem ConfigColumnUser */
                        $toBeDeletedConfigColumnUserItem->softDelete();
                    }
                }

                // 遍历框架服务控制台的用户模型(Http),判断在 Redis 中是否存在,如果不存在,则插入,如果存在且更新时间不相等,则更新
                foreach ($httpCmcConsoleUserItems as $httpCmcConsoleUserItemValue) {
                    $redisCmcConsoleUserItem = RedisCmcConsoleUser::find()->where(['id' => $httpCmcConsoleUserItemValue['id']])->one();

                    if (!isset($redisCmcConsoleUserItem)) {

                        $redisCmcConsoleUser = new RedisCmcConsoleUser();
                        $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                            $httpCmcConsoleUserItemValue);
                        $redisCmcConsoleUser->attributes = $attributes;
                        $redisCmcConsoleUser->insert();

                    } else {
                        if ($httpCmcConsoleUserItemValue['update_time'] != $redisCmcConsoleUserItem['update_time']) {

                            $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                                $httpCmcConsoleUserItemValue);
                            $redisCmcConsoleUserItem->attributes = $attributes;
                            $redisCmcConsoleUserItem->save();

                        }
                    }

                }

                // 从缓存中取回租户ID列表
                $cmcApiGroupIds = $redisCache[$redisCacheGroupIdsKey];
                // 设置当前租户的上次同步时间
                foreach ($cmcApiGroupIds as $cmcApiGroupIdKey => $cmcApiGroupId) {
                    if ($cmcApiGroupId['group_id'] == $sortCmcApiGroupId['group_id']) {
                        $cmcApiGroupIds[$cmcApiGroupIdKey]['cmc_console_user_last_synced_at'] = time();
                        break;
                    }
                }

                // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
                $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);

                // break 结束当前 foreach 结构的执行,即命令行的每一次运行,仅同步成功一个租户下的用户列表
                break;
            }

            // 延缓执行 60 秒
            // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyNoSleepTime']);
            // 将同步标识存放到缓存供下次使用,将数据在缓存中保留 60 秒
            $redisCache->set($redisCacheIdentityKey, $redisCacheIdentityKey, Yii::$app->params['cmcConsoleUser']['isEmptyNoSleepTime']);

            // file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-usage-' . time() . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
            // file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-peak-usage-' . time() . '.txt', memory_get_peak_usage() / 1024 / 1024 . 'MB');

            return ExitCode::OK;
        } else {
            // 延缓执行 60 秒
            // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyNoSleepTime']);
            return ExitCode::OK;
        }
    }

    /**
     * 获取属性列表
     * @param string $groupId 租户ID
     * @param array $httpCmcConsoleUser 框架服务控制台的用户模型(Http)
     *
     * @return array
     */
    private function getAttributes($groupId, $httpCmcConsoleUser) {
        return [
            'id' => $httpCmcConsoleUser['id'],
            'group_id' => $groupId,
            'login_name' => $httpCmcConsoleUser['login_name'],
            'user_token' => $httpCmcConsoleUser['user_token'],
            'user_nick' => $httpCmcConsoleUser['user_nick'],
            'user_pic' => $httpCmcConsoleUser['user_pic'],
            'user_mobile' => $httpCmcConsoleUser['user_mobile'] ? $httpCmcConsoleUser['user_mobile'] : '',
            'user_email' => $httpCmcConsoleUser['user_email'] ? $httpCmcConsoleUser['user_email'] : '',
            'user_sex' => $httpCmcConsoleUser['user_sex'],
            'user_type' => $httpCmcConsoleUser['user_type'],
            'user_birthday' => $httpCmcConsoleUser['user_birthday'],
            'user_chat_id' => $httpCmcConsoleUser['user_chat_id'] ? $httpCmcConsoleUser['user_chat_id'] : '',
            'is_open' => $httpCmcConsoleUser['is_open'],
            'add_time' => $httpCmcConsoleUser['add_time'],
            'update_time' => $httpCmcConsoleUser['update_time'],
        ];
    }
}

</pre>
2、break 结束当前 foreach 结构的执行,即命令行的每一次运行,仅同步成功一个租户下的用户列表。当租户数量过多的时候,假设 100 个租户,平均 1 个租户的同步时间为 10 秒钟 + 下一个租户的同步的间隔时间 60 秒(包含了 bash sleep 5 秒),那么可能某个租户下的用户同步,其最大间隔时间就有可能为 7000 秒钟了。因此,如果支持了集群的部署,假设为 10 个容器,那么某个租户下的用户同步,其最大间隔时间就有可能为 700 秒钟了,同步的即时性提升了 10 倍。/mcloud/yii-cmc-console-user-sync.ini


[program:yii-cmc-console-user-sync]
command = bash -c 'sleep 5 && exec php /mcloud/www/pcs-api/yii cmc-console-user/sync'
autorestart = true
startsecs = 5
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-cmc-console-user-sync-stderr.log
stdout_logfile = /data/logs/yii-cmc-console-user-sync-stdout.log


3、如果要支持集群的部署,现阶段存在的问题:同一个租户下的用户同步,可能在某一时间段,多个容器皆在执行,导致了同步的冗余执行(未达到最大化利用集群部署,进而满足同步的即时性提升至 10 倍的理想值)。因此,应该保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。 4、将同步标识存放到缓存供下次使用,将数据在缓存中保留 60 秒。之前实现的初衷是同步成功一个租户下的用户后,间隔 60 秒再同步下一个租户下的用户,以降低资源消耗。现在如果要支持集群,就需要取消。否则会导致某个容器同步成功一个租户下的用户后,在 60 秒的间隔时间段内,其他容器也无法同步其他租户下的用户。/console/controllers/CmcConsoleUserController.php
<pre class="wp-block-syntaxhighlighter-code">

<?php
/**
 * Created by PhpStorm.
 * User: Qiang Wang
 * Date: 2019/01/09
 * Time: 13:55
 */

namespace console\controllers;

use Yii;
use console\models\ConfigColumnUser;
use console\models\redis\cmc_console\User as RedisCmcConsoleUser;
use console\services\CmcApiGroupService;
use console\services\CmcConsoleUserService;
use yii\base\InvalidArgumentException;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\helpers\ArrayHelper;
use yii\web\HttpException;
use yii\web\ServerErrorHttpException;

/**
 * 框架服务控制台的用户
 *
 * @author Qiang Wang <shuijingwanwq@163.com>
 * @since  1.0
 */
class CmcConsoleUserController extends Controller
{
    /**
     * 同步框架服务控制台的用户模型(Http)至框架服务控制台的用户模型(Redis)、栏目人员配置模型(MySQL)
     *
     * @throws ServerErrorHttpException
     * @throws HttpException 如果登录超时
     * @throws InvalidArgumentException if the $direction or $sortFlag parameters do not have
     */
    public function actionSync()
    {
        // 设置同步标识的缓存键
        $redisCache = Yii::$app->redisCache;

        // HTTP 请求,获取开通有效服务的租户ID列表
        $httpCmcApiGroupIds = CmcApiGroupService::httpGetGroupIds();

        /* 判断 $httpCmcApiGroupIds 是否为空,如果为空,则成功退出 */
        if (empty($httpCmcApiGroupIds)) {
            // 延缓执行 60 * 60 秒
            // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyYesSleepTime']);

            return ExitCode::OK;
        }

        // 设置租户ID列表的缓存键
        $redisCacheGroupIdsKey = 'cmc_api_group_ids';

        // 从缓存中取回租户ID列表
        $redisCacheGroupIdsData = $redisCache[$redisCacheGroupIdsKey];

        // 是否设置租户ID列表的缓存,默认:否
        $isSetRedisCacheGroupIds = false;

        if ($redisCacheGroupIdsData === false) {
            $cmcApiGroupIds = [];
            foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                $cmcApiGroupIds[] = [
                    'group_id' => $httpCmcApiGroupId,
                    'cmc_console_user_last_synced_at' => 0, //上次同步时间
                ];
            }
            // 是否设置租户ID列表的缓存:是
            $isSetRedisCacheGroupIds = true;
        } else {
            // 获取 group_id 值列表
            $redisCacheGroupIds = ArrayHelper::getColumn($redisCacheGroupIdsData, 'group_id');
            $cmcApiGroupIds = $redisCacheGroupIdsData;
            foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                if (!in_array($httpCmcApiGroupId, $redisCacheGroupIds)) {
                    $cmcApiGroupIds[] = [
                        'group_id' => $httpCmcApiGroupId,
                        'cmc_console_user_last_synced_at' => 0, //上次同步时间
                    ];
                    // 是否设置租户ID列表的缓存:是
                    $isSetRedisCacheGroupIds = true;
                }
            }
        }

        // 判断是否设置租户ID列表的缓存
        if ($isSetRedisCacheGroupIds) {
            // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
            $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);
        }


        // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds
        $sortCmcApiGroupIds = $cmcApiGroupIds;
        ArrayHelper::multisort($sortCmcApiGroupIds, 'cmc_console_user_last_synced_at', SORT_ASC);

        foreach ($sortCmcApiGroupIds as $sortCmcApiGroupId) {
            $isLockExist = RedisCmcConsoleUser::isLockExist($sortCmcApiGroupId['group_id']);
            // 返回 true,表示锁定存在,即已经被其他客户端锁定
            if ($isLockExist === true) {
                continue;
            }

            // HTTP请求,获取租户ID下的用户列表
            $httpGetUserListData = [
                'groupId' => $sortCmcApiGroupId['group_id'],
                'loginId' => '',
                'loginTid' => '',
            ];
            $getUserListData = CmcConsoleUserService::httpGetUserList($httpGetUserListData);

            // 框架服务控制台的用户模型(Http),重建数组索引(以 ID 索引结果集)
            $httpCmcConsoleUserItems = ArrayHelper::index($getUserListData['list'], 'id');

            // 销毁变量
            unset($getUserListData['list']);

            // 基于租户ID查询框架服务控制台的用户模型(Redis)(以 ID 索引结果集)
            $redisCmcConsoleUserItems = RedisCmcConsoleUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->indexBy('id')->asArray()->all();

            // 使用键名比较计算数组的差集,如果不为空,则删除出现在 Redis 中但是未出现在 Http 中的记录
            $redisArrayDiffItems = array_diff_key($redisCmcConsoleUserItems, $httpCmcConsoleUserItems);
            // 销毁变量
            unset($redisCmcConsoleUserItems);
            if (!empty($redisArrayDiffItems)) {
                $redisArrayDiffIds = array_keys($redisArrayDiffItems);
                // 销毁变量
                unset($redisArrayDiffItems);
                if (RedisCmcConsoleUser::deleteAllByIds($sortCmcApiGroupId['group_id'], $redisArrayDiffIds) === false) {
                    continue;
                }
            }

            // 基于租户ID查询栏目人员配置模型(MySQL)(以 用户ID 索引结果集)
            $configColumnUserItems = ConfigColumnUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->isDeletedNo()->indexBy('user_id')->asArray()->all();

            // 使用键名比较计算数组的差集,如果不为空,则删除 (软删除) 出现在 栏目人员配置模型(MySQL) 中但是未出现在 框架服务控制台的用户模型(Http) 中的记录
            $diffItems = array_diff_key($configColumnUserItems, $httpCmcConsoleUserItems);

            // 销毁变量
            unset($configColumnUserItems);
            if (!empty($diffItems)) {
                // 基于租户ID、用户ID查询栏目人员配置模型(MySQL)(待删除)
                $toBeDeletedConfigColumnUserItems = ConfigColumnUser::find()->where([
                    'and',
                    ['group_id' => $sortCmcApiGroupId['group_id']],
                    ['in', 'user_id', $diffItems],
                ])->isDeletedNo()->all();
                foreach ($toBeDeletedConfigColumnUserItems as $toBeDeletedConfigColumnUserItem) {
                    /* @var $toBeDeletedConfigColumnUserItem ConfigColumnUser */
                    $toBeDeletedConfigColumnUserItem->softDelete();
                }
            }

            // 遍历框架服务控制台的用户模型(Http),判断在 Redis 中是否存在,如果不存在,则插入,如果存在且更新时间不相等,则更新
            foreach ($httpCmcConsoleUserItems as $httpCmcConsoleUserItemValue) {
                $redisCmcConsoleUserItem = RedisCmcConsoleUser::find()->where(['id' => $httpCmcConsoleUserItemValue['id']])->one();

                if (!isset($redisCmcConsoleUserItem)) {

                    $redisCmcConsoleUser = new RedisCmcConsoleUser();
                    $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                        $httpCmcConsoleUserItemValue);
                    $redisCmcConsoleUser->attributes = $attributes;
                    $redisCmcConsoleUser->insert();

                } else {
                    if ($httpCmcConsoleUserItemValue['update_time'] != $redisCmcConsoleUserItem['update_time']) {

                        $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                            $httpCmcConsoleUserItemValue);
                        $redisCmcConsoleUserItem->attributes = $attributes;
                        $redisCmcConsoleUserItem->save();

                    }
                }

            }

            // 从缓存中取回租户ID列表
            $cmcApiGroupIds = $redisCache[$redisCacheGroupIdsKey];
            // 设置当前租户的上次同步时间
            foreach ($cmcApiGroupIds as $cmcApiGroupIdKey => $cmcApiGroupId) {
                if ($cmcApiGroupId['group_id'] == $sortCmcApiGroupId['group_id']) {
                    $cmcApiGroupIds[$cmcApiGroupIdKey]['cmc_console_user_last_synced_at'] = time();
                    break;
                }
            }

            // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
            $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);

            // break 结束当前 foreach 结构的执行,即命令行的每一次运行,仅同步成功一个租户下的用户列表
            break;
        }

        // 延缓执行 60 秒
        // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyNoSleepTime']);

        // file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-usage-' . time() . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
        // file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-peak-usage-' . time() . '.txt', memory_get_peak_usage() / 1024 / 1024 . 'MB');

        return ExitCode::OK;
    }

}

</pre>
5、此时,当租户数量过多的时候,假设 100 个租户,平均 1 个租户的同步时间为 10 秒钟 + 下一个租户的同步的间隔时间 60 秒(包含了 bash sleep 60 秒),那么可能某个租户下的用户同步,其最大间隔时间就有可能为 7000 秒钟了。因此,如果支持了集群的部署,假设为 10 个容器,那么某个租户下的用户同步,其最大间隔时间就有可能为 700 秒钟了,同步的即时性提升了 10 倍。/mcloud/yii-cmc-console-user-sync.ini


[program:yii-cmc-console-user-sync]
command = bash -c 'sleep 60 && exec php /mcloud/www/pcs-api/yii cmc-console-user/sync'
autorestart = true
startsecs = 5
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-cmc-console-user-sync-stderr.log
stdout_logfile = /data/logs/yii-cmc-console-user-sync-stdout.log


6、编辑 /common/logics/redis/Lock.php,Redis模型的锁定实现
<pre class="wp-block-syntaxhighlighter-code">

<?php

namespace common\logics\redis;

use Yii;

/**
 * This is the model class for table "{{%lock}}".
 *
 * @author Qiang Wang <shuijingwanwq@163.com>
 * @since 1.0
 */
class Lock extends \yii\redis\ActiveRecord
{

    /**
     * Redis模型的锁定实现
     * @param string $lockKeyName 锁定键名
     * 格式如下:
     *
     * 'game_category' //锁定键名,如比赛分类
     *
     * @return bool 成功返回真/失败返回假
     * 格式如下:
     *
     * true //状态,获取锁定成功,可继续执行
     * 
     * 或者
     * 
     * false //状态,获取锁定失败,不可继续执行
     *
     */
    public function lock($lockKeyName)
    {
        // 设置锁定的过期时间,获取相关锁定参数
        $time = time();
        $lockKey = Yii::$app->params['redisLock']['keyPrefix'] . $lockKeyName;
        $lockExpire = $time + Yii::$app->params['redisLock']['timeOut'];
        // 获取 Redis 连接,以执行相关命令
        $redis = Yii::$app->redis;
        // 获取锁定
        $executeCommandResult = $redis->setnx($lockKey, $lockExpire);
        // 返回0,表示已经被其他客户端锁定
        if ($executeCommandResult == 0) {
            // 防止死锁,获取当前锁的过期时间
            $lockCurrentExpire = $redis->get($lockKey);
            // 判断锁是否过期,如果已经过期
            if ($time > $lockCurrentExpire) {
                // 防止并发锁定,检查存储在 key 的旧值是否仍然是过期的时间戳,如果是,则获取锁定,否则返回假
                $executeCommandResult = $redis->getset($lockKey, $lockExpire);
                if ($lockCurrentExpire != $executeCommandResult) {
                    return false;
                }
            }

            // 返回0,表示已经被其他客户端锁定,且不存在死锁,返回假
            if ($executeCommandResult == 0) {
                return false;
            }

        }
        
        return true;
    }
    
    /**
     * 判断Redis模型的锁定是否存在
     * @param string $lockKeyName 锁定键名
     * 格式如下:
     *
     * 'game_category' //锁定键名,如比赛分类
     *
     * @return bool 锁定是否存在
     * 格式如下:
     *
     * true //状态:已存在
     * 
     * 或者
     * 
     * false //状态:不存在
     *
     */
    public function isLockExist($lockKeyName)
    {
        // 获取相关锁定参数
        $time = time();
        $lockKey = Yii::$app->params['redisLock']['keyPrefix'] . $lockKeyName;
        // 获取 Redis 连接,以执行相关命令
        $redis = Yii::$app->redis;
        // 获取锁定
        $executeCommandResult = $redis->get($lockKey);

        // 返回NULL,表示不存在锁定,否则表示存在
        if ($executeCommandResult === null) {
            return false;
        } else {
            // 如果存在锁定,判断锁是否过期,如果已经过期,则仍然认定为不存在锁定
            if ($time > $executeCommandResult) {
                // 如果已经过期,则释放锁定
                $redis->del($lockKey);
                return false;
            }
            
        }

        return true;
    }

    /**
     * Redis模型的释放锁定实现
     * @param string $lockKeyName 锁定键名
     * 格式如下:
     *
     * 'game_category' //锁定键名,如比赛分类
     *
     * @return integer 被删除的keys的数量
     * 格式如下:
     *
     * 1 //被删除的keys的数量
     * 
     * 或者
     * 
     * 0 //被删除的keys的数量
     *
     */
    public function unlock($lockKeyName)
    {
        // 获取相关锁定参数
        $lockKey = Yii::$app->params['redisLock']['keyPrefix'] . $lockKeyName;
        // 获取 Redis 连接,以执行相关命令
        $redis = Yii::$app->redis;
        // 释放锁定
        return $redis->del($lockKey);
    }
}


</pre>
7、编辑 /console/controllers/CmcConsoleUserController.php,Redis模型的锁定实现。在开始一个租户下的用户同步之前,判断Redis模型的锁定是否存在,如果存在,则跳出当前循环,继续下一个租户的同步。在开始一个租户下的用户同步之前,Redis模型的获取锁定实现,如果获取锁定失败,则跳出当前循环,继续下一个租户的同步。一个租户下的用户同步成功之后,释放锁定。结束循环。
<pre class="wp-block-syntaxhighlighter-code">

<?php
/**
 * Created by PhpStorm.
 * User: Qiang Wang
 * Date: 2019/01/09
 * Time: 13:55
 */

namespace console\controllers;

use Yii;
use console\models\ConfigColumnUser;
use console\models\redis\cmc_console\User as RedisCmcConsoleUser;
use console\models\redis\Lock as RedisLock;
use console\services\CmcApiGroupService;
use console\services\CmcConsoleUserService;
use yii\base\InvalidArgumentException;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\helpers\ArrayHelper;
use yii\web\HttpException;
use yii\web\ServerErrorHttpException;

/**
 * 框架服务控制台的用户
 *
 * @author Qiang Wang <shuijingwanwq@163.com>
 * @since  1.0
 */
class CmcConsoleUserController extends Controller
{
    /**
     * 同步框架服务控制台的用户模型(Http)至框架服务控制台的用户模型(Redis)、栏目人员配置模型(MySQL)
     *
     * @throws ServerErrorHttpException
     * @throws HttpException 如果登录超时
     * @throws InvalidArgumentException if the $direction or $sortFlag parameters do not have
     */
    public function actionSync()
    {
        // 设置同步标识的缓存键
        $redisCache = Yii::$app->redisCache;

        // HTTP 请求,获取开通有效服务的租户ID列表
        $httpCmcApiGroupIds = CmcApiGroupService::httpGetGroupIds();

        /* 判断 $httpCmcApiGroupIds 是否为空,如果为空,则成功退出 */
        if (empty($httpCmcApiGroupIds)) {
            // 延缓执行 60 * 60 秒
            // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyYesSleepTime']);

            return ExitCode::OK;
        }

        // 设置租户ID列表的缓存键
        $redisCacheGroupIdsKey = 'cmc_api_group_ids';

        // 从缓存中取回租户ID列表
        $redisCacheGroupIdsData = $redisCache[$redisCacheGroupIdsKey];

        // 是否设置租户ID列表的缓存,默认:否
        $isSetRedisCacheGroupIds = false;

        if ($redisCacheGroupIdsData === false) {
            $cmcApiGroupIds = [];
            foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                $cmcApiGroupIds[] = [
                    'group_id' => $httpCmcApiGroupId,
                    'cmc_console_user_last_synced_at' => 0, //上次同步时间
                ];
            }
            // 是否设置租户ID列表的缓存:是
            $isSetRedisCacheGroupIds = true;
        } else {
            // 获取 group_id 值列表
            $redisCacheGroupIds = ArrayHelper::getColumn($redisCacheGroupIdsData, 'group_id');
            $cmcApiGroupIds = $redisCacheGroupIdsData;
            foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                if (!in_array($httpCmcApiGroupId, $redisCacheGroupIds)) {
                    $cmcApiGroupIds[] = [
                        'group_id' => $httpCmcApiGroupId,
                        'cmc_console_user_last_synced_at' => 0, //上次同步时间
                    ];
                    // 是否设置租户ID列表的缓存:是
                    $isSetRedisCacheGroupIds = true;
                }
            }
        }

        // 判断是否设置租户ID列表的缓存
        if ($isSetRedisCacheGroupIds) {
            // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
            $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);
        }


        // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds
        $sortCmcApiGroupIds = $cmcApiGroupIds;
        ArrayHelper::multisort($sortCmcApiGroupIds, 'cmc_console_user_last_synced_at', SORT_ASC);

        foreach ($sortCmcApiGroupIds as $sortCmcApiGroupId) {
            $isLockExist = RedisCmcConsoleUser::isLockExist($sortCmcApiGroupId['group_id']);
            // 返回 true,表示锁定存在,即已经被其他客户端锁定
            if ($isLockExist === true) {
                continue;
            }

            /* 判断Redis模型的锁定是否存在 */
            $redisLockKeyName = 'cmc_console_user:sync:' . implode(':', ['group_id' => $sortCmcApiGroupId['group_id']]);
            $redisLock = new RedisLock();
            $redisLockResult = $redisLock->isLockExist($redisLockKeyName);
            // 返回 true,表示锁定存在,即已经被其他客户端锁定
            if ($redisLockResult === true) {
                continue;
            }

            /* Redis模型的锁定实现 */
            $redisLockResult = $redisLock->lock($redisLockKeyName);
            // 返回 false,表示已经被其他客户端锁定
            if ($redisLockResult === false) {
                continue;
            }

            // HTTP请求,获取租户ID下的用户列表
            $httpGetUserListData = [
                'groupId' => $sortCmcApiGroupId['group_id'],
                'loginId' => '',
                'loginTid' => '',
            ];
            $getUserListData = CmcConsoleUserService::httpGetUserList($httpGetUserListData);

            // 框架服务控制台的用户模型(Http),重建数组索引(以 ID 索引结果集)
            $httpCmcConsoleUserItems = ArrayHelper::index($getUserListData['list'], 'id');

            // 销毁变量
            unset($getUserListData['list']);

            // 基于租户ID查询框架服务控制台的用户模型(Redis)(以 ID 索引结果集)
            $redisCmcConsoleUserItems = RedisCmcConsoleUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->indexBy('id')->asArray()->all();

            // 使用键名比较计算数组的差集,如果不为空,则删除出现在 Redis 中但是未出现在 Http 中的记录
            $redisArrayDiffItems = array_diff_key($redisCmcConsoleUserItems, $httpCmcConsoleUserItems);
            // 销毁变量
            unset($redisCmcConsoleUserItems);
            if (!empty($redisArrayDiffItems)) {
                $redisArrayDiffIds = array_keys($redisArrayDiffItems);
                // 销毁变量
                unset($redisArrayDiffItems);
                if (RedisCmcConsoleUser::deleteAllByIds($sortCmcApiGroupId['group_id'], $redisArrayDiffIds) === false) {
                    continue;
                }
            }

            // 基于租户ID查询栏目人员配置模型(MySQL)(以 用户ID 索引结果集)
            $configColumnUserItems = ConfigColumnUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->isDeletedNo()->indexBy('user_id')->asArray()->all();

            // 使用键名比较计算数组的差集,如果不为空,则删除 (软删除) 出现在 栏目人员配置模型(MySQL) 中但是未出现在 框架服务控制台的用户模型(Http) 中的记录
            $diffItems = array_diff_key($configColumnUserItems, $httpCmcConsoleUserItems);

            // 销毁变量
            unset($configColumnUserItems);
            if (!empty($diffItems)) {
                // 基于租户ID、用户ID查询栏目人员配置模型(MySQL)(待删除)
                $toBeDeletedConfigColumnUserItems = ConfigColumnUser::find()->where([
                    'and',
                    ['group_id' => $sortCmcApiGroupId['group_id']],
                    ['in', 'user_id', $diffItems],
                ])->isDeletedNo()->all();
                foreach ($toBeDeletedConfigColumnUserItems as $toBeDeletedConfigColumnUserItem) {
                    /* @var $toBeDeletedConfigColumnUserItem ConfigColumnUser */
                    $toBeDeletedConfigColumnUserItem->softDelete();
                }
            }

            // 遍历框架服务控制台的用户模型(Http),判断在 Redis 中是否存在,如果不存在,则插入,如果存在且更新时间不相等,则更新
            foreach ($httpCmcConsoleUserItems as $httpCmcConsoleUserItemValue) {
                $redisCmcConsoleUserItem = RedisCmcConsoleUser::find()->where(['id' => $httpCmcConsoleUserItemValue['id']])->one();

                if (!isset($redisCmcConsoleUserItem)) {

                    $redisCmcConsoleUser = new RedisCmcConsoleUser();
                    $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                        $httpCmcConsoleUserItemValue);
                    $redisCmcConsoleUser->attributes = $attributes;
                    $redisCmcConsoleUser->insert();

                } else {
                    if ($httpCmcConsoleUserItemValue['update_time'] != $redisCmcConsoleUserItem['update_time']) {

                        $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                            $httpCmcConsoleUserItemValue);
                        $redisCmcConsoleUserItem->attributes = $attributes;
                        $redisCmcConsoleUserItem->save();

                    }
                }

            }

            // 从缓存中取回租户ID列表
            $cmcApiGroupIds = $redisCache[$redisCacheGroupIdsKey];
            // 设置当前租户的上次同步时间
            foreach ($cmcApiGroupIds as $cmcApiGroupIdKey => $cmcApiGroupId) {
                if ($cmcApiGroupId['group_id'] == $sortCmcApiGroupId['group_id']) {
                    $cmcApiGroupIds[$cmcApiGroupIdKey]['cmc_console_user_last_synced_at'] = time();
                    break;
                }
            }

            // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
            $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);

            // 释放锁定
            $redisLock->unlock($redisLockKeyName);

            // break 结束当前 foreach 结构的执行,即命令行的每一次运行,仅同步成功一个租户下的用户列表
            break;
        }

        // 延缓执行 60 秒
        // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyNoSleepTime']);

        // file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-usage-' . time() . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
        // file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-peak-usage-' . time() . '.txt', memory_get_peak_usage() / 1024 / 1024 . 'MB');

        return ExitCode::OK;
    }

}

</pre>
8、当一个租户下的用户同步时间长度小于等于 Redis锁定超时时间 的值 10秒时(sleep(8),实际长度超过了 8 秒),此时,此租户已经处于锁定状态,进而导致部署为集群时,可以保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。在本地环境中,模拟了 3 个容器,3 个容器开始同步时间:1582784312、1582784315、1582784318,分别间隔 3 秒、3秒,皆小于 10 秒。第 1 个容器同步了租户:c10e87f39873512a16727e17f57456a5,第 2 个容器同步了租户:015ce30b116ce86058fa6ab4fea4ac63,第 3 个容器同步了租户:4fd58ceba1fbc537b5402302702131eb。3 个容器的并行执行,分别同步了 3 个租户,符合预期。文件生成顺序体现出了执行执行流程。如图1
当一个租户下的用户同步时间长度小于等于 Redis锁定超时时间 的值 10秒时(sleep(8),实际长度超过了 8 秒),此时,此租户已经处于锁定状态,进而导致部署为集群时,可以保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。在本地环境中,模拟了 3 个容器,3 个容器开始同步时间:1582784312、1582784315、1582784318,分别间隔 3 秒、3秒,皆小于 10 秒。第 1 个容器同步了租户:c10e87f39873512a16727e17f57456a5,第 2 个容器同步了租户:015ce30b116ce86058fa6ab4fea4ac63,第 3 个容器同步了租户:4fd58ceba1fbc537b5402302702131eb。3 个容器的并行执行,分别同步了 3 个租户,符合预期。文件生成顺序体现出了执行执行流程。

图1



    'redisLock' => [
        'keyPrefix' => 'pa:lock:', //Redis锁定 key 前缀
        'timeOut' => 10, //Redis锁定超时时间,单位为秒
    ],




            file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-' . $sortCmcApiGroupId['group_id'] . '-' . time() . '.txt', $sortCmcApiGroupId['group_id']);

            /* 判断Redis模型的锁定是否存在 */
            $redisLockKeyName = 'cmc_console_user:sync:' . implode(':', ['group_id' => $sortCmcApiGroupId['group_id']]);
            $redisLock = new RedisLock();
            $isLockExist = $redisLock->isLockExist($redisLockKeyName);
            // 返回 true,表示锁定存在,即已经被其他客户端锁定
            if ($isLockExist === true) {
                file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-is-lock-exist-' . $sortCmcApiGroupId['group_id'] . '-' . time() . '.txt', $sortCmcApiGroupId['group_id']);
                continue;
            }

            /* Redis模型的锁定实现 */
            $lock = $redisLock->lock($redisLockKeyName);
            // 返回 false,表示已经被其他客户端锁定
            if ($lock === false) {
                file_put_contents('E:/wwwroot/pcs-api/console/runtime/logs/cmc-console-user-sync-lock-' . $sortCmcApiGroupId['group_id'] . '-' . time() . '.txt', $sortCmcApiGroupId['group_id']);
                continue;
            }

            // HTTP请求,获取租户ID下的用户列表
            $httpGetUserListData = [
                'groupId' => $sortCmcApiGroupId['group_id'],
                'loginId' => '',
                'loginTid' => '',
            ];
            $getUserListData = CmcConsoleUserService::httpGetUserList($httpGetUserListData);

            // 框架服务控制台的用户模型(Http),重建数组索引(以 ID 索引结果集)
            $httpCmcConsoleUserItems = ArrayHelper::index($getUserListData['list'], 'id');

            // 销毁变量
            unset($getUserListData['list']);

            // 基于租户ID查询框架服务控制台的用户模型(Redis)(以 ID 索引结果集)
            $redisCmcConsoleUserItems = RedisCmcConsoleUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->indexBy('id')->asArray()->all();

            // 使用键名比较计算数组的差集,如果不为空,则删除出现在 Redis 中但是未出现在 Http 中的记录
            $redisArrayDiffItems = array_diff_key($redisCmcConsoleUserItems, $httpCmcConsoleUserItems);
            // 销毁变量
            unset($redisCmcConsoleUserItems);
            if (!empty($redisArrayDiffItems)) {
                $redisArrayDiffIds = array_keys($redisArrayDiffItems);
                // 销毁变量
                unset($redisArrayDiffItems);
                if (RedisCmcConsoleUser::deleteAllByIds($sortCmcApiGroupId['group_id'], $redisArrayDiffIds) === false) {
                    continue;
                }
            }

            // 基于租户ID查询栏目人员配置模型(MySQL)(以 用户ID 索引结果集)
            $configColumnUserItems = ConfigColumnUser::find()->where(['group_id' => $sortCmcApiGroupId['group_id']])->isDeletedNo()->indexBy('user_id')->asArray()->all();

            // 使用键名比较计算数组的差集,如果不为空,则删除 (软删除) 出现在 栏目人员配置模型(MySQL) 中但是未出现在 框架服务控制台的用户模型(Http) 中的记录
            $diffItems = array_diff_key($configColumnUserItems, $httpCmcConsoleUserItems);

            // 销毁变量
            unset($configColumnUserItems);
            if (!empty($diffItems)) {
                // 基于租户ID、用户ID查询栏目人员配置模型(MySQL)(待删除)
                $toBeDeletedConfigColumnUserItems = ConfigColumnUser::find()->where([
                    'and',
                    ['group_id' => $sortCmcApiGroupId['group_id']],
                    ['in', 'user_id', $diffItems],
                ])->isDeletedNo()->all();
                foreach ($toBeDeletedConfigColumnUserItems as $toBeDeletedConfigColumnUserItem) {
                    /* @var $toBeDeletedConfigColumnUserItem ConfigColumnUser */
                    $toBeDeletedConfigColumnUserItem->softDelete();
                }
            }

            // 遍历框架服务控制台的用户模型(Http),判断在 Redis 中是否存在,如果不存在,则插入,如果存在且更新时间不相等,则更新
            foreach ($httpCmcConsoleUserItems as $httpCmcConsoleUserItemValue) {
                $redisCmcConsoleUserItem = RedisCmcConsoleUser::find()->where(['id' => $httpCmcConsoleUserItemValue['id']])->one();

                if (!isset($redisCmcConsoleUserItem)) {

                    $redisCmcConsoleUser = new RedisCmcConsoleUser();
                    $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                        $httpCmcConsoleUserItemValue);
                    $redisCmcConsoleUser->attributes = $attributes;
                    $redisCmcConsoleUser->insert();

                } else {
                    if ($httpCmcConsoleUserItemValue['update_time'] != $redisCmcConsoleUserItem['update_time']) {

                        $attributes = $this->getAttributes($getUserListData['group_info']['group_id'],
                            $httpCmcConsoleUserItemValue);
                        $redisCmcConsoleUserItem->attributes = $attributes;
                        $redisCmcConsoleUserItem->save();

                    }
                }

            }

            // 从缓存中取回租户ID列表
            $cmcApiGroupIds = $redisCache[$redisCacheGroupIdsKey];
            // 设置当前租户的上次同步时间
            foreach ($cmcApiGroupIds as $cmcApiGroupIdKey => $cmcApiGroupId) {
                if ($cmcApiGroupId['group_id'] == $sortCmcApiGroupId['group_id']) {
                    $cmcApiGroupIds[$cmcApiGroupIdKey]['cmc_console_user_last_synced_at'] = time();
                    break;
                }
            }

            // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
            sleep(8);
            $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);

            // 释放锁定
            $redisLock->unlock($redisLockKeyName);


9、打印出从缓存中取回租户ID列表,已经成功同步了 3 个租户:c10e87f39873512a16727e17f57456a5、015ce30b116ce86058fa6ab4fea4ac63、4fd58ceba1fbc537b5402302702131eb,但是,仅有租户 4fd58ceba1fbc537b5402302702131eb 的同步时间得到了更新。虽然在实际的生产环境中,不存在 sleep(8),此为在本地环境模拟并发请求,而添加的。但是,理论上来说,从缓存中取回租户ID列表,到 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留 的间隔时间段(假设租户数量为 10000 个,则遍历 10000 次,时间间隔为 30000 微秒,即 0.03 秒)内,如果存在 多个容器 在运行的话,便会出现相互覆盖的情况。暂时不做处理,即时出现了相互覆盖的情况,其后果是可以接受的。租户:c10e87f39873512a16727e17f57456a5、015ce30b116ce86058fa6ab4fea4ac63 会相继立即重复同步一次。如图2
打印出从缓存中取回租户ID列表,已经成功同步了 3 个租户:c10e87f39873512a16727e17f57456a5、015ce30b116ce86058fa6ab4fea4ac63、4fd58ceba1fbc537b5402302702131eb,但是,仅有租户 4fd58ceba1fbc537b5402302702131eb 的同步时间得到了更新。虽然在实际的生产环境中,不存在 sleep(8),此为在本地环境模拟并发请求,而添加的。但是,理论上来说,从缓存中取回租户ID列表,到 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留 的间隔时间段(假设租户数量为 10000 个,则遍历 10000 次,时间间隔为 30000 微秒,即 0.03 秒)内,如果存在 多个容器 在运行的话,便会出现相互覆盖的情况。暂时不做处理,即时出现了相互覆盖的情况,其后果是可以接受的。租户:c10e87f39873512a16727e17f57456a5、015ce30b116ce86058fa6ab4fea4ac63 会相继立即重复同步一次。

图2



Array
(
    [0] => Array
        (
            [group_id] => c10e87f39873512a16727e17f57456a5
            [cmc_console_user_last_synced_at] => 0
        )

    [1] => Array
        (
            [group_id] => 015ce30b116ce86058fa6ab4fea4ac63
            [cmc_console_user_last_synced_at] => 0
        )

    [2] => Array
        (
            [group_id] => 4fd58ceba1fbc537b5402302702131eb
            [cmc_console_user_last_synced_at] => 1582784318
        )

    [3] => Array
        (
            [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e
            [cmc_console_user_last_synced_at] => 0
        )

)


10、当一个租户下的用户同步时间长度超过 Redis锁定超时时间 的值 10秒后(sleep(60),实际长度超过了 60 秒),此时,此租户已经被自动解除锁定,进而导致部署为集群时,无法保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。在本地环境中,模拟了 3 个容器,3 个容器开始同步时间:1582773882、1582773895、1582773917,分别间隔 13 秒、22秒,皆大于 10 秒。第 3 个容器开始同步时,第 1 个容器仍然在同步中,并未结束(释放锁定)。因此,租户:c10e87f39873512a16727e17f57456a5 被 3 个容器同时同步。如图3
当一个租户下的用户同步时间长度超过 Redis锁定超时时间 的值 10秒后(sleep(60),实际长度超过了 60 秒),此时,此租户已经被自动解除锁定,进而导致部署为集群时,无法保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。在本地环境中,模拟了 3 个容器,3 个容器开始同步时间:1582773882、1582773895、1582773917,分别间隔 13 秒、22秒,皆大于 10 秒。第 3 个容器开始同步时,第 1 个容器仍然在同步中,并未结束(释放锁定)。因此,租户:c10e87f39873512a16727e17f57456a5 被 3 个容器同时同步。

图3




            // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
            sleep(60);
            $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);

            // 释放锁定
            $redisLock->unlock($redisLockKeyName);


11、编辑 /common/logics/redis/Lock.php。public function lock($lockKeyName),添加一个参数,支持自定义Redis锁定超时时间。以基于场景灵活设置。


    /**
     * Redis模型的锁定实现
     * @param string $lockKeyName 锁定键名
     * 格式如下:
     *
     * 'game_category' //锁定键名,如比赛分类
     *
     * @param int $timeOut Redis锁定超时时间,单位为秒
     * 格式如下:3
     *
     * @return bool 成功返回真/失败返回假
     * 格式如下:
     *
     * true //状态,获取锁定成功,可继续执行
     * 
     * 或者
     * 
     * false //状态,获取锁定失败,不可继续执行
     *
     */
    public function lock($lockKeyName, $timeOut = 3)
    {
        // 设置锁定的过期时间,获取相关锁定参数
        $time = time();
        $lockKey = Yii::$app->params['redisLock']['keyPrefix'] . $lockKeyName;
        $lockExpire = $time + $timeOut;
        // 获取 Redis 连接,以执行相关命令
        $redis = Yii::$app->redis;
        // 获取锁定
        $executeCommandResult = $redis->setnx($lockKey, $lockExpire);
        // 返回0,表示已经被其他客户端锁定
        if ($executeCommandResult == 0) {
            // 防止死锁,获取当前锁的过期时间
            $lockCurrentExpire = $redis->get($lockKey);
            // 判断锁是否过期,如果已经过期
            if ($time > $lockCurrentExpire) {
                // 防止并发锁定,检查存储在 key 的旧值是否仍然是过期的时间戳,如果是,则获取锁定,否则返回假
                $executeCommandResult = $redis->getset($lockKey, $lockExpire);
                if ($lockCurrentExpire != $executeCommandResult) {
                    return false;
                }
            }

            // 返回0,表示已经被其他客户端锁定,且不存在死锁,返回假
            if ($executeCommandResult == 0) {
                return false;
            }

        }
        
        return true;
    }


12、Redis模型的锁定实现,Redis锁定超时时间,单位为秒(自定义 600)。一个租户下的用户同步,耗费的最长时间只要不超过 10 分钟,当部署为集群时,就可以保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。


            /* Redis模型的锁定实现 */
            $lock = $redisLock->lock($redisLockKeyName, 600);


13、在开发环境,部署了 3 个容器。以便于测试集群部署时,并发锁定的情况。在 bash sleep 60 秒 的情况下,很难出现并发锁定的情况。不过,当部署为集群时,已经可以保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。当出现租户下的用户同步及时性不够理想的情况,可以通过添加容器的方案提升同步的及时性。在 3 个容器中,总计同步了 30 + 30 + 33 = 93 次。其中 租户 f53df1a8d46108afc8ae9eeb3f0e1f0e、c10e87f39873512a16727e17f57456a5、4fd58ceba1fbc537b5402302702131eb、015ce30b116ce86058fa6ab4fea4ac63 分别同步了:23、24、22、24 次。3 个容器的情况下,在 15:43 至 16:16 的时间段内(32 分钟左右),一个租户的同步次数平均为:23 次。实际测试结果,一个租户的同步时间间隔为:32 * 60 / 24 = 80 秒。理论上的计算公式,一个租户的同步时间间隔为:租户数量 / 容器数量 * 60,结果单位为秒。同步性能符合设计预期。如图4
在开发环境,部署了 3 个容器。以便于测试集群部署时,并发锁定的情况。在 bash sleep 60 秒 的情况下,很难出现并发锁定的情况。不过,当部署为集群时,已经可以保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。当出现租户下的用户同步及时性不够理想的情况,可以通过添加容器的方案提升同步的及时性。在 3 个容器中,总计同步了 30 + 30 + 33 = 93 次。其中 租户 f53df1a8d46108afc8ae9eeb3f0e1f0e、c10e87f39873512a16727e17f57456a5、4fd58ceba1fbc537b5402302702131eb、015ce30b116ce86058fa6ab4fea4ac63 分别同步了:23、24、22、24 次。3 个容器的情况下,在 15:43 至 16:16 的时间段内(32 分钟左右),一个租户的同步次数平均为:23 次。实际测试结果,一个租户的同步时间间隔为:32 * 60 / 24 = 80 秒。理论上的计算公式,一个租户的同步时间间隔为:租户数量 / 容器数量 * 60,结果单位为秒。同步性能符合设计预期。

图4



[root@1d03b809a523 logs]# ls -ltr
total 120
-rw-r--r-- 1 root root 32 Feb 28 15:43 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582875818.txt
-rw-r--r-- 1 root root 32 Feb 28 15:44 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582875879.txt
-rw-r--r-- 1 root root 32 Feb 28 15:45 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582875941.txt
-rw-r--r-- 1 root root 32 Feb 28 15:49 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876192.txt
-rw-r--r-- 1 root root 32 Feb 28 15:50 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876254.txt
-rw-r--r-- 1 root root 32 Feb 28 15:51 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876315.txt
-rw-r--r-- 1 root root 32 Feb 28 15:52 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876377.txt
-rw-r--r-- 1 root root 32 Feb 28 15:53 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876438.txt
-rw-r--r-- 1 root root 32 Feb 28 15:55 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876500.txt
-rw-r--r-- 1 root root 32 Feb 28 15:56 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876561.txt
-rw-r--r-- 1 root root 32 Feb 28 15:57 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876623.txt
-rw-r--r-- 1 root root 32 Feb 28 15:58 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876684.txt
-rw-r--r-- 1 root root 32 Feb 28 15:59 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876746.txt
-rw-r--r-- 1 root root 32 Feb 28 16:00 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876808.txt
-rw-r--r-- 1 root root 32 Feb 28 16:01 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876869.txt
-rw-r--r-- 1 root root 32 Feb 28 16:02 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876931.txt
-rw-r--r-- 1 root root 32 Feb 28 16:03 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876992.txt
-rw-r--r-- 1 root root 32 Feb 28 16:04 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877054.txt
-rw-r--r-- 1 root root 32 Feb 28 16:05 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877115.txt
-rw-r--r-- 1 root root 32 Feb 28 16:06 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877177.txt
-rw-r--r-- 1 root root 32 Feb 28 16:07 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877238.txt
-rw-r--r-- 1 root root 32 Feb 28 16:08 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877300.txt
-rw-r--r-- 1 root root 32 Feb 28 16:09 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877362.txt
-rw-r--r-- 1 root root 32 Feb 28 16:10 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877423.txt
-rw-r--r-- 1 root root 32 Feb 28 16:11 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877485.txt
-rw-r--r-- 1 root root 32 Feb 28 16:12 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877546.txt
-rw-r--r-- 1 root root 32 Feb 28 16:13 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877608.txt
-rw-r--r-- 1 root root 32 Feb 28 16:14 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877669.txt
-rw-r--r-- 1 root root 32 Feb 28 16:15 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877731.txt
-rw-r--r-- 1 root root 32 Feb 28 16:16 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877792.txt

[root@7e1ea0bc777c logs]# ls -ltr
total 120
-rw-r--r-- 1 root root 32 Feb 28 15:43 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582875803.txt
-rw-r--r-- 1 root root 32 Feb 28 15:44 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582875864.txt
-rw-r--r-- 1 root root 32 Feb 28 15:45 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582875926.txt
-rw-r--r-- 1 root root 32 Feb 28 15:49 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876189.txt
-rw-r--r-- 1 root root 32 Feb 28 15:50 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876251.txt
-rw-r--r-- 1 root root 32 Feb 28 15:51 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876313.txt
-rw-r--r-- 1 root root 32 Feb 28 15:52 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876374.txt
-rw-r--r-- 1 root root 32 Feb 28 15:53 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876436.txt
-rw-r--r-- 1 root root 32 Feb 28 15:54 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876497.txt
-rw-r--r-- 1 root root 32 Feb 28 15:55 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876559.txt
-rw-r--r-- 1 root root 32 Feb 28 15:57 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876621.txt
-rw-r--r-- 1 root root 32 Feb 28 15:58 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876682.txt
-rw-r--r-- 1 root root 32 Feb 28 15:59 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876744.txt
-rw-r--r-- 1 root root 32 Feb 28 16:00 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876805.txt
-rw-r--r-- 1 root root 32 Feb 28 16:01 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876867.txt
-rw-r--r-- 1 root root 32 Feb 28 16:02 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876929.txt
-rw-r--r-- 1 root root 32 Feb 28 16:03 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876990.txt
-rw-r--r-- 1 root root 32 Feb 28 16:04 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877052.txt
-rw-r--r-- 1 root root 32 Feb 28 16:05 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877113.txt
-rw-r--r-- 1 root root 32 Feb 28 16:06 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877175.txt
-rw-r--r-- 1 root root 32 Feb 28 16:07 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877237.txt
-rw-r--r-- 1 root root 32 Feb 28 16:08 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877298.txt
-rw-r--r-- 1 root root 32 Feb 28 16:09 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877360.txt
-rw-r--r-- 1 root root 32 Feb 28 16:10 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877422.txt
-rw-r--r-- 1 root root 32 Feb 28 16:11 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877483.txt
-rw-r--r-- 1 root root 32 Feb 28 16:12 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877545.txt
-rw-r--r-- 1 root root 32 Feb 28 16:13 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877606.txt
-rw-r--r-- 1 root root 32 Feb 28 16:14 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877668.txt
-rw-r--r-- 1 root root 32 Feb 28 16:15 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877730.txt
-rw-r--r-- 1 root root 32 Feb 28 16:16 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877791.txt

[root@16fa59fcd4e0 logs]# ls -ltr
total 144
-rw-r--r-- 1 root root 8845 Feb 28 15:43 app.log
-rw-r--r-- 1 root root   32 Feb 28 15:44 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582875846.txt
-rw-r--r-- 1 root root   32 Feb 28 15:45 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582875908.txt
-rw-r--r-- 1 root root   32 Feb 28 15:46 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582875969.txt
-rw-r--r-- 1 root root   32 Feb 28 15:47 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876031.txt
-rw-r--r-- 1 root root   32 Feb 28 15:48 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876093.txt
-rw-r--r-- 1 root root   32 Feb 28 15:49 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876155.txt
-rw-r--r-- 1 root root   32 Feb 28 15:50 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876218.txt
-rw-r--r-- 1 root root   32 Feb 28 15:51 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876279.txt
-rw-r--r-- 1 root root   32 Feb 28 15:52 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876341.txt
-rw-r--r-- 1 root root   32 Feb 28 15:53 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876403.txt
-rw-r--r-- 1 root root   32 Feb 28 15:54 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876465.txt
-rw-r--r-- 1 root root   32 Feb 28 15:55 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876526.txt
-rw-r--r-- 1 root root   32 Feb 28 15:56 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876588.txt
-rw-r--r-- 1 root root   32 Feb 28 15:57 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876650.txt
-rw-r--r-- 1 root root   32 Feb 28 15:58 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876712.txt
-rw-r--r-- 1 root root   32 Feb 28 15:59 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582876773.txt
-rw-r--r-- 1 root root   32 Feb 28 16:00 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582876835.txt
-rw-r--r-- 1 root root   32 Feb 28 16:01 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582876897.txt
-rw-r--r-- 1 root root   32 Feb 28 16:02 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582876959.txt
-rw-r--r-- 1 root root   32 Feb 28 16:03 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877020.txt
-rw-r--r-- 1 root root   32 Feb 28 16:04 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877082.txt
-rw-r--r-- 1 root root   32 Feb 28 16:05 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877144.txt
-rw-r--r-- 1 root root   32 Feb 28 16:06 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877206.txt
-rw-r--r-- 1 root root   32 Feb 28 16:07 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877268.txt
-rw-r--r-- 1 root root   32 Feb 28 16:08 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877330.txt
-rw-r--r-- 1 root root   32 Feb 28 16:09 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877392.txt
-rw-r--r-- 1 root root   32 Feb 28 16:10 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877454.txt
-rw-r--r-- 1 root root   32 Feb 28 16:11 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877515.txt
-rw-r--r-- 1 root root   32 Feb 28 16:12 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877577.txt
-rw-r--r-- 1 root root   32 Feb 28 16:13 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1582877639.txt
-rw-r--r-- 1 root root   32 Feb 28 16:15 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1582877701.txt
-rw-r--r-- 1 root root   32 Feb 28 16:16 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1582877763.txt
-rw-r--r-- 1 root root   32 Feb 28 16:17 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1582877825.txt


14、在 bash sleep 60 秒 的情况下,很难出现并发锁定的情况。或者增加更多的容器,或者提升执行命令行的频率。设置 bash sleep 10 秒。已经出现并发锁定(防止同一个租户下的用户同步同时在多个容器中执行)的情况。当部署为集群时,已经可以保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。理论上的计算公式,一个租户的同步时间间隔为:4 / 3 * 10 = 13,结果单位为秒。符合设计预期。总结:部署的容器数量不要超过租户的数量,以防止并发锁定的情况过于频繁。如图5
在 bash sleep 60 秒 的情况下,很难出现并发锁定的情况。或者增加更多的容器,或者提升执行命令行的频率。设置 bash sleep 10 秒。已经出现并发锁定(防止同一个租户下的用户同步同时在多个容器中执行)的情况。当部署为集群时,已经可以保证同一个租户下的用户同步,在某一时间段,仅在 1 个容器中执行。理论上的计算公式,一个租户的同步时间间隔为:4 / 3 * 10 = 13,结果单位为秒。符合设计预期。总结:部署的容器数量不要超过租户的数量,以防止并发锁定的情况过于频繁。

图5



[root@0f5a081a481a logs]# ls -ltr
total 320
-rw-r--r-- 1 root root 8845 Mar  2 09:50 app.log
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113813.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113825.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113836.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113848.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113860.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113871.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113883.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113894.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113906.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113917.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113929.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113940.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113952.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113964.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113975.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113987.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113998.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114010.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114021.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114033.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114044.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114056.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114067.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114079.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114091.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114102.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114114.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114125.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114137.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114148.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114160.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114171.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-is-lock-exist-c10e87f39873512a16727e17f57456a5-1583114171.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114171.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114183.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-is-lock-exist-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114183.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114183.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114194.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114206.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114218.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114228.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114240.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114252.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114263.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114275.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114286.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114298.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114309.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114321.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114332.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114344.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114355.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-is-lock-exist-4fd58ceba1fbc537b5402302702131eb-1583114355.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114355.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114367.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-is-lock-exist-c10e87f39873512a16727e17f57456a5-1583114367.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114367.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114379.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114390.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114402.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114413.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114425.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114436.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114448.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114460.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114471.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114483.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114494.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114506.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114517.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114529.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114540.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114552.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114563.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114575.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114587.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114598.txt

[root@88f433b87315 logs]# ls -ltr
total 344
-rw-r--r-- 1 root root 9092 Mar  2 09:49 app.log
-rw-r--r-- 1 root root   32 Mar  2 09:49 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113764.txt
-rw-r--r-- 1 root root   32 Mar  2 09:49 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113776.txt
-rw-r--r-- 1 root root   32 Mar  2 09:49 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113788.txt
-rw-r--r-- 1 root root   32 Mar  2 09:49 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113799.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113811.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113823.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113834.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113846.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113857.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113869.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113881.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113892.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113904.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113915.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113927.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113939.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113950.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113962.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113973.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113985.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113997.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114008.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114020.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114032.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114043.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114055.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114066.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114078.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114090.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114101.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114113.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114125.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114136.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114148.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114159.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114171.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114183.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114195.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-is-lock-exist-4fd58ceba1fbc537b5402302702131eb-1583114195.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114195.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114206.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-is-lock-exist-015ce30b116ce86058fa6ab4fea4ac63-1583114206.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114206.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114218.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114229.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114241.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114253.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114264.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114276.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114288.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114299.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114311.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114322.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114334.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114346.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114357.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114369.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114380.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114392.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114404.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114415.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114427.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114439.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114450.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114462.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114474.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-is-lock-exist-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114474.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114474.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114485.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-is-lock-exist-015ce30b116ce86058fa6ab4fea4ac63-1583114485.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114485.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114497.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-is-lock-exist-4fd58ceba1fbc537b5402302702131eb-1583114497.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114497.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114509.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114520.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114532.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114543.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114555.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114567.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114578.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114590.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114601.txt

[root@c2e084f1424c logs]# ls -ltr
total 308
-rw-r--r-- 1 root root 8845 Mar  2 09:49 app.log
-rw-r--r-- 1 root root   32 Mar  2 09:49 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113778.txt
-rw-r--r-- 1 root root   32 Mar  2 09:49 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113790.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113802.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113814.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113825.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113837.txt
-rw-r--r-- 1 root root   32 Mar  2 09:50 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113849.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113861.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113873.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113884.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113896.txt
-rw-r--r-- 1 root root   32 Mar  2 09:51 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113908.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113920.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113932.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113943.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583113955.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583113967.txt
-rw-r--r-- 1 root root   32 Mar  2 09:52 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583113979.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583113990.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114002.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114014.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114026.txt
-rw-r--r-- 1 root root   32 Mar  2 09:53 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114037.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114049.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114061.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114073.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114085.txt
-rw-r--r-- 1 root root   32 Mar  2 09:54 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114096.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114108.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114120.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114132.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114143.txt
-rw-r--r-- 1 root root   32 Mar  2 09:55 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114155.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114167.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114179.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114191.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114202.txt
-rw-r--r-- 1 root root   32 Mar  2 09:56 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114214.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114226.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114238.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114249.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114261.txt
-rw-r--r-- 1 root root   32 Mar  2 09:57 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114273.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114285.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114296.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114308.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114320.txt
-rw-r--r-- 1 root root   32 Mar  2 09:58 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114332.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114343.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114355.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114367.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114379.txt
-rw-r--r-- 1 root root   32 Mar  2 09:59 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114391.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114403.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114414.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114426.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114438.txt
-rw-r--r-- 1 root root   32 Mar  2 10:00 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114450.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114461.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114473.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114485.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114497.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114509.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-is-lock-exist-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114509.txt
-rw-r--r-- 1 root root   32 Mar  2 10:01 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114509.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114520.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114532.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114544.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114556.txt
-rw-r--r-- 1 root root   32 Mar  2 10:02 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114568.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-c10e87f39873512a16727e17f57456a5-1583114580.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-f53df1a8d46108afc8ae9eeb3f0e1f0e-1583114592.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-4fd58ceba1fbc537b5402302702131eb-1583114603.txt
-rw-r--r-- 1 root root   32 Mar  2 10:03 cmc-console-user-sync-015ce30b116ce86058fa6ab4fea4ac63-1583114615.txt


15、总结: (1) 理论上的计算公式,一个租户的同步时间间隔为:租户数量 / 容器数量 * 60,结果单位为秒。 (2) 部署的容器数量不要超过租户的数量,以防止并发锁定的情况过于频繁,且必要性不大(当容器数量等于租户数量时,同步时间间隔为:60 秒)。]]>
https://www.shuijingwanwq.com/2020/03/02/3964/feed/ 0
Docker 部署,基于 Supervisor 的 crontab (bash sleep) 的实现,以降低内存占用 https://www.shuijingwanwq.com/2019/10/12/3555/ https://www.shuijingwanwq.com/2019/10/12/3555/#respond Sat, 12 Oct 2019 07:36:38 +0000 http://www.shuijingwanwq.com/?p=3555 浏览量: 219 1、命令行脚本的运行基于 Supervisor 提供支持,持续运行期间,占用内存过高的问题仍然未得到根本解决,查看网址:https://www.shuijingwanwq.com/2019/07/24/3376/ 2、查看网址:https://stackoverflow.com/questions/27341846/using-supervisor-as-cron ,通过调用 bash sleep 命令,实现间隔一定时间执行命令行脚本,避免持续不间断的运行,以降低内存占用 3、开发环境,Docker 容器的 CPU:0.04%,内存:168MB,如图1
开发环境,Docker 容器的 CPU:0.04%,内存:168MB

图1

4、查看 supervisord 运行状态:supervisorctl status


[root@09a3838784ce /]# ps aux|grep superviosrd
root      1379  0.0  0.0  10696   996 pts/2    S+   15:09   0:00 grep --color=auto superviosrd
[root@09a3838784ce /]# supervisorctl status
cronolog                         RUNNING   pid 446, uptime 0:30:20
nginx                            RUNNING   pid 441, uptime 0:30:20
php-fpm                          RUNNING   pid 440, uptime 0:30:20
report_client                    RUNNING   pid 444, uptime 0:30:20
yii-cmc-console-user-sync        RUNNING   pid 1341, uptime 0:00:58
yii-config-column-user-sync      RUNNING   pid 1382, uptime 0:00:02
yii-log-delete                   RUNNING   pid 1377, uptime 0:00:16


5、编辑 /etc/supervisord.d/yii-cmc-console-user-sync.ini、/etc/supervisord.d/yii-config-column-user-sync.ini、/etc/supervisord.d/yii-log-delete.ini,这将 每60秒、每60秒、每300秒 运行一次命令


[program:yii-cmc-console-user-sync]
command = bash -c 'sleep 60 && exec php /mcloud/www/pcs-api/yii cmc-console-user/sync'
autorestart = true
startsecs = 0
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-cmc-console-user-sync-stderr.log
stdout_logfile = /data/logs/yii-cmc-console-user-sync-stdout.log




[program:yii-config-column-user-sync]
command = bash -c 'sleep 60 && exec php /mcloud/www/pcs-api/yii config-column-user/sync'
autorestart = true
startsecs = 0
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-config-column-user-sync-stderr.log
stdout_logfile = /data/logs/yii-config-column-user-sync-stdout.log




[program:yii-log-delete]
command = bash -c 'sleep 300 && exec php /mcloud/www/pcs-api/yii log/delete'
autorestart = true
startsecs = 0
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-log-delete-stderr.log
stdout_logfile = /data/logs/yii-log-delete-stdout.log


6、在 3 个命令行的初始位置分别写入对应的文本文件,以检测命令行运行的间隔时间


file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
return ExitCode::OK;

file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/config-column-user-sync-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
return ExitCode::OK;

file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/log-delete-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
return ExitCode::OK;


7、查看写入的文本文件,以检测命令行运行的间隔时间,符合预期,如图2
查看写入的文本文件,以检测命令行运行的间隔时间,符合预期

图2



[root@38b6559d8629 /]# cd /mcloud/www/pcs-api/console/runtime/logs/
[root@38b6559d8629 logs]# ls -l
total 124
-rw-r--r-- 1 root root 17084 Oct  9 16:39 app.log
-rw-r--r-- 1 root root    17 Oct  9 16:40 cmc-console-user-sync-memory-get-usage-2019-10-09 16:40:56.txt
-rw-r--r-- 1 root root    17 Oct  9 16:41 cmc-console-user-sync-memory-get-usage-2019-10-09 16:41:57.txt
-rw-r--r-- 1 root root    17 Oct  9 16:42 cmc-console-user-sync-memory-get-usage-2019-10-09 16:42:58.txt
-rw-r--r-- 1 root root    17 Oct  9 16:43 cmc-console-user-sync-memory-get-usage-2019-10-09 16:43:59.txt
-rw-r--r-- 1 root root    17 Oct  9 16:45 cmc-console-user-sync-memory-get-usage-2019-10-09 16:45:00.txt
-rw-r--r-- 1 root root    17 Oct  9 16:46 cmc-console-user-sync-memory-get-usage-2019-10-09 16:46:02.txt
-rw-r--r-- 1 root root    17 Oct  9 16:47 cmc-console-user-sync-memory-get-usage-2019-10-09 16:47:03.txt
-rw-r--r-- 1 root root    17 Oct  9 16:48 cmc-console-user-sync-memory-get-usage-2019-10-09 16:48:04.txt
-rw-r--r-- 1 root root    17 Oct  9 16:49 cmc-console-user-sync-memory-get-usage-2019-10-09 16:49:05.txt
-rw-r--r-- 1 root root    17 Oct  9 16:50 cmc-console-user-sync-memory-get-usage-2019-10-09 16:50:06.txt
-rw-r--r-- 1 root root    17 Oct  9 16:51 cmc-console-user-sync-memory-get-usage-2019-10-09 16:51:07.txt
-rw-r--r-- 1 root root    17 Oct  9 16:52 cmc-console-user-sync-memory-get-usage-2019-10-09 16:52:08.txt
-rw-r--r-- 1 root root    17 Oct  9 16:40 config-column-user-sync-memory-get-usage-2019-10-09 16:40:57.txt
-rw-r--r-- 1 root root    17 Oct  9 16:41 config-column-user-sync-memory-get-usage-2019-10-09 16:41:58.txt
-rw-r--r-- 1 root root    17 Oct  9 16:42 config-column-user-sync-memory-get-usage-2019-10-09 16:42:59.txt
-rw-r--r-- 1 root root    17 Oct  9 16:44 config-column-user-sync-memory-get-usage-2019-10-09 16:44:00.txt
-rw-r--r-- 1 root root    17 Oct  9 16:45 config-column-user-sync-memory-get-usage-2019-10-09 16:45:01.txt
-rw-r--r-- 1 root root    17 Oct  9 16:46 config-column-user-sync-memory-get-usage-2019-10-09 16:46:03.txt
-rw-r--r-- 1 root root    17 Oct  9 16:47 config-column-user-sync-memory-get-usage-2019-10-09 16:47:04.txt
-rw-r--r-- 1 root root    17 Oct  9 16:48 config-column-user-sync-memory-get-usage-2019-10-09 16:48:05.txt
-rw-r--r-- 1 root root    17 Oct  9 16:49 config-column-user-sync-memory-get-usage-2019-10-09 16:49:06.txt
-rw-r--r-- 1 root root    17 Oct  9 16:50 config-column-user-sync-memory-get-usage-2019-10-09 16:50:07.txt
-rw-r--r-- 1 root root    17 Oct  9 16:51 config-column-user-sync-memory-get-usage-2019-10-09 16:51:08.txt
-rw-r--r-- 1 root root    17 Oct  9 16:52 config-column-user-sync-memory-get-usage-2019-10-09 16:52:09.txt
-rw-r--r-- 1 root root    17 Oct  9 16:43 log-delete-memory-get-usage-2019-10-09 16:43:56.txt
-rw-r--r-- 1 root root    17 Oct  9 16:48 log-delete-memory-get-usage-2019-10-09 16:48:57.txt


8、在 3 个命令行的初始位置分别写入对应的文本文件,删除成功退出命令,以检测命令行运行的间隔加运行时间(包含命令行自身运行的时间长度)


file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');

file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/config-column-user-sync-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');

file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/log-delete-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');


9、查看写入的文本文件,以检测命令行运行的间隔加运行时间(包含命令行自身运行的时间长度),符合预期,如图3
查看写入的文本文件,以检测命令行运行的间隔加运行时间(包含命令行自身运行的时间长度),符合预期

图3



[root@adcf56a0b62b /]# cd /mcloud/www/pcs-api/console/runtime/logs/
[root@adcf56a0b62b logs]# ls -l
total 112
-rw-r--r-- 1 root root 17084 Oct  9 16:13 app.log
-rw-r--r-- 1 root root    17 Oct  9 16:15 cmc-console-user-sync-memory-get-usage-2019-10-09 16:15:00.txt
-rw-r--r-- 1 root root    17 Oct  9 16:14 config-column-user-sync-memory-get-usage-2019-10-09 16:14:59.txt
-rw-r--r-- 1 root root    17 Oct  9 16:16 config-column-user-sync-memory-get-usage-2019-10-09 16:16:01.txt
-rw-r--r-- 1 root root    17 Oct  9 16:17 config-column-user-sync-memory-get-usage-2019-10-09 16:17:02.txt
-rw-r--r-- 1 root root    17 Oct  9 16:18 config-column-user-sync-memory-get-usage-2019-10-09 16:18:04.txt
-rw-r--r-- 1 root root    17 Oct  9 16:19 config-column-user-sync-memory-get-usage-2019-10-09 16:19:05.txt
-rw-r--r-- 1 root root    17 Oct  9 16:20 config-column-user-sync-memory-get-usage-2019-10-09 16:20:07.txt
-rw-r--r-- 1 root root    17 Oct  9 16:21 config-column-user-sync-memory-get-usage-2019-10-09 16:21:08.txt
-rw-r--r-- 1 root root    17 Oct  9 16:22 config-column-user-sync-memory-get-usage-2019-10-09 16:22:10.txt
-rw-r--r-- 1 root root    17 Oct  9 16:23 config-column-user-sync-memory-get-usage-2019-10-09 16:23:11.txt
-rw-r--r-- 1 root root    17 Oct  9 16:24 config-column-user-sync-memory-get-usage-2019-10-09 16:24:13.txt
-rw-r--r-- 1 root root    17 Oct  9 16:25 config-column-user-sync-memory-get-usage-2019-10-09 16:25:14.txt
-rw-r--r-- 1 root root    17 Oct  9 16:26 config-column-user-sync-memory-get-usage-2019-10-09 16:26:16.txt
-rw-r--r-- 1 root root    17 Oct  9 16:27 config-column-user-sync-memory-get-usage-2019-10-09 16:27:17.txt
-rw-r--r-- 1 root root    17 Oct  9 16:28 config-column-user-sync-memory-get-usage-2019-10-09 16:28:19.txt
-rw-r--r-- 1 root root    17 Oct  9 16:29 config-column-user-sync-memory-get-usage-2019-10-09 16:29:20.txt
-rw-r--r-- 1 root root    17 Oct  9 16:30 config-column-user-sync-memory-get-usage-2019-10-09 16:30:22.txt
-rw-r--r-- 1 root root    17 Oct  9 16:31 config-column-user-sync-memory-get-usage-2019-10-09 16:31:23.txt
-rw-r--r-- 1 root root    17 Oct  9 16:32 config-column-user-sync-memory-get-usage-2019-10-09 16:32:25.txt
-rw-r--r-- 1 root root    17 Oct  9 16:33 config-column-user-sync-memory-get-usage-2019-10-09 16:33:27.txt
-rw-r--r-- 1 root root    17 Oct  9 16:34 config-column-user-sync-memory-get-usage-2019-10-09 16:34:28.txt
-rw-r--r-- 1 root root    17 Oct  9 16:17 log-delete-memory-get-usage-2019-10-09 16:17:59.txt
-rw-r--r-- 1 root root    17 Oct  9 16:33 log-delete-memory-get-usage-2019-10-09 16:33:00.txt


10、在命令行中抛出异常,以检测 Supervisor 是否持续启动命令行脚本,日志表中持续写入异常日志,仍然持续启动命令行脚本,如图4
在命令行中抛出异常,以检测 Supervisor 是否持续启动命令行脚本,日志表中持续写入异常日志,仍然持续启动命令行脚本

图4



[root@2e54908c8679 /]# ps aux|grep superviosrd
root       858  0.0  0.0  10696   992 pts/2    R+   17:10   0:00 grep --color=auto superviosrd
[root@2e54908c8679 /]# supervisorctl status
cronolog                         RUNNING   pid 446, uptime 0:10:28
nginx                            RUNNING   pid 441, uptime 0:10:28
php-fpm                          RUNNING   pid 440, uptime 0:10:29
report_client                    RUNNING   pid 444, uptime 0:10:28
yii-cmc-console-user-sync        RUNNING   pid 846, uptime 0:00:18
yii-config-column-user-sync      RUNNING   pid 848, uptime 0:00:17
yii-log-delete                   RUNNING   pid 443, uptime 0:10:28
[root@2e54908c8679 /]# cd /mcloud/www/pcs-api/console/runtime/logs/
[root@2e54908c8679 logs]# ls -l
total 256
-rw-r--r-- 1 root root 176844 Oct  9 17:09 app.log
-rw-r--r-- 1 root root     17 Oct  9 17:01 cmc-console-user-sync-memory-get-usage-2019-10-09 17:01:43.txt
-rw-r--r-- 1 root root     17 Oct  9 17:02 cmc-console-user-sync-memory-get-usage-2019-10-09 17:02:44.txt
-rw-r--r-- 1 root root     17 Oct  9 17:03 cmc-console-user-sync-memory-get-usage-2019-10-09 17:03:45.txt
-rw-r--r-- 1 root root     17 Oct  9 17:04 cmc-console-user-sync-memory-get-usage-2019-10-09 17:04:46.txt
-rw-r--r-- 1 root root     17 Oct  9 17:05 cmc-console-user-sync-memory-get-usage-2019-10-09 17:05:47.txt
-rw-r--r-- 1 root root     17 Oct  9 17:06 cmc-console-user-sync-memory-get-usage-2019-10-09 17:06:49.txt
-rw-r--r-- 1 root root     17 Oct  9 17:07 cmc-console-user-sync-memory-get-usage-2019-10-09 17:07:50.txt
-rw-r--r-- 1 root root     17 Oct  9 17:08 cmc-console-user-sync-memory-get-usage-2019-10-09 17:08:51.txt
-rw-r--r-- 1 root root     17 Oct  9 17:09 cmc-console-user-sync-memory-get-usage-2019-10-09 17:09:52.txt
-rw-r--r-- 1 root root     17 Oct  9 17:01 config-column-user-sync-memory-get-usage-2019-10-09 17:01:44.txt
-rw-r--r-- 1 root root     17 Oct  9 17:02 config-column-user-sync-memory-get-usage-2019-10-09 17:02:45.txt
-rw-r--r-- 1 root root     17 Oct  9 17:03 config-column-user-sync-memory-get-usage-2019-10-09 17:03:46.txt
-rw-r--r-- 1 root root     17 Oct  9 17:04 config-column-user-sync-memory-get-usage-2019-10-09 17:04:47.txt
-rw-r--r-- 1 root root     17 Oct  9 17:05 config-column-user-sync-memory-get-usage-2019-10-09 17:05:48.txt
-rw-r--r-- 1 root root     17 Oct  9 17:06 config-column-user-sync-memory-get-usage-2019-10-09 17:06:50.txt
-rw-r--r-- 1 root root     17 Oct  9 17:07 config-column-user-sync-memory-get-usage-2019-10-09 17:07:51.txt
-rw-r--r-- 1 root root     17 Oct  9 17:08 config-column-user-sync-memory-get-usage-2019-10-09 17:08:52.txt
-rw-r--r-- 1 root root     17 Oct  9 17:09 config-column-user-sync-memory-get-usage-2019-10-09 17:09:53.txt
-rw-r--r-- 1 root root     17 Oct  9 17:04 log-delete-memory-get-usage-2019-10-09 17:04:43.txt


11、还原第 8 步骤所做的修改,升级至开发环境,Docker 容器的 CPU:0.04%,内存:335MB,内存增加了 (335MB – 168MB) = 167MB 左右,如图5
还原第 8 步骤所做的修改,升级至开发环境,Docker 容器的 CPU:0.04%,内存:335MB,内存增加了 (335MB - 168MB) = 167MB 左右

图5

12、查看 supervisord 运行状态:supervisorctl status,无甚变化


[root@c37ccca41d34 /]# ps aux|grep superviosrd
root       796  0.0  0.0  10696   992 pts/2    S+   17:48   0:00 grep --color=auto superviosrd
[root@c37ccca41d34 /]# supervisorctl status
cronolog                         RUNNING   pid 446, uptime 0:10:37
nginx                            RUNNING   pid 441, uptime 0:10:37
php-fpm                          RUNNING   pid 440, uptime 0:10:37
report_client                    RUNNING   pid 444, uptime 0:10:37
yii-cmc-console-user-sync        RUNNING   pid 798, uptime 0:00:02
yii-config-column-user-sync      RUNNING   pid 766, uptime 0:00:21
yii-log-delete                   RUNNING   pid 443, uptime 0:10:37


13、总结:基于 Supervisor 的 crontab 的实现,符合预期,命令行每一次的运行结束后,将 每60秒、每60秒、每300秒 后再次开始运行。内存占用的明显上升,根源在于:bash sleep 的持续运行 (占用大量内存) , Yii 2 sleep 的持续运行 (框架在运行期间加载了大量的数据至内存中,内存占用有所减少,但被 bash sleep 的持续运行 所抵消) 14、Supervisor 的配置文件的 program 配置项,将 startsecs 的值调整为 bash sleep 的值,需要考虑进程启动成功的时间, 当 running 状态超过该值时,表明启动成功。注:现在可以确保每次皆是启动成功的


[program:yii-cmc-console-user-sync]
command = bash -c 'sleep 60 && exec php /mcloud/www/pcs-api/yii cmc-console-user/sync'
autorestart = true
startsecs = 60
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-cmc-console-user-sync-stderr.log
stdout_logfile = /data/logs/yii-cmc-console-user-sync-stdout.log




[program:yii-config-column-user-sync]
command = bash -c 'sleep 60 && exec php /mcloud/www/pcs-api/yii config-column-user/sync'
autorestart = true
startsecs = 60
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-config-column-user-sync-stderr.log
stdout_logfile = /data/logs/yii-config-column-user-sync-stdout.log




[program:yii-log-delete]
command = bash -c 'sleep 300 && exec php /mcloud/www/pcs-api/yii log/delete'
autorestart = true
startsecs = 300
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-log-delete-stderr.log
stdout_logfile = /data/logs/yii-log-delete-stdout.log


15、升级至开发环境,Docker 容器的 CPU:0.04%,内存:270MB,内存减少了 (335MB – 270MB) = 65MB 左右,但是,比之最初始的内存占用,内存增加了 (270MB – 168MB) = 102MB 左右,未达到内存占用减少的预期,如图6
升级至开发环境,Docker 容器的 CPU:0.04%,内存:270MB,内存减少了 (335MB - 270MB) = 65MB 左右,但是,比之最初始的内存占用,内存增加了 (270MB - 168MB) = 102MB 左右,未达到内存占用减少的预期

图6

16、查看 supervisord 运行状态:supervisorctl status,第1列是服务名,第2列是运行状态,RUNNING 表示运行中,FATAL 表示运行失败,STARTING 表示正在启动,STOPED 表示任务已停止,第3/4列是进程号,最后是任务已经运行的时间


[root@8db3aeab0ebb /]# ps aux|grep superviosrd
root      2150  0.0  0.0  10696   996 pts/2    R+   10:40   0:00 grep --color=auto superviosrd
[root@8db3aeab0ebb /]# supervisorctl status
cronolog                         RUNNING   pid 446, uptime 20:14:10
nginx                            RUNNING   pid 441, uptime 20:14:10
php-fpm                          RUNNING   pid 440, uptime 20:14:10
report_client                    RUNNING   pid 444, uptime 20:14:10
yii-cmc-console-user-sync        STARTING
yii-config-column-user-sync      STARTING
yii-log-delete                   RUNNING   pid 1906, uptime 0:06:42
[root@8db3aeab0ebb /]# supervisorctl status
cronolog                         RUNNING   pid 446, uptime 20:20:44
nginx                            RUNNING   pid 441, uptime 20:20:44
php-fpm                          RUNNING   pid 440, uptime 20:20:44
report_client                    RUNNING   pid 444, uptime 20:20:44
yii-cmc-console-user-sync        STARTING
yii-config-column-user-sync      RUNNING   pid 2342, uptime 0:01:00
yii-log-delete                   RUNNING   pid 1906, uptime 0:13:16


17、在 3 个命令行的初始位置分别写入对应的文本文件,以检测命令行运行的间隔时间


file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/cmc-console-user-sync-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
return ExitCode::OK;

file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/config-column-user-sync-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
return ExitCode::OK;

file_put_contents('/mcloud/www/pcs-api/console/runtime/logs/log-delete-memory-get-usage-' . date('Y-m-d H:i:s', time()) . '.txt', memory_get_usage() / 1024 / 1024 . 'MB');
return ExitCode::OK;


18、查看写入的文本文件,以检测命令行运行的间隔时间,每60秒、每60秒、每300秒 运行一次命令,符合预期


[root@b93baf1cb6ee /]# cd /mcloud/www/pcs-api/console/runtime/logs/
[root@b93baf1cb6ee logs]# ls -l
total 104
-rw-r--r-- 1 root root 8549 Oct 11 11:09 app.log
-rw-r--r-- 1 root root   17 Oct 11 11:09 cmc-console-user-sync-memory-get-usage-2019-10-11 11:09:21.txt
-rw-r--r-- 1 root root   17 Oct 11 11:10 cmc-console-user-sync-memory-get-usage-2019-10-11 11:10:22.txt
-rw-r--r-- 1 root root   17 Oct 11 11:11 cmc-console-user-sync-memory-get-usage-2019-10-11 11:11:23.txt
-rw-r--r-- 1 root root   17 Oct 11 11:12 cmc-console-user-sync-memory-get-usage-2019-10-11 11:12:25.txt
-rw-r--r-- 1 root root   17 Oct 11 11:13 cmc-console-user-sync-memory-get-usage-2019-10-11 11:13:26.txt
-rw-r--r-- 1 root root   17 Oct 11 11:14 cmc-console-user-sync-memory-get-usage-2019-10-11 11:14:27.txt
-rw-r--r-- 1 root root   17 Oct 11 11:15 cmc-console-user-sync-memory-get-usage-2019-10-11 11:15:28.txt
-rw-r--r-- 1 root root   17 Oct 11 11:16 cmc-console-user-sync-memory-get-usage-2019-10-11 11:16:29.txt
-rw-r--r-- 1 root root   17 Oct 11 11:17 cmc-console-user-sync-memory-get-usage-2019-10-11 11:17:30.txt
-rw-r--r-- 1 root root   17 Oct 11 11:18 cmc-console-user-sync-memory-get-usage-2019-10-11 11:18:31.txt
-rw-r--r-- 1 root root   17 Oct 11 11:19 cmc-console-user-sync-memory-get-usage-2019-10-11 11:19:32.txt
-rw-r--r-- 1 root root   17 Oct 11 11:10 config-column-user-sync-memory-get-usage-2019-10-11 11:10:21.txt
-rw-r--r-- 1 root root   17 Oct 11 11:11 config-column-user-sync-memory-get-usage-2019-10-11 11:11:22.txt
-rw-r--r-- 1 root root   17 Oct 11 11:12 config-column-user-sync-memory-get-usage-2019-10-11 11:12:24.txt
-rw-r--r-- 1 root root   17 Oct 11 11:13 config-column-user-sync-memory-get-usage-2019-10-11 11:13:25.txt
-rw-r--r-- 1 root root   17 Oct 11 11:14 config-column-user-sync-memory-get-usage-2019-10-11 11:14:26.txt
-rw-r--r-- 1 root root   17 Oct 11 11:15 config-column-user-sync-memory-get-usage-2019-10-11 11:15:27.txt
-rw-r--r-- 1 root root   17 Oct 11 11:16 config-column-user-sync-memory-get-usage-2019-10-11 11:16:28.txt
-rw-r--r-- 1 root root   17 Oct 11 11:17 config-column-user-sync-memory-get-usage-2019-10-11 11:17:29.txt
-rw-r--r-- 1 root root   17 Oct 11 11:18 config-column-user-sync-memory-get-usage-2019-10-11 11:18:30.txt
-rw-r--r-- 1 root root   17 Oct 11 11:19 config-column-user-sync-memory-get-usage-2019-10-11 11:19:31.txt
-rw-r--r-- 1 root root   17 Oct 11 11:13 log-delete-memory-get-usage-2019-10-11 11:13:21.txt
-rw-r--r-- 1 root root   17 Oct 11 11:18 log-delete-memory-get-usage-2019-10-11 11:18:22.txt


19、决定注释 Yii 2 命令行中的 sleep ,以降低命令行单次运行的时间长度。Yii 2 sleep 的持续运行 (框架在运行期间加载了大量的数据至内存中)。以便更进一步的降低内存占用(预计)。


            /* 判断 $httpCmcApiGroupIds 是否为空,如果为空,则成功退出 */
            if (empty($httpCmcApiGroupIds)) {
                // 延缓执行 60 * 60 秒
                // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyYesSleepTime']);

                return ExitCode::OK;
            }

            // 延缓执行 60 秒
            // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyNoSleepTime']);
            return ExitCode::OK;

            /* 判断 $cmcApiGroupIds 是否为空,如果为空,则成功退出 */
            if (empty($cmcApiGroupIds)) {
                // 延缓执行 60 * 60 秒
                // sleep(Yii::$app->params['configColumnUser']['isEmptyYesSleepTime']);

                return ExitCode::OK;
            }

            // 延缓执行 60 秒
            // sleep(Yii::$app->params['configColumnUser']['isEmptyNoSleepTime']);
            return ExitCode::OK;

            // 延缓执行 10 * 60 秒
            // sleep(static::SLEEP_TIME);

            return ExitCode::OK;


20、升级至开发环境,Docker 容器的 CPU:0.04%,内存:292MB,内存增加了 (292MB – 270MB) = 20MB 左右,未符合预期,如图7
升级至开发环境,Docker 容器的 CPU:0.04%,内存:292MB,内存增加了 (292MB - 270MB) = 20MB 左右,未符合预期

图7

21、调整间隔时间,每5秒、每5秒、每60秒 运行一次命令,Docker 容器的 CPU:0.04%,内存:109MB,内存减少了 (292MB – 109MB) = 183MB 左右,并且,比之最初始的内存占用,内存减少了 (168MB – 109MB) = 59MB 左右,已经达到内存占用减少的预期,如图8
调整间隔时间,每5秒、每5秒、每60秒 运行一次命令,Docker 容器的 CPU:0.04%,内存:109MB,内存减少了 (292MB - 109MB) = 183MB 左右,并且,比之最初始的内存占用,内存减少了 (168MB - 109MB) = 59MB 左右,已经达到内存占用减少的预期

图8



[program:yii-cmc-console-user-sync]
command = bash -c 'sleep 5 && exec php /mcloud/www/pcs-api/yii cmc-console-user/sync'
autorestart = true
startsecs = 5
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-cmc-console-user-sync-stderr.log
stdout_logfile = /data/logs/yii-cmc-console-user-sync-stdout.log




[program:yii-config-column-user-sync]
command = bash -c 'sleep 5 && exec php /mcloud/www/pcs-api/yii config-column-user/sync'
autorestart = true
startsecs = 5
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-config-column-user-sync-stderr.log
stdout_logfile = /data/logs/yii-config-column-user-sync-stdout.log




[program:yii-log-delete]
command = bash -c 'sleep 60 && exec php /mcloud/www/pcs-api/yii log/delete'
autorestart = true
startsecs = 60
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-log-delete-stderr.log
stdout_logfile = /data/logs/yii-log-delete-stdout.log


22、查看另一个产品:渠道发布的开发环境,Docker 容器的 CPU:0.04%,内存:289MB,如图9
查看另一个产品:渠道发布的开发环境,Docker 容器的 CPU:0.04%,内存:289MB

图9

23、在产品:渠道发布中,基于同样的流程,基于 Supervisor 的 crontab (bash sleep) 的实现,注释 Yii 2 命令行中的 sleep,每3秒、每3秒、每60秒 运行一次命令,升级至开发环境,Docker 容器的 CPU:0.04%,内存:182MB,内存减少了 (289MB – 182MB) = 107MB 左右,符合预期,如图10
在产品:渠道发布中,基于同样的流程,基于 Supervisor 的 crontab (bash sleep) 的实现,注释 Yii 2 命令行中的 sleep,每3秒、每3秒、每60秒 运行一次命令,升级至开发环境,Docker 容器的 CPU:0.04%,内存:182MB,内存减少了 (289MB - 182MB) = 107MB 左右,符合预期

图10



[program:yii-qq-cw-transaction-video-sync]
command = bash -c 'sleep 3 && exec php /mcloud/www/channel-pub-api/yii qq-cw-transaction-video/sync'
autorestart = true
startsecs = 3
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-qq-cw-transaction-video-sync-stderr.log
stdout_logfile = /data/logs/yii-qq-cw-transaction-video-sync-stdout.log




[program:yii-qq-cw-transaction-article-sync]
command = bash -c 'sleep 3 && exec php /mcloud/www/channel-pub-api/yii qq-cw-transaction-article/sync'
autorestart = true
startsecs = 3
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-qq-cw-transaction-article-sync-stderr.log
stdout_logfile = /data/logs/yii-qq-cw-transaction-article-sync-stdout.log




[program:yii-log-delete]
command = bash -c 'sleep 60 && exec php /mcloud/www/channel-pub-api/yii log/delete'
autorestart = true
startsecs = 60
stopwaitsecs = 10
stderr_logfile = /data/logs/yii-log-delete-stderr.log
stdout_logfile = /data/logs/yii-log-delete-stdout.log


24、总结: (1)基于 Supervisor 的 crontab (bash sleep) 的实现,确定可以降低内存占用,crontab (bash sleep) 的值尽量控制在 60 之内,更大的话,内存占用会提升得比较厉害 (2)将 startsecs 的值调整为 bash sleep 的值,可有效降低内存占用 (3)Supervisor 的一个进程的运行时间,尽量控制得更短一些。不论是 bash sleep,还是 php sleep ,一旦其值过大,进行的运行时间过长,就会导致内存的过大占用 (4)之前 php sleep 的时候,为何内存占用得厉害,原因可能在于 Yii 2 sleep 的持续运行 (框架在运行期间加载了大量的数据至内存中)。因此,php 命令行本身对于内存的占用也是原因之一,尽量降低 php 命令行本身的运行时间,注释 php sleep (5)在部署期间,crontab (bash sleep) 的值,建议在多次调整中,综合考虑,取最佳值  ]]>
https://www.shuijingwanwq.com/2019/10/12/3555/feed/ 0