You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
5.8 KiB
PHP
159 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostResourcePoolMembership;
|
|
|
|
/**
|
|
* Transitional authority for host-to-resource-pool scheduling membership.
|
|
*
|
|
* The membership table is canonical. cloud_hosts.resource_pool_id and the
|
|
* host scheduler state are maintained only as a compatibility projection for
|
|
* placement code that has not moved to the relation yet. Every writer first
|
|
* locks the host sentinel and then locks membership rows in resource-pool/id
|
|
* order.
|
|
*/
|
|
final class CloudHostPoolMembershipService
|
|
{
|
|
public const SAFE_PRIORITY = 0;
|
|
public const SAFE_WEIGHT = 100;
|
|
|
|
private const STATES = ['active', 'draining', 'disabled'];
|
|
|
|
/**
|
|
* Lock the authoritative rows and perform the one-time legacy backfill.
|
|
* The caller must already hold a FOR UPDATE lock on the host row.
|
|
*/
|
|
public static function lockedRows(CloudHost $host, int $now)
|
|
{
|
|
$rows = CloudHostResourcePoolMembership::where('host_id', (int) $host->id)
|
|
->orderBy('resource_pool_id')->orderBy('id')->lockForUpdate()->get();
|
|
if ($rows->isEmpty() && $host->resource_pool_id !== null) {
|
|
$row = new CloudHostResourcePoolMembership();
|
|
$row->host_id = (int) $host->id;
|
|
$row->resource_pool_id = (int) $host->resource_pool_id;
|
|
$row->scheduler_state = self::state((string) $host->scheduler_state);
|
|
$row->priority = self::SAFE_PRIORITY;
|
|
$row->weight = self::SAFE_WEIGHT;
|
|
$row->created_at = $row->updated_at = $now;
|
|
$row->save();
|
|
$rows->push($row);
|
|
}
|
|
return $rows;
|
|
}
|
|
|
|
/** Select the first-release placement relation deterministically. */
|
|
public static function selected(CloudHost $host, $rows): ?CloudHostResourcePoolMembership
|
|
{
|
|
$active = null;
|
|
$draining = null;
|
|
$latest = null;
|
|
$activeCount = 0;
|
|
foreach ($rows as $row) {
|
|
if ((string) $row->scheduler_state === 'active') {
|
|
$activeCount++;
|
|
$active = $row;
|
|
}
|
|
if ((string) $row->scheduler_state === 'draining'
|
|
&& ($draining === null || self::newer($row, $draining))) {
|
|
$draining = $row;
|
|
}
|
|
if ($latest === null || self::newer($row, $latest)) {
|
|
$latest = $row;
|
|
}
|
|
}
|
|
if ($activeCount > 1) {
|
|
throw new CloudError('宿主机存在多个启用中的资源池成员关系,请先修复调度配置', 409);
|
|
}
|
|
return $active ?? $draining ?? $latest;
|
|
}
|
|
|
|
/**
|
|
* Replace the first-release selected membership. Until the scheduler can
|
|
* express several memberships in its write API, one host has zero or one
|
|
* authoritative row; the relation schema itself remains ready for a later
|
|
* multi-pool rollout.
|
|
*/
|
|
public static function replaceLocked(CloudHost $host, ?int $poolId,
|
|
string $schedulerState, int $now): ?CloudHostResourcePoolMembership
|
|
{
|
|
if (!in_array($schedulerState, self::STATES, true)) {
|
|
throw new CloudError('调度状态无效');
|
|
}
|
|
if ($poolId === null && $schedulerState !== 'disabled') {
|
|
throw new CloudError('未选择资源池时只能停用调度', 409);
|
|
}
|
|
|
|
$rows = self::lockedRows($host, $now);
|
|
$target = null;
|
|
foreach ($rows as $row) {
|
|
if ($poolId !== null && (int) $row->resource_pool_id === $poolId) {
|
|
$target = $row;
|
|
}
|
|
}
|
|
if ($poolId !== null && $target === null) {
|
|
$target = new CloudHostResourcePoolMembership();
|
|
$target->host_id = (int) $host->id;
|
|
$target->resource_pool_id = $poolId;
|
|
$target->created_at = $now;
|
|
$rows->push($target);
|
|
}
|
|
|
|
foreach ($rows as $row) {
|
|
if ($target !== null
|
|
&& (int) $row->resource_pool_id === (int) $target->resource_pool_id) {
|
|
continue;
|
|
}
|
|
$row->delete();
|
|
}
|
|
if ($target !== null) {
|
|
$target->scheduler_state = $schedulerState;
|
|
$target->priority = self::SAFE_PRIORITY;
|
|
$target->weight = self::SAFE_WEIGHT;
|
|
$target->updated_at = $now;
|
|
$target->save();
|
|
self::assertSingleActive([$target]);
|
|
}
|
|
|
|
// Temporary mirror only. Membership rows above are the authority.
|
|
$host->resource_pool_id = $target?->resource_pool_id;
|
|
$host->scheduler_state = $target === null ? 'disabled' : $schedulerState;
|
|
return $target;
|
|
}
|
|
|
|
/** Heal an old scalar projection after a successful relation backfill. */
|
|
public static function mirrorLocked(CloudHost $host,
|
|
?CloudHostResourcePoolMembership $selected): void
|
|
{
|
|
$host->resource_pool_id = $selected?->resource_pool_id;
|
|
$host->scheduler_state = $selected === null
|
|
? 'disabled' : self::state((string) $selected->scheduler_state);
|
|
}
|
|
|
|
private static function assertSingleActive($rows): void
|
|
{
|
|
$active = 0;
|
|
foreach ($rows as $row) {
|
|
if ((string) $row->scheduler_state === 'active') {
|
|
$active++;
|
|
}
|
|
}
|
|
if ($active > 1) {
|
|
throw new CloudError('宿主机存在多个启用中的资源池成员关系,请先修复调度配置', 409);
|
|
}
|
|
}
|
|
|
|
private static function state(string $state): string
|
|
{
|
|
return in_array($state, self::STATES, true) ? $state : 'disabled';
|
|
}
|
|
|
|
private static function newer($left, $right): bool
|
|
{
|
|
return (int) $left->updated_at > (int) $right->updated_at
|
|
|| ((int) $left->updated_at === (int) $right->updated_at
|
|
&& (int) $left->id > (int) $right->id);
|
|
}
|
|
}
|