You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@servicecomb.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/01/09 14:45:00 UTC

[jira] [Commented] (SCB-168) [pack] resend events on connection loss & load balance

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

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

WillemJiang commented on a change in pull request #107: [SCB-168] load balance
URL: https://github.com/apache/incubator-servicecomb-saga/pull/107#discussion_r160424294
 
 

 ##########
 File path: omega/omega-connector/omega-connector-grpc/src/main/java/org/apache/servicecomb/saga/omega/connector/grpc/LoadBalancedClusterMessageSender.java
 ##########
 @@ -0,0 +1,140 @@
+/*
+ * 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 org.apache.servicecomb.saga.omega.connector.grpc;
+
+import static java.util.Collections.emptyList;
+
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.function.Consumer;
+
+import org.apache.servicecomb.saga.omega.context.ServiceConfig;
+import org.apache.servicecomb.saga.omega.transaction.MessageDeserializer;
+import org.apache.servicecomb.saga.omega.transaction.MessageHandler;
+import org.apache.servicecomb.saga.omega.transaction.MessageSender;
+import org.apache.servicecomb.saga.omega.transaction.MessageSerializer;
+import org.apache.servicecomb.saga.omega.transaction.TxEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+
+public class LoadBalancedClusterMessageSender implements MessageSender {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  private final Map<MessageSender, Long> senders = new HashMap<>();
+  private final Collection<ManagedChannel> channels;
+
+  public LoadBalancedClusterMessageSender(String[] addresses,
+      MessageSerializer serializer,
+      MessageDeserializer deserializer,
+      ServiceConfig serviceConfig,
+      MessageHandler handler) {
+
+    if (addresses.length == 0) {
+      throw new IllegalArgumentException("No reachable cluster address provided");
+    }
+
+    channels = new ArrayList<>(addresses.length);
+    for (String address : addresses) {
+      ManagedChannel channel = ManagedChannelBuilder.forTarget(address)
+          .usePlaintext(true)
+          .build();
+
+      channels.add(channel);
+      senders.put(
+          new GrpcClientMessageSender(
+              address,
+              channel,
+              serializer,
+              deserializer,
+              serviceConfig,
+              handler),
+          0L);
+    }
+  }
+
+  LoadBalancedClusterMessageSender(MessageSender... messageSenders) {
+    for (MessageSender sender : messageSenders) {
+      senders.put(sender, 0L);
+    }
+    channels = emptyList();
+  }
+
+  @Override
+  public void onConnected() {
+    senders.keySet().forEach(sender -> {
+      try {
+        sender.onConnected();
+      } catch (Exception e) {
+        log.error("Failed connecting to alpha at {}", sender.target(), e);
+      }
+    });
+  }
+
+  @Override
+  public void onDisconnected() {
+    senders.keySet().forEach(sender -> {
+      try {
+        sender.onDisconnected();
+      } catch (Exception e) {
+        log.error("Failed disconnecting from alpha at {}", sender.target(), e);
+      }
+    });
+
+  }
+
+  @Override
+  public void close() {
+    channels.forEach(ManagedChannel::shutdownNow);
+  }
+
+  @Override
+  public void send(TxEvent event) {
+    boolean success = false;
+    do {
+      try {
+        withFastestSender(messageSender -> {
 
 Review comment:
   If the sender always failed to send  the message,  the CPU usage could be very high.

----------------------------------------------------------------
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:
users@infra.apache.org


> [pack] resend events on connection loss & load balance
> ------------------------------------------------------
>
>                 Key: SCB-168
>                 URL: https://issues.apache.org/jira/browse/SCB-168
>             Project: Apache ServiceComb
>          Issue Type: Improvement
>          Components: Saga
>            Reporter: Yin Xiang
>            Assignee: Yin Xiang
>             Fix For: saga-0.1.0
>
>
> as a dev, i want my service's transaction managed, once its network is up again, so that it's not in an unknown state



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