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.apache.hadoop.hbase.ipc.RWQueueRpcExecutor.CALL_QUEUE_READ_SHARE_CONF_KEY;
021import static org.apache.hadoop.hbase.ipc.RWQueueRpcExecutor.CALL_QUEUE_SCAN_SHARE_CONF_KEY;
022import static org.apache.hadoop.hbase.ipc.RpcExecutor.CALL_QUEUE_HANDLER_FACTOR_CONF_KEY;
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertTrue;
025import static org.mockito.Mockito.*;
026
027import java.util.List;
028import java.util.concurrent.BlockingQueue;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseConfiguration;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.RPCTests;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Rule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.junit.rules.TestName;
040
041@Category({ RPCTests.class, MediumTests.class })
042public class TestRWQueueRpcExecutor {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestRWQueueRpcExecutor.class);
047
048  @Rule
049  public TestName testName = new TestName();
050
051  private Configuration conf;
052
053  @Before
054  public void setUp() {
055    conf = HBaseConfiguration.create();
056    conf.setFloat(CALL_QUEUE_HANDLER_FACTOR_CONF_KEY, 1.0f);
057    conf.setFloat(CALL_QUEUE_SCAN_SHARE_CONF_KEY, 0.5f);
058    conf.setFloat(CALL_QUEUE_READ_SHARE_CONF_KEY, 0.5f);
059  }
060
061  @Test
062  public void itProvidesCorrectQueuesToBalancers() throws InterruptedException {
063    PriorityFunction qosFunction = mock(PriorityFunction.class);
064    RWQueueRpcExecutor executor =
065      new RWQueueRpcExecutor(testName.getMethodName(), 100, 100, qosFunction, conf, null);
066
067    QueueBalancer readBalancer = executor.getReadBalancer();
068    QueueBalancer writeBalancer = executor.getWriteBalancer();
069    QueueBalancer scanBalancer = executor.getScanBalancer();
070
071    assertTrue(readBalancer instanceof RandomQueueBalancer);
072    assertTrue(writeBalancer instanceof RandomQueueBalancer);
073    assertTrue(scanBalancer instanceof RandomQueueBalancer);
074
075    List<BlockingQueue<CallRunner>> readQueues = ((RandomQueueBalancer) readBalancer).getQueues();
076    List<BlockingQueue<CallRunner>> writeQueues = ((RandomQueueBalancer) writeBalancer).getQueues();
077    List<BlockingQueue<CallRunner>> scanQueues = ((RandomQueueBalancer) scanBalancer).getQueues();
078
079    assertEquals(25, readQueues.size());
080    assertEquals(50, writeQueues.size());
081    assertEquals(25, scanQueues.size());
082
083    verifyDistinct(readQueues, writeQueues, scanQueues);
084    verifyDistinct(writeQueues, readQueues, scanQueues);
085    verifyDistinct(scanQueues, readQueues, writeQueues);
086
087  }
088
089  private void verifyDistinct(List<BlockingQueue<CallRunner>> queues,
090    List<BlockingQueue<CallRunner>>... others) throws InterruptedException {
091    CallRunner mock = mock(CallRunner.class);
092    for (BlockingQueue<CallRunner> queue : queues) {
093      queue.put(mock);
094      assertEquals(1, queue.size());
095    }
096
097    for (List<BlockingQueue<CallRunner>> other : others) {
098      for (BlockingQueue<CallRunner> queue : other) {
099        assertEquals(0, queue.size());
100      }
101    }
102
103    // clear them for next test
104    for (BlockingQueue<CallRunner> queue : queues) {
105      queue.clear();
106    }
107  }
108}