[ 
https://issues.apache.org/jira/browse/SCB-138?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16308978#comment-16308978
 ] 

ASF GitHub Bot commented on SCB-138:
------------------------------------

seanyinx commented on a change in pull request #98: [WIP]SCB-138 use grpc 
bidirectional streaming to connect alpha and omega
URL: 
https://github.com/apache/incubator-servicecomb-saga/pull/98#discussion_r159349423
 
 

 ##########
 File path: 
alpha/alpha-server/src/main/java/io/servicecomb/saga/alpha/server/GrpcTxEventStreamObserver.java
 ##########
 @@ -0,0 +1,90 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.servicecomb.saga.alpha.server;
+
+import static io.servicecomb.saga.alpha.core.EventType.TxStartedEvent;
+
+import java.lang.invoke.MethodHandles;
+import java.util.Date;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.grpc.stub.StreamObserver;
+import io.servicecomb.saga.alpha.core.OmegaCallback;
+import io.servicecomb.saga.alpha.core.TxConsistentService;
+import io.servicecomb.saga.alpha.core.TxEvent;
+import io.servicecomb.saga.pack.contract.grpc.GrpcCompensateCommand;
+import io.servicecomb.saga.pack.contract.grpc.GrpcTxEvent;
+
+class GrpcTxEventStreamObserver implements StreamObserver<GrpcTxEvent> {
+
+  private static Logger LOG = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  private final Map<String, Map<String, OmegaCallback>> omegaCallbacks;
+
+  private final TxConsistentService txConsistentService;
+
+  private final StreamObserver<GrpcCompensateCommand> responseObserver;
+
+  GrpcTxEventStreamObserver(Map<String, Map<String, OmegaCallback>> 
omegaCallbacks,
+      TxConsistentService txConsistentService, 
StreamObserver<GrpcCompensateCommand> responseObserver) {
+    this.omegaCallbacks = omegaCallbacks;
+    this.txConsistentService = txConsistentService;
+    this.responseObserver = responseObserver;
+  }
+
+  @Override
+  public void onNext(GrpcTxEvent message) {
+    // register a callback on started event
+    if (message.getType().equals(TxStartedEvent.name())) {
+      Map<String, OmegaCallback> callbacks = new ConcurrentHashMap<>();
+      callbacks.put(message.getInstanceId(), new 
GrpcOmegaCallback(responseObserver));
+      omegaCallbacks.put(message.getServiceName(), callbacks);
+    }
+
+    // store received event
+    txConsistentService.handle(new TxEvent(
+        message.getServiceName(),
+        message.getInstanceId(),
+        new Date(message.getTimestamp()),
+        message.getGlobalTxId(),
+        message.getLocalTxId(),
+        message.getParentTxId().isEmpty() ? null : message.getParentTxId(),
+        message.getType(),
+        message.getCompensationMethod(),
+        message.getPayloads().toByteArray()
+    ));
+  }
+
+  @Override
+  public void onError(Throwable t) {
+    LOG.error("failed to process grpc message.", t);
 
 Review comment:
   does onError & onCompleted mean the client is disconnected? if so, we need 
to find a way to remove the instance from the callback map

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [pack] omega's callback about transaction state
> -----------------------------------------------
>
>                 Key: SCB-138
>                 URL: https://issues.apache.org/jira/browse/SCB-138
>             Project: Apache ServiceComb
>          Issue Type: New Feature
>          Components: Saga
>            Reporter: Eric Lee
>            Assignee: Eric Lee
>             Fix For: java-chassis-1.0.0-m1
>
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> as a dev, i want to register callback function in the server side, so the 
> server can send messages back to the client directly instead of establishing 
> a new connection to the exact client with its corresponding uri.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to