Skip to content

Commit

Permalink
HHH-4508 - Fixed 'avalable' typo
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_3@17804 1b8cb986-b30d-0410-93ca-fae66ebed9b2
  • Loading branch information
Juraci Krohling committed Oct 20, 2009
1 parent 1bf1352 commit 725e2a8
Show file tree
Hide file tree
Showing 24 changed files with 531 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
*/
package org.hibernate.cache.jbc2;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import javax.transaction.SystemException;
import javax.transaction.Transaction;
Expand All @@ -42,7 +46,12 @@
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.Option;
import org.jboss.cache.config.Configuration.NodeLockingScheme;
import org.jboss.cache.notifications.annotation.CacheListener;
import org.jboss.cache.notifications.annotation.NodeInvalidated;
import org.jboss.cache.notifications.annotation.NodeModified;
import org.jboss.cache.notifications.annotation.ViewChanged;
import org.jboss.cache.notifications.event.NodeInvalidatedEvent;
import org.jboss.cache.notifications.event.NodeModifiedEvent;
import org.jboss.cache.notifications.event.ViewChangedEvent;
import org.jboss.cache.optimistic.DataVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -53,30 +62,52 @@
*
* @author Steve Ebersole
*/
@CacheListener
public abstract class BasicRegionAdapter implements Region {

private enum InvalidateState { INVALID, CLEARING, VALID };

public static final String ITEM = CacheHelper.ITEM;

protected final Cache jbcCache;
protected final String regionName;
protected final Fqn regionFqn;
protected final Fqn internalFqn;
protected Node regionRoot;
protected final boolean optimistic;
protected final TransactionManager transactionManager;
protected final Logger log;
protected final Object regionRootMutex = new Object();
protected final Object memberId;
protected final boolean replication;
protected final Object invalidationMutex = new Object();
protected final AtomicReference<InvalidateState> invalidateState =
new AtomicReference<InvalidateState>(InvalidateState.VALID);
protected final Set<Object> currentView = new HashSet<Object>();

// protected RegionRootListener listener;

public BasicRegionAdapter(Cache jbcCache, String regionName, String regionPrefix) {

this.log = LoggerFactory.getLogger(getClass());

this.jbcCache = jbcCache;
this.transactionManager = jbcCache.getConfiguration().getRuntimeConfig().getTransactionManager();
this.regionName = regionName;
this.regionFqn = createRegionFqn(regionName, regionPrefix);
optimistic = jbcCache.getConfiguration().getNodeLockingScheme() == NodeLockingScheme.OPTIMISTIC;
log = LoggerFactory.getLogger(getClass());
this.internalFqn = CacheHelper.getInternalFqn(regionFqn);
this.optimistic = jbcCache.getConfiguration().getNodeLockingScheme() == NodeLockingScheme.OPTIMISTIC;
this.memberId = jbcCache.getLocalAddress();
this.replication = CacheHelper.isClusteredReplication(jbcCache);

this.jbcCache.addCacheListener(this);

synchronized (currentView) {
List view = jbcCache.getMembers();
if (view != null) {
currentView.addAll(view);
}
}

activateLocalClusterNode();

log.debug("Created Region for " + regionName + " -- regionPrefix is " + regionPrefix);
Expand Down Expand Up @@ -129,13 +160,13 @@ else if (optimistic && regionRoot instanceof NodeSPI) {
if (!regionRoot.isResident()) {
regionRoot.setResident(true);
}
establishInternalNodes();
}
catch (Exception e) {
throw new CacheException(e.getMessage(), e);
}
finally {
if (tx != null)
resume(tx);
resume(tx);
}

}
Expand All @@ -154,6 +185,7 @@ private void establishRegionRootNode()
// For pessimistic locking, we just want to toss out our ref
// to any old invalid root node and get the latest (may be null)
if (!optimistic) {
establishInternalNodes();
regionRoot = jbcCache.getRoot().getChild( regionFqn );
return;
}
Expand Down Expand Up @@ -181,6 +213,7 @@ else if (newRoot instanceof NodeSPI) {
}
// Never evict this node
newRoot.setResident(true);
establishInternalNodes();
}
finally {
resume(tx);
Expand All @@ -189,6 +222,24 @@ else if (newRoot instanceof NodeSPI) {
}
}

private void establishInternalNodes()
{
synchronized (currentView) {
Transaction tx = suspend();
try {
for (Object member : currentView) {
DataVersion version = optimistic ? NonLockingDataVersion.INSTANCE : null;
Fqn f = Fqn.fromRelativeElements(internalFqn, member);
CacheHelper.addNode(jbcCache, f, true, false, version);
}
}
finally {
resume(tx);
}
}

}

public String getName() {
return regionName;
}
Expand All @@ -201,6 +252,11 @@ public Fqn getRegionFqn() {
return regionFqn;
}

public Object getMemberId()
{
return this.memberId;
}

/**
* Checks for the validity of the root cache node for this region,
* creating a new one if it does not exist or is invalid, and also
Expand All @@ -219,6 +275,37 @@ public void ensureRegionRootExists() {
if (regionRoot != null && regionRoot.isValid() && !regionRoot.isResident())
regionRoot.setResident(true);
}

public boolean checkValid()
{
boolean valid = invalidateState.get() == InvalidateState.VALID;

if (!valid) {
synchronized (invalidationMutex) {
if (invalidateState.compareAndSet(InvalidateState.INVALID, InvalidateState.CLEARING)) {
Transaction tx = suspend();
try {
Option opt = new Option();
opt.setLockAcquisitionTimeout(1);
opt.setCacheModeLocal(true);
CacheHelper.removeAll(jbcCache, regionFqn, opt);
invalidateState.compareAndSet(InvalidateState.CLEARING, InvalidateState.VALID);
}
catch (Exception e) {
if (log.isTraceEnabled()) {
log.trace("Could not invalidate region: " + e.getLocalizedMessage());
}
}
finally {
resume(tx);
}
}
}
valid = invalidateState.get() == InvalidateState.VALID;
}

return valid;
}

public void destroy() throws CacheException {
try {
Expand All @@ -242,10 +329,9 @@ public void destroy() throws CacheException {
} catch (Exception e) {
throw new CacheException(e);
}
// finally {
// if (listener != null)
// jbcCache.removeCacheListener(listener);
// }
finally {
jbcCache.removeCacheListener(this);
}
}

protected void deactivateLocalNode() {
Expand All @@ -256,36 +342,75 @@ protected void deactivateLocalNode() {
}
}

public boolean contains(Object key) {
if ( !checkValid() ) {
return false;
}

try {
Option opt = new Option();
opt.setLockAcquisitionTimeout(100);
CacheHelper.setInvocationOption( jbcCache, opt );
return CacheHelper.getAllowingTimeout( jbcCache, regionFqn, key ) != null;
}
catch ( CacheException ce ) {
throw ce;
}
catch ( Throwable t ) {
throw new CacheException( t );
}
}

public long getSizeInMemory() {
// not supported
return -1;
}

public long getElementCountInMemory() {
try {
Set childrenNames = CacheHelper.getChildrenNames(jbcCache, regionFqn);
return childrenNames.size();
} catch (Exception e) {
throw new CacheException(e);
if (checkValid()) {
try {
Set childrenNames = CacheHelper.getChildrenNames(jbcCache, regionFqn);
int size = childrenNames.size();
if (childrenNames.contains(CacheHelper.Internal.NODE)) {
size--;
}
return size;
}
catch ( CacheException ce ) {
throw ce;
}
catch (Exception e) {
throw new CacheException(e);
}
}
else {
return 0;
}
}

public long getElementCountOnDisk() {
return -1;
}

public Map toMap() {
try {
Map result = new HashMap();
Set childrenNames = CacheHelper.getChildrenNames(jbcCache, regionFqn);
for (Object childName : childrenNames) {
result.put(childName, CacheHelper.get(jbcCache,regionFqn, childName));
}
return result;
} catch (CacheException e) {
throw e;
} catch (Exception e) {
throw new CacheException(e);
if (checkValid()) {
try {
Map result = new HashMap();
Set childrenNames = CacheHelper.getChildrenNames(jbcCache, regionFqn);
for (Object childName : childrenNames) {
if (CacheHelper.Internal.NODE != childName) {
result.put(childName, CacheHelper.get(jbcCache,regionFqn, childName));
}
}
return result;
} catch (CacheException e) {
throw e;
} catch (Exception e) {
throw new CacheException(e);
}
}
else {
return Collections.emptyMap();
}
}

Expand Down Expand Up @@ -327,7 +452,7 @@ protected Object suspendAndGet(Object key, Option opt, boolean suppressTimeout)
* @return the transaction that was suspended, or <code>null</code> if
* there wasn't one
*/
protected Transaction suspend() {
public Transaction suspend() {
Transaction tx = null;
try {
if (transactionManager != null) {
Expand All @@ -345,7 +470,7 @@ protected Transaction suspend() {
* @param tx
* the transaction to suspend. May be <code>null</code>.
*/
protected void resume(Transaction tx) {
public void resume(Transaction tx) {
try {
if (tx != null)
transactionManager.resume(tx);
Expand Down Expand Up @@ -404,17 +529,52 @@ public static String escapeRegionName(String regionName, String regionPrefix) {
return escaped;
}

// @CacheListener
// public class RegionRootListener {
//
// @NodeCreated
// public void nodeCreated(NodeCreatedEvent event) {
// if (!event.isPre() && event.getFqn().equals(getRegionFqn())) {
// log.debug("Node created for " + getRegionFqn());
// Node regionRoot = jbcCache.getRoot().getChild(getRegionFqn());
// regionRoot.setResident(true);
// }
// }
//
// }
@NodeModified
public void nodeModified(NodeModifiedEvent event)
{
handleEvictAllModification(event);
}

protected boolean handleEvictAllModification(NodeModifiedEvent event) {

if (!event.isPre() && (replication || event.isOriginLocal()) && event.getData().containsKey(ITEM))
{
if (event.getFqn().isChildOf(internalFqn))
{
invalidateState.set(InvalidateState.INVALID);
return true;
}
}
return false;
}

@NodeInvalidated
public void nodeInvalidated(NodeInvalidatedEvent event)
{
handleEvictAllInvalidation(event);
}

protected boolean handleEvictAllInvalidation(NodeInvalidatedEvent event)
{
if (!event.isPre() && event.getFqn().isChildOf(internalFqn))
{
invalidateState.set(InvalidateState.INVALID);
return true;
}
return false;
}

@ViewChanged
public void viewChanged(ViewChangedEvent event) {

synchronized (currentView) {
List view = event.getNewView().getMembers();
if (view != null) {
currentView.addAll(view);
establishInternalNodes();
}
}

}

}
Loading

0 comments on commit 725e2a8

Please sign in to comment.