You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/03/30 21:00:49 UTC

[GitHub] [hive] prasanthj commented on a change in pull request #944: [Hive-22760] Adding Clock based eviction strategy.

prasanthj commented on a change in pull request #944: [Hive-22760] Adding Clock based eviction strategy.
URL: https://github.com/apache/hive/pull/944#discussion_r400416933
 
 

 ##########
 File path: llap-server/src/java/org/apache/hadoop/hive/llap/cache/ClockCachePolicy.java
 ##########
 @@ -0,0 +1,313 @@
+/*
+ * 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.hadoop.hive.llap.cache;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import org.apache.hadoop.hive.llap.io.metadata.MetadataCache;
+
+import javax.annotation.concurrent.GuardedBy;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * Clock eviction policy. Uses a simple circular list to keep a ring of current used buffers.
+ * New entries are added to tail of the clock hand AKA (clockHand.prev)
+ * Eviction start at the current clock hand following the next pointer.
+ *
+ */
+public class ClockCachePolicy implements LowLevelCachePolicy {
+
+  private static final int DEFAULT_MAX_CIRCLES = 5;
+
+  /**
+   * Lock to protect the state of the policy, used as mutex when modifying the circular linked list.
+   */
+  private final Lock listLock = new ReentrantLock();
+
+  /**
+   * The clock hand shared between threads thus made volatile, to ensure state when read outside of lock.
+   */
+  @GuardedBy("listLock")
+  private volatile LlapCacheableBuffer clockHand;
+
+  private EvictionListener evictionListener;
+  /**
+   * Max number of clock rotation before giving up on clock operation like eviction.
+   */
+  private final int maxCircles;
+
+  public ClockCachePolicy() {
+    maxCircles = DEFAULT_MAX_CIRCLES;
+  }
+
+  public ClockCachePolicy(int maxCircles) {
+    Preconditions.checkState(maxCircles > 0, "Maximum number of clock rotation must be positive and got " + maxCircles);
+    this.maxCircles = maxCircles;
+  }
+
+  /**
+   * Signals to the policy the addition of a new entry to the cache. An entry come with a priority that can be used as
+   * a hint to replacement policy.
+   *
+   * @param buffer   buffer to be cached
+   * @param priority the priority of cached element
+   */
+  @Override public void cache(LlapCacheableBuffer buffer, LowLevelCache.Priority priority) {
+    listLock.lock();
+    try {
+      //noinspection NonAtomicOperationOnVolatileField
+      clockHand = appendToCircularList(clockHand, buffer);
+    } finally {
+      listLock.unlock();
+    }
+  }
+
+  /**
+   * Appends new entry to the tail of circular list.
+   *
+   * @param head   circular list head.
+   * @param buffer new entry to be added.
+   * @return the ring head.
+   */
+  private static LlapCacheableBuffer appendToCircularList(LlapCacheableBuffer head, LlapCacheableBuffer buffer) {
 
 Review comment:
   static required?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org