ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ag...@apache.org
Subject [2/2] ignite git commit: ignite-2552 Code review
Date Fri, 10 Mar 2017 14:37:40 GMT
ignite-2552 Code review


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/748d1fd0
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/748d1fd0
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/748d1fd0

Branch: refs/heads/master
Commit: 748d1fd0a5ff6459f789b71ae1d0e36276965b51
Parents: 19566fb
Author: agura <agura@apache.org>
Authored: Fri Mar 10 17:29:39 2017 +0300
Committer: agura <agura@apache.org>
Committed: Fri Mar 10 17:29:39 2017 +0300

----------------------------------------------------------------------
 .../cache/eviction/AbstractEvictionPolicy.java  | 45 +++++++++-----------
 .../cache/eviction/fifo/FifoEvictionPolicy.java | 10 ++---
 .../cache/eviction/lru/LruEvictionPolicy.java   | 10 ++---
 .../eviction/sorted/SortedEvictionPolicy.java   | 17 +++++---
 4 files changed, 40 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/748d1fd0/modules/core/src/main/java/org/apache/ignite/cache/eviction/AbstractEvictionPolicy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/AbstractEvictionPolicy.java
b/modules/core/src/main/java/org/apache/ignite/cache/eviction/AbstractEvictionPolicy.java
index eca58e6..cf0aa1f 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/AbstractEvictionPolicy.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/AbstractEvictionPolicy.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.cache.eviction;
 
+import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
@@ -24,12 +25,9 @@ import org.apache.ignite.internal.util.typedef.internal.A;
 import org.jsr166.LongAdder8;
 
 /**
- * Common functionality implementation for eviction policies
+ * Common functionality implementation for eviction policies with max size/max memory and
batch eviction support.
  */
-public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
V> {
-    /** Memory size occupied by elements in container. */
-    private final LongAdder8 memSize = new LongAdder8();
-
+public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
V>, Externalizable {
     /** Max memory size occupied by elements in container. */
     private volatile long maxMemSize;
 
@@ -39,6 +37,9 @@ public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
     /** Batch size. */
     private volatile int batchSize = 1;
 
+    /** Memory size occupied by elements in container. */
+    protected final LongAdder8 memSize = new LongAdder8();
+
     /**
      * Shrinks backed container to maximum allowed size.
      */
@@ -48,7 +49,7 @@ public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
         if (maxMem > 0) {
             long startMemSize = memSize.longValue();
 
-            if (startMemSize >= maxMem)
+            if (startMemSize >= maxMem) {
                 for (long i = maxMem; i < startMemSize && memSize.longValue()
> maxMem; ) {
                     int size = shrink0();
 
@@ -57,6 +58,7 @@ public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
 
                     i += size;
                 }
+            }
         }
 
         int max = this.max;
@@ -64,10 +66,12 @@ public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
         if (max > 0) {
             int startSize = getCurrentSize();
 
-            if (startSize >= max + (maxMem > 0 ? 1 : this.batchSize))
-                for (int i = max; i < startSize && getCurrentSize() > max;
i++)
+            if (startSize >= max + (maxMem > 0 ? 1 : this.batchSize)) {
+                for (int i = max; i < startSize && getCurrentSize() > max;
i++) {
                     if (shrink0() == -1)
                         break;
+                }
+            }
         }
     }
 
@@ -77,6 +81,7 @@ public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
             if (!entry.isCached())
                 return;
 
+            // Shrink only if queue was changed.
             if (touch(entry))
                 shrink();
         }
@@ -86,28 +91,19 @@ public abstract class AbstractEvictionPolicy<K, V> implements EvictionPolicy<K,
             if (node != null) {
                 removeMeta(node);
 
-                addToMemorySize(-entry.size());
+                memSize.add(-entry.size());
             }
-
         }
     }
 
     /**
-     * @param x Changing memory size by adding the value.
-     */
-    protected void addToMemorySize(int x) {
-        memSize.add(x);
-    }
-
-    /**
-     *
-     * @return Size of the container.
+     * @return Size of the container with trackable entries.
      */
     protected abstract int getCurrentSize();
 
     /**
      *
-     * @return Size of the memory which was shrinked0.
+     * @return Size of the memory which was shrinked.
      */
     protected abstract int shrink0();
 
@@ -191,18 +187,15 @@ public abstract class AbstractEvictionPolicy<K, V> implements
EvictionPolicy<K,
         return batchSize;
     }
 
-    public LongAdder8 getMemSize() {
-        return memSize;
-    }
-
     /** {@inheritDoc} */
-    public void writeExternal(ObjectOutput out) throws IOException {
+    @Override public void writeExternal(ObjectOutput out) throws IOException {
         out.writeInt(max);
         out.writeInt(batchSize);
         out.writeLong(maxMemSize);
     }
+
     /** {@inheritDoc} */
-    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+    @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
         max = in.readInt();
         batchSize = in.readInt();
         maxMemSize = in.readLong();

http://git-wip-us.apache.org/repos/asf/ignite/blob/748d1fd0/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java
b/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java
index dd7ba0f..f95a588 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java
@@ -17,7 +17,6 @@
 
 package org.apache.ignite.cache.eviction.fifo;
 
-import java.io.Externalizable;
 import java.util.Collection;
 import java.util.Collections;
 import org.apache.ignite.cache.eviction.AbstractEvictionPolicy;
@@ -44,7 +43,7 @@ import org.jsr166.ConcurrentLinkedDeque8.Node;
  * table-like data structures. The {@code FIFO} ordering information is
  * maintained by attaching ordering metadata to cache entries.
  */
-public class FifoEvictionPolicy<K, V> extends AbstractEvictionPolicy<K, V> implements
FifoEvictionPolicyMBean, Externalizable {
+public class FifoEvictionPolicy<K, V> extends AbstractEvictionPolicy<K, V> implements
FifoEvictionPolicyMBean {
     /** */
     private static final long serialVersionUID = 0L;
 
@@ -81,7 +80,7 @@ public class FifoEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imple
 
     /** {@inheritDoc} */
     @Override public int getCurrentSize() {
-        return queue.size();
+        return queue.sizex();
     }
 
     /**
@@ -94,6 +93,7 @@ public class FifoEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imple
     }
 
     /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
     @Override protected boolean removeMeta(Object meta) {
         return queue.unlinkx((Node<EvictableEntry<K, V>>)meta);
     }
@@ -125,7 +125,7 @@ public class FifoEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imple
                         return false;
                     }
 
-                    addToMemorySize(entry.size());
+                    memSize.add(entry.size());
 
                     return true;
                 }
@@ -157,7 +157,7 @@ public class FifoEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imple
         if (meta != null) {
             size = entry.size();
 
-            addToMemorySize(-size);
+            memSize.add(-size);
 
             if (!entry.evict())
                 touch(entry);

http://git-wip-us.apache.org/repos/asf/ignite/blob/748d1fd0/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java
b/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java
index caa4b3f..17835f1 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java
@@ -17,7 +17,6 @@
 
 package org.apache.ignite.cache.eviction.lru;
 
-import java.io.Externalizable;
 import java.util.Collection;
 import java.util.Collections;
 import org.apache.ignite.cache.eviction.AbstractEvictionPolicy;
@@ -43,7 +42,7 @@ import org.jsr166.ConcurrentLinkedDeque8.Node;
  * This implementation is very efficient since it is lock-free and does not create any additional
table-like
  * data structures. The {@code LRU} ordering information is maintained by attaching ordering
metadata to cache entries.
  */
-public class LruEvictionPolicy<K, V> extends AbstractEvictionPolicy<K, V> implements
LruEvictionPolicyMBean, Externalizable {
+public class LruEvictionPolicy<K, V> extends AbstractEvictionPolicy<K, V> implements
LruEvictionPolicyMBean {
     /** */
     private static final long serialVersionUID = 0L;
 
@@ -69,7 +68,7 @@ public class LruEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> implem
 
     /** {@inheritDoc} */
     @Override public int getCurrentSize() {
-        return queue.size();
+        return queue.sizex();
     }
 
     /**
@@ -82,6 +81,7 @@ public class LruEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> implem
     }
 
     /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
     @Override protected boolean removeMeta(Object meta) {
         return queue.unlinkx((Node<EvictableEntry<K, V>>)meta);
     }
@@ -113,7 +113,7 @@ public class LruEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> implem
                         return false;
                     }
 
-                    addToMemorySize(entry.size());
+                    memSize.add(entry.size());
 
                     return true;
                 }
@@ -153,7 +153,7 @@ public class LruEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> implem
         if (meta != null) {
             size = entry.size();
 
-            addToMemorySize(-size);
+            memSize.add(-size);
 
             if (!entry.evict())
                 touch(entry);

http://git-wip-us.apache.org/repos/asf/ignite/blob/748d1fd0/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java
b/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java
index 47f8479..d0cc975 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java
@@ -17,7 +17,6 @@
 
 package org.apache.ignite.cache.eviction.sorted;
 
-import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
@@ -59,7 +58,7 @@ import static org.apache.ignite.configuration.CacheConfiguration.DFLT_CACHE_SIZE
  * <p>
  * User defined comparator should implement {@link Serializable} interface.
  */
-public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K, V>
implements SortedEvictionPolicyMBean, Externalizable {
+public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K, V>
implements SortedEvictionPolicyMBean {
     /** */
     private static final long serialVersionUID = 0L;
 
@@ -108,6 +107,7 @@ public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imp
     public SortedEvictionPolicy(int max, int batchSize, @Nullable Comparator<EvictableEntry<K,
V>> comp) {
         setMaxSize(max);
         setBatchSize(batchSize);
+
         this.comp = comp == null ? new DefaultHolderComparator<K, V>() : new HolderComparator<>(comp);
         this.set = new GridConcurrentSkipListSetEx<>(this.comp);
     }
@@ -161,7 +161,7 @@ public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imp
                         return false;
                     }
 
-                    addToMemorySize(entry.size());
+                    memSize.add(entry.size());
 
                     return true;
                 }
@@ -177,7 +177,7 @@ public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imp
 
     /** {@inheritDoc} */
     @Override public int getCurrentSize() {
-        return set.size();
+        return set.sizex();
     }
 
     /**
@@ -195,10 +195,12 @@ public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imp
 
         EvictableEntry<K, V> entry = h.entry;
 
-        if (entry != null && h.order > 0 && entry.removeMeta(h)) {
+        assert entry != null;
+
+        if (h.order > 0 && entry.removeMeta(h)) {
             size = entry.size();
 
-            addToMemorySize(-size);
+            memSize.add(-size);
 
             if (!entry.evict())
                 touch(entry);
@@ -210,6 +212,7 @@ public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imp
     /** {@inheritDoc} */
     @Override public void writeExternal(ObjectOutput out) throws IOException {
         super.writeExternal(out);
+
         out.writeObject(comp);
     }
 
@@ -217,6 +220,7 @@ public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imp
     @SuppressWarnings("unchecked")
     @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
         super.readExternal(in);
+
         comp = (Comparator<Holder<K, V>>)in.readObject();
     }
 
@@ -225,6 +229,7 @@ public class SortedEvictionPolicy<K, V> extends AbstractEvictionPolicy<K,
V> imp
      *
      * @param meta Holder.
      */
+    @SuppressWarnings("unchecked")
     @Override protected boolean removeMeta(Object meta) {
         Holder<K, V> holder = (Holder<K, V>)meta;
 


Mime
View raw message