Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  *      Copyright (C) 2012 DataStax Inc.
  *
  *   Licensed under the Apache License, Version 2.0 (the "License");
  *   you may not use this file except in compliance with the License.
  *   You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.datastax.driver.core.policies;
A retry policy that wraps another policy, logging the decision made by its sub-policy.

Note that this policy only log the IGNORE and RETRY decisions (since RETHROW decisions are just meant to propagate the cassandra exception). The logging is done at the INFO level.

public class LoggingRetryPolicy implements RetryPolicy {
    private static final Logger logger = LoggerFactory.getLogger(LoggingRetryPolicy.class);
    private final RetryPolicy policy;

    
Creates a new RetryPolicy that logs the decision of policy.

Parameters:
policy the policy to wrap. The policy created by this constructor will return the same decision than policy but will log them.
    public LoggingRetryPolicy(RetryPolicy policy) {
        this. = policy;
    }
    private static ConsistencyLevel cl(ConsistencyLevel clRetryDecision decision) {
        return decision.getRetryConsistencyLevel() == null ? cl : decision.getRetryConsistencyLevel();
    }
    @Override
    public RetryDecision onReadTimeout(Query queryConsistencyLevel clint requiredResponsesint receivedResponsesboolean dataRetrievedint nbRetry) {
        RetryDecision decision = .onReadTimeout(queryclrequiredResponsesreceivedResponsesdataRetrievednbRetry);
        switch (decision.getType()) {
            case :
                String f1 = "Ignoring read timeout (initial consistency: %s, required responses: %d, received responses: %d, data retrieved: %b, retries: %d)";
                .info(String.format(f1clrequiredResponsesreceivedResponsesdataRetrievednbRetry));
                break;
            case :
                String f2 = "Retrying on read timeout at consistency %s (initial consistency: %s, required responses: %d, received responses: %d, data retrieved: %b, retries: %d)";
                .info(String.format(f2cl(cldecision), clrequiredResponsesreceivedResponsesdataRetrievednbRetry));
                break;
        }
        return decision;
    }
    @Override
    public RetryDecision onWriteTimeout(Query queryConsistencyLevel clWriteType writeTypeint requiredAcksint receivedAcksint nbRetry) {
        RetryDecision decision = .onWriteTimeout(queryclwriteTyperequiredAcksreceivedAcksnbRetry);
        switch (decision.getType()) {
            case :
                String f1 = "Ignoring write timeout (initial consistency: %s, write type: %s, required acknowledgments: %d, received acknowledgments: %d, retries: %d)";
                .info(String.format(f1clwriteTyperequiredAcksreceivedAcksnbRetry));
                break;
            case :
                String f2 = "Retrying on write timeout at consistency %s(initial consistency: %s, write type: %s, required acknowledgments: %d, received acknowledgments: %d, retries: %d)";
                .info(String.format(f2cl(cldecision), clwriteTyperequiredAcksreceivedAcksnbRetry));
                break;
        }
        return decision;
    }
    @Override
    public RetryDecision onUnavailable(Query queryConsistencyLevel clint requiredReplicaint aliveReplicaint nbRetry) {
        RetryDecision decision = .onUnavailable(queryclrequiredReplicaaliveReplicanbRetry);
        switch (decision.getType()) {
            case :
                String f1 = "Ignoring unavailable exception (initial consistency: %s, required replica: %d, alive replica: %d, retries: %d)";
                .info(String.format(f1clrequiredReplicaaliveReplicanbRetry));
                break;
            case :
                String f2 = "Retrying on unavailable exception at consistency %s (initial consistency: %s, required replica: %d, alive replica: %d, retries: %d)";
                .info(String.format(f2cl(cldecision), clrequiredReplicaaliveReplicanbRetry));
                break;
        }
        return decision;
    }
New to GrepCode? Check out our FAQ X