private RuntimeException handleException(RuntimeException e) { Throwable cause = e.getCause(); if (cause != null && cause instanceof TException) { throw new ServiceUnavailableException(Constants.Service.TRANSACTION, cause); } throw e; } }
@Override protected final boolean shouldRetry(Exception ex) { // Log the exception try { throw ex; } catch (ServiceUnavailableException e) { SAMPLING_LOG.warn("Failed to contact service {}. Will retry in next run.", e.getServiceName(), e); } catch (TopicNotFoundException e) { SAMPLING_LOG.warn("Failed to fetch from TMS. Will retry in next run.", e); } catch (Exception e) { SAMPLING_LOG.warn("Failed to get and process notifications. Will retry in next run", e); } return true; }
private RuntimeException handleException(RuntimeException e) { Throwable cause = e.getCause(); if (cause != null && cause instanceof TException) { throw new ServiceUnavailableException(Constants.Service.TRANSACTION, cause); } throw e; } }
@Override protected final boolean shouldRetry(Exception ex) { // Log the exception try { throw ex; } catch (ServiceUnavailableException e) { SAMPLING_LOG.warn("Failed to contact service {}. Will retry in next run.", e.getServiceName(), e); } catch (TopicNotFoundException e) { SAMPLING_LOG.warn("Failed to fetch from TMS. Will retry in next run.", e); } catch (Exception e) { SAMPLING_LOG.warn("Failed to get and process notifications. Will retry in next run", e); } return true; }
/** * Handles error response based on the given response code. */ private void handleError(int responseCode, Supplier<String> responseBodySupplier, String errorPrefix) throws IOException { switch (responseCode) { case HttpURLConnection.HTTP_OK: return; case HttpURLConnection.HTTP_BAD_REQUEST: throw new IllegalArgumentException(errorPrefix + ". Reason: " + responseBodySupplier.get()); case HttpURLConnection.HTTP_UNAVAILABLE: throw new ServiceUnavailableException(Constants.Service.MESSAGING_SERVICE); default: throw new IOException(errorPrefix + ". Reason: " + responseBodySupplier.get()); } }
/** * Handles error response based on the given response code. */ private void handleError(int responseCode, Supplier<String> responseBodySupplier, String errorPrefix) throws IOException { switch (responseCode) { case HttpURLConnection.HTTP_OK: return; case HttpURLConnection.HTTP_BAD_REQUEST: throw new IllegalArgumentException(errorPrefix + ". Reason: " + responseBodySupplier.get()); case HttpURLConnection.HTTP_UNAVAILABLE: throw new ServiceUnavailableException(Constants.Service.MESSAGING_SERVICE); default: throw new IOException(errorPrefix + ". Reason: " + responseBodySupplier.get()); } }
/** * Checks if the scheduler started completely and is functional. * * @throws ServiceUnavailableException if the scheduler is not yet functional */ private void checkStarted() { if (!Uninterruptibles.awaitUninterruptibly(startedLatch, 0, TimeUnit.SECONDS)) { throw new ServiceUnavailableException("Core scheduler"); } }
/** * Checks if the scheduler started completely and is functional. * * @throws ServiceUnavailableException if the scheduler is not yet functional */ private void checkStarted() { if (!Uninterruptibles.awaitUninterruptibly(startedLatch, 0, TimeUnit.SECONDS)) { throw new ServiceUnavailableException("Core scheduler"); } }
@Override protected InetSocketAddress getExploreServiceAddress() { Discoverable discoverable = endpointStrategySupplier.get().pick(3L, TimeUnit.SECONDS); if (discoverable != null) { return discoverable.getSocketAddress(); } throw new ServiceUnavailableException(Service.EXPLORE_HTTP_USER_SERVICE); }
@Override protected InetSocketAddress getExploreServiceAddress() { Discoverable discoverable = endpointStrategySupplier.get().pick(3L, TimeUnit.SECONDS); if (discoverable != null) { return discoverable.getSocketAddress(); } throw new ServiceUnavailableException(Service.EXPLORE_HTTP_USER_SERVICE); }
/** * Discover the service address, then append the base path and specified resource to get the URL. * * @param resource the resource to use * @return the resolved URL * @throws ServiceUnavailableException if the service could not be discovered */ public URL resolve(String resource) { Discoverable discoverable = endpointStrategySupplier.get().pick(1L, TimeUnit.SECONDS); if (discoverable == null) { throw new ServiceUnavailableException(discoverableServiceName); } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String urlStr = String.format("%s%s:%d%s%s", scheme, address.getHostName(), address.getPort(), basePath, resource); try { return new URL(urlStr); } catch (MalformedURLException e) { // shouldn't happen. If it does, it means there is some bug in the service announcer throw new IllegalStateException(String.format("Discovered service %s, but it announced malformed URL %s", discoverableServiceName, urlStr), e); } }
/** * Discover the service address, then append the base path and specified resource to get the URL. * * @param resource the resource to use * @return the resolved URL * @throws ServiceUnavailableException if the service could not be discovered */ public URL resolve(String resource) { Discoverable discoverable = endpointStrategySupplier.get().pick(1L, TimeUnit.SECONDS); if (discoverable == null) { throw new ServiceUnavailableException(discoverableServiceName); } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String urlStr = String.format("%s%s:%d%s%s", scheme, address.getHostName(), address.getPort(), basePath, resource); try { return new URL(urlStr); } catch (MalformedURLException e) { // shouldn't happen. If it does, it means there is some bug in the service announcer throw new IllegalStateException(String.format("Discovered service %s, but it announced malformed URL %s", discoverableServiceName, urlStr), e); } }
/** * Throws exception if the given response code is not 200 OK. */ private void throwIfNotOK(int responseCode, HttpURLConnection urlConn) throws IOException { switch (responseCode) { case HttpURLConnection.HTTP_OK: return; case HttpURLConnection.HTTP_BAD_REQUEST: throw new IllegalArgumentException(readError(urlConn)); case HttpURLConnection.HTTP_UNAVAILABLE: throw new ServiceUnavailableException("runtime.monitor", readError(urlConn)); default: throw new IOException("Failed to talk to runtime monitor. Response code is " + responseCode + ". Error is " + readError(urlConn)); } }
/** * Requests graceful termination of the running program. * * @throws IOException if failed to issue the command to the server * @throws IllegalArgumentException if server responded with 400 Bad Request * @throws ServiceUnavailableException if the runtime monitor server is not available */ void requestStop() throws IOException { HttpURLConnection urlConn = connect("runtime/kill"); try { urlConn.setRequestMethod("POST"); throwIfNotOK(urlConn.getResponseCode(), urlConn); } catch (ConnectException e) { throw new ServiceUnavailableException("runtime.monitor", e); } finally { releaseConnection(urlConn); } }
/** * Requests graceful termination of the running program. * * @throws IOException if failed to issue the command to the server * @throws IllegalArgumentException if server responded with 400 Bad Request * @throws ServiceUnavailableException if the runtime monitor server is not available */ void requestStop() throws IOException { HttpURLConnection urlConn = connect("runtime/kill"); try { urlConn.setRequestMethod("POST"); throwIfNotOK(urlConn.getResponseCode(), urlConn); } catch (ConnectException e) { throw new ServiceUnavailableException("runtime.monitor", e); } finally { releaseConnection(urlConn); } }
/** * Throws exception if the given response code is not 200 OK. */ private void throwIfNotOK(int responseCode, HttpURLConnection urlConn) throws IOException { switch (responseCode) { case HttpURLConnection.HTTP_OK: return; case HttpURLConnection.HTTP_BAD_REQUEST: throw new IllegalArgumentException(readError(urlConn)); case HttpURLConnection.HTTP_UNAVAILABLE: throw new ServiceUnavailableException("runtime.monitor", readError(urlConn)); default: throw new IOException("Failed to talk to runtime monitor. Response code is " + responseCode + ". Error is " + readError(urlConn)); } }
/** * Requests the runtime monitor server to shutdown. * * @throws IOException if failed to issue the command to the server * @throws IllegalArgumentException if server responded with 400 Bad Request * @throws ServiceUnavailableException if the runtime monitor server is not available */ void requestShutdown() throws IOException { HttpURLConnection urlConn = connect("runtime/shutdown"); try { urlConn.setRequestMethod("POST"); throwIfNotOK(urlConn.getResponseCode(), urlConn); } catch (ConnectException e) { throw new ServiceUnavailableException("runtime.monitor", e); } finally { releaseConnection(urlConn); } }
/** * Requests the runtime monitor server to shutdown. * * @throws IOException if failed to issue the command to the server * @throws IllegalArgumentException if server responded with 400 Bad Request * @throws ServiceUnavailableException if the runtime monitor server is not available */ void requestShutdown() throws IOException { HttpURLConnection urlConn = connect("runtime/shutdown"); try { urlConn.setRequestMethod("POST"); throwIfNotOK(urlConn.getResponseCode(), urlConn); } catch (ConnectException e) { throw new ServiceUnavailableException("runtime.monitor", e); } finally { releaseConnection(urlConn); } }
private MessagingService getMessagingService() { DelegateService delegateService = delegate.getReference(); if (delegateService == null || !delegate.isMarked()) { throw new ServiceUnavailableException(Constants.Service.MESSAGING_SERVICE, "Messaging service is temporarily unavailable due to leader transition"); } return delegateService.getMessagingService(); }
private MessagingService getMessagingService() { DelegateService delegateService = delegate.getReference(); if (delegateService == null || !delegate.isMarked()) { throw new ServiceUnavailableException(Constants.Service.MESSAGING_SERVICE, "Messaging service is temporarily unavailable due to leader transition"); } return delegateService.getMessagingService(); }