etcd-runner: pass in lock name as a command arg for lock_racer.

release-3.2
fanmin shi 2017-04-25 10:31:15 -07:00
parent 72fb756af3
commit debc69e1f2
1 changed files with 13 additions and 6 deletions

View File

@ -20,24 +20,29 @@ import (
"fmt" "fmt"
"github.com/coreos/etcd/clientv3/concurrency" "github.com/coreos/etcd/clientv3/concurrency"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// NewLockRacerCommand returns the cobra command for "lock-racer runner". // NewLockRacerCommand returns the cobra command for "lock-racer runner".
func NewLockRacerCommand() *cobra.Command { func NewLockRacerCommand() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "lock-racer", Use: "lock-racer [name of lock (defaults to 'racers')]",
Short: "Performs lock race operation", Short: "Performs lock race operation",
Run: runRacerFunc, Run: runRacerFunc,
} }
cmd.Flags().IntVar(&rounds, "rounds", 100, "number of rounds to run")
cmd.Flags().IntVar(&totalClientConnections, "total-client-connections", 10, "total number of client connections") cmd.Flags().IntVar(&totalClientConnections, "total-client-connections", 10, "total number of client connections")
return cmd return cmd
} }
func runRacerFunc(cmd *cobra.Command, args []string) { func runRacerFunc(cmd *cobra.Command, args []string) {
if len(args) > 0 { racers := "racers"
ExitWithError(ExitBadArgs, errors.New("lock-racer does not take any argument")) if len(args) == 1 {
racers = args[0]
}
if len(args) > 1 {
ExitWithError(ExitBadArgs, errors.New("lock-racer takes at most one argument"))
} }
rcs := make([]roundClient, totalClientConnections) rcs := make([]roundClient, totalClientConnections)
@ -61,7 +66,7 @@ func runRacerFunc(cmd *cobra.Command, args []string) {
break break
} }
} }
m := concurrency.NewMutex(s, "racers") m := concurrency.NewMutex(s, racers)
rcs[i].acquire = func() error { return m.Lock(ctx) } rcs[i].acquire = func() error { return m.Lock(ctx) }
rcs[i].validate = func() error { rcs[i].validate = func() error {
if cnt++; cnt != 1 { if cnt++; cnt != 1 {
@ -77,5 +82,7 @@ func runRacerFunc(cmd *cobra.Command, args []string) {
return nil return nil
} }
} }
doRounds(rcs, rounds) // each client creates 1 key from NewMutex() and delete it from Unlock()
// a round involves in 2*len(rcs) requests.
doRounds(rcs, rounds, 2*len(rcs))
} }