/** * Creates a new instance of the given {@link Class} via the default constructor. * * @param cls the {@link Class} to be instantiated * @return the instance */ public static <T> T instantiateDefault(Class<? extends T> cls) { try { return cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RheemException("Could not instantiate class.", e); } }
/** * Creates a new instance of a {@link Class} via the default constructor. * * @param className name of the {@link Class} to be instantiated * @return the instance */ public static <T> T instantiateDefault(String className) { try { @SuppressWarnings("unchecked") // Will fail anyway, if incorrect. Class<T> cls = (Class<T>) Class.forName(className); return cls.newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new RheemException("Could not instantiate class.", e); } }
/** * Ensure that there is a directory represented by the given {@link File}. * * @param file the directory that should be ensured */ public static void ensureDir(File file) { if (file.exists()) { if (!file.isDirectory()) { throw new RheemException(String.format("Could not ensure directory %s: It exists, but is not a directory.", file)); } } else if (!file.mkdirs()) { throw new RheemException(String.format("Could not ensure directory %s: It does not exist, but could also not be created.", file)); } }
/** * Loads a specific {@link Platform} implementation. For platforms to interoperate with this method, they must * provide a {@code static}, parameterless method {@code getInstance()} that returns their singleton instance. * * @param platformClassName the class name of the {@link Platform} * @return the {@link Platform} instance */ public static Platform load(String platformClassName) { try { return ReflectionUtils.executeStaticArglessMethod(platformClassName, "getInstance"); } catch (Exception e) { throw new RheemException("Could not load platform: " + platformClassName, e); } }
/** * Run this instance. Must only be called once. * * @throws RheemException in case the execution fails for any reason */ @Override public void execute() throws RheemException { try { super.execute(); } catch (RheemException e) { throw e; } catch (Throwable t) { throw new RheemException("Job execution failed.", t); } }
private PlanImplementation selectBestPlanNary(List<PlanImplementation> planImplementation) { assert !planImplementation.isEmpty(); return planImplementation.stream() .reduce(this::selectBestPlanBinary) .orElseThrow(() -> new RheemException("No plan was selected.")); }
public void set(Key key, Value value) { throw new RheemException(String.format("Setting values not supported for %s.", this.getClass().getSimpleName())); }
@Override @SuppressWarnings("unchecked") public <T> Collection<T> getBroadcast(String name) { final Broadcast<?> broadcast = this.broadcasts.get(name); if (broadcast == null) { throw new RheemException("No such broadcast found: " + name); } return (Collection<T>) broadcast.getValue(); }
@Override @SuppressWarnings("unchecked") public T deserialize(JSONObject json, Class<? extends T> cls) { if (json == null || json.equals(JSONObject.NULL)) return null; try { final Method fromJsonMethod = cls.getMethod("fromJson", JSONObject.class); return (T) fromJsonMethod.invoke(null, json); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RheemException(String.format("Could not execute %s.fromJson(...).", cls.getCanonicalName()), e); } } }
/** * Queries the {@link Job}-global cache. * * @param key that is associated with the value to be retrieved * @param resultClass the expected {@link Class} of the retrieved value * @return the value associated with the key or else {@code null} */ public <T> T queryJobCache(String key, Class<T> resultClass) { final Object value = this.queryJobCache(key); try { return resultClass.cast(value); } catch (ClassCastException e) { throw new RheemException("Job-cache value cannot be casted as requested.", e); } }
public static FileSystem requireFileSystem(String fileUrl) { return getFileSystem(fileUrl).orElseThrow( () -> new RheemException(String.format("Could not identify filesystem for \"%s\".", fileUrl)) ); }
private void initializeContext(SimpleContext ctx) { try { ctx.setFunction("math", "sqrt", Math.class.getMethod("sqrt", double.class)); ctx.setFunction("rheem", "logGrowth", OptimizationUtils.class.getMethod( "logisticGrowth", double.class, double.class, double.class, double.class) ); } catch (NoSuchMethodException e) { throw new RheemException("Could not initialize JUEL context.", e); } }
/** * As {@link #findActualInputPaths(String)} but requires the presence of only a single input file. */ public static String findActualSingleInputPath(String ostensibleInputFile) { final Collection<String> inputPaths = FileSystems.findActualInputPaths(ostensibleInputFile); if (inputPaths.size() != 1) { throw new RheemException(String.format( "Illegal number of files for \"%s\": %s", ostensibleInputFile, inputPaths )); // TODO: Add support. } return inputPaths.iterator().next(); }
/** * Create an empty file. * * @param file that should be created */ public static void touch(File file) { ensureDir(file.getParentFile()); try (FileOutputStream fos = new FileOutputStream(file)) { } catch (IOException e) { throw new RheemException(String.format("Could not create %s.", file), e); } }
/** * Creates a new {@link PlanEnumerationPruningStrategy} and configures it. * * @param strategyClass the {@link Class} of the {@link PlanEnumerationPruningStrategy}; must have a default constructor * @param configuration provides any potential configuration values * @return the configured {@link PlanEnumerationPruningStrategy} instance */ public static <T extends PlanEnumerationPruningStrategy> T createPruningStrategy(Class<T> strategyClass, Configuration configuration) { try { final T strategy = strategyClass.newInstance(); strategy.configure(configuration); return strategy; } catch (InstantiationException | IllegalAccessException e) { throw new RheemException(String.format("Could not create pruning strategy for %s.", strategyClass.getCanonicalName()), e); } }
@Override public boolean isDirectory(String url) { try { final FileStatus fileStatus = this.getHdfs(url).getFileStatus(new Path(url)); return fileStatus.isDirectory(); } catch (IOException e) { throw new RheemException(String.format("Could not access %s.", url), e); } }
public long getLongProperty(String key) { final OptionalLong optionalLongProperty = this.getOptionalLongProperty(key); if (!optionalLongProperty.isPresent()) { throw new RheemException(String.format("No value for \"%s\".", key)); } return optionalLongProperty.getAsLong(); }
@Override public Tuple<Collection<ExecutionLineageNode>, Collection<ChannelInstance>> execute( ChannelInstance[] inputChannelInstances, ChannelInstance[] outputChannelInstances, OptimizationContext.OperatorContext operatorContext) { assert inputChannelInstances.length == this.getNumInputs(); assert outputChannelInstances.length == this.getNumOutputs(); final FileChannel.Instance inputChannelInstance = (FileChannel.Instance) inputChannelInstances[0]; final StreamChannel.Instance outputChannelInstance = (StreamChannel.Instance) outputChannelInstances[0]; try { return this.runGraphChi(inputChannelInstance, outputChannelInstance, operatorContext); } catch (IOException e) { throw new RheemException(String.format("Running %s failed.", this), e); } }
@Override public void dispose() throws RheemException { try { this.doDispose(); } catch (Throwable t) { throw new RheemException(String.format("Releasing %s failed.", this), t); } finally { if (this.container != null) { this.container.unregister(this); this.container.noteDiscardedReference(true); } } }
@Override @SuppressWarnings("unchecked") public <T> Collection<T> getBroadcast(String name) { for (int i = 0; i < this.operator.getNumInputs(); i++) { final InputSlot<?> input = this.operator.getInput(i); if (input.isBroadcast() && input.getName().equals(name)) { final CollectionChannel.Instance broadcastChannelInstance = (CollectionChannel.Instance) this.inputs[i]; return (Collection<T>) broadcastChannelInstance.provideCollection(); } } throw new RheemException("No such broadcast found: " + name); }