0

我正在关注此文档: http: //gemfire.docs.pivotal.io/docs-gemfire/latest/developing/distributed_regions/locking_in_global_regions.html 创建一个具有全局范围的区域以使用分布式锁定。

缓存.xml:

<client-cache>
<pool>…definition…&lt;/pool>
…
<!--region-attributes For Lock region-->
<region-attributes id="GZ_GLOBAL_REGION_LOCK_ATTRIBUTES" scope="global" pool-name="Zero"/>
…
</client-cache>

从 gemfire.properties 和 cache.xml 创建的 GemFireCache 之后的代码:

private Region<String, Object> getOrCreateLockRegion(GemFireCache gemfireCache) {
    Region<String, Object> region = gemfireCache.getRegion(lockRegionName);
    if (region == null) {
        if(!isUsingClientCache) {
            region = createRegionFactory((Cache)gemfireCache, String.class, Object.class, lockRegionAttributesID).create(lockRegionName);
        } else {
            region = createClientRegionFactory((ClientCache) gemfireCache, String.class, Object.class, lockRegionAttributesID).create(lockRegionName);
        }
    }
    return region;
}

protected <K, V> RegionFactory<K, V> createRegionFactory(Cache gemfireCache, Class<K> keyClass, Class<V> valueClass, String regionAttributeRefID) {
    return gemfireCache
            .<K, V>createRegionFactory(regionAttributeRefID)
            .setKeyConstraint(keyClass)
            .setValueConstraint(valueClass);
}

protected <K, V> ClientRegionFactory<K, V> createClientRegionFactory(ClientCache gemfireCache, Class<K> keyClass, Class<V> valueClass, String regionAttributeRefID) {
    return gemfireCache
            .<K, V>createClientRegionFactory(regionAttributeRefID)
            .setKeyConstraint(keyClass)
            .setValueConstraint(valueClass);
}

我想这会给我一个带有 的区域Scope.Global,以便我可以调用 region.getDistributedLock(“entrykey”); 然后有锁在实例之间进行协调。

但是,当我打电话时getDistributedLock,我得到了一个IllegalStateException: only supported for GLOBAL scope, not LOCAL

而且我发现无论区域属性中配置了什么,ClientRegionFactoryImpl 的构造函数都会强制范围为 Local,而且我没有 API 来覆盖它。这一行:https ://github.com/apache/incubator-geode/blob/develop/geode-core/src/main/java/org/apache/geode/cache/client/internal/ClientRegionFactoryImpl.java#L85

所以问题是,如果我使用客户端-服务器 DS 配置,我应该从客户端使用分布式锁吗?如果没有,我应该怎么做才能让客户端在必要时相互锁定以进行同步?

4

1 回答 1

0

Region 类上的 DistributedLock 和 RegionDistributedLock API 仅在服务器内可用。为了从客户端使用这些锁,您必须编写部署到服务器的函数。然后,客户端将告诉服务器执行可以操作区域以及 DistributedLock 和 RegionDistributedLock API 的函数。有关 FunctionService 的更多信息,请访问:

http://geode.apache.org/docs/guide/developing/function_exec/chapter_overview.html

于 2016-11-30T01:33:27.797 回答