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.collect.Sets; |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 19 | import io.atomix.catalyst.concurrent.ThreadContext; |
| 20 | import io.atomix.catalyst.transport.Address; |
| 21 | import io.atomix.catalyst.transport.Connection; |
| 22 | import io.atomix.catalyst.transport.Server; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 23 | import org.apache.commons.lang3.RandomUtils; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 25 | import org.onosproject.store.cluster.messaging.MessagingService; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 26 | import org.slf4j.Logger; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 27 | import org.slf4j.LoggerFactory; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 28 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 29 | import java.nio.ByteBuffer; |
| 30 | import java.util.Set; |
| 31 | import java.util.concurrent.CompletableFuture; |
| 32 | import java.util.function.Consumer; |
| 33 | |
| 34 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.onosproject.store.primitives.impl.CopycatTransport.CONNECT; |
| 37 | import static org.onosproject.store.primitives.impl.CopycatTransport.FAILURE; |
| 38 | import static org.onosproject.store.primitives.impl.CopycatTransport.SUCCESS; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 39 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 40 | /** |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 41 | * Copycat transport server implementation. |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 42 | */ |
| 43 | public class CopycatTransportServer implements Server { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 45 | private final PartitionId partitionId; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 46 | private final String serverSubject; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 47 | private final MessagingService messagingService; |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 48 | private final Set<CopycatTransportConnection> connections = Sets.newConcurrentHashSet(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 49 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 50 | public CopycatTransportServer(PartitionId partitionId, MessagingService messagingService) { |
| 51 | this.partitionId = checkNotNull(partitionId, "partitionId cannot be null"); |
| 52 | this.serverSubject = String.format("onos-copycat-%s", partitionId); |
| 53 | this.messagingService = checkNotNull(messagingService, "messagingService cannot be null"); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 57 | public CompletableFuture<Void> listen(Address address, Consumer<Connection> consumer) { |
| 58 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 59 | messagingService.registerHandler(serverSubject, (sender, payload) -> { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 60 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 61 | // Only connect messages can be sent to the server. Once a connect message |
| 62 | // is received, the connection will register a separate handler for messaging. |
| 63 | ByteBuffer requestBuffer = ByteBuffer.wrap(payload); |
| 64 | if (requestBuffer.get() != CONNECT) { |
| 65 | ByteBuffer responseBuffer = ByteBuffer.allocate(1); |
| 66 | responseBuffer.put(FAILURE); |
| 67 | return CompletableFuture.completedFuture(responseBuffer.array()); |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 68 | } |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 69 | |
| 70 | // Create the connection and ensure state is cleaned up when the connection is closed. |
| 71 | long connectionId = RandomUtils.nextLong(); |
| 72 | CopycatTransportConnection connection = new CopycatTransportConnection( |
| 73 | connectionId, |
| 74 | CopycatTransportConnection.Mode.SERVER, |
| 75 | partitionId, |
| 76 | sender, |
| 77 | messagingService, |
| 78 | context); |
Jordan Halterman | fda46f9 | 2017-04-14 10:49:44 -0700 | [diff] [blame] | 79 | connection.onClose(connections::remove); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 80 | connections.add(connection); |
| 81 | |
| 82 | CompletableFuture<byte[]> future = new CompletableFuture<>(); |
| 83 | |
| 84 | // We need to ensure the connection event is called on the Copycat thread |
| 85 | // and that the future is not completed until the Copycat server has been |
| 86 | // able to register message handlers, otherwise some messages can be received |
| 87 | // prior to any handlers being registered. |
| 88 | context.executor().execute(() -> { |
| 89 | log.debug("Created connection {}-{}", partitionId, connectionId); |
| 90 | consumer.accept(connection); |
| 91 | |
| 92 | ByteBuffer responseBuffer = ByteBuffer.allocate(9); |
| 93 | responseBuffer.put(SUCCESS); |
| 94 | responseBuffer.putLong(connectionId); |
| 95 | future.complete(responseBuffer.array()); |
| 96 | }); |
| 97 | return future; |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 98 | }); |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 99 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 102 | @Override |
| 103 | public CompletableFuture<Void> close() { |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 104 | return CompletableFuture.allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new)); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public String toString() { |
| 109 | return toStringHelper(this) |
| 110 | .add("partitionId", partitionId) |
| 111 | .toString(); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 112 | } |
| 113 | } |