|
| 1 | +package ydbnbs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/golang/protobuf/proto" |
| 10 | + ydbnbs_config "github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/clients/ydbnbs/config" |
| 11 | + nbsprotos "github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/clients/ydbnbs/protos" |
| 12 | + "github.com/ydb-platform/nbs/cloud/tasks/errors" |
| 13 | + "golang.org/x/exp/maps" |
| 14 | + "google.golang.org/grpc" |
| 15 | + "google.golang.org/grpc/credentials" |
| 16 | + "google.golang.org/grpc/credentials/insecure" |
| 17 | +) |
| 18 | + |
| 19 | +//////////////////////////////////////////////////////////////////////////////// |
| 20 | + |
| 21 | +// Ydb.StatusIds.StatusCode.SUCCESS |
| 22 | +const ydbStatusSuccess int32 = 400000 |
| 23 | + |
| 24 | +type client struct { |
| 25 | + zoneID string |
| 26 | + timeout time.Duration |
| 27 | + dial func(ctx context.Context) (*grpc.ClientConn, error) |
| 28 | +} |
| 29 | + |
| 30 | +func (c *client) ZoneID() string { |
| 31 | + return c.zoneID |
| 32 | +} |
| 33 | + |
| 34 | +func (c *client) CreatePartition( |
| 35 | + ctx context.Context, |
| 36 | + params CreatePartitionParams, |
| 37 | +) (string, error) { |
| 38 | + |
| 39 | + if len(params.DiskID) == 0 { |
| 40 | + return "", errors.NewNonRetriableErrorf("disk id is required") |
| 41 | + } |
| 42 | + if len(params.StoragePoolName) == 0 { |
| 43 | + return "", errors.NewNonRetriableErrorf( |
| 44 | + "storage pool name is required for ssd-nbs2 disk %v", |
| 45 | + params.DiskID, |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + req :=  rotos.CreatePartitionRequest{ |
| 50 | + OperationParams:  rotos.OperationParams{ |
| 51 | + OperationMode: nbsprotos.OperationParams_SYNC, |
| 52 | + }, |
| 53 | + DiskId: params.DiskID, |
| 54 | + BlockSize: params.BlockSize, |
| 55 | + BlocksCount: params.BlocksCount, |
| 56 | + StoragePoolName: params.StoragePoolName, |
| 57 | + StorageMedia: nbsprotos.StorageMediaKind_STORAGE_MEDIA_SSD, |
| 58 | + } |
| 59 | + |
| 60 | + resp :=  rotos.CreatePartitionResponse{} |
| 61 | + err := c.invoke(ctx, "CreatePartition", req, resp) |
| 62 | + if err != nil { |
| 63 | + return "", err |
| 64 | + } |
| 65 | + |
| 66 | + op, err := checkOperation(resp.GetOperation()) |
| 67 | + if err != nil { |
| 68 | + return "", err |
| 69 | + } |
| 70 | + |
| 71 | + result :=  rotos.CreatePartitionResult{} |
| 72 | + err = unpackOperationResult(op, result) |
| 73 | + if err != nil { |
| 74 | + return "", err |
| 75 | + } |
| 76 | + if len(result.GetTabletId()) == 0 { |
| 77 | + return "", errors.NewNonRetriableErrorf( |
| 78 | + "CreatePartition for disk %v returned empty tablet id", |
| 79 | + params.DiskID, |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + return result.GetTabletId(), nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *client) DeletePartition(ctx context.Context, tabletID string) error { |
| 87 | + if len(tabletID) == 0 { |
| 88 | + return errors.NewNonRetriableErrorf("tablet id is required") |
| 89 | + } |
| 90 | + |
| 91 | + req :=  rotos.DeletePartitionRequest{ |
| 92 | + OperationParams:  rotos.OperationParams{ |
| 93 | + OperationMode: nbsprotos.OperationParams_SYNC, |
| 94 | + }, |
| 95 | + TabletId: tabletID, |
| 96 | + } |
| 97 | + |
| 98 | + resp :=  rotos.DeletePartitionResponse{} |
| 99 | + err := c.invoke(ctx, "DeletePartition", req, resp) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + _, err = checkOperation(resp.GetOperation()) |
| 105 | + return err |
| 106 | +} |
| 107 | + |
| 108 | +func (c *client) invoke( |
| 109 | + ctx context.Context, |
| 110 | + method string, |
| 111 | + req proto.Message, |
| 112 | + resp proto.Message, |
| 113 | +) error { |
| 114 | + |
| 115 | + timeout := c.timeout |
| 116 | + if timeout <= 0 { |
| 117 | + timeout = 20 * time.Second |
| 118 | + } |
| 119 | + |
| 120 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 121 | + defer cancel() |
| 122 | + |
| 123 | + conn, err := c.dial(ctx) |
| 124 | + if err != nil { |
| 125 | + return errors.NewRetriableError(err) |
| 126 | + } |
| 127 | + defer conn.Close() |
| 128 | + |
| 129 | + stub := nbsprotos.NewNbsServiceClient(conn) |
| 130 | + switch method { |
| 131 | + case "CreatePartition": |
| 132 | + out, err := stub.CreatePartition(ctx, req.(*nbsprotos.CreatePartitionRequest)) |
| 133 | + if err != nil { |
| 134 | + return errors.NewRetriableError(err) |
| 135 | + } |
| 136 | + proto.Merge(resp, out) |
| 137 | + case "DeletePartition": |
| 138 | + out, err := stub.DeletePartition(ctx, req.(*nbsprotos.DeletePartitionRequest)) |
| 139 | + if err != nil { |
| 140 | + return errors.NewRetriableError(err) |
| 141 | + } |
| 142 | + proto.Merge(resp, out) |
| 143 | + default: |
| 144 | + return errors.NewNonRetriableErrorf("unknown nbs method %v", method) |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +func checkOperation(op *nbsprotos.Operation) (*nbsprotos.Operation, error) { |
| 151 | + if op == nil { |
| 152 | + return nil, errors.NewRetriableErrorf("empty operation in nbs response") |
| 153 | + } |
| 154 | + if !op.GetReady() { |
| 155 | + return nil, errors.NewRetriableErrorf( |
| 156 | + "nbs operation %v is not ready", |
| 157 | + op.GetId(), |
| 158 | + ) |
| 159 | + } |
| 160 | + if op.GetStatus() != ydbStatusSuccess { |
| 161 | + return nil, errors.NewRetriableErrorf( |
| 162 | + "nbs operation %v failed with status %v", |
| 163 | + op.GetId(), |
| 164 | + op.GetStatus(), |
| 165 | + ) |
| 166 | + } |
| 167 | + return op, nil |
| 168 | +} |
| 169 | + |
| 170 | +func unpackOperationResult(op *nbsprotos.Operation, msg proto.Message) error { |
| 171 | + if op.GetResult() == nil { |
| 172 | + return errors.NewNonRetriableErrorf("nbs operation %v has empty result", op.GetId()) |
| 173 | + } |
| 174 | + |
| 175 | + err := proto.Unmarshal(op.GetResult().GetValue(), msg) |
| 176 | + if err != nil { |
| 177 | + return errors.NewNonRetriableErrorf( |
| 178 | + "failed to unpack nbs operation %v result: %w", |
| 179 | + op.GetId(), |
| 180 | + err, |
| 181 | + ) |
| 182 | + } |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +//////////////////////////////////////////////////////////////////////////////// |
| 187 | + |
| 188 | +type factory struct { |
| 189 | + config *ydbnbs_config.ClientConfig |
| 190 | + timeout time.Duration |
| 191 | +} |
| 192 | + |
| 193 | +func (f *factory) GetClient(ctx context.Context, zoneID string) (Client, error) { |
| 194 | + if f.config == nil { |
| 195 | + return nil, errors.NewNonRetriableErrorf( |
| 196 | + "ydb nbs client is not configured, available zones: []", |
| 197 | + ) |
| 198 | + } |
| 199 | + |
| 200 | + zone, ok := f.config.GetZones()[zoneID] |
| 201 | + if !ok { |
| 202 | + return nil, errors.NewNonRetriableErrorf( |
| 203 | + "unknown ydb nbs zone %q, available zones: %q", |
| 204 | + zoneID, |
| 205 | + maps.Keys(f.config.GetZones()), |
| 206 | + ) |
| 207 | + } |
| 208 | + if len(zone.GetEndpoints()) == 0 { |
| 209 | + return nil, errors.NewNonRetriableErrorf( |
| 210 | + "no ydb nbs endpoints for zone %q", |
| 211 | + zoneID, |
| 212 | + ) |
| 213 | + } |
| 214 | + |
| 215 | + endpoint := normalizeEndpoint(zone.GetEndpoints()[0]) |
| 216 | + creds, err := f.transportCredentials() |
| 217 | + if err != nil { |
| 218 | + return nil, err |
| 219 | + } |
| 220 | + |
| 221 | + return &client{ |
| 222 | + zoneID: zoneID, |
| 223 | + timeout: f.timeout, |
| 224 | + dial: func(ctx context.Context) (*grpc.ClientConn, error) { |
| 225 | + return grpc.DialContext(ctx, endpoint, grpc.WithTransportCredentials(creds)) |
| 226 | + }, |
| 227 | + }, nil |
| 228 | +} |
| 229 | + |
| 230 | +func (f *factory) transportCredentials() (credentials.TransportCredentials, error) { |
| 231 | + if f.config.GetInsecure() || len(f.config.GetRootCertsFile()) == 0 { |
| 232 | + return insecure.NewCredentials(), nil |
| 233 | + } |
| 234 | + |
| 235 | + creds, err := credentials.NewClientTLSFromFile(f.config.GetRootCertsFile(), "") |
| 236 | + if err != nil { |
| 237 | + return nil, errors.NewNonRetriableErrorf( |
| 238 | + "failed to load ydb nbs root certs from %v: %w", |
| 239 | + f.config.GetRootCertsFile(), |
| 240 | + err, |
| 241 | + ) |
| 242 | + } |
| 243 | + return creds, nil |
| 244 | +} |
| 245 | + |
| 246 | +func normalizeEndpoint(endpoint string) string { |
| 247 | + endpoint = strings.TrimPrefix(endpoint, "grpc://") |
| 248 | + endpoint = strings.TrimPrefix(endpoint, "grpcs://") |
| 249 | + return endpoint |
| 250 | +} |
| 251 | + |
| 252 | +func NewFactory(config *ydbnbs_config.ClientConfig) (Factory, error) { |
| 253 | + timeout := 20 * time.Second |
| 254 | + if config != nil && len(config.GetRequestTimeout()) > 0 { |
| 255 | + parsed, err := time.ParseDuration(config.GetRequestTimeout()) |
| 256 | + if err != nil { |
| 257 | + return nil, fmt.Errorf("invalid ydb nbs request timeout: %w", err) |
| 258 | + } |
| 259 | + timeout = parsed |
| 260 | + } |
| 261 | + |
| 262 | + return &factory{ |
| 263 | + config: config, |
| 264 | + timeout: timeout, |
| 265 | + }, nil |
| 266 | +} |
0 commit comments