-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:nkallen/querulous
- Loading branch information
Showing
23 changed files
with
279 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,9 @@ | |||
db { | db { | ||
hostname = "localhost" | |||
username = "root" | username = "root" | ||
password = "" | password = "" | ||
} | url_options { | ||
useUnicode = "true" | |||
characterEncoding = "UTF-8" | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,8 +1,8 @@ | |||
#Project properties | #Project properties | ||
#Sat Apr 17 13:53:31 PDT 2010 | #Wed Sep 08 16:18:46 PDT 2010 | ||
project.organization=com.twitter | project.organization=com.twitter | ||
project.name=querulous | project.name=querulous | ||
sbt.version=0.7.4 | sbt.version=0.7.4 | ||
project.version=1.1.13 | project.version=1.2.1 | ||
build.scala.versions=2.7.7 | build.scala.versions=2.7.7 | ||
project.initialize=false | project.initialize=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
package com.twitter.querulous | |||
|
|||
import com.twitter.xrayspecs.{Time, Duration} | |||
import com.twitter.xrayspecs.TimeConversions._ | |||
import java.sql.{SQLException, SQLIntegrityConstraintViolationException} | |||
|
|||
|
|||
trait AutoDisabler { | |||
protected val disableErrorCount: Int | |||
protected val disableDuration: Duration | |||
|
|||
private var disabledUntil: Time = Time.never | |||
private var consecutiveErrors = 0 | |||
|
|||
protected def throwIfDisabled(throwMessage: String): Unit = { | |||
synchronized { | |||
if (Time.now < disabledUntil) { | |||
throw new SQLException("Server is temporarily disabled: " + throwMessage) | |||
} | |||
} | |||
} | |||
|
|||
protected def throwIfDisabled(): Unit = { throwIfDisabled("") } | |||
|
|||
protected def noteOperationOutcome(success: Boolean) { | |||
synchronized { | |||
if (success) { | |||
consecutiveErrors = 0 | |||
} else { | |||
consecutiveErrors += 1 | |||
if (consecutiveErrors >= disableErrorCount) { | |||
disabledUntil = disableDuration.fromNow | |||
} | |||
} | |||
} | |||
} | |||
} |
42 changes: 42 additions & 0 deletions
42
src/main/scala/com/twitter/querulous/ConnectionDestroying.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
package com.twitter.querulous.query | |||
|
|||
import java.sql.Connection | |||
import org.apache.commons.dbcp.{DelegatingConnection => DBCPConnection} | |||
import com.mysql.jdbc.{ConnectionImpl => MySQLConnection} | |||
|
|||
|
|||
// Emergency connection destruction toolkit | |||
|
|||
trait ConnectionDestroying { | |||
def destroyConnection(conn: Connection) { | |||
if ( !conn.isClosed ) | |||
conn match { | |||
case c: DBCPConnection => | |||
destroyDbcpWrappedConnection(c) | |||
case c: MySQLConnection => | |||
destroyMysqlConnection(c) | |||
case _ => error("Unsupported driver type, cannot reliably timeout.") | |||
} | |||
} | |||
|
|||
def destroyDbcpWrappedConnection(conn: DBCPConnection) { | |||
val inner = conn.getInnermostDelegate | |||
|
|||
if ( inner != null ) { | |||
destroyConnection(inner) | |||
} else { | |||
// this should never happen if we use our own ApachePoolingDatabase to get connections. | |||
error("Could not get access to the delegate connection. Make sure the dbcp connection pool allows access to underlying connections.") | |||
} | |||
|
|||
// "close" the wrapper so that it updates its internal bookkeeping, just do it | |||
try { conn.close } catch { case _ => } | |||
} | |||
|
|||
def destroyMysqlConnection(conn: MySQLConnection) { | |||
val abort = Class.forName("com.mysql.jdbc.ConnectionImpl").getDeclaredMethod("abortInternal") | |||
abort.setAccessible(true) | |||
|
|||
abort.invoke(conn) | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/scala/com/twitter/querulous/database/AutoDisablingDatabase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
package com.twitter.querulous.database | |||
|
|||
import com.twitter.xrayspecs.Duration | |||
import com.twitter.xrayspecs.TimeConversions._ | |||
import java.sql.{Connection, SQLException, SQLIntegrityConstraintViolationException} | |||
|
|||
|
|||
class AutoDisablingDatabaseFactory(databaseFactory: DatabaseFactory, disableErrorCount: Int, disableDuration: Duration) extends DatabaseFactory { | |||
def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = { | |||
new AutoDisablingDatabase( | |||
databaseFactory(dbhosts, dbname, username, password, urlOptions), | |||
dbhosts.first, | |||
disableErrorCount, | |||
disableDuration) | |||
} | |||
} | |||
|
|||
class AutoDisablingDatabase(database: Database, dbhost: String, protected val disableErrorCount: Int, protected val disableDuration: Duration) extends Database with AutoDisabler { | |||
def open() = { | |||
throwIfDisabled(dbhost) | |||
try { | |||
val rv = database.open() | |||
noteOperationOutcome(true) | |||
rv | |||
} catch { | |||
case e: SQLException => | |||
noteOperationOutcome(false) | |||
throw e | |||
case e: Exception => | |||
throw e | |||
} | |||
} | |||
|
|||
def close(connection: Connection) { database.close(connection) } | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.