/
LifecycleManager.java
213 lines (188 loc) · 11.4 KB
/
LifecycleManager.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
package org.infinispan.query.remote.impl;
import org.hibernate.search.spi.SearchIntegrator;
import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.logging.LogFactory;
import org.infinispan.commons.marshall.AdvancedExternalizer;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.cache.CustomInterceptorsConfigurationBuilder;
import org.infinispan.configuration.cache.InterceptorConfiguration;
import org.infinispan.configuration.cache.InterceptorConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.factories.components.ComponentMetadataRepo;
import org.infinispan.factories.components.ManageableComponentMetadata;
import org.infinispan.interceptors.BatchingInterceptor;
import org.infinispan.interceptors.InterceptorChain;
import org.infinispan.interceptors.InvocationContextInterceptor;
import org.infinispan.jmx.JmxUtil;
import org.infinispan.jmx.ResourceDMBean;
import org.infinispan.lifecycle.AbstractModuleLifecycle;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.objectfilter.impl.ProtobufMatcher;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.query.remote.ProtobufMetadataManager;
import org.infinispan.query.remote.impl.filter.ContinuousQueryResultExternalizer;
import org.infinispan.query.remote.impl.filter.JPABinaryProtobufFilterAndConverter;
import org.infinispan.query.remote.impl.filter.JPAContinuousQueryProtobufCacheEventFilterConverter;
import org.infinispan.query.remote.impl.filter.JPAProtobufCacheEventFilterConverter;
import org.infinispan.query.remote.impl.filter.JPAProtobufFilterAndConverter;
import org.infinispan.query.remote.impl.indexing.ProtobufValueWrapper;
import org.infinispan.query.remote.impl.indexing.RemoteValueWrapperInterceptor;
import org.infinispan.query.remote.impl.logging.Log;
import org.infinispan.registry.InternalCacheRegistry;
import org.kohsuke.MetaInfServices;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.util.Map;
/**
* @author anistor@redhat.com
* @since 6.0
*/
@MetaInfServices(org.infinispan.lifecycle.ModuleLifecycle.class)
public final class LifecycleManager extends AbstractModuleLifecycle {
private static final Log log = LogFactory.getLog(LifecycleManager.class, Log.class);
@Override
public void cacheManagerStarting(GlobalComponentRegistry gcr, GlobalConfiguration globalCfg) {
Map<Integer, AdvancedExternalizer<?>> externalizerMap = globalCfg.serialization().advancedExternalizers();
externalizerMap.put(ExternalizerIds.PROTOBUF_VALUE_WRAPPER, new ProtobufValueWrapper.Externalizer());
externalizerMap.put(ExternalizerIds.JPA_PROTOBUF_CACHE_EVENT_FILTER_CONVERTER, new JPAProtobufCacheEventFilterConverter.Externalizer());
externalizerMap.put(ExternalizerIds.JPA_PROTOBUF_FILTER_AND_CONVERTER, new JPAProtobufFilterAndConverter.Externalizer());
externalizerMap.put(ExternalizerIds.JPA_CONTINUOUS_QUERY_CACHE_EVENT_FILTER_CONVERTER, new JPAContinuousQueryProtobufCacheEventFilterConverter.Externalizer());
externalizerMap.put(ExternalizerIds.JPA_BINARY_PROTOBUF_FILTER_AND_CONVERTER, new JPABinaryProtobufFilterAndConverter.Externalizer());
externalizerMap.put(ExternalizerIds.JPA_CONTINUOUS_QUERY_RESULT, new ContinuousQueryResultExternalizer());
}
@Override
public void cacheManagerStarted(GlobalComponentRegistry gcr) {
EmbeddedCacheManager cacheManager = gcr.getComponent(EmbeddedCacheManager.class);
initProtobufMetadataManager((DefaultCacheManager) cacheManager, gcr);
}
private void initProtobufMetadataManager(DefaultCacheManager cacheManager, GlobalComponentRegistry gcr) {
ProtobufMetadataManagerImpl protobufMetadataManager = new ProtobufMetadataManagerImpl();
gcr.registerComponent(protobufMetadataManager, ProtobufMetadataManager.class);
registerProtobufMetadataManagerMBean(protobufMetadataManager, gcr, cacheManager.getName());
}
private void registerProtobufMetadataManagerMBean(ProtobufMetadataManager protobufMetadataManager, GlobalComponentRegistry gcr, String cacheManagerName) {
GlobalConfiguration globalCfg = gcr.getGlobalConfiguration();
MBeanServer mBeanServer = JmxUtil.lookupMBeanServer(globalCfg);
String groupName = "type=RemoteQuery,name=" + ObjectName.quote(cacheManagerName);
String jmxDomain = JmxUtil.buildJmxDomain(globalCfg, mBeanServer, groupName);
ComponentMetadataRepo metadataRepo = gcr.getComponentMetadataRepo();
ManageableComponentMetadata metadata = metadataRepo.findComponentMetadata(ProtobufMetadataManagerImpl.class)
.toManageableComponentMetadata();
try {
ResourceDMBean mBean = new ResourceDMBean(protobufMetadataManager, metadata);
ObjectName objName = new ObjectName(jmxDomain + ":" + groupName + ",component=" + metadata.getJmxObjectName());
protobufMetadataManager.setObjectName(objName);
JmxUtil.registerMBean(mBean, objName, mBeanServer);
} catch (Exception e) {
throw new CacheException("Unable to register ProtobufMetadataManager MBean", e);
}
}
@Override
public void cacheManagerStopping(GlobalComponentRegistry gcr) {
unregisterProtobufMetadataManagerMBean(gcr);
}
private void unregisterProtobufMetadataManagerMBean(GlobalComponentRegistry gcr) {
try {
ObjectName objName = gcr.getComponent(ProtobufMetadataManager.class).getObjectName();
MBeanServer mBeanServer = JmxUtil.lookupMBeanServer(gcr.getGlobalConfiguration());
JmxUtil.unregisterMBean(objName, mBeanServer);
} catch (Exception e) {
throw new CacheException("Unable to unregister ProtobufMetadataManager MBean", e);
}
}
/**
* Registers the remote value wrapper interceptor in the cache before it gets started.
*/
@Override
public void cacheStarting(ComponentRegistry cr, Configuration cfg, String cacheName) {
InternalCacheRegistry icr = cr.getGlobalComponentRegistry().getComponent(InternalCacheRegistry.class);
if (!icr.isInternalCache(cacheName)) {
boolean isIndexed = cfg.indexing().index().isEnabled();
boolean isCompatMode = cfg.compatibility().enabled();
if (isIndexed && !isCompatMode) {
log.infof("Registering RemoteValueWrapperInterceptor for cache %s", cacheName);
createRemoteValueWrapperInterceptor(cr, cfg);
}
}
}
private void createRemoteValueWrapperInterceptor(ComponentRegistry cr, Configuration cfg) {
RemoteValueWrapperInterceptor wrapperInterceptor = cr.getComponent(RemoteValueWrapperInterceptor.class);
if (wrapperInterceptor == null) {
wrapperInterceptor = new RemoteValueWrapperInterceptor();
// Interceptor registration not needed, core configuration handling
// already does it for all custom interceptors - UNLESS the InterceptorChain already exists in the component registry!
InterceptorChain ic = cr.getComponent(InterceptorChain.class);
ConfigurationBuilder builder = new ConfigurationBuilder().read(cfg);
InterceptorConfigurationBuilder interceptorBuilder = builder.customInterceptors().addInterceptor();
interceptorBuilder.interceptor(wrapperInterceptor);
if (cfg.invocationBatching().enabled()) {
if (ic != null) ic.addInterceptorAfter(wrapperInterceptor, BatchingInterceptor.class);
interceptorBuilder.after(BatchingInterceptor.class);
} else {
if (ic != null) ic.addInterceptorAfter(wrapperInterceptor, InvocationContextInterceptor.class);
interceptorBuilder.after(InvocationContextInterceptor.class);
}
if (ic != null) {
cr.registerComponent(wrapperInterceptor, RemoteValueWrapperInterceptor.class);
}
cfg.customInterceptors().interceptors(builder.build().customInterceptors().interceptors());
}
}
@Override
public void cacheStarted(ComponentRegistry cr, String cacheName) {
InternalCacheRegistry icr = cr.getGlobalComponentRegistry().getComponent(InternalCacheRegistry.class);
if (!icr.isInternalCache(cacheName)) {
Configuration cfg = cr.getComponent(Configuration.class);
boolean isIndexed = cfg.indexing().index().isEnabled();
boolean isCompatMode = cfg.compatibility().enabled();
if (isIndexed && !isCompatMode) {
if (!verifyChainContainsRemoteValueWrapperInterceptor(cr)) {
throw new IllegalStateException("It was expected to find the RemoteValueWrapperInterceptor registered in the InterceptorChain but it wasn't found");
}
} else if (verifyChainContainsRemoteValueWrapperInterceptor(cr)) {
throw new IllegalStateException("It was NOT expected to find the RemoteValueWrapperInterceptor registered in the InterceptorChain as indexing was disabled, but it was found");
}
ProtobufMetadataManagerImpl protobufMetadataManager = (ProtobufMetadataManagerImpl) cr.getGlobalComponentRegistry().getComponent(ProtobufMetadataManager.class);
SerializationContext serCtx = protobufMetadataManager.getSerializationContext();
cr.registerComponent(new ProtobufMatcher(serCtx), ProtobufMatcher.class);
if (isCompatMode) {
SearchIntegrator searchFactory = cr.getComponent(SearchIntegrator.class);
CompatibilityReflectionMatcher compatibilityReflectionMatcher;
if (searchFactory == null) {
compatibilityReflectionMatcher = new CompatibilityReflectionMatcher(serCtx);
} else {
compatibilityReflectionMatcher = new CompatibilityReflectionMatcher(serCtx, searchFactory);
}
cr.registerComponent(compatibilityReflectionMatcher, CompatibilityReflectionMatcher.class);
}
AdvancedCache<?, ?> cache = cr.getComponent(Cache.class).getAdvancedCache();
RemoteQueryEngine remoteQueryEngine = new RemoteQueryEngine(cache, isIndexed, isCompatMode, serCtx);
cr.registerComponent(remoteQueryEngine, RemoteQueryEngine.class);
}
}
private boolean verifyChainContainsRemoteValueWrapperInterceptor(ComponentRegistry cr) {
InterceptorChain interceptorChain = cr.getComponent(InterceptorChain.class);
return interceptorChain != null && interceptorChain.containsInterceptorType(RemoteValueWrapperInterceptor.class, true);
}
@Override
public void cacheStopped(ComponentRegistry cr, String cacheName) {
Configuration cfg = cr.getComponent(Configuration.class);
removeRemoteIndexingInterceptorFromConfig(cfg);
}
private void removeRemoteIndexingInterceptorFromConfig(Configuration cfg) {
ConfigurationBuilder builder = new ConfigurationBuilder();
CustomInterceptorsConfigurationBuilder customInterceptorsBuilder = builder.customInterceptors();
for (InterceptorConfiguration interceptorConfig : cfg.customInterceptors().interceptors()) {
if (!(interceptorConfig.interceptor() instanceof RemoteValueWrapperInterceptor)) {
customInterceptorsBuilder.addInterceptor().read(interceptorConfig);
}
}
cfg.customInterceptors().interceptors(builder.build().customInterceptors().interceptors());
}
}