|
|
|
|
@ -16,6 +16,83 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
{
|
|
|
|
|
private const FAMILIES = [4, 6];
|
|
|
|
|
|
|
|
|
|
/** Read-only projection of the exact active dual-stack authority. */
|
|
|
|
|
public static function currentClaimSet(
|
|
|
|
|
int $reservationId,
|
|
|
|
|
string $expectedState = 'committed',
|
|
|
|
|
?int $expectedInstanceId = null
|
|
|
|
|
): CloudActivePublicNetworkClaimSet {
|
|
|
|
|
if ($reservationId < 1
|
|
|
|
|
|| !in_array($expectedState,
|
|
|
|
|
['held', 'committed', 'uncertain'], true)
|
|
|
|
|
|| ($expectedInstanceId !== null && $expectedInstanceId < 1)) {
|
|
|
|
|
throw new CloudError('公网网络资源预留无效', 409);
|
|
|
|
|
}
|
|
|
|
|
// This interface is projection-only. A repeatable-read transaction
|
|
|
|
|
// prevents its several exact-ledger reads from accepting a torn pair;
|
|
|
|
|
// mutation callers use the private locking path instead.
|
|
|
|
|
return self::connection()->transaction(static function () use (
|
|
|
|
|
$reservationId,
|
|
|
|
|
$expectedState,
|
|
|
|
|
$expectedInstanceId
|
|
|
|
|
): CloudActivePublicNetworkClaimSet {
|
|
|
|
|
$reservation = self::unlockedReservation($reservationId);
|
|
|
|
|
$hostId = (int) ($reservation->host_id ?? 0);
|
|
|
|
|
$resourcePoolId = (int) ($reservation->resource_pool_id ?? 0);
|
|
|
|
|
$reservationUserId = (int) ($reservation->user_id ?? 0);
|
|
|
|
|
$resourcePoolLineId = $resourcePoolId > 0
|
|
|
|
|
? DB::table('cloud_resource_pools')
|
|
|
|
|
->where('id', $resourcePoolId)->value('line_id')
|
|
|
|
|
: null;
|
|
|
|
|
$reservationInstanceId = $reservation?->instance_id === null
|
|
|
|
|
? null : (int) $reservation->instance_id;
|
|
|
|
|
$instance = $expectedState === 'committed'
|
|
|
|
|
&& $reservationInstanceId !== null
|
|
|
|
|
&& $reservationInstanceId > 0
|
|
|
|
|
? DB::table('cloud_instances')
|
|
|
|
|
->where('id', $reservationInstanceId)->first()
|
|
|
|
|
: null;
|
|
|
|
|
if ($reservation === null || $hostId < 1
|
|
|
|
|
|| $resourcePoolId < 1
|
|
|
|
|
|| $reservationUserId < 1
|
|
|
|
|
|| $resourcePoolLineId === null
|
|
|
|
|
|| (int) $resourcePoolLineId < 1
|
|
|
|
|
|| (string) $reservation->state !== $expectedState
|
|
|
|
|
|| ($expectedState === 'committed'
|
|
|
|
|
&& ($reservationInstanceId === null
|
|
|
|
|
|| $reservationInstanceId < 1
|
|
|
|
|
|| $instance === null
|
|
|
|
|
|| (int) ($instance->reservation_id ?? 0)
|
|
|
|
|
!== $reservationId
|
|
|
|
|
|| (int) ($instance->host_id ?? 0) !== $hostId
|
|
|
|
|
|| (int) ($instance->resource_pool_id ?? 0)
|
|
|
|
|
!== $resourcePoolId
|
|
|
|
|
|| (int) ($instance->user_id ?? 0)
|
|
|
|
|
!== $reservationUserId))
|
|
|
|
|
|| ($expectedState !== 'committed'
|
|
|
|
|
&& $reservationInstanceId !== null)
|
|
|
|
|
|| ($expectedInstanceId !== null
|
|
|
|
|
&& $reservationInstanceId !== $expectedInstanceId)
|
|
|
|
|
|| DB::table('cloud_hosts')->where('id', $hostId)
|
|
|
|
|
->value('id') === null) {
|
|
|
|
|
throw new CloudError('公网网络资源预留权威链不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$claims = self::unlockedClaims($reservationId);
|
|
|
|
|
$set = self::claimSet(
|
|
|
|
|
$claims,
|
|
|
|
|
$expectedState === 'held' ? 'reserved' : $expectedState,
|
|
|
|
|
$expectedState === 'committed'
|
|
|
|
|
? $reservationInstanceId : null,
|
|
|
|
|
$hostId,
|
|
|
|
|
(int) $resourcePoolLineId
|
|
|
|
|
);
|
|
|
|
|
if ($set->reservationId() !== $reservationId) {
|
|
|
|
|
throw new CloudError('公网网络活动声明归属不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
return $set;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function reserveBundle(int $hostId, int $lineId,
|
|
|
|
|
int $reservationId, string $operationId): CloudPublicNetworkSnapshot
|
|
|
|
|
{
|
|
|
|
|
@ -47,9 +124,8 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
['held', 'committed', 'uncertain']);
|
|
|
|
|
$existing = self::lockedClaims($reservationId);
|
|
|
|
|
if ($existing !== []) {
|
|
|
|
|
self::assertClaimSet($existing, $operationId, [
|
|
|
|
|
'reserved', 'committed', 'uncertain',
|
|
|
|
|
]);
|
|
|
|
|
$claimState = self::claimStateForReservation($reservation);
|
|
|
|
|
self::assertClaimSet($existing, $operationId, [$claimState]);
|
|
|
|
|
if ($selection === null) {
|
|
|
|
|
throw new CloudError('公网网络资源预留状态不明确', 409);
|
|
|
|
|
}
|
|
|
|
|
@ -60,9 +136,16 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
'reserved' => ['reserved'],
|
|
|
|
|
'committed' => ['assigned'],
|
|
|
|
|
'uncertain' => ['uncertain'],
|
|
|
|
|
]);
|
|
|
|
|
], $claimState === 'committed'
|
|
|
|
|
? (int) ($reservation->instance_id ?? 0) : null);
|
|
|
|
|
return self::snapshot($existing);
|
|
|
|
|
}
|
|
|
|
|
if (self::lockedHistoricalClaims($reservationId) !== []) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'初始公网网络预留不能复用历史地址声明',
|
|
|
|
|
409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($claimHints !== [] || (string) $reservation->state !== 'held') {
|
|
|
|
|
throw new CloudError('公网网络资源预留状态不明确', 409);
|
|
|
|
|
}
|
|
|
|
|
@ -74,13 +157,14 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
$claims = [];
|
|
|
|
|
foreach (self::FAMILIES as $family) {
|
|
|
|
|
$pool = $selection['pools'][$family];
|
|
|
|
|
$ip = self::reserveAddress($pool, $reservationId,
|
|
|
|
|
$operationId, $now,
|
|
|
|
|
$ip = self::reserveAddress($pool, $now,
|
|
|
|
|
$selection['address_candidates'][$family] ?? null);
|
|
|
|
|
$claimId = DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->insertGetId([
|
|
|
|
|
'reservation_id' => $reservationId,
|
|
|
|
|
'address_family' => $family,
|
|
|
|
|
'claim_generation' => 1,
|
|
|
|
|
'active_claim' => 1,
|
|
|
|
|
'network_attachment_id' =>
|
|
|
|
|
(int) $selection['attachment']->id,
|
|
|
|
|
'public_ip_pool_id' => (int) $pool->id,
|
|
|
|
|
@ -92,6 +176,13 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
'released_at' => null,
|
|
|
|
|
'release_reason' => '',
|
|
|
|
|
]);
|
|
|
|
|
self::bindAddressToClaim(
|
|
|
|
|
$ip,
|
|
|
|
|
$reservationId,
|
|
|
|
|
$operationId,
|
|
|
|
|
$claimId,
|
|
|
|
|
$now
|
|
|
|
|
);
|
|
|
|
|
$claims[] = DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', $claimId)->first();
|
|
|
|
|
}
|
|
|
|
|
@ -126,8 +217,16 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
self::assertReservation($reservation, $hostId, $operationId,
|
|
|
|
|
['held', 'committed', 'uncertain']);
|
|
|
|
|
$claims = self::lockedClaims($reservationId);
|
|
|
|
|
self::assertClaimSet($claims, $operationId,
|
|
|
|
|
['reserved', 'uncertain', 'committed']);
|
|
|
|
|
$claimState = self::claimStateForReservation($reservation);
|
|
|
|
|
self::assertClaimSet(
|
|
|
|
|
$claims,
|
|
|
|
|
$operationId,
|
|
|
|
|
[$claimState]
|
|
|
|
|
);
|
|
|
|
|
if ($claimState === 'committed'
|
|
|
|
|
&& (int) ($reservation->instance_id ?? 0) !== $instanceId) {
|
|
|
|
|
throw new CloudError('实例网络归属与资源预留不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
self::assertClaimsUseSelection($claims, $selection);
|
|
|
|
|
$addresses = self::lockClaimAddresses($claims);
|
|
|
|
|
self::assertClaimAddresses($claims, $addresses, $selection,
|
|
|
|
|
@ -136,22 +235,32 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
'uncertain' => ['uncertain'],
|
|
|
|
|
'committed' => ['assigned'],
|
|
|
|
|
], $instanceId);
|
|
|
|
|
if ($claimState === 'committed') {
|
|
|
|
|
return self::snapshot($claims);
|
|
|
|
|
}
|
|
|
|
|
$now = time();
|
|
|
|
|
foreach ($claims as $claim) {
|
|
|
|
|
$ip = $addresses[(int) $claim->public_ip_id];
|
|
|
|
|
DB::table('cloud_public_ips')->where('id', (int) $ip->id)
|
|
|
|
|
->update([
|
|
|
|
|
$ipUpdated = DB::table('cloud_public_ips')
|
|
|
|
|
->where('id', (int) $ip->id)
|
|
|
|
|
->where('reservation_ip_claim_id', (int) $claim->id)
|
|
|
|
|
->where('operation_id', $operationId)->update([
|
|
|
|
|
'state' => 'assigned',
|
|
|
|
|
'reservation_id' => $reservationId,
|
|
|
|
|
'reservation_ip_claim_id' => (int) $claim->id,
|
|
|
|
|
'instance_id' => $instanceId,
|
|
|
|
|
'operation_id' => $operationId,
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', (int) $claim->id)->update([
|
|
|
|
|
$claimUpdated = DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', (int) $claim->id)
|
|
|
|
|
->where('active_claim', 1)->update([
|
|
|
|
|
'state' => 'committed',
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
if ($ipUpdated !== 1 || $claimUpdated !== 1) {
|
|
|
|
|
throw new CloudError('双栈公网地址提交账本未精确更新', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return self::snapshot(self::lockedClaims($reservationId));
|
|
|
|
|
});
|
|
|
|
|
@ -180,8 +289,12 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
self::assertReservation($reservation, $hostId, $operationId,
|
|
|
|
|
['held', 'uncertain']);
|
|
|
|
|
$claims = self::lockedClaims($reservationId);
|
|
|
|
|
self::assertClaimSet($claims, $operationId,
|
|
|
|
|
['reserved', 'uncertain']);
|
|
|
|
|
$claimState = self::claimStateForReservation($reservation);
|
|
|
|
|
self::assertClaimSet(
|
|
|
|
|
$claims,
|
|
|
|
|
$operationId,
|
|
|
|
|
[$claimState]
|
|
|
|
|
);
|
|
|
|
|
self::assertClaimsUseSelection($claims, $selection);
|
|
|
|
|
$addresses = self::lockClaimAddresses($claims);
|
|
|
|
|
self::assertClaimAddresses($claims, $addresses, $selection,
|
|
|
|
|
@ -189,19 +302,27 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
'reserved' => ['reserved'],
|
|
|
|
|
'uncertain' => ['uncertain'],
|
|
|
|
|
]);
|
|
|
|
|
if ($claimState === 'uncertain') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$now = time();
|
|
|
|
|
foreach ($claims as $claim) {
|
|
|
|
|
DB::table('cloud_public_ips')
|
|
|
|
|
$ipUpdated = DB::table('cloud_public_ips')
|
|
|
|
|
->where('id', (int) $claim->public_ip_id)
|
|
|
|
|
->where('reservation_ip_claim_id', (int) $claim->id)
|
|
|
|
|
->where('operation_id', $operationId)->update([
|
|
|
|
|
'state' => 'uncertain',
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', (int) $claim->id)->update([
|
|
|
|
|
$claimUpdated = DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', (int) $claim->id)
|
|
|
|
|
->where('active_claim', 1)->update([
|
|
|
|
|
'state' => 'uncertain',
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
if ($ipUpdated !== 1 || $claimUpdated !== 1) {
|
|
|
|
|
throw new CloudError('双栈公网地址隔离账本未精确更新', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
@ -234,8 +355,11 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
self::assertReservation($reservation, $hostId, $operationId,
|
|
|
|
|
['held', 'uncertain']);
|
|
|
|
|
$claims = self::lockedClaims($reservationId);
|
|
|
|
|
self::assertClaimSet($claims, $operationId,
|
|
|
|
|
['reserved', 'uncertain']);
|
|
|
|
|
self::assertClaimSet(
|
|
|
|
|
$claims,
|
|
|
|
|
$operationId,
|
|
|
|
|
[self::claimStateForReservation($reservation)]
|
|
|
|
|
);
|
|
|
|
|
self::assertClaimsUseSelection($claims, $selection);
|
|
|
|
|
$addresses = self::lockClaimAddresses($claims);
|
|
|
|
|
self::assertClaimAddresses($claims, $addresses, $selection,
|
|
|
|
|
@ -243,7 +367,7 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
'reserved' => ['reserved'],
|
|
|
|
|
'uncertain' => ['uncertain'],
|
|
|
|
|
]);
|
|
|
|
|
self::releaseLockedClaims($claims, $operationId, $reason);
|
|
|
|
|
self::releaseLockedClaims($claims, $reason);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -280,16 +404,19 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
$selection = self::lockClaimNetworkContext($claimHints, $hostId);
|
|
|
|
|
$reservation = self::lockReservation($reservationId);
|
|
|
|
|
self::assertReservation($reservation, $hostId, $operationId,
|
|
|
|
|
['held', 'committed', 'uncertain']);
|
|
|
|
|
['committed']);
|
|
|
|
|
if ((int) ($reservation->instance_id ?? 0) !== $instanceId) {
|
|
|
|
|
throw new CloudError('实例网络归属与资源预留不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$claims = self::lockedClaims($reservationId);
|
|
|
|
|
self::assertClaimSet($claims, $operationId, ['committed']);
|
|
|
|
|
self::assertActiveClaimSet($claims, ['committed']);
|
|
|
|
|
self::assertClaimsUseSelection($claims, $selection);
|
|
|
|
|
$addresses = self::lockClaimAddresses($claims);
|
|
|
|
|
self::assertClaimAddresses($claims, $addresses, $selection,
|
|
|
|
|
$operationId, [
|
|
|
|
|
null, [
|
|
|
|
|
'committed' => ['assigned'],
|
|
|
|
|
], $instanceId);
|
|
|
|
|
self::releaseLockedClaims($claims, $operationId, $reason);
|
|
|
|
|
self::releaseLockedClaims($claims, $reason);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -303,6 +430,7 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
{
|
|
|
|
|
return DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('reservation_id', $reservationId)
|
|
|
|
|
->where('active_claim', 1)
|
|
|
|
|
->orderBy('address_family')->orderBy('id')->get()->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -333,6 +461,31 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function claimStateForReservation(object $reservation): string
|
|
|
|
|
{
|
|
|
|
|
$state = (string) ($reservation->state ?? '');
|
|
|
|
|
$instanceId = $reservation->instance_id === null
|
|
|
|
|
? null : (int) $reservation->instance_id;
|
|
|
|
|
if (($state === 'committed'
|
|
|
|
|
&& ($instanceId === null || $instanceId < 1))
|
|
|
|
|
|| (in_array($state, ['held', 'uncertain'], true)
|
|
|
|
|
&& $instanceId !== null)) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'公网网络资源预留生命周期不一致',
|
|
|
|
|
409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return match ($state) {
|
|
|
|
|
'held' => 'reserved',
|
|
|
|
|
'committed' => 'committed',
|
|
|
|
|
'uncertain' => 'uncertain',
|
|
|
|
|
default => throw new CloudError(
|
|
|
|
|
'公网网络资源预留生命周期不一致',
|
|
|
|
|
409
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Locks the exact historical attachment and pools before the reservation. */
|
|
|
|
|
private static function lockClaimNetworkContext(array $claimHints,
|
|
|
|
|
int $hostId, ?int $lineId = null): array
|
|
|
|
|
@ -434,18 +587,8 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
object $pool): CloudIpAddress
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$address = CloudIpAddress::parse((string) $row->address);
|
|
|
|
|
if ((int) $row->host_id !== (int) $pool->host_id
|
|
|
|
|
|| (int) $row->public_ip_pool_id !== (int) $pool->id
|
|
|
|
|
|| (int) $row->address_family
|
|
|
|
|
!== (int) $pool->address_family
|
|
|
|
|
|| (int) $row->prefix_length
|
|
|
|
|
!== (int) $pool->guest_prefix_length
|
|
|
|
|
|| !hash_equals($address->text(), (string) $row->address)
|
|
|
|
|
|| !is_string($row->address_binary)
|
|
|
|
|
|| !hash_equals($address->binary(), $row->address_binary)
|
|
|
|
|
|| !self::rawAddressBelongsToPool($address, $pool)
|
|
|
|
|
|| ((string) $row->state !== 'disabled'
|
|
|
|
|
$address = self::assertConcreteAddressIdentity($row, $pool);
|
|
|
|
|
if (((string) $row->state !== 'disabled'
|
|
|
|
|
&& !self::rawAddressAllowedByPool($address, $pool))) {
|
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
|
}
|
|
|
|
|
@ -464,6 +607,30 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Validate immutable identity without reapplying mutable allowlists. */
|
|
|
|
|
private static function assertConcreteAddressIdentity(object $row,
|
|
|
|
|
object $pool): CloudIpAddress
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$address = CloudIpAddress::parse((string) $row->address);
|
|
|
|
|
if ((int) $row->host_id !== (int) $pool->host_id
|
|
|
|
|
|| (int) $row->public_ip_pool_id !== (int) $pool->id
|
|
|
|
|
|| (int) $row->address_family
|
|
|
|
|
!== (int) $pool->address_family
|
|
|
|
|
|| (int) $row->prefix_length
|
|
|
|
|
!== (int) $pool->guest_prefix_length
|
|
|
|
|
|| !hash_equals($address->text(), (string) $row->address)
|
|
|
|
|
|| !is_string($row->address_binary)
|
|
|
|
|
|| !hash_equals($address->binary(), $row->address_binary)
|
|
|
|
|
|| !self::rawAddressBelongsToPool($address, $pool)) {
|
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
|
}
|
|
|
|
|
return $address;
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
throw new CloudError('公网地址具体身份与地址池不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertAddressOwnership(object $row,
|
|
|
|
|
?object $pool = null, array $retainedSourceEvidence = []): void
|
|
|
|
|
{
|
|
|
|
|
@ -471,17 +638,21 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
? null : (int) $row->reservation_id;
|
|
|
|
|
$instanceId = $row->instance_id === null
|
|
|
|
|
? null : (int) $row->instance_id;
|
|
|
|
|
$claimId = $row->reservation_ip_claim_id === null
|
|
|
|
|
? null : (int) $row->reservation_ip_claim_id;
|
|
|
|
|
$operationId = $row->operation_id;
|
|
|
|
|
$validOperation = is_string($operationId)
|
|
|
|
|
&& preg_match('/^[a-f0-9]{32}$/D', $operationId) === 1;
|
|
|
|
|
$state = (string) $row->state;
|
|
|
|
|
$valid = match ($state) {
|
|
|
|
|
'available', 'disabled' => $reservationId === null
|
|
|
|
|
&& $instanceId === null && $operationId === null,
|
|
|
|
|
&& $claimId === null && $instanceId === null
|
|
|
|
|
&& $operationId === null,
|
|
|
|
|
'reserved' => $reservationId !== null && $reservationId > 0
|
|
|
|
|
&& $claimId !== null && $claimId > 0
|
|
|
|
|
&& $instanceId === null && $validOperation,
|
|
|
|
|
'assigned' => $reservationId !== null && $reservationId > 0
|
|
|
|
|
&& $validOperation && (
|
|
|
|
|
&& $claimId !== null && $claimId > 0 && $validOperation && (
|
|
|
|
|
($instanceId !== null && $instanceId > 0)
|
|
|
|
|
|| self::matchesRetainedSourceEvidence(
|
|
|
|
|
$row,
|
|
|
|
|
@ -489,15 +660,10 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
$retainedSourceEvidence
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
'changing' => $reservationId === null
|
|
|
|
|
&& $instanceId !== null && $instanceId > 0
|
|
|
|
|
'changing', 'uncertain' => $reservationId !== null
|
|
|
|
|
&& $reservationId > 0 && $claimId !== null && $claimId > 0
|
|
|
|
|
&& ($instanceId === null || $instanceId > 0)
|
|
|
|
|
&& $validOperation,
|
|
|
|
|
'uncertain' => $validOperation && (
|
|
|
|
|
($reservationId !== null && $reservationId > 0
|
|
|
|
|
&& $instanceId === null)
|
|
|
|
|
|| ($reservationId === null
|
|
|
|
|
&& $instanceId !== null && $instanceId > 0)
|
|
|
|
|
),
|
|
|
|
|
default => false,
|
|
|
|
|
};
|
|
|
|
|
if (!$valid) {
|
|
|
|
|
@ -537,22 +703,29 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertClaimAddresses(array $claims,
|
|
|
|
|
array $addresses, array $selection, string $operationId,
|
|
|
|
|
array $addresses, array $selection, ?string $operationId,
|
|
|
|
|
array $allowedPairs, ?int $instanceId = null): void
|
|
|
|
|
{
|
|
|
|
|
foreach ($claims as $claim) {
|
|
|
|
|
$ip = $addresses[(int) $claim->public_ip_id] ?? null;
|
|
|
|
|
$claimState = (string) $claim->state;
|
|
|
|
|
$claimOperationId = (string) ($claim->operation_id ?? '');
|
|
|
|
|
$expectedOperationId = $operationId ?? $claimOperationId;
|
|
|
|
|
$allowedIpStates = $allowedPairs[$claimState] ?? [];
|
|
|
|
|
$family = (int) $claim->address_family;
|
|
|
|
|
$pool = $selection['pools'][$family] ?? null;
|
|
|
|
|
if ($ip === null || $pool === null || $allowedIpStates === []
|
|
|
|
|
|| !in_array((string) $ip->state, $allowedIpStates, true)
|
|
|
|
|
|| (int) $ip->reservation_id !== (int) $claim->reservation_id
|
|
|
|
|
|| (int) ($ip->reservation_ip_claim_id ?? 0)
|
|
|
|
|
!== (int) $claim->id
|
|
|
|
|
|| (int) $ip->address_family !== (int) $claim->address_family
|
|
|
|
|
|| (int) $ip->public_ip_pool_id
|
|
|
|
|
!== (int) $claim->public_ip_pool_id
|
|
|
|
|
|| !hash_equals($operationId,
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $claimOperationId) !== 1
|
|
|
|
|
|| !hash_equals($claimOperationId,
|
|
|
|
|
(string) ($ip->operation_id ?? ''))
|
|
|
|
|
|| !hash_equals($expectedOperationId,
|
|
|
|
|
(string) ($ip->operation_id ?? ''))) {
|
|
|
|
|
throw new CloudError('公网地址与预留账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
@ -572,26 +745,34 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function releaseLockedClaims(array $claims,
|
|
|
|
|
string $operationId, string $reason): void
|
|
|
|
|
string $reason): void
|
|
|
|
|
{
|
|
|
|
|
$now = time();
|
|
|
|
|
foreach ($claims as $claim) {
|
|
|
|
|
DB::table('cloud_public_ips')
|
|
|
|
|
$ipUpdated = DB::table('cloud_public_ips')
|
|
|
|
|
->where('id', (int) $claim->public_ip_id)
|
|
|
|
|
->where('operation_id', $operationId)->update([
|
|
|
|
|
->where('reservation_ip_claim_id', (int) $claim->id)
|
|
|
|
|
->where('operation_id', (string) $claim->operation_id)
|
|
|
|
|
->update([
|
|
|
|
|
'state' => 'available',
|
|
|
|
|
'reservation_id' => null,
|
|
|
|
|
'reservation_ip_claim_id' => null,
|
|
|
|
|
'instance_id' => null,
|
|
|
|
|
'operation_id' => null,
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', (int) $claim->id)->update([
|
|
|
|
|
$claimUpdated = DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('id', (int) $claim->id)
|
|
|
|
|
->where('active_claim', 1)->update([
|
|
|
|
|
'state' => 'released',
|
|
|
|
|
'active_claim' => null,
|
|
|
|
|
'released_at' => $now,
|
|
|
|
|
'release_reason' => $reason,
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
if ($ipUpdated !== 1 || $claimUpdated !== 1) {
|
|
|
|
|
throw new CloudError('双栈公网地址释放账本未精确更新', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -843,8 +1024,8 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function reserveAddress(object $pool, int $reservationId,
|
|
|
|
|
string $operationId, int $now, ?array $selection): object
|
|
|
|
|
private static function reserveAddress(object $pool, int $now,
|
|
|
|
|
?array $selection): object
|
|
|
|
|
{
|
|
|
|
|
$family = (int) $pool->address_family;
|
|
|
|
|
if ($selection === null) {
|
|
|
|
|
@ -856,12 +1037,15 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($selection['row'] !== null) {
|
|
|
|
|
return self::claimAddress(
|
|
|
|
|
$selection['row'],
|
|
|
|
|
$reservationId,
|
|
|
|
|
$operationId,
|
|
|
|
|
$now
|
|
|
|
|
);
|
|
|
|
|
$row = $selection['row'];
|
|
|
|
|
if ((string) $row->state !== 'available'
|
|
|
|
|
|| $row->reservation_id !== null
|
|
|
|
|
|| $row->reservation_ip_claim_id !== null
|
|
|
|
|
|| $row->instance_id !== null
|
|
|
|
|
|| $row->operation_id !== null) {
|
|
|
|
|
throw new CloudError('公网地址候选已被占用', 409);
|
|
|
|
|
}
|
|
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
$candidate = $selection['candidate'];
|
|
|
|
|
$rangeStart = $selection['range_start'];
|
|
|
|
|
@ -878,10 +1062,11 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
'address' => $candidate->text(),
|
|
|
|
|
'address_binary' => $candidate->binary(),
|
|
|
|
|
'prefix_length' => (int) $pool->guest_prefix_length,
|
|
|
|
|
'state' => 'reserved',
|
|
|
|
|
'reservation_id' => $reservationId,
|
|
|
|
|
'state' => 'available',
|
|
|
|
|
'reservation_id' => null,
|
|
|
|
|
'reservation_ip_claim_id' => null,
|
|
|
|
|
'instance_id' => null,
|
|
|
|
|
'operation_id' => $operationId,
|
|
|
|
|
'operation_id' => null,
|
|
|
|
|
'created_at' => $now,
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
@ -894,18 +1079,26 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
return DB::table('cloud_public_ips')->where('id', $id)->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function claimAddress(object $row, int $reservationId,
|
|
|
|
|
string $operationId, int $now): object
|
|
|
|
|
private static function bindAddressToClaim(object $row, int $reservationId,
|
|
|
|
|
string $operationId, int $claimId, int $now): void
|
|
|
|
|
{
|
|
|
|
|
DB::table('cloud_public_ips')->where('id', (int) $row->id)->update([
|
|
|
|
|
$updated = DB::table('cloud_public_ips')
|
|
|
|
|
->where('id', (int) $row->id)
|
|
|
|
|
->where('state', 'available')
|
|
|
|
|
->whereNull('reservation_id')
|
|
|
|
|
->whereNull('reservation_ip_claim_id')
|
|
|
|
|
->whereNull('instance_id')
|
|
|
|
|
->whereNull('operation_id')->update([
|
|
|
|
|
'state' => 'reserved',
|
|
|
|
|
'reservation_id' => $reservationId,
|
|
|
|
|
'reservation_ip_claim_id' => $claimId,
|
|
|
|
|
'instance_id' => null,
|
|
|
|
|
'operation_id' => $operationId,
|
|
|
|
|
'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
return DB::table('cloud_public_ips')->where('id', (int) $row->id)
|
|
|
|
|
->first();
|
|
|
|
|
if ($updated !== 1) {
|
|
|
|
|
throw new CloudError('公网地址声明绑定失败', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function validatePool(object $pool, int $family): void
|
|
|
|
|
@ -1026,12 +1219,32 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
{
|
|
|
|
|
return DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('reservation_id', $reservationId)
|
|
|
|
|
->where('active_claim', 1)
|
|
|
|
|
->orderBy('address_family')->orderBy('id')->lockForUpdate()->get()
|
|
|
|
|
->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function lockedHistoricalClaims(int $reservationId): array
|
|
|
|
|
{
|
|
|
|
|
return DB::table('cloud_resource_reservation_ips')
|
|
|
|
|
->where('reservation_id', $reservationId)
|
|
|
|
|
->whereNull('active_claim')->orderBy('id')->lockForUpdate()->get()
|
|
|
|
|
->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertClaimSet(array $claims, string $operationId,
|
|
|
|
|
array $allowedStates): void
|
|
|
|
|
{
|
|
|
|
|
self::assertActiveClaimSet($claims, $allowedStates);
|
|
|
|
|
foreach ($claims as $claim) {
|
|
|
|
|
if (!hash_equals($operationId, (string) $claim->operation_id)) {
|
|
|
|
|
throw new CloudError('双栈公网地址操作身份不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertActiveClaimSet(array $claims,
|
|
|
|
|
array $allowedStates): void
|
|
|
|
|
{
|
|
|
|
|
if (count($claims) !== 2) {
|
|
|
|
|
throw new CloudError('双栈公网地址账本不完整', 409);
|
|
|
|
|
@ -1041,7 +1254,10 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
$family = (int) $claim->address_family;
|
|
|
|
|
if (!in_array($family, self::FAMILIES, true)
|
|
|
|
|
|| isset($families[$family])
|
|
|
|
|
|| !hash_equals($operationId, (string) $claim->operation_id)
|
|
|
|
|
|| (int) ($claim->claim_generation ?? 0) < 1
|
|
|
|
|
|| (int) ($claim->active_claim ?? 0) !== 1
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
|
|
|
(string) ($claim->operation_id ?? '')) !== 1
|
|
|
|
|
|| !in_array((string) $claim->state, $allowedStates, true)) {
|
|
|
|
|
throw new CloudError('双栈公网地址账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
@ -1053,6 +1269,17 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function snapshot(array $claims): CloudPublicNetworkSnapshot
|
|
|
|
|
{
|
|
|
|
|
return self::claimSet($claims)->publicNetwork();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function claimSet(
|
|
|
|
|
array $claims,
|
|
|
|
|
?string $expectedState = null,
|
|
|
|
|
?int $expectedInstanceId = null,
|
|
|
|
|
?int $expectedHostId = null,
|
|
|
|
|
?int $expectedLineId = null
|
|
|
|
|
): CloudActivePublicNetworkClaimSet
|
|
|
|
|
{
|
|
|
|
|
if (count($claims) !== 2) {
|
|
|
|
|
throw new CloudError('双栈公网地址账本不完整', 409);
|
|
|
|
|
@ -1066,11 +1293,17 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
}
|
|
|
|
|
$attachment = DB::table('cloud_host_network_attachments')
|
|
|
|
|
->where('id', $attachmentIds[0])->first();
|
|
|
|
|
if ($attachment === null) {
|
|
|
|
|
if ($attachment === null
|
|
|
|
|
|| ($expectedHostId !== null
|
|
|
|
|
&& (int) $attachment->host_id !== $expectedHostId)
|
|
|
|
|
|| ($expectedLineId !== null
|
|
|
|
|
&& (int) $attachment->line_id !== $expectedLineId)) {
|
|
|
|
|
throw new CloudError('公网网络接入不存在', 409);
|
|
|
|
|
}
|
|
|
|
|
$legs = [];
|
|
|
|
|
$dns = [];
|
|
|
|
|
$addresses = [];
|
|
|
|
|
$reservationIds = [];
|
|
|
|
|
foreach ($claims as $claim) {
|
|
|
|
|
$family = (int) $claim->address_family;
|
|
|
|
|
$pool = DB::table('cloud_public_ip_pools')
|
|
|
|
|
@ -1080,10 +1313,16 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
if ($pool === null || $ip === null
|
|
|
|
|
|| (int) $pool->address_family !== $family
|
|
|
|
|
|| (int) $ip->address_family !== $family
|
|
|
|
|
|| ($expectedHostId !== null
|
|
|
|
|
&& ((int) $pool->host_id !== $expectedHostId
|
|
|
|
|
|| (int) $ip->host_id !== $expectedHostId))
|
|
|
|
|
|| (int) $pool->network_attachment_id !== $attachmentIds[0]
|
|
|
|
|
|| (int) $ip->public_ip_pool_id !== (int) $pool->id) {
|
|
|
|
|
throw new CloudError('双栈公网地址引用损坏', 409);
|
|
|
|
|
}
|
|
|
|
|
self::assertConcreteAddressIdentity($ip, $pool);
|
|
|
|
|
$addresses[(int) $ip->id] = $ip;
|
|
|
|
|
$reservationIds[(int) $claim->reservation_id] = true;
|
|
|
|
|
$legs[$family] = [
|
|
|
|
|
'pool_key' => (string) $pool->pool_key,
|
|
|
|
|
'pool_cidr' => (string) $pool->cidr,
|
|
|
|
|
@ -1099,13 +1338,71 @@ final class CloudPublicNetworkAllocationService
|
|
|
|
|
$dns[$text] = $text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return CloudPublicNetworkSnapshot::fromArray([
|
|
|
|
|
if (count($reservationIds) !== 1) {
|
|
|
|
|
throw new CloudError('双栈公网地址不属于同一资源预留', 409);
|
|
|
|
|
}
|
|
|
|
|
self::assertCurrentClaimSetStates(
|
|
|
|
|
$claims,
|
|
|
|
|
$addresses,
|
|
|
|
|
$expectedState,
|
|
|
|
|
$expectedInstanceId
|
|
|
|
|
);
|
|
|
|
|
$snapshot = CloudPublicNetworkSnapshot::fromArray([
|
|
|
|
|
'contract' => 1,
|
|
|
|
|
'network_selector' => (string) $attachment->network_selector,
|
|
|
|
|
'ipv4' => $legs[4] ?? null,
|
|
|
|
|
'ipv6' => $legs[6] ?? null,
|
|
|
|
|
'dns_servers' => array_values($dns),
|
|
|
|
|
]);
|
|
|
|
|
return CloudActivePublicNetworkClaimSet::fromLedger(
|
|
|
|
|
(int) array_key_first($reservationIds),
|
|
|
|
|
$claims,
|
|
|
|
|
$addresses,
|
|
|
|
|
$snapshot
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<int, object> $claims
|
|
|
|
|
* @param array<int, object> $addresses
|
|
|
|
|
*/
|
|
|
|
|
private static function assertCurrentClaimSetStates(array $claims,
|
|
|
|
|
array $addresses, ?string $expectedState,
|
|
|
|
|
?int $expectedInstanceId): void
|
|
|
|
|
{
|
|
|
|
|
$claimStates = [];
|
|
|
|
|
$instanceIds = [];
|
|
|
|
|
foreach ($claims as $claim) {
|
|
|
|
|
$claimState = (string) ($claim->state ?? '');
|
|
|
|
|
$ip = $addresses[(int) ($claim->public_ip_id ?? 0)] ?? null;
|
|
|
|
|
$ipState = is_object($ip) ? (string) ($ip->state ?? '') : '';
|
|
|
|
|
$expectedIpState = match ($claimState) {
|
|
|
|
|
'reserved' => 'reserved',
|
|
|
|
|
'committed' => 'assigned',
|
|
|
|
|
'uncertain' => 'uncertain',
|
|
|
|
|
default => '',
|
|
|
|
|
};
|
|
|
|
|
$instanceId = is_object($ip) && $ip->instance_id !== null
|
|
|
|
|
? (int) $ip->instance_id : null;
|
|
|
|
|
if ($expectedIpState === '' || $ipState !== $expectedIpState
|
|
|
|
|
|| ($claimState !== 'committed' && $instanceId !== null)
|
|
|
|
|
|| ($claimState === 'committed'
|
|
|
|
|
&& ($instanceId === null || $instanceId < 1))) {
|
|
|
|
|
throw new CloudError('活动双栈公网地址状态不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$claimStates[$claimState] = true;
|
|
|
|
|
if ($instanceId !== null) {
|
|
|
|
|
$instanceIds[$instanceId] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (count($claimStates) !== 1
|
|
|
|
|
|| ($expectedState !== null && !isset($claimStates[$expectedState]))
|
|
|
|
|
|| (isset($claimStates['committed']) && count($instanceIds) !== 1)
|
|
|
|
|
|| (!isset($claimStates['committed']) && $instanceIds !== [])
|
|
|
|
|
|| ($expectedInstanceId !== null
|
|
|
|
|
&& !isset($instanceIds[$expectedInstanceId]))) {
|
|
|
|
|
throw new CloudError('活动双栈公网地址集合状态不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function assertIdentity(int $reservationId,
|
|
|
|
|
|