This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
/
produce.go
108 lines (92 loc) · 2.48 KB
/
produce.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
// Copyright © 2017 Salim Alami Idrissi
//
// Licensed 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 cmd
import (
"context"
"fmt"
"io/ioutil"
"log"
"github.com/celrenheit/sandglass-grpc/go/sgproto"
"github.com/celrenheit/sandglass/cmd/cmdcommon"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// produceCmd represents the list command
var produceCmd = &cobra.Command{
Use: "produce",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
log.Fatal("you should select one topic")
}
var (
topic = args[0]
partition = viper.GetString("partition")
)
var data []byte
switch {
case len(args) == 2:
data = []byte(args[1])
case cmd.Flag("file").Changed:
var err error
data, err = ioutil.ReadFile(viper.GetString("file"))
if err != nil {
log.Fatal(err)
}
default:
log.Fatal("you should provide a file or a payload")
}
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
defer cancel()
produce := func(msgs []*sgproto.Message) error {
_, err := client.Produce(ctx, &sgproto.ProduceMessageRequest{
Topic: topic,
Partition: partition,
Messages: msgs,
})
return err
}
batchSize := 10000
messages := make([]*sgproto.Message, 0, batchSize)
for i := 0; i < viper.GetInt("number"); i++ {
messages = append(messages, &sgproto.Message{
Value: data,
})
if len(messages) == batchSize {
if err := produce(messages); err != nil {
panic(err)
}
messages = messages[:0]
}
}
if len(messages) > 0 {
if err := produce(messages); err != nil {
panic(err)
}
}
fmt.Println("OK")
},
}
func init() {
RootCmd.AddCommand(produceCmd)
produceCmd.Flags().String("partition", "", "In which partition should we produce")
produceCmd.Flags().String("file", "", "File to produce")
produceCmd.Flags().IntP("number", "n", 1, "File to produce")
cmdcommon.BindViper(produceCmd.Flags(),
"partition",
"file",
"number",
)
}