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

The following examples show how to use com.gemstone.gemfire.cache.Region#remove() . 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: MultipleDiskRegions.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void removeObject(String regionName) {
  try {

    Object key = null;
    Region region = cache.getRegion(regionName);

    if (region != null) {
      int removeKeyInt = (int) MapBB.getBB().getSharedCounters().incrementAndRead(MapBB.NUM_REMOVE);
        key = ObjectHelper.createName(removeKeyInt);

        Object got = region.get(key);
        if (got == null) {
        Log.getLogWriter().info("removeObject: " + key + " does NOT exist in " + regionName);
        } else {
          region.remove(key);
        Log.getLogWriter().info("----performed remove operation on " + regionName + " with " + key);
      }
    }
  } catch (RegionDestroyedException rdex) {
    Log.getLogWriter().info("RegionDestroyedException...may occur in concurrent environment. Continuing with test.");
    recoverRegion(regionName);
  } catch (Exception ex) {
    throw new TestException(TestHelper.getStackTrace(ex));
  }
}
 
Example 2
Source File: ConcCQMultRegionsClient.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void removeObject() {   

    String key = null;
    String[] regionNames = MapPrms.getRegionNames();
    long counter = CQUtilBB.getBB().getSharedCounters().incrementAndRead(CQUtilBB.NumCounterToDestroy);
    // no duplicate removal during the test, 
    
    for (int i = 0; i < regionNames.length; i++) {
      String regionName = regionNames[i];
      Region region = RegionHelper.getRegion(regionName);
      if (region != null) {        
        key = NameFactory.getObjectNameForCounter(counter);
        Object got = region.get(key); 
        if (got != null) {
          region.remove(key);
          Log.getLogWriter().info("successfully removed this key: " + key);
          MapBB.getBB().getSharedCounters().incrementAndRead(MapBB.NUM_REMOVE);
          return;
       }
      }
    }    
  }
 
Example 3
Source File: ConcCQMultRegionsClient.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void removeObject() {   

    String key = null;
    String[] regionNames = MapPrms.getRegionNames();
    long counter = CQUtilBB.getBB().getSharedCounters().incrementAndRead(CQUtilBB.NumCounterToDestroy);
    // no duplicate removal during the test, 
    
    for (int i = 0; i < regionNames.length; i++) {
      String regionName = regionNames[i];
      Region region = RegionHelper.getRegion(regionName);
      if (region != null) {        
        key = NameFactory.getObjectNameForCounter(counter);
        Object got = region.get(key); 
        if (got != null) {
          region.remove(key);
          Log.getLogWriter().info("successfully removed this key: " + key);
          MapBB.getBB().getSharedCounters().incrementAndRead(MapBB.NUM_REMOVE);
          return;
       }
      }
    }    
  }
 
Example 4
Source File: WanFilterTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void removeEventFilterKeys(){
  Set rootRegions = CacheHelper.getCache().rootRegions();
  Iterator itr = rootRegions.iterator();
  while (itr.hasNext()) {      
    Region r = (Region)itr.next();
    logger.info("removeEventFilterKeys: removing all filter keys from region " + r.getFullPath());
    Iterator keysItr = r.keySet().iterator();
    while (keysItr.hasNext()){
      String k = (String)keysItr.next();
      if(k.contains(FILTER_KEY_PRIFIX)){
        logger.info("removeEventFilterKeys: removing filter key " + k + " from region " + r.getFullPath());
        r.remove(k);          
      }
    }      
  }    
}
 
Example 5
Source File: HAEventIdPropagationDUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * does a remove and return the eventid generated. Eventid is caught in the
 * listener and stored in a static variable*
 */
public static Object removePUTALL_KEY1()
{
  try {
    Region region = cache.getRegion(Region.SEPARATOR + REGION_NAME);
    assertNotNull(region);

    region.remove(PUTALL_KEY1);
    return eventId;
  }
  catch (Exception e) {
    fail("put failed due to " + e);
  }
  return null;
}
 
Example 6
Source File: MultipleDiskRegions.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void cm_remove(String regionName) {
  try {

    Object key = null;
    Region region = cache.getRegion(regionName);

    if (region != null) {

      // ConcurrentMap operations are not supported for peers with EMPTY or NORMAL dataPolicy
      DataPolicy dataPolicy = region.getAttributes().getDataPolicy();
      if (dataPolicy.equals(DataPolicy.NORMAL) || dataPolicy.equals(DataPolicy.EMPTY)) {
        return;
        }

      int removeKeyInt = (int) MapBB.getBB().getSharedCounters().incrementAndRead(MapBB.NUM_REMOVE);
      key = ObjectHelper.createName(removeKeyInt);

      Object got = region.get(key);
      if (got == null) {
        Log.getLogWriter().info("cm_remove: " + key + " does NOT exist in " + regionName);
      } else {
        boolean removed = region.remove(key, got);
        Log.getLogWriter().info("----performed remove operation on " + regionName + " with " + key + " previous value " + got + ".  remove returned " + removed);
    }
    }
  } catch (RegionDestroyedException rdex) {
    Log.getLogWriter().info("RegionDestroyedException...may occur in concurrent environment. Continuing with test.");
    recoverRegion(regionName);
  } catch (Exception ex) {
    throw new TestException(TestHelper.getStackTrace(ex));
  }
}
 
Example 7
Source File: EventIDVerificationDUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static void remove()
{
  try {
    Region r = cache.getRegion("/" + REGION_NAME);
    assertNotNull(r);
    r.remove("key-1");
  }
  catch (Exception ex) {
    fail("test failed due to exception in remove ", ex);
  }
}
 
Example 8
Source File: ConcCQMultRegionsClient.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void removeObject(String regionName) {
  String key = null;
  long counter = CQUtilBB.getBB().getSharedCounters().incrementAndRead(CQUtilBB.NumCounterToDestroy);

  Region region = RegionHelper.getRegion(regionName);
  if (region != null) {        
    int temp = rnd.nextInt(2);
    key = (temp>0) ? thePrimarySecId : NameFactory.getObjectNameForCounter(counter);
    //key = NameFactory.getObjectNameForCounter(counter);
    if (counter <= 100) {
      MasterController.sleepForMs(1000);
    } //to avoid bug 35662 -- allow the put key event propogated to the p2p bridges first before destory 
    if (counter <= 100) {
      MasterController.sleepForMs(1000);
    } //to avoid bug 35662 -- allow the put key event propogated to the p2p bridges first before destory 
    if (counter <= 100) {
      MasterController.sleepForMs(1000);
    } //to avoid bug 35662 -- allow the put key event propogated to the p2p bridges first before destory 
    Object got = region.get(key); 
    if (got != null) {
      region.remove(key);
      Log.getLogWriter().info("successfully removed this key: " + key + " and value " + got.toString());
      if (createIndex) {
        if (testPrimaryIndex  && ((Position)got).getSecId().equals(thePrimarySecId)) {
          updateBBForRemoves(regionName, key);
        }
      }
      else {
        updateBBForRemoves(regionName, key);
      }
   }
  }    
}
 
Example 9
Source File: EventIDVerificationDUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static void remove()
{
  try {
    Region r = cache.getRegion("/" + REGION_NAME);
    assertNotNull(r);
    r.remove("key-1");
  }
  catch (Exception ex) {
    fail("test failed due to exception in remove ", ex);
  }
}
 
Example 10
Source File: DiskOfflineCompactionJUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testForceRollTwoEntriesWithUpdateAndDestroy()
    throws Exception {
  DiskStoreFactory dsf = cache.createDiskStoreFactory();
  dsf.setAutoCompact(false);
  String name = "testForceRollTwoEntriesWithUpdateAndDestroy";
  DiskStore diskStore = dsf.create(name);
  File crfFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_1.crf");
  File drfFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_1.drf");
  File krfFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_1.krf");
  File crf2File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_2.crf");
  File drf2File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_2.drf");
  File krf2File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_2.krf");
  File ifFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + ".if");
  AttributesFactory af = new AttributesFactory();
  af.setDiskStoreName(name);
  af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
  Region r = cache.createRegion("r", af.create());
  int extra_byte_num_per_entry = InternalDataSerializer.calculateBytesForTSandDSID(getDSID((LocalRegion)r));
  r.put("key0", "value0"); //extra key to keep oplog1 from being empty
  r.put("key1", "value1");
  r.put("key2", "value2");
  diskStore.forceRoll();
  r.put("key1", "update1");
  r.put("key2", "update2");
  r.remove("key2");
  cache.close();
  ds.disconnect();
  DiskStoreImpl.validate(name, diskStore.getDiskDirs());
  
  int crfsize = Oplog.OPLOG_DISK_STORE_REC_SIZE + Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE
  + getRVVSize(0, null, false) + Oplog.OPLOG_NEW_ENTRY_BASE_REC_SIZE;
  int createsize0 = getSize4Create(extra_byte_num_per_entry, "key0", "value0");
  int createsize1 = getSize4Create(extra_byte_num_per_entry, "key1", "value1");
  int createsize2 = getSize4Create(extra_byte_num_per_entry, "key2", "value2"); 
  int updatesize1 = getSize4UpdateWithKey(extra_byte_num_per_entry, "key1", "update1");
  int updatesize2 = getSize4UpdateWithKey(extra_byte_num_per_entry, "key2", "update2");
  int tombstonesize1 = getSize4TombstoneWithoutKey(extra_byte_num_per_entry);

  assertEquals(crfsize + createsize0 + createsize1 + createsize2, crfFile.length());
  assertEquals(Oplog.OPLOG_DISK_STORE_REC_SIZE+Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE+getRVVSize(0, null, true), drfFile.length());
  crfsize = Oplog.OPLOG_DISK_STORE_REC_SIZE + Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE
  + getRVVSize(1, new int[] {1}, false);
  assertEquals(crfsize + updatesize1 + updatesize2 + tombstonesize1, crf2File.length());
  assertEquals(Oplog.OPLOG_DISK_STORE_REC_SIZE+Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE+getRVVSize(1, new int[] {0}, true), drf2File.length());
  long originalIfLength = ifFile.length();

  DiskStoreImpl dsi = DiskStoreImpl.offlineCompact(name, diskStore.getDiskDirs(), false, -1);
  assertEquals(3, dsi.getDeadRecordCount());
  assertEquals(3, dsi.getLiveEntryCount());
  assertEquals(false, crfFile.exists());
  assertEquals(false, drfFile.exists());
  assertEquals(false, krfFile.exists());
  assertEquals(false, crf2File.exists());
  assertEquals(false, drf2File.exists());
  assertEquals(false, krf2File.exists());
  File crf3File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_3.crf");
  File drf3File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_3.drf");
  File krf3File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_3.krf");
  assertEquals(true, crf3File.exists());
  assertEquals(true, drf3File.exists());
  assertEquals(true, krf3File.exists());

  // after offline compaction, rvv is reset, and only 3 update-with-key, 
  // i.e. key0, key1, key2(tombstone) in _3.crf
  crfsize = Oplog.OPLOG_DISK_STORE_REC_SIZE + Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE
  + getRVVSize(1, new int[] {1}, false);
  int updatesize0 = getSize4UpdateWithKey(extra_byte_num_per_entry, "key0", "value0");
  tombstonesize1 = getSize4TombstoneWithKey(extra_byte_num_per_entry, "key2");
  assertEquals(crfsize + updatesize0 + updatesize1 + tombstonesize1, crf3File.length());
  assertEquals(Oplog.OPLOG_DISK_STORE_REC_SIZE+Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE+getRVVSize(1, new int[] {0}, true), drf3File.length());
  // Now we preallocate spaces for if files and also crfs and drfs. So the below check is not true any more.
  // See revision: r42359 and r42320
  /*
  if (originalIfLength <= ifFile.length()) {
    fail("expected " + ifFile.length() + " to be < " + originalIfLength);
  }
  */

  connectDSandCache();
  dsf = cache.createDiskStoreFactory();
  diskStore = dsf.create(name);
  af = new AttributesFactory();
  af.setDiskStoreName(name);
  af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
  r = cache.createRegion("r", af.create());
  assertEquals(2, r.size());
  assertEquals("value0", r.get("key0"));
  assertEquals("update1", r.get("key1"));

  // if test passed clean up files
  r.destroyRegion();
  diskStore.destroy();
}
 
Example 11
Source File: MyTransactionFunction.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void verifyTransactionRollback(RegionFunctionContext ctx) {
  Region custPR = ctx.getDataSet();
  Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.OrderPartitionedRegionName);
  CacheTransactionManager mgr = custPR.getCache()
      .getCacheTransactionManager();
  ArrayList args = (ArrayList)ctx.getArguments();
  CustId custId = (CustId)args.get(1);
  Customer newCus = (Customer)args.get(2);
  OrderId orderId = (OrderId)args.get(3);
  Order order = (Order)args.get(4);
  Customer oldCustomer = (Customer)custPR.get(custId);
  Order oldOrder = (Order)orderPR.get(orderId);
  mgr.begin();
  custPR.put(custId, newCus);
  Customer txCust = (Customer)custPR.get(custId);
  orderPR.put(orderId, order);
  Order txOrder = (Order)orderPR.get(orderId);
  Assert.assertTrue(newCus.equals(txCust), "Expected Customer to be:"
      + newCus + " but was:" + txCust);
  Assert.assertTrue(txOrder.equals(order), "Expected Order to be:" + order
      + " but was:" + txOrder);
  mgr.rollback();
  Customer commitedCust = (Customer)custPR.get(custId);
  Assert.assertTrue(oldCustomer.equals(commitedCust),
      "Expected Customer to be:" + oldCustomer + " but was:" + commitedCust);
  Order commitedOrder = (Order)orderPR.get(orderId);
  Assert.assertTrue(oldOrder.equals(commitedOrder), "Expected Order to be:"
      + oldOrder + " but was:" + commitedOrder);
  
  mgr.begin();
  Assert.assertTrue(custPR.remove(custId, oldCustomer));
  orderPR.replace(orderId, order);
  mgr.rollback();
  
  Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
  Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
  
  mgr.begin();
  Assert.assertTrue(custPR.replace(custId, oldCustomer, newCus));
  orderPR.remove(orderId, oldOrder);
  Assert.assertTrue(null == orderPR.putIfAbsent(orderId, order));
  mgr.rollback();
  Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
  Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
  
}
 
Example 12
Source File: WANClient.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/** Destroys numEntries randomKeys in REGION_NAME */
public static void destroyRandomEntriesTask() {
  Region region = RegionHelper.getRegion(REGION_NAME);
  Set aSet = region.keys();

  if (aSet.size() == 0) {
     Log.getLogWriter().info("destroyRandomEntryTask: No keys in region");
     return;
  }

  int numEntries = CacheClientPrms.getNumEntries();
  for (int i = 0; i < numEntries; i++) {
    aSet = region.keys();
    if (aSet.size() == 0) {
      Log.getLogWriter().info("destroyRandomEntriesTask: No keys remain to destroy");
      return;
    }

    Object[] keyList = aSet.toArray();
    int index = rand.nextInt(aSet.size() - 1);
    Log.getLogWriter().info("Number of keys in region = " + aSet.size());
    try {
       Object key = keyList[index];
       Log.getLogWriter().info("Destroying key " + key + " from region " + region.getName());
       if (TestConfig.tab().getRandGen().nextBoolean()) {
       region.destroy(key);
       } else {
         boolean removed = region.remove(key, region.get(key));
         if (!removed) { // might happen with concurrent execution, force destroy
           region.destroy(key);
         }
       }
       Log.getLogWriter().info("Done destroying key " + key + " from region " + region.getName());
    } catch (Exception e) {
       throw new TestException("destroyRandomEntryTask throws " + e + " " + TestHelper.getStackTrace(e));
    }
  }

  // allow time for distribution
  int sleepMs = CacheClientPrms.getSleepSec() * 1000;
  MasterController.sleepForMs( sleepMs );
}
 
Example 13
Source File: DiskRegOplogSwtchingAndRollerJUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testAsyncRollingHappening()
{
  try {
    DiskRegionProperties diskRegionProperties = new DiskRegionProperties();
    diskRegionProperties.setDiskDirs(dirs);
    diskRegionProperties.setMaxOplogSize(512);
    diskRegionProperties.setRolling(true);
    LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
    Region region = DiskRegionHelperFactory.getAsyncPersistOnlyRegion(cache,
        diskRegionProperties);

    CacheObserverHolder.setInstance(new CacheObserverAdapter() {
      public void beforeGoingToCompact()
      {
        synchronized (forWaitNotify) {
          forWaitNotify.notify();
          gotNotification = true;
        }
      }
    });

    
    //This will end up 50% garbage
    //1 live create
    //2 live tombstones
    //2 garbage creates and 1 garbage modify
    region.put("k1", "v1");
    region.put("k2", "v2");
    region.put("k3", "v3");
    ((LocalRegion)region).forceFlush(); // so that the next modify doesn't conflate
    region.put("k2", "v3");
    ((LocalRegion)region).forceFlush(); // so that the next 2 removes don't conflate
    region.remove("k1");
    region.remove("k2");
    ((LocalRegion)region).forceFlush();

    region.forceRolling();

    synchronized (forWaitNotify) {
      if (!gotNotification) {
        forWaitNotify.wait(10000);
      }
    }

    if (!gotNotification) {
      fail("Expected rolling to have happened but did not happen");
    }

  }
  catch (Exception e) {
    fail(" tests failed due to " + e);
  }

  LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
}
 
Example 14
Source File: IndexStatisticsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testCompactRangeIndexNumKeysStats() throws Exception {
  String regionName = "testCompactRegionIndexNumKeysStats_region";
  Region region = CacheUtils.createRegion(regionName, Numbers.class);

  Index index = qs.createIndex("idIndexName", "r.max1", "/" + regionName
      + " r");
  IndexStatistics stats = index.getStatistics();

  // Add an object and check stats
  Numbers obj1 = new Numbers(1);
  obj1.max1 = 20;
  region.put(1, obj1);
  assertEquals(1, stats.getNumberOfValues());
  assertEquals(1, stats.getNumberOfKeys());
  // assertEquals(1, stats.getNumberOfValues(20f));
  assertEquals(1, stats.getNumUpdates());

  // add a second object with the same index key
  Numbers obj2 = new Numbers(1);
  obj2.max1 = 20;
  region.put(2, obj2);
  assertEquals(2, stats.getNumberOfValues());
  assertEquals(1, stats.getNumberOfKeys());
  // assertEquals(2, stats.getNumberOfValues(20f));
  assertEquals(2, stats.getNumUpdates());

  // remove the second object and check that keys are 1
  region.remove(2);
  assertEquals(1, stats.getNumberOfValues());
  assertEquals(1, stats.getNumberOfKeys());
  // assertEquals(1, stats.getNumberOfValues(20f));
  assertEquals(3, stats.getNumUpdates());

  // remove the first object and check that keys are 0
  region.remove(1);
  assertEquals(0, stats.getNumberOfValues());
  assertEquals(0, stats.getNumberOfKeys());
  // assertEquals(0, stats.getNumberOfValues(20f));
  assertEquals(4, stats.getNumUpdates());

  // add object with a different key and check results
  obj2.max1 = 21;
  region.put(3, obj2);
  assertEquals(1, stats.getNumberOfValues());
  assertEquals(1, stats.getNumberOfKeys());
  // assertEquals(0, stats.getNumberOfValues(20f));
  assertEquals(5, stats.getNumUpdates());

  // add object with original key and check that num keys are 2
  obj1.max1 = 20;
  region.put(1, obj1);
  assertEquals(2, stats.getNumberOfValues());
  assertEquals(2, stats.getNumberOfKeys());
  // assertEquals(1, stats.getNumberOfValues(20f));
  assertEquals(6, stats.getNumUpdates());
}
 
Example 15
Source File: WANClient.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/** Destroys numEntries randomKeys in REGION_NAME */
public static void destroyRandomEntriesTask() {
  Region region = RegionHelper.getRegion(REGION_NAME);
  Set aSet = region.keys();

  if (aSet.size() == 0) {
     Log.getLogWriter().info("destroyRandomEntryTask: No keys in region");
     return;
  }

  int numEntries = CacheClientPrms.getNumEntries();
  for (int i = 0; i < numEntries; i++) {
    aSet = region.keys();
    if (aSet.size() == 0) {
      Log.getLogWriter().info("destroyRandomEntriesTask: No keys remain to destroy");
      return;
    }

    Object[] keyList = aSet.toArray();
    int index = rand.nextInt(aSet.size() - 1);
    Log.getLogWriter().info("Number of keys in region = " + aSet.size());
    try {
       Object key = keyList[index];
       Log.getLogWriter().info("Destroying key " + key + " from region " + region.getName());
       if (TestConfig.tab().getRandGen().nextBoolean()) {
       region.destroy(key);
       } else {
         boolean removed = region.remove(key, region.get(key));
         if (!removed) { // might happen with concurrent execution, force destroy
           region.destroy(key);
         }
       }
       Log.getLogWriter().info("Done destroying key " + key + " from region " + region.getName());
    } catch (Exception e) {
       throw new TestException("destroyRandomEntryTask throws " + e + " " + TestHelper.getStackTrace(e));
    }
  }

  // allow time for distribution
  int sleepMs = CacheClientPrms.getSleepSec() * 1000;
  MasterController.sleepForMs( sleepMs );
}
 
Example 16
Source File: MyTransactionFunction.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void verifyTransactionRollback(RegionFunctionContext ctx) {
  Region custPR = ctx.getDataSet();
  Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.OrderPartitionedRegionName);
  CacheTransactionManager mgr = custPR.getCache()
      .getCacheTransactionManager();
  ArrayList args = (ArrayList)ctx.getArguments();
  CustId custId = (CustId)args.get(1);
  Customer newCus = (Customer)args.get(2);
  OrderId orderId = (OrderId)args.get(3);
  Order order = (Order)args.get(4);
  Customer oldCustomer = (Customer)custPR.get(custId);
  Order oldOrder = (Order)orderPR.get(orderId);
  mgr.begin();
  custPR.put(custId, newCus);
  Customer txCust = (Customer)custPR.get(custId);
  orderPR.put(orderId, order);
  Order txOrder = (Order)orderPR.get(orderId);
  Assert.assertTrue(newCus.equals(txCust), "Expected Customer to be:"
      + newCus + " but was:" + txCust);
  Assert.assertTrue(txOrder.equals(order), "Expected Order to be:" + order
      + " but was:" + txOrder);
  mgr.rollback();
  Customer commitedCust = (Customer)custPR.get(custId);
  Assert.assertTrue(oldCustomer.equals(commitedCust),
      "Expected Customer to be:" + oldCustomer + " but was:" + commitedCust);
  Order commitedOrder = (Order)orderPR.get(orderId);
  Assert.assertTrue(oldOrder.equals(commitedOrder), "Expected Order to be:"
      + oldOrder + " but was:" + commitedOrder);
  
  mgr.begin();
  Assert.assertTrue(custPR.remove(custId, oldCustomer));
  orderPR.replace(orderId, order);
  mgr.rollback();
  
  Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
  Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
  
  mgr.begin();
  Assert.assertTrue(custPR.replace(custId, oldCustomer, newCus));
  orderPR.remove(orderId, oldOrder);
  Assert.assertTrue(null == orderPR.putIfAbsent(orderId, order));
  mgr.rollback();
  Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
  Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
  
}
 
Example 17
Source File: DiskRegOplogSwtchingAndRollerJUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testAsyncRollingHappening()
{
  try {
    DiskRegionProperties diskRegionProperties = new DiskRegionProperties();
    diskRegionProperties.setDiskDirs(dirs);
    diskRegionProperties.setMaxOplogSize(512);
    diskRegionProperties.setRolling(true);
    LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
    Region region = DiskRegionHelperFactory.getAsyncPersistOnlyRegion(cache,
        diskRegionProperties);

    CacheObserverHolder.setInstance(new CacheObserverAdapter() {
      public void beforeGoingToCompact()
      {
        synchronized (forWaitNotify) {
          forWaitNotify.notify();
          gotNotification = true;
        }
      }
    });

    
    //This will end up 50% garbage
    //1 live create
    //2 live tombstones
    //2 garbage creates and 1 garbage modify
    region.put("k1", "v1");
    region.put("k2", "v2");
    region.put("k3", "v3");
    ((LocalRegion)region).forceFlush(); // so that the next modify doesn't conflate
    region.put("k2", "v3");
    ((LocalRegion)region).forceFlush(); // so that the next 2 removes don't conflate
    region.remove("k1");
    region.remove("k2");
    ((LocalRegion)region).forceFlush();

    region.forceRolling();

    synchronized (forWaitNotify) {
      if (!gotNotification) {
        forWaitNotify.wait(10000);
      }
    }

    if (!gotNotification) {
      fail("Expected rolling to have happened but did not happen");
    }

  }
  catch (Exception e) {
    fail(" tests failed due to " + e);
  }

  LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
}
 
Example 18
Source File: DiskRegOplogSwtchingAndRollerJUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * DiskRegionRollingJUnitTest :
 * 
 *  
 */
public void testSyncRollingHappening()
{
  try {
    DiskRegionProperties diskRegionProperties = new DiskRegionProperties();
    diskRegionProperties.setDiskDirs(dirs);
    diskRegionProperties.setMaxOplogSize(512);
    diskRegionProperties.setRolling(true);
    LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
    Region region = DiskRegionHelperFactory.getSyncPersistOnlyRegion(cache,
        diskRegionProperties, Scope.LOCAL);

    CacheObserverHolder.setInstance(new CacheObserverAdapter() {
      public void beforeGoingToCompact()
      {
        synchronized (forWaitNotify) {
          forWaitNotify.notify();
          gotNotification = true;
        }
      }
    });

    //This will end up 50% garbage
    //1 live create
    //2 live tombstones
    //2 garbage creates and 1 garbage modify
    region.put("k1", "v1");
    region.put("k2", "v2");
    region.put("k2", "v3");
    region.put("k3", "v3");
    region.remove("k1");
    region.remove("k2");
    

    region.forceRolling();

    synchronized (forWaitNotify) {
      if (!gotNotification) {
        forWaitNotify.wait(10000);
      }
    }

    if (!gotNotification) {
      fail("Expected rolling to have happened but did not happen");
    }

  }
  catch (Exception e) {
    fail(" tests failed due to " + e);
  }
  LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
}
 
Example 19
Source File: DiskRegionJUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * When max-dir-size is exceeded and compaction is enabled we allow oplogs to
 * keep getting created. Make sure that when they do they do not keep putting
 * one op per oplog (which is caused by bug 42464).
 */
public void testBug42464() {
  putsHaveStarted = false;
  DiskRegionProperties diskRegionProperties = new DiskRegionProperties();
  File[] myDirs = new File[] { dirs[0] };
  int[] dirSizes = { 900 };
  diskRegionProperties.setDiskDirsAndSizes(myDirs, dirSizes);
  diskRegionProperties.setMaxOplogSize(500);
  diskRegionProperties.setRolling(true);
  diskRegionProperties.setOverFlowCapacity(1);
  Region region = DiskRegionHelperFactory.getSyncOverFlowOnlyRegion(cache,
      diskRegionProperties);
  DiskStore ds = ((LocalRegion) region).getDiskStore();
  DiskStoreImpl dsi = (DiskStoreImpl) ds;
  if (!Arrays.equals(dirSizes, ds.getDiskDirSizes())) {
    fail("expected=" + Arrays.toString(dirSizes) + " actual="
        + Arrays.toString(ds.getDiskDirSizes()));
  }
  // One entry is kept in memory
  // since the crf max is 500 we should only be able to have 4 entries. The
  // 5th should not fit because of record overhead.
  for (int i = 0; i <= 9; i++) {
    region.getCache().getLogger().info("putting " + i);
    region.put(new Integer(i), new byte[101]);
  }
  // At this point we should have two oplogs that are basically full
  // (they should each contain 4 entries) and a third oplog that
  // contains a single entry. But the 3rd one will end up also containing 4
  // entries.
  // TODO what is the max size of this 3rd oplog's crf? The first two crfs
  // will be close to 400 bytes each. So the max size of the 3rd oplog should
  // be close to 100.
  ArrayList<OverflowOplog> oplogs = dsi.testHookGetAllOverflowOplogs();
  assertEquals(3, oplogs.size());

  // TODO verify oplogs
  // Now make sure that further oplogs can hold 4 entries
  for (int j = 10; j <= 13; j++) {
    region.getCache().getLogger().info("putting " + j);
    region.put(new Integer(j), new byte[101]);
  }
  oplogs = dsi.testHookGetAllOverflowOplogs();
  assertEquals(4, oplogs.size());
  // now remove all entries and make sure old oplogs go away
  for (int i = 0; i <= 13; i++) {
    region.getCache().getLogger().info("removing " + i);
    region.remove(new Integer(i));
  }
  // give background compactor chance to remove oplogs
  oplogs = dsi.testHookGetAllOverflowOplogs();
  int retryCount = 20;
  while (oplogs.size() > 1 && retryCount > 0) {
    DistributedTestCase.staticPause(100);
    oplogs = dsi.testHookGetAllOverflowOplogs();
    retryCount--;
  }
  assertEquals(1, oplogs.size());
}
 
Example 20
Source File: DiskOfflineCompactionJUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void testbug41862() throws Exception {
  DiskStoreFactory dsf = cache.createDiskStoreFactory();
  dsf.setAutoCompact(false);
  String name = "testbug41862";
  DiskStore diskStore = dsf.create(name);
  File crfFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_1.crf");
  File drfFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_1.drf");
  File krfFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_1.krf");
  File ifFile = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + ".if");
  AttributesFactory af = new AttributesFactory();
  af.setDiskStoreName(name);
  af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
  Region r = cache.createRegion("r", af.create());
  int extra_byte_num_per_entry = InternalDataSerializer.calculateBytesForTSandDSID(getDSID((LocalRegion)r));
  r.create("key1", "value1");
  r.create("key2", "value2"); // to keep this oplog from going empty
  ((LocalRegion)r).getDiskStore().forceRoll();
  r.create("key3", "value3");
  r.remove("key1");
  cache.close();
  ds.disconnect();
  DiskStoreImpl.validate(name, diskStore.getDiskDirs());
  
  int crfsize = Oplog.OPLOG_DISK_STORE_REC_SIZE + Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE
  + getRVVSize(0, null, false) + Oplog.OPLOG_NEW_ENTRY_BASE_REC_SIZE;
  int createsize1 = getSize4Create(extra_byte_num_per_entry, "key1", "value1");
  int createsize2 = getSize4Create(extra_byte_num_per_entry, "key2", "value2"); 
  int createsize3 = getSize4Create(extra_byte_num_per_entry, "key3", "value3");
  // 1 tombstone with key
  int tombstonesize1 = getSize4TombstoneWithKey(extra_byte_num_per_entry, "key1");

  assertEquals(crfsize + createsize1 + createsize2, crfFile.length());
  assertEquals(Oplog.OPLOG_DISK_STORE_REC_SIZE+Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE+getRVVSize(0, null, true), drfFile.length());

  File crf2File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_2.crf");
  File drf2File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_2.drf");
  
  crfsize += (getRVVSize(1, new int[] {1}, false) - getRVVSize(0, null, false)); // adjust rvv size
  assertEquals(crfsize + createsize3 + tombstonesize1, crf2File.length());
  assertEquals(Oplog.OPLOG_DISK_STORE_REC_SIZE+Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE+getRVVSize(1, new int[] {0}, true), drf2File.length());
  long originalIfLength = ifFile.length();

  DiskStoreImpl dsi = DiskStoreImpl.offlineCompact(name, diskStore.getDiskDirs(), false, -1);
  assertEquals(1, dsi.getDeadRecordCount());
  assertEquals(3, dsi.getLiveEntryCount());
  assertEquals(false, crfFile.exists());
  assertEquals(false, drfFile.exists());
  assertEquals(false, krfFile.exists());
  
  // offline compaction did not change  _2.crf and _2.drf not changed.
  assertEquals(crfsize + createsize3 + tombstonesize1, crf2File.length());
  assertEquals(Oplog.OPLOG_DISK_STORE_REC_SIZE+Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE+getRVVSize(1, new int[] {0}, true), drf2File.length());

  // offline compaction reset rvv to be empty, create-entry becomes one update-with-key-entry in _3.crf,
  // since there's no creates, then there's no OPLOG_NEW_ENTRY_BASE_REC_SIZE
  crfsize = Oplog.OPLOG_DISK_STORE_REC_SIZE + Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE + getRVVSize(1, new int[] {1}, false);
  int updatesize1 = getSize4UpdateWithKey(extra_byte_num_per_entry, "key3", "value3");
  File crf3File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_3.crf");
  File drf3File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_3.drf");
  File krf3File = new File(diskStore.getDiskDirs()[0], "BACKUP" + name + "_3.krf");
  assertEquals(true, krf3File.exists());
  assertEquals(true, crf3File.exists());
  assertEquals(true, drf3File.exists());
  assertEquals(crfsize + updatesize1, crf3File.length());
  assertEquals(Oplog.OPLOG_DISK_STORE_REC_SIZE+Oplog.OPLOG_GEMFIRE_VERSION_REC_SIZE+getRVVSize(1, new int[] {0}, true), drf3File.length());
  assertEquals(originalIfLength, ifFile.length());

  connectDSandCache();
  dsf = cache.createDiskStoreFactory();
  diskStore = dsf.create(name);
  af = new AttributesFactory();
  af.setDiskStoreName(name);
  af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
  r = cache.createRegion("r", af.create());
  assertEquals(2, r.size());
  assertEquals("value2", r.get("key2"));
  assertEquals("value3", r.get("key3"));

  // if test passed clean up files
  r.destroyRegion();
  diskStore.destroy();
}