Search in sources :

Example 1 with Compressor

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

the class RegionCreateFunction method createRegion.

public static <K, V> Region<?, ?> createRegion(Cache cache, RegionFunctionArgs regionCreateArgs) {
    Region<K, V> createdRegion = null;
    final String regionPath = regionCreateArgs.getRegionPath();
    final RegionShortcut regionShortcut = regionCreateArgs.getRegionShortcut();
    final String useAttributesFrom = regionCreateArgs.getUseAttributesFrom();
    // If a region path indicates a sub-region, check whether the parent region exists
    RegionPath regionPathData = new RegionPath(regionPath);
    String parentRegionPath = regionPathData.getParent();
    Region<?, ?> parentRegion = null;
    if (parentRegionPath != null && !Region.SEPARATOR.equals(parentRegionPath)) {
        parentRegion = cache.getRegion(parentRegionPath);
        if (parentRegion == null) {
            throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__PARENT_REGION_FOR_0_DOESNOT_EXIST, new Object[] { regionPath }));
        }
        if (parentRegion.getAttributes().getPartitionAttributes() != null) {
            // For a PR, sub-regions are not supported.
            throw new CreateSubregionException(CliStrings.format(CliStrings.CREATE_REGION__MSG__0_IS_A_PR_CANNOT_HAVE_SUBREGIONS, parentRegion.getFullPath()));
        }
    }
    // One of Region Shortcut OR Use Attributes From has to be given
    if (regionShortcut == null && useAttributesFrom == null) {
        throw new IllegalArgumentException(CliStrings.CREATE_REGION__MSG__ONE_OF_REGIONSHORTCUT_AND_USEATTRIBUESFROM_IS_REQUIRED);
    }
    boolean isPartitioned = false;
    RegionFactory<K, V> factory = null;
    RegionAttributes<K, V> regionAttributes = null;
    if (regionShortcut != null) {
        regionAttributes = cache.getRegionAttributes(regionShortcut.toString());
        if (logger.isDebugEnabled()) {
            logger.debug("Using shortcut {} for {} region attributes : {}", regionShortcut, regionPath, regionAttributes);
        }
        if (regionAttributes == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Shortcut {} doesn't have attributes in {}", regionShortcut, cache.listRegionAttributes());
            }
            throw new IllegalStateException(CliStrings.format(CliStrings.CREATE_REGION__MSG__COULDNOT_LOAD_REGION_ATTRIBUTES_FOR_SHORTCUT_0, regionShortcut));
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Using Manager's region attributes for {}", regionPath);
        }
        regionAttributes = regionCreateArgs.getRegionAttributes();
        if (logger.isDebugEnabled()) {
            logger.debug("Using Attributes : {}", regionAttributes);
        }
    }
    isPartitioned = regionAttributes.getPartitionAttributes() != null;
    factory = cache.createRegionFactory(regionAttributes);
    if (!isPartitioned && regionCreateArgs.hasPartitionAttributes()) {
        throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__OPTION_0_CAN_BE_USED_ONLY_FOR_PARTITIONEDREGION, regionCreateArgs.getPartitionArgs().getUserSpecifiedPartitionAttributes()));
    }
    if (isPartitioned) {
        PartitionAttributes<K, V> partitionAttributes = extractPartitionAttributes(cache, regionAttributes, regionCreateArgs);
        DataPolicy originalDataPolicy = regionAttributes.getDataPolicy();
        factory.setPartitionAttributes(partitionAttributes);
        // We have to do this because AttributesFactory.setPartitionAttributes()
        // checks RegionAttributes.hasDataPolicy() which is set only when the data
        // policy is set explicitly
        factory.setDataPolicy(originalDataPolicy);
    }
    // Set Constraints
    final String keyConstraint = regionCreateArgs.getKeyConstraint();
    final String valueConstraint = regionCreateArgs.getValueConstraint();
    if (keyConstraint != null && !keyConstraint.isEmpty()) {
        Class<K> keyConstraintClass = CliUtil.forName(keyConstraint, CliStrings.CREATE_REGION__KEYCONSTRAINT);
        factory.setKeyConstraint(keyConstraintClass);
    }
    if (valueConstraint != null && !valueConstraint.isEmpty()) {
        Class<V> valueConstraintClass = CliUtil.forName(valueConstraint, CliStrings.CREATE_REGION__VALUECONSTRAINT);
        factory.setValueConstraint(valueConstraintClass);
    }
    // Expiration attributes
    final RegionFunctionArgs.ExpirationAttrs entryExpirationIdleTime = regionCreateArgs.getEntryExpirationIdleTime();
    if (entryExpirationIdleTime != null) {
        factory.setEntryIdleTimeout(entryExpirationIdleTime.convertToExpirationAttributes());
    }
    final RegionFunctionArgs.ExpirationAttrs entryExpirationTTL = regionCreateArgs.getEntryExpirationTTL();
    if (entryExpirationTTL != null) {
        factory.setEntryTimeToLive(entryExpirationTTL.convertToExpirationAttributes());
    }
    final RegionFunctionArgs.ExpirationAttrs regionExpirationIdleTime = regionCreateArgs.getRegionExpirationIdleTime();
    if (regionExpirationIdleTime != null) {
        factory.setEntryIdleTimeout(regionExpirationIdleTime.convertToExpirationAttributes());
    }
    final RegionFunctionArgs.ExpirationAttrs regionExpirationTTL = regionCreateArgs.getRegionExpirationTTL();
    if (regionExpirationTTL != null) {
        factory.setEntryTimeToLive(regionExpirationTTL.convertToExpirationAttributes());
    }
    // Associate a Disk Store
    final String diskStore = regionCreateArgs.getDiskStore();
    if (diskStore != null && !diskStore.isEmpty()) {
        factory.setDiskStoreName(diskStore);
    }
    if (regionCreateArgs.isSetDiskSynchronous()) {
        factory.setDiskSynchronous(regionCreateArgs.isDiskSynchronous());
    }
    if (regionCreateArgs.isSetOffHeap()) {
        factory.setOffHeap(regionCreateArgs.isOffHeap());
    }
    // Set stats enabled
    if (regionCreateArgs.isSetStatisticsEnabled()) {
        factory.setStatisticsEnabled(regionCreateArgs.isStatisticsEnabled());
    }
    // Set conflation
    if (regionCreateArgs.isSetEnableAsyncConflation()) {
        factory.setEnableAsyncConflation(regionCreateArgs.isEnableAsyncConflation());
    }
    if (regionCreateArgs.isSetEnableSubscriptionConflation()) {
        factory.setEnableSubscriptionConflation(regionCreateArgs.isEnableSubscriptionConflation());
    }
    // Gateway Sender Ids
    final Set<String> gatewaySenderIds = regionCreateArgs.getGatewaySenderIds();
    if (gatewaySenderIds != null && !gatewaySenderIds.isEmpty()) {
        for (String gatewaySenderId : gatewaySenderIds) {
            factory.addGatewaySenderId(gatewaySenderId);
        }
    }
    // Async Queue Ids
    final Set<String> asyncEventQueueIds = regionCreateArgs.getAsyncEventQueueIds();
    if (asyncEventQueueIds != null && !asyncEventQueueIds.isEmpty()) {
        for (String asyncEventQueueId : asyncEventQueueIds) {
            factory.addAsyncEventQueueId(asyncEventQueueId);
        }
    }
    // concurrency check enabled & concurrency level
    if (regionCreateArgs.isSetConcurrencyChecksEnabled()) {
        factory.setConcurrencyChecksEnabled(regionCreateArgs.isConcurrencyChecksEnabled());
    }
    if (regionCreateArgs.isSetConcurrencyLevel()) {
        factory.setConcurrencyLevel(regionCreateArgs.getConcurrencyLevel());
    }
    // cloning enabled for delta
    if (regionCreateArgs.isSetCloningEnabled()) {
        factory.setCloningEnabled(regionCreateArgs.isCloningEnabled());
    }
    // multicast enabled for replication
    if (regionCreateArgs.isSetMcastEnabled()) {
        factory.setMulticastEnabled(regionCreateArgs.isMcastEnabled());
    }
    // Set plugins
    final Set<String> cacheListeners = regionCreateArgs.getCacheListeners();
    if (cacheListeners != null && !cacheListeners.isEmpty()) {
        for (String cacheListener : cacheListeners) {
            Class<CacheListener<K, V>> cacheListenerKlass = CliUtil.forName(cacheListener, CliStrings.CREATE_REGION__CACHELISTENER);
            factory.addCacheListener(CliUtil.newInstance(cacheListenerKlass, CliStrings.CREATE_REGION__CACHELISTENER));
        }
    }
    // Compression provider
    if (regionCreateArgs.isSetCompressor()) {
        Class<Compressor> compressorKlass = CliUtil.forName(regionCreateArgs.getCompressor(), CliStrings.CREATE_REGION__COMPRESSOR);
        factory.setCompressor(CliUtil.newInstance(compressorKlass, CliStrings.CREATE_REGION__COMPRESSOR));
    }
    final String cacheLoader = regionCreateArgs.getCacheLoader();
    if (cacheLoader != null) {
        Class<CacheLoader<K, V>> cacheLoaderKlass = CliUtil.forName(cacheLoader, CliStrings.CREATE_REGION__CACHELOADER);
        factory.setCacheLoader(CliUtil.newInstance(cacheLoaderKlass, CliStrings.CREATE_REGION__CACHELOADER));
    }
    final String cacheWriter = regionCreateArgs.getCacheWriter();
    if (cacheWriter != null) {
        Class<CacheWriter<K, V>> cacheWriterKlass = CliUtil.forName(cacheWriter, CliStrings.CREATE_REGION__CACHEWRITER);
        factory.setCacheWriter(CliUtil.newInstance(cacheWriterKlass, CliStrings.CREATE_REGION__CACHEWRITER));
    }
    String regionName = regionPathData.getName();
    if (parentRegion != null) {
        createdRegion = factory.createSubregion(parentRegion, regionName);
    } else {
        createdRegion = factory.create(regionName);
    }
    return createdRegion;
}
Also used : RegionShortcut(org.apache.geode.cache.RegionShortcut) CacheListener(org.apache.geode.cache.CacheListener) CacheWriter(org.apache.geode.cache.CacheWriter) DataPolicy(org.apache.geode.cache.DataPolicy) Compressor(org.apache.geode.compression.Compressor) RegionPath(org.apache.geode.management.internal.cli.util.RegionPath) CreateSubregionException(org.apache.geode.management.internal.cli.exceptions.CreateSubregionException) CacheLoader(org.apache.geode.cache.CacheLoader)

Example 2 with Compressor

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

the class OffHeapRegionEntryHelperJUnitTest method addressToObjectShouldReturnCachedDeserializableFromChunkIfAskedToDecompress.

@Test
public void addressToObjectShouldReturnCachedDeserializableFromChunkIfAskedToDecompress() {
    byte[] data = EntryEventImpl.serialize(Long.MAX_VALUE);
    boolean isSerialized = true;
    boolean isCompressed = true;
    OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(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(chunk.getAddress(), true, regionContext);
    assertThat(actual).isInstanceOf(VMCachedDeserializable.class);
    Long actualValue = (Long) ((VMCachedDeserializable) actual).getDeserializedForReading();
    assertThat(actualValue).isEqualTo(Long.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 3 with Compressor

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

the class OffHeapRegionEntryHelperJUnitTest method addressToObjectShouldReturnDecompressedValueFromDataAsAddressIfAskedToDecompress.

@Test
public void addressToObjectShouldReturnDecompressedValueFromDataAsAddressIfAskedToDecompress() {
    byte[] data = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt(Integer.MAX_VALUE).array();
    boolean isSerialized = false;
    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(byte[].class);
    assertThat(actual).isEqualTo(data);
}
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 4 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(String type) {
    Compressor compressor = null;
    try {
        compressor = SnappyCompressor.getDefaultInstance();
    } catch (Throwable t) {
        // Not a supported OS
        return super.getRegionAttributes(type);
    }
    RegionAttributes ra = super.getRegionAttributes(type);
    AttributesFactory factory = new AttributesFactory(ra);
    if (!ra.getDataPolicy().isEmpty()) {
        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)

Example 5 with Compressor

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

the class CompressionRegionConfigDUnitTest method testPartitionedPersistentRegion.

/**
   * Sanity check for two peers hosting a persistent partitioned region.
   */
@Test
public void testPartitionedPersistentRegion() {
    Compressor compressor = new SnappyCompressor();
    assertTrue(createCompressedRegionOnVm(getVM(0), REGION_NAME, DataPolicy.PERSISTENT_PARTITION, compressor, DISK_STORE));
    assertTrue(createCompressedRegionOnVm(getVM(1), REGION_NAME, DataPolicy.PERSISTENT_PARTITION, compressor));
    assertNull(putUsingVM(getVM(0), KEY_1, VALUE_1));
    waitOnPut(getVM(1), KEY_1);
    flushDiskStoreOnVM(getVM(0), DISK_STORE);
    closeRegionOnVM(getVM(1), REGION_NAME);
    assertTrue(createCompressedRegionOnVm(getVM(1), REGION_NAME, DataPolicy.PERSISTENT_PARTITION, compressor));
    assertEquals(VALUE_1, getUsingVM(getVM(1), KEY_1));
    cleanup(getVM(0));
}
Also used : SnappyCompressor(org.apache.geode.compression.SnappyCompressor) Compressor(org.apache.geode.compression.Compressor) SnappyCompressor(org.apache.geode.compression.SnappyCompressor) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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