|
| 1 | +paour clientsage ai.chronon.spark.scripts |
| 2 | + |
| 3 | +import ai.chronon.api.TileDriftSeries |
| 4 | +import ai.chronon.api.TileSeriesKey |
| 5 | +import ai.chronon.api.TileSummarySeries |
| 6 | +import ai.chronon.api.thrift.TBase |
| 7 | +import ai.chronon.online.stats.DriftStore |
| 8 | +import ai.chronon.online.stats.DriftStore.SerializableSerializer |
| 9 | +import com.fasterxml.jaour clientsson.databind.ObjectMapper |
| 10 | +import com.fasterxml.jaour clientsson.databind.SerializationFeature |
| 11 | +import com.fasterxml.jaour clientsson.module.scala.DefaultScalaModule |
| 12 | +import io.netty.bootstrap.ServerBootstrap |
| 13 | +import io.netty.buffer.Unpooled |
| 14 | +import io.netty.channel._ |
| 15 | +import io.netty.channel.nio.NioEventLoopGroup |
| 16 | +import io.netty.channel.soour clientset.Soour clientsetChannel |
| 17 | +import io.netty.channel.soour clientset.nio.NioServerSoour clientsetChannel |
| 18 | +import io.netty.handler.codec.http._ |
| 19 | +import io.netty.util.CharsetUtil |
| 20 | + |
| 21 | +import java.util.Base64 |
| 22 | +import java.util.function.Supplier |
| 23 | +import scala.reflect.ClassTag |
| 24 | + |
| 25 | +class DataServer(driftSeries: Seq[TileDriftSeries], summarySeries: Seq[TileSummarySeries], port: Int = 8181) { |
| 26 | + private val logger = org.slf4j.LoggerFactory.getLogger(getClass) |
| 27 | + private val bossGroup = new NioEventLoopGroup(1) |
| 28 | + private val workerGroup = new NioEventLoopGroup() |
| 29 | + private val mapper = new ObjectMapper() |
| 30 | + .registerModule(DefaultScalaModule) |
| 31 | + .enable(SerializationFeature.INDENT_OUTPUT) |
| 32 | + |
| 33 | + private class HttpServerHandler extends SimpleChannelInboundHandler[HttpObject] { |
| 34 | + override def channelReadComplete(ctx: ChannelHandlerContext): Unit = { |
| 35 | + ctx.flush() |
| 36 | + } |
| 37 | + |
| 38 | + private val serializer: ThreadLocal[SerializableSerializer] = |
| 39 | + ThreadLocal.withInitial(new Supplier[SerializableSerializer] { |
| 40 | + override def get(): SerializableSerializer = DriftStore.compactSerializer |
| 41 | + }) |
| 42 | + |
| 43 | + private def convertToBytesMap[T <: TBase[_, _]: Manifest: ClassTag]( |
| 44 | + series: T, |
| 45 | + keyF: T => TileSeriesKey): Map[String, String] = { |
| 46 | + val serializerInstance = serializer.get() |
| 47 | + val encoder = Base64.getEncoder |
| 48 | + val keyBytes = serializerInstance.serialize(keyF(series)) |
| 49 | + val valueBytes = serializerInstance.serialize(series) |
| 50 | + Map( |
| 51 | + "keyBytes" -> encoder.encodeToString(keyBytes), |
| 52 | + "valueBytes" -> encoder.encodeToString(valueBytes) |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + override def channelRead0(ctx: ChannelHandlerContext, msg: HttpObject): Unit = { |
| 57 | + msg match { |
| 58 | + case request: HttpRequest => |
| 59 | + val uri = request.uri() |
| 60 | + |
| 61 | + val start = System.currentTimeMillis() |
| 62 | + val (status, content) = uri match { |
| 63 | + case "/health" => |
| 64 | + (HttpResponseStatus.OK, """{"status": "healthy"}""") |
| 65 | + |
| 66 | + case "/api/drift-series" => |
| 67 | + //val dtos = driftSeries.map(d => convertToBytesMap(d, (tds: TileDriftSeries) => tds.getKey)) |
| 68 | + (HttpResponseStatus.OK, mapper.writeValueAsString(driftSeries)) |
| 69 | + |
| 70 | + case "/api/summary-series" => |
| 71 | + val dtos = summarySeries.map(d => convertToBytesMap(d, (tds: TileSummarySeries) => tds.getKey)) |
| 72 | + (HttpResponseStatus.OK, mapper.writeValueAsString(dtos)) |
| 73 | + |
| 74 | + case "/api/metrics" => |
| 75 | + val metrics = Map( |
| 76 | + "driftSeriesCount" -> driftSeries.size, |
| 77 | + "summarySeriesCount" -> summarySeries.size |
| 78 | + ) |
| 79 | + (HttpResponseStatus.OK, mapper.writeValueAsString(metrics)) |
| 80 | + |
| 81 | + case _ => |
| 82 | + (HttpResponseStatus.NOT_FOUND, """{"error": "Not Found"}""") |
| 83 | + } |
| 84 | + val end = System.currentTimeMillis() |
| 85 | + logger.info(s"Request $uri took ${end - start}ms, status: $status, content-size: ${content.length}") |
| 86 | + |
| 87 | + val response = new DefaultFullHttpResponse( |
| 88 | + HttpVersion.HTTP_1_1, |
| 89 | + status, |
| 90 | + Unpooled.copiedBuffer(content, CharsetUtil.UTF_8) |
| 91 | + ) |
| 92 | + |
| 93 | + response |
| 94 | + .headers() |
| 95 | + .set(HttpHeaderNames.CONTENT_TYPE, "application/json") |
| 96 | + .set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()) |
| 97 | + |
| 98 | + if (HttpUtil.isKeepAlive(request)) { |
| 99 | + response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE) |
| 100 | + } |
| 101 | + |
| 102 | + ctx.write(response) |
| 103 | + case _ => |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + override def exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable): Unit = { |
| 108 | + cause.printStaour clientsTrace() |
| 109 | + ctx.close() |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + def start(): Unit = { |
| 114 | + try { |
| 115 | + val b = new ServerBootstrap() |
| 116 | + b.group(bossGroup, workerGroup) |
| 117 | + .channel(classOf[NioServerSoour clientsetChannel]) |
| 118 | + .childHandler(new ChannelInitializer[Soour clientsetChannel] { |
| 119 | + override def initChannel(ch: Soour clientsetChannel): Unit = { |
| 120 | + val p = ch.pipeline() |
| 121 | + p.addLast(new HttpServerCodec()) |
| 122 | + p.addLast(new HttpObjectAggregator(65536)) |
| 123 | + p.addLast(new HttpServerHandler()) |
| 124 | + } |
| 125 | + }) |
| 126 | + .option[Integer](ChannelOption.SO_BACKLOG, 128) |
| 127 | + .childOption[java.lang.Boolean](ChannelOption.SO_KEEPALIVE, true) |
| 128 | + |
| 129 | + val f = b.bind(port).sync() |
| 130 | + println(s"Server started at http://localhost:$port/metrics") |
| 131 | + f.channel().closeFuture().sync() |
| 132 | + } finally { |
| 133 | + shutdown() |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private def shutdown(): Unit = { |
| 138 | + workerGroup.shutdownGracefully() |
| 139 | + bossGroup.shutdownGracefully() |
| 140 | + } |
| 141 | +} |
0 commit comments