17
17
*/
18
18
package com .tc .config ;
19
19
20
-
21
20
import com .tc .classloader .ServiceLocator ;
22
21
import com .tc .productinfo .ProductInfo ;
23
22
import com .tc .properties .TCPropertiesImpl ;
28
27
import java .io .ByteArrayInputStream ;
29
28
import java .io .InputStream ;
30
29
import java .nio .charset .StandardCharsets ;
31
- import java .util .Collection ;
32
30
import java .util .Collections ;
33
31
import java .util .HashMap ;
34
32
import java .util .Map ;
35
33
import java .util .Objects ;
36
34
import java .util .Properties ;
37
35
import org .terracotta .configuration .ConfigurationException ;
38
36
import com .tc .text .PrettyPrintable ;
37
+ import java .io .File ;
38
+ import java .net .InetSocketAddress ;
39
39
import java .util .List ;
40
+ import java .util .concurrent .locks .Lock ;
41
+ import java .util .concurrent .locks .ReentrantLock ;
42
+ import java .util .stream .Collectors ;
43
+ import org .slf4j .Logger ;
44
+ import org .slf4j .LoggerFactory ;
40
45
41
46
public class ServerConfigurationManager implements PrettyPrintable {
42
47
43
- private final ConfigurationProvider configurationProvider ;
48
+ private final Logger LOGGER = LoggerFactory .getLogger (ServerConfigurationManager .class );
49
+ private final CachingConfigurationProvider configurationProvider ;
50
+ private ServerConfiguration thisServer ;
51
+ private GroupConfiguration groupConfig ;
44
52
private final ServiceLocator serviceLocator ;
45
53
private final List <String > startUpArgs ;
46
54
private final ProductInfo productInfo ;
47
55
48
- private Configuration configuration ;
49
- private ServerConfiguration serverConfiguration ;
50
-
51
56
public ServerConfigurationManager (ConfigurationProvider configurationProvider ,
52
- ServiceLocator classLoader ,
53
- List <String > startUpArgs ) {
57
+ ServiceLocator classLoader ,
58
+ List <String > startUpArgs ) {
54
59
Objects .requireNonNull (configurationProvider );
55
60
Objects .requireNonNull (classLoader );
56
61
Objects .requireNonNull (startUpArgs );
57
62
58
- this .configurationProvider = configurationProvider ;
63
+ this .configurationProvider = new CachingConfigurationProvider ( configurationProvider ) ;
59
64
this .serviceLocator = classLoader ;
60
65
this .startUpArgs = startUpArgs ;
61
66
this .productInfo = generateProductInfo (serviceLocator );
@@ -70,18 +75,27 @@ public ProductInfo getProductInfo() {
70
75
}
71
76
72
77
public void initialize () throws ConfigurationException {
73
- this .configurationProvider .initialize (this .startUpArgs );
74
-
75
- this .configuration = configurationProvider .getConfiguration ();
76
- if (this .configuration == null ) {
77
- throw new ConfigurationException ("unable to determine server configuration" );
78
- }
78
+ Lock lock = this .configurationProvider .lockAndInitialize (this .startUpArgs );
79
+
80
+ try {
81
+ Configuration configuration = configurationProvider .getConfiguration ();
82
+ if (configuration == null ) {
83
+ throw new ConfigurationException ("unable to determine server configuration" );
84
+ }
85
+
86
+ ServerConfiguration base = configuration .getServerConfiguration ();
87
+ if (base == null ) {
88
+ throw new ConfigurationException ("unable to determine server configuration" );
89
+ }
90
+ thisServer = new StableServerConfiguration (base );
91
+
92
+ List <ServerConfiguration > serverConfigurationMap = getConfiguration ().getServerConfigurations ().stream ().map (StableServerConfiguration ::new ).collect (Collectors .toList ());
93
+ groupConfig = new GroupConfiguration (serverConfigurationMap , getServerConfiguration ().getName ());
79
94
80
- this . serverConfiguration = this . configuration .getServerConfiguration ( );
81
- if ( this . serverConfiguration == null ) {
82
- throw new ConfigurationException ( "unable to determine server configuration" );
95
+ processTcProperties ( configuration .getTcProperties () );
96
+ } finally {
97
+ lock . unlock ( );
83
98
}
84
- processTcProperties (configuration .getTcProperties ());
85
99
}
86
100
87
101
public void close () {
@@ -93,69 +107,164 @@ public String[] getProcessArguments() {
93
107
}
94
108
95
109
public ServerConfiguration getServerConfiguration () {
96
- return this . serverConfiguration ;
110
+ return thisServer ;
97
111
}
98
112
99
113
public GroupConfiguration getGroupConfiguration () {
100
- List <ServerConfiguration > serverConfigurationMap = configuration .getServerConfigurations ();
101
- return new GroupConfiguration (serverConfigurationMap , this .serverConfiguration .getName ());
114
+ return groupConfig ;
102
115
}
103
-
116
+
104
117
public InputStream rawConfigFile () {
105
- String text = configuration .getRawConfiguration ();
118
+ String text = getConfiguration () .getRawConfiguration ();
106
119
return new ByteArrayInputStream (text .getBytes (StandardCharsets .UTF_8 ));
107
120
}
108
121
109
122
public String rawConfigString () {
110
- return configuration .getRawConfiguration ();
123
+ return getConfiguration () .getRawConfiguration ();
111
124
}
112
125
113
126
public String [] allCurrentlyKnownServers () {
114
127
return getGroupConfiguration ().getMembers ();
115
128
}
116
129
117
130
public boolean isPartialConfiguration () {
118
- return this . configuration .isPartialConfiguration ();
131
+ return getConfiguration () .isPartialConfiguration ();
119
132
}
120
133
121
134
public ServiceLocator getServiceLocator () {
122
135
return this .serviceLocator ;
123
136
}
124
137
125
138
public Configuration getConfiguration () {
126
- return configuration ;
139
+ return configurationProvider . getConfiguration () ;
127
140
}
128
141
129
142
public ConfigurationProvider getConfigurationProvider () {
130
- return configurationProvider ;
131
- }
132
-
133
- private Map <String , ServerConfiguration > getServerConfigurationMap (Collection <ServerConfiguration > servers ) {
134
- Map <String , ServerConfiguration > serverConfigurationMap = new HashMap <>();
135
- for (ServerConfiguration server : servers ) {
136
- if (server .getName () != null ) {
137
- serverConfigurationMap .put (server .getName (), server );
138
- }
139
- }
140
- return serverConfigurationMap ;
143
+ return configurationProvider .delegateProvider ;
141
144
}
142
145
143
146
private static void processTcProperties (Properties tcProperties ) {
144
147
Map <String , String > propMap = new HashMap <>();
145
148
146
149
if (tcProperties != null ) {
147
- tcProperties .forEach ((k , v )-> propMap .put (k .toString ().trim (), v .toString ().trim ()));
150
+ tcProperties .forEach ((k , v ) -> propMap .put (k .toString ().trim (), v .toString ().trim ()));
148
151
}
149
152
150
153
TCPropertiesImpl .getProperties ().overwriteTcPropertiesFromConfig (propMap );
151
154
}
152
155
153
156
@ Override
154
157
public Map <String , ?> getStateMap () {
155
- if (configuration instanceof PrettyPrintable ) {
156
- return ((PrettyPrintable )configuration ).getStateMap ();
158
+ Configuration config = getConfiguration ();
159
+ if (config instanceof PrettyPrintable ) {
160
+ return ((PrettyPrintable ) config ).getStateMap ();
157
161
} else {
158
162
return Collections .emptyMap ();
159
163
}
160
164
}
165
+
166
+ private static final class CachingConfigurationProvider implements ConfigurationProvider {
167
+
168
+ private final ConfigurationProvider delegateProvider ;
169
+ private final Lock lock = new ReentrantLock ();
170
+
171
+ public CachingConfigurationProvider (ConfigurationProvider delegateProvider ) {
172
+ this .delegateProvider = delegateProvider ;
173
+ }
174
+
175
+ Lock lockAndInitialize (List <String > configurationParams ) throws ConfigurationException {
176
+ lock .lock ();
177
+ initialize (configurationParams );
178
+ return lock ;
179
+ }
180
+
181
+ @ Override
182
+ public void initialize (List <String > configurationParams ) throws ConfigurationException {
183
+ delegateProvider .initialize (configurationParams );
184
+ }
185
+
186
+ @ Override
187
+ public Configuration getConfiguration () {
188
+ lock .lock ();
189
+ try {
190
+ return delegateProvider .getConfiguration ();
191
+ } finally {
192
+ lock .unlock ();
193
+ }
194
+ }
195
+
196
+ @ Override
197
+ public String getConfigurationParamsDescription () {
198
+ return delegateProvider .getConfigurationParamsDescription ();
199
+ }
200
+
201
+ @ Override
202
+ public void close () {
203
+ delegateProvider .close ();
204
+ }
205
+
206
+ @ Override
207
+ public byte [] getSyncData () {
208
+ return delegateProvider .getSyncData ();
209
+ }
210
+
211
+ @ Override
212
+ public void sync (byte [] syncData ) {
213
+ lock .lock ();
214
+ try {
215
+ delegateProvider .sync (syncData );
216
+ } finally {
217
+ lock .unlock ();
218
+ }
219
+ }
220
+ }
221
+
222
+ private static class StableServerConfiguration implements ServerConfiguration {
223
+
224
+ private final InetSocketAddress tsaPort ;
225
+ private final InetSocketAddress groupPort ;
226
+ private final String host ;
227
+ private final String name ;
228
+ private final int reconnectWindow ;
229
+ private final File logDir ;
230
+
231
+ public StableServerConfiguration (ServerConfiguration base ) {
232
+ tsaPort = base .getTsaPort ();
233
+ groupPort = base .getGroupPort ();
234
+ host = base .getHost ();
235
+ name = base .getName ();
236
+ reconnectWindow = base .getClientReconnectWindow ();
237
+ logDir = base .getLogsLocation ();
238
+ }
239
+
240
+ @ Override
241
+ public InetSocketAddress getTsaPort () {
242
+ return tsaPort ;
243
+ }
244
+
245
+ @ Override
246
+ public InetSocketAddress getGroupPort () {
247
+ return groupPort ;
248
+ }
249
+
250
+ @ Override
251
+ public String getHost () {
252
+ return host ;
253
+ }
254
+
255
+ @ Override
256
+ public String getName () {
257
+ return name ;
258
+ }
259
+
260
+ @ Override
261
+ public int getClientReconnectWindow () {
262
+ return reconnectWindow ;
263
+ }
264
+
265
+ @ Override
266
+ public File getLogsLocation () {
267
+ return logDir ;
268
+ }
269
+ }
161
270
}
0 commit comments