use of com.sun.enterprise.util.cluster.SyncRequest in project Payara by payara.
the class ExportSyncBundle method execute.
@Override
public void execute(AdminCommandContext context) {
report = context.getActionReport();
report.setActionExitCode(SUCCESS);
logger = context.getLogger();
// We use our own private payload. Don't use the one in the context!
payload = PayloadImpl.Outbound.newInstance();
try {
if (!isValid()) {
return;
}
if (!setSyncBundleExportFile()) {
return;
}
syncRequest = new SyncRequest();
syncRequest.instance = clusterInstance;
if (!sync()) {
return;
}
// write to the das or temp file
write();
// all OK...download local file
if (isRetrieve) {
pumpItOut(context);
}
} catch (Exception e) {
setError(Strings.get(SYNC_FAIL, e.toString()));
logger.log(SEVERE, Strings.get(SYNC_FAIL, e.toString()), e);
}
}
use of com.sun.enterprise.util.cluster.SyncRequest in project Payara by payara.
the class SynchronizeInstanceCommand method synchronizeInstance.
/**
* Synchronize this server instance. Return true if server is synchronized.
* Return false if synchronization failed, but no files were changed
* (meaning that it is ok to bring the server up).
* Throw a CommandException if synchronization failed in such a way that
* instance startup should not be attempted.
*/
protected boolean synchronizeInstance() throws CommandException {
File dasProperties = getServerDirs().getDasPropertiesFile();
if (logger.isLoggable(Level.FINER))
logger.finer("das.properties: " + dasProperties);
if (!dasProperties.exists()) {
logger.info(Strings.get("Sync.noDASConfigured", dasProperties.toString()));
return false;
}
setDasDefaults(dasProperties);
/*
* Create the remote command object that we'll reuse for each request.
*/
/*
* Because we reuse the command, we also need to reuse the auth token
* (if one is present).
*/
final String origAuthToken = programOpts.getAuthToken();
if (origAuthToken != null) {
programOpts.setAuthToken(AuthTokenManager.markTokenForReuse(origAuthToken));
}
syncCmd = new RemoteCLICommand("_synchronize-files", programOpts, env);
syncCmd.setFileOutputDirectory(instanceDir);
/*
* The sync state file records the fact that we're in the middle
* of a synchronization attempt. When we're done, we remove it.
* If we crash, it will be left behind, and the next sync attempt
* will notice it and force a full sync.
*/
File syncState = new File(instanceDir, SYNC_STATE_FILE);
boolean doFullSync = false;
if (sync.equals("normal") && syncState.exists()) {
String lastSync = DateFormat.getDateTimeInstance().format(new Date(syncState.lastModified()));
logger.info(Strings.get("Sync.fullRequired", lastSync));
doFullSync = true;
}
/*
* Create the sync state file to indicate that
* we've started synchronization. If the file
* already exists (e.g., from a previous failed
* synchronization attempt), that's fine.
*/
try {
syncState.createNewFile();
} catch (IOException ex) {
logger.warning(Strings.get("Sync.cantCreateSyncState", syncState));
}
/*
* If --sync full, we remove all local state related to the instance,
* then do a sync. We only remove the local directories that are
* synchronized from the DAS; any other local directories (logs,
* instance-private state) are left alone.
*/
if (sync.equals("full") || doFullSync) {
if (logger.isLoggable(Level.FINE))
logger.fine(Strings.get("Instance.fullsync", instanceName));
removeSubdirectory("config");
removeSubdirectory("applications");
removeSubdirectory("generated");
removeSubdirectory("lib");
removeSubdirectory("docroot");
removeSubdirectory("endpoints");
}
File domainXml = new File(new File(instanceDir, "config"), "domain.xml");
long dtime = domainXml.exists() ? domainXml.lastModified() : -1;
File docroot = new File(instanceDir, "docroot");
File endpoints = new File(instanceDir, "endpoints");
CommandException exc = null;
try {
/*
* First, synchronize the config directory.
*/
SyncRequest sr = getModTimes("config", SyncLevel.FILES);
synchronizeFiles(sr);
/*
* Was domain.xml updated?
* If not, we're all done.
*/
if (domainXml.lastModified() == dtime) {
if (logger.isLoggable(Level.FINE))
logger.fine(Strings.get("Sync.alreadySynced"));
if (!syncState.delete())
logger.warning(Strings.get("Sync.cantDeleteSyncState", syncState));
/*
* Note that we earlier marked the token for reuse. It's OK
* to return immediately here with the DAS still willing to
* accept the same token again. The token will expire and be
* cleaned up in a little while and it was never exposed in a
* way that could be intercepted and used illicitly.
*/
return true;
}
/*
* Now synchronize the applications.
*/
sr = getModTimes("applications", SyncLevel.DIRECTORY);
synchronizeFiles(sr);
/*
* Did we get any archive files? If so,
* have to unzip them in the applications
* directory.
*/
File appsDir = new File(instanceDir, "applications");
File archiveDir = new File(appsDir, "__internal");
for (File adir : FileUtils.listFiles(archiveDir)) {
File[] af = FileUtils.listFiles(adir);
if (af.length != 1) {
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "IGNORING {0}, # files {1}", new Object[] { adir, af.length });
continue;
}
File archive = af[0];
File appDir = new File(appsDir, adir.getName());
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "UNZIP {0} TO {1}", new Object[] { archive, appDir });
try {
expand(appDir, archive);
} catch (Exception ex) {
}
}
FileUtils.whack(archiveDir);
/*
* Next, the libraries.
* We assume there's usually very few files in the
* "lib" directory so we check them all individually.
*/
sr = getModTimes("lib", SyncLevel.RECURSIVE);
synchronizeFiles(sr);
/*
* Next, the docroot.
* The docroot could be full of files, so we only check
* one level.
*/
sr = getModTimes("docroot", SyncLevel.DIRECTORY);
synchronizeFiles(sr);
/*
* Next, the endpoints.
* The endpoints too could be full of files, so we only check
* one level.
*/
sr = getModTimes("endpoints", SyncLevel.DIRECTORY);
synchronizeFiles(sr);
/*
* Check any subdirectories of the instance config directory.
* We only expect one - the config-specific directory,
* but since we don't have an easy way of knowing the
* name of that directory, we include them all. The
* DAS will tell us to remove anything that shouldn't
* be there.
*/
sr = new SyncRequest();
sr.instance = instanceName;
sr.dir = "config-specific";
File configDir = new File(instanceDir, "config");
for (File f : configDir.listFiles()) {
if (!f.isDirectory())
continue;
getFileModTimes(f, configDir, sr, SyncLevel.DIRECTORY);
}
/*
* Before sending the last sync request revert to using the original
* auth token, if one is present. The token would be retired
* later when it expires anyway, but this is just a little cleaner.
*/
if (origAuthToken != null) {
syncCmd.getProgramOptions().setAuthToken(origAuthToken);
}
synchronizeFiles(sr);
} catch (ConnectException cex) {
if (logger.isLoggable(Level.FINER))
logger.finer("Couldn't connect to DAS: " + cex);
/*
* Don't chain the exception, otherwise asadmin will think it
* it was a connect failure and will list the closest matching
* local command. Not what we want here.
*/
exc = new CommandException(Strings.get("Sync.connectFailed", cex.getMessage()));
} catch (CommandException ex) {
if (logger.isLoggable(Level.FINER))
logger.finer("Exception during synchronization: " + ex);
exc = ex;
}
if (exc != null) {
/*
* Some unexpected failure. If the domain.xml hasn't
* changed, assume no local state has changed and it's safe
* to remove the sync state file. Otherwise, something has
* changed, and we don't know how much has changed, so leave
* the sync state file so we'll do a full sync the next time.
* If nothing has changed, allow the server to come up.
*/
if (domainXml.exists() && domainXml.lastModified() == dtime && docroot.isDirectory() && endpoints.isDirectory()) {
// nothing changed and sync has completed at least once
if (!syncState.delete())
logger.warning(Strings.get("Sync.cantDeleteSyncState", syncState));
return false;
}
throw exc;
}
/*
* Success! Remove sync state file.
*/
if (!syncState.delete())
logger.warning(Strings.get("Sync.cantDeleteSyncState", syncState));
return true;
}
use of com.sun.enterprise.util.cluster.SyncRequest in project Payara by payara.
the class SynchronizeFiles method execute.
@Override
public void execute(AdminCommandContext context) {
ActionReport report = context.getActionReport();
logger = context.getLogger();
SyncRequest sr = null;
try {
// read the input document
JAXBContext jc = JAXBContext.newInstance(SyncRequest.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
// XXX - needed?
unmarshaller.setSchema(null);
sr = (SyncRequest) unmarshaller.unmarshal(fileList);
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "SynchronizeFiles: synchronize dir {0}", sr.dir);
}
} catch (Exception ex) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("SynchronizeFiles: Exception reading request");
logger.fine(ex.toString());
}
report.setActionExitCode(ExitCode.FAILURE);
report.setMessage(strings.getLocalString("sync.exception.reading", "SynchronizeFiles: Exception reading request"));
report.setFailureCause(ex);
return;
}
try {
// verify the server instance is valid
Server server = null;
if (servers != null)
server = servers.getServer(sr.instance);
if (server == null) {
report.setActionExitCode(ExitCode.FAILURE);
report.setMessage(strings.getLocalString("sync.unknown.instance", "Unknown server instance: {0}", sr.instance));
return;
}
sync.synchronize(server, sr, context.getOutboundPayload(), report, logger);
stateService.setState(server.getName(), InstanceState.StateType.NO_RESPONSE, true);
stateService.removeFailedCommandsForInstance(server.getName());
} catch (Exception ex) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("SynchronizeFiles: Exception processing request");
logger.fine(ex.toString());
}
report.setActionExitCode(ExitCode.FAILURE);
report.setMessage(strings.getLocalString("sync.exception.processing", "SynchronizeFiles: Exception processing request"));
report.setFailureCause(ex);
}
}
use of com.sun.enterprise.util.cluster.SyncRequest in project Payara by payara.
the class SynchronizeInstanceCommand method getModTimes.
/**
* Return a SyncRequest with the mod times for all the
* files in the specified directory.
*/
private SyncRequest getModTimes(String dir, SyncLevel level) {
SyncRequest sr = new SyncRequest();
sr.instance = instanceName;
sr.dir = dir;
File fdir = new File(instanceDir, dir);
if (!fdir.exists())
return sr;
getFileModTimes(fdir, fdir, sr, level);
return sr;
}
Aggregations