@Override public void run() { try { if (jobControl.isActive(name())) { try (Lock lock = jobControl.curator().lockMaintenanceJob(name())) { maintain(); } } } catch (UncheckedTimeoutException e) { // another controller instance is running this job at the moment; ok } catch (Throwable t) { log.log(Level.WARNING, this + " failed. Will retry in " + maintenanceInterval.toMinutes() + " minutes", t); } }
@Override public boolean acquire(long timeout, TimeUnit unit) { if (throwExceptionOnLock) throw new CuratorLockException("Thrown by mock"); if (timeoutOnLock) return false; try { lock = locks.lock(path, timeout, unit); return true; } catch (UncheckedTimeoutException e) { return false; } }
/** Take the lock with the given timeout. This may be called multiple times from the same thread - each matched by a close */ public void acquire(Duration timeout) { boolean acquired; try { acquired = mutex.acquire(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (Exception e) { throw new RuntimeException("Exception acquiring lock '" + lockPath + "'", e); } if (! acquired) throw new UncheckedTimeoutException("Timed out after waiting " + timeout.toString() + " to acquire lock '" + lockPath + "'"); }
@Test public void test_application_lock_failure() throws InterruptedException, IOException { String message = "Timed out after waiting PT1M to acquire lock '/provision/v1/locks/foo/bar/default'"; SessionThrowingException session = new SessionThrowingException(new ApplicationLockException(new UncheckedTimeoutException(message))); localRepo.addSession(session); HttpResponse response = createHandler() .handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L)); assertEquals(500, response.getStatus()); Slime data = getData(response); assertThat(data.get().field("error-code").asString(), is(HttpErrorResponse.errorCodes.APPLICATION_LOCK_FAILURE.name())); assertThat(data.get().field("message").asString(), is(message)); }
/** Acquires the single cluster-global, reentrant lock with the specified timeout for active nodes of this application */ public Lock lock(ApplicationId application, Duration timeout) { try { return lock(lockPath(application), timeout); } catch (UncheckedTimeoutException e) { throw new ApplicationLockException(e); } }
@Override public void backupShard(UUID uuid, File source) { try { store.backupShard(uuid, source); } catch (UncheckedTimeoutException e) { throw new PrestoException(RAPTOR_BACKUP_TIMEOUT, "Shard backup timed out"); } }
@Override public void restoreShard(UUID uuid, File target) { try { store.restoreShard(uuid, target); } catch (UncheckedTimeoutException e) { throw new PrestoException(RAPTOR_BACKUP_TIMEOUT, "Shard restore timed out"); } }
@Override public void deleteShard(UUID uuid) { try { store.deleteShard(uuid); } catch (UncheckedTimeoutException e) { throw new PrestoException(RAPTOR_BACKUP_TIMEOUT, "Shard delete timed out"); } }
@Override public boolean shardExists(UUID uuid) { try { return store.shardExists(uuid); } catch (UncheckedTimeoutException e) { throw new PrestoException(RAPTOR_BACKUP_TIMEOUT, "Shard existence check timed out"); } }
private JoinQueryResultDto query(QueryType type, JoinQuery joinQuery, CountStatsData.Builder countBuilder, QueryScope scope, QueryMode mode) { boolean havePermit = false; try { havePermit = acquireSemaphorePermit(); if (havePermit) return unsafeQuery(type, joinQuery, countBuilder, scope, mode); else throw new UncheckedTimeoutException("Too many queries in a short time. Please retry later."); } finally { if (havePermit) releaseSemaphorePermit(); } }
@Override protected GeneratedMessage.ExtendableMessage<?> getErrorDto(UncheckedTimeoutException e) { return ErrorDtos.ClientErrorDto.newBuilder() .setCode(getStatus().getStatusCode()) .setMessageTemplate("server.error.search.timeout") .setMessage(e.getMessage()) .build(); }
/** * Creates a real database connection, failing if one is not obtained in the specified time. * @param timeoutMillis The timeout in milliseconds. * @return The connection. * @throws SQLException on connection problem or timeout waiting for connection. */ private Connection createRealConnection(final long timeoutMillis) throws SQLException { if(timeoutMillis < 1L) { String usePassword = getPassword(); Connection conn = dbConnection.datasource == null ? DriverManager.getConnection(dbConnection.connectionString, dbConnection.user, usePassword) : dbConnection.datasource.getConnection(dbConnection.user, usePassword); if(conn != null) { return conn; } else { throw new SQLException("Unable to create connection: driver/datasource returned [null]"); } } else { try { return connectionTimeLimiter.callWithTimeout(new Callable<Connection>() { public Connection call() throws Exception { return createRealConnection(0L); } }, timeoutMillis, TimeUnit.MILLISECONDS, true); } catch(UncheckedTimeoutException ute) { throw new SQLException("Unable to create connection after waiting " + timeoutMillis + " ms"); } catch(Exception e) { if(e instanceof SQLException) { throw (SQLException)e; } else { throw new SQLException("Unable to create connection: driver/datasource", e); } } } }
@Test(expected = UncheckedTimeoutException.class) public void shouldTriggerTimeout() throws Exception { new SimpleTimeLimiter().callWithTimeout( () -> doSomeHeavyWeightOperation(), NOT_ENOUGH_MS, MILLISECONDS, true); }
/** * Throws UncheckedTimeoutException if current time is past the provided timeout timestamp. * @param timeoutTS timestamp of when the query times out in milliseconds */ public void checkTimeout(long timeoutTS) { if(System.currentTimeMillis() > timeoutTS) { throw new UncheckedTimeoutException("The query took longer than the allowed timeout of " + executionTimeout.toString(PeriodFormat.getDefault())); } }