001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.action.hadoop;
020
021import java.io.IOException;
022import java.io.StringReader;
023import java.util.Iterator;
024import java.util.Map;
025import java.util.Map.Entry;
026
027import org.apache.hadoop.conf.Configuration;
028import org.apache.oozie.action.ActionExecutor.Context;
029import org.apache.oozie.action.oozie.SubWorkflowActionExecutor;
030import org.apache.oozie.client.OozieClient;
031import org.apache.oozie.client.WorkflowAction;
032import org.apache.oozie.command.wf.JobXCommand;
033import org.apache.oozie.service.ConfigurationService;
034import org.apache.oozie.service.Services;
035import org.apache.oozie.util.XConfiguration;
036
037import com.google.common.annotations.VisibleForTesting;
038
039public class OozieJobInfo {
040
041    public static final String BUNDLE_ID = "bundle.id";
042    public static final String BUNDLE_NAME = "bundle.name";
043    public static final String COORD_NAME = "coord.name";
044    public static final String COORD_ID = "coord.id";
045    public static final String COORD_NOMINAL_TIME = "coord.nominal.time";
046    public static final String WORKFLOW_ID = "wf.id";
047    public static final String WORKFLOW_NAME = "wf.name";
048    public static final String WORKFLOW_DEPTH = "wf.depth";
049    public static final String WORKFLOW_SUPER_PARENT = "wf.superparent.id";
050    public static final String ACTION_TYPE = "action.type";
051    public static final String ACTION_NAME = "action.name";
052    public static final String JOB_INFO_KEY = "oozie.job.info";
053    public static final String CONF_JOB_INFO = "oozie.action.jobinfo.enable";
054    public final static String SEPARATOR = ",";
055
056    private Context context;
057    XConfiguration contextConf;
058    private WorkflowAction action;
059    private Configuration actionConf;
060    private static boolean jobInfo = ConfigurationService.getBoolean(OozieJobInfo.CONF_JOB_INFO);
061
062    /**
063     * Instantiates a new oozie job info.
064     *
065     * @param actionConf the action conf
066     * @param context the context
067     * @param action the action
068     * @throws IOException
069     */
070    public OozieJobInfo(Configuration actionConf, Context context, WorkflowAction action) throws IOException {
071        this.context = context;
072        contextConf = new XConfiguration(new StringReader(context.getWorkflow().getConf()));
073        this.action = action;
074        this.actionConf = actionConf;
075    }
076
077    public static boolean isJobInfoEnabled() {
078        return jobInfo;
079    }
080
081    @VisibleForTesting
082    public static void setJobInfo(boolean jobInfo) {
083        OozieJobInfo.jobInfo = jobInfo;
084    }
085
086    /**
087     * Get the job info.
088     *
089     * @return job info
090     * @throws IOException Signals that an I/O exception has occurred.
091     */
092    public String getJobInfo() throws IOException {
093        StringBuffer sb = new StringBuffer();
094        addBundleInfo(sb);
095        addCoordInfo(sb);
096        addWorkflowInfo(sb);
097        addActionInfo(sb);
098        addCustomInfo(sb);
099        return sb.toString();
100
101    }
102
103    private void addBundleInfo(StringBuffer sb) throws IOException {
104        addJobInfo(sb, BUNDLE_ID, contextConf.get(OozieClient.BUNDLE_ID));
105        addJobInfo(sb, BUNDLE_NAME, contextConf.get(OozieJobInfo.BUNDLE_NAME));
106
107    }
108
109    private void addCoordInfo(StringBuffer sb) throws IOException {
110        addJobInfo(sb, COORD_NAME, contextConf.get(OozieJobInfo.COORD_NAME));
111        addJobInfo(sb, COORD_NOMINAL_TIME, contextConf.get(OozieJobInfo.COORD_NOMINAL_TIME));
112        addJobInfo(sb, COORD_ID, contextConf.get(OozieJobInfo.COORD_ID));
113    }
114
115    private void addWorkflowInfo(StringBuffer sb) {
116        addJobInfo(sb, WORKFLOW_ID, context.getWorkflow().getId());
117        addJobInfo(sb, WORKFLOW_NAME, context.getWorkflow().getAppName());
118        addJobInfo(sb, WORKFLOW_DEPTH, contextConf.get(SubWorkflowActionExecutor.SUBWORKFLOW_DEPTH, "0"));
119        addJobInfo(sb, WORKFLOW_SUPER_PARENT, computeSuperParent());
120    }
121
122    private void addActionInfo(StringBuffer sb) {
123        addJobInfo(sb, ACTION_NAME, action.getName());
124        addJobInfo(sb, ACTION_TYPE, action.getType());
125    }
126
127    private void addCustomInfo(StringBuffer sb) throws IOException {
128        addfromConf(actionConf, sb);
129    }
130
131    public void addfromConf(Configuration conf, StringBuffer sb) {
132        Iterator<Map.Entry<String, String>> it = conf.iterator();
133        while (it.hasNext()) {
134            Entry<String, String> entry = it.next();
135            if (entry.getKey().startsWith("oozie.job.info.")) {
136                addJobInfo(sb, entry.getKey().substring("oozie.job.info.".length()), entry.getValue());
137            }
138        }
139    }
140
141    private void addJobInfo(StringBuffer sb, String key, String value) {
142        if (value != null) {
143            sb.append(key).append("=").append(value).append(OozieJobInfo.SEPARATOR);
144        }
145
146    }
147
148    private String computeSuperParent() {
149        String superParentId = contextConf.get(SubWorkflowActionExecutor.SUPER_PARENT_ID);
150        if (superParentId == null) {
151            // Not a sub-workflow
152            if (context.getWorkflow().getParentId() != null) {
153                // return coord id as the super parent id
154                return context.getWorkflow().getParentId();
155            } else {
156                // return the current workflow id as the super parent id.
157                return context.getWorkflow().getId();
158            }
159        }
160        return superParentId;
161    }
162}