/** * Is executed after dependency injection is done to perform any initialization. * * @throws Exception * if an error occurs during {@link PostConstruct} */ @PostConstruct public void postConstruct() throws Exception { this.maxSize = new AtomicLong(bufferProperties.getInitialBufferSize()); this.evictionOccupancyPercentage = new AtomicInteger(Float.floatToIntBits(bufferProperties.getEvictionOccupancyPercentage())); this.objectSizes.setObjectSecurityExpansionRate(bufferProperties.getObjectSecurityExpansionRate(maxSize.get())); this.first = new AtomicReference<IBufferElement<E>>(emptyBufferElement); this.last = new AtomicReference<IBufferElement<E>>(emptyBufferElement); this.lastAnalyzed = new AtomicReference<IBufferElement<E>>(emptyBufferElement); this.lastIndexed = new AtomicReference<IBufferElement<E>>(emptyBufferElement); this.indexingTreeCleaningExecutorService = Executors.newFixedThreadPool(bufferProperties.getIndexingTreeCleaningThreads()); this.flagsSetOnBytes = bufferProperties.getFlagsSetOnBytes(this.maxSize.get()); // initialize processors this.analyzeProcessor = new AnalyzeBufferElementProcessor<>(this, lastAnalyzed, analyzeLock, nothingToAnalyze); this.indexProcessor = new IndexBufferElementProcessor<>(this, lastIndexed, indexingLock, nothingToIndex); if (log.isInfoEnabled()) { log.info("|-Using buffer with maximum size " + NumberFormat.getInstance().format(maxSize) + " bytes..."); log.info("|-Indexing tree maintenance on " + NumberFormat.getInstance().format(flagsSetOnBytes) + " bytes added/removed..."); log.info("|-Using object expansion rate of " + NumberFormat.getInstance().format(objectSizes.getObjectSecurityExpansionRate() * 100) + "%"); } }
/** * Single expansion rate test for buffer occupancy. */ @Test public void singleExpansionRateTestBufferOccupancy() { long oldGenMax = 100; long bufferSize = (long) (oldGenMax * (bufferProperties.maxObjectExpansionRateActiveFromOccupancy - ((bufferProperties.getMaxObjectExpansionRateActiveFromOccupancy() - bufferProperties.getMinObjectExpansionRateActiveTillOccupancy()) / 2))); float expansionRate = bufferProperties.getObjectSecurityExpansionRateBufferOccupancy(bufferSize, oldGenMax); float expectedRate = bufferProperties.getMinObjectExpansionRate() + ((bufferProperties.getMaxObjectExpansionRate() - bufferProperties.getMinObjectExpansionRate()) / 2); assertThat(expansionRate, is(equalTo(expectedRate))); }
/** * Single expansion rate test for buffer size. */ @Test public void singleExpansionRateTestBufferSize() { long bufferSize = bufferProperties.getMaxObjectExpansionRateActiveTillBufferSize() + ((bufferProperties.getMinObjectExpansionRateActiveFromBufferSize() - bufferProperties.getMaxObjectExpansionRateActiveTillBufferSize()) / 2); float expansionRate = bufferProperties.getObjectSecurityExpansionRateBufferSize(bufferSize); float expectedRate = bufferProperties.getMinObjectExpansionRate() + ((bufferProperties.getMaxObjectExpansionRate() - bufferProperties.getMinObjectExpansionRate()) / 2); assertThat(expansionRate, is(equalTo(expectedRate))); }
/** * Returns object security expansion rate based on the property set and given buffer size. * * @param bufferSize * Buffer's size that expansion rate has to be calculated for. * @return Expansion rate in percentages. */ public float getObjectSecurityExpansionRate(long bufferSize) { return (getObjectSecurityExpansionRateBufferSize(bufferSize) + getObjectSecurityExpansionRateBufferOccupancy(bufferSize, getOldGenMax())) / 2; }
/** * Updates the buffer size and to it related properties. */ @PropertyUpdate(properties = { "buffer.minOldSpaceOccupancy", "buffer.maxOldSpaceOccupancy", "buffer.minOldSpaceOccupancyActiveTillOldGenSize", "buffer.maxOldSpaceOccupancyActiveFromOldGenSize" }) protected void updateBufferSizeAndRelated() { this.maxSize.set(bufferProperties.getInitialBufferSize()); this.objectSizes.setObjectSecurityExpansionRate(bufferProperties.getObjectSecurityExpansionRate(maxSize.get())); this.flagsSetOnBytes = bufferProperties.getFlagsSetOnBytes(this.maxSize.get()); }
/** * General Parameterized test to assure that no matter how big buffer size is, expansion rate * will be between min and max related. * * @param bufferSize * Buffer size. */ @Test(dataProvider = "Buffer-Size-Provider") public void parametrizedExpansionRateTest(long bufferSize) { float expansionRate = bufferProperties.getObjectSecurityExpansionRate(bufferSize); assertThat(expansionRate, is(lessThanOrEqualTo(bufferProperties.getMaxObjectExpansionRate()))); assertThat(expansionRate, is(greaterThanOrEqualTo(bufferProperties.getMinObjectExpansionRate()))); float relatedToSize = bufferProperties.getObjectSecurityExpansionRateBufferSize(bufferSize); float relatedToOccupancy = bufferProperties.getObjectSecurityExpansionRateBufferOccupancy(bufferSize, bufferProperties.getOldGenMax()); assertThat(expansionRate, is(equalTo((relatedToSize + relatedToOccupancy) / 2))); }
when(bufferProperties.getInitialBufferSize()).thenReturn(elements); when(bufferProperties.getEvictionOccupancyPercentage()).thenReturn(0.5f); when(bufferProperties.getEvictionFragmentSizePercentage()).thenReturn(0.35f); when(bufferProperties.getFlagsSetOnBytes(anyLong())).thenReturn(flagsSetOnBytes); when(bufferProperties.getIndexingWaitTime()).thenReturn(10L); when(indexingTree.getComponentSize(objectSizes)).thenReturn(10L); buffer.postConstruct();
when(bufferProperties.getInitialBufferSize()).thenReturn(elements); when(bufferProperties.getEvictionOccupancyPercentage()).thenReturn(0.1f); when(bufferProperties.getEvictionFragmentSizePercentage()).thenReturn(0.5f); buffer.postConstruct();
if ((this.getBytesMaintenancePercentage() <= 0) && (this.getBytesMaintenancePercentage() > this.getEvictionOccupancyPercentage())) { throw new BeanInitializationException( "Buffer properties initialization error: The buffer bytes maintenance percentage that activate the clean and update of the indexing tree can not be less or equal than zero nor bigger that eviction occupancy percentage. Initialization value is: " + this.getBytesMaintenancePercentage()); if (this.getIndexingTreeCleaningThreads() <= 0) { throw new BeanInitializationException("Buffer properties initialization error: The number of indexing tree cleaning threads can not be less or equal than zero. Initialization value is: " + this.getIndexingTreeCleaningThreads());
@Test public void increaseExpansionRate() throws Exception { long bufferSize = bufferProperties.getInitialBufferSize(); Configuration configuration = propertyManager.getConfiguration(); SingleProperty<Float> maxExpansionRate = configuration.forLogicalName("buffer.maxObjectExpansionRate"); float oldExpansionrate = bufferProperties.getObjectSecurityExpansionRate(bufferSize); ConfigurationUpdate configurationUpdate = new ConfigurationUpdate(); configurationUpdate.addPropertyUpdate(maxExpansionRate.createAndValidatePropertyUpdate(Float.valueOf(maxExpansionRate.getValue().floatValue() + 0.1f))); propertyManager.updateConfiguration(configurationUpdate, false); float newExpansionrate = bufferProperties.getObjectSecurityExpansionRate(bufferSize); assertThat(newExpansionrate, is(greaterThan(oldExpansionrate))); }
long elements = 1 + random.nextInt(10000); when(bufferProperties.getInitialBufferSize()).thenReturn(elements); when(bufferProperties.getIndexingWaitTime()).thenReturn(5L);
when(bufferProperties.getInitialBufferSize()).thenReturn(elements); when(bufferProperties.getEvictionOccupancyPercentage()).thenReturn(0.99f); buffer.postConstruct();
Thread.sleep(atomicBuffer.bufferProperties.getIndexingWaitTime()); } catch (InterruptedException e) { Thread.interrupted();
/** * Returns the initial buffer size based on the property set. * * @return Size in bytes. */ public long getInitialBufferSize() { long bufferSize = 0; long oldGenMax = getOldGenMax(); // If we did not get the value, throw exception if (oldGenMax == 0) { throw new RuntimeException("Could not calculate the old generation heap space. Please make sure CMR is running on the provided JVM."); } // Otherwise calculate now if (oldGenMax > maxOldSpaceOccupancyActiveFromOldGenSize) { bufferSize = (long) (oldGenMax * maxOldSpaceOccupancy); } else if (oldGenMax < minOldSpaceOccupancyActiveTillOldGenSize) { bufferSize = (long) (oldGenMax * minOldSpaceOccupancy); } else { // delta is the value that defines how much we can extend the minimum heap // occupancy // percentage by analyzing the max memory size // delta is actually representing additional percentage of heap we can take // it is always thru that: minHeapSizeOccupancy + delta < // maxHeapSizeOccupancy float delta = (maxOldSpaceOccupancy - minOldSpaceOccupancy) * ((float) (oldGenMax - minOldSpaceOccupancyActiveTillOldGenSize) / (maxOldSpaceOccupancyActiveFromOldGenSize - minOldSpaceOccupancyActiveTillOldGenSize)); bufferSize = (long) (oldGenMax * (minOldSpaceOccupancy + delta)); } return bufferSize; }
/** * Updates value of the {@link #evictionOccupancyPercentage}. */ @PropertyUpdate(properties = { "buffer.evictionOccupancyPercentage", "buffer.bytesMaintenancePercentage", }) protected void updateEvictionOccupancyPercentage() { this.evictionOccupancyPercentage.set(Float.floatToIntBits(bufferProperties.getEvictionOccupancyPercentage())); }
/** * Updates value of the {@link #evictionOccupancyPercentage}. */ @PropertyUpdate(properties = { "buffer.bytesMaintenancePercentage", }) protected void updateBytesMaintenancePercentage() { this.flagsSetOnBytes = bufferProperties.getFlagsSetOnBytes(this.maxSize.get()); }
/** * Updates the object security expansion rate. */ @PropertyUpdate(properties = { "buffer.minObjectExpansionRate", "buffer.maxObjectExpansionRate", "buffer.maxObjectExpansionRateActiveTillBufferSize", "buffer.minObjectExpansionRateActiveFromBufferSize", "buffer.maxObjectExpansionRateActiveFromOccupancy", "buffer.minObjectExpansionRateActiveTillOccupancy" }) protected void updateObjectSecurityExpansionRate() { this.objectSizes.setObjectSecurityExpansionRate(bufferProperties.getObjectSecurityExpansionRate(maxSize.get())); }
long evictionFragmentMaxSize = (long) (this.getMaxSize() * bufferProperties.getEvictionFragmentSizePercentage()); long fragmentSize = 0; int elementsInFragment = 0;
/** * Init. * * @throws Exception */ @BeforeMethod public void init() throws Exception { MockitoAnnotations.initMocks(this); buffer = new AtomicBuffer<>(); buffer.bufferProperties = bufferProperties; buffer.objectSizes = objectSizes; buffer.indexingTree = indexingTree; buffer.log = LoggerFactory.getLogger(AtomicBuffer.class); when(bufferProperties.getIndexingTreeCleaningThreads()).thenReturn(1); buffer.postConstruct(); }
@Test public void increaseBufferSizeWithOccupancy() throws Exception { Configuration configuration = propertyManager.getConfiguration(); SingleProperty<Float> maxOldSpaceOcc = configuration.forLogicalName("buffer.maxOldSpaceOccupancy"); SingleProperty<Float> minOldSpaceOcc = configuration.forLogicalName("buffer.minOldSpaceOccupancy"); long oldBufferSize = bufferProperties.getInitialBufferSize(); ConfigurationUpdate configurationUpdate = new ConfigurationUpdate(); configurationUpdate.addPropertyUpdate(maxOldSpaceOcc.createAndValidatePropertyUpdate(Float.valueOf(maxOldSpaceOcc.getValue().floatValue() + 0.05f))); configurationUpdate.addPropertyUpdate(minOldSpaceOcc.createAndValidatePropertyUpdate(Float.valueOf(minOldSpaceOcc.getValue().floatValue() + 0.05f))); propertyManager.updateConfiguration(configurationUpdate, false); long newBufferSize = bufferProperties.getInitialBufferSize(); assertThat(newBufferSize, is(greaterThan(oldBufferSize))); assertThat(newBufferSize, is(buffer.getMaxSize())); }