You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by or...@apache.org on 2018/05/07 16:31:25 UTC
qpid-broker-j git commit: QPID-7830: [Broker-J] Cache encoded and
decoded values for 0-10 str8
Repository: qpid-broker-j
Updated Branches:
refs/heads/master d293206d7 -> 5b6dfcd94
QPID-7830: [Broker-J] Cache encoded and decoded values for 0-10 str8
Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/5b6dfcd9
Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/5b6dfcd9
Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/5b6dfcd9
Branch: refs/heads/master
Commit: 5b6dfcd94ef64544040de53d38e2463d327ebcb1
Parents: d293206
Author: Alex Rudyy <or...@apache.org>
Authored: Mon May 7 17:30:38 2018 +0100
Committer: Alex Rudyy <or...@apache.org>
Committed: Mon May 7 17:30:52 2018 +0100
----------------------------------------------------------------------
.../server/protocol/v0_8/AMQShortString.java | 29 ++-------
.../qpid/server/virtualhost/CacheFactory.java | 51 +++++++++++++++
.../server/virtualhost/CacheFactoryTest.java | 59 +++++++++++++++++
.../v0_10/transport/AbstractDecoder.java | 35 ++++++-----
.../v0_10/transport/AbstractEncoder.java | 31 +++++----
.../protocol/v0_10/transport/BBDecoderTest.java | 66 ++++++++++++++++++++
.../protocol/v0_10/transport/BBEncoderTest.java | 25 ++++++++
7 files changed, 246 insertions(+), 50 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/5b6dfcd9/broker-core/src/main/java/org/apache/qpid/server/protocol/v0_8/AMQShortString.java
----------------------------------------------------------------------
diff --git a/broker-core/src/main/java/org/apache/qpid/server/protocol/v0_8/AMQShortString.java b/broker-core/src/main/java/org/apache/qpid/server/protocol/v0_8/AMQShortString.java
index 6d17f8e..d0cf604 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/protocol/v0_8/AMQShortString.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/protocol/v0_8/AMQShortString.java
@@ -23,20 +23,15 @@ package org.apache.qpid.server.protocol.v0_8;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
-import java.security.AccessController;
import java.util.Arrays;
-import javax.security.auth.Subject;
-
import com.google.common.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.qpid.server.bytebuffer.QpidByteBuffer;
-import org.apache.qpid.server.security.QpidPrincipal;
-import org.apache.qpid.server.virtualhost.CacheProvider;
+import org.apache.qpid.server.virtualhost.CacheFactory;
import org.apache.qpid.server.virtualhost.NullCache;
-import org.apache.qpid.server.virtualhost.VirtualHostPrincipal;
/**
* A short string is a representation of an AMQ Short String
@@ -56,7 +51,8 @@ public final class AMQShortString implements Comparable<AMQShortString>
// Unfortunately CacheBuilder does not yet support keyEquivalence, so we have to wrap the keys in ByteBuffers
// rather than using the byte arrays as keys.
- private static ThreadLocal<Cache<ByteBuffer, AMQShortString>> CACHE = new ThreadLocal<>();
+ private static final ThreadLocal<Cache<ByteBuffer, AMQShortString>> CACHE =
+ ThreadLocal.withInitial(() -> CacheFactory.getCache("amqShortStringCache", NULL_CACHE));
private final byte[] _data;
private int _hashCode;
@@ -365,24 +361,7 @@ public final class AMQShortString implements Comparable<AMQShortString>
private static Cache<ByteBuffer, AMQShortString> getShortStringCache()
{
- Cache<ByteBuffer, AMQShortString> cache = CACHE.get();
- if (cache == null)
- {
- cache = NULL_CACHE;
- Subject subject = Subject.getSubject(AccessController.getContext());
- if (subject != null)
- {
- VirtualHostPrincipal principal = QpidPrincipal.getSingletonPrincipal(subject, true, VirtualHostPrincipal.class);
-
- if (principal != null && principal.getVirtualHost() instanceof CacheProvider)
- {
- CacheProvider cacheProvider = (CacheProvider) principal.getVirtualHost();
- cache = cacheProvider.getNamedCache("amqShortStringCache");
- }
- }
- CACHE.set(cache);
- }
- return cache;
+ return CACHE.get();
}
/** Unit testing only */
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/5b6dfcd9/broker-core/src/main/java/org/apache/qpid/server/virtualhost/CacheFactory.java
----------------------------------------------------------------------
diff --git a/broker-core/src/main/java/org/apache/qpid/server/virtualhost/CacheFactory.java b/broker-core/src/main/java/org/apache/qpid/server/virtualhost/CacheFactory.java
new file mode 100644
index 0000000..3c110ab
--- /dev/null
+++ b/broker-core/src/main/java/org/apache/qpid/server/virtualhost/CacheFactory.java
@@ -0,0 +1,51 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.qpid.server.virtualhost;
+
+import java.security.AccessController;
+
+import javax.security.auth.Subject;
+
+import com.google.common.cache.Cache;
+
+import org.apache.qpid.server.security.QpidPrincipal;
+
+public class CacheFactory
+{
+
+ public static <K, V> Cache<K, V> getCache(final String cacheName, final Cache<K, V> defaultCache)
+ {
+ Cache<K, V> cache = defaultCache;
+ Subject subject = Subject.getSubject(AccessController.getContext());
+ if (subject != null)
+ {
+ VirtualHostPrincipal
+ principal = QpidPrincipal.getSingletonPrincipal(subject, true, VirtualHostPrincipal.class);
+
+ if (principal != null && principal.getVirtualHost() instanceof CacheProvider)
+ {
+ CacheProvider cacheProvider = (CacheProvider) principal.getVirtualHost();
+ cache = cacheProvider.getNamedCache(cacheName);
+ }
+ }
+ return cache;
+ }
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/5b6dfcd9/broker-core/src/test/java/org/apache/qpid/server/virtualhost/CacheFactoryTest.java
----------------------------------------------------------------------
diff --git a/broker-core/src/test/java/org/apache/qpid/server/virtualhost/CacheFactoryTest.java b/broker-core/src/test/java/org/apache/qpid/server/virtualhost/CacheFactoryTest.java
new file mode 100644
index 0000000..898c80a
--- /dev/null
+++ b/broker-core/src/test/java/org/apache/qpid/server/virtualhost/CacheFactoryTest.java
@@ -0,0 +1,59 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.qpid.server.virtualhost;
+
+
+import static org.junit.Assert.assertSame;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
+
+import java.security.PrivilegedAction;
+
+import javax.security.auth.Subject;
+
+import com.google.common.cache.Cache;
+import org.junit.Test;
+
+import org.apache.qpid.server.model.VirtualHost;
+
+public class CacheFactoryTest
+{
+
+ @Test
+ public void getCache()
+ {
+ String cacheName = "test";
+ final Cache<Object, Object> cache = new NullCache<>();
+ final CacheProvider virtualHost = mock(CacheProvider.class, withSettings().extraInterfaces(VirtualHost.class));
+ when(virtualHost.getNamedCache(cacheName)).thenReturn(cache);
+ final Subject subject = new Subject();
+ subject.getPrincipals().add(new VirtualHostPrincipal((VirtualHost<?>) virtualHost));
+ subject.setReadOnly();
+
+ Cache<String, String> actualCache = Subject.doAs(subject,
+ (PrivilegedAction<Cache<String, String>>) () -> CacheFactory.getCache(cacheName,
+ null));
+ assertSame(actualCache, cache);
+ verify(virtualHost).getNamedCache(cacheName);
+ }
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/5b6dfcd9/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractDecoder.java
----------------------------------------------------------------------
diff --git a/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractDecoder.java b/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractDecoder.java
index 6aa3cd7..9b8d31f 100644
--- a/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractDecoder.java
+++ b/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractDecoder.java
@@ -30,6 +30,11 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
+import com.google.common.cache.Cache;
+
+import org.apache.qpid.server.virtualhost.CacheFactory;
+import org.apache.qpid.server.virtualhost.NullCache;
+
/**
* AbstractDecoder
@@ -39,14 +44,9 @@ import java.util.UUID;
public abstract class AbstractDecoder implements Decoder
{
-
- private final Map<Binary,String> str8cache = new LinkedHashMap<Binary,String>()
- {
- @Override protected boolean removeEldestEntry(Map.Entry<Binary,String> me)
- {
- return size() > 4*1024;
- }
- };
+ private static final NullCache<Binary, String> NULL_CACHE = new NullCache<>();
+ private static final ThreadLocal<Cache<Binary, String>> CACHE =
+ ThreadLocal.withInitial(() -> CacheFactory.getCache("str8Cache", NULL_CACHE));
protected abstract byte doGet();
@@ -126,19 +126,16 @@ public abstract class AbstractDecoder implements Decoder
{
short size = readUint8();
Binary bin = get(size);
- String str = str8cache.get(bin);
+ String str = getStringCache().getIfPresent(bin);
if (str == null)
{
str = new String(bin.array(), bin.offset(), bin.size(), StandardCharsets.UTF_8);
if(bin.hasExcessCapacity())
{
- str8cache.put(bin.copy(), str);
- }
- else
- {
- str8cache.put(bin, str);
+ bin = bin.copy();
}
+ getStringCache().put(bin, str);
}
return str;
}
@@ -478,4 +475,14 @@ public abstract class AbstractDecoder implements Decoder
}
}
+ private static Cache<Binary, String> getStringCache()
+ {
+ return CACHE.get();
+ }
+
+ /** Unit testing only */
+ static void setStringCache(final Cache<Binary, String> cache)
+ {
+ CACHE.set(cache);
+ }
}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/5b6dfcd9/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractEncoder.java
----------------------------------------------------------------------
diff --git a/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractEncoder.java b/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractEncoder.java
index ff9bdb2..8f6a631 100644
--- a/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractEncoder.java
+++ b/broker-plugins/amqp-0-10-protocol/src/main/java/org/apache/qpid/server/protocol/v0_10/transport/AbstractEncoder.java
@@ -26,11 +26,15 @@ import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
+import com.google.common.cache.Cache;
+
+import org.apache.qpid.server.virtualhost.CacheFactory;
+import org.apache.qpid.server.virtualhost.NullCache;
+
/**
* AbstractEncoder
@@ -41,14 +45,9 @@ import java.util.UUID;
public abstract class AbstractEncoder implements Encoder
{
-
- private final Map<String,byte[]> str8cache = new LinkedHashMap<String,byte[]>()
- {
- @Override protected boolean removeEldestEntry(Map.Entry<String,byte[]> me)
- {
- return size() > 4*1024;
- }
- };
+ private static final NullCache<String, byte[]> NULL_CACHE = new NullCache<>();
+ private static final ThreadLocal<Cache<String, byte[]>> CACHE =
+ ThreadLocal.withInitial(() -> CacheFactory.getCache("encodedStr8BytesCache", NULL_CACHE));
protected abstract void doPut(byte b);
@@ -136,7 +135,7 @@ public abstract class AbstractEncoder implements Encoder
s = "";
}
- byte[] bytes = str8cache.get(s);
+ byte[] bytes = getEncodedStringCache().getIfPresent(s);
if (bytes == null)
{
bytes = s.getBytes(StandardCharsets.UTF_8);
@@ -144,7 +143,7 @@ public abstract class AbstractEncoder implements Encoder
{
throw new IllegalArgumentException(String.format("String too long (%d) for str8", bytes.length));
}
- str8cache.put(s, bytes);
+ getEncodedStringCache().put(s, bytes);
}
writeUint8((short) bytes.length);
put(bytes);
@@ -570,4 +569,14 @@ public abstract class AbstractEncoder implements Encoder
}
}
+ private static Cache<String, byte[]> getEncodedStringCache()
+ {
+ return CACHE.get();
+ }
+
+ /** Unit testing only */
+ static void setEncodedStringCache(final Cache<String, byte[]> cache)
+ {
+ CACHE.set(cache);
+ }
}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/5b6dfcd9/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBDecoderTest.java
----------------------------------------------------------------------
diff --git a/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBDecoderTest.java b/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBDecoderTest.java
new file mode 100644
index 0000000..4d8a508
--- /dev/null
+++ b/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBDecoderTest.java
@@ -0,0 +1,66 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.qpid.server.protocol.v0_10.transport;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
+
+import java.nio.ByteBuffer;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import org.junit.Test;
+
+import org.apache.qpid.test.utils.UnitTestBase;
+
+public class BBDecoderTest extends UnitTestBase
+{
+ @Test
+ public void str8Caching()
+ {
+ String testString = "Test";
+ BBEncoder encoder = new BBEncoder(64);
+ encoder.writeStr8(testString);
+ encoder.writeStr8(testString);
+ ByteBuffer buffer = encoder.buffer();
+
+ BBDecoder decoder = new BBDecoder();
+ decoder.init(buffer);
+ Cache<Binary, String> cache = CacheBuilder.newBuilder().maximumSize(1).build();
+ try
+ {
+ BBDecoder.setStringCache(cache);
+
+ String decodedString1 = decoder.readStr8();
+ String decodedString2 = decoder.readStr8();
+
+ assertThat(testString, is(equalTo(decodedString1)));
+ assertThat(testString, is(equalTo(decodedString2)));
+ assertSame(decodedString1, decodedString2);
+ }
+ finally
+ {
+ cache.cleanUp();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/5b6dfcd9/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBEncoderTest.java
----------------------------------------------------------------------
diff --git a/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBEncoderTest.java b/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBEncoderTest.java
index 7d19454..2decc60 100644
--- a/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBEncoderTest.java
+++ b/broker-plugins/amqp-0-10-protocol/src/test/java/org/apache/qpid/server/protocol/v0_10/transport/BBEncoderTest.java
@@ -22,8 +22,11 @@
package org.apache.qpid.server.protocol.v0_10.transport;
import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
import java.nio.ByteBuffer;
import java.util.Arrays;
@@ -32,6 +35,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
import org.junit.Test;
import org.apache.qpid.test.utils.UnitTestBase;
@@ -124,4 +129,24 @@ public class BBEncoderTest extends UnitTestBase
messageProperties.getApplicationHeaders(),
decoded.getApplicationHeaders());
}
+
+ @Test
+ public void encodedStr8Caching()
+ {
+ String testString = "Test";
+ Cache< String, byte[]> cache = CacheBuilder.newBuilder().maximumSize(2).build();
+ try
+ {
+ BBEncoder encoder = new BBEncoder(64);
+ BBEncoder.setEncodedStringCache(cache);
+ encoder.writeStr8(testString);
+ encoder.writeStr8(testString);
+
+ assertThat(cache.size(), is(equalTo(1L)));
+ }
+ finally
+ {
+ cache.cleanUp();
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org