2017-03-29 08:05:43 -05:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2017-03-30 09:29:23 -05:00
|
|
|
"encoding/json"
|
|
|
|
|
2017-03-29 08:05:43 -05:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/config"
|
2017-03-30 09:29:23 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/storage"
|
2017-04-11 05:52:26 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/sync/syncapi"
|
2017-03-29 08:05:43 -05:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-03-30 09:29:23 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-03-29 08:05:43 -05:00
|
|
|
sarama "gopkg.in/Shopify/sarama.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server contains all the logic for running a sync server
|
|
|
|
type Server struct {
|
|
|
|
roomServerConsumer *common.ContinualConsumer
|
2017-03-30 09:29:23 -05:00
|
|
|
db *storage.SyncServerDatabase
|
2017-04-10 09:12:18 -05:00
|
|
|
rp *RequestPool
|
2017-03-29 08:05:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates a new sync server. Call Start() to begin consuming from room servers.
|
2017-04-10 09:12:18 -05:00
|
|
|
func NewServer(cfg *config.Sync, rp *RequestPool, store *storage.SyncServerDatabase) (*Server, error) {
|
2017-03-29 08:05:43 -05:00
|
|
|
kafkaConsumer, err := sarama.NewConsumer(cfg.KafkaConsumerURIs, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
consumer := common.ContinualConsumer{
|
|
|
|
Topic: cfg.RoomserverOutputTopic,
|
|
|
|
Consumer: kafkaConsumer,
|
|
|
|
PartitionStore: store,
|
|
|
|
}
|
|
|
|
s := &Server{
|
|
|
|
roomServerConsumer: &consumer,
|
2017-03-30 09:29:23 -05:00
|
|
|
db: store,
|
2017-04-07 08:32:42 -05:00
|
|
|
rp: rp,
|
2017-03-29 08:05:43 -05:00
|
|
|
}
|
|
|
|
consumer.ProcessMessage = s.onMessage
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start consuming from room servers
|
|
|
|
func (s *Server) Start() error {
|
|
|
|
return s.roomServerConsumer.Start()
|
|
|
|
}
|
|
|
|
|
2017-04-10 09:12:18 -05:00
|
|
|
// onMessage is called when the sync server receives a new event from the room server output log.
|
|
|
|
// It is not safe for this function to be called from multiple goroutines, or else the
|
|
|
|
// sync stream position may race and be incorrectly calculated.
|
2017-03-29 08:05:43 -05:00
|
|
|
func (s *Server) onMessage(msg *sarama.ConsumerMessage) error {
|
2017-03-30 09:29:23 -05:00
|
|
|
// Parse out the event JSON
|
|
|
|
var output api.OutputRoomEvent
|
|
|
|
if err := json.Unmarshal(msg.Value, &output); err != nil {
|
|
|
|
// If the message was invalid, log it and move on to the next message in the stream
|
|
|
|
log.WithError(err).Errorf("roomserver output log: message parse failure")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ev, err := gomatrixserverlib.NewEventFromTrustedJSON(output.Event, false)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("roomserver output log: event parse failure")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"room_id": ev.RoomID(),
|
|
|
|
}).Info("received event from roomserver")
|
|
|
|
|
2017-04-10 09:12:18 -05:00
|
|
|
syncStreamPos, err := s.db.WriteEvent(&ev, output.AddsStateEventIDs, output.RemovesStateEventIDs)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-03-30 09:29:23 -05:00
|
|
|
// panic rather than continue with an inconsistent database
|
2017-04-05 04:30:13 -05:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event": string(ev.JSON()),
|
|
|
|
log.ErrorKey: err,
|
|
|
|
"add": output.AddsStateEventIDs,
|
|
|
|
"del": output.RemovesStateEventIDs,
|
|
|
|
}).Panicf("roomserver output log: write event failure")
|
2017-03-30 09:29:23 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-11 05:52:26 -05:00
|
|
|
s.rp.OnNewEvent(&ev, syncapi.StreamPosition(syncStreamPos))
|
2017-03-30 09:29:23 -05:00
|
|
|
|
2017-03-29 08:05:43 -05:00
|
|
|
return nil
|
|
|
|
}
|