Jonathan Hart
Committed by Gerrit Code Review

Implement command to view Raft partitions

Change-Id: I9d3cea49877d69c2d7935dadbbad2770349e793a
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.cli.net;
17 +
18 +import org.apache.karaf.shell.commands.Command;
19 +import org.onosproject.cli.AbstractShellCommand;
20 +import org.onosproject.store.service.PartitionInfo;
21 +import org.onosproject.store.service.StorageAdminService;
22 +
23 +import java.util.List;
24 +
25 +/**
26 + * Command to list the database partitions in the system.
27 + */
28 +@Command(scope = "onos", name = "partitions",
29 + description = "Lists information about partitions in the system")
30 +public class PartitionsListCommand extends AbstractShellCommand {
31 +
32 + private static final String FMT = "%-20s %8s %25s %s";
33 +
34 + @Override
35 + protected void execute() {
36 + StorageAdminService storageAdminService = get(StorageAdminService.class);
37 + List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
38 +
39 + print(FMT, "Name", "Term", "Members", "");
40 +
41 + for (PartitionInfo info : partitionInfo) {
42 + boolean first = true;
43 + for (String member : info.members()) {
44 + if (first) {
45 + print(FMT, info.name(), info.term(), member,
46 + member.equals(info.leader()) ? "*" : "");
47 + first = false;
48 + } else {
49 + print(FMT, "", "", member,
50 + member.equals(info.leader()) ? "*" : "");
51 + }
52 + }
53 + }
54 + }
55 +}
...@@ -244,6 +244,9 @@ ...@@ -244,6 +244,9 @@
244 <action class="org.onosproject.cli.net.ClustersListCommand"/> 244 <action class="org.onosproject.cli.net.ClustersListCommand"/>
245 </command> 245 </command>
246 <command> 246 <command>
247 + <action class="org.onosproject.cli.net.PartitionsListCommand"/>
248 + </command>
249 + <command>
247 <action class="org.onosproject.cli.net.ClusterDevicesCommand"/> 250 <action class="org.onosproject.cli.net.ClusterDevicesCommand"/>
248 <completers> 251 <completers>
249 <ref component-id="clusterIdCompleter"/> 252 <ref component-id="clusterIdCompleter"/>
......
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.store.service;
17 +
18 +import com.google.common.collect.ImmutableList;
19 +
20 +import java.util.List;
21 +
22 +/**
23 + * Contains information about a database partition.
24 + */
25 +public class PartitionInfo {
26 + private final String name;
27 + private final long term;
28 + private final List<String> members;
29 + private final String leader;
30 +
31 + /**
32 + * Class constructor.
33 + *
34 + * @param name partition name
35 + * @param term term number
36 + * @param members partition members
37 + * @param leader leader name
38 + */
39 + public PartitionInfo(String name, long term, List<String> members, String leader) {
40 + this.name = name;
41 + this.term = term;
42 + this.members = ImmutableList.copyOf(members);
43 + this.leader = leader;
44 + }
45 +
46 + /**
47 + * Returns the name of the partition.
48 + *
49 + * @return partition name
50 + */
51 + public String name() {
52 + return name;
53 + }
54 +
55 + /**
56 + * Returns the term number.
57 + *
58 + * @return term number
59 + */
60 + public long term() {
61 + return term;
62 + }
63 +
64 + /**
65 + * Returns the list of partition members.
66 + *
67 + * @return partition members
68 + */
69 + public List<String> members() {
70 + return members;
71 + }
72 +
73 + /**
74 + * Returns the partition leader.
75 + *
76 + * @return partition leader
77 + */
78 + public String leader() {
79 + return leader;
80 + }
81 +}
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.store.service;
17 +
18 +import java.util.List;
19 +
20 +/**
21 + * Service for administering storage instances.
22 + */
23 +public interface StorageAdminService {
24 +
25 + /**
26 + * Returns information about all partitions in the system.
27 + *
28 + * @return list of partition information
29 + */
30 + List<PartitionInfo> getPartitionInfo();
31 +}
...@@ -16,19 +16,12 @@ ...@@ -16,19 +16,12 @@
16 16
17 package org.onosproject.store.consistent.impl; 17 package org.onosproject.store.consistent.impl;
18 18
19 -import static org.slf4j.LoggerFactory.getLogger; 19 +import com.google.common.collect.Sets;
20 -
21 -import java.io.File;
22 -import java.io.IOException;
23 -import java.util.Map;
24 -import java.util.Set;
25 -import java.util.stream.Collectors;
26 -
27 import net.kuujo.copycat.cluster.ClusterConfig; 20 import net.kuujo.copycat.cluster.ClusterConfig;
21 +import net.kuujo.copycat.cluster.Member;
28 import net.kuujo.copycat.log.FileLog; 22 import net.kuujo.copycat.log.FileLog;
29 import net.kuujo.copycat.netty.NettyTcpProtocol; 23 import net.kuujo.copycat.netty.NettyTcpProtocol;
30 import net.kuujo.copycat.protocol.Consistency; 24 import net.kuujo.copycat.protocol.Consistency;
31 -
32 import org.apache.felix.scr.annotations.Activate; 25 import org.apache.felix.scr.annotations.Activate;
33 import org.apache.felix.scr.annotations.Component; 26 import org.apache.felix.scr.annotations.Component;
34 import org.apache.felix.scr.annotations.Deactivate; 27 import org.apache.felix.scr.annotations.Deactivate;
...@@ -39,19 +32,28 @@ import org.onosproject.cluster.ClusterService; ...@@ -39,19 +32,28 @@ import org.onosproject.cluster.ClusterService;
39 import org.onosproject.cluster.ControllerNode; 32 import org.onosproject.cluster.ControllerNode;
40 import org.onosproject.cluster.DefaultControllerNode; 33 import org.onosproject.cluster.DefaultControllerNode;
41 import org.onosproject.store.service.ConsistentMap; 34 import org.onosproject.store.service.ConsistentMap;
35 +import org.onosproject.store.service.PartitionInfo;
42 import org.onosproject.store.service.Serializer; 36 import org.onosproject.store.service.Serializer;
37 +import org.onosproject.store.service.StorageAdminService;
43 import org.onosproject.store.service.StorageService; 38 import org.onosproject.store.service.StorageService;
44 import org.onosproject.store.service.TransactionContext; 39 import org.onosproject.store.service.TransactionContext;
45 import org.slf4j.Logger; 40 import org.slf4j.Logger;
46 41
47 -import com.google.common.collect.Sets; 42 +import java.io.File;
43 +import java.io.IOException;
44 +import java.util.List;
45 +import java.util.Map;
46 +import java.util.Set;
47 +import java.util.stream.Collectors;
48 +
49 +import static org.slf4j.LoggerFactory.getLogger;
48 50
49 /** 51 /**
50 * Database manager. 52 * Database manager.
51 */ 53 */
52 @Component(immediate = true, enabled = true) 54 @Component(immediate = true, enabled = true)
53 @Service 55 @Service
54 -public class DatabaseManager implements StorageService { 56 +public class DatabaseManager implements StorageService, StorageAdminService {
55 57
56 private final Logger log = getLogger(getClass()); 58 private final Logger log = getLogger(getClass());
57 private PartitionedDatabase partitionedDatabase; 59 private PartitionedDatabase partitionedDatabase;
...@@ -160,4 +162,29 @@ public class DatabaseManager implements StorageService { ...@@ -160,4 +162,29 @@ public class DatabaseManager implements StorageService {
160 public TransactionContext createTransactionContext() { 162 public TransactionContext createTransactionContext() {
161 return new DefaultTransactionContext(partitionedDatabase); 163 return new DefaultTransactionContext(partitionedDatabase);
162 } 164 }
165 +
166 + @Override
167 + public List<PartitionInfo> getPartitionInfo() {
168 + return partitionedDatabase.getRegisteredPartitions()
169 + .values()
170 + .stream()
171 + .map(DatabaseManager::toPartitionInfo)
172 + .collect(Collectors.toList());
173 + }
174 +
175 + /**
176 + * Maps a Raft Database object to a PartitionInfo object.
177 + *
178 + * @param database database containing input data
179 + * @return PartitionInfo object
180 + */
181 + private static PartitionInfo toPartitionInfo(Database database) {
182 + return new PartitionInfo(database.name(),
183 + database.cluster().term(),
184 + database.cluster().members().stream()
185 + .map(Member::uri)
186 + .collect(Collectors.toList()),
187 + database.cluster().leader() != null ?
188 + database.cluster().leader().uri() : null);
189 + }
163 } 190 }
......