Skip to content

Commit

Permalink
FISH-7869 Add additional timeout for domains commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinchan committed Oct 25, 2023
1 parent 4b0a9cf commit 914c745
Showing 4 changed files with 181 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -40,7 +40,12 @@
package fish.payara.admin.servermgmt.cli;

import com.sun.enterprise.admin.servermgmt.cli.RestartDomainCommand;

import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;

import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandException;
import org.glassfish.api.admin.CommandValidationException;
@@ -62,27 +67,49 @@ public class RestartDomainsCommand extends RestartDomainCommand {
@Param(name = "debug", optional = true)
private Boolean debug;

@Param(optional = true, defaultValue = "600")
private int timeout;

@Param(optional = true, defaultValue = "600")
private int domainTimeout;

@Override
protected void validate()
throws CommandException, CommandValidationException {
if (timeout < 1) {
if (timeout < 1 || domainTimeout < 1) {
throw new CommandValidationException("Timeout must be at least 1 second long.");
}
}

@Override
protected int executeCommand() throws CommandException {
try {
String[] domains = userArgDomainName.split(",");
for (String domainName : domains) {
setDomainName(domainName);
super.executeCommand();
logger.log(Level.FINE, "Restarted domain {0}", domainName);
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
Future future = executorService.submit(() -> {
try {
String[] domains = userArgDomainName.split(",");
for (String domainName : domains) {
setDomainName(domainName);
super.timeout = domainTimeout;
super.executeCommand();
logger.log(Level.FINE, "Restarted domain {0}", domainName);
}
return 0;
} catch (Exception ex){
throw new CommandException(ex.getLocalizedMessage());
}
});
long startTime = System.currentTimeMillis();
while (!timedOut(startTime)) {
if (future.isDone()) {
return 0;
}
return 0;
} catch (Exception ex){
throw new CommandException(ex.getLocalizedMessage());
}
throw new CommandException("Command Timed Out");
}


private boolean timedOut(long startTime) {
return (System.currentTimeMillis() - startTime) > (timeout * 1000);
}


}
Original file line number Diff line number Diff line change
@@ -41,6 +41,10 @@

import com.sun.enterprise.admin.servermgmt.cli.StartDomainCommand;
import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandException;
@@ -75,47 +79,64 @@ public class StartDomainsCommand extends StartDomainCommand {
private String preBootCommand;
@Param(name = "postbootcommandfile", optional = true)
private String postBootCommand;


@Param(optional = true, defaultValue = "600")
private int timeout;

@Param(optional = true, defaultValue = "600")
private int domainTimeout;

@Override
protected void validate()
throws CommandException, CommandValidationException {
if (preBootCommand != null){
if (preBootCommand != null) {
File prebootfile = new File(preBootCommand);
if (!prebootfile.exists()){
if (!prebootfile.exists()) {
throw new CommandValidationException("preboot commands file does not exist: " + prebootfile.getAbsolutePath());
}
}
if (postBootCommand != null){

if (postBootCommand != null) {
File postbootFile = new File(postBootCommand);
if (!postbootFile.exists()){
throw new CommandValidationException("postboot commands file does not exist: "+ postbootFile.getAbsolutePath());
if (!postbootFile.exists()) {
throw new CommandValidationException("postboot commands file does not exist: " + postbootFile.getAbsolutePath());
}
}

if (timeout < 1) {
if (timeout < 1 || domainTimeout < 1) {
throw new CommandValidationException("Timeout must be at least 1 second long.");
}
}

@Override
protected int executeCommand() throws CommandException {
try{
String[] domains = domainName0.split(",");
for (String domainName : domains) {
setDomainName(domainName);
super.initDomain();
programOpts.setHostAndPort(getAdminAddress());
super.executeCommand();
logger.log(Level.FINE, "Started domain {0}", domainName);

ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
Future future = executorService.submit(() -> {
try {
String[] domains = domainName0.split(",");
for (String domainName : domains) {
setDomainName(domainName);
super.timeout = domainTimeout;
super.initDomain();
programOpts.setHostAndPort(getAdminAddress());
super.executeCommand();
logger.log(Level.FINE, "Started domain {0}", domainName);
}
} catch (Exception ex) {
throw new RuntimeException(new CommandException(ex.getLocalizedMessage()));
}
});
long startTime = System.currentTimeMillis();
while (!timedOut(startTime)) {
if (future.isDone()) {
return 0;
}
return 0;
} catch (Exception ex){
throw new CommandException(ex.getLocalizedMessage());
}
throw new CommandException("Command Timed Out");
}

private boolean timedOut(long startTime) {
return (System.currentTimeMillis() - startTime) > (timeout * 1000);
}




}
Original file line number Diff line number Diff line change
@@ -50,6 +50,9 @@
import com.sun.enterprise.util.io.DomainDirs;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import org.glassfish.api.Param;
import org.glassfish.api.admin.*;
@@ -69,6 +72,13 @@ public class StopAllDomainsCommand extends StopDomainCommand {
Boolean force;
@Param(optional = true, defaultValue = "false")
Boolean kill;

@Param(optional = true, defaultValue = "600")
private int timeout;

@Param(optional = true, defaultValue = "60")
private int domainTimeout;

private static final long WAIT_FOR_DAS_TIME_MS = 60000; // 1 minute

private static final LocalStringsImpl strings = new LocalStringsImpl(ListDomainsCommand.class);
@@ -85,42 +95,51 @@ protected void prevalidate() throws CommandException {
protected void validate()
throws CommandException, CommandValidationException {
// no-op to prevent initDomain being called
if (timeout < 1) {
if (timeout < 1 || domainTimeout < 1) {
throw new CommandValidationException("Timeout must be at least 1 second long.");
}
}

@Override
@SuppressWarnings("ThrowFromFinallyBlock")
protected int executeCommand() throws CommandException {
MultiException allExceptions = new MultiException();
try {
String[] domainsList = getDomains();
for (String domain : domainsList) {
setConfig(domain);
ParameterMap parameterMap = new ParameterMap();
parameterMap.insert("timeout", String.valueOf(timeout));
programOpts.updateOptions(parameterMap);
RemoteCLICommand cmd = new RemoteCLICommand("stop-domain", programOpts, env);
cmd.setReadTimeout(timeout * 1000);
logger.log(Level.FINE, "Stopping domain {0}", domain);
try {
cmd.executeAndReturnOutput("stop-domain", "--force", force.toString());
} catch (Exception e) {
allExceptions.addError(e);
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
Future future = executorService.submit(() -> {
MultiException allExceptions = new MultiException();
try {
String[] domainsList = getDomains();
for (String domain : domainsList) {
setConfig(domain);
ParameterMap parameterMap = new ParameterMap();
parameterMap.insert("timeout", String.valueOf(domainTimeout));
programOpts.updateOptions(parameterMap);
RemoteCLICommand cmd = new RemoteCLICommand("stop-domain", programOpts, env);
cmd.setReadTimeout(domainTimeout * 1000);
logger.log(Level.FINE, "Stopping domain {0}", domain);
try {
cmd.executeAndReturnOutput("stop-domain", "--force", force.toString());
} catch (Exception e) {
allExceptions.addError(e);
}
logger.fine("Stopped domain");
}
return 0;
} catch (Exception ex) {
allExceptions.addError(ex);
return 1; // required to compile, but exception should be thrown in finally block
} finally {
if (!allExceptions.getErrors().isEmpty()){
throw new CommandException(allExceptions.getMessage());
}
logger.fine("Stopped domain");
}
return 0;
} catch (Exception ex) {
allExceptions.addError(ex);
return 1; // required to compile, but exception should be thrown in finally block
} finally {
if (!allExceptions.getErrors().isEmpty()){
throw new CommandException(allExceptions.getMessage());
});
long startTime = System.currentTimeMillis();
while (!timedOut(startTime)) {
if (future.isDone()) {
return 0;
}
}

throw new CommandException("Command Timed Out");
}

//Copied from ListDomainCommand.java
@@ -182,4 +201,8 @@ protected int dasNotRunning() throws CommandException {
}
return 0;
}

private boolean timedOut(long startTime) {
return (System.currentTimeMillis() - startTime) > (timeout * 1000);
}
}
Original file line number Diff line number Diff line change
@@ -42,6 +42,10 @@
import com.sun.enterprise.admin.cli.remote.DASUtils;
import com.sun.enterprise.admin.cli.remote.RemoteCLICommand;
import com.sun.enterprise.admin.servermgmt.cli.StopDomainCommand;

import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandException;
@@ -67,46 +71,63 @@ public class StopDomainsCommand extends StopDomainCommand {
Boolean force;
@Param(optional = true, defaultValue = "false")
Boolean kill;

@Param(optional = true, defaultValue = "600")
private int timeout;

@Param(optional = true, defaultValue = "60")
private int domainTimeout;

@Override
protected void validate()
throws CommandException, CommandValidationException {
// no-op to prevent initDomain being called
if (timeout < 1) {
if (timeout < 1 || domainTimeout < 1) {
throw new CommandValidationException("Timeout must be at least 1 second long.");
}
}

@Override
@SuppressWarnings("ThrowFromFinallyBlock")
protected int executeCommand() throws CommandException {
MultiException allExceptions = new MultiException();
try {
String[] domains = userArgDomainName.split(",");
for (String domainName : domains) {
setConfig(domainName);
ParameterMap parameterMap = new ParameterMap();
parameterMap.insert("timeout", String.valueOf(timeout));
programOpts.updateOptions(parameterMap);
RemoteCLICommand cmd = new RemoteCLICommand("stop-domain", programOpts, env);
cmd.setReadTimeout(timeout * 1000);
logger.log(Level.FINE, "Stopping domain {0}", domainName);
try {
cmd.executeAndReturnOutput("stop-domain");
} catch (Exception e) {
allExceptions.addError(e);
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
Future future = executorService.submit(() -> {
MultiException allExceptions = new MultiException();
try {
String[] domains = userArgDomainName.split(",");
for (String domainName : domains) {
setConfig(domainName);
ParameterMap parameterMap = new ParameterMap();
parameterMap.insert("timeout", String.valueOf(domainTimeout));
programOpts.updateOptions(parameterMap);
RemoteCLICommand cmd = new RemoteCLICommand("stop-domain", programOpts, env);
cmd.setReadTimeout(domainTimeout * 1000);
logger.log(Level.FINE, "Stopping domain {0}", domainName);
try {
cmd.executeAndReturnOutput("stop-domain");
} catch (Exception e) {
allExceptions.addError(e);
}
logger.fine("Stopped domain");
}
return 0;
} catch (Exception ex) {
allExceptions.addError(ex);
return 1; // required to compile, but exception should be thrown in finally block
} finally {
if (!allExceptions.getErrors().isEmpty()){
throw new CommandException(allExceptions.getMessage());
}
logger.fine("Stopped domain");
return 0;
}
return 0;
} catch (Exception ex) {
allExceptions.addError(ex);
return 1; // required to compile, but exception should be thrown in finally block
} finally {
if (!allExceptions.getErrors().isEmpty()){
throw new CommandException(allExceptions.getMessage());
});
long startTime = System.currentTimeMillis();
while (!timedOut(startTime)) {
if (future.isDone()) {
return 0;
}
}
throw new CommandException("Command Timed Out");
}

private int setConfig(String domain) throws CommandException{
@@ -151,5 +172,9 @@ protected int dasNotRunning() throws CommandException {
}
return 0;
}


private boolean timedOut(long startTime) {
return (System.currentTimeMillis() - startTime) > (timeout * 1000);
}

}

0 comments on commit 914c745

Please sign in to comment.