All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.logstash.logback.appender.destination.PreferPrimaryDestinationConnectionStrategy Maven / Gradle / Ivy

There is a newer version: 7.2.0
Show newest version
/**
 * 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 net.logstash.logback.appender.destination;

import org.apache.juli.logging.ch.qos.logback.core.util.Duration;

/**
 * The first destination is considered the "primary" destination.
 * The remaining destinations are considered "secondary" destinations.
 * 

* Prefer to connect to the primary destination. * If the primary destination is down, try other destinations. * *

* Connections are attempted to each destination in order until a connection succeeds. * Logs will be sent to the first destination for which the connection succeeds * until the connection breaks or (if the connection is to a secondary destination) * the {@link #secondaryConnectionTTL} elapses. */ public class PreferPrimaryDestinationConnectionStrategy implements DestinationConnectionStrategy { /** * The "primary" destination index; */ private static final int PRIMARY_DESTINATION_INDEX = 0; /** * Time period for connections to secondary destinations to be used * before attempting to reconnect to primary destination. * * When the value is null (the default), the feature is disabled: * the appender will stay on the current destination until an error occurs. */ private Duration secondaryConnectionTTL; /** * The destinationIndex to be returned on the next call to {@link #selectNextDestinationIndex(int, int)}. */ private volatile int nextDestinationIndex = PRIMARY_DESTINATION_INDEX; /** * Time at which the current connection should be automatically closed * to force an attempt to reconnect to the primary server */ private volatile long secondaryConnectionExpirationTime = Long.MAX_VALUE; @Override public int selectNextDestinationIndex(int previousDestinationIndex, int numDestinations) { return nextDestinationIndex; } @Override public void connectSuccess(long connectionStartTimeInMillis, int connectedDestinationIndex, int numDestinations) { /* * If connected to a secondary, remember when the connection should be closed to * force attempt to reconnect to primary */ if (secondaryConnectionTTL != null && connectedDestinationIndex != PRIMARY_DESTINATION_INDEX) { secondaryConnectionExpirationTime = connectionStartTimeInMillis + secondaryConnectionTTL.getMilliseconds(); } else { secondaryConnectionExpirationTime = Long.MAX_VALUE; } nextDestinationIndex = PRIMARY_DESTINATION_INDEX; } @Override public void connectFailed(long connectionStartTimeInMillis, int failedDestinationIndex, int numDestinations) { nextDestinationIndex = (failedDestinationIndex + 1) % numDestinations; } @Override public boolean shouldReconnect(long currentTimeInMillis, int currentDestinationIndex, int numDestinations) { return secondaryConnectionExpirationTime <= currentTimeInMillis; } public Duration getSecondaryConnectionTTL() { return secondaryConnectionTTL; } /** * Time period for connections to secondary destinations to be used * before attempting to reconnect to primary destination. * * When the value is null (the default), the feature is disabled: * the appender will stay on the current destination until an error occurs. * * @param secondaryConnectionTTL */ public void setSecondaryConnectionTTL(Duration secondaryConnectionTTL) { if (secondaryConnectionTTL != null && secondaryConnectionTTL.getMilliseconds() <= 0) { throw new IllegalArgumentException("secondaryConnectionTTL must be > 0"); } this.secondaryConnectionTTL = secondaryConnectionTTL; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy