Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 46 additions & 0 deletions pkg/bkctl/autorecovery/autorecovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package autorecovery

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"

"github.com/spf13/cobra"
)

func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
resourceCmd := cmdutils.NewResourceCmd(
"auto-recovery",
"Operations about auto recovering",
"",
"")

commands := []func(*cmdutils.VerbCmd){
recoverBookieCmd,
listUnderReplicatedLedgerCmd,
whoIsAuditorCmd,
triggerAuditCmd,
setLostBookieRecoveryDelayCmd,
getLostBookieRecoveryDelayCmd,
decommissionCmd,
}

cmdutils.AddVerbCmds(flagGrouping, resourceCmd, commands...)

return resourceCmd
}
69 changes: 69 additions & 0 deletions pkg/bkctl/autorecovery/decommission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package autorecovery

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"
)

func decommissionCmd(vc *cmdutils.VerbCmd) {
var desc cmdutils.LongDescription
desc.CommandUsedFor = "This command is used for decommissioning a bookie."
desc.CommandPermission = "This command does not need any permission."

var examples []cmdutils.Example
c := cmdutils.Example{
Desc: "Decommission a bookie.",
Command: "pulsarctl bookkeeper auto-recovery (bk-ip:bk-port)",
}
examples = append(examples, c)
desc.CommandExamples = examples

var out []cmdutils.Output
successOut := cmdutils.Output{
Desc: "Successfully decommission a bookie.",
Out: "Successfully decommission the bookie (bookie-ip:bookie-port)",
}

argError := cmdutils.Output{
Desc: "The bookie address is not specified or the bookie address is specified more than one.",
Out: "[✖] the bookie address is not specified or the bookie address is specified more than one",
}
out = append(out, successOut, argError)
desc.CommandOutput = out

vc.SetDescription(
"decommission",
"Decommission a bookie.",
desc.ToString(),
desc.ExampleToString())

vc.SetRunFuncWithNameArg(func() error {
return doDecommission(vc)
}, "the bookie address is not specified or the bookie address is specified more than one")
}

func doDecommission(vc *cmdutils.VerbCmd) error {
admin := cmdutils.NewBookieClient()
err := admin.AutoRecovery().Decommission(vc.NameArg)
if err == nil {
vc.Command.Printf("Successfully decommission the bookie %s.\n", vc.NameArg)
}

return err
}
48 changes: 48 additions & 0 deletions pkg/bkctl/autorecovery/decommission_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package autorecovery

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDecommissionArgsErr(t *testing.T) {
// no args specified
args := []string{"decommission"}
_, _, nameErr, err := testAutoRecoveryCommands(decommissionCmd, args)
if err != nil {
t.Fatal(err)
}

assert.NotNil(t, nameErr)
assert.Equal(t, "the bookie address is not specified or the bookie address is specified more than one",
nameErr.Error())

// more than one args specified
args = []string{"decommission", "bookie-1:3181", "bookie-2:3181"}
_, _, nameErr, err = testAutoRecoveryCommands(decommissionCmd, args)
if err != nil {
t.Fatal(err)
}

assert.NotNil(t, nameErr)
assert.Equal(t, "the bookie address is not specified or the bookie address is specified more than one",
nameErr.Error())
}
64 changes: 64 additions & 0 deletions pkg/bkctl/autorecovery/get_lost_bookie_recovery_delay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package autorecovery

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"
)

func getLostBookieRecoveryDelayCmd(vc *cmdutils.VerbCmd) {
var desc cmdutils.LongDescription
desc.CommandUsedFor = "This command is used for getting the lost bookie recovery delay in second of a bookie."
desc.CommandPermission = "This command does not need any permission."

var examples []cmdutils.Example
get := cmdutils.Example{
Desc: "Get the lost Bookie Recovery Delay of a bookie.",
Command: "pulsarctl bookkeeper auto-recovery get-lost-bookie-recovery-delay",
}
examples = append(examples, get)
desc.CommandExamples = examples

var out []cmdutils.Output
successOut := cmdutils.Output{
Desc: "Get the lost bookie recovery delay of a bookie. ",
Out: "lostBookieRecoveryDelay value: (delay)",
}
out = append(out, successOut)
desc.CommandOutput = out

vc.SetDescription(
"get-lost-bookie-recovery-delay",
"Get the lost bookie recovery delay of a bookie.",
desc.ToString(),
desc.ExampleToString())

vc.SetRunFunc(func() error {
return doGetLostBookieRecoveryDelay(vc)
})
}

func doGetLostBookieRecoveryDelay(vc *cmdutils.VerbCmd) error {
admin := cmdutils.NewBookieClient()
out, err := admin.AutoRecovery().GetLostBookieRecoveryDelay()
if err == nil {
vc.Command.Println(out)
}

return err
}
96 changes: 96 additions & 0 deletions pkg/bkctl/autorecovery/list_under_replicated_ledger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package autorecovery

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"

"github.com/spf13/pflag"
)

func listUnderReplicatedLedgerCmd(vc *cmdutils.VerbCmd) {
var desc cmdutils.LongDescription
desc.CommandUsedFor = "This command is used for getting all the under-replicated ledgers which have been marked " +
"for re-replication."
desc.CommandPermission = "This command does not need any permission."

var examples []cmdutils.Example
list := cmdutils.Example{
Desc: "Get all the under-replicated ledgers which have been marked for re-replication.",
Command: "pulsarctl bookkeeper auto-recovery list-under-replicated-ledger",
}

li := cmdutils.Example{
Desc: "Get all the under-replicated ledgers of a bookie which have been marked for re-replication.",
Command: "pulsarctl bookkeeper auto-recovery list-under-replicated-ledger --include (bookie-ip:bookie-port)",
}

le := cmdutils.Example{
Desc: "Get all the under-replicated ledgers except a bookie which have been marked for re-replication.",
Command: "pulsarctl bookkeeper auto-recovery list-under-replicated-ledger --exclude (bookie-ip:bookie-port)",
}
examples = append(examples, list, li, le)
desc.CommandExamples = examples

var out []cmdutils.Output
successOut := cmdutils.Output{
Desc: "Get the under-replicated ledgers successfully.",
Out: `{
[ledgerId1, ledgerId2...]
}`,
}
out = append(out, successOut)
desc.CommandOutput = out

vc.SetDescription(
"list-under-replicated-ledger",
"Get all the under-replicated ledgers which have been marked for re-replication.",
desc.ToString(),
desc.ExampleToString())

var include string
var exclude string
var show bool

vc.SetRunFunc(func() error {
return doListUnderReplicatedLedger(vc, include, exclude, show)
})

vc.FlagSetGroup.InFlagSet("List under replicated ledgers", func(set *pflag.FlagSet) {
set.StringVar(&include, "include", "", "Show the under-replicated ledger of the bookie.")
set.StringVar(&exclude, "exclude", "", "Show the under-replicated ledger exclude the bookie.")
set.BoolVar(&show, "show", false, "Show the replicate ledger list.")
})
}

func doListUnderReplicatedLedger(vc *cmdutils.VerbCmd, include, exclude string, show bool) error {
admin := cmdutils.NewBookieClient()
var l interface{}
var err error
if show {
l, err = admin.AutoRecovery().PrintListUnderReplicatedLedger(include, exclude)
} else {
l, err = admin.AutoRecovery().ListUnderReplicatedLedger(include, exclude)
}

if err == nil {
cmdutils.PrintJSON(vc.Command.OutOrStdout(), l)
}

return err
}
Loading