Skip to content

Commit

Permalink
Merge pull request #5630 from kalinchan/FISH-898
Browse files Browse the repository at this point in the history
FISH-898 Added timeout option to instance commands
  • Loading branch information
Pandrex247 authored Apr 4, 2022
2 parents db1d592 + 3b5428f commit bbb601f
Show file tree
Hide file tree
Showing 24 changed files with 615 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.jline.reader.EndOfFileException;
import org.jline.reader.UserInterruptException;
import org.jvnet.hk2.component.MultiMap;

import java.io.*;
Expand All @@ -79,8 +81,6 @@
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jline.reader.EndOfFileException;
import org.jline.reader.UserInterruptException;

/**
* A remote command handled by the asadmin CLI.
Expand All @@ -105,6 +105,7 @@ public class RemoteCLICommand extends CLICommand {
private RemoteCLICommand.CLIRemoteAdminCommand rac;
private final MultiMap<String, AdminCommandListener<GfSseInboundEvent>> listeners =
new MultiMap<String, AdminCommandListener<GfSseInboundEvent>>();
private int readTimeout;

/**
* A special RemoteAdminCommand that overrides methods so that we can handle
Expand Down Expand Up @@ -156,6 +157,15 @@ public CLIRemoteAdminCommand(String name, String host, int port, boolean secure,
super.setReadTimeout(Integer.parseInt(stimeout));
}
}

/**
* Set the RemoteRestCommand read timeout
* @param readTimeout timeout in milliseconds
*/
@Override
public void setReadTimeout(int readTimeout){
super.setReadTimeout(readTimeout);
}

@Override
public void fetchCommandModel() throws CommandException {
Expand Down Expand Up @@ -924,6 +934,9 @@ private void initializeRemoteAdminCommand() throws CommandException {
programOpts.getPassword(), logger, programOpts.getAuthToken(),programOpts.isNotifyCommand());
rac.setFileOutputDirectory(outputDir);
rac.setInteractive(programOpts.isInteractive());
if (readTimeout > 0) {
rac.setReadTimeout(readTimeout);
}
for (String key : listeners.keySet()) {
for (AdminCommandListener<GfSseInboundEvent> listener : listeners.get(key)) {
rac.registerListener(key, listener);
Expand Down Expand Up @@ -1026,4 +1039,12 @@ private static synchronized ClassLoader getModuleClassLoader() {
moduleClassLoader = new DirectoryClassLoader(modulesDir, CLICommand.class.getClassLoader());
return moduleClassLoader;
}

/**
* Set read timeout for RemoteRestCommand
* @param readTimeout
*/
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,10 @@
* holder.
*/

// Portions Copyright [2016-2021] [Payara Foundation and/or affiliates]
// Portions Copyright [2016-2022] [Payara Foundation and/or affiliates]

package com.sun.enterprise.admin.servermgmt.cli;

import static com.sun.enterprise.admin.servermgmt.domain.DomainConstants.MASTERPASSWORD_FILE;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.Arrays;

import com.sun.enterprise.admin.cli.CLICommand;
import com.sun.enterprise.admin.cli.CLIConstants;
import com.sun.enterprise.admin.cli.ProgramOptions;
Expand All @@ -71,15 +58,9 @@
import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.util.io.FileUtils;
import com.sun.enterprise.util.io.ServerDirs;

import java.net.InetAddress;
import java.net.InetSocketAddress;

import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.CommandException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

Expand All @@ -92,6 +73,19 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.XMLEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.KeyStore;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;

import static com.sun.enterprise.admin.servermgmt.domain.DomainConstants.MASTERPASSWORD_FILE;

/**
* A class that's supposed to capture all the behavior common to operation on a
Expand Down Expand Up @@ -409,23 +403,27 @@ protected boolean isRunning() {
}
}

/**
* Byron Nevins Says: We have quite a historical assortment of ways to determine
* if a server has restarted. There are little teeny timing issues with all of
* them. I'm confident that this new technique will clear them all up. Here we
* are just monitoring the PID of the new server and comparing it to the pid of
* the old server. The oldServerPid is guaranteed to be either the PID of the
* "old" server or -1 if we couldn't get it -- or it isn't running. If it is -1
* then we make the assumption that once we DO get a valid pid that the server
* has started. If the old pid is valid we simply poll until we get a different
* pid. Notice that we will never get a valid pid back unless the server is
* officially up and running and "STARTED" Created April 2013
*
* @param oldServerPid The pid of the server which is being restarted.
* @throws CommandException if we time out.
*/
protected final void waitForRestart(final int oldServerPid) throws CommandException {
long end = getEndTime();
waitForRestart(oldServerPid, CLIConstants.WAIT_FOR_DAS_TIME_MS);
}

/**
* Byron Nevins Says: We have quite a historical assortment of ways to determine
* if a server has restarted. There are little teeny timing issues with all of
* them. I'm confident that this new technique will clear them all up. Here we
* are just monitoring the PID of the new server and comparing it to the pid of
* the old server. The oldServerPid is guaranteed to be either the PID of the
* "old" server or -1 if we couldn't get it -- or it isn't running. If it is -1
* then we make the assumption that once we DO get a valid pid that the server
* has started. If the old pid is valid we simply poll until we get a different
* pid. Notice that we will never get a valid pid back unless the server is
* officially up and running and "STARTED" Created April 2013
*
* @param oldServerPid The pid of the server which is being restarted.
* @throws CommandException if we time out.
*/
protected final void waitForRestart(final int oldServerPid, long timeout) throws CommandException {
long end = getEndTime(timeout);

while (now() < end) {
try {
Expand Down Expand Up @@ -608,10 +606,8 @@ private long now() {
return System.currentTimeMillis();
}

private long getEndTime() {
// it's a method in case we someday allow configuring this VERY long
// timeout at runtime.
return CLIConstants.WAIT_FOR_DAS_TIME_MS + now();
private long getEndTime(long timeout) {
return timeout + now();
}

protected boolean dataGridEncryptionEnabled() throws IOException, XMLStreamException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2022 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/

package com.sun.enterprise.admin.util;

import com.sun.enterprise.admin.remote.RemoteRestAdminCommand;
import org.glassfish.api.ExecutionContext;
import org.glassfish.api.ParamDefaultCalculator;

/**
* Class to use within the @Param annotation for a calculator to get the timeout
* from either a System Property, Environment Property or a constant value
*
* @author Kalin Chan
*/
public class TimeoutParamDefaultCalculator extends ParamDefaultCalculator {

public TimeoutParamDefaultCalculator() {
}

@Override
public String defaultValue(ExecutionContext context) {
return String.valueOf(RemoteRestAdminCommand.getReadTimeout() / 1000) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@
* holder.
*/

// Portions Copyright [2018-2019] [Payara Foundation and/or its affiliates]
// Portions Copyright [2018-2022] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.v3.admin.cluster;

import static java.lang.Math.min;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.FINE;
import static org.glassfish.api.ActionReport.ExitCode.FAILURE;
import static org.glassfish.api.ActionReport.ExitCode.SUCCESS;
import static org.glassfish.api.ActionReport.ExitCode.WARNING;
import com.sun.enterprise.admin.remote.RemoteRestAdminCommand;
import com.sun.enterprise.config.serverbeans.Cluster;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.v3.admin.adapter.AdminEndpointDecider;
import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.*;
import org.glassfish.api.admin.CommandRunner.CommandInvocation;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -58,20 +61,10 @@
import java.util.concurrent.Executors;
import java.util.logging.Logger;

import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.AdminCommandContext;
import org.glassfish.api.admin.CommandException;
import org.glassfish.api.admin.CommandRunner;
import org.glassfish.api.admin.CommandRunner.CommandInvocation;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.api.admin.ProgressStatus;

import com.sun.enterprise.admin.remote.RemoteRestAdminCommand;
import com.sun.enterprise.config.serverbeans.Cluster;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.v3.admin.adapter.AdminEndpointDecider;
import static java.lang.Math.min;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.FINE;
import static org.glassfish.api.ActionReport.ExitCode.*;

/*
* ClusterCommandHelper is a helper class that knows how to execute an
Expand All @@ -92,6 +85,7 @@ public class ClusterCommandHelper {
private final CommandRunner runner;

private ProgressStatus progress;
private long adminTimeout;

/**
* Construct a ClusterCommandHelper
Expand All @@ -102,6 +96,7 @@ public class ClusterCommandHelper {
public ClusterCommandHelper(Domain domain, CommandRunner runner) {
this.domain = domain;
this.runner = runner;
this.adminTimeout = RemoteRestAdminCommand.getReadTimeout();
}

/**
Expand Down Expand Up @@ -241,7 +236,7 @@ public ActionReport runCommand(String command, ParameterMap map, String targetNa

// Make sure we don't wait longer than the admin read timeout. Set
// our limit to be 3 seconds less.
long adminTimeout = RemoteRestAdminCommand.getReadTimeout() - 3000;
adminTimeout = adminTimeout - 3000;
if (adminTimeout <= 0) {
// This should never be the case
adminTimeout = 57 * 1000;
Expand Down Expand Up @@ -436,4 +431,12 @@ public static class ReportResult {
public final List<String> succeededServerNames = new ArrayList<>();
public final List<String> failedServerNames = new ArrayList<>();
}

/**
* Set the timeout for ClusterCommandHelper
* @param adminTimeout in milliseconds
*/
public void setAdminTimeout(long adminTimeout) {
this.adminTimeout = adminTimeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# only if the new code is made subject to such option by the copyright
# holder.
#
## Portions Copyright [2018-2019] [Payara Foundation and/or its affiliates]
## Portions Copyright [2018-2022] [Payara Foundation and/or its affiliates]

#####restart-instance
restart.instance.notInstance=-_restart-instance only works on instances. This is a {0}
Expand Down Expand Up @@ -152,6 +152,7 @@ start.dg.command=Start a deployment group
start.dg=Starting deployment group {0}
stop.dg=Stopping deployment group {0}
restart.dg=Restarting deployment group {0}
restart.dg.timeout=Timed out while waiting for deployment group {0} to restart
## StartClusterCommand
start.cluster.command=Start a cluster
start.cluster=Starting cluster {0}
Expand Down
Loading

0 comments on commit bbb601f

Please sign in to comment.