Skip to content

Commit 0e19567

Browse files
committed
fix(sharding): ensure create table result is set before processing
1 parent ffb2f2f commit 0e19567

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Plugin/Sharding/CreateHandler.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,27 @@ protected static function getShardingFn(): Closure {
156156
/** @var array{0:array{data?:array{0:array{value:string}}}} $result */
157157
$value = simdjson_decode($result[0]['data'][0]['value'] ?? '[]', true);
158158

159-
/** @var array{result:string,status?:string,type?:string} $value */
159+
// FLOW EXPLANATION:
160+
// 1. Table->shard() creates initial state with status='processing', result=null
161+
// 2. Operator->checkTableStatus() monitors queue completion
162+
// 3. When all queue items processed, sets status='done' and result=response_body
163+
// 4. We wait for BOTH status completion AND result to be set
164+
// 5. Only then we can safely call Response::fromBody() with non-null result
165+
166+
/** @var array{result:?string,status?:string,type?:string} $value */
160167
$type = $value['type'] ?? 'unknown';
161168
$status = $value['status'] ?? 'processing';
162-
if ($type === 'create' && $status !== 'processing') {
163-
return TaskResult::fromResponse(Response::fromBody($value['result']));
169+
$result = $value['result'] ?? null;
170+
171+
// Only proceed when:
172+
// - Type is 'create' (table creation)
173+
// - Status is not 'processing' (operation completed)
174+
// - Result is not null (response body is available)
175+
if ($type === 'create' && $status !== 'processing' && $result !== null) {
176+
return TaskResult::fromResponse(Response::fromBody($result));
164177
}
165178
if ((time() - $ts) > $timeout) {
179+
Buddy::debugvv("Sharding: CreateHandler timeout exceeded for table {$payload->table}");
166180
break;
167181
}
168182
Coroutine::sleep(1);

src/Plugin/Sharding/Operator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,18 @@ public function checkTableStatus(string $table): bool {
291291
Buddy::debugvv("Sharding: table status of {$table}: queue size: {$queueSize}, processed: {$processed}");
292292

293293
$isProcessed = $processed === $queueSize;
294-
// Update the state
294+
// Update the state when all queue items are processed
295295
if ($isProcessed) {
296296
$result['status'] = 'done';
297+
298+
// Set the result field that CreateHandler waits for
299+
// - In DEBUG mode: return actual SHOW CREATE TABLE response for debugging
300+
// - In production: return empty success response
297301
$result['result'] = ConfigManager::getInt('DEBUG') && $result['type'] === 'create'
298302
? $this->client->sendRequest("SHOW CREATE TABLE {$table} OPTION force=1")->getBody()
299303
: TaskResult::none()->toString();
304+
305+
Buddy::debugvv("Sharding: table {$table} completed, setting result: " . substr($result['result'], 0, 100));
300306
$this->state->set($stateKey, $result);
301307
}
302308

0 commit comments

Comments
 (0)