Skip to content
This repository has been archived by the owner on May 21, 2020. It is now read-only.

Commit

Permalink
Refactor Source and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
aserrallerios committed Feb 3, 2018
1 parent 485201b commit 7c6d796
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 194 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ In order to use it, you need to provide a Worker builder and the Source settings
```scala
val workerSourceSettings = KinesisWorkerSourceSettings(
bufferSize = 1000,
checkWorkerPeriodicity = 1 minute)
terminateStreamGracePeriod = 1 minute)
val builder: IRecordProcessorFactory => Worker = { recordProcessorFactory =>
new Worker.Builder()
.recordProcessorFactory(recordProcessorFactory)
Expand Down Expand Up @@ -71,7 +71,7 @@ In order to use the Flow you can provide additional settings:

```scala
val checkpointSettings = KinesisWorkerCheckpointSettings(100, 30 seconds)
KinesisWorker(builder, workerSourceSettings)
KinesisWorkerSource(builder, workerSourceSettings)
.via(KinesisWorker.checkpointRecordsFlow(checkpointSettings))
.to(Sink.ignore)
KinesisWorker(builder, workerSourceSettings).to(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Copyright (C) 2018 Albert Serrallé
*/

package aserralle.akka.stream.kcl.worker
package aserralle.akka.stream.kcl

import akka.Done
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorCheckpointer
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShutdownReason
import com.amazonaws.services.kinesis.clientlibrary.types.ExtendedSequenceNumber
Expand All @@ -26,13 +27,17 @@ class CommittableRecord(
recordProcessor.shutdown
def canBeCheckpointed(): Boolean =
recordProcessorShutdownReason().isEmpty
def checkpoint(): Future[Unit] =
Future(checkpointer.checkpoint(record))
def tryToCheckpoint(): Future[Done] =
Future {
checkpointer.checkpoint(record)
Done
}

}

object CommittableRecord {

// Only makes sense to compare Records belonging to the same shard
implicit val orderBySequenceNumber: Ordering[CommittableRecord] =
Ordering.by(_.sequenceNumber)

Expand Down
15 changes: 15 additions & 0 deletions src/main/scala/aserralle/akka/stream/kcl/Errors.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) 2018 Albert Serrallé
*/

package aserralle.akka.stream.kcl

import scala.util.control.NoStackTrace

object Errors {

sealed trait KinesisWorkerSourceError extends NoStackTrace
case class WorkerUnexpectedShutdown(cause: Throwable)
extends KinesisWorkerSourceError

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Copyright (C) 2018 Albert Serrallé
*/

package aserralle.akka.stream.kcl.worker
package aserralle.akka.stream.kcl

import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorCheckpointer
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShutdownReason
import com.amazonaws.services.kinesis.clientlibrary.types.{
ExtendedSequenceNumber,
Expand All @@ -14,22 +15,25 @@ import com.amazonaws.services.kinesis.clientlibrary.types.{

import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration

private[kcl] class IRecordProcessor(
callback: CommittableRecord => Unit
callback: CommittableRecord => Unit,
terminateStreamGracePeriod: FiniteDuration
)(implicit executionContext: ExecutionContext)
extends com.amazonaws.services.kinesis.clientlibrary.interfaces.v2.IRecordProcessor {
private var shardId: String = _
private var extendedSequenceNumber: ExtendedSequenceNumber = _

var shutdown: Option[ShutdownReason] = None
var latestCheckpointer: Option[IRecordProcessorCheckpointer] = None

override def initialize(initializationInput: InitializationInput): Unit = {
this.shardId = initializationInput.getShardId
this.extendedSequenceNumber = initializationInput.getExtendedSequenceNumber
shardId = initializationInput.getShardId
extendedSequenceNumber = initializationInput.getExtendedSequenceNumber
}

override def processRecords(processRecordsInput: ProcessRecordsInput): Unit =
override def processRecords(processRecordsInput: ProcessRecordsInput): Unit = {
latestCheckpointer = Some(processRecordsInput.getCheckpointer)
processRecordsInput.getRecords.asScala.foreach { record =>
callback(
new CommittableRecord(
Expand All @@ -42,8 +46,17 @@ private[kcl] class IRecordProcessor(
)
)
}
}

override def shutdown(shutdownInput: ShutdownInput): Unit =
override def shutdown(shutdownInput: ShutdownInput): Unit = {
shutdown = Some(shutdownInput.getShutdownReason)
latestCheckpointer = Some(shutdownInput.getCheckpointer)
shutdownInput.getShutdownReason match {
case ShutdownReason.TERMINATE =>
Thread.sleep(terminateStreamGracePeriod.toMillis)
case ShutdownReason.ZOMBIE => ()
case ShutdownReason.REQUESTED => ()
}
}

}
31 changes: 0 additions & 31 deletions src/main/scala/aserralle/akka/stream/kcl/KinesisErrors.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.concurrent.duration._

case class KinesisWorkerSourceSettings(
bufferSize: Int,
checkWorkerPeriodicity: FiniteDuration) {
terminateStreamGracePeriod: FiniteDuration) {
require(
bufferSize >= 1,
"Buffer size must be greater than 0; use size 1 to disable stage buffering"
Expand All @@ -29,10 +29,9 @@ object KinesisWorkerSourceSettings {
/**
* Java API
*/
def create(
bufferSize: Int,
checkWorkerPeriodicity: FiniteDuration): KinesisWorkerSourceSettings =
KinesisWorkerSourceSettings(bufferSize, checkWorkerPeriodicity)
def create(bufferSize: Int, terminateStreamGracePeriod: FiniteDuration)
: KinesisWorkerSourceSettings =
KinesisWorkerSourceSettings(bufferSize, terminateStreamGracePeriod)

}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ package aserralle.akka.stream.kcl.javadsl
import java.util.concurrent.Executor

import akka.NotUsed
import aserralle.akka.stream.kcl.worker.CommittableRecord
import aserralle.akka.stream.kcl.{scaladsl, _}
import aserralle.akka.stream.kcl.{CommittableRecord, scaladsl, _}
import akka.stream.javadsl.{Flow, Sink, Source}
import com.amazonaws.services.kinesis.clientlibrary.interfaces.v2.IRecordProcessorFactory
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker
import com.amazonaws.services.kinesis.model.Record

import scala.concurrent.ExecutionContext

object KinesisWorker {
object KinesisWorkerSource {

abstract class WorkerBuilder {
def build(r: IRecordProcessorFactory): Worker
Expand All @@ -26,32 +25,32 @@ object KinesisWorker {
workerBuilder: WorkerBuilder,
settings: KinesisWorkerSourceSettings,
workerExecutor: Executor
): Source[CommittableRecord, NotUsed] =
scaladsl.KinesisWorker
): Source[CommittableRecord, Worker] =
scaladsl.KinesisWorkerSource
.apply(workerBuilder.build, settings)(
ExecutionContext.fromExecutor(workerExecutor))
.asJava

def create(
workerBuilder: WorkerBuilder,
workerExecutor: Executor
): Source[CommittableRecord, NotUsed] =
): Source[CommittableRecord, Worker] =
create(workerBuilder,
KinesisWorkerSourceSettings.defaultInstance,
workerExecutor)

def checkpointRecordsFlow(
settings: KinesisWorkerCheckpointSettings
): Flow[CommittableRecord, Record, NotUsed] =
scaladsl.KinesisWorker.checkpointRecordsFlow(settings).asJava
scaladsl.KinesisWorkerSource.checkpointRecordsFlow(settings).asJava

def checkpointRecordsFlow(): Flow[CommittableRecord, Record, NotUsed] =
checkpointRecordsFlow(KinesisWorkerCheckpointSettings.defaultInstance)

def checkpointRecordsSink(
settings: KinesisWorkerCheckpointSettings
): Sink[CommittableRecord, NotUsed] =
scaladsl.KinesisWorker.checkpointRecordsSink(settings).asJava
scaladsl.KinesisWorkerSource.checkpointRecordsSink(settings).asJava

def checkpointRecordsSink(): Sink[CommittableRecord, NotUsed] =
checkpointRecordsSink(KinesisWorkerCheckpointSettings.defaultInstance)
Expand Down
Loading

1 comment on commit 7c6d796

@aserrallerios
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@semenodm thanks! I'll look into it.

Please sign in to comment.