Skip to content

Commit

Permalink
Add detailed logging to event groups and metadata descriptors services (
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorilla authored and ple13 committed Aug 16, 2024
1 parent f46693b commit c32badb
Show file tree
Hide file tree
Showing 10 changed files with 516 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import io.grpc.Status
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.wfanet.measurement.common.ProtoReflection
import org.wfanet.measurement.common.grpc.failGrpc
import org.wfanet.measurement.common.grpc.grpcRequire
import org.wfanet.measurement.common.identity.ExternalId
import org.wfanet.measurement.common.identity.IdGenerator
import org.wfanet.measurement.gcloud.spanner.AsyncDatabaseClient
import org.wfanet.measurement.internal.kingdom.EventGroupMetadataDescriptor
Expand Down Expand Up @@ -74,7 +74,11 @@ class SpannerEventGroupMetadataDescriptorsService(
request.externalEventGroupMetadataDescriptorId,
)
?.eventGroupMetadataDescriptor
?: failGrpc(Status.NOT_FOUND) { "EventGroupMetadataDescriptor not found" }
?: throw EventGroupMetadataDescriptorNotFoundException(
ExternalId(request.externalDataProviderId),
ExternalId(request.externalEventGroupMetadataDescriptorId),
)
.asStatusRuntimeException(Status.Code.NOT_FOUND, "EventGroupMetadataDescriptor not found.")
}

override suspend fun updateEventGroupMetadataDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,17 @@ class EventGroupNotFoundByMeasurementConsumerException(
}

class EventGroupInvalidArgsException(
val originalExternalMeasurementId: ExternalId,
val providedExternalMeasurementId: ExternalId,
val originalExternalMeasurementConsumerId: ExternalId,
val providedExternalMeasurementConsumerId: ExternalId,
provideDescription: () -> String = { "EventGroup invalid arguments" },
) : KingdomInternalException(ErrorCode.EVENT_GROUP_INVALID_ARGS, provideDescription) {
override val context
get() =
mapOf(
"original_external_measurement_id" to originalExternalMeasurementId.value.toString(),
"provided_external_measurement_id" to providedExternalMeasurementId.value.toString(),
"original_external_measurement_consumer_id" to
originalExternalMeasurementConsumerId.value.toString(),
"provided_external_measurement_consumer_id" to
providedExternalMeasurementConsumerId.value.toString(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ kt_jvm_library(
"//src/main/kotlin/org/wfanet/measurement/api:public_api_version",
"//src/main/kotlin/org/wfanet/measurement/api/v2alpha:principal_server_interceptor",
"//src/main/kotlin/org/wfanet/measurement/api/v2alpha:resource_key",
"//src/main/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha:internal_status_conversion",
"//src/main/proto/wfa/measurement/api/v2alpha:crypto_kt_jvm_proto",
"//src/main/proto/wfa/measurement/api/v2alpha:event_group_kt_jvm_proto",
"//src/main/proto/wfa/measurement/api/v2alpha:event_groups_service_kt_jvm_grpc_proto",
Expand All @@ -164,6 +165,7 @@ kt_jvm_library(
"//src/main/kotlin/org/wfanet/measurement/api:public_api_version",
"//src/main/kotlin/org/wfanet/measurement/api/v2alpha:principal_server_interceptor",
"//src/main/kotlin/org/wfanet/measurement/api/v2alpha:resource_key",
"//src/main/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha:internal_status_conversion",
"//src/main/proto/wfa/measurement/api/v2alpha:crypto_kt_jvm_proto",
"//src/main/proto/wfa/measurement/api/v2alpha:event_group_metadata_descriptor_kt_jvm_proto",
"//src/main/proto/wfa/measurement/api/v2alpha:event_group_metadata_descriptors_service_kt_jvm_grpc_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ class EventGroupMetadataDescriptorsService(
val internalEventGroupMetadataDescriptor =
try {
internalEventGroupMetadataDescriptorsStub.getEventGroupMetadataDescriptor(getRequest)
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.NOT_FOUND ->
failGrpc(Status.NOT_FOUND, ex) { "EventGroupMetadataDescriptor not found" }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}
return internalEventGroupMetadataDescriptor.toEventGroupMetadataDescriptor()
}
Expand Down Expand Up @@ -154,12 +153,11 @@ class EventGroupMetadataDescriptorsService(
val internalEventGroupMetadataDescriptor =
try {
internalEventGroupMetadataDescriptorsStub.createEventGroupMetadataDescriptor(createRequest)
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.NOT_FOUND ->
failGrpc(Status.NOT_FOUND, ex) { "EventGroupMetadataDescriptor not found" }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}
return internalEventGroupMetadataDescriptor.toEventGroupMetadataDescriptor()
}
Expand Down Expand Up @@ -201,14 +199,12 @@ class EventGroupMetadataDescriptorsService(
val internalEventGroupMetadataDescriptor =
try {
internalEventGroupMetadataDescriptorsStub.updateEventGroupMetadataDescriptor(updateRequest)
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.INVALID_ARGUMENT ->
failGrpc(Status.INVALID_ARGUMENT, ex) { "Required field unspecified or invalid." }
Status.Code.NOT_FOUND ->
failGrpc(Status.NOT_FOUND, ex) { "EventGroupMetadataDescriptor not found" }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.INVALID_ARGUMENT -> Status.INVALID_ARGUMENT
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}
return internalEventGroupMetadataDescriptor.toEventGroupMetadataDescriptor()
}
Expand Down Expand Up @@ -314,11 +310,9 @@ class EventGroupMetadataDescriptorsService(
.toList()
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.DEADLINE_EXCEEDED -> Status.DEADLINE_EXCEEDED
else -> Status.UNKNOWN
}
.withCause(e)
.asRuntimeException()
Status.Code.DEADLINE_EXCEEDED -> Status.DEADLINE_EXCEEDED
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}

if (results.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import org.wfanet.measurement.common.api.ChildResourceKey
import org.wfanet.measurement.common.api.ResourceKey
import org.wfanet.measurement.common.base64UrlDecode
import org.wfanet.measurement.common.base64UrlEncode
import org.wfanet.measurement.common.grpc.failGrpc
import org.wfanet.measurement.common.grpc.grpcRequire
import org.wfanet.measurement.common.grpc.grpcRequireNotNull
import org.wfanet.measurement.common.identity.ApiId
Expand Down Expand Up @@ -136,17 +135,15 @@ class EventGroupsService(private val internalEventGroupsStub: InternalEventGroup
internalEventGroupsStub.getEventGroup(internalRequest)
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.NOT_FOUND ->
if (key.parentKey == principal.resourceKey) {
Status.NOT_FOUND
} else {
permissionDeniedStatus()
}
Status.Code.DEADLINE_EXCEEDED -> Status.DEADLINE_EXCEEDED
else -> Status.UNKNOWN
}
.withCause(e)
.asRuntimeException()
Status.Code.NOT_FOUND ->
if (key.parentKey == principal.resourceKey) {
Status.NOT_FOUND
} else {
permissionDeniedStatus()
}
Status.Code.DEADLINE_EXCEEDED -> Status.DEADLINE_EXCEEDED
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}

when (principal) {
Expand Down Expand Up @@ -191,14 +188,13 @@ class EventGroupsService(private val internalEventGroupsStub: InternalEventGroup
}
return try {
internalEventGroupsStub.createEventGroup(internalRequest).toEventGroup()
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.DEADLINE_EXCEEDED -> throw Status.DEADLINE_EXCEEDED.asRuntimeException()
Status.Code.FAILED_PRECONDITION ->
failGrpc(Status.FAILED_PRECONDITION, ex) { ex.message ?: "Failed precondition" }
Status.Code.NOT_FOUND -> failGrpc(Status.NOT_FOUND, ex) { "DataProvider not found." }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.DEADLINE_EXCEEDED -> Status.DEADLINE_EXCEEDED
Status.Code.FAILED_PRECONDITION -> Status.FAILED_PRECONDITION
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}
}

Expand All @@ -221,15 +217,13 @@ class EventGroupsService(private val internalEventGroupsStub: InternalEventGroup
}
return try {
internalEventGroupsStub.updateEventGroup(updateRequest).toEventGroup()
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.INVALID_ARGUMENT ->
failGrpc(Status.INVALID_ARGUMENT, ex) { "Required field unspecified or invalid." }
Status.Code.FAILED_PRECONDITION ->
failGrpc(Status.FAILED_PRECONDITION, ex) { ex.message ?: "Failed precondition." }
Status.Code.NOT_FOUND -> failGrpc(Status.NOT_FOUND, ex) { "EventGroup not found." }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.INVALID_ARGUMENT -> Status.INVALID_ARGUMENT
Status.Code.FAILED_PRECONDITION -> Status.FAILED_PRECONDITION
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}
}

Expand Down Expand Up @@ -297,15 +291,13 @@ class EventGroupsService(private val internalEventGroupsStub: InternalEventGroup

return try {
internalEventGroupsStub.deleteEventGroup(deleteRequest).toEventGroup()
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.INVALID_ARGUMENT ->
failGrpc(Status.INVALID_ARGUMENT, ex) { "Required field unspecified or invalid." }
Status.Code.FAILED_PRECONDITION ->
failGrpc(Status.FAILED_PRECONDITION, ex) { ex.message ?: "Failed precondition." }
Status.Code.NOT_FOUND -> failGrpc(Status.NOT_FOUND, ex) { "EventGroup not found." }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.INVALID_ARGUMENT -> Status.INVALID_ARGUMENT
Status.Code.FAILED_PRECONDITION -> Status.FAILED_PRECONDITION
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}
}

Expand Down Expand Up @@ -344,11 +336,9 @@ class EventGroupsService(private val internalEventGroupsStub: InternalEventGroup
internalEventGroupsStub.streamEventGroups(internalRequest).toList()
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.DEADLINE_EXCEEDED -> Status.DEADLINE_EXCEEDED
else -> Status.UNKNOWN
}
.withCause(e)
.asRuntimeException()
Status.Code.DEADLINE_EXCEEDED -> Status.DEADLINE_EXCEEDED
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}

if (internalEventGroups.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import org.wfanet.measurement.api.v2alpha.DataProviderCertificateKey
import org.wfanet.measurement.api.v2alpha.DataProviderKey
import org.wfanet.measurement.api.v2alpha.DuchyCertificateKey
import org.wfanet.measurement.api.v2alpha.DuchyKey
import org.wfanet.measurement.api.v2alpha.EventGroupKey
import org.wfanet.measurement.api.v2alpha.EventGroupMetadataDescriptorKey
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerCertificateKey
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerEventGroupKey
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerKey
import org.wfanet.measurement.api.v2alpha.MeasurementKey
import org.wfanet.measurement.api.v2alpha.ModelProviderCertificateKey
Expand Down Expand Up @@ -317,13 +320,73 @@ fun Status.toExternalStatusRuntimeException(
errorMessage = "ApiKey not found."
}
ErrorCode.EVENT_GROUP_NOT_FOUND -> {
errorMessage = "EventGroup not found."
if (errorInfo.metadataMap.containsKey("external_data_provider_id")) {
val eventGroupName =
EventGroupKey(
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_data_provider_id"]).toLong()
),
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_event_group_id"]).toLong()
),
)
.toName()
put("eventGroup", eventGroupName)
errorMessage = "EventGroup $eventGroupName not found."
} else if (errorInfo.metadataMap.containsKey("external_measurement_consumer_id")) {
val eventGroupName =
MeasurementConsumerEventGroupKey(
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_measurement_consumer_id"]).toLong()
),
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_event_group_id"]).toLong()
),
)
.toName()
put("eventGroup", eventGroupName)
errorMessage = "EventGroup $eventGroupName not found."
} else {
errorMessage = "EventGroup not found."
}
}
ErrorCode.EVENT_GROUP_INVALID_ARGS -> {
errorMessage = "EventGroup invalid arguments."
val originalMeasurementConsumerName =
MeasurementConsumerKey(
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["original_external_measurement_consumer_id"])
.toLong()
)
)
.toName()
val providedMeasurementConsumerName =
MeasurementConsumerKey(
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["provided_external_measurement_consumer_id"])
.toLong()
)
)
.toName()
put("originalMeasurementConsumer", originalMeasurementConsumerName)
put("providedMeasurementConsumer", providedMeasurementConsumerName)
errorMessage =
"EventGroup argument invalid: expected $originalMeasurementConsumerName but got $providedMeasurementConsumerName"
}
ErrorCode.EVENT_GROUP_METADATA_DESCRIPTOR_NOT_FOUND -> {
errorMessage = "EventGroup metadata descriptor not found."
val eventGroupMetadataDescriptorName =
EventGroupMetadataDescriptorKey(
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_data_provider_id"]).toLong()
),
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_event_group_metadata_descriptor_id"])
.toLong()
),
)
.toName()
put("eventGroupMetadataDescriptor", eventGroupMetadataDescriptorName)
errorMessage =
"EventGroup metadata descriptor $eventGroupMetadataDescriptorName not found."
}
ErrorCode.EVENT_GROUP_METADATA_DESCRIPTOR_ALREADY_EXISTS_WITH_TYPE -> {
errorMessage = "EventGroupMetadataDescriptor with same type already exists."
Expand Down Expand Up @@ -367,7 +430,18 @@ fun Status.toExternalStatusRuntimeException(
errorMessage = "ExchangeStepAttempt $exchangeStepAttemptName not found."
}
ErrorCode.EVENT_GROUP_STATE_ILLEGAL -> {
errorMessage = "EventGroup not found."
val eventGroupName =
EventGroupKey(
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_data_provider_id"]).toLong()
),
externalIdToApiId(
checkNotNull(errorInfo.metadataMap["external_event_group_id"]).toLong()
),
)
.toName()
put("eventGroup", eventGroupName)
errorMessage = "EventGroup $eventGroupName not found."
}
ErrorCode.MEASUREMENT_ETAG_MISMATCH -> {
errorMessage = "Measurement is inconsistent with initial state."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ abstract class EventGroupMetadataDescriptorsServiceTest<
@Test
fun `getEventGroupMetadataDescriptor fails for missing EventGroupMetadataDescriptor`() =
runBlocking {
val externalDataProviderId =
population.createDataProvider(dataProvidersService).externalDataProviderId
val exception =
assertFailsWith<StatusRuntimeException> {
eventGroupMetadataDescriptorService.getEventGroupMetadataDescriptor(
getEventGroupMetadataDescriptorRequest { externalEventGroupMetadataDescriptorId = 1L }
getEventGroupMetadataDescriptorRequest {
this.externalDataProviderId = externalDataProviderId
externalEventGroupMetadataDescriptorId = 1L
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ kt_jvm_test(
test_class = "org.wfanet.measurement.kingdom.service.api.v2alpha.EventGroupsServiceTest",
deps = [
"//src/main/kotlin/org/wfanet/measurement/api/v2alpha/testing",
"//src/main/kotlin/org/wfanet/measurement/kingdom/deploy/gcloud/spanner/common",
"@wfa_common_jvm//imports/java/com/google/common/truth",
"@wfa_common_jvm//imports/java/com/google/common/truth/extensions/proto",
"@wfa_common_jvm//imports/java/com/google/protobuf",
Expand All @@ -115,6 +116,7 @@ kt_jvm_test(
],
test_class = "org.wfanet.measurement.kingdom.service.api.v2alpha.EventGroupMetadataDescriptorsServiceTest",
deps = [
"//src/main/kotlin/org/wfanet/measurement/kingdom/deploy/gcloud/spanner/common",
"@wfa_common_jvm//imports/java/com/google/common/truth",
"@wfa_common_jvm//imports/java/com/google/common/truth/extensions/proto",
"@wfa_common_jvm//imports/java/com/google/protobuf",
Expand Down
Loading

0 comments on commit c32badb

Please sign in to comment.