Tune write low/highwatermark
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / connection / OutboundQueueCacheSlice.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
10 import com.google.common.base.FinalizableReferenceQueue;
11 import com.google.common.base.FinalizableSoftReference;
12 import com.google.common.base.Preconditions;
13 import java.lang.ref.Reference;
14 import java.util.Collection;
15 import java.util.concurrent.ConcurrentLinkedDeque;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 final class OutboundQueueCacheSlice {
20     private static final class QueueRef extends FinalizableSoftReference<OutboundQueueImpl> {
21         private final Collection<?> cache;
22
23         protected QueueRef(final FinalizableReferenceQueue queue, final Collection<?> cache, final OutboundQueueImpl referent) {
24             super(referent, queue);
25             this.cache = Preconditions.checkNotNull(cache);
26         }
27
28         @Override
29         public void finalizeReferent() {
30             cache.remove(this);
31         }
32     }
33
34     private static final Logger LOG = LoggerFactory.getLogger(OutboundQueueCacheSlice.class);
35     private final FinalizableReferenceQueue refQueue = new FinalizableReferenceQueue();
36
37     private final ConcurrentLinkedDeque<QueueRef> cache = new ConcurrentLinkedDeque<>();
38     private final int queueSize;
39     private int refCount = 1;
40
41     OutboundQueueCacheSlice(final int queueSize) {
42         Preconditions.checkArgument(queueSize >= 1);
43         this.queueSize = queueSize;
44     }
45
46     void remove(final QueueRef queueRef) {
47         cache.remove(queueRef);
48     }
49
50     boolean decRef() {
51         if (--refCount == 0) {
52             refQueue.close();
53             return true;
54         } else {
55             return false;
56         }
57     }
58
59     void incRef() {
60         refCount++;
61     }
62
63     int getQueueSize() {
64         return queueSize;
65     }
66
67     OutboundQueueImpl getQueue(final OutboundQueueManager<?> manager, final long baseXid) {
68         final OutboundQueueImpl ret;
69         OutboundQueueImpl cached = null;
70         for (;;) {
71             final Reference<OutboundQueueImpl> item = cache.pollLast();
72             if (item == null) {
73                 break;
74             }
75
76             cached = item.get();
77             if (cached != null) {
78                 ret = cached.reuse(manager, baseXid);
79                 LOG.trace("Reusing queue {} as {} on manager {}", cached, ret, manager);
80                 return ret;
81             }
82         }
83
84         ret = new OutboundQueueImpl(manager, baseXid, queueSize + 1);
85         LOG.trace("Allocated new queue {} on manager {}", ret, manager);
86         return ret;
87     }
88
89     void putQueue(final OutboundQueueImpl queue) {
90         queue.retire();
91         if (cache.offer(new QueueRef(refQueue, cache, queue))) {
92             LOG.trace("Saving queue {} for later reuse", queue);
93         } else {
94             LOG.trace("Queue {} thrown away", queue);
95         }
96     }
97 }