Skip to content

Commit

Permalink
feat: add more debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
allsilaevex committed Mar 5, 2024
1 parent cc13b49 commit e5985da
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/pool/Pool.php
Expand Up @@ -113,7 +113,11 @@ public function borrow(): mixed
'item_old_state' => $poolItemWrapper->getState()->name,
'item_new_state' => $stateForUpdate->name,
];
$errorMessage = sprintf('Can\'t set %s state', $stateForUpdate->name);
$errorMessage = sprintf(
'Can\'t set %s state (old state %s)',
$stateForUpdate->name,
$poolItemWrapper->getState()->name,
);

$this->logger->warning($errorMessage, $context);

Expand Down
58 changes: 54 additions & 4 deletions src/pool/PoolItemWrapper.php
Expand Up @@ -116,15 +116,44 @@ public function getState(): PoolItemState

public function setState(PoolItemState $state): void
{
if ($this->stateStatuses[$this->state->value]->pop(self::CHANNEL_TIMEOUT_SEC) === false) {
throw new LogicException();
$currentState = $this->state;
$statusesSnapshot = $this->takeStatusesSnapshot();

if ($this->stateStatuses[$currentState->value]->pop(self::CHANNEL_TIMEOUT_SEC) === false) {
$debug = [
'before pop' => [
'current state' => $currentState->value,
'new state' => $state->value,
'statuses' => $statusesSnapshot,
],
'after pop' => [
'current state' => $this->state->value,
'new state' => $state->value,
'statuses' => $this->takeStatusesSnapshot(),
],
];

throw new LogicException('debug info = ' . \json_encode($debug));
}

$this->state = $state;
$this->stateUpdatedAt = hrtime(true);

$statusesSnapshot = $this->takeStatusesSnapshot();

if (!$this->stateStatuses[$state->value]->push(true, self::CHANNEL_TIMEOUT_SEC)) {
throw new LogicException();
$debug = [
'before push' => [
'current state' => $state->value,
'statuses' => $statusesSnapshot,
],
'after push' => [
'current state' => $this->state->value,
'statuses' => $this->takeStatusesSnapshot(),
],
];

throw new LogicException('debug info = ' . \json_encode($debug));
}
}

Expand All @@ -149,8 +178,21 @@ public function waitForCompareAndSetState(PoolItemState $expect, PoolItemState $
$this->state = $update;
$this->stateUpdatedAt = hrtime(true);

$statusesSnapshot = $this->takeStatusesSnapshot();

if (!$this->stateStatuses[$update->value]->push(true, self::CHANNEL_TIMEOUT_SEC)) {
throw new LogicException();
$debug = [
'before push' => [
'current state' => $update->value,
'statuses' => $statusesSnapshot,
],
'after push' => [
'current state' => $this->state->value,
'statuses' => $this->takeStatusesSnapshot(),
],
];

throw new LogicException('debug info = ' . \json_encode($debug));
}

return true;
Expand Down Expand Up @@ -180,4 +222,12 @@ public function stats(): array
'current_state_duration_sec' => (hrtime(true) - $this->stateUpdatedAt) * 1e-9,
];
}

/**
* @return array<value-of<PoolItemState>, array<mixed>>
*/
protected function takeStatusesSnapshot(): array
{
return array_map(static fn (Channel $status) => $status->stats(), $this->stateStatuses);
}
}

0 comments on commit e5985da

Please sign in to comment.