use of org.apache.geode.cache.partition.PartitionNotAvailableException in project geode by apache.
the class PartitionedRegion method createBucket.
/**
* Create a bucket for the provided bucket identifier in an atomic fashion.
*
* @param bucketId the bucket identifier for the bucket that needs creation
* @param snoozer tracking object used to determine length of time t for bucket creation
* @return a selected Node on which to perform bucket operations
*/
public InternalDistributedMember createBucket(int bucketId, int size, final RetryTimeKeeper snoozer) {
InternalDistributedMember ret = getNodeForBucketWrite(bucketId, snoozer);
if (ret != null) {
return ret;
}
// In the current co-location scheme, we have to create the bucket for the
// colocatedWith region, before we create bucket for this region
final PartitionedRegion colocatedWith = ColocationHelper.getColocatedRegion(this);
if (colocatedWith != null) {
colocatedWith.createBucket(bucketId, size, snoozer);
}
// THis is for FPR.if the given bucket id is not starting bucket id then
// create bucket for starting bucket id
String partitionName = null;
if (this.isFixedPartitionedRegion()) {
FixedPartitionAttributesImpl fpa = PartitionedRegionHelper.getFixedPartitionAttributesForBucket(this, bucketId);
partitionName = fpa.getPartitionName();
int startBucketId = fpa.getStartingBucketID();
if (startBucketId == -1) {
throw new PartitionNotAvailableException(LocalizedStrings.FOR_FIXED_PARTITION_REGION_0_PARTITION_1_IS_NOT_YET_INITIALIZED_ON_DATASTORE.toString(new Object[] { getName(), partitionName }));
}
if (startBucketId != bucketId) {
createBucket(startBucketId, size, snoozer);
}
}
// Potentially no storage assigned, start bucket creation, be careful of race
// conditions
final long startTime = PartitionedRegionStats.startTime();
if (isDataStore()) {
ret = this.redundancyProvider.createBucketAtomically(bucketId, size, startTime, false, partitionName);
} else {
ret = this.redundancyProvider.createBucketOnDataStore(bucketId, size, startTime, snoozer);
}
return ret;
}
use of org.apache.geode.cache.partition.PartitionNotAvailableException in project geode by apache.
the class FixedPartitioningDUnitTest method testPut_PartitionNotAvailableException.
/**
* This test validates that if the required partition is not available at the time of entry
* operation then PartitionNotAvailabelException is thrown
*/
@Test
public void testPut_PartitionNotAvailableException() {
try {
member1.invoke(() -> FixedPartitioningTestBase.createCacheOnMember());
member1.invoke(() -> FixedPartitioningTestBase.createRegionWithPartitionAttributes("Quarter", null, 1, 0, 12, new QuarterPartitionResolver(), null, false));
member2.invoke(() -> FixedPartitioningTestBase.createCacheOnMember());
FixedPartitionAttributes fpa1 = FixedPartitionAttributes.createFixedPartition(Quarter1, true, 3);
List<FixedPartitionAttributes> fpaList = new ArrayList<FixedPartitionAttributes>();
fpaList.add(fpa1);
member2.invoke(() -> FixedPartitioningTestBase.createRegionWithPartitionAttributes("Quarter", fpaList, 1, 40, 12, new QuarterPartitionResolver(), null, false));
member3.invoke(() -> FixedPartitioningTestBase.createCacheOnMember());
fpa1 = FixedPartitionAttributes.createFixedPartition(Quarter2, true, 3);
fpaList.clear();
fpaList.add(fpa1);
member3.invoke(() -> FixedPartitioningTestBase.createRegionWithPartitionAttributes("Quarter", fpaList, 1, 40, 12, new QuarterPartitionResolver(), null, false));
member1.invoke(() -> FixedPartitioningTestBase.putThorughAccessor("Quarter"));
fail("PartitionNotAvailableException Expected");
} catch (Exception ex) {
if (!((ex.getCause() instanceof PartitionNotAvailableException))) {
Assert.fail("Expected PartitionNotAvailableException ", ex);
}
}
}
use of org.apache.geode.cache.partition.PartitionNotAvailableException in project geode by apache.
the class PartitionedRegionHelper method getHashKey.
/**
* Runs hashCode() on given key/routing object producing a long value and then finds absolute
* value of the modulus with bucketSize. For better key distribution, possibly use MD5 or SHA or
* any unique ID generator for the hash function.
*
* @param event entry event created for this entry operation; can be null
* @param pr the partitioned region on which to operate
* @param operation operation
* @param key the key on which to determine the hash key
* @param callbackArgument the callbackArgument is passed to <code>PartitionResolver</code> to get
* Routing object
* @return the bucket id the key/routing object hashes to
*/
private static int getHashKey(EntryOperation event, PartitionedRegion pr, Operation operation, Object key, Object value, Object callbackArgument) {
// avoid creating EntryOperation if there is no resolver
if (event != null) {
pr = (PartitionedRegion) event.getRegion();
key = event.getKey();
callbackArgument = event.getCallbackArgument();
}
PartitionResolver resolver = getResolver(pr, key, callbackArgument);
Object resolveKey = null;
if (pr.isFixedPartitionedRegion()) {
String partition = null;
if (resolver instanceof FixedPartitionResolver) {
Map<String, Integer[]> partitionMap = pr.getPartitionsMap();
if (event == null) {
event = new EntryOperationImpl(pr, operation, key, value, callbackArgument);
}
partition = ((FixedPartitionResolver) resolver).getPartitionName(event, partitionMap.keySet());
if (partition == null) {
Object[] prms = new Object[] { pr.getName(), resolver };
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_REGION_0_PARTITIONRESOLVER_1_RETURNED_PARTITION_NAME_NULL.toLocalizedString(prms));
}
Integer[] bucketArray = partitionMap.get(partition);
if (bucketArray == null) {
Object[] prms = new Object[] { pr.getName(), partition };
throw new PartitionNotAvailableException(LocalizedStrings.PartitionedRegionHelper_FOR_FIXED_PARTITIONED_REGION_0_FIXED_PARTITION_1_IS_NOT_AVAILABLE_ON_ANY_DATASTORE.toLocalizedString(prms));
}
int numBukets = bucketArray[1];
resolveKey = (numBukets == 1) ? partition : resolver.getRoutingObject(event);
} else if (resolver == null) {
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_FIXED_PARTITIONED_REGION_0_FIXED_PARTITION_RESOLVER_IS_NOT_AVAILABLE.toString(pr.getName()));
} else if (!(resolver instanceof FixedPartitionResolver)) {
Object[] prms = new Object[] { pr.getName(), resolver };
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_FIXED_PARTITIONED_REGION_0_RESOLVER_DEFINED_1_IS_NOT_AN_INSTANCE_OF_FIXEDPARTITIONRESOLVER.toLocalizedString(prms));
}
return assignFixedBucketId(pr, partition, resolveKey);
} else {
// Calculate resolveKey.
if (resolver == null) {
// no custom partitioning at all
resolveKey = key;
if (resolveKey == null) {
throw new IllegalStateException("attempting to hash null");
}
} else {
if (event == null) {
event = new EntryOperationImpl(pr, operation, key, value, callbackArgument);
}
resolveKey = resolver.getRoutingObject(event);
if (resolveKey == null) {
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_THE_ROUTINGOBJECT_RETURNED_BY_PARTITIONRESOLVER_IS_NULL.toLocalizedString());
}
}
// Finally, calculate the hash.
return getHashKey(pr, resolveKey);
}
}
use of org.apache.geode.cache.partition.PartitionNotAvailableException in project geode by apache.
the class FixedPartitioningDUnitTest method testBug43283.
// GEODE-567: async actions, waitForCriterion, time sensitive,
@Category(FlakyTest.class)
// non-thread-safe test hook, eats exceptions (partially fixed)
@Test
public void testBug43283() {
member1.invoke(() -> FixedPartitioningTestBase.createCacheOnMember());
member2.invoke(() -> FixedPartitioningTestBase.createCacheOnMember());
member3.invoke(() -> FixedPartitioningTestBase.createCacheOnMember());
member4.invoke(() -> FixedPartitioningTestBase.createCacheOnMember());
member1.invoke(() -> FixedPartitioningTestBase.setPRObserverBeforeCalculateStartingBucketId());
member2.invoke(() -> FixedPartitioningTestBase.setPRObserverBeforeCalculateStartingBucketId());
member3.invoke(() -> FixedPartitioningTestBase.setPRObserverBeforeCalculateStartingBucketId());
member4.invoke(() -> FixedPartitioningTestBase.setPRObserverBeforeCalculateStartingBucketId());
try {
FixedPartitionAttributes fpa1 = FixedPartitionAttributes.createFixedPartition(Quarter1, true, 3);
List<FixedPartitionAttributes> fpaList = new ArrayList<FixedPartitionAttributes>();
fpaList.add(fpa1);
AsyncInvocation inv1 = member1.invokeAsync(() -> FixedPartitioningTestBase.createRegionWithPartitionAttributes("Quarter", fpaList, 0, 40, 12, new QuarterPartitionResolver(), null, false));
FixedPartitionAttributes fpa2 = FixedPartitionAttributes.createFixedPartition(Quarter2, true, 3);
fpaList.clear();
fpaList.add(fpa2);
AsyncInvocation inv2 = member2.invokeAsync(() -> FixedPartitioningTestBase.createRegionWithPartitionAttributes("Quarter", fpaList, 0, 40, 12, new QuarterPartitionResolver(), null, false));
FixedPartitionAttributes fpa3 = FixedPartitionAttributes.createFixedPartition(Quarter3, true, 3);
fpaList.clear();
fpaList.add(fpa3);
AsyncInvocation inv3 = member3.invokeAsync(() -> FixedPartitioningTestBase.createRegionWithPartitionAttributes("Quarter", fpaList, 0, 40, 12, new QuarterPartitionResolver(), null, false));
member4.invoke(() -> FixedPartitioningTestBase.createRegionWithPartitionAttributes("Quarter", null, 0, 0, 12, new QuarterPartitionResolver(), null, false));
try {
member4.invoke(() -> FixedPartitioningTestBase.putThorughAccessor_Immediate("Quarter"));
} catch (Exception e) {
e.printStackTrace();
if (!(e.getCause() instanceof PartitionNotAvailableException)) {
Assert.fail("exception thrown is not PartitionNotAvailableException", e);
}
}
try {
inv1.join();
inv2.join();
inv3.join();
} catch (InterruptedException e) {
e.printStackTrace();
Assert.fail("Unexpected Exception", e);
}
} finally {
member1.invoke(() -> FixedPartitioningTestBase.resetPRObserverBeforeCalculateStartingBucketId());
member2.invoke(() -> FixedPartitioningTestBase.resetPRObserverBeforeCalculateStartingBucketId());
member3.invoke(() -> FixedPartitioningTestBase.resetPRObserverBeforeCalculateStartingBucketId());
member4.invoke(() -> FixedPartitioningTestBase.resetPRObserverBeforeCalculateStartingBucketId());
}
}
Aggregations