
org.infinispan.query.clustered.ClusteredQueryInvoker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of infinispan-embedded-query
Show all versions of infinispan-embedded-query
Infinispan Embedded Query All-in-One module
The newest version!
package org.infinispan.query.clustered;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.hibernate.search.exception.SearchException;
import org.infinispan.Cache;
import org.infinispan.remoting.responses.Response;
import org.infinispan.remoting.responses.SuccessfulResponse;
import org.infinispan.remoting.rpc.ResponseMode;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.remoting.rpc.RpcOptions;
import org.infinispan.remoting.transport.Address;
/**
* Invoke a ClusteredQueryCommand on the cluster, including on own node.
*
* @author Israel Lacerra
* @author Sanne Grinovero (C) 2011 Red Hat Inc.
* @since 5.1
*/
public class ClusteredQueryInvoker {
private final RpcManager rpcManager;
private final Cache, ?> localCacheInstance;
private final Address myAddress;
private final ExecutorService asyncExecutor;
private final RpcOptions rpcOptions;
ClusteredQueryInvoker(Cache, ?> localCacheInstance, ExecutorService asyncExecutor) {
this.asyncExecutor = asyncExecutor;
this.rpcManager = localCacheInstance.getAdvancedCache().getComponentRegistry().getLocalComponent(RpcManager.class);
this.localCacheInstance = localCacheInstance;
this.myAddress = localCacheInstance.getAdvancedCache().getRpcManager().getAddress();
this.rpcOptions = rpcManager.getRpcOptionsBuilder(ResponseMode.SYNCHRONOUS).timeout(10000, TimeUnit.MILLISECONDS).build();
}
/**
* Retrieves the value (using doc index) in a remote query instance
*
* @param doc
* Doc index of the value on remote query
* @param address
* Address of the node who has the value
* @param queryId
* Id of the query
* @return The value of index doc of the query with queryId on node at address
*/
public Object getValue(int doc, Address address, UUID queryId) {
ClusteredQueryCommand clusteredQuery = ClusteredQueryCommand.retrieveKeyFromLazyQuery(
localCacheInstance, queryId, doc);
if (address.equals(myAddress)) {
Future localResponse = localInvoke(clusteredQuery);
try {
return localResponse.get().getFetchedValue();
} catch (InterruptedException e) {
throw new SearchException("interrupted while searching locally", e);
} catch (ExecutionException e) {
throw new SearchException("Exception while searching locally", e);
}
} else {
List addresss = new ArrayList(1);
addresss.add(address);
Map responses = rpcManager.invokeRemotely(addresss, clusteredQuery, rpcOptions);
List objects = cast(responses);
return objects.get(0).getFetchedValue();
}
}
/**
* Broadcast this ClusteredQueryCommand to all cluster nodes. The command will be also invoked on
* local node.
*
* @param clusteredQuery
* @return A list with all responses
*/
public List broadcast(ClusteredQueryCommand clusteredQuery) {
// invoke on own node
Future localResponse = localInvoke(clusteredQuery);
Map responses = rpcManager.invokeRemotely(null, clusteredQuery, rpcOptions);
List objects = cast(responses);
final QueryResponse localReturnValue;
try {
localReturnValue = localResponse.get();
} catch (InterruptedException e1) {
throw new SearchException("interrupted while searching locally", e1);
} catch (ExecutionException e1) {
throw new SearchException("Exception while searching locally", e1);
}
objects.add(localReturnValue);
return objects;
}
private Future localInvoke(ClusteredQueryCommand clusteredQuery) {
ClusteredQueryCallable clusteredQueryCallable = new ClusteredQueryCallable(clusteredQuery,
localCacheInstance);
return asyncExecutor.submit(clusteredQueryCallable);
}
private List cast(Map responses) {
List objects = new LinkedList();
for (Entry pair : responses.entrySet()) {
Response resp = pair.getValue();
if (resp instanceof SuccessfulResponse) {
QueryResponse response = (QueryResponse) ((SuccessfulResponse) resp).getResponseValue();
objects.add(response);
} else {
throw new SearchException("Unexpected response: " + resp);
}
}
return objects;
}
/**
* Created to call a ClusteredQueryCommand on own node.
*
* @author Israel Lacerra
* @since 5.1
*/
private static final class ClusteredQueryCallable implements Callable {
private final ClusteredQueryCommand clusteredQuery;
private final Cache, ?> localInstance;
ClusteredQueryCallable(ClusteredQueryCommand clusteredQuery, Cache, ?> localInstance) {
this.clusteredQuery = clusteredQuery;
this.localInstance = localInstance;
}
@Override
public QueryResponse call() throws Exception {
try {
return clusteredQuery.perform(localInstance);
} catch (Throwable e) {
throw new Exception(e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy