Enhance debug capabilities
[controller.git] / opendaylight / clustering / services_implementation / src / main / java / org / infinispan / interceptors / distribution / BaseDistributionInterceptor.java
1 /*
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.
7  *
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.
12  *
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.
17  *
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.
22  */
23 package org.infinispan.interceptors.distribution;
24
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;
49
50 import java.util.*;
51
52 /**
53  * Base class for distribution of entries across a cluster.
54  *
55  * @author Manik Surtani
56  * @author Mircea.Markus@jboss.com
57  * @author Pete Muir
58  * @author Dan Berindei <dan@infinispan.org>
59  * @since 4.0
60  */
61 public abstract class BaseDistributionInterceptor extends ClusteringInterceptor {
62
63    protected DistributionManager dm;
64
65    protected ClusteringDependentLogic cdl;
66
67    private static final Log log = LogFactory.getLog(BaseDistributionInterceptor.class);
68
69    @Override
70    protected Log getLog() {
71       return log;
72    }
73
74    @Inject
75    public void injectDependencies(DistributionManager distributionManager, ClusteringDependentLogic cdl) {
76       this.dm = distributionManager;
77       this.cdl = cdl;
78    }
79
80    @Override
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);
84
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);
92
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);
98             }
99          }
100       }
101
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
105       return null;
106    }
107
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");
111       }
112
113       RecipientGenerator recipientGenerator = new SingleKeyRecipientGenerator(command.getKey());
114
115       // see if we need to load values from remote sources first
116       remoteGetBeforeWrite(ctx, command, recipientGenerator);
117
118       // if this is local mode then skip distributing
119       if (isLocalModeForced(command)) {
120          return invokeNextInterceptor(ctx, command);
121       }
122
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()) {
129                     log.tracef(
130                             "Skipping the replication of the conditional command as it did not succeed on primary owner (%s).",
131                             command);
132                     return returnValue;
133                 }
134                 rpcManager.invokeRemotely(recipientGenerator.generateRecipients(), command,
135                         rpcManager.getDefaultRpcOptions(isSync));
136             } else {
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());
141             }
142          return returnValue;
143       } else {
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);
149                return result;
150             }
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));
160             }
161             return result;
162          } else {
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());
165
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;
171
172             return getResponseFromPrimaryOwner(primaryOwner, addressResponseMap);
173          }
174       }
175    }
176
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);
181          return null;
182       }
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);
186       } else {
187          return ((SuccessfulResponse) fromPrimaryOwner).getResponseValue();
188       }
189    }
190
191    protected abstract void remoteGetBeforeWrite(InvocationContext ctx, WriteCommand command, RecipientGenerator keygen) throws Throwable;
192
193    interface RecipientGenerator {
194
195       Collection<Object> getKeys();
196
197       List<Address> generateRecipients();
198    }
199
200    class SingleKeyRecipientGenerator implements RecipientGenerator {
201       private final Object key;
202       private final Set<Object> keys;
203       private List<Address> recipients = null;
204
205       SingleKeyRecipientGenerator(Object key) {
206          this.key = key;
207          keys = Collections.singleton(key);
208       }
209
210       @Override
211       public List<Address> generateRecipients() {
212          if (recipients == null) {
213             recipients = cdl.getOwners(key);
214          }
215          return recipients;
216       }
217
218       @Override
219       public Collection<Object> getKeys() {
220          return keys;
221       }
222    }
223
224    class MultipleKeysRecipientGenerator implements RecipientGenerator {
225
226       private final Collection<Object> keys;
227       private List<Address> recipients = null;
228
229       MultipleKeysRecipientGenerator(Collection<Object> keys) {
230          this.keys = keys;
231       }
232
233       @Override
234       public List<Address> generateRecipients() {
235          if (recipients == null) {
236             recipients = cdl.getOwners(keys);
237          }
238          return recipients;
239       }
240
241       @Override
242       public Collection<Object> getKeys() {
243          return keys;
244       }
245    }
246 }