AMBARI-20236. HiveView 2.0: save as HDFS shows success message on error. (gauravn7)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/728c0daa Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/728c0daa Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/728c0daa Branch: refs/heads/branch-feature-AMBARI-12556 Commit: 728c0daa656a976b9e16985280fb0d822380342e Parents: aaec58b Author: Gaurav Nagar <[email protected]> Authored: Wed Mar 1 14:07:38 2017 +0530 Committer: Gaurav Nagar <[email protected]> Committed: Wed Mar 1 14:08:21 2017 +0530 ---------------------------------------------------------------------- .../backgroundjobs/BackgroundJobController.java | 45 +++++++++++++++---- .../backgroundjobs/BackgroundJobException.java | 30 +++++++++++++ .../view/hive2/resources/jobs/JobService.java | 5 ++- .../backgroundjobs/BackgroundJobController.java | 47 ++++++++++++++++---- .../backgroundjobs/BackgroundJobException.java | 30 +++++++++++++ .../view/hive20/resources/jobs/JobService.java | 7 +-- 6 files changed, 143 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/728c0daa/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobController.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobController.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobController.java index bd3bb23..72fb6b3 100644 --- a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobController.java +++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobController.java @@ -37,17 +37,26 @@ public class BackgroundJobController { return viewSingletonObjects.get(context.getInstanceName()); } - private Map<String, Thread> jobs = new HashMap<String, Thread>(); - public void startJob(String key, Runnable runnable) { + private Map<String, BackgroundJob> jobs = new HashMap<String, BackgroundJob>(); + public void startJob(final String key, Runnable runnable) { if (jobs.containsKey(key)) { - interrupt(key); try { - jobs.get(key).join(); + interrupt(key); + jobs.get(key).getJobThread().join(); } catch (InterruptedException ignored) { + } catch (BackgroundJobException e) { } } Thread t = new Thread(runnable); - jobs.put(key, t); + t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(Thread t, Throwable e) { + if (e instanceof BackgroundJobException) { + jobs.get(key).setJobException((BackgroundJobException) e); + } + } + }); + jobs.put(key, new BackgroundJob(t)); t.start(); } @@ -56,7 +65,7 @@ public class BackgroundJobController { return Thread.State.TERMINATED; } - Thread.State state = jobs.get(key).getState(); + Thread.State state = jobs.get(key).getJobThread().getState(); if (state == Thread.State.TERMINATED) { jobs.remove(key); @@ -70,7 +79,7 @@ public class BackgroundJobController { return false; } - jobs.get(key).interrupt(); + jobs.get(key).getJobThread().interrupt(); return true; } @@ -79,6 +88,26 @@ public class BackgroundJobController { return true; } - return jobs.get(key).isInterrupted(); + return jobs.get(key).getJobThread().isInterrupted(); } + + class BackgroundJob { + + private Thread jobThread; + private BackgroundJobException jobException; + + public BackgroundJob(Thread jobThread) { + this.jobThread = jobThread; + } + + public Thread getJobThread() { + if(jobException != null) throw jobException; + return jobThread; + } + + public void setJobException(BackgroundJobException exception) { + this.jobException = exception; + } + } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/728c0daa/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobException.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobException.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobException.java new file mode 100644 index 0000000..3830dda --- /dev/null +++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/backgroundjobs/BackgroundJobException.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.view.hive2.backgroundjobs; + +public class BackgroundJobException extends RuntimeException { + + public BackgroundJobException(Throwable cause) { + super(cause); + } + + public BackgroundJobException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/728c0daa/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/JobService.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/JobService.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/JobService.java index 6eedcca..5fa96bb 100644 --- a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/JobService.java +++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/JobService.java @@ -27,6 +27,7 @@ import org.apache.ambari.view.hive2.ConnectionFactory; import org.apache.ambari.view.hive2.ConnectionSystem; import org.apache.ambari.view.hive2.actor.message.job.Failure; import org.apache.ambari.view.hive2.backgroundjobs.BackgroundJobController; +import org.apache.ambari.view.hive2.backgroundjobs.BackgroundJobException; import org.apache.ambari.view.hive2.client.AsyncJobRunner; import org.apache.ambari.view.hive2.client.AsyncJobRunnerImpl; import org.apache.ambari.view.hive2.client.ColumnDescription; @@ -291,9 +292,9 @@ public class JobService extends BaseService { stream.close(); } catch (IOException e) { - throw new ServiceFormattedException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); + throw new BackgroundJobException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); } catch (InterruptedException e) { - throw new ServiceFormattedException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); + throw new BackgroundJobException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); } } }); http://git-wip-us.apache.org/repos/asf/ambari/blob/728c0daa/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobController.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobController.java b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobController.java index 0b21a18..3424e45 100644 --- a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobController.java +++ b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobController.java @@ -37,17 +37,26 @@ public class BackgroundJobController { return viewSingletonObjects.get(context.getInstanceName()); } - private Map<String, Thread> jobs = new HashMap<String, Thread>(); - public void startJob(String key, Runnable runnable) { + private Map<String, BackgroundJob> jobs = new HashMap<String, BackgroundJob>(); + public void startJob(final String key, Runnable runnable) { if (jobs.containsKey(key)) { - interrupt(key); try { - jobs.get(key).join(); + interrupt(key); + jobs.get(key).getJobThread().join(); } catch (InterruptedException ignored) { + } catch (BackgroundJobException e) { } } Thread t = new Thread(runnable); - jobs.put(key, t); + t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(Thread t, Throwable e) { + if (e instanceof BackgroundJobException) { + jobs.get(key).setJobException((BackgroundJobException) e); + } + } + }); + jobs.put(key, new BackgroundJob(t)); t.start(); } @@ -56,7 +65,7 @@ public class BackgroundJobController { return Thread.State.TERMINATED; } - Thread.State state = jobs.get(key).getState(); + Thread.State state = jobs.get(key).getJobThread().getState(); if (state == Thread.State.TERMINATED) { jobs.remove(key); @@ -70,7 +79,7 @@ public class BackgroundJobController { return false; } - jobs.get(key).interrupt(); + jobs.get(key).getJobThread().interrupt(); return true; } @@ -79,6 +88,28 @@ public class BackgroundJobController { return true; } - return jobs.get(key).isInterrupted(); + return jobs.get(key).getJobThread().isInterrupted(); } + + class BackgroundJob { + + private Thread jobThread; + private BackgroundJobException jobException; + + public BackgroundJob(Thread jobThread) { + this.jobThread = jobThread; + } + + public Thread getJobThread() { + if(jobException != null) throw jobException; + return jobThread; + } + + public void setJobException(BackgroundJobException exception) { + this.jobException = exception; + } + } + } + + http://git-wip-us.apache.org/repos/asf/ambari/blob/728c0daa/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobException.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobException.java b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobException.java new file mode 100644 index 0000000..2fc72ad --- /dev/null +++ b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/backgroundjobs/BackgroundJobException.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.view.hive20.backgroundjobs; + +public class BackgroundJobException extends RuntimeException { + + public BackgroundJobException(Throwable cause) { + super(cause); + } + + public BackgroundJobException(String message, Throwable cause) { + super(message, cause); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/728c0daa/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/jobs/JobService.java ---------------------------------------------------------------------- diff --git a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/jobs/JobService.java b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/jobs/JobService.java index c5479e7..b5f92ac 100644 --- a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/jobs/JobService.java +++ b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/jobs/JobService.java @@ -27,6 +27,7 @@ import org.apache.ambari.view.hive20.ConnectionFactory; import org.apache.ambari.view.hive20.ConnectionSystem; import org.apache.ambari.view.hive20.actor.message.job.Failure; import org.apache.ambari.view.hive20.backgroundjobs.BackgroundJobController; +import org.apache.ambari.view.hive20.backgroundjobs.BackgroundJobException; import org.apache.ambari.view.hive20.client.AsyncJobRunner; import org.apache.ambari.view.hive20.client.AsyncJobRunnerImpl; import org.apache.ambari.view.hive20.client.ColumnDescription; @@ -296,9 +297,9 @@ public class JobService extends BaseService { stream.close(); } catch (IOException e) { - throw new ServiceFormattedException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); + throw new BackgroundJobException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); } catch (InterruptedException e) { - throw new ServiceFormattedException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); + throw new BackgroundJobException("F010 Could not write CSV to HDFS for job#" + jobController.getJob().getId(), e); } } }); @@ -320,7 +321,7 @@ public class JobService extends BaseService { throw ex; } catch (ItemNotFound itemNotFound) { throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound); - } catch (Exception ex) { + } catch (Exception ex) { throw new ServiceFormattedException(ex.getMessage(), ex); } }
