Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 20 | |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 23 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 24 | import org.apache.commons.lang.math.RandomUtils; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 26 | import org.onosproject.store.cluster.messaging.MessagingService; |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 27 | import org.slf4j.Logger; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 28 | |
| 29 | import com.google.common.collect.Sets; |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 30 | import com.google.common.primitives.Longs; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 31 | |
| 32 | import io.atomix.catalyst.transport.Address; |
| 33 | import io.atomix.catalyst.transport.Client; |
| 34 | import io.atomix.catalyst.transport.Connection; |
| 35 | import io.atomix.catalyst.util.concurrent.ThreadContext; |
| 36 | |
| 37 | /** |
| 38 | * {@link Client} implementation for {@link CopycatTransport}. |
| 39 | */ |
| 40 | public class CopycatTransportClient implements Client { |
| 41 | |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 42 | private final Logger log = getLogger(getClass()); |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 43 | private final PartitionId partitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 44 | private final MessagingService messagingService; |
| 45 | private final CopycatTransport.Mode mode; |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 46 | private final String newConnectionMessageSubject; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 47 | private final Set<CopycatTransportConnection> connections = Sets.newConcurrentHashSet(); |
| 48 | |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 49 | CopycatTransportClient(PartitionId partitionId, MessagingService messagingService, CopycatTransport.Mode mode) { |
| 50 | this.partitionId = checkNotNull(partitionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 51 | this.messagingService = checkNotNull(messagingService); |
| 52 | this.mode = checkNotNull(mode); |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 53 | this.newConnectionMessageSubject = String.format("onos-copycat-server-connection-%s", partitionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public CompletableFuture<Connection> connect(Address remoteAddress) { |
Madan Jampani | b06ccef | 2016-01-25 10:51:16 -0800 | [diff] [blame] | 58 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 59 | return messagingService.sendAndReceive(CopycatTransport.toEndpoint(remoteAddress), |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 60 | newConnectionMessageSubject, |
| 61 | Longs.toByteArray(nextConnectionId())) |
| 62 | .thenApplyAsync(bytes -> { |
| 63 | long connectionId = Longs.fromByteArray(bytes); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 64 | CopycatTransportConnection connection = new CopycatTransportConnection( |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 65 | connectionId, |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 66 | CopycatTransport.Mode.CLIENT, |
| 67 | partitionId, |
| 68 | remoteAddress, |
| 69 | messagingService, |
| 70 | context); |
| 71 | if (mode == CopycatTransport.Mode.CLIENT) { |
| 72 | connection.setBidirectional(); |
| 73 | } |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame^] | 74 | log.debug("Created new outgoing connection[id={}] to {}", connectionId, remoteAddress); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 75 | connections.add(connection); |
| 76 | return connection; |
| 77 | }, context.executor()); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public CompletableFuture<Void> close() { |
| 82 | return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new)); |
| 83 | } |
| 84 | |
| 85 | private long nextConnectionId() { |
| 86 | return RandomUtils.nextLong(); |
| 87 | } |
| 88 | } |