2017-04-20 23:40:52 +01:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
2020-02-05 18:06:39 +00:00
|
|
|
// Copyright 2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 23:40:52 +01:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-09-02 17:13:15 +01:00
|
|
|
package input
|
2017-02-07 17:20:05 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-20 11:42:54 +01:00
|
|
|
"bytes"
|
2017-09-13 16:30:19 +01:00
|
|
|
"context"
|
2020-07-21 15:48:21 +01:00
|
|
|
"fmt"
|
2021-01-13 17:31:46 +00:00
|
|
|
"time"
|
2017-08-21 16:37:11 +01:00
|
|
|
|
2020-07-07 12:51:55 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2017-02-07 17:20:05 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 13:47:31 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2017-03-08 15:10:26 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2017-02-09 16:48:14 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2017-02-07 17:20:05 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-10-20 11:42:54 +01:00
|
|
|
"github.com/matrix-org/util"
|
2021-01-13 17:31:46 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-04-28 11:46:47 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-07 17:20:05 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 17:31:46 +00:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(processRoomEventDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
var processRoomEventDuration = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Namespace: "dendrite",
|
|
|
|
Subsystem: "roomserver",
|
|
|
|
Name: "processroomevent_duration_millis",
|
|
|
|
Help: "How long it takes the roomserver to process an event",
|
|
|
|
Buckets: []float64{ // milliseconds
|
|
|
|
5, 10, 25, 50, 75, 100, 250, 500,
|
|
|
|
1000, 2000, 3000, 4000, 5000, 6000,
|
|
|
|
7000, 8000, 9000, 10000, 15000, 20000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"room_id"},
|
|
|
|
)
|
|
|
|
|
2017-12-15 15:22:06 +00:00
|
|
|
// processRoomEvent can only be called once at a time
|
|
|
|
//
|
|
|
|
// TODO(#375): This should be rewritten to allow concurrent calls. The
|
|
|
|
// difficulty is in ensuring that we correctly annotate events with the correct
|
|
|
|
// state deltas when sending to kafka streams
|
2020-07-07 12:51:55 +01:00
|
|
|
// TODO: Break up function - we should probably do transaction ID checks before calling this.
|
|
|
|
// nolint:gocyclo
|
2020-09-02 17:13:15 +01:00
|
|
|
func (r *Inputer) processRoomEvent(
|
2017-09-13 16:30:19 +01:00
|
|
|
ctx context.Context,
|
2020-09-03 15:22:16 +01:00
|
|
|
input *api.InputRoomEvent,
|
2018-05-26 12:03:35 +01:00
|
|
|
) (eventID string, err error) {
|
2021-01-13 17:31:46 +00:00
|
|
|
// Measure how long it takes to process this event.
|
|
|
|
started := time.Now()
|
|
|
|
defer func() {
|
|
|
|
timetaken := time.Since(started)
|
|
|
|
processRoomEventDuration.With(prometheus.Labels{
|
|
|
|
"room_id": input.Event.RoomID(),
|
|
|
|
}).Observe(float64(timetaken.Milliseconds()))
|
|
|
|
}()
|
|
|
|
|
2017-02-07 17:20:05 +00:00
|
|
|
// Parse and validate the event JSON
|
2020-03-27 16:28:22 +00:00
|
|
|
headered := input.Event
|
|
|
|
event := headered.Unwrap()
|
2017-02-07 17:20:05 +00:00
|
|
|
|
2020-10-20 11:42:54 +01:00
|
|
|
// if we have already got this event then do not process it again, if the input kind is an outlier.
|
|
|
|
// Outliers contain no extra information which may warrant a re-processing.
|
|
|
|
if input.Kind == api.KindOutlier {
|
2020-10-20 12:29:53 +01:00
|
|
|
evs, err2 := r.DB.EventsFromIDs(ctx, []string{event.EventID()})
|
|
|
|
if err2 == nil && len(evs) == 1 {
|
2020-10-20 11:42:54 +01:00
|
|
|
// check hash matches if we're on early room versions where the event ID was a random string
|
2020-10-20 12:29:53 +01:00
|
|
|
idFormat, err2 := headered.RoomVersion.EventIDFormat()
|
|
|
|
if err2 == nil {
|
2020-10-20 11:42:54 +01:00
|
|
|
switch idFormat {
|
|
|
|
case gomatrixserverlib.EventIDFormatV1:
|
|
|
|
if bytes.Equal(event.EventReference().EventSHA256, evs[0].EventReference().EventSHA256) {
|
|
|
|
util.GetLogger(ctx).WithField("event_id", event.EventID()).Infof("Already processed event; ignoring")
|
|
|
|
return event.EventID(), nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
util.GetLogger(ctx).WithField("event_id", event.EventID()).Infof("Already processed event; ignoring")
|
|
|
|
return event.EventID(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:49:24 +01:00
|
|
|
// Check that the event passes authentication checks and work out
|
|
|
|
// the numeric IDs for the auth events.
|
2020-09-16 13:00:52 +01:00
|
|
|
isRejected := false
|
|
|
|
authEventNIDs, rejectionErr := helpers.CheckAuthEvents(ctx, r.DB, headered, input.AuthEventIDs)
|
|
|
|
if rejectionErr != nil {
|
2020-09-29 13:40:29 +01:00
|
|
|
logrus.WithError(rejectionErr).WithField("event_id", event.EventID()).WithField("auth_event_ids", input.AuthEventIDs).Error("helpers.CheckAuthEvents failed for event, rejecting event")
|
2020-09-16 13:00:52 +01:00
|
|
|
isRejected = true
|
2018-05-26 12:03:35 +01:00
|
|
|
}
|
|
|
|
|
2020-09-21 14:55:46 +01:00
|
|
|
var softfail bool
|
2020-10-07 14:05:33 +01:00
|
|
|
if input.Kind == api.KindNew {
|
2020-09-21 14:55:46 +01:00
|
|
|
// Check that the event passes authentication checks based on the
|
|
|
|
// current room state.
|
|
|
|
softfail, err = helpers.CheckForSoftFail(ctx, r.DB, headered, input.StateEventIDs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"event_id": event.EventID(),
|
|
|
|
"type": event.Type(),
|
|
|
|
"room": event.RoomID(),
|
|
|
|
}).WithError(err).Info("Error authing soft-failed event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:49:24 +01:00
|
|
|
// Store the event.
|
2021-11-22 09:13:12 +00:00
|
|
|
_, stateAtEvent, redactionEvent, redactedEventID, err := r.DB.StoreEvent(ctx, event, authEventNIDs, isRejected)
|
2017-02-15 11:05:45 +00:00
|
|
|
if err != nil {
|
2020-07-21 15:48:21 +01:00
|
|
|
return "", fmt.Errorf("r.DB.StoreEvent: %w", err)
|
2017-02-09 16:48:14 +00:00
|
|
|
}
|
2020-09-16 13:00:52 +01:00
|
|
|
|
2020-07-07 12:51:55 +01:00
|
|
|
// if storing this event results in it being redacted then do so.
|
2020-09-16 13:00:52 +01:00
|
|
|
if !isRejected && redactedEventID == event.EventID() {
|
2020-11-16 15:44:53 +00:00
|
|
|
r, rerr := eventutil.RedactEvent(redactionEvent, event)
|
2020-07-07 12:51:55 +01:00
|
|
|
if rerr != nil {
|
2020-07-21 15:48:21 +01:00
|
|
|
return "", fmt.Errorf("eventutil.RedactEvent: %w", rerr)
|
2020-07-07 12:51:55 +01:00
|
|
|
}
|
2020-11-16 15:44:53 +00:00
|
|
|
event = r
|
2020-07-07 12:51:55 +01:00
|
|
|
}
|
2017-02-07 17:20:05 +00:00
|
|
|
|
2020-05-18 17:49:24 +01:00
|
|
|
// For outliers we can stop after we've stored the event itself as it
|
|
|
|
// doesn't have any associated state to store and we don't need to
|
|
|
|
// notify anyone about it.
|
2017-02-07 17:20:05 +00:00
|
|
|
if input.Kind == api.KindOutlier {
|
2020-05-18 17:49:24 +01:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"event_id": event.EventID(),
|
|
|
|
"type": event.Type(),
|
|
|
|
"room": event.RoomID(),
|
2020-09-21 14:55:46 +01:00
|
|
|
"sender": event.Sender(),
|
2020-09-15 11:17:46 +01:00
|
|
|
}).Debug("Stored outlier")
|
2018-05-26 12:03:35 +01:00
|
|
|
return event.EventID(), nil
|
2017-02-07 17:20:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 10:02:48 +01:00
|
|
|
roomInfo, err := r.DB.RoomInfo(ctx, event.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("r.DB.RoomInfo: %w", err)
|
|
|
|
}
|
|
|
|
if roomInfo == nil {
|
|
|
|
return "", fmt.Errorf("r.DB.RoomInfo missing for room %s", event.RoomID())
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:05:45 +00:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID == 0 {
|
|
|
|
// We haven't calculated a state for this event yet.
|
|
|
|
// Lets calculate one.
|
2020-09-16 13:00:52 +01:00
|
|
|
err = r.calculateAndSetState(ctx, input, *roomInfo, &stateAtEvent, event, isRejected)
|
2020-10-19 14:59:13 +01:00
|
|
|
if err != nil && input.Kind != api.KindOld {
|
2020-07-21 15:48:21 +01:00
|
|
|
return "", fmt.Errorf("r.calculateAndSetState: %w", err)
|
2017-09-20 10:59:19 +01:00
|
|
|
}
|
2017-02-15 11:05:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 13:00:52 +01:00
|
|
|
// We stop here if the event is rejected: We've stored it but won't update forward extremities or notify anyone about it.
|
2020-09-21 14:55:46 +01:00
|
|
|
if isRejected || softfail {
|
2020-09-16 13:00:52 +01:00
|
|
|
logrus.WithFields(logrus.Fields{
|
2020-09-21 14:55:46 +01:00
|
|
|
"event_id": event.EventID(),
|
|
|
|
"type": event.Type(),
|
|
|
|
"room": event.RoomID(),
|
|
|
|
"soft_fail": softfail,
|
|
|
|
"sender": event.Sender(),
|
2020-09-16 13:00:52 +01:00
|
|
|
}).Debug("Stored rejected event")
|
|
|
|
return event.EventID(), rejectionErr
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:59:13 +01:00
|
|
|
switch input.Kind {
|
|
|
|
case api.KindNew:
|
|
|
|
if err = r.updateLatestEvents(
|
|
|
|
ctx, // context
|
|
|
|
roomInfo, // room info for the room being updated
|
|
|
|
stateAtEvent, // state at event (below)
|
|
|
|
event, // event
|
|
|
|
input.SendAsServer, // send as server
|
|
|
|
input.TransactionID, // transaction ID
|
|
|
|
input.HasState, // rewrites state?
|
|
|
|
); err != nil {
|
|
|
|
return "", fmt.Errorf("r.updateLatestEvents: %w", err)
|
|
|
|
}
|
|
|
|
case api.KindOld:
|
|
|
|
err = r.WriteOutputEvents(event.RoomID(), []api.OutputEvent{
|
|
|
|
{
|
|
|
|
Type: api.OutputTypeOldRoomEvent,
|
|
|
|
OldRoomEvent: &api.OutputOldRoomEvent{
|
|
|
|
Event: headered,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("r.WriteOutputEvents (old): %w", err)
|
|
|
|
}
|
2020-05-18 17:49:24 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 12:51:55 +01:00
|
|
|
// processing this event resulted in an event (which may not be the one we're processing)
|
|
|
|
// being redacted. We are guaranteed to have both sides (the redaction/redacted event),
|
|
|
|
// so notify downstream components to redact this event - they should have it if they've
|
|
|
|
// been tracking our output log.
|
|
|
|
if redactedEventID != "" {
|
|
|
|
err = r.WriteOutputEvents(event.RoomID(), []api.OutputEvent{
|
|
|
|
{
|
|
|
|
Type: api.OutputTypeRedactedEvent,
|
|
|
|
RedactedEvent: &api.OutputRedactedEvent{
|
|
|
|
RedactedEventID: redactedEventID,
|
|
|
|
RedactedBecause: redactionEvent.Headered(headered.RoomVersion),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-19 14:59:13 +01:00
|
|
|
return "", fmt.Errorf("r.WriteOutputEvents (redactions): %w", err)
|
2020-07-07 12:51:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 14:50:30 +00:00
|
|
|
// Update the extremities of the event graph for the room
|
2020-05-18 17:49:24 +01:00
|
|
|
return event.EventID(), nil
|
2018-05-26 12:03:35 +01:00
|
|
|
}
|
|
|
|
|
2020-09-02 17:13:15 +01:00
|
|
|
func (r *Inputer) calculateAndSetState(
|
2018-05-26 12:03:35 +01:00
|
|
|
ctx context.Context,
|
2020-09-03 15:22:16 +01:00
|
|
|
input *api.InputRoomEvent,
|
2020-09-02 10:02:48 +01:00
|
|
|
roomInfo types.RoomInfo,
|
2018-05-26 12:03:35 +01:00
|
|
|
stateAtEvent *types.StateAtEvent,
|
2020-11-16 15:44:53 +00:00
|
|
|
event *gomatrixserverlib.Event,
|
2020-09-16 13:00:52 +01:00
|
|
|
isRejected bool,
|
2018-05-26 12:03:35 +01:00
|
|
|
) error {
|
2020-03-19 18:33:04 +00:00
|
|
|
var err error
|
2020-09-02 10:02:48 +01:00
|
|
|
roomState := state.NewStateResolution(r.DB, roomInfo)
|
2020-02-05 16:25:58 +00:00
|
|
|
|
2020-09-16 13:00:52 +01:00
|
|
|
if input.HasState && !isRejected {
|
2020-05-20 18:03:06 +01:00
|
|
|
// Check here if we think we're in the room already.
|
2020-05-18 17:49:24 +01:00
|
|
|
stateAtEvent.Overwrite = true
|
2020-05-20 18:03:06 +01:00
|
|
|
var joinEventNIDs []types.EventNID
|
|
|
|
// Request join memberships only for local users only.
|
2020-09-02 10:02:48 +01:00
|
|
|
if joinEventNIDs, err = r.DB.GetMembershipEventNIDsForRoom(ctx, roomInfo.RoomNID, true, true); err == nil {
|
2020-05-20 18:03:06 +01:00
|
|
|
// If we have no local users that are joined to the room then any state about
|
|
|
|
// the room that we have is quite possibly out of date. Therefore in that case
|
|
|
|
// we should overwrite it rather than merge it.
|
|
|
|
stateAtEvent.Overwrite = len(joinEventNIDs) == 0
|
|
|
|
}
|
2020-05-18 17:49:24 +01:00
|
|
|
|
2018-05-26 12:03:35 +01:00
|
|
|
// We've been told what the state at the event is so we don't need to calculate it.
|
|
|
|
// Check that those state events are in the database and store the state.
|
|
|
|
var entries []types.StateEntry
|
2020-05-20 18:03:06 +01:00
|
|
|
if entries, err = r.DB.StateEntriesForEventIDs(ctx, input.StateEventIDs); err != nil {
|
2020-09-15 11:17:46 +01:00
|
|
|
return fmt.Errorf("r.DB.StateEntriesForEventIDs: %w", err)
|
2018-05-26 12:03:35 +01:00
|
|
|
}
|
2020-09-15 11:17:46 +01:00
|
|
|
entries = types.DeduplicateStateEntries(entries)
|
2018-05-26 12:03:35 +01:00
|
|
|
|
2020-09-02 10:02:48 +01:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID, err = r.DB.AddState(ctx, roomInfo.RoomNID, nil, entries); err != nil {
|
2020-09-15 11:17:46 +01:00
|
|
|
return fmt.Errorf("r.DB.AddState: %w", err)
|
2018-05-26 12:03:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-18 17:49:24 +01:00
|
|
|
stateAtEvent.Overwrite = false
|
|
|
|
|
2018-05-26 12:03:35 +01:00
|
|
|
// We haven't been told what the state at the event is so we need to calculate it from the prev_events
|
2020-09-16 13:00:52 +01:00
|
|
|
if stateAtEvent.BeforeStateSnapshotNID, err = roomState.CalculateAndStoreStateBeforeEvent(ctx, event, isRejected); err != nil {
|
2020-09-15 11:17:46 +01:00
|
|
|
return fmt.Errorf("roomState.CalculateAndStoreStateBeforeEvent: %w", err)
|
2018-05-26 12:03:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 11:17:46 +01:00
|
|
|
|
|
|
|
err = r.DB.SetState(ctx, stateAtEvent.EventNID, stateAtEvent.BeforeStateSnapshotNID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("r.DB.SetState: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-08-21 16:37:11 +01:00
|
|
|
}
|