use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class CallbackArgDUnitTest method doTest.
private void doTest() throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(getCurrentExpectedKey(), e.getKey());
assertEquals(callbackArg, e.getCallbackArgument());
assertEquals(true, e.isCallbackArgumentAvailable());
}
};
af.addCacheListener(cl1);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
r2.createSubregion("r3", af.create());
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
ArrayList keys = new ArrayList();
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
keys.add(ee.getKey());
assertEquals(callbackArg, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
CallbackArgDUnitTest.this.invokeCount = 1;
}
};
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
ctm.addListener(tl1);
this.invokeCount = 0;
this.clCount = 0;
this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
doCommitOtherVm();
assertEquals(1, this.invokeCount);
assertEquals(6, this.clCount);
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class CacheCreation method sameAs.
/**
* Returns whether or not this {@code CacheCreation} is equivalent to another {@code Cache}.
*/
public boolean sameAs(Cache other) {
boolean sameConfig = other.getLockLease() == this.getLockLease() && other.getLockTimeout() == this.getLockTimeout() && other.getSearchTimeout() == this.getSearchTimeout() && other.getMessageSyncInterval() == this.getMessageSyncInterval() && other.getCopyOnRead() == this.getCopyOnRead() && other.isServer() == this.isServer();
if (!sameConfig) {
throw new RuntimeException(LocalizedStrings.CacheCreation_SAMECONFIG.toLocalizedString());
} else {
DynamicRegionFactory.Config drc1 = this.getDynamicRegionFactoryConfig();
if (drc1 != null) {
// we have a dynamic region factory
DynamicRegionFactory.Config drc2 = null;
if (other instanceof CacheCreation) {
drc2 = ((CacheCreation) other).getDynamicRegionFactoryConfig();
} else {
drc2 = DynamicRegionFactory.get().getConfig();
}
if (drc2 == null) {
return false;
}
if (!drc1.equals(drc2)) {
return false;
}
} else {
// we have no dynamic region factory; how about other?
if (other instanceof CacheCreation) {
if (((CacheCreation) other).getDynamicRegionFactoryConfig() != null) {
return false;
}
} else {
// other must be real cache in which case we compare to DynamicRegionFactory
if (DynamicRegionFactory.get().isOpen()) {
return false;
}
}
}
Collection<CacheServer> myBridges = this.getCacheServers();
Collection<CacheServer> otherBridges = other.getCacheServers();
if (myBridges.size() != otherBridges.size()) {
throw new RuntimeException(LocalizedStrings.CacheCreation_CACHESERVERS_SIZE.toLocalizedString());
}
for (CacheServer myBridge1 : myBridges) {
CacheServerCreation myBridge = (CacheServerCreation) myBridge1;
boolean found = false;
for (CacheServer otherBridge : otherBridges) {
if (myBridge.sameAs(otherBridge)) {
found = true;
break;
}
}
if (!found) {
throw new RuntimeException(LocalizedStrings.CacheCreation_CACHE_SERVER_0_NOT_FOUND.toLocalizedString(myBridge));
}
}
// compare connection pools
Map<String, Pool> m1 = getPools();
Map<String, Pool> m2 = other instanceof CacheCreation ? ((CacheCreation) other).getPools() : PoolManager.getAll();
int m1Size = m1.size();
// ignore any gateway instances
for (Pool cp : m1.values()) {
if (((PoolImpl) cp).isUsedByGateway()) {
m1Size--;
}
}
int m2Size = m2.size();
// ignore any gateway instances
for (Pool cp : m2.values()) {
if (((PoolImpl) cp).isUsedByGateway()) {
m2Size--;
}
}
if (m2Size == 1) {
// if it is just the DEFAULT pool then ignore it
Pool p = (Pool) m2.values().iterator().next();
if (p.getName().equals("DEFAULT")) {
m2Size = 0;
}
}
if (m1Size != m2Size) {
throw new RuntimeException("pool sizes differ m1Size=" + m1Size + " m2Size=" + m2Size + " m1=" + m1.values() + " m2=" + m2.values());
}
if (m1Size > 0) {
for (Pool pool : m1.values()) {
PoolImpl poolImpl = (PoolImpl) pool;
// ignore any gateway instances
if (!poolImpl.isUsedByGateway()) {
poolImpl.sameAs(m2.get(poolImpl.getName()));
}
}
}
// compare disk stores
for (DiskStore diskStore : this.diskStores.values()) {
DiskStoreAttributesCreation dsac = (DiskStoreAttributesCreation) diskStore;
String name = dsac.getName();
DiskStore ds = other.findDiskStore(name);
if (ds == null) {
getLogger().fine("Disk store " + name + " not found.");
throw new RuntimeException(LocalizedStrings.CacheCreation_DISKSTORE_NOTFOUND_0.toLocalizedString(name));
} else {
if (!dsac.sameAs(ds)) {
getLogger().fine("Attributes for disk store " + name + " do not match");
throw new RuntimeException(LocalizedStrings.CacheCreation_ATTRIBUTES_FOR_DISKSTORE_0_DO_NOT_MATCH.toLocalizedString(name));
}
}
}
Map<String, RegionAttributes<?, ?>> myNamedAttributes = this.listRegionAttributes();
Map<String, RegionAttributes<Object, Object>> otherNamedAttributes = other.listRegionAttributes();
if (myNamedAttributes.size() != otherNamedAttributes.size()) {
throw new RuntimeException(LocalizedStrings.CacheCreation_NAMEDATTRIBUTES_SIZE.toLocalizedString());
}
for (Object object : myNamedAttributes.entrySet()) {
Entry myEntry = (Entry) object;
String myId = (String) myEntry.getKey();
Assert.assertTrue(myEntry.getValue() instanceof RegionAttributesCreation, "Entry value is a " + myEntry.getValue().getClass().getName());
RegionAttributesCreation myAttrs = (RegionAttributesCreation) myEntry.getValue();
RegionAttributes<Object, Object> otherAttrs = other.getRegionAttributes(myId);
if (otherAttrs == null) {
getLogger().fine("No attributes for " + myId);
throw new RuntimeException(LocalizedStrings.CacheCreation_NO_ATTRIBUTES_FOR_0.toLocalizedString(myId));
} else {
if (!myAttrs.sameAs(otherAttrs)) {
getLogger().fine("Attributes for " + myId + " do not match");
throw new RuntimeException(LocalizedStrings.CacheCreation_ATTRIBUTES_FOR_0_DO_NOT_MATCH.toLocalizedString(myId));
}
}
}
Collection<Region<?, ?>> myRoots = this.roots.values();
Collection<Region<?, ?>> otherRoots = other.rootRegions();
if (myRoots.size() != otherRoots.size()) {
throw new RuntimeException(LocalizedStrings.CacheCreation_ROOTS_SIZE.toLocalizedString());
}
for (final Region<?, ?> myRoot : myRoots) {
RegionCreation rootRegion = (RegionCreation) myRoot;
Region<Object, Object> otherRegion = other.getRegion(rootRegion.getName());
if (otherRegion == null) {
throw new RuntimeException(LocalizedStrings.CacheCreation_NO_ROOT_0.toLocalizedString(rootRegion.getName()));
} else if (!rootRegion.sameAs(otherRegion)) {
throw new RuntimeException(LocalizedStrings.CacheCreation_REGIONS_DIFFER.toLocalizedString());
}
}
// If both have a listener, make sure they are equal.
if (getCacheTransactionManager() != null) {
// Currently the GemFireCache always has a CacheTransactionManager,
// whereas that is not true for CacheTransactionManagerCreation.
List<TransactionListener> otherTxListeners = Arrays.asList(other.getCacheTransactionManager().getListeners());
List<TransactionListener> thisTxListeners = Arrays.asList(getCacheTransactionManager().getListeners());
if (!thisTxListeners.equals(otherTxListeners)) {
throw new RuntimeException(LocalizedStrings.CacheCreation_TXLISTENER.toLocalizedString());
}
}
}
if (hasResourceManager()) {
getResourceManager().sameAs(other.getResourceManager());
}
return true;
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class CacheXmlParser method endTransactionListener.
/**
* Create a <code>transaction-listener</code> using the declarable interface and set the
* transaction manager with the newly instantiated listener.
*/
private void endTransactionListener() {
Declarable d = createDeclarable();
if (!(d instanceof TransactionListener)) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_CACHELISTENER.toLocalizedString(d.getClass().getName()));
}
CacheTransactionManagerCreation txMgrCreation = (CacheTransactionManagerCreation) stack.peek();
txMgrCreation.addListener((TransactionListener) d);
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class TXJUnitTest method testListener.
@Test
public void testListener() {
assertTrue(this.txMgr.getListener() == null);
TransactionListener oldListener = this.txMgr.setListener(new TransactionListener() {
@Override
public void afterCommit(TransactionEvent event) {
listenerAfterCommit = 1;
te = event;
}
@Override
public void afterFailedCommit(TransactionEvent event) {
listenerAfterFailedCommit = 1;
te = event;
}
@Override
public void afterRollback(TransactionEvent event) {
listenerAfterRollback = 1;
te = event;
}
@Override
public void close() {
listenerClose = 1;
}
});
assertTrue(oldListener == null);
this.txMgr.begin();
TransactionId myTxId = this.txMgr.getTransactionId();
assertEquals(0, this.listenerAfterRollback);
this.txMgr.rollback();
assertEquals(1, this.listenerAfterRollback);
assertEquals(0, this.te.getCreateEvents().size());
assertEquals(0, this.te.getPutEvents().size());
assertEquals(0, this.te.getInvalidateEvents().size());
assertEquals(0, this.te.getDestroyEvents().size());
assertEquals(0, this.te.getEvents().size());
assertEquals(myTxId, this.te.getTransactionId());
this.txMgr.begin();
myTxId = this.txMgr.getTransactionId();
try {
assertEquals(0, this.listenerAfterCommit);
this.txMgr.commit();
} catch (CommitConflictException unexpected) {
fail("did not expect " + unexpected);
}
assertEquals(1, this.listenerAfterCommit);
assertEquals(0, this.te.getCreateEvents().size());
assertEquals(0, this.te.getPutEvents().size());
assertEquals(0, this.te.getInvalidateEvents().size());
assertEquals(0, this.te.getDestroyEvents().size());
assertEquals(0, this.te.getEvents().size());
assertEquals(myTxId, this.te.getTransactionId());
assertEquals(0, this.listenerClose);
oldListener = this.txMgr.setListener(new TransactionListener() {
@Override
public void afterCommit(TransactionEvent event) {
listenerAfterCommit = 2;
te = event;
}
@Override
public void afterFailedCommit(TransactionEvent event) {
listenerAfterFailedCommit = 2;
}
@Override
public void afterRollback(TransactionEvent event) {
listenerAfterRollback = 2;
te = event;
}
@Override
public void close() {
listenerClose = 2;
}
});
assertEquals(1, this.listenerClose);
this.txMgr.begin();
assertEquals(1, this.listenerAfterRollback);
this.txMgr.rollback();
assertEquals(2, this.listenerAfterRollback);
this.txMgr.begin();
this.txMgr.setListener(oldListener);
assertEquals(2, this.listenerClose);
this.txMgr.rollback();
assertEquals(1, this.listenerAfterRollback);
closeCache();
assertEquals(1, this.listenerClose);
}
use of org.apache.geode.cache.TransactionListener in project geode by apache.
the class TXCommitMessage method basicProcess.
public void basicProcess() {
final DM dm = this.dm;
synchronized (this) {
if (isProcessing()) {
if (logger.isDebugEnabled()) {
logger.debug("TXCommitMessage {} is already in process, returning", this);
}
return;
} else {
setIsProcessing(true);
}
}
if (logger.isDebugEnabled()) {
logger.debug("begin processing TXCommitMessage for {}", this.txIdent);
}
// do this before CacheFactory.getInstance for bug 33471
final int oldLevel = LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
// this gets flipped if we need to fire tx listener
boolean forceListener = false;
// it needs to default to false because we don't want to fire listeners on pr replicates
try {
TXRmtEvent txEvent = null;
final Cache cache = CacheFactory.getInstance(dm.getSystem());
if (cache == null) {
addProcessingException(new CacheClosedException());
// return ... this cache is closed so we can't do anything.
return;
}
final TransactionListener[] tls = cache.getCacheTransactionManager().getListeners();
if (tls.length > 0) {
txEvent = new TXRmtEvent(this.txIdent, cache);
}
try {
// Pre-process each Region in the tx
try {
Iterator it = this.regions.iterator();
while (it.hasNext()) {
boolean failedBeginProcess = true;
RegionCommit rc = (RegionCommit) it.next();
try {
failedBeginProcess = !rc.beginProcess(dm, this.txIdent, txEvent);
} catch (CacheRuntimeException problem) {
processCacheRuntimeException(problem);
} finally {
if (failedBeginProcess) {
// Cause related FarSideEntryOps to skip processing
rc.r = null;
// Skip endProcessing as well
it.remove();
}
}
}
basicProcessOps();
} finally {
// fix for bug 40001
// post-process each Region in the tx
Iterator it = this.regions.iterator();
while (it.hasNext()) {
try {
RegionCommit rc = (RegionCommit) it.next();
rc.endProcess();
if (rc.isForceFireEvent(dm)) {
forceListener = true;
}
} catch (CacheRuntimeException problem) {
processCacheRuntimeException(problem);
}
}
}
/*
* We need to make sure that we should fire a TX afterCommit event.
*/
boolean internalEvent = (txEvent != null && txEvent.hasOnlyInternalEvents());
if (!disableListeners && !internalEvent && (forceListener || (txEvent != null && !txEvent.isEmpty()))) {
for (int i = 0; i < tls.length; i++) {
try {
tls[i].afterCommit(txEvent);
} catch (VirtualMachineError err) {
SystemFailure.initiateFailure(err);
// now, so don't let this thread continue.
throw err;
} catch (Throwable t) {
// Whenever you catch Error or Throwable, you must also
// catch VirtualMachineError (see above). However, there is
// _still_ a possibility that you are dealing with a cascading
// error condition, so you also need to check to see if the JVM
// is still usable:
SystemFailure.checkFailure();
logger.error(LocalizedMessage.create(LocalizedStrings.TXCommitMessage_EXCEPTION_OCCURRED_IN_TRANSACTIONLISTENER), t);
}
}
}
} catch (CancelException e) {
processCacheRuntimeException(e);
} finally {
if (txEvent != null) {
txEvent.freeOffHeapResources();
}
}
} finally {
LocalRegion.setThreadInitLevelRequirement(oldLevel);
if (isAckRequired()) {
ack();
}
if (!GemFireCacheImpl.getExisting("Applying TXCommitMessage").isClient()) {
getTracker().saveTXForClientFailover(txIdent, this);
}
if (logger.isDebugEnabled()) {
logger.debug("completed processing TXCommitMessage for {}", this.txIdent);
}
}
}
Aggregations