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
019
020package org.apache.oozie.command.coord;
021
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.io.StringReader;
025import java.util.Date;
026
027import org.apache.commons.lang.StringUtils;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.oozie.CoordinatorJobBean;
030import org.apache.oozie.ErrorCode;
031import org.apache.oozie.client.CoordinatorJob;
032import org.apache.oozie.client.OozieClient;
033import org.apache.oozie.command.CommandException;
034import org.apache.oozie.executor.jpa.CoordJobQueryExecutor;
035import org.apache.oozie.executor.jpa.JPAExecutorException;
036import org.apache.oozie.executor.jpa.CoordJobQueryExecutor.CoordJobQuery;
037import org.apache.oozie.service.JPAService;
038import org.apache.oozie.service.Services;
039import org.apache.oozie.util.ConfigUtils;
040import org.apache.oozie.util.LogUtils;
041import org.apache.oozie.util.XConfiguration;
042import org.apache.oozie.util.XmlUtils;
043import org.eclipse.jgit.diff.DiffFormatter;
044import org.eclipse.jgit.diff.EditList;
045import org.eclipse.jgit.diff.HistogramDiff;
046import org.eclipse.jgit.diff.RawText;
047import org.eclipse.jgit.diff.RawTextComparator;
048import org.jdom.Element;
049
050/**
051 * This class provides the functionalities to update coordinator job XML and properties. It uses CoordSubmitXCommand
052 * functionality to validate XML and resolve all the variables or properties using job configurations.
053 */
054public class CoordUpdateXCommand extends CoordSubmitXCommand {
055
056    private final String jobId;
057    private boolean showDiff = true;
058    private boolean isConfChange = false;
059
060    //This properties are set in coord jobs by bundle. An update command should not overide it.
061    final static String[] bundleConfList = new String[] { OozieClient.BUNDLE_ID };
062
063    StringBuffer diff = new StringBuffer();
064    CoordinatorJobBean oldCoordJob = null;
065
066    public CoordUpdateXCommand(boolean dryrun, Configuration conf, String jobId) {
067        super(dryrun, conf);
068        this.jobId = jobId;
069        isConfChange = conf.size() == 0 ? false : true;
070    }
071
072    public CoordUpdateXCommand(boolean dryrun, Configuration conf, String jobId, boolean showDiff) {
073        super(dryrun, conf);
074        this.jobId = jobId;
075        this.showDiff = showDiff;
076        isConfChange = conf.size() == 0 ? false : true;
077    }
078
079    @Override
080    protected String storeToDB(String xmlElement, Element eJob, CoordinatorJobBean coordJob) throws CommandException {
081        check(oldCoordJob, coordJob);
082
083        ConfigUtils.checkAndSetDisallowedProperties(conf,
084                this.oldCoordJob.getUser(),
085                new CommandException(ErrorCode.E1003,
086                        String.format("%s=%s", OozieClient.USER_NAME, conf.get(OozieClient.USER_NAME))),
087                true);
088
089        computeDiff(eJob);
090        oldCoordJob.setAppPath(conf.get(OozieClient.COORDINATOR_APP_PATH));
091        if (isConfChange) {
092            oldCoordJob.setConf(XmlUtils.prettyPrint(conf).toString());
093        }
094        oldCoordJob.setMatThrottling(coordJob.getMatThrottling());
095        oldCoordJob.setOrigJobXml(xmlElement);
096        oldCoordJob.setConcurrency(coordJob.getConcurrency());
097        oldCoordJob.setExecution(coordJob.getExecution());
098        oldCoordJob.setTimeout(coordJob.getTimeout());
099        oldCoordJob.setJobXml(XmlUtils.prettyPrint(eJob).toString());
100
101
102        if (!dryrun) {
103            oldCoordJob.setLastModifiedTime(new Date());
104            // Should log the changes, this should be useful for debugging.
105            LOG.info("Coord update changes : " + diff.toString());
106            try {
107                CoordJobQueryExecutor.getInstance().executeUpdate(CoordJobQuery.UPDATE_COORD_JOB, oldCoordJob);
108            }
109            catch (JPAExecutorException jpaee) {
110                throw new CommandException(jpaee);
111            }
112        }
113        return jobId;
114    }
115
116    @Override
117    protected void loadState() throws CommandException {
118        super.loadState();
119        jpaService = Services.get().get(JPAService.class);
120        if (jpaService == null) {
121            throw new CommandException(ErrorCode.E0610);
122        }
123        coordJob = new CoordinatorJobBean();
124        try {
125            oldCoordJob = CoordJobQueryExecutor.getInstance().get(CoordJobQuery.GET_COORD_JOB, jobId);
126        }
127        catch (JPAExecutorException e) {
128            throw new CommandException(e);
129        }
130
131        LogUtils.setLogInfo(oldCoordJob);
132        if (!isConfChange || StringUtils.isEmpty(conf.get(OozieClient.COORDINATOR_APP_PATH))) {
133            try {
134                XConfiguration jobConf = new XConfiguration(new StringReader(oldCoordJob.getConf()));
135
136                if (!isConfChange) {
137                    conf = jobConf;
138                }
139                else {
140                    for (String bundleConfKey : bundleConfList) {
141                        if (jobConf.get(bundleConfKey) != null) {
142                            conf.set(bundleConfKey, jobConf.get(bundleConfKey));
143                        }
144                    }
145                    if (StringUtils.isEmpty(conf.get(OozieClient.COORDINATOR_APP_PATH))) {
146                        conf.set(OozieClient.COORDINATOR_APP_PATH, jobConf.get(OozieClient.COORDINATOR_APP_PATH));
147                    }
148                }
149            }
150            catch (Exception e) {
151                throw new CommandException(ErrorCode.E1023, e.getMessage(), e);
152            }
153        }
154        coordJob.setConf(XmlUtils.prettyPrint(conf).toString());
155        setJob(coordJob);
156        LogUtils.setLogInfo(coordJob);
157
158    }
159
160    @Override
161    protected void verifyPrecondition() throws CommandException {
162        if (coordJob.getStatus() == CoordinatorJob.Status.SUCCEEDED
163                || coordJob.getStatus() == CoordinatorJob.Status.DONEWITHERROR) {
164            LOG.info("Can't update coord job. Job has finished processing");
165            throw new CommandException(ErrorCode.E1023, "Can't update coord job. Job has finished processing");
166        }
167    }
168
169    /**
170     * Gets the difference of job definition and properties.
171     *
172     * @param eJob the e job
173     * @return the diff
174     */
175
176    private void computeDiff(Element eJob) {
177        try {
178            diff.append("**********Job definition changes**********").append(System.getProperty("line.separator"));
179            diff.append(getDiffinGitFormat(oldCoordJob.getJobXml(), XmlUtils.prettyPrint(eJob).toString()));
180            diff.append("******************************************").append(System.getProperty("line.separator"));
181            diff.append("**********Job conf changes****************").append(System.getProperty("line.separator"));
182            if (isConfChange) {
183                diff.append(getDiffinGitFormat(oldCoordJob.getConf(), XmlUtils.prettyPrint(conf).toString()));
184            }
185            else {
186                diff.append("No conf update requested").append(System.getProperty("line.separator"));
187            }
188            diff.append("******************************************").append(System.getProperty("line.separator"));
189        }
190        catch (IOException e) {
191            diff.append("Error computing diff. Error " + e.getMessage());
192            LOG.warn("Error computing diff.", e);
193        }
194    }
195
196    /**
197     * Get the differences in git format.
198     *
199     * @param string1 the string1
200     * @param string2 the string2
201     * @return the diff
202     * @throws IOException Signals that an I/O exception has occurred.
203     */
204    private String getDiffinGitFormat(String string1, String string2) throws IOException {
205        ByteArrayOutputStream out = new ByteArrayOutputStream();
206        RawText rt1 = new RawText(string1.getBytes());
207        RawText rt2 = new RawText(string2.getBytes());
208        EditList diffList = new EditList();
209        diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));
210        new DiffFormatter(out).format(diffList, rt1, rt2);
211        return out.toString();
212    }
213
214    @Override
215    protected String submit() throws CommandException {
216        LOG.info("STARTED Coordinator update");
217        submitJob();
218        LOG.info("ENDED Coordinator update");
219        if (showDiff) {
220            return diff.toString();
221        }
222        else {
223            return "";
224        }
225    }
226
227    /**
228     * Check. Frequency can't be changed. EndTime can't be changed. StartTime can't be changed. AppName can't be changed
229     * Timeunit can't be changed. Timezone can't be changed
230     *
231     * @param oldCoord the old coord
232     * @param newCoord the new coord
233     * @throws CommandException the command exception
234     */
235    public void check(CoordinatorJobBean oldCoord, CoordinatorJobBean newCoord) throws CommandException {
236        if (!oldCoord.getFrequency().equals(newCoord.getFrequency())) {
237            throw new CommandException(ErrorCode.E1023, "Frequency can't be changed. Old frequency = "
238                    + oldCoord.getFrequency() + " new frequency = " + newCoord.getFrequency());
239        }
240
241        if (!oldCoord.getEndTime().equals(newCoord.getEndTime())) {
242            throw new CommandException(ErrorCode.E1023, "End time can't be changed. Old end time = "
243                    + oldCoord.getEndTime() + " new end time = " + newCoord.getEndTime());
244        }
245
246        if (!oldCoord.getStartTime().equals(newCoord.getStartTime())) {
247            throw new CommandException(ErrorCode.E1023, "Start time can't be changed. Old start time = "
248                    + oldCoord.getStartTime() + " new start time = " + newCoord.getStartTime());
249        }
250
251        if (!oldCoord.getAppName().equals(newCoord.getAppName())) {
252            throw new CommandException(ErrorCode.E1023, "Coord name can't be changed. Old name = "
253                    + oldCoord.getAppName() + " new name = " + newCoord.getAppName());
254        }
255
256        if (!oldCoord.getTimeUnitStr().equals(newCoord.getTimeUnitStr())) {
257            throw new CommandException(ErrorCode.E1023, "Timeunit can't be changed. Old Timeunit = "
258                    + oldCoord.getTimeUnitStr() + " new Timeunit = " + newCoord.getTimeUnitStr());
259        }
260
261        if (!oldCoord.getTimeZone().equals(newCoord.getTimeZone())) {
262            throw new CommandException(ErrorCode.E1023, "TimeZone can't be changed. Old timeZone = "
263                    + oldCoord.getTimeZone() + " new timeZone = " + newCoord.getTimeZone());
264        }
265
266    }
267
268    @Override
269    protected void queueMaterializeTransitionXCommand(String jobId) {
270    }
271
272    @Override
273    public void notifyParent() throws CommandException {
274    }
275
276    @Override
277    protected boolean isLockRequired() {
278        return true;
279    }
280
281    @Override
282    public String getEntityKey() {
283        return jobId;
284    }
285
286    @Override
287    public void transitToNext() {
288    }
289
290    @Override
291    public String getKey() {
292        return getName() + "_" + jobId;
293    }
294
295    @Override
296    public String getDryRun(CoordinatorJobBean job) throws Exception{
297        return super.getDryRun(oldCoordJob);
298    }
299}