From 286fc0f665af900c895a2a3904b85edeb2e71b21 Mon Sep 17 00:00:00 2001 From: pandaLIU <563883861@qq.com> Date: Fri, 26 Aug 2022 23:42:28 +0800 Subject: [PATCH 1/3] Updated RootContext --- src/Core/Context/RootContext.php | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Core/Context/RootContext.php b/src/Core/Context/RootContext.php index efecdf3..974342a 100644 --- a/src/Core/Context/RootContext.php +++ b/src/Core/Context/RootContext.php @@ -68,11 +68,15 @@ public static function bind(string $xid): void { if (! $xid) { static::log('debug', 'xid is blank, switch to unbind operation!'); + self::unbind(); } static::log('debug', 'Bind xid: ' . $xid); static::set(static::KEY_XID, $xid); } + /** + * Unbind xid. + */ public static function unbind(): ?string { $xid = static::getXID(); @@ -94,21 +98,34 @@ public static function unbindGlobalLockFlag(): void static::set(static::KEY_GLOBAL_LOCK_FLAG, null); } + /** + * In global transaction boolean. + */ public static function inGlobalTransaction(): bool { return static::getXID() !== null; } + /** + * In tcc branch boolean. + */ public static function inTccBranch(): bool { return static::getBranchType() === BranchType::TCC; } + + /** + * In saga branch boolean. + */ public static function inSagaBranch(): bool { return static::getBranchType() === BranchType::SAGA; } + /** + * get the branch type + */ public static function getBranchType(): ?int { $branchType = null; @@ -118,12 +135,22 @@ public static function getBranchType(): ?int return $branchType; } + /** + * bind branch type + */ public static function bindBranchType(int $branchType) { + if (empty($branchType)) { + throw new IllegalArgumentException('branchType must be not null'); + } + static::log('debug', 'Bind branch type %d', $branchType); static::set(static::KEY_BRANCH_TYPE, $branchType); } + /** + * unbind branch type + */ public static function unbindBranchType(): null|int { $prevBranchType = static::get(static::KEY_BRANCH_TYPE, null); @@ -132,6 +159,9 @@ public static function unbindBranchType(): null|int return $prevBranchType; } + /** + * requires global lock check + */ public static function requireGlobalLock(): bool { return static::get(static::KEY_GLOBAL_LOCK_FLAG) !== null; @@ -147,6 +177,14 @@ public static function assertNotInGlobalTransaction(): void } } + /** + * entry map + */ + public static function entries(): array + { + return Context::getContainer(); + } + private static function log($level = 'debug', ...$content) { if (! static::$logger instanceof LoggerInterface && ApplicationContext::hasContainer()) { From 746f1e61c923cf8b4fb0e4dae29ee8242848ecd3 Mon Sep 17 00:00:00 2001 From: pandaLIU <563883861@qq.com> Date: Mon, 29 Aug 2022 13:50:47 +0800 Subject: [PATCH 2/3] Fixed bind --- src/Core/Context/RootContext.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Core/Context/RootContext.php b/src/Core/Context/RootContext.php index 974342a..aa34158 100644 --- a/src/Core/Context/RootContext.php +++ b/src/Core/Context/RootContext.php @@ -69,9 +69,11 @@ public static function bind(string $xid): void if (! $xid) { static::log('debug', 'xid is blank, switch to unbind operation!'); self::unbind(); + } else { + static::log('debug', 'Bind xid: ' . $xid); + static::set(static::KEY_XID, $xid); } - static::log('debug', 'Bind xid: ' . $xid); - static::set(static::KEY_XID, $xid); + } /** From a6677eaa562a6a9a5ff2bf235b6cb12e48a211c3 Mon Sep 17 00:00:00 2001 From: pandaLIU <563883861@qq.com> Date: Mon, 29 Aug 2022 13:52:50 +0800 Subject: [PATCH 3/3] Updated Comments --- src/Core/Context/RootContext.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Core/Context/RootContext.php b/src/Core/Context/RootContext.php index aa34158..7b15f45 100644 --- a/src/Core/Context/RootContext.php +++ b/src/Core/Context/RootContext.php @@ -126,7 +126,7 @@ public static function inSagaBranch(): bool } /** - * get the branch type + * Get the branch type */ public static function getBranchType(): ?int { @@ -138,7 +138,7 @@ public static function getBranchType(): ?int } /** - * bind branch type + * Bind branch type */ public static function bindBranchType(int $branchType) { @@ -151,7 +151,7 @@ public static function bindBranchType(int $branchType) } /** - * unbind branch type + * Unbind branch type */ public static function unbindBranchType(): null|int { @@ -162,7 +162,7 @@ public static function unbindBranchType(): null|int } /** - * requires global lock check + * Requires global lock check */ public static function requireGlobalLock(): bool { @@ -180,7 +180,7 @@ public static function assertNotInGlobalTransaction(): void } /** - * entry map + * Entry map */ public static function entries(): array {