You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2020/05/15 18:37:22 UTC

[GitHub] [phoenix-queryserver] joshelser commented on a change in pull request #33: PHOENIX-5826 Remove guava from queryserver

joshelser commented on a change in pull request #33:
URL: https://github.com/apache/phoenix-queryserver/pull/33#discussion_r425975849



##########
File path: queryserver/src/main/java/org/apache/phoenix/util/HostAndPort.java
##########
@@ -0,0 +1,284 @@
+/*
+ * Copyright (C) 2011 The Guava Authors

Review comment:
       We should ensure that a top-level NOTICE file propagates this copyright notice, as well as the containing jar.
   
   the ASF parent pom already has convention to append entries to the NOTICE file. One example https://github.com/apache/hbase/blob/master/hbase-thrift/src/main/appended-resources/META-INF/NOTICE. The "src/main/appended-resources" is the magic set up by asf.pom.

##########
File path: queryserver/src/main/java/org/apache/phoenix/util/HostAndPort.java
##########
@@ -0,0 +1,284 @@
+/*
+ * Copyright (C) 2011 The Guava Authors
+ *
+ * Licensed 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.phoenix.util;
+
+import java.io.Serializable;
+import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.annotation.concurrent.Immutable;
+
+/**
+ * An immutable representation of a host and port.
+ *
+ * <p>Example usage:
+ * <pre>
+ * HostAndPort hp = HostAndPort.fromString("[2001:db8::1]")
+ *     .withDefaultPort(80)
+ *     .requireBracketsForIPv6();
+ * hp.getHostText();  // returns "2001:db8::1"
+ * hp.getPort();      // returns 80
+ * hp.toString();     // returns "[2001:db8::1]:80"
+ * </pre>
+ *
+ * <p>Here are some examples of recognized formats:
+ * <ul>
+ *   <li>example.com
+ *   <li>example.com:80
+ *   <li>192.0.2.1
+ *   <li>192.0.2.1:80
+ *   <li>[2001:db8::1]     - {@link #getHostText()} omits brackets
+ *   <li>[2001:db8::1]:80  - {@link #getHostText()} omits brackets
+ *   <li>2001:db8::1       - Use {@link #requireBracketsForIPv6()} to prohibit this
+ * </ul>
+ *
+ * <p>Note that this is not an exhaustive list, because these methods are only
+ * concerned with brackets, colons, and port numbers.  Full validation of the
+ * host field (if desired) is the caller's responsibility.
+ *
+ * @author Paul Marks
+ * @since 10.0

Review comment:
       Can we include a comment somewhere in here which states what version of Guava this was copied from?

##########
File path: queryserver/src/main/java/org/apache/phoenix/util/SimpleLRUCache.java
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.phoenix.util;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Minimal Cache implementation based on ConcurrentHashMap.
+ * 
+ * The maxSize logic will only work if all access is via the the computeIfAbsent() method.
+ *
+ */
+public class SimpleLRUCache <K,V> extends ConcurrentHashMap<K,V> {
+
+    protected static final Logger LOG = LoggerFactory.getLogger(SimpleLRUCache.class);
+
+    int maxSize;
+    int triggerSize;
+
+    private ConcurrentHashMap<Object, AtomicLong> accessed =
+            new ConcurrentHashMap<>();
+
+    public SimpleLRUCache (long maxSize, int concurrencyLevel) {
+        super((int)(maxSize * 1.1), (float)0.75, concurrencyLevel);
+        this.maxSize = (int)maxSize;
+        this.triggerSize = (int)(maxSize * 1.1)+1 ;
+    }
+
+    @Override
+    public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) {
+        V value = super.computeIfAbsent(key, mappingFunction);
+        if (value != null) {
+            accessed.put(key, new AtomicLong(System.currentTimeMillis()));
+            if (this.size() > triggerSize) {
+                evict();
+            }
+        }
+        return value;
+    }
+
+    private void evict() {
+        synchronized(this) {
+            int currentSize = this.size();
+            if (currentSize <= triggerSize) {
+                return;
+            }
+            LOG.warn("UGI Cache capacity exceeded, you may want to increase its size");
+            TreeSet<Entry<Object, AtomicLong>> sortedByLRU = new TreeSet<>(
+                    new Comparator<Entry<Object, AtomicLong>>() {
+                        @Override
+                        public int compare(Entry<Object, AtomicLong> o1, 
+                                Entry<Object, AtomicLong> o2) {
+                            if( Long.compare(o2.getValue().get(), o1.getValue().get()) == 0) {

Review comment:
       Compute this once, and use it for your `if` and `return`




----------------------------------------------------------------
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