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 | 2f9cc71 | 2016-02-15 19:36:21 -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.io.ByteArrayInputStream; |
| 22 | import java.io.DataInputStream; |
| 23 | import java.io.IOException; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.concurrent.CompletableFuture; |
| 26 | import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | import java.util.function.Consumer; |
| 28 | |
| 29 | import org.apache.commons.io.IOUtils; |
| 30 | import org.onlab.util.Tools; |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 31 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 32 | import org.onosproject.store.cluster.messaging.MessagingService; |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 33 | import org.slf4j.Logger; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 34 | |
| 35 | import com.google.common.collect.Maps; |
| 36 | |
| 37 | import io.atomix.catalyst.transport.Address; |
| 38 | import io.atomix.catalyst.transport.Connection; |
| 39 | import io.atomix.catalyst.transport.Server; |
| 40 | import io.atomix.catalyst.util.concurrent.SingleThreadContext; |
| 41 | import io.atomix.catalyst.util.concurrent.ThreadContext; |
| 42 | |
| 43 | /** |
| 44 | * {@link Server} implementation for {@link CopycatTransport}. |
| 45 | */ |
| 46 | public class CopycatTransportServer implements Server { |
| 47 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 48 | private final Logger log = getLogger(getClass()); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 49 | private final AtomicBoolean listening = new AtomicBoolean(false); |
Madan Jampani | 71d13e1 | 2016-01-13 17:14:35 -0800 | [diff] [blame] | 50 | private CompletableFuture<Void> listenFuture = new CompletableFuture<>(); |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 51 | private final PartitionId partitionId; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 52 | private final MessagingService messagingService; |
| 53 | private final String messageSubject; |
| 54 | private final Map<Long, CopycatTransportConnection> connections = Maps.newConcurrentMap(); |
| 55 | |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 56 | CopycatTransportServer(PartitionId partitionId, MessagingService messagingService) { |
| 57 | this.partitionId = checkNotNull(partitionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 58 | this.messagingService = checkNotNull(messagingService); |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 59 | this.messageSubject = String.format("onos-copycat-%s", partitionId); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public CompletableFuture<Void> listen(Address address, Consumer<Connection> listener) { |
Madan Jampani | 71d13e1 | 2016-01-13 17:14:35 -0800 | [diff] [blame] | 64 | if (listening.compareAndSet(false, true)) { |
| 65 | ThreadContext context = ThreadContext.currentContextOrThrow(); |
| 66 | listen(address, listener, context); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 67 | } |
| 68 | return listenFuture; |
| 69 | } |
| 70 | |
Madan Jampani | 71d13e1 | 2016-01-13 17:14:35 -0800 | [diff] [blame] | 71 | private void listen(Address address, Consumer<Connection> listener, ThreadContext context) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 72 | messagingService.registerHandler(messageSubject, (sender, payload) -> { |
| 73 | try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) { |
| 74 | long connectionId = input.readLong(); |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 75 | AtomicBoolean newConnectionCreated = new AtomicBoolean(false); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 76 | CopycatTransportConnection connection = connections.computeIfAbsent(connectionId, k -> { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 77 | newConnectionCreated.set(true); |
| 78 | CopycatTransportConnection newConnection = new CopycatTransportConnection(connectionId, |
| 79 | CopycatTransport.Mode.SERVER, |
| 80 | partitionId, |
| 81 | CopycatTransport.toAddress(sender), |
| 82 | messagingService, |
| 83 | getOrCreateContext(context)); |
| 84 | log.debug("Created new incoming connection {}", connectionId); |
| 85 | newConnection.closeListener(c -> connections.remove(connectionId, c)); |
| 86 | return newConnection; |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 87 | }); |
| 88 | byte[] request = IOUtils.toByteArray(input); |
| 89 | return CompletableFuture.supplyAsync( |
| 90 | () -> { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 91 | if (newConnectionCreated.get()) { |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 92 | listener.accept(connection); |
| 93 | } |
| 94 | return connection; |
| 95 | }, context.executor()).thenCompose(c -> c.handle(request)); |
| 96 | } catch (IOException e) { |
| 97 | return Tools.exceptionalFuture(e); |
| 98 | } |
| 99 | }); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 100 | context.execute(() -> { |
| 101 | listenFuture.complete(null); |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public CompletableFuture<Void> close() { |
| 107 | messagingService.unregisterHandler(messageSubject); |
| 108 | return CompletableFuture.completedFuture(null); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the current execution context or creates one. |
| 113 | */ |
| 114 | private ThreadContext getOrCreateContext(ThreadContext parentContext) { |
| 115 | ThreadContext context = ThreadContext.currentContext(); |
| 116 | if (context != null) { |
| 117 | return context; |
| 118 | } |
Madan Jampani | f778c96 | 2016-01-31 22:56:38 -0800 | [diff] [blame] | 119 | return new SingleThreadContext("copycat-transport-server-" + partitionId, parentContext.serializer().clone()); |
Madan Jampani | 3289fbf | 2016-01-13 14:14:27 -0800 | [diff] [blame] | 120 | } |
| 121 | } |