jihoonson commented on a change in pull request #6431: Add Kinesis Indexing Service to core Druid URL: https://github.com/apache/incubator-druid/pull/6431#discussion_r242373905
########## File path: extensions-core/kinesis-indexing-service/src/test/java/org/apache/druid/indexing/kinesis/KinesisRecordSupplierTest.java ########## @@ -0,0 +1,615 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.druid.indexing.kinesis; + +import com.amazonaws.services.kinesis.AmazonKinesis; +import com.amazonaws.services.kinesis.AmazonKinesisClient; +import com.amazonaws.services.kinesis.model.DescribeStreamResult; +import com.amazonaws.services.kinesis.model.GetRecordsRequest; +import com.amazonaws.services.kinesis.model.GetRecordsResult; +import com.amazonaws.services.kinesis.model.GetShardIteratorResult; +import com.amazonaws.services.kinesis.model.Record; +import com.amazonaws.services.kinesis.model.Shard; +import com.amazonaws.services.kinesis.model.StreamDescription; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.apache.druid.indexing.seekablestream.common.OrderedPartitionableRecord; +import org.apache.druid.indexing.seekablestream.common.StreamPartition; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.easymock.Capture; +import org.easymock.EasyMockSupport; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.anyString; +import static org.easymock.EasyMock.capture; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; + +public class KinesisRecordSupplierTest extends EasyMockSupport +{ + private static final String stream = "stream"; + private static long poll_timeout_millis = 2000; + private static int recordsPerFetch; + private static String shardId1 = "1"; + private static String shardId0 = "0"; + private static String shard1Iterator = "1"; + private static String shard0Iterator = "0"; + private static AmazonKinesis kinesis; + private static DescribeStreamResult describeStreamResult; + private static GetShardIteratorResult getShardIteratorResult0; + private static GetShardIteratorResult getShardIteratorResult1; + private static GetRecordsResult getRecordsResult0; + private static GetRecordsResult getRecordsResult1; + private static StreamDescription streamDescription; + private static Shard shard0; + private static Shard shard1; + private static KinesisRecordSupplier recordSupplier; + private static List<Record> shard1Records = ImmutableList.of( + new Record().withData(JB("2011", "d", "y", "10", "20.0", "1.0")).withSequenceNumber("0"), + new Record().withData(JB("2011", "e", "y", "10", "20.0", "1.0")).withSequenceNumber("1"), + new Record().withData(JB("246140482-04-24T15:36:27.903Z", "x", "z", "10", "20.0", "1.0")).withSequenceNumber("2"), + new Record().withData(ByteBuffer.wrap(StringUtils.toUtf8("unparseable"))).withSequenceNumber("3"), + new Record().withData(ByteBuffer.wrap(StringUtils.toUtf8("unparseable2"))).withSequenceNumber("4"), + new Record().withData(ByteBuffer.wrap(StringUtils.toUtf8("{}"))).withSequenceNumber("5"), + new Record().withData(JB("2013", "f", "y", "10", "20.0", "1.0")).withSequenceNumber("6"), + new Record().withData(JB("2049", "f", "y", "notanumber", "20.0", "1.0")).withSequenceNumber("7"), + new Record().withData(JB("2012", "g", "y", "10", "20.0", "1.0")).withSequenceNumber("8"), + new Record().withData(JB("2011", "h", "y", "10", "20.0", "1.0")).withSequenceNumber("9") + ); + private static List<Record> shard0Records = ImmutableList.of( + new Record().withData(JB("2008", "a", "y", "10", "20.0", "1.0")).withSequenceNumber("0"), + new Record().withData(JB("2009", "b", "y", "10", "20.0", "1.0")).withSequenceNumber("1") + ); + private static List<Object> allRecords = ImmutableList.builder() + .addAll(shard0Records.stream() + .map(x -> new OrderedPartitionableRecord<>( + stream, + shardId0, + x.getSequenceNumber(), + Collections + .singletonList( + toByteArray( + x.getData())) + )) + .collect( + Collectors + .toList())) + .addAll(shard1Records.stream() + .map(x -> new OrderedPartitionableRecord<>( + stream, + shardId1, + x.getSequenceNumber(), + Collections + .singletonList( + toByteArray( + x.getData())) + )) + .collect( + Collectors + .toList())) + .build(); + + private static ByteBuffer JB(String timestamp, String dim1, String dim2, String dimLong, String dimFloat, String met1) + { + try { + return ByteBuffer.wrap(new ObjectMapper().writeValueAsBytes( + ImmutableMap.builder() + .put("timestamp", timestamp) + .put("dim1", dim1) + .put("dim2", dim2) + .put("dimLong", dimLong) + .put("dimFloat", dimFloat) + .put("met1", met1) + .build() + )); + } + catch (Exception e) { + throw Throwables.propagate(e); + } + } + + @Before + public void setupTest() + { + kinesis = createMock(AmazonKinesisClient.class); + describeStreamResult = createMock(DescribeStreamResult.class); + getShardIteratorResult0 = createMock(GetShardIteratorResult.class); + getShardIteratorResult1 = createMock(GetShardIteratorResult.class); + getRecordsResult0 = createMock(GetRecordsResult.class); + getRecordsResult1 = createMock(GetRecordsResult.class); + streamDescription = createMock(StreamDescription.class); + shard0 = createMock(Shard.class); + shard1 = createMock(Shard.class); + recordsPerFetch = 1; + } + + @After + public void tearDownTest() + { + recordSupplier.close(); + recordSupplier = null; + } + + @Test + public void testSupplierSetup() + { + Capture<String> captured = Capture.newInstance(); + expect(kinesis.describeStream(capture(captured))).andReturn(describeStreamResult).once(); + expect(describeStreamResult.getStreamDescription()).andReturn(streamDescription).once(); + expect(streamDescription.getShards()).andReturn(ImmutableList.of(shard0, shard1)).once(); + expect(shard0.getShardId()).andReturn(shardId0).once(); + expect(shard1.getShardId()).andReturn(shardId1).once(); + + replayAll(); + + Set<StreamPartition<String>> partitions = ImmutableSet.of( + StreamPartition.of(stream, shardId0), + StreamPartition.of(stream, shardId1) + ); + + recordSupplier = new KinesisRecordSupplier( + kinesis, + recordsPerFetch, + 0, + 2, + false, + 100, + 5000, + 5000, + 60000, + 5 + ); + + Assert.assertTrue(recordSupplier.getAssignment().isEmpty()); + + recordSupplier.assign(partitions); + + Assert.assertEquals(partitions, recordSupplier.getAssignment()); + Assert.assertEquals(ImmutableSet.of(shardId1, shardId0), recordSupplier.getPartitionIds(stream)); + Assert.assertEquals(Collections.emptyList(), recordSupplier.poll(100)); + + verifyAll(); + Assert.assertEquals(stream, captured.getValue()); + } + + private static GetRecordsRequest generateGetRecordsReq(String shardIterator, int limit) + { + return new GetRecordsRequest().withShardIterator(shardIterator).withLimit(limit); + } + + // filter out EOS markers + private static List<OrderedPartitionableRecord<String, String>> cleanRecords(List<OrderedPartitionableRecord<String, String>> records) + { + return records.stream() + .filter(x -> !x.getSequenceNumber() + .equals(OrderedPartitionableRecord.END_OF_SHARD_MARKER)) + .collect(Collectors.toList()); + } + + @Test + public void testPoll() throws InterruptedException + { + recordsPerFetch = 100; + + expect(kinesis.getShardIterator( + anyObject(), + eq(shardId0), + anyString(), + anyString() + )).andReturn( + getShardIteratorResult0).anyTimes(); + + expect(kinesis.getShardIterator( + anyObject(), + eq(shardId1), + anyString(), + anyString() + )).andReturn( + getShardIteratorResult1).anyTimes(); + + expect(getShardIteratorResult0.getShardIterator()).andReturn(shard0Iterator).anyTimes(); + expect(getShardIteratorResult1.getShardIterator()).andReturn(shard1Iterator).anyTimes(); + expect(kinesis.getRecords(generateGetRecordsReq(shard0Iterator, recordsPerFetch))).andReturn(getRecordsResult0) + .anyTimes(); + expect(kinesis.getRecords(generateGetRecordsReq(shard1Iterator, recordsPerFetch))).andReturn(getRecordsResult1) + .anyTimes(); + expect(getRecordsResult0.getRecords()).andReturn(shard0Records).once(); + expect(getRecordsResult1.getRecords()).andReturn(shard1Records).once(); + expect(getRecordsResult0.getNextShardIterator()).andReturn(null).anyTimes(); + expect(getRecordsResult1.getNextShardIterator()).andReturn(null).anyTimes(); + + replayAll(); + + Set<StreamPartition<String>> partitions = ImmutableSet.of( + StreamPartition.of(stream, shardId0), + StreamPartition.of(stream, shardId1) + ); + + + recordSupplier = new KinesisRecordSupplier( + kinesis, + recordsPerFetch, + 0, + 2, + false, + 100, + 5000, + 5000, + 60000, + 100 + ); + + recordSupplier.assign(partitions); + recordSupplier.seekToEarliest(partitions); + recordSupplier.start(); + + while (recordSupplier.bufferSize() < 12) { + Thread.sleep(100); Review comment: Please add a timeout to this test since it gets stuck here. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
