From 054da97e83206dc18c2e811a28f81b4ab1ec9f19 Mon Sep 17 00:00:00 2001 From: Jonathan Hart Date: Wed, 18 Feb 2015 17:30:28 -0800 Subject: [PATCH] Implement command to view Raft partitions Change-Id: I9d3cea49877d69c2d7935dadbbad2770349e793a --- .../cli/net/PartitionsListCommand.java | 55 +++++++++++++ .../OSGI-INF/blueprint/shell-config.xml | 3 + .../store/service/PartitionInfo.java | 81 +++++++++++++++++++ .../store/service/StorageAdminService.java | 31 +++++++ .../consistent/impl/DatabaseManager.java | 51 +++++++++--- 5 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 cli/src/main/java/org/onosproject/cli/net/PartitionsListCommand.java create mode 100644 core/api/src/main/java/org/onosproject/store/service/PartitionInfo.java create mode 100644 core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java diff --git a/cli/src/main/java/org/onosproject/cli/net/PartitionsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/PartitionsListCommand.java new file mode 100644 index 0000000000..955bbc2005 --- /dev/null +++ b/cli/src/main/java/org/onosproject/cli/net/PartitionsListCommand.java @@ -0,0 +1,55 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.cli.net; + +import org.apache.karaf.shell.commands.Command; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.store.service.PartitionInfo; +import org.onosproject.store.service.StorageAdminService; + +import java.util.List; + +/** + * Command to list the database partitions in the system. + */ +@Command(scope = "onos", name = "partitions", + description = "Lists information about partitions in the system") +public class PartitionsListCommand extends AbstractShellCommand { + + private static final String FMT = "%-20s %8s %25s %s"; + + @Override + protected void execute() { + StorageAdminService storageAdminService = get(StorageAdminService.class); + List partitionInfo = storageAdminService.getPartitionInfo(); + + print(FMT, "Name", "Term", "Members", ""); + + for (PartitionInfo info : partitionInfo) { + boolean first = true; + for (String member : info.members()) { + if (first) { + print(FMT, info.name(), info.term(), member, + member.equals(info.leader()) ? "*" : ""); + first = false; + } else { + print(FMT, "", "", member, + member.equals(info.leader()) ? "*" : ""); + } + } + } + } +} diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml index 6102aa08e5..c772785c33 100644 --- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml +++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml @@ -243,6 +243,9 @@ + + + diff --git a/core/api/src/main/java/org/onosproject/store/service/PartitionInfo.java b/core/api/src/main/java/org/onosproject/store/service/PartitionInfo.java new file mode 100644 index 0000000000..a0f06481d1 --- /dev/null +++ b/core/api/src/main/java/org/onosproject/store/service/PartitionInfo.java @@ -0,0 +1,81 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.service; + +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** + * Contains information about a database partition. + */ +public class PartitionInfo { + private final String name; + private final long term; + private final List members; + private final String leader; + + /** + * Class constructor. + * + * @param name partition name + * @param term term number + * @param members partition members + * @param leader leader name + */ + public PartitionInfo(String name, long term, List members, String leader) { + this.name = name; + this.term = term; + this.members = ImmutableList.copyOf(members); + this.leader = leader; + } + + /** + * Returns the name of the partition. + * + * @return partition name + */ + public String name() { + return name; + } + + /** + * Returns the term number. + * + * @return term number + */ + public long term() { + return term; + } + + /** + * Returns the list of partition members. + * + * @return partition members + */ + public List members() { + return members; + } + + /** + * Returns the partition leader. + * + * @return partition leader + */ + public String leader() { + return leader; + } +} diff --git a/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java b/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java new file mode 100644 index 0000000000..427b95361c --- /dev/null +++ b/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java @@ -0,0 +1,31 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.service; + +import java.util.List; + +/** + * Service for administering storage instances. + */ +public interface StorageAdminService { + + /** + * Returns information about all partitions in the system. + * + * @return list of partition information + */ + List getPartitionInfo(); +} diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java index 1a2758b9f4..354068ba4d 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java +++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java @@ -16,19 +16,12 @@ package org.onosproject.store.consistent.impl; -import static org.slf4j.LoggerFactory.getLogger; - -import java.io.File; -import java.io.IOException; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - +import com.google.common.collect.Sets; import net.kuujo.copycat.cluster.ClusterConfig; +import net.kuujo.copycat.cluster.Member; import net.kuujo.copycat.log.FileLog; import net.kuujo.copycat.netty.NettyTcpProtocol; import net.kuujo.copycat.protocol.Consistency; - import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; @@ -39,19 +32,28 @@ import org.onosproject.cluster.ClusterService; import org.onosproject.cluster.ControllerNode; import org.onosproject.cluster.DefaultControllerNode; import org.onosproject.store.service.ConsistentMap; +import org.onosproject.store.service.PartitionInfo; import org.onosproject.store.service.Serializer; +import org.onosproject.store.service.StorageAdminService; import org.onosproject.store.service.StorageService; import org.onosproject.store.service.TransactionContext; import org.slf4j.Logger; -import com.google.common.collect.Sets; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.slf4j.LoggerFactory.getLogger; /** * Database manager. */ @Component(immediate = true, enabled = true) @Service -public class DatabaseManager implements StorageService { +public class DatabaseManager implements StorageService, StorageAdminService { private final Logger log = getLogger(getClass()); private PartitionedDatabase partitionedDatabase; @@ -160,4 +162,29 @@ public class DatabaseManager implements StorageService { public TransactionContext createTransactionContext() { return new DefaultTransactionContext(partitionedDatabase); } -} \ No newline at end of file + + @Override + public List getPartitionInfo() { + return partitionedDatabase.getRegisteredPartitions() + .values() + .stream() + .map(DatabaseManager::toPartitionInfo) + .collect(Collectors.toList()); + } + + /** + * Maps a Raft Database object to a PartitionInfo object. + * + * @param database database containing input data + * @return PartitionInfo object + */ + private static PartitionInfo toPartitionInfo(Database database) { + return new PartitionInfo(database.name(), + database.cluster().term(), + database.cluster().members().stream() + .map(Member::uri) + .collect(Collectors.toList()), + database.cluster().leader() != null ? + database.cluster().leader().uri() : null); + } +}