2020-02-13 18:27:33 +01:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-05-14 10:53:55 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const outputRoomEventsSchema = `
|
|
|
|
-- Stores output room events received from the roomserver.
|
|
|
|
CREATE TABLE IF NOT EXISTS syncapi_output_room_events (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
event_id TEXT NOT NULL UNIQUE,
|
|
|
|
room_id TEXT NOT NULL,
|
2020-03-19 13:07:01 +01:00
|
|
|
headered_event_json TEXT NOT NULL,
|
2020-02-13 18:27:33 +01:00
|
|
|
type TEXT NOT NULL,
|
|
|
|
sender TEXT NOT NULL,
|
|
|
|
contains_url BOOL NOT NULL,
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
add_state_ids TEXT, -- JSON encoded string array
|
|
|
|
remove_state_ids TEXT, -- JSON encoded string array
|
2020-02-13 18:27:33 +01:00
|
|
|
session_id BIGINT,
|
|
|
|
transaction_id TEXT,
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
exclude_from_sync BOOL NOT NULL DEFAULT FALSE
|
2020-02-13 18:27:33 +01:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const insertEventSQL = "" +
|
|
|
|
"INSERT INTO syncapi_output_room_events (" +
|
2020-03-19 13:07:01 +01:00
|
|
|
"id, room_id, event_id, headered_event_json, type, sender, contains_url, add_state_ids, remove_state_ids, session_id, transaction_id, exclude_from_sync" +
|
2020-02-13 18:27:33 +01:00
|
|
|
") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) " +
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
"ON CONFLICT (event_id) DO UPDATE SET exclude_from_sync = $13"
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
const selectEventsSQL = "" +
|
2020-03-19 13:07:01 +01:00
|
|
|
"SELECT id, headered_event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events WHERE event_id = $1"
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
const selectRecentEventsSQL = "" +
|
2020-03-19 13:07:01 +01:00
|
|
|
"SELECT id, headered_event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events" +
|
2020-02-13 18:27:33 +01:00
|
|
|
" WHERE room_id = $1 AND id > $2 AND id <= $3" +
|
|
|
|
" ORDER BY id DESC LIMIT $4"
|
|
|
|
|
|
|
|
const selectRecentEventsForSyncSQL = "" +
|
2020-03-19 13:07:01 +01:00
|
|
|
"SELECT id, headered_event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events" +
|
2020-02-13 18:27:33 +01:00
|
|
|
" WHERE room_id = $1 AND id > $2 AND id <= $3 AND exclude_from_sync = FALSE" +
|
|
|
|
" ORDER BY id DESC LIMIT $4"
|
|
|
|
|
|
|
|
const selectEarlyEventsSQL = "" +
|
2020-03-19 13:07:01 +01:00
|
|
|
"SELECT id, headered_event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events" +
|
2020-02-13 18:27:33 +01:00
|
|
|
" WHERE room_id = $1 AND id > $2 AND id <= $3" +
|
|
|
|
" ORDER BY id ASC LIMIT $4"
|
|
|
|
|
|
|
|
const selectMaxEventIDSQL = "" +
|
|
|
|
"SELECT MAX(id) FROM syncapi_output_room_events"
|
|
|
|
|
2020-07-08 18:45:39 +02:00
|
|
|
const updateEventJSONSQL = "" +
|
|
|
|
"UPDATE syncapi_output_room_events SET headered_event_json=$1 WHERE event_id=$2"
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
// In order for us to apply the state updates correctly, rows need to be ordered in the order they were received (id).
|
|
|
|
/*
|
|
|
|
$1 = oldPos,
|
|
|
|
$2 = newPos,
|
|
|
|
$3 = pq.StringArray(stateFilterPart.Senders),
|
|
|
|
$4 = pq.StringArray(stateFilterPart.NotSenders),
|
|
|
|
$5 = pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.Types)),
|
|
|
|
$6 = pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.NotTypes)),
|
|
|
|
$7 = stateFilterPart.ContainsURL,
|
|
|
|
$8 = stateFilterPart.Limit,
|
|
|
|
*/
|
|
|
|
const selectStateInRangeSQL = "" +
|
2020-03-19 13:07:01 +01:00
|
|
|
"SELECT id, headered_event_json, exclude_from_sync, add_state_ids, remove_state_ids" +
|
2020-02-13 18:27:33 +01:00
|
|
|
" FROM syncapi_output_room_events" +
|
|
|
|
" WHERE (id > $1 AND id <= $2)" + // old/new pos
|
|
|
|
" AND (add_state_ids IS NOT NULL OR remove_state_ids IS NOT NULL)" +
|
|
|
|
/* " AND ( $3 IS NULL OR sender IN ($3) )" + // sender
|
|
|
|
" AND ( $4 IS NULL OR NOT(sender IN ($4)) )" + // not sender
|
|
|
|
" AND ( $5 IS NULL OR type IN ($5) )" + // type
|
|
|
|
" AND ( $6 IS NULL OR NOT(type IN ($6)) )" + // not type
|
|
|
|
" AND ( $7 IS NULL OR contains_url = $7)" + // contains URL? */
|
|
|
|
" ORDER BY id ASC" +
|
|
|
|
" LIMIT $8" // limit
|
|
|
|
|
2020-09-15 12:17:46 +02:00
|
|
|
const deleteEventsForRoomSQL = "" +
|
|
|
|
"DELETE FROM syncapi_output_room_events WHERE room_id = $1"
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
type outputRoomEventsStatements struct {
|
2020-07-21 16:48:21 +02:00
|
|
|
db *sql.DB
|
2020-05-01 13:41:38 +02:00
|
|
|
streamIDStatements *streamIDStatements
|
|
|
|
insertEventStmt *sql.Stmt
|
|
|
|
selectEventsStmt *sql.Stmt
|
|
|
|
selectMaxEventIDStmt *sql.Stmt
|
|
|
|
selectRecentEventsStmt *sql.Stmt
|
|
|
|
selectRecentEventsForSyncStmt *sql.Stmt
|
|
|
|
selectEarlyEventsStmt *sql.Stmt
|
|
|
|
selectStateInRangeStmt *sql.Stmt
|
2020-07-08 18:45:39 +02:00
|
|
|
updateEventJSONStmt *sql.Stmt
|
2020-09-15 12:17:46 +02:00
|
|
|
deleteEventsForRoomStmt *sql.Stmt
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:53:55 +02:00
|
|
|
func NewSqliteEventsTable(db *sql.DB, streamID *streamIDStatements) (tables.Events, error) {
|
|
|
|
s := &outputRoomEventsStatements{
|
2020-07-21 16:48:21 +02:00
|
|
|
db: db,
|
2020-05-14 10:53:55 +02:00
|
|
|
streamIDStatements: streamID,
|
|
|
|
}
|
|
|
|
_, err := db.Exec(outputRoomEventsSchema)
|
2020-02-13 18:27:33 +01:00
|
|
|
if err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if s.insertEventStmt, err = db.Prepare(insertEventSQL); err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if s.selectEventsStmt, err = db.Prepare(selectEventsSQL); err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if s.selectMaxEventIDStmt, err = db.Prepare(selectMaxEventIDSQL); err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if s.selectRecentEventsStmt, err = db.Prepare(selectRecentEventsSQL); err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if s.selectRecentEventsForSyncStmt, err = db.Prepare(selectRecentEventsForSyncSQL); err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if s.selectEarlyEventsStmt, err = db.Prepare(selectEarlyEventsSQL); err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if s.selectStateInRangeStmt, err = db.Prepare(selectStateInRangeSQL); err != nil {
|
2020-05-14 10:53:55 +02:00
|
|
|
return nil, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
2020-07-08 18:45:39 +02:00
|
|
|
if s.updateEventJSONStmt, err = db.Prepare(updateEventJSONSQL); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-15 12:17:46 +02:00
|
|
|
if s.deleteEventsForRoomStmt, err = db.Prepare(deleteEventsForRoomSQL); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:53:55 +02:00
|
|
|
return s, nil
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-07-08 18:45:39 +02:00
|
|
|
func (s *outputRoomEventsStatements) UpdateEventJSON(ctx context.Context, event *gomatrixserverlib.HeaderedEvent) error {
|
|
|
|
headeredJSON, err := json.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-21 11:42:08 +02:00
|
|
|
_, err = s.updateEventJSONStmt.ExecContext(ctx, headeredJSON, event.EventID())
|
|
|
|
return err
|
2020-07-08 18:45:39 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
// selectStateInRange returns the state events between the two given PDU stream positions, exclusive of oldPos, inclusive of newPos.
|
|
|
|
// Results are bucketed based on the room ID. If the same state is overwritten multiple times between the
|
|
|
|
// two positions, only the most recent state is returned.
|
2020-05-14 10:53:55 +02:00
|
|
|
func (s *outputRoomEventsStatements) SelectStateInRange(
|
2020-05-15 10:41:12 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, r types.Range,
|
2020-02-13 18:27:33 +01:00
|
|
|
stateFilterPart *gomatrixserverlib.StateFilter,
|
|
|
|
) (map[string]map[string]bool, map[string]types.StreamEvent, error) {
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectStateInRangeStmt)
|
2020-02-13 18:27:33 +01:00
|
|
|
|
|
|
|
rows, err := stmt.QueryContext(
|
2020-05-15 10:41:12 +02:00
|
|
|
ctx, r.Low(), r.High(),
|
2020-02-13 18:27:33 +01:00
|
|
|
/*pq.StringArray(stateFilterPart.Senders),
|
|
|
|
pq.StringArray(stateFilterPart.NotSenders),
|
|
|
|
pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.Types)),
|
|
|
|
pq.StringArray(filterConvertTypeWildcardToSQL(stateFilterPart.NotTypes)),
|
|
|
|
stateFilterPart.ContainsURL,*/
|
|
|
|
stateFilterPart.Limit,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-03-17 17:45:40 +01:00
|
|
|
defer rows.Close() // nolint: errcheck
|
2020-02-13 18:27:33 +01:00
|
|
|
// Fetch all the state change events for all rooms between the two positions then loop each event and:
|
|
|
|
// - Keep a cache of the event by ID (99% of state change events are for the event itself)
|
|
|
|
// - For each room ID, build up an array of event IDs which represents cumulative adds/removes
|
|
|
|
// For each room, map cumulative event IDs to events and return. This may need to a batch SELECT based on event ID
|
|
|
|
// if they aren't in the event ID cache. We don't handle state deletion yet.
|
|
|
|
eventIDToEvent := make(map[string]types.StreamEvent)
|
|
|
|
|
|
|
|
// RoomID => A set (map[string]bool) of state event IDs which are between the two positions
|
|
|
|
stateNeeded := make(map[string]map[string]bool)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
streamPos types.StreamPosition
|
|
|
|
eventBytes []byte
|
|
|
|
excludeFromSync bool
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
addIDsJSON string
|
|
|
|
delIDsJSON string
|
2020-02-13 18:27:33 +01:00
|
|
|
)
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
if err := rows.Scan(&streamPos, &eventBytes, &excludeFromSync, &addIDsJSON, &delIDsJSON); err != nil {
|
2020-02-13 18:27:33 +01:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
|
|
|
|
addIDs, delIDs, err := unmarshalStateIDs(addIDsJSON, delIDsJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
// Sanity check for deleted state and whine if we see it. We don't need to do anything
|
|
|
|
// since it'll just mark the event as not being needed.
|
|
|
|
if len(addIDs) < len(delIDs) {
|
|
|
|
log.WithFields(log.Fields{
|
2020-05-15 10:41:12 +02:00
|
|
|
"since": r.From,
|
|
|
|
"current": r.To,
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
"adds": addIDsJSON,
|
|
|
|
"dels": delIDsJSON,
|
2020-02-13 18:27:33 +01:00
|
|
|
}).Warn("StateBetween: ignoring deleted state")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle redacted events
|
2020-03-19 13:07:01 +01:00
|
|
|
var ev gomatrixserverlib.HeaderedEvent
|
|
|
|
if err := json.Unmarshal(eventBytes, &ev); err != nil {
|
2020-02-13 18:27:33 +01:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
needSet := stateNeeded[ev.RoomID()]
|
|
|
|
if needSet == nil { // make set if required
|
|
|
|
needSet = make(map[string]bool)
|
|
|
|
}
|
|
|
|
for _, id := range delIDs {
|
|
|
|
needSet[id] = false
|
|
|
|
}
|
|
|
|
for _, id := range addIDs {
|
|
|
|
needSet[id] = true
|
|
|
|
}
|
|
|
|
stateNeeded[ev.RoomID()] = needSet
|
|
|
|
|
|
|
|
eventIDToEvent[ev.EventID()] = types.StreamEvent{
|
2020-11-16 16:44:53 +01:00
|
|
|
HeaderedEvent: &ev,
|
2020-02-13 18:27:33 +01:00
|
|
|
StreamPosition: streamPos,
|
|
|
|
ExcludeFromSync: excludeFromSync,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateNeeded, eventIDToEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxID returns the ID of the last inserted event in this table. 'txn' is optional. If it is not supplied,
|
|
|
|
// then this function should only ever be used at startup, as it will race with inserting events if it is
|
|
|
|
// done afterwards. If there are no inserted events, 0 is returned.
|
2020-05-14 10:53:55 +02:00
|
|
|
func (s *outputRoomEventsStatements) SelectMaxEventID(
|
2020-02-13 18:27:33 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
) (id int64, err error) {
|
|
|
|
var nullableID sql.NullInt64
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectMaxEventIDStmt)
|
2020-02-13 18:27:33 +01:00
|
|
|
err = stmt.QueryRowContext(ctx).Scan(&nullableID)
|
|
|
|
if nullableID.Valid {
|
|
|
|
id = nullableID.Int64
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsertEvent into the output_room_events table. addState and removeState are an optional list of state event IDs. Returns the position
|
|
|
|
// of the inserted event.
|
2020-05-14 10:53:55 +02:00
|
|
|
func (s *outputRoomEventsStatements) InsertEvent(
|
2020-02-13 18:27:33 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2020-03-19 13:07:01 +01:00
|
|
|
event *gomatrixserverlib.HeaderedEvent, addState, removeState []string,
|
2020-02-13 18:27:33 +01:00
|
|
|
transactionID *api.TransactionID, excludeFromSync bool,
|
2020-07-21 16:48:21 +02:00
|
|
|
) (types.StreamPosition, error) {
|
2020-02-13 18:27:33 +01:00
|
|
|
var txnID *string
|
|
|
|
var sessionID *int64
|
|
|
|
if transactionID != nil {
|
|
|
|
sessionID = &transactionID.SessionID
|
|
|
|
txnID = &transactionID.TransactionID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse content as JSON and search for an "url" key
|
|
|
|
containsURL := false
|
|
|
|
var content map[string]interface{}
|
|
|
|
if json.Unmarshal(event.Content(), &content) != nil {
|
|
|
|
// Set containsURL to true if url is present
|
|
|
|
_, containsURL = content["url"]
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:07:01 +01:00
|
|
|
var headeredJSON []byte
|
2020-07-21 16:48:21 +02:00
|
|
|
headeredJSON, err := json.Marshal(event)
|
2020-02-13 18:27:33 +01:00
|
|
|
if err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return 0, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
addStateJSON, err := json.Marshal(addState)
|
|
|
|
if err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return 0, err
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
}
|
|
|
|
removeStateJSON, err := json.Marshal(removeState)
|
|
|
|
if err != nil {
|
2020-07-21 16:48:21 +02:00
|
|
|
return 0, err
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 11:42:08 +02:00
|
|
|
streamPos, err := s.streamIDStatements.nextStreamID(ctx, txn)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
insertStmt := sqlutil.TxStmt(txn, s.insertEventStmt)
|
|
|
|
_, err = insertStmt.ExecContext(
|
|
|
|
ctx,
|
|
|
|
streamPos,
|
|
|
|
event.RoomID(),
|
|
|
|
event.EventID(),
|
|
|
|
headeredJSON,
|
|
|
|
event.Type(),
|
|
|
|
event.Sender(),
|
|
|
|
containsURL,
|
|
|
|
string(addStateJSON),
|
|
|
|
string(removeStateJSON),
|
|
|
|
sessionID,
|
|
|
|
txnID,
|
|
|
|
excludeFromSync,
|
|
|
|
excludeFromSync,
|
|
|
|
)
|
2020-07-21 16:48:21 +02:00
|
|
|
return streamPos, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:53:55 +02:00
|
|
|
func (s *outputRoomEventsStatements) SelectRecentEvents(
|
2020-02-13 18:27:33 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2020-05-15 10:41:12 +02:00
|
|
|
roomID string, r types.Range, limit int,
|
2020-02-13 18:27:33 +01:00
|
|
|
chronologicalOrder bool, onlySyncEvents bool,
|
2020-06-26 16:34:41 +02:00
|
|
|
) ([]types.StreamEvent, bool, error) {
|
2020-02-13 18:27:33 +01:00
|
|
|
var stmt *sql.Stmt
|
|
|
|
if onlySyncEvents {
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt = sqlutil.TxStmt(txn, s.selectRecentEventsForSyncStmt)
|
2020-02-13 18:27:33 +01:00
|
|
|
} else {
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt = sqlutil.TxStmt(txn, s.selectRecentEventsStmt)
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:34:41 +02:00
|
|
|
rows, err := stmt.QueryContext(ctx, roomID, r.Low(), r.High(), limit+1)
|
2020-02-13 18:27:33 +01:00
|
|
|
if err != nil {
|
2020-06-26 16:34:41 +02:00
|
|
|
return nil, false, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectRecentEvents: rows.close() failed")
|
2020-02-13 18:27:33 +01:00
|
|
|
events, err := rowsToStreamEvents(rows)
|
|
|
|
if err != nil {
|
2020-06-26 16:34:41 +02:00
|
|
|
return nil, false, err
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
if chronologicalOrder {
|
|
|
|
// The events need to be returned from oldest to latest, which isn't
|
|
|
|
// necessary the way the SQL query returns them, so a sort is necessary to
|
|
|
|
// ensure the events are in the right order in the slice.
|
|
|
|
sort.SliceStable(events, func(i int, j int) bool {
|
|
|
|
return events[i].StreamPosition < events[j].StreamPosition
|
|
|
|
})
|
|
|
|
}
|
2020-06-26 16:34:41 +02:00
|
|
|
// we queried for 1 more than the limit, so if we returned one more mark limited=true
|
|
|
|
limited := false
|
|
|
|
if len(events) > limit {
|
|
|
|
limited = true
|
|
|
|
// re-slice the extra (oldest) event out: in chronological order this is the first entry, else the last.
|
|
|
|
if chronologicalOrder {
|
|
|
|
events = events[1:]
|
|
|
|
} else {
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return events, limited, nil
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:53:55 +02:00
|
|
|
func (s *outputRoomEventsStatements) SelectEarlyEvents(
|
2020-02-13 18:27:33 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2020-05-15 10:41:12 +02:00
|
|
|
roomID string, r types.Range, limit int,
|
2020-02-13 18:27:33 +01:00
|
|
|
) ([]types.StreamEvent, error) {
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectEarlyEventsStmt)
|
2020-05-15 10:41:12 +02:00
|
|
|
rows, err := stmt.QueryContext(ctx, roomID, r.Low(), r.High(), limit)
|
2020-02-13 18:27:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectEarlyEvents: rows.close() failed")
|
2020-02-13 18:27:33 +01:00
|
|
|
events, err := rowsToStreamEvents(rows)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// The events need to be returned from oldest to latest, which isn't
|
|
|
|
// necessarily the way the SQL query returns them, so a sort is necessary to
|
|
|
|
// ensure the events are in the right order in the slice.
|
|
|
|
sort.SliceStable(events, func(i int, j int) bool {
|
|
|
|
return events[i].StreamPosition < events[j].StreamPosition
|
|
|
|
})
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectEvents returns the events for the given event IDs. If an event is
|
|
|
|
// missing from the database, it will be omitted.
|
2020-05-14 10:53:55 +02:00
|
|
|
func (s *outputRoomEventsStatements) SelectEvents(
|
2020-02-13 18:27:33 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
|
|
) ([]types.StreamEvent, error) {
|
|
|
|
var returnEvents []types.StreamEvent
|
2020-06-12 15:55:57 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectEventsStmt)
|
2020-02-13 18:27:33 +01:00
|
|
|
for _, eventID := range eventIDs {
|
|
|
|
rows, err := stmt.QueryContext(ctx, eventID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if streamEvents, err := rowsToStreamEvents(rows); err == nil {
|
|
|
|
returnEvents = append(returnEvents, streamEvents...)
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
internal.CloseAndLogIfError(ctx, rows, "selectEvents: rows.close() failed")
|
2020-02-13 18:27:33 +01:00
|
|
|
}
|
|
|
|
return returnEvents, nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:17:46 +02:00
|
|
|
func (s *outputRoomEventsStatements) DeleteEventsForRoom(
|
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
|
|
) (err error) {
|
|
|
|
_, err = sqlutil.TxStmt(txn, s.deleteEventsForRoomStmt).ExecContext(ctx, roomID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
func rowsToStreamEvents(rows *sql.Rows) ([]types.StreamEvent, error) {
|
|
|
|
var result []types.StreamEvent
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
streamPos types.StreamPosition
|
|
|
|
eventBytes []byte
|
|
|
|
excludeFromSync bool
|
|
|
|
sessionID *int64
|
|
|
|
txnID *string
|
|
|
|
transactionID *api.TransactionID
|
|
|
|
)
|
|
|
|
if err := rows.Scan(&streamPos, &eventBytes, &sessionID, &excludeFromSync, &txnID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: Handle redacted events
|
2020-03-19 13:07:01 +01:00
|
|
|
var ev gomatrixserverlib.HeaderedEvent
|
|
|
|
if err := json.Unmarshal(eventBytes, &ev); err != nil {
|
2020-02-13 18:27:33 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessionID != nil && txnID != nil {
|
|
|
|
transactionID = &api.TransactionID{
|
|
|
|
SessionID: *sessionID,
|
|
|
|
TransactionID: *txnID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, types.StreamEvent{
|
2020-11-16 16:44:53 +01:00
|
|
|
HeaderedEvent: &ev,
|
2020-02-13 18:27:33 +01:00
|
|
|
StreamPosition: streamPos,
|
|
|
|
TransactionID: transactionID,
|
|
|
|
ExcludeFromSync: excludeFromSync,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
Add peer-to-peer support into Dendrite via libp2p and fetch (#880)
* Use a fork of pq which supports userCurrent on wasm
* Use sqlite3_js driver when running in JS
* Add cmd/dendritejs to pull in sqlite3_js driver for wasm only
* Update to latest go-sqlite-js version
* Replace prometheus with a stub. sigh
* Hard-code a config and don't use opentracing
* Latest go-sqlite3-js version
* Generate a key for now
* Listen for fetch traffic rather than HTTP
* Latest hacks for js
* libp2p support
* More libp2p
* Fork gjson to allow us to enforce auth checks as before
Previously, all events would come down redacted because the hash
checks would fail. They would fail because sjson.DeleteBytes didn't
remove keys not used for hashing. This didn't work because of a build
tag which included a file which no-oped the index returned.
See https://github.com/tidwall/gjson/issues/157
When it's resolved, let's go back to mainline.
* Use gjson@1.6.0 as it fixes https://github.com/tidwall/gjson/issues/157
* Use latest gomatrixserverlib for sig checks
* Fix a bug which could cause exclude_from_sync to not be set
Caused when sending events over federation.
* Use query variadic to make lookups actually work!
* Latest gomatrixserverlib
* Add notes on getting p2p up and running
Partly so I don't forget myself!
* refactor: Move p2p specific stuff to cmd/dendritejs
This is important or else the normal build of dendrite will fail
because the p2p libraries depend on syscall/js which doesn't work
on normal builds.
Also, clean up main.go to read a bit better.
* Update ho-http-js-libp2p to return errors from RoundTrip
* Add an LRU cache around the key DB
We actually need this for P2P because otherwise we can *segfault*
with things like: "runtime: unexpected return pc for runtime.handleEvent"
where the event is a `syscall/js` event, caused by spamming sql.js
caused by "Checking event signatures for 14 events of room state" which
hammers the key DB repeatedly in quick succession.
Using a cache fixes this, though the underlying cause is probably a bug
in the version of Go I'm on (1.13.7)
* breaking: Add Tracing.Enabled to toggle whether we do opentracing
Defaults to false, which is why this is a breaking change. We need
this flag because WASM builds cannot do opentracing.
* Start adding conditional builds for wasm to handle lib/pq
The general idea here is to have the wasm build have a `NewXXXDatabase`
that doesn't import any postgres package and hence we never import
`lib/pq`, which doesn't work under WASM (undefined `userCurrent`).
* Remove lib/pq for wasm for syncapi
* Add conditional building to remaining storage APIs
* Update build script to set env vars correctly for dendritejs
* sqlite bug fixes
* Docs
* Add a no-op main for dendritejs when not building under wasm
* Use the real prometheus, even for WASM
Instead, the dendrite-sw.js must mock out `process.pid` and
`fs.stat` - which must invoke the callback with an error (e.g `EINVAL`)
in order for it to work:
```
global.process = {
pid: 1,
};
global.fs.stat = function(path, cb) {
cb({
code: "EINVAL",
});
}
```
* Linting
2020-03-06 11:23:55 +01:00
|
|
|
|
|
|
|
func unmarshalStateIDs(addIDsJSON, delIDsJSON string) (addIDs []string, delIDs []string, err error) {
|
|
|
|
if len(addIDsJSON) > 0 {
|
|
|
|
if err = json.Unmarshal([]byte(addIDsJSON), &addIDs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(delIDsJSON) > 0 {
|
|
|
|
if err = json.Unmarshal([]byte(delIDsJSON), &delIDs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|