Search in sources :

Example 1 with QueryProfileStoreContext

use of org.apache.drill.exec.server.QueryProfileStoreContext in project drill by apache.

the class ProfileResources method getProfilesJSON.

@GET
@Path("/profiles.json")
@Produces(MediaType.APPLICATION_JSON)
public QProfiles getProfilesJSON(@Context UriInfo uriInfo) {
    try {
        final QueryProfileStoreContext profileStoreContext = work.getContext().getProfileStoreContext();
        final PersistentStore<QueryProfile> completed = profileStoreContext.getCompletedProfileStore();
        final TransientStore<QueryInfo> running = profileStoreContext.getRunningProfileStore();
        final List<String> errors = Lists.newArrayList();
        final List<ProfileInfo> runningQueries = Lists.newArrayList();
        final Iterator<Map.Entry<String, QueryInfo>> runningEntries = running.entries();
        while (runningEntries.hasNext()) {
            try {
                final Map.Entry<String, QueryInfo> runningEntry = runningEntries.next();
                final QueryInfo profile = runningEntry.getValue();
                if (principal.canManageProfileOf(profile.getUser())) {
                    runningQueries.add(new ProfileInfo(runningEntry.getKey(), profile.getStart(), System.currentTimeMillis(), profile.getForeman().getAddress(), profile.getQuery(), profile.getState().name(), profile.getUser()));
                }
            } catch (Exception e) {
                errors.add(e.getMessage());
                logger.error("Error getting running query info.", e);
            }
        }
        Collections.sort(runningQueries, Collections.reverseOrder());
        final List<ProfileInfo> finishedQueries = Lists.newArrayList();
        //Defining #Profiles to load
        int maxProfilesToLoad = work.getContext().getConfig().getInt(ExecConstants.HTTP_MAX_PROFILES);
        String maxProfilesParams = uriInfo.getQueryParameters().getFirst(MAX_QPROFILES_PARAM);
        if (maxProfilesParams != null && !maxProfilesParams.isEmpty()) {
            maxProfilesToLoad = Integer.valueOf(maxProfilesParams);
        }
        final Iterator<Map.Entry<String, QueryProfile>> range = completed.getRange(0, maxProfilesToLoad);
        while (range.hasNext()) {
            try {
                final Map.Entry<String, QueryProfile> profileEntry = range.next();
                final QueryProfile profile = profileEntry.getValue();
                if (principal.canManageProfileOf(profile.getUser())) {
                    finishedQueries.add(new ProfileInfo(profileEntry.getKey(), profile.getStart(), profile.getEnd(), profile.getForeman().getAddress(), profile.getQuery(), profile.getState().name(), profile.getUser()));
                }
            } catch (Exception e) {
                errors.add(e.getMessage());
                logger.error("Error getting finished query profile.", e);
            }
        }
        Collections.sort(finishedQueries, Collections.reverseOrder());
        return new QProfiles(runningQueries, finishedQueries, errors);
    } catch (Exception e) {
        throw UserException.resourceError(e).message("Failed to get profiles from persistent or ephemeral store.").build(logger);
    }
}
Also used : QueryProfile(org.apache.drill.exec.proto.UserBitShared.QueryProfile) QueryInfo(org.apache.drill.exec.proto.UserBitShared.QueryInfo) UserException(org.apache.drill.common.exceptions.UserException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) QueryProfileStoreContext(org.apache.drill.exec.server.QueryProfileStoreContext) Map(java.util.Map) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Map (java.util.Map)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)1 UserException (org.apache.drill.common.exceptions.UserException)1 QueryInfo (org.apache.drill.exec.proto.UserBitShared.QueryInfo)1 QueryProfile (org.apache.drill.exec.proto.UserBitShared.QueryProfile)1 QueryProfileStoreContext (org.apache.drill.exec.server.QueryProfileStoreContext)1