Skip to content

Commit

Permalink
GERONIMO-5152 refactor what is supplied for recovery attempts to prov…
Browse files Browse the repository at this point in the history
…ide structure for retry. Also a bunch of genericization

git-svn-id: https://svn.apache.org/repos/asf/geronimo/components/txmanager/trunk@911974 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
djencks committed Feb 19, 2010
1 parent dba8916 commit e6ba70e
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 122 deletions.
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


package org.apache.geronimo.connector;

import javax.resource.ResourceException;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.ResourceAdapter;
import javax.transaction.SystemException;
import javax.transaction.xa.XAResource;
import org.apache.geronimo.transaction.manager.NamedXAResource;
import org.apache.geronimo.transaction.manager.NamedXAResourceFactory;
import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;

/**
* @version $Rev$ $Date$
*/
public class ActivationSpecNamedXAResourceFactory implements NamedXAResourceFactory {

private final String name;
private final ActivationSpec activationSpec;
private final ResourceAdapter resourceAdapter;

public ActivationSpecNamedXAResourceFactory(String name, ActivationSpec activationSpec, ResourceAdapter resourceAdapter) {
this.name = name;
this.activationSpec = activationSpec;
this.resourceAdapter = resourceAdapter;
}

public String getName() {
return name;
}

public NamedXAResource getNamedXAResource() throws SystemException {
try {
XAResource[] xaResources = resourceAdapter.getXAResources(new ActivationSpec[]{activationSpec});
if (xaResources == null || xaResources.length == 0) {
return null;
}
return new WrapperNamedXAResource(xaResources[0], name);
} catch (ResourceException e) {
throw (SystemException) new SystemException("Could not get XAResource for recovery for mdb: " + name).initCause(e);
}
}

public void returnNamedXAResource(NamedXAResource namedXAResource) {
// nothing to do AFAICT
}
}
Expand Up @@ -101,6 +101,7 @@ public void activate(final MessageEndpointFactory messageEndpointFactory) throws
public void deactivate(final MessageEndpointFactory messageEndpointFactory) {
ResourceAdapter resourceAdapter = activationSpec.getResourceAdapter();
if (resourceAdapter != null) {
resourceAdapterWrapper.deregisterRecovery(containerId);
resourceAdapterWrapper.endpointDeactivation(messageEndpointFactory, activationSpec);
} else {
//this should never happen, activation spec should have been registered with r.a.
Expand Down
Expand Up @@ -30,6 +30,7 @@
import javax.transaction.xa.XAResource;

import org.apache.geronimo.transaction.manager.NamedXAResource;
import org.apache.geronimo.transaction.manager.NamedXAResourceFactory;
import org.apache.geronimo.transaction.manager.RecoverableTransactionManager;
import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;

Expand Down Expand Up @@ -124,17 +125,11 @@ public void endpointActivation(final MessageEndpointFactory messageEndpointFacto
}

public void doRecovery(ActivationSpec activationSpec, String containerId) {
try {
XAResource[] xaResources = getXAResources(new ActivationSpec[]{activationSpec});
if (xaResources == null || xaResources.length == 0) {
return;
}
NamedXAResource xaResource = new WrapperNamedXAResource(xaResources[0], containerId);
transactionManager.recoverResourceManager(xaResource);
} catch (ResourceException e) {
transactionManager.recoveryError((SystemException) new SystemException("Could not get XAResource for recovery for mdb: " + containerId).initCause(e));
}
transactionManager.registerNamedXAResourceFactory(new ActivationSpecNamedXAResourceFactory(containerId, activationSpec, resourceAdapter));
}

public void deregisterRecovery(String containerId) {
transactionManager.unregisterNamedXAResourceFactory(containerId);
}

public void endpointDeactivation(final MessageEndpointFactory messageEndpointFactory, final ActivationSpec activationSpec) {
Expand Down
Expand Up @@ -26,6 +26,7 @@

import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport;
import org.apache.geronimo.transaction.manager.NamedXAResource;
import org.apache.geronimo.transaction.manager.NamedXAResourceFactory;
import org.apache.geronimo.transaction.manager.RecoverableTransactionManager;

/**
Expand All @@ -34,16 +35,18 @@
public abstract class AbstractConnectionManager implements ConnectionManagerContainer, ConnectionManager, LazyAssociatableConnectionManager, PoolingAttributes {
protected final Interceptors interceptors;
private final RecoverableTransactionManager transactionManager;
private final String name;

//default constructor for use as endpoint
public AbstractConnectionManager() {
interceptors = null;
transactionManager = null;
}
// public AbstractConnectionManager() {
// interceptors = null;
// transactionManager = null;
// }

public AbstractConnectionManager(Interceptors interceptors, RecoverableTransactionManager transactionManager) {
public AbstractConnectionManager(Interceptors interceptors, RecoverableTransactionManager transactionManager, String name) {
this.interceptors = interceptors;
this.transactionManager = transactionManager;
this.name = name;
}

public Object createConnectionFactory(ManagedConnectionFactory mcf) throws ResourceException {
Expand All @@ -53,26 +56,12 @@ public Object createConnectionFactory(ManagedConnectionFactory mcf) throws Resou
protected ConnectionManager getConnectionManager() {
return this;
}

public void doRecovery(ManagedConnectionFactory managedConnectionFactory) {
try {
if (!getIsRecoverable()) {
return;
}
ManagedConnectionInfo mci = new ManagedConnectionInfo(managedConnectionFactory, null);

ConnectionInfo recoveryConnectionInfo = new ConnectionInfo(mci);
getRecoveryStack().getConnection(recoveryConnectionInfo);

// For pooled resources, we may now have a new MCI (not the one constructed above). Make sure we use the correct MCI
NamedXAResource xaResource = (NamedXAResource) recoveryConnectionInfo.getManagedConnectionInfo().getXAResource();
if (xaResource != null) {
transactionManager.recoverResourceManager(xaResource);
getRecoveryStack().returnConnection(recoveryConnectionInfo, ConnectionReturnAction.DESTROY);
}
} catch (ResourceException e) {
transactionManager.recoveryError((SystemException)new SystemException("Could not obtain recovery XAResource for managedConnectionFactory " + managedConnectionFactory).initCause(e));
if (!getIsRecoverable()) {
return;
}
transactionManager.registerNamedXAResourceFactory(new OutboundNamedXAResourceFactory(name, getRecoveryStack(), managedConnectionFactory));
}

/**
Expand Down Expand Up @@ -196,10 +185,12 @@ public void doStart() throws Exception {
}

public void doStop() throws Exception {
transactionManager.unregisterNamedXAResourceFactory(name);
interceptors.getStack().destroy();
}

public void doFail() {
transactionManager.unregisterNamedXAResourceFactory(name);
interceptors.getStack().destroy();
}
}
Expand Up @@ -37,9 +37,9 @@ public class GenericConnectionManager extends AbstractConnectionManager {
protected static final Logger log = LoggerFactory.getLogger(AbstractSinglePoolConnectionInterceptor.class);

//default constructor for use as endpoint
public GenericConnectionManager() {
super();
}
// public GenericConnectionManager() {
// super();
// }

/**
*
Expand All @@ -58,7 +58,7 @@ public GenericConnectionManager(TransactionSupport transactionSupport,
RecoverableTransactionManager transactionManager,
String name,
ClassLoader classLoader) {
super(new InterceptorsImpl(transactionSupport, pooling, subjectSource, name, connectionTracker, transactionManager, classLoader), transactionManager);
super(new InterceptorsImpl(transactionSupport, pooling, subjectSource, name, connectionTracker, transactionManager, classLoader), transactionManager, name);
}

private static class InterceptorsImpl implements AbstractConnectionManager.Interceptors {
Expand Down
@@ -0,0 +1,128 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


package org.apache.geronimo.connector.outbound;

import javax.resource.ResourceException;
import javax.resource.spi.ManagedConnectionFactory;
import javax.transaction.SystemException;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.apache.geronimo.transaction.manager.NamedXAResource;
import org.apache.geronimo.transaction.manager.NamedXAResourceFactory;

/**
* @version $Rev$ $Date$
*/
public class OutboundNamedXAResourceFactory implements NamedXAResourceFactory {

private final String name;
private final ConnectionInterceptor recoveryStack;
private final ManagedConnectionFactory managedConnectionFactory;

public OutboundNamedXAResourceFactory(String name, ConnectionInterceptor recoveryStack, ManagedConnectionFactory managedConnectionFactory) {
this.name = name;
this.recoveryStack = recoveryStack;
this.managedConnectionFactory = managedConnectionFactory;
}

public String getName() {
return name;
}

public NamedXAResource getNamedXAResource() throws SystemException {
try {
ManagedConnectionInfo mci = new ManagedConnectionInfo(managedConnectionFactory, null);

ConnectionInfo recoveryConnectionInfo = new ConnectionInfo(mci);
recoveryStack.getConnection(recoveryConnectionInfo);

// For pooled resources, we may now have a new MCI (not the one constructed above). Make sure we use the correct MCI
return new NamedXAResourceWithConnectioninfo((NamedXAResource) recoveryConnectionInfo.getManagedConnectionInfo().getXAResource(), recoveryConnectionInfo);
} catch (ResourceException e) {
throw (SystemException) new SystemException("Could not get XAResource for recovery for mcf: " + name).initCause(e);
}
}

public void returnNamedXAResource(NamedXAResource namedXAResource) {
NamedXAResourceWithConnectioninfo xares = (NamedXAResourceWithConnectioninfo) namedXAResource;
recoveryStack.returnConnection(xares.getConnectionInfo(), ConnectionReturnAction.DESTROY);
}

private static class NamedXAResourceWithConnectioninfo implements NamedXAResource {

private final NamedXAResource delegate;
private final ConnectionInfo connectionInfo;

private NamedXAResourceWithConnectioninfo(NamedXAResource delegate, ConnectionInfo connectionInfo) {
this.delegate = delegate;
this.connectionInfo = connectionInfo;
}

public ConnectionInfo getConnectionInfo() {
return connectionInfo;
}

public String getName() {
return delegate.getName();
}

public void commit(Xid xid, boolean b) throws XAException {
delegate.commit(xid, b);
}

public void end(Xid xid, int i) throws XAException {
delegate.end(xid, i);
}

public void forget(Xid xid) throws XAException {
delegate.forget(xid);
}

public int getTransactionTimeout() throws XAException {
return delegate.getTransactionTimeout();
}

public boolean isSameRM(XAResource xaResource) throws XAException {
return delegate.isSameRM(xaResource);
}

public int prepare(Xid xid) throws XAException {
return delegate.prepare(xid);
}

public Xid[] recover(int i) throws XAException {
return delegate.recover(i);
}

public void rollback(Xid xid) throws XAException {
delegate.rollback(xid);
}

public boolean setTransactionTimeout(int i) throws XAException {
return delegate.setTransactionTimeout(i);
}

public void start(Xid xid, int i) throws XAException {
delegate.start(xid, i);
}
}
}
Expand Up @@ -67,7 +67,7 @@ public class HOWLLog implements TransactionLog {
private final XALogger logger;
private final Configuration configuration = new Configuration();
private boolean started = false;
private HashMap recovered;
private HashMap<Xid, Recovery.XidBranchesPair> recovered;

public HOWLLog(String bufferClassName,
int bufferSize,
Expand Down Expand Up @@ -219,7 +219,7 @@ public void doStart() throws Exception {
started = true;
setLogFileDir(logFileDir);
log.debug("Initiating transaction manager recovery");
recovered = new HashMap();
recovered = new HashMap<Xid, Recovery.XidBranchesPair>();

logger.open(null);

Expand All @@ -241,15 +241,14 @@ public void doFail() {
public void begin(Xid xid) throws LogException {
}

public Object prepare(Xid xid, List branches) throws LogException {
public Object prepare(Xid xid, List<TransactionBranchInfo> branches) throws LogException {
int branchCount = branches.size();
byte[][] data = new byte[3 + 2 * branchCount][];
data[0] = intToBytes(xid.getFormatId());
data[1] = xid.getGlobalTransactionId();
data[2] = xid.getBranchQualifier();
int i = 3;
for (Iterator iterator = branches.iterator(); iterator.hasNext();) {
TransactionBranchInfo transactionBranchInfo = (TransactionBranchInfo) iterator.next();
for (TransactionBranchInfo transactionBranchInfo : branches) {
data[i++] = transactionBranchInfo.getBranchXid().getBranchQualifier();
data[i++] = transactionBranchInfo.getResourceName().getBytes();
}
Expand Down Expand Up @@ -315,9 +314,9 @@ public void rollback(Xid xid, Object logMark) throws LogException {
}
}

public Collection recover(XidFactory xidFactory) throws LogException {
public Collection<Recovery.XidBranchesPair> recover(XidFactory xidFactory) throws LogException {
log.debug("Initiating transaction manager recovery");
Map recovered = new HashMap();
Map<Xid, Recovery.XidBranchesPair> recovered = new HashMap<Xid, Recovery.XidBranchesPair>();
ReplayListener replayListener = new GeronimoReplayListener(xidFactory, recovered);
logger.replayActiveTx(replayListener);
log.debug("In doubt transactions recovered from log");
Expand Down Expand Up @@ -352,9 +351,9 @@ private int bytesToInt(byte[] buffer) {
private class GeronimoReplayListener implements ReplayListener {

private final XidFactory xidFactory;
private final Map recoveredTx;
private final Map<Xid, Recovery.XidBranchesPair> recoveredTx;

public GeronimoReplayListener(XidFactory xidFactory, Map recoveredTx) {
public GeronimoReplayListener(XidFactory xidFactory, Map<Xid, Recovery.XidBranchesPair> recoveredTx) {
this.xidFactory = xidFactory;
this.recoveredTx = recoveredTx;
}
Expand Down

0 comments on commit e6ba70e

Please sign in to comment.