author | chegar |
Fri, 18 Oct 2019 21:25:01 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58697 | e3ff12d14d43 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net; |
|
27 |
||
28 |
import java.security.PrivilegedAction; |
|
29 |
import java.security.Security; |
|
30 |
||
31 |
public final class InetAddressCachePolicy { |
|
32 |
||
33 |
// Controls the cache policy for successful lookups only |
|
34 |
private static final String cachePolicyProp = "networkaddress.cache.ttl"; |
|
35 |
private static final String cachePolicyPropFallback = |
|
36 |
"sun.net.inetaddr.ttl"; |
|
37 |
||
38 |
// Controls the cache policy for negative lookups only |
|
39 |
private static final String negativeCachePolicyProp = |
|
40 |
"networkaddress.cache.negative.ttl"; |
|
41 |
private static final String negativeCachePolicyPropFallback = |
|
42 |
"sun.net.inetaddr.negative.ttl"; |
|
43 |
||
44 |
public static final int FOREVER = -1; |
|
45 |
public static final int NEVER = 0; |
|
46 |
||
47 |
/* default value for positive lookups */ |
|
48 |
public static final int DEFAULT_POSITIVE = 30; |
|
49 |
||
50 |
/* The Java-level namelookup cache policy for successful lookups: |
|
51 |
* |
|
52 |
* -1: caching forever |
|
53 |
* any positive value: the number of seconds to cache an address for |
|
54 |
* |
|
55 |
* default value is forever (FOREVER), as we let the platform do the |
|
56 |
* caching. For security reasons, this caching is made forever when |
|
57 |
* a security manager is set. |
|
58 |
*/ |
|
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
59 |
private static volatile int cachePolicy = FOREVER; |
2 | 60 |
|
61 |
/* The Java-level namelookup cache policy for negative lookups: |
|
62 |
* |
|
63 |
* -1: caching forever |
|
64 |
* any positive value: the number of seconds to cache an address for |
|
65 |
* |
|
66 |
* default value is 0. It can be set to some other value for |
|
67 |
* performance reasons. |
|
68 |
*/ |
|
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
69 |
private static volatile int negativeCachePolicy = NEVER; |
2 | 70 |
|
71 |
/* |
|
72 |
* Whether or not the cache policy for successful lookups was set |
|
73 |
* using a property (cmd line). |
|
74 |
*/ |
|
7023
9509e6a18f6e
6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents:
5506
diff
changeset
|
75 |
private static boolean propertySet; |
2 | 76 |
|
77 |
/* |
|
78 |
* Whether or not the cache policy for negative lookups was set |
|
79 |
* using a property (cmd line). |
|
80 |
*/ |
|
7023
9509e6a18f6e
6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents:
5506
diff
changeset
|
81 |
private static boolean propertyNegativeSet; |
2 | 82 |
|
83 |
/* |
|
84 |
* Initialize |
|
85 |
*/ |
|
86 |
static { |
|
24041
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
87 |
|
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
88 |
Integer tmp = java.security.AccessController.doPrivileged( |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
89 |
new PrivilegedAction<Integer>() { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
90 |
public Integer run() { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
91 |
try { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
92 |
String tmpString = Security.getProperty(cachePolicyProp); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
93 |
if (tmpString != null) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
94 |
return Integer.valueOf(tmpString); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
95 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
96 |
} catch (NumberFormatException ignored) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
97 |
// Ignore |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
98 |
} |
2 | 99 |
|
24041
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
100 |
try { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
101 |
String tmpString = System.getProperty(cachePolicyPropFallback); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
102 |
if (tmpString != null) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
103 |
return Integer.decode(tmpString); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
104 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
105 |
} catch (NumberFormatException ignored) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
106 |
// Ignore |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
107 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
108 |
return null; |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
109 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
110 |
}); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
111 |
|
2 | 112 |
if (tmp != null) { |
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
113 |
cachePolicy = tmp < 0 ? FOREVER : tmp; |
7023
9509e6a18f6e
6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents:
5506
diff
changeset
|
114 |
propertySet = true; |
2 | 115 |
} else { |
24041
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
116 |
/* No properties defined for positive caching. If there is no |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
117 |
* security manager then use the default positive cache value. |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
118 |
*/ |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
119 |
if (System.getSecurityManager() == null) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
120 |
cachePolicy = DEFAULT_POSITIVE; |
2 | 121 |
} |
122 |
} |
|
24041
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
123 |
tmp = java.security.AccessController.doPrivileged ( |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
124 |
new PrivilegedAction<Integer>() { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
125 |
public Integer run() { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
126 |
try { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
127 |
String tmpString = Security.getProperty(negativeCachePolicyProp); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
128 |
if (tmpString != null) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
129 |
return Integer.valueOf(tmpString); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
130 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
131 |
} catch (NumberFormatException ignored) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
132 |
// Ignore |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
133 |
} |
2 | 134 |
|
24041
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
135 |
try { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
136 |
String tmpString = System.getProperty(negativeCachePolicyPropFallback); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
137 |
if (tmpString != null) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
138 |
return Integer.decode(tmpString); |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
139 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
140 |
} catch (NumberFormatException ignored) { |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
141 |
// Ignore |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
142 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
143 |
return null; |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
144 |
} |
2bb2c08c958b
8040837: Avoid provoking NFEs when initializing InetAddrCachePolicy
mduigou
parents:
7668
diff
changeset
|
145 |
}); |
2 | 146 |
|
147 |
if (tmp != null) { |
|
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
148 |
negativeCachePolicy = tmp < 0 ? FOREVER : tmp; |
7023
9509e6a18f6e
6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents:
5506
diff
changeset
|
149 |
propertyNegativeSet = true; |
2 | 150 |
} |
151 |
} |
|
152 |
||
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
153 |
public static int get() { |
7023
9509e6a18f6e
6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents:
5506
diff
changeset
|
154 |
return cachePolicy; |
2 | 155 |
} |
156 |
||
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
157 |
public static int getNegative() { |
2 | 158 |
return negativeCachePolicy; |
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Sets the cache policy for successful lookups if the user has not |
|
163 |
* already specified a cache policy for it using a |
|
164 |
* command-property. |
|
165 |
* @param newPolicy the value in seconds for how long the lookup |
|
166 |
* should be cached |
|
167 |
*/ |
|
168 |
public static synchronized void setIfNotSet(int newPolicy) { |
|
169 |
/* |
|
170 |
* When setting the new value we may want to signal that the |
|
171 |
* cache should be flushed, though this doesn't seem strictly |
|
172 |
* necessary. |
|
173 |
*/ |
|
7023
9509e6a18f6e
6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents:
5506
diff
changeset
|
174 |
if (!propertySet) { |
2 | 175 |
checkValue(newPolicy, cachePolicy); |
176 |
cachePolicy = newPolicy; |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Sets the cache policy for negative lookups if the user has not |
|
182 |
* already specified a cache policy for it using a |
|
183 |
* command-property. |
|
184 |
* @param newPolicy the value in seconds for how long the lookup |
|
185 |
* should be cached |
|
186 |
*/ |
|
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
187 |
public static void setNegativeIfNotSet(int newPolicy) { |
2 | 188 |
/* |
189 |
* When setting the new value we may want to signal that the |
|
190 |
* cache should be flushed, though this doesn't seem strictly |
|
191 |
* necessary. |
|
192 |
*/ |
|
7023
9509e6a18f6e
6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents:
5506
diff
changeset
|
193 |
if (!propertyNegativeSet) { |
2 | 194 |
// Negative caching does not seem to have any security |
195 |
// implications. |
|
196 |
// checkValue(newPolicy, negativeCachePolicy); |
|
26199
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
197 |
// but we should normalize negative policy |
f30d82a2c06f
8049228: Improve multithreaded scalability of InetAddress cache
plevart
parents:
25859
diff
changeset
|
198 |
negativeCachePolicy = newPolicy < 0 ? FOREVER : newPolicy; |
2 | 199 |
} |
200 |
} |
|
201 |
||
202 |
private static void checkValue(int newPolicy, int oldPolicy) { |
|
203 |
/* |
|
204 |
* If malicious code gets a hold of this method, prevent |
|
205 |
* setting the cache policy to something laxer or some |
|
206 |
* invalid negative value. |
|
207 |
*/ |
|
208 |
if (newPolicy == FOREVER) |
|
209 |
return; |
|
210 |
||
211 |
if ((oldPolicy == FOREVER) || |
|
212 |
(newPolicy < oldPolicy) || |
|
213 |
(newPolicy < FOREVER)) { |
|
214 |
||
215 |
throw new |
|
216 |
SecurityException("can't make InetAddress cache more lax"); |
|
217 |
} |
|
218 |
} |
|
219 |
} |