/
delete.go
120 lines (98 loc) · 2.5 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// client/command/remote/delete.go
//
// Copyright (c) 2017 Junpei Kawamoto
//
// This file is part of simple-kvs.
//
// simple-kvs is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// simple-kvs is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with simple-kvs. If not, see <http://www.gnu.org/licenses/>.
//
package command
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
pb "gopkg.in/cheggaaa/pb.v1"
"golang.org/x/sync/errgroup"
"github.com/itslab-kyushu/simple-kvs/kvs"
"github.com/itslab-kyushu/sss/cfg"
"github.com/urfave/cli"
"google.golang.org/grpc"
)
// deleteOpt defines option values for cmdDelete.
type deleteOpt struct {
Config *cfg.Config
Name string
Log io.Writer
}
// CmdDelete prepares deleting a file and run cmdDelete.
func CmdDelete(c *cli.Context) (err error) {
if c.NArg() != 1 {
return cli.ShowSubcommandHelp(c)
}
var log io.Writer
if c.GlobalBool("quiet") {
log = ioutil.Discard
} else {
log = os.Stderr
}
conf, err := cfg.ReadConfig(c.String("config"))
if err != nil {
return
}
return cmdDelete(&deleteOpt{
Config: conf,
Name: c.Args().First(),
Log: log,
})
}
func cmdDelete(opt *deleteOpt) (err error) {
// Configure logging.
bar := pb.New(opt.Config.NServers())
bar.Output = opt.Log
bar.Prefix("Server")
bar.Start()
defer bar.Finish()
wg, ctx := errgroup.WithContext(context.Background())
cpus := runtime.NumCPU()
semaphore := make(chan struct{}, cpus)
for _, server := range opt.Config.Servers {
semaphore <- struct{}{}
func(server cfg.Server) {
wg.Go(func() (err error) {
defer func() { <-semaphore }()
defer bar.Increment()
conn, err := grpc.Dial(
fmt.Sprintf("%s:%d", server.Address, server.Port),
grpc.WithInsecure(),
grpc.WithCompressor(grpc.NewGZIPCompressor()),
grpc.WithDecompressor(grpc.NewGZIPDecompressor()),
)
if err != nil {
return
}
defer conn.Close()
client := kvs.NewKvsClient(conn)
_, err = client.Delete(ctx, &kvs.Key{
Name: opt.Name,
})
return
})
}(server)
}
return wg.Wait()
}