use of org.apache.geronimo.transaction.manager.XidFactory in project tomee by apache.
the class GeronimoTransactionManagerFactory method create.
public static // Deprecated, use defaultTransactionTimeout
GeronimoTransactionManager create(// Deprecated, use defaultTransactionTimeout
Integer defaultTransactionTimeoutSeconds, final Duration defaultTransactionTimeout, final boolean txRecovery, final byte[] tmId, final String bufferClassName, final int bufferSizeKb, final boolean checksumEnabled, final boolean adler32Checksum, // Deprecated, use flushSleepTime
Integer flushSleepTimeMilliseconds, final Duration flushSleepTime, final String logFileDir, final String logFileExt, final String logFileName, final int maxBlocksPerFile, final int maxBuffers, final int maxLogFiles, final int minBuffers, final int threadsWaitingForceThreshold) throws Exception {
if (flushSleepTime.getUnit() == null) {
flushSleepTime.setUnit(TimeUnit.MILLISECONDS);
}
if (flushSleepTimeMilliseconds == null) {
flushSleepTimeMilliseconds = (int) TimeUnit.MILLISECONDS.convert(flushSleepTime.getTime(), flushSleepTime.getUnit());
}
if (defaultTransactionTimeout.getUnit() == null) {
defaultTransactionTimeout.setUnit(TimeUnit.SECONDS);
}
if (defaultTransactionTimeoutSeconds == null) {
defaultTransactionTimeoutSeconds = (int) TimeUnit.SECONDS.convert(defaultTransactionTimeout.getTime(), defaultTransactionTimeout.getUnit());
}
XidFactory xidFactory = null;
TransactionLog txLog = null;
if (txRecovery) {
SystemInstance.get().setComponent(XAResourceWrapper.class, new GeronimoXAResourceWrapper());
xidFactory = new XidFactoryImpl(tmId == null ? DEFAULT_TM_ID : tmId);
txLog = new HOWLLog(bufferClassName == null ? "org.objectweb.howl.log.BlockLogBuffer" : bufferClassName, bufferSizeKb == 0 ? DEFAULT_BUFFER_SIZE : bufferSizeKb, checksumEnabled, adler32Checksum, flushSleepTimeMilliseconds, logFileDir, logFileExt, logFileName, maxBlocksPerFile, maxBuffers, maxLogFiles, minBuffers, threadsWaitingForceThreshold, xidFactory, SystemInstance.get().getBase().getDirectory("."));
((HOWLLog) txLog).doStart();
}
final GeronimoTransactionManager geronimoTransactionManager = new DestroyableTransactionManager(defaultTransactionTimeoutSeconds, xidFactory, txLog);
final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management").set("j2eeType", "TransactionManager");
LocalMBeanServer.registerDynamicWrapperSilently(new TransactionManagerMBean(geronimoTransactionManager, defaultTransactionTimeout, txLog), jmxName.build());
return geronimoTransactionManager;
}
use of org.apache.geronimo.transaction.manager.XidFactory in project aries by apache.
the class TransactionLogUtils method copyActiveTransactions.
/**
* <p>When <code>org.apache.aries.transaction</code> PID changes, there may be a need to copy
* entries from transaction log when some important configuration changed (like block size)</p>
* @param oldConfiguration previous configuration when configuration changed, may be <code>null</code> when starting bundle
* @param newConfiguration configuration to create new transaction manager
* @return <code>true</code> if there was conversion performed
*/
public static boolean copyActiveTransactions(Dictionary<String, Object> oldConfiguration, Dictionary<String, ?> newConfiguration) throws ConfigurationException, IOException {
if (oldConfiguration == null) {
oldConfiguration = new Hashtable<String, Object>();
}
if (oldConfiguration.get(HOWL_LOG_FILE_DIR) == null) {
// we will be adjusting oldConfiguration to be able to create "old HOWLLog"
oldConfiguration.put(HOWL_LOG_FILE_DIR, newConfiguration.get(HOWL_LOG_FILE_DIR));
}
String oldLogDirectory = (String) oldConfiguration.get(HOWL_LOG_FILE_DIR);
String newLogDirectory = (String) newConfiguration.get(HOWL_LOG_FILE_DIR);
if (newLogDirectory == null || oldLogDirectory == null) {
// handle with exceptions at TM creation time
return false;
}
File oldDir = new File(oldLogDirectory);
oldLogDirectory = oldDir.getAbsolutePath();
File newDir = new File(newLogDirectory);
newLogDirectory = newDir.getAbsolutePath();
// a file which may tell us what's the previous configuation
File transaction_1 = null;
if (!oldDir.equals(newDir)) {
// recent logs are in oldDir, so even if newDir contains some logs, we will remove them
deleteDirectory(newDir);
transaction_1 = new File(oldDir, configuredTransactionLogName(oldConfiguration, 1));
} else {
// we may need to move oldDir to some temporary location, if the configuration is changed
// we'll then have to copy old tx log to new one
transaction_1 = new File(oldDir, configuredTransactionLogName(oldConfiguration, 1));
if (!transaction_1.exists() || transaction_1.length() == 0L) {
oldConfiguration.put(HOWL_LOG_FILE_NAME, getString(newConfiguration, HOWL_LOG_FILE_NAME, "transaction"));
oldConfiguration.put(HOWL_LOG_FILE_EXT, getString(newConfiguration, HOWL_LOG_FILE_EXT, "log"));
transaction_1 = new File(oldDir, configuredTransactionLogName(newConfiguration, 1));
}
}
if (!transaction_1.exists() || transaction_1.length() == 0L) {
// no need to copy anything
return false;
}
BaseTxLogConfig oldTxConfig = transactionLogFileConfig(transaction_1);
BaseTxLogConfig newTxConfig = transactionLogFileConfig(newConfiguration);
if (oldTxConfig == null || oldTxConfig.equals(newTxConfig)) {
// old log files compatible, but maybe we have to copy them
if (!oldDir.equals(newDir)) {
if (!oldDir.renameTo(newDir)) {
log.warn("Can't backup old transaction logs directory: {}", oldDir.getAbsolutePath());
return false;
}
}
// files are compatible - we'll check one more thing - name_N.extension
String oldName = configuredTransactionLogName(oldConfiguration, 1);
String newName = configuredTransactionLogName(newConfiguration, 1);
if (!oldName.equals(newName)) {
final Dictionary<String, Object> finalOldConfiguration = oldConfiguration;
final Dictionary<String, ?> finalNewConfiguration = newConfiguration;
final Map<String, String> changes = new HashMap<String, String>();
newDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
Matcher matcher = TX_FILE_NAME.matcher(name);
if (matcher.matches()) {
if (matcher.group(1).equals(getString(finalOldConfiguration, HOWL_LOG_FILE_NAME, "transaction")) && matcher.group(3).equals(getString(finalOldConfiguration, HOWL_LOG_FILE_EXT, "log"))) {
changes.put(name, String.format("%s_%d.%s", getString(finalNewConfiguration, HOWL_LOG_FILE_NAME, "transaction"), Integer.parseInt(matcher.group(2)), getString(finalNewConfiguration, HOWL_LOG_FILE_EXT, "log")));
}
}
return false;
}
});
for (String old : changes.keySet()) {
new File(newDir, old).renameTo(new File(newDir, changes.get(old)));
}
return true;
}
return false;
}
File backupDir = null;
if (oldDir.equals(newDir)) {
// move old dir to backup dir
backupDir = new File(newLogDirectory + String.format("-%016x", System.currentTimeMillis()));
if (!oldDir.renameTo(backupDir)) {
log.warn("Can't backup old transaction logs directory: {}", oldDir.getAbsolutePath());
return false;
}
oldConfiguration = copy(oldConfiguration);
oldConfiguration.put(HOWL_LOG_FILE_DIR, backupDir.getAbsolutePath());
}
log.info("Copying transaction log from {} to {}", oldDir.getAbsolutePath(), newDir.getAbsolutePath());
oldConfiguration.put(RECOVERABLE, newConfiguration.get(RECOVERABLE));
oldConfiguration.put(HOWL_MAX_LOG_FILES, Integer.toString(oldTxConfig.maxLogFiles));
oldConfiguration.put(HOWL_MAX_BLOCKS_PER_FILE, Integer.toString(oldTxConfig.maxBlocksPerFile));
oldConfiguration.put(HOWL_BUFFER_SIZE, Integer.toString(oldTxConfig.bufferSizeKBytes));
String tmid1 = TransactionManagerService.getString(oldConfiguration, TMID, Activator.PID);
XidFactory xidFactory1 = new XidFactoryImpl(tmid1.substring(0, Math.min(tmid1.length(), 64)).getBytes());
String tmid2 = TransactionManagerService.getString(newConfiguration, TMID, Activator.PID);
XidFactory xidFactory2 = new XidFactoryImpl(tmid2.substring(0, Math.min(tmid2.length(), 64)).getBytes());
org.apache.geronimo.transaction.manager.TransactionLog oldLog = null;
org.apache.geronimo.transaction.manager.TransactionLog newLog = null;
try {
oldLog = TransactionManagerService.createTransactionLog(oldConfiguration, xidFactory1);
newLog = TransactionManagerService.createTransactionLog(newConfiguration, xidFactory2);
if (!(oldLog instanceof HOWLLog)) {
log.info("TransactionLog {} is not recoverable", oldLogDirectory);
return false;
}
if (!(newLog instanceof HOWLLog)) {
log.info("TransactionLog {} is not recoverable", newLogDirectory);
return false;
}
HOWLLog from = (HOWLLog) oldLog;
HOWLLog to = (HOWLLog) newLog;
Collection<Recovery.XidBranchesPair> pairs = from.recover(xidFactory1);
for (Recovery.XidBranchesPair xidBranchesPair : pairs) {
log.info("Copying active transaction with XID {}", xidBranchesPair.getXid());
for (TransactionBranchInfo branchInfo : xidBranchesPair.getBranches()) {
log.info("- Copying branch {} for resource {}", branchInfo.getBranchXid(), branchInfo.getResourceName());
}
to.prepare(xidBranchesPair.getXid(), new ArrayList<TransactionBranchInfo>(xidBranchesPair.getBranches()));
}
log.info("Migration of active transactions finished");
deleteDirectory(backupDir);
return !pairs.isEmpty();
} catch (Exception e) {
log.error("An exception occurred while trying to migrate transaction log after changing configuration.", e);
if (backupDir != null) {
deleteDirectory(newDir);
backupDir.renameTo(oldDir);
}
return false;
} finally {
try {
if (oldLog instanceof HOWLLog) {
((HOWLLog) oldLog).doStop();
}
if (newLog instanceof HOWLLog) {
((HOWLLog) newLog).doStop();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
use of org.apache.geronimo.transaction.manager.XidFactory in project aries by apache.
the class XidFactoryImplTest method testAriesFactory.
@Test
public void testAriesFactory() throws Exception {
XidFactory factory = new XidFactoryImpl("hi".getBytes());
Xid id1 = factory.createXid();
Xid id2 = factory.createXid();
assertFalse("Should not match new: " + id1, factory.matchesGlobalId(id1.getGlobalTransactionId()));
assertFalse("Should not match new: " + id2, factory.matchesGlobalId(id2.getGlobalTransactionId()));
Xid b_id1 = factory.createBranch(id1, 1);
Xid b_id2 = factory.createBranch(id2, 1);
assertFalse("Should not match new branch: " + b_id1, factory.matchesBranchId(b_id1.getBranchQualifier()));
assertFalse("Should not match new branch: " + b_id2, factory.matchesBranchId(b_id2.getBranchQualifier()));
Thread.sleep(5);
XidFactory factory2 = new XidFactoryImpl("hi".getBytes());
assertTrue("Should match old: " + id1, factory2.matchesGlobalId(id1.getGlobalTransactionId()));
assertTrue("Should match old: " + id2, factory2.matchesGlobalId(id2.getGlobalTransactionId()));
assertTrue("Should match old branch: " + b_id1, factory2.matchesBranchId(b_id1.getBranchQualifier()));
assertTrue("Should match old branch: " + b_id2, factory2.matchesBranchId(b_id2.getBranchQualifier()));
}
use of org.apache.geronimo.transaction.manager.XidFactory in project aries by apache.
the class LogTest method testGeronimo.
@Test
@Ignore
public void testGeronimo() throws Exception {
System.err.println("Geronimo");
XidFactory xidFactory = new XidFactoryImpl("hi".getBytes());
HOWLLog txLog = new HOWLLog("org.objectweb.howl.log.BlockLogBuffer", 4, true, true, 50, new File(".").getAbsolutePath(), "log", "geronimo", 512, 0, 2, 4, -1, true, xidFactory, null);
txLog.doStart();
GeronimoTransactionManager tm = new GeronimoTransactionManager(600, xidFactory, txLog);
XAResource xar1 = new TestXAResource("res1");
XAResource xar2 = new TestXAResource("res2");
tm.registerNamedXAResourceFactory(new TestXAResourceFactory("res1"));
tm.registerNamedXAResourceFactory(new TestXAResourceFactory("res2"));
for (int i = minThreads; i <= maxThreads; i *= 10) {
for (int j = minTxPerThread; j <= maxTxPerThread; j *= 10) {
long ms = testThroughput(tm, xar1, xar2, i, j);
System.err.println("TPS (" + i + " threads, " + j + " tx) = " + ((i * j) / (ms / 1000.0)));
}
}
txLog.doStop();
System.err.println();
System.err.flush();
}
Aggregations