Skip to content

Commit 23fb7cc

Browse files
committed
temp storage
Signed-off-by: chyezh <chyezh@outlook.com>
1 parent fb77971 commit 23fb7cc

5 files changed

Lines changed: 256 additions & 8 deletions

File tree

mq/factory.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ func ParsePositionFromCheckpoint(mqType string, messageID []byte) (ifc.MessageID
2222
}
2323
}
2424

25-
func ParseManualMessageID(mqType string, manualID int64) (ifc.MessageID, error) {
25+
func ParseManualMessageID(mqType string, manualID string) (ifc.MessageID, error) {
2626
// pulsar not supported yet
27-
return nil, errors.Newf("not supported mq type: %s", mqType)
27+
switch mqType {
28+
case "pulsar":
29+
return pulsar.StringToMsgID(manualID)
30+
default:
31+
return nil, errors.Newf("not supported mq type: %s", mqType)
32+
}
2833
}
2934

3035
func NewConsumer(mqType, address, channel string, config ifc.MqOption) (ifc.Consumer, error) {

mq/pulsar/message_id_data.pb.go

Lines changed: 177 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mq/pulsar/message_id_data.proto

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
syntax = "proto2";
2+
3+
package milvus.proto.streaming.wal.pulsar;
4+
5+
option go_package = "github.com/milvus-io/milvus/pkg/streaming/walimpls/impls/pulsar";
6+
7+
8+
message MessageIdData {
9+
required uint64 ledgerId = 1;
10+
required uint64 entryId = 2;
11+
optional int32 partition = 3;
12+
optional int32 batch_index = 4;
13+
}

mq/pulsar/pulsar_id.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package pulsar
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67

78
"github.com/apache/pulsar-client-go/pulsar"
9+
"github.com/cockroachdb/errors"
10+
"github.com/gogo/protobuf/proto"
811

912
"github.com/milvus-io/birdwatcher/mq/ifc"
1013
)
@@ -86,6 +89,40 @@ func msgIDToString(messageID pulsar.MessageID) string {
8689
}
8790

8891
// StringToMsgID is used to convert a string to message ID
89-
func stringToMsgID(msgString string) (pulsar.MessageID, error) {
90-
return pulsar.DeserializeMessageID([]byte(msgString))
92+
func StringToMsgID(msgString string) (ifc.MessageID, error) {
93+
ids := strings.Split(msgString, ":")
94+
if len(ids) != 2 {
95+
return nil, errors.Newf("invalid manual id: %s", msgString)
96+
}
97+
ledgerID, err := strconv.ParseUint(ids[0], 10, 64)
98+
if err != nil {
99+
return nil, err
100+
}
101+
entryID, err := strconv.ParseUint(ids[1], 10, 64)
102+
if err != nil {
103+
return nil, err
104+
}
105+
msgID := newMessageIDOfPulsar(ledgerID, entryID, 0)
106+
return DeserializePulsarMsgID(msgID.Serialize())
107+
}
108+
109+
// newMessageIDOfPulsar only for test.
110+
func newMessageIDOfPulsar(ledgerID uint64, entryID uint64, batchIdx int32) pulsar.MessageID {
111+
partitionIdx := int32(0)
112+
id := &MessageIdData{
113+
LedgerId: &ledgerID,
114+
EntryId: &entryID,
115+
BatchIndex: &batchIdx,
116+
Partition: &partitionIdx,
117+
}
118+
119+
msg, err := proto.Marshal(id)
120+
if err != nil {
121+
panic(err)
122+
}
123+
msgID, err := pulsar.DeserializeMessageID(msg)
124+
if err != nil {
125+
panic(err)
126+
}
127+
return msgID
91128
}

states/consume.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package states
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"path"
78

89
"github.com/cockroachdb/errors"
10+
"google.golang.org/protobuf/encoding/protojson"
911
"google.golang.org/protobuf/proto"
1012

1113
"github.com/milvus-io/birdwatcher/framework"
@@ -25,10 +27,19 @@ type ConsumeParam struct {
2527
Topic string `name:"topic" default:"" desc:"topic to consume"`
2628
ShardName string `name:"shard_name" default:"" desc:"shard name(vchannel name) to filter with"`
2729
Detail bool `name:"detail" default:"false" desc:"print msg detail"`
28-
ManualID int64 `name:"manual_id" default:"0" desc:"manual id"`
30+
ManualID string `name:"manual-id" default:"" desc:"manual id"`
31+
OutputFile string `name:"output-file" default:"" desc:"output file"`
2932
}
3033

3134
func (s *InstanceState) ConsumeCommand(ctx context.Context, p *ConsumeParam) error {
35+
of := os.Stdout
36+
if p.OutputFile != "" {
37+
var err error
38+
if of, err = os.OpenFile(p.OutputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644); err != nil {
39+
return err
40+
}
41+
}
42+
3243
var messageID ifc.MessageID
3344
switch p.StartPosition {
3445
case "cp":
@@ -94,15 +105,20 @@ func (s *InstanceState) ConsumeCommand(ctx context.Context, p *ConsumeParam) err
94105
fmt.Printf("%s ", msgType)
95106
switch msgType {
96107
case commonpb.MsgType_Insert, commonpb.MsgType_Delete:
108+
msgID := msg.ID()
97109
v, err := ParseMsg(header.GetBase().GetMsgType(), msg.Payload())
98110
if err != nil {
99111
fmt.Println(err.Error())
100112
}
101113
if p.ShardName == "" || v.GetShardName() == p.ShardName {
114+
fmt.Fprintf(of, "MsgID: %s, Shard: %s\n", msgID.String(), v.GetShardName())
102115
if p.Detail {
103-
fmt.Print(v)
116+
if s, err := protojson.Marshal(v); err == nil {
117+
fmt.Fprintln(of, string(s))
118+
} else {
119+
fmt.Fprintln(of, err.Error())
120+
}
104121
} else {
105-
fmt.Print(v.GetShardName())
106122
err := ValidateMsg(msgType, msg.Payload())
107123
if err != nil {
108124
fmt.Println(err.Error())
@@ -111,7 +127,7 @@ func (s *InstanceState) ConsumeCommand(ctx context.Context, p *ConsumeParam) err
111127
}
112128
default:
113129
}
114-
fmt.Println()
130+
fmt.Fprintln(of)
115131
}
116132
if eq, _ := msg.ID().Equal(latestID.Serialize()); eq {
117133
break

0 commit comments

Comments
 (0)