Search in sources :

Example 66 with StreamEvent

use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.

the class StreamEventCodec method decodePayload.

/**
 * Reverse of {@link #encodePayload(StreamEvent)}.
 *
 * @param payload The byte array containing the queue payload.
 * @return A {@link StreamEvent} reconstructed from payload.
 */
public StreamEvent decodePayload(byte[] payload) {
    ByteBuffer buffer = ByteBuffer.wrap(payload);
    SchemaHash schemaHash = new SchemaHash(buffer);
    Preconditions.checkArgument(schemaHash.equals(STREAM_EVENT_SCHEMA.getSchemaHash()), "Schema from payload not matching StreamEvent schema.");
    Decoder decoder = new BinaryDecoder(new ByteBufferInputStream(buffer));
    try {
        StreamEventData data = StreamEventDataCodec.decode(decoder);
        // Read the timestamp
        long timestamp = decoder.readLong();
        return new StreamEvent(data, timestamp);
    } catch (IOException e) {
        // It should never happens, otherwise something very wrong.
        throw Throwables.propagate(e);
    }
}
Also used : SchemaHash(co.cask.cdap.api.data.schema.SchemaHash) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) ByteBufferInputStream(co.cask.common.io.ByteBufferInputStream) IOException(java.io.IOException) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Decoder(co.cask.cdap.common.io.Decoder) StreamEventData(co.cask.cdap.api.stream.StreamEventData) ByteBuffer(java.nio.ByteBuffer) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder)

Example 67 with StreamEvent

use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.

the class MultiLiveStreamFileReaderTestBase method testOffsets.

@Test
public void testOffsets() throws Exception {
    String streamName = "offsets";
    StreamId streamId = NamespaceId.DEFAULT.stream(streamName);
    Location location = getLocationFactory().create(streamName);
    location.mkdirs();
    // Create a stream with 1 partition.
    StreamConfig config = new StreamConfig(streamId, Long.MAX_VALUE, 10000, Long.MAX_VALUE, location, null, 1000);
    // Write out 200 events in 5 files, with interleaving timestamps
    for (int i = 0; i < 5; i++) {
        FileWriter<StreamEvent> writer = createWriter(config, "bucket" + i);
        for (int j = 0; j < 200; j++) {
            long timestamp = j * 5 + i;
            writer.append(StreamFileTestUtils.createEvent(timestamp, "Testing " + timestamp));
        }
        writer.close();
    }
    // Create a multi reader
    List<StreamFileOffset> sources = Lists.newArrayList();
    Location partitionLocation = StreamUtils.createPartitionLocation(config.getLocation(), 0, Long.MAX_VALUE);
    for (int i = 0; i < 5; i++) {
        Location eventFile = StreamUtils.createStreamLocation(partitionLocation, "bucket" + i, 0, StreamFileType.EVENT);
        sources.add(new StreamFileOffset(eventFile, 0L, 0));
    }
    MultiLiveStreamFileReader reader = new MultiLiveStreamFileReader(config, sources);
    // Reads some events
    List<StreamEvent> events = Lists.newArrayList();
    long expectedTimestamp = 0L;
    // Read 250 events, in batch size of 10.
    for (int i = 0; i < 25; i++) {
        Assert.assertEquals(10, reader.read(events, 10, 0, TimeUnit.SECONDS));
        Assert.assertEquals(10, events.size());
        for (StreamEvent event : events) {
            Assert.assertEquals(expectedTimestamp, event.getTimestamp());
            Assert.assertEquals("Testing " + expectedTimestamp, Charsets.UTF_8.decode(event.getBody()).toString());
            expectedTimestamp++;
        }
        events.clear();
    }
    // Capture the offsets
    Iterable<StreamFileOffset> offsets = ImmutableList.copyOf(Iterables.transform(reader.getPosition(), new Function<StreamFileOffset, StreamFileOffset>() {

        @Override
        public StreamFileOffset apply(StreamFileOffset input) {
            return new StreamFileOffset(input);
        }
    }));
    reader.close();
    // Create another multi reader with the offsets
    sources.clear();
    for (StreamFileOffset offset : offsets) {
        sources.add(offset);
    }
    // Read 750 events, in batch size of 10.
    reader = new MultiLiveStreamFileReader(config, sources);
    for (int i = 0; i < 75; i++) {
        Assert.assertEquals(10, reader.read(events, 10, 0, TimeUnit.SECONDS));
        Assert.assertEquals(10, events.size());
        for (StreamEvent event : events) {
            Assert.assertEquals(expectedTimestamp, event.getTimestamp());
            Assert.assertEquals("Testing " + expectedTimestamp, Charsets.UTF_8.decode(event.getBody()).toString());
            expectedTimestamp++;
        }
        events.clear();
    }
    Assert.assertEquals(0, reader.read(events, 10, 2, TimeUnit.SECONDS));
    reader.close();
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) StreamConfig(co.cask.cdap.data2.transaction.stream.StreamConfig) Function(com.google.common.base.Function) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 68 with StreamEvent

use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.

the class MultiLiveStreamFileReaderTestBase method testLiveFileReader.

@Test
public void testLiveFileReader() throws Exception {
    String streamName = "liveReader";
    StreamId streamId = NamespaceId.DEFAULT.stream(streamName);
    Location location = getLocationFactory().create(streamName);
    location.mkdirs();
    // Create a stream with 5 seconds partition.
    StreamConfig config = new StreamConfig(streamId, 5000, 1000, Long.MAX_VALUE, location, null, 1000);
    // Write 5 events in the first partition
    try (FileWriter<StreamEvent> writer = createWriter(config, "live.0")) {
        for (int i = 0; i < 5; i++) {
            writer.append(StreamFileTestUtils.createEvent(i, "Testing " + i));
        }
    }
    // Writer 5 events in the forth partition (ts = 15 to 19)
    try (FileWriter<StreamEvent> writer = createWriter(config, "live.0")) {
        for (int i = 0; i < 5; i++) {
            writer.append(StreamFileTestUtils.createEvent(i + 15, "Testing " + (i + 15)));
        }
    }
    // Create a LiveStreamFileReader to read 10 events. It should be able to read them all.
    Location partitionLocation = StreamUtils.createPartitionLocation(config.getLocation(), 0, config.getPartitionDuration());
    Location eventLocation = StreamUtils.createStreamLocation(partitionLocation, "live.0", 0, StreamFileType.EVENT);
    List<StreamEvent> events = new ArrayList<>();
    try (LiveStreamFileReader reader = new LiveStreamFileReader(config, new StreamFileOffset(eventLocation, 0, 0))) {
        while (events.size() < 10) {
            // It shouldn't have empty read.
            Assert.assertTrue(reader.read(events, Integer.MAX_VALUE, 0, TimeUnit.SECONDS) > 0);
        }
    }
    Assert.assertEquals(10, events.size());
    // First 5 events must have timestamps 0-4
    Iterator<StreamEvent> itor = events.iterator();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(i, itor.next().getTimestamp());
    }
    // Next 5 events must have timestamps 15-19
    for (int i = 15; i < 20; i++) {
        Assert.assertEquals(i, itor.next().getTimestamp());
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) ArrayList(java.util.ArrayList) StreamConfig(co.cask.cdap.data2.transaction.stream.StreamConfig) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 69 with StreamEvent

use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.

the class StreamFileJanitorTestBase method testCleanupTTL.

@Test
public void testCleanupTTL() throws Exception {
    // Create a stream with 5 seconds TTL, partition duration of 2 seconds
    String streamName = "testCleanupTTL";
    StreamId streamId = NamespaceId.DEFAULT.stream(streamName);
    authorizer.grant(Authorizable.fromEntityId(streamId), ALICE, EnumSet.of(Action.ADMIN));
    StreamAdmin streamAdmin = getStreamAdmin();
    Properties properties = new Properties();
    properties.setProperty(Constants.Stream.PARTITION_DURATION, "2000");
    properties.setProperty(Constants.Stream.TTL, "5000");
    streamAdmin.create(streamId, properties);
    // Truncate to increment generation to 1. This make verification condition easier (won't affect correctness).
    streamAdmin.truncate(streamId);
    StreamConfig config = streamAdmin.getConfig(streamId);
    SecurityRequestContext.setUserId(BOB.getName());
    // Write data with different timestamps that spans across 5 partitions
    FileWriter<StreamEvent> writer = createWriter(streamId);
    for (int i = 0; i < 10; i++) {
        writer.append(StreamFileTestUtils.createEvent(i * 1000, "Testing " + i));
    }
    writer.close();
    // Should see 5 partitions
    Location generationLocation = StreamUtils.createGenerationLocation(config.getLocation(), 1);
    Assert.assertEquals(5, generationLocation.list().size());
    // Perform clean with current time = 10000 (10 seconds since epoch).
    // Since TTL = 5 seconds, 2 partitions will be remove (Ends at 2000 and ends at 4000).
    janitor.clean(config.getLocation(), config.getTTL(), 10000);
    Assert.assertEquals(3, generationLocation.list().size());
    // Cleanup again with current time = 16000, all partitions should be deleted.
    janitor.clean(config.getLocation(), config.getTTL(), 16000);
    Assert.assertTrue(generationLocation.list().isEmpty());
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamAdmin(co.cask.cdap.data2.transaction.stream.StreamAdmin) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) StreamConfig(co.cask.cdap.data2.transaction.stream.StreamConfig) Properties(java.util.Properties) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 70 with StreamEvent

use of co.cask.cdap.api.flow.flowlet.StreamEvent in project cdap by caskdata.

the class StreamFileJanitorTestBase method testCleanupDeletedStream.

/**
 * Test clean up for all streams and verify user who does not have privileges on the streams is able to clean up.
 */
@Test
public void testCleanupDeletedStream() throws Exception {
    StreamId streamId = NamespaceId.DEFAULT.stream("cleanupDelete");
    StreamAdmin streamAdmin = getStreamAdmin();
    authorizer.grant(Authorizable.fromEntityId(streamId), ALICE, EnumSet.of(Action.ADMIN));
    streamAdmin.create(streamId);
    // Write some data
    try (FileWriter<StreamEvent> writer = createWriter(streamId)) {
        for (int i = 0; i < 10; i++) {
            writer.append(StreamFileTestUtils.createEvent(i * 1000, "Testing " + i));
        }
    }
    // Delete the stream
    streamAdmin.drop(streamId);
    SecurityRequestContext.setUserId(BOB.getName());
    // Run janitor. Should be running fine without exception.
    // Even Bob does not have privilege on the stream, he should be able to clean up the streams
    janitor.cleanAll();
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamAdmin(co.cask.cdap.data2.transaction.stream.StreamAdmin) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) Test(org.junit.Test)

Aggregations

StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)84 Test (org.junit.Test)65 Location (org.apache.twill.filesystem.Location)27 StreamId (co.cask.cdap.proto.id.StreamId)24 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)19 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)17 Schema (co.cask.cdap.api.data.schema.Schema)10 IOException (java.io.IOException)9 StreamConfig (co.cask.cdap.data2.transaction.stream.StreamConfig)8 ByteBuffer (java.nio.ByteBuffer)8 ConsumerConfig (co.cask.cdap.data2.queue.ConsumerConfig)7 StreamAdmin (co.cask.cdap.data2.transaction.stream.StreamAdmin)6 TransactionContext (org.apache.tephra.TransactionContext)6 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)5 TypeToken (com.google.common.reflect.TypeToken)5 StreamEventCodec (co.cask.cdap.common.stream.StreamEventCodec)4 IdentityStreamEventDecoder (co.cask.cdap.data.stream.decoder.IdentityStreamEventDecoder)4 File (java.io.File)4 SchemaHash (co.cask.cdap.api.data.schema.SchemaHash)3 QueueName (co.cask.cdap.common.queue.QueueName)3