|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.samza.clustermanager; |
| 20 | + |
| 21 | +import org.apache.samza.config.Config; |
| 22 | + |
| 23 | +import java.lang.reflect.Field; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.concurrent.Semaphore; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +public class MockHostAwareContainerAllocator extends HostAwareContainerAllocator { |
| 29 | + private static final int ALLOCATOR_TIMEOUT_MS = 10000; |
| 30 | + private Semaphore semaphore = new Semaphore(0); |
| 31 | + |
| 32 | + public MockHostAwareContainerAllocator(ClusterResourceManager manager, |
| 33 | + Config config, SamzaApplicationState state) { |
| 34 | + super(manager, ALLOCATOR_TIMEOUT_MS, config, state); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Causes the current thread to block until the expected number of containers have started. |
| 39 | + * |
| 40 | + * @param numExpectedContainers the number of containers expected to start |
| 41 | + * @param timeout the maximum time to wait |
| 42 | + * @param unit the time unit of the {@code timeout} argument |
| 43 | + * |
| 44 | + * @return a boolean that specifies whether containers started within the timeout. |
| 45 | + * @throws InterruptedException if the current thread is interrupted while waiting |
| 46 | + */ |
| 47 | + boolean awaitContainersStart(int numExpectedContainers, long timeout, TimeUnit unit) throws InterruptedException { |
| 48 | + return semaphore.tryAcquire(numExpectedContainers, timeout, unit); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void requestResources(Map<String, String> containerToHostMappings) { |
| 53 | + super.requestResources(containerToHostMappings); |
| 54 | + } |
| 55 | + |
| 56 | + public ResourceRequestState getContainerRequestState() throws Exception { |
| 57 | + Field field = AbstractContainerAllocator.class.getDeclaredField("resourceRequestState"); |
| 58 | + field.setAccessible(true); |
| 59 | + |
| 60 | + return (ResourceRequestState) field.get(this); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void runStreamProcessor(SamzaResourceRequest request, String preferredHost) { |
| 65 | + super.runStreamProcessor(request, preferredHost); |
| 66 | + semaphore.release(); |
| 67 | + } |
| 68 | +} |
0 commit comments