Skip to content
Browse files

refactor the exception translation into a single method. make these t…

…hings graph how many exceptions are happening per host.
  • Loading branch information...
1 parent 3f96894 commit 475b78fb0355a2f2389df8f3e4f433a17716900d Robey Pointer committed Dec 9, 2010
Showing with 51 additions and 37 deletions.
  1. +51 −37 src/main/scala/com/twitter/gizzard/proxy/SqlExceptionWrappingProxy.scala
View
88 src/main/scala/com/twitter/gizzard/proxy/SqlExceptionWrappingProxy.scala
@@ -1,60 +1,74 @@
+/*
+ * Copyright 2010 Twitter, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package com.twitter.gizzard.proxy
import java.sql.{SQLException, SQLTransactionRollbackException}
import shards.ShardId
import scala.reflect.Manifest
import com.mysql.jdbc.exceptions.MySQLTransientException
import com.mysql.jdbc.exceptions.jdbc4.{MySQLTransientException => MySQLTransientException4}
+import com.twitter.ostrich.Stats
import com.twitter.querulous.database.SqlDatabaseTimeoutException
import com.twitter.querulous.query.SqlQueryTimeoutException
import com.twitter.querulous.evaluator.{QueryEvaluator, QueryEvaluatorProxy}
-class SqlExceptionWrappingProxy(shardId: ShardId) extends ExceptionHandlingProxy({e =>
- e match {
- case e: SqlQueryTimeoutException =>
- throw new shards.ShardTimeoutException(e.timeout, shardId, e)
- case e: SqlDatabaseTimeoutException =>
- throw new shards.ShardDatabaseTimeoutException(e.timeout, shardId, e)
- case e: MySQLTransientException =>
- throw new shards.NormalShardException(e.toString, shardId, null)
- case e: MySQLTransientException4 =>
- throw new shards.NormalShardException(e.toString, shardId, null)
- case e: SQLTransactionRollbackException =>
- throw new shards.NormalShardException(e.toString, shardId, null)
- case e: SQLException =>
- if ((e.toString contains "Connection") && (e.toString contains " is closed")) {
- throw new shards.NormalShardException(e.toString, shardId, null)
- } else {
- throw new shards.ShardException(e.toString, e)
- }
- case e: shards.ShardException =>
- throw e
- }
-})
-
-class ShardExceptionWrappingQueryEvaluator(shardId: ShardId, evaluator: QueryEvaluator) extends QueryEvaluatorProxy(evaluator) {
- override protected def delegate[A](f: => A) = {
- try {
- f
- } catch {
+object UnwrapException {
+ def apply(exception: Throwable, shardId: ShardId) = {
+ exception match {
case e: SqlQueryTimeoutException =>
- throw new shards.ShardTimeoutException(e.timeout, shardId, e)
+ new shards.ShardTimeoutException(e.timeout, shardId, e)
case e: SqlDatabaseTimeoutException =>
- throw new shards.ShardDatabaseTimeoutException(e.timeout, shardId, e)
+ new shards.ShardDatabaseTimeoutException(e.timeout, shardId, e)
case e: MySQLTransientException =>
- throw new shards.NormalShardException(e.toString, shardId, null)
+ new shards.NormalShardException(e.toString, shardId, null)
case e: MySQLTransientException4 =>
- throw new shards.NormalShardException(e.toString, shardId, null)
+ new shards.NormalShardException(e.toString, shardId, null)
case e: SQLTransactionRollbackException =>
- throw new shards.NormalShardException(e.toString, shardId, null)
+ new shards.NormalShardException(e.toString, shardId, null)
case e: SQLException =>
if ((e.toString contains "Connection") && (e.toString contains " is closed")) {
- throw new shards.NormalShardException(e.toString, shardId, null)
+ new shards.NormalShardException(e.toString, shardId, null)
} else {
- throw new shards.ShardException(e.toString, e)
+ new shards.ShardException(e.toString, e)
}
case e: shards.ShardException =>
- throw e
- }
+ e
+ }
+ }
+}
+
+/**
+ * Helper class that catches common SQL exceptions and translates them into gizzard shard
+ * exceptions. Not used directly in gizzard itself, but provided as a convenience. FlockDB
+ * has an example use in SqlShard.
+ */
+class SqlExceptionWrappingProxy(shardId: ShardId) extends ExceptionHandlingProxy({ e =>
+ if (shardId.hostname != "localhost") Stats.incr("db_exceptions_" + shardId.hostname)
+ throw UnwrapException(e, shardId)
+})
+
+class ShardExceptionWrappingQueryEvaluator(shardId: ShardId, evaluator: QueryEvaluator) extends QueryEvaluatorProxy(evaluator) {
+ override protected def delegate[A](f: => A) = {
+ try {
+ f
+ } catch {
+ case e: Throwable =>
+ if (shardId.hostname != "localhost") Stats.incr("db_exceptions_" + shardId.hostname)
+ throw UnwrapException(e, shardId)
+ }
}
}

0 comments on commit 475b78f

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