From cb708adaa8b0d0220d42683e2fa8c1106eb53603 Mon Sep 17 00:00:00 2001 From: Artur Ciocanu Date: Wed, 26 Jun 2024 16:24:52 +0300 Subject: [PATCH 1/2] Add missing data to Dapr Metadata and its components Signed-off-by: Artur Ciocanu --- .../java/io/dapr/client/DaprClientImpl.java | 26 ++++++-- .../io/dapr/client/domain/ActorMetadata.java | 41 ++++++++++++ ...AppConnectionPropertiesHealthMetadata.java | 3 + .../AppConnectionPropertiesMetadata.java | 3 + .../dapr/client/domain/ComponentMetadata.java | 20 ++++-- .../io/dapr/client/domain/DaprMetadata.java | 11 +++- .../client/domain/HttpEndpointMetadata.java | 3 + .../io/dapr/client/domain/RuleMetadata.java | 17 ++++- .../client/domain/SubscriptionMetadata.java | 34 +++++----- .../io/dapr/client/DaprClientGrpcTest.java | 65 ++++++++++++++++--- 10 files changed, 180 insertions(+), 43 deletions(-) create mode 100644 sdk/src/main/java/io/dapr/client/domain/ActorMetadata.java diff --git a/sdk/src/main/java/io/dapr/client/DaprClientImpl.java b/sdk/src/main/java/io/dapr/client/DaprClientImpl.java index 895d765db..955b3493a 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientImpl.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientImpl.java @@ -16,6 +16,7 @@ import com.google.common.base.Strings; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; +import io.dapr.client.domain.ActorMetadata; import io.dapr.client.domain.AppConnectionPropertiesHealthMetadata; import io.dapr.client.domain.AppConnectionPropertiesMetadata; import io.dapr.client.domain.BulkPublishEntry; @@ -65,6 +66,8 @@ import io.dapr.v1.CommonProtos; import io.dapr.v1.DaprGrpc; import io.dapr.v1.DaprProtos; +import io.dapr.v1.DaprProtos.ActiveActorsCount; +import io.dapr.v1.DaprProtos.ActorRuntime; import io.dapr.v1.DaprProtos.AppConnectionHealthProperties; import io.dapr.v1.DaprProtos.AppConnectionProperties; import io.dapr.v1.DaprProtos.MetadataHTTPEndpoint; @@ -1266,14 +1269,27 @@ private DaprMetadata buildDaprMetadata(DaprProtos.GetMetadataResponse response) String id = response.getId(); String runtimeVersion = response.getRuntimeVersion(); List enabledFeatures = response.getEnabledFeaturesList(); + List actors = getActors(response); Map attributes = response.getExtendedMetadataMap(); List components = getComponents(response); List httpEndpoints = getHttpEndpoints(response); List subscriptions = getSubscriptions(response); AppConnectionPropertiesMetadata appConnectionProperties = getAppConnectionProperties(response); - return new DaprMetadata(id, runtimeVersion, enabledFeatures, attributes, components, httpEndpoints, subscriptions, - appConnectionProperties); + return new DaprMetadata(id, runtimeVersion, enabledFeatures, actors, attributes, components, httpEndpoints, + subscriptions, appConnectionProperties); + } + + private List getActors(DaprProtos.GetMetadataResponse response) { + ActorRuntime actorRuntime = response.getActorRuntime(); + List activeActorsList = actorRuntime.getActiveActorsList(); + + List actors = new ArrayList<>(); + for (ActiveActorsCount aac : activeActorsList) { + actors.add(new ActorMetadata(aac.getType(), aac.getCount())); + } + + return actors; } private List getComponents(DaprProtos.GetMetadataResponse response) { @@ -1281,7 +1297,7 @@ private List getComponents(DaprProtos.GetMetadataResponse res List components = new ArrayList<>(); for (RegisteredComponents rc : registeredComponentsList) { - components.add(new ComponentMetadata(rc.getName(), rc.getType(), rc.getVersion())); + components.add(new ComponentMetadata(rc.getName(), rc.getType(), rc.getVersion(), rc.getCapabilitiesList())); } return components; @@ -1295,9 +1311,9 @@ private List getSubscriptions(DaprProtos.GetMetadataRespon List rulesList = s.getRules().getRulesList(); List rules = new ArrayList<>(); for (PubsubSubscriptionRule r : rulesList) { - rules.add(new RuleMetadata(r.getPath())); + rules.add(new RuleMetadata(r.getMatch(), r.getPath())); } - subscriptions.add(new SubscriptionMetadata(s.getTopic(), s.getPubsubName(), s.getDeadLetterTopic(), rules)); + subscriptions.add(new SubscriptionMetadata(s.getPubsubName(), s.getTopic(), rules, s.getDeadLetterTopic())); } return subscriptions; diff --git a/sdk/src/main/java/io/dapr/client/domain/ActorMetadata.java b/sdk/src/main/java/io/dapr/client/domain/ActorMetadata.java new file mode 100644 index 000000000..a0420d34c --- /dev/null +++ b/sdk/src/main/java/io/dapr/client/domain/ActorMetadata.java @@ -0,0 +1,41 @@ +/* + * Copyright 2024 The Dapr 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 io.dapr.client.domain; + +/** + * ActorMetadata describes a registered Dapr Actor. + */ +public final class ActorMetadata { + private final String type; + private final int count; + + /** + * Constructor for a ActorMetadata. + * + * @param type of the actor + * @param count number of actors of a particular type + */ + public ActorMetadata(String type, int count) { + this.type = type; + this.count = count; + } + + public String getType() { + return type; + } + + public int getCount() { + return count; + } +} diff --git a/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesHealthMetadata.java b/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesHealthMetadata.java index c014be60f..b8aa1f65f 100644 --- a/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesHealthMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesHealthMetadata.java @@ -13,6 +13,9 @@ package io.dapr.client.domain; +/** + * AppConnectionPropertiesHealthMetadata describes the application health properties. + */ public final class AppConnectionPropertiesHealthMetadata { private final String healthCheckPath; diff --git a/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesMetadata.java b/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesMetadata.java index 1e498c2dd..0b256e47a 100644 --- a/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/AppConnectionPropertiesMetadata.java @@ -13,6 +13,9 @@ package io.dapr.client.domain; +/** + * AppConnectionPropertiesMetadata describes the application connection properties. + */ public final class AppConnectionPropertiesMetadata { private final int port; diff --git a/sdk/src/main/java/io/dapr/client/domain/ComponentMetadata.java b/sdk/src/main/java/io/dapr/client/domain/ComponentMetadata.java index 8e7f1a8cc..b5813944e 100644 --- a/sdk/src/main/java/io/dapr/client/domain/ComponentMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/ComponentMetadata.java @@ -13,16 +13,18 @@ package io.dapr.client.domain; -import java.util.Objects; +import java.util.Collections; +import java.util.List; /** * ComponentMetadata describes a Dapr Component. */ public final class ComponentMetadata { - private String name; - private String type; - private String version; + private final String name; + private final String type; + private final String version; + private final List capabilities; /** * Constructor for a ComponentMetadata. @@ -30,11 +32,13 @@ public final class ComponentMetadata { * @param name of the component * @param type component type * @param version version of the component + * @param capabilities capabilities of the component */ - public ComponentMetadata(String name, String type, String version) { + public ComponentMetadata(String name, String type, String version, List capabilities) { this.name = name; this.type = type; this.version = version; + this.capabilities = capabilities == null ? Collections.emptyList() : Collections.unmodifiableList(capabilities); } public String getName() { @@ -48,5 +52,9 @@ public String getType() { public String getVersion() { return version; } - + + public List getCapabilities() { + return capabilities; + } + } diff --git a/sdk/src/main/java/io/dapr/client/domain/DaprMetadata.java b/sdk/src/main/java/io/dapr/client/domain/DaprMetadata.java index bba77578d..037365b73 100644 --- a/sdk/src/main/java/io/dapr/client/domain/DaprMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/DaprMetadata.java @@ -25,6 +25,7 @@ public final class DaprMetadata { private final String id; private final String runtimeVersion; private final List enabledFeatures; + private final List actors; private final Map attributes; private final List components; private final List httpEndpoints; @@ -37,19 +38,21 @@ public final class DaprMetadata { * @param id of the application * @param runtimeVersion Dapr version * @param enabledFeatures list of enabled features + * @param actors list of registered features * @param attributes map of extended attributes * @param components list of registered components * @param httpEndpoints list of registered http endpoints * @param subscriptions list of registered subscription * @param appConnectionProperties connection properties of the application */ - public DaprMetadata(String id, String runtimeVersion, List enabledFeatures, Map attributes, - List components, List httpEndpoints, + public DaprMetadata(String id, String runtimeVersion, List enabledFeatures, List actors, + Map attributes, List components, List httpEndpoints, List subscriptions, AppConnectionPropertiesMetadata appConnectionProperties) { this.id = id; this.runtimeVersion = runtimeVersion; this.enabledFeatures = enabledFeatures == null ? Collections.emptyList() : Collections.unmodifiableList(enabledFeatures); + this.actors = actors == null ? Collections.emptyList() : Collections.unmodifiableList(actors); this.attributes = attributes == null ? Collections.emptyMap() : Collections.unmodifiableMap(attributes); this.components = components == null ? Collections.emptyList() : Collections.unmodifiableList(components); this.httpEndpoints = httpEndpoints == null ? Collections.emptyList() : Collections.unmodifiableList(httpEndpoints); @@ -69,6 +72,10 @@ public List getEnabledFeatures() { return enabledFeatures; } + public List getActors() { + return actors; + } + public Map getAttributes() { return attributes; } diff --git a/sdk/src/main/java/io/dapr/client/domain/HttpEndpointMetadata.java b/sdk/src/main/java/io/dapr/client/domain/HttpEndpointMetadata.java index 43327925e..c0327859b 100644 --- a/sdk/src/main/java/io/dapr/client/domain/HttpEndpointMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/HttpEndpointMetadata.java @@ -13,6 +13,9 @@ package io.dapr.client.domain; +/** + * HttpEndpointMetadata describes a registered Dapr HTTP endpoint. + */ public final class HttpEndpointMetadata { private final String name; diff --git a/sdk/src/main/java/io/dapr/client/domain/RuleMetadata.java b/sdk/src/main/java/io/dapr/client/domain/RuleMetadata.java index 61a033144..f3499e2fd 100644 --- a/sdk/src/main/java/io/dapr/client/domain/RuleMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/RuleMetadata.java @@ -17,12 +17,25 @@ * RuleMetadata describes the Subscription Rule's Metadata. */ public final class RuleMetadata { - private String path; - public RuleMetadata(String path) { + private final String match; + private final String path; + + /** + * Constructor for a RuleMetadata. + * + * @param match CEL expression to match the message + * @param path path to route the message + */ + public RuleMetadata(String match, String path) { + this.match = match; this.path = path; } + public String getMatch() { + return match; + } + public String getPath() { return path; } diff --git a/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java b/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java index 36509c216..fd88ad07d 100644 --- a/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java @@ -15,48 +15,46 @@ import java.util.Collections; import java.util.List; -import java.util.Objects; /** * SubscriptionMetadata describes the Subscription Metadata. */ public final class SubscriptionMetadata { - private String topic; - private String pubsubname; - private String deadLetterTopic; - private List rules; + + private final String pubsubname; + private final String topic; + private final List rules; + private final String deadLetterTopic; /** * Constructor for a SubscriptionMetadata. * - * @param topic of the pubsub component * @param pubsubname component name - * @param deadLetterTopic dead letter topic + * @param topic of the pubsub component * @param rules subscription path rules + * @param deadLetterTopic dead letter topic */ - public SubscriptionMetadata(String topic, String pubsubname, String deadLetterTopic, List rules) { - this.topic = topic; + public SubscriptionMetadata(String pubsubname, String topic, List rules, String deadLetterTopic) { this.pubsubname = pubsubname; - this.deadLetterTopic = deadLetterTopic; + this.topic = topic; this.rules = rules == null ? Collections.emptyList() : Collections.unmodifiableList(rules); - } - - public String getTopic() { - return topic; + this.deadLetterTopic = deadLetterTopic; } public String getPubsubname() { return pubsubname; } - - public String getDeadLetterTopic() { - return deadLetterTopic; + public String getTopic() { + return topic; } - public List getRules() { return rules; } + public String getDeadLetterTopic() { + return deadLetterTopic; + } + } diff --git a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java index 8e1459094..1081220a9 100644 --- a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java @@ -16,13 +16,32 @@ import com.google.protobuf.Any; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; -import io.dapr.client.domain.*; +import io.dapr.client.domain.AppConnectionPropertiesHealthMetadata; +import io.dapr.client.domain.AppConnectionPropertiesMetadata; +import io.dapr.client.domain.ComponentMetadata; +import io.dapr.client.domain.ConfigurationItem; +import io.dapr.client.domain.DaprMetadata; +import io.dapr.client.domain.DeleteStateRequest; +import io.dapr.client.domain.ExecuteStateTransactionRequest; +import io.dapr.client.domain.GetBulkStateRequest; +import io.dapr.client.domain.GetStateRequest; +import io.dapr.client.domain.PublishEventRequest; +import io.dapr.client.domain.RuleMetadata; +import io.dapr.client.domain.State; +import io.dapr.client.domain.StateOptions; +import io.dapr.client.domain.SubscribeConfigurationResponse; +import io.dapr.client.domain.SubscriptionMetadata; +import io.dapr.client.domain.TransactionalStateOperation; +import io.dapr.client.domain.UnsubscribeConfigurationRequest; +import io.dapr.client.domain.UnsubscribeConfigurationResponse; import io.dapr.serializer.DaprObjectSerializer; import io.dapr.serializer.DefaultObjectSerializer; import io.dapr.utils.TypeRef; import io.dapr.v1.CommonProtos; import io.dapr.v1.DaprGrpc; import io.dapr.v1.DaprProtos; +import io.dapr.v1.DaprProtos.ActiveActorsCount; +import io.dapr.v1.DaprProtos.ActorRuntime; import io.dapr.v1.DaprProtos.AppConnectionHealthProperties; import io.dapr.v1.DaprProtos.AppConnectionProperties; import io.dapr.v1.DaprProtos.MetadataHTTPEndpoint; @@ -2076,6 +2095,15 @@ public static StatusRuntimeException newStatusRuntimeException(String statusCode @Test public void getMetadataTest() { + ActiveActorsCount activeActorsCount = DaprProtos.ActiveActorsCount.newBuilder() + .setType("actor") + .setCount(1) + .build(); + + ActorRuntime actorRuntime = DaprProtos.ActorRuntime.newBuilder() + .addActiveActors(activeActorsCount) + .build(); + RegisteredComponents registeredComponents = DaprProtos.RegisteredComponents.newBuilder() .setName("statestore") .setType("state.redis") @@ -2112,11 +2140,11 @@ public void getMetadataTest() { .setHealth(healthProperties) .build(); - DaprProtos.GetMetadataResponse responseEnvelope = DaprProtos.GetMetadataResponse.newBuilder() .setId("app") .setRuntimeVersion("1.1x.x") .addAllEnabledFeatures(Collections.emptyList()) + .setActorRuntime(actorRuntime) .putAllExtendedMetadata(Collections.emptyMap()) .addAllRegisteredComponents(Collections.singletonList(registeredComponents)) .addAllHttpEndpoints(Collections.singletonList(httpEndpoint)) @@ -2141,20 +2169,37 @@ public void getMetadataTest() { assertEquals(0, metadata.getEnabledFeatures().size()); assertEquals(0, metadata.getAttributes().size()); + // Actors + assertEquals(1, metadata.getActors().size()); + assertEquals(activeActorsCount.getType(), metadata.getActors().get(0).getType()); + assertEquals(activeActorsCount.getCount(), metadata.getActors().get(0).getCount()); + // Components assertEquals(1, metadata.getComponents().size()); - assertEquals(registeredComponents.getName(), metadata.getComponents().get(0).getName()); - assertEquals(registeredComponents.getVersion(), metadata.getComponents().get(0).getVersion()); - assertEquals(registeredComponents.getType(), metadata.getComponents().get(0).getType()); + + ComponentMetadata componentMetadata = metadata.getComponents().get(0); + + assertEquals(registeredComponents.getName(), componentMetadata.getName()); + assertEquals(registeredComponents.getVersion(), componentMetadata.getVersion()); + assertEquals(registeredComponents.getType(), componentMetadata.getType()); + assertEquals(registeredComponents.getCapabilitiesList(), componentMetadata.getCapabilities()); // Subscriptions assertEquals(1, metadata.getSubscriptions().size()); - assertEquals(pubsubSubscription.getPubsubName(), metadata.getSubscriptions().get(0).getPubsubname()); - assertEquals(pubsubSubscription.getTopic(), metadata.getSubscriptions().get(0).getTopic()); + + SubscriptionMetadata subscriptionMetadata = metadata.getSubscriptions().get(0); + + assertEquals(pubsubSubscription.getPubsubName(), subscriptionMetadata.getPubsubname()); + assertEquals(pubsubSubscription.getTopic(), subscriptionMetadata.getTopic()); + assertEquals(pubsubSubscription.getDeadLetterTopic(), subscriptionMetadata.getDeadLetterTopic()); // Subscription Rules - assertEquals(1, metadata.getSubscriptions().get(0).getRules().size()); - assertEquals(pubsubSubscription.getRules().getRules(0).getPath(), metadata.getSubscriptions().get(0).getRules().get(0).getPath()); + assertEquals(1, subscriptionMetadata.getRules().size()); + + RuleMetadata ruleMetadata = subscriptionMetadata.getRules().get(0); + + assertEquals(pubsubSubscription.getRules().getRules(0).getMatch(), ruleMetadata.getMatch()); + assertEquals(pubsubSubscription.getRules().getRules(0).getPath(), ruleMetadata.getPath()); // HTTP Endpoints assertEquals(1, metadata.getHttpEndpoints().size()); @@ -2191,4 +2236,4 @@ public void getMetadataExceptionTest() { "UNKNOWN: ", () -> result.block()); } -} \ No newline at end of file +} From de231e51b008ff21267450034b4ed9141a051f33 Mon Sep 17 00:00:00 2001 From: Artur Ciocanu Date: Wed, 26 Jun 2024 16:32:26 +0300 Subject: [PATCH 2/2] Add metadata field to subscription Signed-off-by: Artur Ciocanu --- sdk/src/main/java/io/dapr/client/DaprClientImpl.java | 3 ++- .../io/dapr/client/domain/SubscriptionMetadata.java | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sdk/src/main/java/io/dapr/client/DaprClientImpl.java b/sdk/src/main/java/io/dapr/client/DaprClientImpl.java index 955b3493a..aabc07a60 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientImpl.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientImpl.java @@ -1313,7 +1313,8 @@ private List getSubscriptions(DaprProtos.GetMetadataRespon for (PubsubSubscriptionRule r : rulesList) { rules.add(new RuleMetadata(r.getMatch(), r.getPath())); } - subscriptions.add(new SubscriptionMetadata(s.getPubsubName(), s.getTopic(), rules, s.getDeadLetterTopic())); + subscriptions.add(new SubscriptionMetadata(s.getPubsubName(), s.getTopic(), s.getMetadataMap(), rules, + s.getDeadLetterTopic())); } return subscriptions; diff --git a/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java b/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java index fd88ad07d..76d8dce77 100644 --- a/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java +++ b/sdk/src/main/java/io/dapr/client/domain/SubscriptionMetadata.java @@ -15,6 +15,7 @@ import java.util.Collections; import java.util.List; +import java.util.Map; /** * SubscriptionMetadata describes the Subscription Metadata. @@ -23,6 +24,7 @@ public final class SubscriptionMetadata { private final String pubsubname; private final String topic; + private final Map metadata; private final List rules; private final String deadLetterTopic; @@ -31,12 +33,15 @@ public final class SubscriptionMetadata { * * @param pubsubname component name * @param topic of the pubsub component + * @param metadata of the pubsub component * @param rules subscription path rules * @param deadLetterTopic dead letter topic */ - public SubscriptionMetadata(String pubsubname, String topic, List rules, String deadLetterTopic) { + public SubscriptionMetadata(String pubsubname, String topic, Map metadata, List rules, + String deadLetterTopic) { this.pubsubname = pubsubname; this.topic = topic; + this.metadata = metadata == null ? Collections.emptyMap() : Collections.unmodifiableMap(metadata); this.rules = rules == null ? Collections.emptyList() : Collections.unmodifiableList(rules); this.deadLetterTopic = deadLetterTopic; } @@ -49,6 +54,10 @@ public String getTopic() { return topic; } + public Map getMetadata() { + return metadata; + } + public List getRules() { return rules; }