-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat-cdc:use multi chann communication between consumer and pika master
Signed-off-by: LeeHao <1838249551@qq.com>
- Loading branch information
Showing
11 changed files
with
161 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
# Pika cdc | ||
**A tool for incremental synchronization of pika command** | ||
|
||
By imitating a pika slave | ||
|
||
# Build | ||
## generate proto file | ||
**Make sure the system has protoc installed** | ||
```bash | ||
cd pika | ||
protoc --go_out=. *.proto | ||
brew install protobuf | ||
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest | ||
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest | ||
``` | ||
|
||
## Build pika cdc | ||
```bash | ||
make | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,90 @@ | ||
package consumer | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/segmentio/kafka-go" | ||
"github.com/sirupsen/logrus" | ||
"log" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Kafka struct { | ||
servers []string | ||
topic string | ||
retries int | ||
conns []*kafka.Conn | ||
wg sync.WaitGroup | ||
messageChan chan kafka.Message | ||
stopChan chan bool | ||
once sync.Once | ||
protocol Protocol | ||
servers []string | ||
topic string | ||
retries int | ||
conns []*kafka.Conn | ||
wg sync.WaitGroup | ||
msgChanns map[string]chan []byte | ||
stopChan chan bool | ||
once sync.Once | ||
protocol Protocol | ||
} | ||
|
||
func (k *Kafka) SendCmdMessage(msg []byte) error { | ||
select { | ||
case k.messageChan <- kafka.Message{Value: k.protocol.ToConsumer(msg)}: | ||
return nil | ||
case <-time.After(2 * time.Second): | ||
e := errors.New("send pika cmd timeout") | ||
logrus.Warn("{}", e) | ||
return e | ||
} | ||
} | ||
|
||
func (k *Kafka) sendMessage() { | ||
for { | ||
select { | ||
case msg := <-k.messageChan: | ||
for _, conn := range k.conns { | ||
_, _ = conn.WriteMessages(msg) | ||
} | ||
case _ = <-k.stopChan: | ||
return | ||
} | ||
} | ||
func (k *Kafka) SendCmdMessage(dbName string, msg []byte) error { | ||
//retries := k.retries | ||
//select { | ||
//case *k.messageChan <- k.protocol.ToConsumer(msg): | ||
// return nil | ||
//case <-time.After(2 * time.Second): | ||
// e := errors.New("send pika cmd timeout and retry send pika cmd") | ||
// logrus.Warn("{}", e) | ||
// retries-- | ||
// if retries <= 0 { | ||
// break | ||
// } | ||
//} | ||
return nil | ||
} | ||
|
||
func (k *Kafka) Name() string { | ||
return "Kafka" | ||
} | ||
|
||
func NewKafka(servers []string, topic string, retries int) (*Kafka, error) { | ||
func NewKafka(servers []string, topic string, retries int, msgChanns map[string]chan []byte) (*Kafka, error) { | ||
k := &Kafka{} | ||
k.protocol = &KafkaProtocol{} | ||
for _, server := range servers { | ||
conn, err := kafka.DialLeader(context.Background(), "tcp", server, topic, 0) | ||
if err != nil { | ||
return k, err | ||
} else { | ||
conn.SetWriteDeadline(time.Now().Add(10 * time.Second)) | ||
k.conns = append(k.conns, conn) | ||
} | ||
} | ||
k.messageChan = make(chan kafka.Message) | ||
k.stopChan = make(chan bool) | ||
go k.sendMessage() | ||
//k.protocol = &KafkaProtocol{} | ||
//for _, server := range servers { | ||
// conn, err := kafka.DialLeader(context.Background(), "tcp", server, topic, 0) | ||
// if err != nil { | ||
// return k, err | ||
// } else { | ||
// conn.SetWriteDeadline(time.Now().Add(10 * time.Second)) | ||
// k.conns = append(k.conns, conn) | ||
// } | ||
//} | ||
//k.messageChan = msgChanns | ||
//k.stopChan = make(chan bool) | ||
//k.retries = retries | ||
return k, nil | ||
} | ||
|
||
func (k *Kafka) close() error { | ||
k.stopChan <- true | ||
close(k.stopChan) | ||
close(k.messageChan) | ||
for _, conn := range k.conns { | ||
err := conn.Close() | ||
if err != nil { | ||
log.Println(err) | ||
return err | ||
} | ||
} | ||
//k.stopChan <- true | ||
//close(k.stopChan) | ||
//close(*k.messageChan) | ||
//for _, conn := range k.conns { | ||
// err := conn.Close() | ||
// if err != nil { | ||
// logrus.Warn(err) | ||
// return err | ||
// } | ||
//} | ||
return nil | ||
} | ||
func (k *Kafka) Close() error { | ||
var err error | ||
err = nil | ||
k.once.Do(func() { | ||
err = k.close() | ||
}) | ||
return err | ||
//var err error | ||
//err = nil | ||
//k.once.Do(func() { | ||
// err = k.close() | ||
//}) | ||
//return err | ||
return nil | ||
} | ||
func (k *Kafka) Run() { | ||
|
||
//select { | ||
//case msg := <-*k.messageChan: | ||
// k.SendCmdMessage(msg) | ||
//case <-k.stopChan: | ||
// return | ||
//} | ||
} | ||
func (k *Kafka) Stop() { | ||
|
||
k.stopChan <- true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.