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