@@ -2,6 +2,8 @@ package mcp
22
33import (
44 "fmt"
5+ "mokapi/engine"
6+ "mokapi/engine/common"
57 "mokapi/kafka"
68 "mokapi/runtime"
79 "slices"
@@ -14,7 +16,8 @@ type Kafka struct {
1416 Type string `json:"type"`
1517 Brokers []Broker `json:"brokers"`
1618
17- info * runtime.KafkaInfo
19+ info * runtime.KafkaInfo
20+ client * engine.KafkaClient
1821}
1922
2023type Broker struct {
@@ -36,7 +39,8 @@ type Topic struct {
3639
3740 Operations []KafkaOperation `json:"operations,omitempty"`
3841
39- info * runtime.KafkaInfo
42+ info * runtime.KafkaInfo
43+ client * engine.KafkaClient
4044}
4145
4246type KafkaPartition struct {
@@ -74,9 +78,10 @@ func (m *mokapi) getKafkaApi(name string) any {
7478 for _ , api := range m .app .Kafka .List () {
7579 if api .Info .Name == name {
7680 result := & Kafka {
77- Name : name ,
78- Type : "kafka" ,
79- info : api ,
81+ Name : name ,
82+ Type : "kafka" ,
83+ info : api ,
84+ client : engine .NewKafkaClient (m .app ),
8085 }
8186 for it := api .Servers .Iter (); it .Next (); {
8287 b := it .Value ()
@@ -128,6 +133,7 @@ func (k *Kafka) GetTopic(name string) (Topic, error) {
128133 },
129134 Description : ch .Value .Description ,
130135 info : k .info ,
136+ client : k .client ,
131137 }
132138
133139 topic := k .info .Store .Topic (name )
@@ -180,45 +186,55 @@ func (k *Kafka) GetTopic(name string) (Topic, error) {
180186 t .Operations = append (t .Operations , result )
181187 }
182188
189+ slices .SortStableFunc (t .Operations , func (a , b KafkaOperation ) int {
190+ r := strings .Compare (a .Action , b .Action )
191+ if r != 0 {
192+ return r
193+ }
194+ return strings .Compare (a .Title , b .Title )
195+ })
196+
183197 return t , nil
184198}
185199
186200func (t * Topic ) Produce (partition int , value string , key string , headers map [string ]string ) error {
187- topic := t .info .Store .Topic (t .Name )
188- if topic == nil {
189- return fmt .Errorf ("topic '%s' not found" , t .Name )
190- }
191- p := topic .Partition (partition )
192- if p == nil {
193- return fmt .Errorf ("partition '%d' not found" , partition )
194- }
195-
196- r := & kafka.Record {
197- Time : time .Now (),
198- Key : kafka .NewBytes ([]byte (key )),
199- Value : kafka .NewBytes ([]byte (value )),
201+ msg := common.KafkaMessage {
202+ Value : []byte (value ),
203+ Data : nil ,
204+ Headers : headers ,
205+ Partition : partition ,
200206 }
201- if headers != nil {
202- for k , v := range headers {
203- r .Headers = append (r .Headers , kafka.RecordHeader {
204- Key : k ,
205- Value : []byte (v ),
206- })
207- }
207+ if key != "" {
208+ msg .Key = []byte (key )
208209 }
209210
210- result , err := p .Write (kafka.RecordBatch {
211- Records : []* kafka.Record {r },
211+ _ , err := t .client .Produce (& common.KafkaProduceArgs {
212+ Cluster : t .info .Info .Name ,
213+ Topic : t .Name ,
214+ Messages : []common.KafkaMessage {msg },
215+ Retry : common.KafkaProduceRetry {
216+ MaxRetryTime : 3 * time .Minute ,
217+ InitialRetryTime : 500 * time .Millisecond ,
218+ Retries : 10 ,
219+ Factor : 2 ,
220+ },
221+ ClientId : "mokapi-mcp" ,
212222 })
213223 if err != nil {
214224 return err
215225 }
216- if result .ErrorCode != kafka .None {
217- return fmt .Errorf ("%d: %s" , result .ErrorCode , result .ErrorMessage )
226+
227+ topic := t .info .Store .Topic (t .Name )
228+ if topic == nil {
229+ return fmt .Errorf ("topic '%s' not found" , t .Name )
230+ }
231+ p := topic .Partition (partition )
232+ if p == nil {
233+ return fmt .Errorf ("partition '%s' not found" , t .Name )
218234 }
219235 for _ , pt := range t .Partitions {
220236 if pt .Index == p .Index {
221- pt .Offset += 1
237+ pt .Offset = p . Offset ()
222238 }
223239 }
224240 return nil
0 commit comments