|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.util; |
| 21 | + |
| 22 | +import com.google.common.cache.Cache; |
| 23 | +import com.google.common.cache.CacheBuilder; |
| 24 | +import com.google.common.cache.Weigher; |
| 25 | +import org.elasticsearch.common.unit.ByteSizeValue; |
| 26 | +import org.elasticsearch.test.ElasticsearchTestCase; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static org.hamcrest.Matchers.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * Asserts that Guava's caches can get stuck in an overflow state where they |
| 33 | + * never clear themselves based on their "weight" policy if the weight grows |
| 34 | + * beyond MAX_INT. If the noEvictionIf* methods start failing after upgrading |
| 35 | + * Guava then the problem with Guava's caches can probably be considered fixed |
| 36 | + * and {@code ByteSizeValue#MAX_GUAVA_CACHE_SIZE} can likely be removed. |
| 37 | + */ |
| 38 | +public class GuavaCacheOverflowTest extends ElasticsearchTestCase { |
| 39 | + private final int tenMeg = ByteSizeValue.parseBytesSizeValue("10MB").bytesAsInt(); |
| 40 | + |
| 41 | + private Cache<Integer, Object> cache; |
| 42 | + |
| 43 | + @Test |
| 44 | + public void noEvictionIfWeightMaxWeightIs32GB() { |
| 45 | + checkNoEviction("32GB"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void noEvictionIfWeightMaxWeightIsGreaterThan32GB() { |
| 50 | + checkNoEviction(between(33, 50) + "GB"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void evictionIfWeightSlowlyGoesOverMaxWeight() { |
| 55 | + buildCache("30GB"); |
| 56 | + // Add about 100GB of weight to the cache |
| 57 | + int entries = 10240; |
| 58 | + fillCache(entries); |
| 59 | + |
| 60 | + // And as expected, some are purged. |
| 61 | + int missing = 0; |
| 62 | + for (int i = 0; i < 31; i++) { |
| 63 | + if (cache.getIfPresent(i + tenMeg) == null) { |
| 64 | + missing++; |
| 65 | + } |
| 66 | + } |
| 67 | + assertThat(missing, both(greaterThan(0)).and(lessThan(entries))); |
| 68 | + } |
| 69 | + |
| 70 | + private void buildCache(String size) { |
| 71 | + cache = CacheBuilder.newBuilder().concurrencyLevel(16).maximumWeight(ByteSizeValue.parseBytesSizeValue(size).bytes()) |
| 72 | + .weigher(new Weigher<Integer, Object>() { |
| 73 | + @Override |
| 74 | + public int weigh(Integer key, Object value) { |
| 75 | + return key; |
| 76 | + } |
| 77 | + }).build(); |
| 78 | + } |
| 79 | + |
| 80 | + private void fillCache(int entries) { |
| 81 | + for (int i = 0; i < entries; i++) { |
| 82 | + cache.put(i + tenMeg, i); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void checkNoEviction(String size) { |
| 87 | + buildCache(size); |
| 88 | + // Adds ~100GB worth of weight to the cache |
| 89 | + fillCache(10240); |
| 90 | + // But nothing has been purged! |
| 91 | + for (int i = 0; i < 10000; i++) { |
| 92 | + assertNotNull(cache.getIfPresent(i + tenMeg)); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments