1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.dfs;
12
13 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
14 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
15 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_LIMIT;
16 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_SIZE;
17 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CONCURRENCY_LEVEL;
18 import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
19
20 import java.text.MessageFormat;
21 import java.time.Duration;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.function.Consumer;
25
26 import org.eclipse.jgit.internal.JGitText;
27 import org.eclipse.jgit.internal.storage.pack.PackExt;
28 import org.eclipse.jgit.lib.Config;
29
30
31
32
33
34 public class DfsBlockCacheConfig {
35
36 public static final int KB = 1024;
37
38
39 public static final int MB = 1024 * KB;
40
41
42 public static final int DEFAULT_CACHE_HOT_MAX = 1;
43
44 private long blockLimit;
45 private int blockSize;
46 private double streamRatio;
47 private int concurrencyLevel;
48
49 private Consumer<Long> refLock;
50 private Map<PackExt, Integer> cacheHotMap;
51
52 private IndexEventConsumer indexEventConsumer;
53
54
55
56
57 public DfsBlockCacheConfig() {
58 setBlockLimit(32 * MB);
59 setBlockSize(64 * KB);
60 setStreamRatio(0.30);
61 setConcurrencyLevel(32);
62 cacheHotMap = Collections.emptyMap();
63 }
64
65
66
67
68
69
70
71
72 public long getBlockLimit() {
73 return blockLimit;
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public DfsBlockCacheConfig setBlockLimit(long newLimit) {
91 if (newLimit <= 0) {
92 throw new IllegalArgumentException(MessageFormat.format(
93 JGitText.get().blockLimitNotPositive,
94 Long.valueOf(newLimit)));
95 }
96 blockLimit = newLimit;
97 return this;
98 }
99
100
101
102
103
104
105
106
107 public int getBlockSize() {
108 return blockSize;
109 }
110
111
112
113
114
115
116
117
118
119 public DfsBlockCacheConfig setBlockSize(int newSize) {
120 int size = Math.max(512, newSize);
121 if ((size & (size - 1)) != 0) {
122 throw new IllegalArgumentException(
123 JGitText.get().blockSizeNotPowerOf2);
124 }
125 blockSize = size;
126 return this;
127 }
128
129
130
131
132
133
134
135 public int getConcurrencyLevel() {
136 return concurrencyLevel;
137 }
138
139
140
141
142
143
144
145
146
147 public DfsBlockCacheConfig setConcurrencyLevel(
148 final int newConcurrencyLevel) {
149 concurrencyLevel = newConcurrencyLevel;
150 return this;
151 }
152
153
154
155
156
157
158
159
160
161 public double getStreamRatio() {
162 return streamRatio;
163 }
164
165
166
167
168
169
170
171
172 public DfsBlockCacheConfig setStreamRatio(double ratio) {
173 streamRatio = Math.max(0, Math.min(ratio, 1.0));
174 return this;
175 }
176
177
178
179
180
181
182 public Consumer<Long> getRefLockWaitTimeConsumer() {
183 return refLock;
184 }
185
186
187
188
189
190
191
192
193 public DfsBlockCacheConfig setRefLockWaitTimeConsumer(Consumer<Long> c) {
194 refLock = c;
195 return this;
196 }
197
198
199
200
201
202
203 public Map<PackExt, Integer> getCacheHotMap() {
204 return cacheHotMap;
205 }
206
207
208
209
210
211
212
213
214 public DfsBlockCacheConfig setCacheHotMap(
215 Map<PackExt, Integer> cacheHotMap) {
216 this.cacheHotMap = Collections.unmodifiableMap(cacheHotMap);
217 return this;
218 }
219
220
221
222
223
224
225 public IndexEventConsumer getIndexEventConsumer() {
226 return indexEventConsumer;
227 }
228
229
230
231
232
233
234
235
236 public DfsBlockCacheConfig setIndexEventConsumer(
237 IndexEventConsumer indexEventConsumer) {
238 this.indexEventConsumer = indexEventConsumer;
239 return this;
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public DfsBlockCacheConfig fromConfig(Config rc) {
256 long cfgBlockLimit = rc.getLong(
257 CONFIG_CORE_SECTION,
258 CONFIG_DFS_SECTION,
259 CONFIG_KEY_BLOCK_LIMIT,
260 getBlockLimit());
261 int cfgBlockSize = rc.getInt(
262 CONFIG_CORE_SECTION,
263 CONFIG_DFS_SECTION,
264 CONFIG_KEY_BLOCK_SIZE,
265 getBlockSize());
266 if (cfgBlockLimit % cfgBlockSize != 0) {
267 throw new IllegalArgumentException(MessageFormat.format(
268 JGitText.get().blockLimitNotMultipleOfBlockSize,
269 Long.valueOf(cfgBlockLimit),
270 Long.valueOf(cfgBlockSize)));
271 }
272
273 setBlockLimit(cfgBlockLimit);
274 setBlockSize(cfgBlockSize);
275
276 setConcurrencyLevel(rc.getInt(
277 CONFIG_CORE_SECTION,
278 CONFIG_DFS_SECTION,
279 CONFIG_KEY_CONCURRENCY_LEVEL,
280 getConcurrencyLevel()));
281
282 String v = rc.getString(
283 CONFIG_CORE_SECTION,
284 CONFIG_DFS_SECTION,
285 CONFIG_KEY_STREAM_RATIO);
286 if (v != null) {
287 try {
288 setStreamRatio(Double.parseDouble(v));
289 } catch (NumberFormatException e) {
290 throw new IllegalArgumentException(MessageFormat.format(
291 JGitText.get().enumValueNotSupported3,
292 CONFIG_CORE_SECTION,
293 CONFIG_DFS_SECTION,
294 CONFIG_KEY_STREAM_RATIO, v), e);
295 }
296 }
297 return this;
298 }
299
300
301 public interface IndexEventConsumer {
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 void acceptRequestedEvent(int packExtPos, boolean cacheHit,
321 long loadMicros, long bytes, Duration lastEvictionDuration);
322
323
324
325
326
327
328
329
330
331
332
333
334
335 default void acceptEvictedEvent(int packExtPos, long bytes,
336 int totalCacheHitCount, Duration lastEvictionDuration) {
337
338 }
339
340
341
342
343 default boolean shouldReportEvictedEvent() {
344 return false;
345 }
346 }
347 }