PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/core/src/test/java/org/infinispan/util/mocks/ControlledCommandFactory.java

https://bitbucket.org/cprenzberg/infinispan
Java | 352 lines | 274 code | 54 blank | 24 comment | 7 complexity | 0ef49cebee98f424eb6d26b71dba05ca MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
  4. * as indicated by the @author tags. All rights reserved.
  5. * See the copyright.txt in the distribution for a
  6. * full listing of individual contributors.
  7. *
  8. * This copyrighted material is made available to anyone wishing to use,
  9. * modify, copy, or redistribute it subject to the terms and conditions
  10. * of the GNU Lesser General Public License, v. 2.1.
  11. * This program is distributed in the hope that it will be useful, but WITHOUT A
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  14. * You should have received a copy of the GNU Lesser General Public License,
  15. * v.2.1 along with this distribution; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. package org.infinispan.util.mocks;
  20. import org.infinispan.Cache;
  21. import org.infinispan.atomic.Delta;
  22. import org.infinispan.commands.CancelCommand;
  23. import org.infinispan.commands.CommandsFactory;
  24. import org.infinispan.commands.CreateCacheCommand;
  25. import org.infinispan.commands.ReplicableCommand;
  26. import org.infinispan.commands.control.LockControlCommand;
  27. import org.infinispan.commands.read.DistributedExecuteCommand;
  28. import org.infinispan.commands.read.EntrySetCommand;
  29. import org.infinispan.commands.read.GetCacheEntryCommand;
  30. import org.infinispan.commands.read.GetKeyValueCommand;
  31. import org.infinispan.commands.read.KeySetCommand;
  32. import org.infinispan.commands.read.MapCombineCommand;
  33. import org.infinispan.commands.read.ReduceCommand;
  34. import org.infinispan.commands.read.SizeCommand;
  35. import org.infinispan.commands.read.ValuesCommand;
  36. import org.infinispan.commands.remote.ClusteredGetCommand;
  37. import org.infinispan.commands.remote.MultipleRpcCommand;
  38. import org.infinispan.commands.remote.SingleRpcCommand;
  39. import org.infinispan.commands.remote.recovery.CompleteTransactionCommand;
  40. import org.infinispan.commands.remote.recovery.GetInDoubtTransactionsCommand;
  41. import org.infinispan.commands.remote.recovery.GetInDoubtTxInfoCommand;
  42. import org.infinispan.commands.remote.recovery.TxCompletionNotificationCommand;
  43. import org.infinispan.commands.tx.CommitCommand;
  44. import org.infinispan.commands.tx.PrepareCommand;
  45. import org.infinispan.commands.tx.RollbackCommand;
  46. import org.infinispan.commands.tx.VersionedCommitCommand;
  47. import org.infinispan.commands.tx.VersionedPrepareCommand;
  48. import org.infinispan.commands.write.*;
  49. import org.infinispan.container.versioning.EntryVersion;
  50. import org.infinispan.context.Flag;
  51. import org.infinispan.distexec.mapreduce.Mapper;
  52. import org.infinispan.distexec.mapreduce.Reducer;
  53. import org.infinispan.factories.ComponentRegistry;
  54. import org.infinispan.manager.EmbeddedCacheManager;
  55. import org.infinispan.remoting.transport.Address;
  56. import org.infinispan.statetransfer.StateChunk;
  57. import org.infinispan.statetransfer.StateRequestCommand;
  58. import org.infinispan.statetransfer.StateResponseCommand;
  59. import org.infinispan.test.TestingUtil;
  60. import org.infinispan.transaction.xa.GlobalTransaction;
  61. import org.infinispan.util.concurrent.ReclosableLatch;
  62. import org.infinispan.util.logging.Log;
  63. import org.infinispan.util.logging.LogFactory;
  64. import javax.transaction.xa.Xid;
  65. import java.util.ArrayList;
  66. import java.util.Collection;
  67. import java.util.List;
  68. import java.util.Map;
  69. import java.util.Set;
  70. import java.util.UUID;
  71. import java.util.concurrent.Callable;
  72. import java.util.concurrent.atomic.AtomicInteger;
  73. /**
  74. * @author Mircea Markus
  75. * @since 5.2
  76. */
  77. public class ControlledCommandFactory implements CommandsFactory {
  78. private static Log log = LogFactory.getLog(ControlledCommandFactory.class);
  79. public final CommandsFactory actual;
  80. public final ReclosableLatch gate = new ReclosableLatch(true);
  81. public final AtomicInteger remoteCommandsReceived = new AtomicInteger(0);
  82. public final AtomicInteger blockTypeCommandsReceived = new AtomicInteger(0);
  83. public final List<ReplicableCommand> receivedCommands = new ArrayList<ReplicableCommand>();
  84. public final Class<? extends ReplicableCommand> toBlock;
  85. public ControlledCommandFactory(CommandsFactory actual, Class<? extends ReplicableCommand> toBlock) {
  86. this.actual = actual;
  87. this.toBlock = toBlock;
  88. }
  89. public int received(Class<? extends ReplicableCommand> command) {
  90. int result = 0;
  91. for (ReplicableCommand r : receivedCommands) {
  92. if (r.getClass() == command) {
  93. result++;
  94. }
  95. }
  96. return result;
  97. }
  98. @Override
  99. public void initializeReplicableCommand(ReplicableCommand command, boolean isRemote) {
  100. log.tracef("Received command %s", command);
  101. receivedCommands.add(command);
  102. if (isRemote) {
  103. remoteCommandsReceived.incrementAndGet();
  104. if (toBlock != null && command.getClass().isAssignableFrom(toBlock)) {
  105. blockTypeCommandsReceived.incrementAndGet();
  106. try {
  107. gate.await();
  108. log.tracef("gate is opened, processing the lock cleanup: %s", command);
  109. } catch (InterruptedException e) {
  110. throw new RuntimeException(e);
  111. }
  112. }
  113. }
  114. actual.initializeReplicableCommand(command, isRemote);
  115. }
  116. public static ControlledCommandFactory registerControlledCommandFactory(Cache cache, Class<? extends ReplicableCommand> toBlock) {
  117. ComponentRegistry componentRegistry = cache.getAdvancedCache().getComponentRegistry();
  118. final ControlledCommandFactory ccf = new ControlledCommandFactory(componentRegistry.getCommandsFactory(), toBlock);
  119. TestingUtil.replaceField(ccf, "commandsFactory", componentRegistry, ComponentRegistry.class);
  120. componentRegistry.registerComponent(ccf, CommandsFactory.class);
  121. //hack: re-add the component registry to the GlobalComponentRegistry's "namedComponents" (CHM) in order to correctly publish it for
  122. // when it will be read by the InboundInvocationHandlder. InboundInvocationHandlder reads the value from the GlobalComponentRegistry.namedComponents before using it
  123. componentRegistry.getGlobalComponentRegistry().registerNamedComponentRegistry(componentRegistry, EmbeddedCacheManager.DEFAULT_CACHE_NAME);
  124. return ccf;
  125. }
  126. @Override
  127. public PutKeyValueCommand buildPutKeyValueCommand(Object key, Object value, long lifespanMillis, long maxIdleTimeMillis, Set<Flag> flags) {
  128. return actual.buildPutKeyValueCommand(key, value, lifespanMillis, maxIdleTimeMillis, flags);
  129. }
  130. @Override
  131. public VersionedPutKeyValueCommand buildVersionedPutKeyValueCommand(Object key, Object value, long lifespanMillis, long maxIdleTimeMillis, EntryVersion version, Set<Flag> flags) {
  132. return actual.buildVersionedPutKeyValueCommand(key, value, lifespanMillis, maxIdleTimeMillis, version, flags);
  133. }
  134. @Override
  135. public RemoveCommand buildRemoveCommand(Object key, Object value, Set<Flag> flags) {
  136. return actual.buildRemoveCommand(key, value, flags);
  137. }
  138. @Override
  139. public InvalidateCommand buildInvalidateCommand(Set<Flag> flags, Object... keys) {
  140. return actual.buildInvalidateCommand(flags, keys);
  141. }
  142. @Override
  143. public InvalidateCommand buildInvalidateFromL1Command(boolean forRehash, Set<Flag> flags, Object... keys) {
  144. return actual.buildInvalidateFromL1Command(forRehash, flags, keys);
  145. }
  146. @Override
  147. public InvalidateCommand buildInvalidateFromL1Command(boolean forRehash, Set<Flag> flags, Collection<Object> keys) {
  148. return actual.buildInvalidateFromL1Command(forRehash, flags, keys);
  149. }
  150. @Override
  151. public InvalidateCommand buildInvalidateFromL1Command(Address origin, boolean forRehash, Set<Flag> flags, Collection<Object> keys) {
  152. return actual.buildInvalidateFromL1Command(origin, forRehash, flags, keys);
  153. }
  154. @Override
  155. public ReplaceCommand buildReplaceCommand(Object key, Object oldValue, Object newValue, long lifespanMillis, long maxIdleTimeMillis, Set<Flag> flags) {
  156. return actual.buildReplaceCommand(key, oldValue, newValue, lifespanMillis, maxIdleTimeMillis, flags);
  157. }
  158. @Override
  159. public SizeCommand buildSizeCommand() {
  160. return actual.buildSizeCommand();
  161. }
  162. @Override
  163. public GetKeyValueCommand buildGetKeyValueCommand(Object key, Set<Flag> flags) {
  164. return actual.buildGetKeyValueCommand(key, flags);
  165. }
  166. @Override
  167. public GetCacheEntryCommand buildGetCacheEntryCommand(Object key, Set<Flag> flags) {
  168. return actual.buildGetCacheEntryCommand(key, flags);
  169. }
  170. @Override
  171. public KeySetCommand buildKeySetCommand() {
  172. return actual.buildKeySetCommand();
  173. }
  174. @Override
  175. public ValuesCommand buildValuesCommand() {
  176. return actual.buildValuesCommand();
  177. }
  178. @Override
  179. public EntrySetCommand buildEntrySetCommand() {
  180. return actual.buildEntrySetCommand();
  181. }
  182. @Override
  183. public PutMapCommand buildPutMapCommand(Map<?, ?> map, long lifespanMillis, long maxIdleTimeMillis, Set<Flag> flags) {
  184. return actual.buildPutMapCommand(map, lifespanMillis, maxIdleTimeMillis, flags);
  185. }
  186. @Override
  187. public ClearCommand buildClearCommand(Set<Flag> flags) {
  188. return actual.buildClearCommand(flags);
  189. }
  190. @Override
  191. public EvictCommand buildEvictCommand(Object key, Set<Flag> flags) {
  192. return actual.buildEvictCommand(key, flags);
  193. }
  194. @Override
  195. public PrepareCommand buildPrepareCommand(GlobalTransaction gtx, List<WriteCommand> modifications, boolean onePhaseCommit) {
  196. return actual.buildPrepareCommand(gtx, modifications, onePhaseCommit);
  197. }
  198. @Override
  199. public VersionedPrepareCommand buildVersionedPrepareCommand(GlobalTransaction gtx, List<WriteCommand> modifications, boolean onePhase) {
  200. return actual.buildVersionedPrepareCommand(gtx, modifications, onePhase);
  201. }
  202. @Override
  203. public CommitCommand buildCommitCommand(GlobalTransaction gtx) {
  204. return actual.buildCommitCommand(gtx);
  205. }
  206. @Override
  207. public VersionedCommitCommand buildVersionedCommitCommand(GlobalTransaction gtx) {
  208. return actual.buildVersionedCommitCommand(gtx);
  209. }
  210. @Override
  211. public RollbackCommand buildRollbackCommand(GlobalTransaction gtx) {
  212. return actual.buildRollbackCommand(gtx);
  213. }
  214. @Override
  215. public MultipleRpcCommand buildReplicateCommand(List<ReplicableCommand> toReplicate) {
  216. return actual.buildReplicateCommand(toReplicate);
  217. }
  218. @Override
  219. public SingleRpcCommand buildSingleRpcCommand(ReplicableCommand call) {
  220. return actual.buildSingleRpcCommand(call);
  221. }
  222. @Override
  223. public ClusteredGetCommand buildClusteredGetCommand(Object key, Set<Flag> flags, boolean acquireRemoteLock, GlobalTransaction gtx) {
  224. return actual.buildClusteredGetCommand(key, flags, acquireRemoteLock, gtx);
  225. }
  226. @Override
  227. public LockControlCommand buildLockControlCommand(Collection<Object> keys, Set<Flag> flags, GlobalTransaction gtx) {
  228. return actual.buildLockControlCommand(keys, flags, gtx);
  229. }
  230. @Override
  231. public LockControlCommand buildLockControlCommand(Object key, Set<Flag> flags, GlobalTransaction gtx) {
  232. return actual.buildLockControlCommand(key, flags, gtx);
  233. }
  234. @Override
  235. public LockControlCommand buildLockControlCommand(Collection<Object> keys, Set<Flag> flags) {
  236. return actual.buildLockControlCommand(keys, flags);
  237. }
  238. @Override
  239. public StateRequestCommand buildStateRequestCommand(StateRequestCommand.Type subtype, Address sender, int viewId, Set<Integer> segments) {
  240. return actual.buildStateRequestCommand(subtype, sender, viewId, segments);
  241. }
  242. @Override
  243. public StateResponseCommand buildStateResponseCommand(Address sender, int viewId, Collection<StateChunk> stateChunks) {
  244. return actual.buildStateResponseCommand(sender, viewId, stateChunks);
  245. }
  246. @Override
  247. public String getCacheName() {
  248. return actual.getCacheName();
  249. }
  250. @Override
  251. public GetInDoubtTransactionsCommand buildGetInDoubtTransactionsCommand() {
  252. return actual.buildGetInDoubtTransactionsCommand();
  253. }
  254. @Override
  255. public TxCompletionNotificationCommand buildTxCompletionNotificationCommand(Xid xid, GlobalTransaction globalTransaction) {
  256. return actual.buildTxCompletionNotificationCommand(xid, globalTransaction);
  257. }
  258. @Override
  259. public <T> DistributedExecuteCommand<T> buildDistributedExecuteCommand(Callable<T> callable, Address sender, Collection keys) {
  260. return actual.buildDistributedExecuteCommand(callable, sender, keys);
  261. }
  262. @Override
  263. public <KIn, VIn, KOut, VOut> MapCombineCommand<KIn, VIn, KOut, VOut> buildMapCombineCommand(String taskId, Mapper<KIn, VIn, KOut, VOut> m, Reducer<KOut, VOut> r, Collection<KIn> keys) {
  264. return actual.buildMapCombineCommand(taskId, m, r, keys);
  265. }
  266. @Override
  267. public <KOut, VOut> ReduceCommand<KOut, VOut> buildReduceCommand(String taskId, String destinationCache, Reducer<KOut, VOut> r, Collection<KOut> keys) {
  268. return actual.buildReduceCommand(taskId, destinationCache, r, keys);
  269. }
  270. @Override
  271. public GetInDoubtTxInfoCommand buildGetInDoubtTxInfoCommand() {
  272. return actual.buildGetInDoubtTxInfoCommand();
  273. }
  274. @Override
  275. public CompleteTransactionCommand buildCompleteTransactionCommand(Xid xid, boolean commit) {
  276. return actual.buildCompleteTransactionCommand(xid, commit);
  277. }
  278. @Override
  279. public TxCompletionNotificationCommand buildTxCompletionNotificationCommand(long internalId) {
  280. return actual.buildTxCompletionNotificationCommand(internalId);
  281. }
  282. @Override
  283. public ApplyDeltaCommand buildApplyDeltaCommand(Object deltaAwareValueKey, Delta delta, Collection keys) {
  284. return actual.buildApplyDeltaCommand(deltaAwareValueKey, delta, keys);
  285. }
  286. @Override
  287. public CreateCacheCommand buildCreateCacheCommand(String cacheName, String cacheConfigurationName) {
  288. return actual.buildCreateCacheCommand(cacheName, cacheConfigurationName);
  289. }
  290. @Override
  291. public CancelCommand buildCancelCommandCommand(UUID commandUUID) {
  292. return actual.buildCancelCommandCommand(commandUUID);
  293. }
  294. @Override
  295. public CreateCacheCommand buildCreateCacheCommand(String tmpCacheName, String defaultTmpCacheConfigurationName, boolean start, int size) {
  296. return actual.buildCreateCacheCommand(tmpCacheName, defaultTmpCacheConfigurationName, start, size);
  297. }
  298. }