org.apache.flume.lifecycle.LifecycleState Java Examples

The following examples show how to use org.apache.flume.lifecycle.LifecycleState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PollingPropertiesFileConfigurationProvider.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
  LOGGER.info("Configuration provider starting");

  Preconditions.checkState(file != null,
      "The parameter file must not be null");

  executorService = Executors.newSingleThreadScheduledExecutor(
          new ThreadFactoryBuilder().setNameFormat("conf-file-poller-%d")
              .build());

  FileWatcherRunnable fileWatcherRunnable =
      new FileWatcherRunnable(file, counterGroup);

  executorService.scheduleWithFixedDelay(fileWatcherRunnable, 0, interval,
      TimeUnit.SECONDS);

  lifecycleState = LifecycleState.START;

  LOGGER.debug("Configuration provider started");
}
 
Example #2
Source File: NGSISinkTest.java    From fiware-cygnus with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * [CygnusSink.start] -------- The sink starts properly.
 */
@Test
public void testStart() {
    System.out.println(getTestTraceHead("[NGSISink.start]") + "-------- The sink starts properly");
    NGSISinkImpl sink = new NGSISinkImpl();
    sink.configure(createContext(null, null, null, null, null, null, null, null, null, null, null));
    sink.setChannel(new MemoryChannel());
    sink.start();
    LifecycleState state = sink.getLifecycleState();
    
    try {
        assertEquals(LifecycleState.START, state);
        System.out.println(getTestTraceHead("[NGSISink.start]")
                + "-  OK  - The sink started properly, the lifecycle state is '" + state.toString() + "'");
    } catch (AssertionError e) {
        System.out.println(getTestTraceHead("[NGSISink.start]")
                + "- FAIL - The sink did not start properly, the lifecycle state is '" + state.toString() + "'");
    } // try catch
}
 
Example #3
Source File: PollableSourceRunner.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
  PollableSource source = (PollableSource) getSource();
  ChannelProcessor cp = source.getChannelProcessor();
  cp.initialize();
  source.start();

  runner = new PollingRunner();

  runner.source = source;
  runner.counterGroup = counterGroup;
  runner.shouldStop = shouldStop;

  runnerThread = new Thread(runner);
  runnerThread.setName(getClass().getSimpleName() + "-" + 
      source.getClass().getSimpleName() + "-" + source.getName());
  runnerThread.start();

  lifecycleState = LifecycleState.START;
}
 
Example #4
Source File: PollableSourceRunner.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public void stop() {

  runner.shouldStop.set(true);

  try {
    runnerThread.interrupt();
    runnerThread.join();
  } catch (InterruptedException e) {
    logger
    .warn(
        "Interrupted while waiting for polling runner to stop. Please report this.",
        e);
    Thread.currentThread().interrupt();
  }

  Source source = getSource();
  source.stop();
  ChannelProcessor cp = source.getChannelProcessor();
  cp.close();

  lifecycleState = LifecycleState.STOP;
}
 
Example #5
Source File: SinkRunner.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
  SinkProcessor policy = getPolicy();

  policy.start();

  runner = new PollingRunner();

  runner.policy = policy;
  runner.counterGroup = counterGroup;
  runner.shouldStop = new AtomicBoolean();

  runnerThread = new Thread(runner);
  runnerThread.setName("SinkRunner-PollingRunner-" +
      policy.getClass().getSimpleName());
  runnerThread.start();

  lifecycleState = LifecycleState.START;
}
 
Example #6
Source File: BasicSourceSemantics.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void start() {
  if (exception != null) {
    logger.error(String.format("Cannot start due to error: name = %s",
        getName()), exception);
  } else {
    try {
      Preconditions.checkState(channelProcessor != null,
          "No channel processor configured");
      doStart();
      setLifecycleState(LifecycleState.START);
    } catch (Exception e) {
      logger.error(String.format(
          "Unexpected error performing start: name = %s", getName()), e);
      exception = e;
      setLifecycleState(LifecycleState.ERROR);
    }
  }
}
 
Example #7
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void testLifecycle() throws InterruptedException,
    InstantiationException, IllegalAccessException {
  setUp();
  Server server = createServer(new MockAvroServer());

  server.start();

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();
}
 
Example #8
Source File: BasicSourceSemantics.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void configure(Context context) {
  if(isStarted()) {
    throw new IllegalStateException("Configure called when started");
  } else {
    try {
      exception = null;
      setLifecycleState(LifecycleState.IDLE);
      doConfigure(context);
    } catch (Exception e) {
      exception = e;
      setLifecycleState(LifecycleState.ERROR);
      // causes source to be removed by configuration code
      Throwables.propagate(e);
    }
  }
}
 
Example #9
Source File: PollingPropertiesFileConfigurationProvider.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public void stop() {
  LOGGER.info("Configuration provider stopping");

  executorService.shutdown();
  try{
    while(!executorService.awaitTermination(500, TimeUnit.MILLISECONDS)) {
      LOGGER.debug("Waiting for file watcher to terminate");
    }
  } catch (InterruptedException e) {
    LOGGER.debug("Interrupted while waiting for file watcher to terminate");
    Thread.currentThread().interrupt();
  }
  lifecycleState = LifecycleState.STOP;
  LOGGER.debug("Configuration provider stopped");
}
 
Example #10
Source File: TestBasicSourceSemantics.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoConfigureThrowsException() throws Exception {
  source = spy(new DoNothingSource() {
    @Override
    protected void doConfigure(Context context) throws FlumeException {
      throw new FlumeException("dummy");
    }
  });
  source.setChannelProcessor(channelProcessor);
  try {
    source.configure(context);
    Assert.fail();
  } catch (FlumeException expected) {

  }
  Assert.assertFalse(source.isStarted());
  Assert.assertEquals(LifecycleState.ERROR, source.getLifecycleState());
  Assert.assertNotNull(source.getStartException());
}
 
Example #11
Source File: PollingPropertiesFileConfigurationProvider.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    LOGGER.info("Configuration provider starting");

    Preconditions.checkState(file != null,
            "The parameter file must not be null");

    executorService = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder().setNameFormat("conf-file-poller-%d")
                    .build());

    FileWatcherRunnable fileWatcherRunnable =
            new FileWatcherRunnable(file, counterGroup);

    executorService.scheduleWithFixedDelay(fileWatcherRunnable, 0, interval,
            TimeUnit.SECONDS);

    lifecycleState = LifecycleState.START;

    LOGGER.debug("Configuration provider started");
}
 
Example #12
Source File: NifiSessionFactoryChannel.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected BasicTransactionSemantics createTransaction() {
    LifecycleState lifecycleState = getLifecycleState();
    if (lifecycleState == LifecycleState.STOP) {
        throw new ChannelFullException("Can't write to a stopped channel");
    }
    return new NifiTransaction(sessionFactory.createSession(), relationship);
}
 
Example #13
Source File: EmbeddedAgent.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
private void stopLogError(LifecycleAware lifeCycleAware) {
  try {
    if(LifecycleState.START.equals(lifeCycleAware.getLifecycleState())) {
      lifeCycleAware.stop();
    }
  } catch (Exception e) {
    LOGGER.warn("Exception while stopping " + lifeCycleAware, e);
  }
}
 
Example #14
Source File: AbstractSinkProcessor.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
  for(Sink s : sinkList) {
    //s.start();
    s.stop(); //modify by judasheng, FLUME-1718
  }
  state = LifecycleState.STOP;
}
 
Example #15
Source File: RegexEventSerializerIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingColumnsInEvent() throws EventDeliveryException, SQLException {
    
    final String fullTableName = "FLUME_TEST";
    initSinkContextWithDefaults(fullTableName);
  
    sink = new PhoenixSink();
    Configurables.configure(sink, sinkContext);
    assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
  
    final Channel channel = this.initChannel();
    sink.setChannel(channel);
    
    sink.start();
    final String eventBody = "val1";
    final Event event = EventBuilder.withBody(Bytes.toBytes(eventBody));
    // put event in channel
    Transaction transaction = channel.getTransaction();
    transaction.begin();
    channel.put(event);
    transaction.commit();
    transaction.close();

    sink.process();
    
    int rowsInDb = countRows(fullTableName);
    assertEquals(0 , rowsInDb);
       
    sink.stop();
    assertEquals(LifecycleState.STOP, sink.getLifecycleState());
    
}
 
Example #16
Source File: NifiSessionFactoryChannel.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected BasicTransactionSemantics createTransaction() {
    LifecycleState lifecycleState = getLifecycleState();
    if (lifecycleState == LifecycleState.STOP) {
        throw new ChannelFullException("Can't write to a stopped channel");
    }
    return new NifiTransaction(sessionFactory.createSession(), relationship);
}
 
Example #17
Source File: AbstractSource.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void start() {
  Preconditions.checkState(channelProcessor != null,
      "No channel processor configured");

  lifecycleState = LifecycleState.START;
}
 
Example #18
Source File: PhoenixSinkIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testSinkLifecycle () {
    
    String ddl = "CREATE TABLE flume_test " +
            "  (flume_time timestamp not null, col1 varchar , col2 varchar" +
            "  CONSTRAINT pk PRIMARY KEY (flume_time))\n";
    
    sinkContext = new Context ();
    sinkContext.put(FlumeConstants.CONFIG_TABLE, "flume_test");
    sinkContext.put(FlumeConstants.CONFIG_JDBC_URL, getUrl());
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER,EventSerializers.REGEX.name());
    sinkContext.put(FlumeConstants.CONFIG_TABLE_DDL, ddl);
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_REGULAR_EXPRESSION,"^([^\t]+)\t([^\t]+)$");
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_COLUMN_NAMES,"col1,col2");
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_ROWKEY_TYPE_GENERATOR,DefaultKeyGenerator.TIMESTAMP.name());

    
    sink = new PhoenixSink();
    Configurables.configure(sink, sinkContext);
    Assert.assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
  
    final Channel channel = this.initChannel();
    sink.setChannel(channel);
    
    sink.start();
    Assert.assertEquals(LifecycleState.START, sink.getLifecycleState());
    sink.stop();
    Assert.assertEquals(LifecycleState.STOP, sink.getLifecycleState());
}
 
Example #19
Source File: TestRegexEventSerializer.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testKeyGenerator() throws EventDeliveryException, SQLException {
    
    final String fullTableName = "FLUME_TEST";
    initSinkContextWithDefaults(fullTableName);
    
    sink = new PhoenixSink();
    Configurables.configure(sink, sinkContext);
 
    assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
  
    final Channel channel = this.initChannel();
    sink.setChannel(channel);
    
    sink.start();
    final String eventBody = "val1\tval2";
    final Event event = EventBuilder.withBody(Bytes.toBytes(eventBody));
    // put event in channel
    Transaction transaction = channel.getTransaction();
    transaction.begin();
    channel.put(event);
    transaction.commit();
    transaction.close();

    sink.process();
   
    int rowsInDb = countRows(fullTableName);
    assertEquals(1 , rowsInDb);
    
    sink.stop();
    assertEquals(LifecycleState.STOP, sink.getLifecycleState());
     
}
 
Example #20
Source File: PhoenixSinkIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTable () throws Exception {
    
    String ddl = "CREATE TABLE flume_test " +
            "  (flume_time timestamp not null, col1 varchar , col2 varchar" +
            "  CONSTRAINT pk PRIMARY KEY (flume_time))\n";

    final String fullTableName = "FLUME_TEST";
    sinkContext = new Context ();
    sinkContext.put(FlumeConstants.CONFIG_TABLE, fullTableName);
    sinkContext.put(FlumeConstants.CONFIG_JDBC_URL, getUrl());
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER,EventSerializers.REGEX.name());
    sinkContext.put(FlumeConstants.CONFIG_TABLE_DDL, ddl);
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_REGULAR_EXPRESSION,"^([^\t]+)\t([^\t]+)$");
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_COLUMN_NAMES,"col1,col2");
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_ROWKEY_TYPE_GENERATOR,DefaultKeyGenerator.TIMESTAMP.name());

    
    sink = new PhoenixSink();
    Configurables.configure(sink, sinkContext);
    Assert.assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
  
    final Channel channel = this.initChannel();
    sink.setChannel(channel);
    
    sink.start();
    HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
    try {
        boolean exists = admin.tableExists(fullTableName);
        Assert.assertTrue(exists);
    }finally {
        admin.close();
    }
}
 
Example #21
Source File: EventDrivenSourceRunner.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Override
public void start() {
  Source source = getSource();
  ChannelProcessor cp = source.getChannelProcessor();
  cp.initialize();
  source.start();
  lifecycleState = LifecycleState.START;
}
 
Example #22
Source File: EventDrivenSourceRunner.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
  Source source = getSource();
  source.stop();
  ChannelProcessor cp = source.getChannelProcessor();
  cp.close();
  lifecycleState = LifecycleState.STOP;
}
 
Example #23
Source File: TestPollingPropertiesFileConfigurationProvider.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {

  baseDir = Files.createTempDir();

  configFile = new File(baseDir, TESTFILE.getName());
  Files.copy(TESTFILE, configFile);

  eventBus = new EventBus("test");
  provider =
      new PollingPropertiesFileConfigurationProvider("host1",
          configFile, eventBus, 1);
  provider.start();
  LifecycleController.waitForOneOf(provider, LifecycleState.START_OR_ERROR);
}
 
Example #24
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess() throws InterruptedException,
    EventDeliveryException, InstantiationException, IllegalAccessException {
  setUp();

  Event event = EventBuilder.withBody("test event 1", Charsets.UTF_8);
  Server server = createServer(new MockAvroServer());

  server.start();

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  Transaction transaction = channel.getTransaction();

  transaction.begin();
  for (int i = 0; i < 10; i++) {
    channel.put(event);
  }
  transaction.commit();
  transaction.close();

  for (int i = 0; i < 5; i++) {
    Sink.Status status = sink.process();
    Assert.assertEquals(Sink.Status.READY, status);
  }

  Assert.assertEquals(Sink.Status.BACKOFF, sink.process());

  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();
}
 
Example #25
Source File: FlumeAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@After
public void teardown() throws Exception {
    removeAppenders(avroLogger);
    eventSource.stop();
    Assert.assertTrue("Reached stop or error", LifecycleController
            .waitForOneOf(eventSource, LifecycleState.STOP_OR_ERROR));
    Assert.assertEquals("Server is stopped", LifecycleState.STOP,
            eventSource.getLifecycleState());
}
 
Example #26
Source File: TestAvroSource.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Test
public void testLifecycle() throws InterruptedException {
  boolean bound = false;

  for (int i = 0; i < 100 && !bound; i++) {
    try {
      Context context = new Context();

      context.put("port", String.valueOf(selectedPort = 41414 + i));
      context.put("bind", "0.0.0.0");

      Configurables.configure(source, context);

      source.start();
      bound = true;
    } catch (ChannelException e) {
      /*
       * NB: This assume we're using the Netty server under the hood and the
       * failure is to bind. Yucky.
       */
    }
  }

  Assert
      .assertTrue("Reached start or error", LifecycleController.waitForOneOf(
          source, LifecycleState.START_OR_ERROR));
  Assert.assertEquals("Server is started", LifecycleState.START,
      source.getLifecycleState());

  source.stop();
  Assert.assertTrue("Reached stop or error",
      LifecycleController.waitForOneOf(source, LifecycleState.STOP_OR_ERROR));
  Assert.assertEquals("Server is stopped", LifecycleState.STOP,
      source.getLifecycleState());
}
 
Example #27
Source File: TestRegexEventSerializer.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testMissingColumnsInEvent() throws EventDeliveryException, SQLException {
    
    final String fullTableName = "FLUME_TEST";
    initSinkContextWithDefaults(fullTableName);
  
    sink = new PhoenixSink();
    Configurables.configure(sink, sinkContext);
    assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
  
    final Channel channel = this.initChannel();
    sink.setChannel(channel);
    
    sink.start();
    final String eventBody = "val1";
    final Event event = EventBuilder.withBody(Bytes.toBytes(eventBody));
    // put event in channel
    Transaction transaction = channel.getTransaction();
    transaction.begin();
    channel.put(event);
    transaction.commit();
    transaction.close();

    sink.process();
    
    int rowsInDb = countRows(fullTableName);
    assertEquals(0 , rowsInDb);
       
    sink.stop();
    assertEquals(LifecycleState.STOP, sink.getLifecycleState());
    
}
 
Example #28
Source File: TestBasicSourceSemantics.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoStartThrowsException() throws Exception {
  source = spyAndConfigure(new DoNothingSource() {
    @Override
    protected void doStart() throws FlumeException {
      throw new FlumeException("dummy");
    }
  });
  source.start();
  Assert.assertFalse(source.isStarted());
  Assert.assertEquals(LifecycleState.ERROR, source.getLifecycleState());
  Assert.assertNotNull(source.getStartException());
}
 
Example #29
Source File: TestBasicSourceSemantics.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoStopThrowsException() throws Exception {
  source = spyAndConfigure(new DoNothingSource() {
    @Override
    protected void doStop() throws FlumeException {
      throw new FlumeException("dummy");
    }
  });
  source.start();
  source.stop();
  Assert.assertFalse(source.isStarted());
  Assert.assertEquals(LifecycleState.ERROR, source.getLifecycleState());
  Assert.assertNull(source.getStartException());
}
 
Example #30
Source File: TestRegexEventSerializer.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testMismatchKeyGenerator() throws EventDeliveryException, SQLException {
    
    final String fullTableName = "FLUME_TEST";
    initSinkContextWithDefaults(fullTableName);
    setConfig(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_ROWKEY_TYPE_GENERATOR,DefaultKeyGenerator.UUID.name());
 
    sink = new PhoenixSink();
    Configurables.configure(sink, sinkContext);
    assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
   
  
    final Channel channel = this.initChannel();
    sink.setChannel(channel);
    
    sink.start();
    final String eventBody = "val1\tval2";
    final Event event = EventBuilder.withBody(Bytes.toBytes(eventBody));
    // put event in channel
    Transaction transaction = channel.getTransaction();
    transaction.begin();
    channel.put(event);
    transaction.commit();
    transaction.close();

    try {
        sink.process();
        fail();
    }catch(Exception ex){
        assertTrue(ex.getCause().getMessage().contains("java.text.ParseException: Unparseable date:"));
    }
 }