Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fsck display tree #92

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class CassandraAppStorage extends AbstractAppStorage {

public static final String ORPHAN_DATA = "ORPHAN_DATA";

public static final String DISPLAY_TREE = "DISPLAY_TREE";

private final String fileSystemName;

private final Supplier<CassandraContext> contextSupplier;
Expand Down Expand Up @@ -1488,7 +1490,7 @@ public void close() {
@Override
public List<String> getSupportedFileSystemChecks() {
return ImmutableList.of(FileSystemCheckOptions.EXPIRED_INCONSISTENT_NODES,
REF_NOT_FOUND, ORPHAN_NODE, ORPHAN_DATA);
REF_NOT_FOUND, ORPHAN_NODE, ORPHAN_DATA, DISPLAY_TREE);
}

@Override
Expand All @@ -1510,6 +1512,9 @@ public List<FileSystemCheckIssue> checkFileSystem(FileSystemCheckOptions options
case ORPHAN_DATA:
checkOrphanData(results, options);
break;
case DISPLAY_TREE:
displayTree(results);
break;
default:
LOGGER.warn("Check {} not supported in {}", type, getClass());
}
Expand All @@ -1518,6 +1523,11 @@ public List<FileSystemCheckIssue> checkFileSystem(FileSystemCheckOptions options
return results;
}

private void displayTree(List<FileSystemCheckIssue> results) {
ResultSet allTables = getSession().execute(select(ID, NAME, PSEUDO_CLASS, CHILD_ID, PARENT_ID).from(CHILDREN_BY_NAME_AND_CLASS));
results.add(new DisplayTreeIssueBuilder(allTables).build());
}

private void checkOrphanData(List<FileSystemCheckIssue> results, FileSystemCheckOptions options) {
Set<UUID> existingNodeIds = new HashSet<>();
Set<UUID> orphanDataIds = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Copyright (c) 2021, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.afs.cassandra;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.powsybl.afs.storage.check.FileSystemCheckIssue;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* @author Yichen TANG <yichen.tang at rte-france.com>
*/
class DisplayTreeIssueBuilder {

private final TreeNode root = new TreeNode("display tree");
private final Map<String, TreeNode> nodes = new HashMap<>();
private final Map<String, NodeInfo> infos = new HashMap<>();

DisplayTreeIssueBuilder(ResultSet resultSet) {
for (Row row : resultSet) {
RowBean rowBean = new RowBean(row.getUUID(0).toString(),
row.getString(1),
row.getString(2),
row.getUUID(3) == null ? null : row.getUUID(3).toString());
cacheNodeInfo(rowBean);
nodes.computeIfAbsent(rowBean.id, TreeNode::new);
if (rowBean.cId != null) {
nodes.computeIfAbsent(rowBean.cId, TreeNode::new);
nodes.get(rowBean.id).children.add(rowBean.cId);
}
if (row.getUUID(4) == null) {
root.children.add(rowBean.id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the tree is displayed multiple times because we don't check here if the node has already been added:
if the root node of the filesystem has multiple children, it will appear multiple times in the table (once for itself, once for each children), so we will go through that code multiple times

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the tree is displayed multiple times because we don't check here if the node has already been added:
if the root node of the filesystem has multiple children, it will appear multiple times in the table (once for itself, once for each children), so we will go through that code multiple times

Fixed by 'Set'

}
}
}

private void cacheNodeInfo(RowBean bean) {
if (!infos.containsKey(bean.id)) {
infos.put(bean.id, new NodeInfo(bean.name, bean.pseudoClass));
}
}

static class NodeInfo {

private final String name;
private final String pseudoClass;

NodeInfo(String name, String pseudoClass) {
this.name = name;
this.pseudoClass = pseudoClass;
}

@Override
public String toString() {
return name + "(" + pseudoClass + ")";
}
}

static class RowBean {

private final String id;
private final String name;
private final String pseudoClass;
private final String cId;

RowBean(String id, String name, String pseudoClass, String cId) {
this.id = id;
this.name = name;
this.pseudoClass = pseudoClass;
this.cId = cId;
}
}

static class TreeNode {
String id;
String name;
Set<String> children = new HashSet<>();

TreeNode(String id) {
this.id = id;
}

TreeNode(String id, String name) {
this.id = id;
this.name = name;
}

@Override
public String toString() {
return id + "_" + name;
}
}

FileSystemCheckIssue build() {
StringBuilder sb = new StringBuilder();
printNode(sb, root, 0);
return new FileSystemCheckIssue().setDescription(sb.toString());
}

void printNode(StringBuilder sb, TreeNode node, int level) {
for (int i = 0; i < level - 1; i++) {
sb.append(" ");
}
if (level != 0) {
sb.append(infos.get(node.id))
.append(" ")
.append(node.id)
.append("\n");
}
int nextLevel = level + 1;
node.children.forEach(e -> printNode(sb, nodes.get(e), nextLevel));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ void testSupportedChecks() {
.containsExactlyInAnyOrder(CassandraAppStorage.REF_NOT_FOUND,
FileSystemCheckOptions.EXPIRED_INCONSISTENT_NODES,
CassandraAppStorage.ORPHAN_NODE,
CassandraAppStorage.ORPHAN_DATA
CassandraAppStorage.ORPHAN_DATA,
CassandraAppStorage.DISPLAY_TREE
);
}

Expand Down