Skip to content

Commit

Permalink
JAVA-3166: Enhanced exception handling of C* driver executeAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-antoniak committed Aug 26, 2024
1 parent d2d13b2 commit 585472d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.datastax.spark.connector.writer

import java.util.concurrent.{CompletionStage, Semaphore}
import java.util.concurrent.{CompletableFuture, CompletionStage, Semaphore}
import java.util.function.BiConsumer

import com.datastax.spark.connector.util.Logging

import scala.jdk.CollectionConverters._
Expand Down Expand Up @@ -41,7 +40,12 @@ class AsyncExecutor[T, R](asyncAction: T => CompletionStage[R], maxConcurrentTas
val executionTimestamp = System.nanoTime()

def tryFuture(): Future[R] = {
val value = asyncAction(task)
var value : CompletionStage[R] = null
try {
value = asyncAction(task)
} catch {
case e: RuntimeException => value = CompletableFuture.failedStage(e);
}

value.whenComplete(new BiConsumer[R, Throwable] {
private def release() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.datastax.spark.connector.writer

import com.datastax.oss.driver.api.core.cql.{AsyncResultSet, SimpleStatement, Statement}

import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.{Callable, CompletableFuture, CompletionStage}

Expand Down Expand Up @@ -55,4 +57,20 @@ class AsyncExecutorTest {
totalFinishedExecutionsCounter.get() shouldBe taskCount
asyncExecutor.getLatestException() shouldBe None
}

@Test
def testGracefullyHandleCqlSessionExecuteExceptions() {
val executor = new AsyncExecutor[Statement[_], AsyncResultSet](
_ => {
// simulate exception returned by session.executeAsync() (not future)
throw new IllegalStateException("something bad happened")
}, 10, None, None
)
val stmt = SimpleStatement.newInstance("INSERT INTO table1 (key, value) VALUES (1, '100')");
val future = executor.executeAsync(stmt)
assertTrue(future.isCompleted)
val value = future.value.get
assertTrue(value.isInstanceOf[Failure[_]])
assertTrue(value.asInstanceOf[Failure[_]].exception.isInstanceOf[IllegalStateException])
}
}

0 comments on commit 585472d

Please sign in to comment.