Skip to content

Commit

Permalink
Better usage control of node cache size #10782
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock authored and rymsha committed Dec 3, 2024
1 parent fad5d17 commit 53f906d
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class PatternIndexConfigDocument
{
private final ImmutableSortedSet<PathIndexConfig> pathIndexConfigs;

private final Map<String, PathIndexConfig> pathIndexConfigMap;
private final ImmutableMap<String, PathIndexConfig> pathIndexConfigMap;

private final IndexConfig defaultConfig;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,14 @@
package com.enonic.xp.util;

import java.util.Locale;

public class ByteSizeParser
public final class ByteSizeParser
{

private static final long BYTE_FACTOR = 1L;

private static final long KB_FACTOR = BYTE_FACTOR * 1024L;

private static final long MB_FACTOR = KB_FACTOR * 1024L;

private static final long GB_FACTOR = MB_FACTOR * 1024L;

private static final long TB_FACTOR = GB_FACTOR * 1024L;

private static final long PB_FACTOR = TB_FACTOR * 1024L;

public static long parse( final String sValue )
private ByteSizeParser()
{
String postFix = sValue.
substring( sValue.length() - Math.min( 2, sValue.length() ) ).
toLowerCase( Locale.ROOT );

long bytes;

if ( postFix.endsWith( "k" ) )
{
bytes = (long) parseByteValue( sValue, KB_FACTOR, 1 );
}
else if ( postFix.endsWith( "kb" ) )
{
bytes = (long) ( parseByteValue( sValue, KB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "m" ) )
{
bytes = (long) ( parseByteValue( sValue, MB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "mb" ) )
{
bytes = (long) ( parseByteValue( sValue, MB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "g" ) )
{
bytes = (long) ( parseByteValue( sValue, GB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "gb" ) )
{
bytes = (long) ( parseByteValue( sValue, GB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "t" ) )
{
bytes = (long) ( parseByteValue( sValue, TB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "tb" ) )
{
bytes = (long) ( parseByteValue( sValue, TB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "p" ) )
{
bytes = (long) ( parseByteValue( sValue, PB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "pb" ) )
{
bytes = (long) ( parseByteValue( sValue, PB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "b" ) )
{
bytes = Long.parseLong( sValue.substring( 0, sValue.length() - 1 ) );
}
else
{
bytes = Long.parseLong( sValue );
}

return bytes;
}

private static double parseByteValue( final String sValue, final long kbFactor, final int postfixLenght )
public static long parse( final String sValue )
{
try
{
return Double.parseDouble( sValue.substring( 0, sValue.length() - postfixLenght ) ) * kbFactor;
}
catch ( NumberFormatException e )
{
throw new IllegalArgumentException( "Wrong format of size-value [" + sValue + "]" );
}
return com.enonic.xp.core.internal.ByteSizeParser.parse( sValue );
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private enum BlobRecordWeigher
@Override
public int weigh( final BlobKey key, final BlobRecord record )
{
return (int) record.getLength();
return (int) Math.min( record.getLength(), Integer.MAX_VALUE );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import com.enonic.xp.content.ContentService;
import com.enonic.xp.core.impl.image.effect.ImageScaleFunction;
import com.enonic.xp.core.internal.MemoryLimitParser;
import com.enonic.xp.core.internal.SimpleCsvParser;
import com.enonic.xp.home.HomeDir;
import com.enonic.xp.image.Cropping;
Expand Down Expand Up @@ -70,8 +71,7 @@ public ImageServiceImpl( @Reference final ContentService contentService,
this.imageScaleFunctionBuilder = imageScaleFunctionBuilder;
this.imageFilterBuilder = imageFilterBuilder;

this.circuitBreaker = new MemoryCircuitBreaker(
toMegaBytes( new MemoryLimitParser( Runtime.getRuntime()::maxMemory ).parse( config.memoryLimit() ) ) );
this.circuitBreaker = new MemoryCircuitBreaker( toMegaBytes( MemoryLimitParser.maxHeap().parse( config.memoryLimit() ) ) );

this.progressiveOnFormats = SimpleCsvParser.parseLine( config.progressive() )
.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.enonic.xp.core.internal;

import java.util.Locale;

public final class ByteSizeParser
{

private ByteSizeParser()
{
}

private static final long BYTE_FACTOR = 1L;

private static final long KB_FACTOR = BYTE_FACTOR * 1024L;

private static final long MB_FACTOR = KB_FACTOR * 1024L;

private static final long GB_FACTOR = MB_FACTOR * 1024L;

private static final long TB_FACTOR = GB_FACTOR * 1024L;

private static final long PB_FACTOR = TB_FACTOR * 1024L;

public static long parse( final String sValue )
{
String postFix = sValue.
substring( sValue.length() - Math.min( 2, sValue.length() ) ).
toLowerCase( Locale.ROOT );

long bytes;

if ( postFix.endsWith( "k" ) )
{
bytes = (long) parseByteValue( sValue, KB_FACTOR, 1 );
}
else if ( postFix.endsWith( "kb" ) )
{
bytes = (long) ( parseByteValue( sValue, KB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "m" ) )
{
bytes = (long) ( parseByteValue( sValue, MB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "mb" ) )
{
bytes = (long) ( parseByteValue( sValue, MB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "g" ) )
{
bytes = (long) ( parseByteValue( sValue, GB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "gb" ) )
{
bytes = (long) ( parseByteValue( sValue, GB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "t" ) )
{
bytes = (long) ( parseByteValue( sValue, TB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "tb" ) )
{
bytes = (long) ( parseByteValue( sValue, TB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "p" ) )
{
bytes = (long) ( parseByteValue( sValue, PB_FACTOR, 1 ) );
}
else if ( postFix.endsWith( "pb" ) )
{
bytes = (long) ( parseByteValue( sValue, PB_FACTOR, 2 ) );
}
else if ( postFix.endsWith( "b" ) )
{
bytes = Long.parseLong( sValue.substring( 0, sValue.length() - 1 ) );
}
else
{
bytes = Long.parseLong( sValue );
}

return bytes;
}

private static double parseByteValue( final String sValue, final long kbFactor, final int postfixLenght )
{
try
{
return Double.parseDouble( sValue.substring( 0, sValue.length() - postfixLenght ) ) * kbFactor;
}
catch ( NumberFormatException e )
{
throw new IllegalArgumentException( "Wrong format of size-value [" + sValue + "]" );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.enonic.xp.core.impl.image;
package com.enonic.xp.core.internal;

import java.util.function.LongSupplier;

import com.enonic.xp.util.ByteSizeParser;

public class MemoryLimitParser
public final class MemoryLimitParser
{
private final LongSupplier baselineSupplier;

Expand All @@ -24,4 +22,9 @@ public long parse( final String value )
return ByteSizeParser.parse( value );
}
}

public static MemoryLimitParser maxHeap()
{
return new MemoryLimitParser( Runtime.getRuntime()::maxMemory );
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.enonic.xp.util;
package com.enonic.xp.core.internal;

import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.enonic.xp.core.impl.image;
package com.enonic.xp.core.internal;

import java.util.function.LongSupplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public interface RepoConfiguration
{
Path getSnapshotsDir();

int cacheSize();
String cacheCapacity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Path getSnapshotsDir()
}

@Override
public int cacheSize()
public String cacheCapacity()
{
return Integer.parseInt( this.config.get( "cache.size" ) );
return this.config.get( "cache.capacity" );
}
}
Loading

0 comments on commit 53f906d

Please sign in to comment.