Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -35,18 +35,26 @@
description = "Finds containers from the database based on the option provided."
)
public class ListContainers extends AbstractSubcommand implements Callable<Void> {

@CommandLine.Option(names = {"--state"},
description = "Life cycle state of the container.",
required = true)
private HddsProtos.LifeCycleState state;

@CommandLine.ArgGroup(multiplicity = "1")
private ExclusiveOptions exclusiveOptions;

@CommandLine.Mixin
private ListOptions listOptions;

@CommandLine.ParentCommand
private ContainerLogController parent;

private static final class ExclusiveOptions {
@CommandLine.Option(names = {"--state"},
description = "Life cycle state of the container.")
private HddsProtos.LifeCycleState state;

@CommandLine.Option(names = {"--duplicate-open"},
description = "List all the containers which have duplicate open states.")
private boolean duplicateOpen;
}

@Override
public Void call() throws Exception {

Expand All @@ -57,7 +65,11 @@ public Void call() throws Exception {

ContainerDatanodeDatabase cdd = new ContainerDatanodeDatabase(dbPath.toString());

cdd.listContainersByState(state.name(), listOptions.getLimit());
if (exclusiveOptions.duplicateOpen) {
cdd.findDuplicateOpenContainer();
} else if (exclusiveOptions.state != null) {
cdd.listContainersByState(exclusiveOptions.state.name(), listOptions.getLimit());
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,69 @@ private List<DatanodeContainerInfo> getContainerLogData(Long containerID, Connec

return logEntries;
}

private void createIdxContainerlogContainerId(Connection conn) throws SQLException {
String sql = SQLDBConstants.CREATE_CONTAINER_ID_INDEX;
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
}
}

public void findDuplicateOpenContainer() throws SQLException {
String sql = SQLDBConstants.SELECT_DISTINCT_CONTAINER_IDS_QUERY;

try (Connection connection = getConnection()) {

createIdxContainerlogContainerId(connection);

try (PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery()) {
int count = 0;

while (resultSet.next()) {
Long containerID = resultSet.getLong("container_id");
List<DatanodeContainerInfo> logEntries = getContainerLogDataFoOpen(containerID, connection);
boolean hasIssue = checkForMultipleOpenStates(logEntries);
if (hasIssue) {
int openStateCount = (int) logEntries.stream()
.filter(entry -> "OPEN".equalsIgnoreCase(entry.getState()))
.count();
count++;
out.println("Container ID: " + containerID + " - OPEN state count: " + openStateCount);
}
}

out.println("Total containers that might have duplicate OPEN state : " + count);

}
} catch (SQLException e) {
throw new SQLException("Error while retrieving containers." + e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException("Unexpected error: " + e);
}
}

private List<DatanodeContainerInfo> getContainerLogDataFoOpen(Long containerID, Connection connection)
throws SQLException {
String query = SQLDBConstants.SELECT_CONTAINER_DETAILS_OPEN_STATE;
List<DatanodeContainerInfo> logEntries = new ArrayList<>();

try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setLong(1, containerID);
try (ResultSet rs = preparedStatement.executeQuery()) {
while (rs.next()) {
DatanodeContainerInfo entry = new DatanodeContainerInfo.Builder()
.setTimestamp(rs.getString("timestamp"))
.setContainerId(rs.getLong("container_id"))
.setDatanodeId(rs.getString("datanode_id"))
.setState(rs.getString("container_state"))
.build();
logEntries.add(entry);
}
}
}

return logEntries;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public final class SQLDBConstants {
"WHERE d.container_id = ? ORDER BY d.datanode_id ASC, d.timestamp ASC;";
public static final String CREATE_DCL_CONTAINER_STATE_TIME_INDEX = "CREATE INDEX IF NOT EXISTS " +
"idx_dcl_container_state_time ON DatanodeContainerLogTable(container_id, container_state, timestamp);";
public static final String CREATE_CONTAINER_ID_INDEX = "CREATE INDEX IF NOT EXISTS idx_containerlog_container_id " +
"ON ContainerLogTable(container_id);";
public static final String SELECT_DISTINCT_CONTAINER_IDS_QUERY =
"SELECT DISTINCT container_id FROM ContainerLogTable";
public static final String SELECT_CONTAINER_DETAILS_OPEN_STATE = "SELECT d.timestamp, d.container_id, " +
"d.datanode_id, d.container_state FROM DatanodeContainerLogTable d " +
"WHERE d.container_id = ? AND d.container_state = 'OPEN' ORDER BY d.timestamp ASC;";

private SQLDBConstants() {
//Never constructed
Expand Down