id )->orderBy('resource_pool_id')->orderBy('id')->lockForUpdate()->get(); } public static function findForPool($rows, int $resourcePoolId): ?CloudHostResourcePoolMembership { foreach ($rows as $row) { if ((int) $row->resource_pool_id === $resourcePoolId) { return $row; } } return null; } /** @return array */ public static function activeRows($rows): array { $active = []; foreach ($rows as $row) { if ((string) $row->scheduler_state === 'active') { $active[] = $row; } } usort($active, [self::class, 'compare']); return $active; } /** * A deterministic representative for compact legacy projections only. * Placement must always select an exact resource_pool_id and must not use * this method as scheduling authority. */ public static function primary($rows): ?CloudHostResourcePoolMembership { $rank = ['active' => 0, 'draining' => 1, 'disabled' => 2]; $ordered = []; foreach ($rows as $row) { $ordered[] = $row; } usort($ordered, static function ($left, $right) use ($rank): int { $leftState = $rank[(string) $left->scheduler_state] ?? 3; $rightState = $rank[(string) $right->scheduler_state] ?? 3; if ($leftState !== $rightState) { return $leftState <=> $rightState; } return self::compare($left, $right); }); return $ordered[0] ?? null; } /** @deprecated Use an exact resource-pool lookup for business decisions. */ public static function selected($rows): ?CloudHostResourcePoolMembership { return self::primary($rows); } /** * Insert or update exactly one membership. Other memberships are never * deleted or relabelled as a side effect. * * The caller holds the host row lock and passes the rows returned by * lockedRows(), preserving the global host -> memberships lock order. */ public static function upsertLocked( CloudHost $host, $rows, int $resourcePoolId, string $schedulerState, int $priority, int $weight, int $expectedRevision, int $now ): CloudHostResourcePoolMembership { self::assertValues( $resourcePoolId, $schedulerState, $priority, $weight, $expectedRevision ); $membership = self::findForPool($rows, $resourcePoolId); $actualRevision = $membership === null ? 0 : max(1, (int) $membership->revision); if ($actualRevision !== $expectedRevision) { throw new CloudError( '资源池成员关系已被修改,请刷新后重试', 409 ); } if ($actualRevision >= PHP_INT_MAX) { throw new CloudError('资源池成员关系版本已达到上限', 409); } if ($membership === null) { $membership = new CloudHostResourcePoolMembership(); $membership->host_id = (int) $host->id; $membership->resource_pool_id = $resourcePoolId; $membership->created_at = $now; } $membership->scheduler_state = $schedulerState; $membership->priority = $priority; $membership->weight = $weight; $membership->revision = $actualRevision + 1; $membership->updated_at = $now; $membership->save(); return $membership; } /** Delete exactly one membership after its caller has checked references. */ public static function removeLocked( $rows, int $resourcePoolId, int $expectedRevision ): void { if ($resourcePoolId < 1 || $expectedRevision < 1) { throw new CloudError('资源池成员关系版本无效,请刷新后重试', 422); } $membership = self::findForPool($rows, $resourcePoolId); if ($membership === null) { throw new CloudError('宿主机资源池成员关系不存在', 404); } if ((int) $membership->revision !== $expectedRevision) { throw new CloudError( '资源池成员关系已被修改,请刷新后重试', 409 ); } $membership->delete(); } public static function isState(string $value): bool { return in_array($value, self::STATES, true); } private static function assertValues( int $resourcePoolId, string $schedulerState, int $priority, int $weight, int $expectedRevision ): void { if ($resourcePoolId < 1) { throw new CloudError('资源池 ID 无效', 422); } if (!self::isState($schedulerState)) { throw new CloudError('调度状态无效', 422); } if ($priority < self::MIN_PRIORITY || $priority > self::MAX_PRIORITY) { throw new CloudError('资源池成员优先级无效', 422); } if ($weight < self::MIN_WEIGHT || $weight > self::MAX_WEIGHT) { throw new CloudError('资源池成员权重无效', 422); } if ($expectedRevision < 0) { throw new CloudError('资源池成员关系版本无效,请刷新后重试', 422); } } private static function compare($left, $right): int { return [ (int) $left->priority, (int) $left->resource_pool_id, (int) $left->id, ] <=> [ (int) $right->priority, (int) $right->resource_pool_id, (int) $right->id, ]; } }