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 }
127 tableName = splits[splits.length - 4];
128 return;
129 }
130 }
131
132
133 cfName = SchemaMetrics.UNKNOWN;
134 tableName = SchemaMetrics.UNKNOWN;
135 }
136
137
138
139
140
141
142 public SchemaConfigured(Path path) {
143 this(null, path);
144 }
145
146
147
148
149
150
151 public SchemaConfigured(Configuration conf, String tableName, String cfName)
152 {
153 this(conf);
154 this.tableName = tableName;
155 this.cfName = cfName;
156 }
157
158 public SchemaConfigured(SchemaAware that) {
159 tableName = that.getTableName();
160 cfName = that.getColumnFamilyName();
161 schemaMetrics = that.getSchemaMetrics();
162 }
163
164 @Override
165 public String getTableName() {
166 return tableName;
167 }
168
169 @Override
170 public String getColumnFamilyName() {
171 return cfName;
172 }
173
174 @Override
175 public SchemaMetrics getSchemaMetrics() {
176 if (schemaMetrics == null) {
177 if (tableName == null || cfName == null) {
178 throw new IllegalStateException("Schema metrics requested before " +
179 "table/CF name initialization: " + schemaConfAsJSON());
180 }
181 schemaMetrics = SchemaMetrics.getInstance(tableName, cfName);
182 }
183 return schemaMetrics;
184 }
185
186
187
188
189
190
191
192 public void passSchemaMetricsTo(SchemaConfigured target) {
193 if (isNull()) {
194 resetSchemaMetricsConf(target);
195 return;
196 }
197
198 if (!isSchemaConfigured()) {
199
200 throw new IllegalStateException("Table name/CF not initialized: " +
201 schemaConfAsJSON());
202 }
203
204 if (conflictingWith(target)) {
205
206 throw new IllegalArgumentException("Trying to change table name to \"" +
207 tableName + "\", CF name to \"" + cfName + "\" from " +
208 target.schemaConfAsJSON());
209 }
210
211 target.tableName = tableName;
212 target.cfName = cfName;
213 target.schemaMetrics = schemaMetrics;
214 target.schemaConfigurationChanged();
215 }
216
217
218
219
220
221
222
223
224 public static void resetSchemaMetricsConf(SchemaConfigured target) {
225 target.tableName = null;
226 target.cfName = null;
227 target.schemaMetrics = null;
228 target.schemaConfigurationChanged();
229 }
230
231 @Override
232 public long heapSize() {
233 return SCHEMA_CONFIGURED_ALIGNED_HEAP_SIZE;
234 }
235
236 public String schemaConfAsJSON() {
237 return "{\"tableName\":\"" + tableName + "\",\"cfName\":\"" + cfName
238 + "\"}";
239 }
240
241 protected boolean isSchemaConfigured() {
242 return tableName != null && cfName != null;
243 }
244
245 private boolean isNull() {
246 return tableName == null && cfName == null && schemaMetrics == null;
247 }
248
249
250
251
252
253
254
255 boolean conflictingWith(SchemaConfigured other) {
256 return (other.tableName != null && !tableName.equals(other.tableName)) ||
257 (other.cfName != null && !cfName.equals(other.cfName));
258 }
259
260
261
262
263
264 protected void schemaConfigurationChanged() {
265 }
266
267 }