Github user huitseeker commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7578#discussion_r35231803
  
    --- Diff: 
extras/kinesis-asl/src/main/scala/org/apache/spark/streaming/kinesis/KinesisBackedBlockRDD.scala
 ---
    @@ -0,0 +1,223 @@
    +/*
    + * 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.spark.streaming.kinesis
    +
    +import scala.collection.JavaConversions._
    +
    +import com.amazonaws.auth.{AWSCredentials, 
DefaultAWSCredentialsProviderChain}
    +import com.amazonaws.services.kinesis.AmazonKinesisClient
    +import com.amazonaws.services.kinesis.model._
    +
    +import org.apache.spark._
    +import org.apache.spark.rdd.{BlockRDD, BlockRDDPartition}
    +import org.apache.spark.storage.BlockId
    +import org.apache.spark.util.NextIterator
    +
    +
    +/** Class representing a range of Kinesis sequence numbers */
    +private[kinesis]
    +case class SequenceNumberRange(
    +    streamName: String, shardId: String, fromSeqNumber: String, 
toSeqNumber: String)
    +
    +/** Class representing an array of Kinesis sequence number ranges */
    +private[kinesis]
    +case class SequenceNumberRanges(ranges: Array[SequenceNumberRange]) {
    +  def isEmpty(): Boolean = ranges.isEmpty
    +  def nonEmpty(): Boolean = ranges.nonEmpty
    +  override def toString(): String = 
ranges.mkString("SequenceNumberRanges(", ", ", ")")
    +}
    +
    +private[kinesis]
    +object SequenceNumberRanges {
    +  def apply(range: SequenceNumberRange): SequenceNumberRanges = {
    +    new SequenceNumberRanges(Array(range))
    +  }
    +}
    +
    +
    +/** Partition storing the information of the ranges of Kinesis sequence 
numbers to read */
    +private[kinesis]
    +class KinesisBackedBlockRDDPartition(
    +    idx: Int,
    +    blockId: BlockId,
    +    val isBlockIdValid: Boolean,
    +    val seqNumberRanges: SequenceNumberRanges
    +  ) extends BlockRDDPartition(blockId, idx)
    +
    +/**
    + * A BlockRDD where the block data is backed by Kinesis, which can 
accessed using the
    + * sequence numbers of the corresponding blocks.
    + */
    +private[kinesis]
    +class KinesisBackedBlockRDD(
    +    sc: SparkContext,
    +    regionId: String,
    +    endpointUrl: String,
    +    @transient blockIds: Array[BlockId],
    +    @transient arrayOfseqNumberRanges: Array[SequenceNumberRanges],
    +    @transient isBlockIdValid: Array[Boolean] = Array.empty,
    +    awsCredentialsOption: Option[SerializableAWSCredentials] = None
    +) extends BlockRDD[Array[Byte]](sc, blockIds) {
    +
    +  require(blockIds.length == arrayOfseqNumberRanges.length,
    +    "Number of blockIds is not equal to the number of sequence number 
ranges")
    +
    +  override def isValid(): Boolean = true
    +
    +  override def getPartitions: Array[Partition] = {
    +    Array.tabulate(blockIds.length) { i =>
    +      val isValid = if (isBlockIdValid.length == 0) true else 
isBlockIdValid(i)
    +      new KinesisBackedBlockRDDPartition(i, blockIds(i), isValid, 
arrayOfseqNumberRanges(i))
    +    }
    +  }
    +
    +  override def compute(split: Partition, context: TaskContext): 
Iterator[Array[Byte]] = {
    +    val blockManager = SparkEnv.get.blockManager
    +    val partition = split.asInstanceOf[KinesisBackedBlockRDDPartition]
    +    val blockId = partition.blockId
    +
    +    def getBlockFromBlockManager(): Option[Iterator[Array[Byte]]] = {
    +      logDebug(s"Read partition data of $this from block manager, block 
$blockId")
    +      
blockManager.get(blockId).map(_.data.asInstanceOf[Iterator[Array[Byte]]])
    +    }
    +
    +    def getBlockFromKinesis(): Iterator[Array[Byte]] = {
    +      val credenentials = awsCredentialsOption.getOrElse {
    +        new DefaultAWSCredentialsProviderChain().getCredentials()
    +      }
    +      partition.seqNumberRanges.ranges.iterator.flatMap { range =>
    +        new KinesisSequenceRangeIterator(credenentials, endpointUrl, 
regionId, range)
    +      }
    +    }
    +    if (partition.isBlockIdValid) {
    +      getBlockFromBlockManager().getOrElse { getBlockFromKinesis() }
    +    } else {
    +      getBlockFromKinesis()
    +    }
    +  }
    +}
    +
    +
    +/** An iterator that return the Kinesis data based on the given range of 
Sequence numbers */
    +private[kinesis]
    +class KinesisSequenceRangeIterator(
    +    credentials: AWSCredentials,
    +    endpointUrl: String,
    +    regionId: String,
    +    range: SequenceNumberRange
    +  ) extends NextIterator[Array[Byte]] {
    +
    +  private val backoffTimeMillis = 1000
    +  private val client = new AmazonKinesisClient(credentials)
    +
    +  private var toSeqNumberReceived = false
    +  private var lastSeqNumber: String = null
    +  private var internalIterator: Iterator[Record] = null
    +
    +  client.setEndpoint(endpointUrl, "kinesis", regionId)
    +
    +  override protected def getNext(): Array[Byte] = {
    +    var nextBytes: Array[Byte] = null
    +    if (toSeqNumberReceived) {
    +      finished = true
    +    } else {
    +
    +      if (internalIterator == null) {
    +
    +        // If the internal iterator has not been initialized,
    +        // then fetch records from starting sequence number
    +        getRecords(ShardIteratorType.AT_SEQUENCE_NUMBER, 
range.fromSeqNumber)
    --- End diff --
    
    Forgive my unfamiliarity with Kinesis, but are sequence numbers contiguous 
and increasing ? Because this seems to be the assumption here. In [the 
doc](https://docs.aws.amazon.com/kinesis/latest/dev/key-concepts.html) I find 
the scary quote:
    > Sequence numbers cannot be used as indexes to sets of data within the 
same stream. To logically separate sets of data, use partition keys or create a 
separate stream for each data set.
    
    
[Here](https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/kinesis/AmazonKinesisClient.html)
 I also see:
    
    > Sequence numbers generally increase over time. To guarantee strictly 
increasing ordering, use the SequenceNumberForOrdering parameter
    
    If they aren't, you might find yourself with :
    - missing data, that may be with a sequence number `fromSeqNumber < x < 
lastSeqNumber`, but not iterated on here because you request here with 
`ShardIteratorType.AT_SEQUENCE_NUMBER` rather than 
`ShardIteratorType.TRIM_HORIZON` 
    - superfluous data, because you're not checking that `fromSeqNumber < 
nextRecord.getSequenceNumber() < lastSeqNumber` before returning `nextRecord`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to