From a399f43d7bdf74a15861a5e0f165763934d1ca10 Mon Sep 17 00:00:00 2001 From: Denis Plotnikov Date: Mon, 18 Sep 2023 18:33:54 +0400 Subject: [PATCH 1/6] [TH2-5073] SendRawMessage intial implementation --- build.gradle | 2 +- .../java/com/exactpro/th2/act/ActHandler.java | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 05a20df..ee1cc07 100644 --- a/build.gradle +++ b/build.gradle @@ -94,7 +94,7 @@ dependencies { implementation 'com.exactpro.th2:common:5.4.0-dev' implementation 'com.exactpro.th2:common-utils:2.2.0-dev' - implementation 'com.exactpro.th2:grpc-act-template:4.1.0' + implementation 'com.exactpro.th2:grpc-act-template:4.1.0-TH2-5073+' implementation 'com.exactpro.th2:grpc-check1:4.2.0-dev' implementation "com.fasterxml.jackson.core:jackson-core" diff --git a/src/main/java/com/exactpro/th2/act/ActHandler.java b/src/main/java/com/exactpro/th2/act/ActHandler.java index c2eb165..0d33a70 100644 --- a/src/main/java/com/exactpro/th2/act/ActHandler.java +++ b/src/main/java/com/exactpro/th2/act/ActHandler.java @@ -20,6 +20,8 @@ import com.exactpro.th2.act.grpc.PlaceMessageRequestOrBuilder; import com.exactpro.th2.act.grpc.PlaceMessageResponse; import com.exactpro.th2.act.grpc.SendMessageResponse; +import com.exactpro.th2.act.grpc.SendRawMessageRequest; +import com.exactpro.th2.act.grpc.SendRawMessageResponse; import com.exactpro.th2.act.impl.MessageResponseMonitor; import com.exactpro.th2.act.rules.FieldCheckRule; import com.exactpro.th2.check1.grpc.Check1Service; @@ -27,6 +29,7 @@ import com.exactpro.th2.check1.grpc.CheckpointResponse; import com.exactpro.th2.common.event.Event; import com.exactpro.th2.common.event.Event.Status; +import com.exactpro.th2.common.event.EventUtils; import com.exactpro.th2.common.event.IBodyData; import com.exactpro.th2.common.event.bean.TreeTable; import com.exactpro.th2.common.event.bean.builder.CollectionBuilder; @@ -42,6 +45,8 @@ import com.exactpro.th2.common.grpc.MessageID; import com.exactpro.th2.common.grpc.MessageMetadata; import com.exactpro.th2.common.grpc.MessageOrBuilder; +import com.exactpro.th2.common.grpc.RawMessageMetadata; +import com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.RawMessage; import com.exactpro.th2.common.grpc.RequestStatus; import com.exactpro.th2.common.grpc.Value; import com.exactpro.th2.common.schema.message.MessageRouter; @@ -57,6 +62,7 @@ import io.grpc.Context; import io.grpc.Deadline; import io.grpc.stub.StreamObserver; +import java.nio.charset.StandardCharsets; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,6 +84,7 @@ import static com.exactpro.th2.common.grpc.RequestStatus.Status.SUCCESS; import static com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.Direction.INCOMING; import static com.exactpro.th2.common.utils.message.MessageHolderUtilsKt.getTreeTable; +import static com.exactpro.th2.common.utils.message.MessageUtilsKt.toTransport; import static com.exactpro.th2.common.utils.message.transport.MessageUtilsKt.toBatch; import static com.exactpro.th2.common.utils.message.transport.MessageUtilsKt.toGroup; import static com.exactpro.th2.common.utils.message.transport.MessageUtilsKt.toTreeTable; @@ -169,6 +176,47 @@ public void placeOrderCancelReplaceRequest(PlaceMessageRequest request, StreamOb } } + @Override + public void sendRawMessage(SendRawMessageRequest request, StreamObserver responseObserver) { + long startPlaceMessage = System.currentTimeMillis(); + try { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Sending message request: " + shortDebugString(request)); + } + + String actName = "sendRawMessage"; + // FIXME store parent with fail in case of children fail + EventID parentId = createAndStoreParentEvent(request, actName, PASSED); + + Checkpoint checkpoint = registerCheckPoint(parentId); + + if (Context.current().isCancelled()) { + LOGGER.warn("'{}' request cancelled by client", actName); + sendRawMessageErrorResponse(responseObserver, "Request has been cancelled by client"); + } + + try { + sendRawMessage(request.getRaw(), parentId); + } catch (Exception ex) { + createAndStoreErrorEvent("sendMessage", ex.getMessage(), Instant.now(), parentId); + throw ex; + } + + SendRawMessageResponse response = SendRawMessageResponse.newBuilder() + .setStatus(RequestStatus.newBuilder().setStatus(SUCCESS).build()) + .setCheckpointId(checkpoint) + .build(); + responseObserver.onNext(response); + responseObserver.onCompleted(); + + } catch (RuntimeException | IOException e) { + LOGGER.error("Failed to send a message. Message = {}", request.getRaw(), e); + sendRawMessageErrorResponse(responseObserver, "Send message failed. Error: " + e.getMessage()); + } finally { + LOGGER.debug("Sending the message has been finished in {}", System.currentTimeMillis() - startPlaceMessage); + } + } + @Override public void sendMessage(PlaceMessageRequest request, StreamObserver responseObserver) { long startPlaceMessage = System.currentTimeMillis(); @@ -390,6 +438,26 @@ private EventID createAndStoreParentEvent(PlaceMessageRequestOrBuilder request, } } + private EventID createAndStoreParentEvent(SendRawMessageRequest request, String actName, Status status) throws IOException { + long startTime = System.currentTimeMillis(); + + Event event = start() + .name(actName + ' ' + request.getRaw().getMetadata().getId().getConnectionId().getSessionAlias()) + .type(actName) + .status(status) + .endTimestamp(); // FIXME set properly as is in the last child + + com.exactpro.th2.common.grpc.Event protoEvent = event.toProto(request.getParentEventId()); + //FIXME process response + try { + eventBatchMessageRouter.send(EventBatch.newBuilder().addEvents(event.toProto(request.getParentEventId())).build(), "publish", "event"); + LOGGER.debug("createAndStoreParentEvent for {} in {} ms", actName, System.currentTimeMillis() - startTime); + return protoEvent.getId(); + } catch (IOException e) { + throw new RuntimeException("Can not send event = " + protoEvent.getId().getId(), e); + } + } + private void createAndStoreNoResponseEvent( String actName, NoResponseBodySupplier noResponseBodySupplier, Instant start, @@ -507,6 +575,32 @@ private void sendMessage(Message message, EventID parentEventId) throws IOExcept } } + private void sendRawMessage(com.exactpro.th2.common.grpc.RawMessage rawMessage, EventID parentEventId) throws IOException { + try { + LOGGER.debug("Sending the message started"); + RawMessageMetadata metadata = rawMessage.getMetadata(); + RawMessage transportRawMessage = RawMessage.builder() + .setId(toTransport(metadata.getId())) + .setProtocol(metadata.getProtocol()) + .setMetadata(metadata.getPropertiesMap()) + .setEventId(EventUtilsKt.toTransport(parentEventId)) + .setBody(rawMessage.getBody().toByteArray()) + .build(); + //May be use in future for filtering + //request.getConnectionId().getSessionAlias(); + MessageID messageID = metadata.getId(); + messageRouter.send(toBatch(toGroup(transportRawMessage), messageID.getBookName(), messageID.getConnectionId().getSessionGroup()), "send_raw"); + //TODO remove after solving issue TH2-217 + //TODO process response + EventBatch eventBatch = EventBatch.newBuilder() + .addEvents(createSendRawMessageEvent(transportRawMessage, parentEventId)) + .build(); + eventBatchMessageRouter.send(eventBatch, "publish", "event"); + } finally { + LOGGER.debug("Sending the message ended"); + } + } + private com.exactpro.th2.common.grpc.Event createSendMessageEvent(ParsedMessage message, EventID parentEventId) throws IOException { Event event = start() .name("Send '" + message.getType() + "' message to connectivity"); @@ -517,6 +611,17 @@ private com.exactpro.th2.common.grpc.Event createSendMessageEvent(ParsedMessage return event.toProto(parentEventId); } + private com.exactpro.th2.common.grpc.Event createSendRawMessageEvent(RawMessage message, EventID parentEventId) throws IOException { + String bodyString = message.getBody().toString(StandardCharsets.UTF_8); + Event event = start() + .name("Send '" + bodyString + "' message to connectivity"); + com.exactpro.th2.common.event.bean.Message messageBean = EventUtils.createMessageBean(bodyString); + event.status(Status.PASSED); + event.bodyData(messageBean); + event.type("Outgoing message"); + return event.toProto(parentEventId); + } + private void createAndStoreErrorEvent(String actName, String message, Instant start, EventID parentEventId) throws IOException { Event errorEvent = Event.from(start) .endTimestamp() @@ -593,6 +698,20 @@ private void sendMessageErrorResponse( LOGGER.debug("Error response : {}", message); } + private void sendRawMessageErrorResponse( + StreamObserver responseObserver, + String message + ) { + responseObserver.onNext(SendRawMessageResponse.newBuilder() + .setStatus(RequestStatus.newBuilder() + .setStatus(ERROR) + .setMessage(message) + .build()) + .build()); + responseObserver.onCompleted(); + LOGGER.debug("Error response : {}", message); + } + private Checkpoint registerCheckPoint(EventID parentEventId) { LOGGER.debug("Registering the checkpoint started"); CheckpointResponse response = check1Service.createCheckpoint(CheckpointRequest.newBuilder() From 41a5dd888f830faffc75c9447d21797f330d9062 Mon Sep 17 00:00:00 2001 From: Denis Plotnikov Date: Tue, 19 Sep 2023 14:30:26 +0400 Subject: [PATCH 2/6] [TH2-5073] Update README.md --- README.md | 5 ++++- gradle.properties | 2 +- src/main/java/com/exactpro/th2/act/ActHandler.java | 8 +++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 680bfc9..48cf2ef 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# th2 act template (5.0.0) +# th2 act template (5.1.0) ## Overview @@ -71,6 +71,9 @@ the `protobuf-description-base64` label. Such descriptors can be used to interac ## Release Notes +### 5.1.0 ++ Added RPC to send raw messages. + ### 5.0.0 + Migrated to th2 transport protocol diff --git a/gradle.properties b/gradle.properties index 9287b7a..e802869 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -release_version=5.0.0 +release_version=5.1.0 nexus_url= nexus_user= nexus_password= diff --git a/src/main/java/com/exactpro/th2/act/ActHandler.java b/src/main/java/com/exactpro/th2/act/ActHandler.java index 0d33a70..2e374dc 100644 --- a/src/main/java/com/exactpro/th2/act/ActHandler.java +++ b/src/main/java/com/exactpro/th2/act/ActHandler.java @@ -97,6 +97,7 @@ public class ActHandler extends ActImplBase { private static final int DEFAULT_RESPONSE_TIMEOUT = 10_000; + private static final String SEND_RAW_QUEUE_ATTRIBUTE = "send_raw"; private static final Logger LOGGER = LoggerFactory.getLogger(ActHandler.class); private final Check1Service check1Service; @@ -586,12 +587,9 @@ private void sendRawMessage(com.exactpro.th2.common.grpc.RawMessage rawMessage, .setEventId(EventUtilsKt.toTransport(parentEventId)) .setBody(rawMessage.getBody().toByteArray()) .build(); - //May be use in future for filtering - //request.getConnectionId().getSessionAlias(); MessageID messageID = metadata.getId(); - messageRouter.send(toBatch(toGroup(transportRawMessage), messageID.getBookName(), messageID.getConnectionId().getSessionGroup()), "send_raw"); - //TODO remove after solving issue TH2-217 - //TODO process response + messageRouter.send(toBatch(toGroup(transportRawMessage), messageID.getBookName(), messageID.getConnectionId().getSessionGroup()), SEND_RAW_QUEUE_ATTRIBUTE); + EventBatch eventBatch = EventBatch.newBuilder() .addEvents(createSendRawMessageEvent(transportRawMessage, parentEventId)) .build(); From a3dfce907cfc04be3d13128bd8928f4bb5091a7a Mon Sep 17 00:00:00 2001 From: Denis Plotnikov Date: Tue, 19 Sep 2023 15:14:43 +0400 Subject: [PATCH 3/6] [TH2-5073] Handle all exception types that can be trown during execution --- src/main/java/com/exactpro/th2/act/ActHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/exactpro/th2/act/ActHandler.java b/src/main/java/com/exactpro/th2/act/ActHandler.java index 2e374dc..af02704 100644 --- a/src/main/java/com/exactpro/th2/act/ActHandler.java +++ b/src/main/java/com/exactpro/th2/act/ActHandler.java @@ -194,6 +194,7 @@ public void sendRawMessage(SendRawMessageRequest request, StreamObserver Date: Tue, 19 Sep 2023 16:13:19 +0400 Subject: [PATCH 4/6] [TH2-5073] Update README.md, hex dump in event for cases when raw message is sent --- README.md | 30 ++++++++++++++++++- build.gradle | 4 ++- .../java/com/exactpro/th2/act/ActHandler.java | 7 ++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 48cf2ef..be73472 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,14 @@ Most of them consists of the next steps: 3. Sends the passed business message to Codec via mq pin 4. Waits the specific business message from Codec during specified timeout 5. Returns responded business message with checkpoint - ![picture](scheme.png) +### Sending raw messages +It is also possible to send raw messages ( containing metadata and having payload in original format ) directly to conn components without waiting while message will be encoded by codec and then sent to conn component. +For that you need to: +1. Send rpc request using `SendRawMessageRequest` and `sendRawMessage` rpc that is defined in [th2-grpc-act-template](https://github.com/th2-net/th2-grpc-act-template/blob/master/src/main/proto/th2_grpc_act_template/act_template.proto "act_template.proto") +2. Transport group with `RawMessage` will be published to pins with `send_raw` attribute. + ## Custom resources for infra-mgr ```yaml @@ -60,6 +65,28 @@ spec: - field-name: session_alias expected-value: conn2_session_alias operation: EQUAL + - name: to_conn1_transport + connection-type: mq + attributes: + - publish + - transport-group + - send_raw + filters: + - metadata: + - field-name: session_alias + expected-value: conn1_session_alias + operation: EQUAL + - name: to_conn2_transport + connection-type: mq + attributes: + - publish + - transport-group + - send_raw + filters: + - metadata: + - field-name: session_alias + expected-value: conn1_session_alias + operation: EQUAL ``` ## Descriptor gradle plugin @@ -73,6 +100,7 @@ the `protobuf-description-base64` label. Such descriptors can be used to interac ### 5.1.0 + Added RPC to send raw messages. ++ Updated grpc-act-template to `4.2.0` ### 5.0.0 diff --git a/build.gradle b/build.gradle index ee1cc07..e6159b7 100644 --- a/build.gradle +++ b/build.gradle @@ -94,7 +94,7 @@ dependencies { implementation 'com.exactpro.th2:common:5.4.0-dev' implementation 'com.exactpro.th2:common-utils:2.2.0-dev' - implementation 'com.exactpro.th2:grpc-act-template:4.1.0-TH2-5073+' + implementation 'com.exactpro.th2:grpc-act-template:4.2.0-dev' implementation 'com.exactpro.th2:grpc-check1:4.2.0-dev' implementation "com.fasterxml.jackson.core:jackson-core" @@ -103,6 +103,8 @@ dependencies { implementation "org.slf4j:slf4j-api" implementation "org.apache.commons:commons-lang3" + implementation "commons-codec:commons-codec:1.16.0" + testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' testImplementation 'io.strikt:strikt-core:0.34.1' diff --git a/src/main/java/com/exactpro/th2/act/ActHandler.java b/src/main/java/com/exactpro/th2/act/ActHandler.java index af02704..27f9ee5 100644 --- a/src/main/java/com/exactpro/th2/act/ActHandler.java +++ b/src/main/java/com/exactpro/th2/act/ActHandler.java @@ -62,7 +62,7 @@ import io.grpc.Context; import io.grpc.Deadline; import io.grpc.stub.StreamObserver; -import java.nio.charset.StandardCharsets; +import io.netty.buffer.ByteBufUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -611,10 +611,9 @@ private com.exactpro.th2.common.grpc.Event createSendMessageEvent(ParsedMessage } private com.exactpro.th2.common.grpc.Event createSendRawMessageEvent(RawMessage message, EventID parentEventId) throws IOException { - String bodyString = message.getBody().toString(StandardCharsets.UTF_8); Event event = start() - .name("Send '" + bodyString + "' message to connectivity"); - com.exactpro.th2.common.event.bean.Message messageBean = EventUtils.createMessageBean(bodyString); + .name("Sent raw message to connectivity"); + com.exactpro.th2.common.event.bean.Message messageBean = EventUtils.createMessageBean(ByteBufUtil.hexDump(message.getBody())); event.status(Status.PASSED); event.bodyData(messageBean); event.type("Outgoing message"); From eede6b57da90cd4fabddb94425d78481decd8fa0 Mon Sep 17 00:00:00 2001 From: Denis Plotnikov Date: Tue, 19 Sep 2023 16:15:06 +0400 Subject: [PATCH 5/6] [TH2-5073] Remove reduntant dependency declaration --- build.gradle | 2 -- 1 file changed, 2 deletions(-) diff --git a/build.gradle b/build.gradle index e6159b7..bbd46d9 100644 --- a/build.gradle +++ b/build.gradle @@ -103,8 +103,6 @@ dependencies { implementation "org.slf4j:slf4j-api" implementation "org.apache.commons:commons-lang3" - implementation "commons-codec:commons-codec:1.16.0" - testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' testImplementation 'io.strikt:strikt-core:0.34.1' From 79c8bc7add162a86ef47438ede66e2af3c41f40f Mon Sep 17 00:00:00 2001 From: Denis Plotnikov Date: Tue, 19 Sep 2023 16:21:47 +0400 Subject: [PATCH 6/6] [TH2-5073] Use release grpc-act-template version --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index bbd46d9..85b7ceb 100644 --- a/build.gradle +++ b/build.gradle @@ -94,7 +94,7 @@ dependencies { implementation 'com.exactpro.th2:common:5.4.0-dev' implementation 'com.exactpro.th2:common-utils:2.2.0-dev' - implementation 'com.exactpro.th2:grpc-act-template:4.2.0-dev' + implementation 'com.exactpro.th2:grpc-act-template:4.2.0' implementation 'com.exactpro.th2:grpc-check1:4.2.0-dev' implementation "com.fasterxml.jackson.core:jackson-core"