@@ -23,32 +23,34 @@ import kotlin.coroutines.CoroutineContext
2323val logger: Logger = LoggerFactory .getLogger(" io.hstream.impl.Utils" )
2424
2525suspend fun <Resp > unaryCallWithCurrentUrlsCoroutine (serverUrls : List <String >, channelProvider : ChannelProvider , call : suspend (stub: HStreamApiCoroutineStub ) -> Resp ): Resp {
26+ // Note: A failed grpc call can throw both 'StatusException' and 'StatusRuntimeException'.
27+ // This function is for handling them.
28+ suspend fun handleGRPCException (i : Int , e : Throwable ) {
29+ logger.error(" call unary rpc with url [{}] error" , serverUrls[i], e)
30+ val status = Status .fromThrowable(e)
31+ if (status.code == Status .UNAVAILABLE .code) {
32+ if (i == serverUrls.size - 1 ) {
33+ throw HStreamDBClientException (e)
34+ } else {
35+ logger.info(" unary rpc will be retried with url [{}]" , serverUrls[i + 1 ])
36+ delay(DefaultSettings .REQUEST_RETRY_INTERVAL_SECONDS * 1000 )
37+ return
38+ }
39+ } else {
40+ throw HStreamDBClientException (e)
41+ }
42+ }
43+
2644 check(serverUrls.isNotEmpty())
2745 logger.debug(" call unaryCallWithCurrentUrlsCoroutine with urls [{}]" , serverUrls)
2846 for (i in serverUrls.indices) {
2947 val stub = HStreamApiCoroutineStub (channelProvider.get(serverUrls[i]))
3048 try {
3149 return call(stub)
32- } catch (e: Exception ) {
33- // Note: a failed grpc call can throw both 'StatusException' and 'StatusRuntimeException'.
34- when (e) {
35- is StatusException , is StatusRuntimeException -> {
36- logger.error(" call unary rpc with url [{}] error" , serverUrls[i], e)
37- val status = Status .fromThrowable(e)
38- if (status.code == Status .UNAVAILABLE .code) {
39- if (i == serverUrls.size - 1 ) {
40- throw HStreamDBClientException (e)
41- } else {
42- logger.info(" unary rpc will be retried with url [{}]" , serverUrls[i + 1 ])
43- delay(DefaultSettings .REQUEST_RETRY_INTERVAL_SECONDS * 1000 )
44- continue
45- }
46- } else {
47- throw HStreamDBClientException (e)
48- }
49- }
50- else -> throw e
51- }
50+ } catch (e: StatusException ) {
51+ handleGRPCException(i, e)
52+ } catch (e: StatusRuntimeException ) {
53+ handleGRPCException(i, e)
5254 }
5355 }
5456
@@ -72,28 +74,31 @@ suspend fun refreshClusterInfo(serverUrls: List<String>, channelProvider: Channe
7274}
7375
7476suspend fun <Resp > unaryCallCoroutine (urlsRef : AtomicReference <List <String >>, channelProvider : ChannelProvider , call : suspend (stub: HStreamApiCoroutineStub ) -> Resp ): Resp {
77+ // Note: A failed grpc call can throw both 'StatusException' and 'StatusRuntimeException'.
78+ // This function is for handling them.
79+ suspend fun handleGRPCException (urls : List <String >, e : Throwable ): Resp {
80+ logger.error(" unary rpc error with url [{}]" , urls[0 ], e)
81+ val status = Status .fromThrowable(e)
82+ if (status.code == Status .UNAVAILABLE .code && urls.size > 1 ) {
83+ val newServerUrls = refreshClusterInfo(urls.subList(1 , urls.size), channelProvider)
84+ urlsRef.set(newServerUrls)
85+ return unaryCallWithCurrentUrlsCoroutine(urlsRef.get(), channelProvider, call)
86+ } else {
87+ throw HStreamDBClientException (e)
88+ }
89+ }
90+
7591 val urls = urlsRef.get()
7692 check(urls.isNotEmpty())
7793
7894 logger.debug(" unary rpc with urls [{}]" , urls)
7995
8096 try {
8197 return call(HStreamApiCoroutineStub (channelProvider.get(urls[0 ])))
82- } catch (e: Exception ) {
83- when (e) {
84- is StatusException , is StatusRuntimeException -> {
85- logger.error(" unary rpc error with url [{}]" , urls[0 ], e)
86- val status = Status .fromThrowable(e)
87- if (status.code == Status .UNAVAILABLE .code && urls.size > 1 ) {
88- val newServerUrls = refreshClusterInfo(urls.subList(1 , urls.size), channelProvider)
89- urlsRef.set(newServerUrls)
90- return unaryCallWithCurrentUrlsCoroutine(urlsRef.get(), channelProvider, call)
91- } else {
92- throw HStreamDBClientException (e)
93- }
94- }
95- else -> throw e
96- }
98+ } catch (e: StatusException ) {
99+ return handleGRPCException(urls, e)
100+ } catch (e: StatusRuntimeException ) {
101+ return handleGRPCException(urls, e)
97102 }
98103}
99104
0 commit comments