@Override public <T extends Request<T>> RequestAllocator<T> createRequestAllocator(RequestAllocatorConfig<T> configuration) { return new PreOrderAllocator<>(configuration); } }
@Override protected Iterator<T> getJoinIterator(Iterator<? extends Requestor<T>> requestors, final ConcurrentBoundedPriorityIterable<T> requestIterable) { List<Iterator<T>> iteratorList = Lists.newArrayList(); while (requestors.hasNext()) { Requestor<T> requestor = requestors.next(); if (!(requestor instanceof PushDownRequestor)) { throw new RuntimeException(String.format("%s can only be used with %s.", PreOrderAllocator.class, PushDownRequestor.class)); } try { iteratorList.add(((PushDownRequestor<T>) requestor).getRequests(getConfiguration().getPrioritizer())); } catch (IOException ioe) { log.error("Failed to get requests from " + requestor); } } PriorityMultiIterator<T> multiIterator = new PriorityMultiIterator<>(iteratorList, getConfiguration().getPrioritizer()); return new InterruptibleIterator<>(multiIterator, new Callable<Boolean>() { @Override public Boolean call() throws Exception { return requestIterable.isFull(); } }); } }
@Test public void testAllocateRequests() throws Exception { StringRequest.StringRequestEstimator estimator = new StringRequest.StringRequestEstimator(); RequestAllocatorConfig<StringRequest> configuration = RequestAllocatorConfig.builder(estimator).withPrioritizer(new StringRequest.StringRequestComparator()).build(); PreOrderAllocator<StringRequest> allocator = new PreOrderAllocator<>(configuration); ResourcePool pool = ResourcePool.builder().maxResource(StringRequest.MEMORY, 100.).build(); List<Requestor<StringRequest>> requests = Lists.<Requestor<StringRequest>>newArrayList( new StringRequestor("r1", "a-50", "f-50", "k-20"), new StringRequestor("r2", "j-10", "b-20", "e-20"), new StringRequestor("r3", "g-20", "c-200", "d-30")); AllocatedRequestsIterator<StringRequest> result = allocator.allocateRequests(requests.iterator(), pool); List<StringRequest> resultList = Lists.newArrayList(result); Assert.assertEquals(resultList.size(), 3); Assert.assertEquals(resultList.get(0).getString(), "a-50"); Assert.assertEquals(resultList.get(1).getString(), "b-20"); // No c because it is too large to fit Assert.assertEquals(resultList.get(2).getString(), "d-30"); Assert.assertTrue(estimator.getQueriedRequests().contains("a-50")); Assert.assertTrue(estimator.getQueriedRequests().contains("b-20")); Assert.assertTrue(estimator.getQueriedRequests().contains("c-200")); Assert.assertTrue(estimator.getQueriedRequests().contains("d-30")); Assert.assertFalse(estimator.getQueriedRequests().contains("e-20")); Assert.assertFalse(estimator.getQueriedRequests().contains("f-50")); }
@Override public <T extends Request<T>> RequestAllocator<T> createRequestAllocator(RequestAllocatorConfig<T> configuration) { return new PreOrderAllocator<>(configuration); } }
@Override protected Iterator<T> getJoinIterator(Iterator<? extends Requestor<T>> requestors, final ConcurrentBoundedPriorityIterable<T> requestIterable) { List<Iterator<T>> iteratorList = Lists.newArrayList(); while (requestors.hasNext()) { Requestor<T> requestor = requestors.next(); if (!(requestor instanceof PushDownRequestor)) { throw new RuntimeException(String.format("%s can only be used with %s.", PreOrderAllocator.class, PushDownRequestor.class)); } try { iteratorList.add(((PushDownRequestor<T>) requestor).getRequests(getConfiguration().getPrioritizer())); } catch (IOException ioe) { log.error("Failed to get requests from " + requestor); } } PriorityMultiIterator<T> multiIterator = new PriorityMultiIterator<>(iteratorList, getConfiguration().getPrioritizer()); return new InterruptibleIterator<>(multiIterator, new Callable<Boolean>() { @Override public Boolean call() throws Exception { return requestIterable.isFull(); } }); } }