This project has retired. For details please refer to its
Attic page.
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 package org.apache.oozie.command.wf;
019
020 import org.apache.hadoop.conf.Configuration;
021 import org.apache.oozie.WorkflowJobBean;
022 import org.apache.oozie.ErrorCode;
023 import org.apache.oozie.service.JPAService;
024 import org.apache.oozie.service.WorkflowStoreService;
025 import org.apache.oozie.service.WorkflowAppService;
026 import org.apache.oozie.service.Services;
027 import org.apache.oozie.service.DagXLogInfoService;
028 import org.apache.oozie.util.InstrumentUtils;
029 import org.apache.oozie.util.LogUtils;
030 import org.apache.oozie.util.XLog;
031 import org.apache.oozie.util.ParamChecker;
032 import org.apache.oozie.util.XConfiguration;
033 import org.apache.oozie.util.XmlUtils;
034 import org.apache.oozie.command.CommandException;
035 import org.apache.oozie.executor.jpa.WorkflowJobInsertJPAExecutor;
036 import org.apache.oozie.store.StoreException;
037 import org.apache.oozie.workflow.WorkflowApp;
038 import org.apache.oozie.workflow.WorkflowException;
039 import org.apache.oozie.workflow.WorkflowInstance;
040 import org.apache.oozie.workflow.WorkflowLib;
041 import org.apache.oozie.util.PropertiesUtils;
042 import org.apache.oozie.client.OozieClient;
043 import org.apache.oozie.client.WorkflowJob;
044 import org.apache.oozie.client.XOozieClient;
045 import org.jdom.Element;
046 import org.jdom.Namespace;
047
048 import java.util.Date;
049 import java.util.Map;
050 import java.util.Set;
051 import java.util.HashSet;
052
053 public abstract class SubmitHttpXCommand extends WorkflowXCommand<String> {
054
055 protected static final Set<String> MANDATORY_OOZIE_CONFS = new HashSet<String>();
056 protected static final Set<String> OPTIONAL_OOZIE_CONFS = new HashSet<String>();
057
058 static {
059 MANDATORY_OOZIE_CONFS.add(XOozieClient.JT);
060 MANDATORY_OOZIE_CONFS.add(XOozieClient.NN);
061 MANDATORY_OOZIE_CONFS.add(OozieClient.LIBPATH);
062
063 OPTIONAL_OOZIE_CONFS.add(XOozieClient.FILES);
064 OPTIONAL_OOZIE_CONFS.add(XOozieClient.ARCHIVES);
065 }
066
067 private Configuration conf;
068 private String authToken;
069
070 public SubmitHttpXCommand(String name, String type, Configuration conf, String authToken) {
071 super(name, type, 1);
072 this.conf = ParamChecker.notNull(conf, "conf");
073 this.authToken = ParamChecker.notEmpty(authToken, "authToken");
074 }
075
076 private static final Set<String> DISALLOWED_DEFAULT_PROPERTIES = new HashSet<String>();
077 private static final Set<String> DISALLOWED_USER_PROPERTIES = new HashSet<String>();
078
079 static {
080 String[] badUserProps = { PropertiesUtils.DAYS, PropertiesUtils.HOURS, PropertiesUtils.MINUTES,
081 PropertiesUtils.KB, PropertiesUtils.MB, PropertiesUtils.GB, PropertiesUtils.TB, PropertiesUtils.PB,
082 PropertiesUtils.RECORDS, PropertiesUtils.MAP_IN, PropertiesUtils.MAP_OUT, PropertiesUtils.REDUCE_IN,
083 PropertiesUtils.REDUCE_OUT, PropertiesUtils.GROUPS };
084 PropertiesUtils.createPropertySet(badUserProps, DISALLOWED_USER_PROPERTIES);
085
086 String[] badDefaultProps = { PropertiesUtils.HADOOP_USER};
087 PropertiesUtils.createPropertySet(badUserProps, DISALLOWED_DEFAULT_PROPERTIES);
088 PropertiesUtils.createPropertySet(badDefaultProps, DISALLOWED_DEFAULT_PROPERTIES);
089 }
090
091 /**
092 * Generate workflow xml from conf object
093 *
094 * @param conf the configuration object
095 * @return workflow xml def string representation
096 */
097 abstract protected String getWorkflowXml(Configuration conf);
098
099 /* (non-Javadoc)
100 * @see org.apache.oozie.command.XCommand#execute()
101 */
102 @Override
103 protected String execute() throws CommandException {
104 InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation());
105 WorkflowAppService wps = Services.get().get(WorkflowAppService.class);
106 try {
107 XLog.Info.get().setParameter(DagXLogInfoService.TOKEN, conf.get(OozieClient.LOG_TOKEN));
108 String wfXml = getWorkflowXml(conf);
109 LOG.debug("workflow xml created on the server side is :\n");
110 LOG.debug(wfXml);
111 WorkflowApp app = wps.parseDef(wfXml, conf);
112 XConfiguration protoActionConf = wps.createProtoActionConf(conf, authToken, false);
113 WorkflowLib workflowLib = Services.get().get(WorkflowStoreService.class).getWorkflowLibWithNoDB();
114
115 PropertiesUtils.checkDisallowedProperties(conf, DISALLOWED_USER_PROPERTIES);
116
117 // Resolving all variables in the job properties.
118 // This ensures the Hadoop Configuration semantics is preserved.
119 XConfiguration resolvedVarsConf = new XConfiguration();
120 for (Map.Entry<String, String> entry : conf) {
121 resolvedVarsConf.set(entry.getKey(), conf.get(entry.getKey()));
122 }
123 conf = resolvedVarsConf;
124
125 WorkflowInstance wfInstance;
126 try {
127 wfInstance = workflowLib.createInstance(app, conf);
128 }
129 catch (WorkflowException e) {
130 throw new StoreException(e);
131 }
132
133 Configuration conf = wfInstance.getConf();
134
135 WorkflowJobBean workflow = new WorkflowJobBean();
136 workflow.setId(wfInstance.getId());
137 workflow.setAppName(app.getName());
138 workflow.setAppPath(conf.get(OozieClient.APP_PATH));
139 workflow.setConf(XmlUtils.prettyPrint(conf).toString());
140 workflow.setProtoActionConf(protoActionConf.toXmlString());
141 workflow.setCreatedTime(new Date());
142 workflow.setLastModifiedTime(new Date());
143 workflow.setLogToken(conf.get(OozieClient.LOG_TOKEN, ""));
144 workflow.setStatus(WorkflowJob.Status.PREP);
145 workflow.setRun(0);
146 workflow.setUser(conf.get(OozieClient.USER_NAME));
147 workflow.setGroup(conf.get(OozieClient.GROUP_NAME));
148 workflow.setAuthToken(authToken);
149 workflow.setWorkflowInstance(wfInstance);
150 workflow.setExternalId(conf.get(OozieClient.EXTERNAL_ID));
151
152 LogUtils.setLogInfo(workflow, logInfo);
153 JPAService jpaService = Services.get().get(JPAService.class);
154 if (jpaService != null) {
155 jpaService.execute(new WorkflowJobInsertJPAExecutor(workflow));
156 }
157 else {
158 LOG.error(ErrorCode.E0610);
159 return null;
160 }
161
162 return workflow.getId();
163 }
164 catch (WorkflowException ex) {
165 throw new CommandException(ex);
166 }
167 catch (Exception ex) {
168 throw new CommandException(ErrorCode.E0803, ex);
169 }
170 }
171
172 static private void addSection(Element X, Namespace ns, String filesStr, String tagName) {
173 if (filesStr != null) {
174 String[] files = filesStr.split(",");
175 for (String f : files) {
176 Element tagElement = new Element(tagName, ns);
177 if (f.contains("#")) {
178 tagElement.addContent(f);
179 }
180 else {
181 String filename = f.substring(f.lastIndexOf("/") + 1, f.length());
182 if (filename == null || filename.isEmpty()) {
183 tagElement.addContent(f);
184 }
185 else {
186 tagElement.addContent(f + "#" + filename);
187 }
188 }
189 X.addContent(tagElement);
190 }
191 }
192 }
193
194 /**
195 * Add file section in X.
196 *
197 * @param parent XML element to be appended
198 * @param conf Configuration object
199 * @param ns XML element namespace
200 */
201 static void addFileSection(Element X, Configuration conf, Namespace ns) {
202 String filesStr = conf.get(XOozieClient.FILES);
203 addSection(X, ns, filesStr, "file");
204 }
205
206 /**
207 * Add archive section in X.
208 *
209 * @param parent XML element to be appended
210 * @param conf Configuration object
211 * @param ns XML element namespace
212 */
213 static void addArchiveSection(Element X, Configuration conf, Namespace ns) {
214 String archivesStr = conf.get(XOozieClient.ARCHIVES);
215 addSection(X, ns, archivesStr, "archive");
216 }
217 }