HBASE-2889 Tool to look at HLogs -- parse and tail -f; added part 1, some fixup of hlog main

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@995182 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2010-09-08 18:33:51 +00:00
parent 4481fafee4
commit 3b16358da3
2 changed files with 92 additions and 39 deletions

View File

@ -1788,9 +1788,54 @@ public class HLog implements Syncable {
return new Path(regiondir, RECOVERED_EDITS_DIR);
}
public static final long FIXED_OVERHEAD = ClassSize.align(
ClassSize.OBJECT + (5 * ClassSize.REFERENCE) +
ClassSize.ATOMIC_INTEGER + Bytes.SIZEOF_INT + (3 * Bytes.SIZEOF_LONG));
private static void usage() {
System.err.println("Usage: java org.apache.hbase.HLog" +
" {--dump <logfile>... | --split <logdir>...}");
System.err.println("Usage: HLog <ARGS>");
System.err.println("Arguments:");
System.err.println(" --dump Dump textual representation of passed one or more files");
System.err.println(" For example: HLog --dump hdfs://example.com:9000/hbase/.logs/MACHINE/LOGFILE");
System.err.println(" --split Split the passed directory of WAL logs");
System.err.println(" For example: HLog --split hdfs://example.com:9000/hbase/.logs/DIR");
}
private static void dump(final Configuration conf, final Path p)
throws IOException {
FileSystem fs = FileSystem.get(conf);
if (!fs.exists(p)) {
throw new FileNotFoundException(p.toString());
}
if (!fs.isFile(p)) {
throw new IOException(p + " is not a file");
}
Reader log = getReader(fs, p, conf);
try {
int count = 0;
HLog.Entry entry;
while ((entry = log.next()) != null) {
System.out.println("#" + count + ", pos=" + log.getPosition() + " " +
entry.toString());
count++;
}
} finally {
log.close();
}
}
private static void split(final Configuration conf, final Path p)
throws IOException {
FileSystem fs = FileSystem.get(conf);
if (!fs.exists(p)) {
throw new FileNotFoundException(p.toString());
}
final Path baseDir = new Path(conf.get(HConstants.HBASE_DIR));
final Path oldLogDir = new Path(baseDir, HConstants.HREGION_OLDLOGDIR_NAME);
if (!fs.getFileStatus(p).isDir()) {
throw new IOException(p + " is not a directory");
}
splitLog(baseDir, p, oldLogDir, fs, conf);
}
/**
@ -1809,44 +1854,24 @@ public class HLog implements Syncable {
if (args[0].compareTo("--dump") != 0) {
if (args[0].compareTo("--split") == 0) {
dump = false;
} else {
usage();
System.exit(-1);
}
}
Configuration conf = HBaseConfiguration.create();
FileSystem fs = FileSystem.get(conf);
final Path baseDir = new Path(conf.get(HConstants.HBASE_DIR));
final Path oldLogDir = new Path(baseDir, HConstants.HREGION_OLDLOGDIR_NAME);
for (int i = 1; i < args.length; i++) {
try {
Path logPath = new Path(args[i]);
if (!fs.exists(logPath)) {
throw new FileNotFoundException(args[i] + " does not exist");
}
if (dump) {
if (!fs.isFile(logPath)) {
throw new IOException(args[i] + " is not a file");
}
Reader log = getReader(fs, logPath, conf);
try {
HLog.Entry entry;
while ((entry = log.next()) != null) {
System.out.println(entry.toString());
}
} finally {
log.close();
}
dump(conf, logPath);
} else {
if (!fs.getFileStatus(logPath).isDir()) {
throw new IOException(args[i] + " is not a directory");
}
splitLog(baseDir, logPath, oldLogDir, fs, conf);
split(conf, logPath);
}
} catch (Throwable t) {
t.printStackTrace();
System.exit(-1);
}
}
}
public static final long FIXED_OVERHEAD = ClassSize.align(
ClassSize.OBJECT + (5 * ClassSize.REFERENCE) +
ClassSize.ATOMIC_INTEGER + Bytes.SIZEOF_INT + (3 * Bytes.SIZEOF_LONG));
}

View File

@ -29,6 +29,7 @@ import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.regionserver.wal.HLog;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.apache.hadoop.io.SequenceFile;
import org.mortbay.log.Log;
public class SequenceFileLogReader implements HLog.Reader {
@ -93,6 +94,9 @@ public class SequenceFileLogReader implements HLog.Reader {
Configuration conf;
WALReader reader;
// Needed logging exceptions
Path path;
int edit = 0;
public SequenceFileLogReader() { }
@ -100,12 +104,17 @@ public class SequenceFileLogReader implements HLog.Reader {
public void init(FileSystem fs, Path path, Configuration conf)
throws IOException {
this.conf = conf;
this.path = path;
reader = new WALReader(fs, path, conf);
}
@Override
public void close() throws IOException {
reader.close();
try {
reader.close();
} catch (IOException ioe) {
throw addFileInfoToException(ioe);
}
}
@Override
@ -115,21 +124,29 @@ public class SequenceFileLogReader implements HLog.Reader {
@Override
public HLog.Entry next(HLog.Entry reuse) throws IOException {
if (reuse == null) {
HLog.Entry e = reuse;
if (e == null) {
HLogKey key = HLog.newKey(conf);
WALEdit val = new WALEdit();
if (reader.next(key, val)) {
return new HLog.Entry(key, val);
}
} else if (reader.next(reuse.getKey(), reuse.getEdit())) {
return reuse;
e = new HLog.Entry(key, val);
}
return null;
boolean b = false;
try {
b = this.reader.next(e.getKey(), e.getEdit());
} catch (IOException ioe) {
throw addFileInfoToException(ioe);
}
edit++;
return b? e: null;
}
@Override
public void seek(long pos) throws IOException {
reader.seek(pos);
try {
reader.seek(pos);
} catch (IOException ioe) {
throw addFileInfoToException(ioe);
}
}
@Override
@ -137,4 +154,15 @@ public class SequenceFileLogReader implements HLog.Reader {
return reader.getPosition();
}
}
private IOException addFileInfoToException(final IOException ioe)
throws IOException {
long pos = -1;
try {
pos = getPosition();
} catch (IOException e) {
Log.warn("Failed getting position to add to throw", e);
}
return new IOException((this.path == null? "": this.path.toString()) +
", pos=" + pos + ", edit=" + this.edit, ioe);
}
}