Skip to content

Commit 76e1501

Browse files
committed
Merge pull request #33 from gluster/GlusterDebugFileSystem
Debug shim for the GlusterFileSystem.
2 parents ca478fc + b87d71c commit 76e1501

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/**
2+
*
3+
* Copyright (c) 2011 Red Hat Inc <http://www.redhat.com>
4+
* This file is part of GlusterFS.
5+
*
6+
* Licensed under the Apache License, Version 2.0
7+
* (the "License"); you may not use this file except in compliance with
8+
* the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15+
* implied. See the License for the specific language governing
16+
* permissions and limitations under the License.
17+
*
18+
*/
19+
20+
/**
21+
* Hard-core debugging shim which uses the GlusterFileSystem super class for the heavy lifting, but logs every call
22+
* to /tmp/glusterfs-N.log
23+
*
24+
* This Shim can be interchanged with the GlusterFileSystem to debug calls.
25+
*
26+
* Each JVM instance of GlusterDebugFileSystem will generate a new log, incremented on what's already there.
27+
*
28+
* To use, specify this class as your file system in hadoop:
29+
*
30+
* <property>
31+
* <name>fs.glusterfs.impl</name>
32+
* <value>org.apache.hadoop.fs.glusterfs.GlusterDebugFileSystem</value>
33+
* </property>
34+
*
35+
* Authoer: [email protected]
36+
*
37+
*
38+
*/
39+
40+
package org.apache.hadoop.fs.glusterfs;
41+
42+
import java.io.BufferedWriter;
43+
import java.io.File;
44+
import java.io.FileNotFoundException;
45+
import java.io.FileWriter;
46+
import java.io.IOException;
47+
import java.io.PrintWriter;
48+
import java.net.URI;
49+
import java.text.DateFormat;
50+
import java.text.SimpleDateFormat;
51+
import java.util.Date;
52+
53+
import org.apache.hadoop.conf.Configuration;
54+
import org.apache.hadoop.fs.BlockLocation;
55+
import org.apache.hadoop.fs.FSDataInputStream;
56+
import org.apache.hadoop.fs.FSDataOutputStream;
57+
import org.apache.hadoop.fs.FileStatus;
58+
import org.apache.hadoop.fs.Path;
59+
import org.apache.hadoop.fs.permission.FsPermission;
60+
import org.apache.hadoop.util.Progressable;
61+
62+
public class GlusterDebugFileSystem extends GlusterFileSystem{
63+
64+
private static File logFile=null;
65+
private static final String LOG_PREFIX="/tmp/glusterfs";
66+
private static boolean showStackTrace = true;
67+
68+
public static synchronized String getStackTrace(int stripTopElements){
69+
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
70+
String traceString = "";
71+
// remove the specified top elements of the stack trace (to avoid debug methods in the trace). the +1 is for this methods call.
72+
for(int i=stripTopElements+1;i<trace.length;i++){
73+
traceString += "\t[" + trace[i].getFileName() + "] " + trace[i].getClassName() + "." + trace[i].getMethodName() + "() line:" + trace[i].getLineNumber() + "\n";
74+
}
75+
76+
return traceString;
77+
}
78+
79+
public static synchronized void logMachine(String text){
80+
81+
DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
82+
Date date=new Date();
83+
if(logFile==null){
84+
for(int i=0;i<1000000;i++){
85+
logFile=new File(LOG_PREFIX+"-"+i+".log");
86+
if(!logFile.exists()){
87+
try{
88+
logFile.createNewFile();
89+
break;
90+
}catch (IOException e){
91+
}
92+
}
93+
}
94+
}
95+
96+
try{
97+
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(logFile, true)));
98+
out.write("(" + dateFormat.format(date)+") : "+text+"\n");
99+
String stackTrace = GlusterDebugFileSystem.getStackTrace(3);
100+
if(showStackTrace) out.write(stackTrace);
101+
out.close();
102+
}catch (FileNotFoundException e){
103+
e.printStackTrace();
104+
}catch (IOException e){
105+
e.printStackTrace();
106+
}
107+
108+
}
109+
public void initialize(URI uri,Configuration conf) throws IOException{
110+
GlusterDebugFileSystem.logMachine("Init of the GlusterDebugFileSystem....");
111+
GlusterDebugFileSystem.logMachine("init(" + uri + "," + conf);
112+
super.initialize(uri,conf);
113+
}
114+
115+
public URI getUri(){
116+
GlusterDebugFileSystem.logMachine("getUri()");
117+
return super.getUri();
118+
}
119+
120+
public String getName(){
121+
GlusterDebugFileSystem.logMachine("getName()");
122+
return super.getName();
123+
}
124+
125+
public Path getWorkingDirectory(){
126+
GlusterDebugFileSystem.logMachine("getWorkingDirectory()");
127+
return super.getWorkingDirectory();
128+
}
129+
130+
public Path getHomeDirectory(){
131+
GlusterDebugFileSystem.logMachine("getHomeDirectory()");
132+
return super.getHomeDirectory();
133+
}
134+
135+
public void setWorkingDirectory(Path dir){
136+
GlusterDebugFileSystem.logMachine("setWorkingDirectory("+dir+")");
137+
super.setWorkingDirectory(dir);
138+
}
139+
140+
public boolean exists(Path path) throws IOException{
141+
GlusterDebugFileSystem.logMachine("exists("+path+")");
142+
return super.exists(path);
143+
}
144+
145+
public boolean mkdirs(Path path,FsPermission permission) throws IOException{
146+
GlusterDebugFileSystem.logMachine("mkdirs("+path+","+permission+")");
147+
return super.mkdirs(path, permission);
148+
}
149+
150+
@Deprecated
151+
public boolean isDirectory(Path path) throws IOException{
152+
GlusterDebugFileSystem.logMachine("isDirectory("+path+")");
153+
return super.isDirectory(path);
154+
}
155+
156+
public boolean isFile(Path path) throws IOException{
157+
GlusterDebugFileSystem.logMachine("isFile("+path+")");
158+
return super.isFile(path);
159+
}
160+
161+
public Path[] listPaths(Path path) throws IOException{
162+
GlusterDebugFileSystem.logMachine("listPaths("+path+")");
163+
return super.listPaths(path);
164+
}
165+
166+
public FileStatus[] listStatus(Path path) throws IOException{
167+
GlusterDebugFileSystem.logMachine("listStatus("+path+")");
168+
return super.listStatus(path);
169+
170+
}
171+
172+
public FileStatus getFileStatusFromFileString(String path) throws IOException{
173+
GlusterDebugFileSystem.logMachine("getFileStatusFromFileString("+path+")");
174+
return super.getFileStatusFromFileString(path);
175+
}
176+
177+
public void setPermission(Path p,FsPermission permission){
178+
GlusterDebugFileSystem.logMachine("setPermission("+p+","+permission+")");
179+
super.setPermission(p, permission);
180+
}
181+
182+
public FileStatus getFileStatus(Path path) throws IOException{
183+
GlusterDebugFileSystem.logMachine("getFileStatus("+path+")");
184+
return super.getFileStatus(path);
185+
}
186+
187+
public FSDataOutputStream create(Path path,FsPermission permission,boolean overwrite,int bufferSize,short replication,long blockSize,Progressable progress) throws IOException{
188+
GlusterDebugFileSystem.logMachine("create("+path+","+permission+","+(overwrite ? "true" : "false")+","+bufferSize+","+replication+","+blockSize+",progress"+"):");
189+
return super.create(path, permission, overwrite, bufferSize, replication, blockSize, progress);
190+
}
191+
192+
public FSDataInputStream open(Path path) throws IOException{
193+
GlusterDebugFileSystem.logMachine("open("+path+")");
194+
return super.open(path);
195+
}
196+
197+
public FSDataInputStream open(Path path,int bufferSize) throws IOException{
198+
GlusterDebugFileSystem.logMachine("open("+path+","+bufferSize+")");
199+
return super.open(path, bufferSize);
200+
}
201+
202+
public FSDataOutputStream append(Path f,int bufferSize,Progressable progress) throws IOException{
203+
GlusterDebugFileSystem.logMachine("append("+f+","+bufferSize+",progress)");
204+
return super.append(f, bufferSize, progress);
205+
}
206+
207+
public boolean rename(Path src,Path dst) throws IOException{
208+
GlusterDebugFileSystem.logMachine("rename("+src+","+dst+")");
209+
return super.rename(src, dst);
210+
}
211+
212+
public boolean delete(Path path) throws IOException{
213+
GlusterDebugFileSystem.logMachine("delete("+path+")");
214+
return super.delete(path);
215+
}
216+
217+
public boolean delete(Path path,boolean recursive) throws IOException{
218+
GlusterDebugFileSystem.logMachine("delete("+path+(recursive ? ",true" : ",false")+"): ");
219+
return super.delete(path, recursive);
220+
}
221+
222+
@Deprecated
223+
public long getLength(Path path) throws IOException{
224+
GlusterDebugFileSystem.logMachine("getLength("+path+") ");
225+
return super.getLength(path);
226+
}
227+
228+
public short getDefaultReplication(Path path) throws IOException{
229+
GlusterDebugFileSystem.logMachine("getDefaultReplication("+path+") ");
230+
return super.getDefaultReplication(path);
231+
}
232+
233+
public long getBlockSize(Path path) throws IOException{
234+
GlusterDebugFileSystem.logMachine("getBlockSize("+path+") ");
235+
return super.getBlockSize(path);
236+
}
237+
238+
public long getDefaultBlockSize(){
239+
GlusterDebugFileSystem.logMachine("getDefaultBlockSize() ");
240+
return super.getDefaultBlockSize();
241+
}
242+
243+
public BlockLocation[] getFileBlockLocations(FileStatus file,long start,long len) throws IOException{
244+
GlusterDebugFileSystem.logMachine("getFileBlockLocations("+file+","+start+","+len+")");
245+
return super.getFileBlockLocations(file, start, len);
246+
}
247+
248+
public void setOwner(Path p,String username,String groupname) throws IOException{
249+
GlusterDebugFileSystem.logMachine("setOwner("+p+","+username+","+groupname+")");
250+
super.setOwner(p, username, groupname);
251+
}
252+
253+
public void copyFromLocalFile(boolean delSrc,Path src,Path dst) throws IOException{
254+
GlusterDebugFileSystem.logMachine("copyFromLocalFile("+delSrc+","+src+","+dst+")");
255+
super.copyFromLocalFile(delSrc, src, dst);
256+
}
257+
258+
public void copyToLocalFile(boolean delSrc,Path src,Path dst) throws IOException{
259+
GlusterDebugFileSystem.logMachine("copyFromLocalFile("+delSrc+","+src+","+dst+")");
260+
super.copyToLocalFile(delSrc, src, dst);
261+
}
262+
263+
public Path startLocalOutput(Path fsOutputFile,Path tmpLocalFile) throws IOException{
264+
GlusterDebugFileSystem.logMachine("startLocalOutput("+fsOutputFile+","+tmpLocalFile+")");
265+
return super.startLocalOutput(fsOutputFile, tmpLocalFile);
266+
}
267+
268+
public void completeLocalOutput(Path fsOutputFile,Path tmpLocalFile) throws IOException{
269+
GlusterDebugFileSystem.logMachine("completeLocalOutput("+fsOutputFile+","+tmpLocalFile+")");
270+
super.completeLocalOutput(fsOutputFile, tmpLocalFile);
271+
}
272+
273+
}

0 commit comments

Comments
 (0)