Repository: hbase-site Updated Branches: refs/heads/asf-site b5f95ca57 -> bd675fa38
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/bd675fa3/devapidocs/src-html/org/apache/hadoop/hbase/regionserver/ChunkCreator.ChunkType.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/regionserver/ChunkCreator.ChunkType.html b/devapidocs/src-html/org/apache/hadoop/hbase/regionserver/ChunkCreator.ChunkType.html index 93bb11a..6cd219e 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/regionserver/ChunkCreator.ChunkType.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/regionserver/ChunkCreator.ChunkType.html @@ -106,13 +106,13 @@ <span class="sourceLineNo">098</span> float poolSizePercentage, float indexChunkSizePercentage,<a name="line.98"></a> <span class="sourceLineNo">099</span> float initialCountPercentage,<a name="line.99"></a> <span class="sourceLineNo">100</span> HeapMemoryManager heapMemoryManager) {<a name="line.100"></a> -<span class="sourceLineNo">101</span> this.dataChunksPool = initializePool(globalMemStoreSize,<a name="line.101"></a> +<span class="sourceLineNo">101</span> this.dataChunksPool = initializePool("data", globalMemStoreSize,<a name="line.101"></a> <span class="sourceLineNo">102</span> (1 - indexChunkSizePercentage) * poolSizePercentage,<a name="line.102"></a> <span class="sourceLineNo">103</span> initialCountPercentage, chunkSize, heapMemoryManager);<a name="line.103"></a> <span class="sourceLineNo">104</span> // The index chunks pool is needed only when the index type is CCM.<a name="line.104"></a> <span class="sourceLineNo">105</span> // Since the pools are not created at all when the index type isn't CCM,<a name="line.105"></a> <span class="sourceLineNo">106</span> // we don't need to check it here.<a name="line.106"></a> -<span class="sourceLineNo">107</span> this.indexChunksPool = initializePool(globalMemStoreSize,<a name="line.107"></a> +<span class="sourceLineNo">107</span> this.indexChunksPool = initializePool("index", globalMemStoreSize,<a name="line.107"></a> <span class="sourceLineNo">108</span> indexChunkSizePercentage * poolSizePercentage,<a name="line.108"></a> <span class="sourceLineNo">109</span> initialCountPercentage, (int) (indexChunkSizePercentage * chunkSize),<a name="line.109"></a> <span class="sourceLineNo">110</span> heapMemoryManager);<a name="line.110"></a> @@ -339,313 +339,315 @@ <span class="sourceLineNo">331</span> private static final int statThreadPeriod = 60 * 5;<a name="line.331"></a> <span class="sourceLineNo">332</span> private final AtomicLong chunkCount = new AtomicLong();<a name="line.332"></a> <span class="sourceLineNo">333</span> private final LongAdder reusedChunkCount = new LongAdder();<a name="line.333"></a> -<span class="sourceLineNo">334</span><a name="line.334"></a> -<span class="sourceLineNo">335</span> MemStoreChunkPool(int chunkSize, int maxCount, int initialCount, float poolSizePercentage) {<a name="line.335"></a> -<span class="sourceLineNo">336</span> this.chunkSize = chunkSize;<a name="line.336"></a> -<span class="sourceLineNo">337</span> this.maxCount = maxCount;<a name="line.337"></a> -<span class="sourceLineNo">338</span> this.poolSizePercentage = poolSizePercentage;<a name="line.338"></a> -<span class="sourceLineNo">339</span> this.reclaimedChunks = new LinkedBlockingQueue<>();<a name="line.339"></a> -<span class="sourceLineNo">340</span> for (int i = 0; i < initialCount; i++) {<a name="line.340"></a> -<span class="sourceLineNo">341</span> Chunk chunk = createChunk(true, CompactingMemStore.IndexType.ARRAY_MAP, chunkSize);<a name="line.341"></a> -<span class="sourceLineNo">342</span> chunk.init();<a name="line.342"></a> -<span class="sourceLineNo">343</span> reclaimedChunks.add(chunk);<a name="line.343"></a> -<span class="sourceLineNo">344</span> }<a name="line.344"></a> -<span class="sourceLineNo">345</span> chunkCount.set(initialCount);<a name="line.345"></a> -<span class="sourceLineNo">346</span> final String n = Thread.currentThread().getName();<a name="line.346"></a> -<span class="sourceLineNo">347</span> scheduleThreadPool = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()<a name="line.347"></a> -<span class="sourceLineNo">348</span> .setNameFormat(n + "-MemStoreChunkPool Statistics").setDaemon(true).build());<a name="line.348"></a> -<span class="sourceLineNo">349</span> this.scheduleThreadPool.scheduleAtFixedRate(new StatisticsThread(), statThreadPeriod,<a name="line.349"></a> -<span class="sourceLineNo">350</span> statThreadPeriod, TimeUnit.SECONDS);<a name="line.350"></a> -<span class="sourceLineNo">351</span> }<a name="line.351"></a> -<span class="sourceLineNo">352</span><a name="line.352"></a> -<span class="sourceLineNo">353</span> /**<a name="line.353"></a> -<span class="sourceLineNo">354</span> * Poll a chunk from the pool, reset it if not null, else create a new chunk to return if we have<a name="line.354"></a> -<span class="sourceLineNo">355</span> * not yet created max allowed chunks count. When we have already created max allowed chunks and<a name="line.355"></a> -<span class="sourceLineNo">356</span> * no free chunks as of now, return null. It is the responsibility of the caller to make a chunk<a name="line.356"></a> -<span class="sourceLineNo">357</span> * then.<a name="line.357"></a> -<span class="sourceLineNo">358</span> * Note: Chunks returned by this pool must be put back to the pool after its use.<a name="line.358"></a> -<span class="sourceLineNo">359</span> * @return a chunk<a name="line.359"></a> -<span class="sourceLineNo">360</span> * @see #putbackChunks(Chunk)<a name="line.360"></a> -<span class="sourceLineNo">361</span> */<a name="line.361"></a> -<span class="sourceLineNo">362</span> Chunk getChunk() {<a name="line.362"></a> -<span class="sourceLineNo">363</span> return getChunk(CompactingMemStore.IndexType.ARRAY_MAP);<a name="line.363"></a> -<span class="sourceLineNo">364</span> }<a name="line.364"></a> -<span class="sourceLineNo">365</span><a name="line.365"></a> -<span class="sourceLineNo">366</span> Chunk getChunk(CompactingMemStore.IndexType chunkIndexType) {<a name="line.366"></a> -<span class="sourceLineNo">367</span> Chunk chunk = reclaimedChunks.poll();<a name="line.367"></a> -<span class="sourceLineNo">368</span> if (chunk != null) {<a name="line.368"></a> -<span class="sourceLineNo">369</span> chunk.reset();<a name="line.369"></a> -<span class="sourceLineNo">370</span> reusedChunkCount.increment();<a name="line.370"></a> -<span class="sourceLineNo">371</span> } else {<a name="line.371"></a> -<span class="sourceLineNo">372</span> // Make a chunk iff we have not yet created the maxCount chunks<a name="line.372"></a> -<span class="sourceLineNo">373</span> while (true) {<a name="line.373"></a> -<span class="sourceLineNo">374</span> long created = this.chunkCount.get();<a name="line.374"></a> -<span class="sourceLineNo">375</span> if (created < this.maxCount) {<a name="line.375"></a> -<span class="sourceLineNo">376</span> if (this.chunkCount.compareAndSet(created, created + 1)) {<a name="line.376"></a> -<span class="sourceLineNo">377</span> chunk = createChunkForPool(chunkIndexType, chunkSize);<a name="line.377"></a> -<span class="sourceLineNo">378</span> break;<a name="line.378"></a> -<span class="sourceLineNo">379</span> }<a name="line.379"></a> -<span class="sourceLineNo">380</span> } else {<a name="line.380"></a> -<span class="sourceLineNo">381</span> break;<a name="line.381"></a> -<span class="sourceLineNo">382</span> }<a name="line.382"></a> -<span class="sourceLineNo">383</span> }<a name="line.383"></a> -<span class="sourceLineNo">384</span> }<a name="line.384"></a> -<span class="sourceLineNo">385</span> return chunk;<a name="line.385"></a> -<span class="sourceLineNo">386</span> }<a name="line.386"></a> -<span class="sourceLineNo">387</span><a name="line.387"></a> -<span class="sourceLineNo">388</span> int getChunkSize() {<a name="line.388"></a> -<span class="sourceLineNo">389</span> return chunkSize;<a name="line.389"></a> -<span class="sourceLineNo">390</span> }<a name="line.390"></a> -<span class="sourceLineNo">391</span><a name="line.391"></a> -<span class="sourceLineNo">392</span> /**<a name="line.392"></a> -<span class="sourceLineNo">393</span> * Add the chunks to the pool, when the pool achieves the max size, it will skip the remaining<a name="line.393"></a> -<span class="sourceLineNo">394</span> * chunks<a name="line.394"></a> -<span class="sourceLineNo">395</span> * @param c<a name="line.395"></a> -<span class="sourceLineNo">396</span> */<a name="line.396"></a> -<span class="sourceLineNo">397</span> private void putbackChunks(Chunk c) {<a name="line.397"></a> -<span class="sourceLineNo">398</span> int toAdd = this.maxCount - reclaimedChunks.size();<a name="line.398"></a> -<span class="sourceLineNo">399</span> if (c.isFromPool() && c.size == chunkSize && toAdd > 0) {<a name="line.399"></a> -<span class="sourceLineNo">400</span> reclaimedChunks.add(c);<a name="line.400"></a> -<span class="sourceLineNo">401</span> } else {<a name="line.401"></a> -<span class="sourceLineNo">402</span> // remove the chunk (that is not going to pool)<a name="line.402"></a> -<span class="sourceLineNo">403</span> // though it is initially from the pool or not<a name="line.403"></a> -<span class="sourceLineNo">404</span> ChunkCreator.this.removeChunk(c.getId());<a name="line.404"></a> -<span class="sourceLineNo">405</span> }<a name="line.405"></a> -<span class="sourceLineNo">406</span> }<a name="line.406"></a> -<span class="sourceLineNo">407</span><a name="line.407"></a> -<span class="sourceLineNo">408</span> private class StatisticsThread extends Thread {<a name="line.408"></a> -<span class="sourceLineNo">409</span> StatisticsThread() {<a name="line.409"></a> -<span class="sourceLineNo">410</span> super("MemStoreChunkPool.StatisticsThread");<a name="line.410"></a> -<span class="sourceLineNo">411</span> setDaemon(true);<a name="line.411"></a> -<span class="sourceLineNo">412</span> }<a name="line.412"></a> -<span class="sourceLineNo">413</span><a name="line.413"></a> -<span class="sourceLineNo">414</span> @Override<a name="line.414"></a> -<span class="sourceLineNo">415</span> public void run() {<a name="line.415"></a> -<span class="sourceLineNo">416</span> logStats();<a name="line.416"></a> -<span class="sourceLineNo">417</span> }<a name="line.417"></a> -<span class="sourceLineNo">418</span><a name="line.418"></a> -<span class="sourceLineNo">419</span> private void logStats() {<a name="line.419"></a> -<span class="sourceLineNo">420</span> if (!LOG.isDebugEnabled()) return;<a name="line.420"></a> -<span class="sourceLineNo">421</span> long created = chunkCount.get();<a name="line.421"></a> -<span class="sourceLineNo">422</span> long reused = reusedChunkCount.sum();<a name="line.422"></a> -<span class="sourceLineNo">423</span> long total = created + reused;<a name="line.423"></a> -<span class="sourceLineNo">424</span> LOG.debug("Stats (chunk size=" + chunkSize + "): "<a name="line.424"></a> -<span class="sourceLineNo">425</span> + "current pool size=" + reclaimedChunks.size()<a name="line.425"></a> -<span class="sourceLineNo">426</span> + ",created chunk count=" + created<a name="line.426"></a> -<span class="sourceLineNo">427</span> + ",reused chunk count=" + reused<a name="line.427"></a> -<span class="sourceLineNo">428</span> + ",reuseRatio=" + (total == 0 ? "0" : StringUtils.formatPercent(<a name="line.428"></a> -<span class="sourceLineNo">429</span> (float) reused / (float) total, 2)));<a name="line.429"></a> -<span class="sourceLineNo">430</span> }<a name="line.430"></a> -<span class="sourceLineNo">431</span> }<a name="line.431"></a> -<span class="sourceLineNo">432</span><a name="line.432"></a> -<span class="sourceLineNo">433</span> private int getMaxCount() {<a name="line.433"></a> -<span class="sourceLineNo">434</span> return this.maxCount;<a name="line.434"></a> -<span class="sourceLineNo">435</span> }<a name="line.435"></a> -<span class="sourceLineNo">436</span><a name="line.436"></a> -<span class="sourceLineNo">437</span> @Override<a name="line.437"></a> -<span class="sourceLineNo">438</span> public void onHeapMemoryTune(long newMemstoreSize, long newBlockCacheSize) {<a name="line.438"></a> -<span class="sourceLineNo">439</span> // don't do any tuning in case of offheap memstore<a name="line.439"></a> -<span class="sourceLineNo">440</span> if (isOffheap()) {<a name="line.440"></a> -<span class="sourceLineNo">441</span> LOG.warn("Not tuning the chunk pool as it is offheap");<a name="line.441"></a> -<span class="sourceLineNo">442</span> return;<a name="line.442"></a> -<span class="sourceLineNo">443</span> }<a name="line.443"></a> -<span class="sourceLineNo">444</span> int newMaxCount =<a name="line.444"></a> -<span class="sourceLineNo">445</span> (int) (newMemstoreSize * poolSizePercentage / getChunkSize());<a name="line.445"></a> -<span class="sourceLineNo">446</span> if (newMaxCount != this.maxCount) {<a name="line.446"></a> -<span class="sourceLineNo">447</span> // We need an adjustment in the chunks numbers<a name="line.447"></a> -<span class="sourceLineNo">448</span> if (newMaxCount > this.maxCount) {<a name="line.448"></a> -<span class="sourceLineNo">449</span> // Max chunks getting increased. Just change the variable. Later calls to getChunk() would<a name="line.449"></a> -<span class="sourceLineNo">450</span> // create and add them to Q<a name="line.450"></a> -<span class="sourceLineNo">451</span> LOG.info("Max count for chunks increased from " + this.maxCount + " to " + newMaxCount);<a name="line.451"></a> -<span class="sourceLineNo">452</span> this.maxCount = newMaxCount;<a name="line.452"></a> -<span class="sourceLineNo">453</span> } else {<a name="line.453"></a> -<span class="sourceLineNo">454</span> // Max chunks getting decreased. We may need to clear off some of the pooled chunks now<a name="line.454"></a> -<span class="sourceLineNo">455</span> // itself. If the extra chunks are serving already, do not pool those when we get them back<a name="line.455"></a> -<span class="sourceLineNo">456</span> LOG.info("Max count for chunks decreased from " + this.maxCount + " to " + newMaxCount);<a name="line.456"></a> -<span class="sourceLineNo">457</span> this.maxCount = newMaxCount;<a name="line.457"></a> -<span class="sourceLineNo">458</span> if (this.reclaimedChunks.size() > newMaxCount) {<a name="line.458"></a> -<span class="sourceLineNo">459</span> synchronized (this) {<a name="line.459"></a> -<span class="sourceLineNo">460</span> while (this.reclaimedChunks.size() > newMaxCount) {<a name="line.460"></a> -<span class="sourceLineNo">461</span> this.reclaimedChunks.poll();<a name="line.461"></a> -<span class="sourceLineNo">462</span> }<a name="line.462"></a> -<span class="sourceLineNo">463</span> }<a name="line.463"></a> -<span class="sourceLineNo">464</span> }<a name="line.464"></a> -<span class="sourceLineNo">465</span> }<a name="line.465"></a> -<span class="sourceLineNo">466</span> }<a name="line.466"></a> -<span class="sourceLineNo">467</span> }<a name="line.467"></a> -<span class="sourceLineNo">468</span> }<a name="line.468"></a> -<span class="sourceLineNo">469</span><a name="line.469"></a> -<span class="sourceLineNo">470</span> @VisibleForTesting<a name="line.470"></a> -<span class="sourceLineNo">471</span> static void clearDisableFlag() {<a name="line.471"></a> -<span class="sourceLineNo">472</span> chunkPoolDisabled = false;<a name="line.472"></a> -<span class="sourceLineNo">473</span> }<a name="line.473"></a> -<span class="sourceLineNo">474</span><a name="line.474"></a> -<span class="sourceLineNo">475</span> private MemStoreChunkPool initializePool(long globalMemStoreSize, float poolSizePercentage,<a name="line.475"></a> -<span class="sourceLineNo">476</span> float initialCountPercentage, int chunkSize,<a name="line.476"></a> -<span class="sourceLineNo">477</span> HeapMemoryManager heapMemoryManager) {<a name="line.477"></a> -<span class="sourceLineNo">478</span> if (poolSizePercentage <= 0) {<a name="line.478"></a> -<span class="sourceLineNo">479</span> LOG.info("PoolSizePercentage is less than 0. So not using pool");<a name="line.479"></a> -<span class="sourceLineNo">480</span> return null;<a name="line.480"></a> -<span class="sourceLineNo">481</span> }<a name="line.481"></a> -<span class="sourceLineNo">482</span> if (chunkPoolDisabled) {<a name="line.482"></a> +<span class="sourceLineNo">334</span> private final String label;<a name="line.334"></a> +<span class="sourceLineNo">335</span><a name="line.335"></a> +<span class="sourceLineNo">336</span> MemStoreChunkPool(String label, int chunkSize, int maxCount, int initialCount,<a name="line.336"></a> +<span class="sourceLineNo">337</span> float poolSizePercentage) {<a name="line.337"></a> +<span class="sourceLineNo">338</span> this.label = label;<a name="line.338"></a> +<span class="sourceLineNo">339</span> this.chunkSize = chunkSize;<a name="line.339"></a> +<span class="sourceLineNo">340</span> this.maxCount = maxCount;<a name="line.340"></a> +<span class="sourceLineNo">341</span> this.poolSizePercentage = poolSizePercentage;<a name="line.341"></a> +<span class="sourceLineNo">342</span> this.reclaimedChunks = new LinkedBlockingQueue<>();<a name="line.342"></a> +<span class="sourceLineNo">343</span> for (int i = 0; i < initialCount; i++) {<a name="line.343"></a> +<span class="sourceLineNo">344</span> Chunk chunk = createChunk(true, CompactingMemStore.IndexType.ARRAY_MAP, chunkSize);<a name="line.344"></a> +<span class="sourceLineNo">345</span> chunk.init();<a name="line.345"></a> +<span class="sourceLineNo">346</span> reclaimedChunks.add(chunk);<a name="line.346"></a> +<span class="sourceLineNo">347</span> }<a name="line.347"></a> +<span class="sourceLineNo">348</span> chunkCount.set(initialCount);<a name="line.348"></a> +<span class="sourceLineNo">349</span> final String n = Thread.currentThread().getName();<a name="line.349"></a> +<span class="sourceLineNo">350</span> scheduleThreadPool = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()<a name="line.350"></a> +<span class="sourceLineNo">351</span> .setNameFormat(n + "-MemStoreChunkPool Statistics").setDaemon(true).build());<a name="line.351"></a> +<span class="sourceLineNo">352</span> this.scheduleThreadPool.scheduleAtFixedRate(new StatisticsThread(), statThreadPeriod,<a name="line.352"></a> +<span class="sourceLineNo">353</span> statThreadPeriod, TimeUnit.SECONDS);<a name="line.353"></a> +<span class="sourceLineNo">354</span> }<a name="line.354"></a> +<span class="sourceLineNo">355</span><a name="line.355"></a> +<span class="sourceLineNo">356</span> /**<a name="line.356"></a> +<span class="sourceLineNo">357</span> * Poll a chunk from the pool, reset it if not null, else create a new chunk to return if we have<a name="line.357"></a> +<span class="sourceLineNo">358</span> * not yet created max allowed chunks count. When we have already created max allowed chunks and<a name="line.358"></a> +<span class="sourceLineNo">359</span> * no free chunks as of now, return null. It is the responsibility of the caller to make a chunk<a name="line.359"></a> +<span class="sourceLineNo">360</span> * then.<a name="line.360"></a> +<span class="sourceLineNo">361</span> * Note: Chunks returned by this pool must be put back to the pool after its use.<a name="line.361"></a> +<span class="sourceLineNo">362</span> * @return a chunk<a name="line.362"></a> +<span class="sourceLineNo">363</span> * @see #putbackChunks(Chunk)<a name="line.363"></a> +<span class="sourceLineNo">364</span> */<a name="line.364"></a> +<span class="sourceLineNo">365</span> Chunk getChunk() {<a name="line.365"></a> +<span class="sourceLineNo">366</span> return getChunk(CompactingMemStore.IndexType.ARRAY_MAP);<a name="line.366"></a> +<span class="sourceLineNo">367</span> }<a name="line.367"></a> +<span class="sourceLineNo">368</span><a name="line.368"></a> +<span class="sourceLineNo">369</span> Chunk getChunk(CompactingMemStore.IndexType chunkIndexType) {<a name="line.369"></a> +<span class="sourceLineNo">370</span> Chunk chunk = reclaimedChunks.poll();<a name="line.370"></a> +<span class="sourceLineNo">371</span> if (chunk != null) {<a name="line.371"></a> +<span class="sourceLineNo">372</span> chunk.reset();<a name="line.372"></a> +<span class="sourceLineNo">373</span> reusedChunkCount.increment();<a name="line.373"></a> +<span class="sourceLineNo">374</span> } else {<a name="line.374"></a> +<span class="sourceLineNo">375</span> // Make a chunk iff we have not yet created the maxCount chunks<a name="line.375"></a> +<span class="sourceLineNo">376</span> while (true) {<a name="line.376"></a> +<span class="sourceLineNo">377</span> long created = this.chunkCount.get();<a name="line.377"></a> +<span class="sourceLineNo">378</span> if (created < this.maxCount) {<a name="line.378"></a> +<span class="sourceLineNo">379</span> if (this.chunkCount.compareAndSet(created, created + 1)) {<a name="line.379"></a> +<span class="sourceLineNo">380</span> chunk = createChunkForPool(chunkIndexType, chunkSize);<a name="line.380"></a> +<span class="sourceLineNo">381</span> break;<a name="line.381"></a> +<span class="sourceLineNo">382</span> }<a name="line.382"></a> +<span class="sourceLineNo">383</span> } else {<a name="line.383"></a> +<span class="sourceLineNo">384</span> break;<a name="line.384"></a> +<span class="sourceLineNo">385</span> }<a name="line.385"></a> +<span class="sourceLineNo">386</span> }<a name="line.386"></a> +<span class="sourceLineNo">387</span> }<a name="line.387"></a> +<span class="sourceLineNo">388</span> return chunk;<a name="line.388"></a> +<span class="sourceLineNo">389</span> }<a name="line.389"></a> +<span class="sourceLineNo">390</span><a name="line.390"></a> +<span class="sourceLineNo">391</span> int getChunkSize() {<a name="line.391"></a> +<span class="sourceLineNo">392</span> return chunkSize;<a name="line.392"></a> +<span class="sourceLineNo">393</span> }<a name="line.393"></a> +<span class="sourceLineNo">394</span><a name="line.394"></a> +<span class="sourceLineNo">395</span> /**<a name="line.395"></a> +<span class="sourceLineNo">396</span> * Add the chunks to the pool, when the pool achieves the max size, it will skip the remaining<a name="line.396"></a> +<span class="sourceLineNo">397</span> * chunks<a name="line.397"></a> +<span class="sourceLineNo">398</span> * @param c<a name="line.398"></a> +<span class="sourceLineNo">399</span> */<a name="line.399"></a> +<span class="sourceLineNo">400</span> private void putbackChunks(Chunk c) {<a name="line.400"></a> +<span class="sourceLineNo">401</span> int toAdd = this.maxCount - reclaimedChunks.size();<a name="line.401"></a> +<span class="sourceLineNo">402</span> if (c.isFromPool() && c.size == chunkSize && toAdd > 0) {<a name="line.402"></a> +<span class="sourceLineNo">403</span> reclaimedChunks.add(c);<a name="line.403"></a> +<span class="sourceLineNo">404</span> } else {<a name="line.404"></a> +<span class="sourceLineNo">405</span> // remove the chunk (that is not going to pool)<a name="line.405"></a> +<span class="sourceLineNo">406</span> // though it is initially from the pool or not<a name="line.406"></a> +<span class="sourceLineNo">407</span> ChunkCreator.this.removeChunk(c.getId());<a name="line.407"></a> +<span class="sourceLineNo">408</span> }<a name="line.408"></a> +<span class="sourceLineNo">409</span> }<a name="line.409"></a> +<span class="sourceLineNo">410</span><a name="line.410"></a> +<span class="sourceLineNo">411</span> private class StatisticsThread extends Thread {<a name="line.411"></a> +<span class="sourceLineNo">412</span> StatisticsThread() {<a name="line.412"></a> +<span class="sourceLineNo">413</span> super("MemStoreChunkPool.StatisticsThread");<a name="line.413"></a> +<span class="sourceLineNo">414</span> setDaemon(true);<a name="line.414"></a> +<span class="sourceLineNo">415</span> }<a name="line.415"></a> +<span class="sourceLineNo">416</span><a name="line.416"></a> +<span class="sourceLineNo">417</span> @Override<a name="line.417"></a> +<span class="sourceLineNo">418</span> public void run() {<a name="line.418"></a> +<span class="sourceLineNo">419</span> logStats();<a name="line.419"></a> +<span class="sourceLineNo">420</span> }<a name="line.420"></a> +<span class="sourceLineNo">421</span><a name="line.421"></a> +<span class="sourceLineNo">422</span> private void logStats() {<a name="line.422"></a> +<span class="sourceLineNo">423</span> if (!LOG.isDebugEnabled()) return;<a name="line.423"></a> +<span class="sourceLineNo">424</span> long created = chunkCount.get();<a name="line.424"></a> +<span class="sourceLineNo">425</span> long reused = reusedChunkCount.sum();<a name="line.425"></a> +<span class="sourceLineNo">426</span> long total = created + reused;<a name="line.426"></a> +<span class="sourceLineNo">427</span> LOG.debug("{} Stats (chunk size={}): current pool size={}, created chunk count={}, " +<a name="line.427"></a> +<span class="sourceLineNo">428</span> "reused chunk count={}, reuseRatio={}", label, chunkSize, reclaimedChunks.size(),<a name="line.428"></a> +<span class="sourceLineNo">429</span> created, reused,<a name="line.429"></a> +<span class="sourceLineNo">430</span> (total == 0? "0": StringUtils.formatPercent((float)reused/(float)total,2)));<a name="line.430"></a> +<span class="sourceLineNo">431</span> }<a name="line.431"></a> +<span class="sourceLineNo">432</span> }<a name="line.432"></a> +<span class="sourceLineNo">433</span><a name="line.433"></a> +<span class="sourceLineNo">434</span> private int getMaxCount() {<a name="line.434"></a> +<span class="sourceLineNo">435</span> return this.maxCount;<a name="line.435"></a> +<span class="sourceLineNo">436</span> }<a name="line.436"></a> +<span class="sourceLineNo">437</span><a name="line.437"></a> +<span class="sourceLineNo">438</span> @Override<a name="line.438"></a> +<span class="sourceLineNo">439</span> public void onHeapMemoryTune(long newMemstoreSize, long newBlockCacheSize) {<a name="line.439"></a> +<span class="sourceLineNo">440</span> // don't do any tuning in case of offheap memstore<a name="line.440"></a> +<span class="sourceLineNo">441</span> if (isOffheap()) {<a name="line.441"></a> +<span class="sourceLineNo">442</span> LOG.warn("{} not tuning the chunk pool as it is offheap", label);<a name="line.442"></a> +<span class="sourceLineNo">443</span> return;<a name="line.443"></a> +<span class="sourceLineNo">444</span> }<a name="line.444"></a> +<span class="sourceLineNo">445</span> int newMaxCount =<a name="line.445"></a> +<span class="sourceLineNo">446</span> (int) (newMemstoreSize * poolSizePercentage / getChunkSize());<a name="line.446"></a> +<span class="sourceLineNo">447</span> if (newMaxCount != this.maxCount) {<a name="line.447"></a> +<span class="sourceLineNo">448</span> // We need an adjustment in the chunks numbers<a name="line.448"></a> +<span class="sourceLineNo">449</span> if (newMaxCount > this.maxCount) {<a name="line.449"></a> +<span class="sourceLineNo">450</span> // Max chunks getting increased. Just change the variable. Later calls to getChunk() would<a name="line.450"></a> +<span class="sourceLineNo">451</span> // create and add them to Q<a name="line.451"></a> +<span class="sourceLineNo">452</span> LOG.info("{} max count for chunks increased from {} to {}", this.label, this.maxCount,<a name="line.452"></a> +<span class="sourceLineNo">453</span> newMaxCount);<a name="line.453"></a> +<span class="sourceLineNo">454</span> this.maxCount = newMaxCount;<a name="line.454"></a> +<span class="sourceLineNo">455</span> } else {<a name="line.455"></a> +<span class="sourceLineNo">456</span> // Max chunks getting decreased. We may need to clear off some of the pooled chunks now<a name="line.456"></a> +<span class="sourceLineNo">457</span> // itself. If the extra chunks are serving already, do not pool those when we get them back<a name="line.457"></a> +<span class="sourceLineNo">458</span> LOG.info("{} max count for chunks decreased from {} to {}", this.label, this.maxCount,<a name="line.458"></a> +<span class="sourceLineNo">459</span> newMaxCount);<a name="line.459"></a> +<span class="sourceLineNo">460</span> this.maxCount = newMaxCount;<a name="line.460"></a> +<span class="sourceLineNo">461</span> if (this.reclaimedChunks.size() > newMaxCount) {<a name="line.461"></a> +<span class="sourceLineNo">462</span> synchronized (this) {<a name="line.462"></a> +<span class="sourceLineNo">463</span> while (this.reclaimedChunks.size() > newMaxCount) {<a name="line.463"></a> +<span class="sourceLineNo">464</span> this.reclaimedChunks.poll();<a name="line.464"></a> +<span class="sourceLineNo">465</span> }<a name="line.465"></a> +<span class="sourceLineNo">466</span> }<a name="line.466"></a> +<span class="sourceLineNo">467</span> }<a name="line.467"></a> +<span class="sourceLineNo">468</span> }<a name="line.468"></a> +<span class="sourceLineNo">469</span> }<a name="line.469"></a> +<span class="sourceLineNo">470</span> }<a name="line.470"></a> +<span class="sourceLineNo">471</span> }<a name="line.471"></a> +<span class="sourceLineNo">472</span><a name="line.472"></a> +<span class="sourceLineNo">473</span> @VisibleForTesting<a name="line.473"></a> +<span class="sourceLineNo">474</span> static void clearDisableFlag() {<a name="line.474"></a> +<span class="sourceLineNo">475</span> chunkPoolDisabled = false;<a name="line.475"></a> +<span class="sourceLineNo">476</span> }<a name="line.476"></a> +<span class="sourceLineNo">477</span><a name="line.477"></a> +<span class="sourceLineNo">478</span> private MemStoreChunkPool initializePool(String label, long globalMemStoreSize,<a name="line.478"></a> +<span class="sourceLineNo">479</span> float poolSizePercentage, float initialCountPercentage, int chunkSize,<a name="line.479"></a> +<span class="sourceLineNo">480</span> HeapMemoryManager heapMemoryManager) {<a name="line.480"></a> +<span class="sourceLineNo">481</span> if (poolSizePercentage <= 0) {<a name="line.481"></a> +<span class="sourceLineNo">482</span> LOG.info("{} poolSizePercentage is less than 0. So not using pool", label);<a name="line.482"></a> <span class="sourceLineNo">483</span> return null;<a name="line.483"></a> <span class="sourceLineNo">484</span> }<a name="line.484"></a> -<span class="sourceLineNo">485</span> if (poolSizePercentage > 1.0) {<a name="line.485"></a> -<span class="sourceLineNo">486</span> throw new IllegalArgumentException(<a name="line.486"></a> -<span class="sourceLineNo">487</span> MemStoreLAB.CHUNK_POOL_MAXSIZE_KEY + " must be between 0.0 and 1.0");<a name="line.487"></a> -<span class="sourceLineNo">488</span> }<a name="line.488"></a> -<span class="sourceLineNo">489</span> int maxCount = (int) (globalMemStoreSize * poolSizePercentage / chunkSize);<a name="line.489"></a> -<span class="sourceLineNo">490</span> if (initialCountPercentage > 1.0 || initialCountPercentage < 0) {<a name="line.490"></a> -<span class="sourceLineNo">491</span> throw new IllegalArgumentException(<a name="line.491"></a> -<span class="sourceLineNo">492</span> MemStoreLAB.CHUNK_POOL_INITIALSIZE_KEY + " must be between 0.0 and 1.0");<a name="line.492"></a> -<span class="sourceLineNo">493</span> }<a name="line.493"></a> -<span class="sourceLineNo">494</span> int initialCount = (int) (initialCountPercentage * maxCount);<a name="line.494"></a> -<span class="sourceLineNo">495</span> LOG.info("Allocating MemStoreChunkPool with chunk size "<a name="line.495"></a> -<span class="sourceLineNo">496</span> + StringUtils.byteDesc(chunkSize) + ", max count " + maxCount<a name="line.496"></a> -<span class="sourceLineNo">497</span> + ", initial count " + initialCount);<a name="line.497"></a> -<span class="sourceLineNo">498</span> MemStoreChunkPool memStoreChunkPool = new MemStoreChunkPool(chunkSize, maxCount,<a name="line.498"></a> -<span class="sourceLineNo">499</span> initialCount, poolSizePercentage);<a name="line.499"></a> -<span class="sourceLineNo">500</span> if (heapMemoryManager != null && memStoreChunkPool != null) {<a name="line.500"></a> -<span class="sourceLineNo">501</span> // Register with Heap Memory manager<a name="line.501"></a> -<span class="sourceLineNo">502</span> heapMemoryManager.registerTuneObserver(memStoreChunkPool);<a name="line.502"></a> -<span class="sourceLineNo">503</span> }<a name="line.503"></a> -<span class="sourceLineNo">504</span> return memStoreChunkPool;<a name="line.504"></a> -<span class="sourceLineNo">505</span> }<a name="line.505"></a> -<span class="sourceLineNo">506</span><a name="line.506"></a> -<span class="sourceLineNo">507</span> @VisibleForTesting<a name="line.507"></a> -<span class="sourceLineNo">508</span> int getMaxCount() {<a name="line.508"></a> -<span class="sourceLineNo">509</span> return getMaxCount(ChunkType.DATA_CHUNK);<a name="line.509"></a> -<span class="sourceLineNo">510</span> }<a name="line.510"></a> -<span class="sourceLineNo">511</span><a name="line.511"></a> -<span class="sourceLineNo">512</span> @VisibleForTesting<a name="line.512"></a> -<span class="sourceLineNo">513</span> int getMaxCount(ChunkType chunkType) {<a name="line.513"></a> -<span class="sourceLineNo">514</span> switch (chunkType) {<a name="line.514"></a> -<span class="sourceLineNo">515</span> case INDEX_CHUNK:<a name="line.515"></a> -<span class="sourceLineNo">516</span> if (indexChunksPool != null) {<a name="line.516"></a> -<span class="sourceLineNo">517</span> return indexChunksPool.getMaxCount();<a name="line.517"></a> -<span class="sourceLineNo">518</span> }<a name="line.518"></a> -<span class="sourceLineNo">519</span> break;<a name="line.519"></a> -<span class="sourceLineNo">520</span> case DATA_CHUNK:<a name="line.520"></a> -<span class="sourceLineNo">521</span> if (dataChunksPool != null) {<a name="line.521"></a> -<span class="sourceLineNo">522</span> return dataChunksPool.getMaxCount();<a name="line.522"></a> -<span class="sourceLineNo">523</span> }<a name="line.523"></a> -<span class="sourceLineNo">524</span> break;<a name="line.524"></a> -<span class="sourceLineNo">525</span> default:<a name="line.525"></a> -<span class="sourceLineNo">526</span> throw new IllegalArgumentException(<a name="line.526"></a> -<span class="sourceLineNo">527</span> "chunkType must either be INDEX_CHUNK or DATA_CHUNK");<a name="line.527"></a> -<span class="sourceLineNo">528</span> }<a name="line.528"></a> -<span class="sourceLineNo">529</span><a name="line.529"></a> -<span class="sourceLineNo">530</span> return 0;<a name="line.530"></a> -<span class="sourceLineNo">531</span> }<a name="line.531"></a> -<span class="sourceLineNo">532</span><a name="line.532"></a> -<span class="sourceLineNo">533</span> @VisibleForTesting<a name="line.533"></a> -<span class="sourceLineNo">534</span> int getPoolSize() {<a name="line.534"></a> -<span class="sourceLineNo">535</span> return getPoolSize(ChunkType.DATA_CHUNK);<a name="line.535"></a> -<span class="sourceLineNo">536</span> }<a name="line.536"></a> -<span class="sourceLineNo">537</span><a name="line.537"></a> -<span class="sourceLineNo">538</span> @VisibleForTesting<a name="line.538"></a> -<span class="sourceLineNo">539</span> int getPoolSize(ChunkType chunkType) {<a name="line.539"></a> -<span class="sourceLineNo">540</span> switch (chunkType) {<a name="line.540"></a> -<span class="sourceLineNo">541</span> case INDEX_CHUNK:<a name="line.541"></a> -<span class="sourceLineNo">542</span> if (indexChunksPool != null) {<a name="line.542"></a> -<span class="sourceLineNo">543</span> return indexChunksPool.reclaimedChunks.size();<a name="line.543"></a> -<span class="sourceLineNo">544</span> }<a name="line.544"></a> -<span class="sourceLineNo">545</span> break;<a name="line.545"></a> -<span class="sourceLineNo">546</span> case DATA_CHUNK:<a name="line.546"></a> -<span class="sourceLineNo">547</span> if (dataChunksPool != null) {<a name="line.547"></a> -<span class="sourceLineNo">548</span> return dataChunksPool.reclaimedChunks.size();<a name="line.548"></a> -<span class="sourceLineNo">549</span> }<a name="line.549"></a> -<span class="sourceLineNo">550</span> break;<a name="line.550"></a> -<span class="sourceLineNo">551</span> default:<a name="line.551"></a> -<span class="sourceLineNo">552</span> throw new IllegalArgumentException(<a name="line.552"></a> -<span class="sourceLineNo">553</span> "chunkType must either be INDEX_CHUNK or DATA_CHUNK");<a name="line.553"></a> -<span class="sourceLineNo">554</span> }<a name="line.554"></a> -<span class="sourceLineNo">555</span> return 0;<a name="line.555"></a> -<span class="sourceLineNo">556</span> }<a name="line.556"></a> -<span class="sourceLineNo">557</span><a name="line.557"></a> -<span class="sourceLineNo">558</span> @VisibleForTesting<a name="line.558"></a> -<span class="sourceLineNo">559</span> boolean isChunkInPool(int chunkId) {<a name="line.559"></a> -<span class="sourceLineNo">560</span> Chunk c = getChunk(chunkId);<a name="line.560"></a> -<span class="sourceLineNo">561</span> if (c==null) {<a name="line.561"></a> -<span class="sourceLineNo">562</span> return false;<a name="line.562"></a> -<span class="sourceLineNo">563</span> }<a name="line.563"></a> -<span class="sourceLineNo">564</span><a name="line.564"></a> -<span class="sourceLineNo">565</span> // chunks that are from pool will return true chunk reference not null<a name="line.565"></a> -<span class="sourceLineNo">566</span> if (dataChunksPool != null && dataChunksPool.reclaimedChunks.contains(c)) {<a name="line.566"></a> -<span class="sourceLineNo">567</span> return true;<a name="line.567"></a> -<span class="sourceLineNo">568</span> } else if (indexChunksPool != null && indexChunksPool.reclaimedChunks.contains(c)) {<a name="line.568"></a> +<span class="sourceLineNo">485</span> if (chunkPoolDisabled) {<a name="line.485"></a> +<span class="sourceLineNo">486</span> return null;<a name="line.486"></a> +<span class="sourceLineNo">487</span> }<a name="line.487"></a> +<span class="sourceLineNo">488</span> if (poolSizePercentage > 1.0) {<a name="line.488"></a> +<span class="sourceLineNo">489</span> throw new IllegalArgumentException(<a name="line.489"></a> +<span class="sourceLineNo">490</span> MemStoreLAB.CHUNK_POOL_MAXSIZE_KEY + " must be between 0.0 and 1.0");<a name="line.490"></a> +<span class="sourceLineNo">491</span> }<a name="line.491"></a> +<span class="sourceLineNo">492</span> int maxCount = (int) (globalMemStoreSize * poolSizePercentage / chunkSize);<a name="line.492"></a> +<span class="sourceLineNo">493</span> if (initialCountPercentage > 1.0 || initialCountPercentage < 0) {<a name="line.493"></a> +<span class="sourceLineNo">494</span> throw new IllegalArgumentException(label + " " + MemStoreLAB.CHUNK_POOL_INITIALSIZE_KEY +<a name="line.494"></a> +<span class="sourceLineNo">495</span> " must be between 0.0 and 1.0");<a name="line.495"></a> +<span class="sourceLineNo">496</span> }<a name="line.496"></a> +<span class="sourceLineNo">497</span> int initialCount = (int) (initialCountPercentage * maxCount);<a name="line.497"></a> +<span class="sourceLineNo">498</span> LOG.info("{} allocating {} MemStoreChunkPool with chunk size {}, max count {}, " +<a name="line.498"></a> +<span class="sourceLineNo">499</span> "initial count {}", label, StringUtils.byteDesc(chunkSize), maxCount, initialCount);<a name="line.499"></a> +<span class="sourceLineNo">500</span> MemStoreChunkPool memStoreChunkPool = new MemStoreChunkPool(label, chunkSize, maxCount,<a name="line.500"></a> +<span class="sourceLineNo">501</span> initialCount, poolSizePercentage);<a name="line.501"></a> +<span class="sourceLineNo">502</span> if (heapMemoryManager != null && memStoreChunkPool != null) {<a name="line.502"></a> +<span class="sourceLineNo">503</span> // Register with Heap Memory manager<a name="line.503"></a> +<span class="sourceLineNo">504</span> heapMemoryManager.registerTuneObserver(memStoreChunkPool);<a name="line.504"></a> +<span class="sourceLineNo">505</span> }<a name="line.505"></a> +<span class="sourceLineNo">506</span> return memStoreChunkPool;<a name="line.506"></a> +<span class="sourceLineNo">507</span> }<a name="line.507"></a> +<span class="sourceLineNo">508</span><a name="line.508"></a> +<span class="sourceLineNo">509</span> @VisibleForTesting<a name="line.509"></a> +<span class="sourceLineNo">510</span> int getMaxCount() {<a name="line.510"></a> +<span class="sourceLineNo">511</span> return getMaxCount(ChunkType.DATA_CHUNK);<a name="line.511"></a> +<span class="sourceLineNo">512</span> }<a name="line.512"></a> +<span class="sourceLineNo">513</span><a name="line.513"></a> +<span class="sourceLineNo">514</span> @VisibleForTesting<a name="line.514"></a> +<span class="sourceLineNo">515</span> int getMaxCount(ChunkType chunkType) {<a name="line.515"></a> +<span class="sourceLineNo">516</span> switch (chunkType) {<a name="line.516"></a> +<span class="sourceLineNo">517</span> case INDEX_CHUNK:<a name="line.517"></a> +<span class="sourceLineNo">518</span> if (indexChunksPool != null) {<a name="line.518"></a> +<span class="sourceLineNo">519</span> return indexChunksPool.getMaxCount();<a name="line.519"></a> +<span class="sourceLineNo">520</span> }<a name="line.520"></a> +<span class="sourceLineNo">521</span> break;<a name="line.521"></a> +<span class="sourceLineNo">522</span> case DATA_CHUNK:<a name="line.522"></a> +<span class="sourceLineNo">523</span> if (dataChunksPool != null) {<a name="line.523"></a> +<span class="sourceLineNo">524</span> return dataChunksPool.getMaxCount();<a name="line.524"></a> +<span class="sourceLineNo">525</span> }<a name="line.525"></a> +<span class="sourceLineNo">526</span> break;<a name="line.526"></a> +<span class="sourceLineNo">527</span> default:<a name="line.527"></a> +<span class="sourceLineNo">528</span> throw new IllegalArgumentException(<a name="line.528"></a> +<span class="sourceLineNo">529</span> "chunkType must either be INDEX_CHUNK or DATA_CHUNK");<a name="line.529"></a> +<span class="sourceLineNo">530</span> }<a name="line.530"></a> +<span class="sourceLineNo">531</span><a name="line.531"></a> +<span class="sourceLineNo">532</span> return 0;<a name="line.532"></a> +<span class="sourceLineNo">533</span> }<a name="line.533"></a> +<span class="sourceLineNo">534</span><a name="line.534"></a> +<span class="sourceLineNo">535</span> @VisibleForTesting<a name="line.535"></a> +<span class="sourceLineNo">536</span> int getPoolSize() {<a name="line.536"></a> +<span class="sourceLineNo">537</span> return getPoolSize(ChunkType.DATA_CHUNK);<a name="line.537"></a> +<span class="sourceLineNo">538</span> }<a name="line.538"></a> +<span class="sourceLineNo">539</span><a name="line.539"></a> +<span class="sourceLineNo">540</span> @VisibleForTesting<a name="line.540"></a> +<span class="sourceLineNo">541</span> int getPoolSize(ChunkType chunkType) {<a name="line.541"></a> +<span class="sourceLineNo">542</span> switch (chunkType) {<a name="line.542"></a> +<span class="sourceLineNo">543</span> case INDEX_CHUNK:<a name="line.543"></a> +<span class="sourceLineNo">544</span> if (indexChunksPool != null) {<a name="line.544"></a> +<span class="sourceLineNo">545</span> return indexChunksPool.reclaimedChunks.size();<a name="line.545"></a> +<span class="sourceLineNo">546</span> }<a name="line.546"></a> +<span class="sourceLineNo">547</span> break;<a name="line.547"></a> +<span class="sourceLineNo">548</span> case DATA_CHUNK:<a name="line.548"></a> +<span class="sourceLineNo">549</span> if (dataChunksPool != null) {<a name="line.549"></a> +<span class="sourceLineNo">550</span> return dataChunksPool.reclaimedChunks.size();<a name="line.550"></a> +<span class="sourceLineNo">551</span> }<a name="line.551"></a> +<span class="sourceLineNo">552</span> break;<a name="line.552"></a> +<span class="sourceLineNo">553</span> default:<a name="line.553"></a> +<span class="sourceLineNo">554</span> throw new IllegalArgumentException(<a name="line.554"></a> +<span class="sourceLineNo">555</span> "chunkType must either be INDEX_CHUNK or DATA_CHUNK");<a name="line.555"></a> +<span class="sourceLineNo">556</span> }<a name="line.556"></a> +<span class="sourceLineNo">557</span> return 0;<a name="line.557"></a> +<span class="sourceLineNo">558</span> }<a name="line.558"></a> +<span class="sourceLineNo">559</span><a name="line.559"></a> +<span class="sourceLineNo">560</span> @VisibleForTesting<a name="line.560"></a> +<span class="sourceLineNo">561</span> boolean isChunkInPool(int chunkId) {<a name="line.561"></a> +<span class="sourceLineNo">562</span> Chunk c = getChunk(chunkId);<a name="line.562"></a> +<span class="sourceLineNo">563</span> if (c==null) {<a name="line.563"></a> +<span class="sourceLineNo">564</span> return false;<a name="line.564"></a> +<span class="sourceLineNo">565</span> }<a name="line.565"></a> +<span class="sourceLineNo">566</span><a name="line.566"></a> +<span class="sourceLineNo">567</span> // chunks that are from pool will return true chunk reference not null<a name="line.567"></a> +<span class="sourceLineNo">568</span> if (dataChunksPool != null && dataChunksPool.reclaimedChunks.contains(c)) {<a name="line.568"></a> <span class="sourceLineNo">569</span> return true;<a name="line.569"></a> -<span class="sourceLineNo">570</span> }<a name="line.570"></a> -<span class="sourceLineNo">571</span> return false;<a name="line.571"></a> -<span class="sourceLineNo">572</span> }<a name="line.572"></a> -<span class="sourceLineNo">573</span><a name="line.573"></a> -<span class="sourceLineNo">574</span> /*<a name="line.574"></a> -<span class="sourceLineNo">575</span> * Only used in testing<a name="line.575"></a> -<span class="sourceLineNo">576</span> */<a name="line.576"></a> -<span class="sourceLineNo">577</span> @VisibleForTesting<a name="line.577"></a> -<span class="sourceLineNo">578</span> void clearChunksInPool() {<a name="line.578"></a> -<span class="sourceLineNo">579</span> if (dataChunksPool != null) {<a name="line.579"></a> -<span class="sourceLineNo">580</span> dataChunksPool.reclaimedChunks.clear();<a name="line.580"></a> -<span class="sourceLineNo">581</span> }<a name="line.581"></a> -<span class="sourceLineNo">582</span> if (indexChunksPool != null) {<a name="line.582"></a> -<span class="sourceLineNo">583</span> indexChunksPool.reclaimedChunks.clear();<a name="line.583"></a> -<span class="sourceLineNo">584</span> }<a name="line.584"></a> -<span class="sourceLineNo">585</span> }<a name="line.585"></a> -<span class="sourceLineNo">586</span><a name="line.586"></a> -<span class="sourceLineNo">587</span> int getChunkSize() {<a name="line.587"></a> -<span class="sourceLineNo">588</span> return getChunkSize(ChunkType.DATA_CHUNK);<a name="line.588"></a> -<span class="sourceLineNo">589</span> }<a name="line.589"></a> -<span class="sourceLineNo">590</span><a name="line.590"></a> -<span class="sourceLineNo">591</span> int getChunkSize(ChunkType chunkType) {<a name="line.591"></a> -<span class="sourceLineNo">592</span> switch (chunkType) {<a name="line.592"></a> -<span class="sourceLineNo">593</span> case INDEX_CHUNK:<a name="line.593"></a> -<span class="sourceLineNo">594</span> if (indexChunksPool != null) {<a name="line.594"></a> -<span class="sourceLineNo">595</span> return indexChunksPool.getChunkSize();<a name="line.595"></a> -<span class="sourceLineNo">596</span> }<a name="line.596"></a> -<span class="sourceLineNo">597</span> case DATA_CHUNK:<a name="line.597"></a> -<span class="sourceLineNo">598</span> if (dataChunksPool != null) {<a name="line.598"></a> -<span class="sourceLineNo">599</span> return dataChunksPool.getChunkSize();<a name="line.599"></a> -<span class="sourceLineNo">600</span> } else { // When pools are empty<a name="line.600"></a> -<span class="sourceLineNo">601</span> return chunkSize;<a name="line.601"></a> -<span class="sourceLineNo">602</span> }<a name="line.602"></a> -<span class="sourceLineNo">603</span> default:<a name="line.603"></a> -<span class="sourceLineNo">604</span> throw new IllegalArgumentException(<a name="line.604"></a> -<span class="sourceLineNo">605</span> "chunkType must either be INDEX_CHUNK or DATA_CHUNK");<a name="line.605"></a> -<span class="sourceLineNo">606</span> }<a name="line.606"></a> -<span class="sourceLineNo">607</span> }<a name="line.607"></a> -<span class="sourceLineNo">608</span><a name="line.608"></a> -<span class="sourceLineNo">609</span> synchronized void putbackChunks(Set<Integer> chunks) {<a name="line.609"></a> -<span class="sourceLineNo">610</span> // if there is no pool just try to clear the chunkIdMap in case there is something<a name="line.610"></a> -<span class="sourceLineNo">611</span> if (dataChunksPool == null && indexChunksPool == null) {<a name="line.611"></a> -<span class="sourceLineNo">612</span> this.removeChunks(chunks);<a name="line.612"></a> -<span class="sourceLineNo">613</span> return;<a name="line.613"></a> -<span class="sourceLineNo">614</span> }<a name="line.614"></a> -<span class="sourceLineNo">615</span><a name="line.615"></a> -<span class="sourceLineNo">616</span> // if there is a pool, go over all chunk IDs that came back, the chunks may be from pool or not<a name="line.616"></a> -<span class="sourceLineNo">617</span> for (int chunkID : chunks) {<a name="line.617"></a> -<span class="sourceLineNo">618</span> // translate chunk ID to chunk, if chunk initially wasn't in pool<a name="line.618"></a> -<span class="sourceLineNo">619</span> // this translation will (most likely) return null<a name="line.619"></a> -<span class="sourceLineNo">620</span> Chunk chunk = ChunkCreator.this.getChunk(chunkID);<a name="line.620"></a> -<span class="sourceLineNo">621</span> if (chunk != null) {<a name="line.621"></a> -<span class="sourceLineNo">622</span> if (chunk.isFromPool() && chunk.isIndexChunk()) {<a name="line.622"></a> -<span class="sourceLineNo">623</span> indexChunksPool.putbackChunks(chunk);<a name="line.623"></a> -<span class="sourceLineNo">624</span> } else if (chunk.isFromPool() && chunk.size == dataChunksPool.getChunkSize()) {<a name="line.624"></a> -<span class="sourceLineNo">625</span> dataChunksPool.putbackChunks(chunk);<a name="line.625"></a> -<span class="sourceLineNo">626</span> } else {<a name="line.626"></a> -<span class="sourceLineNo">627</span> // chunks which are not from one of the pools<a name="line.627"></a> -<span class="sourceLineNo">628</span> // should be released without going to the pools.<a name="line.628"></a> -<span class="sourceLineNo">629</span> // Removing them from chunkIdMap will cause their removal by the GC.<a name="line.629"></a> -<span class="sourceLineNo">630</span> this.removeChunk(chunkID);<a name="line.630"></a> -<span class="sourceLineNo">631</span> }<a name="line.631"></a> -<span class="sourceLineNo">632</span> }<a name="line.632"></a> -<span class="sourceLineNo">633</span> // if chunk is null, it was never covered by the chunkIdMap (and so wasn't in pool also),<a name="line.633"></a> -<span class="sourceLineNo">634</span> // so we have nothing to do on its release<a name="line.634"></a> -<span class="sourceLineNo">635</span> }<a name="line.635"></a> -<span class="sourceLineNo">636</span> return;<a name="line.636"></a> -<span class="sourceLineNo">637</span> }<a name="line.637"></a> -<span class="sourceLineNo">638</span><a name="line.638"></a> -<span class="sourceLineNo">639</span>}<a name="line.639"></a> +<span class="sourceLineNo">570</span> } else if (indexChunksPool != null && indexChunksPool.reclaimedChunks.contains(c)) {<a name="line.570"></a> +<span class="sourceLineNo">571</span> return true;<a name="line.571"></a> +<span class="sourceLineNo">572</span> }<a name="line.572"></a> +<span class="sourceLineNo">573</span> return false;<a name="line.573"></a> +<span class="sourceLineNo">574</span> }<a name="line.574"></a> +<span class="sourceLineNo">575</span><a name="line.575"></a> +<span class="sourceLineNo">576</span> /*<a name="line.576"></a> +<span class="sourceLineNo">577</span> * Only used in testing<a name="line.577"></a> +<span class="sourceLineNo">578</span> */<a name="line.578"></a> +<span class="sourceLineNo">579</span> @VisibleForTesting<a name="line.579"></a> +<span class="sourceLineNo">580</span> void clearChunksInPool() {<a name="line.580"></a> +<span class="sourceLineNo">581</span> if (dataChunksPool != null) {<a name="line.581"></a> +<span class="sourceLineNo">582</span> dataChunksPool.reclaimedChunks.clear();<a name="line.582"></a> +<span class="sourceLineNo">583</span> }<a name="line.583"></a> +<span class="sourceLineNo">584</span> if (indexChunksPool != null) {<a name="line.584"></a> +<span class="sourceLineNo">585</span> indexChunksPool.reclaimedChunks.clear();<a name="line.585"></a> +<span class="sourceLineNo">586</span> }<a name="line.586"></a> +<span class="sourceLineNo">587</span> }<a name="line.587"></a> +<span class="sourceLineNo">588</span><a name="line.588"></a> +<span class="sourceLineNo">589</span> int getChunkSize() {<a name="line.589"></a> +<span class="sourceLineNo">590</span> return getChunkSize(ChunkType.DATA_CHUNK);<a name="line.590"></a> +<span class="sourceLineNo">591</span> }<a name="line.591"></a> +<span class="sourceLineNo">592</span><a name="line.592"></a> +<span class="sourceLineNo">593</span> int getChunkSize(ChunkType chunkType) {<a name="line.593"></a> +<span class="sourceLineNo">594</span> switch (chunkType) {<a name="line.594"></a> +<span class="sourceLineNo">595</span> case INDEX_CHUNK:<a name="line.595"></a> +<span class="sourceLineNo">596</span> if (indexChunksPool != null) {<a name="line.596"></a> +<span class="sourceLineNo">597</span> return indexChunksPool.getChunkSize();<a name="line.597"></a> +<span class="sourceLineNo">598</span> }<a name="line.598"></a> +<span class="sourceLineNo">599</span> case DATA_CHUNK:<a name="line.599"></a> +<span class="sourceLineNo">600</span> if (dataChunksPool != null) {<a name="line.600"></a> +<span class="sourceLineNo">601</span> return dataChunksPool.getChunkSize();<a name="line.601"></a> +<span class="sourceLineNo">602</span> } else { // When pools are empty<a name="line.602"></a> +<span class="sourceLineNo">603</span> return chunkSize;<a name="line.603"></a> +<span class="sourceLineNo">604</span> }<a name="line.604"></a> +<span class="sourceLineNo">605</span> default:<a name="line.605"></a> +<span class="sourceLineNo">606</span> throw new IllegalArgumentException(<a name="line.606"></a> +<span class="sourceLineNo">607</span> "chunkType must either be INDEX_CHUNK or DATA_CHUNK");<a name="line.607"></a> +<span class="sourceLineNo">608</span> }<a name="line.608"></a> +<span class="sourceLineNo">609</span> }<a name="line.609"></a> +<span class="sourceLineNo">610</span><a name="line.610"></a> +<span class="sourceLineNo">611</span> synchronized void putbackChunks(Set<Integer> chunks) {<a name="line.611"></a> +<span class="sourceLineNo">612</span> // if there is no pool just try to clear the chunkIdMap in case there is something<a name="line.612"></a> +<span class="sourceLineNo">613</span> if (dataChunksPool == null && indexChunksPool == null) {<a name="line.613"></a> +<span class="sourceLineNo">614</span> this.removeChunks(chunks);<a name="line.614"></a> +<span class="sourceLineNo">615</span> return;<a name="line.615"></a> +<span class="sourceLineNo">616</span> }<a name="line.616"></a> +<span class="sourceLineNo">617</span><a name="line.617"></a> +<span class="sourceLineNo">618</span> // if there is a pool, go over all chunk IDs that came back, the chunks may be from pool or not<a name="line.618"></a> +<span class="sourceLineNo">619</span> for (int chunkID : chunks) {<a name="line.619"></a> +<span class="sourceLineNo">620</span> // translate chunk ID to chunk, if chunk initially wasn't in pool<a name="line.620"></a> +<span class="sourceLineNo">621</span> // this translation will (most likely) return null<a name="line.621"></a> +<span class="sourceLineNo">622</span> Chunk chunk = ChunkCreator.this.getChunk(chunkID);<a name="line.622"></a> +<span class="sourceLineNo">623</span> if (chunk != null) {<a name="line.623"></a> +<span class="sourceLineNo">624</span> if (chunk.isFromPool() && chunk.isIndexChunk()) {<a name="line.624"></a> +<span class="sourceLineNo">625</span> indexChunksPool.putbackChunks(chunk);<a name="line.625"></a> +<span class="sourceLineNo">626</span> } else if (chunk.isFromPool() && chunk.size == dataChunksPool.getChunkSize()) {<a name="line.626"></a> +<span class="sourceLineNo">627</span> dataChunksPool.putbackChunks(chunk);<a name="line.627"></a> +<span class="sourceLineNo">628</span> } else {<a name="line.628"></a> +<span class="sourceLineNo">629</span> // chunks which are not from one of the pools<a name="line.629"></a> +<span class="sourceLineNo">630</span> // should be released without going to the pools.<a name="line.630"></a> +<span class="sourceLineNo">631</span> // Removing them from chunkIdMap will cause their removal by the GC.<a name="line.631"></a> +<span class="sourceLineNo">632</span> this.removeChunk(chunkID);<a name="line.632"></a> +<span class="sourceLineNo">633</span> }<a name="line.633"></a> +<span class="sourceLineNo">634</span> }<a name="line.634"></a> +<span class="sourceLineNo">635</span> // if chunk is null, it was never covered by the chunkIdMap (and so wasn't in pool also),<a name="line.635"></a> +<span class="sourceLineNo">636</span> // so we have nothing to do on its release<a name="line.636"></a> +<span class="sourceLineNo">637</span> }<a name="line.637"></a> +<span class="sourceLineNo">638</span> return;<a name="line.638"></a> +<span class="sourceLineNo">639</span> }<a name="line.639"></a> <span class="sourceLineNo">640</span><a name="line.640"></a> +<span class="sourceLineNo">641</span>}<a name="line.641"></a> +<span class="sourceLineNo">642</span><a name="line.642"></a>