blob: afa98ccd5461d9dc44bc709d711977a5771676bb [file] [log] [blame]
Madan Jampani3289fbf2016-01-13 14:14:27 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani3289fbf2016-01-13 14:14:27 -08003 *
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 */
16package org.onosproject.store.primitives.impl;
17
Jordan Haltermane9c37092017-03-21 11:16:14 -070018import com.google.common.base.Throwables;
Madan Jampani3289fbf2016-01-13 14:14:27 -080019import com.google.common.collect.Sets;
Jordan Haltermane9c37092017-03-21 11:16:14 -070020import io.atomix.catalyst.concurrent.ThreadContext;
Madan Jampani3289fbf2016-01-13 14:14:27 -080021import io.atomix.catalyst.transport.Address;
22import io.atomix.catalyst.transport.Client;
23import io.atomix.catalyst.transport.Connection;
Jordan Haltermane9c37092017-03-21 11:16:14 -070024import io.atomix.catalyst.transport.TransportException;
25import org.onosproject.cluster.PartitionId;
26import org.onosproject.store.cluster.messaging.Endpoint;
27import org.onosproject.store.cluster.messaging.MessagingException;
28import org.onosproject.store.cluster.messaging.MessagingService;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.net.ConnectException;
33import java.nio.ByteBuffer;
34import java.util.Set;
35import java.util.concurrent.CompletableFuture;
36
37import static com.google.common.base.MoreObjects.toStringHelper;
38import static com.google.common.base.Preconditions.checkNotNull;
39import static org.onosproject.store.primitives.impl.CopycatTransport.CONNECT;
40import static org.onosproject.store.primitives.impl.CopycatTransport.SUCCESS;
Madan Jampani3289fbf2016-01-13 14:14:27 -080041
42/**
Jordan Haltermane9c37092017-03-21 11:16:14 -070043 * Copycat transport client implementation.
Madan Jampani3289fbf2016-01-13 14:14:27 -080044 */
45public class CopycatTransportClient implements Client {
Jordan Haltermane9c37092017-03-21 11:16:14 -070046 private final Logger log = LoggerFactory.getLogger(getClass());
Madan Jampanif778c962016-01-31 22:56:38 -080047 private final PartitionId partitionId;
Jordan Haltermane9c37092017-03-21 11:16:14 -070048 private final String serverSubject;
Madan Jampani3289fbf2016-01-13 14:14:27 -080049 private final MessagingService messagingService;
Madan Jampani3289fbf2016-01-13 14:14:27 -080050 private final Set<CopycatTransportConnection> connections = Sets.newConcurrentHashSet();
51
Jordan Haltermane9c37092017-03-21 11:16:14 -070052 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 Jampani3289fbf2016-01-13 14:14:27 -080056 }
57
58 @Override
Jordan Haltermane9c37092017-03-21 11:16:14 -070059 public CompletableFuture<Connection> connect(Address address) {
60 CompletableFuture<Connection> future = new CompletableFuture<>();
Madan Jampanib06ccef2016-01-25 10:51:16 -080061 ThreadContext context = ThreadContext.currentContextOrThrow();
Jordan Haltermane9c37092017-03-21 11:16:14 -070062 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 }
Brian O'Connor740e98c2017-06-29 17:07:17 -070078 // TODO ONOS-6788 we might consider demoting this warning during startup when there is
79 // a race between the server registering handlers and the client sending messages
Jordan Haltermane9c37092017-03-21 11:16:14 -070080 log.warn("Connection to {} failed! Reason: {}", address, wrappedError);
81 future.completeExceptionally(wrappedError);
82 } else {
83 // If the connection is successful, the server will send back a
84 // connection ID indicating where to send messages for the connection.
85 ByteBuffer responseBuffer = ByteBuffer.wrap(payload);
86 if (responseBuffer.get() == SUCCESS) {
87 long connectionId = responseBuffer.getLong();
88 CopycatTransportConnection connection = new CopycatTransportConnection(
89 connectionId,
90 CopycatTransportConnection.Mode.CLIENT,
91 partitionId,
92 endpoint,
93 messagingService,
94 context);
Jordan Haltermanfda46f92017-04-14 10:49:44 -070095 connection.onClose(connections::remove);
Jordan Haltermane9c37092017-03-21 11:16:14 -070096 connections.add(connection);
97 future.complete(connection);
98 log.debug("Created connection {}-{} to {}", partitionId, connectionId, address);
99 } else {
100 log.warn("Connection to {} failed!");
101 future.completeExceptionally(new ConnectException());
102 }
103 }
104 });
105 return future;
Madan Jampani3289fbf2016-01-13 14:14:27 -0800106 }
107
108 @Override
109 public CompletableFuture<Void> close() {
110 return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new));
111 }
112
Jordan Haltermane9c37092017-03-21 11:16:14 -0700113 @Override
114 public String toString() {
115 return toStringHelper(this)
116 .add("partitionId", partitionId)
117 .toString();
Madan Jampani3289fbf2016-01-13 14:14:27 -0800118 }
Jordan Haltermane9c37092017-03-21 11:16:14 -0700119}
120