-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
twitter-server: Add SecurityManager check to ContentionHandler
Problem Attempting to access the `ManagementFactory.getThreadMXBean` to enable contention monitoring can throw a `SecurityException` if the `SecurityManager` does not allow for that permission. Solution Ensure that the `ContentionHandler` checks the `SecurityManager` before attempting to instantiate a `ContentionSnapshot`. JIRA Issues: CSL-10056 Differential Revision: https://phabricator.twitter.biz/D531873
- Loading branch information
Showing
3 changed files
with
118 additions
and
14 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
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
72 changes: 72 additions & 0 deletions
72
server/src/test/scala/com/twitter/server/handler/ContentionHandlerTest.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,72 @@ | ||
package com.twitter.server.handler | ||
|
||
import com.twitter.conversions.DurationOps._ | ||
import com.twitter.finagle.http.{Request, Status} | ||
import com.twitter.util.{Await, Awaitable, Promise} | ||
import java.util.concurrent.locks.ReentrantLock | ||
import org.scalatest.concurrent.Eventually | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.time.{Millis, Seconds, Span} | ||
|
||
object ContentionHandlerTest { | ||
class Philosopher { | ||
val ready = new Promise[Unit] | ||
private val lock = new ReentrantLock() | ||
def dine(neighbor: Philosopher): Unit = { | ||
lock.lockInterruptibly() | ||
ready.setValue(()) | ||
Await.ready(neighbor.ready) | ||
neighbor.dine(this) | ||
lock.unlock() | ||
} | ||
} | ||
} | ||
|
||
class ContentionHandlerTest extends AnyFunSuite with Eventually { | ||
import ContentionHandlerTest._ | ||
|
||
implicit override val patienceConfig: PatienceConfig = { | ||
PatienceConfig(timeout = scaled(Span(15, Seconds)), interval = scaled(Span(5, Millis))) | ||
} | ||
|
||
test("ContentionHandler#instantiation") { | ||
new ContentionHandler | ||
} | ||
|
||
test("ContentionHandler#detect deadlocks") { | ||
val handler = new ContentionHandler | ||
|
||
val descartes = new Philosopher() | ||
val plato = new Philosopher() | ||
|
||
val d = new Thread(new Runnable() { | ||
def run(): Unit = { descartes.dine(plato) } | ||
}) | ||
d.start() | ||
|
||
val p = new Thread(new Runnable() { | ||
def run(): Unit = { plato.dine(descartes) } | ||
}) | ||
p.start() | ||
Await.all(descartes.ready, plato.ready) | ||
|
||
eventually { | ||
assertResponseMessage(handler, "DEADLOCKS:") | ||
} | ||
d.interrupt() | ||
p.interrupt() | ||
p.join() | ||
d.join() | ||
assertResponseMessage(handler, "") | ||
} | ||
|
||
private[this] def await[T](a: Awaitable[T]): T = Await.result(a, 2.seconds) | ||
|
||
private[this] def assertResponseMessage(handler: ContentionHandler, message: String): Unit = { | ||
val request = Request("/") | ||
val response = await(handler.apply(request)) | ||
assert(response.status == Status.Ok) | ||
val responseBody = response.contentString | ||
assert(responseBody.startsWith(message)) | ||
} | ||
} |