From 070c2391fda479b4383805c176abef2acd6f756b Mon Sep 17 00:00:00 2001 From: ZiYangJia <107540964+ZiYangJia@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:29:35 +0800 Subject: [PATCH 01/20] Update README-CN.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 配置加上需运行barrier表的迁移文件 --- README-CN.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README-CN.md b/README-CN.md index b2a1c92..463ff8f 100644 --- a/README-CN.md +++ b/README-CN.md @@ -73,6 +73,7 @@ composer require dtm/dtm-client ```bash php bin/hyperf.php vendor:publish dtm/dtm-client ``` +另需运行vendor/dtm/dtm-client/publish下的barrier表迁移文件 如果您是在非 Hyperf 框架中使用,可复制 `./vendor/dtm/dtm-client/publish/dtm.php` 文件到对应的配置目录中。 From ace2909e3c36d61dbcd96231e56f6843c89f5b36 Mon Sep 17 00:00:00 2001 From: "Mr.tang" Date: Wed, 22 Oct 2025 14:43:49 +0800 Subject: [PATCH 02/20] Refactor Saga class to use Context for state management --- src/Saga.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Saga.php b/src/Saga.php index d3acdd2..ea31013 100644 --- a/src/Saga.php +++ b/src/Saga.php @@ -12,15 +12,12 @@ use DtmClient\Api\ApiInterface; use DtmClient\Constants\Protocol; use DtmClient\Constants\TransType; +use DtmClient\Context\Context; use DtmClient\Exception\UnsupportedException; use Google\Protobuf\Internal\Message; class Saga extends AbstractTransaction { - protected array $orders = []; - - protected bool $concurrent = false; - protected ApiInterface $api; public function __construct(ApiInterface $api) @@ -33,6 +30,8 @@ public function init(?string $gid = null) if ($gid === null) { $gid = $this->generateGid(); } + Context::set('concurrent', false); + Context::set('orders', []); TransContext::init($gid, TransType::SAGA, ''); } @@ -60,13 +59,15 @@ public function add(string $action, string $compensate, array|object $payload): public function addBranchOrder(int $branch, array $preBranches): static { - $this->orders[$branch] = $preBranches; + $orders = Context::get('orders', []); + $orders[$branch] = $preBranches; + Context::set('orders', $orders); return $this; } public function enableConcurrent() { - $this->concurrent = true; + Context::set('concurrent', true); } public function submit() @@ -77,10 +78,10 @@ public function submit() public function addConcurrentContext() { - if ($this->concurrent) { + if (Context::get('concurrent', false)) { TransContext::setCustomData(json_encode([ - 'concurrent' => $this->concurrent, - 'orders' => $this->orders ?: null, + 'concurrent' => Context::get('concurrent'), + 'orders' => Context::get('orders') ?: null, ])); } } From ded4c2ce0b6a1710f0d9b5c12ddbd479694ba721 Mon Sep 17 00:00:00 2001 From: "Mr.tang" Date: Wed, 22 Oct 2025 14:52:43 +0800 Subject: [PATCH 03/20] Refactor context keys to use class constants --- src/Saga.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Saga.php b/src/Saga.php index ea31013..9d51bfc 100644 --- a/src/Saga.php +++ b/src/Saga.php @@ -18,6 +18,10 @@ class Saga extends AbstractTransaction { + protected const CONCURRENT = self::class . '.concurrent'; + + protected const ORDERS = self::class . '.orders'; + protected ApiInterface $api; public function __construct(ApiInterface $api) @@ -30,8 +34,8 @@ public function init(?string $gid = null) if ($gid === null) { $gid = $this->generateGid(); } - Context::set('concurrent', false); - Context::set('orders', []); + Context::set(static::CONCURRENT, false); + Context::set(static::ORDERS, []); TransContext::init($gid, TransType::SAGA, ''); } @@ -59,15 +63,15 @@ public function add(string $action, string $compensate, array|object $payload): public function addBranchOrder(int $branch, array $preBranches): static { - $orders = Context::get('orders', []); + $orders = Context::get(static::ORDERS, []); $orders[$branch] = $preBranches; - Context::set('orders', $orders); + Context::set(static::ORDERS, $orders); return $this; } public function enableConcurrent() { - Context::set('concurrent', true); + Context::set(static::CONCURRENT, true); } public function submit() @@ -78,10 +82,10 @@ public function submit() public function addConcurrentContext() { - if (Context::get('concurrent', false)) { + if (Context::get(static::CONCURRENT, false)) { TransContext::setCustomData(json_encode([ - 'concurrent' => Context::get('concurrent'), - 'orders' => Context::get('orders') ?: null, + 'concurrent' => Context::get(static::CONCURRENT), + 'orders' => Context::get(static::ORDERS) ?: null, ])); } } From 8373733d67eac6c988515ac837c5b877b89b2501 Mon Sep 17 00:00:00 2001 From: "Mr.tang" Date: Sat, 24 Jan 2026 22:32:12 +0800 Subject: [PATCH 04/20] Update conditions for branch operations in MySqlBarrier --- src/MySqlBarrier.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MySqlBarrier.php b/src/MySqlBarrier.php index d30c23d..ad5b5db 100644 --- a/src/MySqlBarrier.php +++ b/src/MySqlBarrier.php @@ -53,7 +53,7 @@ public function call(callable $businessCall): bool } if ( - ($op == Operation::BRANCH_CANCEL || $op == Operation::BRANCH_COMPENSATE) && $originAffected > 0 // null compensate + ($op == Operation::BRANCH_CANCEL || $op == Operation::BRANCH_COMPENSATE || $op == Operation::ROLLBACK) && $originAffected > 0 // null compensate || $currentAffected == 0// repeated request or dangled request ) { $this->DBTransaction->commit(); From 78056ccac673b716641fcf60d779e7a6eb878af1 Mon Sep 17 00:00:00 2001 From: tangwei Date: Sun, 25 Jan 2026 22:06:13 +0800 Subject: [PATCH 05/20] add $originOP Branch::BranchRollback --- src/MySqlBarrier.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MySqlBarrier.php b/src/MySqlBarrier.php index ad5b5db..5aa8b73 100644 --- a/src/MySqlBarrier.php +++ b/src/MySqlBarrier.php @@ -40,6 +40,7 @@ public function call(callable $businessCall): bool $originOP = [ Branch::BranchCancel => Branch::BranchTry, Branch::BranchCompensate => Branch::BranchAction, + Branch::BranchRollback => Branch::BranchRollback, ][$op] ?? ''; $this->DBTransaction->beginTransaction(); From 602eff614b6fa90583c3d72d7b0339988e46bbb0 Mon Sep 17 00:00:00 2001 From: tw Date: Tue, 27 Jan 2026 11:05:48 +0800 Subject: [PATCH 06/20] add msg topic --- src/Api/ApiInterface.php | 2 ++ src/Api/GrpcApi.php | 5 +++++ src/Api/HttpApi.php | 5 +++++ src/Api/JsonRpcHttpApi.php | 5 +++++ src/Constants/Operation.php | 2 ++ src/Msg.php | 14 ++++++++++++++ 6 files changed, 33 insertions(+) diff --git a/src/Api/ApiInterface.php b/src/Api/ApiInterface.php index dcd9084..4578b83 100644 --- a/src/Api/ApiInterface.php +++ b/src/Api/ApiInterface.php @@ -26,5 +26,7 @@ public function query(array $body); public function queryAll(array $body); + public function subscribe(array $body); + public function transRequestBranch(RequestBranch $requestBranch); } diff --git a/src/Api/GrpcApi.php b/src/Api/GrpcApi.php index 5010457..546dffb 100644 --- a/src/Api/GrpcApi.php +++ b/src/Api/GrpcApi.php @@ -79,6 +79,11 @@ public function queryAll(array $body) throw new UnsupportedException('Unsupported QueryAll operation'); } + public function subscribe(array $body) + { + throw new UnsupportedException('Unsupported QueryAll operation'); + } + public function transRequestBranch(RequestBranch $requestBranch) { [$hostname, $method] = $this->parseHostnameAndMethod($requestBranch->url); diff --git a/src/Api/HttpApi.php b/src/Api/HttpApi.php index 4377016..2c14b95 100644 --- a/src/Api/HttpApi.php +++ b/src/Api/HttpApi.php @@ -85,6 +85,11 @@ public function queryAll(array $body) return $this->transQuery($body, Operation::QUERY_ALL); } + public function subscribe(array $body) + { + return $this->transQuery($body, Operation::SUBSCRIBE); + } + public function getClient(): Client { return $this->client; diff --git a/src/Api/JsonRpcHttpApi.php b/src/Api/JsonRpcHttpApi.php index a2f4817..d9ca45c 100644 --- a/src/Api/JsonRpcHttpApi.php +++ b/src/Api/JsonRpcHttpApi.php @@ -81,6 +81,11 @@ public function queryAll(array $body) throw new UnsupportedException('Unsupported Query operation'); } + public function subscribe(array $body) + { + throw new UnsupportedException('Unsupported Query operation'); + } + public function getClient(): Client { return $this->client; diff --git a/src/Constants/Operation.php b/src/Constants/Operation.php index 053fa41..c7d6c73 100644 --- a/src/Constants/Operation.php +++ b/src/Constants/Operation.php @@ -31,4 +31,6 @@ class Operation public const ACTION = 'action'; public const ROLLBACK = 'rollback'; + + public const SUBSCRIBE = 'subscribe'; } diff --git a/src/Msg.php b/src/Msg.php index 71d2d71..466b0cd 100644 --- a/src/Msg.php +++ b/src/Msg.php @@ -36,6 +36,12 @@ public function init(?string $gid = null) TransContext::init($gid, TransType::MSG, ''); } + public function addTopic(string $action, $payload) + { + $action = sprintf('topic://%s', $action); + $this->add($action, $payload); + } + public function add(string $action, $payload) { TransContext::addStep(['action' => $action]); @@ -83,6 +89,14 @@ public function doAndSubmit(string $queryPrepared, callable $businessCall) } } + public function subscribe(string $topic, string $url, string $remark = '') + { + $body['topic'] = $topic; + $body['url'] = $url; + $body['remark'] = $remark; + return $this->api->subscribe($body); + } + protected function queryPrepared(string $queryPrepared) { // If busicall return an error other than failure, we will query the result From f6e531fb0e7fc72226d149d2b5bd63cfb80118f6 Mon Sep 17 00:00:00 2001 From: tangwei Date: Tue, 27 Jan 2026 23:43:47 +0800 Subject: [PATCH 07/20] add db builder --- publish/dtm.php | 1 + src/Barrier.php | 7 ++- src/Constants/DbType.php | 2 + src/DbBarrier.php | 59 +++++++++++++++++++ src/DbTransaction/DBTransactionInterface.php | 4 ++ src/DbTransaction/HyperfDbTransaction.php | 10 ++++ .../HyperfSimpleDbTransaction.php | 12 +++- src/DbTransaction/LaravelDbTransaction.php | 11 +++- 8 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/DbBarrier.php diff --git a/publish/dtm.php b/publish/dtm.php index 2c37ff7..2d6259b 100644 --- a/publish/dtm.php +++ b/publish/dtm.php @@ -19,6 +19,7 @@ 'barrier' => [ 'db' => [ 'type' => DbType::MySQL, + // 'type' => DbType::DB, ], 'apply' => [], ], diff --git a/src/Barrier.php b/src/Barrier.php index aea87e4..cc906bc 100644 --- a/src/Barrier.php +++ b/src/Barrier.php @@ -21,10 +21,13 @@ class Barrier protected MySqlBarrier $mySqlBarrier; - public function __construct(ConfigInterface $config, MySqlBarrier $mySqlBarrier) + protected DbBarrier $dbBarrier; + + public function __construct(ConfigInterface $config, MySqlBarrier $mySqlBarrier, DbBarrier $dbBarrier) { $this->config = $config; $this->mySqlBarrier = $mySqlBarrier; + $this->dbBarrier = $dbBarrier; } public function call(callable $businessCall) @@ -77,6 +80,8 @@ protected function getBarrier(): BarrierInterface switch ($this->config->get('dtm.barrier.db.type', DbType::MySQL)) { case DbType::MySQL: return $this->mySqlBarrier; + case DbType::DB: + return $this->dbBarrier; default: throw new UnsupportedException('Barrier DB type is unsupported.'); } diff --git a/src/Constants/DbType.php b/src/Constants/DbType.php index 8fabd53..780e42d 100644 --- a/src/Constants/DbType.php +++ b/src/Constants/DbType.php @@ -12,5 +12,7 @@ class DbType { public const MySQL = 'mysql'; + public const DB = 'db'; + public const Redis = 'redis'; } diff --git a/src/DbBarrier.php b/src/DbBarrier.php new file mode 100644 index 0000000..09d96b6 --- /dev/null +++ b/src/DbBarrier.php @@ -0,0 +1,59 @@ +DBTransaction->insert('barrier', [ + 'trans_type' => $transType, + 'gid' => $gid, + 'branch_id' => $branchId, + 'op' => $op, + 'barrier_id' => $barrierID, + 'reason' => $reason, + ]); + + return (int)$bool; + } + + + public function queryPrepared(string $transType, string $gid): bool + { + $this->DBTransaction->insert('barrier', [ + 'trans_type' => $transType, + 'gid' => $gid, + 'branch_id' => Branch::MsgDoBranch0, + 'op' => Branch::MsgDoOp, + 'barrier_id' => Branch::MsgDoBarrier1, + 'reason' => Operation::ROLLBACK, + ]); + + $reason = $this->DBTransaction->queryBuilder('barrier','barrier', [ + 'gid' => $gid, + 'branch_id' => Branch::MsgDoBranch0, + 'op' => Branch::MsgDoOp, + 'barrier_id' => Branch::MsgDoBarrier1, + ])[0] ?? []; + + if ($reason['reason'] ?? '' == Operation::ROLLBACK) { + throw new FailureException(); + } + return true; + } +} diff --git a/src/DbTransaction/DBTransactionInterface.php b/src/DbTransaction/DBTransactionInterface.php index 8915b84..382d1b9 100644 --- a/src/DbTransaction/DBTransactionInterface.php +++ b/src/DbTransaction/DBTransactionInterface.php @@ -20,8 +20,12 @@ public function rollback(); public function execute(string $sql, array $bindings = []): int; + public function insert(string $table, array $data = []): bool; + public function query(string $sql, array $bindings = []): bool|array; + public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array; + public function xaExecute(string $sql, array $bindings = []): int; public function xaQuery(string $sql, array $bindings = []): bool|array; diff --git a/src/DbTransaction/HyperfDbTransaction.php b/src/DbTransaction/HyperfDbTransaction.php index efbc1cb..d9850bc 100644 --- a/src/DbTransaction/HyperfDbTransaction.php +++ b/src/DbTransaction/HyperfDbTransaction.php @@ -42,4 +42,14 @@ public function query(string $sql, array $bindings = []): bool|array { return Db::select($sql, $bindings); } + + public function insert(string $table, array $data = []): bool + { + return Db::table($table)->insert($data); + } + + public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array + { + return Db::table($table)->select($select)->where($where)->limit($limit)->get()->toArray(); + } } diff --git a/src/DbTransaction/HyperfSimpleDbTransaction.php b/src/DbTransaction/HyperfSimpleDbTransaction.php index 9dd8ea7..f1c46f8 100644 --- a/src/DbTransaction/HyperfSimpleDbTransaction.php +++ b/src/DbTransaction/HyperfSimpleDbTransaction.php @@ -42,5 +42,15 @@ public function query(string $sql, array $bindings = []): bool|array { return DB::query($sql, $bindings); } - + + + public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array + { + throw new \Exception('Not implemented'); + } + + public function insert(string $table, array $data = []): bool + { + throw new \Exception('Not implemented'); + } } diff --git a/src/DbTransaction/LaravelDbTransaction.php b/src/DbTransaction/LaravelDbTransaction.php index b98a480..0932234 100644 --- a/src/DbTransaction/LaravelDbTransaction.php +++ b/src/DbTransaction/LaravelDbTransaction.php @@ -34,9 +34,18 @@ public function execute(string $sql, array $bindings = []): int { return DB::affectingStatement($sql, $bindings); } - public function query(string $sql, array $bindings = []): bool|array { return DB::select($sql, $bindings); } + + public function insert(string $table, array $data = []): bool + { + return DB::table($table)->insert($data); + } + + public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array + { + return DB::table($table)->select($select)->where($where)->limit($limit)->get()->toArray(); + } } From 1ce29d2515b33f3e914e2c2830b7221b6d89a363 Mon Sep 17 00:00:00 2001 From: tw Date: Wed, 28 Jan 2026 11:51:35 +0800 Subject: [PATCH 08/20] =?UTF-8?q?db=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DbBarrier.php | 57 ++++++++++--------- src/DbTransaction/DBTransactionInterface.php | 7 ++- src/DbTransaction/HyperfDbTransaction.php | 10 +--- .../HyperfSimpleDbTransaction.php | 8 +-- src/DbTransaction/LaravelDbTransaction.php | 9 +-- 5 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/DbBarrier.php b/src/DbBarrier.php index 09d96b6..7394d09 100644 --- a/src/DbBarrier.php +++ b/src/DbBarrier.php @@ -14,28 +14,11 @@ class DbBarrier extends MySqlBarrier { - protected function insertBarrier(string $transType, string $gid, string $branchId, string $op, string $barrierID, string $reason): int - { - if (empty($op)) { - return 0; - } - - $bool = $this->DBTransaction->insert('barrier', [ - 'trans_type' => $transType, - 'gid' => $gid, - 'branch_id' => $branchId, - 'op' => $op, - 'barrier_id' => $barrierID, - 'reason' => $reason, - ]); - - return (int)$bool; - } - - public function queryPrepared(string $transType, string $gid): bool { - $this->DBTransaction->insert('barrier', [ + $db = $this->DBTransaction->connection(); + $table = $db->raw('barrier'); + $db->table($table)->insertOrIgnore([ 'trans_type' => $transType, 'gid' => $gid, 'branch_id' => Branch::MsgDoBranch0, @@ -44,16 +27,34 @@ public function queryPrepared(string $transType, string $gid): bool 'reason' => Operation::ROLLBACK, ]); - $reason = $this->DBTransaction->queryBuilder('barrier','barrier', [ - 'gid' => $gid, - 'branch_id' => Branch::MsgDoBranch0, - 'op' => Branch::MsgDoOp, - 'barrier_id' => Branch::MsgDoBarrier1, - ])[0] ?? []; - - if ($reason['reason'] ?? '' == Operation::ROLLBACK) { + $reason = $db->table($table) + ->select('reason') + ->where([ + 'gid' => $gid, + 'branch_id' => Branch::MsgDoBranch0, + 'op' => Branch::MsgDoOp, + 'barrier_id' => Branch::MsgDoBarrier1, + ])->first(); + if ($reason->reason == Operation::ROLLBACK) { throw new FailureException(); } return true; } + + protected function insertBarrier(string $transType, string $gid, string $branchId, string $op, string $barrierID, string $reason): int + { + if (empty($op)) { + return 0; + } + $db = $this->DBTransaction->connection(); + $table = $db->raw('barrier'); + return $db->table($table)->insertOrIgnore([ + 'trans_type' => $transType, + 'gid' => $gid, + 'branch_id' => $branchId, + 'op' => $op, + 'barrier_id' => $barrierID, + 'reason' => $reason, + ]); + } } diff --git a/src/DbTransaction/DBTransactionInterface.php b/src/DbTransaction/DBTransactionInterface.php index 382d1b9..53c3f6b 100644 --- a/src/DbTransaction/DBTransactionInterface.php +++ b/src/DbTransaction/DBTransactionInterface.php @@ -20,12 +20,13 @@ public function rollback(); public function execute(string $sql, array $bindings = []): int; - public function insert(string $table, array $data = []): bool; + /** + * @return \Hyperf\Database\ConnectionInterface|\Illuminate\Database\Connection + */ + public function connection(); public function query(string $sql, array $bindings = []): bool|array; - public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array; - public function xaExecute(string $sql, array $bindings = []): int; public function xaQuery(string $sql, array $bindings = []): bool|array; diff --git a/src/DbTransaction/HyperfDbTransaction.php b/src/DbTransaction/HyperfDbTransaction.php index d9850bc..57c6e41 100644 --- a/src/DbTransaction/HyperfDbTransaction.php +++ b/src/DbTransaction/HyperfDbTransaction.php @@ -42,14 +42,8 @@ public function query(string $sql, array $bindings = []): bool|array { return Db::select($sql, $bindings); } - - public function insert(string $table, array $data = []): bool - { - return Db::table($table)->insert($data); - } - - public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array + public function connection() { - return Db::table($table)->select($select)->where($where)->limit($limit)->get()->toArray(); + return Db::connection(); } } diff --git a/src/DbTransaction/HyperfSimpleDbTransaction.php b/src/DbTransaction/HyperfSimpleDbTransaction.php index f1c46f8..1518101 100644 --- a/src/DbTransaction/HyperfSimpleDbTransaction.php +++ b/src/DbTransaction/HyperfSimpleDbTransaction.php @@ -43,13 +43,7 @@ public function query(string $sql, array $bindings = []): bool|array return DB::query($sql, $bindings); } - - public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array - { - throw new \Exception('Not implemented'); - } - - public function insert(string $table, array $data = []): bool + public function connection() { throw new \Exception('Not implemented'); } diff --git a/src/DbTransaction/LaravelDbTransaction.php b/src/DbTransaction/LaravelDbTransaction.php index 0932234..0c37076 100644 --- a/src/DbTransaction/LaravelDbTransaction.php +++ b/src/DbTransaction/LaravelDbTransaction.php @@ -39,13 +39,8 @@ public function query(string $sql, array $bindings = []): bool|array return DB::select($sql, $bindings); } - public function insert(string $table, array $data = []): bool + public function connection() { - return DB::table($table)->insert($data); - } - - public function queryBuilder(string $table, mixed $select, array $where = [], int $limit = 1): array - { - return DB::table($table)->select($select)->where($where)->limit($limit)->get()->toArray(); + return DB::connection(); } } From bee9c6c589fb57de64355fd92b12e049bfbf8d2c Mon Sep 17 00:00:00 2001 From: tw Date: Wed, 28 Jan 2026 11:58:44 +0800 Subject: [PATCH 09/20] =?UTF-8?q?=E4=BC=98=E5=8C=96msg=20call=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Msg.php | 2 +- src/MySqlBarrier.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Msg.php b/src/Msg.php index 466b0cd..def35fd 100644 --- a/src/Msg.php +++ b/src/Msg.php @@ -75,7 +75,7 @@ public function doAndSubmit(string $queryPrepared, callable $businessCall) $this->barrier->barrierFrom(TransType::MSG, TransContext::getGid(), '00', 'msg'); $this->prepare($queryPrepared); try { - $businessCall(); + $this->barrier->call($businessCall); $this->submit(); } catch (FailureException $failureException) { $this->api->abort([ diff --git a/src/MySqlBarrier.php b/src/MySqlBarrier.php index 5aa8b73..d581d5d 100644 --- a/src/MySqlBarrier.php +++ b/src/MySqlBarrier.php @@ -15,6 +15,7 @@ use DtmClient\DbTransaction\DBTransactionInterface; use DtmClient\Exception\DuplicatedException; use DtmClient\Exception\FailureException; +use Psr\Http\Message\ResponseInterface; class MySqlBarrier implements BarrierInterface { @@ -40,7 +41,7 @@ public function call(callable $businessCall): bool $originOP = [ Branch::BranchCancel => Branch::BranchTry, Branch::BranchCompensate => Branch::BranchAction, - Branch::BranchRollback => Branch::BranchRollback, + Branch::BranchRollback => Branch::BranchAction, ][$op] ?? ''; $this->DBTransaction->beginTransaction(); @@ -62,7 +63,7 @@ public function call(callable $businessCall): bool } $response = $businessCall(); - if ($response->getStatusCode() !== Result::SUCCESS_STATUS) { + if ($response instanceof ResponseInterface && $response->getStatusCode() !== Result::SUCCESS_STATUS) { throw new FailureException(); } From a034493a200872c44a06c386f2e5ebac24135d5d Mon Sep 17 00:00:00 2001 From: tw Date: Wed, 28 Jan 2026 13:37:30 +0800 Subject: [PATCH 10/20] Format code --- src/DbTransaction/DBTransactionInterface.php | 6 +++--- src/DbTransaction/HyperfDbTransaction.php | 1 + src/DbTransaction/HyperfSimpleDbTransaction.php | 5 +++-- src/DbTransaction/LaravelDbTransaction.php | 7 ++----- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/DbTransaction/DBTransactionInterface.php b/src/DbTransaction/DBTransactionInterface.php index 53c3f6b..2222ab2 100644 --- a/src/DbTransaction/DBTransactionInterface.php +++ b/src/DbTransaction/DBTransactionInterface.php @@ -20,13 +20,13 @@ public function rollback(); public function execute(string $sql, array $bindings = []): int; + public function query(string $sql, array $bindings = []): bool|array; + /** - * @return \Hyperf\Database\ConnectionInterface|\Illuminate\Database\Connection + * @return \Hyperf\Database\ConnectionInterface|\Illuminate\Database\ConnectionInterface */ public function connection(); - public function query(string $sql, array $bindings = []): bool|array; - public function xaExecute(string $sql, array $bindings = []): int; public function xaQuery(string $sql, array $bindings = []): bool|array; diff --git a/src/DbTransaction/HyperfDbTransaction.php b/src/DbTransaction/HyperfDbTransaction.php index 57c6e41..d99a64b 100644 --- a/src/DbTransaction/HyperfDbTransaction.php +++ b/src/DbTransaction/HyperfDbTransaction.php @@ -42,6 +42,7 @@ public function query(string $sql, array $bindings = []): bool|array { return Db::select($sql, $bindings); } + public function connection() { return Db::connection(); diff --git a/src/DbTransaction/HyperfSimpleDbTransaction.php b/src/DbTransaction/HyperfSimpleDbTransaction.php index 1518101..c771960 100644 --- a/src/DbTransaction/HyperfSimpleDbTransaction.php +++ b/src/DbTransaction/HyperfSimpleDbTransaction.php @@ -8,6 +8,7 @@ */ namespace DtmClient\DbTransaction; +use Exception; use Hyperf\Contract\ConfigInterface; use Hyperf\DB\DB; @@ -38,13 +39,13 @@ public function execute(string $sql, array $bindings = []): int return DB::execute($sql, $bindings); } - public function query(string $sql, array $bindings = []): bool|array + public function query(string $sql, array $bindings = []): array|bool { return DB::query($sql, $bindings); } public function connection() { - throw new \Exception('Not implemented'); + throw new Exception('Not implemented'); } } diff --git a/src/DbTransaction/LaravelDbTransaction.php b/src/DbTransaction/LaravelDbTransaction.php index 0c37076..3838fab 100644 --- a/src/DbTransaction/LaravelDbTransaction.php +++ b/src/DbTransaction/LaravelDbTransaction.php @@ -1,11 +1,7 @@ Date: Wed, 28 Jan 2026 13:41:07 +0800 Subject: [PATCH 11/20] Format code --- src/DbTransaction/HyperfSimpleDbTransaction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DbTransaction/HyperfSimpleDbTransaction.php b/src/DbTransaction/HyperfSimpleDbTransaction.php index c771960..ab47f8a 100644 --- a/src/DbTransaction/HyperfSimpleDbTransaction.php +++ b/src/DbTransaction/HyperfSimpleDbTransaction.php @@ -39,7 +39,7 @@ public function execute(string $sql, array $bindings = []): int return DB::execute($sql, $bindings); } - public function query(string $sql, array $bindings = []): array|bool + public function query(string $sql, array $bindings = []): bool|array { return DB::query($sql, $bindings); } From 6363b01687aa1edd37b9ce1360900ba17aa48700 Mon Sep 17 00:00:00 2001 From: tw Date: Wed, 28 Jan 2026 14:22:51 +0800 Subject: [PATCH 12/20] Format code --- src/DbBarrier.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DbBarrier.php b/src/DbBarrier.php index 7394d09..29038a1 100644 --- a/src/DbBarrier.php +++ b/src/DbBarrier.php @@ -35,6 +35,7 @@ public function queryPrepared(string $transType, string $gid): bool 'op' => Branch::MsgDoOp, 'barrier_id' => Branch::MsgDoBarrier1, ])->first(); + if ($reason->reason == Operation::ROLLBACK) { throw new FailureException(); } From 80f38d10764f03d1ec7378e4dffd797d8cae0411 Mon Sep 17 00:00:00 2001 From: tw Date: Wed, 28 Jan 2026 19:38:33 +0800 Subject: [PATCH 13/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0msg=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-CN.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/README-CN.md b/README-CN.md index 463ff8f..b4dd181 100644 --- a/README-CN.md +++ b/README-CN.md @@ -375,3 +375,73 @@ class XAController ``` 上面的代码首先注册了一个全局XA事务,然后添加了两个子事务transIn、transOut。子事务全部执行成功之后,提交给dtm。dtm收到提交的xa全局事务后,会调用所有子事务的xa commit,完成整个xa事务。 + +## 二阶段消息 + +### 代码示例 + +以下展示在 Hyperf 框架中的使用方法,其它框架类似 + +```php +msg = $msg; + } + + #[RequestMapping(path: 'msg')] + public function msg() + { + $this->msg->init(); + $this->msg->add('http://127.0.0.1:19501/msg/test', ['name' => 'dtmMsg']); + //添加Topic + //$this->msg->addTopic('TransIn', ['name' => 'Topic dtmMsg']); + + $this->msg->doAndSubmit('http://127.0.0.1:19501/msg/queryPrepared', function () { + var_dump('执行业务'); + }); + return TransContext::getGid(); + } + + #[RequestMapping(path: 'queryPrepared')] + public function queryPrepared(Barrier $barrier) + { + var_dump(__METHOD__); + $transType = $this->request->query('trans_type'); + $gid = $this->request->query('gid'); + $barrier->queryPrepared($transType, $gid); + } + + #[RequestMapping(path: 'test')] + public function test() + { + var_dump(__METHOD__); + return 'rest'; + } + + #[RequestMapping(path: 'subscribe')] + public function subscribe() + { + //订阅 + $this->msg->subscribe('TransIn', 'http://127.0.0.1:19501/msg/test', 'subscribe test'); + return 'subscribe'; + } +} +``` \ No newline at end of file From 037b5f3861706d8f3ef4f4bddbe06d18190294ce Mon Sep 17 00:00:00 2001 From: tw Date: Thu, 29 Jan 2026 14:58:17 +0800 Subject: [PATCH 14/20] TCC add branchHeaders --- src/TCC.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/TCC.php b/src/TCC.php index ec313dd..c36589a 100644 --- a/src/TCC.php +++ b/src/TCC.php @@ -73,6 +73,7 @@ public function callBranch($body, string $tryUrl, string $confirmUrl, string $ca $branchRequest->branchId = $branchId; $branchRequest->op = Operation::TRY; $branchRequest->body = $body; + $branchRequest->branchHeaders = TransContext::getBranchHeaders(); return $this->api->transRequestBranch($branchRequest); case Protocol::GRPC: if (! $body instanceof Message) { From 7c3e307cf21cd5945a62497eb24d0054dd77a858 Mon Sep 17 00:00:00 2001 From: tw Date: Fri, 30 Jan 2026 10:29:56 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Middleware/DtmMiddleware.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Middleware/DtmMiddleware.php b/src/Middleware/DtmMiddleware.php index 9c785d3..e3b3cea 100644 --- a/src/Middleware/DtmMiddleware.php +++ b/src/Middleware/DtmMiddleware.php @@ -16,6 +16,7 @@ use DtmClient\Exception\OngingException; use DtmClient\Exception\RuntimeException; use Hyperf\Contract\ConfigInterface; +use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Di\Annotation\AnnotationCollector; use Hyperf\Grpc\StatusCode; use Hyperf\HttpMessage\Stream\SwooleStream; @@ -33,11 +34,14 @@ class DtmMiddleware implements MiddlewareInterface protected ConfigInterface $config; - public function __construct(Barrier $barrier, ResponseInterface $response, ConfigInterface $config) + protected StdoutLoggerInterface $logger; + + public function __construct(Barrier $barrier, ResponseInterface $response, ConfigInterface $config, StdoutLoggerInterface $logger) { $this->barrier = $barrier; $this->response = $response; $this->config = $config; + $this->logger = $logger; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface @@ -137,6 +141,7 @@ protected function handlerBarrierCall(callable $businessCall): ResponseInterface $this->isGRPC() && $response = $response->withTrailer('grpc-status', (string) $failureException->getCode())->withTrailer('grpc-message', $failureException->getMessage()); return $response; } catch (\Throwable $throwable) { + $this->logger->error($throwable); $code = $this->isGRPC() ? 200 : Result::FAILURE_STATUS; $response = $response->withStatus($code); $this->isGRPC() && $response = $response->withTrailer('grpc-status', (string) Result::FAILURE_STATUS)->withTrailer('grpc-message', $throwable->getMessage()); From f6deddcb9420f95a31bbb29a221bf143b837a73e Mon Sep 17 00:00:00 2001 From: tangwei Date: Fri, 30 Jan 2026 22:26:19 +0800 Subject: [PATCH 16/20] add doAndSubmitDB method --- src/Msg.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Msg.php b/src/Msg.php index def35fd..663c2f0 100644 --- a/src/Msg.php +++ b/src/Msg.php @@ -1,11 +1,7 @@ api->submit(TransContext::toArray()); } + public function doAndSubmitDB(string $queryPrepared, callable $businessCall) + { + $this->doAndSubmit($queryPrepared, fn () => $this->barrier->call($businessCall)); + } + public function doAndSubmit(string $queryPrepared, callable $businessCall) { $this->barrier->barrierFrom(TransType::MSG, TransContext::getGid(), '00', 'msg'); $this->prepare($queryPrepared); try { - $this->barrier->call($businessCall); + $businessCall(); $this->submit(); } catch (FailureException $failureException) { $this->api->abort([ @@ -83,7 +84,7 @@ public function doAndSubmit(string $queryPrepared, callable $businessCall) 'trans_type' => TransType::MSG, ]); throw $failureException; - } catch (\Exception $exception) { + } catch (Exception $exception) { $this->queryPrepared($queryPrepared); throw $exception; } From 6203351ad53c82a449e389539acf43485946aef1 Mon Sep 17 00:00:00 2001 From: tangwei Date: Fri, 30 Jan 2026 22:29:38 +0800 Subject: [PATCH 17/20] optimize --- README-CN.md | 2 +- src/Msg.php | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README-CN.md b/README-CN.md index b4dd181..36a3688 100644 --- a/README-CN.md +++ b/README-CN.md @@ -414,7 +414,7 @@ class MsgController extends AbstractSagaController //添加Topic //$this->msg->addTopic('TransIn', ['name' => 'Topic dtmMsg']); - $this->msg->doAndSubmit('http://127.0.0.1:19501/msg/queryPrepared', function () { + $this->msg->doAndSubmitDB('http://127.0.0.1:19501/msg/queryPrepared', function () { var_dump('执行业务'); }); return TransContext::getGid(); diff --git a/src/Msg.php b/src/Msg.php index 663c2f0..c41efe8 100644 --- a/src/Msg.php +++ b/src/Msg.php @@ -1,7 +1,11 @@ TransType::MSG, ]); throw $failureException; - } catch (Exception $exception) { + } catch (\Exception $exception) { $this->queryPrepared($queryPrepared); throw $exception; } From b9fe31351e42c1099d173da9a52a700d9ed0e6b3 Mon Sep 17 00:00:00 2001 From: tangwei Date: Sun, 1 Feb 2026 22:59:47 +0800 Subject: [PATCH 18/20] optimize return array|ResponseInterface --- src/TCC.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TCC.php b/src/TCC.php index ec313dd..06c1e82 100644 --- a/src/TCC.php +++ b/src/TCC.php @@ -16,6 +16,7 @@ use DtmClient\Exception\InvalidArgumentException; use DtmClient\Exception\UnsupportedException; use Google\Protobuf\Internal\Message; +use Psr\Http\Message\ResponseInterface; class TCC extends AbstractTransaction { @@ -51,6 +52,7 @@ public function globalTransaction(callable $callback, ?string $gid = null) /** * @param array|Message $body + * @return array|ResponseInterface */ public function callBranch($body, string $tryUrl, string $confirmUrl, string $cancelUrl) { From edae028f5cf03565fcea0f946ba7fe8e4064e030 Mon Sep 17 00:00:00 2001 From: tw Date: Wed, 4 Feb 2026 11:10:22 +0800 Subject: [PATCH 19/20] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95-=E8=BD=ACstring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Middleware/DtmMiddleware.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Middleware/DtmMiddleware.php b/src/Middleware/DtmMiddleware.php index e3b3cea..33eed7b 100644 --- a/src/Middleware/DtmMiddleware.php +++ b/src/Middleware/DtmMiddleware.php @@ -141,7 +141,7 @@ protected function handlerBarrierCall(callable $businessCall): ResponseInterface $this->isGRPC() && $response = $response->withTrailer('grpc-status', (string) $failureException->getCode())->withTrailer('grpc-message', $failureException->getMessage()); return $response; } catch (\Throwable $throwable) { - $this->logger->error($throwable); + $this->logger->error((string)$throwable); $code = $this->isGRPC() ? 200 : Result::FAILURE_STATUS; $response = $response->withStatus($code); $this->isGRPC() && $response = $response->withTrailer('grpc-status', (string) Result::FAILURE_STATUS)->withTrailer('grpc-message', $throwable->getMessage()); From 1cf7e3cfd51c6f9a9bdad659121d7b7449b51cd1 Mon Sep 17 00:00:00 2001 From: tangwei Date: Wed, 29 Jul 2026 11:46:23 +0800 Subject: [PATCH 20/20] =?UTF-8?q?=E9=80=82=E9=85=8Dhyperf=203.2=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将PHP最低版本从8.0提升到8.2 - 更新Hyperf相关依赖到3.2.0版本 - 修改BarrierTest构造函数注入DbBarrier实例 - 替换Hyperf\Context和Hyperf\Coroutine\Channel\Pool导入路径 - 添加Hyperf\Support\make函数导入 - 移除LaravelDbTransaction文件的PHPStan忽略规则 - 使用Context替代反射访问Saga内部属性 - 添加测试前清理Context状态的setUp方法 - 简化GitHub Actions工作流配置并优化测试环境设置 --- .github/workflows/test.yml | 101 ++++++--------------------- composer.json | 27 ++++--- phpstan.neon | 7 +- src/JsonRpc/JsonRpcClient.php | 2 + src/JsonRpc/JsonRpcClientManager.php | 2 + tests/Cases/Api/GrpcApiTest.php | 4 +- tests/Cases/BarrierTest.php | 11 ++- tests/Cases/SagaTest.php | 23 +++--- 8 files changed, 71 insertions(+), 106 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 50ab91b..97b579f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,93 +2,38 @@ name: PHPUnit on: [ push, pull_request ] -env: - SWOOLE_VERSION: '4.8.6' - SWOW_VERSION: 'develop' - jobs: - ci: - name: Test PHP ${{ matrix.php-version }} on ${{ matrix.engine }} - runs-on: "${{ matrix.os }}" + ci32: + runs-on: ubuntu-latest + container: hyperf/hyperf:${{ matrix.hyperf-version }} strategy: matrix: - os: [ ubuntu-latest ] - php-version: [ '8.0', '8.1' ] - engine: [ 'none', 'swoole', 'swow' ] - max-parallel: 6 + hyperf-version: + - "8.2-alpine-v3.22-swoole" + - "8.3-alpine-v3.23-swoole" + - "8.4-alpine-v3.23-swoole" fail-fast: false + max-parallel: 15 + steps: - name: Checkout - uses: actions/checkout@v3 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - tools: phpize - ini-values: | - opcache.enable_cli=1 - - coverage: none - - name: Setup Swoole - if: ${{ matrix.engine == 'swoole' }} + uses: actions/checkout@v4 + - name: Setup Environment run: | - sudo apt-get clean - sudo apt-get update - sudo apt-get upgrade -f - sudo apt-get install libcurl4-openssl-dev - wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz - mkdir -p swoole - tar -xf swoole.tar.gz -C swoole --strip-components=1 - rm swoole.tar.gz - cd swoole - phpize - ./configure --enable-openssl --enable-http2 --enable-swoole-curl --enable-swoole-json - make -j$(nproc) - sudo make install - sudo sh -c "echo extension=swoole > /etc/php/${{ matrix.php-version }}/cli/conf.d/swoole.ini" + pwd + ls -al + php -v + php -m php --ri swoole - - name: Setup Swow - if: ${{ matrix.engine == 'swow' }} - run: | - wget https://github.com/swow/swow/archive/"${SWOW_VERSION}".tar.gz -O swow.tar.gz - mkdir -p swow - tar -xf swow.tar.gz -C swow --strip-components=1 - rm swow.tar.gz - cd swow/ext || exit + composer -V - phpize - ./configure --enable-debug - make -j "$(nproc)" - sudo make install - sudo sh -c "echo extension=swow > /etc/php/${{ matrix.php-version }}/cli/conf.d/swow.ini" - php --ri swow - - - name: Setup Packages + - name: Install Dependencies run: | - composer update -o --no-scripts - composer update doctrine/instantiator:^1.0 - composer require symfony/finder:^5.0 - composer require hyperf/di - composer require hyperf/grpc-client - composer require hyperf/framework - composer require psr/log:^1.0 - composer require psr/container:^1.0 - composer require symfony/console:^5.0 - composer require hyperf/dispatcher - composer require hyperf/server - composer require hyperf/http-server - composer require hyperf/db - composer require hyperf/db-connection - composer require hyperf/json-rpc - composer require hyperf/rpc-client - composer require symfony/serializer:^5.0 + composer update -o + composer info - - name: Setup Swow Packages - if: ${{ matrix.engine == 'swow' }} - run: | - composer require hyperf/engine-swow + - name: Static Analysis + run: composer analyse - - name: Run Test Cases - run: | - composer analyse - composer test + - name: Run Tests + run: composer test \ No newline at end of file diff --git a/composer.json b/composer.json index d3a6f63..6eac07e 100644 --- a/composer.json +++ b/composer.json @@ -20,26 +20,35 @@ } }, "require": { - "php": ">=8.0", + "php": ">=8.2", "ext-json": "*", "guzzlehttp/guzzle": "^7.4", - "hyperf/context": "^2.2|^3.0|^3.1", + "hyperf/context": "~3.2.0", "psr/http-server-middleware": "^1.0" }, "require-dev": { + "hyperf/config": "~3.2.0", + "hyperf/db": "~3.2.0", + "hyperf/redis": "~3.2.0", + "hyperf/di": "~3.2.0", + "hyperf/grpc-client": "~3.2.0", + "hyperf/rpc-client": "~3.2.0", + "hyperf/json-rpc": "~3.2.0", + "hyperf/http-server": "~3.2.0", + "hyperf/db-connection": "~3.2.0", "friendsofphp/php-cs-fixer": "^3.0", "mockery/mockery": "^1.0", "phpstan/phpstan": "^1.0", "phpunit/phpunit": "^9.5" }, "suggest": { - "hyperf/config": "^2.2|^3.0|^3.1", - "hyperf/db": "^2,2|^3.0|^3.1", - "hyperf/redis": "^2,2|^3.0|^3.1", - "hyperf/di": "^2.2|^3.0|^3.1", - "hyperf/grpc-client": "^2.2|^3.0|^3.1", - "hyperf/rpc-client": "^2.2|^3.0|^3.1", - "hyperf/json-rpc": "^2.2|^3.0|^3.1", + "hyperf/config": "~3.2.0", + "hyperf/db": "~3.2.0", + "hyperf/redis": "~3.2.0", + "hyperf/di": "~3.2.0", + "hyperf/grpc-client": "~3.2.0", + "hyperf/rpc-client": "~3.2.0", + "hyperf/json-rpc": "~3.2.0", "ext-openssl": "Required to use HTTPS.", "ext-pdo": "Required to use MySQL Client.", "ext-pdo_mysql": "Required to use MySQL Client.", diff --git a/phpstan.neon b/phpstan.neon index 7e0bc12..dc005b9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,16 +5,13 @@ # parameters: level: 1 + excludePaths: + - */src/DbTransaction/LaravelDbTransaction.php ignoreErrors: - - '#Call to static method beginTransaction\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#' - - '#Call to static method commit\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#' - - '#Call to static method rollback\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#' - '#Static call to instance method Hyperf\\DB\\DB::execute\(\)\.#' - '#Static call to instance method Hyperf\\DB\\DB::beginTransaction\(\)\.#' - '#Static call to instance method Hyperf\\DB\\DB::commit\(\)\.#' - '#Static call to instance method Hyperf\\DB\\DB::rollback\(\)\.#' - '#Call to static method currentRouteAction\(\) on an unknown class Illuminate\\Support\\Facades\\Route\.#' - '#Parameter \$request of method DtmClient\\Middleware\\DtmLaravelMiddleware::handle\(\) has invalid type Illuminate\\Http\\Request\.#' - - '#Call to static method affectingStatement\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#' - '#Static call to instance method Hyperf\\DB\\DB::query\(\)\.#' - - '#Call to static method select\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#' diff --git a/src/JsonRpc/JsonRpcClient.php b/src/JsonRpc/JsonRpcClient.php index 15ae095..e118f05 100644 --- a/src/JsonRpc/JsonRpcClient.php +++ b/src/JsonRpc/JsonRpcClient.php @@ -17,6 +17,8 @@ use Hyperf\RpcClient\Exception\RequestException; use Psr\Container\ContainerInterface; +use function Hyperf\Support\make; + class JsonRpcClient extends AbstractServiceClient { public function __construct(ContainerInterface $container) diff --git a/src/JsonRpc/JsonRpcClientManager.php b/src/JsonRpc/JsonRpcClientManager.php index a5ee657..db491c2 100644 --- a/src/JsonRpc/JsonRpcClientManager.php +++ b/src/JsonRpc/JsonRpcClientManager.php @@ -8,6 +8,8 @@ */ namespace DtmClient\JsonRpc; +use function Hyperf\Support\make; + class JsonRpcClientManager { /** diff --git a/tests/Cases/Api/GrpcApiTest.php b/tests/Cases/Api/GrpcApiTest.php index 9d5b587..1171876 100644 --- a/tests/Cases/Api/GrpcApiTest.php +++ b/tests/Cases/Api/GrpcApiTest.php @@ -11,10 +11,10 @@ use DtmClient\Api\GrpcApi; use DtmClient\Grpc\GrpcClientManager; use DtmClientTest\Cases\AbstractTestCase; +use Hyperf\Context\ApplicationContext; +use Hyperf\Coroutine\Channel\Pool as ChannelPool; use Hyperf\Contract\ConfigInterface; use Hyperf\GrpcClient\BaseClient; -use Hyperf\Utils\ApplicationContext; -use Hyperf\Utils\ChannelPool; use Mockery; use Psr\Container\ContainerInterface; diff --git a/tests/Cases/BarrierTest.php b/tests/Cases/BarrierTest.php index 26c54d2..cde9637 100644 --- a/tests/Cases/BarrierTest.php +++ b/tests/Cases/BarrierTest.php @@ -6,6 +6,7 @@ use DtmClient\Constants\DbType; use DtmClient\Constants\Protocol; use DtmClient\Constants\TransType; +use DtmClient\DbBarrier; use DtmClient\MySqlBarrier; use DtmClient\TransContext; use Hyperf\Contract\ConfigInterface; @@ -21,7 +22,11 @@ public function testCall() $mySqlBarrier->method('call')->willReturn(true); - $barrier = new Barrier($configInterface, $mySqlBarrier); + $dbBarrier = $this->createMock(DbBarrier::class); + + $dbBarrier->method('call')->willReturn(true); + + $barrier = new Barrier($configInterface, $mySqlBarrier, $dbBarrier); $this->assertTrue($barrier->call(function () { return true; })); @@ -34,7 +39,9 @@ public function testBarrierFrom() $mySqlBarrier = $this->createMock(MySqlBarrier::class); - $barrier = new Barrier($configInterface, $mySqlBarrier); + $dbBarrier = $this->createMock(DbBarrier::class); + + $barrier = new Barrier($configInterface, $mySqlBarrier, $dbBarrier); $barrier->barrierFrom(TransType::TCC, 'gid', 'branchId', 'try', 'phase2Url', 'testDtm'); $this->assertSame(TransContext::toArray(), [ diff --git a/tests/Cases/SagaTest.php b/tests/Cases/SagaTest.php index c2b7b94..93d2cb6 100644 --- a/tests/Cases/SagaTest.php +++ b/tests/Cases/SagaTest.php @@ -5,12 +5,21 @@ use DtmClient\Api\ApiInterface; use DtmClient\Constants\Protocol; use DtmClient\Constants\TransType; +use DtmClient\Context\Context; use DtmClient\Grpc\Message\DtmBranchRequest; use DtmClient\Saga; use DtmClient\TransContext; class SagaTest extends AbstractTestCase { + protected function setUp(): void + { + parent::setUp(); + // 清理 Context,避免测试间状态泄漏 + Context::set('DtmClient\Saga.concurrent', false); + Context::set('DtmClient\Saga.orders', []); + } + public function testInit() { $api = \Mockery::mock(ApiInterface::class); @@ -83,9 +92,7 @@ public function testAddBranchOrder() $saga->addBranchOrder(1, ['preBranches']); $saga->addBranchOrder(2, ['preBranches1']); - $ordersProperty = new \ReflectionProperty($saga, 'orders'); - $ordersProperty->setAccessible(true); - $orders = $ordersProperty->getValue($saga); + $orders = Context::get('DtmClient\Saga.orders'); $this->assertEquals([1 => ['preBranches'], 2 => ['preBranches1']], $orders); } @@ -97,9 +104,7 @@ public function testEnableConcurrent() $saga->enableConcurrent(); - $concurrentProperty = new \ReflectionProperty($saga, 'concurrent'); - $concurrentProperty->setAccessible(true); - $concurrent = $concurrentProperty->getValue($saga); + $concurrent = Context::get('DtmClient\Saga.concurrent'); $this->assertTrue($concurrent); } @@ -110,10 +115,8 @@ public function testEnableConcurrentWithoutAddBranchOrders() $saga = new Saga($api); $saga->enableConcurrent(); - $ordersProperty = new \ReflectionProperty($saga, 'orders'); - $ordersProperty->setAccessible(true); - $orders = $ordersProperty->getValue($saga); - $this->assertSame(null, $orders ?: null); + $orders = Context::get('DtmClient\Saga.orders'); + $this->assertSame([], $orders); } public function testSubmit()