Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAA-896: add ability to set failover strategy for failover manager
- Loading branch information
Showing
7 changed files
with
205 additions
and
59 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...ore/src/main/java/org/kaaproject/kaa/client/channel/failover/DefaultFailoverStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright 2014-2016 CyberVision, 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 org.kaaproject.kaa.client.channel.failover; | ||
|
||
import org.kaaproject.kaa.client.channel.FailoverDecision; | ||
import org.kaaproject.kaa.client.channel.FailoverDecision.FailoverAction; | ||
import org.kaaproject.kaa.client.channel.FailoverStatus; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Reference implementation for {@link FailoverStrategy}. | ||
*/ | ||
public class DefaultFailoverStrategy implements FailoverStrategy { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DefaultFailoverStrategy.class); | ||
|
||
private static final long DEFAULT_BOOTSTRAP_SERVERS_RETRY_PERIOD = 2; | ||
private static final long DEFAULT_OPERATION_SERVERS_RETRY_PERIOD = 2; | ||
private static final long DEFAULT_NO_CONNECTIVITY_RETRY_PERIOD = 5; | ||
private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS; | ||
|
||
private long bootstrapServersRetryPeriod; | ||
private long operationsServersRetryPeriod; | ||
private long noConnectivityRetryPeriod; | ||
private TimeUnit timeUnit; | ||
|
||
public DefaultFailoverStrategy() { | ||
this(DEFAULT_BOOTSTRAP_SERVERS_RETRY_PERIOD, | ||
DEFAULT_OPERATION_SERVERS_RETRY_PERIOD, | ||
DEFAULT_NO_CONNECTIVITY_RETRY_PERIOD, | ||
DEFAULT_TIME_UNIT); | ||
} | ||
|
||
public DefaultFailoverStrategy(long bootstrapServersRetryPeriod, | ||
long operationsServersRetryPeriod, | ||
long noConnectivityRetryPeriod, | ||
TimeUnit timeUnit) { | ||
this.bootstrapServersRetryPeriod = bootstrapServersRetryPeriod; | ||
this.operationsServersRetryPeriod = operationsServersRetryPeriod; | ||
this.noConnectivityRetryPeriod = noConnectivityRetryPeriod; | ||
this.timeUnit = timeUnit; | ||
} | ||
|
||
@Override | ||
public FailoverDecision onFailover(FailoverStatus failoverStatus) { | ||
LOG.trace("Producing failover decision for failover status: {}", failoverStatus); | ||
switch (failoverStatus) { | ||
case BOOTSTRAP_SERVERS_NA: | ||
return new FailoverDecision(FailoverAction.RETRY, bootstrapServersRetryPeriod, timeUnit); | ||
case CURRENT_BOOTSTRAP_SERVER_NA: | ||
return new FailoverDecision(FailoverAction.USE_NEXT_BOOTSTRAP, bootstrapServersRetryPeriod, timeUnit); | ||
case NO_OPERATION_SERVERS_RECEIVED: | ||
return new FailoverDecision(FailoverAction.USE_NEXT_BOOTSTRAP, bootstrapServersRetryPeriod, timeUnit); | ||
case OPERATION_SERVERS_NA: | ||
return new FailoverDecision(FailoverAction.RETRY, operationsServersRetryPeriod, timeUnit); | ||
case NO_CONNECTIVITY: | ||
return new FailoverDecision(FailoverAction.RETRY, noConnectivityRetryPeriod, timeUnit); | ||
default: | ||
return new FailoverDecision(FailoverAction.NOOP); | ||
} | ||
} | ||
|
||
@Override | ||
public long getBootstrapServersRetryPeriod() { | ||
return bootstrapServersRetryPeriod; | ||
} | ||
|
||
@Override | ||
public long getOperationServersRetryPeriod() { | ||
return operationsServersRetryPeriod; | ||
} | ||
|
||
@Override | ||
public TimeUnit getTimeUnit() { | ||
return timeUnit; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...-java-core/src/main/java/org/kaaproject/kaa/client/channel/failover/FailoverStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2014-2016 CyberVision, 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 org.kaaproject.kaa.client.channel.failover; | ||
|
||
import org.kaaproject.kaa.client.channel.FailoverDecision; | ||
import org.kaaproject.kaa.client.channel.FailoverStatus; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Failover strategy is responsible for producing failover decisions based on failover statuses. | ||
*/ | ||
public interface FailoverStrategy { | ||
|
||
/** | ||
* Needs to be invoked to determine a decision that resolves the failover. | ||
* | ||
* @param failoverStatus current status of the failover. | ||
* | ||
* @return decision which is meant to resolve the failover. | ||
* | ||
* @see org.kaaproject.kaa.client.channel.FailoverDecision | ||
* @see org.kaaproject.kaa.client.channel.FailoverStatus | ||
*/ | ||
FailoverDecision onFailover(FailoverStatus failoverStatus); | ||
|
||
/** | ||
* Use the {@link #getTimeUnit()} method to get current time unit. | ||
* | ||
* @return period of time after which will be made attempt to tweak bootstrap server. | ||
*/ | ||
long getBootstrapServersRetryPeriod(); | ||
|
||
/** | ||
* Use the {@link #getTimeUnit()} method to get current time unit. | ||
* | ||
* @return period of time after which will be made attempt to tweak operation server. | ||
*/ | ||
long getOperationServersRetryPeriod(); | ||
|
||
/** | ||
* @return time unit used within a scope of current failover strategy. | ||
*/ | ||
TimeUnit getTimeUnit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters