Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: add update partition count dataplane api #1638

Open
wants to merge 5 commits into
base: release-2.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
backend: add update partition count dataplane api
  • Loading branch information
bojand committed Mar 6, 2025
commit ed9cd7887b52d445c32ffc4e30b238770591486b
1,117 changes: 1,117 additions & 0 deletions backend/pkg/api/connect/integration/topic_v1_test.go

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions backend/pkg/api/connect/service/topic/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,127 @@ func (s *Service) CreateTopic(ctx context.Context, req *connect.Request[v1.Creat
connectResponse.Header().Set("x-http-code", strconv.Itoa(http.StatusCreated))
return connectResponse, nil
}

// AddTopicPartitions add partition counts to an existing topic.
func (s *Service) AddTopicPartitions(ctx context.Context, req *connect.Request[v1.AddTopicPartitionsRequest]) (*connect.Response[v1.AddTopicPartitionsResponse], error) {
topicName := req.Msg.GetTopicName()

res, err := s.consoleSvc.AddPartitionsToTopics(ctx,
int(req.Msg.GetPartitionCount()),
[]string{topicName},
req.Msg.GetValidateOnly())
if err != nil {
return nil, apierrors.NewConnectError(
connect.CodeInternal,
err,
apierrors.NewErrorInfo(v1.Reason_REASON_KAFKA_API_ERROR.String(), apierrors.KeyValsFromKafkaError(err)...),
)
}

if res[topicName].Err != nil {
return nil, s.handleKafkaTopicPartitionError(res[topicName].Err, res[topicName].ErrMessage)
}

return connect.NewResponse(&v1.AddTopicPartitionsResponse{}), nil
}

// SetTopicPartitions sets partition counts to an existing topic.
func (s *Service) SetTopicPartitions(ctx context.Context, req *connect.Request[v1.SetTopicPartitionsRequest]) (*connect.Response[v1.SetTopicPartitionsResponse], error) {
topicName := req.Msg.GetTopicName()

res, err := s.consoleSvc.SetPartitionsToTopics(ctx,
int(req.Msg.GetPartitionCount()),
[]string{topicName},
req.Msg.GetValidateOnly())
if err != nil {
return nil, apierrors.NewConnectError(
connect.CodeInternal,
err,
apierrors.NewErrorInfo(v1.Reason_REASON_KAFKA_API_ERROR.String(), apierrors.KeyValsFromKafkaError(err)...),
)
}

if res[topicName].Err != nil {
return nil, s.handleKafkaTopicPartitionError(res[topicName].Err, res[topicName].ErrMessage)
}

return connect.NewResponse(&v1.SetTopicPartitionsResponse{}), nil
}

// AddPartitionsToTopics add partition counts to topics.
func (s *Service) AddPartitionsToTopics(ctx context.Context, req *connect.Request[v1.AddPartitionsToTopicsRequest]) (*connect.Response[v1.AddPartitionsToTopicsResponse], error) {
statuses, err := s.consoleSvc.AddPartitionsToTopics(ctx,
int(req.Msg.GetPartitionCount()),
req.Msg.GetTopicNames(),
req.Msg.GetValidateOnly())
if err != nil {
return nil, apierrors.NewConnectError(
connect.CodeInternal,
err,
apierrors.NewErrorInfo(v1.Reason_REASON_KAFKA_API_ERROR.String(), apierrors.KeyValsFromKafkaError(err)...),
)
}

resMsg := &v1.AddPartitionsToTopicsResponse{
Statuses: make([]*v1.AlterTopicPartitionStatus, 0, len(statuses)),
}

for topicName, result := range statuses {
errMsg := ""
err := result.Err
if err != nil {
if result.ErrMessage != "" {
err = fmt.Errorf(result.ErrMessage+": %w", err)
}

errMsg = err.Error()
}

resMsg.Statuses = append(resMsg.Statuses, &v1.AlterTopicPartitionStatus{
TopicName: topicName,
Success: errMsg == "",
Error: errMsg,
})
}

return connect.NewResponse(resMsg), nil
}

// SetPartitionsToTopics sets partition counts to topics.
func (s *Service) SetPartitionsToTopics(ctx context.Context, req *connect.Request[v1.SetPartitionsToTopicsRequest]) (*connect.Response[v1.SetPartitionsToTopicsResponse], error) {
statuses, err := s.consoleSvc.SetPartitionsToTopics(ctx,
int(req.Msg.GetPartitionCount()),
req.Msg.GetTopicNames(),
req.Msg.GetValidateOnly())
if err != nil {
return nil, apierrors.NewConnectError(
connect.CodeInternal,
err,
apierrors.NewErrorInfo(v1.Reason_REASON_KAFKA_API_ERROR.String(), apierrors.KeyValsFromKafkaError(err)...),
)
}

resMsg := &v1.SetPartitionsToTopicsResponse{
Statuses: make([]*v1.AlterTopicPartitionStatus, 0, len(statuses)),
}

for topicName, result := range statuses {
errMsg := ""
err := result.Err
if err != nil {
if result.ErrMessage != "" {
err = fmt.Errorf(result.ErrMessage+": %w", err)
}

errMsg = err.Error()
}

resMsg.Statuses = append(resMsg.Statuses, &v1.AlterTopicPartitionStatus{
TopicName: topicName,
Success: errMsg == "",
Error: errMsg,
})
}

return connect.NewResponse(resMsg), nil
}
29 changes: 29 additions & 0 deletions backend/pkg/api/connect/service/topic/v1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/twmb/franz-go/pkg/kerr"

apierrors "github.com/redpanda-data/console/backend/pkg/api/connect/errors"
v1 "github.com/redpanda-data/console/backend/pkg/protogen/redpanda/api/dataplane/v1"
)

// handleKafkaTopicError handles topic specific error codes, such as UNKNOWN_TOPIC_OR_PARTITION and
Expand All @@ -41,3 +42,31 @@ func (*Service) handleKafkaTopicError(kafkaErrorCode int16, errorMessage *string
return apierrors.NewConnectErrorFromKafkaErrorCode(kafkaErrorCode, errorMessage)
}
}

func (*Service) handleKafkaTopicPartitionError(kafkaErr error, errorMessage string) *connect.Error {
var code connect.Code

switch {
case errors.Is(kafkaErr, kerr.UnknownTopicOrPartition):
code = connect.CodeNotFound
case errors.Is(kafkaErr, kerr.InvalidTopicException):
code = connect.CodeNotFound
case errors.Is(kafkaErr, kerr.InvalidPartitions):
code = connect.CodeInvalidArgument
case errors.Is(kafkaErr, kerr.InvalidRequest):
code = connect.CodeInvalidArgument
default:
code = connect.CodeInternal
}

err := kafkaErr
if errorMessage != "" {
err = fmt.Errorf(errorMessage+": %w", err)
}

return apierrors.NewConnectError(
code,
err,
apierrors.NewErrorInfo(v1.Reason_REASON_KAFKA_API_ERROR.String(), apierrors.KeyValsFromKafkaError(kafkaErr)...),
)
}
5 changes: 5 additions & 0 deletions backend/pkg/console/servicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/cloudhut/common/rest"
"github.com/twmb/franz-go/pkg/kadm"
"github.com/twmb/franz-go/pkg/kgo"
"github.com/twmb/franz-go/pkg/kmsg"

Expand Down Expand Up @@ -93,4 +94,8 @@ type Servicer interface {
IncrementalAlterConfigsKafka(ctx context.Context, req *kmsg.IncrementalAlterConfigsRequest) (*kmsg.IncrementalAlterConfigsResponse, error)
// AlterConfigs proxies the request/response to set configs (not incrementally) via the Kafka API.
AlterConfigs(ctx context.Context, req *kmsg.AlterConfigsRequest) (*kmsg.AlterConfigsResponse, error)
// Adds partitions to existing topics.
AddPartitionsToTopics(ctx context.Context, add int, topicNames []string, validateOnly bool) (kadm.CreatePartitionsResponses, error)
// Sets partition counts to existing topics.
SetPartitionsToTopics(ctx context.Context, add int, topicNames []string, validateOnly bool) (kadm.CreatePartitionsResponses, error)
}
19 changes: 19 additions & 0 deletions backend/pkg/console/topic_partitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/cloudhut/common/rest"
"github.com/twmb/franz-go/pkg/kadm"
"github.com/twmb/franz-go/pkg/kerr"
"github.com/twmb/franz-go/pkg/kmsg"
"go.uber.org/zap"
Expand Down Expand Up @@ -377,3 +378,21 @@ func (s *Service) describePartitionLogDirs(ctx context.Context, topicMetadata ma

return partitionLogDirs
}

// AddPartitionsToTopics adds partition counts to existing topics.
func (s *Service) AddPartitionsToTopics(ctx context.Context, add int, topicNames []string, validateOnly bool) (kadm.CreatePartitionsResponses, error) {
if validateOnly {
return s.kafkaSvc.ValidateAddPartitionsToTopics(ctx, add, topicNames)
}

return s.kafkaSvc.AddPartitionsToTopics(ctx, add, topicNames)
}

// SetPartitionsToTopics sets partition counts to existing topics.
func (s *Service) SetPartitionsToTopics(ctx context.Context, count int, topicNames []string, validateOnly bool) (kadm.CreatePartitionsResponses, error) {
if validateOnly {
return s.kafkaSvc.ValidateSetPartitionsToTopics(ctx, count, topicNames)
}

return s.kafkaSvc.SetPartitionsToTopics(ctx, count, topicNames)
}
36 changes: 36 additions & 0 deletions backend/pkg/kafka/partition_counts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2025 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

package kafka

import (
"context"

"github.com/twmb/franz-go/pkg/kadm"
)

// AddPartitionsToTopics adds partition counts to topics.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are somewhat weird for these functions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated comments.

func (s *Service) AddPartitionsToTopics(ctx context.Context, add int, topicNames []string) (kadm.CreatePartitionsResponses, error) {
return s.KafkaAdmClient.CreatePartitions(ctx, add, topicNames...)
}

// ValidateAddPartitionsToTopics is same as AddPartitionsToTopics but only performs the validation.
func (s *Service) ValidateAddPartitionsToTopics(ctx context.Context, add int, topicNames []string) (kadm.CreatePartitionsResponses, error) {
return s.KafkaAdmClient.ValidateCreatePartitions(ctx, add, topicNames...)
}

// SetPartitionsToTopics sets partition counts to topics.
func (s *Service) SetPartitionsToTopics(ctx context.Context, count int, topicNames []string) (kadm.CreatePartitionsResponses, error) {
return s.KafkaAdmClient.UpdatePartitions(ctx, count, topicNames...)
}

// ValidateSetPartitionsToTopics is same as SetPartitionsToTopics but only performs the validation.
func (s *Service) ValidateSetPartitionsToTopics(ctx context.Context, count int, topicNames []string) (kadm.CreatePartitionsResponses, error) {
return s.KafkaAdmClient.ValidateUpdatePartitions(ctx, count, topicNames...)
}
Loading
Loading