Skip to content

Commit

Permalink
Solve #1508, Define interface to get historical data for resources (#…
Browse files Browse the repository at this point in the history
…1509)
  • Loading branch information
littlezhou committed Jan 30, 2018
1 parent 4a0efcf commit 8dca82d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
12 changes: 11 additions & 1 deletion smart-common/src/main/java/org/smartdata/model/Utilization.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@
package org.smartdata.model;

public class Utilization {
private long timeStamp;
private long total;
private long used;

public Utilization(long total, long used) {
public Utilization(long timeStamp, long total, long used) {
this.timeStamp = timeStamp;
this.total = total;
this.used = used;
}

public long getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}

public long getTotal() {
return total;
}
Expand Down
24 changes: 24 additions & 0 deletions smart-engine/src/main/java/org/smartdata/server/SmartEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class SmartEngine extends AbstractService {
private ConfManager confMgr;
Expand Down Expand Up @@ -141,4 +143,26 @@ public CmdletManager getCmdletManager() {
public Utilization getUtilization(String resourceName) throws IOException {
return getStatesManager().getStorageUtilization(resourceName);
}

public List<Utilization> getHistUtilization(String resourceName, long granularity,
long begin, long end) throws IOException {
long now = System.currentTimeMillis();
if (begin == end && Math.abs(begin - now) <= 5) {
return Arrays.asList(getUtilization(resourceName));
}

// fake data
List<Utilization> utils = new ArrayList<>();
long ts = end;
if (ts % granularity != 0) {
ts += granularity;
ts = (ts / granularity) * granularity;
}
Random rand = new Random(ts);

for (; ts <= end; ts += granularity) {
utils.add(new Utilization(ts, 100, rand.nextInt(100)));
}
return utils;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,15 @@ public List<CachedFileStatus> getCachedFileStatus() throws IOException {

public Utilization getStorageUtilization(String resourceName) throws IOException {
try {
long now = System.currentTimeMillis();
if (!resourceName.equals("cache")) {
long capacity =
serverContext.getMetaStore().getStoreCapacityOfDifferentStorageType(resourceName);
long free = serverContext.getMetaStore().getStoreFreeOfDifferentStorageType(resourceName);
return new Utilization(capacity, capacity - free);
return new Utilization(now, capacity, capacity - free);
} else {
StorageCapacity storageCapacity = serverContext.getMetaStore().getStorageCapacity("cache");
return new Utilization(
return new Utilization(now,
storageCapacity.getCapacity(),
storageCapacity.getCapacity() - storageCapacity.getFree());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.util.List;

/**
Expand Down Expand Up @@ -105,6 +106,48 @@ public Response utilization(@PathParam("resourceName") String resourceName) {
}
}

/**
*
* @param resourceName
* @param timeGranularity Time interval of successive data points in milliseconds
* @param beginTs Begin timestamp in milliseconds. If <=0 denotes the value related to 'endTs'
* @param endTs Like 'beginTs'. If <= 0 denotes the time related to current server time.
* @return
*/
@GET
@Path("/primary/hist_utilization/{resourceName}/{timeGranularity}/{beginTs}/{endTs}")
public Response utilization(@PathParam("resourceName") String resourceName,
@PathParam("timeGranularity") String timeGranularity,
@PathParam("beginTs") String beginTs,
@PathParam("endTs") String endTs) {
try {
long now = System.currentTimeMillis();
long granularity = Long.valueOf(timeGranularity);
long tsEnd = Long.valueOf(endTs);
long tsBegin = Long.valueOf(beginTs);
if (granularity == 0) {
granularity = 1;
}
if (tsEnd <= 0) {
tsEnd += now;
}
if (tsBegin <= 0) {
tsBegin += tsEnd;
}
if (tsBegin > tsEnd) {
return new JsonResponse<>(Status.BAD_REQUEST, "Invalid time range").build();
}

return new JsonResponse<>(Response.Status.OK,
smartEngine.getHistUtilization(resourceName, granularity, tsBegin, tsEnd)).build();
} catch (Exception e) {
logger.error("Exception in ClusterRestApi while getting [" + resourceName
+ "] utilization", e);
return new JsonResponse<>(Response.Status.INTERNAL_SERVER_ERROR,
e.getMessage(), ExceptionUtils.getStackTrace(e)).build();
}
}

@GET
@Path("/primary/fileinfo")
public Response fileInfo(String path) {
Expand Down

0 comments on commit 8dca82d

Please sign in to comment.