-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add classes and configs to be run for measurement retention policy (#955
- Loading branch information
Showing
11 changed files
with
715 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/org/wfanet/measurement/kingdom/batch/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") | ||
|
||
package(default_visibility = [ | ||
"//src/main/kotlin/org/wfanet/measurement/kingdom/batch/spanner:__subpackages__", | ||
"//src/main/kotlin/org/wfanet/measurement/kingdom/deploy/common:__subpackages__", | ||
]) | ||
|
||
kt_jvm_library( | ||
name = "pending_measurements_cancellation", | ||
srcs = ["PendingMeasurementsCancellation.kt"], | ||
deps = [ | ||
"//imports/java/io/opentelemetry/api", | ||
"//src/main/proto/wfa/measurement/internal/kingdom:measurements_service_kt_jvm_grpc_proto", | ||
"@wfa_common_jvm//imports/kotlin/kotlinx/coroutines:core", | ||
"@wfa_common_jvm//src/main/kotlin/org/wfanet/measurement/common", | ||
], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "completed_measurements_deletion", | ||
srcs = ["CompletedMeasurementsDeletion.kt"], | ||
deps = [ | ||
"//imports/java/io/opentelemetry/api", | ||
"//src/main/proto/wfa/measurement/internal/kingdom:measurements_service_kt_jvm_grpc_proto", | ||
"@wfa_common_jvm//imports/kotlin/kotlinx/coroutines:core", | ||
"@wfa_common_jvm//src/main/kotlin/org/wfanet/measurement/common", | ||
], | ||
) |
111 changes: 111 additions & 0 deletions
111
src/main/kotlin/org/wfanet/measurement/kingdom/batch/CompletedMeasurementsDeletion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2023 The Cross-Media Measurement Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.wfanet.measurement.kingdom.batch | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry | ||
import io.opentelemetry.api.OpenTelemetry | ||
import io.opentelemetry.api.metrics.LongCounter | ||
import io.opentelemetry.api.metrics.Meter | ||
import java.time.Clock | ||
import java.time.Duration | ||
import java.util.logging.Logger | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.runBlocking | ||
import org.wfanet.measurement.common.toProtoTime | ||
import org.wfanet.measurement.internal.kingdom.DeleteMeasurementRequest | ||
import org.wfanet.measurement.internal.kingdom.Measurement | ||
import org.wfanet.measurement.internal.kingdom.MeasurementsGrpcKt.MeasurementsCoroutineStub | ||
import org.wfanet.measurement.internal.kingdom.StreamMeasurementsRequestKt | ||
import org.wfanet.measurement.internal.kingdom.batchDeleteMeasurementsRequest | ||
import org.wfanet.measurement.internal.kingdom.deleteMeasurementRequest | ||
import org.wfanet.measurement.internal.kingdom.streamMeasurementsRequest | ||
|
||
private val COMPLETED_MEASUREMENT_STATES = | ||
listOf(Measurement.State.SUCCEEDED, Measurement.State.FAILED, Measurement.State.CANCELLED) | ||
private const val MAX_BATCH_DELETE = 1000 | ||
|
||
class CompletedMeasurementsDeletion( | ||
private val measurementsService: MeasurementsCoroutineStub, | ||
private val timeToLive: Duration, | ||
private val dryRun: Boolean = false, | ||
private val clock: Clock = Clock.systemUTC(), | ||
private val openTelemetry: OpenTelemetry = GlobalOpenTelemetry.get() | ||
) { | ||
private val meter: Meter = openTelemetry.getMeter(CompletedMeasurementsDeletion::class.java.name) | ||
private val completedMeasurementDeletionCounter: LongCounter = | ||
meter | ||
.counterBuilder("completed_measurements_deletion_total") | ||
.setDescription("Total number of completed measurements deleted under retention policy") | ||
.build() | ||
fun run() { | ||
if (timeToLive.toMillis() == 0L) { | ||
logger.warning("Time to live cannot be 0. TTL=$timeToLive") | ||
} | ||
val currentTime = clock.instant() | ||
runBlocking { | ||
var measurementsToDelete: List<Measurement> = | ||
measurementsService | ||
.streamMeasurements( | ||
streamMeasurementsRequest { | ||
filter = | ||
StreamMeasurementsRequestKt.filter { | ||
states += COMPLETED_MEASUREMENT_STATES | ||
updatedBefore = currentTime.minus(timeToLive).toProtoTime() | ||
} | ||
} | ||
) | ||
.toList() | ||
|
||
if (dryRun) { | ||
logger.info { "Measurements that would have been deleted: $measurementsToDelete" } | ||
} else { | ||
while (measurementsToDelete.isNotEmpty()) { | ||
val batchMeasurementsToDelete = measurementsToDelete.take(MAX_BATCH_DELETE) | ||
val deleteRequests: List<DeleteMeasurementRequest> = | ||
batchMeasurementsToDelete.map { | ||
deleteMeasurementRequest { | ||
externalMeasurementId = it.externalMeasurementId | ||
externalMeasurementConsumerId = it.externalMeasurementConsumerId | ||
etag = it.etag | ||
} | ||
} | ||
measurementsService.batchDeleteMeasurements( | ||
batchDeleteMeasurementsRequest { requests += deleteRequests } | ||
) | ||
completedMeasurementDeletionCounter.add(deleteRequests.size.toLong()) | ||
|
||
measurementsToDelete = | ||
measurementsService | ||
.streamMeasurements( | ||
streamMeasurementsRequest { | ||
filter = | ||
StreamMeasurementsRequestKt.filter { | ||
states += COMPLETED_MEASUREMENT_STATES | ||
updatedBefore = currentTime.minus(timeToLive).toProtoTime() | ||
} | ||
} | ||
) | ||
.toList() | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private val logger: Logger = Logger.getLogger(this::class.java.name) | ||
} | ||
} |
Oops, something went wrong.