Search in sources :

Example 31 with StreamProperties

use of co.cask.cdap.proto.StreamProperties in project cdap by caskdata.

the class StreamCoordinatorTestBase method testConfig.

@Test
public void testConfig() throws Exception {
    final StreamAdmin streamAdmin = getStreamAdmin();
    final String streamName = "testConfig";
    final StreamId streamId = NamespaceId.DEFAULT.stream(streamName);
    streamAdmin.create(streamId);
    StreamCoordinatorClient coordinator = getStreamCoordinator();
    final BlockingDeque<Integer> thresholds = new LinkedBlockingDeque<>();
    final BlockingDeque<Long> ttls = new LinkedBlockingDeque<>();
    coordinator.addListener(streamId, new StreamPropertyListener() {

        @Override
        public void thresholdChanged(StreamId streamId, int threshold) {
            thresholds.add(threshold);
        }

        @Override
        public void ttlChanged(StreamId streamId, long ttl) {
            ttls.add(ttl);
        }
    });
    // Have two threads, one update the threshold, one update the ttl
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch completeLatch = new CountDownLatch(2);
    for (int i = 0; i < 2; i++) {
        final int threadId = i;
        Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    barrier.await();
                    for (int i = 0; i < 100; i++) {
                        Long ttl = (threadId == 0) ? (long) (i * 1000) : null;
                        Integer threshold = (threadId == 1) ? i : null;
                        streamAdmin.updateConfig(streamId, new StreamProperties(ttl, null, threshold));
                    }
                    completeLatch.countDown();
                } catch (Exception e) {
                    throw Throwables.propagate(e);
                }
            }
        };
        t.start();
    }
    Assert.assertTrue(completeLatch.await(60, TimeUnit.SECONDS));
    // Check the last threshold and ttl are correct. We don't check if the listener gets every update as it's
    // possible that it doesn't see every updates, but only the latest value (that's what ZK watch guarantees).
    Assert.assertTrue(validateLastElement(thresholds, 99));
    Assert.assertTrue(validateLastElement(ttls, 99000L));
    // Verify the config is right
    StreamConfig config = streamAdmin.getConfig(streamId);
    Assert.assertEquals(99, config.getNotificationThresholdMB());
    Assert.assertEquals(99000L, config.getTTL());
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) StreamProperties(co.cask.cdap.proto.StreamProperties) StreamConfig(co.cask.cdap.data2.transaction.stream.StreamConfig) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) StreamAdmin(co.cask.cdap.data2.transaction.stream.StreamAdmin) Test(org.junit.Test)

Aggregations

StreamProperties (co.cask.cdap.proto.StreamProperties)31 StreamId (co.cask.cdap.proto.id.StreamId)17 Test (org.junit.Test)14 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)11 Schema (co.cask.cdap.api.data.schema.Schema)8 HttpURLConnection (java.net.HttpURLConnection)7 IOException (java.io.IOException)5 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)4 NotFoundException (co.cask.cdap.common.NotFoundException)3 CoordinatorStreamProperties (co.cask.cdap.data.stream.CoordinatorStreamProperties)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 Reader (java.io.Reader)3 Path (javax.ws.rs.Path)3 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)2 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)2 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)2 NotificationFeedException (co.cask.cdap.notifications.feeds.NotificationFeedException)2 ColumnDesc (co.cask.cdap.proto.ColumnDesc)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2