-
Notifications
You must be signed in to change notification settings - Fork 612
/
GlobalComponentRegistry.java
287 lines (242 loc) · 10.9 KB
/
GlobalComponentRegistry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package org.infinispan.factories;
import net.jcip.annotations.ThreadSafe;
import org.infinispan.Version;
import org.infinispan.commands.module.ModuleCommandFactory;
import org.infinispan.commands.module.ModuleCommandInitializer;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.util.InfinispanCollections;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.configuration.global.ShutdownHookBehavior;
import org.infinispan.factories.annotations.SurvivesRestarts;
import org.infinispan.factories.components.ComponentMetadataRepo;
import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.jmx.CacheManagerJmxRegistration;
import org.infinispan.lifecycle.ComponentStatus;
import org.infinispan.lifecycle.ModuleLifecycle;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.manager.EmbeddedCacheManagerStartupException;
import org.infinispan.notifications.cachemanagerlistener.CacheManagerNotifier;
import org.infinispan.notifications.cachemanagerlistener.CacheManagerNotifierImpl;
import org.infinispan.persistence.factory.CacheStoreFactoryRegistry;
import org.infinispan.registry.ClusterRegistry;
import org.infinispan.registry.InternalCacheRegistry;
import org.infinispan.registry.impl.ClusterRegistryImpl;
import org.infinispan.registry.impl.InternalCacheRegistryImpl;
import org.infinispan.remoting.transport.Transport;
import org.infinispan.topology.ClusterTopologyManager;
import org.infinispan.topology.LocalTopologyManager;
import org.infinispan.util.ModuleProperties;
import org.infinispan.util.TimeService;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.infinispan.xsite.GlobalXSiteAdminOperations;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* A global component registry where shared components are stored.
*
* @author Manik Surtani
* @since 4.0
*/
@Scope(Scopes.GLOBAL)
@SurvivesRestarts
@ThreadSafe
public class GlobalComponentRegistry extends AbstractComponentRegistry {
private static final Log log = LogFactory.getLog(GlobalComponentRegistry.class);
private static volatile boolean versionLogged = false;
/**
* Hook to shut down the cache when the JVM exits.
*/
private Thread shutdownHook;
/**
* A flag that the shutdown hook sets before calling cache.stop(). Allows stop() to identify if it has been called
* from a shutdown hook.
*/
private boolean invokedFromShutdownHook;
private final GlobalConfiguration globalConfiguration;
/**
* Tracking set of created caches in order to make it easy to remove a cache on remote nodes.
*/
private final Set<String> createdCaches;
private final ModuleProperties moduleProperties = new ModuleProperties();
private final ComponentMetadataRepo componentMetadataRepo;
final Collection<ModuleLifecycle> moduleLifecycles;
final ConcurrentMap<String, ComponentRegistry> namedComponents = new ConcurrentHashMap<String, ComponentRegistry>(4);
protected final WeakReference<ClassLoader> defaultClassLoader;
/**
* Creates an instance of the component registry. The configuration passed in is automatically registered.
*
* @param configuration configuration with which this is created
*/
public GlobalComponentRegistry(GlobalConfiguration configuration,
EmbeddedCacheManager cacheManager,
Set<String> createdCaches) {
ClassLoader configuredClassLoader = configuration.classLoader();
moduleLifecycles = moduleProperties.resolveModuleLifecycles(configuredClassLoader);
componentMetadataRepo = new ComponentMetadataRepo();
// Load up the component metadata
componentMetadataRepo.initialize(moduleProperties.getModuleMetadataFiles(configuredClassLoader), configuredClassLoader);
defaultClassLoader = new WeakReference<ClassLoader>(registerDefaultClassLoader(configuredClassLoader));
try {
// this order is important ...
globalConfiguration = configuration;
registerComponent(this, GlobalComponentRegistry.class);
registerComponent(configuration, GlobalConfiguration.class);
registerComponent(cacheManager, EmbeddedCacheManager.class);
registerComponent(new CacheManagerJmxRegistration(), CacheManagerJmxRegistration.class);
registerComponent(new CacheManagerNotifierImpl(), CacheManagerNotifier.class);
registerComponent(new InternalCacheRegistryImpl(), InternalCacheRegistry.class);
registerComponent(new ClusterRegistryImpl(), ClusterRegistry.class);
registerComponent(new CacheStoreFactoryRegistry(), CacheStoreFactoryRegistry.class);
registerComponent(new GlobalXSiteAdminOperations(), GlobalXSiteAdminOperations.class);
moduleProperties.loadModuleCommandHandlers(configuredClassLoader);
Map<Byte, ModuleCommandFactory> factories = moduleProperties.moduleCommandFactories();
if (factories != null && !factories.isEmpty())
registerNonVolatileComponent(factories, KnownComponentNames.MODULE_COMMAND_FACTORIES);
else
registerNonVolatileComponent(
InfinispanCollections.<Object, Object>emptyMap(), KnownComponentNames.MODULE_COMMAND_FACTORIES);
this.createdCaches = createdCaches;
// This is necessary to make sure the transport has been started and is available to other components that
// may need it. This is a messy approach though - a proper fix will be in ISPN-1698
getOrCreateComponent(Transport.class);
// These two should not be necessary, but they are here as a workaround for ISPN-2371
getOrCreateComponent(LocalTopologyManager.class);
getOrCreateComponent(ClusterTopologyManager.class);
} catch (Exception e) {
throw new CacheException("Unable to construct a GlobalComponentRegistry!", e);
}
}
@Override
protected ClassLoader getClassLoader() {
return defaultClassLoader.get();
}
@Override
protected Log getLog() {
return log;
}
@Override
public ComponentMetadataRepo getComponentMetadataRepo() {
return componentMetadataRepo;
}
@Override
protected synchronized void removeShutdownHook() {
// if this is called from a source other than the shutdown hook, de-register the shutdown hook.
if (!invokedFromShutdownHook && shutdownHook != null) Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
@Override
public TimeService getTimeService() {
return getOrCreateComponent(TimeService.class);
}
@Override
protected synchronized void addShutdownHook() {
ArrayList<MBeanServer> al = MBeanServerFactory.findMBeanServer(null);
ShutdownHookBehavior shutdownHookBehavior = globalConfiguration.shutdown().hookBehavior();
boolean registerShutdownHook = (shutdownHookBehavior == ShutdownHookBehavior.DEFAULT && al.isEmpty())
|| shutdownHookBehavior == ShutdownHookBehavior.REGISTER;
if (registerShutdownHook) {
log.tracef("Registering a shutdown hook. Configured behavior = %s", shutdownHookBehavior);
shutdownHook = new Thread() {
@Override
public void run() {
try {
invokedFromShutdownHook = true;
GlobalComponentRegistry.this.stop();
} finally {
invokedFromShutdownHook = false;
}
}
};
Runtime.getRuntime().addShutdownHook(shutdownHook);
} else {
log.tracef("Not registering a shutdown hook. Configured behavior = %s", shutdownHookBehavior);
}
}
public final ComponentRegistry getNamedComponentRegistry(String name) {
//no need so sync this method as namedComponents is thread safe and correctly published (final)
return namedComponents.get(name);
}
public synchronized final void registerNamedComponentRegistry(ComponentRegistry componentRegistry, String name) {
namedComponents.put(name, componentRegistry);
}
public synchronized final void unregisterNamedComponentRegistry(String name) {
namedComponents.remove(name);
}
public synchronized final void rewireNamedRegistries() {
for (ComponentRegistry cr : namedComponents.values())
cr.rewire();
}
public Map<Byte,ModuleCommandInitializer> getModuleCommandInitializers() {
//moduleProperties is final so we don't need to synchronize this method for safe-publishing
return Collections.unmodifiableMap(moduleProperties.moduleCommandInitializers());
}
@Override
public synchronized void start() {
try {
boolean needToNotify = state != ComponentStatus.RUNNING && state != ComponentStatus.INITIALIZING;
if (needToNotify) {
for (ModuleLifecycle l : moduleLifecycles) {
l.cacheManagerStarting(this, globalConfiguration);
}
}
super.start();
if (!versionLogged) {
log.version(Version.printVersion());
versionLogged = true;
}
if (needToNotify && state == ComponentStatus.RUNNING) {
for (ModuleLifecycle l : moduleLifecycles) {
l.cacheManagerStarted(this);
}
}
} catch (RuntimeException rte) {
try {
resetVolatileComponents();
rewire();
} catch (Exception e) {
if (log.isDebugEnabled())
log.unableToResetGlobalComponentRegistryAfterRestart(e);
else
log.unableToResetGlobalComponentRegistryAfterRestart(e.getClass().getSimpleName(), e.getMessage(), e);
}
throw new EmbeddedCacheManagerStartupException(rte);
}
}
@Override
public synchronized void stop() {
boolean needToNotify = state == ComponentStatus.RUNNING || state == ComponentStatus.INITIALIZING;
if (needToNotify) {
for (ModuleLifecycle l : moduleLifecycles) {
l.cacheManagerStopping(this);
}
}
super.stop();
if (state == ComponentStatus.TERMINATED && needToNotify) {
for (ModuleLifecycle l : moduleLifecycles) {
l.cacheManagerStopped(this);
}
}
}
public final GlobalConfiguration getGlobalConfiguration() {
//this is final so no need to synchronise it
return globalConfiguration;
}
/**
* Removes a cache with the given name, returning true if the cache was removed.
*/
public synchronized boolean removeCache(String cacheName) {
return createdCaches.remove(cacheName);
}
public ModuleProperties getModuleProperties() {
return moduleProperties;
}
}