|
| 1 | +/** |
| 2 | + * Copyright 2015 Cloudera Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.kitesdk.data.oozie; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | +import org.apache.avro.generic.GenericRecord; |
| 24 | +import org.apache.hadoop.conf.Configuration; |
| 25 | +import org.apache.oozie.ErrorCode; |
| 26 | +import org.apache.oozie.XException; |
| 27 | +import org.apache.oozie.action.hadoop.LauncherURIHandler; |
| 28 | +import org.apache.oozie.dependency.URIHandler; |
| 29 | +import org.apache.oozie.dependency.URIHandlerException; |
| 30 | +import org.apache.oozie.service.HadoopAccessorException; |
| 31 | +import org.apache.oozie.service.URIHandlerService; |
| 32 | +import org.apache.oozie.util.XLog; |
| 33 | +import org.kitesdk.data.DatasetException; |
| 34 | +import org.kitesdk.data.DatasetNotFoundException; |
| 35 | +import org.kitesdk.data.Datasets; |
| 36 | +import org.kitesdk.data.Signalable; |
| 37 | +import org.kitesdk.data.URIBuilder; |
| 38 | +import org.kitesdk.data.View; |
| 39 | + |
| 40 | +/** |
| 41 | + * A Kite URI handler that works with {@link Signalable} views. |
| 42 | + * |
| 43 | + * To be considered as {@link #exists(URI, Configuration, String) existing} the |
| 44 | + * view must have been signaled as ready. |
| 45 | + */ |
| 46 | +public class KiteURIHandler implements URIHandler { |
| 47 | + |
| 48 | + private static final XLog LOG = XLog.getLog(KiteURIHandler.class); |
| 49 | + |
| 50 | + private Set<String> supportedSchemes; |
| 51 | + private List<Class<?>> classesToShip; |
| 52 | + |
| 53 | + @Override |
| 54 | + public void init(final Configuration conf) { |
| 55 | + supportedSchemes = new HashSet<String>(); |
| 56 | + final String[] schemes = conf.getStrings( |
| 57 | + URIHandlerService.URI_HANDLER_SUPPORTED_SCHEMES_PREFIX |
| 58 | + + this.getClass().getSimpleName() |
| 59 | + + URIHandlerService.URI_HANDLER_SUPPORTED_SCHEMES_SUFFIX, "view", "dataset"); |
| 60 | + supportedSchemes.addAll(Arrays.asList(schemes)); |
| 61 | + classesToShip = new KiteLauncherURIHandler().getClassesForLauncher(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Set<String> getSupportedSchemes() { |
| 66 | + return supportedSchemes; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Class<? extends LauncherURIHandler> getLauncherURIHandlerClass() { |
| 71 | + return KiteLauncherURIHandler.class; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public List<Class<?>> getClassesForLauncher() { |
| 76 | + return classesToShip; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public DependencyType getDependencyType(final URI uri) |
| 81 | + throws URIHandlerException { |
| 82 | + return DependencyType.PULL; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void registerForNotification(final URI uri, final Configuration conf, |
| 87 | + final String user, final String actionID) throws URIHandlerException { |
| 88 | + throw new UnsupportedOperationException( |
| 89 | + "Notifications are not supported for " + uri); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean unregisterFromNotification(final URI uri, final String actionID) { |
| 94 | + throw new UnsupportedOperationException( |
| 95 | + "Notifications are not supported for " + uri); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Context getContext(final URI uri, final Configuration conf, |
| 100 | + final String user) throws URIHandlerException { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + @SuppressWarnings("rawtypes") |
| 105 | + @Override |
| 106 | + public boolean exists(final URI uri, final Context context) throws URIHandlerException { |
| 107 | + try { |
| 108 | + View<GenericRecord> view = Datasets.load(uri); |
| 109 | + if(view instanceof Signalable) { |
| 110 | + return ((Signalable)view).isReady(); |
| 111 | + } |
| 112 | + } catch (IllegalArgumentException ex) { |
| 113 | + LOG.error("the URI " + uri + " was not a view or dataset"); |
| 114 | + } catch (DatasetNotFoundException ex) { |
| 115 | + LOG.error("the dataset for the URI "+ uri +" did not actually exist"); |
| 116 | + } catch (DatasetException e) { |
| 117 | + throw new HadoopAccessorException(ErrorCode.E0902, e); |
| 118 | + } |
| 119 | + // didn't meet all the requirements for a URI/view that we want to use with oozie |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean exists(final URI uri, final Configuration conf, final String user) |
| 125 | + throws URIHandlerException { |
| 126 | + // currently does not handle access limitations between the oozie user and |
| 127 | + // a potential dataset owner |
| 128 | + return exists(uri, null); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String getURIWithDoneFlag(final String uri, final String doneFlag) |
| 133 | + throws URIHandlerException { |
| 134 | + return uri; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void validate(final String uri) throws URIHandlerException { |
| 139 | + try { |
| 140 | + URI uriParsed = URI.create(uri); |
| 141 | + String scheme = uriParsed.getScheme(); |
| 142 | + if(!(URIBuilder.VIEW_SCHEME.equals(scheme) || URIBuilder.DATASET_SCHEME.equals(scheme))) { |
| 143 | + LOG.error("Unexpected scheme: view, uri was "+ uri); |
| 144 | + XException xException = new XException(ErrorCode.E0904, scheme, uri); |
| 145 | + throw new URIHandlerException(xException); |
| 146 | + } |
| 147 | + } catch (IllegalArgumentException iae) { |
| 148 | + XException xException = new XException(ErrorCode.E0906, iae); |
| 149 | + throw new URIHandlerException(xException); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void destroy() { |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments