INT-3359: Add GemfireLockRegistry
JIRA: https://jira.spring.io/browse/INT-3359
This commit is contained in:
committed by
Gary Russell
parent
89a7743640
commit
93ad65e7ce
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.integration.gemfire.util;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import org.springframework.integration.support.locks.LockRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.gemstone.gemfire.cache.Cache;
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
import com.gemstone.gemfire.cache.Scope;
|
||||
|
||||
/**
|
||||
* Implementation of {@link LockRegistry} providing a distributed lock using Gemfire.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*/
|
||||
public class GemfireLockRegistry implements LockRegistry {
|
||||
|
||||
public static final String LOCK_REGISTRY_REGION = "LockRegistry";
|
||||
|
||||
private final Region<?, ?> region;
|
||||
|
||||
public GemfireLockRegistry(Cache cache) {
|
||||
Assert.notNull(cache, "'cache' must not be null");
|
||||
this.region = cache.createRegionFactory().setScope(Scope.GLOBAL).create(LOCK_REGISTRY_REGION);
|
||||
}
|
||||
|
||||
public GemfireLockRegistry(Region<?, ?> region) {
|
||||
Assert.notNull(region, "'region' must not be null");
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lock obtain(Object lockKey) {
|
||||
return this.region.getDistributedLock(lockKey);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides utility classes.
|
||||
*/
|
||||
package org.springframework.integration.gemfire.util;
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:gfe="http://www.springframework.org/schema/gemfire"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<gfe:cache />
|
||||
|
||||
<bean id="lockRegistry" class="org.springframework.integration.gemfire.util.GemfireLockRegistry">
|
||||
<constructor-arg ref="gemfireCache"/>
|
||||
</bean>
|
||||
|
||||
<bean id="lockRegistry2" class="org.springframework.integration.gemfire.util.GemfireLockRegistry">
|
||||
<constructor-arg value="#{gemfireCache.getRegion(T(org.springframework.integration.gemfire.util.GemfireLockRegistry).LOCK_REGISTRY_REGION)}"/>
|
||||
</bean>
|
||||
|
||||
<bean id="latching"
|
||||
class="org.springframework.integration.gemfire.util.AggregatorWithGemfireLocksTests$LatchingReleaseStrategy"/>
|
||||
|
||||
<bean id="sms" class="org.springframework.integration.store.SimpleMessageStore"/>
|
||||
|
||||
<int:aggregator input-channel="in" release-strategy="latching" output-channel="out"
|
||||
message-store="sms"
|
||||
expire-groups-upon-completion="true" lock-registry="lockRegistry"/>
|
||||
|
||||
<int:aggregator input-channel="in2" release-strategy="latching" output-channel="out"
|
||||
message-store="sms"
|
||||
expire-groups-upon-completion="true" lock-registry="lockRegistry2"/>
|
||||
|
||||
<int:channel id="out">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.integration.gemfire.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.IntegrationMessageHeaderAccessor;
|
||||
import org.springframework.integration.aggregator.ReleaseStrategy;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
* @since 4.0
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
public class AggregatorWithGemfireLocksTests {
|
||||
|
||||
@Autowired
|
||||
private LatchingReleaseStrategy releaseStrategy;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel in;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel in2;
|
||||
|
||||
@Autowired
|
||||
private PollableChannel out;
|
||||
|
||||
private volatile Exception exception;
|
||||
|
||||
@Test
|
||||
public void testLockSingleGroup() throws Exception {
|
||||
this.releaseStrategy.reset(1);
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1));
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 1));
|
||||
assertTrue(this.releaseStrategy.latch2.await(10, TimeUnit.SECONDS));
|
||||
this.releaseStrategy.latch1.countDown();
|
||||
assertNotNull(this.out.receive(10000));
|
||||
assertEquals(1, this.releaseStrategy.maxCallers.get());
|
||||
assertNull("Unexpected exception:" + (this.exception != null ? this.exception.toString() : ""), this.exception);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLockThreeGroups() throws Exception {
|
||||
this.releaseStrategy.reset(3);
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1));
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 1));
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 2));
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 2));
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 3));
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 3));
|
||||
assertTrue(this.releaseStrategy.latch2.await(10, TimeUnit.SECONDS));
|
||||
this.releaseStrategy.latch1.countDown();
|
||||
this.releaseStrategy.latch1.countDown();
|
||||
this.releaseStrategy.latch1.countDown();
|
||||
assertNotNull(this.out.receive(10000));
|
||||
assertNotNull(this.out.receive(10000));
|
||||
assertNotNull(this.out.receive(10000));
|
||||
assertEquals(3, this.releaseStrategy.maxCallers.get());
|
||||
assertNull("Unexpected exception:" + (this.exception != null ? this.exception.toString() : ""), this.exception);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDistributedAggregator() throws Exception {
|
||||
this.releaseStrategy.reset(1);
|
||||
Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1));
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
in2.send(new GenericMessage<String>("bar", stubHeaders(2, 2, 1)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
exception = e;
|
||||
}
|
||||
}
|
||||
});
|
||||
assertTrue(this.releaseStrategy.latch2.await(10, TimeUnit.SECONDS));
|
||||
this.releaseStrategy.latch1.countDown();
|
||||
assertNotNull(this.out.receive(10000));
|
||||
assertEquals(1, this.releaseStrategy.maxCallers.get());
|
||||
assertNull("Unexpected exception:" + (this.exception != null ? this.exception.toString() : ""), this.exception);
|
||||
}
|
||||
|
||||
private Runnable asyncSend(final String payload, final int sequence, final int correlation) {
|
||||
return new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
in.send(new GenericMessage<String>(payload, stubHeaders(sequence, 2, correlation)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
exception = e;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Map<String, Object> stubHeaders(int sequenceNumber, int sequenceSize, int correlationId) {
|
||||
Map<String, Object> headers = new HashMap<String, Object>();
|
||||
headers.put(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, sequenceNumber);
|
||||
headers.put(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, sequenceSize);
|
||||
headers.put(IntegrationMessageHeaderAccessor.CORRELATION_ID, correlationId);
|
||||
return headers;
|
||||
}
|
||||
|
||||
public static class LatchingReleaseStrategy implements ReleaseStrategy {
|
||||
|
||||
private volatile CountDownLatch latch1;
|
||||
|
||||
private volatile CountDownLatch latch2;
|
||||
|
||||
private volatile AtomicInteger callers;
|
||||
|
||||
private volatile AtomicInteger maxCallers;
|
||||
|
||||
@Override
|
||||
public boolean canRelease(MessageGroup group) {
|
||||
synchronized(this) {
|
||||
this.callers.incrementAndGet();
|
||||
this.maxCallers.set(Math.max(this.maxCallers.get(), this.callers.get()));
|
||||
}
|
||||
this.latch2.countDown();
|
||||
try {
|
||||
this.latch1.await(10, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
this.callers.decrementAndGet();
|
||||
return group.size() > 1;
|
||||
}
|
||||
|
||||
public void reset(int expectedConcurrency) {
|
||||
this.latch1 = new CountDownLatch(expectedConcurrency);
|
||||
this.latch2 = new CountDownLatch(expectedConcurrency);
|
||||
this.callers = new AtomicInteger();
|
||||
this.maxCallers = new AtomicInteger();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -178,4 +178,26 @@ In the above examle, the cache and region are configured using the spring-gemfir
|
||||
Note the <emphasis>pool</emphasis> element is configured with the address of a cache server (a locator may be substituted here). The region is configured as a 'PROXY' so that no data will be stored locally. The region's id corresponds to a region with the same name configured in the cache server.
|
||||
</para>
|
||||
</section>
|
||||
<section id="gemfire-lock-registry">
|
||||
<title>Gemfire Lock Registry</title>
|
||||
|
||||
<para>
|
||||
Starting with <emphasis>version 4.0</emphasis>, the <classname>GemfireLockRegistry</classname> is
|
||||
available. Certain components (for example aggregator and resequencer) use a lock obtained from
|
||||
a <interfacename>LockRegistry</interfacename> instance to ensure
|
||||
that only one thread is manipulating a group at a time. The <classname>DefaultLockRegistry</classname>
|
||||
performs this function within a single component; you can now configure an external lock registry
|
||||
on these components. When used with a shared <interfacename>MessageGroupStore</interfacename>,
|
||||
the <classname>GemfireLockRegistry</classname> can be use to provide this functionality across
|
||||
multiple application instances, such that only one instance can manipulate the group at a time.
|
||||
<note>
|
||||
One of the <classname>GemfireLockRegistry</classname> constructors requires a <interfacename>Region</interfacename>
|
||||
as an argument; it is used to obtain a <interfacename>Lock</interfacename> via the
|
||||
<code>getDistributedLock()</code> method. This operation requires <code>GLOBAL</code>
|
||||
scope for the <interfacename>Region</interfacename>.
|
||||
Another constructor requires <interfacename>Cache</interfacename> and the <interfacename>Region</interfacename>
|
||||
will be created with <code>GLOBAL</code> scope and with the name <code>LockRegistry</code>.
|
||||
</note>
|
||||
</para>
|
||||
</section>
|
||||
</chapter>
|
||||
|
||||
@@ -133,13 +133,15 @@
|
||||
For more information, see <xref linkend="redis-outbound-gateway"/>.
|
||||
</para>
|
||||
</section>
|
||||
<section id="4.0-redis-lock-registry">
|
||||
<title>RedisLockRegistry</title>
|
||||
<section id="4.0-redis-gemfire-lock-registry">
|
||||
<title>RedisLockRegistry and GemfireLockRegistry</title>
|
||||
<para>
|
||||
The <classname>RedisLockRegistry</classname> is now available supporting global locks visible
|
||||
to multiple application instances/servers. This can be used with aggregating message handlers
|
||||
across multiple application instances such that group release will occur on only one instance.
|
||||
For more information, see <xref linkend="redis-lock-registry"/> and <xref linkend="aggregator"/>.
|
||||
The <classname>RedisLockRegistry</classname> and <classname>GemfireLockRegistry</classname> are now
|
||||
available supporting global locks visible to multiple application instances/servers.
|
||||
These can be used with aggregating message handlers across multiple application instances such
|
||||
that group release will occur on only one instance.
|
||||
For more information, see <xref linkend="redis-lock-registry"/>, <xref linkend="gemfire-lock-registry"/>
|
||||
and <xref linkend="aggregator"/>.
|
||||
</para>
|
||||
</section>
|
||||
<section id="4.0-poller-annotation">
|
||||
|
||||
Reference in New Issue
Block a user