2017-04-20 17:40:52 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-12 10:06:26 -05:00
|
|
|
package consumers
|
2017-03-29 08:05:43 -05:00
|
|
|
|
|
|
|
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/common"
|
2017-03-30 09:29:23 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-04-20 11:22:44 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/config"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/sync"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2017-03-30 09:29:23 -05:00
|
|
|
"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-05-10 04:42:00 -05:00
|
|
|
notifier *sync.Notifier
|
2017-03-29 08:05:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates a new sync server. Call Start() to begin consuming from room servers.
|
2017-05-10 04:42:00 -05:00
|
|
|
func NewServer(cfg *config.Sync, n *sync.Notifier, 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-05-10 04:42:00 -05:00
|
|
|
notifier: n,
|
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-05-10 04:42:00 -05:00
|
|
|
s.notifier.OnNewEvent(&ev, types.StreamPosition(syncStreamPos))
|
2017-03-30 09:29:23 -05:00
|
|
|
|
2017-03-29 08:05:43 -05:00
|
|
|
return nil
|
|
|
|
}
|