You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by zh...@apache.org on 2020/08/10 03:27:13 UTC

[incubator-doris] branch master updated: Secure singleton mode (#4257)

This is an automated email from the ASF dual-hosted git repository.

zhaoc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 411ced5  Secure singleton mode (#4257)
411ced5 is described below

commit 411ced5715aec36510a5df948ab37781bea09672
Author: xinghuayu007 <14...@qq.com>
AuthorDate: Mon Aug 10 11:26:56 2020 +0800

    Secure singleton mode (#4257)
    
    Co-authored-by: wangxixu <wa...@xiaomi.com>
---
 .../java/org/apache/doris/service/ExecuteEnv.java  |  8 ++-
 .../org/apache/doris/service/ExecuteEnvTest.java   | 68 ++++++++++++++++++++++
 2 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/service/ExecuteEnv.java b/fe/fe-core/src/main/java/org/apache/doris/service/ExecuteEnv.java
old mode 100644
new mode 100755
index 4115b1e..1c4aa39
--- a/fe/fe-core/src/main/java/org/apache/doris/service/ExecuteEnv.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/service/ExecuteEnv.java
@@ -23,7 +23,7 @@ import org.apache.doris.qe.MultiLoadMgr;
 
 // Execute environment, used to save other module, need to singleton
 public class ExecuteEnv {
-    private static ExecuteEnv INSTANCE;
+    private volatile static ExecuteEnv INSTANCE;
     private MultiLoadMgr multiLoadMgr;
     private ConnectScheduler scheduler;
 
@@ -33,7 +33,11 @@ public class ExecuteEnv {
 
     public static ExecuteEnv getInstance() {
         if (INSTANCE == null) {
-            INSTANCE = new ExecuteEnv();
+            synchronized (ExecuteEnv.class) {
+                if (INSTANCE == null) {
+                    INSTANCE = new ExecuteEnv();
+                }
+            }
         }
         return INSTANCE;
     }
diff --git a/fe/fe-core/src/test/java/org/apache/doris/service/ExecuteEnvTest.java b/fe/fe-core/src/test/java/org/apache/doris/service/ExecuteEnvTest.java
new file mode 100755
index 0000000..724c27a
--- /dev/null
+++ b/fe/fe-core/src/test/java/org/apache/doris/service/ExecuteEnvTest.java
@@ -0,0 +1,68 @@
+// 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.doris.service;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.validation.constraints.AssertTrue;
+import java.util.HashSet;
+import java.util.Set;
+
+public class ExecuteEnvTest {
+    int THREAD_MAX_NUM = 10;
+    int[] oids = new int[THREAD_MAX_NUM];
+
+    @Test
+    public void testGetInstance() {
+        Set<Thread> tds = new HashSet<Thread>();
+        for (int i = 0 ;i < THREAD_MAX_NUM; i++) {
+            Thread td = new Thread(new MyTest(i, oids));
+            tds.add(td);
+            td.start();
+        }
+
+        for (Thread td : tds) {
+            try {
+                td.join();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+        for (int i = 1; i < THREAD_MAX_NUM; i++) {
+            Assert.assertEquals(oids[i-1], oids[i]);
+        }
+    }
+}
+
+class MyTest implements Runnable {
+    public int index;
+    public int[] oids;
+
+    MyTest(int index, int[] oids) {
+        this.index = index;
+        this.oids = oids;
+    }
+
+    @Override
+    public void run() {
+        ExecuteEnv instance = ExecuteEnv.getInstance();
+        int oid = instance.hashCode();
+        oids[index] = oid;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org