use of org.apache.geronimo.transaction.manager.TransactionBranchInfo in project aries by apache.
the class LogConversionTest method transaction.
private void transaction(HOWLLog log, int transactionBranchCount, boolean commit) throws Exception {
Xid xid = xidFactory.createXid();
List<TransactionBranchInfo> txBranches = new LinkedList<TransactionBranchInfo>();
for (int b = 1; b <= transactionBranchCount; b++) {
// TransactionImpl.enlistResource()
Xid branchXid = xidFactory.createBranch(xid, b);
txBranches.add(new TestTransactionBranchInfo(branchXid, String.format("res-%02d", b)));
}
// org.apache.geronimo.transaction.manager.TransactionImpl.internalPrepare()
Object logMark = log.prepare(xid, txBranches);
if (commit) {
// org.apache.geronimo.transaction.manager.CommitTask.run()
log.commit(xid, logMark);
}
}
use of org.apache.geronimo.transaction.manager.TransactionBranchInfo 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);
}
}
}
Aggregations