1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
52
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
63
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
84
85
86
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
115
116
117
118 public void invalidateMaxAge(String tableName) {
119 maxAgeMap.remove(tableName);
120 }
121 }