/** * Start the configuration of a <code>lru-memory-size</code> eviction controller. Allow for any of * the attributes to be missing. Store the attributes on the stack anticipating the declaration of * an {@link ObjectSizer}. * */ private void startLRUMemorySize(Attributes atts) { String lruAction = atts.getValue(ACTION); EvictionAction action = EvictionAction.DEFAULT_EVICTION_ACTION; if (lruAction != null) { action = EvictionAction.parseAction(lruAction); } String maximum = atts.getValue(MAXIMUM); int max = EvictionAttributes.DEFAULT_MEMORY_MAXIMUM; if (maximum != null) { max = parseInt(maximum); } // Store for later addition of ObjectSizer, if any (the cast is for clarity sake) stack.push(EvictionAttributes.createLRUMemoryAttributes(max, null, action)); }
public static PartitionedRegion createMockQueueRegion(GemFireCacheImpl cache, String regionName) { // Mock queue region PartitionedRegion queueRegion = mock(PartitionedRegion.class); when(queueRegion.getFullPath()).thenReturn(regionName); when(queueRegion.getPrStats()).thenReturn(mock(PartitionedRegionStats.class)); when(queueRegion.getDataStore()).thenReturn(mock(PartitionedRegionDataStore.class)); when(queueRegion.getCache()).thenReturn(cache); EvictionAttributesImpl ea = (EvictionAttributesImpl) EvictionAttributes .createLRUMemoryAttributes(100, null, EvictionAction.OVERFLOW_TO_DISK); EvictionController eviction = AbstractEvictionController.create(ea, false, cache.getDistributedSystem(), "queueRegion"); when(queueRegion.getEvictionController()).thenReturn(eviction); return queueRegion; }
public EvictionAttributes convertToEvictionAttributes() { EvictionAction action = EvictionAction.parseAction(evictionAction); ObjectSizer sizer; if (objectSizer != null) { try { Class<ObjectSizer> sizerClass = (Class<ObjectSizer>) ClassPathLoader.getLatest().forName(objectSizer); sizer = sizerClass.newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new IllegalArgumentException( "Unable to instantiate class " + objectSizer + " - " + e.toString()); } } else { sizer = ObjectSizer.DEFAULT; } if (maxMemory == null && maxEntryCount == null) { return EvictionAttributes.createLRUHeapAttributes(sizer, action); } else if (maxMemory != null) { return EvictionAttributes.createLRUMemoryAttributes(maxMemory, sizer, action); } else { return EvictionAttributes.createLRUEntryAttributes(maxEntryCount, action); } } }
/** * Byte arrays */ public static void main4(String[] args) throws Exception { DistributedSystem system = DistributedSystem.connect(new java.util.Properties()); Cache cache = CacheFactory.create(system); AttributesFactory factory = new AttributesFactory(); factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, (ObjectSizer) null, EvictionAction.OVERFLOW_TO_DISK)); LocalRegion region = (LocalRegion) cache.createRegion("TestDiskRegion", factory.create()); // DiskRegion dr = region.getDiskRegion(); // DiskRegionStats diskStats = dr.getStats(); // EvictionStatistics lruStats = getLRUStats(region); // int total; // for (total = 0; lruStats.getEvictions() > 100; total++) { // region.put(new Integer(total), String.valueOf(total).getBytes()); // } // for (int i = 0; i < total; i++) { // byte[] bytes = (byte[]) region.get(new Integer(i)); // Assert.assertTrue((new String(bytes)).equals(String.valueOf(i))); // } for (int i = 0; i < 100000; i++) { System.out.println(i); region.put(String.valueOf(i), String.valueOf(i).getBytes()); } }
factory.setDataPolicy(DataPolicy.REPLICATE); factory.setEvictionAttributes( EvictionAttributes.createLRUMemoryAttributes(100, null, EvictionAction.OVERFLOW_TO_DISK)); RegionAttributes attributes = factory.create();
/** * Filling up the region with keys and values */ public static void main5(String[] args) throws Exception { DistributedSystem system = DistributedSystem.connect(new java.util.Properties()); Cache cache = CacheFactory.create(system); AttributesFactory factory = new AttributesFactory(); factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, (ObjectSizer) null, EvictionAction.OVERFLOW_TO_DISK)); LocalRegion region = (LocalRegion) cache.createRegion("TestDiskRegion", factory.create()); // DiskRegion dr = region.getDiskRegion(); // DiskRegionStats diskStats = dr.getStats(); EvictionCounters lruStats = getLRUStats(region); for (int i = 0; i < 10000; i++) { int[] array = new int[1000]; array[0] = i; try { region.put(array, new Integer(i)); } catch (IllegalStateException ex) { System.out.println("Ran out of space: " + ex); return; } } String s = "Limit is " + lruStats.getLimit() + " evictions are " + lruStats.getEvictions(); throw new RuntimeException(s); }
public static void main1(String[] args) throws Exception { DistributedSystem system = DistributedSystem.connect(new java.util.Properties()); Cache cache = CacheFactory.create(system); AttributesFactory factory = new AttributesFactory(); factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, (ObjectSizer) null, EvictionAction.OVERFLOW_TO_DISK)); factory.setCacheListener(new CacheListenerAdapter() { @Override public void afterUpdate(EntryEvent event) { System.out.println("UPDATE: " + event.getKey() + " -> (" + event.getOldValue() + " -> " + event.getNewValue() + ")"); } }); LocalRegion region = (LocalRegion) cache.createRegion("TestDiskRegion", factory.create()); DiskRegion dr = region.getDiskRegion(); DiskRegionStats diskStats = dr.getStats(); EvictionCounters lruStats = getLRUStats(region); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Hit enter to perform action"); for (int i = 0; true; i++) { br.readLine(); // Thread.sleep(500); Object key = new Integer(i); Object value = new byte[200000]; region.put(key, value); System.out.println(key + " -> " + value + " evictions = " + lruStats.getEvictions() + ", writes = " + diskStats.getWrites()); } }
Cache cache = CacheFactory.create(system); AttributesFactory factory = new AttributesFactory(); factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, (ObjectSizer) null, EvictionAction.OVERFLOW_TO_DISK)); DiskStoreFactory dsf = cache.createDiskStoreFactory();
/** * Note: hydra invokes this with setRefid=false. */ public void setAttributes(RegionAttributes attrs, boolean setRefid) { this.hasAttributes = true; if (attrs instanceof RegionAttributesCreation) { this.attrs = (RegionAttributesCreation) attrs; } else { this.attrs = new RegionAttributesCreation(this.cache, attrs, false); } if ((setRefid && (this.attrs.getRefid() == null))) { this.attrs.setRefid(getRefid()); } if (attrs.getPartitionAttributes() != null && attrs.getEvictionAttributes() != null && attrs.getEvictionAttributes().getAlgorithm().isLRUMemory() && attrs.getPartitionAttributes().getLocalMaxMemory() != 0 && attrs.getEvictionAttributes() .getMaximum() != attrs.getPartitionAttributes().getLocalMaxMemory()) { getCache().getLogger().warning(String.format( "For region %s with data policy PARTITION, memory LRU eviction attribute maximum has been reset from %sMB to local-max-memory %sMB", new Object[] {this.getName(), attrs.getEvictionAttributes().getMaximum(), attrs.getPartitionAttributes().getLocalMaxMemory()})); this.attrs.setEvictionAttributes(attrs.getEvictionAttributes().createLRUMemoryAttributes( attrs.getPartitionAttributes().getLocalMaxMemory(), attrs.getEvictionAttributes().getObjectSizer(), attrs.getEvictionAttributes().getAction())); } }
EvictionAttributes createAttributes() { switch (type) { case HEAP_PERCENTAGE: if (threshold != null) { throw new IllegalArgumentException("HEAP_PERCENTAGE (LRU_HEAP algorithm) does not support threshold (a.k.a. maximum)!"); } return EvictionAttributes.createLRUHeapAttributes(getObjectSizer(), getAction()); case MEMORY_SIZE: return (threshold != null ? EvictionAttributes.createLRUMemoryAttributes(getThreshold(), getObjectSizer(), getAction()) : EvictionAttributes.createLRUMemoryAttributes(getObjectSizer(), getAction())); case ENTRY_COUNT: default: return (threshold != null ? EvictionAttributes.createLRUEntryAttributes(getThreshold(), getAction()) : EvictionAttributes.createLRUEntryAttributes(DEFAULT_LRU_MAXIMUM_ENTRIES, getAction())); } }
EvictionAttributes createAttributes() { switch (type) { case HEAP_PERCENTAGE: if (threshold != null) { throw new IllegalArgumentException("HEAP_PERCENTAGE (LRU_HEAP algorithm) does not support threshold (a.k.a. maximum)!"); } return EvictionAttributes.createLRUHeapAttributes(getObjectSizer(), getAction()); case MEMORY_SIZE: return (threshold != null ? EvictionAttributes.createLRUMemoryAttributes(getThreshold(), getObjectSizer(), getAction()) : EvictionAttributes.createLRUMemoryAttributes(getObjectSizer(), getAction())); case ENTRY_COUNT: default: return (threshold != null ? EvictionAttributes.createLRUEntryAttributes(getThreshold(), getAction()) : EvictionAttributes.createLRUEntryAttributes(DEFAULT_LRU_MAXIMUM_ENTRIES, getAction())); } }