List of usage examples for java.util.function Supplier get
T get();
From source file:org.apache.hadoop.hbase.client.AsyncRpcRetryingCaller.java
protected void onError(Throwable error, Supplier<String> errMsg, Consumer<Throwable> updateCachedLocation) { error = translateException(error);/*from w w w. j a va2s .c o m*/ if (tries > startLogErrorsCnt) { LOG.warn(errMsg.get() + ", tries = " + tries + ", maxAttempts = " + maxAttempts + ", timeout = " + TimeUnit.NANOSECONDS.toMillis(operationTimeoutNs) + " ms, time elapsed = " + elapsedMs() + " ms", error); } RetriesExhaustedException.ThrowableWithExtraContext qt = new RetriesExhaustedException.ThrowableWithExtraContext( error, EnvironmentEdgeManager.currentTime(), ""); exceptions.add(qt); if (error instanceof DoNotRetryIOException || tries >= maxAttempts) { completeExceptionally(); return; } long delayNs; if (operationTimeoutNs > 0) { long maxDelayNs = remainingTimeNs() - SLEEP_DELTA_NS; if (maxDelayNs <= 0) { completeExceptionally(); return; } delayNs = Math.min(maxDelayNs, getPauseTime(pauseNs, tries - 1)); } else { delayNs = getPauseTime(pauseNs, tries - 1); } updateCachedLocation.accept(error); tries++; retryTimer.newTimeout(t -> doCall(), delayNs, TimeUnit.NANOSECONDS); }
From source file:org.apache.hadoop.hbase.client.AsyncSingleRequestRpcRetryingCaller.java
private void onError(Throwable error, Supplier<String> errMsg, Consumer<Throwable> updateCachedLocation) { error = translateException(error);//from w ww.j av a2 s .c o m if (tries > startLogErrorsCnt) { LOG.warn(errMsg.get(), error); } RetriesExhaustedException.ThrowableWithExtraContext qt = new RetriesExhaustedException.ThrowableWithExtraContext( error, EnvironmentEdgeManager.currentTime(), ""); exceptions.add(qt); if (error instanceof DoNotRetryIOException || tries >= maxAttempts) { completeExceptionally(); return; } long delayNs; if (operationTimeoutNs > 0) { long maxDelayNs = operationTimeoutNs - (System.nanoTime() - startNs); if (maxDelayNs <= 0) { completeExceptionally(); return; } delayNs = Math.min(maxDelayNs, getPauseTime(pauseNs, tries - 1)); } else { delayNs = getPauseTime(pauseNs, tries - 1); } updateCachedLocation.accept(error); tries++; retryTimer.newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { // always restart from beginning. locateThenCall(); } }, delayNs, TimeUnit.NANOSECONDS); }
From source file:org.apache.hadoop.hive.metastore.PersistenceManagerProvider.java
private static <T> T retry(Supplier<T> s) { Exception ex = null;/* w w w . jav a 2 s. c o m*/ int myRetryLimit = retryLimit; while (myRetryLimit > 0) { try { return s.get(); } catch (Exception e) { myRetryLimit--; boolean retriable = isRetriableException(e); if (myRetryLimit > 0 && retriable) { LOG.info("Retriable exception while invoking method, retrying. {} attempts left", myRetryLimit, e); try { Thread.sleep(retryInterval); } catch (InterruptedException ie) { // Restore the interrupted status, since we do not want to catch it. LOG.debug("Interrupted while sleeping before retrying.", ie); Thread.currentThread().interrupt(); } // If we're here, we'll proceed down the next while loop iteration. } else { // we've reached our limit, throw the last one. if (retriable) { LOG.warn("Exception retry limit reached, not retrying any longer.", e); } else { LOG.debug("Non-retriable exception.", e); } ex = e; } } } throw new RuntimeException(ex); }
From source file:org.apache.metron.parsers.ParserRunnerImpl.java
/** * Initializes MessageParsers and MessageFilters for sensor types configured in this ParserRunner. Objects are created * using reflection and the MessageParser configure and init methods are called. * @param parserConfigSupplier Parser configurations *//* w ww .j av a 2s . co m*/ private void initializeParsers(Supplier<ParserConfigurations> parserConfigSupplier) { LOG.info("Initializing parsers..."); sensorToParserComponentMap = new HashMap<>(); for (String sensorType : sensorTypes) { if (parserConfigSupplier.get().getSensorParserConfig(sensorType) == null) { throw new IllegalStateException(String.format( "Could not initialize parsers. Cannot find configuration for sensor %s.", sensorType)); } SensorParserConfig parserConfig = parserConfigSupplier.get().getSensorParserConfig(sensorType); LOG.info("Creating parser for sensor {} with parser class = {} and filter class = {} ", sensorType, parserConfig.getParserClassName(), parserConfig.getFilterClassName()); // create message parser MessageParser<JSONObject> parser = ReflectionUtils.createInstance(parserConfig.getParserClassName()); // create message filter MessageFilter<JSONObject> filter = null; parserConfig.getParserConfig().putIfAbsent("stellarContext", stellarContext); if (!StringUtils.isEmpty(parserConfig.getFilterClassName())) { filter = Filters.get(parserConfig.getFilterClassName(), parserConfig.getParserConfig()); } parser.configure(parserConfig.getParserConfig()); parser.init(); sensorToParserComponentMap.put(sensorType, new ParserComponent(parser, filter)); } }
From source file:org.apache.metron.profiler.spark.cli.BatchProfilerCLIOptions.java
BatchProfilerCLIOptions(Supplier<Option> optionSupplier) { this.option = optionSupplier.get(); }
From source file:org.apache.metron.stellar.common.utils.StellarProcessorUtils.java
public static void runWithArguments(String function, List<Object> arguments, Object expected) { Supplier<Stream<Map.Entry<String, Object>>> kvStream = () -> StreamSupport .stream(new XRange(arguments.size()), false) .map(i -> new AbstractMap.SimpleImmutableEntry<>("var" + i, arguments.get(i))); String args = kvStream.get().map(kv -> kv.getKey()).collect(Collectors.joining(",")); Map<String, Object> variables = kvStream.get() .collect(Collectors.toMap(kv -> kv.getKey(), kv -> kv.getValue())); String stellarStatement = function + "(" + args + ")"; String reason = stellarStatement + " != " + expected + " with variables: " + variables; if (expected instanceof Double) { Assert.assertEquals(reason, (Double) expected, (Double) run(stellarStatement, variables), 1e-6); } else {//from w ww . j a v a2s.co m Assert.assertEquals(reason, expected, run(stellarStatement, variables)); } }
From source file:org.apache.nifi.cluster.coordination.node.NodeClusterCoordinator.java
private NodeIdentifier waitForNodeIdentifier(final Supplier<NodeIdentifier> fetchNodeId) { NodeIdentifier localNodeId = null;//from w ww. jav a 2 s .c o m while (localNodeId == null) { localNodeId = fetchNodeId.get(); if (localNodeId == null) { if (closed) { return null; } try { Thread.sleep(100L); } catch (final InterruptedException ie) { Thread.currentThread().interrupt(); return null; } } } return localNodeId; }
From source file:org.apache.nifi.controller.scheduling.ProcessorLifecycleIT.java
private void assertCondition(final Supplier<Boolean> supplier, final long delayToleranceMillis) { final long startTime = System.currentTimeMillis(); while (((System.currentTimeMillis() - startTime) < delayToleranceMillis) && !supplier.get()) { try {//from ww w . j av a 2 s.c o m Thread.sleep(50); } catch (InterruptedException ex) { Thread.interrupted(); break; } } assertTrue(supplier.get()); }
From source file:org.apache.nifi.minifi.bootstrap.configuration.ingestors.FileChangeIngestor.java
@Override public void initialize(Properties properties, ConfigurationFileHolder configurationFileHolder, ConfigurationChangeNotifier configurationChangeNotifier) { final String rawPath = properties.getProperty(CONFIG_FILE_PATH_KEY); final String rawPollingDuration = properties.getProperty(POLLING_PERIOD_INTERVAL_KEY, Long.toString(DEFAULT_POLLING_PERIOD_INTERVAL)); if (rawPath == null || rawPath.isEmpty()) { throw new IllegalArgumentException( "Property, " + CONFIG_FILE_PATH_KEY + ", for the path of the config file must be specified."); }/*from w ww. j a v a 2 s .c o m*/ try { setConfigFilePath(Paths.get(rawPath)); setPollingPeriod(Long.parseLong(rawPollingDuration), DEFAULT_POLLING_PERIOD_UNIT); setWatchService(initializeWatcher(configFilePath)); } catch (Exception e) { throw new IllegalStateException("Could not successfully initialize file change notifier.", e); } this.configurationChangeNotifier = configurationChangeNotifier; final String differentiatorName = properties.getProperty(DIFFERENTIATOR_KEY); if (differentiatorName != null && !differentiatorName.isEmpty()) { Supplier<Differentiator<InputStream>> differentiatorSupplier = DIFFERENTIATOR_CONSTRUCTOR_MAP .get(differentiatorName); if (differentiatorSupplier == null) { throw new IllegalArgumentException( "Property, " + DIFFERENTIATOR_KEY + ", has value " + differentiatorName + " which does not " + "correspond to any in the PullHttpChangeIngestor Map:" + DIFFERENTIATOR_CONSTRUCTOR_MAP.keySet()); } differentiator = differentiatorSupplier.get(); } else { differentiator = WholeConfigDifferentiator.getInputStreamDifferentiator(); } differentiator.initialize(properties, configurationFileHolder); }
From source file:org.apache.nifi.minifi.bootstrap.configuration.ingestors.RestChangeIngestor.java
@Override public void initialize(Properties properties, ConfigurationFileHolder configurationFileHolder, ConfigurationChangeNotifier configurationChangeNotifier) { logger.info("Initializing"); final String differentiatorName = properties.getProperty(DIFFERENTIATOR_KEY); if (differentiatorName != null && !differentiatorName.isEmpty()) { Supplier<Differentiator<InputStream>> differentiatorSupplier = DIFFERENTIATOR_CONSTRUCTOR_MAP .get(differentiatorName); if (differentiatorSupplier == null) { throw new IllegalArgumentException( "Property, " + DIFFERENTIATOR_KEY + ", has value " + differentiatorName + " which does not " + "correspond to any in the PullHttpChangeIngestor Map:" + DIFFERENTIATOR_CONSTRUCTOR_MAP.keySet()); }/*from ww w. java 2 s . com*/ differentiator = differentiatorSupplier.get(); } else { differentiator = WholeConfigDifferentiator.getInputStreamDifferentiator(); } differentiator.initialize(properties, configurationFileHolder); // create the secure connector if keystore location is specified if (properties.getProperty(KEYSTORE_LOCATION_KEY) != null) { createSecureConnector(properties); } else { // create the unsecure connector otherwise createConnector(properties); } this.configurationChangeNotifier = configurationChangeNotifier; HandlerCollection handlerCollection = new HandlerCollection(true); handlerCollection.addHandler(new JettyHandler()); jetty.setHandler(handlerCollection); }