blob: c88242c6f0494b77c4755a9244c2513c2f6be3ab [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 }
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 Haltermanfda46f92017-04-14 10:49:44 -070093 connection.onClose(connections::remove);
Jordan Haltermane9c37092017-03-21 11:16:14 -070094 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 Jampani3289fbf2016-01-13 14:14:27 -0800104 }
105
106 @Override
107 public CompletableFuture<Void> close() {
108 return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new));
109 }
110
Jordan Haltermane9c37092017-03-21 11:16:14 -0700111 @Override
112 public String toString() {
113 return toStringHelper(this)
114 .add("partitionId", partitionId)
115 .toString();
Madan Jampani3289fbf2016-01-13 14:14:27 -0800116 }
Jordan Haltermane9c37092017-03-21 11:16:14 -0700117}
118