|
|
|
|
@ -10,6 +10,7 @@ use App\Models\CloudTrafficLedgerEvent;
|
|
|
|
|
use App\Models\CloudTrafficPool;
|
|
|
|
|
use App\Models\CloudTrafficPoolGroup;
|
|
|
|
|
use App\Models\CloudTrafficScanFailure;
|
|
|
|
|
use App\Models\CloudTrafficUsageSettlementBucket;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shared prepaid traffic accounting.
|
|
|
|
|
@ -833,6 +834,9 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
$forfeited += $amount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$consumed += (int) CloudTrafficUsageSettlementBucket::where(
|
|
|
|
|
'entitlement_id', (int) $entitlement->id
|
|
|
|
|
)->where('state', 'open')->sum('amount_bytes');
|
|
|
|
|
if ($granted !== (int) $entitlement->granted_bytes
|
|
|
|
|
|| $consumed !== (int) $entitlement->consumed_bytes
|
|
|
|
|
|| $forfeited !== (int) $entitlement->forfeited_bytes) {
|
|
|
|
|
@ -842,6 +846,10 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
$ledgerOverage = (int) CloudTrafficLedgerEvent::where(
|
|
|
|
|
'pool_id', $poolId
|
|
|
|
|
)->where('event_type', 'overage_recorded')->sum('amount_bytes')
|
|
|
|
|
+ (int) CloudTrafficUsageSettlementBucket::where(
|
|
|
|
|
'pool_id', $poolId
|
|
|
|
|
)->where('allocation_kind', 'overage')
|
|
|
|
|
->where('state', 'open')->sum('amount_bytes')
|
|
|
|
|
- (int) CloudTrafficLedgerEvent::where('pool_id', $poolId)
|
|
|
|
|
->where('event_type', 'overage_settled')->sum('amount_bytes');
|
|
|
|
|
if ($ledgerOverage !== (int) $pool->overage_bytes) {
|
|
|
|
|
@ -1009,6 +1017,7 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
): array
|
|
|
|
|
{
|
|
|
|
|
$now = time();
|
|
|
|
|
self::sealDueUsageBuckets(200, $now);
|
|
|
|
|
$limit = max(1, $limit);
|
|
|
|
|
$afterInstanceId = max(0, $afterInstanceId);
|
|
|
|
|
$rows = CloudInstance::query()
|
|
|
|
|
@ -1251,11 +1260,16 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$instanceId = (int) $instance->id;
|
|
|
|
|
$sourceId = 'instance:' . $instanceId . ':rx:' . $download
|
|
|
|
|
. ':tx:' . $upload;
|
|
|
|
|
$lastEventId = null;
|
|
|
|
|
$bucketStart = intdiv($observedAt, 3600) * 3600;
|
|
|
|
|
$lastEventId = self::sealOpenUsageBucketsLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
$instanceId,
|
|
|
|
|
$bucketStart
|
|
|
|
|
);
|
|
|
|
|
if ((string) $period->traffic_quota_mode !== 'unlimited') {
|
|
|
|
|
$remaining = $delta;
|
|
|
|
|
$remainingDownload = $downloadDelta;
|
|
|
|
|
$remainingUpload = $uploadDelta;
|
|
|
|
|
$entitlements = CloudTrafficEntitlement::where(
|
|
|
|
|
'pool_id', (int) $pool->id
|
|
|
|
|
)->where('quota_mode', 'finite')
|
|
|
|
|
@ -1281,7 +1295,6 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
});
|
|
|
|
|
})->orderBy('ends_at')->orderBy('id')
|
|
|
|
|
->lockForUpdate()->get();
|
|
|
|
|
$ordinal = 0;
|
|
|
|
|
foreach ($entitlements as $entitlement) {
|
|
|
|
|
if ($remaining <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
@ -1300,51 +1313,55 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
$entitlement->version = (int) $entitlement->version + 1;
|
|
|
|
|
$entitlement->updated_at = $applyAt;
|
|
|
|
|
$entitlement->save();
|
|
|
|
|
$event = self::appendEventLocked($pool, [
|
|
|
|
|
'event_key' => $sourceId . ':allocation:'
|
|
|
|
|
. (int) $entitlement->id,
|
|
|
|
|
'event_type' => 'usage_allocated',
|
|
|
|
|
'source_type' => 'traffic_observation',
|
|
|
|
|
'source_id' => $sourceId,
|
|
|
|
|
'source_revision' => 1,
|
|
|
|
|
'instance_id' => $instanceId,
|
|
|
|
|
'service_period_id' =>
|
|
|
|
|
(int) $entitlement->service_period_id,
|
|
|
|
|
'entitlement_id' => (int) $entitlement->id,
|
|
|
|
|
'amount_bytes' => $take,
|
|
|
|
|
'effective_at' => $observedAt,
|
|
|
|
|
'observed_at' => $observedAt,
|
|
|
|
|
'payload' => [
|
|
|
|
|
'download_delta_bytes' => $downloadDelta,
|
|
|
|
|
'upload_delta_bytes' => $uploadDelta,
|
|
|
|
|
'ordinal' => $ordinal,
|
|
|
|
|
'entitlement_id' => (int) $entitlement->id,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
$lastEventId = (int) $event->id;
|
|
|
|
|
$takeDownload = min($remainingDownload, $take);
|
|
|
|
|
$takeUpload = $take - $takeDownload;
|
|
|
|
|
$remainingDownload -= $takeDownload;
|
|
|
|
|
$remainingUpload -= $takeUpload;
|
|
|
|
|
$bucket = self::recordUsageBucketLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
$instanceId,
|
|
|
|
|
(int) $entitlement->service_period_id,
|
|
|
|
|
(int) $entitlement->id,
|
|
|
|
|
'entitlement',
|
|
|
|
|
$bucketStart,
|
|
|
|
|
$take,
|
|
|
|
|
$takeDownload,
|
|
|
|
|
$takeUpload,
|
|
|
|
|
$observedAt,
|
|
|
|
|
$applyAt
|
|
|
|
|
);
|
|
|
|
|
if ((string) $entitlement->state === 'exhausted') {
|
|
|
|
|
$lastEventId = self::sealUsageBucketLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
$bucket
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$remaining -= $take;
|
|
|
|
|
$ordinal++;
|
|
|
|
|
}
|
|
|
|
|
if ($remaining > 0) {
|
|
|
|
|
$pool->overage_bytes = (int) $pool->overage_bytes + $remaining;
|
|
|
|
|
$event = self::appendEventLocked($pool, [
|
|
|
|
|
'event_key' => $sourceId . ':overage',
|
|
|
|
|
'event_type' => 'overage_recorded',
|
|
|
|
|
'source_type' => 'traffic_observation',
|
|
|
|
|
'source_id' => $sourceId,
|
|
|
|
|
'source_revision' => 1,
|
|
|
|
|
'instance_id' => $instanceId,
|
|
|
|
|
'service_period_id' => (int) $period->id,
|
|
|
|
|
'entitlement_id' => null,
|
|
|
|
|
'amount_bytes' => $remaining,
|
|
|
|
|
'effective_at' => $observedAt,
|
|
|
|
|
'observed_at' => $observedAt,
|
|
|
|
|
'payload' => [
|
|
|
|
|
'download_delta_bytes' => $downloadDelta,
|
|
|
|
|
'upload_delta_bytes' => $uploadDelta,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
$lastEventId = (int) $event->id;
|
|
|
|
|
$takeDownload = min($remainingDownload, $remaining);
|
|
|
|
|
$takeUpload = $remaining - $takeDownload;
|
|
|
|
|
$remainingDownload -= $takeDownload;
|
|
|
|
|
$remainingUpload -= $takeUpload;
|
|
|
|
|
self::recordUsageBucketLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
$instanceId,
|
|
|
|
|
(int) $period->id,
|
|
|
|
|
null,
|
|
|
|
|
'overage',
|
|
|
|
|
$bucketStart,
|
|
|
|
|
$remaining,
|
|
|
|
|
$takeDownload,
|
|
|
|
|
$takeUpload,
|
|
|
|
|
$observedAt,
|
|
|
|
|
$applyAt
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ($remainingDownload !== 0 || $remainingUpload !== 0) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'traffic_settlement_direction_allocation_incomplete'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -1364,6 +1381,266 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
return self::resultLocked($pool, true, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Seal a bounded set of completed hourly usage accumulators. */
|
|
|
|
|
public static function sealDueUsageBuckets(
|
|
|
|
|
int $limit = 200,
|
|
|
|
|
?int $now = null
|
|
|
|
|
): int {
|
|
|
|
|
$now ??= time();
|
|
|
|
|
$limit = max(1, min(2000, $limit));
|
|
|
|
|
$currentHour = intdiv($now, 3600) * 3600;
|
|
|
|
|
$ids = CloudTrafficUsageSettlementBucket::where('state', 'open')
|
|
|
|
|
->where('bucket_start', '<', $currentHour)
|
|
|
|
|
->orderBy('bucket_start')->orderBy('id')
|
|
|
|
|
->limit($limit)->pluck('id')
|
|
|
|
|
->map(static fn ($id): int => (int) $id)->all();
|
|
|
|
|
$sealed = 0;
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
|
$hint = CloudTrafficUsageSettlementBucket::find($id);
|
|
|
|
|
if ($hint === null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$connection = $hint->getConnection();
|
|
|
|
|
$sealed += $connection->transaction(
|
|
|
|
|
static function () use ($id, $currentHour): int {
|
|
|
|
|
$hint = CloudTrafficUsageSettlementBucket::find($id);
|
|
|
|
|
if ($hint === null || (string) $hint->state !== 'open'
|
|
|
|
|
|| (int) $hint->bucket_start >= $currentHour) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$pool = CloudTrafficPool::where(
|
|
|
|
|
'id', (int) $hint->pool_id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
if ($pool === null) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'traffic_settlement_pool_missing'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$eventId = self::sealOpenUsageBucketsLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
(int) $hint->instance_id,
|
|
|
|
|
$currentHour,
|
|
|
|
|
$hint->entitlement_id === null
|
|
|
|
|
? null : (int) $hint->entitlement_id,
|
|
|
|
|
(string) $hint->allocation_kind,
|
|
|
|
|
$id
|
|
|
|
|
);
|
|
|
|
|
return $eventId === null ? 0 : 1;
|
|
|
|
|
},
|
|
|
|
|
3
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $sealed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function recordUsageBucketLocked(
|
|
|
|
|
CloudTrafficPool $pool,
|
|
|
|
|
int $instanceId,
|
|
|
|
|
int $servicePeriodId,
|
|
|
|
|
?int $entitlementId,
|
|
|
|
|
string $kind,
|
|
|
|
|
int $bucketStart,
|
|
|
|
|
int $amount,
|
|
|
|
|
int $download,
|
|
|
|
|
int $upload,
|
|
|
|
|
int $observedAt,
|
|
|
|
|
int $now
|
|
|
|
|
): CloudTrafficUsageSettlementBucket {
|
|
|
|
|
if (!in_array($kind, ['entitlement', 'overage'], true)
|
|
|
|
|
|| $amount < 1 || $download < 0 || $upload < 0
|
|
|
|
|
|| $download + $upload !== $amount
|
|
|
|
|
|| $bucketStart % 3600 !== 0
|
|
|
|
|
|| $observedAt < $bucketStart
|
|
|
|
|
|| $observedAt >= $bucketStart + 3600
|
|
|
|
|
|| ($kind === 'entitlement' && ($entitlementId ?? 0) < 1)
|
|
|
|
|
|| ($kind === 'overage' && $entitlementId !== null)) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'traffic_settlement_bucket_input_invalid'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$targetId = $entitlementId ?? 0;
|
|
|
|
|
$identity = CloudTrafficUsageSettlementBucket::where(
|
|
|
|
|
'pool_id', (int) $pool->id
|
|
|
|
|
)->where('instance_id', $instanceId)
|
|
|
|
|
->where('service_period_id', $servicePeriodId)
|
|
|
|
|
->where('allocation_kind', $kind)
|
|
|
|
|
->where('allocation_target_id', $targetId)
|
|
|
|
|
->where('bucket_start', $bucketStart);
|
|
|
|
|
$bucket = (clone $identity)->where('state', 'open')
|
|
|
|
|
->orderByDesc('bucket_revision')->lockForUpdate()->first();
|
|
|
|
|
if ($bucket === null) {
|
|
|
|
|
// A late counter sample may arrive after the hour was sealed, or
|
|
|
|
|
// an attention/overage transition may have sealed the current
|
|
|
|
|
// hour early. Preserve the immutable event and start a new
|
|
|
|
|
// revision for the remaining delta.
|
|
|
|
|
$lastRevision = (int) (clone $identity)
|
|
|
|
|
->lockForUpdate()->max('bucket_revision');
|
|
|
|
|
$bucket = new CloudTrafficUsageSettlementBucket();
|
|
|
|
|
$bucket->pool_id = (int) $pool->id;
|
|
|
|
|
$bucket->user_id = (int) $pool->user_id;
|
|
|
|
|
$bucket->instance_id = $instanceId;
|
|
|
|
|
$bucket->service_period_id = $servicePeriodId;
|
|
|
|
|
$bucket->entitlement_id = $entitlementId;
|
|
|
|
|
$bucket->allocation_kind = $kind;
|
|
|
|
|
$bucket->allocation_target_id = $targetId;
|
|
|
|
|
$bucket->bucket_start = $bucketStart;
|
|
|
|
|
$bucket->bucket_revision = $lastRevision + 1;
|
|
|
|
|
$bucket->amount_bytes = $amount;
|
|
|
|
|
$bucket->download_bytes = $download;
|
|
|
|
|
$bucket->upload_bytes = $upload;
|
|
|
|
|
$bucket->sample_count = 1;
|
|
|
|
|
$bucket->first_observed_at = $observedAt;
|
|
|
|
|
$bucket->last_observed_at = $observedAt;
|
|
|
|
|
$bucket->state = 'open';
|
|
|
|
|
$bucket->sealed_event_id = null;
|
|
|
|
|
$bucket->created_at = $now;
|
|
|
|
|
$bucket->updated_at = $now;
|
|
|
|
|
$bucket->save();
|
|
|
|
|
return $bucket;
|
|
|
|
|
}
|
|
|
|
|
if ((string) $bucket->state !== 'open'
|
|
|
|
|
|| (int) $bucket->user_id !== (int) $pool->user_id
|
|
|
|
|
|| (int) $bucket->service_period_id !== $servicePeriodId
|
|
|
|
|
|| (int) ($bucket->entitlement_id ?? 0) !== $targetId) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'traffic_settlement_bucket_identity_conflict'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
foreach ([
|
|
|
|
|
'amount_bytes' => $amount,
|
|
|
|
|
'download_bytes' => $download,
|
|
|
|
|
'upload_bytes' => $upload,
|
|
|
|
|
] as $field => $increment) {
|
|
|
|
|
if ($increment > PHP_INT_MAX - (int) $bucket->{$field}) {
|
|
|
|
|
throw new \OverflowException(
|
|
|
|
|
'traffic_settlement_bucket_overflow'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$bucket->{$field} = (int) $bucket->{$field} + $increment;
|
|
|
|
|
}
|
|
|
|
|
$bucket->sample_count = (int) $bucket->sample_count + 1;
|
|
|
|
|
$bucket->first_observed_at = min(
|
|
|
|
|
(int) $bucket->first_observed_at,
|
|
|
|
|
$observedAt
|
|
|
|
|
);
|
|
|
|
|
$bucket->last_observed_at = max(
|
|
|
|
|
(int) $bucket->last_observed_at,
|
|
|
|
|
$observedAt
|
|
|
|
|
);
|
|
|
|
|
$bucket->updated_at = $now;
|
|
|
|
|
$bucket->save();
|
|
|
|
|
return $bucket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Seal matching buckets in deterministic lock order.
|
|
|
|
|
*
|
|
|
|
|
* @return null|int last immutable event ID
|
|
|
|
|
*/
|
|
|
|
|
private static function sealOpenUsageBucketsLocked(
|
|
|
|
|
CloudTrafficPool $pool,
|
|
|
|
|
?int $instanceId = null,
|
|
|
|
|
?int $beforeBucket = null,
|
|
|
|
|
?int $entitlementId = null,
|
|
|
|
|
?string $kind = null,
|
|
|
|
|
?int $onlyBucketId = null
|
|
|
|
|
): ?int {
|
|
|
|
|
$query = CloudTrafficUsageSettlementBucket::where(
|
|
|
|
|
'pool_id', (int) $pool->id
|
|
|
|
|
)->where('state', 'open');
|
|
|
|
|
if ($instanceId !== null) {
|
|
|
|
|
$query->where('instance_id', $instanceId);
|
|
|
|
|
}
|
|
|
|
|
if ($beforeBucket !== null) {
|
|
|
|
|
$query->where('bucket_start', '<', $beforeBucket);
|
|
|
|
|
}
|
|
|
|
|
if ($entitlementId !== null) {
|
|
|
|
|
$query->where('entitlement_id', $entitlementId);
|
|
|
|
|
}
|
|
|
|
|
if ($kind !== null) {
|
|
|
|
|
$query->where('allocation_kind', $kind);
|
|
|
|
|
}
|
|
|
|
|
if ($onlyBucketId !== null) {
|
|
|
|
|
$query->where('id', $onlyBucketId);
|
|
|
|
|
}
|
|
|
|
|
$candidateIds = (clone $query)->orderBy('bucket_start')
|
|
|
|
|
->orderBy('id')->pluck('id')
|
|
|
|
|
->map(static fn ($id): int => (int) $id)->all();
|
|
|
|
|
if ($candidateIds === []) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$entitlementIds = CloudTrafficUsageSettlementBucket::whereIn(
|
|
|
|
|
'id', $candidateIds
|
|
|
|
|
)->whereNotNull('entitlement_id')->orderBy('entitlement_id')
|
|
|
|
|
->pluck('entitlement_id')->map(
|
|
|
|
|
static fn ($id): int => (int) $id
|
|
|
|
|
)->unique()->values()->all();
|
|
|
|
|
if ($entitlementIds !== []) {
|
|
|
|
|
CloudTrafficEntitlement::whereIn('id', $entitlementIds)
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
}
|
|
|
|
|
$buckets = CloudTrafficUsageSettlementBucket::whereIn(
|
|
|
|
|
'id', $candidateIds
|
|
|
|
|
)->where('state', 'open')->orderBy('bucket_start')->orderBy('id')
|
|
|
|
|
->lockForUpdate()->get();
|
|
|
|
|
$lastEventId = null;
|
|
|
|
|
foreach ($buckets as $bucket) {
|
|
|
|
|
$lastEventId = self::sealUsageBucketLocked($pool, $bucket);
|
|
|
|
|
}
|
|
|
|
|
return $lastEventId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function sealUsageBucketLocked(
|
|
|
|
|
CloudTrafficPool $pool,
|
|
|
|
|
CloudTrafficUsageSettlementBucket $bucket
|
|
|
|
|
): int {
|
|
|
|
|
if ((int) $bucket->pool_id !== (int) $pool->id) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'traffic_settlement_bucket_pool_conflict'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ((string) $bucket->state === 'sealed') {
|
|
|
|
|
$eventId = (int) ($bucket->sealed_event_id ?? 0);
|
|
|
|
|
if ($eventId < 1) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
'traffic_settlement_sealed_event_missing'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $eventId;
|
|
|
|
|
}
|
|
|
|
|
$eventType = (string) $bucket->allocation_kind === 'entitlement'
|
|
|
|
|
? 'usage_allocated' : 'overage_recorded';
|
|
|
|
|
$event = self::appendEventLocked($pool, [
|
|
|
|
|
'event_key' => 'traffic-settlement-bucket:' . (int) $bucket->id,
|
|
|
|
|
'event_type' => $eventType,
|
|
|
|
|
'source_type' => 'traffic_settlement',
|
|
|
|
|
'source_id' => (string) $bucket->id,
|
|
|
|
|
'source_revision' => 1,
|
|
|
|
|
'instance_id' => (int) $bucket->instance_id,
|
|
|
|
|
'service_period_id' => (int) $bucket->service_period_id,
|
|
|
|
|
'entitlement_id' => $bucket->entitlement_id === null
|
|
|
|
|
? null : (int) $bucket->entitlement_id,
|
|
|
|
|
'amount_bytes' => (int) $bucket->amount_bytes,
|
|
|
|
|
'effective_at' => (int) $bucket->bucket_start,
|
|
|
|
|
'observed_at' => (int) $bucket->last_observed_at,
|
|
|
|
|
'payload' => [
|
|
|
|
|
'bucket_start' => (int) $bucket->bucket_start,
|
|
|
|
|
'bucket_revision' => (int) $bucket->bucket_revision,
|
|
|
|
|
'bucket_seconds' => 3600,
|
|
|
|
|
'download_bytes' => (int) $bucket->download_bytes,
|
|
|
|
|
'upload_bytes' => (int) $bucket->upload_bytes,
|
|
|
|
|
'sample_count' => (int) $bucket->sample_count,
|
|
|
|
|
'first_observed_at' => (int) $bucket->first_observed_at,
|
|
|
|
|
'last_observed_at' => (int) $bucket->last_observed_at,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
$bucket->state = 'sealed';
|
|
|
|
|
$bucket->sealed_event_id = (int) $event->id;
|
|
|
|
|
$bucket->updated_at = time();
|
|
|
|
|
$bucket->save();
|
|
|
|
|
return (int) $event->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function insertPoolIfMissing(
|
|
|
|
|
$connection,
|
|
|
|
|
int $userId,
|
|
|
|
|
@ -1494,6 +1771,13 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
if ((string) $entitlement->state === 'expired') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
self::sealOpenUsageBucketsLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
(int) $entitlement->instance_id,
|
|
|
|
|
null,
|
|
|
|
|
(int) $entitlement->id,
|
|
|
|
|
'entitlement'
|
|
|
|
|
);
|
|
|
|
|
$remaining = self::entitlementRemaining($entitlement);
|
|
|
|
|
if ($remaining > 0) {
|
|
|
|
|
$entitlement->forfeited_bytes =
|
|
|
|
|
@ -1531,6 +1815,13 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
if ($remainingDebt <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
self::sealOpenUsageBucketsLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
'overage'
|
|
|
|
|
);
|
|
|
|
|
$entitlements = CloudTrafficEntitlement::where(
|
|
|
|
|
'pool_id', (int) $pool->id
|
|
|
|
|
)->where('quota_mode', 'finite')->where('state', 'active')
|
|
|
|
|
@ -1741,6 +2032,10 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
string $code,
|
|
|
|
|
int $observedAt
|
|
|
|
|
): void {
|
|
|
|
|
self::sealOpenUsageBucketsLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
(int) $cursor->instance_id
|
|
|
|
|
);
|
|
|
|
|
if ((string) $cursor->state === 'attention'
|
|
|
|
|
&& (string) $cursor->attention_code === $code) {
|
|
|
|
|
if ((string) $pool->state !== 'attention') {
|
|
|
|
|
|