Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
make exceptions go to logs, ostrich, thrift appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
fizx committed Oct 18, 2010
1 parent 9bc8391 commit f70aa08
Showing 1 changed file with 58 additions and 24 deletions.
82 changes: 58 additions & 24 deletions src/main/scala/com/twitter/flockdb/EdgesService.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ import com.twitter.gizzard.shards.ShardBlackHoleException
import com.twitter.gizzard.thrift.conversions.Sequences._ import com.twitter.gizzard.thrift.conversions.Sequences._
import com.twitter.results.{Cursor, ResultWindow} import com.twitter.results.{Cursor, ResultWindow}
import operations.{ExecuteOperations, SelectOperation} import operations.{ExecuteOperations, SelectOperation}
import com.twitter.ostrich.Stats
import queries._ import queries._
import thrift.FlockException import thrift.FlockException

import net.lag.logging.Logger


class EdgesService(val nameServer: NameServer[shards.Shard], class EdgesService(val nameServer: NameServer[shards.Shard],
val forwardingManager: ForwardingManager, val forwardingManager: ForwardingManager,
val copyFactory: CopyFactory[shards.Shard], val copyFactory: CopyFactory[shards.Shard],
val schedule: PrioritizingJobScheduler, val schedule: PrioritizingJobScheduler,
future: Future, replicationFuture: Future) { future: Future, replicationFuture: Future) {

private val log = Logger.get(getClass.getName)
private val selectCompiler = new SelectCompiler(forwardingManager) private val selectCompiler = new SelectCompiler(forwardingManager)
private val executeCompiler = new ExecuteCompiler(schedule, forwardingManager) private val executeCompiler = new ExecuteCompiler(schedule, forwardingManager)


Expand All @@ -43,52 +46,83 @@ class EdgesService(val nameServer: NameServer[shards.Shard],
} }


def contains(sourceId: Long, graphId: Int, destinationId: Long): Boolean = { def contains(sourceId: Long, graphId: Int, destinationId: Long): Boolean = {
forwardingManager.find(sourceId, graphId, Direction.Forward).get(sourceId, destinationId).map { edge => rethrowExceptionsAsThrift {
edge.state == State.Normal || edge.state == State.Negative forwardingManager.find(sourceId, graphId, Direction.Forward).get(sourceId, destinationId).map { edge =>
}.getOrElse(false) edge.state == State.Normal || edge.state == State.Negative
}.getOrElse(false)
}
} }


def get(sourceId: Long, graphId: Int, destinationId: Long): Edge = { def get(sourceId: Long, graphId: Int, destinationId: Long): Edge = {
forwardingManager.find(sourceId, graphId, Direction.Forward).get(sourceId, destinationId).getOrElse { rethrowExceptionsAsThrift {
throw new FlockException("Record not found: (%d, %d, %d)".format(sourceId, graphId, destinationId)) forwardingManager.find(sourceId, graphId, Direction.Forward).get(sourceId, destinationId).getOrElse {
throw new FlockException("Record not found: (%d, %d, %d)".format(sourceId, graphId, destinationId))
}
} }
} }


def select(query: SelectQuery): ResultWindow[Long] = select(List(query)).first def select(query: SelectQuery): ResultWindow[Long] = select(List(query)).first


def select(queries: Seq[SelectQuery]): Seq[ResultWindow[Long]] = { def select(queries: Seq[SelectQuery]): Seq[ResultWindow[Long]] = {
queries.parallel(future).map { query => rethrowExceptionsAsThrift {
try { queries.parallel(future).map { query =>
selectCompiler(query.operations).select(query.page) try {
} catch { selectCompiler(query.operations).select(query.page)
case e: ShardBlackHoleException => } catch {
throw new FlockException("Shard is blackholed: " + e) case e: ShardBlackHoleException =>
throw new FlockException("Shard is blackholed: " + e)
}
} }
} }
} }


def selectEdges(queries: Seq[EdgeQuery]): Seq[ResultWindow[Edge]] = { def selectEdges(queries: Seq[EdgeQuery]): Seq[ResultWindow[Edge]] = {
queries.parallel(future).map { query => rethrowExceptionsAsThrift {
val term = query.term queries.parallel(future).map { query =>
val shard = forwardingManager.find(term.sourceId, term.graphId, Direction(term.isForward)) val term = query.term
val states = if (term.states.isEmpty) List(State.Normal) else term.states val shard = forwardingManager.find(term.sourceId, term.graphId, Direction(term.isForward))
val states = if (term.states.isEmpty) List(State.Normal) else term.states


if (term.destinationIds.isDefined) { if (term.destinationIds.isDefined) {
val results = shard.intersectEdges(term.sourceId, states, term.destinationIds.get) val results = shard.intersectEdges(term.sourceId, states, term.destinationIds.get)
new ResultWindow(results.map { edge => (edge, Cursor(edge.destinationId)) }, query.page.count, query.page.cursor) new ResultWindow(results.map { edge => (edge, Cursor(edge.destinationId)) }, query.page.count, query.page.cursor)
} else { } else {
shard.selectEdges(term.sourceId, states, query.page.count, query.page.cursor) shard.selectEdges(term.sourceId, states, query.page.count, query.page.cursor)
}
} }
} }
} }


def execute(operations: ExecuteOperations) { def execute(operations: ExecuteOperations) {
executeCompiler(operations) rethrowExceptionsAsThrift {
executeCompiler(operations)
}
} }


def count(queries: Seq[Seq[SelectOperation]]): Seq[Int] = { def count(queries: Seq[Seq[SelectOperation]]): Seq[Int] = {
queries.parallel(future).map { query => rethrowExceptionsAsThrift {
selectCompiler(query).sizeEstimate queries.parallel(future).map { query =>
selectCompiler(query).sizeEstimate
}
}
}

private def countAndRethrow(e: Throwable) = {
Stats.incr(e.getClass.getName)
throw(new FlockException(e.getMessage))
}

private def rethrowExceptionsAsThrift[A](block: => A): A = {
try {
block
} catch {
case e: FlockException => throw(e)
case e: com.twitter.gizzard.shards.ShardTimeoutException => countAndRethrow(e)
case e: com.twitter.gizzard.shards.ShardDatabaseTimeoutException => countAndRethrow(e)
case e: Throwable => {
log.error(e, "Unhandled error in EdgesService")
throw(new FlockException(e.getMessage))
}
} }
} }
} }

0 comments on commit f70aa08

Please sign in to comment.