|
| 1 | +package gearpump.benchmark |
| 2 | + |
| 3 | +import akka.actor.ActorSystem |
| 4 | +import benchmark.common.Utils |
| 5 | +import benchmark.common.advertising.{CampaignProcessorCommon, RedisAdCampaignCache} |
| 6 | +import io.gearpump.Message |
| 7 | +import io.gearpump.partitioner.{Partitioner, UnicastPartitioner, HashPartitioner} |
| 8 | +import io.gearpump.cluster.UserConfig |
| 9 | +import io.gearpump.cluster.client.ClientContext |
| 10 | +import io.gearpump.streaming.kafka.lib.StringMessageDecoder |
| 11 | +import io.gearpump.streaming.{StreamApplication, Processor} |
| 12 | +import io.gearpump.streaming.kafka.{KafkaSource, KafkaStorageFactory} |
| 13 | +import io.gearpump.streaming.source.{DefaultTimeStampFilter, DataSourceProcessor} |
| 14 | +import io.gearpump.streaming.task.{StartTime, Task, TaskContext} |
| 15 | +import io.gearpump.util.{AkkaApp, Graph} |
| 16 | +import org.json.JSONObject |
| 17 | +import io.gearpump.util.Graph.Node |
| 18 | +import scala.collection.JavaConverters._ |
| 19 | + |
| 20 | +object Advertising extends AkkaApp{ |
| 21 | + |
| 22 | + def application(args: Array[String], system: ActorSystem) : StreamApplication = { |
| 23 | + implicit val actorSystem = system |
| 24 | + val commonConfig = Utils.findAndReadConfigFile(args(0), true).asInstanceOf[java.util.Map[String, Any]] |
| 25 | + |
| 26 | + val cores = commonConfig.get("process.cores").asInstanceOf[Int] |
| 27 | + val topic = commonConfig.get("kafka.topic").asInstanceOf[String] |
| 28 | + val partitions = commonConfig.get("kafka.partitions").asInstanceOf[Int] |
| 29 | + val redisHost = commonConfig.get("redis.host").asInstanceOf[String] |
| 30 | + |
| 31 | + val zookeeperHosts = commonConfig.get("zookeeper.servers").asInstanceOf[java.util.List[String]] match { |
| 32 | + case l: java.util.List[String] => l.asScala.toSeq |
| 33 | + case other => throw new ClassCastException(other + " not a List[String]") |
| 34 | + } |
| 35 | + val zookeeperPort = commonConfig.get("zookeeper.port").asInstanceOf[Int] |
| 36 | + val zookeeperConnect = zookeeperHosts.map(_ + ":" + zookeeperPort).mkString(",") |
| 37 | + |
| 38 | + val kafkaHosts = commonConfig.get("kafka.brokers").asInstanceOf[java.util.List[String]] match { |
| 39 | + case l: java.util.List[String] => l.asScala.toSeq |
| 40 | + case other => throw new ClassCastException(other + " not a List[String]") |
| 41 | + } |
| 42 | + val kafkaPort = commonConfig.get("kafka.port").asInstanceOf[Int] |
| 43 | + val brokerList = kafkaHosts.map(_ + ":" + kafkaPort).mkString(",") |
| 44 | + |
| 45 | + val parallel = Math.max(1, cores / 7) |
| 46 | + val gearConfig = UserConfig.empty.withString("redis.host", redisHost) |
| 47 | + val offsetStorageFactory = new KafkaStorageFactory(zookeeperConnect, brokerList) |
| 48 | + val source = new KafkaSource(topic, zookeeperConnect, offsetStorageFactory, new StringMessageDecoder, new DefaultTimeStampFilter) |
| 49 | + val sourceProcessor = DataSourceProcessor(source, partitions) |
| 50 | + val deserializer = Processor[DeserializeTask](parallel) |
| 51 | + val filter = Processor[EventFilterTask](parallel) |
| 52 | + val projection = Processor[EventProjectionTask](parallel) |
| 53 | + val join = Processor[RedisJoinTask](parallel) |
| 54 | + val campaign = Processor[CampaignProcessorTask](parallel * 2) |
| 55 | + val partitioner = new AdPartitioner |
| 56 | + |
| 57 | + val graph = Graph(sourceProcessor ~ new HashPartitioner ~> deserializer ~> filter ~> projection ~> join ~ partitioner ~> campaign) |
| 58 | + StreamApplication("Advertising", graph, gearConfig) |
| 59 | + } |
| 60 | + |
| 61 | + override def main(akkaConf: Advertising.Config, args: Array[String]): Unit = { |
| 62 | + val context = ClientContext(akkaConf) |
| 63 | + context.submit(application(args, context.system)) |
| 64 | + context.close() |
| 65 | + } |
| 66 | + |
| 67 | + override def help: Unit = {} |
| 68 | +} |
| 69 | + |
| 70 | +class AdPartitioner extends UnicastPartitioner { |
| 71 | + override def getPartition(msg: Message, partitionNum: Int, currentPartitionId: Int): Int = { |
| 72 | + (msg.msg.asInstanceOf[(String, String, String)]._1.hashCode & Integer.MAX_VALUE) % partitionNum |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class DeserializeTask(taskContext : TaskContext, conf: UserConfig) extends Task(taskContext, conf) { |
| 77 | + override def onNext(msg : Message) : Unit = { |
| 78 | + val jsonObj = new JSONObject(msg.msg.asInstanceOf[String]) |
| 79 | + val tuple = ( |
| 80 | + jsonObj.getString("user_id"), |
| 81 | + jsonObj.getString("page_id"), |
| 82 | + jsonObj.getString("ad_id"), |
| 83 | + jsonObj.getString("ad_type"), |
| 84 | + jsonObj.getString("event_type"), |
| 85 | + jsonObj.getString("event_time"), |
| 86 | + jsonObj.getString("ip_address") |
| 87 | + ) |
| 88 | + taskContext.output(Message(tuple, msg.timestamp)) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class EventFilterTask(taskContext : TaskContext, conf: UserConfig) extends Task(taskContext, conf) { |
| 93 | + override def onNext(msg: Message): Unit = { |
| 94 | + val tuple = msg.msg.asInstanceOf[(String, String, String, String, String, String, String)] |
| 95 | + if(tuple._5 == "view") { |
| 96 | + taskContext.output(msg) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +class EventProjectionTask(taskContext : TaskContext, conf: UserConfig) extends Task(taskContext, conf) { |
| 102 | + override def onNext(msg: Message): Unit = { |
| 103 | + val tuple = msg.msg.asInstanceOf[(String, String, String, String, String, String, String)] |
| 104 | + taskContext.output(Message((tuple._3, tuple._6), msg.timestamp)) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class RedisJoinTask(taskContext : TaskContext, conf: UserConfig) extends Task(taskContext, conf) { |
| 109 | + private val redisHost = conf.getString("redis.host").get |
| 110 | + private val redisAdCampaignCache = new RedisAdCampaignCache(redisHost) |
| 111 | + |
| 112 | + override def onStart(startTime : StartTime) : Unit = { |
| 113 | + redisAdCampaignCache.prepare() |
| 114 | + } |
| 115 | + |
| 116 | + override def onNext(msg: Message): Unit = { |
| 117 | + val (ad_id, event_time) = msg.msg.asInstanceOf[(String, String)] |
| 118 | + val campaign_id = redisAdCampaignCache.execute(ad_id) |
| 119 | + if(campaign_id != null) { |
| 120 | + val result = (campaign_id, ad_id, event_time) |
| 121 | + taskContext.output(Message(result, msg.timestamp)) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class CampaignProcessorTask(taskContext : TaskContext, conf: UserConfig) extends Task(taskContext, conf) { |
| 127 | + private val redisHost = conf.getString("redis.host").get |
| 128 | + private val campaignProcessorCommon = new CampaignProcessorCommon(redisHost) |
| 129 | + |
| 130 | + override def onStart(startTime : StartTime) : Unit = { |
| 131 | + campaignProcessorCommon.prepare() |
| 132 | + } |
| 133 | + |
| 134 | + override def onNext(msg: Message): Unit = { |
| 135 | + val (campaign_id, _, event_time) = msg.msg.asInstanceOf[(String, String, String)] |
| 136 | + campaignProcessorCommon.execute(campaign_id, event_time) |
| 137 | + } |
| 138 | +} |
0 commit comments