-
Notifications
You must be signed in to change notification settings - Fork 12
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 #65 from ashwanthkumar/aggregator-support
Aggregator support
- Loading branch information
Showing
16 changed files
with
422 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
language: java | ||
sudo: false | ||
jdk: | ||
- oraclejdk8 | ||
env: | ||
global: | ||
secure: MmyMAHnhCNXJIclWXIZIIb2ZpJZknvaqZ2HUpxf+LIsr0dSwg3OhULc6hOEsktusio4rf7/mK5FihOEz4Lb4FFAzTuTn23YyPjjNY4BHIoqF+v5sgy0z8EGUl+DymoJExQ02CRwy6gyIk0MUYbcuZMbUEg5S8v/JvLL1hgEhv+8eAN3dGrMoog8NsDuYlhzZlFXXm8DN83PZ/9boagWq+WGHajNCUC7Ra7y4TfhRUKEDZfoln39xeazQwdUFq9iZ0urGM+QhZxL6wf2G68DAHfTmor/ahVZlOMcbmhakVqxVYz17f0ovKlvD2ybBDzaqae5j/AiTkEmHTEvXzXeSJT0VHE0/JuSHH+r+1tJxnrT+zg6s9YSOUDaZRkIWmLFe/jWV3X8AZ9MwETZDQptyTgxHqIYM8HBQA8j3CkjzgLMFkZfqwVgJRc2QjUwKKttmLEM7mJlxuyNoqD5fNHhFJCJBnJtJQAV3J6CEse4a1qOQRJoFBGIp8u5l/ej9Bwdf+WVp+mXt3uQOfr6L6PgYE+Ll9gy/gAPwn8IgllYN9q/ypGc9BO2fe77uWfDgQsTjwNfNnA0wKf3YYYunNnMN58oFBjukpZZKiyUOjgEdrrcCX/I6zMRDaA2YrW4s+JG1Idr2przEouPTbNjanw0hqQzz96Gslsv6FNhRYJaJicw= | ||
- secure: MmyMAHnhCNXJIclWXIZIIb2ZpJZknvaqZ2HUpxf+LIsr0dSwg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Z373EttxVM7dywQuPPu/bwyEI2fq44/JUQD+3AJ2Exgoh1NFrHT+H+NCDXY+x7W8S+lD9adlrmvHZfNZ1I401V8u9mEApw0XWaQCIvKfqXogk4UDtzxAvJKv0J7A1k70xH59OtnU5kHWF5j1805hkVhp/1+AC6UNVaiNLAoRNSH5PQFRtfJz4WyfMmonR+ULtBsFTdw6ON1gxbHAHiAY0WQmcBwUsF3gOn8GZrXKGxizKb6QutH64wI= | ||
- SONATYPE_USERNAME: ashwanthkumar | ||
cache: | ||
directories: | ||
- $HOME/.m2 | ||
- $HOME/DENV | ||
after_success: | ||
- git config user.name "Ashwanth Kumar" | ||
- git config user.email "[email protected]" | ||
- git remote add gh-token "https://${GH_TOKEN}@github.com/ashwanthkumar/suuchi.git"; | ||
- git fetch gh-token && git fetch gh-token gh-pages:gh-pages; | ||
- sudo pip install mkdocs==0.15.3 | ||
- mkdocs gh-deploy -v --clean --remote-name gh-token; | ||
- bash bin/hooks_publish_docs.sh | ||
- bash <(curl -s https://codecov.io/bash) | ||
- mvn deploy --settings travis-settings.xml -DskipTests=true -B |
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,15 @@ | ||
#!/usr/bin/env bash | ||
|
||
git config user.name "Ashwanth Kumar" | ||
git config user.email "[email protected]" | ||
git remote add gh-token "https://${GH_TOKEN}@github.com/ashwanthkumar/suuchi.git"; | ||
git fetch gh-token && git fetch gh-token gh-pages:gh-pages; | ||
|
||
# Deploy Docs only for builds out of master and not PRs or tags. | ||
if ([ "$TRAVIS_BRANCH" == "master" ] || [ ! -z "$TRAVIS_TAG" ]) && | ||
[ "$TRAVIS_PULL_REQUEST" == "false" ]; then | ||
virtualenv --system-site-packages ${HOME}/DENV | ||
source ${HOME}/DENV/bin/activate | ||
pip install mkdocs==0.15.3 | ||
mkdocs gh-deploy -v --clean --remote-name gh-token; | ||
fi |
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
85 changes: 85 additions & 0 deletions
85
suuchi-core/src/main/scala/in/ashwanthkumar/suuchi/router/AggregationRouter.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,85 @@ | ||
package in.ashwanthkumar.suuchi.router | ||
|
||
import java.util | ||
import java.util.concurrent.TimeUnit | ||
|
||
import com.google.common.util.concurrent.Futures | ||
import com.twitter.algebird.Aggregator | ||
import in.ashwanthkumar.suuchi.cluster.MemberAddress | ||
import in.ashwanthkumar.suuchi.rpc.CachedChannelPool | ||
import io.grpc._ | ||
import io.grpc.stub.{ClientCalls, MetadataUtils, StreamObserver, StreamObservers} | ||
import org.slf4j.LoggerFactory | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
trait Aggregation { | ||
def aggregator[ReqT, RespT]: PartialFunction[MethodDescriptor[ReqT, RespT], Aggregator[RespT, Any, RespT]] | ||
} | ||
|
||
class AggregationRouter(members: List[MemberAddress], agg: Aggregation) extends ServerInterceptor { | ||
val channelPool = CachedChannelPool() | ||
val log = LoggerFactory.getLogger(classOf[AggregationRouter]) | ||
|
||
override def interceptCall[ReqT, RespT](incomingRequest: ServerCall[ReqT, RespT], headers: Metadata, next: ServerCallHandler[ReqT, RespT]): ServerCall.Listener[ReqT] = { | ||
val isBroadcastRequest = headers.containsKey(Headers.BROADCAST_REQUEST_KEY) | ||
if (isBroadcastRequest || !agg.aggregator.isDefinedAt(incomingRequest.getMethodDescriptor)) { | ||
next.startCall(incomingRequest, headers) | ||
} else { | ||
// ServerCall.Listener for ServerStreaming methods | ||
headers.put(Headers.BROADCAST_REQUEST_KEY, true) | ||
incomingRequest.request(2) | ||
new ServerCall.Listener[ReqT] { | ||
val aggregator = agg.aggregator.apply(incomingRequest.getMethodDescriptor) | ||
var request: ReqT = _ | ||
|
||
override def onCancel() = { | ||
log.debug("AggregationRouter#onCancel") | ||
incomingRequest.close(Status.CANCELLED, headers) | ||
} | ||
override def onHalfClose() = { | ||
log.debug("AggregationRouter#onHalfClose") | ||
try { | ||
val gathered = scatter(members, channelPool, incomingRequest.getMethodDescriptor, headers, request) | ||
val reduced = aggregator.apply(gathered.asScala) | ||
incomingRequest.sendHeaders(headers) | ||
incomingRequest.sendMessage(reduced) | ||
incomingRequest.close(Status.OK, headers) | ||
} catch { | ||
case e: Throwable => | ||
log.error(e.getMessage, e) | ||
incomingRequest.close(Status.INTERNAL.withCause(e), headers) | ||
} | ||
} | ||
override def onReady() = { | ||
log.debug("AggregationRouter#onReady") | ||
} | ||
override def onMessage(message: ReqT) = { | ||
// We don't do the aggregation here but on onHalfClose() | ||
request = message | ||
} | ||
override def onComplete() = { | ||
log.debug("AggregationRouter#onComplete") | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected def scatter[ReqT, RespT](nodes: List[MemberAddress], channelPool: CachedChannelPool, methodDescriptor: MethodDescriptor[ReqT, RespT], headers: Metadata, input: ReqT): util.List[RespT] = { | ||
AggregationRouter.scatter(nodes, channelPool, methodDescriptor, headers, input) | ||
} | ||
} | ||
|
||
object AggregationRouter { | ||
def scatter[ReqT, RespT](nodes: List[MemberAddress], channelPool: CachedChannelPool, methodDescriptor: MethodDescriptor[ReqT, RespT], headers:Metadata, input: ReqT): util.List[RespT] = { | ||
val scatterRequests = nodes.map(destination => { | ||
val channel = channelPool.get(destination, insecure = true) | ||
val clientCall = ClientInterceptors.interceptForward(channel, MetadataUtils.newAttachHeadersInterceptor(headers)) | ||
.newCall(methodDescriptor, CallOptions.DEFAULT.withDeadlineAfter(10, TimeUnit.MINUTES)) // TODO (ashwanthkumar): Make this deadline configurable | ||
ClientCalls.futureUnaryCall(clientCall, input) | ||
}) | ||
|
||
Futures.allAsList(scatterRequests: _*).get() | ||
} | ||
} | ||
|
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
24 changes: 24 additions & 0 deletions
24
suuchi-core/src/main/scala/in/ashwanthkumar/suuchi/rpc/SuuchiAggregatorService.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,24 @@ | ||
package in.ashwanthkumar.suuchi.rpc | ||
|
||
import com.twitter.algebird.{Aggregator, LongRing, Semigroup} | ||
import in.ashwanthkumar.suuchi.router.Aggregation | ||
import in.ashwanthkumar.suuchi.rpc.generated.SuuchiRPC.ReduceResponse | ||
import in.ashwanthkumar.suuchi.rpc.generated.{AggregatorGrpc, SuuchiRPC} | ||
import io.grpc.stub.StreamObserver | ||
|
||
class SuuchiAggregatorService extends AggregatorGrpc.AggregatorImplBase { | ||
override def reduce(request: SuuchiRPC.ReduceRequest, responseObserver: StreamObserver[SuuchiRPC.ReduceResponse]) = { | ||
responseObserver.onNext(ReduceResponse.newBuilder().setOutput(1).build()) | ||
responseObserver.onCompleted() | ||
} | ||
} | ||
|
||
class SumOfNumbers extends Aggregation { | ||
override def aggregator[ReqT, RespT] = { | ||
case AggregatorGrpc.METHOD_REDUCE => new Aggregator[ReduceResponse, Long, ReduceResponse] { | ||
override def prepare(input: ReduceResponse) = input.getOutput | ||
override def semigroup: Semigroup[Long] = LongRing | ||
override def present(reduced: Long) = ReduceResponse.newBuilder().setOutput(reduced).build() | ||
}.asInstanceOf[Aggregator[RespT, Any, RespT]] | ||
} | ||
} |
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,24 @@ | ||
syntax = "proto3"; | ||
|
||
option java_package = "in.ashwanthkumar.suuchi.core.tests"; | ||
option java_outer_classname = "SuuchiTestRPC"; | ||
|
||
message ReduceRequest { | ||
} | ||
|
||
message ReduceResponse { | ||
int64 output = 1; | ||
} | ||
|
||
service Aggregator { | ||
rpc Reduce (ReduceRequest) returns (ReduceResponse); | ||
} | ||
|
||
message FooRequest { | ||
} | ||
message FooResponse { | ||
} | ||
|
||
service Random { | ||
rpc Foo (FooRequest) returns (FooResponse); | ||
} |
Oops, something went wrong.