[Mondrian] Implementation of ResponseQueue

Luc Boudreau lucboudreau at gmail.com
Mon Jun 10 15:28:15 EDT 2013


Matt and I have been looking into performance bottlenecks. One that we
found was in the way that the Actor of SegmentCacheManagerImpl implements
the ResponseQueue. Because it synchronized the call to ResponseQueue.take,
it was possible for a long running command to prevent other threads from
obtaining the response that they needed out of the queue.

Another problem was that it relied on a WeakHashMap and the GC was
responsible of cleaning it up periodically.

We came up with the following replacement for the ResponseQueue and were
curious if someone can spot a potential problem with this new brew.

Cheers!

--------------------------------------------------------------------------------
    private static class ResponseQueue<K, V> {
        private final ConcurrentHashMap<K, SlotFuture<V>> queue;

        /**
         * Creates a ResponseQueue with given capacity.
         *
         * @param capacity Capacity
         */
        public ResponseQueue(int capacity) {
            queue = new ConcurrentHashMap<K, SlotFuture<V>>(capacity);
        }

        /**
         * Places a (request, response) pair onto the queue.
         *
         * @param k Request
         * @param v Response
         * @throws InterruptedException if interrupted while waiting
         */
        public void put(K k, V v) throws InterruptedException {
            queue.putIfAbsent(k, new SlotFuture<V>());
            queue.get(k).put(v);
        }

        /**
         * Retrieves the response from the queue matching the given key,
         * blocking until it is received.
         *
         * @param k Response
         * @return Response
         * @throws InterruptedException if interrupted while waiting
         */
        public V take(K k) throws InterruptedException {
            queue.putIfAbsent(k, new SlotFuture<V>());
            V v = Util.safeGet(queue.get(k), "");
            queue.remove(k);
            return v;
        }
    }
--------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.pentaho.org/pipermail/mondrian/attachments/20130610/54d35b9a/attachment.html 


More information about the Mondrian mailing list