-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into cbini/398-add-support-latest-version-jsons…
…chema-library
- Loading branch information
Showing
31 changed files
with
439 additions
and
321 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
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
74 changes: 0 additions & 74 deletions
74
airbyte-cdk/bulk/core/load/src/main/kotlin/io/airbyte/cdk/load/state/MemoryManager.kt
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
airbyte-cdk/bulk/core/load/src/main/kotlin/io/airbyte/cdk/load/state/ReservationManager.kt
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,79 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.state | ||
|
||
import io.airbyte.cdk.load.util.CloseableCoroutine | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
import java.util.concurrent.atomic.AtomicLong | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
/** Releasable reservation of memory. */ | ||
class Reserved<T>( | ||
private val parentManager: ReservationManager, | ||
val bytesReserved: Long, | ||
val value: T, | ||
) : CloseableCoroutine { | ||
private var released = AtomicBoolean(false) | ||
|
||
suspend fun release() { | ||
if (!released.compareAndSet(false, true)) { | ||
return | ||
} | ||
parentManager.release(bytesReserved) | ||
} | ||
|
||
fun <U> replace(value: U): Reserved<U> = Reserved(parentManager, bytesReserved, value) | ||
|
||
override suspend fun close() { | ||
release() | ||
} | ||
} | ||
|
||
/** | ||
* Manages memory usage for the destination. | ||
* | ||
* TODO: Better initialization of available runtime memory? | ||
* | ||
* TODO: Some degree of logging/monitoring around how accurate we're actually being? | ||
*/ | ||
class ReservationManager(val totalCapacityBytes: Long) { | ||
|
||
private var usedBytes = AtomicLong(0L) | ||
private var updateChannel = MutableStateFlow(0L) | ||
private val reserveLock = Mutex() | ||
|
||
val remainingCapacityBytes: Long | ||
get() = totalCapacityBytes - usedBytes.get() | ||
|
||
/* Attempt to reserve memory. If enough memory is not available, waits until it is, then reserves. */ | ||
suspend fun <T> reserve(bytes: Long, reservedFor: T): Reserved<T> { | ||
reserve(bytes) | ||
|
||
return Reserved(this, bytes, reservedFor) | ||
} | ||
|
||
/* Attempt to reserve memory. If enough memory is not available, waits until it is, then reserves. */ | ||
suspend fun reserve(bytes: Long) { | ||
if (bytes > totalCapacityBytes) { | ||
throw IllegalArgumentException( | ||
"Requested ${bytes}b exceeds ${totalCapacityBytes}b total" | ||
) | ||
} | ||
|
||
reserveLock.withLock { | ||
while (usedBytes.get() + bytes > totalCapacityBytes) { | ||
updateChannel.first() | ||
} | ||
usedBytes.addAndGet(bytes) | ||
} | ||
} | ||
|
||
suspend fun release(bytes: Long) { | ||
updateChannel.value = usedBytes.addAndGet(-bytes) | ||
} | ||
} |
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.