Fix star import and enable checkstyle rule to prevent it.
[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 java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
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;
56
57
58 /**
59  * Base class for distribution of entries across a cluster.
60  *
61  * @author Manik Surtani
62  * @author Mircea.Markus@jboss.com
63  * @author Pete Muir
64  * @author Dan Berindei <dan@infinispan.org>
65  * @since 4.0
66  */
67 public abstract class BaseDistributionInterceptor extends ClusteringInterceptor {
68
69    protected DistributionManager dm;
70
71    protected ClusteringDependentLogic cdl;
72
73    private static final Log log = LogFactory.getLog(BaseDistributionInterceptor.class);
74
75    @Override
76    protected Log getLog() {
77       return log;
78    }
79
80    @Inject
81    public void injectDependencies(DistributionManager distributionManager, ClusteringDependentLogic cdl) {
82       this.dm = distributionManager;
83       this.cdl = cdl;
84    }
85
86    @Override
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);
90
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);
98
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);
104             }
105          }
106       }
107
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
111       return null;
112    }
113
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");
117       }
118
119       RecipientGenerator recipientGenerator = new SingleKeyRecipientGenerator(command.getKey());
120
121       // see if we need to load values from remote sources first
122       remoteGetBeforeWrite(ctx, command, recipientGenerator);
123
124       // if this is local mode then skip distributing
125       if (isLocalModeForced(command)) {
126          return invokeNextInterceptor(ctx, command);
127       }
128
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()) {
135                     log.tracef(
136                             "Skipping the replication of the conditional command as it did not succeed on primary owner (%s).",
137                             command);
138                     return returnValue;
139                 }
140                 rpcManager.invokeRemotely(recipientGenerator.generateRecipients(), command,
141                         rpcManager.getDefaultRpcOptions(isSync));
142             } else {
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());
147             }
148          return returnValue;
149       } else {
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);
155                return result;
156             }
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));
166             }
167             return result;
168          } else {
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());
171
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;
177
178             return getResponseFromPrimaryOwner(primaryOwner, addressResponseMap);
179          }
180       }
181    }
182
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);
187          return null;
188       }
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);
192       } else {
193          return ((SuccessfulResponse) fromPrimaryOwner).getResponseValue();
194       }
195    }
196
197    protected abstract void remoteGetBeforeWrite(InvocationContext ctx, WriteCommand command, RecipientGenerator keygen) throws Throwable;
198
199    interface RecipientGenerator {
200
201       Collection<Object> getKeys();
202
203       List<Address> generateRecipients();
204    }
205
206    class SingleKeyRecipientGenerator implements RecipientGenerator {
207       private final Object key;
208       private final Set<Object> keys;
209       private List<Address> recipients = null;
210
211       SingleKeyRecipientGenerator(Object key) {
212          this.key = key;
213          keys = Collections.singleton(key);
214       }
215
216       @Override
217       public List<Address> generateRecipients() {
218          if (recipients == null) {
219             recipients = cdl.getOwners(key);
220          }
221          return recipients;
222       }
223
224       @Override
225       public Collection<Object> getKeys() {
226          return keys;
227       }
228    }
229
230    class MultipleKeysRecipientGenerator implements RecipientGenerator {
231
232       private final Collection<Object> keys;
233       private List<Address> recipients = null;
234
235       MultipleKeysRecipientGenerator(Collection<Object> keys) {
236          this.keys = keys;
237       }
238
239       @Override
240       public List<Address> generateRecipients() {
241          if (recipients == null) {
242             recipients = cdl.getOwners(keys);
243          }
244          return recipients;
245       }
246
247       @Override
248       public Collection<Object> getKeys() {
249          return keys;
250       }
251    }
252 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.