incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [11/13] git commit: Removing thread local variables.
Date Tue, 30 Aug 2016 01:57:56 GMT
http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/991fb043/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java
----------------------------------------------------------------------
diff --git a/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java b/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java
index 8c0e9ba..766cc59 100644
--- a/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java
+++ b/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java
@@ -50,6 +50,7 @@ import org.apache.blur.trace.Trace;
 import org.apache.blur.trace.Trace.TraceId;
 import org.apache.blur.trace.Tracer;
 import org.apache.blur.user.UserContext;
+import org.apache.blur.utils.ThreadValue;
 
 public class BlurClientManager {
 
@@ -141,7 +142,7 @@ public class BlurClientManager {
     List<Connection> shuffledConnections = new ArrayList<Connection>();
   }
 
-  private static ThreadLocal<Random> _random = new ThreadLocal<Random>() {
+  private static ThreadValue<Random> _random = new ThreadValue<Random>() {
     @Override
     protected Random initialValue() {
       return new Random();

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/991fb043/blur-thrift/src/main/java/org/apache/blur/thrift/sasl/TSaslTransport.java
----------------------------------------------------------------------
diff --git a/blur-thrift/src/main/java/org/apache/blur/thrift/sasl/TSaslTransport.java b/blur-thrift/src/main/java/org/apache/blur/thrift/sasl/TSaslTransport.java
index 3cf80a4..15361f0 100644
--- a/blur-thrift/src/main/java/org/apache/blur/thrift/sasl/TSaslTransport.java
+++ b/blur-thrift/src/main/java/org/apache/blur/thrift/sasl/TSaslTransport.java
@@ -38,6 +38,7 @@ import org.apache.blur.thirdparty.thrift_0_9_0.transport.TMemoryInputTransport;
 import org.apache.blur.thirdparty.thrift_0_9_0.transport.TSocket;
 import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransport;
 import org.apache.blur.thirdparty.thrift_0_9_0.transport.TTransportException;
+import org.apache.blur.utils.ThreadValue;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -311,7 +312,7 @@ abstract class TSaslTransport extends TTransport {
     _currentConnection.set(null);
   }
 
-  static ThreadLocal<InetSocketAddress> _currentConnection = new ThreadLocal<InetSocketAddress>();
+  static ThreadValue<InetSocketAddress> _currentConnection = new ThreadValue<InetSocketAddress>();
 
   private void setupConnectionInfo() {
     if (underlyingTransport instanceof TSocket) {

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/991fb043/blur-util/src/main/java/org/apache/blur/trace/Trace.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/trace/Trace.java b/blur-util/src/main/java/org/apache/blur/trace/Trace.java
index 65c2bfa..8f4bbce 100644
--- a/blur-util/src/main/java/org/apache/blur/trace/Trace.java
+++ b/blur-util/src/main/java/org/apache/blur/trace/Trace.java
@@ -27,6 +27,7 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
+import org.apache.blur.utils.ThreadValue;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -77,10 +78,10 @@ public class Trace {
 
     }
   };
-  private static ThreadLocal<TraceCollector> _tracer = new ThreadLocal<TraceCollector>();
+  private static ThreadValue<TraceCollector> _tracer = new ThreadValue<TraceCollector>();
   private static TraceStorage _storage;
   private static String _nodeName;
-  private static ThreadLocal<Random> _random = new ThreadLocal<Random>() {
+  private static ThreadValue<Random> _random = new ThreadValue<Random>() {
     @Override
     protected Random initialValue() {
       return new Random();

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/991fb043/blur-util/src/main/java/org/apache/blur/user/UserContext.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/user/UserContext.java b/blur-util/src/main/java/org/apache/blur/user/UserContext.java
index 6ae373b..1b4fb1a 100644
--- a/blur-util/src/main/java/org/apache/blur/user/UserContext.java
+++ b/blur-util/src/main/java/org/apache/blur/user/UserContext.java
@@ -19,6 +19,8 @@ package org.apache.blur.user;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.blur.utils.ThreadValue;
+
 public class UserContext {
 
   private static User _defaultUser;
@@ -45,7 +47,7 @@ public class UserContext {
     return new User(user.getUsername(), null);
   }
 
-  private static ThreadLocal<User> _user = new ThreadLocal<User>() {
+  private static ThreadValue<User> _user = new ThreadValue<User>() {
     @Override
     protected User initialValue() {
       return getDefaultUser();

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/991fb043/blur-util/src/main/java/org/apache/blur/utils/ThreadValue.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/utils/ThreadValue.java b/blur-util/src/main/java/org/apache/blur/utils/ThreadValue.java
new file mode 100644
index 0000000..80c59b6
--- /dev/null
+++ b/blur-util/src/main/java/org/apache/blur/utils/ThreadValue.java
@@ -0,0 +1,56 @@
+/**
+ * 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.utils;
+
+import java.util.concurrent.ConcurrentMap;
+
+import com.google.common.collect.MapMaker;
+
+public class ThreadValue<T> {
+
+  static class Value<T> {
+    T value;
+
+    Value(T value) {
+      this.value = value;
+    }
+  }
+
+  private final ConcurrentMap<Thread, Value<T>> refs = new MapMaker().weakKeys().makeMap();
+
+  protected T initialValue() {
+    return null;
+  }
+
+  public T get() {
+    Value<T> value = refs.get(Thread.currentThread());
+    if (value == null) {
+      refs.put(Thread.currentThread(), value = new Value<>(initialValue()));
+    }
+    return value.value;
+  }
+
+  public void set(T v) {
+    Value<T> value = refs.get(Thread.currentThread());
+    if (value == null) {
+      refs.put(Thread.currentThread(), value = new Value<>(v));
+    } else {
+      value.value = v;
+    }
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/991fb043/blur-util/src/test/java/org/apache/blur/utils/BlurConstantsTest.java
----------------------------------------------------------------------
diff --git a/blur-util/src/test/java/org/apache/blur/utils/BlurConstantsTest.java b/blur-util/src/test/java/org/apache/blur/utils/BlurConstantsTest.java
index 25b5967..139afb2 100644
--- a/blur-util/src/test/java/org/apache/blur/utils/BlurConstantsTest.java
+++ b/blur-util/src/test/java/org/apache/blur/utils/BlurConstantsTest.java
@@ -43,7 +43,7 @@ public class BlurConstantsTest {
         "DELETE_MARKER_VALUE", "DELETE_MARKER", "BLUR_ZOOKEEPER_TIMEOUT_DEFAULT", "BLUR_THRIFT_DEFAULT_MAX_FRAME_SIZE",
         "ZK_WAIT_TIME", "ACL_DISCOVER", "ACL_READ", "FAST_DECOMPRESSION", "FAST", "HIGH_COMPRESSION",
"DEFAULT_VALUE",
         "OFF_HEAP", "DEFAULT", "BLUR_CLUSTER", "BLUR_NODENAME", "BLUR_HTTP_STATUS_RUNNING_PORT",
-        "BLUR_STREAM_SERVER_RUNNING_PORT", "SHARED_MERGE_SCHEDULER_PREFIX"));
+        "BLUR_STREAM_SERVER_RUNNING_PORT", "SHARED_MERGE_SCHEDULER_PREFIX", "UPDATE_ROW",
"NEW_ROW", "INTERNAL"));
     _emptyDefaultProperties = new HashSet<String>();
     _emptyDefaultProperties.addAll(Arrays.asList("BLUR_HDFS_TRACE_PATH", "BLUR_SHARD_HOSTNAME",
         "BLUR_SHARD_BLOCK_CACHE_TOTAL_SIZE", "BLUR_CONTROLLER_HOSTNAME", "BLUR_SHARD_READ_INTERCEPTOR",


Mime
View raw message