All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scray.service.qservice.thrifscala.ScrayCombinedStatefulTService$FinagleService.scala Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
/**
 * Generated by Scrooge
 *   version: 4.16.0
 *   rev: 0201cac9fdd6188248d42da91fd14c87744cc4a5
 *   built at: 20170421-124523
 */
package scray.service.qservice.thrifscala

import com.twitter.finagle.Thrift
import com.twitter.finagle.stats.{NullStatsReceiver, StatsReceiver}
import com.twitter.scrooge.{ThriftStruct, TReusableMemoryTransport}
import com.twitter.util.{Future, Return, Throw, Throwables}
import java.nio.ByteBuffer
import java.util.Arrays
import org.apache.thrift.protocol._
import org.apache.thrift.TApplicationException
import org.apache.thrift.transport.TMemoryInputTransport
import scala.collection.mutable.{
  ArrayBuffer => mutable$ArrayBuffer, HashMap => mutable$HashMap}
import scala.collection.{Map, Set}

import scala.language.higherKinds

/**
 * Combined Scray service
 * Combines ScrayMetaTService operations with ScrayStatefulTService operations
 * The combined service is thus provided on a single endpoint (port)
 */
@javax.annotation.Generated(value = Array("com.twitter.scrooge.Compiler"))
class ScrayCombinedStatefulTService$FinagleService(
  iface: ScrayCombinedStatefulTService[Future],
  protocolFactory: TProtocolFactory,
  stats: StatsReceiver,
  maxThriftBufferSize: Int,
  serviceName: String
) extends com.twitter.finagle.Service[Array[Byte], Array[Byte]] {
  import ScrayCombinedStatefulTService._

  def this(
    iface: ScrayCombinedStatefulTService[Future],
    protocolFactory: TProtocolFactory,
    stats: StatsReceiver,
    maxThriftBufferSize: Int
  ) = this(iface, protocolFactory, stats, maxThriftBufferSize, "ScrayCombinedStatefulTService")

  def this(
    iface: ScrayCombinedStatefulTService[Future],
    protocolFactory: TProtocolFactory
  ) = this(iface, protocolFactory, NullStatsReceiver, Thrift.maxThriftBufferSize)

  private[this] val tlReusableBuffer = new ThreadLocal[TReusableMemoryTransport] {
    override def initialValue() = TReusableMemoryTransport(512)
  }

  private[this] def reusableBuffer: TReusableMemoryTransport = {
    val buf = tlReusableBuffer.get()
    buf.reset()
    buf
  }

  private[this] val resetCounter = stats.scope("buffer").counter("resetCount")

  private[this] def resetBuffer(trans: TReusableMemoryTransport): Unit = {
    if (trans.currentCapacity > maxThriftBufferSize) {
      resetCounter.incr()
      tlReusableBuffer.remove()
    }
  }

  protected val functionMap = new mutable$HashMap[String, (TProtocol, Int) => Future[Array[Byte]]]()

  protected def addFunction(name: String, f: (TProtocol, Int) => Future[Array[Byte]]): Unit = {
    functionMap(name) = f
  }

  protected def exception(name: String, seqid: Int, code: Int, message: String): Future[Array[Byte]] = {
    try {
      val x = new TApplicationException(code, message)
      val memoryBuffer = reusableBuffer
      try {
        val oprot = protocolFactory.getProtocol(memoryBuffer)

        oprot.writeMessageBegin(new TMessage(name, TMessageType.EXCEPTION, seqid))
        x.write(oprot)
        oprot.writeMessageEnd()
        oprot.getTransport().flush()
        Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()))
      } finally {
        resetBuffer(memoryBuffer)
      }
    } catch {
      case e: Exception => Future.exception(e)
    }
  }

  protected def reply(name: String, seqid: Int, result: ThriftStruct): Future[Array[Byte]] = {
    try {
      val memoryBuffer = reusableBuffer
      try {
        val oprot = protocolFactory.getProtocol(memoryBuffer)

        oprot.writeMessageBegin(new TMessage(name, TMessageType.REPLY, seqid))
        result.write(oprot)
        oprot.writeMessageEnd()

        Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()))
      } finally {
        resetBuffer(memoryBuffer)
      }
    } catch {
      case e: Exception => Future.exception(e)
    }
  }

  final def apply(request: Array[Byte]): Future[Array[Byte]] = {
    val inputTransport = new TMemoryInputTransport(request)
    val iprot = protocolFactory.getProtocol(inputTransport)

    try {
      val msg = iprot.readMessageBegin()
      val func = functionMap.get(msg.name)
      func match {
        case _root_.scala.Some(fn) =>
          fn(iprot, msg.seqid)
        case _ =>
          TProtocolUtil.skip(iprot, TType.STRUCT)
          exception(msg.name, msg.seqid, TApplicationException.UNKNOWN_METHOD,
            "Invalid method name: '" + msg.name + "'")
      }
    } catch {
      case e: Exception => Future.exception(e)
    }
  }

  // ---- end boilerplate.

  private[this] val scopedStats = if (serviceName != "") stats.scope(serviceName) else stats
  private[this] object __stats_getServiceEndpoints {
    val RequestsCounter = scopedStats.scope("getServiceEndpoints").counter("requests")
    val SuccessCounter = scopedStats.scope("getServiceEndpoints").counter("success")
    val FailuresCounter = scopedStats.scope("getServiceEndpoints").counter("failures")
    val FailuresScope = scopedStats.scope("getServiceEndpoints").scope("failures")
  }
  addFunction("getServiceEndpoints", { (iprot: TProtocol, seqid: Int) =>
    try {
      __stats_getServiceEndpoints.RequestsCounter.incr()
      val args = GetServiceEndpoints.Args.decode(iprot)
      iprot.readMessageEnd()
      (try {
        iface.getServiceEndpoints()
      } catch {
        case e: Exception => Future.exception(e)
      }).flatMap { value: Seq[scray.service.qservice.thrifscala.ScrayTServiceEndpoint] =>
        reply("getServiceEndpoints", seqid, GetServiceEndpoints.Result(success = Some(value)))
      }.rescue {
        case e => Future.exception(e)
      }.respond {
        case Return(_) =>
          __stats_getServiceEndpoints.SuccessCounter.incr()
        case Throw(ex) =>
          __stats_getServiceEndpoints.FailuresCounter.incr()
          __stats_getServiceEndpoints.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
      }
    } catch {
      case e: TProtocolException => {
        iprot.readMessageEnd()
        exception("getServiceEndpoints", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
      }
      case e: Exception => Future.exception(e)
    }
  })
  private[this] object __stats_addServiceEndpoint {
    val RequestsCounter = scopedStats.scope("addServiceEndpoint").counter("requests")
    val SuccessCounter = scopedStats.scope("addServiceEndpoint").counter("success")
    val FailuresCounter = scopedStats.scope("addServiceEndpoint").counter("failures")
    val FailuresScope = scopedStats.scope("addServiceEndpoint").scope("failures")
  }
  addFunction("addServiceEndpoint", { (iprot: TProtocol, seqid: Int) =>
    try {
      __stats_addServiceEndpoint.RequestsCounter.incr()
      val args = AddServiceEndpoint.Args.decode(iprot)
      iprot.readMessageEnd()
      (try {
        iface.addServiceEndpoint(args.endpoint)
      } catch {
        case e: Exception => Future.exception(e)
      }).flatMap { value: scray.service.qservice.thrifscala.ScrayTServiceEndpoint =>
        reply("addServiceEndpoint", seqid, AddServiceEndpoint.Result(success = Some(value)))
      }.rescue {
        case e => Future.exception(e)
      }.respond {
        case Return(_) =>
          __stats_addServiceEndpoint.SuccessCounter.incr()
        case Throw(ex) =>
          __stats_addServiceEndpoint.FailuresCounter.incr()
          __stats_addServiceEndpoint.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
      }
    } catch {
      case e: TProtocolException => {
        iprot.readMessageEnd()
        exception("addServiceEndpoint", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
      }
      case e: Exception => Future.exception(e)
    }
  })
  private[this] object __stats_refreshServiceEndpoint {
    val RequestsCounter = scopedStats.scope("refreshServiceEndpoint").counter("requests")
    val SuccessCounter = scopedStats.scope("refreshServiceEndpoint").counter("success")
    val FailuresCounter = scopedStats.scope("refreshServiceEndpoint").counter("failures")
    val FailuresScope = scopedStats.scope("refreshServiceEndpoint").scope("failures")
  }
  addFunction("refreshServiceEndpoint", { (iprot: TProtocol, seqid: Int) =>
    try {
      __stats_refreshServiceEndpoint.RequestsCounter.incr()
      val args = RefreshServiceEndpoint.Args.decode(iprot)
      iprot.readMessageEnd()
      (try {
        iface.refreshServiceEndpoint(args.endpointID)
      } catch {
        case e: Exception => Future.exception(e)
      }).flatMap { value: Unit =>
        reply("refreshServiceEndpoint", seqid, RefreshServiceEndpoint.Result())
      }.rescue {
        case e => Future.exception(e)
      }.respond {
        case Return(_) =>
          __stats_refreshServiceEndpoint.SuccessCounter.incr()
        case Throw(ex) =>
          __stats_refreshServiceEndpoint.FailuresCounter.incr()
          __stats_refreshServiceEndpoint.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
      }
    } catch {
      case e: TProtocolException => {
        iprot.readMessageEnd()
        exception("refreshServiceEndpoint", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
      }
      case e: Exception => Future.exception(e)
    }
  })
  private[this] object __stats_ping {
    val RequestsCounter = scopedStats.scope("ping").counter("requests")
    val SuccessCounter = scopedStats.scope("ping").counter("success")
    val FailuresCounter = scopedStats.scope("ping").counter("failures")
    val FailuresScope = scopedStats.scope("ping").scope("failures")
  }
  addFunction("ping", { (iprot: TProtocol, seqid: Int) =>
    try {
      __stats_ping.RequestsCounter.incr()
      val args = Ping.Args.decode(iprot)
      iprot.readMessageEnd()
      (try {
        iface.ping()
      } catch {
        case e: Exception => Future.exception(e)
      }).flatMap { value: Boolean =>
        reply("ping", seqid, Ping.Result(success = Some(value)))
      }.rescue {
        case e => Future.exception(e)
      }.respond {
        case Return(_) =>
          __stats_ping.SuccessCounter.incr()
        case Throw(ex) =>
          __stats_ping.FailuresCounter.incr()
          __stats_ping.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
      }
    } catch {
      case e: TProtocolException => {
        iprot.readMessageEnd()
        exception("ping", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
      }
      case e: Exception => Future.exception(e)
    }
  })
  private[this] object __stats_shutdown {
    val RequestsCounter = scopedStats.scope("shutdown").counter("requests")
    val SuccessCounter = scopedStats.scope("shutdown").counter("success")
    val FailuresCounter = scopedStats.scope("shutdown").counter("failures")
    val FailuresScope = scopedStats.scope("shutdown").scope("failures")
  }
  addFunction("shutdown", { (iprot: TProtocol, seqid: Int) =>
    try {
      __stats_shutdown.RequestsCounter.incr()
      val args = Shutdown.Args.decode(iprot)
      iprot.readMessageEnd()
      (try {
        iface.shutdown(args.waitNanos)
      } catch {
        case e: Exception => Future.exception(e)
      }).flatMap { value: Unit =>
        reply("shutdown", seqid, Shutdown.Result())
      }.rescue {
        case e => Future.exception(e)
      }.respond {
        case Return(_) =>
          __stats_shutdown.SuccessCounter.incr()
        case Throw(ex) =>
          __stats_shutdown.FailuresCounter.incr()
          __stats_shutdown.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
      }
    } catch {
      case e: TProtocolException => {
        iprot.readMessageEnd()
        exception("shutdown", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
      }
      case e: Exception => Future.exception(e)
    }
  })
  private[this] object __stats_query {
    val RequestsCounter = scopedStats.scope("query").counter("requests")
    val SuccessCounter = scopedStats.scope("query").counter("success")
    val FailuresCounter = scopedStats.scope("query").counter("failures")
    val FailuresScope = scopedStats.scope("query").scope("failures")
  }
  addFunction("query", { (iprot: TProtocol, seqid: Int) =>
    try {
      __stats_query.RequestsCounter.incr()
      val args = Query.Args.decode(iprot)
      iprot.readMessageEnd()
      (try {
        iface.query(args.query)
      } catch {
        case e: Exception => Future.exception(e)
      }).flatMap { value: scray.service.qmodel.thrifscala.ScrayUUID =>
        reply("query", seqid, Query.Result(success = Some(value)))
      }.rescue {
        case e: scray.service.qservice.thrifscala.ScrayTException => {
          reply("query", seqid, Query.Result(ex = Some(e)))
        }
        case e => Future.exception(e)
      }.respond {
        case Return(_) =>
          __stats_query.SuccessCounter.incr()
        case Throw(ex) =>
          __stats_query.FailuresCounter.incr()
          __stats_query.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
      }
    } catch {
      case e: TProtocolException => {
        iprot.readMessageEnd()
        exception("query", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
      }
      case e: Exception => Future.exception(e)
    }
  })
  private[this] object __stats_getResults {
    val RequestsCounter = scopedStats.scope("getResults").counter("requests")
    val SuccessCounter = scopedStats.scope("getResults").counter("success")
    val FailuresCounter = scopedStats.scope("getResults").counter("failures")
    val FailuresScope = scopedStats.scope("getResults").scope("failures")
  }
  addFunction("getResults", { (iprot: TProtocol, seqid: Int) =>
    try {
      __stats_getResults.RequestsCounter.incr()
      val args = GetResults.Args.decode(iprot)
      iprot.readMessageEnd()
      (try {
        iface.getResults(args.queryId)
      } catch {
        case e: Exception => Future.exception(e)
      }).flatMap { value: scray.service.qservice.thrifscala.ScrayTResultFrame =>
        reply("getResults", seqid, GetResults.Result(success = Some(value)))
      }.rescue {
        case e: scray.service.qservice.thrifscala.ScrayTException => {
          reply("getResults", seqid, GetResults.Result(ex = Some(e)))
        }
        case e => Future.exception(e)
      }.respond {
        case Return(_) =>
          __stats_getResults.SuccessCounter.incr()
        case Throw(ex) =>
          __stats_getResults.FailuresCounter.incr()
          __stats_getResults.FailuresScope.counter(Throwables.mkString(ex): _*).incr()
      }
    } catch {
      case e: TProtocolException => {
        iprot.readMessageEnd()
        exception("getResults", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
      }
      case e: Exception => Future.exception(e)
    }
  })
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy