|
|
|
|
@ -23,6 +23,7 @@ use App\Models\CloudTrafficUsageSettlementBucket;
|
|
|
|
|
final class CloudTrafficPoolAccounting
|
|
|
|
|
{
|
|
|
|
|
public const CLOSING_SECONDS = 90;
|
|
|
|
|
public const SETTLEMENT_WINDOW_SECONDS = 86400;
|
|
|
|
|
|
|
|
|
|
/** Reconcile one service period into its one entitlement. */
|
|
|
|
|
public static function reconcileServicePeriod(
|
|
|
|
|
@ -1260,11 +1261,14 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$instanceId = (int) $instance->id;
|
|
|
|
|
$bucketStart = intdiv($observedAt, 3600) * 3600;
|
|
|
|
|
$bucketStart = intdiv(
|
|
|
|
|
$observedAt,
|
|
|
|
|
self::SETTLEMENT_WINDOW_SECONDS
|
|
|
|
|
) * self::SETTLEMENT_WINDOW_SECONDS;
|
|
|
|
|
$lastEventId = self::sealOpenUsageBucketsLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
$instanceId,
|
|
|
|
|
$bucketStart
|
|
|
|
|
$observedAt
|
|
|
|
|
);
|
|
|
|
|
if ((string) $period->traffic_quota_mode !== 'unlimited') {
|
|
|
|
|
$remaining = $delta;
|
|
|
|
|
@ -1381,17 +1385,16 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
return self::resultLocked($pool, true, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Seal a bounded set of completed hourly usage accumulators. */
|
|
|
|
|
/** Seal a bounded set of completed 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')
|
|
|
|
|
->where('seal_at', '<=', $now)
|
|
|
|
|
->orderBy('seal_at')->orderBy('id')
|
|
|
|
|
->limit($limit)->pluck('id')
|
|
|
|
|
->map(static fn ($id): int => (int) $id)->all();
|
|
|
|
|
$sealed = 0;
|
|
|
|
|
@ -1402,10 +1405,10 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
}
|
|
|
|
|
$connection = $hint->getConnection();
|
|
|
|
|
$sealed += $connection->transaction(
|
|
|
|
|
static function () use ($id, $currentHour): int {
|
|
|
|
|
static function () use ($id, $now): int {
|
|
|
|
|
$hint = CloudTrafficUsageSettlementBucket::find($id);
|
|
|
|
|
if ($hint === null || (string) $hint->state !== 'open'
|
|
|
|
|
|| (int) $hint->bucket_start >= $currentHour) {
|
|
|
|
|
|| (int) $hint->seal_at > $now) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$pool = CloudTrafficPool::where(
|
|
|
|
|
@ -1419,7 +1422,7 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
$eventId = self::sealOpenUsageBucketsLocked(
|
|
|
|
|
$pool,
|
|
|
|
|
(int) $hint->instance_id,
|
|
|
|
|
$currentHour,
|
|
|
|
|
null,
|
|
|
|
|
$hint->entitlement_id === null
|
|
|
|
|
? null : (int) $hint->entitlement_id,
|
|
|
|
|
(string) $hint->allocation_kind,
|
|
|
|
|
@ -1449,9 +1452,10 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
if (!in_array($kind, ['entitlement', 'overage'], true)
|
|
|
|
|
|| $amount < 1 || $download < 0 || $upload < 0
|
|
|
|
|
|| $download + $upload !== $amount
|
|
|
|
|
|| $bucketStart % 3600 !== 0
|
|
|
|
|
|| $bucketStart % self::SETTLEMENT_WINDOW_SECONDS !== 0
|
|
|
|
|
|| $observedAt < $bucketStart
|
|
|
|
|
|| $observedAt >= $bucketStart + 3600
|
|
|
|
|
|| $observedAt >= $bucketStart
|
|
|
|
|
+ self::SETTLEMENT_WINDOW_SECONDS
|
|
|
|
|
|| ($kind === 'entitlement' && ($entitlementId ?? 0) < 1)
|
|
|
|
|
|| ($kind === 'overage' && $entitlementId !== null)) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
@ -1459,19 +1463,22 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$targetId = $entitlementId ?? 0;
|
|
|
|
|
$bucketSeconds = self::SETTLEMENT_WINDOW_SECONDS;
|
|
|
|
|
$sealAt = $bucketStart + $bucketSeconds;
|
|
|
|
|
$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);
|
|
|
|
|
->where('bucket_start', $bucketStart)
|
|
|
|
|
->where('bucket_seconds', $bucketSeconds);
|
|
|
|
|
$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
|
|
|
|
|
// A late counter sample may arrive after the window was sealed, or
|
|
|
|
|
// an attention/overage transition may have sealed the current
|
|
|
|
|
// hour early. Preserve the immutable event and start a new
|
|
|
|
|
// window early. Preserve the immutable event and start a new
|
|
|
|
|
// revision for the remaining delta.
|
|
|
|
|
$lastRevision = (int) (clone $identity)
|
|
|
|
|
->lockForUpdate()->max('bucket_revision');
|
|
|
|
|
@ -1484,6 +1491,8 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
$bucket->allocation_kind = $kind;
|
|
|
|
|
$bucket->allocation_target_id = $targetId;
|
|
|
|
|
$bucket->bucket_start = $bucketStart;
|
|
|
|
|
$bucket->bucket_seconds = $bucketSeconds;
|
|
|
|
|
$bucket->seal_at = $sealAt;
|
|
|
|
|
$bucket->bucket_revision = $lastRevision + 1;
|
|
|
|
|
$bucket->amount_bytes = $amount;
|
|
|
|
|
$bucket->download_bytes = $download;
|
|
|
|
|
@ -1540,7 +1549,7 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
private static function sealOpenUsageBucketsLocked(
|
|
|
|
|
CloudTrafficPool $pool,
|
|
|
|
|
?int $instanceId = null,
|
|
|
|
|
?int $beforeBucket = null,
|
|
|
|
|
?int $dueAt = null,
|
|
|
|
|
?int $entitlementId = null,
|
|
|
|
|
?string $kind = null,
|
|
|
|
|
?int $onlyBucketId = null
|
|
|
|
|
@ -1551,8 +1560,8 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
if ($instanceId !== null) {
|
|
|
|
|
$query->where('instance_id', $instanceId);
|
|
|
|
|
}
|
|
|
|
|
if ($beforeBucket !== null) {
|
|
|
|
|
$query->where('bucket_start', '<', $beforeBucket);
|
|
|
|
|
if ($dueAt !== null) {
|
|
|
|
|
$query->where('seal_at', '<=', $dueAt);
|
|
|
|
|
}
|
|
|
|
|
if ($entitlementId !== null) {
|
|
|
|
|
$query->where('entitlement_id', $entitlementId);
|
|
|
|
|
@ -1626,7 +1635,8 @@ final class CloudTrafficPoolAccounting
|
|
|
|
|
'payload' => [
|
|
|
|
|
'bucket_start' => (int) $bucket->bucket_start,
|
|
|
|
|
'bucket_revision' => (int) $bucket->bucket_revision,
|
|
|
|
|
'bucket_seconds' => 3600,
|
|
|
|
|
'bucket_seconds' => (int) $bucket->bucket_seconds,
|
|
|
|
|
'seal_at' => (int) $bucket->seal_at,
|
|
|
|
|
'download_bytes' => (int) $bucket->download_bytes,
|
|
|
|
|
'upload_bytes' => (int) $bucket->upload_bytes,
|
|
|
|
|
'sample_count' => (int) $bucket->sample_count,
|
|
|
|
|
|