Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EventLog abstraction (implemented by AkkaPersistenceEventLog and KafkaEventLog). #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import scala.reflect.ClassTag
*
* @param journalId Id of the Akka Persistence journal.
*/
class AkkaPersistenceEventLog(journalId: String)(implicit system: ActorSystem) {
class AkkaPersistenceEventLog(journalId: String)(implicit system: ActorSystem) extends EventLog[String] {
private val journal = new Journal(journalId)

/**
Expand Down
74 changes: 74 additions & 0 deletions src/main/scala/com/github/krasserm/ases/log/EventLog.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed 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 com.github.krasserm.ases.log

import scala.concurrent.Future
import scala.reflect.ClassTag
import akka.{Done, NotUsed}
import akka.stream.scaladsl.{Flow, Sink, Source}
import com.github.krasserm.ases.{Delivery, Durable, Emitted}

/**
* Akka Stream API for events logs. Each individual event log is identified
* by an identifier. The type of the identifier (supplied as type argument
* `Identifier`) will depend on the concrete event log implementation.
*/
trait EventLog[Identifier] {

/**
* Creates a flow representing the event log identified by `identifier`.
* Input events are written to the journal and emitted as output events
* after successful write. Before input events are requested from upstream
* the flow emits events that have been previously written to the journal
* (recovery phase).
*
* During recovery, events are emitted as [[Delivered]] messages, recovery completion
* is signaled as [[Recovered]] message, both subtypes of [[Delivery]]. After recovery,
* events are again emitted as [[Delivered]] messages.
*
* It is the application's responsibility to ensure that there is only a
* single materialized instance with given `persistenceId` writing to the
* journal.
Copy link
Owner

Choose a reason for hiding this comment

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

This is an implementation detail of the Akka Persistence event log. Multiple materialization of a Kafka event log for the same topic partition can be made. Also, persistenceId refers to an implementation detail here.

*
* @param identifier identifier of the event log.
* @tparam A event type. Only events that are instances of given type are
* emitted by the flow.
*/
def flow[A: ClassTag](identifier: Identifier): Flow[Emitted[A], Delivery[Durable[A]], NotUsed]

/**
* Creates a source that replays events of the event log identified by `identifier`.
* The source completes when the end of the log has been reached.
*
* During recovery, events are emitted as [[Delivered]] messages, recovery completion
* is signaled as [[Recovered]] message, both subtypes of [[Delivery]].
Copy link
Owner

Choose a reason for hiding this comment

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

A source from a Kafka event log continues to emit Delivered messages.

*
* @param identifier identifier of the event log.
* @tparam A event type. Only events that are instances of given type are
* emitted by the source.
*/
def source[A: ClassTag](identifier: Identifier): Source[Delivery[Durable[A]], NotUsed]

/**
* Creates a sink that writes events to the event log identified by
* `identifier`.
*
* @param identifier persistence id of the event log.
*/
def sink[A](identifier: Identifier): Sink[Emitted[A], Future[Done]]

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import scala.reflect.ClassTag
* @param host Kafka (bootstrap server) host.
* @param port Kafka (bootstrap server) port.
*/
class KafkaEventLog(host: String, port: Int)(implicit system: ActorSystem) {
class KafkaEventLog(host: String, port: Int)(implicit system: ActorSystem) extends EventLog[TopicPartition] {
private val kafkaSource = new KafkaSource(host, port)
private val kafkaSink = new KafkaSink(host, port)
private val kafkaCodec = new KafkaCodec()
Expand Down