Skip to content

Commit

Permalink
Merge pull request #187 from tsegismont/jira/HWKMETRICS-44
Browse files Browse the repository at this point in the history
HWKMETRICS-44 Tenant list endpoint does not report internal server error correctly
  • Loading branch information
Stefan Negrea committed Mar 24, 2015
2 parents 5d7f0d1 + cc11b19 commit 4cbf4cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import java.net.URI;
import java.util.Collection;
import java.util.List;

import javax.inject.Inject;
Expand All @@ -35,12 +34,11 @@
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import org.hawkular.metrics.api.jaxrs.callback.SimpleDataCallback;
import org.hawkular.metrics.api.jaxrs.callback.TenantCreatedCallback;
import org.hawkular.metrics.core.api.MetricsService;
import org.hawkular.metrics.core.api.Tenant;

import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.wordnik.swagger.annotations.Api;
Expand Down Expand Up @@ -91,34 +89,16 @@ public void createTenant(
Futures.addCallback(insertFuture, tenantCreatedCallback);
}



@GET
@ApiOperation(value = "Returns a list of tenants.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Returned a list of tenants successfully."),
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Returned a list of tenants successfully."),
@ApiResponse(code = 204, message = "No tenants were found."),
@ApiResponse(code = 500, message = "Unexpected error occurred while fetching tenants.",
response = ApiError.class)
})
public void findTenants(@Suspended AsyncResponse response) {
public void findTenants(@Suspended AsyncResponse asyncResponse) {
ListenableFuture<List<Tenant>> tenantsFuture = metricsService.getTenants();
Futures.addCallback(tenantsFuture, new FutureCallback<Collection<Tenant>>() {
@Override
public void onSuccess(Collection<Tenant> tenants) {
if (tenants.isEmpty()) {
response.resume(Response.ok().status(Status.NO_CONTENT).build());
return;
}
response.resume(Response.status(Status.OK).entity(tenants).build());
}

@Override
public void onFailure(Throwable t) {
ApiError errors = new ApiError(
"Failed to fetch tenants due to an "
+ "unexpected error: " + Throwables.getRootCause(t).getMessage());
response.resume(Response.status(Status.INTERNAL_SERVER_ERROR).entity(errors).build());
}
});
Futures.addCallback(tenantsFuture, new SimpleDataCallback<>(asyncResponse));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class MetricsServiceCassandra implements MetricsService {

private static final AvailabilityMetricMapper AVAILABILITY_METRIC_MAPPER = new AvailabilityMetricMapper();

private static final TenantMapper TENANT_MAPPER = new TenantMapper();

private static class DataRetentionKey {
private final String tenantId;
private final MetricId metricId;
Expand Down Expand Up @@ -408,15 +410,30 @@ public ListenableFuture<Void> apply(ResultSet resultSet) {

@Override
public ListenableFuture<List<Tenant>> getTenants() {
TenantMapper mapper = new TenantMapper();
List<String> ids = loadTenantIds();
List<ListenableFuture<Tenant>> tenantFutures = new ArrayList<>(ids.size());
for (String id : ids) {
ResultSetFuture queryFuture = dataAccess.findTenant(id);
ListenableFuture<Tenant> tenantFuture = Futures.transform(queryFuture, mapper, metricsTasks);
tenantFutures.add(tenantFuture);
}
return Futures.allAsList(tenantFutures);
ListenableFuture<List<String>> tenantIdsFuture = Futures.transform(
dataAccess.findAllTenantIds(), (ResultSet input) -> {
if (input.isExhausted()) {
return null;
}
List<String> tenantIds = new ArrayList<>();
for (Row row : input) {
tenantIds.add(row.getString(0));
}
return tenantIds;
}, metricsTasks
);
return Futures.transform(
tenantIdsFuture, (List<String> input) -> {
if (input == null) {
return Futures.immediateFuture(null);
}
List<ListenableFuture<Tenant>> tenantFutures = new ArrayList<>();
for (String tenantId : input) {
tenantFutures.add(Futures.transform(dataAccess.findTenant(tenantId), TENANT_MAPPER));
}
return Futures.allAsList(tenantFutures);
}
);
}

private List<String> loadTenantIds() {
Expand Down

0 comments on commit 4cbf4cb

Please sign in to comment.