-
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 'fail_fast_on_apache_pool_reserve'
- Loading branch information
Showing
6 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/twitter/querulous/database/TimingOutDatabase.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.twitter.querulous.database | ||
|
||
import java.util.concurrent.{TimeoutException => JTimeoutException, _} | ||
import com.twitter.xrayspecs.Duration | ||
|
||
class TimingOutDatabaseFactory(databaseFactory: DatabaseFactory, poolSize: Int, timeout: Duration) extends DatabaseFactory { | ||
def apply(dbhosts: List[String], dbname: String, username: String, password: String): Database = { | ||
new TimingOutDatabase(databaseFactory(dbhosts, dbname, username, password), poolSize, timeout) | ||
} | ||
} | ||
|
||
class TimingOutDatabase(database: Database, poolSize: Int, timeout: Duration) extends Database { | ||
private val executor = new ThreadPoolExecutor(poolSize, poolSize, 0, TimeUnit.SECONDS, new SynchronousQueue[Runnable]) | ||
|
||
override def open(): Connection = { | ||
val future = new FutureTask(new Callable[Connection] { | ||
def call = { | ||
database.open() | ||
} | ||
}) | ||
try { | ||
executor.execute(future) | ||
future.get(timeout.inMillis, TimeUnit.MILLISECONDS) | ||
} catch { | ||
case e: JTimeoutException => throw new TimeoutException | ||
case e: RejectedExecutionException => throw new TimeoutException | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.twitter.querulous | ||
|
||
import net.lag.configgy.Configgy | ||
import com.twitter.querulous.database.{MemoizingDatabaseFactory, ApachePoolingDatabaseFactory} | ||
import com.twitter.querulous.query.SqlQueryFactory | ||
import com.twitter.querulous.evaluator.StandardQueryEvaluatorFactory | ||
import com.twitter.xrayspecs.Time | ||
import com.twitter.xrayspecs.TimeConversions._ | ||
|
||
object TestEvaluator { | ||
val testDatabaseFactory = new MemoizingDatabaseFactory(new ApachePoolingDatabaseFactory(10, 10, 1.second, 10.millis, false, 0.seconds)) | ||
val testQueryFactory = new SqlQueryFactory | ||
val testEvaluatorFactory = new StandardQueryEvaluatorFactory(testDatabaseFactory, testQueryFactory) | ||
} | ||
|
||
|
||
|
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
47 changes: 47 additions & 0 deletions
47
src/test/scala/com/twitter/querulous/unit/TimingOutDatabaseSpec.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.twitter.querulous.unit | ||
|
||
import java.util.concurrent.{CountDownLatch, TimeUnit} | ||
import com.twitter.querulous.TimeoutException | ||
import com.twitter.querulous.database.{Database, TimingOutDatabase} | ||
import com.twitter.xrayspecs.Time | ||
import com.twitter.xrayspecs.TimeConversions._ | ||
import org.specs.Specification | ||
import org.specs.mock.{JMocker, ClassMocker} | ||
|
||
object TimingOutDatabaseSpec extends Specification with JMocker with ClassMocker { | ||
"TimingOutDatabaseSpec" should { | ||
Time.reset() | ||
val latch = new CountDownLatch(1) | ||
val timeout = 1.second | ||
val connection = mock[Connection] | ||
val database = new Database { | ||
def open() = { | ||
latch.await(100.seconds.inMillis, TimeUnit.MILLISECONDS) | ||
connection | ||
} | ||
def close(connection: Connection) = () | ||
} | ||
val timingOutDatabase = new TimingOutDatabase(database, 1, timeout) | ||
|
||
"timeout" in { | ||
try { | ||
val epsilon = 100.millis | ||
var start = Time.now | ||
timingOutDatabase.open() must throwA[TimeoutException] | ||
var end = Time.now | ||
(end.inMillis - start.inMillis) must beCloseTo(timeout.inMillis, epsilon.inMillis) | ||
|
||
// subsequent failures should be instantaneous: | ||
start = Time.now | ||
timingOutDatabase.open() must throwA[TimeoutException] | ||
end = Time.now | ||
(end.inMillis - start.inMillis) must beCloseTo(0L, epsilon.inMillis) | ||
} finally { | ||
latch.countDown() | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|