Search in sources :

Example 1 with EntryExistsException

use of org.apache.geode.cache.EntryExistsException in project geode by apache.

the class RegionTestCase method testBadRegionAccess.

/**
   * Tests that creating or getting entries at an improper time throws exceptions.
   *
   * @see Region#get
   * @see Region#getEntry
   * @see Region#create
   */
@Test
public void testBadRegionAccess() throws CacheException {
    String name = this.getUniqueName();
    Region region = createRegion(name);
    Object key = name;
    assertNull(region.get(key));
    assertNull(region.getEntry(key));
    Integer value = new Integer(42);
    region.create(key, value);
    try {
        // partitioned regions are logging the EntryExistsException, so emit
        // a directive to ignore it
        region.getCache().getLogger().info("<ExpectedException action=add>" + "org.apache.geode.cache.EntryExistsException" + "</ExpectedException>");
        region.create(key, value);
        fail("Should have thrown an EntryExistsException");
    } catch (EntryExistsException ex) {
    // okay...
    } finally {
        region.getCache().getLogger().info("<ExpectedException action=remove>" + "org.apache.geode.cache.EntryExistsException" + "</ExpectedException>");
    }
}
Also used : LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) EntryExistsException(org.apache.geode.cache.EntryExistsException) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 2 with EntryExistsException

use of org.apache.geode.cache.EntryExistsException in project geode by apache.

the class PartitionedRegionDUnitTest method setupExtendedTest.

private void setupExtendedTest(final String regionName, final int numVals) {
    Host host = Host.getHost(0);
    SerializableRunnable createPR = new SerializableRunnable("createPartitionedRegion") {

        public void run() {
            try {
                createRegion(regionName, "root", getRegionAttributes());
            } catch (CacheException ex) {
                Assert.fail("While creating Partitioned region", ex);
            }
        }
    };
    for (int i = 1; i < 4; i++) {
        host.getVM(i).invoke(createPR);
    }
    VM vm0 = host.getVM(0);
    vm0.invoke(new SerializableRunnable("Populate Partitioned Region") {

        public void run() {
            Region region = null;
            try {
                region = createRegion(regionName, "root", getRegionAttributes());
                // since random keys are being used, we might hit duplicates
                region.getCache().getLogger().info("<ExpectedException action=add>" + "org.apache.geode.cache.EntryExistsException" + "</ExpectedException>");
                java.util.Random rand = new java.util.Random(System.currentTimeMillis());
                for (int i = 0; i < numVals; i++) {
                    boolean created = false;
                    while (!created) {
                        try {
                            int val = rand.nextInt(100000000);
                            String key = String.valueOf(val);
                            region.create(key, new Integer(val));
                            created = true;
                        } catch (EntryExistsException eee) {
                        // loop to try again
                        }
                    }
                }
            } catch (Exception ex) {
                Assert.fail("while creating or populating partitioned region", ex);
            } finally {
                if (region != null) {
                    region.getCache().getLogger().info("<ExpectedException action=remove>" + "org.apache.geode.cache.EntryExistsException" + "</ExpectedException>");
                }
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) Random(java.util.Random) EntryExistsException(org.apache.geode.cache.EntryExistsException) EntryExistsException(org.apache.geode.cache.EntryExistsException) CacheException(org.apache.geode.cache.CacheException) PartitionedRegionException(org.apache.geode.internal.cache.PartitionedRegionException) Random(java.util.Random) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region)

Example 3 with EntryExistsException

use of org.apache.geode.cache.EntryExistsException in project geode by apache.

the class PartitionedRegionAPIDUnitTest method partitionedRegionTest.

public void partitionedRegionTest(final String prName) {
    /*
     * Do put(), create(), invalidate() operations through VM with PR having both Accessor and
     * Datastore
     */
    // String exceptionStr = "";
    vm0.invoke(new CacheSerializableRunnable("doPutCreateInvalidateOperations1") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            final Region pr = cache.getRegion(prName);
            if (pr == null) {
                fail(prName + " not created");
            }
            int size = 0;
            // if (pr.getAttributes().getScope() == Scope.DISTRIBUTED_ACK) {
            size = pr.size();
            assertEquals("Size doesnt return expected value", 0, size);
            assertEquals("isEmpty doesnt return proper state of the PartitionedRegion", true, pr.isEmpty());
            assertEquals(0, pr.keySet().size());
            // }
            for (int i = putRange_1Start; i <= putRange_1End; i++) {
                // System.out.println("Putting entry for key = " + i);
                pr.put(Integer.toString(i), Integer.toString(i));
            }
            // if (pr.getAttributes().getScope() == Scope.DISTRIBUTED_ACK) {
            size = pr.size();
            assertEquals("Size doesn't return expected value", putRange_1End, size);
            assertEquals("isEmpty doesnt return proper state of the PartitionedRegion", false, pr.isEmpty());
            // Positive assertion of functionality in a distributed env.
            // For basic functional support (or lack of), please see
            // PartitionedRegionSingleNodeOperationsJUnitTest
            assertEquals(putRange_1End, pr.keySet().size());
            Set ks = pr.keySet();
            Iterator ksI = ks.iterator();
            while (ksI.hasNext()) {
                try {
                    ksI.remove();
                    fail("Expected key set iterator to be read only");
                } catch (Exception expected) {
                }
                Object key = ksI.next();
                assertEquals(String.class, key.getClass());
                Integer.parseInt((String) key);
            }
            try {
                ksI.remove();
                fail("Expected key set iterator to be read only");
            } catch (Exception expected) {
            }
            assertFalse(ksI.hasNext());
            try {
                ksI.next();
                fail("Expected no such element exception");
            } catch (NoSuchElementException expected) {
                assertFalse(ksI.hasNext());
            }
            // }
            String exceptionStr = ReplyException.class.getName() + "||" + EntryNotFoundException.class.getName();
            vm1.invoke(addExceptionTag1(exceptionStr));
            vm2.invoke(addExceptionTag1(exceptionStr));
            vm3.invoke(addExceptionTag1(exceptionStr));
            addExceptionTag1(exceptionStr);
            for (int i = putRange_1Start; i <= putRange_1End; i++) {
                // System.out.println("Putting entry for key = " + i);
                try {
                    pr.destroy(Integer.toString(i));
                } catch (EntryNotFoundException enfe) {
                    searchForKey((PartitionedRegion) pr, Integer.toString(i));
                    throw enfe;
                }
            }
            vm1.invoke(removeExceptionTag1(exceptionStr));
            vm2.invoke(removeExceptionTag1(exceptionStr));
            vm3.invoke(removeExceptionTag1(exceptionStr));
            removeExceptionTag1(exceptionStr);
            // if (pr.getAttributes().getScope() == Scope.DISTRIBUTED_ACK) {
            size = pr.size();
            assertEquals("Size doesnt return expected value = 0 instead it returns" + size, size, 0);
            assertEquals("isEmpty doesnt return proper state of the PartitionedRegion", pr.isEmpty(), true);
            // }
            for (int i = putRange_1Start; i <= putRange_1End; i++) {
                // System.out.println("Putting entry for key = " + i);
                pr.put(Integer.toString(i), Integer.toString(i));
            }
            // createInvalidateChange
            for (int i = createRange_1Start; i <= createRange_1End; i++) {
                Object val = null;
                Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                pr.create(key, val);
            }
            final String expectedExceptions = EntryExistsException.class.getName();
            getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
            exceptionStr = ReplyException.class.getName() + "||" + expectedExceptions;
            vm1.invoke(addExceptionTag1(exceptionStr));
            vm2.invoke(addExceptionTag1(exceptionStr));
            vm3.invoke(addExceptionTag1(exceptionStr));
            addExceptionTag1(exceptionStr);
            for (int i = createRange_1Start; i <= createRange_1End; i++) {
                Object val = null;
                Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                try {
                    pr.create(key, val);
                    fail("EntryExistsException is not thrown");
                } catch (EntryExistsException expected) {
                // cache.getLogger().fine("EntryExistsException is properly thrown");
                }
            }
            vm1.invoke(removeExceptionTag1(exceptionStr));
            vm2.invoke(removeExceptionTag1(exceptionStr));
            vm3.invoke(removeExceptionTag1(exceptionStr));
            removeExceptionTag1(exceptionStr);
            getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
            // if (pr.getAttributes().getScope() == Scope.DISTRIBUTED_ACK) {
            size = pr.size();
            assertEquals("Size doesnt return expected value", size, 10);
            // }
            LogWriterUtils.getLogWriter().fine("All the puts done successfully for vm0.");
            {
                PartitionedRegion ppr = (PartitionedRegion) pr;
                try {
                    ppr.dumpAllBuckets(true);
                } catch (ReplyException re) {
                    fail();
                }
            }
        }
    });
    /*
     * Do put(), create(), invalidate() operations through VM with PR having only Accessor(no data
     * store)
     */
    vm1.invoke(new CacheSerializableRunnable("doPutCreateInvalidateOperations2") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            Region pr = cache.getRegion(prName);
            if (pr == null) {
                fail("PR not created");
            }
            for (int i = putRange_2Start; i <= putRange_2End; i++) {
                // System.out.println("Putting entry for key = " + i);
                pr.put(Integer.toString(i), Integer.toString(i));
            }
            // createInvalidateChange
            for (int i = createRange_2Start; i <= createRange_2End; i++) {
                Object val = null;
                Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                pr.create(key, val);
            }
            final String entryExistsException = EntryExistsException.class.getName();
            getCache().getLogger().info("<ExpectedException action=add>" + entryExistsException + "</ExpectedException>");
            String exceptionStr = ReplyException.class.getName() + ":" + entryExistsException;
            vm0.invoke(addExceptionTag1(exceptionStr));
            vm2.invoke(addExceptionTag1(exceptionStr));
            vm3.invoke(addExceptionTag1(exceptionStr));
            addExceptionTag1(exceptionStr);
            for (int i = createRange_2Start; i <= createRange_2End; i++) {
                Object val = null;
                Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                try {
                    pr.create(key, val);
                    fail("EntryExistsException is not thrown");
                } catch (EntryExistsException expected) {
                // cache.getLogger().fine("EntryExistsException is properly thrown");
                }
            }
            vm0.invoke(removeExceptionTag1(exceptionStr));
            vm2.invoke(removeExceptionTag1(exceptionStr));
            vm3.invoke(removeExceptionTag1(exceptionStr));
            removeExceptionTag1(exceptionStr);
            getCache().getLogger().info("<ExpectedException action=remove>" + entryExistsException + "</ExpectedException>");
            cache.getLogger().fine("All the puts done successfully for vm1.");
        }
    });
    /*
     * Do destroy() operations through VM with PR having only Accessor(no data store). It also
     * verifies that EntryNotFoundException is thrown if the entry is already destroyed.
     */
    vm1.invoke(new CacheSerializableRunnable("doRemoveOperations1") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            Region pr = cache.getRegion(prName);
            if (pr == null) {
                fail("PR not created");
            }
            for (int i = removeRange_1Start; i <= removeRange_1End; i++) {
                // System.out.println("destroying entry for key = " + i);
                final String key = Integer.toString(i);
                try {
                    pr.destroy(key);
                } catch (EntryNotFoundException enfe) {
                    searchForKey((PartitionedRegion) pr, key);
                    throw enfe;
                }
            }
            final String entryNotFoundException = EntryNotFoundException.class.getName();
            getCache().getLogger().info("<ExpectedException action=add>" + entryNotFoundException + "</ExpectedException>");
            String exceptionStr = ReplyException.class.getName() + "||" + entryNotFoundException;
            vm0.invoke(addExceptionTag1(exceptionStr));
            vm2.invoke(addExceptionTag1(exceptionStr));
            vm3.invoke(addExceptionTag1(exceptionStr));
            addExceptionTag1(exceptionStr);
            for (int i = removeRange_1Start; i <= removeRange_1End; i++) {
                final String key = Integer.toString(i);
                try {
                    pr.destroy(key);
                    fail("EntryNotFoundException is not thrown in destroy operation for key = " + i);
                } catch (EntryNotFoundException expected) {
                }
            }
            vm0.invoke(removeExceptionTag1(exceptionStr));
            vm2.invoke(removeExceptionTag1(exceptionStr));
            vm3.invoke(removeExceptionTag1(exceptionStr));
            removeExceptionTag1(exceptionStr);
            getCache().getLogger().info("<ExpectedException action=remove>" + entryNotFoundException + "</ExpectedException>");
            LogWriterUtils.getLogWriter().fine("All the remove done successfully for vm0.");
        }
    });
    /*
     * Do more put(), create(), invalidate() operations through VM with PR having Accessor + data
     * store
     */
    vm2.invoke(new CacheSerializableRunnable("doPutCreateInvalidateOperations3") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            Region pr = cache.getRegion(prName);
            assertNotNull("PR not created", pr);
            for (int i = putRange_3Start; i <= putRange_3End; i++) {
                // System.out.println("Putting entry for key = " + i);
                pr.put(Integer.toString(i), Integer.toString(i));
            }
            for (int i = createRange_3Start; i <= createRange_3End; i++) {
                Object val = null;
                Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                pr.create(key, val);
            }
            final String entryExistsException = EntryExistsException.class.getName();
            getCache().getLogger().info("<ExpectedException action=add>" + entryExistsException + "</ExpectedException>");
            String exceptionStr = ReplyException.class.getName() + "||" + entryExistsException;
            vm0.invoke(addExceptionTag1(exceptionStr));
            vm1.invoke(addExceptionTag1(exceptionStr));
            vm3.invoke(addExceptionTag1(exceptionStr));
            addExceptionTag1(exceptionStr);
            for (int i = createRange_3Start; i <= createRange_3End; i++) {
                Object val = null;
                Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                try {
                    pr.create(key, val);
                    fail("EntryExistsException is not thrown");
                } catch (EntryExistsException expected) {
                // getLogWriter().fine("EntryExistsException is properly thrown");
                }
            }
            vm0.invoke(removeExceptionTag1(exceptionStr));
            vm1.invoke(removeExceptionTag1(exceptionStr));
            vm3.invoke(removeExceptionTag1(exceptionStr));
            removeExceptionTag1(exceptionStr);
            getCache().getLogger().info("<ExpectedException action=remove>" + entryExistsException + "</ExpectedException>");
        }
    });
    /*
     * Do more remove() operations through VM with PR having Accessor + data store
     */
    vm2.invoke(new CacheSerializableRunnable("doRemoveOperations2") {

        public void run2() throws CacheException {
            int i = 0;
            Cache cache = getCache();
            Region pr = cache.getRegion(prName);
            assertNotNull("PR not created", pr);
            if (pr == null) {
                fail("PR not created");
            }
            String key;
            for (i = removeRange_2Start; i <= removeRange_2End; i++) {
                // System.out.println("destroying entry for key = " + i);
                key = Integer.toString(i);
                try {
                    pr.destroy(key);
                } catch (EntryNotFoundException enfe) {
                    searchForKey((PartitionedRegion) pr, key);
                    throw enfe;
                }
            }
            final String entryNotFound = EntryNotFoundException.class.getName();
            getCache().getLogger().info("<ExpectedException action=add>" + entryNotFound + "</ExpectedException>");
            String exceptionStr = ReplyException.class.getName() + "||" + entryNotFound;
            vm0.invoke(addExceptionTag1(exceptionStr));
            vm1.invoke(addExceptionTag1(exceptionStr));
            vm3.invoke(addExceptionTag1(exceptionStr));
            addExceptionTag1(exceptionStr);
            for (i = removeRange_2Start; i <= removeRange_2End; i++) {
                // System.out.println("destroying entry for key = " + i);
                try {
                    pr.destroy(Integer.toString(i));
                    fail("EntryNotFoundException is not thrown in destroy operation for key = " + (Integer.toString(i)));
                } catch (EntryNotFoundException expected) {
                }
            }
            vm0.invoke(removeExceptionTag1(exceptionStr));
            vm1.invoke(removeExceptionTag1(exceptionStr));
            vm3.invoke(removeExceptionTag1(exceptionStr));
            removeExceptionTag1(exceptionStr);
            getCache().getLogger().info("<ExpectedException action=remove>" + entryNotFound + "</ExpectedException>");
        }
    });
    /*
     * Do more put() operations through VM with PR having only Accessor
     */
    vm3.invoke(new CacheSerializableRunnable("doPutCreateInvalidateOperations4") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            Region pr = cache.getRegion(prName);
            assertNotNull("PR not created", pr);
            for (int i = putRange_4Start; i <= putRange_4End; i++) {
                // System.out.println("Putting entry for key = " + i);
                pr.put(Integer.toString(i), Integer.toString(i));
            }
            for (int i = createRange_4Start; i <= createRange_4End; i++) {
                Object val = null;
                final Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                pr.create(key, val);
            }
            final String entryExistsException = EntryExistsException.class.getName();
            getCache().getLogger().info("<ExpectedException action=add>" + entryExistsException + "</ExpectedException>");
            String exceptionStr = ReplyException.class.getName() + "||" + entryExistsException;
            vm0.invoke(addExceptionTag1(exceptionStr));
            vm1.invoke(addExceptionTag1(exceptionStr));
            vm2.invoke(addExceptionTag1(exceptionStr));
            addExceptionTag1(exceptionStr);
            for (int i = createRange_4Start; i <= createRange_4End; i++) {
                Object val = null;
                final Object key = Integer.toString(i);
                if (i % 2 == 0) {
                    val = Integer.toString(i);
                }
                try {
                    pr.create(key, val);
                    fail("EntryExistsException is not thrown");
                } catch (EntryExistsException expected) {
                // getLogWriter().fine("EntryExistsException is properly thrown");
                }
            }
            vm0.invoke(removeExceptionTag1(exceptionStr));
            vm1.invoke(removeExceptionTag1(exceptionStr));
            vm2.invoke(removeExceptionTag1(exceptionStr));
            removeExceptionTag1(exceptionStr);
            getCache().getLogger().info("<ExpectedException action=remove>" + entryExistsException + "</ExpectedException>");
        }
    });
    /*
     * validate the data in PartionedRegion at different VM's
     * 
     */
    CacheSerializableRunnable validateRegionAPIs = new CacheSerializableRunnable("validateInserts") {

        public void run2() {
            Cache cache = getCache();
            Region pr = cache.getRegion(prName);
            assertNotNull("PR not created", pr);
            // Validation with get() operation.
            for (int i = putRange_1Start; i <= putRange_4End; i++) {
                Object val = pr.get(Integer.toString(i));
                if ((i >= removeRange_1Start && i <= removeRange_1End) || (i >= removeRange_2Start && i <= removeRange_2End)) {
                    assertNull("Remove validation failed for key " + i, val);
                } else {
                    assertNotNull("put() not done for key " + i, val);
                }
            }
            // validation with containsKey() operation.
            for (int i = putRange_1Start; i <= putRange_4End; i++) {
                boolean conKey = pr.containsKey(Integer.toString(i));
                if ((i >= removeRange_1Start && i <= removeRange_1End) || (i >= removeRange_2Start && i <= removeRange_2End)) {
                    assertFalse("containsKey() remove validation failed for key = " + i, conKey);
                } else {
                    assertTrue("containsKey() Validation failed for key = " + i, conKey);
                }
                LogWriterUtils.getLogWriter().fine("containsKey() Validated entry for key = " + i);
            }
            // validation with containsValueForKey() operation
            for (int i = putRange_1Start; i <= putRange_4End; i++) {
                boolean conKey = pr.containsValueForKey(Integer.toString(i));
                if ((i >= removeRange_1Start && i <= removeRange_1End) || (i >= removeRange_2Start && i <= removeRange_2End)) {
                    assertFalse("containsValueForKey() remove validation failed for key = " + i, conKey);
                } else {
                    assertTrue("containsValueForKey() Validation failed for key = " + i, conKey);
                }
                LogWriterUtils.getLogWriter().fine("containsValueForKey() Validated entry for key = " + i);
            }
        }
    };
    // validate the data from all the VM's
    vm0.invoke(validateRegionAPIs);
    vm1.invoke(validateRegionAPIs);
    vm2.invoke(validateRegionAPIs);
    vm3.invoke(validateRegionAPIs);
    /*
     * destroy the Region.
     */
    vm0.invoke(new CacheSerializableRunnable("destroyRegionOp") {

        public void run2() {
            Cache cache = getCache();
            Region pr = cache.getRegion(prName);
            assertNotNull("Region already destroyed.", pr);
            pr.destroyRegion();
            assertTrue("Region isDestroyed false", pr.isDestroyed());
            assertNull("Region not destroyed.", cache.getRegion(prName));
        }
    });
    /*
     * validate the data after the region.destroy() operation.
     */
    CacheSerializableRunnable validateAfterRegionDestroy = new CacheSerializableRunnable("validateInsertsAfterRegionDestroy") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            Region pr = null;
            pr = cache.getRegion(prName);
            assertNull("Region not destroyed.", pr);
            Region rootRegion = cache.getRegion(Region.SEPARATOR + PartitionedRegionHelper.PR_ROOT_REGION_NAME);
            // Verify allPartitionedRegion.
            // Region allPrs = rootRegion
            // .getSubregion(PartitionedRegionHelper.PARTITIONED_REGION_CONFIG_NAME);
            Object configObj = rootRegion.get(prName.substring(1));
            if (configObj != null) {
                fail("PRConfig found in allPartitionedRegion Metadata for this PR.");
            }
            // Verify b2n region.
            // Region b2nReg = rootRegion
            // .getSubregion(PartitionedRegionHelper.BUCKET_2_NODE_TABLE_PREFIX);
            // if (b2nReg != null) {
            // fail("PRConfig found in allPartitionedRegion Metadata for this PR.");
            // }
            // Verify bucket Regions.
            Set subreg = rootRegion.subregions(false);
            for (java.util.Iterator itr = subreg.iterator(); itr.hasNext(); ) {
                Region reg = (Region) itr.next();
                String name = reg.getName();
                if ((name.indexOf(PartitionedRegionHelper.BUCKET_REGION_PREFIX)) != -1) {
                    fail("Bucket exists. Bucket = " + name);
                }
            }
            // verify prIdToPr Map.
            boolean con = PartitionedRegion.prIdToPR.containsKey("PR1");
            if (con == true) {
                fail("prIdToPR contains pr reference ");
            }
        }
    };
    // validateAfterRegionDestory from all VM's
    vm0.invoke(validateAfterRegionDestroy);
    vm1.invoke(validateAfterRegionDestroy);
    vm2.invoke(validateAfterRegionDestroy);
    vm3.invoke(validateAfterRegionDestroy);
}
Also used : Set(java.util.Set) Iterator(java.util.Iterator) CacheException(org.apache.geode.cache.CacheException) EntryExistsException(org.apache.geode.cache.EntryExistsException) ReplyException(org.apache.geode.distributed.internal.ReplyException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryExistsException(org.apache.geode.cache.EntryExistsException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) NoSuchElementException(java.util.NoSuchElementException) CacheException(org.apache.geode.cache.CacheException) ReplyException(org.apache.geode.distributed.internal.ReplyException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Iterator(java.util.Iterator) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) NoSuchElementException(java.util.NoSuchElementException) Cache(org.apache.geode.cache.Cache)

Example 4 with EntryExistsException

use of org.apache.geode.cache.EntryExistsException in project geode by apache.

the class PartitionedRegion method createRemotely.

/**
   * Creates the key/value pair into the remote target that is managing the key's bucket.
   * 
   * @param recipient member id of the recipient of the operation
   * @param bucketId the id of the bucket that the key hashed to
   * @param event the event prompting this request
   * @throws PrimaryBucketException if the bucket on that node is not the primary copy
   * @throws ForceReattemptException if the peer is no longer available
   */
private boolean createRemotely(DistributedMember recipient, Integer bucketId, EntryEventImpl event, boolean requireOldValue) throws PrimaryBucketException, ForceReattemptException {
    boolean ret = false;
    long eventTime = event.getEventTime(0L);
    PutMessage.PutResponse reply = (PutMessage.PutResponse) PutMessage.send(recipient, this, event, // expectedOldValue
    eventTime, // expectedOldValue
    true, // expectedOldValue
    false, // expectedOldValue
    null, requireOldValue);
    PutResult pr = null;
    if (reply != null) {
        this.prStats.incPartitionMessagesSent();
        try {
            pr = reply.waitForResult();
            event.setOperation(pr.op);
            event.setVersionTag(pr.versionTag);
            if (requireOldValue) {
                event.setOldValue(pr.oldValue, true);
            }
            ret = pr.returnValue;
        } catch (EntryExistsException ignore) {
            // This might not be necessary and is here for safety sake
            ret = false;
        } catch (TransactionDataNotColocatedException tdnce) {
            throw tdnce;
        } catch (TransactionDataRebalancedException e) {
            throw e;
        } catch (CacheException ce) {
            throw new PartitionedRegionException(LocalizedStrings.PartitionedRegion_CREATE_OF_ENTRY_ON_0_FAILED.toLocalizedString(recipient), ce);
        } catch (RegionDestroyedException rde) {
            if (logger.isDebugEnabled()) {
                logger.debug("createRemotely: caught exception", rde);
            }
            throw new RegionDestroyedException(toString(), getFullPath());
        }
    }
    return ret;
}
Also used : CacheException(org.apache.geode.cache.CacheException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) EntryExistsException(org.apache.geode.cache.EntryExistsException) PutResult(org.apache.geode.internal.cache.partitioned.PutMessage.PutResult) PutMessage(org.apache.geode.internal.cache.partitioned.PutMessage) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException)

Example 5 with EntryExistsException

use of org.apache.geode.cache.EntryExistsException in project geode by apache.

the class PRQueryDUnitHelper method getCacheSerializableRunnableForPRRandomOps.

/**
   * This function puts portfolio objects into the created Region (PR or RR). Also, other operation
   * like, invalidate, destroy and create are performed in random manner based on
   * {@link Random#nextInt(int)}.
   * 
   * @param regionName
   * @param to
   * @param from
   * @return cacheSerializable object
   */
public CacheSerializableRunnable getCacheSerializableRunnableForPRRandomOps(final String regionName, final int from, final int to) {
    SerializableRunnable prPuts = new CacheSerializableRunnable("PRPuts") {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region region = cache.getRegion(regionName);
            for (int i = 0; i < 3; i++) {
                for (int j = from; j < to; j++) {
                    int op = new Random().nextInt(4);
                    try {
                        switch(op) {
                            case 0:
                                // Put operation
                                region.put(new Integer(j), new Portfolio(j));
                                break;
                            case 1:
                                // invalidate
                                if (region.containsKey(new Integer(j))) {
                                    region.invalidate(new Integer(j));
                                }
                                break;
                            case 2:
                                if (region.containsKey(new Integer(j))) {
                                    region.destroy(new Integer(j));
                                }
                                break;
                            case 3:
                                if (!region.containsKey(new Integer(j))) {
                                    region.create(new Integer(j), null);
                                }
                                break;
                            default:
                                break;
                        }
                    } catch (EntryExistsException e) {
                        // Do nothing let it go
                        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("EntryExistsException was thrown for key " + j);
                    } catch (EntryNotFoundException e) {
                        // Do nothing let it go
                        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("EntryNotFoundException was thrown for key " + j);
                    }
                }
            }
        }
    };
    return (CacheSerializableRunnable) prPuts;
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Random(java.util.Random) Portfolio(org.apache.geode.cache.query.data.Portfolio) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) EntryExistsException(org.apache.geode.cache.EntryExistsException) Cache(org.apache.geode.cache.Cache)

Aggregations

EntryExistsException (org.apache.geode.cache.EntryExistsException)11 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)6 Region (org.apache.geode.cache.Region)6 CacheException (org.apache.geode.cache.CacheException)5 Test (org.junit.Test)5 LocalRegion (org.apache.geode.internal.cache.LocalRegion)4 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)4 Cache (org.apache.geode.cache.Cache)3 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 Random (java.util.Random)2 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)2 CommitConflictException (org.apache.geode.cache.CommitConflictException)2 EntryEvent (org.apache.geode.cache.EntryEvent)2 TransactionId (org.apache.geode.cache.TransactionId)2 TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)2 TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1