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