Search in sources :

Example 1 with XLogStreamer

use of org.apache.oozie.util.XLogStreamer in project oozie by apache.

the class TestZKXLogStreamingService method doStreamLog.

protected String doStreamLog(XLogFilter xf, Map<String, String[]> param, boolean isErrorLog, Date startTime, Date endTime) throws Exception {
    StringWriter w = new StringWriter();
    ZKXLogStreamingService zkxlss = new ZKXLogStreamingService();
    try {
        Services services = Services.get();
        services.setService(ZKJobsConcurrencyService.class);
        zkxlss.init(services);
        // Sleep to allow ZKUtils ServiceCache to update
        sleep(1000);
        if (isErrorLog) {
            zkxlss.streamLog(new XLogErrorStreamer(xf, param), startTime, endTime, w);
        } else {
            zkxlss.streamLog(new XLogStreamer(xf, param), startTime, endTime, w);
        }
    } finally {
        zkxlss.destroy();
    }
    String wStr = w.toString();
    System.out.println("\ndoStreamLog:\n" + wStr + "\n");
    return wStr;
}
Also used : StringWriter(java.io.StringWriter) XLogErrorStreamer(org.apache.oozie.util.XLogErrorStreamer) XLogStreamer(org.apache.oozie.util.XLogStreamer)

Example 2 with XLogStreamer

use of org.apache.oozie.util.XLogStreamer in project oozie by apache.

the class CoordinatorEngine method streamLog.

/**
 * Add list of actions to the filter based on conditions
 *
 * @param jobId Job Id
 * @param logRetrievalScope Value for the retrieval type
 * @param logRetrievalType Based on which filter criteria the log is retrieved
 * @param writer writer to stream the log to
 * @param requestParameters additional parameters from the request
 * @throws IOException in case of IO error
 * @throws BaseEngineException if there is an error during streaming
 * @throws CommandException if a parameter could not be parsed
 */
public void streamLog(String jobId, String logRetrievalScope, String logRetrievalType, Writer writer, Map<String, String[]> requestParameters) throws IOException, BaseEngineException, CommandException {
    Date startTime = null;
    Date endTime = null;
    XLogFilter filter = new XLogFilter(new XLogUserFilterParam(requestParameters));
    filter.setParameter(DagXLogInfoService.JOB, jobId);
    if (logRetrievalScope != null && logRetrievalType != null) {
        // if coordinator action logs are to be retrieved based on action id range
        if (logRetrievalType.equals(RestConstants.JOB_LOG_ACTION)) {
            // Use set implementation that maintains order or elements to achieve reproducibility:
            Set<String> actionSet = new LinkedHashSet<String>();
            String[] list = logRetrievalScope.split(",");
            for (String s : list) {
                s = s.trim();
                if (s.contains("-")) {
                    String[] range = s.split("-");
                    if (range.length != 2) {
                        throw new CommandException(ErrorCode.E0302, "format is wrong for action's range '" + s + "'");
                    }
                    int start;
                    int end;
                    try {
                        start = Integer.parseInt(range[0].trim());
                    } catch (NumberFormatException ne) {
                        throw new CommandException(ErrorCode.E0302, "could not parse " + range[0].trim() + "into an integer", ne);
                    }
                    try {
                        end = Integer.parseInt(range[1].trim());
                    } catch (NumberFormatException ne) {
                        throw new CommandException(ErrorCode.E0302, "could not parse " + range[1].trim() + "into an integer", ne);
                    }
                    if (start > end) {
                        throw new CommandException(ErrorCode.E0302, "format is wrong for action's range '" + s + "'");
                    }
                    for (int i = start; i <= end; i++) {
                        actionSet.add(jobId + "@" + i);
                    }
                } else {
                    try {
                        Integer.parseInt(s);
                    } catch (NumberFormatException ne) {
                        throw new CommandException(ErrorCode.E0302, "format is wrong for action id'" + s + "'. Integer only.");
                    }
                    actionSet.add(jobId + "@" + s);
                }
            }
            if (actionSet.size() >= maxNumActionsForLog) {
                throw new CommandException(ErrorCode.E0302, "Retrieving log of too many coordinator actions. Max count is " + maxNumActionsForLog + " actions");
            }
            Iterator<String> actionsIterator = actionSet.iterator();
            StringBuilder orSeparatedActions = new StringBuilder("");
            boolean orRequired = false;
            while (actionsIterator.hasNext()) {
                if (orRequired) {
                    orSeparatedActions.append("|");
                }
                orSeparatedActions.append(actionsIterator.next().toString());
                orRequired = true;
            }
            if (actionSet.size() > 1 && orRequired) {
                orSeparatedActions.insert(0, "(");
                orSeparatedActions.append(")");
            }
            filter.setParameter(DagXLogInfoService.ACTION, orSeparatedActions.toString());
            if (actionSet != null && actionSet.size() == 1) {
                CoordinatorActionBean actionBean = getCoordAction(actionSet.iterator().next());
                startTime = actionBean.getCreatedTime();
                endTime = actionBean.getStatus().equals(CoordinatorAction.Status.RUNNING) ? new Date() : actionBean.getLastModifiedTime();
                filter.setActionList(true);
            } else if (actionSet != null && actionSet.size() > 0) {
                List<String> tempList = new ArrayList<String>(actionSet);
                Collections.sort(tempList, new Comparator<String>() {

                    public int compare(String a, String b) {
                        return Integer.valueOf(a.substring(a.lastIndexOf("@") + 1)).compareTo(Integer.valueOf(b.substring(b.lastIndexOf("@") + 1)));
                    }
                });
                startTime = getCoordAction(tempList.get(0)).getCreatedTime();
                endTime = CoordActionsInDateRange.getCoordActionsLastModifiedDate(jobId, tempList.get(0), tempList.get(tempList.size() - 1));
                filter.setActionList(true);
            }
        }
        // this block gets the corresponding list of coordinator actions to be used by the log filter
        if (logRetrievalType.equalsIgnoreCase(RestConstants.JOB_LOG_DATE)) {
            List<String> coordActionIdList = null;
            try {
                coordActionIdList = CoordActionsInDateRange.getCoordActionIdsFromDates(jobId, logRetrievalScope);
            } catch (XException xe) {
                throw new CommandException(ErrorCode.E0302, "Error in date range for coordinator actions", xe);
            }
            if (coordActionIdList.size() >= maxNumActionsForLog) {
                throw new CommandException(ErrorCode.E0302, "Retrieving log of too many coordinator actions. Max count is " + maxNumActionsForLog + " actions");
            }
            StringBuilder orSeparatedActions = new StringBuilder("");
            boolean orRequired = false;
            for (String coordActionId : coordActionIdList) {
                if (orRequired) {
                    orSeparatedActions.append("|");
                }
                orSeparatedActions.append(coordActionId);
                orRequired = true;
            }
            if (coordActionIdList.size() > 1 && orRequired) {
                orSeparatedActions.insert(0, "(");
                orSeparatedActions.append(")");
            }
            filter.setParameter(DagXLogInfoService.ACTION, orSeparatedActions.toString());
            if (coordActionIdList != null && coordActionIdList.size() == 1) {
                CoordinatorActionBean actionBean = getCoordAction(coordActionIdList.get(0));
                startTime = actionBean.getCreatedTime();
                endTime = actionBean.getStatus().equals(CoordinatorAction.Status.RUNNING) ? new Date() : actionBean.getLastModifiedTime();
                filter.setActionList(true);
            } else if (coordActionIdList != null && coordActionIdList.size() > 0) {
                Collections.sort(coordActionIdList, new Comparator<String>() {

                    public int compare(String a, String b) {
                        return Integer.valueOf(a.substring(a.lastIndexOf("@") + 1)).compareTo(Integer.valueOf(b.substring(b.lastIndexOf("@") + 1)));
                    }
                });
                startTime = getCoordAction(coordActionIdList.get(0)).getCreatedTime();
                endTime = CoordActionsInDateRange.getCoordActionsLastModifiedDate(jobId, coordActionIdList.get(0), coordActionIdList.get(coordActionIdList.size() - 1));
                filter.setActionList(true);
            }
        }
    }
    if (startTime == null || endTime == null) {
        CoordinatorJobBean job = getCoordJobWithNoActionInfo(jobId);
        if (startTime == null) {
            startTime = job.getCreatedTime();
        }
        if (endTime == null) {
            if (job.isTerminalStatus()) {
                endTime = job.getLastModifiedTime();
            }
            if (endTime == null) {
                endTime = new Date();
            }
        }
    }
    Services.get().get(XLogStreamingService.class).streamLog(new XLogStreamer(filter, requestParameters), startTime, endTime, writer);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XLogFilter(org.apache.oozie.util.XLogFilter) XLogStreamer(org.apache.oozie.util.XLogStreamer) CommandException(org.apache.oozie.command.CommandException) Date(java.util.Date) Comparator(java.util.Comparator) XLogStreamingService(org.apache.oozie.service.XLogStreamingService) List(java.util.List) ArrayList(java.util.ArrayList) XLogUserFilterParam(org.apache.oozie.util.XLogUserFilterParam)

Aggregations

XLogStreamer (org.apache.oozie.util.XLogStreamer)2 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 CommandException (org.apache.oozie.command.CommandException)1 XLogStreamingService (org.apache.oozie.service.XLogStreamingService)1 XLogErrorStreamer (org.apache.oozie.util.XLogErrorStreamer)1 XLogFilter (org.apache.oozie.util.XLogFilter)1 XLogUserFilterParam (org.apache.oozie.util.XLogUserFilterParam)1