Skip to content

[BUG]MySqlBarrier中没有接收业务方法的结果,导致即使业务方法返回的是409状态码,中间件DtmMiddleware最终的返回结果还是200。 #66

@qiannianlei

Description

@qiannianlei
<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use _PHPStan_048a4e187\Nette\Neon\Exception;
use DtmClient\Annotation\Barrier;
use DtmClient\Api\ApiInterface;
use DtmClient\TCC;
use DtmClient\TransContext;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Throwable;

class TccController extends AbstractController
{
    #[Inject]
    protected TCC $tcc;

    #[Inject]
    protected ApiInterface $api;

    public function successCase()
    {
        try {
            $this->tcc->globalTransaction(function (TCC $tcc) {

                $tcc->callBranch(
                    ['trans_name' => 'trans_A'],
                    $this->serviceUri . '/tcc/transA/try',
                    $this->serviceUri . '/tcc/transA/confirm',
                    $this->serviceUri . '/tcc/transA/cancel'
                );

                $tcc->callBranch(
                    ['trans_name' => 'trans_B'],
                    $this->serviceUri . '/tcc/transB/try/fail',
                    $this->serviceUri . '/tcc/transB/confirm',
                    $this->serviceUri . '/tcc/transB/cancel'
                );

            });
        } catch (Throwable $e) {
            var_dump($e->getMessage(), $e->getTraceAsString());
        }
        return TransContext::getGid();
    }

    public function queryAllCase()
    {
        $result = $this->api->queryAll(['last_id' => '']);
        var_dump($result);
    }

    public function rollbackCase()
    {
        try {
            $this->tcc->globalTransaction(function (TCC $tcc) {
                $tcc->callBranch(
                    ['trans_name' => 'trans_A'],
                    $this->serviceUri . '/tcc/transA/try',
                    $this->serviceUri . '/tcc/transA/confirm',
                    $this->serviceUri . '/tcc/transA/cancel'
                );

                $tcc->callBranch(
                    ['trans_name' => 'trans_B'],
                    $this->serviceUri . '/tcc/transB/try/fail',
                    $this->serviceUri . '/tcc/transB/confirm',
                    $this->serviceUri . '/tcc/transB/cancel'
                );
            });
        } catch (Throwable $exception) {
            // Do Nothing
        }
    }

    #[Barrier]
    public function transATry(RequestInterface $request): array
    {
        echo 'transATry'.PHP_EOL;
        return [
            'dtm_result' => 'SUCCESS',
        ];
    }

    public function transAConfirm(RequestInterface $request): array
    {
        echo 'transAConfirm'.PHP_EOL;
        return [
            'dtm_result' => 'SUCCESS',
        ];
    }

    #[Barrier]
    public function transACancel(RequestInterface $request): array
    {
        echo 'transACancel'.PHP_EOL;
        return [
            'dtm_result' => 'SUCCESS',
        ];
    }

    #[Barrier]
    public function transBTry(): array
    {
        echo 'transBTry'.PHP_EOL;
        return [
            'dtm_result' => 'SUCCESS',
        ];
    }

    #[Barrier]
    public function transBTryFail(ResponseInterface $response)
    {
        echo 'transBTryFail'.PHP_EOL;
        return $response->withStatus(409);
    }

    public function transBConfirm(): array
    {
        echo 'transBConfirm'.PHP_EOL;
        return [
            'dtm_result' => 'SUCCESS',
        ];
    }

    #[Barrier]
    public function transBCancel(): array
    {
        echo 'transBCancel'.PHP_EOL;
        return [
            'dtm_result' => 'SUCCESS',
        ];
    }
}

运行结果

image

transBTryFail方法返回409,但整个事务还是成功了

修改点

  1. 中间件对处理结果返回
    image

  2. MysqlBarrier对返回值做判断
    image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions