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();
}
}
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);
}
}
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();
}
}
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);
}
}
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();
}
}
}
Aggregations