1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.HashMap;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FileStatus;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.Path;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 @InterfaceAudience.Private
59 public class DynamicClassLoader extends ClassLoaderBase {
60 private static final Log LOG =
61 LogFactory.getLog(DynamicClassLoader.class);
62
63
64 private static final String DYNAMIC_JARS_DIR = File.separator
65 + "jars" + File.separator;
66
67 private static final String DYNAMIC_JARS_DIR_KEY = "hbase.dynamic.jars.dir";
68
69 private static final String DYNAMIC_JARS_OPTIONAL_CONF_KEY = "hbase.use.dynamic.jars";
70 private static final boolean DYNAMIC_JARS_OPTIONAL_DEFAULT = true;
71
72
73 private final boolean userConfigUseDynamicJars;
74
75 private final boolean useDynamicJars;
76
77 private File localDir;
78
79
80 private FileSystem remoteDirFs;
81 private Path remoteDir;
82
83
84 private HashMap<String, Long> jarModifiedTime;
85
86
87
88
89
90
91
92
93 public DynamicClassLoader(
94 final Configuration conf, final ClassLoader parent) {
95 super(parent);
96
97
98 userConfigUseDynamicJars = conf.getBoolean(
99 DYNAMIC_JARS_OPTIONAL_CONF_KEY, DYNAMIC_JARS_OPTIONAL_DEFAULT);
100
101 boolean dynamicJarsEnabled = userConfigUseDynamicJars;
102 if (dynamicJarsEnabled) {
103 try {
104 initTempDir(conf);
105 dynamicJarsEnabled = true;
106 } catch (Exception e) {
107 LOG.error("Disabling the DynamicClassLoader as it failed to initialize its temp directory."
108 + " Check your configuration and filesystem permissions. Custom coprocessor code may"
109 + " not be loaded as a result of this failure.", e);
110 dynamicJarsEnabled = false;
111 }
112 }
113 useDynamicJars = dynamicJarsEnabled;
114 }
115
116
117
118 private synchronized void initTempDir(final Configuration conf) {
119 jarModifiedTime = new HashMap<String, Long>();
120 String localDirPath = conf.get(
121 LOCAL_DIR_KEY, DEFAULT_LOCAL_DIR) + DYNAMIC_JARS_DIR;
122 localDir = new File(localDirPath);
123 if (!localDir.mkdirs() && !localDir.isDirectory()) {
124 throw new RuntimeException("Failed to create local dir " + localDir.getPath()
125 + ", DynamicClassLoader failed to init");
126 }
127
128 String remotePath = conf.get(DYNAMIC_JARS_DIR_KEY);
129 if (remotePath == null || remotePath.equals(localDirPath)) {
130 remoteDir = null;
131 } else {
132 remoteDir = new Path(remotePath);
133 try {
134 remoteDirFs = remoteDir.getFileSystem(conf);
135 } catch (IOException ioe) {
136 LOG.warn("Failed to identify the fs of dir "
137 + remoteDir + ", ignored", ioe);
138 remoteDir = null;
139 }
140 }
141 }
142
143 @Override
144 public Class<?> loadClass(String name)
145 throws ClassNotFoundException {
146 try {
147 return parent.loadClass(name);
148 } catch (ClassNotFoundException e) {
149 if (useDynamicJars) {
150 LOG.debug("Class " + name + " not found - using dynamical class loader");
151 return tryRefreshClass(name);
152 } else if (userConfigUseDynamicJars) {
153
154 LOG.debug("Not checking DynamicClassLoader for missing class because it is disabled."
155 + " See the log for previous errors.");
156 }
157 throw e;
158 }
159 }
160
161
162 private Class<?> tryRefreshClass(String name)
163 throws ClassNotFoundException {
164 synchronized (getClassLoadingLock(name)) {
165
166 Class<?> clasz = findLoadedClass(name);
167 if (clasz != null) {
168 if (LOG.isDebugEnabled()) {
169 LOG.debug("Class " + name + " already loaded");
170 }
171 }
172 else {
173 try {
174 if (LOG.isDebugEnabled()) {
175 LOG.debug("Finding class: " + name);
176 }
177 clasz = findClass(name);
178 } catch (ClassNotFoundException cnfe) {
179
180 if (LOG.isDebugEnabled()) {
181 LOG.debug("Loading new jar files, if any");
182 }
183 loadNewJars();
184
185 if (LOG.isDebugEnabled()) {
186 LOG.debug("Finding class again: " + name);
187 }
188 clasz = findClass(name);
189 }
190 }
191 return clasz;
192 }
193 }
194
195 private synchronized void loadNewJars() {
196
197 File[] files = localDir == null ? null : localDir.listFiles();
198 if (files != null) {
199 for (File file : files) {
200 String fileName = file.getName();
201 if (jarModifiedTime.containsKey(fileName)) {
202 continue;
203 }
204 if (file.isFile() && fileName.endsWith(".jar")) {
205 jarModifiedTime.put(fileName, Long.valueOf(file.lastModified()));
206 try {
207 URL url = file.toURI().toURL();
208 addURL(url);
209 } catch (MalformedURLException mue) {
210
211 LOG.warn("Failed to load new jar " + fileName, mue);
212 }
213 }
214 }
215 }
216
217
218 FileStatus[] statuses = null;
219 if (remoteDir != null) {
220 try {
221 statuses = remoteDirFs.listStatus(remoteDir);
222 } catch (IOException ioe) {
223 LOG.warn("Failed to check remote dir status " + remoteDir, ioe);
224 }
225 }
226 if (statuses == null || statuses.length == 0) {
227 return;
228 }
229
230 for (FileStatus status: statuses) {
231 if (status.isDirectory()) continue;
232 Path path = status.getPath();
233 String fileName = path.getName();
234 if (!fileName.endsWith(".jar")) {
235 if (LOG.isDebugEnabled()) {
236 LOG.debug("Ignored non-jar file " + fileName);
237 }
238 continue;
239 }
240 Long cachedLastModificationTime = jarModifiedTime.get(fileName);
241 if (cachedLastModificationTime != null) {
242 long lastModified = status.getModificationTime();
243 if (lastModified < cachedLastModificationTime.longValue()) {
244
245
246
247
248
249
250
251 continue;
252 }
253 }
254 try {
255
256 File dst = new File(localDir, fileName);
257 remoteDirFs.copyToLocalFile(path, new Path(dst.getPath()));
258 jarModifiedTime.put(fileName, Long.valueOf(dst.lastModified()));
259 URL url = dst.toURI().toURL();
260 addURL(url);
261 } catch (IOException ioe) {
262 LOG.warn("Failed to load new jar " + fileName, ioe);
263 }
264 }
265 }
266 }