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

需求描述

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

难点

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

解决方案

基于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重构。
相关推荐
damoluomu1 天前
挑 PHP CMS 之前,先学会看它的协议
php
未济1 天前
linux 配置环境变量
linux
傲世仙尊1 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
_艾伦 耶格尔.1 天前
进程间通信
linux
Liuqy-051 天前
Linux IO编程——静态库、动态库
linux
彧azz1 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅1 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK1 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid
琥珀色糖1 天前
SimlpeHttp
linux·服务器
qeen871 天前
【Linux】操作系统之进程介绍(二)
linux·笔记·学习·进程