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.codec.prefixtree.decode;
20  
21  import java.nio.ByteBuffer;
22  import java.util.Queue;
23  import java.util.concurrent.LinkedBlockingQueue;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  
27  /**
28   * Pools PrefixTreeArraySearcher objects. Each Searcher can consist of hundreds or thousands of
29   * objects and 1 is needed for each HFile during a Get operation. With tens of thousands of
30   * Gets/second, reusing these searchers may save a lot of young gen collections.
31   * <p/>
32   * Alternative implementation would be a ByteBufferSearcherPool (not implemented yet).
33   */
34  @InterfaceAudience.Private
35  public class ArraySearcherPool {
36  
37    /**
38     * One decoder is needed for each storefile for each Get operation so we may need hundreds at the
39     * same time, however, decoding is a CPU bound activity so should limit this to something in the
40     * realm of maximum reasonable active threads.
41     */
42    private static final Integer MAX_POOL_SIZE = 1000;
43  
44    protected Queue<PrefixTreeArraySearcher> pool
45      = new LinkedBlockingQueue<PrefixTreeArraySearcher>(MAX_POOL_SIZE);
46  
47    public PrefixTreeArraySearcher checkOut(ByteBuffer buffer, boolean includesMvccVersion) {
48      PrefixTreeArraySearcher searcher = pool.poll();//will return null if pool is empty
49      searcher = DecoderFactory.ensureArraySearcherValid(buffer, searcher, includesMvccVersion);
50      return searcher;
51    }
52  
53    public void checkIn(PrefixTreeArraySearcher searcher) {
54      searcher.releaseBlockReference();
55      pool.offer(searcher);
56    }
57  
58    @Override
59    public String toString() {
60      return ("poolSize:" + pool.size());
61    }
62  
63  }