-
Notifications
You must be signed in to change notification settings - Fork 68
Add topic operation commands #56
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
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9362444
Add partitioned topic command CURD
zymap bcba093
Revert changes
zymap a2ae20f
Add test for command partitioned topic
zymap 6510c8d
Revert the refactor
zymap b32bf3b
Add non-persistent topic commands
zymap 59c3f58
Revert changes
zymap 4a12631
Revert changes
zymap b0a9dee
Fix typo
zymap b89037f
Add partitions value check
zymap 823c80f
Move to a new package
zymap 12b1205
Add topic stats command
zymap cea7aa8
Rename `partitioned-stats` to `partition-stats`
zymap 8a430f4
Combine partitioned topic and non-partitioned topic stats command
zymap 1538806
Remove partitioned stats file
zymap 2adb8ae
Fix descriptions
zymap 65e2e87
Fix conflict
zymap a839fac
Remove unused code
zymap 28d9248
Add topic operation commands
zymap 01f7829
Revert changes
zymap ebebd2d
Add topic command `terminate`
zymap 6e907bf
Merge branch 'master' into topic-operation
zymap 64ea9ac
Merge branch 'master' into topic-operation
zymap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||
| package compact | ||||||
|
|
||||||
| import ( | ||||||
| "github.com/pkg/errors" | ||||||
| "github.com/streamnative/pulsarctl/pkg/cmdutils" | ||||||
| . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" | ||||||
| . "github.com/streamnative/pulsarctl/pkg/pulsar" | ||||||
| ) | ||||||
|
|
||||||
| func CompactCmd(vc *cmdutils.VerbCmd) { | ||||||
| var desc LongDescription | ||||||
| desc.CommandUsedFor = "This command is used for compacting a persistent topic." | ||||||
| desc.CommandPermission = "This command is requires tenant admin permissions." | ||||||
|
|
||||||
| var examples []Example | ||||||
| compact := Example{ | ||||||
| Desc: "Compact a persistent topic <topic-name>", | ||||||
| Command: "pulsarctl topic compact <topic-name>", | ||||||
| } | ||||||
| desc.CommandExamples = append(examples, compact) | ||||||
|
|
||||||
| var out []Output | ||||||
| successOut := Output{ | ||||||
| Desc: "normal output", | ||||||
| Out: "Sending compact topic <topic-name> request successfully", | ||||||
| } | ||||||
| out = append(out, successOut, ArgError, TopicNotFoundError) | ||||||
| out = append(out, TopicNameErrors...) | ||||||
| out = append(out, NamespaceErrors...) | ||||||
| desc.CommandOutput = out | ||||||
|
|
||||||
| vc.SetDescription( | ||||||
| "compact", | ||||||
| "Compact a topic", | ||||||
| desc.ToString()) | ||||||
|
|
||||||
| vc.SetRunFuncWithNameArg(func() error { | ||||||
| return doCompact(vc) | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| func doCompact(vc *cmdutils.VerbCmd) error { | ||||||
| // for testing | ||||||
| if vc.NameError != nil { | ||||||
| return vc.NameError | ||||||
| } | ||||||
|
|
||||||
| topic, err := GetTopicName(vc.NameArg) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| if !topic.IsPersistent() { | ||||||
| return errors.New("Need to provide a persistent topic.") | ||||||
| } | ||||||
|
|
||||||
| admin := cmdutils.NewPulsarClient() | ||||||
| err = admin.Topics().Compact(*topic) | ||||||
| if err == nil { | ||||||
| vc.Command.Printf("Sending compact topic %s request successfully/n", topic.String()) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| return err | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,103 @@ | ||||||
| package compact | ||||||
|
|
||||||
| import ( | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/pkg/errors" | ||||||
| "github.com/streamnative/pulsarctl/pkg/cmdutils" | ||||||
| . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" | ||||||
| . "github.com/streamnative/pulsarctl/pkg/pulsar" | ||||||
| ) | ||||||
|
|
||||||
| func CompactStatusCmd(vc *cmdutils.VerbCmd) { | ||||||
| var desc LongDescription | ||||||
| desc.CommandUsedFor = "This command is used for getting status of compaction on a topic." | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please fix the example output as well. |
||||||
| desc.CommandPermission = "This command requires tenant admin permissions." | ||||||
|
|
||||||
| var examples []Example | ||||||
| compactStatus := Example{ | ||||||
| Desc: "Get status of compaction of a persistent topic <topic-name>", | ||||||
| Command: "pulsarctl topic compact-status <topic-name>", | ||||||
| } | ||||||
| desc.CommandExamples = append(examples, compactStatus) | ||||||
|
|
||||||
| var out []Output | ||||||
| successOut := Output{ | ||||||
| Desc: "normal output", | ||||||
| Out: "Compaction was a success", | ||||||
| } | ||||||
|
|
||||||
| notRun := Output{ | ||||||
| Desc: "Compaction is not running", | ||||||
| Out: "Compaction has not been run for <topic-name> since broker startup", | ||||||
| } | ||||||
|
|
||||||
| running := Output{ | ||||||
| Desc: "Compaction is running", | ||||||
| Out: "Compaction is currently running", | ||||||
| } | ||||||
|
|
||||||
| errorOut := Output{ | ||||||
| Desc: "Compaction is error", | ||||||
| Out: "Error in compaction", | ||||||
| } | ||||||
| out = append(out, successOut, notRun, running, errorOut, ArgError, TopicNotFoundError) | ||||||
| out = append(out, TopicNameErrors...) | ||||||
| out = append(out, NamespaceErrors...) | ||||||
| desc.CommandOutput = out | ||||||
|
|
||||||
| vc.SetDescription( | ||||||
| "compact-status", | ||||||
| "Get status of compaction on a topic", | ||||||
| desc.ToString()) | ||||||
|
|
||||||
| var wait bool | ||||||
|
|
||||||
| vc.SetRunFuncWithNameArg(func() error { | ||||||
| return doCompactStatus(vc, wait) | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| func doCompactStatus(vc *cmdutils.VerbCmd, wait bool) error { | ||||||
| // for testing | ||||||
| if vc.NameError != nil { | ||||||
| return vc.NameError | ||||||
| } | ||||||
|
|
||||||
| topic, err := GetTopicName(vc.NameArg) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| if topic.GetDomain().String() != "persistent" { | ||||||
| return errors.New("Need to provide a persistent topic.") | ||||||
| } | ||||||
|
|
||||||
| admin := cmdutils.NewPulsarClient() | ||||||
| status, err := admin.Topics().CompactStatus(*topic) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| for wait && status.Status == RUNNING { | ||||||
| time.Sleep( 1 * time.Second) | ||||||
| status, err = admin.Topics().CompactStatus(*topic) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| switch status.Status { | ||||||
| case NOT_RUN: | ||||||
| vc.Command.Printf("Compaction has not been run for %s since broker startup/n", topic.String()) | ||||||
| case RUNNING: | ||||||
| vc.Command.Printf("Compaction is currently running/n") | ||||||
| case SUCCESS: | ||||||
| vc.Command.Printf("Compaction was a success/n") | ||||||
| case ERROR: | ||||||
| vc.Command.Printf("Error in Compaction/n") | ||||||
| err = errors.New(status.LastError) | ||||||
| } | ||||||
|
|
||||||
| return err | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package compact | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCompactStatusArgsError(t *testing.T) { | ||
| args := []string{"compact-status"} | ||
| _, _, nameErr, _ := TestTopicCommands(CompactStatusCmd, args) | ||
| assert.NotNil(t, nameErr) | ||
| assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) | ||
| } | ||
|
|
||
| func TestCompactStatusNonExistingTopicError(t *testing.T) { | ||
| args := []string{"compact-status", "test-non-existing-compact-status"} | ||
| _, execErr, _, _ := TestTopicCommands(CompactStatusCmd, args) | ||
| assert.NotNil(t, execErr) | ||
| assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) | ||
| } | ||
|
|
||
| func TestCompactStatusNonPersistentTopicError(t *testing.T) { | ||
| args := []string{"compact-status", "non-persistent://public/default/test-non-persistent-topic-compact-status"} | ||
| _, execErr, _, _ := TestTopicCommands(CompactStatusCmd, args) | ||
| assert.NotNil(t, execErr) | ||
| assert.Equal(t, "Need to provide a persistent topic.", execErr.Error()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package compact | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" | ||
| . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCompactCmd(t *testing.T) { | ||
| args := []string{"create", "test-compact-topic", "0"} | ||
| _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) | ||
| assert.Nil(t, execErr) | ||
|
|
||
| args = []string{"compact-status", "test-compact-topic"} | ||
| out, execErr, _, _ := TestTopicCommands(CompactStatusCmd, args) | ||
| assert.Nil(t, execErr) | ||
| assert.Equal(t, "Compaction has not been run for " + | ||
| "persistent://public/default/test-compact-topic since broker startup/n", out.String()) | ||
|
|
||
| args = []string{"compact", "test-compact-topic"} | ||
| _, execErr, _, _ = TestTopicCommands(CompactCmd, args) | ||
| assert.Nil(t, execErr) | ||
|
|
||
| args = []string{"compact-status", "test-compact-topic"} | ||
| out, execErr, _, _ = TestTopicCommands(CompactStatusCmd, args) | ||
| assert.Nil(t, execErr) | ||
|
|
||
| assert.Equal(t, "Compaction is currently running/n", out.String()) | ||
| } | ||
|
|
||
| func TestCompactArgError(t *testing.T) { | ||
| args := []string{"compact"} | ||
| _, _, nameErr, _ := TestTopicCommands(CompactCmd, args) | ||
| assert.NotNil(t, nameErr) | ||
| assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) | ||
| } | ||
|
|
||
| func TestCompactNonExistingTopic(t *testing.T) { | ||
| args := []string{"compact", "test-compact-non-existing-topic"} | ||
| _, execErr, _, _ := TestTopicCommands(CompactCmd, args) | ||
| assert.NotNil(t, execErr) | ||
| assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) | ||
| } | ||
|
|
||
| func TestCompactNonPersistentTopic(t *testing.T) { | ||
| args := []string{"compact", "non-persistent://public/default/test-compact-non-persistent-topic"} | ||
| _, execErr, _, _ := TestTopicCommands(CompactCmd, args) | ||
| assert.NotNil(t, execErr) | ||
| assert.Equal(t, "Need to provide a persistent topic.", execErr.Error()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you change the message here? If this method is used for other purpose, please create a new method and make sure the error message is self-explained and specific.