Search in sources :

Example 71 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class OpExecutorImpl method execute.

public Object execute(Op op, int retries) {
    if (this.serverAffinity.get()) {
        ServerLocation loc = this.affinityServerLocation.get();
        if (loc == null) {
            loc = getNextOpServerLocation();
            this.affinityServerLocation.set(loc);
            if (logger.isDebugEnabled()) {
                logger.debug("setting server affinity to {}", this.affinityServerLocation.get());
            }
        }
        return executeWithServerAffinity(loc, op);
    }
    boolean success = false;
    Set attemptedServers = new HashSet();
    Connection conn = (Connection) (threadLocalConnections ? localConnection.get() : null);
    if (conn == null || conn.isDestroyed()) {
        conn = connectionManager.borrowConnection(serverTimeout);
    } else if (threadLocalConnections) {
        // Fix for 43718. Clear the thread local connection
        // while we're performing the op. It will be reset
        // if the op succeeds.
        localConnection.set(null);
        try {
            this.connectionManager.activate(conn);
        } catch (ConnectionDestroyedException ex) {
            conn = connectionManager.borrowConnection(serverTimeout);
        }
    }
    try {
        for (int attempt = 0; true; attempt++) {
            // attempt's version stamp
            if (attempt == 1 && (op instanceof AbstractOp)) {
                AbstractOp absOp = (AbstractOp) op;
                absOp.getMessage().setIsRetry();
            }
            try {
                authenticateIfRequired(conn, op);
                Object result = executeWithPossibleReAuthentication(conn, op);
                success = true;
                return result;
            } catch (MessageTooLargeException e) {
                throw new GemFireIOException("unable to transmit message to server", e);
            } catch (Exception e) {
                // This method will throw an exception if we need to stop
                // It also unsets the threadlocal connection and notifies
                // the connection manager if there are failures.
                handleException(e, conn, attempt, attempt >= retries && retries != -1);
                attemptedServers.add(conn.getServer());
                try {
                    conn = connectionManager.exchangeConnection(conn, attemptedServers, serverTimeout);
                } catch (NoAvailableServersException nse) {
                    // if retries is -1, don't try again after the last server has failed
                    if (retries == -1 || TRY_SERVERS_ONCE) {
                        handleException(e, conn, attempt, true);
                    } else {
                        // try one of the failed servers again, until we exceed the retry attempts.
                        attemptedServers.clear();
                        try {
                            conn = connectionManager.exchangeConnection(conn, attemptedServers, serverTimeout);
                        } catch (NoAvailableServersException nse2) {
                            handleException(e, conn, attempt, true);
                        }
                    }
                }
            }
        }
    } finally {
        if (threadLocalConnections) {
            this.connectionManager.passivate(conn, success);
            // Fix for 43718. If the thread local was set to a different
            // connection deeper in the call stack, return that connection
            // and set our connection on the thread local.
            Connection existingConnection = localConnection.get();
            if (existingConnection != null && existingConnection != conn) {
                connectionManager.returnConnection(existingConnection);
            }
            if (!conn.isDestroyed()) {
                localConnection.set(conn);
            } else {
                localConnection.set(null);
            }
        } else {
            connectionManager.returnConnection(conn);
        }
    }
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) MessageTooLargeException(org.apache.geode.internal.cache.tier.sockets.MessageTooLargeException) TransactionException(org.apache.geode.cache.TransactionException) SocketException(java.net.SocketException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) PutAllPartialResultException(org.apache.geode.internal.cache.PutAllPartialResultException) BatchException(org.apache.geode.internal.cache.tier.BatchException) ConnectionDestroyedException(org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) CacheRuntimeException(org.apache.geode.cache.CacheRuntimeException) EOFException(java.io.EOFException) BufferUnderflowException(java.nio.BufferUnderflowException) AuthenticationRequiredException(org.apache.geode.security.AuthenticationRequiredException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException) MessageTooLargeException(org.apache.geode.internal.cache.tier.sockets.MessageTooLargeException) ConnectionDestroyedException(org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException)

Example 72 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class OpExecutorImpl method executeOnServer.

private Object executeOnServer(ServerLocation p_server, Op op, boolean accessed, boolean onlyUseExistingCnx) {
    ServerLocation server = p_server;
    boolean returnCnx = true;
    boolean pingOp = (op instanceof PingOp.PingOpImpl);
    Connection conn = null;
    if (pingOp) {
        // not create a pooled cnx when all we have is queue cnxs.
        if (this.queueManager != null) {
            // see if our QueueManager has a connection to this guy that we can send
            // the ping on.
            Endpoint ep = (Endpoint) this.endpointManager.getEndpointMap().get(server);
            if (ep != null) {
                QueueConnections qcs = this.queueManager.getAllConnectionsNoWait();
                conn = qcs.getConnection(ep);
                if (conn != null) {
                    // we found one to do the ping on
                    returnCnx = false;
                }
            }
        }
    }
    if (conn == null) {
        if (useThreadLocalConnection(op, pingOp)) {
            // no need to set threadLocal to null while the op is in progress since
            // 43718 does not impact single-hop
            conn = getActivatedThreadLocalConnectionForSingleHop(server, onlyUseExistingCnx);
            returnCnx = false;
        } else {
            conn = connectionManager.borrowConnection(server, serverTimeout, onlyUseExistingCnx);
        }
    }
    boolean success = true;
    try {
        return executeWithPossibleReAuthentication(conn, op);
    } catch (Exception e) {
        success = false;
        // This method will throw an exception if we need to stop
        // It also unsets the threadlocal connection and notifies
        // the connection manager if there are failures.
        handleException(e, conn, 0, true);
        // this shouldn't actually be reached, handle exception will throw something
        throw new ServerConnectivityException("Received error connecting to server", e);
    } finally {
        if (this.serverAffinity.get() && this.affinityServerLocation.get() == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("setting server affinity to {} server:{}", conn.getEndpoint().getMemberId(), conn.getServer());
            }
            this.affinityServerLocation.set(conn.getServer());
        }
        if (useThreadLocalConnection(op, pingOp)) {
            this.connectionManager.passivate(conn, success);
            setThreadLocalConnectionForSingleHop(server, conn);
        }
        if (returnCnx) {
            connectionManager.returnConnection(conn, accessed);
        }
    }
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation) QueueConnections(org.apache.geode.cache.client.internal.QueueManager.QueueConnections) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) MessageTooLargeException(org.apache.geode.internal.cache.tier.sockets.MessageTooLargeException) TransactionException(org.apache.geode.cache.TransactionException) SocketException(java.net.SocketException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) PutAllPartialResultException(org.apache.geode.internal.cache.PutAllPartialResultException) BatchException(org.apache.geode.internal.cache.tier.BatchException) ConnectionDestroyedException(org.apache.geode.cache.client.internal.pooling.ConnectionDestroyedException) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) CacheRuntimeException(org.apache.geode.cache.CacheRuntimeException) EOFException(java.io.EOFException) BufferUnderflowException(java.nio.BufferUnderflowException) AuthenticationRequiredException(org.apache.geode.security.AuthenticationRequiredException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException)

Example 73 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class RemoveAllOp method execute.

/**
   * Does a region put on a server using connections from the given pool to communicate with the
   * server.
   * 
   * @param pool the pool to use to communicate with the server.
   * @param region the name of the region to do the removeAll on
   * @param keys the Collection of keys to remove
   * @param eventId the event id for this removeAll
   */
public static VersionedObjectList execute(ExecutablePool pool, Region region, Collection<Object> keys, EventID eventId, int retryAttempts, Object callbackArg) {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    ClientMetadataService cms = ((LocalRegion) region).getCache().getClientMetadataService();
    Map<ServerLocation, HashSet> serverToFilterMap = cms.getServerToFilterMap(keys, region, true);
    if (serverToFilterMap == null || serverToFilterMap.isEmpty()) {
        AbstractOp op = new RemoveAllOpImpl(region, keys, eventId, ((PoolImpl) pool).getPRSingleHopEnabled(), callbackArg);
        op.initMessagePart();
        return (VersionedObjectList) pool.execute(op);
    }
    List callableTasks = constructAndGetRemoveAllTasks(region, eventId, serverToFilterMap, (PoolImpl) pool, callbackArg);
    if (isDebugEnabled) {
        logger.debug("RemoveAllOp#execute : Number of removeAll tasks is :{}", callableTasks.size());
    }
    HashMap<ServerLocation, RuntimeException> failedServers = new HashMap<ServerLocation, RuntimeException>();
    PutAllPartialResult result = new PutAllPartialResult(keys.size());
    try {
        Map<ServerLocation, Object> results = SingleHopClientExecutor.submitBulkOp(callableTasks, cms, (LocalRegion) region, failedServers);
        for (Map.Entry<ServerLocation, Object> entry : results.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof PutAllPartialResultException) {
                PutAllPartialResultException pap = (PutAllPartialResultException) value;
                if (isDebugEnabled) {
                    logger.debug("RemoveAll SingleHop encountered BulkOpPartialResultException exception: {}, failedServers are {}", pap, failedServers.keySet());
                }
                result.consolidate(pap.getResult());
            } else {
                if (value != null) {
                    VersionedObjectList list = (VersionedObjectList) value;
                    result.addKeysAndVersions(list);
                }
            }
        }
    } catch (RuntimeException ex) {
        logger.debug("single-hop removeAll encountered unexpected exception: {}", ex);
        throw ex;
    }
    if (!failedServers.isEmpty()) {
        if (retryAttempts == 0) {
            throw failedServers.values().iterator().next();
        }
        // add them to the partial result set
        if (result.getSucceededKeysAndVersions().size() == 0) {
            // if there're failed servers, we need to save the succeed keys in submitRemoveAll
            // if retry succeeded, everything is ok, otherwise, the saved "succeeded
            // keys" should be consolidated into PutAllPartialResultException
            // succeedKeySet is used to send back to client in PartialResult case
            // so it's not a must to use LinkedHashSet
            Set succeedKeySet = new LinkedHashSet();
            Set<ServerLocation> serverSet = serverToFilterMap.keySet();
            for (ServerLocation server : serverSet) {
                if (!failedServers.containsKey(server)) {
                    succeedKeySet.addAll(serverToFilterMap.get(server));
                }
            }
            // save succeedKeys, but if retries all succeeded, discard the PutAllPartialResult
            result.addKeys(succeedKeySet);
        }
        // send maps for the failed servers one by one instead of merging
        // them into one big map. The reason is, we have to keep the same event
        // ids for each sub map. There is a unit test in PutAllCSDUnitTest for
        // the otherwise case.
        boolean oneSubMapRetryFailed = false;
        Set<ServerLocation> failedServerSet = failedServers.keySet();
        for (ServerLocation failedServer : failedServerSet) {
            // Throwable failedServers.values().iterator().next();
            RuntimeException savedRTE = failedServers.get(failedServer);
            if (savedRTE instanceof PutAllPartialResultException) {
                // will not retry for BulkOpPartialResultException
                // but it means at least one sub map ever failed
                oneSubMapRetryFailed = true;
                continue;
            }
            Collection<Object> newKeys = serverToFilterMap.get(failedServer);
            try {
                VersionedObjectList v = RemoveAllOp.execute(pool, region, newKeys, eventId, true, callbackArg);
                if (v == null) {
                    result.addKeys(newKeys);
                } else {
                    result.addKeysAndVersions(v);
                }
            } catch (PutAllPartialResultException pre) {
                oneSubMapRetryFailed = true;
                logger.debug("Retry failed with BulkOpPartialResultException: {} Before retry: {}", pre, result.getKeyListString());
                result.consolidate(pre.getResult());
            } catch (Exception rte) {
                oneSubMapRetryFailed = true;
                Object firstKey = newKeys.iterator().next();
                result.saveFailedKey(firstKey, rte);
            }
        }
        // If all retries succeeded, the PRE in first tries can be ignored
        if (oneSubMapRetryFailed && result.hasFailure()) {
            PutAllPartialResultException pre = new PutAllPartialResultException(result);
            throw pre;
        }
    }
    return result.getSucceededKeysAndVersions();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) HashMap(java.util.HashMap) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) PutAllPartialResult(org.apache.geode.internal.cache.PutAllPartialResultException.PutAllPartialResult) CacheClosedException(org.apache.geode.cache.CacheClosedException) PutAllPartialResultException(org.apache.geode.internal.cache.PutAllPartialResultException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) PutAllPartialResultException(org.apache.geode.internal.cache.PutAllPartialResultException) ArrayList(java.util.ArrayList) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 74 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class ExecuteFunctionOp method constructAndGetFunctionTasks.

static List constructAndGetFunctionTasks(final PoolImpl pool, final Function function, Object args, MemberMappedArgument memberMappedArg, byte hasResult, ResultCollector rc, boolean isFnSerializationReqd, UserAttributes attributes) {
    final List<SingleHopOperationCallable> tasks = new ArrayList<SingleHopOperationCallable>();
    List<ServerLocation> servers = pool.getConnectionSource().getAllServers();
    for (ServerLocation server : servers) {
        final AbstractOp op = new ExecuteFunctionOpImpl(function, args, memberMappedArg, hasResult, rc, isFnSerializationReqd, (byte) 0, null, /* onGroups does not use single-hop for now */
        false, false);
        SingleHopOperationCallable task = new SingleHopOperationCallable(server, pool, op, attributes);
        tasks.add(task);
    }
    return tasks;
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ArrayList(java.util.ArrayList)

Example 75 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class ExecuteRegionFunctionSingleHopOp method constructAndGetExecuteFunctionTasks.

static List<SingleHopOperationCallable> constructAndGetExecuteFunctionTasks(String region, ServerRegionFunctionExecutor serverRegionExecutor, final Map<ServerLocation, ? extends HashSet> serverToFilterMap, final PoolImpl pool, final Function function, byte hasResult, ResultCollector rc, ClientMetadataService cms, boolean allBucket) {
    final List<SingleHopOperationCallable> tasks = new ArrayList<SingleHopOperationCallable>();
    ArrayList<ServerLocation> servers = new ArrayList<ServerLocation>(serverToFilterMap.keySet());
    if (logger.isDebugEnabled()) {
        logger.debug("Constructing tasks for the servers {}", servers);
    }
    for (ServerLocation server : servers) {
        ServerRegionFunctionExecutor executor = (ServerRegionFunctionExecutor) serverRegionExecutor.withFilter(serverToFilterMap.get(server));
        AbstractOp op = new ExecuteRegionFunctionSingleHopOpImpl(region, function, executor, rc, hasResult, new HashSet<String>(), allBucket);
        SingleHopOperationCallable task = new SingleHopOperationCallable(new ServerLocation(server.getHostName(), server.getPort()), pool, op, UserAttributes.userAttributes.get());
        tasks.add(task);
    }
    return tasks;
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ArrayList(java.util.ArrayList) ServerRegionFunctionExecutor(org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor)

Aggregations

ServerLocation (org.apache.geode.distributed.internal.ServerLocation)95 ArrayList (java.util.ArrayList)26 Test (org.junit.Test)21 HashSet (java.util.HashSet)19 List (java.util.List)18 HashMap (java.util.HashMap)17 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)17 ServerConnectivityException (org.apache.geode.cache.client.ServerConnectivityException)13 Map (java.util.Map)12 Iterator (java.util.Iterator)10 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)9 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 Set (java.util.Set)8 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)8 LinkedHashSet (java.util.LinkedHashSet)7 Host (org.apache.geode.test.dunit.Host)7 VersionedObjectList (org.apache.geode.internal.cache.tier.sockets.VersionedObjectList)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5