alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.service.impl; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 18 | import static java.util.concurrent.Executors.newCachedThreadPool; |
| 19 | import static org.onlab.util.Tools.namedThreads; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import static org.onosproject.store.service.impl.ClusterMessagingProtocol.*; |
| 22 | import static org.onosproject.store.service.impl.ClusterMessagingProtocol.DB_SERIALIZER; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 23 | |
| 24 | import java.util.concurrent.CompletableFuture; |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 25 | import java.util.concurrent.ExecutorService; |
Madan Jampani | 778f7ad | 2014-11-05 22:46:15 -0800 | [diff] [blame] | 26 | import java.util.function.BiConsumer; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 27 | |
| 28 | import net.kuujo.copycat.protocol.PingRequest; |
| 29 | import net.kuujo.copycat.protocol.PollRequest; |
| 30 | import net.kuujo.copycat.protocol.RequestHandler; |
| 31 | import net.kuujo.copycat.protocol.SubmitRequest; |
| 32 | import net.kuujo.copycat.protocol.SyncRequest; |
| 33 | import net.kuujo.copycat.spi.protocol.ProtocolServer; |
| 34 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 36 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 37 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | |
| 40 | /** |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame] | 41 | * ONOS Cluster messaging based Copycat protocol server. |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 42 | */ |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 43 | public class ClusterMessagingProtocolServer implements ProtocolServer { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 46 | |
| 47 | private final ClusterCommunicationService clusterCommunicator; |
| 48 | |
Yuta HIGUCHI | 76b54bf | 2014-11-07 01:56:55 -0800 | [diff] [blame] | 49 | private volatile RequestHandler handler; |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 50 | |
| 51 | private ExecutorService pool; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 52 | |
| 53 | public ClusterMessagingProtocolServer(ClusterCommunicationService clusterCommunicator) { |
Yuta HIGUCHI | 76b54bf | 2014-11-07 01:56:55 -0800 | [diff] [blame] | 54 | this.clusterCommunicator = clusterCommunicator; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void requestHandler(RequestHandler handler) { |
| 59 | this.handler = handler; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public CompletableFuture<Void> listen() { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 64 | if (pool == null || pool.isShutdown()) { |
| 65 | pool = newCachedThreadPool(namedThreads("copycat-netty-messaging-server-%d")); |
| 66 | } |
| 67 | |
| 68 | clusterCommunicator.addSubscriber(COPYCAT_PING, new PingHandler()); |
| 69 | clusterCommunicator.addSubscriber(COPYCAT_SYNC, new SyncHandler()); |
| 70 | clusterCommunicator.addSubscriber(COPYCAT_POLL, new PollHandler()); |
| 71 | clusterCommunicator.addSubscriber(COPYCAT_SUBMIT, new SubmitHandler()); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 72 | return CompletableFuture.completedFuture(null); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public CompletableFuture<Void> close() { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 77 | clusterCommunicator.removeSubscriber(COPYCAT_PING); |
| 78 | clusterCommunicator.removeSubscriber(COPYCAT_SYNC); |
| 79 | clusterCommunicator.removeSubscriber(COPYCAT_POLL); |
| 80 | clusterCommunicator.removeSubscriber(COPYCAT_SUBMIT); |
| 81 | if (pool != null) { |
| 82 | pool.shutdownNow(); |
| 83 | pool = null; |
| 84 | } |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 85 | return CompletableFuture.completedFuture(null); |
| 86 | } |
| 87 | |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 88 | private final class PingHandler extends CopycatMessageHandler<PingRequest> { |
| 89 | |
| 90 | @Override |
| 91 | public void raftHandle(PingRequest request, ClusterMessage message) { |
| 92 | pool.submit(new Runnable() { |
| 93 | |
| 94 | @Override |
| 95 | public void run() { |
| 96 | currentHandler().ping(request) |
| 97 | .whenComplete(new PostExecutionTask<>(message)); |
| 98 | } |
| 99 | }); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private final class SyncHandler extends CopycatMessageHandler<SyncRequest> { |
| 104 | |
| 105 | @Override |
| 106 | public void raftHandle(SyncRequest request, ClusterMessage message) { |
| 107 | pool.submit(new Runnable() { |
| 108 | |
| 109 | @Override |
| 110 | public void run() { |
| 111 | currentHandler().sync(request) |
| 112 | .whenComplete(new PostExecutionTask<>(message)); |
| 113 | } |
| 114 | }); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private final class PollHandler extends CopycatMessageHandler<PollRequest> { |
| 119 | |
| 120 | @Override |
| 121 | public void raftHandle(PollRequest request, ClusterMessage message) { |
| 122 | pool.submit(new Runnable() { |
| 123 | |
| 124 | @Override |
| 125 | public void run() { |
| 126 | currentHandler().poll(request) |
| 127 | .whenComplete(new PostExecutionTask<>(message)); |
| 128 | } |
| 129 | }); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private final class SubmitHandler extends CopycatMessageHandler<SubmitRequest> { |
| 134 | |
| 135 | @Override |
| 136 | public void raftHandle(SubmitRequest request, ClusterMessage message) { |
| 137 | pool.submit(new Runnable() { |
| 138 | |
| 139 | @Override |
| 140 | public void run() { |
| 141 | currentHandler().submit(request) |
| 142 | .whenComplete(new PostExecutionTask<>(message)); |
| 143 | } |
| 144 | }); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private abstract class CopycatMessageHandler<T> implements ClusterMessageHandler { |
| 149 | |
| 150 | public abstract void raftHandle(T request, ClusterMessage message); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 151 | |
| 152 | @Override |
| 153 | public void handle(ClusterMessage message) { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 154 | T request = DB_SERIALIZER.decode(message.payload()); |
| 155 | raftHandle(request, message); |
| 156 | } |
| 157 | |
| 158 | RequestHandler currentHandler() { |
| 159 | RequestHandler currentHandler = handler; |
| 160 | if (currentHandler == null) { |
Yuta HIGUCHI | 5fb6c96 | 2014-11-07 13:07:40 -0800 | [diff] [blame] | 161 | // there is a slight window of time during state transition, |
| 162 | // where handler becomes null |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 163 | long sleepMs = 1; |
Yuta HIGUCHI | 5fb6c96 | 2014-11-07 13:07:40 -0800 | [diff] [blame] | 164 | for (int i = 0; i < 10; ++i) { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 165 | currentHandler = handler; |
| 166 | if (currentHandler != null) { |
Yuta HIGUCHI | 5fb6c96 | 2014-11-07 13:07:40 -0800 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | try { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 170 | sleepMs <<= 1; |
| 171 | Thread.sleep(sleepMs); |
Yuta HIGUCHI | 5fb6c96 | 2014-11-07 13:07:40 -0800 | [diff] [blame] | 172 | } catch (InterruptedException e) { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 173 | log.error("Interrupted", e); |
| 174 | return handler; |
Yuta HIGUCHI | 5fb6c96 | 2014-11-07 13:07:40 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 177 | if (currentHandler == null) { |
Yuta HIGUCHI | 2fe6334 | 2014-11-17 21:33:38 -0800 | [diff] [blame] | 178 | log.error("There was no handler registered!"); |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 179 | return handler; |
Yuta HIGUCHI | 5fb6c96 | 2014-11-07 13:07:40 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 182 | return currentHandler; |
Madan Jampani | 778f7ad | 2014-11-05 22:46:15 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 185 | final class PostExecutionTask<R> implements BiConsumer<R, Throwable> { |
Madan Jampani | 778f7ad | 2014-11-05 22:46:15 -0800 | [diff] [blame] | 186 | |
| 187 | private final ClusterMessage message; |
| 188 | |
| 189 | public PostExecutionTask(ClusterMessage message) { |
| 190 | this.message = message; |
| 191 | } |
| 192 | |
| 193 | @Override |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 194 | public void accept(R response, Throwable error) { |
| 195 | if (error != null) { |
| 196 | log.error("Processing {} failed.", message.subject(), error); |
Madan Jampani | 778f7ad | 2014-11-05 22:46:15 -0800 | [diff] [blame] | 197 | } else { |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 198 | try { |
Yuta HIGUCHI | 5fb6c96 | 2014-11-07 13:07:40 -0800 | [diff] [blame] | 199 | log.trace("responding to {}", message.subject()); |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 200 | message.respond(DB_SERIALIZER.encode(response)); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 201 | } catch (Exception e) { |
Yuta HIGUCHI | 03e77e9 | 2014-11-22 17:53:01 -0800 | [diff] [blame] | 202 | log.error("Failed responding with {}", response.getClass().getName(), e); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 203 | } |
Madan Jampani | 778f7ad | 2014-11-05 22:46:15 -0800 | [diff] [blame] | 204 | } |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 208 | } |