-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial Redis client implementation (#5)
- Loading branch information
Showing
9 changed files
with
278 additions
and
31 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
modules/redis-client/src/main/scala/io/renku/queue/client/Message.scala
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,25 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.queue.client | ||
|
||
import scodec.bits.ByteVector | ||
|
||
final case class Message(id: MessageId, payload: ByteVector) | ||
|
||
final case class MessageId(value: String) extends AnyVal |
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
24 changes: 24 additions & 0 deletions
24
modules/redis-client/src/main/scala/io/renku/queue/client/QueueName.scala
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,24 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.queue.client | ||
|
||
opaque type QueueName = String | ||
object QueueName { | ||
def apply(v: String): QueueName = new QueueName(v) | ||
} |
60 changes: 60 additions & 0 deletions
60
modules/redis-client/src/main/scala/io/renku/redis/client/RedisQueueClient.scala
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,60 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.redis.client | ||
|
||
import cats.effect.Async | ||
import cats.syntax.all.* | ||
import dev.profunktor.redis4cats.connection.RedisClient | ||
import dev.profunktor.redis4cats.effect.Log | ||
import dev.profunktor.redis4cats.streams.RedisStream | ||
import dev.profunktor.redis4cats.streams.data.{XAddMessage, XReadMessage} | ||
import fs2.Stream | ||
import io.renku.queue.client.{Message, MessageId, QueueClient, QueueName} | ||
import scodec.bits.ByteVector | ||
|
||
class RedisQueueClient[F[_]: Async: Log](client: RedisClient) extends QueueClient[F] { | ||
|
||
private val payloadKey = "payload" | ||
|
||
override def enqueue(queueName: QueueName, message: ByteVector): F[Unit] = | ||
val m = Stream | ||
.emit[F, XAddMessage[String, ByteVector]]( | ||
XAddMessage(queueName.toString, Map(payloadKey -> message)) | ||
) | ||
createConnection.flatMap(_.append(m)).compile.drain | ||
|
||
override def acquireEventsStream( | ||
queueName: QueueName, | ||
chunkSize: Int | ||
): Stream[F, Message] = | ||
createConnection >>= { | ||
_.read(Set(queueName.toString), chunkSize) | ||
.map(toMessage) | ||
.collect { case Some(m) => m } | ||
} | ||
|
||
private def toMessage(m: XReadMessage[String, ByteVector]): Option[Message] = | ||
m.body | ||
.get(payloadKey) | ||
.map(Message(MessageId(m.id.value), _)) | ||
|
||
private def createConnection = | ||
RedisStream | ||
.mkStreamingConnection[F, String, ByteVector](client, StringBytesCodec.instance) | ||
} |
44 changes: 44 additions & 0 deletions
44
modules/redis-client/src/main/scala/io/renku/redis/client/StringBytesCodec.scala
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,44 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.redis.client | ||
|
||
import dev.profunktor.redis4cats.data.RedisCodec | ||
import io.lettuce.core.codec.{ByteArrayCodec, RedisCodec as JRedisCodec, StringCodec} | ||
import scodec.bits.ByteVector | ||
|
||
import java.nio.ByteBuffer | ||
|
||
object StringBytesCodec: | ||
|
||
val instance: RedisCodec[String, ByteVector] = RedisCodec { | ||
new JRedisCodec[String, ByteVector] { | ||
|
||
override def decodeKey(bytes: ByteBuffer): String = | ||
StringCodec.UTF8.decodeKey(bytes) | ||
|
||
override def decodeValue(bytes: ByteBuffer): ByteVector = | ||
ByteVector.view(ByteArrayCodec.INSTANCE.decodeValue(bytes)) | ||
|
||
override def encodeKey(key: String): ByteBuffer = | ||
StringCodec.UTF8.encodeKey(key) | ||
|
||
override def encodeValue(value: ByteVector): ByteBuffer = | ||
ByteArrayCodec.INSTANCE.encodeValue(value.toArray) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
modules/redis-client/src/test/scala/io/renku/redis/client/RedisClientGenerators.scala
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,34 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.redis.client | ||
|
||
import io.renku.queue.client.QueueName | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Gen.alphaLowerChar | ||
|
||
object RedisClientGenerators: | ||
|
||
val queueNameGen: Gen[QueueName] = | ||
Gen | ||
.chooseNum(3, 10) | ||
.flatMap(Gen.stringOfN(_, alphaLowerChar).map(QueueName(_))) | ||
|
||
given Gen[QueueName] = queueNameGen | ||
|
||
extension [V](gen: Gen[V]) def generateOne: V = gen.sample.getOrElse(generateOne) |
60 changes: 60 additions & 0 deletions
60
modules/redis-client/src/test/scala/io/renku/redis/client/RedisQueueClientSpec.scala
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,60 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.redis.client | ||
|
||
import cats.effect.IO | ||
import fs2.* | ||
import fs2.concurrent.SignallingRef | ||
import io.renku.redis.client.RedisClientGenerators.* | ||
import io.renku.redis.client.util.RedisSpec | ||
import munit.CatsEffectSuite | ||
import scodec.bits.ByteVector | ||
|
||
class RedisQueueClientSpec extends CatsEffectSuite with RedisSpec: | ||
|
||
test("can enqueue and dequeue events"): | ||
withRedisClient.asQueueClient().use { client => | ||
val queue = RedisClientGenerators.queueNameGen.generateOne | ||
for | ||
dequeued <- SignallingRef.of[IO, List[String]](Nil) | ||
|
||
message1 = "message1" | ||
_ <- client.enqueue(queue, toByteVector(message1)) | ||
|
||
fiber <- client | ||
.acquireEventsStream(queue, chunkSize = 1) | ||
.evalMap(event => dequeued.update(toStringUft8(event.payload) :: _)) | ||
.compile | ||
.drain | ||
.start | ||
_ <- dequeued.waitUntil(_ == List(message1)) | ||
|
||
message2 = "message2" | ||
_ <- client.enqueue(queue, toByteVector(message2)) | ||
_ <- dequeued.waitUntil(_.toSet == Set(message1, message2)) | ||
|
||
_ <- fiber.cancel | ||
yield () | ||
} | ||
|
||
private def toByteVector(v: String): ByteVector = | ||
ByteVector.encodeUtf8(v).fold(throw _, identity) | ||
|
||
private lazy val toStringUft8: ByteVector => String = | ||
_.decodeUtf8.fold(throw _, identity) |
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