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.
119 lines
4.0 KiB
PHP
119 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostResourcePoolMembership;
|
|
|
|
/** Authoritative host-to-resource-pool scheduling membership. */
|
|
final class CloudHostPoolMembershipService
|
|
{
|
|
public const SAFE_PRIORITY = 0;
|
|
public const SAFE_WEIGHT = 100;
|
|
|
|
private const STATES = ['active', 'draining', 'disabled'];
|
|
|
|
/** The caller must already hold a FOR UPDATE lock on the host row. */
|
|
public static function lockedRows(CloudHost $host)
|
|
{
|
|
return CloudHostResourcePoolMembership::where('host_id', (int) $host->id)
|
|
->orderBy('resource_pool_id')->orderBy('id')->lockForUpdate()->get();
|
|
}
|
|
|
|
/** Select the first-release placement relation deterministically. */
|
|
public static function selected($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);
|
|
$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]);
|
|
}
|
|
|
|
return $target;
|
|
}
|
|
|
|
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 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);
|
|
}
|
|
}
|