@Test(expected = ResourceNotAvailableException.class) public void cookiePoolFullTest() { transactionManager.doInTransaction(() -> { for (long i = MIN_COOKIE; i <= MAX_COOKIE + 1; i++) { assertTrue(cookiePool.allocate(format("flow_%d", i)) > 0); } }); }
@TransactionRequired public long allocate(String flowId) { if (nextCookie > 0) { if (nextCookie <= maxCookie && !flowCookieRepository.exists(nextCookie)) { addCookie(flowId, nextCookie); return nextCookie++; } else { nextCookie = 0; } } if (nextCookie == 0) { long numOfPools = (maxCookie - minCookie) / poolSize; if (numOfPools > 1) { long poolToTake = Math.abs(new Random().nextInt()) % numOfPools; Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie( minCookie + poolToTake * poolSize, minCookie + (poolToTake + 1) * poolSize - 1); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } nextCookie = -1; } if (nextCookie == -1) { Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie(minCookie, maxCookie); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } throw new ResourceNotAvailableException("No cookie available"); }
CookiePool { @TransactionRequired public long allocate(String flowId) { if (nextCookie > 0) { if (nextCookie <= maxCookie && !flowCookieRepository.exists(nextCookie)) { addCookie(flowId, nextCookie); return nextCookie++; } else { nextCookie = 0; } } if (nextCookie == 0) { long numOfPools = (maxCookie - minCookie) / poolSize; if (numOfPools > 1) { long poolToTake = Math.abs(new Random().nextInt()) % numOfPools; Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie( minCookie + poolToTake * poolSize, minCookie + (poolToTake + 1) * poolSize - 1); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } nextCookie = -1; } if (nextCookie == -1) { Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie(minCookie, maxCookie); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } throw new ResourceNotAvailableException("No cookie available"); } }
CookiePool { @TransactionRequired public long allocate(String flowId) { if (nextCookie > 0) { if (nextCookie <= maxCookie && !flowCookieRepository.exists(nextCookie)) { addCookie(flowId, nextCookie); return nextCookie++; } else { nextCookie = 0; } } if (nextCookie == 0) { long numOfPools = (maxCookie - minCookie) / poolSize; if (numOfPools > 1) { long poolToTake = Math.abs(new Random().nextInt()) % numOfPools; Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie( minCookie + poolToTake * poolSize, minCookie + (poolToTake + 1) * poolSize - 1); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } nextCookie = -1; } if (nextCookie == -1) { Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie(minCookie, maxCookie); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } throw new ResourceNotAvailableException("No cookie available"); } CookiePool(PersistenceManager persistenceManager, long minCookie, long maxCookie, int poolSize); }
CookiePool { @TransactionRequired public long allocate(String flowId) { if (nextCookie > 0) { if (nextCookie <= maxCookie && !flowCookieRepository.exists(nextCookie)) { addCookie(flowId, nextCookie); return nextCookie++; } else { nextCookie = 0; } } if (nextCookie == 0) { long numOfPools = (maxCookie - minCookie) / poolSize; if (numOfPools > 1) { long poolToTake = Math.abs(new Random().nextInt()) % numOfPools; Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie( minCookie + poolToTake * poolSize, minCookie + (poolToTake + 1) * poolSize - 1); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } nextCookie = -1; } if (nextCookie == -1) { Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie(minCookie, maxCookie); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } throw new ResourceNotAvailableException("No cookie available"); } CookiePool(PersistenceManager persistenceManager, long minCookie, long maxCookie, int poolSize); @TransactionRequired long allocate(String flowId); void deallocate(long unmaskedCookie); }
CookiePool { @TransactionRequired public long allocate(String flowId) { if (nextCookie > 0) { if (nextCookie <= maxCookie && !flowCookieRepository.exists(nextCookie)) { addCookie(flowId, nextCookie); return nextCookie++; } else { nextCookie = 0; } } if (nextCookie == 0) { long numOfPools = (maxCookie - minCookie) / poolSize; if (numOfPools > 1) { long poolToTake = Math.abs(new Random().nextInt()) % numOfPools; Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie( minCookie + poolToTake * poolSize, minCookie + (poolToTake + 1) * poolSize - 1); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } nextCookie = -1; } if (nextCookie == -1) { Optional<Long> availableCookie = flowCookieRepository.findFirstUnassignedCookie(minCookie, maxCookie); if (availableCookie.isPresent()) { nextCookie = availableCookie.get(); addCookie(flowId, nextCookie); return nextCookie++; } } throw new ResourceNotAvailableException("No cookie available"); } CookiePool(PersistenceManager persistenceManager, long minCookie, long maxCookie, int poolSize); @TransactionRequired long allocate(String flowId); void deallocate(long unmaskedCookie);  }