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;
019
020 import org.apache.hadoop.conf.Configuration;
021 import org.apache.hadoop.fs.FileStatus;
022 import org.apache.hadoop.fs.FileSystem;
023 import org.apache.hadoop.fs.FileUtil;
024 import org.apache.hadoop.fs.Path;
025 import org.apache.oozie.ErrorCode;
026 import org.apache.oozie.service.HadoopAccessorException;
027 import org.apache.oozie.service.HadoopAccessorService;
028 import org.apache.oozie.service.Services;
029 import org.apache.oozie.service.XLogCopyService;
030 import org.apache.oozie.util.XLog;
031
032 import java.io.IOException;
033 import java.net.URI;
034 import java.util.List;
035 public class XLogPurgeXCommand extends XCommand<Void> {
036 private List<String> wfList;
037 private List<String> coordList;
038 private List<String> bundleList;
039
040 public XLogPurgeXCommand(List<String> wfList, List<String> coordList, List<String> bundleList) {
041 super("XLogPurge", "XLogPurge", 0);
042 this.wfList = wfList;
043 this.coordList = coordList;
044 this.bundleList = bundleList;
045 }
046
047
048 /* (non-Javadoc)
049 * @see org.apache.oozie.command.XCommand#execute()
050 */
051 @Override
052 protected Void execute() throws CommandException {
053 XLogCopyService xls = Services.get().get(XLogCopyService.class);
054 String hdfsDir = xls.getConfHdfsLogDir();
055
056 XLog.Info.get().clear();
057 XLog log = XLog.getLog(getClass());
058 HadoopAccessorService has = Services.get().get(HadoopAccessorService.class);
059 URI uri = new Path(hdfsDir).toUri();
060 Configuration fsConf = has.createJobConf(uri.getAuthority());
061 FileSystem fs = null;
062
063 try {
064 fs = has.createFileSystem(System.getProperty("user.name"), uri, fsConf);
065 }
066 catch (Exception ex) {
067 log.error("user has to be specified to access hdfs",
068 new HadoopAccessorException(ErrorCode.E0902, "user has to be specified to access FileSystem"));
069 }
070
071 deleteJobLogs(fs, hdfsDir, wfList);
072 deleteJobLogs(fs, hdfsDir, coordList);
073 deleteJobLogs(fs, hdfsDir, bundleList);
074
075 try {
076 fs.close();
077 }
078 catch (IOException ex) {
079 LOG.error("cannot close filesystem");
080 }
081 return null;
082 }
083
084 private void deleteJobLogs(FileSystem fs, String hdfsDir, List<String> jobIds) {
085 Path[] paths = null;
086 try {
087 FileStatus[] fileStatuses = fs.listStatus(new Path(hdfsDir));
088 paths = FileUtil.stat2Paths(fileStatuses);
089 }
090 catch (IOException ex) {
091 LOG.error("file not found " + ex.getMessage());
092 }
093
094 for (Path path : paths) {
095 for (String jobId : jobIds) {
096 final Path p = new Path(path, jobId + ".log");
097 try {
098 if (fs.exists(p)) {
099 fs.delete(p, true);
100 }
101 }
102 catch (IOException ex) {
103 LOG.error("cannot delete job logs in hdfs",
104 new HadoopAccessorException(ErrorCode.E0902, "cannot delete file " + p));
105 }
106 }
107 }
108 }
109
110 /* (non-Javadoc)
111 * @see org.apache.oozie.command.XCommand#loadState()
112 */
113 @Override
114 protected void loadState() throws CommandException {
115 }
116
117 /* (non-Javadoc)
118 * @see org.apache.oozie.command.XCommand#getEntityKey()
119 */
120 @Override
121 public String getEntityKey() {
122 return null;
123 }
124
125 /* (non-Javadoc)
126 * @see org.apache.oozie.command.XCommand#isLockRequired()
127 */
128 @Override
129 protected boolean isLockRequired() {
130 return false;
131 }
132
133 /* (non-Javadoc)
134 * @see org.apache.oozie.command.XCommand#verifyPrecondition()
135 */
136 @Override
137 protected void verifyPrecondition() throws CommandException, PreconditionException {
138 }
139 }