Java Code Examples for com.gemstone.gemfire.cache.Region#getCache()

The following examples show how to use com.gemstone.gemfire.cache.Region#getCache() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegionEntryHelper.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the entry at the key is valid. If it should have expired
 * but hasn't, log a warning.
 * @param region
 * @param key
 * @return
 */
private static boolean isValid(Region region, Serializable key) {
  boolean valid = true;
  Region.Entry entry = region.getEntry(key);
  if (entry != null) {
    CacheStatistics statistics = entry.getStatistics();
    GemFireCacheImpl cache = (GemFireCacheImpl) region.getCache(); 
    long msSinceLastAccessed = cache.cacheTimeMillis() - statistics.getLastAccessedTime();
    long timeout = region.getAttributes().getEntryIdleTimeout().getTimeout() * 1000;
    if (msSinceLastAccessed > timeout) {
      StringBuilder builder = new StringBuilder();
      builder
          .append("It has been ")
          .append(msSinceLastAccessed)
          .append(" ms since entry at key=")
          .append(key)
          .append(" was accessed. It should be expired but isn't.");
      region.getCache().getLogger().warning(builder.toString());
      valid = false;
    }
  }
  return valid;
}
 
Example 2
Source File: RegionEntryHelper.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the entry at the key is valid. If it should have expired
 * but hasn't, log a warning.
 * @param region
 * @param key
 * @return
 */
private static boolean isValid(Region region, Serializable key) {
  boolean valid = true;
  Region.Entry entry = region.getEntry(key);
  if (entry != null) {
    CacheStatistics statistics = entry.getStatistics();
    GemFireCacheImpl cache = (GemFireCacheImpl) region.getCache(); 
    long msSinceLastAccessed = cache.cacheTimeMillis() - statistics.getLastAccessedTime();
    long timeout = region.getAttributes().getEntryIdleTimeout().getTimeout() * 1000;
    if (msSinceLastAccessed > timeout) {
      StringBuilder builder = new StringBuilder();
      builder
          .append("It has been ")
          .append(msSinceLastAccessed)
          .append(" ms since entry at key=")
          .append(key)
          .append(" was accessed. It should be expired but isn't.");
      region.getCache().getLogger().warning(builder.toString());
      valid = false;
    }
  }
  return valid;
}
 
Example 3
Source File: MultiRegionFunctionExecutor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void validateExecution(Function function, Set targetMembers) {
  GemFireCacheImpl cache = null;
  for (Region r : regions) {
    cache = (GemFireCacheImpl)r.getCache();
    break;
  }
  /*
  if (cache != null && cache.getTxManager().getTXState() != null) {
    if (targetMembers.size() > 1) {
      throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_ON_MORE_THAN_ONE_NODE
          .toLocalizedString());
    } else {
      assert targetMembers.size() == 1;
      DistributedMember funcTarget = (DistributedMember)targetMembers.iterator().next();
      DistributedMember target = cache.getTxManager().getTXState().getTarget();
      if (target == null) {
        cache.getTxManager().getTXState().setTarget(funcTarget);
      } else if (!target.equals(funcTarget)) {
        throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_EXECUTION_NOT_COLOCATED_0_1
            .toLocalizedString(new Object[] {target,funcTarget}));
      }
    }
  }
  */
  if (!function.hasResult() && cache != null
      && cache.getTxManager().getTXState() != null) {
    throw new TransactionException(LocalizedStrings
        .TXState_FUNCTION_WITH_NO_RESULT_NOT_SUPPORTED_IN_A_TRANSACTION
            .toLocalizedString());
  }
  if (function.optimizeForWrite() && cache.getResourceManager().getHeapMonitor().
      containsHeapCriticalMembers(targetMembers) &&
      !MemoryThresholds.isLowMemoryExceptionDisabled()) {
    Set<InternalDistributedMember> hcm  = cache.getResourceAdvisor().adviseCritialMembers();
    Set<DistributedMember> sm = SetUtils.intersection(hcm, targetMembers);
    throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(
        new Object[] {function.getId(), sm}), sm);
  }
}
 
Example 4
Source File: PartitionRegionHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Gathers details about the specified partitioned region. Returns null if
 * the partitioned region is not locally defined.
 * 
 * @param region the region to get info about
 * @return details about the specified partitioned region
 * @since 6.0
 */
public static PartitionRegionInfo getPartitionRegionInfo(
    final Region<?,?> region) {
  try {
    PartitionedRegion pr = isPartitionedCheck(region);
    GemFireCacheImpl cache =  (GemFireCacheImpl) region.getCache();
    return pr.getRedundancyProvider().buildPartitionedRegionInfo(
        false, cache.getResourceManager().getLoadProbe()); // may return null
  } 
  catch (ClassCastException e) {
    // not a PR so return null
  }
  return null;
}
 
Example 5
Source File: HDFSRegionOperationsJUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void verifyLRURegionAttributesForPR(Region r) {
  for (int i =0; i<200; i++) {
    r.put(i, "value"+i);
  }
  RegionAttributes<Integer, String> ra = r.getAttributes();
  assertNotNull(ra.getEvictionAttributes());
  // default eviction action for region shortcut
  assertEquals(EvictionAction.OVERFLOW_TO_DISK, ra.getEvictionAttributes().getAction());

  GemFireCacheImpl cache = (GemFireCacheImpl) r.getCache();
  assertEquals(80.0f, cache.getResourceManager().getEvictionHeapPercentage());
  DiskStore ds = cache.findDiskStore(null);
  assertNotNull(ds);
  Set s = cache.getResourceManager().getResourceListeners(ResourceType.HEAP_MEMORY);
  Iterator it = s.iterator();
  boolean regionFound = false;
  while (it.hasNext()) {
    Object o = it.next();
    if (o instanceof PartitionedRegion) {
      PartitionedRegion pr = (PartitionedRegion) o;
      if (getName().equals(pr.getName())) {
        regionFound = true;
      } else {
        continue;
      }
      for (BucketRegion br : pr.getDataStore().getAllLocalBucketRegions()) {
        assertNotNull(br.getAttributes().getEvictionAttributes());
        assertEquals(EvictionAlgorithm.LRU_HEAP, br.getAttributes().getEvictionAttributes().getAlgorithm());
        assertEquals(EvictionAction.OVERFLOW_TO_DISK, br.getAttributes().getEvictionAttributes().getAction());
      }
    }
  }
  assertTrue(regionFound);

}
 
Example 6
Source File: MultiRegionFunctionExecutor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void validateExecution(Function function, Set targetMembers) {
  GemFireCacheImpl cache = null;
  for (Region r : regions) {
    cache = (GemFireCacheImpl)r.getCache();
    break;
  }
  /*
  if (cache != null && cache.getTxManager().getTXState() != null) {
    if (targetMembers.size() > 1) {
      throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_ON_MORE_THAN_ONE_NODE
          .toLocalizedString());
    } else {
      assert targetMembers.size() == 1;
      DistributedMember funcTarget = (DistributedMember)targetMembers.iterator().next();
      DistributedMember target = cache.getTxManager().getTXState().getTarget();
      if (target == null) {
        cache.getTxManager().getTXState().setTarget(funcTarget);
      } else if (!target.equals(funcTarget)) {
        throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_EXECUTION_NOT_COLOCATED_0_1
            .toLocalizedString(new Object[] {target,funcTarget}));
      }
    }
  }
  */
  if (!function.hasResult() && cache != null
      && cache.getTxManager().getTXState() != null) {
    throw new TransactionException(LocalizedStrings
        .TXState_FUNCTION_WITH_NO_RESULT_NOT_SUPPORTED_IN_A_TRANSACTION
            .toLocalizedString());
  }
  if (function.optimizeForWrite() && cache.getResourceManager().getHeapMonitor().
      containsHeapCriticalMembers(targetMembers) &&
      !MemoryThresholds.isLowMemoryExceptionDisabled()) {
    Set<InternalDistributedMember> hcm  = cache.getResourceAdvisor().adviseCritialMembers();
    Set<DistributedMember> sm = SetUtils.intersection(hcm, targetMembers);
    throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(
        new Object[] {function.getId(), sm}), sm);
  }
}
 
Example 7
Source File: PartitionRegionHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Gathers details about the specified partitioned region. Returns null if
 * the partitioned region is not locally defined.
 * 
 * @param region the region to get info about
 * @return details about the specified partitioned region
 * @since 6.0
 */
public static PartitionRegionInfo getPartitionRegionInfo(
    final Region<?,?> region) {
  try {
    PartitionedRegion pr = isPartitionedCheck(region);
    GemFireCacheImpl cache =  (GemFireCacheImpl) region.getCache();
    return pr.getRedundancyProvider().buildPartitionedRegionInfo(
        false, cache.getResourceManager().getLoadProbe()); // may return null
  } 
  catch (ClassCastException e) {
    // not a PR so return null
  }
  return null;
}
 
Example 8
Source File: HDFSRegionOperationsJUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void verifyLRURegionAttributesForPR(Region r) {
  for (int i =0; i<200; i++) {
    r.put(i, "value"+i);
  }
  RegionAttributes<Integer, String> ra = r.getAttributes();
  assertNotNull(ra.getEvictionAttributes());
  // default eviction action for region shortcut
  assertEquals(EvictionAction.OVERFLOW_TO_DISK, ra.getEvictionAttributes().getAction());

  GemFireCacheImpl cache = (GemFireCacheImpl) r.getCache();
  assertEquals(80.0f, cache.getResourceManager().getEvictionHeapPercentage());
  DiskStore ds = cache.findDiskStore(null);
  assertNotNull(ds);
  Set s = cache.getResourceManager().getResourceListeners(ResourceType.HEAP_MEMORY);
  Iterator it = s.iterator();
  boolean regionFound = false;
  while (it.hasNext()) {
    Object o = it.next();
    if (o instanceof PartitionedRegion) {
      PartitionedRegion pr = (PartitionedRegion) o;
      if (getName().equals(pr.getName())) {
        regionFound = true;
      } else {
        continue;
      }
      for (BucketRegion br : pr.getDataStore().getAllLocalBucketRegions()) {
        assertNotNull(br.getAttributes().getEvictionAttributes());
        assertEquals(EvictionAlgorithm.LRU_HEAP, br.getAttributes().getEvictionAttributes().getAlgorithm());
        assertEquals(EvictionAction.OVERFLOW_TO_DISK, br.getAttributes().getEvictionAttributes().getAction());
      }
    }
  }
  assertTrue(regionFound);

}
 
Example 9
Source File: ls.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public Object execute(String command, String regionPath, Object arg) throws Exception
{
	Region region = CacheFactory.getAnyInstance().getRegion(regionPath);
	if (region == null) {
		code = AggregateResults.CODE_ERROR;
		codeMessage = "Undefined region: " + regionPath;
		return null;
	}
	
	Cache cache = region.getCache();
	ListMessage topMessage = new ListMessage();
	if (command.startsWith("ls -c")) {
		List<CacheServer> cacheServerList = cache.getCacheServers();
		if (cacheServerList.size() > 0) {
			for (CacheServer cacheServer : cacheServerList) {
				MapMessage cacheServerMessage = new MapMessage();
				String groups[] = cacheServer.getGroups();
				if (groups.length > 0) {
					String groupsStr = "";
					for (int i = 0; i < groups.length; i++) {
						groupsStr += groups[i];
						if (i < groups.length - 1) {
							groupsStr += ", ";
						}
					}
					cacheServerMessage.put("ServerGroups", groupsStr);
				} else {
					cacheServerMessage.put("ServerGroups", "");
				}
				
				cacheServerMessage.put("BindAddress", cacheServer.getBindAddress());
				cacheServerMessage.put("HostnameForClients", cacheServer.getHostnameForClients());
				cacheServerMessage.put("LoadPollInterval", cacheServer.getLoadPollInterval());
				cacheServerMessage.put("MaxConnections", cacheServer.getMaxConnections());
				cacheServerMessage.put("MaximumMessageCount", cacheServer.getMaximumMessageCount());
				cacheServerMessage.put("MaximumTimeBetweenPings", cacheServer.getMaximumTimeBetweenPings());
				cacheServerMessage.put("MaxThreads", cacheServer.getMaxThreads());
				cacheServerMessage.put("MessageTimeToLive", cacheServer.getMessageTimeToLive());
				cacheServerMessage.put("NotifyBySubscription", cacheServer.getNotifyBySubscription());
				cacheServerMessage.put("Port", cacheServer.getPort());
				cacheServerMessage.put("SocketBufferSize", cacheServer.getSocketBufferSize());
				cacheServerMessage.put("TcpNoDelay", cacheServer.getTcpNoDelay());
				
				topMessage.add(cacheServerMessage);
			}
		}
	}
	
	return new GfshData(topMessage);
}
 
Example 10
Source File: TestPartitionListener.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void afterRegionCreate(Region region) {
  theCache = region.getCache();
}
 
Example 11
Source File: ls.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public Object execute(String command, String regionPath, Object arg) throws Exception
{
	Region region = CacheFactory.getAnyInstance().getRegion(regionPath);
	if (region == null) {
		code = AggregateResults.CODE_ERROR;
		codeMessage = "Undefined region: " + regionPath;
		return null;
	}
	
	Cache cache = region.getCache();
	ListMessage topMessage = new ListMessage();
	if (command.startsWith("ls -c")) {
		List<CacheServer> cacheServerList = cache.getCacheServers();
		if (cacheServerList.size() > 0) {
			for (CacheServer cacheServer : cacheServerList) {
				MapMessage cacheServerMessage = new MapMessage();
				String groups[] = cacheServer.getGroups();
				if (groups.length > 0) {
					String groupsStr = "";
					for (int i = 0; i < groups.length; i++) {
						groupsStr += groups[i];
						if (i < groups.length - 1) {
							groupsStr += ", ";
						}
					}
					cacheServerMessage.put("ServerGroups", groupsStr);
				} else {
					cacheServerMessage.put("ServerGroups", "");
				}
				
				cacheServerMessage.put("BindAddress", cacheServer.getBindAddress());
				cacheServerMessage.put("HostnameForClients", cacheServer.getHostnameForClients());
				cacheServerMessage.put("LoadPollInterval", cacheServer.getLoadPollInterval());
				cacheServerMessage.put("MaxConnections", cacheServer.getMaxConnections());
				cacheServerMessage.put("MaximumMessageCount", cacheServer.getMaximumMessageCount());
				cacheServerMessage.put("MaximumTimeBetweenPings", cacheServer.getMaximumTimeBetweenPings());
				cacheServerMessage.put("MaxThreads", cacheServer.getMaxThreads());
				cacheServerMessage.put("MessageTimeToLive", cacheServer.getMessageTimeToLive());
				cacheServerMessage.put("NotifyBySubscription", cacheServer.getNotifyBySubscription());
				cacheServerMessage.put("Port", cacheServer.getPort());
				cacheServerMessage.put("SocketBufferSize", cacheServer.getSocketBufferSize());
				cacheServerMessage.put("TcpNoDelay", cacheServer.getTcpNoDelay());
				
				topMessage.add(cacheServerMessage);
			}
		}
	}
	
	return new GfshData(topMessage);
}
 
Example 12
Source File: TestPartitionListener.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void afterRegionCreate(Region region) {
  theCache = region.getCache();
}