/** * Converts unchecked exceptions to appropriate API exceptions. Specifically, if the subsystem fails to acquire * the synchronization lock for a non-read operation it will throw a TimeoutException. This method converts * that to a ServiceUnavailableException. All other exceptions are rethrown as-is. * * This method never returns, it always results in an exception being thrown. The return value is present to * support more natural exception handling by the caller. */ private RuntimeException convertUncheckedException(Exception e) { if (Throwables.getRootCause(e) instanceof TimeoutException) { _lockTimeoutMeter.mark(); throw new ServiceUnavailableException("Failed to acquire update lock, try again later", new Random().nextInt(5) + 1); } throw Throwables.propagate(e); }
@Override public Response toResponse(ServiceUnavailableException exception) { return Response.status(Response.Status.SERVICE_UNAVAILABLE) .header("X-BV-Exception", ServiceUnavailableException.class.getName()) .header("Retry-After", exception.getRetryAfterSeconds()) .entity(exception) .type(MediaType.APPLICATION_JSON_TYPE) .build(); } }
protected RuntimeException convertException(EmoClientException e) { EmoResponse response = e.getResponse(); String exceptionType = response.getFirstHeader("X-BV-Exception"); if (response.getStatus() == Response.Status.BAD_REQUEST.getStatusCode() && IllegalArgumentException.class.getName().equals(exceptionType)) { return new IllegalArgumentException(response.getEntity(String.class), e); } else if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode() && UnknownMoveException.class.getName().equals(exceptionType)) { return response.getEntity(UnknownMoveException.class); } else if (response.getStatus() == Response.Status.FORBIDDEN.getStatusCode() && UnauthorizedException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(UnauthorizedException.class).initCause(e); } else { return (RuntimeException) new UnauthorizedException().initCause(e); } } else if (response.getStatus() == Response.Status.SERVICE_UNAVAILABLE.getStatusCode() && ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e); } } return e; }
ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e);
ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e);
@SuppressWarnings("ThrowableNotThrown") @Override public Response toResponse(PartitionForwardingException exception) { // To prevent herding advise the caller to retry after 1 to 5 seconds, chosen randomly. return _providers.getExceptionMapper(ServiceUnavailableException.class) .toResponse(new ServiceUnavailableException("Service unavailable, try again later", new Random().nextInt(5) + 1)); } }
ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e);
private void executeIfAcceptingWrites(Runnable runnable) { _writerPhaser.register(); try { if (_writesAccepted) { runnable.run(); } else { _writesRejectedCounter.inc(); throw new ServiceUnavailableException(); } } finally { _writerPhaser.arriveAndDeregister(); } }
ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e);
private void executeIfAcceptingWrites(Runnable runnable) { _writerPhaser.register(); try { if (_writesAccepted) { runnable.run(); } else { _writesRejectedCounter.inc(); throw new ServiceUnavailableException(); } } finally { _writerPhaser.arriveAndDeregister(); } }
ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e);
@Override public void updateAllForFacade(Iterable<Update> updates, Set<String> tags) { _writerPhaser.register(); try { Iterator<Update> updateIterator = updates.iterator(); _delegate.updateAllForFacade(closeableIterator(updateIterator), tags); if (updateIterator.hasNext()) { _writesRejectedCounter.inc(); throw new ServiceUnavailableException(); } } finally { _writerPhaser.arriveAndDeregister(); } }
ServiceUnavailableException.class.getName().equals(exceptionType)) { if (response.hasEntity()) { return (RuntimeException) response.getEntity(ServiceUnavailableException.class).initCause(e); } else { return (RuntimeException) new ServiceUnavailableException().initCause(e);
@Override public void updateAll(Iterable<Update> updates, Set<String> tags) { _writerPhaser.register(); try { Iterator<Update> updateIterator = updates.iterator(); _delegate.updateAll(closeableIterator(updateIterator), tags); if (updateIterator.hasNext()) { _writesRejectedCounter.inc(); throw new ServiceUnavailableException(); } } finally { _writerPhaser.arriveAndDeregister(); } }
@Override public void updateAll(Iterable<Update> updates, Set<String> tags) { _writerPhaser.register(); try { Iterator<Update> updateIterator = updates.iterator(); _delegate.updateAll(closeableIterator(updateIterator), tags); if (updateIterator.hasNext()) { _writesRejectedCounter.inc(); throw new ServiceUnavailableException(); } } finally { _writerPhaser.arriveAndDeregister(); } }
@Override public void updateAllForFacade(Iterable<Update> updates, Set<String> tags) { _writerPhaser.register(); try { Iterator<Update> updateIterator = updates.iterator(); _delegate.updateAllForFacade(closeableIterator(updateIterator), tags); if (updateIterator.hasNext()) { _writesRejectedCounter.inc(); throw new ServiceUnavailableException(); } } finally { _writerPhaser.arriveAndDeregister(); } }
private CreateEmoApiKeyResponse createRandomApiKey(AuthIdentityModification<ApiKey> modification) { // Since the API key is randomly generated call create in a loop to ensure we don't grab one that is already picked String key = null; String internalId = null; int attempt = 0; while (key == null && ++attempt < 10) { key = generateRandomApiKey(); try { internalId = _authIdentityManager.createIdentity(key, modification); } catch (IdentityExistsException e) { // API keys are randomly generated, so this should be exceptionally rare. Try again with // a new randomly-generated key key = null; } catch (Exception e) { throw convertUncheckedException(e); } } if (key == null) { throw new ServiceUnavailableException("Failed to generate unique key", 1); } return new CreateEmoApiKeyResponse(key, internalId); }
private String migrateToRandomApiKey(String id) { // Since the API key is randomly generated call create in a loop to ensure we don't grab one that is already picked String key = null; int attempt = 0; while (key == null && ++attempt < 10) { key = generateRandomApiKey(); try { _authIdentityManager.migrateIdentity(id, key); } catch (IdentityNotFoundException e) { throw new EmoApiKeyNotFoundException(); } catch (IdentityExistsException e) { // API keys are randomly generated, so this should be exceptionally rare. Try again with // a new randomly-generated key key = null; } catch (Exception e) { throw convertUncheckedException(e); } } if (key == null) { throw new ServiceUnavailableException("Failed to generate unique key", 1); } return key; }