1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.metrics;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.fs.Path;
25 import org.apache.hadoop.hbase.io.HeapSize;
26 import org.apache.hadoop.hbase.io.hfile.HFile;
27 import org.apache.hadoop.hbase.regionserver.HRegion;
28 import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics.SchemaAware;
29 import org.apache.hadoop.hbase.util.ClassSize;
30
31
32
33
34
35
36
37
38
39
40 public class SchemaConfigured implements HeapSize, SchemaAware {
41 private static final Log LOG = LogFactory.getLog(SchemaConfigured.class);
42
43
44 private String cfName;
45 private String tableName;
46
47
48
49
50
51
52
53
54
55
56 private SchemaMetrics schemaMetrics;
57
58 static {
59 if (ClassSize.OBJECT <= 0 || ClassSize.REFERENCE <= 0) {
60 throw new AssertionError("Class sizes are not initialized");
61 }
62 }
63
64
65
66
67
68
69 public static final int SCHEMA_CONFIGURED_UNALIGNED_HEAP_SIZE =
70 ClassSize.OBJECT + 3 * ClassSize.REFERENCE;
71
72 private static final int SCHEMA_CONFIGURED_ALIGNED_HEAP_SIZE =
73 ClassSize.align(SCHEMA_CONFIGURED_UNALIGNED_HEAP_SIZE);
74
75
76 private SchemaConfigured(Configuration conf) {
77 SchemaMetrics.configureGlobally(conf);
78
79
80
81 }
82
83
84
85
86
87 public static SchemaConfigured createUnknown() {
88 return new SchemaConfigured(null, SchemaMetrics.UNKNOWN,
89 SchemaMetrics.UNKNOWN);
90 }
91
92
93
94
95
96 public SchemaConfigured() {
97 }
98
99
100
101
102
103
104
105 public SchemaConfigured(Configuration conf, Path path) {
106 this(conf);
107
108 if (path != null) {
109 String splits[] = path.toString().split("/");
110 int numPathLevels = splits.length;
111 if (numPathLevels > 0 && splits[0].isEmpty()) {
112
113 --numPathLevels;
114 }
115 if (numPathLevels < HFile.MIN_NUM_HFILE_PATH_LEVELS) {
116 LOG.warn("Could not determine table and column family of the HFile "
117 + "path " + path + ". Expecting at least "
118 + HFile.MIN_NUM_HFILE_PATH_LEVELS + " path components.");
119 path = null;
120 } else {
121 cfName = splits[splits.length - 2];
122 if (cfName.equals(HRegion.REGION_TEMP_SUBDIR)) {
123
124
125 cfName = null;
126 } else {
127 cfName = cfName.intern();
128 }
129 tableName = splits[splits.length - 4].intern();
130 return;
131 }
132 }
133
134
135 cfName = SchemaMetrics.UNKNOWN;
136 tableName = SchemaMetrics.UNKNOWN;
137 }
138
139
140
141
142
143
144 public SchemaConfigured(Path path) {
145 this(null, path);
146 }
147
148
149
150
151
152
153 public SchemaConfigured(Configuration conf, String tableName, String cfName)
154 {
155 this(conf);
156 this.tableName = tableName != null ? tableName.intern() : tableName;
157 this.cfName = cfName != null ? cfName.intern() : cfName;
158 }
159
160 public SchemaConfigured(SchemaAware that) {
161 tableName = that.getTableName().intern();
162 cfName = that.getColumnFamilyName().intern();
163 schemaMetrics = that.getSchemaMetrics();
164 }
165
166 @Override
167 public String getTableName() {
168 return tableName;
169 }
170
171 @Override
172 public String getColumnFamilyName() {
173 return cfName;
174 }
175
176 @Override
177 public SchemaMetrics getSchemaMetrics() {
178 if (schemaMetrics == null) {
179 if (tableName == null || cfName == null) {
180 throw new IllegalStateException("Schema metrics requested before " +
181 "table/CF name initialization: " + schemaConfAsJSON());
182 }
183 schemaMetrics = SchemaMetrics.getInstance(tableName, cfName);
184 }
185 return schemaMetrics;
186 }
187
188
189
190
191
192
193
194 public void passSchemaMetricsTo(SchemaConfigured target) {
195 if (isNull()) {
196 resetSchemaMetricsConf(target);
197 return;
198 }
199
200 if (!isSchemaConfigured()) {
201
202 throw new IllegalStateException("Table name/CF not initialized: " +
203 schemaConfAsJSON());
204 }
205
206 if (conflictingWith(target)) {
207
208 throw new IllegalArgumentException("Trying to change table name to \"" +
209 tableName + "\", CF name to \"" + cfName + "\" from " +
210 target.schemaConfAsJSON());
211 }
212
213 target.tableName = tableName.intern();
214 target.cfName = cfName.intern();
215 target.schemaMetrics = schemaMetrics;
216 target.schemaConfigurationChanged();
217 }
218
219
220
221
222
223
224
225
226 public static void resetSchemaMetricsConf(SchemaConfigured target) {
227 target.tableName = null;
228 target.cfName = null;
229 target.schemaMetrics = null;
230 target.schemaConfigurationChanged();
231 }
232
233 @Override
234 public long heapSize() {
235 return SCHEMA_CONFIGURED_ALIGNED_HEAP_SIZE;
236 }
237
238 public String schemaConfAsJSON() {
239 return "{\"tableName\":\"" + tableName + "\",\"cfName\":\"" + cfName
240 + "\"}";
241 }
242
243 protected boolean isSchemaConfigured() {
244 return tableName != null && cfName != null;
245 }
246
247 private boolean isNull() {
248 return tableName == null && cfName == null && schemaMetrics == null;
249 }
250
251
252
253
254
255
256
257 boolean conflictingWith(SchemaConfigured other) {
258 return (other.tableName != null && !tableName.equals(other.tableName)) ||
259 (other.cfName != null && !cfName.equals(other.cfName));
260 }
261
262
263
264
265
266 protected void schemaConfigurationChanged() {
267 }
268
269 }