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 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 20 | import java.util.Vector; |
| 21 | |
| 22 | import net.kuujo.copycat.cluster.TcpClusterConfig; |
| 23 | import net.kuujo.copycat.cluster.TcpMember; |
Madan Jampani | 5ce3025 | 2014-11-17 20:53:17 -0800 | [diff] [blame] | 24 | import net.kuujo.copycat.event.LeaderElectEvent; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 25 | import net.kuujo.copycat.internal.log.ConfigurationEntry; |
| 26 | import net.kuujo.copycat.internal.log.CopycatEntry; |
| 27 | import net.kuujo.copycat.internal.log.OperationEntry; |
| 28 | import net.kuujo.copycat.internal.log.SnapshotEntry; |
| 29 | import net.kuujo.copycat.protocol.PingRequest; |
| 30 | import net.kuujo.copycat.protocol.PingResponse; |
| 31 | import net.kuujo.copycat.protocol.PollRequest; |
| 32 | import net.kuujo.copycat.protocol.PollResponse; |
| 33 | import net.kuujo.copycat.protocol.Response.Status; |
| 34 | import net.kuujo.copycat.protocol.SubmitRequest; |
| 35 | import net.kuujo.copycat.protocol.SubmitResponse; |
| 36 | import net.kuujo.copycat.protocol.SyncRequest; |
| 37 | import net.kuujo.copycat.protocol.SyncResponse; |
| 38 | import net.kuujo.copycat.spi.protocol.Protocol; |
| 39 | import net.kuujo.copycat.spi.protocol.ProtocolClient; |
| 40 | import net.kuujo.copycat.spi.protocol.ProtocolServer; |
| 41 | |
| 42 | import org.apache.felix.scr.annotations.Activate; |
| 43 | import org.apache.felix.scr.annotations.Component; |
| 44 | import org.apache.felix.scr.annotations.Deactivate; |
| 45 | import org.apache.felix.scr.annotations.Reference; |
| 46 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 47 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.cluster.ClusterService; |
| 49 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 50 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 51 | import org.onosproject.store.serializers.KryoNamespaces; |
| 52 | import org.onosproject.store.serializers.KryoSerializer; |
| 53 | import org.onosproject.store.serializers.StoreSerializer; |
| 54 | import org.onosproject.store.service.impl.DatabaseStateMachine.State; |
| 55 | import org.onosproject.store.service.impl.DatabaseStateMachine.TableMetadata; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 56 | import org.onlab.util.KryoNamespace; |
| 57 | import org.slf4j.Logger; |
| 58 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 59 | /** |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame] | 60 | * ONOS Cluster messaging based Copycat protocol. |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 61 | */ |
Yuta HIGUCHI | 2103df4 | 2014-12-02 10:59:37 -0800 | [diff] [blame] | 62 | @Component(immediate = false) |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 63 | @Service |
Yuta HIGUCHI | 0c1c100 | 2014-11-05 13:47:25 -0800 | [diff] [blame] | 64 | public class ClusterMessagingProtocol |
| 65 | implements DatabaseProtocolService, Protocol<TcpMember> { |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 5001ba9 | 2014-11-04 21:33:54 -0800 | [diff] [blame] | 70 | protected ClusterService clusterService; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 5001ba9 | 2014-11-04 21:33:54 -0800 | [diff] [blame] | 73 | protected ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 74 | |
| 75 | public static final MessageSubject COPYCAT_PING = |
| 76 | new MessageSubject("copycat-raft-consensus-ping"); |
| 77 | public static final MessageSubject COPYCAT_SYNC = |
| 78 | new MessageSubject("copycat-raft-consensus-sync"); |
| 79 | public static final MessageSubject COPYCAT_POLL = |
| 80 | new MessageSubject("copycat-raft-consensus-poll"); |
| 81 | public static final MessageSubject COPYCAT_SUBMIT = |
| 82 | new MessageSubject("copycat-raft-consensus-submit"); |
| 83 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 84 | static final int AFTER_COPYCAT = KryoNamespaces.BEGIN_USER_CUSTOM_ID + 50; |
| 85 | |
| 86 | static final KryoNamespace COPYCAT = KryoNamespace.newBuilder() |
| 87 | .register(KryoNamespaces.API) |
| 88 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 89 | .register(PingRequest.class) |
| 90 | .register(PingResponse.class) |
| 91 | .register(PollRequest.class) |
| 92 | .register(PollResponse.class) |
| 93 | .register(SyncRequest.class) |
| 94 | .register(SyncResponse.class) |
| 95 | .register(SubmitRequest.class) |
| 96 | .register(SubmitResponse.class) |
| 97 | .register(Status.class) |
| 98 | .register(ConfigurationEntry.class) |
| 99 | .register(SnapshotEntry.class) |
| 100 | .register(CopycatEntry.class) |
| 101 | .register(OperationEntry.class) |
| 102 | .register(TcpClusterConfig.class) |
| 103 | .register(TcpMember.class) |
Madan Jampani | 5ce3025 | 2014-11-17 20:53:17 -0800 | [diff] [blame] | 104 | .register(LeaderElectEvent.class) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 105 | .register(Vector.class) |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 106 | .build(); |
| 107 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 108 | // serializer used for CopyCat Protocol |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 109 | public static final StoreSerializer DB_SERIALIZER = new KryoSerializer() { |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 110 | @Override |
| 111 | protected void setupKryoPool() { |
| 112 | serializerPool = KryoNamespace.newBuilder() |
| 113 | .register(COPYCAT) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 114 | .nextId(AFTER_COPYCAT) |
| 115 | // for snapshot |
| 116 | .register(State.class) |
| 117 | .register(TableMetadata.class) |
Yuta HIGUCHI | 6593489 | 2014-12-04 17:47:44 -0800 | [diff] [blame] | 118 | // TODO: Move this out to API? |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 119 | .register(TableModificationEvent.class) |
| 120 | .register(TableModificationEvent.Type.class) |
| 121 | .build(); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 122 | } |
| 123 | }; |
| 124 | |
| 125 | @Activate |
| 126 | public void activate() { |
Yuta HIGUCHI | 79a1e5e | 2014-11-05 17:42:01 -0800 | [diff] [blame] | 127 | log.info("Started"); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Deactivate |
| 131 | public void deactivate() { |
Yuta HIGUCHI | 79a1e5e | 2014-11-05 17:42:01 -0800 | [diff] [blame] | 132 | log.info("Stopped"); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public ProtocolServer createServer(TcpMember member) { |
| 137 | return new ClusterMessagingProtocolServer(clusterCommunicator); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public ProtocolClient createClient(TcpMember member) { |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 142 | return new ClusterMessagingProtocolClient(clusterService, |
| 143 | clusterCommunicator, |
| 144 | clusterService.getLocalNode(), |
| 145 | member); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 146 | } |
Madan Jampani | 7e634e6 | 2014-11-09 21:24:13 -0800 | [diff] [blame] | 147 | } |