1
2
3
4
5
6
7
8
9
10
11 package org.apache.hadoop.hbase.namespace;
12
13 import java.io.IOException;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.hadoop.hbase.HBaseIOException;
18 import org.apache.hadoop.hbase.HRegionInfo;
19 import org.apache.hadoop.hbase.MetaTableAccessor;
20 import org.apache.hadoop.hbase.NamespaceDescriptor;
21 import org.apache.hadoop.hbase.TableExistsException;
22 import org.apache.hadoop.hbase.TableName;
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.master.MasterServices;
25 import org.apache.hadoop.hbase.quotas.QuotaExceededException;
26
27
28
29
30
31
32 @InterfaceAudience.Private
33 public class NamespaceAuditor {
34 private static final Log LOG = LogFactory.getLog(NamespaceAuditor.class);
35 static final String NS_AUDITOR_INIT_TIMEOUT = "hbase.namespace.auditor.init.timeout";
36 static final int DEFAULT_NS_AUDITOR_INIT_TIMEOUT = 120000;
37 private NamespaceStateManager stateManager;
38 private MasterServices masterServices;
39
40 public NamespaceAuditor(MasterServices masterServices) {
41 this.masterServices = masterServices;
42 stateManager = new NamespaceStateManager(masterServices, masterServices.getZooKeeper());
43 }
44
45 public void start() throws IOException {
46 stateManager.start();
47 LOG.info("NamespaceAuditor started.");
48 }
49
50
51
52
53
54
55
56
57
58 public void checkQuotaToCreateTable(TableName tName, int regions) throws IOException {
59 if (stateManager.isInitialized()) {
60
61 if (MetaTableAccessor.tableExists(this.masterServices.getConnection(), tName)) {
62 throw new TableExistsException(tName);
63 }
64 stateManager.checkAndUpdateNamespaceTableCount(tName, regions);
65 } else {
66 checkTableTypeAndThrowException(tName);
67 }
68 }
69
70
71
72
73
74
75
76 public void checkQuotaToUpdateRegion(TableName tName, int regions) throws IOException {
77 if (stateManager.isInitialized()) {
78 stateManager.checkAndUpdateNamespaceRegionCount(tName, regions);
79 } else {
80 checkTableTypeAndThrowException(tName);
81 }
82 }
83
84 private void checkTableTypeAndThrowException(TableName name) throws IOException {
85 if (name.isSystemTable()) {
86 LOG.debug("Namespace auditor checks not performed for table " + name.getNameAsString());
87 } else {
88 throw new HBaseIOException(name
89 + " is being created even before namespace auditor has been initialized.");
90 }
91 }
92
93 public void checkQuotaToSplitRegion(HRegionInfo hri) throws IOException {
94 if (!stateManager.isInitialized()) {
95 throw new IOException(
96 "Split operation is being performed even before namespace auditor is initialized.");
97 } else if (!stateManager.checkAndUpdateNamespaceRegionCount(hri.getTable(),
98 hri.getRegionName(), 1)) {
99 throw new QuotaExceededException("Region split not possible for :" + hri.getEncodedName()
100 + " as quota limits are exceeded ");
101 }
102 }
103
104 public void updateQuotaForRegionMerge(HRegionInfo hri) throws IOException {
105 if (!stateManager.isInitialized()) {
106 throw new IOException(
107 "Merge operation is being performed even before namespace auditor is initialized.");
108 } else if (!stateManager
109 .checkAndUpdateNamespaceRegionCount(hri.getTable(), hri.getRegionName(), -1)) {
110 throw new QuotaExceededException("Region split not possible for :" + hri.getEncodedName()
111 + " as quota limits are exceeded ");
112 }
113 }
114
115 public void addNamespace(NamespaceDescriptor ns) throws IOException {
116 stateManager.addNamespace(ns.getName());
117 }
118
119 public void deleteNamespace(String namespace) throws IOException {
120 stateManager.deleteNamespace(namespace);
121 }
122
123 public void removeFromNamespaceUsage(TableName tableName) throws IOException {
124 stateManager.removeTable(tableName);
125 }
126
127 public void removeRegionFromNamespaceUsage(HRegionInfo hri) throws IOException {
128 stateManager.removeRegionFromTable(hri);
129 }
130
131
132
133
134
135 public NamespaceTableAndRegionInfo getState(String namespace) {
136 if (stateManager.isInitialized()) {
137 return stateManager.getState(namespace);
138 }
139 return null;
140 }
141
142
143
144
145
146 public boolean isInitialized() {
147 return stateManager.isInitialized();
148 }
149 }