use of org.apache.hadoop.ozone.recon.api.types.ContainerMetadata in project ozone by apache.
the class TestContainerEndpoint method testGetContainers.
@Test
public void testGetContainers() {
Response response = containerEndpoint.getContainers(-1, 0L);
ContainersResponse responseObject = (ContainersResponse) response.getEntity();
ContainersResponse.ContainersResponseData data = responseObject.getContainersResponseData();
assertEquals(2, data.getTotalCount());
List<ContainerMetadata> containers = new ArrayList<>(data.getContainers());
Iterator<ContainerMetadata> iterator = containers.iterator();
ContainerMetadata containerMetadata = iterator.next();
assertEquals(1L, containerMetadata.getContainerID());
// Number of keys for CID:1 should be 3 because of two different versions
// of key_two stored in CID:1
assertEquals(3L, containerMetadata.getNumberOfKeys());
containerMetadata = iterator.next();
assertEquals(2L, containerMetadata.getContainerID());
assertEquals(2L, containerMetadata.getNumberOfKeys());
// test if limit works as expected
response = containerEndpoint.getContainers(1, 0L);
responseObject = (ContainersResponse) response.getEntity();
data = responseObject.getContainersResponseData();
containers = new ArrayList<>(data.getContainers());
assertEquals(1, containers.size());
assertEquals(2, data.getTotalCount());
}
use of org.apache.hadoop.ozone.recon.api.types.ContainerMetadata in project ozone by apache.
the class ContainerEndpoint method getContainers.
/**
* Return @{@link org.apache.hadoop.ozone.recon.api.types.ContainerMetadata}
* for the containers starting from the given "prev-key" query param for the
* given "limit". The given "prev-key" is skipped from the results returned.
*
* @param limit max no. of containers to get.
* @param prevKey the containerID after which results are returned.
* @return {@link Response}
*/
@GET
public Response getContainers(@DefaultValue(DEFAULT_FETCH_COUNT) @QueryParam(RECON_QUERY_LIMIT) int limit, @DefaultValue(PREV_CONTAINER_ID_DEFAULT_VALUE) @QueryParam(RECON_QUERY_PREVKEY) long prevKey) {
Map<Long, ContainerMetadata> containersMap;
long containersCount;
try {
containersMap = reconContainerMetadataManager.getContainers(limit, prevKey);
containersCount = reconContainerMetadataManager.getCountForContainers();
} catch (IOException ioEx) {
throw new WebApplicationException(ioEx, Response.Status.INTERNAL_SERVER_ERROR);
}
ContainersResponse containersResponse = new ContainersResponse(containersCount, containersMap.values());
return Response.ok(containersResponse).build();
}
use of org.apache.hadoop.ozone.recon.api.types.ContainerMetadata in project ozone by apache.
the class TestContainerEndpoint method testGetContainersWithPrevKey.
@Test
public void testGetContainersWithPrevKey() {
Response response = containerEndpoint.getContainers(1, 1L);
ContainersResponse responseObject = (ContainersResponse) response.getEntity();
ContainersResponse.ContainersResponseData data = responseObject.getContainersResponseData();
assertEquals(2, data.getTotalCount());
List<ContainerMetadata> containers = new ArrayList<>(data.getContainers());
Iterator<ContainerMetadata> iterator = containers.iterator();
ContainerMetadata containerMetadata = iterator.next();
assertEquals(1, containers.size());
assertEquals(2L, containerMetadata.getContainerID());
response = containerEndpoint.getContainers(-1, 0L);
responseObject = (ContainersResponse) response.getEntity();
data = responseObject.getContainersResponseData();
containers = new ArrayList<>(data.getContainers());
assertEquals(2, containers.size());
assertEquals(2, data.getTotalCount());
iterator = containers.iterator();
containerMetadata = iterator.next();
assertEquals(1L, containerMetadata.getContainerID());
// test for negative cases
response = containerEndpoint.getContainers(-1, 5L);
responseObject = (ContainersResponse) response.getEntity();
data = responseObject.getContainersResponseData();
containers = new ArrayList<>(data.getContainers());
assertEquals(0, containers.size());
assertEquals(2, data.getTotalCount());
response = containerEndpoint.getContainers(-1, -1L);
responseObject = (ContainersResponse) response.getEntity();
data = responseObject.getContainersResponseData();
containers = new ArrayList<>(data.getContainers());
assertEquals(2, containers.size());
assertEquals(2, data.getTotalCount());
}
use of org.apache.hadoop.ozone.recon.api.types.ContainerMetadata in project ozone by apache.
the class ReconContainerMetadataManagerImpl method getContainers.
/**
* Iterate the DB to construct a Map of containerID -> containerMetadata
* only for the given limit from the given start key. The start containerID
* is skipped from the result.
*
* Return all the containers if limit < 0.
*
* @param limit No of containers to get.
* @param prevContainer containerID after which the
* list of containers are scanned.
* @return Map of containerID -> containerMetadata.
* @throws IOException on failure.
*/
@Override
public Map<Long, ContainerMetadata> getContainers(int limit, long prevContainer) throws IOException {
Map<Long, ContainerMetadata> containers = new LinkedHashMap<>();
TableIterator<ContainerKeyPrefix, ? extends KeyValue<ContainerKeyPrefix, Integer>> containerIterator = containerKeyTable.iterator();
ContainerKeyPrefix seekKey;
if (prevContainer > 0L) {
seekKey = new ContainerKeyPrefix(prevContainer);
KeyValue<ContainerKeyPrefix, Integer> seekKeyValue = containerIterator.seek(seekKey);
// prevContainer containerId. If not, then return empty result
if (seekKeyValue != null && seekKeyValue.getKey().getContainerId() != prevContainer) {
return containers;
} else {
// seek to the prevContainer+1 containerID to start scan
seekKey = new ContainerKeyPrefix(prevContainer + 1);
containerIterator.seek(seekKey);
}
}
while (containerIterator.hasNext()) {
KeyValue<ContainerKeyPrefix, Integer> keyValue = containerIterator.next();
ContainerKeyPrefix containerKeyPrefix = keyValue.getKey();
Long containerID = containerKeyPrefix.getContainerId();
Integer numberOfKeys = keyValue.getValue();
// and one more new entity needs to be added to the containers map
if (containers.size() == limit && !containers.containsKey(containerID)) {
break;
}
// initialize containerMetadata with 0 as number of keys.
containers.computeIfAbsent(containerID, ContainerMetadata::new);
// increment number of keys for the containerID
ContainerMetadata containerMetadata = containers.get(containerID);
containerMetadata.setNumberOfKeys(containerMetadata.getNumberOfKeys() + numberOfKeys);
containers.put(containerID, containerMetadata);
}
return containers;
}
Aggregations