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

    https://github.com/apache/spark/pull/7578#discussion_r35280686
  
    --- 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 --
    
    Good doubts!
    
    Sequence numbers are increasing within each shard for the data being 
consumed. Thats how the Kinesis Client Library keeps track of the data read (
    
    What the document refers to as `SequenceNumberForOrdering` is to ensure 
ordering guarantees at the time of pushing data into Kinesis. Once data have 
been pushed in and Kinesis internal have assigned sequence numbers to each 
record, the ordering is well-defined and guaranteed. On consumption, the 
ordering will always be the same. 
    
    However sequence numbers are not contiguous (unlike Kafka offsets), hence 
they cannot be used as an index like 0 being first record, 1 being second, 2 
being third. And there is lies the problem of why we cannot use the direct 
approach for Kinesis. See linked design doc for more explanations and 
discussions.



---
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