Skip to content

Commit f5af597

Browse files
committed
fix(sharding): improve PHPStan type annotations and typings
- Add precise array shapes for health, queue, and cleanup methods - Fix nullable and optional keys in health check results - Replace null coalescing with explicit empty string in rollbackQuery - Add explicit type checks to prevent mixed types - Correct test imports for PHPStan compliance
1 parent d092d13 commit f5af597

File tree

9 files changed

+43
-42
lines changed

9 files changed

+43
-42
lines changed

src/Plugin/Sharding/CleanupManager.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(
1818

1919
/**
2020
* Comprehensive cleanup of all orphaned resources
21-
* @return array Cleanup results
21+
* @return array{timestamp: int, actions_taken: array<string>, resources_cleaned: int, errors: array<string>} Cleanup results
2222
*/
2323
public function performFullCleanup(): array {
2424
$results = [
@@ -58,7 +58,7 @@ public function performFullCleanup(): array {
5858

5959
/**
6060
* Find and clean up orphaned temporary clusters
61-
* @return array Cleanup results
61+
* @return array{cleaned_count: int, errors: array<string>} Cleanup results
6262
*/
6363
public function cleanupOrphanedTemporaryClusters(): array {
6464
$results = ['cleaned_count' => 0, 'errors' => []];
@@ -71,7 +71,7 @@ public function cleanupOrphanedTemporaryClusters(): array {
7171
$clusters = $clusterData[0]['data'] ?? [];
7272

7373
foreach ($clusters as $cluster) {
74-
$clusterName = $cluster['cluster'] ?? '';
74+
$clusterName = $cluster['cluster'];
7575

7676
// Check if it's a temporary cluster (starts with temp_move_)
7777
if (strpos($clusterName, 'temp_move_') !== 0) {
@@ -100,7 +100,7 @@ public function cleanupOrphanedTemporaryClusters(): array {
100100

101101
/**
102102
* Clean up failed operation groups from queue
103-
* @return array Cleanup results
103+
* @return array{cleaned_count: int, errors: array<string>} Cleanup results
104104
*/
105105
public function cleanupFailedOperationGroups(): array {
106106
$results = ['cleaned_count' => 0, 'errors' => []];
@@ -158,7 +158,7 @@ public function cleanupFailedOperationGroups(): array {
158158

159159
/**
160160
* Clean up expired queue items (older than 7 days)
161-
* @return array Cleanup results
161+
* @return array{cleaned_count: int, errors: array<string>} Cleanup results
162162
*/
163163
public function cleanupExpiredQueueItems(): array {
164164
$results = ['cleaned_count' => 0, 'errors' => []];
@@ -203,7 +203,7 @@ public function cleanupExpiredQueueItems(): array {
203203

204204
/**
205205
* Clean up stale state entries
206-
* @return array Cleanup results
206+
* @return array{cleaned_count: int, errors: array<string>} Cleanup results
207207
*/
208208
public function cleanupStaleStateEntries(): array {
209209
$results = ['cleaned_count' => 0, 'errors' => []];

src/Plugin/Sharding/Cluster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function runQuery(
109109
?string $operationGroup = null
110110
): int {
111111
if ($queue) {
112-
$queueId = $queue->add($this->nodeId, $query, $rollbackQuery, $operationGroup);
112+
$queueId = $queue->add($this->nodeId, $query, $rollbackQuery ?? '', $operationGroup);
113113
} else {
114114
$this->client->sendRequest($query, disableAgentHeader: true);
115115
}

src/Plugin/Sharding/HealthMonitor.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717

1818
/**
1919
* Perform comprehensive health check
20-
* @return array Health status
20+
* @return array{overall_status: string, timestamp: int, checks: array<string, mixed>, issues: array<mixed>, warnings: array<mixed>, recommendations: array<string>} Health status
2121
*/
2222
public function performHealthCheck(): array {
2323
$health = [
@@ -83,7 +83,7 @@ public function performHealthCheck(): array {
8383

8484
/**
8585
* Attempt automatic recovery for detected issues
86-
* @return array Recovery results
86+
* @return array{timestamp: int, actions_taken: array<string>, recovered_tables: array<string>, failed_recoveries: array<string>, cleanup_performed: bool} Recovery results
8787
*/
8888
public function performAutoRecovery(): array {
8989
$results = [
@@ -109,8 +109,8 @@ public function performAutoRecovery(): array {
109109

110110
/**
111111
* Process health issues and attempt recovery
112-
* @param array $issues
113-
* @param array &$results
112+
* @param array<mixed> $issues
113+
* @param array{timestamp: int, actions_taken: array<string>, recovered_tables: array<string>, failed_recoveries: array<string>, cleanup_performed: bool} &$results
114114
*/
115115
private function processHealthIssues(array $issues, array &$results): void {
116116
foreach ($issues as $issue) {
@@ -124,8 +124,8 @@ private function processHealthIssues(array $issues, array &$results): void {
124124

125125
/**
126126
* Recover stuck operations for given tables
127-
* @param array $tables
128-
* @param array &$results
127+
* @param array<string> $tables
128+
* @param array{timestamp: int, actions_taken: array<string>, recovered_tables: array<string>, failed_recoveries: array<string>, cleanup_performed: bool} &$results
129129
*/
130130
private function recoverStuckOperations(array $tables, array &$results): void {
131131
foreach ($tables as $tableName) {
@@ -144,8 +144,8 @@ private function recoverStuckOperations(array $tables, array &$results): void {
144144

145145
/**
146146
* Recover failed operations for given tables
147-
* @param array $tables
148-
* @param array &$results
147+
* @param array<string> $tables
148+
* @param array{timestamp: int, actions_taken: array<string>, recovered_tables: array<string>, failed_recoveries: array<string>, cleanup_performed: bool} &$results
149149
*/
150150
private function recoverFailedOperations(array $tables, array &$results): void {
151151
foreach ($tables as $tableName) {
@@ -164,8 +164,8 @@ private function recoverFailedOperations(array $tables, array &$results): void {
164164

165165
/**
166166
* Perform cleanup if warnings exist
167-
* @param array $warnings
168-
* @param array &$results
167+
* @param array<mixed> $warnings
168+
* @param array{timestamp: int, actions_taken: array<string>, recovered_tables: array<string>, failed_recoveries: array<string>, cleanup_performed: bool} &$results
169169
*/
170170
private function performCleanupIfNeeded(array $warnings, array &$results): void {
171171
if (empty($warnings)) {
@@ -180,7 +180,7 @@ private function performCleanupIfNeeded(array $warnings, array &$results): void
180180

181181
/**
182182
* Check for stuck rebalancing operations
183-
* @return array Check results
183+
* @return array{stuck_tables: array<string>, check_time: int, error?: string} Check results
184184
*/
185185
private function checkStuckOperations(): array {
186186
$results = ['stuck_tables' => [], 'check_time' => time()];
@@ -223,7 +223,7 @@ private function checkStuckOperations(): array {
223223

224224
/**
225225
* Check for failed operations
226-
* @return array Check results
226+
* @return array{failed_tables: array<string>, check_time: int, error?: string} Check results
227227
*/
228228
private function checkFailedOperations(): array {
229229
$results = ['failed_tables' => [], 'check_time' => time()];
@@ -262,7 +262,7 @@ private function checkFailedOperations(): array {
262262

263263
/**
264264
* Check for orphaned resources
265-
* @return array Check results
265+
* @return array{orphaned_count: int, details: array<string>, check_time: int, error?: string} Check results
266266
*/
267267
private function checkOrphanedResources(): array {
268268
$results = ['orphaned_count' => 0, 'details' => [], 'check_time' => time()];
@@ -275,7 +275,7 @@ private function checkOrphanedResources(): array {
275275
$clusters = $data[0]['data'] ?? [];
276276

277277
foreach ($clusters as $cluster) {
278-
$clusterName = $cluster['cluster'] ?? '';
278+
$clusterName = $cluster['cluster'];
279279
if (strpos($clusterName, 'temp_move_') !== 0) {
280280
continue;
281281
}
@@ -292,7 +292,7 @@ private function checkOrphanedResources(): array {
292292

293293
/**
294294
* Check queue health
295-
* @return array Check results
295+
* @return array{depth: int, threshold: int, high_depth: bool, check_time: int, error?: string} Check results
296296
*/
297297
private function checkQueueHealth(): array {
298298
$results = ['depth' => 0, 'threshold' => 100, 'high_depth' => false, 'check_time' => time()];
@@ -322,8 +322,8 @@ private function checkQueueHealth(): array {
322322

323323
/**
324324
* Generate recommendations based on health check
325-
* @param array $health
326-
* @return array Recommendations
325+
* @param array{overall_status: string, timestamp: int, checks: array<string, mixed>, issues: array<mixed>, warnings: array<mixed>, recommendations: array<string>} $health
326+
* @return array<string> Recommendations
327327
*/
328328
private function generateRecommendations(array $health): array {
329329
$recommendations = [];
@@ -361,7 +361,7 @@ private function generateRecommendations(array $health): array {
361361
/**
362362
* Recover stuck operation
363363
* @param string $tableName
364-
* @return array Recovery result
364+
* @return array{success: bool, message?: string, error?: string} Recovery result
365365
*/
366366
private function recoverStuckOperation(string $tableName): array {
367367
try {
@@ -377,7 +377,7 @@ private function recoverStuckOperation(string $tableName): array {
377377
/**
378378
* Recover failed operation
379379
* @param string $tableName
380-
* @return array Recovery result
380+
* @return array{success: bool, message?: string, error?: string} Recovery result
381381
*/
382382
private function recoverFailedOperation(string $tableName): array {
383383
try {

src/Plugin/Sharding/Queue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public function rollbackOperationGroup(string $operationGroup): bool {
356356
/**
357357
* Get rollback commands for operation group in reverse order
358358
* @param string $operationGroup
359-
* @return array Rollback commands in reverse execution order
359+
* @return array<array{id:int,node:string,rollback_query:string,rollback_wait_for_id:int}> Rollback commands in reverse execution order
360360
*/
361361
protected function getRollbackCommands(string $operationGroup): array {
362362
$table = $this->cluster->getSystemTableName($this->table);
@@ -379,7 +379,7 @@ protected function getRollbackCommands(string $operationGroup): array {
379379

380380
/**
381381
* Execute rollback commands in sequence
382-
* @param array $rollbackCommands
382+
* @param array<array{id:int,node:string,rollback_query:string,rollback_wait_for_id:int}> $rollbackCommands
383383
* @return bool Success status
384384
*/
385385
protected function executeRollbackSequence(array $rollbackCommands): bool {

src/Plugin/Sharding/Table.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,14 @@ public function rebalance(Queue $queue): void {
490490
$state->set("rebalance_group:{$this->name}", null); // Clear operation group
491491
} catch (\Throwable $t) {
492492
// Enhanced error handling with rollback
493-
$this->handleRebalancingFailure($t, $operationGroup ?? '', $queue, $state ?? null);
493+
$this->handleRebalancingFailure($t, $operationGroup, $queue, $state ?? null);
494494
}
495495
}
496496

497497
/**
498498
* Stop rebalancing operation
499499
* @param bool $graceful If true, complete current operation before stopping
500-
* @return array Status information
500+
* @return array{status: string, message: string, current_status?: string, graceful?: bool, rollback_executed?: bool} Status information
501501
*/
502502
public function stopRebalancing(bool $graceful = true): array {
503503
$state = new State($this->client);
@@ -524,7 +524,7 @@ public function stopRebalancing(bool $graceful = true): array {
524524

525525
// Immediate stop with rollback
526526
$operationGroup = $state->get("rebalance_group:{$this->name}");
527-
if ($operationGroup) {
527+
if (is_string($operationGroup)) {
528528
$queue = new Queue($this->cluster, $this->client);
529529
$queue->rollbackOperationGroup($operationGroup);
530530
}
@@ -576,7 +576,7 @@ public function resumeRebalancing(): bool {
576576

577577
/**
578578
* Get detailed rebalancing progress
579-
* @return array Progress information
579+
* @return array{status: string, table: string, operation_group: mixed, can_stop: bool, can_pause: bool, can_resume: bool, queue_total?: int, queue_completed?: int, queue_failed?: int, queue_pending?: int, progress_percentage?: float} Progress information
580580
*/
581581
public function getRebalancingProgress(): array {
582582
$state = new State($this->client);
@@ -593,7 +593,7 @@ public function getRebalancingProgress(): array {
593593
'can_resume' => $status === 'paused',
594594
];
595595

596-
if ($operationGroup) {
596+
if (is_string($operationGroup)) {
597597
// Get queue progress for this operation group
598598
$queueProgress = $this->getQueueProgress($operationGroup);
599599
$progress = array_merge($progress, $queueProgress);
@@ -709,7 +709,7 @@ protected function handleRebalancingFailure(
709709
/**
710710
* Get queue progress for operation group
711711
* @param string $operationGroup
712-
* @return array
712+
* @return array{queue_total: int, queue_completed: int, queue_failed: int, queue_pending: int, progress_percentage: float}
713713
*/
714714
protected function getQueueProgress(string $operationGroup): array {
715715
$table = $this->cluster->getSystemTableName('system.sharding_queue');
@@ -729,10 +729,10 @@ protected function getQueueProgress(string $operationGroup): array {
729729
$data = $result[0]['data'][0] ?? [];
730730

731731
return [
732-
'queue_total' => $data['total'] ?? 0,
733-
'queue_completed' => $data['completed'] ?? 0,
734-
'queue_failed' => $data['failed'] ?? 0,
735-
'queue_pending' => $data['pending'] ?? 0,
732+
'queue_total' => $data['total'],
733+
'queue_completed' => $data['completed'],
734+
'queue_failed' => $data['failed'],
735+
'queue_pending' => $data['pending'],
736736
'progress_percentage' => $data['total'] > 0
737737
? round(($data['completed'] / $data['total']) * 100, 2)
738738
: 0,

test/Plugin/Sharding/OutageQueueCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Manticoresearch\Buddy\Base\Plugin\Sharding\Queue;
1414
use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
1515
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableCluster;
16-
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableQueue;
1716
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableTable;
17+
use Manticoresearch\BuddyTest\Plugin\Sharding\TestDoubles\TestableQueue;
1818
use PHPUnit\Framework\TestCase;
1919

2020
/**

test/Plugin/Sharding/QueueCommandVerificationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Manticoresearch\Buddy\Base\Plugin\Sharding\Queue;
1414
use Manticoresearch\Buddy\Core\ManticoreSearch\Client;
1515
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableCluster;
16-
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableQueue;
1716
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableTable;
17+
use Manticoresearch\BuddyTest\Plugin\Sharding\TestDoubles\TestableQueue;
1818
use PHPUnit\Framework\TestCase;
1919

2020
/**

test/Plugin/Sharding/QueueRollbackIntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
final class QueueRollbackIntegrationTest extends TestCase {
2121

2222
private TestableQueue $queue;
23+
2324
private TestableTable $table;
2425

2526
protected function setUp(): void {

test/Plugin/Sharding/TableRebalanceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use Ds\Vector;
1414
use Manticoresearch\Buddy\Base\Plugin\Sharding\Util;
1515
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableCluster;
16-
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableQueue;
17-
use Manticoresearch\Buddy\Test\Plugin\Sharding\TestDoubles\TestableTable;
16+
use Manticoresearch\BuddyTest\Plugin\Sharding\TestDoubles\TestableQueue;
17+
use Manticoresearch\BuddyTest\Plugin\Sharding\TestDoubles\TestableTable;
1818
use PHPUnit\Framework\TestCase;
1919

2020
/** @package */

0 commit comments

Comments
 (0)