2021-05-26 19:37:10 +08:00
package kafka
import (
2023-03-02 12:00:31 +08:00
"context"
2022-06-08 17:17:55 +08:00
"errors"
2023-04-28 18:33:33 +08:00
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
log "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2023-03-21 12:28:21 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2022-09-15 01:22:20 +08:00
2021-05-26 19:37:10 +08:00
"github.com/Shopify/sarama"
2023-04-28 18:33:33 +08:00
"google.golang.org/protobuf/proto"
2022-09-15 01:22:20 +08:00
2023-03-16 10:46:06 +08:00
prome "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
2021-05-26 19:37:10 +08:00
)
2023-05-19 19:43:43 +08:00
var errEmptyMsg = errors . New ( "binary msg is empty" )
2023-03-21 12:28:21 +08:00
2021-05-26 19:37:10 +08:00
type Producer struct {
topic string
addr [ ] string
config * sarama . Config
producer sarama . SyncProducer
}
func NewKafkaProducer ( addr [ ] string , topic string ) * Producer {
p := Producer { }
2022-06-15 18:02:48 +08:00
p . config = sarama . NewConfig ( ) //Instantiate a sarama Config
p . config . Producer . Return . Successes = true //Whether to enable the successes channel to be notified after the message is sent successfully
p . config . Producer . Return . Errors = true
2021-07-08 11:56:39 +08:00
p . config . Producer . RequiredAcks = sarama . WaitForAll //Set producer Message Reply level 0 1 all
p . config . Producer . Partitioner = sarama . NewHashPartitioner //Set the hash-key automatic hash partition. When sending a message, you must specify the key value of the message. If there is no key, the partition will be selected randomly
2022-09-16 16:51:12 +08:00
if config . Config . Kafka . SASLUserName != "" && config . Config . Kafka . SASLPassword != "" {
2022-09-16 11:57:01 +08:00
p . config . Net . SASL . Enable = true
2022-09-16 16:51:12 +08:00
p . config . Net . SASL . User = config . Config . Kafka . SASLUserName
p . config . Net . SASL . Password = config . Config . Kafka . SASLPassword
2022-09-16 11:57:01 +08:00
}
2021-05-26 19:37:10 +08:00
p . addr = addr
p . topic = topic
2021-07-08 11:56:39 +08:00
producer , err := sarama . NewSyncProducer ( p . addr , p . config ) //Initialize the client
2021-05-26 19:37:10 +08:00
if err != nil {
2021-11-26 14:20:50 +08:00
panic ( err . Error ( ) )
2021-05-26 19:37:10 +08:00
}
p . producer = producer
return & p
}
2023-05-19 19:43:43 +08:00
2023-03-22 18:35:21 +08:00
func GetMQHeaderWithContext ( ctx context . Context ) ( [ ] sarama . RecordHeader , error ) {
2023-05-10 17:18:04 +08:00
operationID , opUserID , platform , connID , err := mcontext . GetCtxInfos ( ctx )
2023-03-22 18:35:21 +08:00
if err != nil {
return nil , err
}
return [ ] sarama . RecordHeader {
{ Key : [ ] byte ( constant . OperationID ) , Value : [ ] byte ( operationID ) } ,
{ Key : [ ] byte ( constant . OpUserID ) , Value : [ ] byte ( opUserID ) } ,
{ Key : [ ] byte ( constant . OpUserPlatform ) , Value : [ ] byte ( platform ) } ,
{ Key : [ ] byte ( constant . ConnID ) , Value : [ ] byte ( connID ) } } , err
}
2023-05-19 19:43:43 +08:00
2023-03-22 18:35:21 +08:00
func GetContextWithMQHeader ( header [ ] * sarama . RecordHeader ) context . Context {
var values [ ] string
for _ , recordHeader := range header {
values = append ( values , string ( recordHeader . Value ) )
}
2023-04-28 18:39:21 +08:00
return mcontext . WithMustInfoCtx ( values ) // TODO
2023-03-22 18:35:21 +08:00
}
2023-05-19 19:43:43 +08:00
func ( p * Producer ) SendMessage ( ctx context . Context , key string , msg proto . Message ) ( int32 , int64 , error ) {
log . ZDebug ( ctx , "SendMessage" , "msg" , msg , "topic" , p . topic , "key" , key )
2021-05-26 19:37:10 +08:00
kMsg := & sarama . ProducerMessage { }
kMsg . Topic = p . topic
2022-06-08 17:24:22 +08:00
kMsg . Key = sarama . StringEncoder ( key )
2023-05-19 19:43:43 +08:00
bMsg , err := proto . Marshal ( msg )
2021-05-26 19:37:10 +08:00
if err != nil {
2023-03-21 12:28:21 +08:00
return 0 , 0 , utils . Wrap ( err , "kafka proto Marshal err" )
2021-05-26 19:37:10 +08:00
}
2022-06-08 17:17:55 +08:00
if len ( bMsg ) == 0 {
2023-05-19 19:43:43 +08:00
return 0 , 0 , utils . Wrap ( errEmptyMsg , "" )
2022-06-08 17:17:55 +08:00
}
2021-05-26 19:37:10 +08:00
kMsg . Value = sarama . ByteEncoder ( bMsg )
2022-06-15 18:02:48 +08:00
if kMsg . Key . Length ( ) == 0 || kMsg . Value . Length ( ) == 0 {
2023-05-19 19:43:43 +08:00
return 0 , 0 , utils . Wrap ( errEmptyMsg , "" )
2022-06-15 18:02:48 +08:00
}
2023-03-02 12:00:31 +08:00
kMsg . Metadata = ctx
2023-03-22 18:35:21 +08:00
header , err := GetMQHeaderWithContext ( ctx )
2023-03-21 12:28:21 +08:00
if err != nil {
return 0 , 0 , utils . Wrap ( err , "" )
}
2023-03-22 18:35:21 +08:00
kMsg . Headers = header
2023-02-23 17:28:57 +08:00
partition , offset , err := p . producer . SendMessage ( kMsg )
2023-03-22 10:54:39 +08:00
log . ZDebug ( ctx , "ByteEncoder SendMessage end" , "key " , kMsg . Key , "key length" , kMsg . Value . Length ( ) )
2023-02-23 17:28:57 +08:00
if err == nil {
2023-02-24 11:13:16 +08:00
prome . Inc ( prome . SendMsgCounter )
2022-09-15 01:22:20 +08:00
}
2023-02-23 17:28:57 +08:00
return partition , offset , utils . Wrap ( err , "" )
2021-05-26 19:37:10 +08:00
}