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