|
| 1 | +package rawtopic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Topic" |
| 7 | + |
| 8 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/clone" |
| 9 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawoptional" |
| 10 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawscheme" |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopiccommon" |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawydb" |
| 13 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/operation" |
| 14 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" |
| 15 | +) |
| 16 | + |
| 17 | +type DescribeConsumerRequest struct { |
| 18 | + OperationParams rawydb.OperationParams |
| 19 | + Path string |
| 20 | + Consumer string |
| 21 | + IncludeStats bool |
| 22 | +} |
| 23 | + |
| 24 | +func (req *DescribeConsumerRequest) ToProto() *Ydb_Topic.DescribeConsumerRequest { |
| 25 | + return &Ydb_Topic.DescribeConsumerRequest{ |
| 26 | + OperationParams: req.OperationParams.ToProto(), |
| 27 | + Path: req.Path, |
| 28 | + Consumer: req.Consumer, |
| 29 | + IncludeStats: req.IncludeStats, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type DescribeConsumerResult struct { |
| 34 | + Operation rawydb.Operation |
| 35 | + Self rawscheme.Entry |
| 36 | + Consumer Consumer |
| 37 | + Partitions []DescribeConsumerResultPartitionInfo |
| 38 | +} |
| 39 | + |
| 40 | +func (res *DescribeConsumerResult) FromProto(response operation.Response) error { |
| 41 | + if err := res.Operation.FromProtoWithStatusCheck(response.GetOperation()); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + protoResult := &Ydb_Topic.DescribeConsumerResult{} |
| 45 | + if err := response.GetOperation().GetResult().UnmarshalTo(protoResult); err != nil { |
| 46 | + return xerrors.WithStackTrace( |
| 47 | + fmt.Errorf( |
| 48 | + "ydb: describe consumer result failed on unmarshal grpc result: %w", err, |
| 49 | + ), |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + if err := res.Self.FromProto(protoResult.GetSelf()); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + res.Consumer.MustFromProto(protoResult.GetConsumer()) |
| 58 | + |
| 59 | + protoPartitions := protoResult.GetPartitions() |
| 60 | + res.Partitions = make([]DescribeConsumerResultPartitionInfo, len(protoPartitions)) |
| 61 | + for i, protoPartition := range protoPartitions { |
| 62 | + if err := res.Partitions[i].FromProto(protoPartition); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +type MultipleWindowsStat struct { |
| 71 | + PerMinute int64 |
| 72 | + PerHour int64 |
| 73 | + PerDay int64 |
| 74 | +} |
| 75 | + |
| 76 | +func (stat *MultipleWindowsStat) MustFromProto(proto *Ydb_Topic.MultipleWindowsStat) { |
| 77 | + stat.PerMinute = proto.GetPerMinute() |
| 78 | + stat.PerHour = proto.GetPerHour() |
| 79 | + stat.PerDay = proto.GetPerDay() |
| 80 | +} |
| 81 | + |
| 82 | +type PartitionStats struct { |
| 83 | + PartitionsOffset rawtopiccommon.OffsetRange |
| 84 | + StoreSizeBytes int64 |
| 85 | + LastWriteTime rawoptional.Time |
| 86 | + MaxWriteTimeLag rawoptional.Duration |
| 87 | + BytesWritten MultipleWindowsStat |
| 88 | +} |
| 89 | + |
| 90 | +func (ps *PartitionStats) FromProto(proto *Ydb_Topic.PartitionStats) error { |
| 91 | + if proto == nil { |
| 92 | + return nil |
| 93 | + } |
| 94 | + if err := ps.PartitionsOffset.FromProto(proto.GetPartitionOffsets()); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + ps.StoreSizeBytes = proto.GetStoreSizeBytes() |
| 98 | + ps.LastWriteTime.MustFromProto(proto.GetLastWriteTime()) |
| 99 | + ps.MaxWriteTimeLag.ToProto() |
| 100 | + ps.BytesWritten.MustFromProto(proto.GetBytesWritten()) |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +type PartitionConsumerStats struct { |
| 106 | + LastReadOffset int64 |
| 107 | + CommittedOffset int64 |
| 108 | + ReadSessionID string |
| 109 | + PartitionReadSessionCreateTime rawoptional.Time |
| 110 | + LastReadTime rawoptional.Time |
| 111 | + MaxReadTimeLag rawoptional.Duration |
| 112 | + MaxWriteTimeLag rawoptional.Duration |
| 113 | + BytesRead MultipleWindowsStat |
| 114 | + ReaderName string |
| 115 | +} |
| 116 | + |
| 117 | +func (stats *PartitionConsumerStats) FromProto(proto *Ydb_Topic.DescribeConsumerResult_PartitionConsumerStats) error { |
| 118 | + if proto == nil { |
| 119 | + return nil |
| 120 | + } |
| 121 | + stats.LastReadOffset = proto.GetLastReadOffset() |
| 122 | + stats.CommittedOffset = proto.GetCommittedOffset() |
| 123 | + stats.ReadSessionID = proto.GetReadSessionId() |
| 124 | + stats.PartitionReadSessionCreateTime.MustFromProto(proto.GetPartitionReadSessionCreateTime()) |
| 125 | + stats.LastReadTime.MustFromProto(proto.GetLastReadTime()) |
| 126 | + stats.MaxReadTimeLag.MustFromProto(proto.GetMaxReadTimeLag()) |
| 127 | + stats.MaxWriteTimeLag.MustFromProto(proto.GetMaxWriteTimeLag()) |
| 128 | + stats.BytesRead.MustFromProto(proto.GetBytesRead()) |
| 129 | + stats.ReaderName = proto.GetReaderName() |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +type DescribeConsumerResultPartitionInfo struct { |
| 135 | + PartitionID int64 |
| 136 | + Active bool |
| 137 | + ChildPartitionIDs []int64 |
| 138 | + ParentPartitionIDs []int64 |
| 139 | + PartitionStats PartitionStats |
| 140 | + PartitionConsumerStats PartitionConsumerStats |
| 141 | +} |
| 142 | + |
| 143 | +func (pi *DescribeConsumerResultPartitionInfo) FromProto(proto *Ydb_Topic.DescribeConsumerResult_PartitionInfo) error { |
| 144 | + pi.PartitionID = proto.GetPartitionId() |
| 145 | + pi.Active = proto.GetActive() |
| 146 | + |
| 147 | + pi.ChildPartitionIDs = clone.Int64Slice(proto.GetChildPartitionIds()) |
| 148 | + pi.ParentPartitionIDs = clone.Int64Slice(proto.GetParentPartitionIds()) |
| 149 | + |
| 150 | + return pi.PartitionStats.FromProto(proto.GetPartitionStats()) |
| 151 | +} |
0 commit comments