Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 1 | package org.onlab.onos.store.service.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Arrays; |
| 7 | import java.util.List; |
| 8 | |
| 9 | import net.kuujo.copycat.Copycat; |
| 10 | import net.kuujo.copycat.StateMachine; |
| 11 | import net.kuujo.copycat.cluster.TcpCluster; |
| 12 | import net.kuujo.copycat.cluster.TcpClusterConfig; |
| 13 | import net.kuujo.copycat.cluster.TcpMember; |
| 14 | import net.kuujo.copycat.log.ChronicleLog; |
| 15 | import net.kuujo.copycat.log.Log; |
| 16 | |
| 17 | import org.apache.felix.scr.annotations.Activate; |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame^] | 18 | import org.apache.felix.scr.annotations.Component; |
Yuta HIGUCHI | 657626e | 2014-11-04 20:54:58 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Reference; |
| 21 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame^] | 22 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 23 | import org.onlab.onos.cluster.ClusterService; |
| 24 | import org.onlab.onos.cluster.ControllerNode; |
| 25 | import org.onlab.onos.store.service.DatabaseAdminService; |
| 26 | import org.onlab.onos.store.service.DatabaseException; |
| 27 | import org.onlab.onos.store.service.DatabaseService; |
| 28 | import org.onlab.onos.store.service.NoSuchTableException; |
| 29 | import org.onlab.onos.store.service.OptimisticLockException; |
| 30 | import org.onlab.onos.store.service.OptionalResult; |
| 31 | import org.onlab.onos.store.service.PreconditionFailedException; |
| 32 | import org.onlab.onos.store.service.ReadRequest; |
| 33 | import org.onlab.onos.store.service.ReadResult; |
| 34 | import org.onlab.onos.store.service.WriteAborted; |
| 35 | import org.onlab.onos.store.service.WriteRequest; |
| 36 | import org.onlab.onos.store.service.WriteResult; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import com.google.common.collect.Lists; |
| 40 | |
| 41 | /** |
| 42 | * Strongly consistent and durable state management service based on |
| 43 | * Copycat implementation of Raft consensus protocol. |
| 44 | */ |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame^] | 45 | @Component(immediate = true) |
| 46 | @Service |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 47 | public class DatabaseManager implements DatabaseService, DatabaseAdminService { |
| 48 | |
| 49 | private final Logger log = getLogger(getClass()); |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | ClusterService clusterService; |
| 53 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | ClusterMessagingProtocol copycatMessagingProtocol; |
| 56 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 57 | public static final String LOG_FILE_PREFIX = "onos-copy-cat-log"; |
| 58 | |
| 59 | private Copycat copycat; |
| 60 | private DatabaseClient client; |
| 61 | |
| 62 | @Activate |
| 63 | public void activate() { |
Madan Jampani | dfbfa18 | 2014-11-04 22:06:41 -0800 | [diff] [blame^] | 64 | |
| 65 | // TODO: Not every node can be part of the consensus ring. |
| 66 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 67 | TcpMember localMember = |
| 68 | new TcpMember( |
| 69 | clusterService.getLocalNode().ip().toString(), |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 70 | clusterService.getLocalNode().tcpPort()); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 71 | List<TcpMember> remoteMembers = Lists.newArrayList(); |
| 72 | |
| 73 | for (ControllerNode node : clusterService.getNodes()) { |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 74 | TcpMember member = new TcpMember(node.ip().toString(), node.tcpPort()); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 75 | if (!member.equals(localMember)) { |
| 76 | remoteMembers.add(member); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Configure the cluster. |
| 81 | TcpClusterConfig config = new TcpClusterConfig(); |
| 82 | |
| 83 | config.setLocalMember(localMember); |
| 84 | config.setRemoteMembers(remoteMembers.toArray(new TcpMember[]{})); |
| 85 | |
| 86 | // Create the cluster. |
| 87 | TcpCluster cluster = new TcpCluster(config); |
| 88 | |
| 89 | StateMachine stateMachine = new DatabaseStateMachine(); |
| 90 | ControllerNode thisNode = clusterService.getLocalNode(); |
| 91 | Log consensusLog = new ChronicleLog(LOG_FILE_PREFIX + "_" + thisNode.id()); |
| 92 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 93 | copycat = new Copycat(stateMachine, consensusLog, cluster, copycatMessagingProtocol); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 94 | copycat.start(); |
| 95 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 96 | client = new DatabaseClient(copycatMessagingProtocol.createClient(localMember)); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 97 | |
| 98 | log.info("Started."); |
| 99 | } |
| 100 | |
Yuta HIGUCHI | 657626e | 2014-11-04 20:54:58 -0800 | [diff] [blame] | 101 | @Deactivate |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 102 | public void deactivate() { |
| 103 | copycat.stop(); |
Yuta HIGUCHI | 657626e | 2014-11-04 20:54:58 -0800 | [diff] [blame] | 104 | log.info("Stopped."); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean createTable(String name) { |
| 109 | return client.createTable(name); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void dropTable(String name) { |
| 114 | client.dropTable(name); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void dropAllTables() { |
| 119 | client.dropAllTables(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public List<String> listTables() { |
| 124 | return client.listTables(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public ReadResult read(ReadRequest request) { |
| 129 | return batchRead(Arrays.asList(request)).get(0).get(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public List<OptionalResult<ReadResult, DatabaseException>> batchRead( |
| 134 | List<ReadRequest> batch) { |
| 135 | List<OptionalResult<ReadResult, DatabaseException>> readResults = new ArrayList<>(batch.size()); |
| 136 | for (InternalReadResult internalReadResult : client.batchRead(batch)) { |
| 137 | if (internalReadResult.status() == InternalReadResult.Status.NO_SUCH_TABLE) { |
| 138 | readResults.add(new DatabaseOperationResult<ReadResult, DatabaseException>( |
| 139 | new NoSuchTableException())); |
| 140 | } else { |
| 141 | readResults.add(new DatabaseOperationResult<ReadResult, DatabaseException>( |
| 142 | internalReadResult.result())); |
| 143 | } |
| 144 | } |
| 145 | return readResults; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public WriteResult write(WriteRequest request) { |
| 150 | return batchWrite(Arrays.asList(request)).get(0).get(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public List<OptionalResult<WriteResult, DatabaseException>> batchWrite( |
| 155 | List<WriteRequest> batch) { |
| 156 | List<OptionalResult<WriteResult, DatabaseException>> writeResults = new ArrayList<>(batch.size()); |
| 157 | for (InternalWriteResult internalWriteResult : client.batchWrite(batch)) { |
| 158 | if (internalWriteResult.status() == InternalWriteResult.Status.NO_SUCH_TABLE) { |
| 159 | writeResults.add(new DatabaseOperationResult<WriteResult, DatabaseException>( |
| 160 | new NoSuchTableException())); |
| 161 | } else if (internalWriteResult.status() == InternalWriteResult.Status.OPTIMISTIC_LOCK_FAILURE) { |
| 162 | writeResults.add(new DatabaseOperationResult<WriteResult, DatabaseException>( |
| 163 | new OptimisticLockException())); |
| 164 | } else if (internalWriteResult.status() == InternalWriteResult.Status.PREVIOUS_VALUE_MISMATCH) { |
| 165 | // TODO: throw a different exception? |
| 166 | writeResults.add(new DatabaseOperationResult<WriteResult, DatabaseException>( |
| 167 | new PreconditionFailedException())); |
| 168 | } else if (internalWriteResult.status() == InternalWriteResult.Status.ABORTED) { |
| 169 | writeResults.add(new DatabaseOperationResult<WriteResult, DatabaseException>( |
| 170 | new WriteAborted())); |
| 171 | } else { |
| 172 | writeResults.add(new DatabaseOperationResult<WriteResult, DatabaseException>( |
| 173 | internalWriteResult.result())); |
| 174 | } |
| 175 | } |
| 176 | return writeResults; |
| 177 | |
| 178 | } |
| 179 | |
| 180 | private class DatabaseOperationResult<R, E extends DatabaseException> implements OptionalResult<R, E> { |
| 181 | |
| 182 | private final R result; |
| 183 | private final DatabaseException exception; |
| 184 | |
| 185 | public DatabaseOperationResult(R result) { |
| 186 | this.result = result; |
| 187 | this.exception = null; |
| 188 | } |
| 189 | |
| 190 | public DatabaseOperationResult(DatabaseException exception) { |
| 191 | this.result = null; |
| 192 | this.exception = exception; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public R get() { |
| 197 | if (result != null) { |
| 198 | return result; |
| 199 | } |
| 200 | throw exception; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public boolean hasValidResult() { |
| 205 | return result != null; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public String toString() { |
| 210 | if (result != null) { |
| 211 | return result.toString(); |
| 212 | } else { |
| 213 | return exception.toString(); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |