Search in sources :

Example 16 with ZKClientService

use of org.apache.twill.zookeeper.ZKClientService in project cdap by caskdata.

the class TwillAppLifecycleEventHandler method initialize.

@Override
public void initialize(EventHandlerContext context) {
    super.initialize(context);
    this.runningPublished = new AtomicBoolean();
    this.twillRunId = context.getRunId();
    this.programRunId = GSON.fromJson(context.getSpecification().getConfigs().get("programRunId"), ProgramRunId.class);
    // Fetch cConf and hConf from resources jar
    File cConfFile = new File("resources.jar/resources/" + CDAP_CONF_FILE_NAME);
    File hConfFile = new File("resources.jar/resources/" + HADOOP_CONF_FILE_NAME);
    if (cConfFile.exists() && hConfFile.exists()) {
        CConfiguration cConf = CConfiguration.create();
        cConf.clear();
        Configuration hConf = new Configuration();
        hConf.clear();
        try {
            cConf.addResource(cConfFile.toURI().toURL());
            hConf.addResource(hConfFile.toURI().toURL());
            // Create the injector to inject a program state writer
            Injector injector = Guice.createInjector(new ConfigModule(cConf, hConf), new ZKClientModule(), new KafkaClientModule(), new DiscoveryRuntimeModule().getDistributedModules(), new MessagingClientModule(), new AbstractModule() {

                @Override
                protected void configure() {
                    bind(ProgramStateWriter.class).to(MessagingProgramStateWriter.class);
                }
            });
            zkClientService = injector.getInstance(ZKClientService.class);
            zkClientService.startAndWait();
            this.programStateWriter = injector.getInstance(ProgramStateWriter.class);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    } else {
        LOG.warn("{} and {} were not found in the resources.jar. Not recording program states", CDAP_CONF_FILE_NAME, HADOOP_CONF_FILE_NAME);
        this.programStateWriter = new NoOpProgramStateWriter();
    }
}
Also used : MessagingClientModule(co.cask.cdap.messaging.guice.MessagingClientModule) NoOpProgramStateWriter(co.cask.cdap.app.runtime.NoOpProgramStateWriter) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Configuration(org.apache.hadoop.conf.Configuration) ConfigModule(co.cask.cdap.common.guice.ConfigModule) MessagingProgramStateWriter(co.cask.cdap.internal.app.program.MessagingProgramStateWriter) CConfiguration(co.cask.cdap.common.conf.CConfiguration) AbstractModule(com.google.inject.AbstractModule) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZKClientModule(co.cask.cdap.common.guice.ZKClientModule) ZKClientService(org.apache.twill.zookeeper.ZKClientService) MessagingProgramStateWriter(co.cask.cdap.internal.app.program.MessagingProgramStateWriter) ProgramStateWriter(co.cask.cdap.app.runtime.ProgramStateWriter) NoOpProgramStateWriter(co.cask.cdap.app.runtime.NoOpProgramStateWriter) Injector(com.google.inject.Injector) KafkaClientModule(co.cask.cdap.common.guice.KafkaClientModule) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) File(java.io.File) DiscoveryRuntimeModule(co.cask.cdap.common.guice.DiscoveryRuntimeModule)

Example 17 with ZKClientService

use of org.apache.twill.zookeeper.ZKClientService in project cdap by caskdata.

the class RouterMain method init.

@Override
public void init(String[] args) {
    LOG.info("Initializing Router...");
    try {
        // Load configuration
        cConf = CConfiguration.create();
        if (cConf.getBoolean(Constants.Security.ENABLED)) {
            int foundPaths = RouterAuditLookUp.getInstance().getNumberOfPaths();
            if (cConf.getBoolean(Constants.Router.ROUTER_AUDIT_PATH_CHECK_ENABLED) && foundPaths != ExceptedNumberOfAuditPolicyPaths.EXPECTED_PATH_NUMBER) {
                LOG.error("Failed to start the router due to the incorrect number of paths with AuditPolicy. " + "Expected: {}, found: {}", ExceptedNumberOfAuditPolicyPaths.EXPECTED_PATH_NUMBER, foundPaths);
                System.exit(1);
            }
            // Enable Kerberos login
            SecurityUtil.enableKerberosLogin(cConf);
        }
        // Initialize ZK client
        String zookeeper = cConf.get(Constants.Zookeeper.QUORUM);
        if (zookeeper == null) {
            LOG.error("No ZooKeeper quorum provided.");
            System.exit(1);
        }
        Injector injector = createGuiceInjector(cConf);
        zkClientService = injector.getInstance(ZKClientService.class);
        // Get the Router
        router = injector.getInstance(NettyRouter.class);
        // Get RouteStore so that we can close it when shutting down
        routeStore = injector.getInstance(RouteStore.class);
        LOG.info("Router initialized.");
    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw Throwables.propagate(t);
    }
}
Also used : ZKClientService(org.apache.twill.zookeeper.ZKClientService) Injector(com.google.inject.Injector) RouteStore(co.cask.cdap.route.store.RouteStore) ZKRouteStore(co.cask.cdap.route.store.ZKRouteStore)

Example 18 with ZKClientService

use of org.apache.twill.zookeeper.ZKClientService in project cdap by caskdata.

the class ResourceBalancerServiceTest method testServiceStartFailure.

@Test
public void testServiceStartFailure() throws Exception {
    ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
    zkClient.startAndWait();
    try (ZKDiscoveryService discoveryService = new ZKDiscoveryService(zkClient)) {
        // Test the failure on start case
        final TestBalancerService startFailureService = new TestBalancerService("test", 4, zkClient, discoveryService, discoveryService, true, false);
        startFailureService.startAndWait();
        // The resource balance service should fail
        Tasks.waitFor(Service.State.FAILED, new Callable<Service.State>() {

            @Override
            public Service.State call() throws Exception {
                return startFailureService.state();
            }
        }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    } finally {
        zkClient.stopAndWait();
    }
}
Also used : ZKDiscoveryService(org.apache.twill.discovery.ZKDiscoveryService) ZKClientService(org.apache.twill.zookeeper.ZKClientService) IOException(java.io.IOException) Test(org.junit.Test)

Example 19 with ZKClientService

use of org.apache.twill.zookeeper.ZKClientService in project cdap by caskdata.

the class LeaderElectionMessagingServiceTest method testFencing.

@Test
public void testFencing() throws IOException, InterruptedException, ExecutionException, TimeoutException {
    final TopicId topicId = NamespaceId.SYSTEM.topic("topic");
    // Change the fencing time
    long oldFencingDelay = cConf.getLong(Constants.MessagingSystem.HA_FENCING_DELAY_SECONDS);
    cConf.setLong(Constants.MessagingSystem.HA_FENCING_DELAY_SECONDS, 3L);
    try {
        Injector injector = createInjector(0);
        ZKClientService zkClient = injector.getInstance(ZKClientService.class);
        zkClient.startAndWait();
        final MessagingService messagingService = injector.getInstance(MessagingService.class);
        if (messagingService instanceof Service) {
            ((Service) messagingService).startAndWait();
        }
        // Shouldn't be serving request yet.
        try {
            messagingService.listTopics(NamespaceId.SYSTEM);
            Assert.fail("Expected service unavailable exception");
        } catch (ServiceUnavailableException e) {
        // expected
        }
        // Retry until pass the fencing delay (with some buffer)
        Tasks.waitFor(topicId, new Callable<TopicId>() {

            @Override
            public TopicId call() throws Exception {
                try {
                    return messagingService.getTopic(topicId).getTopicId();
                } catch (ServiceUnavailableException e) {
                    return null;
                }
            }
        }, 10L, TimeUnit.SECONDS, 200, TimeUnit.MILLISECONDS);
        if (messagingService instanceof Service) {
            ((Service) messagingService).stopAndWait();
        }
        zkClient.stopAndWait();
    } finally {
        cConf.setLong(Constants.MessagingSystem.HA_FENCING_DELAY_SECONDS, oldFencingDelay);
    }
}
Also used : ZKClientService(org.apache.twill.zookeeper.ZKClientService) Injector(com.google.inject.Injector) MessagingService(co.cask.cdap.messaging.MessagingService) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) ZKClientService(org.apache.twill.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) TopicId(co.cask.cdap.proto.id.TopicId) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) TimeoutException(java.util.concurrent.TimeoutException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MessagingService(co.cask.cdap.messaging.MessagingService) Test(org.junit.Test)

Example 20 with ZKClientService

use of org.apache.twill.zookeeper.ZKClientService in project cdap by caskdata.

the class TransactionServiceTest method testHA.

@Test(timeout = 30000)
public void testHA() throws Exception {
    // NOTE: we play with blocking/nonblocking a lot below
    // as until we integrate with "leader election" stuff, service blocks on start if it is not a leader
    // TODO: fix this by integration with generic leader election stuff
    CConfiguration cConf = CConfiguration.create();
    // tests should use the current user for HDFS
    cConf.set(Constants.CFG_HDFS_USER, System.getProperty("user.name"));
    cConf.set(Constants.Zookeeper.QUORUM, zkServer.getConnectionStr());
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, tmpFolder.newFolder().getAbsolutePath());
    Injector injector = Guice.createInjector(new ConfigModule(cConf), new ZKClientModule(), new NonCustomLocationUnitTestModule().getModule(), new DiscoveryRuntimeModule().getDistributedModules(), new TransactionMetricsModule(), new AbstractModule() {

        @Override
        protected void configure() {
            bind(NamespaceQueryAdmin.class).to(SimpleNamespaceQueryAdmin.class);
            bind(UGIProvider.class).to(UnsupportedUGIProvider.class);
            bind(OwnerAdmin.class).to(DefaultOwnerAdmin.class);
        }
    }, new DataFabricModules().getDistributedModules(), Modules.override(new DataSetsModules().getDistributedModules()).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(MetadataStore.class).to(NoOpMetadataStore.class);
        }
    }), new AuthorizationTestModule(), new AuthorizationEnforcementModule().getInMemoryModules(), new AuthenticationContextModules().getNoOpModule());
    ZKClientService zkClient = injector.getInstance(ZKClientService.class);
    zkClient.startAndWait();
    try {
        final Table table = createTable("myTable");
        // tx service client
        // NOTE: we can init it earlier than we start services, it should pick them up when they are available
        TransactionSystemClient txClient = injector.getInstance(TransactionSystemClient.class);
        TransactionExecutor txExecutor = new DefaultTransactionExecutor(txClient, ImmutableList.of((TransactionAware) table));
        // starting tx service, tx client can pick it up
        TransactionService first = createTxService(zkServer.getConnectionStr(), Networks.getRandomPort(), hConf, tmpFolder.newFolder());
        first.startAndWait();
        Assert.assertNotNull(txClient.startShort());
        verifyGetAndPut(table, txExecutor, null, "val1");
        // starting another tx service should not hurt
        TransactionService second = createTxService(zkServer.getConnectionStr(), Networks.getRandomPort(), hConf, tmpFolder.newFolder());
        // NOTE: we don't have to wait for start as client should pick it up anyways, but we do wait to ensure
        // the case with two active is handled well
        second.startAndWait();
        // wait for affect a bit
        TimeUnit.SECONDS.sleep(1);
        Assert.assertNotNull(txClient.startShort());
        verifyGetAndPut(table, txExecutor, "val1", "val2");
        // shutting down the first one is fine: we have another one to pick up the leader role
        first.stopAndWait();
        Assert.assertNotNull(txClient.startShort());
        verifyGetAndPut(table, txExecutor, "val2", "val3");
        // doing same trick again to failover to the third one
        TransactionService third = createTxService(zkServer.getConnectionStr(), Networks.getRandomPort(), hConf, tmpFolder.newFolder());
        // NOTE: we don't have to wait for start as client should pick it up anyways
        third.start();
        // stopping second one
        second.stopAndWait();
        Assert.assertNotNull(txClient.startShort());
        verifyGetAndPut(table, txExecutor, "val3", "val4");
        // releasing resources
        third.stop();
    } finally {
        try {
            dropTable("myTable");
        } finally {
            zkClient.stopAndWait();
        }
    }
}
Also used : ConfigModule(co.cask.cdap.common.guice.ConfigModule) TransactionMetricsModule(co.cask.cdap.data.runtime.TransactionMetricsModule) ZKClientModule(co.cask.cdap.common.guice.ZKClientModule) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) Injector(com.google.inject.Injector) SimpleNamespaceQueryAdmin(co.cask.cdap.common.namespace.SimpleNamespaceQueryAdmin) DiscoveryRuntimeModule(co.cask.cdap.common.guice.DiscoveryRuntimeModule) UnsupportedUGIProvider(co.cask.cdap.security.impersonation.UnsupportedUGIProvider) InMemoryTable(co.cask.cdap.data2.dataset2.lib.table.inmemory.InMemoryTable) Table(co.cask.cdap.api.dataset.table.Table) TransactionService(org.apache.tephra.distributed.TransactionService) AuthenticationContextModules(co.cask.cdap.security.auth.context.AuthenticationContextModules) DataSetsModules(co.cask.cdap.data.runtime.DataSetsModules) TransactionExecutor(org.apache.tephra.TransactionExecutor) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) NonCustomLocationUnitTestModule(co.cask.cdap.common.guice.NonCustomLocationUnitTestModule) DefaultOwnerAdmin(co.cask.cdap.security.impersonation.DefaultOwnerAdmin) CConfiguration(co.cask.cdap.common.conf.CConfiguration) AuthorizationTestModule(co.cask.cdap.security.authorization.AuthorizationTestModule) AbstractModule(com.google.inject.AbstractModule) MetadataStore(co.cask.cdap.data2.metadata.store.MetadataStore) NoOpMetadataStore(co.cask.cdap.data2.metadata.store.NoOpMetadataStore) ZKClientService(org.apache.twill.zookeeper.ZKClientService) TransactionAware(org.apache.tephra.TransactionAware) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) DataFabricModules(co.cask.cdap.data.runtime.DataFabricModules) AuthorizationEnforcementModule(co.cask.cdap.security.authorization.AuthorizationEnforcementModule) Test(org.junit.Test)

Aggregations

ZKClientService (org.apache.twill.zookeeper.ZKClientService)27 Injector (com.google.inject.Injector)14 Test (org.junit.Test)14 CConfiguration (co.cask.cdap.common.conf.CConfiguration)11 IOException (java.io.IOException)9 ConfigModule (co.cask.cdap.common.guice.ConfigModule)8 ZKClientModule (co.cask.cdap.common.guice.ZKClientModule)8 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)7 AbstractModule (com.google.inject.AbstractModule)6 Configuration (org.apache.hadoop.conf.Configuration)6 DataFabricModules (co.cask.cdap.data.runtime.DataFabricModules)5 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)5 TransactionMetricsModule (co.cask.cdap.data.runtime.TransactionMetricsModule)5 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)5 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)5 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)5 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)5 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)5 ZKDiscoveryService (org.apache.twill.discovery.ZKDiscoveryService)5 SystemDatasetRuntimeModule (co.cask.cdap.data.runtime.SystemDatasetRuntimeModule)4