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 java.io.IOException; 022import java.io.StringReader; 023import java.net.URI; 024import java.net.URISyntaxException; 025 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileSystem; 028import org.apache.hadoop.fs.Path; 029import org.apache.oozie.ErrorCode; 030import org.apache.oozie.WorkflowJobBean; 031import org.apache.oozie.client.WorkflowJob; 032import org.apache.oozie.command.CommandException; 033import org.apache.oozie.command.PreconditionException; 034import org.apache.oozie.service.HadoopAccessorException; 035import org.apache.oozie.service.HadoopAccessorService; 036import org.apache.oozie.service.Services; 037import org.apache.oozie.util.XConfiguration; 038 039import com.google.common.annotations.VisibleForTesting; 040 041/** 042 * This Command is expected to be called when a Workflow moves to any terminal 043 * state ( such as SUCCEEDED, KILLED, FAILED). This class primarily removes the 044 * temporary directory created for specific workflow id 045 */ 046public class WfEndXCommand extends WorkflowXCommand<Void> { 047 048 private WorkflowJobBean jobBean = null; 049 050 public WfEndXCommand(WorkflowJobBean jobBean) { 051 super("wf_end", "wf_end", 1); 052 this.jobBean = jobBean; 053 } 054 055 @Override 056 protected Void execute() throws CommandException { 057 LOG.debug("STARTED WFEndXCommand " + jobBean.getId()); 058 deleteWFDir(); 059 LOG.debug("ENDED WFEndXCommand " + jobBean.getId()); 060 return null; 061 } 062 063 private void deleteWFDir() throws CommandException { 064 FileSystem fs; 065 try { 066 fs = getAppFileSystem(jobBean); 067 String wfDir = Services.get().getSystemId() + "/" + jobBean.getId(); 068 Path wfDirPath = new Path(fs.getHomeDirectory(), wfDir); 069 070 LOG.debug("WF tmp dir :" + wfDirPath); 071 boolean keepActionDir = keepWfActionDir(); 072 if (!keepActionDir && fs.exists(wfDirPath)) { 073 fs.delete(wfDirPath, true); 074 } 075 else if (keepActionDir) { 076 LOG.debug(KEEP_WF_ACTION_DIR + " is set to true"); 077 } 078 else { 079 LOG.debug("Tmp dir doesn't exist :" + wfDirPath); 080 } 081 } 082 catch (Exception e) { 083 LOG.error("Unable to delete WF temp dir of wf id :" + jobBean.getId(), e); 084 throw new CommandException(ErrorCode.E0819, jobBean.getId(), e); 085 } 086 } 087 088 @VisibleForTesting 089 protected boolean keepWfActionDir() throws IOException { 090 if (jobBean.getProtoActionConf() == null) { 091 return false; 092 } 093 Configuration wfConf = getWfConfiguration(); 094 return wfConf.getBoolean(KEEP_WF_ACTION_DIR, false); 095 } 096 097 @VisibleForTesting 098 protected Configuration getWfConfiguration() throws IOException { 099 return new XConfiguration(new StringReader(jobBean.getProtoActionConf())); 100 } 101 102 protected FileSystem getAppFileSystem(WorkflowJob workflow) throws HadoopAccessorException, IOException, 103 URISyntaxException { 104 URI uri = new URI(workflow.getAppPath()); 105 HadoopAccessorService has = Services.get().get(HadoopAccessorService.class); 106 Configuration fsConf = has.createConfiguration(uri.getAuthority()); 107 return has.createFileSystem(workflow.getUser(), uri, fsConf); 108 } 109 110 @Override 111 public String getEntityKey() { 112 return jobBean.getId(); 113 } 114 115 @Override 116 protected boolean isLockRequired() { 117 return false; 118 } 119 120 @Override 121 protected void loadState() throws CommandException { 122 123 } 124 125 @Override 126 protected void verifyPrecondition() throws CommandException, PreconditionException { 127 128 } 129 130}