Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kn: implement SdkBuffer #1214

Merged
merged 18 commits into from
Jan 10, 2025
Merged

kn: implement SdkBuffer #1214

merged 18 commits into from
Jan 10, 2025

Conversation

lauzadis
Copy link
Contributor

@lauzadis lauzadis commented Jan 9, 2025

Issue #

Description of changes

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@lauzadis lauzadis added the no-changelog Indicates that a changelog entry isn't required for a pull request. Use sparingly. label Jan 9, 2025

This comment has been minimized.

2 similar comments

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@lauzadis lauzadis added the acknowledge-api-break Acknowledge that a change is API breaking and may be backwards-incompatible. Review carefully! label Jan 9, 2025

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@lauzadis lauzadis marked this pull request as ready for review January 9, 2025 21:48
@lauzadis lauzadis requested a review from a team as a code owner January 9, 2025 21:48
Comment on lines +54 to +56
public suspend fun SdkSource.readToByteArray(): ByteArray = withContext(SdkDispatchers.IO) {
use { it.buffer().readByteArray() }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: the only reason I could see these were expect/actual is because of Dispatchers.IO, but we already have an expect/actual version of that called SdkDispatchers, so I've moved these to common and deleted the JVM/Native implementations

Comment on lines 12 to 19
@InternalApi
public actual object SdkDispatchers {
/**
* The CoroutineDispatcher that is designed for offloading blocking IO tasks to a shared pool of threads.
*/
public actual val IO: CoroutineDispatcher
get() = TODO("Not yet implemented")
get() = Dispatchers.IO
}
Copy link
Contributor Author

@lauzadis lauzadis Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: we made this expect/actual because Native didn't have Dispatchers.IO at the time. It does now, so we could remove SdkDispatchers and just use Dispatchers.IO everywhere... but I think we'll need it again in the future when we support platforms like JS and WASM.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless JetBrains implements dispatchers on those platforms first! 😁

Comment on lines 22 to 24
import aws.smithy.kotlin.runtime.time.TimestampFormat
import kotlinx.coroutines.IO
import kotlinx.coroutines.withContext
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why was this import added when nothing else changed in this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was working in this file earlier, this import is unused now and I'm surprised ktlint didn't complain

Comment on lines 35 to 44
/**
* Used to wrap calls to Okio, catching Okio exceptions (e.g. okio.EOFException) and throwing our own (e.g. aws.smithy.kotlin.runtime.io.EOFException).
*/
internal inline fun <T> SdkBufferedSource.wrapOkio(block: SdkBufferedSource.() -> T): T = try {
block()
} catch (e: okio.EOFException) {
throw EOFException("Okio operation failed", e)
} catch (e: okio.IOException) {
throw IOException("Okio operation failed", e)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Consider adding the Okio exception message to the wrapped exception message:

throw EOFException("Okio operation failed: ${e.message}", e)

Different loggers and exception dumpers may print an entire exception including the cause but some won't and users may only have a top-level exception message to work with in some contexts.

Comment on lines 15 to 18
public actual open class EOFException actual constructor(message: String?, cause: Throwable?) : IOException(message) {
public actual constructor() : this(null, null)
public actual constructor(message: String?) : this(message, null)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correctness: Pass the constructor cause parameter to the superclass constructor:

class EOFException actual constructor(message: String?, cause: Throwable?) : IOException(message, cause)

Comment on lines 89 to 64
actual override fun readShortLe(): Short {
TODO("Not yet implemented")
actual override fun emit() {
wrapOkio { inner.emit() }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Same as JVM implementation? Can this be moved to common?

Comment on lines 30 to 34
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is SdkBuffer) return false
return inner == other.inner
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Same as JVM implementation? Can this be moved to common?

Comment on lines 12 to 19
@InternalApi
public actual object SdkDispatchers {
/**
* The CoroutineDispatcher that is designed for offloading blocking IO tasks to a shared pool of threads.
*/
public actual val IO: CoroutineDispatcher
get() = TODO("Not yet implemented")
get() = Dispatchers.IO
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless JetBrains implements dispatchers on those platforms first! 😁

@@ -4,157 +4,102 @@
*/
package aws.smithy.kotlin.runtime.io

import okio.Buffer
import aws.smithy.kotlin.runtime.io.internal.*

public actual class SdkBuffer :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Do we still need platform-specific implementations of SDK buffer? Seems like basically everything is commonized or commonizable at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference I see is JVM has inputStream and outputStream methods. I'll see if this can be made common.

I think at some point we were considering using CRT for Native I/O. Okio is multiplatform so I was thinking we should just use that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Their IO model matches ours better than CRT iirc.

This comment has been minimized.

Copy link

Affected Artifacts

Changed in size
Artifact Pull Request (bytes) Latest Release (bytes) Delta (bytes) Delta (percentage)
http-test-jvm.jar 61,633 58,718 2,915 4.96%
crt-util-jvm.jar 21,448 20,954 494 2.36%
runtime-core-jvm.jar 824,791 812,469 12,322 1.52%
aws-signing-tests-jvm.jar 456,614 456,568 46 0.01%
test-suite-jvm.jar 96,903 97,180 -277 -0.29%

@lauzadis lauzadis merged commit 2653fbf into kn-main Jan 10, 2025
18 checks passed
@lauzadis lauzadis deleted the kn-buffer branch January 10, 2025 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledge-api-break Acknowledge that a change is API breaking and may be backwards-incompatible. Review carefully! no-changelog Indicates that a changelog entry isn't required for a pull request. Use sparingly.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants