Skip to content
Browse files

adding sbt example

  • Loading branch information...
1 parent f8e7bd8 commit ca78c51362cd53abbd196acfbd0bde159d5b9d0d Trisha Quan committed
View
28 examples/sbt/.gitignore
@@ -0,0 +1,28 @@
+target/
+dist/
+project/boot/
+project/plugins/project/
+project/plugins/src_managed/
+**/project/boot/
+**/project/plugins/project/
+**/project/plugins/src_managed/
+*.log
+*.tmproj
+lib_managed/
+.*.sw[a-z]
+*.iml
+*.pyc
+.idea/
+.idea/*
+.DS_Store
+.ensime
+.ivyjars
+.run_classpath
+Gemfile.lock
+/.py/
+#*
+*#
+.#*
+*~
+.*~
+/bin/sbt-launch-0.7.5.jar
View
12 examples/sbt/README
@@ -0,0 +1,12 @@
+COMMANDS
+sbt update: will pull in all of your dependencies
+sbt compile: compiles.
+sbt clean-dist package-dist: cleans out any previous dists and creates a new one
+
+If you do compile or package-dist you also want to include NO_VALIDATE=1 and NO_TESTS=1 to stop the tests from running.
+
+TO RUN
+0) "sbt update", if needed
+1) "NO_VALIDATE=1 NO_TESTS=1 sbt clean-dist package-dist" to clean up and create a new dist dir
+2) "cd dist/sbt-example"
+3) "java -jar sbt-example-1.0.jar -f config/echo.scala"
View
20 examples/sbt/config/echo.scala
@@ -0,0 +1,20 @@
+import com.twitter.parrot.config.ParrotLauncherConfig
+
+new ParrotLauncherConfig {
+ distDir = "."
+ jobName = "load_echo"
+ port = 50017
+ victims = "localhost"
+ log = "config/sample.log"
+ requestRate = 1
+ numInstances = 1
+ duration = 5
+ timeUnit = "MINUTES"
+
+ imports = "import com.twitter.jexample.EchoLoadTest"
+ responseType = "Array[Byte]"
+ transport = "ThriftTransport"
+ loadTest = "new EchoLoadTest(service.get)"
+ parser = "thrift"
+}
+
View
BIN examples/sbt/libs/iago-0.5.1.jar
Binary file not shown.
View
8 examples/sbt/project/build.properties
@@ -0,0 +1,8 @@
+#Project properties
+#Tue Aug 16 15:22:32 PDT 2011
+project.organization=com.twitter
+project.name=sbt-example
+sbt.version=0.7.4
+project.version=1.0
+build.scala.versions=2.9.2
+project.initialize=false
View
39 examples/sbt/project/build/Project.scala
@@ -0,0 +1,39 @@
+import sbt._
+import com.twitter.sbt._
+
+class Project(info: ProjectInfo) extends StandardServiceProject(info)
+ with PackageDist
+ with CompileThriftFinagle
+ with DefaultRepos {
+
+ //Assumes you are using SBT for iago as well, and that it's in ivy
+ // (ex. ~/.ivy2/local/com/twitter/iago/0.5.1/iago-0.5.1.jar)
+
+ val libThrift = "thrift" % "libthrift" % "0.5.0"
+ val finagleCore = "com.twitter" % "finagle-core" % "5.3.0"
+ val finagleServerSets = "com.twitter" % "finagle-serversets" % "5.3.0"
+ val finagleMemcached = "com.twitter" % "finagle-memcached" % "5.3.0"
+ val finagleStream = "com.twitter" % "finagle-stream" % "5.3.0"
+ val finagleThrift = "com.twitter" % "finagle-thrift" % "5.3.0"
+ val finagleOstrich = "com.twitter" % "finagle-ostrich4" % "5.3.0"
+ val iago = "com.twitter" % "iago" % "0.5.1"
+ val ostrich = "com.twitter" % "ostrich" % "8.0.1"
+ val utilThrift = "com.twitter" % "util-thrift" % "5.3.0"
+ def customRun(mainClass: String, args: String*) = task { _ =>
+ runTask(Some(mainClass), runClasspath, args) dependsOn(compile, copyResources)
+ }
+
+ lazy val server = customRun("com.twitter.example.EchoServer")
+ lazy val client = customRun("com.twitter.example.EchoClient")
+
+ lazy val startParrot = customRun("com.twitter.parrot.launcher.LauncherMain", "-f", "config/echo.scala")
+ lazy val killParrot = customRun("com.twitter.parrot.launcher.LauncherMain", "-f", "config/echo.scala", "-k")
+
+ override def mainClass = Some("com.twitter.parrot.launcher.LauncherMain")
+ override def ivyXML =
+ <dependencies>
+ <exclude org="javax.jms"/>
+ <exclude org="com.sun.jdmk"/>
+ <exclude org="com.sun.jmx"/>
+ </dependencies>
+}
View
13 examples/sbt/project/plugins/Plugins.scala
@@ -0,0 +1,13 @@
+import sbt._
+import scala.collection.jcl
+
+class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
+ val environment = jcl.Map(System.getenv())
+
+ override def repositories = super.repositories++ Seq("twitter.com" at "http://maven.twttr.com/")
+
+ override def ivyRepositories = super.ivyRepositories ++ repositories
+
+ val defaultProject = "com.twitter" % "standard-project" % "0.12.10"
+ val sbtThrift = "com.twitter" % "sbt-thrift" % "1.4.2"
+}
View
36 examples/sbt/src/main/java/com/twitter/jexample/EchoLoadTest.java
@@ -0,0 +1,36 @@
+package com.twitter.jexample;
+
+import com.twitter.example.thrift.EchoService;
+import com.twitter.parrot.processor.ThriftLoadTest;
+import com.twitter.parrot.server.ParrotRequest;
+import com.twitter.parrot.server.ParrotService;
+import com.twitter.parrot.thrift.ParrotJob;
+import com.twitter.util.Future;
+import com.twitter.util.FutureEventListener;
+import org.apache.thrift.protocol.TBinaryProtocol;
+
+import java.util.List;
+
+public class EchoLoadTest extends ThriftLoadTest {
+ EchoService.ServiceToClient client = null;
+
+ public EchoLoadTest(ParrotService<ParrotRequest, byte[]> parrotService) {
+ super(parrotService);
+ client = new EchoService.ServiceToClient(service(), new TBinaryProtocol.Factory());
+ }
+
+ public void processLines(ParrotJob job, List<String> lines) {
+ for(String line: lines) {
+ Future<String> future = client.echo(line);
+ future.addEventListener(new FutureEventListener<String>() {
+ public void onSuccess(String msg) {
+ System.out.println("response: " + msg);
+ }
+
+ public void onFailure(Throwable cause) {
+ System.out.println("Error: " + cause);
+ }
+ });
+ }
+ }
+}
View
33 examples/sbt/src/main/scala/com/twitter/example/EchoClient.scala
@@ -0,0 +1,33 @@
+package com.twitter.example
+
+import java.net.InetSocketAddress
+import org.apache.thrift.protocol.TBinaryProtocol
+
+import com.twitter.finagle.builder.ClientBuilder
+import com.twitter.finagle.Service
+import com.twitter.finagle.thrift.{ThriftClientFramedCodec, ThriftClientRequest}
+
+import thrift.EchoService
+
+object EchoClient {
+ def main(args: Array[String]) {
+ // Create a raw Thrift client service. This implements the
+ // ThriftClientRequest => Future[Array[Byte]] interface.
+ val service: Service[ThriftClientRequest, Array[Byte]] = ClientBuilder()
+ .hosts(new InetSocketAddress(EchoServer.port))
+ .codec(ThriftClientFramedCodec())
+ .hostConnectionLimit(1)
+ .build()
+
+ // Wrap the raw Thrift service in a Client decorator. The client
+ // provides a convenient procedural interface for accessing the Thrift
+ // server.
+ val client = new EchoService.ServiceToClient(service, new TBinaryProtocol.Factory())
+
+ client.echo("hello") onSuccess { response =>
+ println("Received response: " + response)
+ } ensure {
+ service.release()
+ }
+ }
+}
View
26 examples/sbt/src/main/scala/com/twitter/example/EchoLoadTest.scala
@@ -0,0 +1,26 @@
+package com.twitter.example
+
+import org.apache.thrift.protocol.TBinaryProtocol
+
+import com.twitter.parrot.processor.ThriftRecordProcessor
+import com.twitter.parrot.thrift.ParrotJob
+import com.twitter.parrot.server.{ParrotRequest,ParrotService}
+import com.twitter.logging.Logger
+
+import thrift.EchoService
+
+class EchoLoadTest(parrotService: ParrotService[ParrotRequest, Array[Byte]]) extends ThriftRecordProcessor(parrotService) {
+ val client = new EchoService.ServiceToClient(service, new TBinaryProtocol.Factory())
+ val log = Logger.get(getClass)
+
+ def processLines(job: ParrotJob, lines: Seq[String]) {
+ lines map { line =>
+ client.echo(line) respond { rep =>
+ if (rep == "hello") {
+ client.echo("OMIGOD IT'S TALKING TO US")
+ }
+ log.info("response: " + rep)
+ }
+ }
+ }
+}
View
67 examples/sbt/src/main/scala/com/twitter/example/EchoServer.scala
@@ -0,0 +1,67 @@
+package com.twitter.example
+
+import java.net.{InetAddress, InetSocketAddress}
+import java.util.concurrent.atomic.AtomicInteger
+import org.apache.thrift.protocol.TBinaryProtocol
+
+import collection.JavaConversions._
+
+import com.twitter.common.quantity.{Time, Amount}
+import com.twitter.common.zookeeper.{ServerSetImpl, ZooKeeperClient}
+import com.twitter.finagle.builder.{ServerBuilder}
+import com.twitter.finagle.thrift.ThriftServerFramedCodec
+import com.twitter.finagle.zookeeper.ZookeeperServerSetCluster
+import com.twitter.logging.Logger
+import com.twitter.util.Future
+
+import thrift.EchoService
+
+object EchoServer {
+ var port = 8081
+ val log = Logger.get(getClass)
+ val requestCount = new AtomicInteger(0)
+
+ def main(args: Array[String]) {
+ args foreach { arg =>
+ val splits = arg.split("=")
+ if (splits(0) == "thriftPort") {
+ port = splits(1).toInt
+ }
+ }
+ serve(port)
+ }
+
+ def serve(port: Int) {
+ // Implement the Thrift Interface
+ val processor = new EchoService.ServiceIface {
+ def echo(message: String) = {
+ log.info("echoing message: %s", message)
+ requestCount.incrementAndGet
+ Future.value(message)
+ }
+ }
+
+ // Convert the Thrift Processor to a Finagle Service
+ val service = new EchoService.Service(processor, new TBinaryProtocol.Factory())
+
+ val address = new InetSocketAddress(port)
+
+ ServerBuilder()
+ .bindTo(address)
+ .codec(ThriftServerFramedCodec())
+ .name("thriftserver")
+ .build(service)
+
+ // Zookeeper Support
+ val zkHost = "localhost" //put your zookeeper host here
+ val zkPort = 2181
+ val zkCluster: Array[InetSocketAddress] = InetAddress.getAllByName(zkHost).map(new InetSocketAddress(_, zkPort))
+ val zkClient = new ZooKeeperClient(Amount.of(50, Time.MILLISECONDS), asJavaIterable(zkCluster.toIterable))
+ val serverSet = new ServerSetImpl(zkClient, "/twitter/services/parrot-examples")
+ val cluster = new ZookeeperServerSetCluster(serverSet)
+
+ cluster.join(address)
+ }
+
+ def getRequestCount = requestCount.get
+}
View
6 examples/sbt/src/main/thrift/echo/EchoService.thrift
@@ -0,0 +1,6 @@
+namespace java com.twitter.example.thrift
+namespace rb ParrotEcho
+
+service EchoService {
+ string echo(1: string message)
+}
View
48 examples/sbt/src/scripts/echo-server.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+#
+# echo mesos startup script.
+
+APP_NAME="echo"
+MAIN_JAR="parrot-examples-1.0.jar"
+
+APP_HOME=`pwd`
+PIDFILE=$APP_HOME/$APP_NAME.pid
+LOG_HOME=$APP_HOME
+
+MAIN_CLASS="com.twitter.example.EchoServer"
+HEAP_OPTS="-Xmx128m -Xms128m -XX:NewSize=64m"
+GC_OPTS="-XX:+UseConcMarkSweepGC -verbosegc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+UseParNewGC -Xloggc:$LOG_HOME/gc.log"
+JAVA_OPTS="-server $GC_OPTS $HEAP_OPTS $PROFILE_OPTS"
+
+# Used to set JAVA_HOME sanely if not already set.
+function find_java() {
+ if [ ! -z $JAVA_HOME ]; then
+ return
+ fi
+ potential=$(ls -r1d /opt/jdk /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home /usr/java/default /usr/java/j* 2>/dev/null)
+ for p in $potential; do
+ if [ -x $p/bin/java ]; then
+ JAVA_HOME=$p
+ break
+ fi
+ done
+}
+
+find_java
+
+case "$1" in
+
+ # start-local is meant for development and runs your server in the foreground.
+ start-local)
+ ${JAVA_HOME}/bin/java ${JAVA_OPTS} -cp ${APP_HOME}/${MAIN_JAR} ${MAIN_CLASS}
+ ;;
+
+ start)
+ echo "Starting Echo Server v1"
+ ${JAVA_HOME}/bin/java ${JAVA_OPTS} -cp ${APP_HOME}/${MAIN_JAR} ${MAIN_CLASS} $2 $3 $4 $5 $6 $7 $8 $9 ${10}
+ echo "done."
+ ;;
+
+esac
+
+exit 0
View
3 src/main/scala/com/twitter/parrot/launcher/ParrotLauncher.scala
@@ -163,8 +163,7 @@ class ParrotLauncher(config: ParrotLauncherConfig) {
private[this] def createRemoteConfigs() {
log.debug("Creating configs.")
- List( ("/templates/template.mesos", targetDstFolder + "/config.mesos"),
- ("/templates/template-feeder.scala", targetDstFolder + "/mesos-feeder.scala"),
+ List( ("/templates/template-feeder.scala", targetDstFolder + "/mesos-feeder.scala"),
("/templates/template-server.scala", targetDstFolder + "/mesos-server.scala") ) foreach {
case (src, dst) => templatize(src, dst, symbols)
}

0 comments on commit ca78c51

Please sign in to comment.
Something went wrong with that request. Please try again.