View Javadoc

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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.procedure2;
20  
21  import java.io.IOException;
22  import java.util.concurrent.CountDownLatch;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
28  import org.apache.hadoop.hbase.procedure2.store.NoopProcedureStore;
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.hbase.testclassification.MasterTests;
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  import static org.junit.Assert.assertFalse;
37  import static org.junit.Assert.assertTrue;
38  
39  @Category({MasterTests.class, SmallTests.class})
40  public class TestProcedureInMemoryChore {
41    private static final Log LOG = LogFactory.getLog(TestProcedureInMemoryChore.class);
42  
43    private static final int PROCEDURE_EXECUTOR_SLOTS = 1;
44  
45    private TestProcEnv procEnv;
46    private NoopProcedureStore procStore;
47    private ProcedureExecutor<TestProcEnv> procExecutor;
48  
49    private HBaseCommonTestingUtility htu;
50  
51    @SuppressWarnings("rawtypes")
52    @Before
53    public void setUp() throws IOException {
54      htu = new HBaseCommonTestingUtility();
55  
56      procEnv = new TestProcEnv();
57      procStore = new NoopProcedureStore();
58      procExecutor = new ProcedureExecutor(htu.getConfiguration(), procEnv, procStore);
59      procExecutor.testing = new ProcedureExecutor.Testing();
60      procStore.start(PROCEDURE_EXECUTOR_SLOTS);
61      procExecutor.start(PROCEDURE_EXECUTOR_SLOTS, true);
62    }
63  
64    @After
65    public void tearDown() throws IOException {
66      procExecutor.stop();
67      procStore.stop(false);
68    }
69  
70    @Test
71    public void testChoreAddAndRemove() throws Exception {
72      final int timeoutMSec = 50;
73      final int nCountDown = 5;
74  
75      // submit the chore and wait for execution
76      CountDownLatch latch = new CountDownLatch(nCountDown);
77      TestLatchChore chore = new TestLatchChore(timeoutMSec, latch);
78      procExecutor.addChore(chore);
79      assertTrue(chore.isRunnable());
80      latch.await();
81  
82      // remove the chore and verify it is no longer executed
83      assertTrue(chore.isRunnable());
84      procExecutor.removeChore(chore);
85      latch = new CountDownLatch(nCountDown);
86      chore.setLatch(latch);
87      latch.await(timeoutMSec * nCountDown, TimeUnit.MILLISECONDS);
88      LOG.info("chore latch count=" + latch.getCount());
89      assertFalse(chore.isRunnable());
90      assertTrue("latchCount=" + latch.getCount(), latch.getCount() > 0);
91    }
92  
93    public static class TestLatchChore extends ProcedureInMemoryChore<TestProcEnv> {
94      private CountDownLatch latch;
95  
96      public TestLatchChore(final int timeoutMSec, final CountDownLatch latch) {
97        super(timeoutMSec);
98        setLatch(latch);
99      }
100 
101     public void setLatch(final CountDownLatch latch) {
102       this.latch = latch;
103     }
104 
105     @Override
106     protected void periodicExecute(final TestProcEnv env) {
107       latch.countDown();
108     }
109   }
110 
111   private static class TestProcEnv {
112   }
113 }