Application.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package controllers;
  2. import akka.dispatch.ExecutionContexts;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.databind.node.ObjectNode;
  5. import models.World;
  6. import play.Play;
  7. import play.core.NamedThreadFactory;
  8. import play.libs.F;
  9. import play.libs.Json;
  10. import play.mvc.Controller;
  11. import play.mvc.Result;
  12. import scala.concurrent.ExecutionContext;
  13. import utils.Predicate;
  14. import utils.Predicated;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Random;
  18. import java.util.concurrent.*;
  19. public class Application extends Controller {
  20. private static final int MAX_QUERIES_PER_REQUEST = 20;
  21. private static final int TEST_DATABASE_ROWS = 10000;
  22. private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  23. private static final int partitionCount = Play.application().configuration().getInt("db.default.partitionCount");
  24. private static final int maxConnections =
  25. partitionCount * Play.application().configuration().getInt("db.default.maxConnectionsPerPartition");
  26. private static final int minConnections =
  27. partitionCount * Play.application().configuration().getInt("db.default.minConnectionsPerPartition");
  28. private static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(minConnections, maxConnections,
  29. 0L, TimeUnit.MILLISECONDS,
  30. new LinkedBlockingQueue<Runnable>(),
  31. new NamedThreadFactory("dbEc"));
  32. private static final ExecutionContext dbEc = ExecutionContexts.fromExecutorService(tpe);
  33. // A predicate for checking our ability to service database requests is determined by ensuring that the request
  34. // queue doesn't fill up beyond a certain threshold. For convenience we use the max number of connections * the max
  35. // # of db requests per web request to determine this threshold. It is a rough check as we don't know how many
  36. // queries we're going to make or what other threads are running in parallel etc. Nevertheless, the check is
  37. // adequate in order to throttle the acceptance of requests to the size of the pool.
  38. public static class IsDbAvailable implements Predicate {
  39. @Override
  40. public boolean condition() {
  41. return (tpe.getQueue().size() < maxConnections * MAX_QUERIES_PER_REQUEST);
  42. }
  43. }
  44. public static Result json() {
  45. final ObjectNode result = OBJECT_MAPPER.createObjectNode();
  46. result.put("message", "Hello World!");
  47. return ok(result);
  48. }
  49. @Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
  50. public static F.Promise<Result> db(final Integer queries) {
  51. final Random random = ThreadLocalRandom.current();
  52. final List<F.Promise<? extends World>> promises = new ArrayList<F.Promise<? extends World>>(queries);
  53. for (int i = 0; i < queries; ++i) {
  54. final F.Promise<World> p = F.Promise.promise(new F.Function0<World>() {
  55. @Override
  56. public World apply() throws Throwable {
  57. return World.findById(Long.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1));
  58. }
  59. }, dbEc);
  60. promises.add(p);
  61. }
  62. return F.Promise.sequence(promises).map(new F.Function<List<World>, Result>() {
  63. @Override
  64. public Result apply(List<World> worlds) {
  65. return ok(Json.toJson(worlds));
  66. }
  67. });
  68. }
  69. }