2 * JBoss, Home of Professional Open Source
3 * Copyright 2009 Red Hat Inc. and/or its affiliates and other
4 * contributors as indicated by the @author tags. All rights reserved.
5 * See the copyright.txt in the distribution for a full listing of
6 * individual contributors.
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This software is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this software; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 package org.infinispan.interceptors.distribution;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
32 import org.infinispan.CacheException;
33 import org.infinispan.commands.FlagAffectedCommand;
34 import org.infinispan.commands.remote.ClusteredGetCommand;
35 import org.infinispan.commands.write.DataWriteCommand;
36 import org.infinispan.commands.write.WriteCommand;
37 import org.infinispan.container.entries.InternalCacheEntry;
38 import org.infinispan.container.entries.InternalCacheValue;
39 import org.infinispan.context.InvocationContext;
40 import org.infinispan.context.impl.TxInvocationContext;
41 import org.infinispan.distribution.DistributionManager;
42 import org.infinispan.factories.annotations.Inject;
43 import org.infinispan.interceptors.ClusteringInterceptor;
44 import org.infinispan.interceptors.locking.ClusteringDependentLogic;
45 import org.infinispan.remoting.responses.ClusteredGetResponseValidityFilter;
46 import org.infinispan.remoting.responses.ExceptionResponse;
47 import org.infinispan.remoting.responses.Response;
48 import org.infinispan.remoting.responses.SuccessfulResponse;
49 import org.infinispan.remoting.rpc.ResponseFilter;
50 import org.infinispan.remoting.rpc.ResponseMode;
51 import org.infinispan.remoting.rpc.RpcOptions;
52 import org.infinispan.remoting.transport.Address;
53 import org.infinispan.transaction.xa.GlobalTransaction;
54 import org.infinispan.util.logging.Log;
55 import org.infinispan.util.logging.LogFactory;
59 * Base class for distribution of entries across a cluster.
61 * @author Manik Surtani
62 * @author Mircea.Markus@jboss.com
64 * @author Dan Berindei <dan@infinispan.org>
67 public abstract class BaseDistributionInterceptor extends ClusteringInterceptor {
69 protected DistributionManager dm;
71 protected ClusteringDependentLogic cdl;
73 private static final Log log = LogFactory.getLog(BaseDistributionInterceptor.class);
76 protected Log getLog() {
81 public void injectDependencies(DistributionManager distributionManager, ClusteringDependentLogic cdl) {
82 this.dm = distributionManager;
87 protected final InternalCacheEntry retrieveFromRemoteSource(Object key, InvocationContext ctx, boolean acquireRemoteLock, FlagAffectedCommand command) throws Exception {
88 GlobalTransaction gtx = acquireRemoteLock ? ((TxInvocationContext)ctx).getGlobalTransaction() : null;
89 ClusteredGetCommand get = cf.buildClusteredGetCommand(key, command.getFlags(), acquireRemoteLock, gtx);
91 List<Address> targets = new ArrayList<Address>(stateTransferManager.getCacheTopology().getReadConsistentHash().locateOwners(key));
92 // if any of the recipients has left the cluster since the command was issued, just don't wait for its response
93 targets.retainAll(rpcManager.getTransport().getMembers());
94 ResponseFilter filter = new ClusteredGetResponseValidityFilter(targets, rpcManager.getAddress());
95 RpcOptions options = rpcManager.getRpcOptionsBuilder(ResponseMode.WAIT_FOR_VALID_RESPONSE, false)
96 .responseFilter(filter).build();
97 Map<Address, Response> responses = rpcManager.invokeRemotely(targets, get, options);
99 if (!responses.isEmpty()) {
100 for (Response r : responses.values()) {
101 if (r instanceof SuccessfulResponse) {
102 InternalCacheValue cacheValue = (InternalCacheValue) ((SuccessfulResponse) r).getResponseValue();
103 return cacheValue.toInternalCacheEntry(key);
108 // TODO If everyone returned null, and the read CH has changed, retry the remote get.
109 // Otherwise our get command might be processed by the old owners after they have invalidated their data
110 // and we'd return a null even though the key exists on
114 protected final Object handleNonTxWriteCommand(InvocationContext ctx, DataWriteCommand command) throws Throwable {
115 if (ctx.isInTxScope()) {
116 throw new CacheException("Attempted execution of non-transactional write command in a transactional invocation context");
119 RecipientGenerator recipientGenerator = new SingleKeyRecipientGenerator(command.getKey());
121 // see if we need to load values from remote sources first
122 remoteGetBeforeWrite(ctx, command, recipientGenerator);
124 // if this is local mode then skip distributing
125 if (isLocalModeForced(command)) {
126 return invokeNextInterceptor(ctx, command);
129 boolean isSync = isSynchronous(command);
130 if (!ctx.isOriginLocal()) {
131 Object returnValue = invokeNextInterceptor(ctx, command);
132 Address primaryOwner = cdl.getPrimaryOwner(command.getKey());
133 if (primaryOwner.equals(rpcManager.getAddress())) {
134 if (command.isConditional() && !command.isSuccessful()) {
136 "Skipping the replication of the conditional command as it did not succeed on primary owner (%s).",
140 rpcManager.invokeRemotely(recipientGenerator.generateRecipients(), command,
141 rpcManager.getDefaultRpcOptions(isSync));
143 log.tracef("Didn't invoke RPC because primaryOwner (%s) didn't match this node (%s)", primaryOwner,
144 rpcManager.getAddress());
145 log.tracef("Hashcode is (%s) for Key (%s)",
146 command.getKey().hashCode(), command.getKey());
150 Address primaryOwner = cdl.getPrimaryOwner(command.getKey());
151 if (primaryOwner.equals(rpcManager.getAddress())) {
152 Object result = invokeNextInterceptor(ctx, command);
153 if (command.isConditional() && !command.isSuccessful()) {
154 log.tracef("Skipping the replication of the conditional command as it did not succeed on primary owner (%s).", command);
157 List<Address> recipients = recipientGenerator.generateRecipients();
158 log.tracef("I'm the primary owner, sending the command to all (%s) the recipients in order to be applied.", recipients);
159 // check if a single owner has been configured and the target for the key is the local address
160 boolean isSingleOwnerAndLocal = cacheConfiguration.clustering().hash().numOwners() == 1
161 && recipients != null
162 && recipients.size() == 1
163 && recipients.get(0).equals(rpcManager.getTransport().getAddress());
164 if (!isSingleOwnerAndLocal) {
165 rpcManager.invokeRemotely(recipients, command, rpcManager.getDefaultRpcOptions(isSync));
169 log.tracef("I'm not the primary owner, so sending the command to the primary owner(%s) in order to be forwarded", primaryOwner);
170 log.tracef("Hashcode is (%s) for Key (%s)", command.getKey().hashCode(), command.getKey());
172 Object localResult = invokeNextInterceptor(ctx, command);
173 boolean isSyncForwarding = isSync || isNeedReliableReturnValues(command);
174 Map<Address, Response> addressResponseMap = rpcManager.invokeRemotely(Collections.singletonList(primaryOwner), command,
175 rpcManager.getDefaultRpcOptions(isSyncForwarding));
176 if (!isSyncForwarding) return localResult;
178 return getResponseFromPrimaryOwner(primaryOwner, addressResponseMap);
183 private Object getResponseFromPrimaryOwner(Address primaryOwner, Map<Address, Response> addressResponseMap) {
184 Response fromPrimaryOwner = addressResponseMap.get(primaryOwner);
185 if (fromPrimaryOwner == null) {
186 log.tracef("Primary owner %s returned null", primaryOwner);
189 if (!fromPrimaryOwner.isSuccessful()) {
190 Throwable cause = fromPrimaryOwner instanceof ExceptionResponse ? ((ExceptionResponse)fromPrimaryOwner).getException() : null;
191 throw new CacheException("Got unsuccessful response from primary owner: " + fromPrimaryOwner, cause);
193 return ((SuccessfulResponse) fromPrimaryOwner).getResponseValue();
197 protected abstract void remoteGetBeforeWrite(InvocationContext ctx, WriteCommand command, RecipientGenerator keygen) throws Throwable;
199 interface RecipientGenerator {
201 Collection<Object> getKeys();
203 List<Address> generateRecipients();
206 class SingleKeyRecipientGenerator implements RecipientGenerator {
207 private final Object key;
208 private final Set<Object> keys;
209 private List<Address> recipients = null;
211 SingleKeyRecipientGenerator(Object key) {
213 keys = Collections.singleton(key);
217 public List<Address> generateRecipients() {
218 if (recipients == null) {
219 recipients = cdl.getOwners(key);
225 public Collection<Object> getKeys() {
230 class MultipleKeysRecipientGenerator implements RecipientGenerator {
232 private final Collection<Object> keys;
233 private List<Address> recipients = null;
235 MultipleKeysRecipientGenerator(Collection<Object> keys) {
240 public List<Address> generateRecipients() {
241 if (recipients == null) {
242 recipients = cdl.getOwners(keys);
248 public Collection<Object> getKeys() {