blob: 516aa4422542d0e473683377b089df6c55971c40 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.service.impl;
Madan Jampani9b19a822014-11-04 21:37:13 -080017
Madan Jampani9b19a822014-11-04 21:37:13 -080018import static org.slf4j.LoggerFactory.getLogger;
19
Madan Jampani9b19a822014-11-04 21:37:13 -080020import java.util.Vector;
21
22import net.kuujo.copycat.cluster.TcpClusterConfig;
23import net.kuujo.copycat.cluster.TcpMember;
Madan Jampani5ce30252014-11-17 20:53:17 -080024import net.kuujo.copycat.event.LeaderElectEvent;
Madan Jampani9b19a822014-11-04 21:37:13 -080025import net.kuujo.copycat.internal.log.ConfigurationEntry;
26import net.kuujo.copycat.internal.log.CopycatEntry;
27import net.kuujo.copycat.internal.log.OperationEntry;
28import net.kuujo.copycat.internal.log.SnapshotEntry;
29import net.kuujo.copycat.protocol.PingRequest;
30import net.kuujo.copycat.protocol.PingResponse;
31import net.kuujo.copycat.protocol.PollRequest;
32import net.kuujo.copycat.protocol.PollResponse;
33import net.kuujo.copycat.protocol.Response.Status;
34import net.kuujo.copycat.protocol.SubmitRequest;
35import net.kuujo.copycat.protocol.SubmitResponse;
36import net.kuujo.copycat.protocol.SyncRequest;
37import net.kuujo.copycat.protocol.SyncResponse;
38import net.kuujo.copycat.spi.protocol.Protocol;
39import net.kuujo.copycat.spi.protocol.ProtocolClient;
40import net.kuujo.copycat.spi.protocol.ProtocolServer;
41
42import org.apache.felix.scr.annotations.Activate;
43import org.apache.felix.scr.annotations.Component;
44import org.apache.felix.scr.annotations.Deactivate;
45import org.apache.felix.scr.annotations.Reference;
46import org.apache.felix.scr.annotations.ReferenceCardinality;
47import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.cluster.ClusterService;
49import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
50import org.onosproject.store.cluster.messaging.MessageSubject;
51import org.onosproject.store.serializers.KryoNamespaces;
52import org.onosproject.store.serializers.KryoSerializer;
53import org.onosproject.store.serializers.StoreSerializer;
54import org.onosproject.store.service.impl.DatabaseStateMachine.State;
55import org.onosproject.store.service.impl.DatabaseStateMachine.TableMetadata;
Madan Jampani9b19a822014-11-04 21:37:13 -080056import org.onlab.util.KryoNamespace;
57import org.slf4j.Logger;
58
Madan Jampani9b19a822014-11-04 21:37:13 -080059/**
Madan Jampanidfbfa182014-11-04 22:06:41 -080060 * ONOS Cluster messaging based Copycat protocol.
Madan Jampani9b19a822014-11-04 21:37:13 -080061 */
Yuta HIGUCHI2103df42014-12-02 10:59:37 -080062@Component(immediate = false)
Madan Jampani9b19a822014-11-04 21:37:13 -080063@Service
Yuta HIGUCHI0c1c1002014-11-05 13:47:25 -080064public class ClusterMessagingProtocol
65 implements DatabaseProtocolService, Protocol<TcpMember> {
Madan Jampani9b19a822014-11-04 21:37:13 -080066
67 private final Logger log = getLogger(getClass());
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI5001ba92014-11-04 21:33:54 -080070 protected ClusterService clusterService;
Madan Jampani9b19a822014-11-04 21:37:13 -080071
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHI5001ba92014-11-04 21:33:54 -080073 protected ClusterCommunicationService clusterCommunicator;
Madan Jampani9b19a822014-11-04 21:37:13 -080074
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 HIGUCHI91768e32014-11-22 05:06:35 -080084 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 Jampani9b19a822014-11-04 21:37:13 -080089 .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 Jampani5ce30252014-11-17 20:53:17 -0800104 .register(LeaderElectEvent.class)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800105 .register(Vector.class)
Madan Jampani9b19a822014-11-04 21:37:13 -0800106 .build();
107
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800108 // serializer used for CopyCat Protocol
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800109 public static final StoreSerializer DB_SERIALIZER = new KryoSerializer() {
Madan Jampani9b19a822014-11-04 21:37:13 -0800110 @Override
111 protected void setupKryoPool() {
112 serializerPool = KryoNamespace.newBuilder()
113 .register(COPYCAT)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800114 .nextId(AFTER_COPYCAT)
115 // for snapshot
116 .register(State.class)
117 .register(TableMetadata.class)
Yuta HIGUCHI65934892014-12-04 17:47:44 -0800118 // TODO: Move this out to API?
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800119 .register(TableModificationEvent.class)
120 .register(TableModificationEvent.Type.class)
121 .build();
Madan Jampani9b19a822014-11-04 21:37:13 -0800122 }
123 };
124
125 @Activate
126 public void activate() {
Yuta HIGUCHI79a1e5e2014-11-05 17:42:01 -0800127 log.info("Started");
Madan Jampani9b19a822014-11-04 21:37:13 -0800128 }
129
130 @Deactivate
131 public void deactivate() {
Yuta HIGUCHI79a1e5e2014-11-05 17:42:01 -0800132 log.info("Stopped");
Madan Jampani9b19a822014-11-04 21:37:13 -0800133 }
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 HIGUCHI71b9d092014-11-12 13:31:11 -0800142 return new ClusterMessagingProtocolClient(clusterService,
143 clusterCommunicator,
144 clusterService.getLocalNode(),
145 member);
Madan Jampani9b19a822014-11-04 21:37:13 -0800146 }
Madan Jampani7e634e62014-11-09 21:24:13 -0800147}