记录一次测试正常,但是上线后出现问题的事件

需求描述

用户可以领取多张优惠券,每个优惠券附带用户身份信息,比如在某段时间内获取了某些属性课程的用户才可以使用该张优惠券。 当用户下单时,需要根据当前登录的用户身份过滤出当前商品可用的优惠券。

难点

因为一个用户可以有多个优惠券,如果每个优惠券逐个检查,当用户持有大量优惠券时,可能会接口超时。

解决方案

基于swoole协程,在php-fpm中调用shell_exec执行多协程并发,每个协程检查一个优惠券。 代码如下:

bash 复制代码
\Swoole\Runtime::enableCoroutine();

$result = [];
$userIdentity = new UserIdentity();
$userCouponId2BatchInfo = $params['userCouponId2BatchInfo'];
$orderCouponMap = $params['orderCouponMap'];
$appId = $params['appId'];
$userId = $params['userId'];
$productInfoList = $params['productInfoList'];

foreach($orderCouponMap as $userCouponId => $userCouponProdIds) {
            go(function() use ($userCouponId2BatchInfo, $userCouponId, $userCouponProdIds, $userIdentity, &$result, $appId, 
                $userId, $productInfoList) {
             

                if(!isset($userCouponId2BatchInfo[$userCouponId])) {
                    return;
                }
                $batchInfo = $userCouponId2BatchInfo[$userCouponId];

                if($batchInfo['user_limit_identity'] == 0) {
                    $result[$userCouponId] = $userCouponProdIds;
                } else if($batchInfo['user_limit_identity'] == 1) {
                    $userLimitInfo = $batchInfo['user_limit_identity_info'];
                    if(is_string($userLimitInfo)) {
                        $userLimitInfo = json_decode($userLimitInfo, true);
                    }
                    if($userIdentity->match($appId, $userId, $userLimitInfo, $productInfoList)) {
                        $result[$userCouponId] = $userCouponProdIds;
                    }
                } else if($batchInfo['user_limit_identity'] == 2) {
                    $userLimitInfo = $batchInfo['user_limit_identity_info'];
                    if(is_string($userLimitInfo)) {
                        $userLimitInfo = json_decode($userLimitInfo, true);
                    }
                    if(!$userIdentity->match($appId, $userId, $userLimitInfo, $productInfoList)) {
                        $result[$userCouponId] = $userCouponProdIds;
                    }
                }
            });
        }
        
swoole_event_wait();
        
echo json_encode($result);

在业务请求的处理器Controller中,如下调用上面的命令:

ini 复制代码
$cmd = sprintf("/usr/local/php7/bin/php /app/Exec/Command/UserCoupon/UserIdentityMatch.php '%s'", json_encode($params));
        
$result = shell_exec($cmd);

在测试环境和灰度环境验收一切正常,但是当上线以后,部分帐号展示不了预期能够使用的优惠券。

问题排查

通过查看php_error.log看到如下报错:

css 复制代码
[10-Apr-2024 09:40:49 Asia/Shanghai] PHP Warning:  shell_exec(): Unable to execute '/usr/local/php7/bin/php 

将代码中的$cmd命令输出到文件中,手动执行,报错:

bash 复制代码
# sh /tmp/test.sh 
/tmp/test.sh: line 1: /usr/local/php7/bin/php: Argument list too long

查阅资料以后发现,linux系统有个ARG_MAX作为命令行和命令行参数的总大小限制,

arduino 复制代码
# getconf ARG_MAX
131072

再查看脚本命令行大小

bash 复制代码
# ll /tmp/test.sh 
-rw-r--r--    1 root     root        163067 Apr 10 09:49 /tmp/test.sh

原来是命令行参数太大导致!所以有些帐号会出现问题。而测试帐号在灰度和测试环境因为没有超过命令行参数限制。

bash 复制代码
# ll /tmp/test.sh 
-rw-r--r--    1 root     root         58658 Apr 10 09:54 /tmp/test.sh

解决方案

  1. 启动一个本地swoole服务,通过socket通信调用swoole。
  2. 使用golang重构。
相关推荐
物联网老王6 小时前
Ubuntu Linux Cursor 安装与使用一
linux·运维·ubuntu
一位摩羯座DBA8 小时前
Redhat&Centos挂载镜像
linux·运维·centos
学习3人组8 小时前
CentOS配置网络
linux·网络·centos
weixin_307779138 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
漫步企鹅9 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
cui_win9 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
梦在深巷、9 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
冰橙子id9 小时前
linux系统安全
linux·安全·系统安全
stark张宇10 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
Johny_Zhao10 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki