Search in sources :

Example 16 with XLog

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

the class CoordELFunctions method coord_futureRange_sync.

private static String coord_futureRange_sync(int startOffset, int endOffset, int instance) throws Exception {
    final XLog LOG = XLog.getLog(CoordELFunctions.class);
    final Thread currentThread = Thread.currentThread();
    ELEvaluator eval = ELEvaluator.getCurrent();
    String retVal = "";
    // in minutes
    int datasetFrequency = (int) getDSFrequency();
    TimeUnit dsTimeUnit = getDSTimeUnit();
    int[] instCount = new int[1];
    Calendar nominalInstanceCal = getCurrentInstance(getActionCreationtime(), instCount);
    StringBuilder resolvedInstances = new StringBuilder();
    StringBuilder resolvedURIPaths = new StringBuilder();
    if (nominalInstanceCal != null) {
        Calendar initInstance = getInitialInstanceCal();
        nominalInstanceCal = (Calendar) initInstance.clone();
        nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), instCount[0] * datasetFrequency);
        SyncCoordDataset ds = (SyncCoordDataset) eval.getVariable(DATASET);
        if (ds == null) {
            throw new RuntimeException("Associated Dataset should be defined with key " + DATASET);
        }
        String uriTemplate = ds.getUriTemplate();
        Configuration conf = (Configuration) eval.getVariable(CONFIGURATION);
        if (conf == null) {
            throw new RuntimeException("Associated Configuration should be defined with key " + CONFIGURATION);
        }
        int available = 0, checkedInstance = 0;
        boolean resolved = false;
        String user = ParamChecker.notEmpty((String) eval.getVariable(OozieClient.USER_NAME), OozieClient.USER_NAME);
        String doneFlag = ds.getDoneFlag();
        URIHandlerService uriService = Services.get().get(URIHandlerService.class);
        URIHandler uriHandler = null;
        Context uriContext = null;
        try {
            while (instance >= checkedInstance && !currentThread.isInterrupted()) {
                ELEvaluator uriEval = getUriEvaluator(nominalInstanceCal);
                String uriPath = uriEval.evaluate(uriTemplate, String.class);
                if (uriHandler == null) {
                    URI uri = new URI(uriPath);
                    uriHandler = uriService.getURIHandler(uri);
                    uriContext = uriHandler.getContext(uri, conf, user, true);
                }
                String uriWithDoneFlag = uriHandler.getURIWithDoneFlag(uriPath, doneFlag);
                if (uriHandler.exists(new URI(uriWithDoneFlag), uriContext)) {
                    if (available == endOffset) {
                        LOG.debug("Matched future(" + available + "): " + uriWithDoneFlag);
                        resolved = true;
                        resolvedInstances.append(DateUtils.formatDateOozieTZ(nominalInstanceCal));
                        resolvedURIPaths.append(uriPath);
                        retVal = resolvedInstances.toString();
                        eval.setVariable(CoordELConstants.RESOLVED_PATH, resolvedURIPaths.toString());
                        break;
                    } else if (available >= startOffset) {
                        LOG.debug("Matched future(" + available + "): " + uriWithDoneFlag);
                        resolvedInstances.append(DateUtils.formatDateOozieTZ(nominalInstanceCal)).append(INSTANCE_SEPARATOR);
                        resolvedURIPaths.append(uriPath).append(INSTANCE_SEPARATOR);
                    }
                    available++;
                }
                // nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), datasetFrequency);
                nominalInstanceCal = (Calendar) initInstance.clone();
                instCount[0]++;
                nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), instCount[0] * datasetFrequency);
                checkedInstance++;
            // DateUtils.moveToEnd(nominalInstanceCal, getDSEndOfFlag());
            }
            if (!StringUtils.isEmpty(resolvedURIPaths.toString()) && eval.getVariable(CoordELConstants.RESOLVED_PATH) == null) {
                eval.setVariable(CoordELConstants.RESOLVED_PATH, resolvedURIPaths.toString());
            }
        } finally {
            if (uriContext != null) {
                uriContext.destroy();
            }
        }
        if (!resolved) {
            // return unchanged future function with variable 'is_resolved'
            // to 'false'
            eval.setVariable(CoordELConstants.IS_RESOLVED, Boolean.FALSE);
            if (startOffset == endOffset) {
                retVal = "${coord:future(" + startOffset + ", " + instance + ")}";
            } else {
                retVal = "${coord:futureRange(" + startOffset + ", " + endOffset + ", " + instance + ")}";
            }
        } else {
            eval.setVariable(CoordELConstants.IS_RESOLVED, Boolean.TRUE);
        }
    } else {
        // No feasible nominal time
        eval.setVariable(CoordELConstants.IS_RESOLVED, Boolean.TRUE);
        retVal = "";
    }
    return retVal;
}
Also used : Context(org.apache.oozie.dependency.URIHandler.Context) Configuration(org.apache.hadoop.conf.Configuration) XLog(org.apache.oozie.util.XLog) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) URI(java.net.URI) URIHandlerService(org.apache.oozie.service.URIHandlerService) URIHandler(org.apache.oozie.dependency.URIHandler) ELEvaluator(org.apache.oozie.util.ELEvaluator)

Example 17 with XLog

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

the class CoordELFunctions method coord_currentRange_sync.

private static String coord_currentRange_sync(int start, int end) throws Exception {
    final XLog LOG = XLog.getLog(CoordELFunctions.class);
    // in minutes
    int datasetFrequency = getDSFrequency();
    TimeUnit dsTimeUnit = getDSTimeUnit();
    // used as pass by ref
    int[] instCount = new int[1];
    Calendar nominalInstanceCal = getCurrentInstance(getActionCreationtime(), instCount);
    if (nominalInstanceCal == null) {
        LOG.warn("If the initial instance of the dataset is later than the nominal time, an empty string is" + " returned. This means that no data is available at the current-instance specified by the user" + " and the user could try modifying his initial-instance to an earlier time.");
        return "";
    } else {
        Calendar initInstance = getInitialInstanceCal();
        // Add in the reverse order - newest instance first.
        nominalInstanceCal = (Calendar) initInstance.clone();
        nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), (instCount[0] + start) * datasetFrequency);
        List<String> instances = new ArrayList<String>();
        for (int i = start; i <= end; i++) {
            if (nominalInstanceCal.compareTo(initInstance) < 0) {
                LOG.warn("If the initial instance of the dataset is later than the current-instance specified," + " such as coord:current({0}) in this case, an empty string is returned. This means that" + " no data is available at the current-instance specified by the user and the user could" + " try modifying his initial-instance to an earlier time.", start);
            } else {
                instances.add(DateUtils.formatDateOozieTZ(nominalInstanceCal));
            }
            nominalInstanceCal.add(dsTimeUnit.getCalendarUnit(), datasetFrequency);
        }
        instances = Lists.reverse(instances);
        return StringUtils.join(instances, CoordELFunctions.INSTANCE_SEPARATOR);
    }
}
Also used : XLog(org.apache.oozie.util.XLog) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList)

Example 18 with XLog

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

the class LiteWorkflowStoreService method getUserRetryMax.

private static int getUserRetryMax(NodeHandler.Context context) throws WorkflowException {
    XLog log = XLog.getLog(LiteWorkflowStoreService.class);
    int ret = ConfigurationService.getInt(CONF_USER_RETRY_MAX);
    int max = ret;
    String userRetryMax = context.getNodeDef().getUserRetryMax();
    if (!userRetryMax.equals("null")) {
        try {
            ret = Integer.parseInt(userRetryMax);
            if (ret > max) {
                ret = max;
                log.warn(ErrorCode.E0820.getTemplate(), ret, max);
            }
        } catch (NumberFormatException nfe) {
            throw new WorkflowException(ErrorCode.E0700, nfe.getMessage(), nfe);
        }
    } else {
        ret = 0;
    }
    return ret;
}
Also used : XLog(org.apache.oozie.util.XLog) WorkflowException(org.apache.oozie.workflow.WorkflowException)

Example 19 with XLog

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

the class LiteWorkflowStoreService method liteExecute.

/**
 * Delegation method used by the Action and Decision {@link NodeHandler} on start. <p> This method provides the
 * necessary information to create ActionExecutors.
 *
 * @param context NodeHandler context.
 * @param actionType the action type.
 * @throws WorkflowException thrown if there was an error parsing the action configuration.
 */
@SuppressWarnings("unchecked")
protected static void liteExecute(NodeHandler.Context context, String actionType) throws WorkflowException {
    XLog log = XLog.getLog(LiteWorkflowStoreService.class);
    String jobId = context.getProcessInstance().getId();
    String nodeName = context.getNodeDef().getName();
    String skipVar = context.getProcessInstance().getVar(context.getNodeDef().getName() + WorkflowInstance.NODE_VAR_SEPARATOR + ReRunXCommand.TO_SKIP);
    boolean skipAction = false;
    if (skipVar != null) {
        skipAction = skipVar.equals("true");
    }
    WorkflowActionBean action = new WorkflowActionBean();
    String actionId = Services.get().get(UUIDService.class).generateChildId(jobId, nodeName);
    if (!skipAction) {
        String nodeConf = context.getNodeDef().getConf();
        if (actionType == null) {
            try {
                Element element = XmlUtils.parseXml(nodeConf);
                actionType = element.getName();
                nodeConf = XmlUtils.prettyPrint(element).toString();
            } catch (JDOMException ex) {
                throw new WorkflowException(ErrorCode.E0700, ex.getMessage(), ex);
            }
        }
        log.debug(" Creating action for node [{0}]", nodeName);
        action.setType(actionType);
        action.setConf(nodeConf);
        action.setLogToken(((WorkflowJobBean) context.getTransientVar(WORKFLOW_BEAN)).getLogToken());
        action.setStatus(WorkflowAction.Status.PREP);
        action.setJobId(jobId);
    }
    String executionPath = context.getExecutionPath();
    action.setExecutionPath(executionPath);
    action.setCred(context.getNodeDef().getCred());
    log.debug("Setting action for cred: '" + context.getNodeDef().getCred() + "', name: '" + context.getNodeDef().getName() + "'");
    action.setUserRetryCount(0);
    int userRetryMax = getUserRetryMax(context);
    int userRetryInterval = getUserRetryInterval(context);
    action.setUserRetryMax(userRetryMax);
    action.setUserRetryInterval(userRetryInterval);
    log.debug("Setting action for userRetryMax: '" + userRetryMax + "', userRetryInterval: '" + userRetryInterval + "', name: '" + context.getNodeDef().getName() + "'");
    action.setName(nodeName);
    action.setId(actionId);
    context.setVar(nodeName + WorkflowInstance.NODE_VAR_SEPARATOR + ACTION_ID, actionId);
    List list = (List) context.getTransientVar(ACTIONS_TO_START);
    if (list == null) {
        list = new ArrayList();
        context.setTransientVar(ACTIONS_TO_START, list);
    }
    list.add(action);
}
Also used : XLog(org.apache.oozie.util.XLog) Element(org.jdom.Element) WorkflowException(org.apache.oozie.workflow.WorkflowException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JDOMException(org.jdom.JDOMException) WorkflowActionBean(org.apache.oozie.WorkflowActionBean)

Example 20 with XLog

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

the class Services method setServiceInternal.

private void setServiceInternal(Class<? extends Service> klass, boolean logging) throws ServiceException {
    try {
        Service newService = (Service) ReflectionUtils.newInstance(klass, null);
        Service oldService = services.get(newService.getInterface());
        if (oldService != null) {
            oldService.destroy();
        }
        if (logging) {
            XLog log = new XLog(LogFactory.getLog(getClass()));
            log.trace("Initializing service[{0}] class[{1}]", newService.getInterface(), newService.getClass());
        }
        newService.init(this);
        services.put(newService.getInterface(), newService);
    } catch (ServiceException ex) {
        XLog.getLog(getClass()).fatal(ex.getMessage(), ex);
        destroy();
        throw ex;
    }
}
Also used : XLog(org.apache.oozie.util.XLog)

Aggregations

XLog (org.apache.oozie.util.XLog)21 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)5 URI (java.net.URI)4 URIHandlerService (org.apache.oozie.service.URIHandlerService)4 File (java.io.File)3 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 Configuration (org.apache.hadoop.conf.Configuration)3 CommandException (org.apache.oozie.command.CommandException)3 URIHandler (org.apache.oozie.dependency.URIHandler)3 ELEvaluator (org.apache.oozie.util.ELEvaluator)3 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 Context (org.apache.oozie.dependency.URIHandler.Context)2 Instrumentation (org.apache.oozie.util.Instrumentation)2 WorkflowException (org.apache.oozie.workflow.WorkflowException)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 StringReader (java.io.StringReader)1