-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry connection failure while instantiating connection provider (#144)
Signed-off-by: Khor Shu Heng <[email protected]> Co-authored-by: Khor Shu Heng <[email protected]>
- Loading branch information
1 parent
2871ba6
commit fcb80a0
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
spark/ingestion/src/main/scala/feast/ingestion/errorhanders/RetryStrategy.scala
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,37 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright 2018-2022 The Feast Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 feast.ingestion.errorhanders | ||
|
||
import scala.annotation.tailrec | ||
import scala.concurrent.duration.Duration | ||
|
||
object RetryStrategy { | ||
|
||
@tailrec | ||
def fixedBackOff[T <: Exception, U](retryInterval: Duration, maxRetries: Int)( | ||
fn: => Either[T, U] | ||
): U = { | ||
fn match { | ||
case Right(x) => x | ||
case Left(_) if maxRetries > 0 => { | ||
Thread.sleep(retryInterval.toMillis) | ||
fixedBackOff(retryInterval, maxRetries - 1)(fn) | ||
} | ||
case Left(e) => throw (e) | ||
} | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
spark/ingestion/src/test/scala/feast/ingestion/errorhandlers/RetryStrategySpec.scala
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,68 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright 2018-2022 The Feast Authors | ||
* | ||
* 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 | ||
* | ||
* https://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 feast.ingestion.errorhandlers | ||
|
||
import feast.ingestion.UnitSpec | ||
import feast.ingestion.errorhanders.RetryStrategy | ||
|
||
import scala.concurrent.duration.DurationInt | ||
import scala.util.{Failure, Try} | ||
|
||
class RetryStrategySpec extends UnitSpec { | ||
|
||
case class SomeException(message: String) extends Exception | ||
|
||
"function that always succeed" should "not be retried" in { | ||
var i = 0 | ||
|
||
def alwaysSucceedFunc: Either[SomeException, Int] = { | ||
i += 1 | ||
Right(0) | ||
} | ||
val result = RetryStrategy.fixedBackOff(1.second, 2)(alwaysSucceedFunc) | ||
i should be(1) | ||
result should be(0) | ||
} | ||
|
||
"function that always failed" should "be retried up to the maximum attempt and throw exception" in { | ||
var i = 0 | ||
|
||
def alwaysFailFunc: Either[SomeException, Int] = { | ||
i += 1 | ||
Left(SomeException("error")) | ||
} | ||
val result = Try { RetryStrategy.fixedBackOff(10.milli, 2)(alwaysFailFunc) } | ||
i should be(3) | ||
result should matchPattern { case Failure(_: SomeException) => } | ||
} | ||
|
||
"function that succeeds when retried" should "immediately return value when succeeded" in { | ||
var i = 0 | ||
|
||
def succeedWhenRetriedFunc: Either[SomeException, Int] = { | ||
i += 1 | ||
if (i < 2) { | ||
Left(SomeException("error")) | ||
} else { | ||
Right(0) | ||
} | ||
} | ||
val result = RetryStrategy.fixedBackOff(1.second, 2)(succeedWhenRetriedFunc) | ||
i should be(2) | ||
result should be(0) | ||
} | ||
} |