Search in sources :

Example 16 with Compressor

use of org.apache.geode.compression.Compressor in project geode by apache.

the class OffHeapRegionBase method doRegionTest.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void doRegionTest(final RegionShortcut rs, final String rName, boolean compressed) {
    boolean isPersistent = rs == RegionShortcut.LOCAL_PERSISTENT || rs == RegionShortcut.REPLICATE_PERSISTENT || rs == RegionShortcut.PARTITION_PERSISTENT;
    GemFireCacheImpl gfc = createCache(isPersistent);
    Region r = null;
    try {
        gfc.setCopyOnRead(true);
        final MemoryAllocator ma = gfc.getOffHeapStore();
        assertNotNull(ma);
        Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> assertEquals(0, ma.getUsedMemory()));
        Compressor compressor = null;
        if (compressed) {
            compressor = SnappyCompressor.getDefaultInstance();
        }
        r = gfc.createRegionFactory(rs).setOffHeap(true).setCompressor(compressor).create(rName);
        assertEquals(true, r.isEmpty());
        assertEquals(0, ma.getUsedMemory());
        Object data = new Integer(123456789);
        r.put("key1", data);
        // System.out.println("After put of Integer value off heap used memory=" +
        // ma.getUsedMemory());
        assertTrue(ma.getUsedMemory() == 0);
        assertEquals(data, r.get("key1"));
        r.invalidate("key1");
        assertEquals(0, ma.getUsedMemory());
        r.put("key1", data);
        assertTrue(ma.getUsedMemory() == 0);
        long usedBeforeUpdate = ma.getUsedMemory();
        r.put("key1", data);
        assertEquals(usedBeforeUpdate, ma.getUsedMemory());
        assertEquals(data, r.get("key1"));
        r.destroy("key1");
        assertEquals(0, ma.getUsedMemory());
        data = new Long(0x007FFFFFL);
        r.put("key1", data);
        if (!compressed) {
            assertTrue(ma.getUsedMemory() == 0);
        }
        assertEquals(data, r.get("key1"));
        data = new Long(0xFF8000000L);
        r.put("key1", data);
        if (!compressed) {
            assertTrue(ma.getUsedMemory() == 0);
        }
        assertEquals(data, r.get("key1"));
        // now lets set data to something that will be stored offheap
        data = new Long(Long.MAX_VALUE);
        r.put("key1", data);
        assertEquals(data, r.get("key1"));
        // System.out.println("After put of Integer value off heap used memory=" +
        // ma.getUsedMemory());
        assertTrue(ma.getUsedMemory() > 0);
        data = new Long(Long.MIN_VALUE);
        r.put("key1", data);
        assertEquals(data, r.get("key1"));
        // System.out.println("After put of Integer value off heap used memory=" +
        // ma.getUsedMemory());
        assertTrue(ma.getUsedMemory() > 0);
        r.invalidate("key1");
        assertEquals(0, ma.getUsedMemory());
        r.put("key1", data);
        assertTrue(ma.getUsedMemory() > 0);
        usedBeforeUpdate = ma.getUsedMemory();
        r.put("key1", data);
        assertEquals(usedBeforeUpdate, ma.getUsedMemory());
        assertEquals(data, r.get("key1"));
        r.destroy("key1");
        assertEquals(0, ma.getUsedMemory());
        // confirm that byte[] do use off heap
        {
            byte[] originalBytes = new byte[1024];
            Object oldV = r.put("byteArray", originalBytes);
            long startUsedMemory = ma.getUsedMemory();
            assertEquals(null, oldV);
            byte[] readBytes = (byte[]) r.get("byteArray");
            if (originalBytes == readBytes) {
                fail("Expected different byte[] identity");
            }
            if (!Arrays.equals(readBytes, originalBytes)) {
                fail("Expected byte array contents to be equal");
            }
            assertEquals(startUsedMemory, ma.getUsedMemory());
            oldV = r.put("byteArray", originalBytes);
            if (!compressed) {
                // we default to old value being null for offheap
                assertEquals(null, oldV);
            }
            assertEquals(startUsedMemory, ma.getUsedMemory());
            readBytes = (byte[]) r.putIfAbsent("byteArray", originalBytes);
            if (originalBytes == readBytes) {
                fail("Expected different byte[] identity");
            }
            if (!Arrays.equals(readBytes, originalBytes)) {
                fail("Expected byte array contents to be equal");
            }
            assertEquals(startUsedMemory, ma.getUsedMemory());
            if (!r.replace("byteArray", readBytes, originalBytes)) {
                fail("Expected replace to happen");
            }
            assertEquals(startUsedMemory, ma.getUsedMemory());
            byte[] otherBytes = new byte[1024];
            otherBytes[1023] = 1;
            if (r.replace("byteArray", otherBytes, originalBytes)) {
                fail("Expected replace to not happen");
            }
            if (r.replace("byteArray", "bogus string", originalBytes)) {
                fail("Expected replace to not happen");
            }
            if (r.remove("byteArray", "bogus string")) {
                fail("Expected remove to not happen");
            }
            assertEquals(startUsedMemory, ma.getUsedMemory());
            if (!r.remove("byteArray", originalBytes)) {
                fail("Expected remove to happen");
            }
            assertEquals(0, ma.getUsedMemory());
            oldV = r.putIfAbsent("byteArray", "string value");
            assertEquals(null, oldV);
            assertEquals("string value", r.get("byteArray"));
            if (r.replace("byteArray", "string valuE", originalBytes)) {
                fail("Expected replace to not happen");
            }
            if (!r.replace("byteArray", "string value", originalBytes)) {
                fail("Expected replace to happen");
            }
            // we default to old value being null for offheap
            oldV = r.destroy("byteArray");
            if (!compressed) {
                assertEquals(null, oldV);
            }
            MyCacheListener listener = new MyCacheListener();
            r.getAttributesMutator().addCacheListener(listener);
            try {
                Object valueToReplace = "string value1";
                r.put("byteArray", valueToReplace);
                assertEquals(null, listener.ohOldValue);
                if (!compressed) {
                    assertEquals("string value1", listener.ohNewValue.getDeserializedForReading());
                    valueToReplace = listener.ohNewValue;
                }
                if (!r.replace("byteArray", valueToReplace, "string value2")) {
                    fail("expected replace to happen");
                }
                if (!compressed) {
                    assertEquals("string value2", listener.ohNewValue.getDeserializedForReading());
                    assertEquals("string value1", listener.ohOldValue.getDeserializedForReading());
                }
            // make sure that a custom equals/hashCode are not used when comparing values.
            } finally {
                r.getAttributesMutator().removeCacheListener(listener);
            }
        }
        assertTrue(ma.getUsedMemory() > 0);
        {
            Object key = "MyValueWithPartialEquals";
            MyValueWithPartialEquals v1 = new MyValueWithPartialEquals("s1");
            MyValueWithPartialEquals v2 = new MyValueWithPartialEquals("s2");
            MyValueWithPartialEquals v3 = new MyValueWithPartialEquals("s1");
            r.put(key, v1);
            try {
                if (r.replace(key, v2, "should not happen")) {
                    fail("v1 should not be equal to v2 on an offheap region");
                }
                if (!r.replace(key, v3, "should happen")) {
                    fail("v1 should be equal to v3 on an offheap region");
                }
                r.put(key, v1);
                if (r.remove(key, v2)) {
                    fail("v1 should not be equal to v2 on an offheap region");
                }
                if (!r.remove(key, v3)) {
                    fail("v1 should be equal to v3 on an offheap region");
                }
            } finally {
                r.remove(key);
            }
        }
        {
            Object key = "MyPdxWithPartialEquals";
            MyPdxWithPartialEquals v1 = new MyPdxWithPartialEquals("s", "1");
            MyPdxWithPartialEquals v2 = new MyPdxWithPartialEquals("s", "2");
            MyPdxWithPartialEquals v3 = new MyPdxWithPartialEquals("t", "1");
            r.put(key, v1);
            try {
                if (r.replace(key, v3, "should not happen")) {
                    fail("v1 should not be equal to v3 on an offheap region");
                }
                if (!r.replace(key, v2, "should happen")) {
                    fail("v1 should be equal to v2 on an offheap region");
                }
                r.put(key, v1);
                if (r.remove(key, v3)) {
                    fail("v1 should not be equal to v3 on an offheap region");
                }
                if (!r.remove(key, v2)) {
                    fail("v1 should be equal to v2 on an offheap region");
                }
            } finally {
                r.remove(key);
            }
        }
        byte[] value = new byte[1024];
        /* while (value != null) */
        {
            r.put("byteArray", value);
        }
        r.remove("byteArray");
        assertEquals(0, ma.getUsedMemory());
        r.put("key1", data);
        assertTrue(ma.getUsedMemory() > 0);
        r.invalidateRegion();
        assertEquals(0, ma.getUsedMemory());
        r.put("key1", data);
        assertTrue(ma.getUsedMemory() > 0);
        try {
            r.clear();
            Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> assertEquals(0, ma.getUsedMemory()));
        } catch (UnsupportedOperationException ok) {
        }
        r.put("key1", data);
        assertTrue(ma.getUsedMemory() > 0);
        if (r.getAttributes().getDataPolicy().withPersistence()) {
            r.put("key2", Integer.valueOf(1234567890));
            r.put("key3", new Long(0x007FFFFFL));
            r.put("key4", new Long(0xFF8000000L));
            assertEquals(4, r.size());
            r.close();
            Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> assertEquals(0, ma.getUsedMemory()));
            // simple test of recovery
            r = gfc.createRegionFactory(rs).setOffHeap(true).create(rName);
            assertEquals(4, r.size());
            assertEquals(data, r.get("key1"));
            assertEquals(Integer.valueOf(1234567890), r.get("key2"));
            assertEquals(new Long(0x007FFFFFL), r.get("key3"));
            assertEquals(new Long(0xFF8000000L), r.get("key4"));
            closeCache(gfc, true);
            assertEquals(0, ma.getUsedMemory());
            gfc = createCache();
            if (ma != gfc.getOffHeapStore()) {
                fail("identity of offHeapStore changed when cache was recreated");
            }
            r = gfc.createRegionFactory(rs).setOffHeap(true).create(rName);
            assertTrue(ma.getUsedMemory() > 0);
            assertEquals(4, r.size());
            assertEquals(data, r.get("key1"));
            assertEquals(Integer.valueOf(1234567890), r.get("key2"));
            assertEquals(new Long(0x007FFFFFL), r.get("key3"));
            assertEquals(new Long(0xFF8000000L), r.get("key4"));
        }
        r.destroyRegion();
        Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> assertEquals(0, ma.getUsedMemory()));
    } finally {
        if (r != null && !r.isDestroyed()) {
            r.destroyRegion();
        }
        closeCache(gfc, false);
    }
}
Also used : Compressor(org.apache.geode.compression.Compressor) SnappyCompressor(org.apache.geode.compression.SnappyCompressor) Region(org.apache.geode.cache.Region)

Example 17 with Compressor

use of org.apache.geode.compression.Compressor in project geode by apache.

the class OffHeapRegionEntryHelperJUnitTest method addressToObjectShouldReturnCachedDeserializableFromSerializedDataAsAddressIfAskedToDecompress.

@Test
public void addressToObjectShouldReturnCachedDeserializableFromSerializedDataAsAddressIfAskedToDecompress() {
    byte[] data = EntryEventImpl.serialize(Integer.MAX_VALUE);
    boolean isSerialized = true;
    boolean isCompressed = true;
    long address = OffHeapRegionEntryHelper.encodeDataAsAddress(data, isSerialized, isCompressed);
    // create the mock context
    RegionEntryContext regionContext = mock(RegionEntryContext.class);
    CachePerfStats cacheStats = mock(CachePerfStats.class);
    Compressor compressor = mock(Compressor.class);
    long startTime = 10000L;
    // mock required things
    when(regionContext.getCompressor()).thenReturn(compressor);
    when(compressor.decompress(data)).thenReturn(data);
    when(regionContext.getCachePerfStats()).thenReturn(cacheStats);
    when(cacheStats.startDecompression()).thenReturn(startTime);
    Object actual = OffHeapRegionEntryHelper.addressToObject(address, true, regionContext);
    assertThat(actual).isInstanceOf(VMCachedDeserializable.class);
    Integer actualValue = (Integer) ((VMCachedDeserializable) actual).getDeserializedForReading();
    assertThat(actualValue).isEqualTo(Integer.MAX_VALUE);
}
Also used : CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) Compressor(org.apache.geode.compression.Compressor) RegionEntryContext(org.apache.geode.internal.cache.RegionEntryContext) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with Compressor

use of org.apache.geode.compression.Compressor in project geode by apache.

the class OffHeapStoredObjectJUnitTest method getDecompressedBytesShouldReturnDecompressedBytesIfCompressed.

@Test
public void getDecompressedBytesShouldReturnDecompressedBytesIfCompressed() {
    Object regionEntryValue = getValue();
    byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
    boolean isSerialized = true;
    boolean isCompressed = true;
    OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
    RegionEntryContext regionContext = mock(RegionEntryContext.class);
    CachePerfStats cacheStats = mock(CachePerfStats.class);
    Compressor compressor = mock(Compressor.class);
    long startTime = 10000L;
    // mock required things
    when(regionContext.getCompressor()).thenReturn(compressor);
    when(compressor.decompress(regionEntryValueAsBytes)).thenReturn(regionEntryValueAsBytes);
    when(regionContext.getCachePerfStats()).thenReturn(cacheStats);
    when(cacheStats.startDecompression()).thenReturn(startTime);
    // invoke the thing
    byte[] bytes = chunk.getDecompressedBytes(regionContext);
    // verify the thing happened
    verify(cacheStats, atLeastOnce()).startDecompression();
    verify(compressor, times(1)).decompress(regionEntryValueAsBytes);
    verify(cacheStats, atLeastOnce()).endDecompression(startTime);
    assertArrayEquals(regionEntryValueAsBytes, bytes);
    chunk.release();
}
Also used : CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) Compressor(org.apache.geode.compression.Compressor) RegionEntryContext(org.apache.geode.internal.cache.RegionEntryContext) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 19 with Compressor

use of org.apache.geode.compression.Compressor in project geode by apache.

the class CreateAlterDestroyRegionCommands method validateRegionFunctionArgs.

private void validateRegionFunctionArgs(InternalCache cache, RegionFunctionArgs regionFunctionArgs) {
    if (regionFunctionArgs.getRegionPath() == null) {
        throw new IllegalArgumentException(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_REGION_PATH);
    }
    ManagementService managementService = ManagementService.getExistingManagementService(cache);
    DistributedSystemMXBean dsMBean = managementService.getDistributedSystemMXBean();
    String useAttributesFrom = regionFunctionArgs.getUseAttributesFrom();
    if (useAttributesFrom != null && !useAttributesFrom.isEmpty() && regionExists(cache, useAttributesFrom)) {
        if (!regionExists(cache, useAttributesFrom)) {
            // check already done in createRegion !!!
            throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_REGION_PATH_FOR_0_REGIONPATH_1_NOT_FOUND, new Object[] { CliStrings.CREATE_REGION__USEATTRIBUTESFROM, useAttributesFrom }));
        }
        if (!regionFunctionArgs.isSetUseAttributesFrom() || regionFunctionArgs.getRegionAttributes() == null) {
            throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__COULD_NOT_RETRIEVE_REGION_ATTRS_FOR_PATH_0_VERIFY_REGION_EXISTS, useAttributesFrom));
        }
    }
    if (regionFunctionArgs.hasPartitionAttributes()) {
        RegionFunctionArgs.PartitionArgs partitionArgs = regionFunctionArgs.getPartitionArgs();
        String colocatedWith = partitionArgs.getPrColocatedWith();
        if (colocatedWith != null && !colocatedWith.isEmpty()) {
            String[] listAllRegionPaths = dsMBean.listAllRegionPaths();
            String foundRegionPath = null;
            for (String regionPath : listAllRegionPaths) {
                if (regionPath.equals(colocatedWith)) {
                    foundRegionPath = regionPath;
                    break;
                }
            }
            if (foundRegionPath == null) {
                throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_REGION_PATH_FOR_0_REGIONPATH_1_NOT_FOUND, new Object[] { CliStrings.CREATE_REGION__COLOCATEDWITH, colocatedWith }));
            }
            ManagementService mgmtService = ManagementService.getExistingManagementService(cache);
            DistributedRegionMXBean distributedRegionMXBean = mgmtService.getDistributedRegionMXBean(foundRegionPath);
            String regionType = distributedRegionMXBean.getRegionType();
            if (!(DataPolicy.PARTITION.toString().equals(regionType) || DataPolicy.PERSISTENT_PARTITION.toString().equals(regionType))) {
                throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__COLOCATEDWITH_REGION_0_IS_NOT_PARTITIONEDREGION, new Object[] { colocatedWith }));
            }
        }
        if (partitionArgs.isSetPRLocalMaxMemory()) {
            int prLocalMaxMemory = partitionArgs.getPrLocalMaxMemory();
            if (prLocalMaxMemory < 0) {
                throw new IllegalArgumentException(LocalizedStrings.AttributesFactory_PARTITIONATTRIBUTES_LOCALMAXMEMORY_MUST_NOT_BE_NEGATIVE.toLocalizedString());
            }
        }
        if (partitionArgs.isSetPRTotalMaxMemory()) {
            long prTotalMaxMemory = partitionArgs.getPrTotalMaxMemory();
            if (prTotalMaxMemory <= 0) {
                throw new IllegalArgumentException(LocalizedStrings.AttributesFactory_TOTAL_SIZE_OF_PARTITION_REGION_MUST_BE_0.toLocalizedString());
            }
        }
        if (partitionArgs.isSetPRRedundantCopies()) {
            int prRedundantCopies = partitionArgs.getPrRedundantCopies();
            switch(prRedundantCopies) {
                case 0:
                case 1:
                case 2:
                case 3:
                    break;
                default:
                    throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__REDUNDANT_COPIES_SHOULD_BE_ONE_OF_0123, new Object[] { prRedundantCopies }));
            }
        }
    }
    String keyConstraint = regionFunctionArgs.getKeyConstraint();
    if (keyConstraint != null && !isClassNameValid(keyConstraint)) {
        throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_KEYCONSTRAINT_0_IS_INVALID, new Object[] { keyConstraint }));
    }
    String valueConstraint = regionFunctionArgs.getValueConstraint();
    if (valueConstraint != null && !isClassNameValid(valueConstraint)) {
        throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_VALUECONSTRAINT_0_IS_INVALID, new Object[] { valueConstraint }));
    }
    Set<String> cacheListeners = regionFunctionArgs.getCacheListeners();
    if (cacheListeners != null && !cacheListeners.isEmpty()) {
        for (String cacheListener : cacheListeners) {
            if (!isClassNameValid(cacheListener)) {
                throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_CACHELISTENER_0_IS_INVALID, new Object[] { cacheListener }));
            }
        }
    }
    String cacheLoader = regionFunctionArgs.getCacheLoader();
    if (cacheLoader != null && !isClassNameValid(cacheLoader)) {
        throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_CACHELOADER_0_IS_INVALID, new Object[] { cacheLoader }));
    }
    String cacheWriter = regionFunctionArgs.getCacheWriter();
    if (cacheWriter != null && !isClassNameValid(cacheWriter)) {
        throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_CACHEWRITER_0_IS_INVALID, new Object[] { cacheWriter }));
    }
    Set<String> gatewaySenderIds = regionFunctionArgs.getGatewaySenderIds();
    if (gatewaySenderIds != null && !gatewaySenderIds.isEmpty()) {
        String[] gatewaySenders = dsMBean.listGatewaySenders();
        if (gatewaySenders.length == 0) {
            throw new IllegalArgumentException(CliStrings.CREATE_REGION__MSG__NO_GATEWAYSENDERS_IN_THE_SYSTEM);
        } else {
            List<String> gatewaySendersList = new ArrayList<>(Arrays.asList(gatewaySenders));
            gatewaySenderIds = new HashSet<>(gatewaySenderIds);
            gatewaySenderIds.removeAll(gatewaySendersList);
            if (!gatewaySenderIds.isEmpty()) {
                throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_GATEWAYSENDER_ID_UNKNOWN_0, new Object[] { gatewaySenderIds }));
            }
        }
    }
    if (regionFunctionArgs.isSetConcurrencyLevel()) {
        int concurrencyLevel = regionFunctionArgs.getConcurrencyLevel();
        if (concurrencyLevel < 0) {
            throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_POSITIVE_INT_FOR_CONCURRENCYLEVEL_0_IS_NOT_VALID, new Object[] { concurrencyLevel }));
        }
    }
    String diskStore = regionFunctionArgs.getDiskStore();
    if (diskStore != null) {
        RegionShortcut regionShortcut = regionFunctionArgs.getRegionShortcut();
        if (regionShortcut != null && !PERSISTENT_OVERFLOW_SHORTCUTS.contains(regionShortcut)) {
            String subMessage = LocalizedStrings.DiskStore_IS_USED_IN_NONPERSISTENT_REGION.toLocalizedString();
            String message = subMessage + ". " + CliStrings.format(CliStrings.CREATE_REGION__MSG__USE_ONE_OF_THESE_SHORTCUTS_0, new Object[] { String.valueOf(PERSISTENT_OVERFLOW_SHORTCUTS) });
            throw new IllegalArgumentException(message);
        }
        RegionAttributes<?, ?> regionAttributes = regionFunctionArgs.getRegionAttributes();
        if (regionAttributes != null && !regionAttributes.getDataPolicy().withPersistence()) {
            String subMessage = LocalizedStrings.DiskStore_IS_USED_IN_NONPERSISTENT_REGION.toLocalizedString();
            String message = subMessage + ". " + CliStrings.format(CliStrings.CREATE_REGION__MSG__USE_ATTRIBUTES_FROM_REGION_0_IS_NOT_WITH_PERSISTENCE, new Object[] { String.valueOf(regionFunctionArgs.getUseAttributesFrom()) });
            throw new IllegalArgumentException(message);
        }
        if (!diskStoreExists(cache, diskStore)) {
            throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__SPECIFY_VALID_DISKSTORE_UNKNOWN_DISKSTORE_0, new Object[] { diskStore }));
        }
    }
    RegionFunctionArgs.ExpirationAttrs entryExpirationIdleTime = regionFunctionArgs.getEntryExpirationIdleTime();
    RegionFunctionArgs.ExpirationAttrs entryExpirationTTL = regionFunctionArgs.getEntryExpirationTTL();
    RegionFunctionArgs.ExpirationAttrs regionExpirationIdleTime = regionFunctionArgs.getRegionExpirationIdleTime();
    RegionFunctionArgs.ExpirationAttrs regionExpirationTTL = regionFunctionArgs.getRegionExpirationTTL();
    if ((!regionFunctionArgs.isSetStatisticsEnabled() || !regionFunctionArgs.isStatisticsEnabled()) && (entryExpirationIdleTime != null || entryExpirationTTL != null || regionExpirationIdleTime != null || regionExpirationTTL != null)) {
        String message = LocalizedStrings.AttributesFactory_STATISTICS_MUST_BE_ENABLED_FOR_EXPIRATION.toLocalizedString();
        throw new IllegalArgumentException(message + ".");
    }
    boolean compressorFailure = false;
    if (regionFunctionArgs.isSetCompressor()) {
        String compressorClassName = regionFunctionArgs.getCompressor();
        Object compressor = null;
        try {
            Class<?> compressorClass = ClassPathLoader.getLatest().forName(compressorClassName);
            compressor = compressorClass.newInstance();
        } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
            compressorFailure = true;
        }
        if (compressorFailure || !(compressor instanceof Compressor)) {
            throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__INVALID_COMPRESSOR, new Object[] { regionFunctionArgs.getCompressor() }));
        }
    }
    if (regionFunctionArgs.hasPartitionAttributes()) {
        if (regionFunctionArgs.isPartitionResolverSet()) {
            String partitionResolverClassName = regionFunctionArgs.getPartitionResolver();
            try {
                Class<PartitionResolver> resolverClass = (Class<PartitionResolver>) ClassPathLoader.getLatest().forName(partitionResolverClassName);
                PartitionResolver partitionResolver = resolverClass.newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__INVALID_PARTITION_RESOLVER, new Object[] { regionFunctionArgs.getCompressor() }), e);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RegionShortcut(org.apache.geode.cache.RegionShortcut) ManagementService(org.apache.geode.management.ManagementService) RegionFunctionArgs(org.apache.geode.management.internal.cli.functions.RegionFunctionArgs) Compressor(org.apache.geode.compression.Compressor) PartitionResolver(org.apache.geode.cache.PartitionResolver) DistributedRegionMXBean(org.apache.geode.management.DistributedRegionMXBean) ConverterHint(org.apache.geode.management.cli.ConverterHint) DistributedSystemMXBean(org.apache.geode.management.DistributedSystemMXBean)

Example 20 with Compressor

use of org.apache.geode.compression.Compressor in project geode by apache.

the class DistributedAckRegionCompressionDUnitTest method getRegionAttributes.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected RegionAttributes getRegionAttributes() {
    Compressor compressor = null;
    try {
        compressor = SnappyCompressor.getDefaultInstance();
    } catch (Throwable t) {
        // Not a supported OS
        return super.getRegionAttributes();
    }
    RegionAttributes attrs = super.getRegionAttributes();
    AttributesFactory factory = new AttributesFactory(attrs);
    factory.setCompressor(compressor);
    return factory.create();
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Compressor(org.apache.geode.compression.Compressor) SnappyCompressor(org.apache.geode.compression.SnappyCompressor)

Aggregations

Compressor (org.apache.geode.compression.Compressor)23 SnappyCompressor (org.apache.geode.compression.SnappyCompressor)13 Test (org.junit.Test)13 RegionEntryContext (org.apache.geode.internal.cache.RegionEntryContext)7 UnitTest (org.apache.geode.test.junit.categories.UnitTest)7 CachePerfStats (org.apache.geode.internal.cache.CachePerfStats)6 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 AttributesFactory (org.apache.geode.cache.AttributesFactory)4 RegionAttributes (org.apache.geode.cache.RegionAttributes)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 RegionShortcut (org.apache.geode.cache.RegionShortcut)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 EmptyStackException (java.util.EmptyStackException)1 InternalGemFireException (org.apache.geode.InternalGemFireException)1 CacheException (org.apache.geode.cache.CacheException)1 CacheListener (org.apache.geode.cache.CacheListener)1 CacheLoader (org.apache.geode.cache.CacheLoader)1 CacheWriter (org.apache.geode.cache.CacheWriter)1 CacheWriterException (org.apache.geode.cache.CacheWriterException)1