incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject git commit: Some large updates to the block cache v2 to make it more stable
Date Thu, 21 Nov 2013 21:07:24 GMT
Updated Branches:
  refs/heads/apache-blur-0.2 3ace5e4cd -> 89f5a5b08


Some large updates to the block cache v2 to make it more stable


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/89f5a5b0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/89f5a5b0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/89f5a5b0

Branch: refs/heads/apache-blur-0.2
Commit: 89f5a5b0891cba4e2bc35d956b98546a017de504
Parents: 3ace5e4
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Thu Nov 21 16:06:39 2013 -0500
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Thu Nov 21 16:06:39 2013 -0500

----------------------------------------------------------------------
 .../blur/store/blockcache_v2/BaseCache.java     | 122 ++-----------------
 .../store/blockcache_v2/CacheIndexInput.java    |   7 +-
 .../blur/store/blockcache_v2/CacheValue.java    |  48 +++-----
 .../blockcache_v2/CacheValueBufferPool.java     | 110 +++++++++++++++++
 .../cachevalue/BaseCacheValue.java              |  33 ++---
 .../cachevalue/ByteArrayCacheValue.java         |   8 +-
 .../cachevalue/DetachableCacheValue.java        | 101 +++++++++++++++
 .../cachevalue/UnsafeCacheValue.java            |  18 ---
 .../blockcache_v2/CacheIndexInputTest.java      |   8 +-
 .../apache/blur/metrics/MetricsConstants.java   |   6 +
 10 files changed, 256 insertions(+), 205 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/BaseCache.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/BaseCache.java b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/BaseCache.java
index 2e25ad8..bcb279c 100644
--- a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/BaseCache.java
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/BaseCache.java
@@ -28,23 +28,19 @@ import static org.apache.blur.metrics.MetricsConstants.SIZE;
 
 import java.io.Closeable;
 import java.io.IOException;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.blur.log.Log;
 import org.apache.blur.log.LogFactory;
-import org.apache.blur.store.blockcache_v2.cachevalue.ByteArrayCacheValue;
-import org.apache.blur.store.blockcache_v2.cachevalue.UnsafeCacheValue;
+import org.apache.blur.store.blockcache_v2.cachevalue.DetachableCacheValue;
 import org.apache.lucene.store.IOContext;
 
 import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
@@ -69,38 +65,17 @@ public class BaseCache extends Cache implements Closeable {
     @Override
     public void onEviction(CacheKey key, CacheValue value) {
       _evictions.mark();
-      value.evict();
-      addToReleaseQueue(key, value);
+      _cacheValueBufferPool.returnToPool(value.detachFromCache());
     }
   }
 
   class BaseCacheWeigher implements Weigher<CacheValue> {
     @Override
     public int weightOf(CacheValue value) {
-      return value.size();
+      return value.length();
     }
   }
 
-  static class ReleaseEntry {
-    CacheKey _key;
-    CacheValue _value;
-    final long _createTime = System.currentTimeMillis();
-
-    @Override
-    public String toString() {
-      return "ReleaseEntry [_key=" + _key + ", _value=" + _value + ", _createTime=" + _createTime
+ "]";
-    }
-
-    public boolean hasLivedToLong(long warningTimeForEntryCleanup) {
-      long now = System.currentTimeMillis();
-      if (_createTime + warningTimeForEntryCleanup < now) {
-        return true;
-      }
-      return false;
-    }
-
-  }
-
   private final ConcurrentLinkedHashMap<CacheKey, CacheValue> _cacheMap;
   private final FileNameFilter _readFilter;
   private final FileNameFilter _writeFilter;
@@ -116,10 +91,8 @@ public class BaseCache extends Cache implements Closeable {
   private final Meter _evictions;
   private final Meter _removals;
   private final Thread _oldFileDaemonThread;
-  private final Thread _oldCacheValueDaemonThread;
   private final AtomicBoolean _running = new AtomicBoolean(true);
-  private final BlockingQueue<ReleaseEntry> _releaseQueue;
-  private final long _warningTimeForEntryCleanup = TimeUnit.SECONDS.toMillis(10);
+  private final CacheValueBufferPool _cacheValueBufferPool;
 
   public BaseCache(long totalNumberOfBytes, Size fileBufferSize, Size cacheBlockSize, FileNameFilter
readFilter,
       FileNameFilter writeFilter, Quiet quiet, STORE store) {
@@ -135,7 +108,8 @@ public class BaseCache extends Cache implements Closeable {
     _misses = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE, MISS), MISS, TimeUnit.SECONDS);
     _evictions = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE, EVICTION), EVICTION,
TimeUnit.SECONDS);
     _removals = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE, REMOVAL), REMOVAL,
TimeUnit.SECONDS);
-    _releaseQueue = new LinkedBlockingQueue<ReleaseEntry>();
+    // @TODO make configurable
+    _cacheValueBufferPool = new CacheValueBufferPool(_store, 1000);
     Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, CACHE, ENTRIES), new Gauge<Long>()
{
       @Override
       public Long value() {
@@ -165,55 +139,6 @@ public class BaseCache extends Cache implements Closeable {
     _oldFileDaemonThread.setName("BaseCacheOldFileCleanup");
     _oldFileDaemonThread.setPriority(Thread.MIN_PRIORITY);
     _oldFileDaemonThread.start();
-
-    _oldCacheValueDaemonThread = new Thread(new Runnable() {
-      @Override
-      public void run() {
-        while (_running.get()) {
-          cleanupOldCacheValues();
-          try {
-            Thread.sleep(_10_SECOND);
-          } catch (InterruptedException e) {
-            return;
-          }
-        }
-      }
-    });
-    _oldCacheValueDaemonThread.setDaemon(true);
-    _oldCacheValueDaemonThread.setName("BaseCacheCleanupCacheValues");
-    _oldCacheValueDaemonThread.start();
-  }
-
-  protected void cleanupOldCacheValues() {
-    Iterator<ReleaseEntry> iterator = _releaseQueue.iterator();
-    Map<Long, FileIdKey> entriesToCleanup = new HashMap<Long, FileIdKey>(_oldFileNameIdMap);
-    while (iterator.hasNext()) {
-      ReleaseEntry entry = iterator.next();
-      CacheKey key = entry._key;
-      long fileId = key.getFileId();
-      // Still referenced
-      entriesToCleanup.remove(fileId);
-
-      CacheValue value = entry._value;
-      boolean release = false;
-      if (value.refCount() == 0) {
-        release = true;
-      } else if (entry.hasLivedToLong(_warningTimeForEntryCleanup)) {
-        FileIdKey fileIdKey = _oldFileNameIdMap.get(fileId);
-        LOG.warn("CacheValue has not been released [{0}] for [{1}] for over [{2} ms] forcing
release.", entry,
-            fileIdKey, _warningTimeForEntryCleanup);
-        release = true;
-      }
-      if (release) {
-        value.release();
-        iterator.remove();
-        long capacity = _cacheMap.capacity();
-        _cacheMap.setCapacity(capacity + value.size());
-      }
-    }
-    for (Long l : entriesToCleanup.keySet()) {
-      _oldFileNameIdMap.remove(l);
-    }
   }
 
   protected void cleanupOldFiles() {
@@ -224,9 +149,8 @@ public class BaseCache extends Cache implements Closeable {
       if (!validFileIds.contains(fileId)) {
         CacheValue remove = _cacheMap.remove(key);
         if (remove != null) {
-          remove.evict();
           _removals.mark();
-          addToReleaseQueue(key, remove);
+          _cacheValueBufferPool.returnToPool(remove.detachFromCache());
         }
       }
     }
@@ -237,28 +161,7 @@ public class BaseCache extends Cache implements Closeable {
     _running.set(false);
     _cacheMap.clear();
     _oldFileDaemonThread.interrupt();
-    _oldCacheValueDaemonThread.interrupt();
-    for (ReleaseEntry entry : _releaseQueue) {
-      entry._value.release();
-    }
-  }
-
-  private void addToReleaseQueue(CacheKey key, CacheValue value) {
-    if (value != null) {
-      if (value.refCount() == 0) {
-        value.release();
-        return;
-      }
-      long capacity = _cacheMap.capacity();
-      _cacheMap.setCapacity(capacity - value.size());
-
-      ReleaseEntry releaseEntry = new ReleaseEntry();
-      releaseEntry._key = key;
-      releaseEntry._value = value;
-
-      LOG.debug("CacheValue was not released [{0}]", releaseEntry);
-      _releaseQueue.add(releaseEntry);
-    }
+    _cacheValueBufferPool.close();
   }
 
   @Override
@@ -268,14 +171,7 @@ public class BaseCache extends Cache implements Closeable {
 
   @Override
   public CacheValue newInstance(CacheDirectory directory, String fileName, int cacheBlockSize)
{
-    switch (_store) {
-    case ON_HEAP:
-      return new ByteArrayCacheValue(cacheBlockSize);
-    case OFF_HEAP:
-      return new UnsafeCacheValue(cacheBlockSize);
-    default:
-      throw new RuntimeException("Unknown type [" + _store + "]");
-    }
+    return new DetachableCacheValue(_cacheValueBufferPool.getCacheValue(cacheBlockSize));
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheIndexInput.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheIndexInput.java
b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheIndexInput.java
index 5d02963..59d3775 100644
--- a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheIndexInput.java
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheIndexInput.java
@@ -92,7 +92,7 @@ public class CacheIndexInput extends IndexInput {
   }
 
   private boolean isCacheValueValid() {
-    if (_cacheValue != null && !_cacheValue.isEvicted()) {
+    if (_cacheValue != null) {
       return true;
     }
     return false;
@@ -256,9 +256,6 @@ public class CacheIndexInput extends IndexInput {
     CacheIndexInput clone = (CacheIndexInput) super.clone();
     clone._key = _key.clone();
     clone._indexInput = _indexInput.clone();
-    if (isCacheValueValid()) {
-      clone._cacheValue.incRef();
-    }
     clone._quiet = _cache.shouldBeQuiet(_directory, _fileName);
     return clone;
   }
@@ -290,7 +287,6 @@ public class CacheIndexInput extends IndexInput {
 
   private void releaseCache() {
     if (_cacheValue != null) {
-      _cacheValue.decRef();
       _cacheValue = null;
     }
   }
@@ -315,7 +311,6 @@ public class CacheIndexInput extends IndexInput {
       _store.putBuffer(buffer);
       _cache.put(_key.clone(), _cacheValue);
     }
-    _cacheValue.incRef();
     _blockPosition = getBlockPosition();
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValue.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValue.java
b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValue.java
index 0b24c47..2e0a5bf 100644
--- a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValue.java
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValue.java
@@ -22,24 +22,12 @@ import javax.swing.text.Position;
 public interface CacheValue {
   
   /**
-   * Marks value as evicted.
+   * Detach from the base cache.
+   * @return old cache value.
    */
-  void evict();
+  CacheValue detachFromCache();
   
   /**
-   * Gets whether or not value is evicted.
-   * @return
-   */
-  boolean isEvicted();
-
-  /**
-   * The actual size of the the underlying resource.
-   * 
-   * @return the size.
-   */
-  int size();
-
-  /**
    * The length of the data in this block.
    * 
    * @return the length.
@@ -83,20 +71,20 @@ public interface CacheValue {
    */
   byte read(int position);
 
-  /**
-   * Increments the reference.
-   */
-  void incRef();
-
-  /**
-   * Decrements the reference.
-   */
-  void decRef();
-
-  /**
-   * Gets the reference count.
-   */
-  long refCount();
+//  /**
+//   * Increments the reference.
+//   */
+//  void incRef();
+//
+//  /**
+//   * Decrements the reference.
+//   */
+//  void decRef();
+//
+//  /**
+//   * Gets the reference count.
+//   */
+//  long refCount();
 
   /**
    * Releases any underlying resources.
@@ -131,7 +119,7 @@ public interface CacheValue {
   long readLong(int position);
 
   /**
-   * This method will trim the existing {@link CacheValue} and produce
+   * This method <i>may</i> trim the existing {@link CacheValue} and produce
    * potentially a new {@link CacheValue} with the same data up to the length
    * provided. Also if a new {@link CacheValue} is produced then this method is
    * responsible to calling release on the old {@link CacheValue}.

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValueBufferPool.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValueBufferPool.java
b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValueBufferPool.java
new file mode 100644
index 0000000..3904642
--- /dev/null
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/CacheValueBufferPool.java
@@ -0,0 +1,110 @@
+/**
+ * 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.blur.store.blockcache_v2;
+
+import static org.apache.blur.metrics.MetricsConstants.CACHE_POOL;
+import static org.apache.blur.metrics.MetricsConstants.CREATED;
+import static org.apache.blur.metrics.MetricsConstants.DESTROYED;
+import static org.apache.blur.metrics.MetricsConstants.ORG_APACHE_BLUR;
+import static org.apache.blur.metrics.MetricsConstants.REUSED;
+
+import java.io.Closeable;
+import java.util.Map.Entry;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.blur.store.blockcache_v2.BaseCache.STORE;
+import org.apache.blur.store.blockcache_v2.cachevalue.ByteArrayCacheValue;
+import org.apache.blur.store.blockcache_v2.cachevalue.UnsafeCacheValue;
+
+import com.yammer.metrics.Metrics;
+import com.yammer.metrics.core.Meter;
+import com.yammer.metrics.core.MetricName;
+
+public class CacheValueBufferPool implements Closeable {
+
+  private final STORE _store;
+  private final ConcurrentMap<Integer, BlockingQueue<CacheValue>> _cacheValuePool
= new ConcurrentHashMap<Integer, BlockingQueue<CacheValue>>();
+  private final int _capacity;
+  private final Meter _reused;
+  private final Meter _detroyed;
+  private final Meter _created;
+
+  public CacheValueBufferPool(STORE store, int capacity) {
+    _store = store;
+    _capacity = capacity;
+    _created = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE_POOL, CREATED), CREATED,
TimeUnit.SECONDS);
+    _reused = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE_POOL, REUSED), REUSED,
TimeUnit.SECONDS);
+    _detroyed = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE_POOL, DESTROYED),
DESTROYED, TimeUnit.SECONDS);
+  }
+
+  public CacheValue getCacheValue(int cacheBlockSize) {
+    BlockingQueue<CacheValue> blockingQueue = getPool(cacheBlockSize);
+    CacheValue cacheValue = blockingQueue.poll();
+    if (cacheValue == null) {
+      _created.mark();
+      return createCacheValue(cacheBlockSize);
+    }
+    _reused.mark();
+    return cacheValue;
+  }
+
+  private BlockingQueue<CacheValue> getPool(int cacheBlockSize) {
+    BlockingQueue<CacheValue> blockingQueue = _cacheValuePool.get(_cacheValuePool);
+    if (blockingQueue == null) {
+      blockingQueue = buildNewBlockQueue(cacheBlockSize);
+    }
+    return blockingQueue;
+  }
+
+  private BlockingQueue<CacheValue> buildNewBlockQueue(int cacheBlockSize) {
+    _cacheValuePool.putIfAbsent(cacheBlockSize, new ArrayBlockingQueue<CacheValue>(_capacity));
+    return _cacheValuePool.get(cacheBlockSize);
+  }
+
+  private CacheValue createCacheValue(int cacheBlockSize) {
+    switch (_store) {
+    case ON_HEAP:
+      return new ByteArrayCacheValue(cacheBlockSize);
+    case OFF_HEAP:
+      return new UnsafeCacheValue(cacheBlockSize);
+    default:
+      throw new RuntimeException("Unknown type [" + _store + "]");
+    }
+  }
+
+  public void returnToPool(CacheValue cacheValue) {
+    BlockingQueue<CacheValue> blockingQueue = getPool(cacheValue.length());
+    if (!blockingQueue.offer(cacheValue)) {
+      _detroyed.mark();
+      cacheValue.release();
+    }
+  }
+
+  @Override
+  public void close() {
+    for (Entry<Integer, BlockingQueue<CacheValue>> e : _cacheValuePool.entrySet())
{
+      BlockingQueue<CacheValue> queue = _cacheValuePool.remove(e.getKey());
+      for (CacheValue cacheValue : queue) {
+        cacheValue.release();
+      }
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/BaseCacheValue.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/BaseCacheValue.java
b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/BaseCacheValue.java
index cf58beb..62aeb93 100644
--- a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/BaseCacheValue.java
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/BaseCacheValue.java
@@ -30,14 +30,14 @@ import com.yammer.metrics.Metrics;
 import com.yammer.metrics.core.MetricName;
 
 @SuppressWarnings("serial")
-public abstract class BaseCacheValue extends AtomicLong implements CacheValue {
+public abstract class BaseCacheValue implements CacheValue {
 
   private static final AtomicLong _neededFinalizedCall = new AtomicLong();
-  
+
   public static class Evicted extends RuntimeException {
-    
+
   }
-  
+
   private final int _length;
   protected volatile boolean _released = false;
   protected volatile boolean _evicted = false;
@@ -136,21 +136,6 @@ public abstract class BaseCacheValue extends AtomicLong implements CacheValue
{
   protected abstract void readInternal(int position, byte[] buf, int offset, int length);
 
   @Override
-  public final void incRef() {
-    incrementAndGet();
-  }
-
-  @Override
-  public final void decRef() {
-    decrementAndGet();
-  }
-
-  @Override
-  public final long refCount() {
-    return get();
-  }
-
-  @Override
   protected void finalize() throws Throwable {
     // @TODO this may not be needed.
     if (!_released) {
@@ -158,14 +143,14 @@ public abstract class BaseCacheValue extends AtomicLong implements CacheValue
{
       _neededFinalizedCall.incrementAndGet();
     }
   }
-  
+
   @Override
-  public void evict() {
-    _evicted = true;
+  public CacheValue trim(int length) {
+    return this;
   }
 
   @Override
-  public boolean isEvicted() {
-    return _evicted;
+  public CacheValue detachFromCache() {
+    throw new RuntimeException("Not implemented.");
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/ByteArrayCacheValue.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/ByteArrayCacheValue.java
b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/ByteArrayCacheValue.java
index c1d53cc..e36f041 100644
--- a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/ByteArrayCacheValue.java
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/ByteArrayCacheValue.java
@@ -19,10 +19,9 @@ package org.apache.blur.store.blockcache_v2.cachevalue;
 
 import org.apache.blur.store.blockcache_v2.CacheValue;
 
-@SuppressWarnings("serial")
 public class ByteArrayCacheValue extends BaseCacheValue {
 
-  private final byte[] _buffer;
+  final byte[] _buffer;
 
   public ByteArrayCacheValue(int length) {
     super(length);
@@ -50,11 +49,6 @@ public class ByteArrayCacheValue extends BaseCacheValue {
   }
 
   @Override
-  public int size() {
-    return length();
-  }
-
-  @Override
   public CacheValue trim(int length) {
     if (_buffer.length == length) {
       return this;

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/DetachableCacheValue.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/DetachableCacheValue.java
b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/DetachableCacheValue.java
new file mode 100644
index 0000000..fe9e0b9
--- /dev/null
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/DetachableCacheValue.java
@@ -0,0 +1,101 @@
+/**
+ * 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.blur.store.blockcache_v2.cachevalue;
+
+import static org.apache.blur.metrics.MetricsConstants.CACHE;
+import static org.apache.blur.metrics.MetricsConstants.DETACHES;
+import static org.apache.blur.metrics.MetricsConstants.ORG_APACHE_BLUR;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.blur.store.blockcache_v2.CacheValue;
+
+import com.yammer.metrics.Metrics;
+import com.yammer.metrics.core.Meter;
+import com.yammer.metrics.core.MetricName;
+
+public class DetachableCacheValue implements CacheValue {
+
+  private static final Meter _detaches;
+
+  static {
+    _detaches = Metrics.newMeter(new MetricName(ORG_APACHE_BLUR, CACHE, DETACHES), DETACHES,
TimeUnit.SECONDS);
+  }
+  
+  private volatile CacheValue _baseCacheValue;
+  
+  public DetachableCacheValue(CacheValue cacheValue) {
+    _baseCacheValue = cacheValue;
+  }
+
+  @Override
+  public CacheValue detachFromCache() {
+    _detaches.mark();
+    CacheValue result = _baseCacheValue;
+    int length = _baseCacheValue.length();
+    ByteArrayCacheValue byteArrayCacheValue = new ByteArrayCacheValue(length);
+    _baseCacheValue.read(0, byteArrayCacheValue._buffer, 0, length);
+    _baseCacheValue = byteArrayCacheValue;
+    return result;
+  }
+
+  @Override
+  public int length() {
+    return _baseCacheValue.length();
+  }
+
+  @Override
+  public void write(int position, byte[] buf, int offset, int length) {
+    _baseCacheValue.write(position, buf, offset, length);
+  }
+
+  @Override
+  public void read(int position, byte[] buf, int offset, int length) {
+    _baseCacheValue.read(position, buf, offset, length);
+  }
+
+  @Override
+  public byte read(int position) {
+    return _baseCacheValue.read(position);
+  }
+
+  @Override
+  public void release() {
+    _baseCacheValue.release();
+  }
+
+  @Override
+  public short readShort(int position) {
+    return _baseCacheValue.readShort(position);
+  }
+
+  @Override
+  public int readInt(int position) {
+    return _baseCacheValue.readInt(position);
+  }
+
+  @Override
+  public long readLong(int position) {
+    return _baseCacheValue.readLong(position);
+  }
+
+  @Override
+  public CacheValue trim(int length) {
+    return this;
+  }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/UnsafeCacheValue.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/UnsafeCacheValue.java
b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/UnsafeCacheValue.java
index 603fb11..5aa4436 100644
--- a/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/UnsafeCacheValue.java
+++ b/blur-store/src/main/java/org/apache/blur/store/blockcache_v2/cachevalue/UnsafeCacheValue.java
@@ -25,14 +25,12 @@ import java.lang.reflect.Field;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.blur.metrics.AtomicLongGauge;
-import org.apache.blur.store.blockcache_v2.CacheValue;
 
 import sun.misc.Unsafe;
 
 import com.yammer.metrics.Metrics;
 import com.yammer.metrics.core.MetricName;
 
-@SuppressWarnings("serial")
 public class UnsafeCacheValue extends BaseCacheValue {
 
   private static final String JAVA_NIO_BITS = "java.nio.Bits";
@@ -102,20 +100,4 @@ public class UnsafeCacheValue extends BaseCacheValue {
       new Throwable().printStackTrace();
     }
   }
-
-  @Override
-  public int size() {
-    return _capacity;
-  }
-
-  @Override
-  public CacheValue trim(int length) {
-    if (length == _capacity) {
-      return this;
-    }
-    UnsafeCacheValue unsafeCacheValue = new UnsafeCacheValue(length);
-    _unsafe.copyMemory(_address, unsafeCacheValue._address, length);
-    release();
-    return unsafeCacheValue;
-  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-store/src/test/java/org/apache/blur/store/blockcache_v2/CacheIndexInputTest.java
----------------------------------------------------------------------
diff --git a/blur-store/src/test/java/org/apache/blur/store/blockcache_v2/CacheIndexInputTest.java
b/blur-store/src/test/java/org/apache/blur/store/blockcache_v2/CacheIndexInputTest.java
index 7b8ab82..ee4b1bb 100644
--- a/blur-store/src/test/java/org/apache/blur/store/blockcache_v2/CacheIndexInputTest.java
+++ b/blur-store/src/test/java/org/apache/blur/store/blockcache_v2/CacheIndexInputTest.java
@@ -19,7 +19,6 @@ package org.apache.blur.store.blockcache_v2;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
 
 import java.io.IOException;
 import java.util.Random;
@@ -178,12 +177,7 @@ public class CacheIndexInputTest {
     EvictionListener<CacheKey, CacheValue> listener = new EvictionListener<CacheKey,
CacheValue>() {
       @Override
       public void onEviction(CacheKey key, CacheValue value) {
-        if (value.refCount() == 0) {
-          value.release();
-        } else {
-          // doing something else...
-          fail();
-        }
+        value.release();
       }
     };
     Weigher<CacheValue> weigher = new Weigher<CacheValue>() {

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/89f5a5b0/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java b/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java
index 97b1aa8..b99b304 100644
--- a/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java
+++ b/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java
@@ -21,6 +21,10 @@ package org.apache.blur.metrics;
  * 
  */
 public class MetricsConstants {
+  
+  public static final String CREATED = "Created";
+  public static final String DESTROYED = "Destroyed";
+  public static final String REUSED = "Reused";
   public static final String LUCENE = "Lucene";
   public static final String BLUR = "Blur";
   public static final String ORG_APACHE_BLUR = "org.apache.blur";
@@ -37,9 +41,11 @@ public class MetricsConstants {
   public static final String HDFS = "HDFS";
   public static final String LOCAL = "local";
   public static final String HIT = "Hit";
+  public static final String DETACHES = "Detaches";
   public static final String REMOVAL = "Removal";
   public static final String MISS = "Miss";
   public static final String CACHE = "Cache";
+  public static final String CACHE_POOL = "CachePool";
   public static final String JVM = "JVM";
   public static final String HEAP_USED = "Heap Used";
   public static final String CPU_USED = "Cpu Used";


Mime
View raw message