Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,973 additions
and
86 deletions.
There are no files selected for viewing
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
111 changes: 111 additions & 0 deletions
111
.../main/java/com/datastax/oss/driver/api/core/connection/ExponentialReconnectionPolicy.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,111 @@ | ||
/* | ||
* Copyright (C) 2017-2017 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.oss.driver.api.core.connection; | ||
|
||
import com.datastax.oss.driver.api.core.config.CoreDriverOption; | ||
import com.datastax.oss.driver.api.core.config.DriverConfigProfile; | ||
import com.datastax.oss.driver.api.core.context.DriverContext; | ||
import com.google.common.base.Preconditions; | ||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A reconnection policy that waits exponentially longer between each reconnection attempt (but | ||
* keeps a constant delay once a maximum delay is reached). | ||
*/ | ||
public class ExponentialReconnectionPolicy implements ReconnectionPolicy { | ||
|
||
private final long baseDelayMs; | ||
private final long maxDelayMs; | ||
private final long maxAttempts; | ||
|
||
/** Builds a new instance. */ | ||
public ExponentialReconnectionPolicy(DriverContext context) { | ||
DriverConfigProfile config = context.config().defaultProfile(); | ||
this.baseDelayMs = | ||
config.getDuration(CoreDriverOption.RECONNECTION_CONFIG_BASE_DELAY, TimeUnit.MILLISECONDS); | ||
this.maxDelayMs = | ||
config.getDuration(CoreDriverOption.RECONNECTION_CONFIG_MAX_DELAY, TimeUnit.MILLISECONDS); | ||
|
||
Preconditions.checkArgument( | ||
baseDelayMs > 0, | ||
"%s must be strictly positive (got %s)", | ||
CoreDriverOption.RECONNECTION_CONFIG_BASE_DELAY.getPath(), | ||
baseDelayMs); | ||
Preconditions.checkArgument( | ||
maxDelayMs >= 0, | ||
"%s must be positive (got %s)", | ||
CoreDriverOption.RECONNECTION_CONFIG_MAX_DELAY.getPath(), | ||
maxDelayMs); | ||
Preconditions.checkArgument( | ||
maxDelayMs >= baseDelayMs, | ||
"%s must be bigger than %s (got %s, %s)", | ||
CoreDriverOption.RECONNECTION_CONFIG_MAX_DELAY.getPath(), | ||
CoreDriverOption.RECONNECTION_CONFIG_BASE_DELAY.getPath(), | ||
maxDelayMs, | ||
baseDelayMs); | ||
|
||
// Maximum number of attempts after which we overflow | ||
int ceil = (baseDelayMs & (baseDelayMs - 1)) == 0 ? 0 : 1; | ||
this.maxAttempts = 64 - Long.numberOfLeadingZeros(Long.MAX_VALUE / baseDelayMs) - ceil; | ||
} | ||
|
||
/** | ||
* The base delay in milliseconds for this policy (e.g. the delay before the first reconnection | ||
* attempt). | ||
* | ||
* @return the base delay in milliseconds for this policy. | ||
*/ | ||
public long getBaseDelayMs() { | ||
return baseDelayMs; | ||
} | ||
|
||
/** | ||
* The maximum delay in milliseconds between reconnection attempts for this policy. | ||
* | ||
* @return the maximum delay in milliseconds between reconnection attempts for this policy. | ||
*/ | ||
public long getMaxDelayMs() { | ||
return maxDelayMs; | ||
} | ||
|
||
/** | ||
* A new schedule that used an exponentially growing delay between reconnection attempts. | ||
* | ||
* <p>For this schedule, reconnection attempt {@code i} will be tried {@code Math.min(2^(i-1) * | ||
* getBaseDelayMs(), getMaxDelayMs())} milliseconds after the previous one. | ||
* | ||
* @return the newly created schedule. | ||
*/ | ||
@Override | ||
public ReconnectionSchedule newSchedule() { | ||
return new ExponentialSchedule(); | ||
} | ||
|
||
private class ExponentialSchedule implements ReconnectionSchedule { | ||
|
||
private int attempts; | ||
|
||
@Override | ||
public Duration nextDelay() { | ||
long delay = | ||
(attempts > maxAttempts) | ||
? maxDelayMs | ||
: Math.min(baseDelayMs * (1L << attempts++), maxDelayMs); | ||
return Duration.ofMillis(delay); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
core/src/main/java/com/datastax/oss/driver/api/core/connection/HeartbeatException.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,27 @@ | ||
/* | ||
* Copyright (C) 2017-2017 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.oss.driver.api.core.connection; | ||
|
||
public class HeartbeatException extends ConnectionException { | ||
|
||
public HeartbeatException(String message) { | ||
super(message); | ||
} | ||
|
||
public HeartbeatException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
core/src/main/java/com/datastax/oss/driver/internal/core/channel/AvailableIdsHolder.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,25 @@ | ||
/* | ||
* Copyright (C) 2017-2017 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.oss.driver.internal.core.channel; | ||
|
||
/** | ||
* Hack to expose the number of available ids on a channel. We want to access it on the {@link | ||
* DriverChannel} instance, but it's updated from {@link InFlightHandler}, and we want volatile for | ||
* efficiency. | ||
*/ | ||
class AvailableIdsHolder { | ||
volatile int value; | ||
} |
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
Oops, something went wrong.