github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/grpcwrapper/rawtopic/rawtopicreader/messages.go (about) 1 package rawtopicreader 2 3 import ( 4 "errors" 5 "time" 6 7 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Topic" 8 9 "github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawoptional" 10 "github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopiccommon" 11 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 12 ) 13 14 var ( 15 errUnexpectedNilStreamReadMessageReadResponse = xerrors.Wrap(errors.New("ydb: unexpected nil Ydb_Topic.StreamReadMessage_ReadResponse")) //nolint:lll 16 errNilPartitionData = xerrors.Wrap(errors.New("ydb: unexpected nil partition data")) 17 errUnexpectedNilBatchInPartitionData = xerrors.Wrap(errors.New("ydb: unexpected nil batch in partition data")) //nolint:lll 18 errUnexpectedMessageNilInPartitionData = xerrors.Wrap(errors.New("ydb: unexpected message nil in partition data")) //nolint:lll 19 20 errUnexpectedProtoNilStartPartitionSessionRequest = xerrors.Wrap(errors.New("ydb: unexpected proto nil start partition session request")) //nolint:lll 21 errUnexpectedNilPartitionSession = xerrors.Wrap(errors.New("ydb: unexpected proto nil partition session in start partition session request")) //nolint:lll 22 errUnexpectedGrpcNilStopPartitionSessionRequest = xerrors.Wrap(errors.New("ydb: unexpected grpc nil stop partition session request")) //nolint:lll 23 ) 24 25 type PartitionSessionID int64 26 27 func (id *PartitionSessionID) FromInt64(v int64) { 28 *id = PartitionSessionID(v) 29 } 30 31 func (id PartitionSessionID) ToInt64() int64 { 32 return int64(id) 33 } 34 35 type OptionalOffset struct { 36 Offset rawtopiccommon.Offset 37 HasValue bool 38 } 39 40 func (offset *OptionalOffset) FromInt64Pointer(v *int64) { 41 if v == nil { 42 offset.HasValue = false 43 offset.Offset.FromInt64(-1) 44 } else { 45 offset.HasValue = true 46 offset.Offset.FromInt64(*v) 47 } 48 } 49 50 func (offset *OptionalOffset) FromInt64(v int64) { 51 offset.FromInt64Pointer(&v) 52 } 53 54 func (offset OptionalOffset) ToInt64() int64 { 55 return offset.Offset.ToInt64() 56 } 57 58 func (offset OptionalOffset) ToInt64Pointer() *int64 { 59 if offset.HasValue { 60 v := offset.Offset.ToInt64() 61 62 return &v 63 } 64 65 return nil 66 } 67 68 // 69 // UpdateTokenRequest 70 // 71 72 type UpdateTokenRequest struct { 73 clientMessageImpl 74 75 rawtopiccommon.UpdateTokenRequest 76 } 77 78 type UpdateTokenResponse struct { 79 rawtopiccommon.UpdateTokenResponse 80 81 serverMessageImpl 82 83 rawtopiccommon.ServerMessageMetadata 84 } 85 86 // 87 // InitRequest 88 // 89 90 type InitRequest struct { 91 clientMessageImpl 92 93 TopicsReadSettings []TopicReadSettings 94 95 Consumer string 96 } 97 98 func (r *InitRequest) toProto() *Ydb_Topic.StreamReadMessage_InitRequest { 99 p := &Ydb_Topic.StreamReadMessage_InitRequest{ 100 Consumer: r.Consumer, 101 } 102 103 p.TopicsReadSettings = make([]*Ydb_Topic.StreamReadMessage_InitRequest_TopicReadSettings, len(r.TopicsReadSettings)) 104 for topicSettingsIndex := range r.TopicsReadSettings { 105 srcTopicSettings := &r.TopicsReadSettings[topicSettingsIndex] 106 dstTopicSettings := &Ydb_Topic.StreamReadMessage_InitRequest_TopicReadSettings{} 107 p.TopicsReadSettings[topicSettingsIndex] = dstTopicSettings 108 109 dstTopicSettings.Path = srcTopicSettings.Path 110 dstTopicSettings.MaxLag = srcTopicSettings.MaxLag.ToProto() 111 dstTopicSettings.ReadFrom = srcTopicSettings.ReadFrom.ToProto() 112 113 partitionsIDs := make([]int64, len(srcTopicSettings.PartitionsID)) 114 copy(partitionsIDs, srcTopicSettings.PartitionsID) 115 116 dstTopicSettings.PartitionIds = partitionsIDs 117 } 118 119 return p 120 } 121 122 // GetConsumer for implement trace.TopicReadStreamInitRequestInfo 123 func (r *InitRequest) GetConsumer() string { 124 return r.Consumer 125 } 126 127 // GetTopics for implement trace.TopicReadStreamInitRequestInfo 128 func (r *InitRequest) GetTopics() []string { 129 res := make([]string, len(r.TopicsReadSettings)) 130 for i := range res { 131 res[i] = r.TopicsReadSettings[i].Path 132 } 133 134 return res 135 } 136 137 type TopicReadSettings struct { 138 Path string 139 PartitionsID []int64 140 141 MaxLag rawoptional.Duration 142 ReadFrom rawoptional.Time 143 } 144 145 type InitResponse struct { 146 serverMessageImpl 147 148 rawtopiccommon.ServerMessageMetadata 149 150 SessionID string 151 } 152 153 func (g *InitResponse) fromProto(p *Ydb_Topic.StreamReadMessage_InitResponse) { 154 g.SessionID = p.GetSessionId() 155 } 156 157 // 158 // ReadRequest 159 // 160 161 type ReadRequest struct { 162 clientMessageImpl 163 164 BytesSize int 165 } 166 167 func (r *ReadRequest) toProto() *Ydb_Topic.StreamReadMessage_ReadRequest { 168 return &Ydb_Topic.StreamReadMessage_ReadRequest{BytesSize: int64(r.BytesSize)} 169 } 170 171 type ReadResponse struct { 172 serverMessageImpl 173 174 rawtopiccommon.ServerMessageMetadata 175 176 BytesSize int 177 PartitionData []PartitionData 178 } 179 180 // GetBytesSize implements trace.TopicReaderDataResponseInfo 181 func (r *ReadResponse) GetBytesSize() int { 182 return r.BytesSize 183 } 184 185 // GetPartitionBatchMessagesCounts implements trace.TopicReaderDataResponseInfo 186 func (r *ReadResponse) GetPartitionBatchMessagesCounts() (partitionDataCount, batchCount, messagesCount int) { 187 partitionDataCount = len(r.PartitionData) 188 for partitionIndex := range r.PartitionData { 189 partitionData := &r.PartitionData[partitionIndex] 190 batchCount += len(partitionData.Batches) 191 for batchIndex := range partitionData.Batches { 192 messagesCount += len(partitionData.Batches[batchIndex].MessageData) 193 } 194 } 195 196 return partitionDataCount, batchCount, messagesCount 197 } 198 199 func (r *ReadResponse) fromProto(p *Ydb_Topic.StreamReadMessage_ReadResponse) error { 200 if p == nil { 201 return xerrors.WithStackTrace(errUnexpectedNilStreamReadMessageReadResponse) 202 } 203 r.BytesSize = int(p.GetBytesSize()) 204 205 r.PartitionData = make([]PartitionData, len(p.GetPartitionData())) 206 for partitionIndex := range p.GetPartitionData() { 207 srcPartition := p.GetPartitionData()[partitionIndex] 208 if srcPartition == nil { 209 return xerrors.WithStackTrace(errNilPartitionData) 210 } 211 dstPartition := &r.PartitionData[partitionIndex] 212 dstPartition.PartitionSessionID.FromInt64(srcPartition.GetPartitionSessionId()) 213 214 dstPartition.Batches = make([]Batch, len(srcPartition.GetBatches())) 215 216 for batchIndex := range srcPartition.GetBatches() { 217 srcBatch := srcPartition.GetBatches()[batchIndex] 218 if srcBatch == nil { 219 return xerrors.WithStackTrace(errUnexpectedNilBatchInPartitionData) 220 } 221 dstBatch := &dstPartition.Batches[batchIndex] 222 223 dstBatch.ProducerID = srcBatch.GetProducerId() 224 dstBatch.WriteSessionMeta = srcBatch.GetWriteSessionMeta() 225 dstBatch.Codec.MustFromProto(Ydb_Topic.Codec(srcBatch.GetCodec())) 226 227 dstBatch.WrittenAt = srcBatch.GetWrittenAt().AsTime() 228 229 dstBatch.MessageData = make([]MessageData, len(srcBatch.GetMessageData())) 230 for messageIndex := range srcBatch.GetMessageData() { 231 srcMessage := srcBatch.GetMessageData()[messageIndex] 232 if srcMessage == nil { 233 return xerrors.WithStackTrace(errUnexpectedMessageNilInPartitionData) 234 } 235 dstMessage := &dstBatch.MessageData[messageIndex] 236 237 dstMessage.Offset.FromInt64(srcMessage.GetOffset()) 238 dstMessage.SeqNo = srcMessage.GetSeqNo() 239 dstMessage.CreatedAt = srcMessage.GetCreatedAt().AsTime() 240 dstMessage.Data = srcMessage.GetData() 241 dstMessage.UncompressedSize = srcMessage.GetUncompressedSize() 242 dstMessage.MessageGroupID = srcMessage.GetMessageGroupId() 243 if len(srcMessage.GetMetadataItems()) > 0 { 244 dstMessage.MetadataItems = make([]rawtopiccommon.MetadataItem, 0, len(srcMessage.GetMetadataItems())) 245 for _, protoItem := range srcMessage.GetMetadataItems() { 246 dstMessage.MetadataItems = append(dstMessage.MetadataItems, rawtopiccommon.MetadataItem{ 247 Key: protoItem.GetKey(), 248 Value: protoItem.GetValue()[:len(protoItem.GetValue()):len(protoItem.GetValue())], 249 }) 250 } 251 } 252 } 253 } 254 } 255 256 return nil 257 } 258 259 type PartitionData struct { 260 PartitionSessionID PartitionSessionID 261 262 Batches []Batch 263 } 264 type Batch struct { 265 Codec rawtopiccommon.Codec 266 267 ProducerID string 268 WriteSessionMeta map[string]string // nil if session meta is empty 269 WrittenAt time.Time 270 271 MessageData []MessageData 272 } 273 274 type MessageData struct { 275 Offset rawtopiccommon.Offset 276 SeqNo int64 277 CreatedAt time.Time 278 Data []byte 279 UncompressedSize int64 280 MessageGroupID string 281 MetadataItems []rawtopiccommon.MetadataItem 282 } 283 284 // 285 // CommitOffsetRequest 286 // 287 288 type CommitOffsetRequest struct { 289 clientMessageImpl 290 291 CommitOffsets []PartitionCommitOffset 292 } 293 294 func (r *CommitOffsetRequest) toProto() *Ydb_Topic.StreamReadMessage_CommitOffsetRequest { 295 res := &Ydb_Topic.StreamReadMessage_CommitOffsetRequest{} 296 res.CommitOffsets = make( 297 []*Ydb_Topic.StreamReadMessage_CommitOffsetRequest_PartitionCommitOffset, 298 len(r.CommitOffsets), 299 ) 300 301 for sessionIndex := range r.CommitOffsets { 302 srcPartitionCommitOffset := &r.CommitOffsets[sessionIndex] 303 dstCommitOffset := &Ydb_Topic.StreamReadMessage_CommitOffsetRequest_PartitionCommitOffset{ 304 PartitionSessionId: srcPartitionCommitOffset.PartitionSessionID.ToInt64(), 305 } 306 res.CommitOffsets[sessionIndex] = dstCommitOffset 307 308 dstCommitOffset.Offsets = make([]*Ydb_Topic.OffsetsRange, len(srcPartitionCommitOffset.Offsets)) 309 for offsetIndex := range srcPartitionCommitOffset.Offsets { 310 dstCommitOffset.Offsets[offsetIndex] = srcPartitionCommitOffset.Offsets[offsetIndex].ToProto() 311 } 312 } 313 314 return res 315 } 316 317 type PartitionCommitOffset struct { 318 PartitionSessionID PartitionSessionID 319 Offsets []rawtopiccommon.OffsetRange 320 } 321 322 type CommitOffsetResponse struct { 323 serverMessageImpl 324 325 rawtopiccommon.ServerMessageMetadata 326 327 PartitionsCommittedOffsets []PartitionCommittedOffset 328 } 329 330 func (r *CommitOffsetResponse) fromProto(proto *Ydb_Topic.StreamReadMessage_CommitOffsetResponse) error { 331 r.PartitionsCommittedOffsets = make([]PartitionCommittedOffset, len(proto.GetPartitionsCommittedOffsets())) 332 for i := range r.PartitionsCommittedOffsets { 333 srcCommitted := proto.GetPartitionsCommittedOffsets()[i] 334 if srcCommitted == nil { 335 return xerrors.WithStackTrace(errors.New("unexpected nil while parse commit offset response")) 336 } 337 dstCommitted := &r.PartitionsCommittedOffsets[i] 338 339 dstCommitted.PartitionSessionID.FromInt64(srcCommitted.GetPartitionSessionId()) 340 dstCommitted.CommittedOffset.FromInt64(srcCommitted.GetCommittedOffset()) 341 } 342 343 return nil 344 } 345 346 type PartitionCommittedOffset struct { 347 PartitionSessionID PartitionSessionID 348 CommittedOffset rawtopiccommon.Offset 349 } 350 351 // 352 // PartitionSessionStatusRequest 353 // 354 355 type PartitionSessionStatusRequest struct { 356 clientMessageImpl 357 358 PartitionSessionID PartitionSessionID 359 } 360 361 func (r *PartitionSessionStatusRequest) toProto() *Ydb_Topic.StreamReadMessage_PartitionSessionStatusRequest { 362 return &Ydb_Topic.StreamReadMessage_PartitionSessionStatusRequest{ 363 PartitionSessionId: r.PartitionSessionID.ToInt64(), 364 } 365 } 366 367 type PartitionSessionStatusResponse struct { 368 serverMessageImpl 369 370 rawtopiccommon.ServerMessageMetadata 371 372 PartitionSessionID PartitionSessionID 373 PartitionOffsets rawtopiccommon.OffsetRange 374 WriteTimeHighWatermark time.Time 375 } 376 377 func (r *PartitionSessionStatusResponse) fromProto( 378 p *Ydb_Topic.StreamReadMessage_PartitionSessionStatusResponse, 379 ) error { 380 r.PartitionSessionID.FromInt64(p.GetPartitionSessionId()) 381 if err := r.PartitionOffsets.FromProto(p.GetPartitionOffsets()); err != nil { 382 return err 383 } 384 r.WriteTimeHighWatermark = p.GetWriteTimeHighWatermark().AsTime() 385 386 return nil 387 } 388 389 // 390 // StartPartitionSessionRequest 391 // 392 393 type StartPartitionSessionRequest struct { 394 serverMessageImpl 395 396 rawtopiccommon.ServerMessageMetadata 397 398 PartitionSession PartitionSession 399 CommittedOffset rawtopiccommon.Offset 400 PartitionOffsets rawtopiccommon.OffsetRange 401 } 402 403 func (r *StartPartitionSessionRequest) fromProto(p *Ydb_Topic.StreamReadMessage_StartPartitionSessionRequest) error { 404 if p == nil { 405 return xerrors.WithStackTrace(errUnexpectedProtoNilStartPartitionSessionRequest) 406 } 407 408 if p.GetPartitionSession() == nil { 409 return xerrors.WithStackTrace(errUnexpectedNilPartitionSession) 410 } 411 r.PartitionSession.PartitionID = p.GetPartitionSession().GetPartitionId() 412 r.PartitionSession.Path = p.GetPartitionSession().GetPath() 413 r.PartitionSession.PartitionSessionID.FromInt64(p.GetPartitionSession().GetPartitionSessionId()) 414 415 r.CommittedOffset.FromInt64(p.GetCommittedOffset()) 416 417 return r.PartitionOffsets.FromProto(p.GetPartitionOffsets()) 418 } 419 420 type PartitionSession struct { 421 PartitionSessionID PartitionSessionID 422 Path string // Topic path of partition 423 PartitionID int64 424 } 425 426 type StartPartitionSessionResponse struct { 427 clientMessageImpl 428 429 PartitionSessionID PartitionSessionID 430 ReadOffset OptionalOffset 431 CommitOffset OptionalOffset 432 } 433 434 func (r *StartPartitionSessionResponse) toProto() *Ydb_Topic.StreamReadMessage_StartPartitionSessionResponse { 435 res := &Ydb_Topic.StreamReadMessage_StartPartitionSessionResponse{ 436 PartitionSessionId: r.PartitionSessionID.ToInt64(), 437 ReadOffset: r.ReadOffset.ToInt64Pointer(), 438 CommitOffset: r.CommitOffset.ToInt64Pointer(), 439 } 440 441 return res 442 } 443 444 // 445 // StopPartitionSessionRequest 446 // 447 448 type StopPartitionSessionRequest struct { 449 serverMessageImpl 450 451 rawtopiccommon.ServerMessageMetadata 452 453 PartitionSessionID PartitionSessionID 454 Graceful bool 455 CommittedOffset rawtopiccommon.Offset 456 } 457 458 func (r *StopPartitionSessionRequest) fromProto(proto *Ydb_Topic.StreamReadMessage_StopPartitionSessionRequest) error { 459 if proto == nil { 460 return xerrors.WithStackTrace(errUnexpectedGrpcNilStopPartitionSessionRequest) 461 } 462 r.PartitionSessionID.FromInt64(proto.GetPartitionSessionId()) 463 r.Graceful = proto.GetGraceful() 464 r.CommittedOffset.FromInt64(proto.GetCommittedOffset()) 465 466 return nil 467 } 468 469 type StopPartitionSessionResponse struct { 470 clientMessageImpl 471 472 PartitionSessionID PartitionSessionID 473 } 474 475 func (r *StopPartitionSessionResponse) toProto() *Ydb_Topic.StreamReadMessage_StopPartitionSessionResponse { 476 return &Ydb_Topic.StreamReadMessage_StopPartitionSessionResponse{ 477 PartitionSessionId: r.PartitionSessionID.ToInt64(), 478 } 479 }