Skip to content
Draft
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 @@ -72,6 +72,8 @@
import org.apache.hadoop.ozone.recon.api.types.KeysResponse;
import org.apache.hadoop.ozone.recon.api.types.MissingContainerMetadata;
import org.apache.hadoop.ozone.recon.api.types.MissingContainersResponse;
import org.apache.hadoop.ozone.recon.api.types.DatanodeUnhealthyContainersResponse;
import org.apache.hadoop.ozone.recon.api.types.DatanodeUnhealthySummary;
import org.apache.hadoop.ozone.recon.api.types.UnhealthyContainerMetadata;
import org.apache.hadoop.ozone.recon.api.types.UnhealthyContainersResponse;
import org.apache.hadoop.ozone.recon.api.types.UnhealthyContainersSummary;
Expand Down Expand Up @@ -812,4 +814,144 @@ public Response getOmContainersDeletedInSCM(
response.put("containerDiscrepancyInfo", containerDiscrepancyInfoList);
return Response.ok(response).build();
}

/**
* Return a summary of unhealthy containers grouped by DataNode.
* For each DataNode, the response includes a breakdown of unhealthy
* container counts by state (MISSING, UNDER_REPLICATED, etc.).
*
* @param state Optional filter: only count containers in this state.
* @param limit Max number of unhealthy containers to scan for aggregation.
* @return {@link Response} containing a list of {@link DatanodeUnhealthySummary}.
*/
@GET
@Path("/unhealthy/byDatanode")
public Response getUnhealthyContainersByDatanode(
@QueryParam("state") String state,
@DefaultValue("0") @QueryParam(RECON_QUERY_LIMIT) int limit) {

ContainerSchemaDefinition.UnHealthyContainerStates containerState = null;
if (StringUtils.isNotEmpty(state)) {
try {
containerState = ContainerSchemaDefinition
.UnHealthyContainerStates.valueOf(state);
} catch (IllegalArgumentException e) {
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
}
}

final int effectiveLimit = (limit <= 0 || limit > maxCsvExportRecords)
? maxCsvExportRecords : limit;

// Fetch unhealthy containers from the DB
List<ContainerHealthSchemaManager.UnhealthyContainerRecord> records =
containerHealthSchemaManager.getUnhealthyContainers(
containerState, 0L, 0L, effectiveLimit);

// Build DataNode -> Summary map
Map<String, DatanodeUnhealthySummary> dnSummaryMap = new HashMap<>();

for (ContainerHealthSchemaManager.UnhealthyContainerRecord record : records) {
long containerID = record.getContainerId();
String containerStateStr = record.getContainerState();
try {
ContainerInfo containerInfo =
containerManager.getContainer(ContainerID.valueOf(containerID));
List<ContainerHistory> datanodes =
containerManager.getLatestContainerHistory(containerID,
containerInfo.getReplicationConfig().getRequiredNodes());

for (ContainerHistory dn : datanodes) {
String uuid = dn.getDatanodeUuid();
DatanodeUnhealthySummary summary = dnSummaryMap.computeIfAbsent(
uuid, k -> new DatanodeUnhealthySummary(uuid,
dn.getDatanodeHost()));
summary.incrementStateCount(containerStateStr);
}
} catch (IOException e) {
LOG.warn("Failed to get container info/history for container {}",
containerID, e);
}
}

// Sort by total unhealthy count descending
List<DatanodeUnhealthySummary> summaries = dnSummaryMap.values().stream()
.sorted(Comparator.comparingInt(
DatanodeUnhealthySummary::getTotalUnhealthyContainers).reversed())
.collect(Collectors.toList());

Map<String, Object> response = new HashMap<>();
response.put("datanodes", summaries);
return Response.ok(response).build();
}

/**
* Return unhealthy containers for a specific DataNode.
* The response includes the full container metadata for each unhealthy
* container whose replica history includes the given DataNode.
*
* @param datanodeUuid The UUID of the DataNode to query.
* @param state Optional filter by unhealthy state.
* @param limit Max number of containers to scan.
* @return {@link Response} containing {@link DatanodeUnhealthyContainersResponse}.
*/
@GET
@Path("/unhealthy/byDatanode/{uuid}")
public Response getUnhealthyContainersForDatanode(
@PathParam("uuid") String datanodeUuid,
@QueryParam("state") String state,
@DefaultValue(DEFAULT_FETCH_COUNT) @QueryParam(RECON_QUERY_LIMIT)
int limit) {

ContainerSchemaDefinition.UnHealthyContainerStates containerState = null;
if (StringUtils.isNotEmpty(state)) {
try {
containerState = ContainerSchemaDefinition
.UnHealthyContainerStates.valueOf(state);
} catch (IllegalArgumentException e) {
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
}
}

final int effectiveLimit = (limit <= 0 || limit > maxCsvExportRecords)
? maxCsvExportRecords : limit;

// Fetch all unhealthy containers
List<ContainerHealthSchemaManager.UnhealthyContainerRecord> records =
containerHealthSchemaManager.getUnhealthyContainers(
containerState, 0L, 0L, effectiveLimit);

// Filter to containers that have this DataNode in their replica history
List<UnhealthyContainerMetadata> matchingContainers = new ArrayList<>();
String datanodeHost = "N/A";

for (ContainerHealthSchemaManager.UnhealthyContainerRecord record : records) {
try {
UnhealthyContainerMetadata meta = toUnhealthyMetadata(record);
boolean hasDatanode = meta.getReplicas() != null
&& meta.getReplicas().stream()
.anyMatch(r -> datanodeUuid.equals(r.getDatanodeUuid()));

if (hasDatanode) {
matchingContainers.add(meta);
// Get the hostname from the first match
if ("N/A".equals(datanodeHost)) {
datanodeHost = meta.getReplicas().stream()
.filter(r -> datanodeUuid.equals(r.getDatanodeUuid()))
.map(ContainerHistory::getDatanodeHost)
.findFirst().orElse("N/A");
}
}
} catch (UncheckedIOException e) {
LOG.warn("Failed to get metadata for container {}",
record.getContainerId(), e);
}
}

DatanodeUnhealthyContainersResponse response =
new DatanodeUnhealthyContainersResponse(
datanodeUuid, datanodeHost,
matchingContainers.size(), matchingContainers);
return Response.ok(response).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.hadoop.ozone.recon.api.types;

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

/**
* Response object for the drill-down of unhealthy containers
* on a specific DataNode.
*/
@XmlAccessorType(XmlAccessType.FIELD)
public class DatanodeUnhealthyContainersResponse {

@XmlElement(name = "datanodeUuid")
private String datanodeUuid;

@XmlElement(name = "datanodeHost")
private String datanodeHost;

@XmlElement(name = "totalCount")
private int totalCount;

@XmlElement(name = "containers")
private List<UnhealthyContainerMetadata> containers;

public DatanodeUnhealthyContainersResponse() {
}

public DatanodeUnhealthyContainersResponse(
String datanodeUuid, String datanodeHost,
int totalCount, List<UnhealthyContainerMetadata> containers) {
this.datanodeUuid = datanodeUuid;
this.datanodeHost = datanodeHost;
this.totalCount = totalCount;
this.containers = containers;
}

public String getDatanodeUuid() {
return datanodeUuid;
}

public String getDatanodeHost() {
return datanodeHost;
}

public int getTotalCount() {
return totalCount;
}

public List<UnhealthyContainerMetadata> getContainers() {
return containers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.hadoop.ozone.recon.api.types;

import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

/**
* Summary of unhealthy containers for a specific DataNode.
*/
@XmlAccessorType(XmlAccessType.FIELD)
public class DatanodeUnhealthySummary {

@XmlElement(name = "datanodeUuid")
private String datanodeUuid;

@XmlElement(name = "datanodeHost")
private String datanodeHost;

@XmlElement(name = "totalUnhealthyContainers")
private int totalUnhealthyContainers;

@XmlElement(name = "stateCounts")
private Map<String, Integer> stateCounts;

public DatanodeUnhealthySummary() {
this.stateCounts = new HashMap<>();
}

public DatanodeUnhealthySummary(String datanodeUuid, String datanodeHost) {
this.datanodeUuid = datanodeUuid;
this.datanodeHost = datanodeHost;
this.totalUnhealthyContainers = 0;
this.stateCounts = new HashMap<>();
}

public void incrementStateCount(String state) {
stateCounts.merge(state, 1, Integer::sum);
totalUnhealthyContainers++;
}

public String getDatanodeUuid() {
return datanodeUuid;
}

public String getDatanodeHost() {
return datanodeHost;
}

public int getTotalUnhealthyContainers() {
return totalUnhealthyContainers;
}

public Map<String, Integer> getStateCounts() {
return stateCounts;
}
}
Loading