@@ -50,30 +50,41 @@ public class KylinConfig extends KylinConfigBase {
50
50
public static final String KYLIN_CONF = "KYLIN_CONF" ;
51
51
52
52
// static cached instances
53
- private static KylinConfig ENV_INSTANCE = null ;
53
+ private static KylinConfig SYS_ENV_INSTANCE = null ;
54
+
55
+ // thread-local instances, will override SYS_ENV_INSTANCE
56
+ private static final transient ThreadLocal <KylinConfig > THREAD_ENV_INSTANCE = new ThreadLocal <>();
54
57
55
58
public static KylinConfig getInstanceFromEnv () {
56
59
synchronized (KylinConfig .class ) {
57
- if (ENV_INSTANCE == null ) {
60
+ KylinConfig config = THREAD_ENV_INSTANCE .get ();
61
+ if (config != null ) {
62
+ return config ;
63
+ }
64
+
65
+ if (SYS_ENV_INSTANCE == null ) {
58
66
try {
59
- KylinConfig config = new KylinConfig ();
67
+ config = new KylinConfig ();
60
68
config .reloadKylinConfig (getKylinProperties ());
61
69
62
70
logger .info ("Initialized a new KylinConfig from getInstanceFromEnv : " + System .identityHashCode (config ));
63
- ENV_INSTANCE = config ;
71
+ SYS_ENV_INSTANCE = config ;
64
72
} catch (IllegalArgumentException e ) {
65
73
throw new IllegalStateException ("Failed to find KylinConfig " , e );
66
74
}
67
75
}
68
- return ENV_INSTANCE ;
76
+ return SYS_ENV_INSTANCE ;
69
77
}
70
78
}
71
79
72
80
//Only used in test cases!!!
73
81
public static void destroyInstance () {
74
- logger .info ("Destory KylinConfig" );
75
- dumpStackTrace ();
76
- ENV_INSTANCE = null ;
82
+ synchronized (KylinConfig .class ) {
83
+ logger .info ("Destroy KylinConfig" );
84
+ dumpStackTrace ();
85
+ SYS_ENV_INSTANCE = null ;
86
+ THREAD_ENV_INSTANCE .remove ();
87
+ }
77
88
}
78
89
79
90
public enum UriType {
@@ -158,12 +169,12 @@ private static Properties streamToProps(InputStream is) throws IOException {
158
169
159
170
public static void setKylinConfigInEnvIfMissing (Properties prop ) {
160
171
synchronized (KylinConfig .class ) {
161
- if (ENV_INSTANCE == null ) {
172
+ if (SYS_ENV_INSTANCE == null ) {
162
173
try {
163
174
KylinConfig config = new KylinConfig ();
164
175
config .reloadKylinConfig (prop );
165
- logger .info ("Resetting ENV_INSTANCE by a input stream: " + System .identityHashCode (config ));
166
- ENV_INSTANCE = config ;
176
+ logger .info ("Resetting SYS_ENV_INSTANCE by a input stream: " + System .identityHashCode (config ));
177
+ SYS_ENV_INSTANCE = config ;
167
178
} catch (IllegalArgumentException e ) {
168
179
throw new IllegalStateException ("Failed to find KylinConfig " , e );
169
180
}
@@ -177,6 +188,10 @@ public static void setKylinConfigInEnvIfMissing(String propsInStr) throws IOExce
177
188
setKylinConfigInEnvIfMissing (props );
178
189
}
179
190
191
+ public static void setKylinConfigThreadLocal (KylinConfig config ) {
192
+ THREAD_ENV_INSTANCE .set (config );
193
+ }
194
+
180
195
public static KylinConfig createKylinConfig (String propsInStr ) throws IOException {
181
196
Properties props = new Properties ();
182
197
props .load (new StringReader (propsInStr ));
0 commit comments