-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from twitter/develop
Merge from twitter/storehaus
- Loading branch information
Showing
38 changed files
with
2,184 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
storehaus-algebra/src/main/scala/com/twitter/storehaus/algebra/HHFilteredStore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* 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.twitter.storehaus.algebra | ||
|
||
import com.twitter.storehaus.{Store, EagerWriteThroughCacheStore } | ||
import com.twitter.storehaus.cache.MutableCache | ||
import com.twitter.util.Future | ||
import com.twitter.storehaus.cache.{MutableCache, HeavyHittersPercent, WriteOperationUpdateFrequency, RollOverFrequencyMS, HHFilteredCache} | ||
|
||
object HHFilteredStore { | ||
def buildStore[K, V](store: Store[K, V], cache: MutableCache[K, Future[Option[V]]], hhPct: HeavyHittersPercent, | ||
writeUpdateFreq: WriteOperationUpdateFrequency, rolloverFreq: RollOverFrequencyMS): Store[K, V] = { | ||
val filteredCacheStore = new HHFilteredCache(cache, hhPct, writeUpdateFreq, rolloverFreq) | ||
new EagerWriteThroughCacheStore[K, V](store, filteredCacheStore) | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
storehaus-algebra/src/main/scala/com/twitter/storehaus/algebra/reporting/Reporter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* 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.twitter.storehaus.algebra.reporting | ||
|
||
import com.twitter.storehaus.{Store, ReadableStore, WritableStore} | ||
import com.twitter.storehaus.algebra.Mergeable | ||
import com.twitter.util.{Future, Time} | ||
|
||
|
||
object Reporter { | ||
def sideEffect[T, U](params: U, f: Future[T], sideEffect: (U, Future[T]) => Future[Unit]): Future[T] = | ||
Future.join(f, sideEffect(params, f)).map(_._1) | ||
|
||
def sideEffect[T, K, U](params: U, f: Map[K, Future[T]], sideEffect: (U, Map[K, Future[T]]) => Map[K, Future[Unit]]): Map[K, Future[T]] = { | ||
val effected = sideEffect(params, f) | ||
f.map{case (k, v) => | ||
val unitF = effected.getOrElse(k, sys.error("Reporter for multi side effect didn't return a future for key" + k.toString)) | ||
(k, Future.join(v, unitF).map(_._1)) | ||
} | ||
} | ||
} | ||
|
||
trait ReadableStoreReporter[S <: ReadableStore[K, V], K, V] extends ReadableStore[K, V] { | ||
def self: S | ||
override def get(k: K): Future[Option[V]] = Reporter.sideEffect(k, self.get(k), traceGet) | ||
override def multiGet[K1 <: K](keys: Set[K1]) = Reporter.sideEffect(keys, self.multiGet(keys), traceMultiGet) | ||
|
||
protected def traceMultiGet[K1 <: K](ks: Set[K1], request: Map[K1, Future[Option[V]]]): Map[K1, Future[Unit]] | ||
protected def traceGet(k: K, request: Future[Option[V]]): Future[Unit] | ||
override def close(time: Time) = self.close(time) | ||
} | ||
|
||
|
||
trait WritableStoreReporter[S <: WritableStore[K, V],K, V] extends WritableStore[K, V] { | ||
def self: S | ||
override def put(kv: (K, V)): Future[Unit] = Reporter.sideEffect(kv, self.put(kv), tracePut) | ||
override def multiPut[K1 <: K](kvs: Map[K1, V]) = Reporter.sideEffect(kvs, self.multiPut(kvs), traceMultiPut) | ||
|
||
protected def tracePut(kv: (K, V), request: Future[Unit]): Future[Unit] | ||
protected def traceMultiPut[K1 <: K](kvs: Map[K1, V], request: Map[K1, Future[Unit]]): Map[K1, Future[Unit]] | ||
override def close(time: Time) = self.close(time) | ||
} | ||
|
||
|
||
trait MergeableReporter[S <: Mergeable[K, V],K, V] extends Mergeable[K, V] { | ||
def self: S | ||
override def merge(kv: (K, V)) = Reporter.sideEffect(kv, self.merge(kv), traceMerge) | ||
override def multiMerge[K1 <: K](kvs: Map[K1, V]) = Reporter.sideEffect(kvs, self.multiMerge(kvs), traceMultiMerge) | ||
|
||
protected def traceMerge(kv: (K, V), request: Future[Option[V]]): Future[Unit] | ||
protected def traceMultiMerge[K1 <: K](kvs: Map[K1, V], request: Map[K1, Future[Option[V]]]): Map[K1, Future[Unit]] | ||
override def close(time: Time) = self.close(time) | ||
} | ||
|
||
|
||
trait StoreReporter[S <: Store[K, V], K, V] extends ReadableStoreReporter[S, K, V] with WritableStoreReporter[S, K, Option[V]] { | ||
def self: S | ||
} |
98 changes: 98 additions & 0 deletions
98
...est/scala/com/twitter/storehaus/algebra/reporting/ReportingMergeableStoreProperties.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
|
||
/* | ||
* Copyright 2013 Twitter Inc. | ||
* | ||
* 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.twitter.storehaus.algebra.reporting | ||
|
||
import com.twitter.util.{ Await, Future } | ||
import org.scalacheck.{ Arbitrary, Properties } | ||
import org.scalacheck.Gen.choose | ||
import org.scalacheck.Prop._ | ||
|
||
import com.twitter.storehaus.algebra._ | ||
|
||
object ReportingMergeableStoreProperties extends Properties("ReportingMergeableStore") { | ||
import MergeableStoreProperties.{mergeableStoreTest, newStore} | ||
|
||
|
||
|
||
class DummyReporter[K, V](val self: Mergeable[K, V]) extends MergeableProxy[K, V] with MergeableReporter[Mergeable[K, V], K, V] { | ||
def traceMerge(kv: (K, V), request: Future[Option[V]]) = request.unit | ||
def traceMultiMerge[K1 <: K](kvs: Map[K1, V], request: Map[K1, Future[Option[V]]]) = request.mapValues(_.unit) | ||
} | ||
|
||
property("Mergable stat store obeys the mergeable store proporites") = | ||
mergeableStoreTest { | ||
val store = newStore[Int, Int] | ||
new MergeableStoreProxy[Int, Int] with MergeableReporter[Mergeable[Int, Int], Int, Int] { | ||
val self = store | ||
def traceMerge(kv: (Int, Int), request: Future[Option[Int]]) = request.unit | ||
def traceMultiMerge[K1 <: Int](kvs: Map[K1, Int], request: Map[K1, Future[Option[Int]]]) = request.mapValues(_.unit) | ||
} | ||
} | ||
|
||
property("merge Some/None count matches") = forAll { (base: Map[Int, Int], merge: Map[Int, Int]) => | ||
var mergeWithSomeCount = 0 | ||
var mergeWithNoneCount = 0 | ||
val baseStore = MergeableStore.fromStore(newStore[Int, Int]) | ||
baseStore.multiMerge(base) | ||
|
||
val wrappedStore = new DummyReporter[Int, Int](baseStore) { | ||
override def traceMerge(kv: (Int, Int), request: Future[Option[Int]]) = { | ||
request.map { optV => | ||
optV match { | ||
case Some(_) => mergeWithSomeCount += 1 | ||
case None => mergeWithNoneCount += 1 | ||
} | ||
}.unit | ||
} | ||
} | ||
|
||
merge.map(kv => wrappedStore.merge((kv._1, kv._2))) | ||
|
||
val existsBeforeList = merge.keySet.toList.map(k => base.get(k)) | ||
|
||
existsBeforeList.collect{case Some(_) => 1}.size == mergeWithSomeCount && | ||
existsBeforeList.collect{case None => 1}.size == mergeWithNoneCount | ||
} | ||
|
||
property("multiMerge Some/None count matches") = forAll { (base: Map[Int, Int], merge: Map[Int, Int]) => | ||
var mergeWithSomeCount = 0 | ||
var mergeWithNoneCount = 0 | ||
val baseStore = MergeableStore.fromStore(newStore[Int, Int]) | ||
baseStore.multiMerge(base) | ||
|
||
val wrappedStore = new DummyReporter[Int, Int](baseStore) { | ||
override def traceMultiMerge[K1 <: Int](kvs: Map[K1, Int], request: Map[K1, Future[Option[Int]]]) = { | ||
request.mapValues{optV => | ||
optV.map{ v => | ||
v match { | ||
case Some(_) => mergeWithSomeCount += 1 | ||
case None => mergeWithNoneCount += 1 | ||
} | ||
}.unit | ||
} | ||
} | ||
} | ||
|
||
wrappedStore.multiMerge(merge) | ||
|
||
val existsBeforeList = merge.keySet.toList.map(k => base.get(k)) | ||
|
||
existsBeforeList.collect{case Some(_) => 1}.size == mergeWithSomeCount && | ||
existsBeforeList.collect{case None => 1}.size == mergeWithNoneCount | ||
} | ||
} |
Oops, something went wrong.