View Javadoc

1   /*
2    * Copyright 2010 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.rest;
22  
23  import java.io.IOException;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.concurrent.atomic.AtomicBoolean;
28  
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.HBaseConfiguration;
31  import org.apache.hadoop.hbase.HColumnDescriptor;
32  import org.apache.hadoop.hbase.client.HTableInterface;
33  import org.apache.hadoop.hbase.client.HTablePool;
34  import org.apache.hadoop.hbase.rest.metrics.RESTMetrics;
35  
36  /**
37   * Singleton class encapsulating global REST servlet state and functions.
38   */
39  public class RESTServlet implements Constants {
40  
41    private static RESTServlet instance;
42  
43    Configuration conf;
44    HTablePool pool;
45    AtomicBoolean stopping = new AtomicBoolean(false);
46    Map<String,Integer> maxAgeMap = 
47      Collections.synchronizedMap(new HashMap<String,Integer>());
48    RESTMetrics metrics = new RESTMetrics();
49  
50    /**
51     * @return the RESTServlet singleton instance
52     * @throws IOException
53     */
54    public synchronized static RESTServlet getInstance() throws IOException {
55      if (instance == null) {
56        instance = new RESTServlet();
57      }
58      return instance;
59    }
60  
61    /**
62     * Constructor
63     * @throws IOException
64     */
65    public RESTServlet() throws IOException {
66      this.conf = HBaseConfiguration.create();
67      this.pool = new HTablePool(conf, 10);
68    }
69  
70    HTablePool getTablePool() {
71      return pool;
72    }
73  
74    Configuration getConfiguration() {
75      return conf;
76    }
77  
78    RESTMetrics getMetrics() {
79      return metrics;
80    }
81  
82    /**
83     * @param tableName the table name
84     * @return the maximum cache age suitable for use with this table, in
85     *  seconds 
86     * @throws IOException
87     */
88    public int getMaxAge(String tableName) throws IOException {
89      Integer i = maxAgeMap.get(tableName);
90      if (i != null) {
91        return i.intValue();
92      }
93      HTableInterface table = pool.getTable(tableName);
94      try {
95        int maxAge = DEFAULT_MAX_AGE;
96        for (HColumnDescriptor family : 
97            table.getTableDescriptor().getFamilies()) {
98          int ttl = family.getTimeToLive();
99          if (ttl < 0) {
100           continue;
101         }
102         if (ttl < maxAge) {
103           maxAge = ttl;
104         }
105       }
106       maxAgeMap.put(tableName, maxAge);
107       return maxAge;
108     } finally {
109       pool.putTable(table);
110     }
111   }
112 
113   /**
114    * Signal that a previously calculated maximum cache age has been
115    * invalidated by a schema change.
116    * @param tableName the table name
117    */
118   public void invalidateMaxAge(String tableName) {
119     maxAgeMap.remove(tableName);
120   }
121 }