Skip to content

Commit

Permalink
0001357: The Pro wrapper.log should be included in the support snapshot.
Browse files Browse the repository at this point in the history
Move Snapshot code outside of ClientSymmetricEngine to SnapshotUtil.
  • Loading branch information
abrougher committed Aug 20, 2013
1 parent 9de51f8 commit 440e114
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 198 deletions.
Expand Up @@ -21,19 +21,12 @@
package org.jumpmind.symmetric;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Properties;

Expand All @@ -42,9 +35,7 @@

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.JdbcDatabasePlatformFactory;
import org.jumpmind.db.sql.JdbcSqlTemplate;
Expand All @@ -56,20 +47,16 @@
import org.jumpmind.security.SecurityServiceFactory.SecurityServiceType;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.SystemConstants;
import org.jumpmind.symmetric.common.TableConstants;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.db.JdbcSymmetricDialectFactory;
import org.jumpmind.symmetric.ext.ExtensionPointManager;
import org.jumpmind.symmetric.ext.IExtensionPointManager;
import org.jumpmind.symmetric.io.data.DbExport;
import org.jumpmind.symmetric.io.data.DbExport.Format;
import org.jumpmind.symmetric.io.stage.IStagingManager;
import org.jumpmind.symmetric.io.stage.StagingManager;
import org.jumpmind.symmetric.job.IJobManager;
import org.jumpmind.symmetric.job.JobManager;
import org.jumpmind.symmetric.model.TriggerHistory;
import org.jumpmind.symmetric.util.SnapshotUtil;
import org.jumpmind.util.AppUtils;
import org.jumpmind.util.JarBuilder;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
Expand Down Expand Up @@ -156,7 +143,7 @@ public ClientSymmetricEngine() {
public ClientSymmetricEngine(boolean registerEngine) {
this((Properties) null, registerEngine);
}

@Override
protected SecurityServiceType getSecurityServiceType() {
return SecurityServiceType.CLIENT;
Expand Down Expand Up @@ -240,7 +227,7 @@ public static IDatabasePlatform createDatabasePlatform(TypedProperties propertie
jndiFactory.setJndiName(jndiName);
jndiFactory.afterPropertiesSet();
dataSource = (DataSource)jndiFactory.getObject();

if (dataSource == null) {
throw new SymmetricException("Could not locate the configured datasource in jndi. The jndi name is %s", jndiName);
}
Expand Down Expand Up @@ -378,201 +365,24 @@ public synchronized void destroy() {
}
}
}

public List<File> listSnapshots() {
File snapshotsDir = getSnapshotDirectory();
File snapshotsDir = SnapshotUtil.getSnapshotDirectory(this);
List<File> files = new ArrayList<File>(FileUtils.listFiles(snapshotsDir, new String[] {"zip"}, false));
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
public int compare(File o1, File o2) {
return -o1.compareTo(o2);
}
});
return files;
}

protected File getSnapshotDirectory() {
File snapshotsDir = new File(parameterService.getTempDirectory(), "snapshots");
snapshotsDir.mkdirs();
return snapshotsDir;
}


public ApplicationContext getSpringContext() {
return springContext;
}

public File snapshot() {

String dirName = getEngineName().replaceAll(" ", "-") + "-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());

File snapshotsDir = getSnapshotDirectory();

File logfile = new File(parameterService.getString(ParameterConstants.SERVER_LOG_FILE));

File tmpDir = new File(parameterService.getTempDirectory(), dirName);
tmpDir.mkdirs();

if (logfile.exists() && logfile.isFile()) {
try {
FileUtils.copyFileToDirectory(logfile, tmpDir);
} catch (IOException e) {
log.warn("Failed to copy the log file to the snapshot directory", e);
}
} else {
log.warn("Could not find {} to copy to the snapshot directory",
logfile.getAbsolutePath());
}

List<TriggerHistory> triggerHistories = triggerRouterService.getActiveTriggerHistories();
List<Table> tables = new ArrayList<Table>();
for (TriggerHistory triggerHistory : triggerHistories) {
Table table = platform.getTableFromCache(triggerHistory.getSourceCatalogName(),
triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(),
false);
if (table != null) {
tables.add(table);
}
}

FileWriter fwriter = null;
try {
fwriter = new FileWriter(new File(tmpDir, "config-export.csv"));
dataExtractorService.extractConfigurationStandalone(nodeService.findIdentity(),
fwriter, TableConstants.SYM_NODE, TableConstants.SYM_NODE_SECURITY,
TableConstants.SYM_NODE_IDENTITY, TableConstants.SYM_NODE_HOST,
TableConstants.SYM_NODE_CHANNEL_CTL);
} catch (IOException e) {
log.warn("Failed to export symmetric configuration", e);
} finally {
IOUtils.closeQuietly(fwriter);
}

FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "table-definitions.xml"));
DbExport export = new DbExport(platform);
export.setFormat(Format.XML);
export.setNoData(true);
export.exportTables(fos, tables.toArray(new Table[tables.size()]));
} catch (IOException e) {
log.warn("Failed to export table definitions", e);
} finally {
IOUtils.closeQuietly(fos);
}

fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "runtime-data.xml"));
DbExport export = new DbExport(platform);
export.setFormat(Format.XML);
export.setNoCreateInfo(true);
export.exportTables(
fos,
new String[] {
TableConstants.getTableName(getTablePrefix(), TableConstants.SYM_NODE),
TableConstants.getTableName(getTablePrefix(),
TableConstants.SYM_NODE_SECURITY),
TableConstants.getTableName(getTablePrefix(),
TableConstants.SYM_NODE_HOST),
TableConstants.getTableName(getTablePrefix(),
TableConstants.SYM_TRIGGER_HIST),
TableConstants.getTableName(getTablePrefix(), TableConstants.SYM_LOCK),
TableConstants.getTableName(getTablePrefix(), TableConstants.SYM_NODE_COMMUNICATION)});
} catch (IOException e) {
log.warn("Failed to export table definitions", e);
} finally {
IOUtils.closeQuietly(fos);
}

final int THREAD_INDENT_SPACE = 50;
fwriter = null;
try {
fwriter = new FileWriter(new File(tmpDir, "threads.txt"));
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] threadIds = threadBean.getAllThreadIds();
for (long l : threadIds) {
ThreadInfo info = threadBean.getThreadInfo(l, 100);
if (info != null) {
String threadName = info.getThreadName();
fwriter.append(StringUtils.rightPad(threadName, THREAD_INDENT_SPACE));
StackTraceElement[] trace = info.getStackTrace();
boolean first = true;
for (StackTraceElement stackTraceElement : trace) {
if (!first) {
fwriter.append(StringUtils.rightPad("", THREAD_INDENT_SPACE));
} else {
first = false;
}
fwriter.append(stackTraceElement.getClassName());
fwriter.append(".");
fwriter.append(stackTraceElement.getMethodName());
fwriter.append("()");
int lineNumber = stackTraceElement.getLineNumber();
if (lineNumber > 0) {
fwriter.append(": ");
fwriter.append(Integer.toString(stackTraceElement.getLineNumber()));
}
fwriter.append("\n");
}
fwriter.append("\n");
}
}
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fwriter);
}

fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "parameters.properties"));
Properties effectiveParameters = parameterService.getAllParameters();
effectiveParameters.store(fos, "parameters.properties");
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fos);
}

fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "runtime-stats.properties"));
Properties runtimeProperties = new Properties();
runtimeProperties.setProperty("unrouted.data.count",
Long.toString(routerService.getUnroutedDataCount()));
runtimeProperties.setProperty("outgoing.errors.count",
Long.toString(outgoingBatchService.countOutgoingBatchesInError()));
runtimeProperties.setProperty("outgoing.tosend.count",
Long.toString(outgoingBatchService.countOutgoingBatchesUnsent()));
runtimeProperties.setProperty("incoming.errors.count",
Long.toString(incomingBatchService.countIncomingBatchesInError()));
runtimeProperties.store(fos, "runtime-stats.properties");
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fos);
}

fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "system.properties"));
System.getProperties().store(fos, "system.properties");
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fos);
}

try {
File jarFile = new File(snapshotsDir, tmpDir.getName()
+ ".zip");
JarBuilder builder = new JarBuilder(tmpDir, jarFile, new File[] { tmpDir }, Version.version());
builder.build();
FileUtils.deleteDirectory(tmpDir);
return jarFile;
} catch (IOException e) {
throw new IoException("Failed to package snapshot files into archive", e);
}

return SnapshotUtil.createSnapshot(this);
}

}

0 comments on commit 440e114

Please sign in to comment.