Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ val allModules = mapOf(
Pair("here-naksha-lib-auth", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-lib-base", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-lib-core", Pair(CleanAndTest.OFF, PublishModule.YES)),
Pair("here-naksha-lib-circuit-breaker", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-lib-diff", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-lib-ext-manager", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-lib-geo", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Descriptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private val Descriptions = mapOf(
//"here-naksha-lib-auth-naksha" to "TBD",
"here-naksha-lib-base" to "Naksha library, providing basic cross-platform capabilities (extending the raw Kotlin features).",
"here-naksha-lib-core" to "Naksha library, core parts of Naksha-Hub.",
"here-naksha-lib-circuit-breaker" to "Naksha Circuit Breaker Library",
"here-naksha-lib-diff" to "Naksha library, provide tools to calculate differences, patched, and to apply them.",
"here-naksha-lib-ext-manager" to "Naksha library, the Extension-Manager library to be included in applications to load extensions.",
//"here-naksha-lib-extension" to "TBD",
Expand Down
73 changes: 73 additions & 0 deletions docs/HTTP_STORAGE_CIRCUIT_BREAKER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# HTTP Storage Circuit Breaker

This document describes how circuit breaker works for [`com.here.naksha.storage.http.HttpStorage`](../here-naksha-storage-http/src/jvmMain/java/com/here/naksha/storage/http/HttpStorage.java).

## Where it is applied

The CircuitBreaker is applied whenever Naksha wants to forward a request to HttpStorage.

**Key Points:**
- The CB config is read from the individual HTTP Storage definition.
- At runtime, the CB logic is executed against individual `storageId`.
- Different Storage definitions can imply different CB behavior based on their respective configurations.
- Whenever a Storage definition changes (via storage `updatedAt` value), the internal CB cache for that `storageId` is invalidated and a new CB is created with the updated configuration.
- This ensures that configuration changes are immediately reflected in the CB behavior without requiring an application restart.

## Why it exists

The goal is to isolate slow or failing requests by `storageId`.
If one storage starts timing out or responding slowly, only that storage's traffic should be throttled.
Requests for other storages should not be affected.


## HTTP Storage Configuration

For detailed configuration properties and their validation rules, refer to:
- [HttpStorageProperties](../here-naksha-storage-http/src/jvmMain/java/com/here/naksha/storage/http/HttpStorageProperties.java) - HTTP storage-specific settings (URL, timeouts, headers, etc.)
- [CircuitBreakerProps](../here-naksha-lib-circuit-breaker/src/jvmMain/java/com/here/naksha/lib/circuitbreaker/models/CircuitBreakerProps.java) - Circuit breaker configuration

### Example Storage JSON

```json
{
"id": "my_storage",
"type": "Storage",
"className": "com.here.naksha.storage.http.HttpStorage",
"properties": {
"url": "https://example.com/api",
"connectTimeout": 10,
"socketTimeout": 30,
"httpInterface": "ffwAdapter",
"maxRetries": 3,
"headers": {
"Authorization": "Bearer token123"
},
"circuitBreakerConfig": {
"slidingWindowSize": 20,
"minimumNumberOfCalls": 10,
"slowCallDurationThresholdMs": 29000,
"slowCallRateThreshold": 50,
"waitDurationInOpenStateMs": 120000,
"permittedNumberOfCallsInHalfOpenState": 4
}
}
}
```

**Note:** The `circuitBreakerConfig` configuration is optional. If omitted, HTTP requests are sent without circuit breaking.


## CircuitBreaker Behavior

- **CLOSED**: This is default state during start of application where requests flow normally.
- **OPEN**: Requests are rejected immediately when circuit is in OPEN state. Circuit moves to or remains in OPEN state whenever the slow call rate exceeds `slowCallRateThreshold` (after minimum `minimumNumberOfCalls` have been observed).
- **HALF_OPEN**: Circuit transitions from OPEN to HALF_OPEN after waiting `waitDurationInOpenStateMs`. Limited number of probe calls (`permittedNumberOfCallsInHalfOpenState`) are allowed. It moves back to OPEN state if slow call rate exceeds `slowCallRateThreshold`. It moves to CLOSED state if slow call rate drops below `slowCallRateThreshold`.
- If probe calls are fast enough, the breaker closes.
- If probe calls are still slow, the breaker opens again.

## Notes

- The breaker is configured per `storageId`.
- Storage updates are detected through `updatedAt`.
- The storage JSON validation happens when storages are created or updated through the storage API.

3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jvm.here_naksha_lib_hub=23
jvm.here_naksha_lib_ext_manager=23
jvm.here_naksha_handler_activitylog=23

jvm.here_naksha_lib_circuit_breaker=17
jvm.here_naksha_storage_http=17
jvm.here_naksha_lib_handlers=17
# In Linux: ~/.gradle/gradle.properties
# If publishing to maven central, use: https://mvnrepository.com/artifact/io.github.naksha-oss/
mavenUrl=some_maven_repo_publish_url
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gson = "2.14.0" # https://github.com/google/gson
jsonio = "4.108.0" # https://github.com/jdereg/json-io
fastjson = "2.0.57" # https://github.com/alibaba/fastjson
simdjson = "0.4.0" # https://github.com/simdjson/simdjson-java
resilience4j = "2.4.0"

[libraries]
jetbrains_annotations = { module = "org.jetbrains:annotations", version = "26.1.0" }
Expand Down Expand Up @@ -106,7 +107,6 @@ test_containers_postgres = { module = "org.testcontainers:postgresql", versio

flipkart_zjsonpatch = "com.flipkart.zjsonpatch:zjsonpatch:0.4.16"
json_assert = "org.skyscreamer:jsonassert:1.5.3"
resillience4j_retry = "io.github.resilience4j:resilience4j-retry:2.3.0"
otel = "io.opentelemetry:opentelemetry-api:1.51.0"
cytodynamics = "com.linkedin.cytodynamics:cytodynamics-nucleus:0.2.0"
kotlinx_datetime = "org.jetbrains.kotlinx:kotlinx-datetime:0.7.0"
Expand All @@ -120,6 +120,7 @@ gson = { module = "com.google.code.gson:gson", version.re
jsonio = { module = "com.cedarsoftware:json-io", version.ref = "jsonio" }
fastjson = { module = "com.alibaba:fastjson", version.ref="fastjson" }
simdjson = { module = "org.simdjson:simdjson-java", version.ref = "simdjson" }
resilience4j_circuitbreaker = { module = "io.github.resilience4j:resilience4j-circuitbreaker", version.ref = "resilience4j" }

[plugins]
kotlin_multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Expand Down
2 changes: 1 addition & 1 deletion here-naksha-app-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ kotlin {
jvmTest {
dependencies {
implementation(libs.bundles.testing)
implementation(libs.resillience4j.retry)
implementation(libs.resilience4j.circuitbreaker)
implementation(libs.test.containers)
runtimeOnly(libs.junit.platform.launcher) // https://github.com/gradle/gradle/issues/34512
}
Expand Down
Loading
Loading