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.command.wf; 020 021import org.apache.hadoop.conf.Configuration; 022import org.apache.oozie.WorkflowJobBean; 023import org.apache.oozie.ErrorCode; 024import org.apache.oozie.service.JPAService; 025import org.apache.oozie.service.WorkflowStoreService; 026import org.apache.oozie.service.WorkflowAppService; 027import org.apache.oozie.service.Services; 028import org.apache.oozie.service.DagXLogInfoService; 029import org.apache.oozie.util.InstrumentUtils; 030import org.apache.oozie.util.LogUtils; 031import org.apache.oozie.util.XLog; 032import org.apache.oozie.util.ParamChecker; 033import org.apache.oozie.util.XConfiguration; 034import org.apache.oozie.util.XmlUtils; 035import org.apache.oozie.command.CommandException; 036import org.apache.oozie.executor.jpa.WorkflowJobInsertJPAExecutor; 037import org.apache.oozie.store.StoreException; 038import org.apache.oozie.workflow.WorkflowApp; 039import org.apache.oozie.workflow.WorkflowException; 040import org.apache.oozie.workflow.WorkflowInstance; 041import org.apache.oozie.workflow.WorkflowLib; 042import org.apache.oozie.util.PropertiesUtils; 043import org.apache.oozie.client.OozieClient; 044import org.apache.oozie.client.WorkflowJob; 045import org.apache.oozie.client.XOozieClient; 046import org.jdom.Element; 047import org.jdom.Namespace; 048 049import com.google.common.collect.ImmutableSet; 050 051import java.util.Date; 052import java.util.List; 053import java.util.Map; 054import java.util.Set; 055import java.util.HashSet; 056 057public abstract class SubmitHttpXCommand extends WorkflowXCommand<String> { 058 059 static final Set<String> MANDATORY_OOZIE_CONFS = ImmutableSet.of(XOozieClient.RM, XOozieClient.NN, OozieClient.LIBPATH); 060 static final Set<String> OPTIONAL_OOZIE_CONFS = ImmutableSet.of(XOozieClient.FILES, XOozieClient.ARCHIVES); 061 062 private Configuration conf; 063 064 public SubmitHttpXCommand(String name, String type, Configuration conf) { 065 super(name, type, 1); 066 this.conf = ParamChecker.notNull(conf, "conf"); 067 } 068 069 private static final Set<String> DISALLOWED_USER_PROPERTIES = new HashSet<String>(); 070 071 static { 072 String[] badUserProps = { PropertiesUtils.DAYS, PropertiesUtils.HOURS, PropertiesUtils.MINUTES, 073 PropertiesUtils.KB, PropertiesUtils.MB, PropertiesUtils.GB, PropertiesUtils.TB, PropertiesUtils.PB, 074 PropertiesUtils.RECORDS, PropertiesUtils.MAP_IN, PropertiesUtils.MAP_OUT, PropertiesUtils.REDUCE_IN, 075 PropertiesUtils.REDUCE_OUT, PropertiesUtils.GROUPS }; 076 PropertiesUtils.createPropertySet(badUserProps, DISALLOWED_USER_PROPERTIES); 077 } 078 079 abstract protected Element generateSection(Configuration conf, Namespace ns); 080 081 abstract protected Namespace getSectionNamespace(); 082 083 abstract protected String getWorkflowName(); 084 085 protected void checkMandatoryConf(Configuration conf) { 086 for (String key : MANDATORY_OOZIE_CONFS) { 087 String value = conf.get(key); 088 if (value == null) { 089 throw new RuntimeException(key + " is not specified"); 090 } 091 } 092 } 093 094 protected Namespace getWorkflowNamespace() { 095 return Namespace.getNamespace("uri:oozie:workflow:0.2"); 096 } 097 /** 098 * Generate workflow xml from conf object 099 * 100 * @param conf the configuration object 101 * @return workflow xml def string representation 102 */ 103 protected String getWorkflowXml(Configuration conf) { 104 checkMandatoryConf(conf); 105 106 Namespace ns = getWorkflowNamespace(); 107 Element root = new Element("workflow-app", ns); 108 String name = getWorkflowName(); 109 root.setAttribute("name", "oozie-" + name); 110 111 Element start = new Element("start", ns); 112 String nodeName = name + "1"; 113 start.setAttribute("to", nodeName); 114 root.addContent(start); 115 116 Element action = new Element("action", ns); 117 action.setAttribute("name", nodeName); 118 119 Element ele = generateSection(conf, getSectionNamespace()); 120 action.addContent(ele); 121 122 Element ok = new Element("ok", ns); 123 ok.setAttribute("to", "end"); 124 action.addContent(ok); 125 126 Element error = new Element("error", ns); 127 error.setAttribute("to", "fail"); 128 action.addContent(error); 129 130 root.addContent(action); 131 132 Element kill = new Element("kill", ns); 133 kill.setAttribute("name", "fail"); 134 Element message = new Element("message", ns); 135 message.addContent(name + " failed, error message[${wf:errorMessage(wf:lastErrorNode())}]"); 136 kill.addContent(message); 137 root.addContent(kill); 138 139 Element end = new Element("end", ns); 140 end.setAttribute("name", "end"); 141 root.addContent(end); 142 143 return XmlUtils.prettyPrint(root).toString(); 144 }; 145 146 protected Element generateConfigurationSection(List<String> Dargs, Namespace ns) { 147 Element configuration = new Element("configuration", ns); 148 for (String arg : Dargs) { 149 String name = null, value = null; 150 int pos = arg.indexOf("="); 151 if (pos == -1) { // "-D<name>" or "-D" only 152 name = arg.substring(2, arg.length()); 153 value = ""; 154 } 155 else { // "-D<name>=<value>" 156 name = arg.substring(2, pos); 157 value = arg.substring(pos + 1, arg.length()); 158 } 159 160 Element property = new Element("property", ns); 161 Element nameElement = new Element("name", ns); 162 nameElement.addContent(name); 163 property.addContent(nameElement); 164 Element valueElement = new Element("value", ns); 165 valueElement.addContent(value); 166 property.addContent(valueElement); 167 configuration.addContent(property); 168 } 169 170 return configuration; 171 } 172 173 /* (non-Javadoc) 174 * @see org.apache.oozie.command.XCommand#execute() 175 */ 176 @Override 177 protected String execute() throws CommandException { 178 InstrumentUtils.incrJobCounter(getName(), 1, getInstrumentation()); 179 WorkflowAppService wps = Services.get().get(WorkflowAppService.class); 180 try { 181 XLog.Info.get().setParameter(DagXLogInfoService.TOKEN, conf.get(OozieClient.LOG_TOKEN)); 182 String wfXml = getWorkflowXml(conf); 183 LOG.debug("workflow xml created on the server side is :\n"); 184 LOG.debug(wfXml); 185 WorkflowApp app = wps.parseDef(wfXml, conf); 186 XConfiguration protoActionConf = wps.createProtoActionConf(conf, false); 187 WorkflowLib workflowLib = Services.get().get(WorkflowStoreService.class).getWorkflowLibWithNoDB(); 188 189 PropertiesUtils.checkDisallowedProperties(conf, DISALLOWED_USER_PROPERTIES); 190 PropertiesUtils.checkDefaultDisallowedProperties(conf); 191 192 // Resolving all variables in the job properties. 193 // This ensures the Hadoop Configuration semantics is preserved. 194 XConfiguration resolvedVarsConf = new XConfiguration(); 195 for (Map.Entry<String, String> entry : conf) { 196 resolvedVarsConf.set(entry.getKey(), conf.get(entry.getKey())); 197 } 198 conf = resolvedVarsConf; 199 200 WorkflowInstance wfInstance; 201 try { 202 wfInstance = workflowLib.createInstance(app, conf); 203 } 204 catch (WorkflowException e) { 205 throw new StoreException(e); 206 } 207 208 Configuration conf = wfInstance.getConf(); 209 210 WorkflowJobBean workflow = new WorkflowJobBean(); 211 workflow.setId(wfInstance.getId()); 212 workflow.setAppName(app.getName()); 213 workflow.setAppPath(conf.get(OozieClient.APP_PATH)); 214 workflow.setConf(XmlUtils.prettyPrint(conf).toString()); 215 workflow.setProtoActionConf(protoActionConf.toXmlString()); 216 workflow.setCreatedTime(new Date()); 217 workflow.setLastModifiedTime(new Date()); 218 workflow.setLogToken(conf.get(OozieClient.LOG_TOKEN, "")); 219 workflow.setStatus(WorkflowJob.Status.PREP); 220 workflow.setRun(0); 221 workflow.setUser(conf.get(OozieClient.USER_NAME)); 222 workflow.setGroup(conf.get(OozieClient.GROUP_NAME)); 223 workflow.setWorkflowInstance(wfInstance); 224 workflow.setExternalId(conf.get(OozieClient.EXTERNAL_ID)); 225 226 LogUtils.setLogInfo(workflow); 227 JPAService jpaService = Services.get().get(JPAService.class); 228 if (jpaService != null) { 229 jpaService.execute(new WorkflowJobInsertJPAExecutor(workflow)); 230 } 231 else { 232 LOG.error(ErrorCode.E0610); 233 return null; 234 } 235 236 return workflow.getId(); 237 } 238 catch (WorkflowException ex) { 239 throw new CommandException(ex); 240 } 241 catch (Exception ex) { 242 throw new CommandException(ErrorCode.E0803, ex.getMessage(), ex); 243 } 244 } 245 246 static private void addSection(Element X, Namespace ns, String filesStr, String tagName) { 247 if (filesStr != null) { 248 String[] files = filesStr.split(","); 249 for (String f : files) { 250 Element tagElement = new Element(tagName, ns); 251 if (f.contains("#")) { 252 tagElement.addContent(f); 253 } 254 else { 255 String filename = f.substring(f.lastIndexOf("/") + 1, f.length()); 256 if (filename == null || filename.isEmpty()) { 257 tagElement.addContent(f); 258 } 259 else { 260 tagElement.addContent(f + "#" + filename); 261 } 262 } 263 X.addContent(tagElement); 264 } 265 } 266 } 267 268 /** 269 * Add file section in X. 270 * 271 * @param X XML element to be appended 272 * @param conf Configuration object 273 * @param ns XML element namespace 274 */ 275 static void addFileSection(Element X, Configuration conf, Namespace ns) { 276 String filesStr = conf.get(XOozieClient.FILES); 277 addSection(X, ns, filesStr, "file"); 278 } 279 280 /** 281 * Add archive section in X. 282 * 283 * @param X XML element to be appended 284 * @param conf Configuration object 285 * @param ns XML element namespace 286 */ 287 static void addArchiveSection(Element X, Configuration conf, Namespace ns) { 288 String archivesStr = conf.get(XOozieClient.ARCHIVES); 289 addSection(X, ns, archivesStr, "archive"); 290 } 291}