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 org.infinispan.CacheException;
26 import org.infinispan.commands.FlagAffectedCommand;
27 import org.infinispan.commands.remote.ClusteredGetCommand;
28 import org.infinispan.commands.write.DataWriteCommand;
29 import org.infinispan.commands.write.WriteCommand;
30 import org.infinispan.container.entries.InternalCacheEntry;
31 import org.infinispan.container.entries.InternalCacheValue;
32 import org.infinispan.context.InvocationContext;
33 import org.infinispan.context.impl.TxInvocationContext;
34 import org.infinispan.distribution.DistributionManager;
35 import org.infinispan.factories.annotations.Inject;
36 import org.infinispan.interceptors.ClusteringInterceptor;
37 import org.infinispan.interceptors.locking.ClusteringDependentLogic;
38 import org.infinispan.remoting.responses.ClusteredGetResponseValidityFilter;
39 import org.infinispan.remoting.responses.ExceptionResponse;
40 import org.infinispan.remoting.responses.Response;
41 import org.infinispan.remoting.responses.SuccessfulResponse;
42 import org.infinispan.remoting.rpc.ResponseFilter;
43 import org.infinispan.remoting.rpc.ResponseMode;
44 import org.infinispan.remoting.rpc.RpcOptions;
45 import org.infinispan.remoting.transport.Address;
46 import org.infinispan.transaction.xa.GlobalTransaction;
47 import org.infinispan.util.logging.Log;
48 import org.infinispan.util.logging.LogFactory;
53 * Base class for distribution of entries across a cluster.
55 * @author Manik Surtani
56 * @author Mircea.Markus@jboss.com
58 * @author Dan Berindei <dan@infinispan.org>
61 public abstract class BaseDistributionInterceptor extends ClusteringInterceptor {
63 protected DistributionManager dm;
65 protected ClusteringDependentLogic cdl;
67 private static final Log log = LogFactory.getLog(BaseDistributionInterceptor.class);
70 protected Log getLog() {
75 public void injectDependencies(DistributionManager distributionManager, ClusteringDependentLogic cdl) {
76 this.dm = distributionManager;
81 protected final InternalCacheEntry retrieveFromRemoteSource(Object key, InvocationContext ctx, boolean acquireRemoteLock, FlagAffectedCommand command) throws Exception {
82 GlobalTransaction gtx = acquireRemoteLock ? ((TxInvocationContext)ctx).getGlobalTransaction() : null;
83 ClusteredGetCommand get = cf.buildClusteredGetCommand(key, command.getFlags(), acquireRemoteLock, gtx);
85 List<Address> targets = new ArrayList<Address>(stateTransferManager.getCacheTopology().getReadConsistentHash().locateOwners(key));
86 // if any of the recipients has left the cluster since the command was issued, just don't wait for its response
87 targets.retainAll(rpcManager.getTransport().getMembers());
88 ResponseFilter filter = new ClusteredGetResponseValidityFilter(targets, rpcManager.getAddress());
89 RpcOptions options = rpcManager.getRpcOptionsBuilder(ResponseMode.WAIT_FOR_VALID_RESPONSE, false)
90 .responseFilter(filter).build();
91 Map<Address, Response> responses = rpcManager.invokeRemotely(targets, get, options);
93 if (!responses.isEmpty()) {
94 for (Response r : responses.values()) {
95 if (r instanceof SuccessfulResponse) {
96 InternalCacheValue cacheValue = (InternalCacheValue) ((SuccessfulResponse) r).getResponseValue();
97 return cacheValue.toInternalCacheEntry(key);
102 // TODO If everyone returned null, and the read CH has changed, retry the remote get.
103 // Otherwise our get command might be processed by the old owners after they have invalidated their data
104 // and we'd return a null even though the key exists on
108 protected final Object handleNonTxWriteCommand(InvocationContext ctx, DataWriteCommand command) throws Throwable {
109 if (ctx.isInTxScope()) {
110 throw new CacheException("Attempted execution of non-transactional write command in a transactional invocation context");
113 RecipientGenerator recipientGenerator = new SingleKeyRecipientGenerator(command.getKey());
115 // see if we need to load values from remote sources first
116 remoteGetBeforeWrite(ctx, command, recipientGenerator);
118 // if this is local mode then skip distributing
119 if (isLocalModeForced(command)) {
120 return invokeNextInterceptor(ctx, command);
123 boolean isSync = isSynchronous(command);
124 if (!ctx.isOriginLocal()) {
125 Object returnValue = invokeNextInterceptor(ctx, command);
126 Address primaryOwner = cdl.getPrimaryOwner(command.getKey());
127 if (primaryOwner.equals(rpcManager.getAddress())) {
128 if (command.isConditional() && !command.isSuccessful()) {
130 "Skipping the replication of the conditional command as it did not succeed on primary owner (%s).",
134 rpcManager.invokeRemotely(recipientGenerator.generateRecipients(), command,
135 rpcManager.getDefaultRpcOptions(isSync));
137 log.errorf("Didn't invoke RPC because primaryOwner (%s) didn't match this node (%s)", primaryOwner,
138 rpcManager.getAddress());
139 log.errorf("Hashcode is (%s) for Key (%s) .. it could be inconsistent in the cluster!",
140 command.getKey().hashCode(), command.getKey());
144 Address primaryOwner = cdl.getPrimaryOwner(command.getKey());
145 if (primaryOwner.equals(rpcManager.getAddress())) {
146 Object result = invokeNextInterceptor(ctx, command);
147 if (command.isConditional() && !command.isSuccessful()) {
148 log.tracef("Skipping the replication of the conditional command as it did not succeed on primary owner (%s).", command);
151 List<Address> recipients = recipientGenerator.generateRecipients();
152 log.tracef("I'm the primary owner, sending the command to all (%s) the recipients in order to be applied.", recipients);
153 // check if a single owner has been configured and the target for the key is the local address
154 boolean isSingleOwnerAndLocal = cacheConfiguration.clustering().hash().numOwners() == 1
155 && recipients != null
156 && recipients.size() == 1
157 && recipients.get(0).equals(rpcManager.getTransport().getAddress());
158 if (!isSingleOwnerAndLocal) {
159 rpcManager.invokeRemotely(recipients, command, rpcManager.getDefaultRpcOptions(isSync));
163 log.tracef("I'm not the primary owner, so sending the command to the primary owner(%s) in order to be forwarded", primaryOwner);
164 log.tracef("Hashcode is (%s) for Key (%s)", command.getKey().hashCode(), command.getKey());
166 Object localResult = invokeNextInterceptor(ctx, command);
167 boolean isSyncForwarding = isSync || isNeedReliableReturnValues(command);
168 Map<Address, Response> addressResponseMap = rpcManager.invokeRemotely(Collections.singletonList(primaryOwner), command,
169 rpcManager.getDefaultRpcOptions(isSyncForwarding));
170 if (!isSyncForwarding) return localResult;
172 return getResponseFromPrimaryOwner(primaryOwner, addressResponseMap);
177 private Object getResponseFromPrimaryOwner(Address primaryOwner, Map<Address, Response> addressResponseMap) {
178 Response fromPrimaryOwner = addressResponseMap.get(primaryOwner);
179 if (fromPrimaryOwner == null) {
180 log.tracef("Primary owner %s returned null", primaryOwner);
183 if (!fromPrimaryOwner.isSuccessful()) {
184 Throwable cause = fromPrimaryOwner instanceof ExceptionResponse ? ((ExceptionResponse)fromPrimaryOwner).getException() : null;
185 throw new CacheException("Got unsuccessful response from primary owner: " + fromPrimaryOwner, cause);
187 return ((SuccessfulResponse) fromPrimaryOwner).getResponseValue();
191 protected abstract void remoteGetBeforeWrite(InvocationContext ctx, WriteCommand command, RecipientGenerator keygen) throws Throwable;
193 interface RecipientGenerator {
195 Collection<Object> getKeys();
197 List<Address> generateRecipients();
200 class SingleKeyRecipientGenerator implements RecipientGenerator {
201 private final Object key;
202 private final Set<Object> keys;
203 private List<Address> recipients = null;
205 SingleKeyRecipientGenerator(Object key) {
207 keys = Collections.singleton(key);
211 public List<Address> generateRecipients() {
212 if (recipients == null) {
213 recipients = cdl.getOwners(key);
219 public Collection<Object> getKeys() {
224 class MultipleKeysRecipientGenerator implements RecipientGenerator {
226 private final Collection<Object> keys;
227 private List<Address> recipients = null;
229 MultipleKeysRecipientGenerator(Collection<Object> keys) {
234 public List<Address> generateRecipients() {
235 if (recipients == null) {
236 recipients = cdl.getOwners(keys);
242 public Collection<Object> getKeys() {