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 | |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Arrays; |
| 7 | import java.util.Collection; |
Madan Jampani | 7e634e6 | 2014-11-09 21:24:13 -0800 | [diff] [blame] | 8 | import java.util.Collections; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 9 | import java.util.HashMap; |
| 10 | import java.util.HashSet; |
| 11 | import java.util.LinkedList; |
| 12 | import java.util.Vector; |
| 13 | |
| 14 | import net.kuujo.copycat.cluster.TcpClusterConfig; |
| 15 | import net.kuujo.copycat.cluster.TcpMember; |
| 16 | import net.kuujo.copycat.internal.log.ConfigurationEntry; |
| 17 | import net.kuujo.copycat.internal.log.CopycatEntry; |
| 18 | import net.kuujo.copycat.internal.log.OperationEntry; |
| 19 | import net.kuujo.copycat.internal.log.SnapshotEntry; |
| 20 | import net.kuujo.copycat.protocol.PingRequest; |
| 21 | import net.kuujo.copycat.protocol.PingResponse; |
| 22 | import net.kuujo.copycat.protocol.PollRequest; |
| 23 | import net.kuujo.copycat.protocol.PollResponse; |
| 24 | import net.kuujo.copycat.protocol.Response.Status; |
| 25 | import net.kuujo.copycat.protocol.SubmitRequest; |
| 26 | import net.kuujo.copycat.protocol.SubmitResponse; |
| 27 | import net.kuujo.copycat.protocol.SyncRequest; |
| 28 | import net.kuujo.copycat.protocol.SyncResponse; |
| 29 | import net.kuujo.copycat.spi.protocol.Protocol; |
| 30 | import net.kuujo.copycat.spi.protocol.ProtocolClient; |
| 31 | import net.kuujo.copycat.spi.protocol.ProtocolServer; |
| 32 | |
| 33 | import org.apache.felix.scr.annotations.Activate; |
| 34 | import org.apache.felix.scr.annotations.Component; |
| 35 | import org.apache.felix.scr.annotations.Deactivate; |
| 36 | import org.apache.felix.scr.annotations.Reference; |
| 37 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 38 | import org.apache.felix.scr.annotations.Service; |
| 39 | import org.onlab.onos.cluster.ClusterService; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 40 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 41 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
| 42 | import org.onlab.onos.store.serializers.ImmutableListSerializer; |
| 43 | import org.onlab.onos.store.serializers.ImmutableMapSerializer; |
| 44 | import org.onlab.onos.store.serializers.ImmutableSetSerializer; |
| 45 | import org.onlab.onos.store.serializers.KryoSerializer; |
Madan Jampani | bd1f026 | 2014-11-12 01:51:25 -0800 | [diff] [blame] | 46 | import org.onlab.onos.store.service.BatchReadRequest; |
| 47 | import org.onlab.onos.store.service.BatchWriteRequest; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 48 | import org.onlab.onos.store.service.ReadRequest; |
| 49 | import org.onlab.onos.store.service.ReadResult; |
Madan Jampani | bd1f026 | 2014-11-12 01:51:25 -0800 | [diff] [blame] | 50 | import org.onlab.onos.store.service.ReadStatus; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 51 | import org.onlab.onos.store.service.VersionedValue; |
| 52 | import org.onlab.onos.store.service.WriteRequest; |
| 53 | import org.onlab.onos.store.service.WriteResult; |
Madan Jampani | bd1f026 | 2014-11-12 01:51:25 -0800 | [diff] [blame] | 54 | import org.onlab.onos.store.service.WriteStatus; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 55 | import org.onlab.util.KryoNamespace; |
| 56 | import org.slf4j.Logger; |
| 57 | |
| 58 | import com.esotericsoftware.kryo.Kryo; |
| 59 | import com.esotericsoftware.kryo.io.Input; |
| 60 | import com.esotericsoftware.kryo.serializers.CollectionSerializer; |
| 61 | import com.google.common.collect.ImmutableList; |
| 62 | import com.google.common.collect.ImmutableMap; |
| 63 | import com.google.common.collect.ImmutableSet; |
| 64 | |
| 65 | /** |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame] | 66 | * ONOS Cluster messaging based Copycat protocol. |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 67 | */ |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 68 | @Component(immediate = true) |
| 69 | @Service |
Yuta HIGUCHI | 0c1c100 | 2014-11-05 13:47:25 -0800 | [diff] [blame] | 70 | public class ClusterMessagingProtocol |
| 71 | implements DatabaseProtocolService, Protocol<TcpMember> { |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 72 | |
| 73 | private final Logger log = getLogger(getClass()); |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 5001ba9 | 2014-11-04 21:33:54 -0800 | [diff] [blame] | 76 | protected ClusterService clusterService; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 5001ba9 | 2014-11-04 21:33:54 -0800 | [diff] [blame] | 79 | protected ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 80 | |
| 81 | public static final MessageSubject COPYCAT_PING = |
| 82 | new MessageSubject("copycat-raft-consensus-ping"); |
| 83 | public static final MessageSubject COPYCAT_SYNC = |
| 84 | new MessageSubject("copycat-raft-consensus-sync"); |
| 85 | public static final MessageSubject COPYCAT_POLL = |
| 86 | new MessageSubject("copycat-raft-consensus-poll"); |
| 87 | public static final MessageSubject COPYCAT_SUBMIT = |
| 88 | new MessageSubject("copycat-raft-consensus-submit"); |
| 89 | |
| 90 | private static final KryoNamespace COPYCAT = KryoNamespace.newBuilder() |
| 91 | .register(PingRequest.class) |
| 92 | .register(PingResponse.class) |
| 93 | .register(PollRequest.class) |
| 94 | .register(PollResponse.class) |
| 95 | .register(SyncRequest.class) |
| 96 | .register(SyncResponse.class) |
| 97 | .register(SubmitRequest.class) |
| 98 | .register(SubmitResponse.class) |
| 99 | .register(Status.class) |
| 100 | .register(ConfigurationEntry.class) |
| 101 | .register(SnapshotEntry.class) |
| 102 | .register(CopycatEntry.class) |
| 103 | .register(OperationEntry.class) |
| 104 | .register(TcpClusterConfig.class) |
| 105 | .register(TcpMember.class) |
| 106 | .build(); |
| 107 | |
| 108 | private static final KryoNamespace DATABASE = KryoNamespace.newBuilder() |
| 109 | .register(ReadRequest.class) |
| 110 | .register(WriteRequest.class) |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 111 | .register(WriteRequest.Type.class) |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 112 | .register(WriteResult.class) |
| 113 | .register(ReadResult.class) |
Madan Jampani | bd1f026 | 2014-11-12 01:51:25 -0800 | [diff] [blame] | 114 | .register(BatchReadRequest.class) |
| 115 | .register(BatchWriteRequest.class) |
| 116 | .register(ReadStatus.class) |
| 117 | .register(WriteStatus.class) |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 118 | .register(VersionedValue.class) |
| 119 | .build(); |
| 120 | |
| 121 | public static final KryoNamespace COMMON = KryoNamespace.newBuilder() |
| 122 | .register(Arrays.asList().getClass(), new CollectionSerializer() { |
| 123 | @Override |
| 124 | @SuppressWarnings("rawtypes") |
| 125 | protected Collection<?> create(Kryo kryo, Input input, Class<Collection> type) { |
| 126 | return new ArrayList(); |
| 127 | } |
| 128 | }) |
| 129 | .register(ImmutableMap.class, new ImmutableMapSerializer()) |
| 130 | .register(ImmutableList.class, new ImmutableListSerializer()) |
| 131 | .register(ImmutableSet.class, new ImmutableSetSerializer()) |
| 132 | .register( |
| 133 | Vector.class, |
| 134 | ArrayList.class, |
| 135 | Arrays.asList().getClass(), |
| 136 | HashMap.class, |
| 137 | HashSet.class, |
| 138 | LinkedList.class, |
Madan Jampani | 7e634e6 | 2014-11-09 21:24:13 -0800 | [diff] [blame] | 139 | Collections.singletonList("").getClass(), |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 140 | byte[].class) |
| 141 | .build(); |
| 142 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame] | 143 | // serializer used for CopyCat Protocol |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 144 | public static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 145 | @Override |
| 146 | protected void setupKryoPool() { |
| 147 | serializerPool = KryoNamespace.newBuilder() |
| 148 | .register(COPYCAT) |
| 149 | .register(COMMON) |
| 150 | .register(DATABASE) |
| 151 | .build() |
| 152 | .populate(1); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | @Activate |
| 157 | public void activate() { |
Yuta HIGUCHI | 79a1e5e | 2014-11-05 17:42:01 -0800 | [diff] [blame] | 158 | log.info("Started"); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Deactivate |
| 162 | public void deactivate() { |
Yuta HIGUCHI | 79a1e5e | 2014-11-05 17:42:01 -0800 | [diff] [blame] | 163 | log.info("Stopped"); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public ProtocolServer createServer(TcpMember member) { |
| 168 | return new ClusterMessagingProtocolServer(clusterCommunicator); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public ProtocolClient createClient(TcpMember member) { |
Yuta HIGUCHI | 71b9d09 | 2014-11-12 13:31:11 -0800 | [diff] [blame] | 173 | return new ClusterMessagingProtocolClient(clusterService, |
| 174 | clusterCommunicator, |
| 175 | clusterService.getLocalNode(), |
| 176 | member); |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 177 | } |
Madan Jampani | 7e634e6 | 2014-11-09 21:24:13 -0800 | [diff] [blame] | 178 | } |