1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.wal;
20
21 import static org.apache.hadoop.hbase.wal.DefaultWALProvider.META_WAL_PROVIDER_ID;
22 import static org.apache.hadoop.hbase.wal.DefaultWALProvider.WAL_FILE_NAME_DELIMITER;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ConcurrentMap;
30 import java.util.concurrent.locks.Lock;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.conf.Configuration;
35 import org.apache.hadoop.hbase.classification.InterfaceAudience;
36
37 import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.apache.hadoop.hbase.util.IdReadWriteLock;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @InterfaceAudience.Private
58 public class RegionGroupingProvider implements WALProvider {
59 private static final Log LOG = LogFactory.getLog(RegionGroupingProvider.class);
60
61
62
63
64 public static interface RegionGroupingStrategy {
65
66
67
68
69 String group(final byte[] identifier, byte[] namespace);
70 void init(Configuration config, String providerId);
71 }
72
73
74
75
76 static enum Strategies {
77 defaultStrategy(BoundedGroupingStrategy.class),
78 identity(IdentityGroupingStrategy.class),
79 bounded(BoundedGroupingStrategy.class),
80 namespace(NamespaceGroupingStrategy.class);
81
82 final Class<? extends RegionGroupingStrategy> clazz;
83 Strategies(Class<? extends RegionGroupingStrategy> clazz) {
84 this.clazz = clazz;
85 }
86 }
87
88
89
90
91
92 RegionGroupingStrategy getStrategy(final Configuration conf, final String key,
93 final String defaultValue) throws IOException {
94 Class<? extends RegionGroupingStrategy> clazz;
95 try {
96 clazz = Strategies.valueOf(conf.get(key, defaultValue)).clazz;
97 } catch (IllegalArgumentException exception) {
98
99
100
101 clazz = conf.getClass(key, IdentityGroupingStrategy.class, RegionGroupingStrategy.class);
102 }
103 LOG.info("Instantiating RegionGroupingStrategy of type " + clazz);
104 try {
105 final RegionGroupingStrategy result = clazz.newInstance();
106 result.init(conf, providerId);
107 return result;
108 } catch (InstantiationException exception) {
109 LOG.error("couldn't set up region grouping strategy, check config key " +
110 REGION_GROUPING_STRATEGY);
111 LOG.debug("Exception details for failure to load region grouping strategy.", exception);
112 throw new IOException("couldn't set up region grouping strategy", exception);
113 } catch (IllegalAccessException exception) {
114 LOG.error("couldn't set up region grouping strategy, check config key " +
115 REGION_GROUPING_STRATEGY);
116 LOG.debug("Exception details for failure to load region grouping strategy.", exception);
117 throw new IOException("couldn't set up region grouping strategy", exception);
118 }
119 }
120
121 public static final String REGION_GROUPING_STRATEGY = "hbase.wal.regiongrouping.strategy";
122 public static final String DEFAULT_REGION_GROUPING_STRATEGY = Strategies.defaultStrategy.name();
123
124
125 public static final String DELEGATE_PROVIDER = "hbase.wal.regiongrouping.delegate.provider";
126 public static final String DEFAULT_DELEGATE_PROVIDER = WALFactory.Providers.defaultProvider
127 .name();
128
129 private static final String META_WAL_GROUP_NAME = "meta";
130
131
132 private final ConcurrentMap<String, WALProvider> cached = new ConcurrentHashMap<>();
133
134 private final IdReadWriteLock createLock = new IdReadWriteLock();
135
136 private RegionGroupingStrategy strategy = null;
137 private WALFactory factory = null;
138 private List<WALActionsListener> listeners = null;
139 private String providerId = null;
140 private Class<? extends WALProvider> providerClass;
141
142 @Override
143 public void init(final WALFactory factory, final Configuration conf,
144 final List<WALActionsListener> listeners, final String providerId) throws IOException {
145 if (null != strategy) {
146 throw new IllegalStateException("WALProvider.init should only be called once.");
147 }
148 this.factory = factory;
149 this.listeners = null == listeners ? null : Collections.unmodifiableList(listeners);
150 StringBuilder sb = new StringBuilder().append(factory.factoryId);
151 if (providerId != null) {
152 if (providerId.startsWith(WAL_FILE_NAME_DELIMITER)) {
153 sb.append(providerId);
154 } else {
155 sb.append(WAL_FILE_NAME_DELIMITER).append(providerId);
156 }
157 }
158 this.providerId = sb.toString();
159 this.strategy = getStrategy(conf, REGION_GROUPING_STRATEGY, DEFAULT_REGION_GROUPING_STRATEGY);
160 this.providerClass = factory.getProviderClass(DELEGATE_PROVIDER, DEFAULT_DELEGATE_PROVIDER);
161 }
162
163 private WALProvider createProvider(String group) throws IOException {
164 if (META_WAL_PROVIDER_ID.equals(providerId)) {
165 return factory.createProvider(providerClass, listeners, META_WAL_PROVIDER_ID);
166 } else {
167 return factory.createProvider(providerClass, listeners, group);
168 }
169 }
170
171 @Override
172 public List<WAL> getWALs() throws IOException {
173 List<WAL> wals = new ArrayList<WAL>();
174 for (WALProvider provider : cached.values()) {
175 wals.addAll(provider.getWALs());
176 }
177 return wals;
178 }
179
180 private WAL getWAL(final String group) throws IOException {
181 WALProvider provider = cached.get(group);
182 if (provider == null) {
183 Lock lock = createLock.getLock(group.hashCode()).writeLock();
184 lock.lock();
185 try {
186 provider = cached.get(group);
187 if (provider == null) {
188 provider = createProvider(group);
189 cached.put(group, provider);
190 }
191 } finally {
192 lock.unlock();
193 }
194 }
195 return provider.getWAL(null, null);
196 }
197
198 @Override
199 public WAL getWAL(final byte[] identifier, byte[] namespace) throws IOException {
200 final String group;
201 if (META_WAL_PROVIDER_ID.equals(this.providerId)) {
202 group = META_WAL_GROUP_NAME;
203 } else {
204 group = strategy.group(identifier, namespace);
205 }
206 return getWAL(group);
207 }
208
209 @Override
210 public void shutdown() throws IOException {
211
212 IOException failure = null;
213 for (WALProvider provider: cached.values()) {
214 try {
215 provider.shutdown();
216 } catch (IOException e) {
217 LOG.error("Problem shutting down wal provider '" + provider + "': " + e.getMessage());
218 if (LOG.isDebugEnabled()) {
219 LOG.debug("Details of problem shutting down wal provider '" + provider + "'", e);
220 }
221 failure = e;
222 }
223 }
224 if (failure != null) {
225 throw failure;
226 }
227 }
228
229 @Override
230 public void close() throws IOException {
231
232 IOException failure = null;
233 for (WALProvider provider : cached.values()) {
234 try {
235 provider.close();
236 } catch (IOException e) {
237 LOG.error("Problem closing wal provider '" + provider + "': " + e.getMessage());
238 if (LOG.isDebugEnabled()) {
239 LOG.debug("Details of problem closing wal provider '" + provider + "'", e);
240 }
241 failure = e;
242 }
243 }
244 if (failure != null) {
245 throw failure;
246 }
247 }
248
249 static class IdentityGroupingStrategy implements RegionGroupingStrategy {
250 @Override
251 public void init(Configuration config, String providerId) {}
252 @Override
253 public String group(final byte[] identifier, final byte[] namespace) {
254 return "identity-" + Bytes.toString(identifier);
255 }
256 }
257
258 @Override
259 public long getNumLogFiles() {
260 long numLogFiles = 0;
261 for (WALProvider provider : cached.values()) {
262 numLogFiles += provider.getNumLogFiles();
263 }
264 return numLogFiles;
265 }
266
267 @Override
268 public long getLogFileSize() {
269 long logFileSize = 0;
270 for (WALProvider provider : cached.values()) {
271 logFileSize += provider.getLogFileSize();
272 }
273 return logFileSize;
274 }
275 }