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