|
| 1 | +package io.hstream.impl |
| 2 | + |
| 3 | +import com.google.protobuf.InvalidProtocolBufferException |
| 4 | +import io.hstream.* |
| 5 | +import io.hstream.ReceivedRecord |
| 6 | +import io.hstream.internal.* |
| 7 | +import io.hstream.util.GrpcUtils |
| 8 | +import io.hstream.util.RecordUtils |
| 9 | +import kotlinx.coroutines.* |
| 10 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 11 | +import org.slf4j.LoggerFactory |
| 12 | +import java.time.Instant |
| 13 | +import java.util.UUID |
| 14 | +import java.util.concurrent.ArrayBlockingQueue |
| 15 | +import java.util.concurrent.Executors |
| 16 | +import java.util.concurrent.TimeUnit |
| 17 | +import java.util.concurrent.atomic.AtomicBoolean |
| 18 | +import java.util.concurrent.atomic.AtomicReference |
| 19 | + |
| 20 | +class StreamKeyReaderKtImpl( |
| 21 | + private val client: HStreamClientKtImpl, |
| 22 | + private val streamName: String, |
| 23 | + private val key: String, |
| 24 | + private val from: StreamShardOffset, |
| 25 | + private val until: StreamShardOffset?, |
| 26 | + private val bufferSize: Int, |
| 27 | +) : StreamKeyReader { |
| 28 | + private val readerScope = CoroutineScope(Dispatchers.IO) |
| 29 | + private val readerName: String = UUID.randomUUID().toString() |
| 30 | + private val executorService = Executors.newSingleThreadExecutor() |
| 31 | + private val requestFlow = MutableSharedFlow<ReadStreamByKeyRequest>() |
| 32 | + private val buffer = ArrayBlockingQueue<ReceivedRecord>(bufferSize) |
| 33 | + private val exceptionRef: AtomicReference<Exception> = AtomicReference(null) |
| 34 | + private val isStopped: AtomicBoolean = AtomicBoolean(false) |
| 35 | + |
| 36 | + init { |
| 37 | + doStart() |
| 38 | + } |
| 39 | + |
| 40 | + private fun doStart() { |
| 41 | + logger.info("streamKeyReader $readerName is starting") |
| 42 | + val lookupRequest = LookupResourceRequest.newBuilder() |
| 43 | + .setResType(ResourceType.ResStream) |
| 44 | + .setResId(streamName) |
| 45 | + .build() |
| 46 | + val lookupResp = client.unaryCallBlocked { it.lookupResource(lookupRequest) } |
| 47 | + val serverUrl = lookupResp.host + ":" + lookupResp.port |
| 48 | + val requestBuilder = ReadStreamByKeyRequest.newBuilder() |
| 49 | + .setReaderId(readerName) |
| 50 | + .setStreamName(streamName) |
| 51 | + .setKey(key) |
| 52 | + .setFrom(GrpcUtils.streamShardOffsetToGrpc(from)) |
| 53 | + .setReadRecordCount(bufferSize.toLong()) |
| 54 | + if (until != null) { |
| 55 | + requestBuilder.until = GrpcUtils.streamShardOffsetToGrpc(until) |
| 56 | + } |
| 57 | + val respFlow = client.getCoroutineStub(serverUrl).readStreamByKey(requestFlow) |
| 58 | + readerScope.launch { |
| 59 | + launch { |
| 60 | + // wait until rpc called |
| 61 | + while (requestFlow.subscriptionCount.value == 0) { |
| 62 | + delay(100) |
| 63 | + } |
| 64 | + try{ |
| 65 | + requestFlow.emit(requestBuilder.build()) |
| 66 | + } catch (e: Exception) { |
| 67 | + logger.error("steamKeyReader $readerName failed", e) |
| 68 | + exceptionRef.compareAndSet(null, e) |
| 69 | + isStopped.set(true) |
| 70 | + } |
| 71 | + } |
| 72 | + launch { |
| 73 | + try { |
| 74 | + respFlow.collect { |
| 75 | + saveToBuffer(it) |
| 76 | + } |
| 77 | + } catch (e: Exception) { |
| 78 | + logger.error("steamKeyReader $readerName failed", e) |
| 79 | + exceptionRef.compareAndSet(null, e) |
| 80 | + isStopped.set(true) |
| 81 | + } |
| 82 | + // wait for saveToBuffer complete |
| 83 | + delay(100) |
| 84 | + isStopped.set(true) |
| 85 | + logger.info("server stopped") |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override fun close() { |
| 92 | + readerScope.cancel() |
| 93 | + executorService.shutdownNow() |
| 94 | + logger.info("StreamKeyReader $readerName closed") |
| 95 | + } |
| 96 | + |
| 97 | + override fun hasNext() : Boolean { |
| 98 | + return !isStopped.get() || !buffer.isEmpty() |
| 99 | + } |
| 100 | + |
| 101 | + override fun next(): ReceivedRecord? { |
| 102 | + |
| 103 | + var res: ReceivedRecord? = null |
| 104 | + |
| 105 | + while (res == null) { |
| 106 | + res = buffer.poll(100, TimeUnit.MILLISECONDS) |
| 107 | + |
| 108 | + if(res == null) { |
| 109 | + val e = exceptionRef.get() |
| 110 | + if(e != null) throw e |
| 111 | + |
| 112 | + if(isStopped.get()) return null |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + readerScope.launch { |
| 117 | + try{ |
| 118 | + requestFlow.emit(ReadStreamByKeyRequest.newBuilder().setReadRecordCount(1).build()) |
| 119 | + } catch (e: Exception) { |
| 120 | + logger.error("steamKeyReader $readerName failed", e) |
| 121 | + exceptionRef.compareAndSet(null, e) |
| 122 | + isStopped.set(true) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return res |
| 127 | + } |
| 128 | + |
| 129 | + private fun saveToBuffer(value: ReadStreamByKeyResponse) { |
| 130 | + for ((i, receivedRecord) in value.receivedRecordsList.withIndex()) { |
| 131 | + val recordId = value.getRecordIds(i) |
| 132 | + executorService.submit { |
| 133 | + val res = toReceivedRecord(receivedRecord, recordId, Instant.now()) |
| 134 | + buffer.put(res) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + companion object { |
| 140 | + private val logger = LoggerFactory.getLogger(StreamKeyReaderKtImpl::class.java) |
| 141 | + |
| 142 | + private fun toReceivedRecord(hStreamRecord: HStreamRecord, recordId: RecordId, createdTime: Instant): ReceivedRecord { |
| 143 | + return try { |
| 144 | + val header = RecordUtils.parseRecordHeaderFromHStreamRecord(hStreamRecord) |
| 145 | + if (RecordUtils.isRawRecord(hStreamRecord)) { |
| 146 | + |
| 147 | + val rawRecord = RecordUtils.parseRawRecordFromHStreamRecord(hStreamRecord) |
| 148 | + ReceivedRecord( |
| 149 | + GrpcUtils.recordIdFromGrpc(recordId), |
| 150 | + Record.newBuilder().partitionKey(header.partitionKey).rawRecord(rawRecord).build(), |
| 151 | + createdTime |
| 152 | + ) |
| 153 | + } else { |
| 154 | + val hRecord = RecordUtils.parseHRecordFromHStreamRecord(hStreamRecord) |
| 155 | + ReceivedRecord( |
| 156 | + GrpcUtils.recordIdFromGrpc(recordId), |
| 157 | + Record.newBuilder().partitionKey(header.partitionKey).hRecord(hRecord).build(), |
| 158 | + createdTime |
| 159 | + ) |
| 160 | + } |
| 161 | + } catch (e: InvalidProtocolBufferException) { |
| 162 | + throw HStreamDBClientException.InvalidRecordException("parse HStreamRecord error", e) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments