Skip to content

Commit

Permalink
Fixed the memory parsing to match the Spark side
Browse files Browse the repository at this point in the history
- allow for b/k/m/g/t/p or kb/mb/gb/tb/pb to match the units used on the Spark
  side (so that the same works correctly for memory-size and when it is set as Spark property)
- moved the method to ClientSharedUtils and use the same in OffHeapStorage
  • Loading branch information
Sumedh Wale committed Oct 23, 2017
1 parent b417706 commit e5b98c2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@
import com.gemstone.gemfire.internal.jndi.JNDIInvoker;
import com.gemstone.gemfire.internal.jta.TransactionManagerImpl;
import com.gemstone.gemfire.internal.offheap.MemoryAllocator;
import com.gemstone.gemfire.internal.offheap.OffHeapStorage;
import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl.ChunkType;
import com.gemstone.gemfire.internal.shared.BufferAllocator;
import com.gemstone.gemfire.internal.shared.ClientSharedUtils;
import com.gemstone.gemfire.internal.shared.HeapBufferAllocator;
import com.gemstone.gemfire.internal.shared.NativeCalls;
import com.gemstone.gemfire.internal.shared.SystemProperties;
Expand Down Expand Up @@ -1104,16 +1104,16 @@ public void run() {
GemFireCacheImpl.pdxInstance = this;

// set the buffer allocator for the cache (off-heap or heap)
long memorySize = OffHeapStorage.parseOffHeapMemorySize(
getSystem().getConfig().getMemorySize());
String memorySizeStr = getSystem().getConfig().getMemorySize();
long memorySize = ClientSharedUtils.parseMemorySize(memorySizeStr, 0L, 0);
if (memorySize == 0) {
// check in callbacks
StoreCallbacks callbacks = CallbackFactoryProvider.getStoreCallbacks();
memorySize = callbacks.getExecutionPoolSize(true) +
callbacks.getStoragePoolSize(true);
}
this.memorySize = memorySize;
if (memorySize > 0) {
this.memorySize = memorySize;
if (!GemFireVersion.isEnterpriseEdition()) {
throw new IllegalArgumentException("The off-heap column store (enabled by property " +
"memory-size) is not supported in SnappyData OSS version.");
Expand All @@ -1126,6 +1126,8 @@ public void run() {
InvocationTargetException e) {
throw new IllegalStateException("Could not configure managed buffer allocator.", e);
}
} else if (memorySize < 0) {
throw new IllegalArgumentException("Invalid memory-size: " + memorySizeStr);
} else {
// the allocation sizes will be initialized from the heap size
this.bufferAllocator = HeapBufferAllocator.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
import com.gemstone.gemfire.distributed.internal.InternalLocator;
import com.gemstone.gemfire.internal.ClassPathLoader;
import com.gemstone.gemfire.internal.LogWriterImpl;
import com.gemstone.gemfire.internal.StatisticsTypeFactoryImpl;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
import com.gemstone.gemfire.internal.shared.ClientSharedUtils;

/**
* Enables off-heap storage by creating a MemoryAllocator.
Expand Down Expand Up @@ -124,7 +124,7 @@ public static long parseOffHeapMemorySize(String value) {
if (value == null || value.length() == 0) {
return 0;
}
final long parsed = parseLongWithUnits(value, 0L, 1024*1024);
final long parsed = ClientSharedUtils.parseMemorySize(value, 0L, 20);
if (parsed < 0) {
return 0;
}
Expand All @@ -135,7 +135,8 @@ public static long calcMaxSlabSize(long offHeapMemorySize) {
final String offHeapSlabConfig = System.getProperty("gemfire.OFF_HEAP_SLAB_SIZE");
long result = 0;
if (offHeapSlabConfig != null && !offHeapSlabConfig.equals("")) {
result = parseLongWithUnits(offHeapSlabConfig, MAX_SLAB_SIZE, 1024*1024);
result = ClientSharedUtils.parseMemorySize(
offHeapSlabConfig, MAX_SLAB_SIZE, 20);
if (result > offHeapMemorySize) {
result = offHeapMemorySize;
}
Expand Down Expand Up @@ -179,7 +180,8 @@ public static MemoryAllocator createOffHeapStorage(LogWriter lw, StatisticsFacto
if (offHeapMemorySize == 0 && !Boolean.getBoolean(InternalLocator.FORCE_LOCATOR_DM_TYPE)) {
String offHeapConfig = System.getProperty("gemfire.OFF_HEAP_TOTAL_SIZE");
if (offHeapConfig != null && !offHeapConfig.equals("")) {
offHeapMemorySize = parseLongWithUnits(offHeapConfig, 0L, 1024*1024);
offHeapMemorySize = ClientSharedUtils.parseMemorySize(
offHeapConfig, 0L, 20);
}
}

Expand Down Expand Up @@ -238,28 +240,6 @@ private static int calcSlabCount(long maxSlabSize, long offHeapMemorySize) {
return (int)result;
}

private static long parseLongWithUnits(String v, long defaultValue, int defaultMultiplier) {
if (v == null || v.equals("")) {
return defaultValue;
}
int unitMultiplier = defaultMultiplier;
if (v.toLowerCase().endsWith("g")) {
unitMultiplier = 1024*1024*1024;
v = v.substring(0, v.length()-1);
} else if (v.toLowerCase().endsWith("m")) {
unitMultiplier = 1024*1024;
v = v.substring(0, v.length()-1);
}
try {
long result = Long.parseLong(v);
result *= unitMultiplier;
return result;
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Memory size must be specified as <n>[g|m], where <n> is the size and [g|m] specifies the units in gigabytes or megabytes.");
}
}

private final Statistics stats;

private OffHeapStorage(StatisticsFactory f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,54 @@ public static void setThriftDefault(boolean defaultValue) {
bigIntMagnitude = mag;
}

private static int getShiftMultipler(char unitChar) {
switch (Character.toLowerCase(unitChar)) {
case 'p': return 50;
case 't': return 40;
case 'g': return 30;
case 'm': return 20;
case 'k': return 10;
case 'b': return 0;
default: return -1;
}
}

public static long parseMemorySize(String v, long defaultValue,
int defaultShiftMultiplier) {
if (v == null || v.length() == 0) {
return defaultValue;
}
final int len = v.length();
int unitShiftMultiplier = getShiftMultipler(v.charAt(len - 1));
int trimChars = unitShiftMultiplier >= 0 ? 1 : 0;
if (unitShiftMultiplier == 0) {
// ends with 'b' so could be 'mb', 'gb' etc
if (len > 1) {
unitShiftMultiplier = getShiftMultipler(v.charAt(len - 2));
if (unitShiftMultiplier > 0) {
trimChars = 2;
} else {
unitShiftMultiplier = 0;
}
}
} else if (unitShiftMultiplier < 0) {
unitShiftMultiplier = defaultShiftMultiplier;
}
if (trimChars > 0) {
v = v.substring(0, len - trimChars);
}
try {
return Long.parseLong(v) << unitShiftMultiplier;
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Memory size specification = " + v +
": memory size must be specified as <n>[p|t|g|m|k], where <n> is " +
"the size and and [p|t|g|m|k] specifies the units in peta|tera|giga|" +
"mega|kilobytes. No unit or with 'b' specifies in bytes. " +
"Each of these units can be followed optionally by 'b' i.e. " +
"pb|tb|gb|mb|kb.", nfe);
}
}

public static String newWrappedString(final char[] chars, final int offset,
final int size) {
if (size >= 0) {
Expand Down

0 comments on commit e5b98c2

Please sign in to comment.