Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 18 | import com.google.common.base.Throwables; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 20 | import io.atomix.catalyst.concurrent.ThreadContext; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 21 | import io.atomix.catalyst.transport.Address; |
| 22 | import io.atomix.catalyst.transport.Client; |
| 23 | import io.atomix.catalyst.transport.Connection; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 24 | import io.atomix.catalyst.transport.TransportException; |
| 25 | import org.onosproject.cluster.PartitionId; |
| 26 | import org.onosproject.store.cluster.messaging.Endpoint; |
| 27 | import org.onosproject.store.cluster.messaging.MessagingException; |
| 28 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.net.ConnectException; |
| 33 | import java.nio.ByteBuffer; |
| 34 | import java.util.Set; |
| 35 | import java.util.concurrent.CompletableFuture; |
| 36 | |
| 37 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | import static org.onosproject.store.primitives.impl.CopycatTransport.CONNECT; |
| 40 | import static org.onosproject.store.primitives.impl.CopycatTransport.SUCCESS; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 43 | * Copycat transport client implementation. |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 44 | */ |
| 45 | public class CopycatTransportClient implements Client { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 47 | private final PartitionId partitionId; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 48 | private final String serverSubject; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 49 | private final MessagingService messagingService; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 50 | private final Set<CopycatTransportConnection> connections = Sets.newConcurrentHashSet(); |
| 51 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 52 | public CopycatTransportClient(PartitionId partitionId, MessagingService messagingService) { |
| 53 | this.partitionId = checkNotNull(partitionId, "partitionId cannot be null"); |
| 54 | this.serverSubject = String.format("onos-copycat-%s", partitionId); |
| 55 | this.messagingService = checkNotNull(messagingService, "messagingService cannot be null"); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 59 | public CompletableFuture<Connection> connect(Address address) { |
| 60 | CompletableFuture<Connection> future = new CompletableFuture<>(); |
Madan Jampani | b06ccef | 2016-01-25 10:51:16 -0800 | [diff] [blame] | 61 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 62 | Endpoint endpoint = CopycatTransport.toEndpoint(address); |
| 63 | |
| 64 | log.debug("Connecting to {}", address); |
| 65 | |
| 66 | ByteBuffer requestBuffer = ByteBuffer.allocate(1); |
| 67 | requestBuffer.put(CONNECT); |
| 68 | |
| 69 | // Send a connect request to the server to get a unique connection ID. |
| 70 | messagingService.sendAndReceive(endpoint, serverSubject, requestBuffer.array(), context.executor()) |
| 71 | .whenComplete((payload, error) -> { |
| 72 | Throwable wrappedError = error; |
| 73 | if (error != null) { |
| 74 | Throwable rootCause = Throwables.getRootCause(error); |
| 75 | if (MessagingException.class.isAssignableFrom(rootCause.getClass())) { |
| 76 | wrappedError = new TransportException(error); |
| 77 | } |
| 78 | log.warn("Connection to {} failed! Reason: {}", address, wrappedError); |
| 79 | future.completeExceptionally(wrappedError); |
| 80 | } else { |
| 81 | // If the connection is successful, the server will send back a |
| 82 | // connection ID indicating where to send messages for the connection. |
| 83 | ByteBuffer responseBuffer = ByteBuffer.wrap(payload); |
| 84 | if (responseBuffer.get() == SUCCESS) { |
| 85 | long connectionId = responseBuffer.getLong(); |
| 86 | CopycatTransportConnection connection = new CopycatTransportConnection( |
| 87 | connectionId, |
| 88 | CopycatTransportConnection.Mode.CLIENT, |
| 89 | partitionId, |
| 90 | endpoint, |
| 91 | messagingService, |
| 92 | context); |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 93 | connection.onClose(connections::remove); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 94 | connections.add(connection); |
| 95 | future.complete(connection); |
| 96 | log.debug("Created connection {}-{} to {}", partitionId, connectionId, address); |
| 97 | } else { |
| 98 | log.warn("Connection to {} failed!"); |
| 99 | future.completeExceptionally(new ConnectException()); |
| 100 | } |
| 101 | } |
| 102 | }); |
| 103 | return future; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public CompletableFuture<Void> close() { |
| 108 | return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new)); |
| 109 | } |
| 110 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 111 | @Override |
| 112 | public String toString() { |
| 113 | return toStringHelper(this) |
| 114 | .add("partitionId", partitionId) |
| 115 | .toString(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 116 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 117 | } |
| 118 | |