diff --git a/cdap-common/pom.xml b/cdap-common/pom.xml index cc81058e854b..22892e382584 100644 --- a/cdap-common/pom.xml +++ b/cdap-common/pom.xml @@ -261,7 +261,6 @@ org.apache.maven.plugins maven-jar-plugin - 2.4 test-jar diff --git a/cdap-common/src/main/java/io/cdap/cdap/common/service/Services.java b/cdap-common/src/main/java/io/cdap/cdap/common/service/Services.java index 74d619454692..2a5510d6d110 100644 --- a/cdap-common/src/main/java/io/cdap/cdap/common/service/Services.java +++ b/cdap-common/src/main/java/io/cdap/cdap/common/service/Services.java @@ -85,4 +85,54 @@ public static void startAndWait(Service service, long timeout, TimeUnit timeoutU throws TimeoutException, InterruptedException, ExecutionException { startAndWait(service, timeout, timeoutUnit, null); } + + /** + * Starts a service and waits for it to be running, using reflection + * to be compatible with both Guava 13 and Guava 15+ / 20+. + */ + public static void startAndWait(Service service) { + try { + try { + // Guava 15+ + service.getClass().getMethod("startAsync").invoke(service); + service.getClass().getMethod("awaitRunning").invoke(service); + } catch (NoSuchMethodException e) { + // Guava 13 + Object future = service.getClass().getMethod("start").invoke(service); + if (future instanceof ListenableFuture) { + ((ListenableFuture) future).get(); + } + } + } catch (Exception e) { + if (e.getCause() instanceof RuntimeException) { + throw (RuntimeException) e.getCause(); + } + throw new RuntimeException(e.getCause() != null ? e.getCause() : e); + } + } + + /** + * Stops a service and waits for it to be terminated, using reflection + * to be compatible with both Guava 13 and Guava 15+ / 20+. + */ + public static void stopAndWait(Service service) { + try { + try { + // Guava 15+ + service.getClass().getMethod("stopAsync").invoke(service); + service.getClass().getMethod("awaitTerminated").invoke(service); + } catch (NoSuchMethodException e) { + // Guava 13 + Object future = service.getClass().getMethod("stop").invoke(service); + if (future instanceof ListenableFuture) { + ((ListenableFuture) future).get(); + } + } + } catch (Exception e) { + if (e.getCause() instanceof RuntimeException) { + throw (RuntimeException) e.getCause(); + } + throw new RuntimeException(e.getCause() != null ? e.getCause() : e); + } + } } diff --git a/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/NettyRouter.java b/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/NettyRouter.java index 022874607582..3caf64fb3eea 100644 --- a/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/NettyRouter.java +++ b/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/NettyRouter.java @@ -16,8 +16,8 @@ package io.cdap.cdap.gateway.router; -import com.google.common.base.Strings; import com.google.common.base.Throwables; +import com.google.common.base.Strings; import com.google.common.util.concurrent.AbstractIdleService; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.inject.Inject; @@ -141,7 +141,7 @@ public Optional getBoundAddress() { protected void startUp() throws Exception { // If internal authorization enforcement is enabled, we avoid re-initialization of the token manager. if (SecurityUtil.isManagedSecurity(cConf) && !SecurityUtil.isInternalAuthEnabled(cConf)) { - tokenValidator.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(tokenValidator); } ChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); serverCancellable = startServer(createServerBootstrap(channelGroup), channelGroup); @@ -157,14 +157,14 @@ protected void shutDown() { serverCancellable.cancel(); // If internal authorization enforcement is enabled, we avoid duplicate cleanup of the token manager. if (SecurityUtil.isManagedSecurity(cConf) && !SecurityUtil.isInternalAuthEnabled(cConf)) { - tokenValidator.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(tokenValidator); + } LOG.info("Stopped Netty Router."); } - @Override - protected Executor executor(final State state) { + protected Executor executor() { final AtomicInteger id = new AtomicInteger(); return runnable -> { Thread t = new Thread(runnable, String.format("NettyRouter-%d", id.incrementAndGet())); diff --git a/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/RouterMain.java b/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/RouterMain.java index 970a6135fbd8..3f9b2db69b6b 100644 --- a/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/RouterMain.java +++ b/cdap-gateway/src/main/java/io/cdap/cdap/gateway/router/RouterMain.java @@ -101,7 +101,7 @@ public void init(String[] args) { LOG.info("Router initialized."); } catch (Throwable t) { LOG.error(t.getMessage(), t); - throw Throwables.propagate(t); + throw new RuntimeException(t); } } @@ -116,7 +116,7 @@ public void start() throws Exception { + "ZooKeeper quorum settings are correct in " + "cdap-site.xml. Currently configured as: %s", zkClientService.getConnectString())); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); LOG.info("Router started."); } diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/GatewayTestBase.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/GatewayTestBase.java index b23aaca4a0ea..2c67ede92972 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/GatewayTestBase.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/GatewayTestBase.java @@ -16,7 +16,6 @@ package io.cdap.cdap.gateway; -import com.google.common.io.Closeables; import com.google.common.util.concurrent.Service; import com.google.gson.Gson; import com.google.gson.JsonObject; @@ -179,38 +178,38 @@ protected void configure() { messagingService = injector.getInstance(MessagingService.class); if (messagingService instanceof Service) { - ((Service) messagingService).startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait((Service) messagingService); } txService = injector.getInstance(TransactionManager.class); - txService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(txService); // Define all StructuredTable before starting any services that need StructuredTable StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class)); metadataStorage = injector.getInstance(MetadataStorage.class); metadataStorage.createIndex(); metadataService = injector.getInstance(MetadataService.class); - metadataService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(metadataService); dsOpService = injector.getInstance(DatasetOpExecutorService.class); - dsOpService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(dsOpService); datasetService = injector.getInstance(DatasetService.class); - datasetService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(datasetService); appFabricServer = injector.getInstance(AppFabricServer.class); - appFabricServer.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(appFabricServer); appFabricProcessorService = injector.getInstance(AppFabricProcessorService.class); - appFabricProcessorService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(appFabricProcessorService); logQueryService = injector.getInstance(LogQueryService.class); - logQueryService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(logQueryService); metricsQueryService = injector.getInstance(MetricsQueryService.class); - metricsQueryService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(metricsQueryService); metricsCollectionService = injector.getInstance(MetricsCollectionService.class); - metricsCollectionService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(metricsCollectionService); namespaceAdmin = injector.getInstance(NamespaceAdmin.class); namespaceAdmin.create(TEST_NAMESPACE_META1); namespaceAdmin.create(TEST_NAMESPACE_META2); // Restart handlers to check if they are resilient across restarts. router = injector.getInstance(NettyRouter.class); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); port = router.getBoundAddress().orElseThrow(IllegalStateException::new).getPort(); return injector; @@ -220,19 +219,25 @@ public static void stopGateway(CConfiguration conf) throws Exception { namespaceAdmin.delete(new NamespaceId(TEST_NAMESPACE1)); namespaceAdmin.delete(new NamespaceId(TEST_NAMESPACE2)); namespaceAdmin.delete(NamespaceId.DEFAULT); - appFabricServer.stopAndWait(); - appFabricProcessorService.stopAndWait(); - metricsCollectionService.stopAndWait(); - metricsQueryService.stopAndWait(); - logQueryService.stopAndWait(); - router.stopAndWait(); - datasetService.stopAndWait(); - dsOpService.stopAndWait(); - metadataService.stopAndWait(); - Closeables.closeQuietly(metadataStorage); - txService.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(appFabricServer); + io.cdap.cdap.common.service.Services.stopAndWait(appFabricProcessorService); + io.cdap.cdap.common.service.Services.stopAndWait(metricsCollectionService); + io.cdap.cdap.common.service.Services.stopAndWait(metricsQueryService); + io.cdap.cdap.common.service.Services.stopAndWait(logQueryService); + io.cdap.cdap.common.service.Services.stopAndWait(router); + io.cdap.cdap.common.service.Services.stopAndWait(datasetService); + io.cdap.cdap.common.service.Services.stopAndWait(dsOpService); + io.cdap.cdap.common.service.Services.stopAndWait(metadataService); + try { + + metadataStorage.close(); + + } catch (Exception ignored) { + + } + io.cdap.cdap.common.service.Services.stopAndWait(txService); if (messagingService instanceof Service) { - ((Service) messagingService).stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait((Service) messagingService); } conf.clear(); } diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/log/LogHttpHandlerTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/log/LogHttpHandlerTest.java index 0f9300c579c4..abbcd7137caf 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/log/LogHttpHandlerTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/log/LogHttpHandlerTest.java @@ -149,18 +149,18 @@ protected void configure() { })); transactionManager = injector.getInstance(TransactionManager.class); - transactionManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(transactionManager); StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class)); dsOpService = injector.getInstance(DatasetOpExecutorService.class); - dsOpService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(dsOpService); datasetService = injector.getInstance(DatasetService.class); - datasetService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(datasetService); logQueryService = injector.getInstance(LogQueryService.class); - logQueryService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(logQueryService); - mockLogReader = (MockLogReader) injector.getInstance(LogReader.class); + mockLogReader = (MockLogReader) injector.getInstance(LogReader.class); mockLogReader.generateLogs(); discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class); @@ -168,11 +168,10 @@ protected void configure() { @AfterClass public static void tearDown() { - logQueryService.stopAndWait(); - - datasetService.stopAndWait(); - dsOpService.stopAndWait(); - transactionManager.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(logQueryService); + io.cdap.cdap.common.service.Services.stopAndWait(datasetService); + io.cdap.cdap.common.service.Services.stopAndWait(dsOpService); + io.cdap.cdap.common.service.Services.stopAndWait(transactionManager); } @Test diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/metrics/MetricsSuiteTestBase.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/metrics/MetricsSuiteTestBase.java index 58375282b8b3..ad19f903ab88 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/metrics/MetricsSuiteTestBase.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/handlers/metrics/MetricsSuiteTestBase.java @@ -174,20 +174,20 @@ protected void configure() { })); transactionManager = injector.getInstance(TransactionManager.class); - transactionManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(transactionManager); StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class)); dsOpService = injector.getInstance(DatasetOpExecutorService.class); - dsOpService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(dsOpService); datasetService = injector.getInstance(DatasetService.class); - datasetService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(datasetService); metrics = injector.getInstance(MetricsQueryService.class); - metrics.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(metrics); collectionService = injector.getInstance(MetricsCollectionService.class); - collectionService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(collectionService); // initialize the dataset instantiator DiscoveryServiceClient discoveryClient = injector.getInstance(DiscoveryServiceClient.class); @@ -202,11 +202,11 @@ protected void configure() { } public static void stopMetricsService(CConfiguration conf) { - collectionService.stopAndWait(); - datasetService.stopAndWait(); - dsOpService.stopAndWait(); - transactionManager.stopAndWait(); - metrics.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(collectionService); + io.cdap.cdap.common.service.Services.stopAndWait(datasetService); + io.cdap.cdap.common.service.Services.stopAndWait(dsOpService); + io.cdap.cdap.common.service.Services.stopAndWait(transactionManager); + io.cdap.cdap.common.service.Services.stopAndWait(metrics); conf.clear(); } diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuditLogTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuditLogTest.java index 30db4cab8093..2ed0c90e7217 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuditLogTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuditLogTest.java @@ -103,7 +103,7 @@ public static void init() throws Exception { successValidator, new MockAccessTokenIdentityExtractor(successValidator), discoveryService, new NoOpAeadCipher()); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); httpService = NettyHttpService.builder("test").setHttpHandlers(new TestHandler()).build(); httpService.start(); @@ -119,7 +119,7 @@ public static void init() throws Exception { public static void finish() throws Exception { cancelDiscovery.cancel(); httpService.stop(); - router.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(router); } @Test diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuthServerAnnounceTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuthServerAnnounceTest.java index 2bffff417718..25839ad1362f 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuthServerAnnounceTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/AuthServerAnnounceTest.java @@ -145,12 +145,12 @@ protected void startUp() { new RouterServiceLookup(cConf, (DiscoveryServiceClient) discoveryService, new RouterPathLookup()), validator, userIdentityExtractor, discoveryServiceClient, new NoOpAeadCipher()); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); } @Override protected void shutDown() { - router.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(router); } InetSocketAddress getRouterAddress() { diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ConfigBasedRequestBlockingTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ConfigBasedRequestBlockingTest.java index 48ff938ef4bf..066053deac35 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ConfigBasedRequestBlockingTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ConfigBasedRequestBlockingTest.java @@ -68,7 +68,7 @@ public static void init() throws Exception { successValidator, new MockAccessTokenIdentityExtractor(successValidator), discoveryService, new NoOpAeadCipher()); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); httpService = NettyHttpService.builder("test").setHttpHandlers(new AuditLogTest.TestHandler()) .build(); @@ -121,7 +121,7 @@ public void testRouterStatus() throws Exception { public static void finish() throws Exception { cancelDiscovery.cancel(); httpService.stop(); - router.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(router); } private void testGet(int expectedStatus, String expectedResponse, String path) diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpTest.java index 3169cc9caa25..970544afecd7 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpTest.java @@ -107,12 +107,12 @@ protected void startUp() { new RouterPathLookup()), new SuccessTokenValidator(), userIdentityExtractor, discoveryServiceClient, new NoOpAeadCipher()); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); } @Override protected void shutDown() { - router.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(router); } public InetSocketAddress getRouterAddress() { diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpsTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpsTest.java index e112b344a1a7..aefd9c98489f 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpsTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterHttpsTest.java @@ -201,12 +201,12 @@ protected void startUp() { new RouterPathLookup()), new SuccessTokenValidator(), userIdentityExtractor, discoveryServiceClient, new NoOpAeadCipher()); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); } @Override protected void shutDown() { - router.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(router); } public InetSocketAddress getRouterAddress() { diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterPipelineTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterPipelineTest.java index 422557635380..6a27c99c7dfb 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterPipelineTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterPipelineTest.java @@ -209,7 +209,7 @@ private void deploy(int num) throws Exception { LocationFactory lf = new LocalLocationFactory(TMP_FOLDER.newFolder()); Location programJar = AppJarHelper.createDeploymentJar(lf, DummyApp.class); - GATEWAY_SERVER.setExpectedJarBytes(ByteStreams.toByteArray(Locations.newInputSupplier(programJar))); + GATEWAY_SERVER.setExpectedJarBytes(ByteStreams.toByteArray(Locations.newInputSupplier(programJar).getInput())); for (int i = 0; i < num; i++) { LOG.info("Deploying {}/{}", i, num); @@ -220,7 +220,7 @@ private void deploy(int num) throws Exception { urlConn.setDoOutput(true); urlConn.setDoInput(true); - ByteStreams.copy(Locations.newInputSupplier(programJar), urlConn.getOutputStream()); + ByteStreams.copy(Locations.newInputSupplier(programJar).getInput(), urlConn.getOutputStream()); Assert.assertEquals(200, urlConn.getResponseCode()); urlConn.getInputStream().close(); urlConn.disconnect(); diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterTestBase.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterTestBase.java index 06ec876f3b8e..eda578b30650 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterTestBase.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/NettyRouterTestBase.java @@ -21,9 +21,6 @@ import com.google.common.collect.Lists; import com.google.common.io.ByteStreams; import com.google.common.util.concurrent.AbstractIdleService; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.Service; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.AsyncHttpClient; import com.ning.http.client.AsyncHttpClientConfig; @@ -69,7 +66,6 @@ import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; @@ -148,13 +144,10 @@ private String resolveUri(String path) throws URISyntaxException { @Before public void startUp() throws Exception { routerService = createRouterService(HOSTNAME, discoveryService); - List> futures = new ArrayList<>(); - futures.add(routerService.start()); + io.cdap.cdap.common.service.Services.startAndWait(routerService); for (ServerService server : allServers) { - futures.add(server.start()); + io.cdap.cdap.common.service.Services.startAndWait(server); } - Futures.allAsList(futures).get(); - // Wait for both servers of defaultService to be registered ServiceDiscovered discover = ((DiscoveryServiceClient) discoveryService) .discover(APP_FABRIC_SERVICE); @@ -173,12 +166,10 @@ public void onChange(ServiceDiscovered serviceDiscovered) { @After public void tearDown() throws Exception { - List> futures = new ArrayList<>(); for (ServerService server : allServers) { - futures.add(server.stop()); + io.cdap.cdap.common.service.Services.stopAndWait(server); } - futures.add(routerService.stop()); - Futures.successfulAsList(futures).get(); + io.cdap.cdap.common.service.Services.stopAndWait(routerService); } @Test @@ -531,7 +522,7 @@ public void testConnectionClose2() throws Exception { }); t.start(); - defaultServer1.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(defaultServer1); Assert.assertEquals(200, result.get().intValue()); Assert.assertEquals(1, defaultServer1.getNumRequests()); Assert.assertEquals(1, defaultServer2.getNumRequests()); @@ -561,9 +552,10 @@ public void testConfigReloading() throws Exception { successValidator, new MockAccessTokenIdentityExtractor(successValidator), discoveryService, new NoOpAeadCipher()); - router1.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router1); + - // Configure router with config-reloading time set to 0 + // Configure router with config-reloading time set to 0 CConfiguration cConfSpy2 = Mockito.spy(CConfiguration.create()); cConfSpy2.setLong(Constants.Router.CCONF_RELOAD_INTERVAL_SECONDS, 0); cConfSpy2.setInt(Constants.Router.ROUTER_PORT, 0); @@ -573,15 +565,17 @@ public void testConfigReloading() throws Exception { successValidator, new MockAccessTokenIdentityExtractor(successValidator), discoveryService, new NoOpAeadCipher()); - router2.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router2); - // Wait sometime for cConf to reload + + // Wait sometime for cConf to reload Thread.sleep(TimeUnit.MILLISECONDS.convert(reloadIntervalSeconds + 2, TimeUnit.SECONDS)); Mockito.verify(cConfSpy1, Mockito.times(1)).reloadConfiguration(); Mockito.verify(cConfSpy2, Mockito.never()).reloadConfiguration(); - router1.stopAndWait(); - router2.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(router1); + io.cdap.cdap.common.service.Services.stopAndWait(router2); + } protected HttpURLConnection openUrl(URL url) throws Exception { diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RouterResource.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RouterResource.java index cca9d8d4bfc7..867e38748b75 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RouterResource.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RouterResource.java @@ -77,12 +77,12 @@ protected void before() { new RouterServiceLookup(cConf, (DiscoveryServiceClient) discoveryService, new RouterPathLookup()), mockValidator, extractor, discoveryServiceClient, new NoOpAeadCipher()); - router.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(router); } @Override protected void after() { - router.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(router); } InetSocketAddress getRouterAddress() { diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RoutingToDataSetsTest.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RoutingToDataSetsTest.java index b565b5ce1067..ef1cbd1afbf3 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RoutingToDataSetsTest.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/RoutingToDataSetsTest.java @@ -84,21 +84,21 @@ public static void before() throws Exception { new RouterServiceLookup(cConf, discoveryServiceClient, new RouterPathLookup()), new SuccessTokenValidator(), userIdentityExtractor, discoveryServiceClient, new NoOpAeadCipher()); - nettyRouter.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(nettyRouter); // Starting mock DataSet service DiscoveryService discoveryService = injector.getInstance(DiscoveryService.class); mockService = new MockHttpService(discoveryService, Constants.Service.DATASET_MANAGER, new MockDatasetTypeHandler(), new MockDatasetInstanceHandler()); - mockService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(mockService); } @AfterClass public static void after() { try { - nettyRouter.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(nettyRouter); } finally { - mockService.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(mockService); } } diff --git a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ServerResource.java b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ServerResource.java index e67444d6ba7e..5e1855a1836a 100644 --- a/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ServerResource.java +++ b/cdap-gateway/src/test/java/io/cdap/cdap/gateway/router/ServerResource.java @@ -16,7 +16,6 @@ package io.cdap.cdap.gateway.router; -import com.google.common.base.Throwables; import io.cdap.cdap.api.common.Bytes; import io.cdap.cdap.common.discovery.ResolvingDiscoverable; import io.cdap.http.AbstractHttpHandler; @@ -179,7 +178,7 @@ public void finished(HttpResponder responder) { @Override public void handleError(Throwable cause) { - throw Throwables.propagate(cause); + throw new RuntimeException(cause); } }; } diff --git a/cdap-security/pom.xml b/cdap-security/pom.xml index b534ac3ee133..94b69f1d4d4f 100644 --- a/cdap-security/pom.xml +++ b/cdap-security/pom.xml @@ -190,7 +190,6 @@ org.apache.maven.plugins maven-jar-plugin - 2.4 test-jar @@ -214,7 +213,6 @@ org.apache.maven.plugins maven-jar-plugin - 2.4 org.apache.maven.plugins diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/AbstractKeyManager.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/AbstractKeyManager.java index d440b0443066..63d80590a1b9 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/AbstractKeyManager.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/AbstractKeyManager.java @@ -18,7 +18,6 @@ import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Throwables; import com.google.common.util.concurrent.AbstractIdleService; import io.cdap.cdap.api.common.Bytes; import io.cdap.cdap.common.conf.CConfiguration; @@ -157,7 +156,7 @@ public final void validateMAC(Codec codec, Signed signedMessage) throw new InvalidDigestException("Token signature is not valid!"); } } catch (IOException ioe) { - throw Throwables.propagate(ioe); + throw new RuntimeException(ioe); } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessToken.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessToken.java index 9d2941fc1479..4780e9aed2af 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessToken.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessToken.java @@ -124,10 +124,10 @@ public int hashCode() { @Override public String toString() { - return Objects.toStringHelper(this) - .add("identifier", identifier) - .add("keyId", keyId) - .add("digest", Bytes.toStringBinary(digest)) - .toString(); + return "AccessToken{" + + "identifier=" + identifier + + ", keyId=" + keyId + + ", digest=" + Bytes.toStringBinary(digest) + + "}"; } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessTokenValidator.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessTokenValidator.java index 25a4896eed36..d3dd34172c85 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessTokenValidator.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/AccessTokenValidator.java @@ -42,13 +42,13 @@ public AccessTokenValidator(TokenManager tokenManager, Codec access @Override protected void startUp() throws Exception { LOG.info("Starting up AccessTokenValidator service"); - tokenManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager); } @Override protected void shutDown() throws Exception { LOG.info("Shutting down AccessTokenValidator service"); - tokenManager.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(tokenManager); } @Override diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/DistributedKeyManager.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/DistributedKeyManager.java index 2b0493c7b196..e06b764b62d9 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/DistributedKeyManager.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/DistributedKeyManager.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.auth; -import com.google.common.base.Throwables; import com.google.inject.Inject; import io.cdap.cdap.common.conf.CConfiguration; import io.cdap.cdap.common.conf.Constants; @@ -96,7 +95,7 @@ protected void doInit() { try { keyCache.init(); } catch (InterruptedException ie) { - throw Throwables.propagate(ie); + throw new RuntimeException(ie); } this.leaderElection = new LeaderElection(zookeeper, "/leader", new ElectionHandler() { @Override @@ -114,7 +113,7 @@ public void follower() { LOG.debug("Transitioned to follower"); } }); - this.leaderElection.start(); + io.cdap.cdap.common.service.Services.startAndWait(this.leaderElection); startExpirationThread(); } @@ -123,7 +122,7 @@ public void shutDown() { if (timer != null) { timer.cancel(); } - leaderElection.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(leaderElection); } @Override diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/KeyIdentifier.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/KeyIdentifier.java index 69f5d2a81bb5..6982df5bb8d0 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/KeyIdentifier.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/KeyIdentifier.java @@ -106,9 +106,9 @@ public int hashCode() { @Override public String toString() { - return Objects.toStringHelper(this) - .add("keyId", keyId) - .add("expiration", expiration) - .toString(); + return "KeyIdentifier{" + + "keyId=" + keyId + + ", expiration=" + expiration + + "}"; } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/TokenManager.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/TokenManager.java index 3749baf1e6fb..1263da20cbd2 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/TokenManager.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/TokenManager.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.auth; -import com.google.common.base.Throwables; import com.google.common.util.concurrent.AbstractIdleService; import com.google.inject.Inject; import io.cdap.cdap.common.io.Codec; @@ -44,13 +43,13 @@ public TokenManager(KeyManager keyManager, Codec identifierCodec) @Override public void startUp() { LOG.info("Starting TokenManager service"); - this.keyManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(this.keyManager); } @Override public void shutDown() { LOG.info("Shutting down TokenManager service."); - this.keyManager.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(this.keyManager); } /** @@ -64,7 +63,7 @@ public AccessToken signIdentifier(UserIdentity identifier) { KeyManager.DigestId digest = keyManager.generateMAC(identifierCodec.encode(identifier)); return new AccessToken(identifier, digest.getId(), digest.getDigest()); } catch (IOException ioe) { - throw Throwables.propagate(ioe); + throw new RuntimeException(ioe); } catch (InvalidKeyException ike) { throw new IllegalStateException("Invalid key configured for KeyManager.", ike); } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/UserIdentity.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/UserIdentity.java index c1690d982a77..f27f762611c2 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/UserIdentity.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/UserIdentity.java @@ -162,12 +162,12 @@ public int hashCode() { @Override public String toString() { - return Objects.toStringHelper(this) - .add("username", username) - .add("tokenType", identifierType) - .add("groups", groups) - .add("issueTimestamp", issueTimestamp) - .add("expireTimestamp", expireTimestamp) - .toString(); + return "UserIdentity{" + + "username='" + username + '\'' + + ", tokenType=" + identifierType + + ", groups=" + groups + + ", issueTimestamp=" + issueTimestamp + + ", expireTimestamp=" + expireTimestamp + + "}"; } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/AuthenticationContextModules.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/AuthenticationContextModules.java index c451542c0415..21a0a3db1c4b 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/AuthenticationContextModules.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/AuthenticationContextModules.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.auth.context; -import com.google.common.base.Throwables; import com.google.inject.AbstractModule; import com.google.inject.Inject; import com.google.inject.Injector; @@ -158,7 +157,8 @@ private String getUsername() { try { return UserGroupInformation.getCurrentUser().getShortUserName(); } catch (IOException e) { - throw Throwables.propagate(e); + throw new RuntimeException(e); + } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/MasterAuthenticationContext.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/MasterAuthenticationContext.java index 06657748e00d..8d827a039bec 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/MasterAuthenticationContext.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/MasterAuthenticationContext.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.auth.context; -import com.google.common.base.Throwables; import io.cdap.cdap.proto.security.Credential; import io.cdap.cdap.proto.security.Principal; import io.cdap.cdap.security.spi.authentication.AuthenticationContext; @@ -51,7 +50,7 @@ public Principal getPrincipal() { try { userId = UserGroupInformation.getCurrentUser().getShortUserName(); } catch (IOException e) { - throw Throwables.propagate(e); + throw new RuntimeException(e); } } return new Principal(userId, Principal.PrincipalType.USER, userCredential); diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/SystemAuthenticationContext.java b/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/SystemAuthenticationContext.java index c3e19e4b647b..52130ce282da 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/SystemAuthenticationContext.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/auth/context/SystemAuthenticationContext.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.auth.context; -import com.google.common.base.Throwables; import com.google.inject.Inject; import io.cdap.cdap.proto.security.Credential; import io.cdap.cdap.proto.security.Principal; @@ -80,7 +79,7 @@ public Principal getPrincipal() { try { userId = UserGroupInformation.getCurrentUser().getShortUserName(); } catch (IOException e) { - throw Throwables.propagate(e); + throw new RuntimeException(e); } long currentTimestamp = System.currentTimeMillis(); UserIdentity identity = new UserIdentity(userId, UserIdentity.IdentifierType.INTERNAL, diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AccessControllerInstantiator.java b/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AccessControllerInstantiator.java index 9b52be01fdde..2abe6ae9e89c 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AccessControllerInstantiator.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AccessControllerInstantiator.java @@ -19,8 +19,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.base.Supplier; -import com.google.common.base.Throwables; -import com.google.common.io.Closeables; import com.google.common.reflect.TypeToken; import com.google.inject.Inject; import io.cdap.cdap.common.conf.CConfiguration; @@ -158,7 +156,7 @@ public AccessControllerSpi get() { accessControllerClassLoader); return accessController; } catch (Exception e) { - throw Throwables.propagate(e); + throw new RuntimeException(e); } } } @@ -311,7 +309,14 @@ public void close() throws IOException { } catch (Throwable t) { LOG.warn("Failed to destroy accessController.", t); } finally { - Closeables.closeQuietly(accessControllerClassLoader); + try { + + accessControllerClassLoader.close(); + + } catch (Exception ignored) { + + } + } } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AuthorizerWrapper.java b/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AuthorizerWrapper.java index c326dd6cf3e9..5382065c0e0a 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AuthorizerWrapper.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/authorization/AuthorizerWrapper.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.authorization; -import com.google.common.base.Throwables; import io.cdap.cdap.api.security.AccessException; import io.cdap.cdap.common.security.AuthEnforceUtil; import io.cdap.cdap.proto.element.EntityType; @@ -54,7 +53,7 @@ public void initialize(AuthorizationContext context) { try { authorizer.initialize(context); } catch (Exception e) { - throw Throwables.propagate(e); + throw new RuntimeException(e); } } @@ -117,7 +116,7 @@ public void destroy() { try { authorizer.destroy(); } catch (Exception e) { - throw Throwables.propagate(e); + throw new RuntimeException(e); } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/impersonation/ImpersonationUtils.java b/cdap-security/src/main/java/io/cdap/cdap/security/impersonation/ImpersonationUtils.java index da4c942075b2..5b5974d025d6 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/impersonation/ImpersonationUtils.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/impersonation/ImpersonationUtils.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.impersonation; -import com.google.common.base.Throwables; import io.cdap.cdap.proto.NamespaceMeta; import java.lang.reflect.UndeclaredThrowableException; import java.security.PrivilegedExceptionAction; @@ -50,7 +49,17 @@ public T run() throws Exception { } catch (UndeclaredThrowableException e) { // UserGroupInformation#doAs will wrap any checked exceptions, so unwrap and rethrow here Throwable wrappedException = e.getUndeclaredThrowable(); - Throwables.propagateIfPossible(wrappedException); + if (wrappedException instanceof RuntimeException) { + + throw (RuntimeException) wrappedException; + + } + + if (wrappedException instanceof Error) { + + throw (Error) wrappedException; + + } if (wrappedException instanceof Exception) { throw (Exception) wrappedException; @@ -59,7 +68,7 @@ public T run() throws Exception { // this should never happen LOG.warn("Unexpected exception while executing callable as {}.", ugi.getUserName(), wrappedException); - throw Throwables.propagate(wrappedException); + throw new RuntimeException(wrappedException); } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/runtime/AuthenticationServerMain.java b/cdap-security/src/main/java/io/cdap/cdap/security/runtime/AuthenticationServerMain.java index 85399509197a..2cc7d1b05110 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/runtime/AuthenticationServerMain.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/runtime/AuthenticationServerMain.java @@ -87,7 +87,7 @@ public void start() throws Exception { + "ZooKeeper quorum settings are correct in " + "cdap-site.xml. Currently configured as: %s", zkClientService.getConnectString())); - authServer.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(authServer); } catch (Exception e) { Throwable rootCause = Throwables.getRootCause(e); if (rootCause instanceof ServiceBindException) { diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/server/ExternalAuthenticationServer.java b/cdap-security/src/main/java/io/cdap/cdap/security/server/ExternalAuthenticationServer.java index 2edf817d29c8..5fe32042374d 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/server/ExternalAuthenticationServer.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/server/ExternalAuthenticationServer.java @@ -16,9 +16,9 @@ package io.cdap.cdap.security.server; +import com.google.common.base.Throwables; import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import com.google.common.base.Throwables; import com.google.common.util.concurrent.AbstractIdleService; import com.google.inject.Inject; import com.google.inject.name.Named; @@ -293,8 +293,7 @@ private Map getAuthHandlerConfigs(Configuration configuration) { return props; } - @Override - protected Executor executor(State state) { + protected Executor executor() { final AtomicInteger id = new AtomicInteger(); //noinspection NullableProblems diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/server/GrantAccessToken.java b/cdap-security/src/main/java/io/cdap/cdap/security/server/GrantAccessToken.java index 96c57e21b042..703528525db9 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/server/GrantAccessToken.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/server/GrantAccessToken.java @@ -73,7 +73,7 @@ public GrantAccessToken(TokenManager tokenManager, public void init() { // TokenManager may have already been started in AbstractServiceMain if internal auth is enabled. if (!tokenManager.isRunning()) { - tokenManager.start(); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager); } } @@ -81,7 +81,7 @@ public void init() { * Stop the TokenManager. */ public void destroy() { - tokenManager.stop(); + io.cdap.cdap.common.service.Services.stopAndWait(tokenManager); } /** diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapAuthenticationHandler.java b/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapAuthenticationHandler.java index c98cb26b481a..3f996c8d001b 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapAuthenticationHandler.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapAuthenticationHandler.java @@ -61,9 +61,9 @@ public AppConfigurationEntry[] getAppConfigurationEntry(String s) { String ldapsVerifyCertificate = handlerProps.get("ldapsVerifyCertificate"); String useLdaps = handlerProps.get("useLdaps"); - if (Boolean.parseBoolean(Objects.firstNonNull(useLdaps, "false"))) { + if (Boolean.parseBoolean(useLdaps != null ? useLdaps : "false")) { ldapSSLVerifyCertificate = Boolean.parseBoolean( - Objects.firstNonNull(ldapsVerifyCertificate, "true")); + ldapsVerifyCertificate != null ? ldapsVerifyCertificate : "true"); } return new AppConfigurationEntry[]{ diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapLoginModule.java b/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapLoginModule.java index 75e5d14ff503..2d755b010eca 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapLoginModule.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/server/LdapLoginModule.java @@ -16,7 +16,6 @@ package io.cdap.cdap.security.server; -import com.google.common.base.Throwables; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; @@ -76,7 +75,7 @@ public X509Certificate[] getAcceptedIssuers() { trustAllFactory = sc.getSocketFactory(); } catch (GeneralSecurityException e) { LOG.error("Could not disable certificate verification for connections to LDAP.", e); - throw Throwables.propagate(e); + throw new RuntimeException(e); } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/store/DefaultSecureStoreService.java b/cdap-security/src/main/java/io/cdap/cdap/security/store/DefaultSecureStoreService.java index 268b3cb93b59..79830661470d 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/store/DefaultSecureStoreService.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/store/DefaultSecureStoreService.java @@ -145,11 +145,11 @@ public final void delete(String namespace, String name) throws Exception { @Override protected void startUp() throws Exception { - secureStoreService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(secureStoreService); } @Override protected void shutDown() throws Exception { - secureStoreService.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(secureStoreService); } } diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/tools/AccessTokenGeneratorService.java b/cdap-security/src/main/java/io/cdap/cdap/security/tools/AccessTokenGeneratorService.java index a6491022cfb6..3728de11b6c6 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/tools/AccessTokenGeneratorService.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/tools/AccessTokenGeneratorService.java @@ -109,7 +109,7 @@ public void stop() { } catch (Exception e) { LOG.warn("Exception when stopping AccessTokenGeneratorService", e); } - handler.tokenManager.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(handler.tokenManager); } @Override diff --git a/cdap-security/src/main/java/io/cdap/cdap/security/zookeeper/SharedResourceCache.java b/cdap-security/src/main/java/io/cdap/cdap/security/zookeeper/SharedResourceCache.java index c87e464c6b8a..6f47345c8822 100644 --- a/cdap-security/src/main/java/io/cdap/cdap/security/zookeeper/SharedResourceCache.java +++ b/cdap-security/src/main/java/io/cdap/cdap/security/zookeeper/SharedResourceCache.java @@ -60,6 +60,7 @@ public class SharedResourceCache extends AbstractLoadingCache { private static final String ZNODE_PATH_SEP = "/"; private static final int MAX_RETRIES = 3; private static final Logger LOG = LoggerFactory.getLogger(SharedResourceCache.class); + private static final java.util.concurrent.Executor DIRECT_EXECUTOR = Runnable::run; private final List znodeACL; @@ -91,7 +92,7 @@ public void init() throws InterruptedException { } } catch (ExecutionException ee) { // recheck if already created - throw Throwables.propagate(ee.getCause()); + throw new RuntimeException(ee.getCause()); } this.resources = reloadAll(); listeners.notifyUpdate(); @@ -117,7 +118,7 @@ public void onSuccess(NodeData result) { loaded.put(nodeName, resource); listeners.notifyResourceUpdate(nodeName, resource); } catch (IOException ioe) { - throw Throwables.propagate(ioe); + throw new RuntimeException(ioe); } } @@ -126,7 +127,8 @@ public void onFailure(Throwable t) { LOG.error("Failed to get data for child node {}", nodeName, t); listeners.notifyError(nodeName, t); } - }); + }, + DIRECT_EXECUTOR); LOG.debug("Added future for {}", child); } } @@ -192,14 +194,14 @@ public void onFailure(Throwable t) { listeners.notifyError(name, t); completion.setException(t); } - } - ); + }, + DIRECT_EXECUTOR); - // Block until it is done + // Block until it is done completion.get(); } catch (Exception ioe) { - throw Throwables.propagate(ioe); + throw new RuntimeException(ioe); } } @@ -228,7 +230,8 @@ public void onFailure(Throwable t) { LOG.error("Failed to remove znode {}", znode, t); listeners.notifyError(name, t); } - }); + }, + DIRECT_EXECUTOR); } /** @@ -348,7 +351,8 @@ public void onSuccess(NodeData result) { public void onFailure(Throwable t) { resourceCallback.onFailure(t); } - }); + }, + DIRECT_EXECUTOR); } private class ZKWatcher implements Watcher { @@ -403,7 +407,11 @@ public void run() { listener.onUpdate(); } catch (Throwable t) { LOG.error("Exception notifying listener {}", listener, t); - Throwables.propagateIfInstanceOf(t, Error.class); + if (t instanceof Error) { + + throw (Error) t; + + } } } } @@ -419,7 +427,11 @@ public void run() { listener.onResourceUpdate(name, resource); } catch (Throwable t) { LOG.error("Exception notifying listener {}", listener, t); - Throwables.propagateIfInstanceOf(t, Error.class); + if (t instanceof Error) { + + throw (Error) t; + + } } } } @@ -435,7 +447,11 @@ public void run() { listener.onResourceDelete(name); } catch (Throwable t) { LOG.error("Exception notifying listener {}", listener, t); - Throwables.propagateIfInstanceOf(t, Error.class); + if (t instanceof Error) { + + throw (Error) t; + + } } } } @@ -451,7 +467,11 @@ public void run() { listener.onError(name, throwable); } catch (Throwable t) { LOG.error("Exception notifying listener {}", listener, t); - Throwables.propagateIfInstanceOf(t, Error.class); + if (t instanceof Error) { + + throw (Error) t; + + } } } } diff --git a/cdap-security/src/test/java/io/cdap/cdap/security/auth/DistributedKeyManagerTest.java b/cdap-security/src/test/java/io/cdap/cdap/security/auth/DistributedKeyManagerTest.java index c37de574bf86..24e89824d031 100644 --- a/cdap-security/src/test/java/io/cdap/cdap/security/auth/DistributedKeyManagerTest.java +++ b/cdap-security/src/test/java/io/cdap/cdap/security/auth/DistributedKeyManagerTest.java @@ -117,8 +117,8 @@ public void testKeyDistribution() throws Exception { new TestingTokenManager(manager1, injector1.getInstance(UserIdentityCodec.class)); TestingTokenManager tokenManager2 = new TestingTokenManager(manager2, injector2.getInstance(UserIdentityCodec.class)); - tokenManager1.startAndWait(); - tokenManager2.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager1); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager2); long now = System.currentTimeMillis(); UserIdentity ident1 = new UserIdentity("testuser", UserIdentity.IdentifierType.EXTERNAL, @@ -136,8 +136,8 @@ public void testKeyDistribution() throws Exception { assertEquals(token1.getIdentifier().getGroups(), token2.getIdentifier().getGroups()); assertEquals(token1, token2); - tokenManager1.stopAndWait(); - tokenManager2.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(tokenManager1); + io.cdap.cdap.common.service.Services.stopAndWait(tokenManager2); } @Test @@ -160,21 +160,20 @@ protected ImmutablePair> getTokenManagerAndCode DistributedKeyManager keyManager = getKeyManager(injector1, true); TokenManager tokenManager = new TokenManager(keyManager, injector1.getInstance(UserIdentityCodec.class)); - tokenManager.startAndWait(); return new ImmutablePair<>(tokenManager, injector1.getInstance(AccessTokenCodec.class)); } private DistributedKeyManager getKeyManager(Injector injector, boolean expectLeader) throws Exception { ZKClientService zk = injector.getInstance(ZKClientService.class); - zk.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(zk); WaitableDistributedKeyManager keyManager = new WaitableDistributedKeyManager(injector.getInstance(CConfiguration.class), injector.getInstance(Key.get(new TypeLiteral>() { })), zk); - keyManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(keyManager); if (expectLeader) { Tasks.waitFor(true, () -> keyManager.getCurrentKey() != null, 5L, TimeUnit.SECONDS); } diff --git a/cdap-security/src/test/java/io/cdap/cdap/security/auth/FileBasedTokenManagerTest.java b/cdap-security/src/test/java/io/cdap/cdap/security/auth/FileBasedTokenManagerTest.java index e535d834b1b0..e9d4f14f58b6 100644 --- a/cdap-security/src/test/java/io/cdap/cdap/security/auth/FileBasedTokenManagerTest.java +++ b/cdap-security/src/test/java/io/cdap/cdap/security/auth/FileBasedTokenManagerTest.java @@ -60,7 +60,6 @@ protected ImmutablePair> getTokenManagerAndCode new FileBasedCoreSecurityModule(), new InMemoryDiscoveryModule()); TokenManager tokenManager = injector.getInstance(TokenManager.class); - tokenManager.startAndWait(); Codec tokenCodec = injector.getInstance(AccessTokenCodec.class); return new ImmutablePair<>(tokenManager, tokenCodec); } @@ -79,14 +78,14 @@ public void testFileBasedKey() throws Exception { new ConfigModule(cConf), new FileBasedCoreSecurityModule(), new InMemoryDiscoveryModule()).getInstance(TokenManager.class); - tokenManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager); TokenManager tokenManager2 = Guice.createInjector( new IOModule(), new ConfigModule(cConf), new FileBasedCoreSecurityModule(), new InMemoryDiscoveryModule()).getInstance(TokenManager.class); - tokenManager2.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager2); Assert.assertNotSame("ERROR: Both token managers refer to the same object.", tokenManager, tokenManager2); @@ -129,7 +128,7 @@ public void testKeyUpdate() throws Exception { keyFile.setLastModified(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(10)); try { - keyManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(keyManager); // Upon the key manager starts, the current key should be the same as the one from the key file. Assert.assertEquals(keyIdentifier, keyManager.currentKey); @@ -142,7 +141,7 @@ public void testKeyUpdate() throws Exception { Tasks.waitFor(keyIdentifier, () -> keyManager.currentKey, 20, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); } finally { - keyManager.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(keyManager); } } diff --git a/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestInMemoryTokenManager.java b/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestInMemoryTokenManager.java index 2c61c976fdbf..ec8a44b4dc9c 100644 --- a/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestInMemoryTokenManager.java +++ b/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestInMemoryTokenManager.java @@ -36,7 +36,6 @@ protected ImmutablePair> getTokenManagerAndCode Injector injector = Guice.createInjector(new IOModule(), new CoreSecurityRuntimeModule().getStandaloneModules(), new ConfigModule(), new InMemoryDiscoveryModule()); TokenManager tokenManager = injector.getInstance(TokenManager.class); - tokenManager.startAndWait(); Codec tokenCodec = injector.getInstance(AccessTokenCodec.class); return new ImmutablePair<>(tokenManager, tokenCodec); } diff --git a/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestTokenManager.java b/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestTokenManager.java index a4a29eb1eb54..4ee91b8ac8f1 100644 --- a/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestTokenManager.java +++ b/cdap-security/src/test/java/io/cdap/cdap/security/auth/TestTokenManager.java @@ -42,7 +42,7 @@ public abstract class TestTokenManager { public void testTokenValidation() throws Exception { ImmutablePair> pair = getTokenManagerAndCodec(); TokenManager tokenManager = pair.getFirst(); - tokenManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager); Codec tokenCodec = pair.getSecond(); long now = System.currentTimeMillis(); @@ -91,14 +91,14 @@ public void testTokenValidation() throws Exception { // expected } - tokenManager.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(tokenManager); } @Test public void testTokenSerialization() throws Exception { ImmutablePair> pair = getTokenManagerAndCodec(); TokenManager tokenManager = pair.getFirst(); - tokenManager.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(tokenManager); Codec tokenCodec = pair.getSecond(); long now = System.currentTimeMillis(); @@ -116,6 +116,6 @@ public void testTokenSerialization() throws Exception { // should be valid since we just signed it tokenManager.validateSecret(token2); - tokenManager.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(tokenManager); } } diff --git a/cdap-security/src/test/java/io/cdap/cdap/security/server/ExternalAuthenticationServerTestBase.java b/cdap-security/src/test/java/io/cdap/cdap/security/server/ExternalAuthenticationServerTestBase.java index 9e378e10531c..4e0f4b9a382a 100644 --- a/cdap-security/src/test/java/io/cdap/cdap/security/server/ExternalAuthenticationServerTestBase.java +++ b/cdap-security/src/test/java/io/cdap/cdap/security/server/ExternalAuthenticationServerTestBase.java @@ -124,14 +124,14 @@ protected void configure() { startExternalAuthenticationServer(); - server.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(server); LOG.info("Auth server running on address {}", server.getSocketAddress()); TimeUnit.SECONDS.sleep(3); } protected void tearDown() throws Exception { stopExternalAuthenticationServer(); - server.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(server); // Clear any security properties for zookeeper. System.clearProperty(Constants.External.Zookeeper.ENV_AUTH_PROVIDER_1); Configuration.setConfiguration(null); diff --git a/cdap-security/src/test/java/io/cdap/cdap/security/store/secretmanager/SecretManagerSecureStoreServiceTest.java b/cdap-security/src/test/java/io/cdap/cdap/security/store/secretmanager/SecretManagerSecureStoreServiceTest.java index b6d2cb829b02..2cdbe0250888 100644 --- a/cdap-security/src/test/java/io/cdap/cdap/security/store/secretmanager/SecretManagerSecureStoreServiceTest.java +++ b/cdap-security/src/test/java/io/cdap/cdap/security/store/secretmanager/SecretManagerSecureStoreServiceTest.java @@ -48,12 +48,12 @@ public static void setUp() throws Exception { namespaceClient.create(namespaceMeta); secureStoreService = new SecretManagerSecureStoreService(namespaceClient, new MockSecretManagerContext(), "mock", new MockSecretManager()); - secureStoreService.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(secureStoreService); } @AfterClass public static void cleanUp() { - secureStoreService.stopAndWait(); + io.cdap.cdap.common.service.Services.stopAndWait(secureStoreService); } @Test diff --git a/cdap-security/src/test/java/io/cdap/cdap/security/zookeeper/SharedResourceCacheTest.java b/cdap-security/src/test/java/io/cdap/cdap/security/zookeeper/SharedResourceCacheTest.java index 40fa80649cb4..3eb0edec11fd 100644 --- a/cdap-security/src/test/java/io/cdap/cdap/security/zookeeper/SharedResourceCacheTest.java +++ b/cdap-security/src/test/java/io/cdap/cdap/security/zookeeper/SharedResourceCacheTest.java @@ -19,7 +19,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import com.google.common.base.Stopwatch; import com.google.common.collect.Lists; import com.google.common.util.concurrent.SettableFuture; import com.google.inject.Guice; @@ -84,7 +83,7 @@ public void testCache() throws Exception { // create 2 cache instances ZKClientService zkClient1 = injector1.getInstance(ZKClientService.class); - zkClient1.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(zkClient1); SharedResourceCache cache1 = new SharedResourceCache<>(zkClient1, new StringCodec(), parentNode, acls); cache1.init(); @@ -95,7 +94,7 @@ public void testCache() throws Exception { cache1.put(key1, value1); ZKClientService zkClient2 = injector2.getInstance(ZKClientService.class); - zkClient2.startAndWait(); + io.cdap.cdap.common.service.Services.startAndWait(zkClient2); SharedResourceCache cache2 = new SharedResourceCache<>(zkClient2, new StringCodec(), parentNode, acls); cache2.init(); @@ -194,8 +193,8 @@ private void waitForEntry(SharedResourceCache cache, String key, String String value = cache.get(key); boolean isPresent = expectedValue.equals(value); - Stopwatch watch = new Stopwatch().start(); - while (!isPresent && watch.elapsedTime(TimeUnit.MILLISECONDS) < timeToWaitMillis) { + long startTime = System.currentTimeMillis(); + while (!isPresent && (System.currentTimeMillis() - startTime) < timeToWaitMillis) { TimeUnit.MILLISECONDS.sleep(200); value = cache.get(key); isPresent = expectedValue.equals(value); diff --git a/pom.xml b/pom.xml index 64c19fa20c92..ca994c6b016b 100644 --- a/pom.xml +++ b/pom.xml @@ -1596,6 +1596,7 @@ third-party-licenses/** .gitpod.yml .gitpod.Dockerfile + **/target/**