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