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

Exceptions on the cache-store should be ignored for Read/WriteThroughStore #225

Merged
merged 2 commits into from
Apr 4, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -17,7 +17,7 @@
package com.twitter.storehaus

import com.twitter.concurrent.AsyncMutex
import com.twitter.util.Future
import com.twitter.util.{ Future, Return }

/**
* Provides read-through caching on a readable store fronted by a cache.
Expand Down Expand Up @@ -46,27 +46,22 @@ class ReadThroughStore[K, V](backingStore: ReadableStore[K, V], cache: Store[K,
mutex.acquire.flatMap { p =>
cache.put((k, storeValue))
.map { u : Unit => storeValue }
.onFailure { case x: Exception => storeValue }
.rescue { case x: Exception => Future.value(storeValue) }
.ensure { p.release }
}
}
}

override def get(k: K): Future[Option[V]] =
cache.get(k).flatMap { cacheValue =>
cacheValue match {
case None => getFromBackingStore(k)
case some => Future.value(some)
}
} onFailure { case x: Exception =>
getFromBackingStore(k)
}
override def get(k: K): Future[Option[V]] = cache.get(k) transform {
case Return(v @ Some(_)) => Future.value(v)
case _ => getFromBackingStore(k)
}

override def multiGet[K1 <: K](ks: Set[K1]): Map[K1, Future[Option[V]]] = {
// attempt to read from cache first
val cacheResults : Map[K1, Future[Either[Option[V], Exception]]] =
cache.multiGet(ks).map { case (k, f) =>
(k, f.map { optv => Left(optv) } onFailure { case x: Exception => Right(x) })
(k, f.map { optv => Left(optv) } rescue { case x: Exception => Future.value(Right(x)) })
}

// attempt to read all failed keys and cache misses from backing store
Expand All @@ -89,4 +84,3 @@ class ReadThroughStore[K, V](backingStore: ReadableStore[K, V], cache: Store[K,
FutureOps.liftValues(ks, f, { (k: K1) => Future.None })
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ class WriteThroughStore[K, V](backingStore: Store[K, V], cache: Store[K, V], inv

override def put(kv: (K, Option[V])): Future[Unit] = mutex.acquire.flatMap { p =>
// write key to backing store first
backingStore.put(kv).flatMap { u: Unit =>
backingStore.put(kv).flatMap { _ =>
// now write key to cache, best effort
cache.put(kv) onFailure { case x: Exception => u }
} onFailure { case x: Exception =>
cache.put(kv) rescue { case _ => Future.Unit }
} rescue { case x =>
// write to backing store failed
// now optionally invalidate the key in cache, best effort
if (invalidate) {
cache.put((kv._1, None)).flatMap { u: Unit => throw x } onFailure { throw x }
cache.put((kv._1, None)) transform { _ => Future.exception(x) }
} else {
throw x
Future.exception(x)
}
} ensure {
p.release
Expand All @@ -61,7 +61,7 @@ class WriteThroughStore[K, V](backingStore: Store[K, V], cache: Store[K, V], inv
// write keys to backing store first
val storeResults : Map[K1, Future[Either[Unit, Exception]]] =
backingStore.multiPut(kvs).map { case (k, f) =>
(k, f.map { u: Unit => Left(u) }.onFailure { case x: Exception => Right(x) })
(k, f.map { u: Unit => Left(u) }.rescue { case x: Exception => Future.value(Right(x)) })
}

// perform cache operations based on how writes to backing store go
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.twitter.storehaus
Copy link
Contributor

Choose a reason for hiding this comment

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

This file needs the copyright/license header

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, thanks


import com.twitter.util.Future

class ExceptionStore[K, V](f: Float = 0.5f) extends Store[K, V] {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this idea. Did you intend to use f to throw exceptions with some probability? It's not currently being used in this code though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I originally intended to use configurable possibility.
But when I ran some test, WriteThroughStore won't pass storeTest on random exceptions.
(on the case 'failure on write-to-cache, and then success on read-from-cache')
I should set possibility to 1.0f, but removed those function instead..

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Will investigate failures on random exceptions separately.

override def get(k: K): Future[Option[V]] = {
Future.exception(new RuntimeException())
}

override def put(kv: (K, Option[V])): Future[Unit] = {
Future.exception(new RuntimeException())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ object ReadThroughStoreProperties extends Properties("ReadThroughStoreProperties
readableStoreLaws[String, Int] { m =>
new ReadThroughStore(ReadableStore.fromMap(m), new ConcurrentHashMapStore[String,Int])
}

property("ReadThroughStore should ignore exceptions on the cache-store") =
readableStoreLaws[String, Int] { m =>
new ReadThroughStore(ReadableStore.fromMap(m),
new ExceptionStore())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ object WriteThroughStoreProperties extends Properties("WriteThroughStoreProperti
new WriteThroughStore(new ConcurrentHashMapStore[String,Int],
new ConcurrentHashMapStore[String,Int], false)
}
}

property("WriteThroughStore should ignore on the cache-store") =
storeTest {
new WriteThroughStore(new ConcurrentHashMapStore[String, Int],
new ExceptionStore())
}
}