1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.rest;
20
21 import java.io.IOException;
22 import java.security.PrivilegedAction;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.client.HBaseAdmin;
27 import org.apache.hadoop.hbase.client.HConnectionManager;
28 import org.apache.hadoop.hbase.client.HConnectionWrapper;
29 import org.apache.hadoop.hbase.client.HTableInterface;
30 import org.apache.hadoop.hbase.client.HTablePool;
31 import org.apache.hadoop.security.UserGroupInformation;
32
33
34
35
36 @InterfaceAudience.Private
37 public class RESTServlet implements Constants {
38 private static RESTServlet INSTANCE;
39 private final Configuration conf;
40 private final HTablePool pool;
41 private final MetricsREST metrics = new MetricsREST();
42 private final HBaseAdmin admin;
43 private final UserGroupInformation ugi;
44
45
46
47
48
49 public synchronized static RESTServlet getInstance() throws IOException {
50 assert(INSTANCE != null);
51 return INSTANCE;
52 }
53
54
55
56
57
58
59 public synchronized static RESTServlet getInstance(Configuration conf)
60 throws IOException {
61 return getInstance(conf, null);
62 }
63
64 public synchronized static RESTServlet getInstance(Configuration conf,
65 UserGroupInformation ugi) throws IOException {
66 if (INSTANCE == null) {
67 INSTANCE = new RESTServlet(conf, ugi);
68 }
69 return INSTANCE;
70 }
71
72 public synchronized static void stop() {
73 if (INSTANCE != null) INSTANCE = null;
74 }
75
76
77
78
79
80
81 RESTServlet(final Configuration conf,
82 final UserGroupInformation ugi) throws IOException {
83 this.conf = conf;
84 this.ugi = ugi;
85 int maxSize = conf.getInt("hbase.rest.htablepool.size", 10);
86 if (ugi == null) {
87 pool = new HTablePool(conf, maxSize);
88 admin = new HBaseAdmin(conf);
89 } else {
90 admin = new HBaseAdmin(new HConnectionWrapper(ugi,
91 HConnectionManager.getConnection(new Configuration(conf))));
92
93 pool = new HTablePool(conf, maxSize) {
94
95
96
97
98
99
100 @Override
101 protected HTableInterface createHTable(final String tableName) {
102 return ugi.doAs(new PrivilegedAction<HTableInterface>() {
103 @Override
104 public HTableInterface run() {
105 return callCreateHTable(tableName);
106 }
107 });
108
109 }
110
111
112
113
114 HTableInterface callCreateHTable(final String tableName) {
115 return super.createHTable(tableName);
116 }
117 };
118 }
119 }
120
121 HBaseAdmin getAdmin() {
122 return admin;
123 }
124
125 HTablePool getTablePool() {
126 return pool;
127 }
128
129 Configuration getConfiguration() {
130 return conf;
131 }
132
133 MetricsREST getMetrics() {
134 return metrics;
135 }
136
137
138
139
140
141
142 boolean isReadOnly() {
143 return getConfiguration().getBoolean("hbase.rest.readonly", false);
144 }
145
146 UserGroupInformation getUser() {
147 return ugi;
148 }
149 }