24
24
import org .elasticsearch .ElasticsearchIllegalArgumentException ;
25
25
import org .elasticsearch .common .component .AbstractComponent ;
26
26
import org .elasticsearch .common .inject .Inject ;
27
- import org .elasticsearch .common .recycler .* ;
27
+ import org .elasticsearch .common .recycler .Recycler ;
28
28
import org .elasticsearch .common .settings .Settings ;
29
+ import org .elasticsearch .common .util .concurrent .EsExecutors ;
29
30
30
31
import java .util .Locale ;
31
32
33
+ import static org .elasticsearch .common .recycler .Recyclers .*;
34
+
32
35
@ SuppressWarnings ("unchecked" )
33
36
public class CacheRecycler extends AbstractComponent {
34
37
@@ -66,8 +69,9 @@ public CacheRecycler(Settings settings) {
66
69
final Type type = Type .parse (settings .get ("type" ));
67
70
int limit = settings .getAsInt ("limit" , 10 );
68
71
int smartSize = settings .getAsInt ("smart_size" , 1024 );
72
+ final int availableProcessors = EsExecutors .boundedNumberOfProcessors (settings );
69
73
70
- hashMap = build (type , limit , smartSize , new Recycler .C <ObjectObjectOpenHashMap >() {
74
+ hashMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <ObjectObjectOpenHashMap >() {
71
75
@ Override
72
76
public ObjectObjectOpenHashMap newInstance (int sizing ) {
73
77
return new ObjectObjectOpenHashMap (size (sizing ));
@@ -78,7 +82,7 @@ public void clear(ObjectObjectOpenHashMap value) {
78
82
value .clear ();
79
83
}
80
84
});
81
- hashSet = build (type , limit , smartSize , new Recycler .C <ObjectOpenHashSet >() {
85
+ hashSet = build (type , limit , smartSize , availableProcessors , new Recycler .C <ObjectOpenHashSet >() {
82
86
@ Override
83
87
public ObjectOpenHashSet newInstance (int sizing ) {
84
88
return new ObjectOpenHashSet (size (sizing ), 0.5f );
@@ -89,7 +93,7 @@ public void clear(ObjectOpenHashSet value) {
89
93
value .clear ();
90
94
}
91
95
});
92
- doubleObjectMap = build (type , limit , smartSize , new Recycler .C <DoubleObjectOpenHashMap >() {
96
+ doubleObjectMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <DoubleObjectOpenHashMap >() {
93
97
@ Override
94
98
public DoubleObjectOpenHashMap newInstance (int sizing ) {
95
99
return new DoubleObjectOpenHashMap (size (sizing ));
@@ -100,7 +104,7 @@ public void clear(DoubleObjectOpenHashMap value) {
100
104
value .clear ();
101
105
}
102
106
});
103
- longObjectMap = build (type , limit , smartSize , new Recycler .C <LongObjectOpenHashMap >() {
107
+ longObjectMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <LongObjectOpenHashMap >() {
104
108
@ Override
105
109
public LongObjectOpenHashMap newInstance (int sizing ) {
106
110
return new LongObjectOpenHashMap (size (sizing ));
@@ -111,7 +115,7 @@ public void clear(LongObjectOpenHashMap value) {
111
115
value .clear ();
112
116
}
113
117
});
114
- longLongMap = build (type , limit , smartSize , new Recycler .C <LongLongOpenHashMap >() {
118
+ longLongMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <LongLongOpenHashMap >() {
115
119
@ Override
116
120
public LongLongOpenHashMap newInstance (int sizing ) {
117
121
return new LongLongOpenHashMap (size (sizing ));
@@ -122,7 +126,7 @@ public void clear(LongLongOpenHashMap value) {
122
126
value .clear ();
123
127
}
124
128
});
125
- intIntMap = build (type , limit , smartSize , new Recycler .C <IntIntOpenHashMap >() {
129
+ intIntMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <IntIntOpenHashMap >() {
126
130
@ Override
127
131
public IntIntOpenHashMap newInstance (int sizing ) {
128
132
return new IntIntOpenHashMap (size (sizing ));
@@ -133,7 +137,7 @@ public void clear(IntIntOpenHashMap value) {
133
137
value .clear ();
134
138
}
135
139
});
136
- floatIntMap = build (type , limit , smartSize , new Recycler .C <FloatIntOpenHashMap >() {
140
+ floatIntMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <FloatIntOpenHashMap >() {
137
141
@ Override
138
142
public FloatIntOpenHashMap newInstance (int sizing ) {
139
143
return new FloatIntOpenHashMap (size (sizing ));
@@ -144,7 +148,7 @@ public void clear(FloatIntOpenHashMap value) {
144
148
value .clear ();
145
149
}
146
150
});
147
- doubleIntMap = build (type , limit , smartSize , new Recycler .C <DoubleIntOpenHashMap >() {
151
+ doubleIntMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <DoubleIntOpenHashMap >() {
148
152
@ Override
149
153
public DoubleIntOpenHashMap newInstance (int sizing ) {
150
154
return new DoubleIntOpenHashMap (size (sizing ));
@@ -155,7 +159,7 @@ public void clear(DoubleIntOpenHashMap value) {
155
159
value .clear ();
156
160
}
157
161
});
158
- longIntMap = build (type , limit , smartSize , new Recycler .C <LongIntOpenHashMap >() {
162
+ longIntMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <LongIntOpenHashMap >() {
159
163
@ Override
160
164
public LongIntOpenHashMap newInstance (int sizing ) {
161
165
return new LongIntOpenHashMap (size (sizing ));
@@ -166,7 +170,7 @@ public void clear(LongIntOpenHashMap value) {
166
170
value .clear ();
167
171
}
168
172
});
169
- objectIntMap = build (type , limit , smartSize , new Recycler .C <ObjectIntOpenHashMap >() {
173
+ objectIntMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <ObjectIntOpenHashMap >() {
170
174
@ Override
171
175
public ObjectIntOpenHashMap newInstance (int sizing ) {
172
176
return new ObjectIntOpenHashMap (size (sizing ));
@@ -177,7 +181,7 @@ public void clear(ObjectIntOpenHashMap value) {
177
181
value .clear ();
178
182
}
179
183
});
180
- intObjectMap = build (type , limit , smartSize , new Recycler .C <IntObjectOpenHashMap >() {
184
+ intObjectMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <IntObjectOpenHashMap >() {
181
185
@ Override
182
186
public IntObjectOpenHashMap newInstance (int sizing ) {
183
187
return new IntObjectOpenHashMap (size (sizing ));
@@ -188,7 +192,7 @@ public void clear(IntObjectOpenHashMap value) {
188
192
value .clear ();
189
193
}
190
194
});
191
- objectFloatMap = build (type , limit , smartSize , new Recycler .C <ObjectFloatOpenHashMap >() {
195
+ objectFloatMap = build (type , limit , smartSize , availableProcessors , new Recycler .C <ObjectFloatOpenHashMap >() {
192
196
@ Override
193
197
public ObjectFloatOpenHashMap newInstance (int sizing ) {
194
198
return new ObjectFloatOpenHashMap (size (sizing ));
@@ -253,12 +257,12 @@ static int size(int sizing) {
253
257
return sizing > 0 ? sizing : 256 ;
254
258
}
255
259
256
- private <T > Recycler <T > build (Type type , int limit , int smartSize , Recycler .C <T > c ) {
260
+ private <T > Recycler <T > build (Type type , int limit , int smartSize , int availableProcessors , Recycler .C <T > c ) {
257
261
Recycler <T > recycler ;
258
262
try {
259
- recycler = type .build (c , limit );
263
+ recycler = type .build (c , limit , availableProcessors );
260
264
if (smartSize > 0 ) {
261
- recycler = new Recycler . Sizing < T > (recycler , smartSize );
265
+ recycler = sizing (recycler , none ( c ) , smartSize );
262
266
}
263
267
} catch (IllegalArgumentException ex ) {
264
268
throw new ElasticsearchIllegalArgumentException ("no type support [" + type + "] for recycler" );
@@ -270,40 +274,44 @@ private <T> Recycler<T> build(Type type, int limit, int smartSize, Recycler.C<T>
270
274
public static enum Type {
271
275
SOFT_THREAD_LOCAL {
272
276
@ Override
273
- <T > Recycler <T > build (Recycler .C <T > c , int limit ) {
274
- return new SoftThreadLocalRecycler < T >( c , limit );
277
+ <T > Recycler <T > build (Recycler .C <T > c , int limit , int availableProcessors ) {
278
+ return threadLocal ( softFactory ( dequeFactory ( c , limit )) );
275
279
}
280
+ },
281
+ THREAD_LOCAL {
276
282
@ Override
277
- boolean perThread ( ) {
278
- return true ;
283
+ < T > Recycler < T > build ( Recycler . C < T > c , int limit , int availableProcessors ) {
284
+ return threadLocal ( dequeFactory ( c , limit )) ;
279
285
}
280
286
},
281
- THREAD_LOCAL {
287
+ QUEUE {
282
288
@ Override
283
- <T > Recycler <T > build (Recycler .C <T > c , int limit ) {
284
- return new ThreadLocalRecycler < T > (c , limit );
289
+ <T > Recycler <T > build (Recycler .C <T > c , int limit , int availableProcessors ) {
290
+ return concurrentDeque (c , limit );
285
291
}
292
+ },
293
+ SOFT_CONCURRENT {
286
294
@ Override
287
- boolean perThread ( ) {
288
- return true ;
295
+ < T > Recycler < T > build ( Recycler . C < T > c , int limit , int availableProcessors ) {
296
+ return concurrent ( softFactory ( dequeFactory ( c , limit )), availableProcessors ) ;
289
297
}
290
298
},
291
- QUEUE {
299
+ CONCURRENT {
292
300
@ Override
293
- <T > Recycler <T > build (Recycler .C <T > c , int limit ) {
294
- return new QueueRecycler < T >( c , limit );
301
+ <T > Recycler <T > build (Recycler .C <T > c , int limit , int availableProcessors ) {
302
+ return concurrent ( dequeFactory ( c , limit ), availableProcessors );
295
303
}
296
304
},
297
305
NONE {
298
306
@ Override
299
- <T > Recycler <T > build (Recycler .C <T > c , int limit ) {
300
- return new NoneRecycler < T > (c );
307
+ <T > Recycler <T > build (Recycler .C <T > c , int limit , int availableProcessors ) {
308
+ return none (c );
301
309
}
302
310
};
303
311
304
312
public static Type parse (String type ) {
305
313
if (Strings .isNullOrEmpty (type )) {
306
- return SOFT_THREAD_LOCAL ;
314
+ return SOFT_CONCURRENT ;
307
315
}
308
316
try {
309
317
return Type .valueOf (type .toUpperCase (Locale .ROOT ));
@@ -312,9 +320,6 @@ public static Type parse(String type) {
312
320
}
313
321
}
314
322
315
- abstract <T > Recycler <T > build (Recycler .C <T > c , int limit );
316
- boolean perThread () {
317
- return false ;
318
- }
323
+ abstract <T > Recycler <T > build (Recycler .C <T > c , int limit , int availableProcessors );
319
324
}
320
325
}
0 commit comments