001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.Mockito.doAnswer;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024import java.io.IOException;
025import java.lang.reflect.Field;
026import java.net.InetSocketAddress;
027import java.util.concurrent.ThreadPoolExecutor;
028import java.util.concurrent.TimeUnit;
029import java.util.concurrent.atomic.AtomicInteger;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseConfiguration;
033import org.apache.hadoop.hbase.monitoring.MonitoredRPCHandlerImpl;
034import org.apache.hadoop.hbase.testclassification.RPCTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.Before;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.mockito.invocation.InvocationOnMock;
041import org.mockito.stubbing.Answer;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045@Category({RPCTests.class, SmallTests.class})
046public class TestFifoRpcScheduler {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050      HBaseClassTestRule.forClass(TestFifoRpcScheduler.class);
051
052  private static final Logger LOG = LoggerFactory.getLogger(TestFifoRpcScheduler.class);
053
054  private AtomicInteger callExecutionCount;
055
056  private final RpcScheduler.Context CONTEXT = new RpcScheduler.Context() {
057    @Override
058    public InetSocketAddress getListenerAddress() {
059      return InetSocketAddress.createUnresolved("127.0.0.1", 1000);
060    }
061  };
062  private Configuration conf;
063
064  @Before
065  public void setUp() {
066    conf = HBaseConfiguration.create();
067    callExecutionCount = new AtomicInteger(0);
068  }
069
070  private ThreadPoolExecutor disableHandlers(RpcScheduler scheduler) {
071    ThreadPoolExecutor rpcExecutor=null;
072
073    try {
074      Field ExecutorField = scheduler.getClass().getDeclaredField("executor");
075      ExecutorField.setAccessible(true);
076
077      scheduler.start();
078      rpcExecutor = (ThreadPoolExecutor) ExecutorField.get(scheduler);
079
080      rpcExecutor.setMaximumPoolSize(1);
081      rpcExecutor.allowCoreThreadTimeOut(true);
082      rpcExecutor.setCorePoolSize(0);
083      rpcExecutor.setKeepAliveTime(1, TimeUnit.MICROSECONDS);
084
085      // Wait for 2 seconds, so that idle threads will die
086      Thread.sleep(2000);
087
088    } catch (NoSuchFieldException e) {
089      LOG.error("No such field exception:"+e);
090    } catch (IllegalAccessException e) {
091      LOG.error("Illegal access exception:"+e);
092    } catch (InterruptedException e) {
093      LOG.error("Interrupted exception:"+e);
094    }
095
096    return rpcExecutor;
097  }
098
099  @Test
100  public void testCallQueueInfo() throws IOException, InterruptedException {
101
102    ThreadPoolExecutor rpcExecutor;
103    RpcScheduler scheduler = new FifoRpcScheduler(
104            conf, 1);
105
106    scheduler.init(CONTEXT);
107
108    // Set number of handlers to a minimum value
109    disableHandlers(scheduler);
110
111    int totalCallMethods = 30;
112    int unableToDispatch = 0;
113
114    for (int i = totalCallMethods; i>0; i--) {
115      CallRunner task = createMockTask();
116      task.setStatus(new MonitoredRPCHandlerImpl());
117
118      if(!scheduler.dispatch(task)) {
119        unableToDispatch++;
120      }
121
122      Thread.sleep(10);
123    }
124
125
126    CallQueueInfo callQueueInfo = scheduler.getCallQueueInfo();
127    int executionCount = callExecutionCount.get();
128    int callQueueSize = 0;
129
130    for (String callQueueName:callQueueInfo.getCallQueueNames()) {
131      for (String calledMethod: callQueueInfo.getCalledMethodNames(callQueueName)) {
132        callQueueSize += callQueueInfo.getCallMethodCount(callQueueName, calledMethod);
133      }
134    }
135
136    assertEquals(totalCallMethods - unableToDispatch, callQueueSize + executionCount);
137
138    scheduler.stop();
139  }
140
141  private CallRunner createMockTask() {
142    ServerCall call = mock(ServerCall.class);
143    CallRunner task = mock(CallRunner.class);
144    when(task.getRpcCall()).thenReturn(call);
145
146    doAnswer(new Answer<Void>() {
147      @Override public Void answer (InvocationOnMock invocation) throws Throwable {
148        callExecutionCount.incrementAndGet();
149        Thread.sleep(1000);
150        return null;
151      }
152    }).when(task).run();
153
154    return task;
155  }
156
157}