Skip to content

Commit 50efe42

Browse files
klevy-toastCopilotcrossoverJie
authored
Update namespace & topic admin methods to return nil if unset (#1433)
Co-authored-by: Copilot <[email protected]> Co-authored-by: crossoverJie <[email protected]>
1 parent c0db482 commit 50efe42

File tree

5 files changed

+492
-113
lines changed

5 files changed

+492
-113
lines changed

pulsaradmin/pkg/admin/namespace.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ type Namespaces interface {
9494
// GetNamespaceMessageTTLWithContext returns the message TTL for a namespace. Returns -1 if not set
9595
GetNamespaceMessageTTLWithContext(ctx context.Context, namespace string) (int, error)
9696

97-
// GetRetention returns the retention configuration for a namespace
97+
// GetRetention returns the retention configuration for a namespace.
98+
// Returns nil if the retention policy is not configured at the namespace level.
9899
GetRetention(namespace string) (*utils.RetentionPolicies, error)
99100

100-
// GetRetentionWithContext returns the retention configuration for a namespace
101+
// GetRetentionWithContext returns the retention configuration for a namespace.
102+
// Returns nil if the retention policy is not configured at the namespace level.
101103
GetRetentionWithContext(ctx context.Context, namespace string) (*utils.RetentionPolicies, error)
102104

103105
// SetRetention sets the retention configuration for all the topics on a namespace
@@ -132,10 +134,12 @@ type Namespaces interface {
132134
// RemoveBacklogQuotaWithContext removes a backlog quota policy from a namespace
133135
RemoveBacklogQuotaWithContext(ctx context.Context, namespace string) error
134136

135-
// GetTopicAutoCreation returns the topic auto-creation config for a namespace
137+
// GetTopicAutoCreation returns the topic auto-creation config for a namespace.
138+
// Returns nil if the topic auto-creation config is not configured at the namespace level.
136139
GetTopicAutoCreation(namespace utils.NameSpaceName) (*utils.TopicAutoCreationConfig, error)
137140

138-
// GetTopicAutoCreationWithContext returns the topic auto-creation config for a namespace
141+
// GetTopicAutoCreationWithContext returns the topic auto-creation config for a namespace.
142+
// Returns nil if the topic auto-creation config is not configured at the namespace level.
139143
GetTopicAutoCreationWithContext(
140144
ctx context.Context,
141145
namespace utils.NameSpaceName,
@@ -392,10 +396,12 @@ type Namespaces interface {
392396
// SetPersistenceWithContext sets the persistence configuration for all the topics on a namespace
393397
SetPersistenceWithContext(ctx context.Context, namespace string, persistence utils.PersistencePolicies) error
394398

395-
// GetPersistence returns the persistence configuration for a namespace
399+
// GetPersistence returns the persistence configuration for a namespace.
400+
// Returns nil if the persistence policy is not configured at the namespace level.
396401
GetPersistence(namespace string) (*utils.PersistencePolicies, error)
397402

398-
// GetPersistenceWithContext returns the persistence configuration for a namespace
403+
// GetPersistenceWithContext returns the persistence configuration for a namespace.
404+
// Returns nil if the persistence policy is not configured at the namespace level.
399405
GetPersistenceWithContext(ctx context.Context, namespace string) (*utils.PersistencePolicies, error)
400406

401407
// SetBookieAffinityGroup sets bookie affinity group for a namespace to isolate namespace write to bookies that are
@@ -416,10 +422,12 @@ type Namespaces interface {
416422
// DeleteBookieAffinityGroupWithContext deletes bookie affinity group configured for a namespace
417423
DeleteBookieAffinityGroupWithContext(ctx context.Context, namespace string) error
418424

419-
// GetBookieAffinityGroup returns bookie affinity group configured for a namespace
425+
// GetBookieAffinityGroup returns bookie affinity group configured for a namespace.
426+
// Returns nil if the bookie affinity group is not configured at the namespace level.
420427
GetBookieAffinityGroup(namespace string) (*utils.BookieAffinityGroupData, error)
421428

422-
// GetBookieAffinityGroupWithContext returns bookie affinity group configured for a namespace
429+
// GetBookieAffinityGroupWithContext returns bookie affinity group configured for a namespace.
430+
// Returns nil if the bookie affinity group is not configured at the namespace level.
423431
GetBookieAffinityGroupWithContext(ctx context.Context, namespace string) (*utils.BookieAffinityGroupData, error)
424432

425433
// Unload a namespace from the current serving broker
@@ -905,8 +913,11 @@ func (n *namespaces) GetRetentionWithContext(ctx context.Context, namespace stri
905913
return nil, err
906914
}
907915
endpoint := n.pulsar.endpoint(n.basePath, nsName.String(), "retention")
908-
err = n.pulsar.Client.GetWithContext(ctx, endpoint, &policy)
909-
return &policy, err
916+
body, err := n.pulsar.Client.GetBodyWithContext(ctx, endpoint, &policy)
917+
if body != nil {
918+
return &policy, err
919+
}
920+
return nil, err
910921
}
911922

912923
func (n *namespaces) GetBacklogQuotaMap(namespace string) (map[utils.BacklogQuotaType]utils.BacklogQuota, error) {
@@ -970,8 +981,11 @@ func (n *namespaces) GetTopicAutoCreationWithContext(
970981
) (*utils.TopicAutoCreationConfig, error) {
971982
var topicAutoCreation utils.TopicAutoCreationConfig
972983
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "autoTopicCreation")
973-
err := n.pulsar.Client.GetWithContext(ctx, endpoint, &topicAutoCreation)
974-
return &topicAutoCreation, err
984+
body, err := n.pulsar.Client.GetBodyWithContext(ctx, endpoint, &topicAutoCreation)
985+
if body != nil {
986+
return &topicAutoCreation, err
987+
}
988+
return nil, err
975989
}
976990

977991
func (n *namespaces) SetTopicAutoCreation(namespace utils.NameSpaceName, config utils.TopicAutoCreationConfig) error {
@@ -1463,8 +1477,11 @@ func (n *namespaces) GetBookieAffinityGroupWithContext(
14631477
return nil, err
14641478
}
14651479
endpoint := n.pulsar.endpoint(n.basePath, nsName.String(), "persistence", "bookieAffinity")
1466-
err = n.pulsar.Client.GetWithContext(ctx, endpoint, &data)
1467-
return &data, err
1480+
body, err := n.pulsar.Client.GetBodyWithContext(ctx, endpoint, &data)
1481+
if body != nil {
1482+
return &data, err
1483+
}
1484+
return nil, err
14681485
}
14691486

14701487
func (n *namespaces) GetPersistence(namespace string) (*utils.PersistencePolicies, error) {
@@ -1481,8 +1498,11 @@ func (n *namespaces) GetPersistenceWithContext(
14811498
return nil, err
14821499
}
14831500
endpoint := n.pulsar.endpoint(n.basePath, nsName.String(), "persistence")
1484-
err = n.pulsar.Client.GetWithContext(ctx, endpoint, &persistence)
1485-
return &persistence, err
1501+
body, err := n.pulsar.Client.GetBodyWithContext(ctx, endpoint, &persistence)
1502+
if body != nil {
1503+
return &persistence, err
1504+
}
1505+
return nil, err
14861506
}
14871507

14881508
func (n *namespaces) Unload(namespace string) error {

pulsaradmin/pkg/admin/namespace_test.go

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package admin
1919

2020
import (
21+
"os"
2122
"testing"
2223
"time"
2324

@@ -156,23 +157,20 @@ func TestGetTopicAutoCreation(t *testing.T) {
156157
assert.Equal(t, nil, err)
157158
topicAutoCreation, err := admin.Namespaces().GetTopicAutoCreation(*namespace)
158159
assert.Equal(t, nil, err)
160+
assert.NotNil(t, topicAutoCreation, "Expected non-nil when topic auto creation is configured")
159161
expected := utils.TopicAutoCreationConfig{
160162
Allow: true,
161163
Type: utils.NonPartitioned,
162164
}
163165
assert.Equal(t, expected, *topicAutoCreation)
164166

165-
// remove the topic auto creation config and get it
167+
// remove the topic auto creation config and get it - should return nil
166168
err = admin.Namespaces().RemoveTopicAutoCreation(*namespace)
167169
assert.Equal(t, nil, err)
168170

169171
topicAutoCreation, err = admin.Namespaces().GetTopicAutoCreation(*namespace)
170172
assert.Equal(t, nil, err)
171-
expected = utils.TopicAutoCreationConfig{
172-
Allow: false,
173-
Type: "",
174-
}
175-
assert.Equal(t, expected, *topicAutoCreation)
173+
assert.Nil(t, topicAutoCreation, "Expected nil when topic auto creation is not configured")
176174
}
177175

178176
func TestRevokeSubPermission(t *testing.T) {
@@ -763,3 +761,104 @@ func TestNamespaces_MaxProducersPerTopic(t *testing.T) {
763761
assert.NoError(t, err)
764762
assert.Equal(t, 50, maxProducers)
765763
}
764+
765+
func TestNamespaces_Retention(t *testing.T) {
766+
config := &config.Config{}
767+
admin, err := New(config)
768+
require.NoError(t, err)
769+
require.NotNil(t, admin)
770+
771+
namespaceName := "public/default"
772+
773+
// Initial state: policy not configured, should return nil
774+
retention, err := admin.Namespaces().GetRetention(namespaceName)
775+
assert.NoError(t, err)
776+
assert.Nil(t, retention, "Expected nil when retention is not configured")
777+
778+
// Set new retention policy
779+
newRetention := utils.RetentionPolicies{
780+
RetentionSizeInMB: 1024,
781+
RetentionTimeInMinutes: 60,
782+
}
783+
err = admin.Namespaces().SetRetention(namespaceName, newRetention)
784+
assert.NoError(t, err)
785+
786+
// Verify retention is set
787+
retention, err = admin.Namespaces().GetRetention(namespaceName)
788+
assert.NoError(t, err)
789+
assert.NotNil(t, retention, "Expected non-nil when retention is configured")
790+
assert.Equal(t, int64(1024), retention.RetentionSizeInMB)
791+
assert.Equal(t, 60, retention.RetentionTimeInMinutes)
792+
}
793+
794+
func TestNamespaces_BookieAffinityGroup(t *testing.T) {
795+
readFile, err := os.ReadFile("../../../integration-tests/tokens/admin-token")
796+
require.NoError(t, err)
797+
798+
config := &config.Config{
799+
Token: string(readFile),
800+
}
801+
admin, err := New(config)
802+
require.NoError(t, err)
803+
require.NotNil(t, admin)
804+
805+
namespaceName := "public/default"
806+
807+
// Initial state: policy not configured, should return nil
808+
bookieAffinity, err := admin.Namespaces().GetBookieAffinityGroup(namespaceName)
809+
assert.NoError(t, err)
810+
assert.Nil(t, bookieAffinity, "Expected nil when bookie affinity group is not configured")
811+
812+
// Set new bookie affinity group
813+
newBookieAffinity := utils.BookieAffinityGroupData{
814+
BookkeeperAffinityGroupPrimary: "primary-group",
815+
BookkeeperAffinityGroupSecondary: "secondary-group",
816+
}
817+
err = admin.Namespaces().SetBookieAffinityGroup(namespaceName, newBookieAffinity)
818+
assert.NoError(t, err)
819+
820+
// Verify bookie affinity group is set
821+
bookieAffinity, err = admin.Namespaces().GetBookieAffinityGroup(namespaceName)
822+
assert.NoError(t, err)
823+
assert.NotNil(t, bookieAffinity, "Expected non-nil when bookie affinity group is configured")
824+
assert.Equal(t, "primary-group", bookieAffinity.BookkeeperAffinityGroupPrimary)
825+
assert.Equal(t, "secondary-group", bookieAffinity.BookkeeperAffinityGroupSecondary)
826+
827+
// Remove bookie affinity group - should return nil
828+
err = admin.Namespaces().DeleteBookieAffinityGroup(namespaceName)
829+
assert.NoError(t, err)
830+
bookieAffinity, err = admin.Namespaces().GetBookieAffinityGroup(namespaceName)
831+
assert.NoError(t, err)
832+
assert.Nil(t, bookieAffinity, "Expected nil after removing bookie affinity group")
833+
}
834+
835+
func TestNamespaces_Persistence(t *testing.T) {
836+
config := &config.Config{}
837+
admin, err := New(config)
838+
require.NoError(t, err)
839+
require.NotNil(t, admin)
840+
841+
namespaceName := "public/default"
842+
843+
// Initial state: policy not configured, should return nil
844+
persistence, err := admin.Namespaces().GetPersistence(namespaceName)
845+
assert.NoError(t, err)
846+
assert.Nil(t, persistence, "Expected nil when persistence is not configured")
847+
848+
// Set new persistence policy
849+
newPersistence := utils.PersistencePolicies{
850+
BookkeeperEnsemble: 1,
851+
BookkeeperWriteQuorum: 1,
852+
BookkeeperAckQuorum: 1,
853+
ManagedLedgerMaxMarkDeleteRate: 10.0,
854+
}
855+
err = admin.Namespaces().SetPersistence(namespaceName, newPersistence)
856+
assert.NoError(t, err)
857+
858+
// Verify persistence is set
859+
persistence, err = admin.Namespaces().GetPersistence(namespaceName)
860+
assert.NoError(t, err)
861+
assert.NotNil(t, persistence, "Expected non-nil when persistence is configured")
862+
assert.Equal(t, 1, persistence.BookkeeperEnsemble)
863+
assert.Equal(t, 1, persistence.BookkeeperWriteQuorum)
864+
}

0 commit comments

Comments
 (0)