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