vali 6 years ago
parent
commit
5f3b0bf9e1

+ 85 - 44
app/Logic/SettelementLogic.php

@@ -11,6 +11,7 @@ namespace App\Logic;
 use App\Http\Response\Response;
 use App\Lib\Settlement\SettlementBase;
 use Illuminate\Database\Capsule\Manager as DB;
+use swoole;
 
 trait SettelementLogic
 {
@@ -45,61 +46,74 @@ trait SettelementLogic
                 if (empty($chekArr) || count($chekArr) != 1) {
                     throw  new \Exception('不同场比赛不能同时结算!');
                 }
+                unset($chekArr);
             }
 
-            if (!$this->DataPre($order_ids, $bettype, $settype, $game_code, $match_id, $change_status)) {
-                throw new  \Exception('没找到订单信息');
-            }
-            $this->UndoSettlement();
 
+            //分页处理数据
+            $PageOrder_ids_1 = array_chunk($order_ids, 500);
+            unset($order_ids);
+            //$PageOrder_ids[0] = $PageOrder_ids_1['0'];
 
-            while (true) {
-                if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['task_worker_num'])) {
-                    co::sleep(30);
-                } else {
-                    $order_id = trim(array_shift($order_ids));
-
-                    if (empty($order_id)) {
-                        break;
-                    }
+            foreach ($PageOrder_ids_1 as $p_order_ids) {
 
+                if (!$this->DataPre($p_order_ids, $bettype, $settype, $game_code, $match_id, 0)) {
+                    throw new  \Exception('没找到订单信息');
+                }
 
-                    $nowBuyDatasMainModel = $this->PDatas['Money_buy_Orders_Array'][$order_id];
-                    if ($nowBuyDatasMainModel->settle_status == 2 && $settype == 1) {
-                        continue;
+                $this->UndoSettlement();
+
+                while (true) {
+                    if ($this->httpserver->taskWorkingNum->get() >= intval($this->config['sets']['task_worker_num'])) {
+                        //echo "Work coutn: " . $this->httpserver->taskWorkingNum->get() . "\n";
+                        \co::sleep(0.01);
+                    } else {
+                        $order_id = trim(array_shift($p_order_ids));
+
+                        if (empty($order_id)) {
+                            break;
+                        }
+
+                        $nowBuyDatasMainModel = $this->PDatas['Money_buy_Orders_Array'][$order_id];
+                        if ($nowBuyDatasMainModel->settle_status == 2 && $settype == 1) {
+                            continue;
+                        }
+
+                        $account_indent = $nowBuyDatasMainModel->account_identity;
+                        $batch_id = $nowBuyDatasMainModel->batch_id;
+
+                        $paras = [
+                            'type' => 'Settelement',
+                            'bet_type' => $bettype,
+                            'set_type' => $settype,
+                            'order_id' => $order_id,
+                            'game_code' => $nowBuyDatasMainModel->game_code,
+                            'match_id' => $match_id,
+                            'simplexData' => $nowBuyDatasMainModel,
+                            'matchDatas' => $this->PDatas['Money_buy_Match_array'][$batch_id],
+                            'settelementMidDataID' => isset($this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]) ? $this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]->id : 0,
+                            'account' => $this->PDatas['account_map_array'][$account_indent]->account,
+                            'money_prize' => isset($this->PDatas['money_prize_map'][$order_id]) ? $this->PDatas['money_prize_map'][$order_id]->id : 0
+                        ];
+
+                        $this->onTask($this->httpserver, $paras);
                     }
-
-                    $account_indent = $nowBuyDatasMainModel->account_identity;
-                    $batch_id = $nowBuyDatasMainModel->batch_id;
-
-                    $paras = [
-                        'type' => 'Settelement',
-                        'bet_type' => $bettype,
-                        'set_type' => $settype,
-                        'order_id' => $order_id,
-                        'game_code' => $nowBuyDatasMainModel->game_code,
-                        'match_id' => $match_id,
-                        'simplexData' => $nowBuyDatasMainModel,
-                        'matchDatas' => $this->PDatas['Money_buy_Match_array'][$batch_id],
-                        'settelementMidDataID' => isset($this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]) ? $this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]->id : 0,
-                        'account' => $this->PDatas['account_map_array'][$account_indent]->account,
-                        'money_prize' => isset($this->PDatas['money_prize_map'][$order_id]) ? $this->PDatas['money_prize_map'][$order_id]->id : 0
-                    ];
-
-                    $this->onTask($this->httpserver, $paras);
                 }
+
             }
             $this->cgStatus($game_code, $match_id, $change_status);
 
             LABRETURN:
             $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
             $response->end($data);
+            unset($bettype, $settype, $game_code, $match_id, $change_status, $PageOrder_ids_1, $p_order_id, $data);
             return;
         } catch (\Exception $e) {
             $this->PDatas = [];
 
             $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
             $response->end($data);
+            unset($data);
             return;
         }
 
@@ -122,7 +136,9 @@ trait SettelementLogic
             $e = str_replace(";", ";", $e->getMessage());
             $sql = "update  $table  set  remark='$e' where  order_id='$order_id' ";
             $this->PushSqlToArray($sql);
+            unset($sql, $e);
         }
+        unset($order_id, $bet_type, $table);
     }
 
 
@@ -186,6 +202,7 @@ trait SettelementLogic
             $money_prize
         );
 
+        unset($matchDatas, $simplexData, $account_identity, $bet_type, $order_id, $match_id, $game_code, $account, $money_prize, $returnMoney);
     }
 
     private function WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_ident, $money)
@@ -254,6 +271,7 @@ trait SettelementLogic
             $chuanNum = intval(substr($simplexData->str_type, 0, 1));
             $lasPeilv = $settlementBase->stringComputing([$in_array, $chuanNum]);
             $money = floatPointDigit($this->BuyDatasMainModel->money * $lasPeilv);
+            unset($chuanNum, $lasPeilv);
 
         } else {
             $money = $simplexData->money;
@@ -264,6 +282,8 @@ trait SettelementLogic
         $this->WriteOrAddSettlement($this->game_code, $this->match_id, $this->orderType, $order_id, $this->BuyDatasMainModel->account_identity, $money);
         $this->insertData($order_id, $money, $this->BuyDatasMainModel->account_identity, 2, $val->game_code, $this->BuyDatasMainModel->info_identity, $this->BuyDatasMainModel->money, $this->match_id, $account, $money_prize);
 
+        unset($matchDatas, $simplexData, $account_identity, $bet_type, $order_id, $match_id, $game_code, $account, $money_prize, $money);
+
         return true;
     }
 
@@ -280,7 +300,6 @@ trait SettelementLogic
      */
     private function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money, $match_id = 0, $account = '', $money_prizeID = 0)
     {
-
         // 添加流水记录
         $info_identity = UUID();
         $money_time = date('Y-m-d H:i:s', time());
@@ -311,23 +330,38 @@ trait SettelementLogic
             $sql .= "('$info_identity','$order_id','$account_identity','$account','$game_name','$buy_identity',$money,'$money_time',1,$returnMoney,$returnMoney - $money)";
             $this->PushSqlToArray($sql);
         }
+        unset($info_identity, $money_time, $trade_desc, $reason, $content, $sql);
         return true;
     }
 
 
+    private function resetPDatas()
+    {
+        $bettype = $this->PDatas['bettype'];
+        $settype = $this->PDatas['settype'];
+        $game_code = $this->PDatas['game_code'];
+        $match_id = $this->PDatas['match_id'];
+        $this->PDatas = [];
+
+        $this->PDatas['bettype'] = $bettype;
+        $this->PDatas['settype'] = $settype;
+        $this->PDatas['game_code'] = $game_code;
+        $this->PDatas['match_id'] = $match_id;
+        return;
+    }
+
     //数据预批量获取
     private function DataPre($order_ids, $bettype, $settype, $game_code, $match_id, $change_status)
     {
+        $this->resetPdatas();
 
-        //DB::connection()->enableQueryLog();
         if ($bettype == 1) {
             $moneytable = 'money_buy_simplex';
-            $ret = DB::table('money_buy_simplex')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
+            $ret = DB::table($moneytable)->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
         } else {
             $moneytable = 'money_buy_str';
-            $ret = DB::table('money_buy_str')->whereIn('order_id', $order_ids)->get();
+            $ret = DB::table($moneytable)->whereIn('order_id', $order_ids)->get();
         }
-        //print_r(DB::getQueryLog());
 
         if (empty($ret) || count($ret) <= 0) {
             return false;
@@ -384,6 +418,7 @@ trait SettelementLogic
                     $sql_tmp = "update $moneytable set  settle_status=1, gain_money=0  where order_id in ($order_str)  ";
                 }
                 $this->PushSqlToArray($sql_tmp);
+                unset($sql_tmp);
             }
         }
 
@@ -394,6 +429,7 @@ trait SettelementLogic
             }
         }
 
+        unset($ret, $tmpbatchid, $userindentys, $moneytable);
         return true;
     }
 
@@ -434,6 +470,8 @@ trait SettelementLogic
 
         $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status];
 
+        unset($bettype, $idoarr, $settype, $game_code, $match_id, $change_status);
+
         return $ret;
     }
 
@@ -455,6 +493,7 @@ trait SettelementLogic
             $this->PushSqlToArray("update $table1 set  status=3  where   match_id=$match_id ");
             $this->PushSqlToArray("update $table2 set  status=3  where   match_id=$match_id ");
         }
+        unset($table1, $table2);
     }
 
     //得么一个临时变量
@@ -478,7 +517,6 @@ trait SettelementLogic
         $ret = DB::select($sql);
 
         return $ret;
-
     }
 
 
@@ -496,8 +534,10 @@ trait SettelementLogic
         foreach ($nowArray as $order => $val) {
             $money = abs(floatval($val->money));
             $acc = $val->account_identity;
-            $sql = "update account_detailed  set  cash=cash-$money where identity='$acc' ";
-            $this->sqlArray[] = $sql;
+            $sql1 = "update account_detailed  set  cash=cash-$money where identity='$acc' ";
+            $sql2 = "update money_prize  set  prize_money=0,get_money=0 where order_id='$val->order_id' and  account_identity='$acc'";
+            $this->sqlArray[] = $sql1;
+            $this->sqlArray[] = $sql2;
             $ids[] = $order;
         }
 
@@ -505,9 +545,10 @@ trait SettelementLogic
             return "'.$i.'";
         }, $ids);
         $ids_array = implode(",", $ids);
-        $sql = "update settlement_middle_detail  set  money=0 where order_id in($ids_array) and bet_type=$this->orderType  ";
+        $sql = "update settlement_middle_detail  set  money=0 where order_id in($ids_array) and bet_type=$bet_type  ";
         $this->sqlArray[] = $sql;
 
+        unset($nowArray, $bet_type, $ids, $ids_array, $sql);
         return true;
     }
 

+ 15 - 0
commands/bootRedisToSql.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 9:36
+ */
+
+define('START_TIME', microtime(true));
+require __DIR__ . '/../vendor/autoload.php';
+\datainf\lib\boot::init();
+
+$ser = new \datainf\logic\HttpServerRedisToSql(\datainf\lib\GlobConfigs::getKey('HttpRedisToSql'));
+$ser->start();
+

+ 3 - 1
commands/bootSettelementHttp.php

@@ -5,9 +5,11 @@
  * Date: 2019/5/20
  * Time: 9:36
  */
+set_time_limit(600);
+ini_set('memory_limit', '1024M');
 
 define('START_TIME', microtime(true));
-require __DIR__.'/../vendor/autoload.php';
+require __DIR__ . '/../vendor/autoload.php';
 \datainf\lib\boot::init();
 
 $ser = new \datainf\logic\HttpServerSettelement(\datainf\lib\GlobConfigs::getKey('SettelementHttpServer'));

+ 2 - 2
commands/clientws4.php

@@ -78,7 +78,7 @@ function getIds($type, $match_id, $game_code = '')
     }
 }
 
-$ids = getIds(1, 866, 'zg');
+$ids = getIds(1, 483, 'zg');
 echo "ids_len:" . count($ids) . "\n";
 
 function request_post($url = '', $param = '')
@@ -107,7 +107,7 @@ $data = [
     'bettype' => 1,
     'settype' => 2,
     'game_code' => 'zq',
-    'match_id' => 866,
+    'match_id' => 483,
     'change_status' => 1
 ];
 

+ 16 - 0
datainf/lib/functions.php

@@ -31,3 +31,19 @@ function UUID()
     return $prefix . $uuid;
 }
 
+
+//把订单转为带字符串的包裹的字符
+function orders_to_str($order_array)
+{
+    return array_map(function ($i) {
+        return strval("'$i'");
+    }, $order_array);
+}
+
+//把订单转为带字符串的包裹的字符
+function orders_to_strval($order_array)
+{
+    return array_map(function ($i) {
+        return strval($i);
+    }, $order_array);
+}

+ 115 - 0
datainf/logic/HttpServerRedisToSql.php

@@ -0,0 +1,115 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/26
+ * Time: 12:03
+ */
+
+namespace datainf\logic;
+
+use App\Lib\ModelBase;
+use App\Http\Response\Response;
+
+use Illuminate\Database\Capsule\Manager as DB;
+use datainf\lib\GlobConfigs;
+use swoole;
+
+class HttpServerRedisToSql
+{
+    private $httpserver;
+    private $config;
+    private $redisonfig = [];
+    const   SQLKEY = 'ALLSQLKEY';
+
+    public function __construct($config)
+    {
+        $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
+        $this->httpserver->set($config['sets']);
+        $this->config = $config;
+
+        $this->redisonfig = GlobConfigs::getKey('redis');
+
+        $taskWorkingNum = new \swoole\Atomic();
+        $this->httpserver->taskWorkingNum = $taskWorkingNum;
+
+        $this->httpserver->on('request', array($this, 'OnRequest'));
+        $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
+        $this->httpserver->on('task', array($this, 'onTask'));
+        $this->httpserver->on('finish', array($this, 'onFinish'));
+
+    }
+
+    public function onWorkerStart($serv, $worker_id)
+    {
+        if (!$serv->taskworker) {
+            ModelBase::init();
+
+            $obj = $this;
+            Swoole\Timer::tick(500, function ($id) use ($obj) {
+                $obj->BatchSqlRedisToDB();
+            });
+        }
+
+    }
+
+    //sql批量写入数据库
+    private function BatchSqlRedisToDB()
+    {
+        $redisconfig = $this->redisonfig;
+        go(function () use ($redisconfig) {
+            $redis = new Swoole\Coroutine\Redis();
+            $redis->connect($redisconfig['host'], $redisconfig['port']);
+
+            if ($redis->llen(self::SQLKEY) <= 0) {
+                return;
+            }
+
+            $batchsql = [];
+            $max = 5000;
+
+            for ($i = 0; $i < $max; $i++) {
+                $now = $redis->rpop(self::SQLKEY);
+                if (empty($now)) {
+                    break;
+                } else {
+                    $batchsql[] = str_replace(";", ":", trim(trim($now), ";"));
+                }
+            }
+
+            if (!empty($batchsql)) {
+                $pdo = DB::getPdo();
+                $sqlstr = implode(";", $batchsql);
+                $pdo->exec($sqlstr);
+            }
+
+            return;
+        });
+    }
+
+
+    public function OnRequest($request, $response)
+    {
+        $data = Response::generate('', 1, '', '任务正常运行.');
+        $response->end($data);
+        return;
+    }
+
+    public function onTask($serv, $task)
+    {
+
+    }
+
+
+    public function onFinish($serv, int $task_id, $data)
+    {
+
+    }
+
+
+    public function start()
+    {
+        $this->httpserver->start();
+    }
+
+}

+ 50 - 83
datainf/logic/HttpServerSettelement.php

@@ -25,9 +25,7 @@ class HttpServerSettelement
     private $dbpooleconfig = [];
     private $redisonfig = [];
     const   SQLKEY = 'ALLSQLKEY';
-    private $TaskSqlArray = [];
-    private $TaskSqlArrayCopy = [];
-
+    private $TaskSqlQueue;
 
     use SettelementLogic;
 
@@ -55,25 +53,23 @@ class HttpServerSettelement
         $name = 'Settelement_' . $name . ($serv->worker_id < 10 ? '0' . $serv->worker_id : $serv->worker_id);
         swoole_set_process_name($name);
 
+        $this->TaskSqlQueue = new \SplQueue();
+
         $GLOBALS['model'] = '';
         $GLOBALS['modeltime'] = 0;
 
         if (!$serv->taskworker) {
             $this->InitDb();
-        } else {
-
-            $obj = $this;
-            Swoole\Timer::tick(1000, function ($id) use ($obj) {
-                $obj->TaskSQlToRedis();
-            });
         }
 
-        if ($worker_id == 0) {
-            $obj = $this;
-            Swoole\Timer::tick(500, function ($id) use ($obj) {
-                $obj->BatchSqlRedisToDB();
-            });
-        }
+        $obj = $this;
+        Swoole\Timer::tick(1000, function ($id) use ($obj, $serv) {
+            $obj->TaskSQlToRedis();
+            if (memory_get_usage() > 1000000) {
+                echo $serv->worker_id . " 内存使用量:" . (memory_get_usage() / 1000) . 'k  峰值:' . (memory_get_peak_usage() / 1000) . "k\n";
+            }
+        });
+
     }
 
 
@@ -86,6 +82,8 @@ class HttpServerSettelement
         $request_uri = substr($request->server['request_uri'], 1);
         $urls = ['WinFail', 'Settelement'];
 
+        echo "现在任务数:".$this->httpserver->taskWorkingNum->get()."\n";
+
         if (!in_array($request_uri, $urls)) {
             $data = Response::generate('', 0, '', '无效的url');
             $response->end($data);
@@ -108,6 +106,8 @@ class HttpServerSettelement
             }
         }
 
+        echo "计数器值:" . $this->httpserver->taskWorkingNum->get() . "\n";
+
 
         if ($request_uri == 'WinFail') {
             return $this->doWinFailse($request, $response, $paras);
@@ -117,7 +117,6 @@ class HttpServerSettelement
             return $this->doSettelement($request, $response, $paras);
         }
 
-
         return;
     }
 
@@ -208,10 +207,11 @@ class HttpServerSettelement
             }
             unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass);
 
-
+            /*
             $this->httpserver->taskWorkingNum->add();
             $this->sqltoRedisqueue($this->TaskSqlArray);
             $this->httpserver->taskWorkingNum->sub();
+            */
 
             $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
             $response->end($data);
@@ -282,97 +282,64 @@ class HttpServerSettelement
     //生成更新sql;
     private function makesql_up_buymatch_winorfalse($id, $result, $utime, $matchResult = '')
     {
-        $matchResult = str_replace(";", ";", $matchResult);
         $sql = "update money_buy_match set result=$result, utime='$utime',matchResult='$matchResult' where   id=$id ";
         return $sql;
     }
 
 
-    /*
-    private function DbpooleClient($sqlArray = [])
-    {
-        go(function () use ($sqlArray) {
-            $client = new \swoole_client(SWOOLE_SOCK_TCP);
-            if (!$client->connect('127.0.0.1', $this->dbpooleconfig['poole_port'], 1)) {
-                return false;
-            }
-            foreach ($sqlArray as $val) {
-                $cmds[] = $val;
-            }
-            $this->sqltoRedisqueue($cmds);
-            $client->close();
-        });
-    }
-    */
-
     private function PushSqlToArray($cmdArray)
     {
         if (is_string($cmdArray)) {
-            array_push($this->TaskSqlArray, $cmdArray);
+            $this->TaskSqlQueue->enqueue($cmdArray);
         } else {
             foreach ($cmdArray as $sql) {
-                array_push($this->TaskSqlArray, $sql);
+                $this->TaskSqlQueue->enqueue($sql);
             }
         }
     }
 
     private function TaskSQlToRedis()
     {
-        if (empty($this->TaskSqlArray)) {
+        if ($this->TaskSqlQueue->isEmpty()) {
             return;
         }
-
-        $this->TaskSqlArrayCopy = $this->TaskSqlArray;
-        $this->TaskSqlArray = [];
-        $this->sqltoRedisqueue($this->TaskSqlArrayCopy);
-        $this->TaskSqlArrayCopy = [];
+        $this->sqltoRedisqueue();
     }
 
-    private function sqltoRedisqueue($cmdArray)
+    private function sqltoRedisqueue()
     {
         $redisconfig = $this->redisonfig;
-        go(function () use ($cmdArray, $redisconfig) {
-            $redis = new Swoole\Coroutine\Redis();
-            $redis->setOptions(['compatibility_mode' => true]);
-            $redis->connect($redisconfig['host'], $redisconfig['port']);
-            if (is_array($cmdArray)) {
-                $redis->multi();
-                foreach ($cmdArray as $cmd) {
-                    $redis->lpush(self::SQLKEY, $cmd);
-                }
-                $redis->exec();
-            } else {
-                $redis->lpush(self::SQLKEY, $cmdArray);
-            }
-        });
-    }
+        $sqlQueue = $this->TaskSqlQueue;
 
-    //sql批量写入数据库
-    private function BatchSqlRedisToDB()
-    {
-        $redisconfig = $this->redisonfig;
-        go(function () use ($redisconfig) {
-            $redis = new Swoole\Coroutine\Redis();
-            $redis->connect($redisconfig['host'], $redisconfig['port']);
-
-            $batchsql = [];
-            $max = 1000;
-
-            for ($i = 0; $i < $max; $i++) {
-                $now = $redis->rpop(self::SQLKEY);
-                if (empty($now)) {
-                    break;
-                } else {
-                    $batchsql[] = str_replace(";", ";", $now);
+        $redis = new \Redis();
+        $ret = $redis->connect($redisconfig['host'], $redisconfig['port']);
+        if (!$ret) {
+            unset($redis, $sqlQueue, $redisconfig, $ret);
+            return false;
+        }
+
+        $i = 1;
+        $maxStep = 5000;
+        $sql = '';
+        if (!$sqlQueue->isEmpty()) {
+            $redis->pipeline();
+            while ($i < $maxStep) {
+                if (!$sqlQueue->isEmpty()) {
+                    $sql = $sqlQueue->dequeue();
+                    if (empty($sql)) {
+                        break;
+                    }
+                    $redis->lpush(self::SQLKEY, $sql);
                 }
+                $i++;
             }
-            if (!empty($batchsql)) {
-                $pdo = DB::getPdo();
-                $sqlstr = implode(";", $batchsql);
-                $pdo->exec($sqlstr);
-            }
-            return;
-        });
+            $redis->exec();
+        }
+        $redis->close();
+
+        unset($redis, $sqlQueue, $sql, $i, $maxStep, $redisconfig, $ret);
+        return;
+
     }