Skip to content

Commit

Permalink
ZOOKEEPER-3085: define exit codes in enum
Browse files Browse the repository at this point in the history
Define the used exit codes in one enum. contrib projects are excluded, as they can have seperate exit codes (and they do use it differently then in core ZK).
Some exit codes overlap, but refactoring the exit code used is not backward compatible.

Author: Norbert Kalmar <nkalmar@yahoo.com>

Reviewers: lvfangmin@apache.org, breed@apache.org, andor@apache.org

Closes #572 from nkalmar/ZOOKEEPER-3085 and squashes the following commits:

dbc18a6 [Norbert Kalmar] ZOOKEEPER-3085 change comments to javadoc comment in enum code defs
908c171 [Norbert Kalmar] ZOOKEEPER-3085 add missing enum usage and some renames
c49dcef [Norbert Kalmar] ZOOKEEPER-3085 use the enum value in ZooKeeperMain.exitCode as well
611aa4e [Norbert Kalmar] ZOOKEEPER-3085 define exit codes in enum
  • Loading branch information
nkalmar authored and anmolnar committed Aug 6, 2018
1 parent e3bae03 commit 44d2736
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 55 deletions.
5 changes: 3 additions & 2 deletions src/java/main/org/apache/zookeeper/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.zookeeper.common.Time;
import org.apache.zookeeper.server.ExitCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -151,7 +152,7 @@ protected void setWorkingDirectory(File dir) {
protected void run() throws IOException {
if (lastTime + interval > Time.currentElapsedTime())
return;
exitCode = 0; // reset for next run
exitCode = ExitCode.EXECUTION_FINISHED.getValue(); // reset for next run
runCommand();
}

Expand Down Expand Up @@ -224,7 +225,7 @@ public void run() {
completed.set(true);
//the timeout thread handling
//taken care in finally block
if (exitCode != 0) {
if (exitCode != ExitCode.EXECUTION_FINISHED.getValue()) {
throw new ExitCodeException(exitCode, errMsg.toString());
}
} catch (InterruptedException ie) {
Expand Down
8 changes: 5 additions & 3 deletions src/java/main/org/apache/zookeeper/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.apache.zookeeper;

import org.apache.zookeeper.server.ExitCode;

public class Version implements org.apache.zookeeper.version.Info {

/*
Expand Down Expand Up @@ -57,7 +59,7 @@ public static void printUsage() {
.print("Usage:\tjava -cp ... org.apache.zookeeper.Version "
+ "[--full | --short | --revision],\n\tPrints --full version "
+ "info if no arg specified.");
System.exit(1);
System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
}

/**
Expand All @@ -77,14 +79,14 @@ public static void main(String[] args) {
}
if (args.length == 0 || (args.length == 1 && args[0].equals("--full"))) {
System.out.println(getFullVersion());
System.exit(0);
System.exit(ExitCode.EXECUTION_FINISHED.getValue());
}
if (args[0].equals("--short"))
System.out.println(getVersion());
else if (args[0].equals("--revision"))
System.out.println(getVersionRevision());
else
printUsage();
System.exit(0);
System.exit(ExitCode.EXECUTION_FINISHED.getValue());
}
}
5 changes: 3 additions & 2 deletions src/java/main/org/apache/zookeeper/ZooKeeperMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.apache.zookeeper.cli.SyncCommand;
import org.apache.zookeeper.client.ZKClientConfig;
import org.apache.zookeeper.admin.ZooKeeperAdmin;
import org.apache.zookeeper.server.ExitCode;

/**
* The command line client to ZooKeeper.
Expand All @@ -84,7 +85,7 @@ public class ZooKeeperMain {
protected HashMap<Integer,String> history = new HashMap<Integer,String>( );
protected int commandCount = 0;
protected boolean printWatches = true;
protected int exitCode = 0;
protected int exitCode = ExitCode.EXECUTION_FINISHED.getValue();

protected ZooKeeper zk;
protected String host = "";
Expand Down Expand Up @@ -586,7 +587,7 @@ protected boolean processCmd(MyCommandOptions co) throws CliException, IOExcepti
boolean watch = false;
try {
watch = processZKCmd(co);
exitCode = 0;
exitCode = ExitCode.EXECUTION_FINISHED.getValue();
} catch (CliException ex) {
exitCode = ex.getExitCode();
System.err.println(ex.getMessage());
Expand Down
41 changes: 38 additions & 3 deletions src/java/main/org/apache/zookeeper/server/ExitCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,43 @@
/**
* Exit code used to exit server
*/
public class ExitCode {
public enum ExitCode {

/** Execution finished normally */
EXECUTION_FINISHED(0),

/** Unexpected errors like IO Exceptions */
UNEXPECTED_ERROR(1),

/** Invalid arguments during invocations */
INVALID_INVOCATION(2),

/** Cannot access datadir when trying to replicate server */
UNABLE_TO_ACCESS_DATADIR(3),

/** Unable to start admin server at ZooKeeper startup */
ERROR_STARTING_ADMIN_SERVER(4),

/** Severe error during snapshot IO */
TXNLOG_ERROR_TAKING_SNAPSHOT(10),

/** zxid from COMMIT does not match the one from pendingTxns queue */
UNMATCHED_TXN_COMMIT(12),

/** Unexpected packet from leader, or unable to truncate log on Leader.TRUNC */
QUORUM_PACKET_ERROR(13),

/** Unable to bind to the quorum (election) port after multiple retry */
UNABLE_TO_BIND_QUORUM_PORT(14);

private final int value;

ExitCode(final int newValue) {
value = newValue;
}

public int getValue() {
return value;
}

/* Represents unexpected error */
public final static int UNEXPECTED_ERROR = 1;
}
4 changes: 2 additions & 2 deletions src/java/main/org/apache/zookeeper/server/LogFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class LogFormatter {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("USAGE: LogFormatter log_file");
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
}
FileInputStream fis = new FileInputStream(args[0]);
BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
Expand All @@ -55,7 +55,7 @@ public static void main(String[] args) throws Exception {

if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
System.err.println("Invalid magic number for " + args[0]);
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
}
System.out.println("ZooKeeper Transactional Log File with dbid "
+ fhdr.getDbid() + " txnlog format version "
Expand Down
2 changes: 1 addition & 1 deletion src/java/main/org/apache/zookeeper/server/PurgeTxnLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,6 @@ private static int validateAndGetCount(String number) {

private static void printUsageThenExit() {
printUsage();
System.exit(1);
System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SnapshotFormatter {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("USAGE: SnapshotFormatter snapshot_file");
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
}

new SnapshotFormatter().run(args[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static String op2String(int op) {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("USAGE: TraceFormatter trace_file");
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
}
FileChannel fc = new FileInputStream(args[0]).getChannel();
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public ZooKeeperCriticalThread(String threadName, ZooKeeperServerListener listen
@Override
protected void handleException(String threadName, Throwable e) {
LOG.error("Severe unrecoverable error, from thread : {}", threadName, e);
listener.notifyStopping(threadName, ExitCode.UNEXPECTED_ERROR);
listener.notifyStopping(threadName, ExitCode.UNEXPECTED_ERROR.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public void takeSnapshot(boolean syncSnap){
LOG.error("Severe unrecoverable error, exiting", e);
// This is a severe error that we cannot recover from,
// so we need to exit
System.exit(10);
System.exit(ExitCode.TXNLOG_ERROR_TAKING_SNAPSHOT.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ public static void main(String[] args) {
LOG.error("Invalid arguments, exiting abnormally", e);
LOG.info(USAGE);
System.err.println(USAGE);
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
} catch (ConfigException e) {
LOG.error("Invalid config, exiting abnormally", e);
System.err.println("Invalid config, exiting abnormally");
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
} catch (DatadirException e) {
LOG.error("Unable to access datadir, exiting abnormally", e);
System.err.println("Unable to access datadir, exiting abnormally");
System.exit(3);
System.exit(ExitCode.UNABLE_TO_ACCESS_DATADIR.getValue());
} catch (AdminServerException e) {
LOG.error("Unable to start AdminServer, exiting abnormally", e);
System.err.println("Unable to start AdminServer, exiting abnormally");
System.exit(4);
System.exit(ExitCode.ERROR_STARTING_ADMIN_SERVER.getValue());
} catch (Exception e) {
LOG.error("Unexpected exception, exiting abnormally", e);
System.exit(1);
System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
}
LOG.info("Exiting normally");
System.exit(0);
System.exit(ExitCode.EXECUTION_FINISHED.getValue());
}

protected void initializeAndRun(String[] args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.jute.BinaryInputArchive;
import org.apache.jute.BinaryOutputArchive;
import org.apache.jute.Record;
import org.apache.zookeeper.server.ExitCode;
import org.apache.zookeeper.server.TraceFormatter;
import org.apache.zookeeper.server.util.SerializeUtils;
import org.apache.zookeeper.txn.TxnHeader;
Expand Down Expand Up @@ -114,12 +115,12 @@ public TxnLogToolkit(boolean recoveryMode, boolean verbose, String txnLogFileNam
this.force = force;
txnLogFile = new File(txnLogFileName);
if (!txnLogFile.exists() || !txnLogFile.canRead()) {
throw new TxnLogToolkitException(1, "File doesn't exist or not readable: %s", txnLogFile);
throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(), "File doesn't exist or not readable: %s", txnLogFile);
}
if (recoveryMode) {
recoveryLogFile = new File(txnLogFile.toString() + ".fixed");
if (recoveryLogFile.exists()) {
throw new TxnLogToolkitException(1, "Recovery file %s already exists or not writable", recoveryLogFile);
throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(), "Recovery file %s already exists or not writable", recoveryLogFile);
}
}

Expand All @@ -135,7 +136,7 @@ public void dump(Scanner scanner) throws Exception {
FileHeader fhdr = new FileHeader();
fhdr.deserialize(logStream, "fileheader");
if (fhdr.getMagic() != TXNLOG_MAGIC) {
throw new TxnLogToolkitException(2, "Invalid magic number for %s", txnLogFile.getName());
throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Invalid magic number for %s", txnLogFile.getName());
}
System.out.println("ZooKeeper Transactional Log File with dbid "
+ fhdr.getDbid() + " txnlog format version "
Expand Down Expand Up @@ -187,7 +188,7 @@ public void dump(Scanner scanner) throws Exception {
printTxn(bytes);
}
if (logStream.readByte("EOR") != 'B') {
throw new TxnLogToolkitException(1, "Last transaction was partial.");
throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(), "Last transaction was partial.");
}
if (recoveryMode) {
filePadding.padFile(recoveryFos.getChannel());
Expand All @@ -209,7 +210,7 @@ private boolean askForFix(Scanner scanner) throws TxnLogToolkitException {
case 'N':
return false;
case 'A':
throw new TxnLogToolkitException(0, "Recovery aborted.");
throw new TxnLogToolkitException(ExitCode.EXECUTION_FINISHED.getValue(), "Recovery aborted.");
}
}
}
Expand Down Expand Up @@ -289,7 +290,7 @@ private static TxnLogToolkit parseCommandLine(String[] args) throws TxnLogToolki
}
return new TxnLogToolkit(cli.hasOption("recover"), cli.hasOption("verbose"), cli.getArgs()[0], cli.hasOption("yes"));
} catch (ParseException e) {
throw new TxnLogToolkitParseException(options, 1, e.getMessage());
throw new TxnLogToolkitParseException(options, ExitCode.UNEXPECTED_ERROR.getValue(), e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.jute.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.zookeeper.server.ExitCode;
import org.apache.zookeeper.server.FinalRequestProcessor;
import org.apache.zookeeper.server.Request;
import org.apache.zookeeper.server.RequestProcessor;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void commit(long zxid) {
LOG.error("Committing zxid 0x" + Long.toHexString(zxid)
+ " but next pending txn 0x"
+ Long.toHexString(firstElementZxid));
System.exit(12);
System.exit(ExitCode.UNMATCHED_TXN_COMMIT.getValue());
}
Request request = pendingTxns.remove();
commitProcessor.commit(request);
Expand Down
7 changes: 3 additions & 4 deletions src/java/main/org/apache/zookeeper/server/quorum/Learner.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.jute.InputArchive;
import org.apache.jute.OutputArchive;
import org.apache.jute.Record;
import org.apache.zookeeper.server.ExitCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.zookeeper.ZooDefs.OpCode;
Expand All @@ -50,8 +51,6 @@
import org.apache.zookeeper.server.util.ZxidUtils;
import org.apache.zookeeper.txn.SetDataTxn;
import org.apache.zookeeper.txn.TxnHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class is the superclass of two of the three main actors in a ZK
Expand Down Expand Up @@ -409,15 +408,15 @@ else if (qp.getType() == Leader.SNAP) {
// not able to truncate the log
LOG.error("Not able to truncate the log "
+ Long.toHexString(qp.getZxid()));
System.exit(13);
System.exit(ExitCode.QUORUM_PACKET_ERROR.getValue());
}
zk.getZKDatabase().setlastProcessedZxid(qp.getZxid());

}
else {
LOG.error("Got unexpected packet from leader: {}, exiting ... ",
LearnerHandler.packetToString(qp));
System.exit(13);
System.exit(ExitCode.QUORUM_PACKET_ERROR.getValue());

}
zk.getZKDatabase().initConfigInZKDatabase(self.getQuorumVerifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.zookeeper.server.ExitCode;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
import org.apache.zookeeper.server.util.ConfigUtils;
import org.apache.zookeeper.server.ZooKeeperThread;
Expand Down Expand Up @@ -921,7 +922,7 @@ public void run() {
// After leaving listener thread, the host cannot join the
// quorum anymore, this is a severe error that we cannot
// recover from, so we need to exit
System.exit(14);
System.exit(ExitCode.UNABLE_TO_BIND_QUORUM_PORT.getValue());
}
} else if (ss != null) {
// Clean up for shutdown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.zookeeper.jmx.ManagedUtil;
import org.apache.zookeeper.server.ExitCode;
import org.apache.zookeeper.server.ServerCnxnFactory;
import org.apache.zookeeper.server.ZKDatabase;
import org.apache.zookeeper.server.DatadirCleanupManager;
Expand Down Expand Up @@ -84,25 +85,25 @@ public static void main(String[] args) {
LOG.error("Invalid arguments, exiting abnormally", e);
LOG.info(USAGE);
System.err.println(USAGE);
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
} catch (ConfigException e) {
LOG.error("Invalid config, exiting abnormally", e);
System.err.println("Invalid config, exiting abnormally");
System.exit(2);
System.exit(ExitCode.INVALID_INVOCATION.getValue());
} catch (DatadirException e) {
LOG.error("Unable to access datadir, exiting abnormally", e);
System.err.println("Unable to access datadir, exiting abnormally");
System.exit(3);
System.exit(ExitCode.UNABLE_TO_ACCESS_DATADIR.getValue());
} catch (AdminServerException e) {
LOG.error("Unable to start AdminServer, exiting abnormally", e);
System.err.println("Unable to start AdminServer, exiting abnormally");
System.exit(4);
System.exit(ExitCode.ERROR_STARTING_ADMIN_SERVER.getValue());
} catch (Exception e) {
LOG.error("Unexpected exception, exiting abnormally", e);
System.exit(1);
System.exit(ExitCode.UNEXPECTED_ERROR.getValue());
}
LOG.info("Exiting normally");
System.exit(0);
System.exit(ExitCode.EXECUTION_FINISHED.getValue());
}

protected void initializeAndRun(String[] args)
Expand Down
Loading

0 comments on commit 44d2736

Please sign in to comment.