Skip to content

Commit

Permalink
JournalTool
Browse files Browse the repository at this point in the history
  • Loading branch information
peisun1115 committed Apr 4, 2017
1 parent 1129e35 commit 2e4509d
Showing 1 changed file with 54 additions and 36 deletions.
Expand Up @@ -11,19 +11,25 @@


package alluxio.master.journal; package alluxio.master.journal;


import alluxio.AlluxioURI;
import alluxio.Configuration;
import alluxio.PropertyKey;
import alluxio.RuntimeConstants; import alluxio.RuntimeConstants;
import alluxio.proto.journal.Journal.JournalEntry; import alluxio.proto.journal.Journal.JournalEntry;
import alluxio.util.CommonUtils;


import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;


import javax.annotation.concurrent.NotThreadSafe; import javax.annotation.concurrent.NotThreadSafe;


Expand All @@ -38,31 +44,36 @@
*/ */
@NotThreadSafe @NotThreadSafe
public final class JournalTool { public final class JournalTool {
private static final Logger LOG = LoggerFactory.getLogger(JournalTool.class);
/** Separator to place at the end of each journal entry. */ /** Separator to place at the end of each journal entry. */
private static final String ENTRY_SEPARATOR = StringUtils.repeat('-', 80); private static final String ENTRY_SEPARATOR = StringUtils.repeat('-', 80);
/** Amount of time to wait before giving up on the user supplying a journal log via stdin. */ /** Amount of time to wait before giving up on the user supplying a journal log via stdin. */
private static final long TIMEOUT_MS = 2000;
private static final int EXIT_FAILED = -1; private static final int EXIT_FAILED = -1;
private static final int EXIT_SUCCEEDED = 0; private static final int EXIT_SUCCEEDED = 0;
private static final Options OPTIONS = new Options() private static final Options OPTIONS = new Options()
.addOption("help", false, "Show help for this test") .addOption("help", false, "Show help for this test.")
.addOption("noTimeout", false, "Wait indefinitely for stdin to supply input"); .addOption("master", true, "The name of the master (e.g. FileSystemMaster, BlockMaster). "
+ "Set to FileSystemMaster by default.")
.addOption("start", true, "The start log sequence number (inclusive). Set to 0 by default.")
.addOption("end", true, "The end log sequence number (exclusive). Set to +inf by default.");


private static boolean sNoTimeout = false; private static boolean sHelp;
private static boolean sHelp = false; private static String sMaster;
private static long sStart;
private static long sEnd;


private JournalTool() {} // prevent instantiation private JournalTool() {} // prevent instantiation


/** /**
* Reads a journal via * Reads a journal via
* {@code java -cp \ * {@code java -cp \
* assembly/target/alluxio-assemblies-<ALLUXIO-VERSION>-jar-with-dependencies.jar \ * assembly/target/alluxio-assemblies-<ALLUXIO-VERSION>-jar-with-dependencies.jar \
* alluxio.master.journal.JournalTool < journal/FileSystemMaster/log.out}. * alluxio.master.journal.JournalTool -master BlockMaster -start 0x1000 -end 0x1001}.
* *
* @param args arguments passed to the tool * @param args arguments passed to the tool
* @throws IOException if a non-Alluxio related exception occurs * @throws IOException if a non-Alluxio related exception occurs
*/ */
public static void main(String[] args) throws IOException { public static void main(String[] args) {
if (!parseInputArgs(args)) { if (!parseInputArgs(args)) {
usage(); usage();
System.exit(EXIT_FAILED); System.exit(EXIT_FAILED);
Expand All @@ -71,16 +82,37 @@ public static void main(String[] args) throws IOException {
usage(); usage();
System.exit(EXIT_SUCCEEDED); System.exit(EXIT_SUCCEEDED);
} }
if (!sNoTimeout && !stdinHasData()) {
System.exit(EXIT_FAILED);
}


JournalFormatter formatter = new ProtoBufJournalFormatter(); JournalFactory factory = new Journal.Factory(getJournalLocation());
JournalInputStream journalStream = formatter.deserialize(System.in); Journal journal = factory.create(sMaster);
JournalReader reader = journal.getReader(
JournalReaderCreateOptions.defaults().setPrimary(true).setNextSequenceNumber(sStart));
JournalEntry entry; JournalEntry entry;
while ((entry = journalStream.read()) != null) { try {
System.out.print(entry); while ((entry = reader.read()) != null) {
System.out.println(ENTRY_SEPARATOR); if (entry.getSequenceNumber() >= sEnd) {
break;
}
System.out.println(ENTRY_SEPARATOR);
System.out.print(entry);
}
} catch (Exception e) {
LOG.error("Failed to read next journal entry.", e);
}
}

/**
* @return the journal location
*/
private static URI getJournalLocation() {
String journalDirectory = Configuration.get(PropertyKey.MASTER_JOURNAL_FOLDER);
if (!journalDirectory.endsWith(AlluxioURI.SEPARATOR)) {
journalDirectory += AlluxioURI.SEPARATOR;
}
try {
return new URI(journalDirectory);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} }
} }


Expand All @@ -92,40 +124,26 @@ public static void main(String[] args) throws IOException {
* @return true if parsing succeeded * @return true if parsing succeeded
*/ */
private static boolean parseInputArgs(String[] args) { private static boolean parseInputArgs(String[] args) {
CommandLineParser parser = new BasicParser(); CommandLineParser parser = new DefaultParser();
CommandLine cmd; CommandLine cmd;
try { try {
cmd = parser.parse(OPTIONS, args); cmd = parser.parse(OPTIONS, args);
} catch (ParseException e) { } catch (ParseException e) {
System.out.println("Failed to parse input args: " + e); System.out.println("Failed to parse input args: " + e);
return false; return false;
} }
sNoTimeout = cmd.hasOption("noTimeout");
sHelp = cmd.hasOption("help"); sHelp = cmd.hasOption("help");
return true; sMaster = cmd.getOptionValue("master", "FileSystemMaster");
} sStart = Long.parseLong(cmd.getOptionValue("start", "0"));

sEnd = Long.parseLong(cmd.getOptionValue("end", Long.valueOf(Long.MAX_VALUE).toString()));
/**
* @return true if stdin has data before {@link #TIMEOUT_MS} elapses
*/
private static boolean stdinHasData() throws IOException {
long start = System.currentTimeMillis();
while (System.in.available() == 0) {
if (System.currentTimeMillis() - start > TIMEOUT_MS) {
System.out.println(
"Timed out waiting for input from stdin. Use -noTimeout to wait longer.");
return false;
}
CommonUtils.sleepMs(50);
}
return true; return true;
} }


private static void usage() { private static void usage() {
new HelpFormatter().printHelp( new HelpFormatter().printHelp(
"java -cp alluxio-" + RuntimeConstants.VERSION "java -cp alluxio-" + RuntimeConstants.VERSION
+ "-jar-with-dependencies.jar alluxio.master.journal.JournalTool", + "-jar-with-dependencies.jar alluxio.master.journal.JournalTool",
"Read an Alluxio journal from stdin and write it human-readably to stdout", OPTIONS, "", "Read an Alluxio journal and write it human-readably to stdout", OPTIONS, "",
true); true);
} }
} }

0 comments on commit 2e4509d

Please sign in to comment.