github.com/influxdata/influxdb/v2@v2.7.6/replications/transport/middleware_kv.go (about) 1 package transport 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/binary" 7 "fmt" 8 9 "github.com/influxdata/influxdb/v2/kit/platform" 10 11 "github.com/influxdata/influxdb/v2" 12 "github.com/influxdata/influxdb/v2/kv" 13 ) 14 15 var replicationsBucket = []byte("replicationsv2") 16 17 func newTelemetryCollectingService(kv kv.Store, underlying ReplicationService) *telemetryService { 18 return &telemetryService{ 19 kv: kv, 20 underlying: underlying, 21 } 22 } 23 24 type telemetryService struct { 25 kv kv.Store 26 underlying ReplicationService 27 } 28 29 func (t telemetryService) ListReplications(ctx context.Context, filter influxdb.ReplicationListFilter) (*influxdb.Replications, error) { 30 return t.underlying.ListReplications(ctx, filter) 31 } 32 33 func (t telemetryService) GetReplication(ctx context.Context, id platform.ID) (*influxdb.Replication, error) { 34 return t.underlying.GetReplication(ctx, id) 35 } 36 37 func (t telemetryService) UpdateReplication(ctx context.Context, id platform.ID, request influxdb.UpdateReplicationRequest) (*influxdb.Replication, error) { 38 return t.underlying.UpdateReplication(ctx, id, request) 39 } 40 41 func (t telemetryService) ValidateNewReplication(ctx context.Context, request influxdb.CreateReplicationRequest) error { 42 return t.underlying.ValidateNewReplication(ctx, request) 43 } 44 45 func (t telemetryService) ValidateUpdatedReplication(ctx context.Context, id platform.ID, request influxdb.UpdateReplicationRequest) error { 46 return t.underlying.ValidateUpdatedReplication(ctx, id, request) 47 } 48 49 func (t telemetryService) ValidateReplication(ctx context.Context, id platform.ID) error { 50 return t.underlying.ValidateReplication(ctx, id) 51 } 52 53 func (t telemetryService) CreateReplication(ctx context.Context, request influxdb.CreateReplicationRequest) (*influxdb.Replication, error) { 54 conn, err := t.underlying.CreateReplication(ctx, request) 55 if err != nil { 56 return conn, err 57 } 58 err = t.storeReplicationMetrics(ctx, request.OrgID) 59 return conn, err 60 } 61 62 func (t telemetryService) DeleteReplication(ctx context.Context, id platform.ID) error { 63 rc, err := t.underlying.GetReplication(ctx, id) 64 if err != nil { 65 return err 66 } 67 68 err = t.underlying.DeleteReplication(ctx, id) 69 if err != nil { 70 return err 71 } 72 return t.storeReplicationMetrics(ctx, rc.OrgID) 73 } 74 75 func (t telemetryService) storeReplicationMetrics(ctx context.Context, orgID platform.ID) error { 76 if err := t.kv.Update(ctx, func(tx kv.Tx) error { 77 encodedID, err := orgID.Encode() 78 if err != nil { 79 return platform.ErrInvalidID 80 } 81 bucket, err := tx.Bucket(replicationsBucket) 82 if err != nil { 83 return err 84 } 85 count, err := t.countReplications(ctx, orgID) 86 if err != nil { 87 return err 88 } 89 return bucket.Put(encodedID, count) 90 }); err != nil { 91 return fmt.Errorf("updating telemetry failed: %v", err) 92 } 93 return nil 94 } 95 96 func (t telemetryService) countReplications(ctx context.Context, orgID platform.ID) ([]byte, error) { 97 req := influxdb.ReplicationListFilter{ 98 OrgID: orgID, 99 } 100 list, err := t.underlying.ListReplications(ctx, req) 101 if err != nil { 102 return nil, err 103 } 104 105 b := make([]byte, 0, 8) 106 buf := bytes.NewBuffer(b) 107 err = binary.Write(buf, binary.BigEndian, int64(len(list.Replications))) 108 return buf.Bytes(), err 109 }