aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfigTest.java
blob: afa3179cdee3b11082ac8b140735dd1d3270b1af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright (C) 2016, Philipp Marx <philippmarx@gmx.de> and
 * other copyright owners as documented in the project's IP log.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v1.0 which accompanies this
 * distribution, is reproduced below, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package org.eclipse.jgit.internal.storage.dfs;

import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheConfig.DEFAULT_NAME;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_CACHE_PREFIX;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_LIMIT;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BLOCK_SIZE;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CONCURRENCY_LEVEL;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PACK_EXTENSIONS;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_RATIO;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheConfig.DfsBlockCachePackExtConfig;
import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.eclipse.jgit.lib.Config;
import org.junit.Test;

@SuppressWarnings("boxing")
public class DfsBlockCacheConfigTest {

	@Test
	public void blockSizeNotPowerOfTwoExpectsException() {
		assertThrows(JGitText.get().blockSizeNotPowerOf2,
				IllegalArgumentException.class,
				() -> new DfsBlockCacheConfig().setBlockSize(1000));
	}

	@Test
	public void negativeBlockSizeIsConvertedToDefault() {
		DfsBlockCacheConfig config = new DfsBlockCacheConfig();
		config.setBlockSize(-1);

		assertThat(config.getBlockSize(), is(512));
	}

	@Test
	public void tooSmallBlockSizeIsConvertedToDefault() {
		DfsBlockCacheConfig config = new DfsBlockCacheConfig();
		config.setBlockSize(10);

		assertThat(config.getBlockSize(), is(512));
	}

	@Test
	public void validBlockSize() {
		DfsBlockCacheConfig config = new DfsBlockCacheConfig();
		config.setBlockSize(65536);

		assertThat(config.getBlockSize(), is(65536));
	}

	@Test
	public void fromConfigs() {
		Config config = new Config();
		config.setLong(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_BLOCK_LIMIT, 50 * 1024);
		config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_BLOCK_SIZE, 1024);
		config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_CONCURRENCY_LEVEL, 3);
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_STREAM_RATIO, "0.5");

		DfsBlockCacheConfig cacheConfig = new DfsBlockCacheConfig()
				.fromConfig(config);
		assertThat(cacheConfig.getBlockLimit(), is(50L * 1024L));
		assertThat(cacheConfig.getBlockSize(), is(1024));
		assertThat(cacheConfig.getConcurrencyLevel(), is(3));
		assertThat(cacheConfig.getStreamRatio(), closeTo(0.5, 0.0001));
	}

	@Test
	public void fromConfig_blockLimitNotAMultipleOfBlockSize_throws() {
		Config config = new Config();
		config.setLong(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_BLOCK_LIMIT, 1025);
		config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_BLOCK_SIZE, 1024);

		assertThrows(IllegalArgumentException.class,
				() -> new DfsBlockCacheConfig().fromConfig(config));
	}

	@Test
	public void fromConfig_streamRatioInvalidFormat_throws() {
		Config config = new Config();
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_STREAM_RATIO, "0.a5");

		assertThrows(IllegalArgumentException.class,
				() -> new DfsBlockCacheConfig().fromConfig(config));
	}

	@Test
	public void fromConfig_generatesDfsBlockCachePackExtConfigs() {
		Config config = new Config();
		addPackExtConfigEntry(config, "pack", List.of(PackExt.PACK),
				/* blockLimit= */ 20 * 512, /* blockSize= */ 512);

		addPackExtConfigEntry(config, "bitmap", List.of(PackExt.BITMAP_INDEX),
				/* blockLimit= */ 25 * 1024, /* blockSize= */ 1024);

		addPackExtConfigEntry(config, "index",
				List.of(PackExt.INDEX, PackExt.OBJECT_SIZE_INDEX,
						PackExt.REVERSE_INDEX),
				/* blockLimit= */ 30 * 1024, /* blockSize= */ 1024);

		DfsBlockCacheConfig cacheConfig = new DfsBlockCacheConfig()
				.fromConfig(config);
		var configs = cacheConfig.getPackExtCacheConfigurations();
		assertThat(configs, hasSize(3));
		var packConfig = getConfigForExt(configs, PackExt.PACK);
		assertThat(packConfig.getBlockLimit(), is(20L * 512L));
		assertThat(packConfig.getBlockSize(), is(512));

		var bitmapConfig = getConfigForExt(configs, PackExt.BITMAP_INDEX);
		assertThat(bitmapConfig.getBlockLimit(), is(25L * 1024L));
		assertThat(bitmapConfig.getBlockSize(), is(1024));

		var indexConfig = getConfigForExt(configs, PackExt.INDEX);
		assertThat(indexConfig.getBlockLimit(), is(30L * 1024L));
		assertThat(indexConfig.getBlockSize(), is(1024));
		assertThat(getConfigForExt(configs, PackExt.OBJECT_SIZE_INDEX),
				is(indexConfig));
		assertThat(getConfigForExt(configs, PackExt.REVERSE_INDEX),
				is(indexConfig));
	}

	@Test
	public void fromConfig_withExistingCacheHotMap_configWithPackExtConfigsHasHotMaps() {
		Config config = new Config();
		addPackExtConfigEntry(config, "pack", List.of(PackExt.PACK),
				/* blockLimit= */ 20 * 512, /* blockSize= */ 512);

		addPackExtConfigEntry(config, "bitmap", List.of(PackExt.BITMAP_INDEX),
				/* blockLimit= */ 25 * 1024, /* blockSize= */ 1024);

		addPackExtConfigEntry(config, "index",
				List.of(PackExt.INDEX, PackExt.OBJECT_SIZE_INDEX,
						PackExt.REVERSE_INDEX),
				/* blockLimit= */ 30 * 1024, /* blockSize= */ 1024);

		Map<PackExt, Integer> cacheHotMap = Map.of(PackExt.PACK, 1,
				PackExt.BITMAP_INDEX, 2, PackExt.INDEX, 3, PackExt.REFTABLE, 4);

		DfsBlockCacheConfig cacheConfig = new DfsBlockCacheConfig();
		cacheConfig.setCacheHotMap(cacheHotMap);
		cacheConfig.fromConfig(config);

		var configs = cacheConfig.getPackExtCacheConfigurations();
		assertThat(cacheConfig.getCacheHotMap(), is(cacheHotMap));
		assertThat(configs, hasSize(3));
		var packConfig = getConfigForExt(configs, PackExt.PACK);
		assertThat(packConfig.getCacheHotMap(), is(Map.of(PackExt.PACK, 1)));

		var bitmapConfig = getConfigForExt(configs, PackExt.BITMAP_INDEX);
		assertThat(bitmapConfig.getCacheHotMap(),
				is(Map.of(PackExt.BITMAP_INDEX, 2)));

		var indexConfig = getConfigForExt(configs, PackExt.INDEX);
		assertThat(indexConfig.getCacheHotMap(), is(Map.of(PackExt.INDEX, 3)));
	}

	@Test
	public void setCacheHotMap_configWithPackExtConfigs_setsHotMaps() {
		Config config = new Config();
		addPackExtConfigEntry(config, "pack", List.of(PackExt.PACK),
				/* blockLimit= */ 20 * 512, /* blockSize= */ 512);

		addPackExtConfigEntry(config, "bitmap", List.of(PackExt.BITMAP_INDEX),
				/* blockLimit= */ 25 * 1024, /* blockSize= */ 1024);

		addPackExtConfigEntry(config, "index",
				List.of(PackExt.INDEX, PackExt.OBJECT_SIZE_INDEX,
						PackExt.REVERSE_INDEX),
				/* blockLimit= */ 30 * 1024, /* blockSize= */ 1024);

		Map<PackExt, Integer> cacheHotMap = Map.of(PackExt.PACK, 1,
				PackExt.BITMAP_INDEX, 2, PackExt.INDEX, 3, PackExt.REFTABLE, 4);

		DfsBlockCacheConfig cacheConfig = new DfsBlockCacheConfig()
				.fromConfig(config);
		cacheConfig.setCacheHotMap(cacheHotMap);

		var configs = cacheConfig.getPackExtCacheConfigurations();
		assertThat(cacheConfig.getCacheHotMap(), is(cacheHotMap));
		assertThat(configs, hasSize(3));
		var packConfig = getConfigForExt(configs, PackExt.PACK);
		assertThat(packConfig.getCacheHotMap(), is(Map.of(PackExt.PACK, 1)));

		var bitmapConfig = getConfigForExt(configs, PackExt.BITMAP_INDEX);
		assertThat(bitmapConfig.getCacheHotMap(),
				is(Map.of(PackExt.BITMAP_INDEX, 2)));

		var indexConfig = getConfigForExt(configs, PackExt.INDEX);
		assertThat(indexConfig.getCacheHotMap(), is(Map.of(PackExt.INDEX, 3)));
	}

	@Test
	public void fromConfigs_baseConfigOnly_nameSetFromConfigDfsSubSection() {
		Config config = new Config();

		DfsBlockCacheConfig blockCacheConfig = new DfsBlockCacheConfig()
				.fromConfig(config);
		assertThat(blockCacheConfig.getName(), equalTo(DEFAULT_NAME));
	}

	@Test
	public void fromConfigs_namesSetFromConfigDfsCachePrefixSubSections() {
		Config config = new Config();
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_STREAM_RATIO, "0.5");
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "name1",
				CONFIG_KEY_PACK_EXTENSIONS, PackExt.PACK.name());
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "name2",
				CONFIG_KEY_PACK_EXTENSIONS, PackExt.BITMAP_INDEX.name());

		DfsBlockCacheConfig blockCacheConfig = new DfsBlockCacheConfig()
				.fromConfig(config);
		assertThat(blockCacheConfig.getName(), equalTo("dfs"));
		assertThat(
				blockCacheConfig.getPackExtCacheConfigurations().get(0)
						.getPackExtCacheConfiguration().getName(),
				equalTo("dfs.name1"));
		assertThat(
				blockCacheConfig.getPackExtCacheConfigurations().get(1)
						.getPackExtCacheConfiguration().getName(),
				equalTo("dfs.name2"));
	}

	@Test
	public void fromConfigs_dfsBlockCachePackExtConfigWithDuplicateExtensions_throws() {
		Config config = new Config();
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "pack1",
				CONFIG_KEY_PACK_EXTENSIONS, PackExt.PACK.name());

		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "pack2",
				CONFIG_KEY_PACK_EXTENSIONS, PackExt.PACK.name());

		assertThrows(IllegalArgumentException.class,
				() -> new DfsBlockCacheConfig().fromConfig(config));
	}

	@Test
	public void fromConfigs_dfsBlockCachePackExtConfigWithEmptyExtensions_throws() {
		Config config = new Config();
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "pack1",
				CONFIG_KEY_PACK_EXTENSIONS, "");

		assertThrows(IllegalArgumentException.class,
				() -> new DfsBlockCacheConfig().fromConfig(config));
	}

	@Test
	public void fromConfigs_dfsBlockCachePackExtConfigWithNoExtensions_throws() {
		Config config = new Config();
		config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_CACHE_PREFIX + "pack1",
				CONFIG_KEY_BLOCK_SIZE, 0);

		assertThrows(IllegalArgumentException.class,
				() -> new DfsBlockCacheConfig().fromConfig(config));
	}

	@Test
	public void fromConfigs_dfsBlockCachePackExtConfigWithUnknownExtensions_throws() {
		Config config = new Config();
		config.setString(CONFIG_CORE_SECTION,
				CONFIG_DFS_CACHE_PREFIX + "unknownExt",
				CONFIG_KEY_PACK_EXTENSIONS, "NotAKnownExt");

		assertThrows(IllegalArgumentException.class,
				() -> new DfsBlockCacheConfig().fromConfig(config));
	}

	@Test
	public void writeConfigurationDebug_writesConfigsToWriter()
			throws Exception {
		Config config = new Config();
		config.setLong(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_BLOCK_LIMIT, 50 * 1024);
		config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_BLOCK_SIZE, 1024);
		config.setInt(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_CONCURRENCY_LEVEL, 3);
		config.setString(CONFIG_CORE_SECTION, CONFIG_DFS_SECTION,
				CONFIG_KEY_STREAM_RATIO, "0.5");
		addPackExtConfigEntry(config, "pack", List.of(PackExt.PACK),
				/* blockLimit= */ 20 * 512, /* blockSize= */ 512);

		DfsBlockCacheConfig cacheConfig = new DfsBlockCacheConfig()
				.fromConfig(config);
		Map<PackExt, Integer> hotmap = Map.of(PackExt.PACK, 10);
		cacheConfig.setCacheHotMap(hotmap);

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		cacheConfig.print(new PrintWriter(byteArrayOutputStream, true,
				StandardCharsets.UTF_8));

		String writenConfig = byteArrayOutputStream
				.toString(StandardCharsets.UTF_8);

		List<String> writenLines = Arrays.asList(writenConfig.split("\n"));
		assertThat(writenLines,
				equalTo(List.of("Name: dfs", "  BlockLimit: " + (50 * 1024),
						"  BlockSize: 1024", "  StreamRatio: 0.5",
						"  ConcurrencyLevel: 3",
						"  CacheHotMapEntry: " + PackExt.PACK + " : " + 10,
						"  Name: dfs.pack", "    BlockLimit: " + 20 * 512,
						"    BlockSize: 512", "    StreamRatio: 0.3",
						"    ConcurrencyLevel: 32",
						"    CacheHotMapEntry: " + PackExt.PACK + " : " + 10,
						"    PackExts: " + List.of(PackExt.PACK))));
	}

	private static void addPackExtConfigEntry(Config config, String configName,
			List<PackExt> packExts, long blockLimit, int blockSize) {
		String packExtConfigName = CONFIG_DFS_CACHE_PREFIX + configName;
		config.setString(CONFIG_CORE_SECTION, packExtConfigName,
				CONFIG_KEY_PACK_EXTENSIONS, packExts.stream().map(PackExt::name)
						.collect(Collectors.joining(" ")));
		config.setLong(CONFIG_CORE_SECTION, packExtConfigName,
				CONFIG_KEY_BLOCK_LIMIT, blockLimit);
		config.setInt(CONFIG_CORE_SECTION, packExtConfigName,
				CONFIG_KEY_BLOCK_SIZE, blockSize);
	}

	private static DfsBlockCacheConfig getConfigForExt(
			List<DfsBlockCachePackExtConfig> configs, PackExt packExt) {
		for (DfsBlockCachePackExtConfig config : configs) {
			if (config.getPackExts().contains(packExt)) {
				return config.getPackExtCacheConfiguration();
			}
		}
		return null;
	}
}