@@ -77,6 +77,7 @@ public enum ChunkType {
77
77
static boolean chunkPoolDisabled = false ;
78
78
private MemStoreChunkPool dataChunksPool ;
79
79
private final int chunkSize ;
80
+ private int indexChunkSize ;
80
81
private MemStoreChunkPool indexChunksPool ;
81
82
82
83
ChunkCreator (int chunkSize , boolean offheap , long globalMemStoreSize , float poolSizePercentage ,
@@ -94,13 +95,14 @@ private void initializePools(int chunkSize, long globalMemStoreSize,
94
95
HeapMemoryManager heapMemoryManager ) {
95
96
this .dataChunksPool = initializePool ("data" , globalMemStoreSize ,
96
97
(1 - indexChunkSizePercentage ) * poolSizePercentage ,
97
- initialCountPercentage , chunkSize , heapMemoryManager );
98
+ initialCountPercentage , chunkSize , ChunkType . DATA_CHUNK , heapMemoryManager );
98
99
// The index chunks pool is needed only when the index type is CCM.
99
100
// Since the pools are not created at all when the index type isn't CCM,
100
101
// we don't need to check it here.
102
+ this .indexChunkSize = (int ) (indexChunkSizePercentage * chunkSize );
101
103
this .indexChunksPool = initializePool ("index" , globalMemStoreSize ,
102
104
indexChunkSizePercentage * poolSizePercentage ,
103
- initialCountPercentage , ( int ) ( indexChunkSizePercentage * chunkSize ) ,
105
+ initialCountPercentage , this . indexChunkSize , ChunkType . INDEX_CHUNK ,
104
106
heapMemoryManager );
105
107
}
106
108
@@ -163,14 +165,20 @@ Chunk getChunk(CompactingMemStore.IndexType chunkIndexType) {
163
165
Chunk getChunk (CompactingMemStore .IndexType chunkIndexType , ChunkType chunkType ) {
164
166
switch (chunkType ) {
165
167
case INDEX_CHUNK :
166
- if (indexChunksPool != null ) {
167
- return getChunk (chunkIndexType , indexChunksPool .getChunkSize ());
168
+ if (indexChunksPool == null ) {
169
+ if (indexChunkSize <= 0 ) {
170
+ throw new IllegalArgumentException (
171
+ "chunkType is INDEX_CHUNK but indexChunkSize is:[" + this .indexChunkSize + "]" );
172
+ }
173
+ return getChunk (chunkIndexType , chunkType , indexChunkSize );
174
+ } else {
175
+ return getChunk (chunkIndexType , chunkType , indexChunksPool .getChunkSize ());
168
176
}
169
177
case DATA_CHUNK :
170
178
if (dataChunksPool == null ) {
171
- return getChunk (chunkIndexType , chunkSize );
179
+ return getChunk (chunkIndexType , chunkType , chunkSize );
172
180
} else {
173
- return getChunk (chunkIndexType , dataChunksPool .getChunkSize ());
181
+ return getChunk (chunkIndexType , chunkType , dataChunksPool .getChunkSize ());
174
182
}
175
183
default :
176
184
throw new IllegalArgumentException (
@@ -184,14 +192,14 @@ Chunk getChunk(CompactingMemStore.IndexType chunkIndexType, ChunkType chunkType)
184
192
* @param chunkIndexType whether the requested chunk is going to be used with CellChunkMap index
185
193
* @param size the size of the chunk to be allocated, in bytes
186
194
*/
187
- Chunk getChunk (CompactingMemStore .IndexType chunkIndexType , int size ) {
195
+ Chunk getChunk (CompactingMemStore .IndexType chunkIndexType , ChunkType chunkType , int size ) {
188
196
Chunk chunk = null ;
189
197
MemStoreChunkPool pool = null ;
190
198
191
- // if the size is suitable for one of the pools
192
- if (dataChunksPool != null && size == dataChunksPool . getChunkSize () ) {
199
+ // if it is one of the pools
200
+ if (dataChunksPool != null && chunkType == ChunkType . DATA_CHUNK ) {
193
201
pool = dataChunksPool ;
194
- } else if (indexChunksPool != null && size == indexChunksPool . getChunkSize () ) {
202
+ } else if (indexChunksPool != null && chunkType == ChunkType . INDEX_CHUNK ) {
195
203
pool = indexChunksPool ;
196
204
}
197
205
@@ -211,7 +219,7 @@ Chunk getChunk(CompactingMemStore.IndexType chunkIndexType, int size) {
211
219
if (chunk == null ) {
212
220
// the second parameter explains whether CellChunkMap index is requested,
213
221
// in that case, put allocated on demand chunk mapping into chunkIdMap
214
- chunk = createChunk (false , chunkIndexType , size );
222
+ chunk = createChunk (false , chunkIndexType , chunkType , size );
215
223
}
216
224
217
225
// now we need to actually do the expensive memory allocation step in case of a new chunk,
@@ -228,14 +236,15 @@ Chunk getChunk(CompactingMemStore.IndexType chunkIndexType, int size) {
228
236
*/
229
237
Chunk getJumboChunk (int jumboSize ) {
230
238
int allocSize = jumboSize + SIZEOF_CHUNK_HEADER ;
231
- if (allocSize <= dataChunksPool .getChunkSize ()) {
239
+
240
+ if (allocSize <= this .getChunkSize (ChunkType .DATA_CHUNK )) {
232
241
LOG .warn ("Jumbo chunk size " + jumboSize + " must be more than regular chunk size "
233
- + dataChunksPool .getChunkSize () + ". Converting to regular chunk." );
242
+ + this .getChunkSize (ChunkType . DATA_CHUNK ) + ". Converting to regular chunk." );
234
243
return getChunk (CompactingMemStore .IndexType .CHUNK_MAP );
235
244
}
236
245
// the new chunk is going to hold the jumbo cell data and needs to be referenced by
237
246
// a strong map. Therefore the CCM index type
238
- return getChunk (CompactingMemStore .IndexType .CHUNK_MAP , allocSize );
247
+ return getChunk (CompactingMemStore .IndexType .CHUNK_MAP , ChunkType . JUMBO_CHUNK , allocSize );
239
248
}
240
249
241
250
/**
@@ -245,15 +254,16 @@ Chunk getJumboChunk(int jumboSize) {
245
254
* @param size the size of the chunk to be allocated, in bytes
246
255
* @return the chunk
247
256
*/
248
- private Chunk createChunk (boolean pool , CompactingMemStore .IndexType chunkIndexType , int size ) {
257
+ private Chunk createChunk (boolean pool , CompactingMemStore .IndexType chunkIndexType ,
258
+ ChunkType chunkType , int size ) {
249
259
Chunk chunk = null ;
250
260
int id = chunkID .getAndIncrement ();
251
261
assert id > 0 ;
252
262
// do not create offheap chunk on demand
253
263
if (pool && this .offheap ) {
254
- chunk = new OffheapChunk (size , id , pool );
264
+ chunk = new OffheapChunk (size , id , chunkType , pool );
255
265
} else {
256
- chunk = new OnheapChunk (size , id , pool );
266
+ chunk = new OnheapChunk (size , id , chunkType , pool );
257
267
}
258
268
if (pool || (chunkIndexType == CompactingMemStore .IndexType .CHUNK_MAP )) {
259
269
// put the pool chunk into the chunkIdMap so it is not GC-ed
@@ -264,12 +274,13 @@ private Chunk createChunk(boolean pool, CompactingMemStore.IndexType chunkIndexT
264
274
265
275
// Chunks from pool are created covered with strong references anyway
266
276
// TODO: change to CHUNK_MAP if it is generally defined
267
- private Chunk createChunkForPool (CompactingMemStore .IndexType chunkIndexType , int chunkSize ) {
277
+ private Chunk createChunkForPool (CompactingMemStore .IndexType chunkIndexType , ChunkType chunkType ,
278
+ int chunkSize ) {
268
279
if (chunkSize != dataChunksPool .getChunkSize () &&
269
280
chunkSize != indexChunksPool .getChunkSize ()) {
270
281
return null ;
271
282
}
272
- return createChunk (true , chunkIndexType , chunkSize );
283
+ return createChunk (true , chunkIndexType , chunkType , chunkSize );
273
284
}
274
285
275
286
// Used to translate the ChunkID into a chunk ref
@@ -309,6 +320,7 @@ void clearChunkIds() {
309
320
*/
310
321
private class MemStoreChunkPool implements HeapMemoryTuneObserver {
311
322
private final int chunkSize ;
323
+ private final ChunkType chunkType ;
312
324
private int maxCount ;
313
325
314
326
// A queue of reclaimed chunks
@@ -323,15 +335,18 @@ private class MemStoreChunkPool implements HeapMemoryTuneObserver {
323
335
private final LongAdder reusedChunkCount = new LongAdder ();
324
336
private final String label ;
325
337
326
- MemStoreChunkPool (String label , int chunkSize , int maxCount , int initialCount ,
338
+ MemStoreChunkPool (String label , int chunkSize , ChunkType chunkType , int maxCount ,
339
+ int initialCount ,
327
340
float poolSizePercentage ) {
328
341
this .label = label ;
329
342
this .chunkSize = chunkSize ;
343
+ this .chunkType = chunkType ;
330
344
this .maxCount = maxCount ;
331
345
this .poolSizePercentage = poolSizePercentage ;
332
346
this .reclaimedChunks = new LinkedBlockingQueue <>();
333
347
for (int i = 0 ; i < initialCount ; i ++) {
334
- Chunk chunk = createChunk (true , CompactingMemStore .IndexType .ARRAY_MAP , chunkSize );
348
+ Chunk chunk =
349
+ createChunk (true , CompactingMemStore .IndexType .ARRAY_MAP , chunkType , chunkSize );
335
350
chunk .init ();
336
351
reclaimedChunks .add (chunk );
337
352
}
@@ -367,7 +382,7 @@ Chunk getChunk(CompactingMemStore.IndexType chunkIndexType) {
367
382
long created = this .chunkCount .get ();
368
383
if (created < this .maxCount ) {
369
384
if (this .chunkCount .compareAndSet (created , created + 1 )) {
370
- chunk = createChunkForPool (chunkIndexType , chunkSize );
385
+ chunk = createChunkForPool (chunkIndexType , chunkType , chunkSize );
371
386
break ;
372
387
}
373
388
} else {
@@ -465,7 +480,7 @@ static void clearDisableFlag() {
465
480
}
466
481
467
482
private MemStoreChunkPool initializePool (String label , long globalMemStoreSize ,
468
- float poolSizePercentage , float initialCountPercentage , int chunkSize ,
483
+ float poolSizePercentage , float initialCountPercentage , int chunkSize , ChunkType chunkType ,
469
484
HeapMemoryManager heapMemoryManager ) {
470
485
if (poolSizePercentage <= 0 ) {
471
486
LOG .info ("{} poolSizePercentage is less than 0. So not using pool" , label );
@@ -486,8 +501,8 @@ private MemStoreChunkPool initializePool(String label, long globalMemStoreSize,
486
501
int initialCount = (int ) (initialCountPercentage * maxCount );
487
502
LOG .info ("Allocating {} MemStoreChunkPool with chunk size {}, max count {}, initial count {}" ,
488
503
label , StringUtils .byteDesc (chunkSize ), maxCount , initialCount );
489
- MemStoreChunkPool memStoreChunkPool = new MemStoreChunkPool (label , chunkSize , maxCount ,
490
- initialCount , poolSizePercentage );
504
+ MemStoreChunkPool memStoreChunkPool = new MemStoreChunkPool (label , chunkSize , chunkType ,
505
+ maxCount , initialCount , poolSizePercentage );
491
506
if (heapMemoryManager != null && memStoreChunkPool != null ) {
492
507
// Register with Heap Memory manager
493
508
heapMemoryManager .registerTuneObserver (memStoreChunkPool );
@@ -578,6 +593,8 @@ int getChunkSize(ChunkType chunkType) {
578
593
case INDEX_CHUNK :
579
594
if (indexChunksPool != null ) {
580
595
return indexChunksPool .getChunkSize ();
596
+ } else {
597
+ return indexChunkSize ;
581
598
}
582
599
case DATA_CHUNK :
583
600
if (dataChunksPool != null ) {
@@ -606,7 +623,7 @@ synchronized void putbackChunks(Set<Integer> chunks) {
606
623
if (chunk != null ) {
607
624
if (chunk .isFromPool () && chunk .isIndexChunk ()) {
608
625
indexChunksPool .putbackChunks (chunk );
609
- } else if (chunk .isFromPool () && chunk .size == dataChunksPool . getChunkSize ()) {
626
+ } else if (chunk .isFromPool () && chunk .isDataChunk ()) {
610
627
dataChunksPool .putbackChunks (chunk );
611
628
} else {
612
629
// chunks which are not from one of the pools
@@ -621,5 +638,13 @@ synchronized void putbackChunks(Set<Integer> chunks) {
621
638
return ;
622
639
}
623
640
641
+ MemStoreChunkPool getIndexChunksPool () {
642
+ return this .indexChunksPool ;
643
+ }
644
+
645
+ MemStoreChunkPool getDataChunksPool () {
646
+ return this .dataChunksPool ;
647
+ }
648
+
624
649
}
625
650
0 commit comments