mirror of https://github.com/apache/druid.git
fix over counting of bytes in ByteCountingLRUMap (#4168)
This commit is contained in:
parent
b2954d5fea
commit
5c9198347b
|
@ -19,6 +19,8 @@
|
|||
|
||||
package io.druid.client.cache;
|
||||
|
||||
import io.druid.java.util.common.logger.Logger;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
@ -29,8 +31,6 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import io.druid.java.util.common.logger.Logger;
|
||||
|
||||
/**
|
||||
*/
|
||||
class ByteCountingLRUMap extends LinkedHashMap<ByteBuffer, byte[]>
|
||||
|
@ -104,7 +104,11 @@ class ByteCountingLRUMap extends LinkedHashMap<ByteBuffer, byte[]>
|
|||
remove(keyToRemove);
|
||||
}
|
||||
|
||||
return super.put(key, value);
|
||||
byte[] old = super.put(key, value);
|
||||
if (old != null) {
|
||||
numBytes.addAndGet(-key.remaining() - old.length);
|
||||
}
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -86,6 +86,19 @@ public class ByteCountingLRUMapTest
|
|||
assertMapValues(0, 0, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSameKeyUpdate() throws Exception
|
||||
{
|
||||
final ByteBuffer k = ByteBuffer.allocate(1);
|
||||
|
||||
assertMapValues(0, 0, 0);
|
||||
map.put(k, new byte[1]);
|
||||
map.put(k, new byte[2]);
|
||||
map.put(k, new byte[5]);
|
||||
map.put(k, new byte[3]);
|
||||
assertMapValues(1, 4, 0);
|
||||
}
|
||||
|
||||
private void assertMapValues(final int size, final int numBytes, final int evictionCount)
|
||||
{
|
||||
Assert.assertEquals(size, map.size());
|
||||
|
|
Loading…
Reference in New Issue