co.cask.cdap.data.stream.DistributedStreamCoordinatorClientTest.java Source code

Java tutorial

Introduction

Here is the source code for co.cask.cdap.data.stream.DistributedStreamCoordinatorClientTest.java

Source

/*
 * Copyright  2014-2016 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package co.cask.cdap.data.stream;

import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.common.guice.ConfigModule;
import co.cask.cdap.common.guice.DiscoveryRuntimeModule;
import co.cask.cdap.common.guice.ZKClientModule;
import co.cask.cdap.common.namespace.DefaultNamespacedLocationFactory;
import co.cask.cdap.common.namespace.NamespacedLocationFactory;
import co.cask.cdap.data.runtime.DataFabricModules;
import co.cask.cdap.data.runtime.DataSetsModules;
import co.cask.cdap.data.runtime.TransactionMetricsModule;
import co.cask.cdap.data.stream.service.InMemoryStreamMetaStore;
import co.cask.cdap.data.stream.service.StreamMetaStore;
import co.cask.cdap.data.view.ViewAdminModules;
import co.cask.cdap.data2.metadata.store.MetadataStore;
import co.cask.cdap.data2.metadata.store.NoOpMetadataStore;
import co.cask.cdap.data2.transaction.stream.StreamAdmin;
import co.cask.cdap.explore.guice.ExploreClientModule;
import co.cask.cdap.notifications.feeds.guice.NotificationFeedServiceRuntimeModule;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.util.Modules;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.twill.filesystem.FileContextLocationFactory;
import org.apache.twill.filesystem.LocationFactory;
import org.apache.twill.internal.zookeeper.InMemoryZKServer;
import org.apache.twill.zookeeper.ZKClientService;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.io.IOException;

/**
 * Tests for {@link DistributedStreamCoordinatorClient}
 */
public class DistributedStreamCoordinatorClientTest extends StreamCoordinatorTestBase {

    private static InMemoryZKServer zkServer;
    private static ZKClientService zkClient;
    private static MiniDFSCluster dfsCluster;
    private static StreamAdmin streamAdmin;
    private static StreamCoordinatorClient coordinatorClient;

    @BeforeClass
    public static void init() throws IOException {
        zkServer = InMemoryZKServer.builder().setDataDir(tmpFolder.newFolder()).build();
        zkServer.startAndWait();

        Configuration hConf = new Configuration();
        hConf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, tmpFolder.newFolder().getAbsolutePath());
        dfsCluster = new MiniDFSCluster.Builder(hConf).numDataNodes(1).build();
        dfsCluster.waitClusterUp();
        final LocationFactory lf = new FileContextLocationFactory(dfsCluster.getFileSystem().getConf());
        final NamespacedLocationFactory nlf = new DefaultNamespacedLocationFactory(cConf, lf);

        cConf.set(Constants.Zookeeper.QUORUM, zkServer.getConnectionStr());

        Injector injector = Guice.createInjector(new ConfigModule(cConf), new ZKClientModule(),
                new DiscoveryRuntimeModule().getDistributedModules(),
                new DataFabricModules().getDistributedModules(),
                Modules.override(new DataSetsModules().getDistributedModules()).with(new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(MetadataStore.class).to(NoOpMetadataStore.class);
                    }
                }), new TransactionMetricsModule(), new NotificationFeedServiceRuntimeModule().getInMemoryModules(),
                new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(LocationFactory.class).toInstance(lf);
                        bind(NamespacedLocationFactory.class).toInstance(nlf);
                    }
                }, new ExploreClientModule(), new ViewAdminModules().getInMemoryModules(),
                Modules.override(new StreamAdminModules().getDistributedModules()).with(new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(StreamMetaStore.class).to(InMemoryStreamMetaStore.class);
                    }
                }));

        zkClient = injector.getInstance(ZKClientService.class);
        zkClient.startAndWait();

        setupNamespaces(injector.getInstance(NamespacedLocationFactory.class));
        streamAdmin = injector.getInstance(StreamAdmin.class);
        coordinatorClient = injector.getInstance(StreamCoordinatorClient.class);
        coordinatorClient.startAndWait();
    }

    @AfterClass
    public static void finish() {
        coordinatorClient.stopAndWait();
        dfsCluster.shutdown();
        zkClient.stopAndWait();
        zkServer.stopAndWait();
    }

    @Override
    protected StreamCoordinatorClient getStreamCoordinator() {
        return coordinatorClient;
    }

    @Override
    protected StreamAdmin getStreamAdmin() {
        return streamAdmin;
    }
}