View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.client;
22  
23  import org.apache.hadoop.hbase.hbql.impl.QueryExecutorPoolImpl;
24  import org.apache.hadoop.hbase.hbql.impl.Utils;
25  import org.apache.hadoop.hbase.hbql.util.Maps;
26  
27  import java.util.Map;
28  import java.util.Set;
29  
30  public class QueryExecutorPoolManager {
31  
32      public static int     defaultMaxExecutorPoolSize = 5;
33      public static int     defaultMinThreadCount      = 1;
34      public static int     defaultMaxThreadCount      = 10;
35      public static long    defaultKeepAliveSecs       = Long.MAX_VALUE;
36      public static boolean defaultThreadsReadResults  = true;
37      public static int     defaultCompletionQueueSize = 25;
38  
39      private static Map<String, QueryExecutorPoolImpl> executorPoolMap = Maps.newConcurrentHashMap();
40  
41      private static Map<String, QueryExecutorPoolImpl> getExecutorPoolMap() {
42          return QueryExecutorPoolManager.executorPoolMap;
43      }
44  
45      public static QueryExecutorPool newQueryExecutorPool(final String poolName,
46                                                           final int maxExecutorPoolSize,
47                                                           final int minThreadCount,
48                                                           final int maxThreadCount,
49                                                           final long keepAliveSecs,
50                                                           final boolean threadsReadResults,
51                                                           final int completionQueueSize) throws HBqlException {
52  
53          if (Utils.isValidString(poolName) && getExecutorPoolMap().containsKey(poolName))
54              throw new HBqlException("QueryExecutorPool already exists: " + poolName);
55  
56          final QueryExecutorPoolImpl executorPool = new QueryExecutorPoolImpl(poolName,
57                                                                               maxExecutorPoolSize,
58                                                                               minThreadCount,
59                                                                               maxThreadCount,
60                                                                               keepAliveSecs,
61                                                                               threadsReadResults,
62                                                                               completionQueueSize);
63          QueryExecutorPoolManager.getExecutorPoolMap().put(executorPool.getName(), executorPool);
64  
65          return executorPool;
66      }
67  
68      public static boolean dropQueryExecutorPool(final String name) {
69  
70          if (Utils.isValidString(name) && getExecutorPoolMap().containsKey(name)) {
71              final QueryExecutorPoolImpl queryExecutorPool = getExecutorPoolMap().remove(name);
72              queryExecutorPool.shutdown();
73              return true;
74          }
75  
76          return false;
77      }
78  
79      public static boolean queryExecutorPoolExists(final String name) {
80          return Utils.isValidString(name) && QueryExecutorPoolManager.getExecutorPoolMap().containsKey(name);
81      }
82  
83      public static QueryExecutorPool getQueryExecutorPool(final String poolName) throws HBqlException {
84          if (!QueryExecutorPoolManager.getExecutorPoolMap().containsKey(poolName))
85              throw new HBqlException("QueryExecutorPool does not exist: " + poolName);
86  
87          return QueryExecutorPoolManager.getExecutorPoolMap().get(poolName);
88      }
89  
90      public static Set<String> getQueryExecutorPoolNames() {
91          return QueryExecutorPoolManager.getExecutorPoolMap().keySet();
92      }
93  }