Java Code Examples for org.apache.curator.utils.ThreadUtils#checkInterrupted()
The following examples show how to use
org.apache.curator.utils.ThreadUtils#checkInterrupted() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistentWatcher.java From curator with Apache License 2.0 | 6 votes |
/** * Remove the watcher */ @Override public void close() { if ( state.compareAndSet(State.STARTED, State.CLOSED) ) { listeners.clear(); client.getConnectionStateListenable().removeListener(connectionStateListener); try { client.watchers().remove(watcher).guaranteed().inBackground().forPath(basePath); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); log.debug(String.format("Could not remove watcher for path: %s", basePath), e); } } }
Example 2
Source File: NamespaceWatcher.java From xian with Apache License 2.0 | 6 votes |
@Override public void process(WatchedEvent event) { if ( client != null ) { if ( actualWatcher != null ) { actualWatcher.process(new NamespaceWatchedEvent(client, event)); } else if ( curatorWatcher != null ) { try { curatorWatcher.process(new NamespaceWatchedEvent(client, event)); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); client.logError("Watcher exception", e); } } } }
Example 3
Source File: FailedOperationManager.java From curator with Apache License 2.0 | 6 votes |
void addFailedOperation(T details) { if ( debugListener != null ) { debugListener.pathAddedForGuaranteedOperation(details); } if ( client.getState() == CuratorFrameworkState.STARTED ) { log.debug("Details being added to guaranteed operation set: " + details); try { executeGuaranteedOperationInBackground(details); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); addFailedOperation(details); } } }
Example 4
Source File: InterProcessSemaphore.java From xian with Apache License 2.0 | 6 votes |
/** * <p>Acquire <code>qty</code> leases. If there are not enough leases available, this method * blocks until either the maximum number of leases is increased enough or other clients/processes * close enough leases.</p> * * <p>The client must close the leases when it is done with them. You should do this in a * <code>finally</code> block. NOTE: You can use {@link #returnAll(Collection)} for this.</p> * * @param qty number of leases to acquire * @return the new leases * @throws Exception ZK errors, interruptions, etc. */ public Collection<Lease> acquire(int qty) throws Exception { Preconditions.checkArgument(qty > 0, "qty cannot be 0"); ImmutableList.Builder<Lease> builder = ImmutableList.builder(); try { while ( qty-- > 0 ) { String path = internals.attemptLock(-1, null, null); builder.add(makeLease(path)); } } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); returnAll(builder.build()); throw e; } return builder.build(); }
Example 5
Source File: JsonServiceInstancesMarshaller.java From curator with Apache License 2.0 | 6 votes |
@Override public ServiceInstances<T> readFrom(Class<ServiceInstances<T>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { try { List<ServiceInstance<T>> instances = Lists.newArrayList(); ObjectMapper mapper = new ObjectMapper(); JsonNode tree = mapper.reader().readTree(entityStream); for ( int i = 0; i < tree.size(); ++i ) { JsonNode node = tree.get(i); ServiceInstance<T> instance = JsonServiceInstanceMarshaller.readInstance(node, context); instances.add(instance); } return new ServiceInstances<T>(instances); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); throw new WebApplicationException(e); } }
Example 6
Source File: LeaderLatch.java From xian with Apache License 2.0 | 5 votes |
private void handleStateChange(ConnectionState newState) { switch ( newState ) { default: { // NOP break; } case RECONNECTED: { try { reset(); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); log.error("Could not reset leader latch", e); setLeadership(false); } break; } case SUSPENDED: case LOST: { setLeadership(false); break; } } }
Example 7
Source File: AsyncWrappers.java From curator with Apache License 2.0 | 5 votes |
private static void lockIf(CompletableFuture<Boolean> future, InterProcessLock lock, long timeout, TimeUnit unit) { try { future.complete(lock.acquire(timeout, unit)); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); future.completeExceptionally(e); } }
Example 8
Source File: NamespaceImpl.java From curator with Apache License 2.0 | 5 votes |
String fixForNamespace(String path, boolean isSequential) { if ( ensurePathNeeded.get() ) { try { final CuratorZookeeperClient zookeeperClient = client.getZookeeperClient(); RetryLoop.callWithRetry ( zookeeperClient, new Callable<Object>() { @Override public Object call() throws Exception { ZKPaths.mkdirs(zookeeperClient.getZooKeeper(), ZKPaths.makePath("/", namespace), true, client.getAclProvider(), true); return null; } } ); ensurePathNeeded.set(false); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); client.logError("Ensure path threw exception", e); } } return ZKPaths.fixForNamespace(namespace, path, isSequential); }
Example 9
Source File: RetryLoop.java From curator with Apache License 2.0 | 5 votes |
/** * Convenience utility: creates a retry loop calling the given proc and retrying if needed * * @param client Zookeeper * @param proc procedure to call with retry * @param <T> return type * @return procedure result * @throws Exception any non-retriable errors */ public static <T> T callWithRetry(CuratorZookeeperClient client, Callable<T> proc) throws Exception { client.internalBlockUntilConnectedOrTimedOut(); T result = null; ThreadLocalRetryLoop threadLocalRetryLoop = new ThreadLocalRetryLoop(); RetryLoop retryLoop = threadLocalRetryLoop.getRetryLoop(client::newRetryLoop); try { while ( retryLoop.shouldContinue() ) { try { result = proc.call(); retryLoop.markComplete(); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); retryLoop.takeException(e); } } } finally { threadLocalRetryLoop.release(); } return result; }
Example 10
Source File: LeaderLatch.java From xian with Apache License 2.0 | 5 votes |
/** * Remove this instance from the leadership election. If this instance is the leader, leadership * is released. IMPORTANT: the only way to release leadership is by calling close(). All LeaderLatch * instances must eventually be closed. * * @param closeMode allows the default close mode to be overridden at the time the latch is closed. * @throws IOException errors */ public synchronized void close(CloseMode closeMode) throws IOException { Preconditions.checkState(state.compareAndSet(State.STARTED, State.CLOSED), "Already closed or has not been started"); Preconditions.checkNotNull(closeMode, "closeMode cannot be null"); cancelStartTask(); try { setNode(null); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); throw new IOException(e); } finally { client.getConnectionStateListenable().removeListener(listener); switch ( closeMode ) { case NOTIFY_LEADER: { setLeadership(false); listeners.clear(); break; } default: { listeners.clear(); setLeadership(false); break; } } } }
Example 11
Source File: InterProcessMultiLock.java From curator with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} * * <p>NOTE: locks are released in the reverse order that they were acquired.</p> * * @throws Exception ZK errors, interruptions, current thread does not own the lock * */ @Override public synchronized void release() throws Exception { Exception baseException = null; for ( InterProcessLock lock : reverse(locks) ) { try { lock.release(); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); if ( baseException == null ) { baseException = e; } else { baseException = new Exception(baseException); } } } if ( baseException != null ) { throw baseException; } }
Example 12
Source File: TreeCache.java From xian with Apache License 2.0 | 5 votes |
@Override public void process(WatchedEvent event) { LOG.debug("process: {}", event); try { switch ( event.getType() ) { case NodeCreated: Preconditions.checkState(parent == null, "unexpected NodeCreated on non-root node"); wasCreated(); break; case NodeChildrenChanged: refreshChildren(); break; case NodeDataChanged: refreshData(); break; case NodeDeleted: wasDeleted(); break; } } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); handleException(e); } }
Example 13
Source File: PathChildrenCache.java From xian with Apache License 2.0 | 5 votes |
private void handleStateChange(ConnectionState newState) { switch ( newState ) { case SUSPENDED: { offerOperation(new EventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CONNECTION_SUSPENDED, null))); break; } case LOST: { offerOperation(new EventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CONNECTION_LOST, null))); break; } case CONNECTED: case RECONNECTED: { try { offerOperation(new RefreshOperation(this, RefreshMode.FORCE_GET_DATA_AND_STAT)); offerOperation(new EventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CONNECTION_RECONNECTED, null))); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); handleException(e); } break; } } }
Example 14
Source File: NamespaceImpl.java From xian with Apache License 2.0 | 5 votes |
String fixForNamespace(String path, boolean isSequential) { if ( ensurePathNeeded.get() ) { try { final CuratorZookeeperClient zookeeperClient = client.getZookeeperClient(); RetryLoop.callWithRetry ( zookeeperClient, new Callable<Object>() { @Override public Object call() throws Exception { ZKPaths.mkdirs(zookeeperClient.getZooKeeper(), ZKPaths.makePath("/", namespace), true, client.getAclProvider(), true); return null; } } ); ensurePathNeeded.set(false); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); client.logError("Ensure path threw exception", e); } } return ZKPaths.fixForNamespace(namespace, path, isSequential); }
Example 15
Source File: SessionFailRetryLoop.java From curator with Apache License 2.0 | 5 votes |
/** * Convenience utility: creates a "session fail" retry loop calling the given proc * * @param client Zookeeper * @param mode how to handle session failures * @param proc procedure to call with retry * @param <T> return type * @return procedure result * @throws Exception any non-retriable errors */ public static<T> T callWithRetry(CuratorZookeeperClient client, Mode mode, Callable<T> proc) throws Exception { T result = null; SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(mode); retryLoop.start(); try { while ( retryLoop.shouldContinue() ) { try { result = proc.call(); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); retryLoop.takeException(e); } } } finally { retryLoop.close(); } return result; }
Example 16
Source File: ModeledCacheImpl.java From curator with Apache License 2.0 | 5 votes |
@Override public void childEvent(CuratorFramework client, TreeCacheEvent event) { try { internalChildEvent(event); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); listenerContainer.forEach(l -> l.handleException(e)); } }
Example 17
Source File: CuratorFrameworkImpl.java From xian with Apache License 2.0 | 4 votes |
private void performBackgroundOperation(OperationAndData<?> operationAndData) { try { if ( client.isConnected() ) { operationAndData.callPerformBackgroundOperation(); } else { client.getZooKeeper(); // important - allow connection resets, timeouts, etc. to occur if ( operationAndData.getElapsedTimeMs() >= client.getConnectionTimeoutMs() ) { throw new CuratorConnectionLossException(); } operationAndData.sleepFor(1, TimeUnit.SECONDS); queueOperation(operationAndData); } } catch ( Throwable e ) { ThreadUtils.checkInterrupted(e); /** * Fix edge case reported as CURATOR-52. ConnectionState.checkTimeouts() throws KeeperException.ConnectionLossException * when the initial (or previously failed) connection cannot be re-established. This needs to be run through the retry policy * and callbacks need to get invoked, etc. */ if ( e instanceof CuratorConnectionLossException ) { WatchedEvent watchedEvent = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.Disconnected, null); CuratorEvent event = new CuratorEventImpl(this, CuratorEventType.WATCHED, KeeperException.Code.CONNECTIONLOSS.intValue(), null, null, operationAndData.getContext(), null, null, null, watchedEvent, null); if ( checkBackgroundRetry(operationAndData, event) ) { queueOperation(operationAndData); } else { logError("Background retry gave up", e); } } else { handleBackgroundOperationException(operationAndData, e); } } }
Example 18
Source File: CuratorFrameworkImpl.java From curator with Apache License 2.0 | 4 votes |
void performBackgroundOperation(OperationAndData<?> operationAndData) { try { if ( !operationAndData.isConnectionRequired() || client.isConnected() ) { operationAndData.callPerformBackgroundOperation(); } else { client.getZooKeeper(); // important - allow connection resets, timeouts, etc. to occur if ( operationAndData.getElapsedTimeMs() >= client.getConnectionTimeoutMs() ) { throw new CuratorConnectionLossException(); } sleepAndQueueOperation(operationAndData); } } catch ( Throwable e ) { ThreadUtils.checkInterrupted(e); /** * Fix edge case reported as CURATOR-52. ConnectionState.checkTimeouts() throws KeeperException.ConnectionLossException * when the initial (or previously failed) connection cannot be re-established. This needs to be run through the retry policy * and callbacks need to get invoked, etc. */ if ( e instanceof CuratorConnectionLossException ) { WatchedEvent watchedEvent = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.Disconnected, null); CuratorEvent event = new CuratorEventImpl(this, CuratorEventType.WATCHED, KeeperException.Code.CONNECTIONLOSS.intValue(), null, null, operationAndData.getContext(), null, null, null, watchedEvent, null, null); if ( checkBackgroundRetry(operationAndData, event) ) { queueOperation(operationAndData); } else { logError("Background retry gave up", e); } } else { handleBackgroundOperationException(operationAndData, e); } } }
Example 19
Source File: CuratorFrameworkImpl.java From curator with Apache License 2.0 | 4 votes |
@Override public void start() { log.info("Starting"); if ( !state.compareAndSet(CuratorFrameworkState.LATENT, CuratorFrameworkState.STARTED) ) { throw new IllegalStateException("Cannot be started more than once"); } try { connectionStateManager.start(); // ordering dependency - must be called before client.start() final ConnectionStateListener listener = new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if ( ConnectionState.CONNECTED == newState || ConnectionState.RECONNECTED == newState ) { logAsErrorConnectionErrors.set(true); } } @Override public boolean doNotProxy() { return true; } }; this.getConnectionStateListenable().addListener(listener); client.start(); executorService = Executors.newSingleThreadScheduledExecutor(threadFactory); executorService.submit(new Callable<Object>() { @Override public Object call() throws Exception { backgroundOperationsLoop(); return null; } }); if ( ensembleTracker != null ) { ensembleTracker.start(); } log.info(schemaSet.toDocumentation()); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); handleBackgroundOperationException(null, e); } }
Example 20
Source File: ExhibitorEnsembleProvider.java From xian with Apache License 2.0 | 4 votes |
private Map<String, String> queryExhibitors(Exhibitors localExhibitors) { Map<String, String> values = newValues(); long start = System.currentTimeMillis(); int retries = 0; boolean done = false; while ( !done ) { List<String> hostnames = Lists.newArrayList(localExhibitors.getHostnames()); if ( hostnames.size() == 0 ) { done = true; } else { String hostname = hostnames.get(random.nextInt(hostnames.size())); try { String encoded = restClient.getRaw(hostname, localExhibitors.getRestPort(), restUriPath, MIME_TYPE); values.putAll(decodeExhibitorList(encoded)); done = true; } catch ( Throwable e ) { ThreadUtils.checkInterrupted(e); if ( retryPolicy.allowRetry(retries++, System.currentTimeMillis() - start, RetryLoop.getDefaultRetrySleeper()) ) { log.warn("Couldn't get servers from Exhibitor. Retrying.", e); } else { log.error("Couldn't get servers from Exhibitor. Giving up.", e); done = true; } } } } return values; }