Also reduce hits on query API

This commit is contained in:
Neil Alexander 2020-04-22 10:22:28 +01:00
parent c3dd404778
commit fc4e82c246
6 changed files with 21 additions and 11 deletions

View file

@ -270,7 +270,7 @@ func testRoomserver(input []string, wantOutput []string, checkQueries func(api.R
cmd.Args = []string{"dendrite-room-server", "--config", filepath.Join(dir, test.ConfigFile)} cmd.Args = []string{"dendrite-room-server", "--config", filepath.Join(dir, test.ConfigFile)}
gotOutput, err := runAndReadFromTopic(cmd, cfg.RoomServerURL()+"/metrics", doInput, outputTopic, len(wantOutput), func() { gotOutput, err := runAndReadFromTopic(cmd, cfg.RoomServerURL()+"/metrics", doInput, outputTopic, len(wantOutput), func() {
queryAPI, _ := api.NewRoomserverQueryAPIHTTP("http://"+string(cfg.Listen.RoomServer), &http.Client{Timeout: timeoutHTTP}) queryAPI, _ := api.NewRoomserverQueryAPIHTTP("http://"+string(cfg.Listen.RoomServer), &http.Client{Timeout: timeoutHTTP}, nil)
checkQueries(queryAPI) checkQueries(queryAPI)
}) })
if err != nil { if err != nil {

View file

@ -23,6 +23,7 @@ import (
"golang.org/x/crypto/ed25519" "golang.org/x/crypto/ed25519"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/dendrite/common/keydb" "github.com/matrix-org/dendrite/common/keydb"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -56,6 +57,7 @@ type BaseDendrite struct {
APIMux *mux.Router APIMux *mux.Router
httpClient *http.Client httpClient *http.Client
Cfg *config.Dendrite Cfg *config.Dendrite
Cache caching.Cache
KafkaConsumer sarama.Consumer KafkaConsumer sarama.Consumer
KafkaProducer sarama.SyncProducer KafkaProducer sarama.SyncProducer
} }
@ -87,6 +89,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string) *BaseDendrite {
componentName: componentName, componentName: componentName,
tracerCloser: closer, tracerCloser: closer,
Cfg: cfg, Cfg: cfg,
Cache: caching.NewInMemoryLRUCache(), // TODO: make configurable
APIMux: mux.NewRouter().UseEncodedPath(), APIMux: mux.NewRouter().UseEncodedPath(),
httpClient: &http.Client{Timeout: HTTPClientTimeout}, httpClient: &http.Client{Timeout: HTTPClientTimeout},
KafkaConsumer: kafkaConsumer, KafkaConsumer: kafkaConsumer,
@ -116,7 +119,6 @@ func (b *BaseDendrite) CreateHTTPRoomserverAPIs() (
roomserverAPI.RoomserverInputAPI, roomserverAPI.RoomserverInputAPI,
roomserverAPI.RoomserverQueryAPI, roomserverAPI.RoomserverQueryAPI,
) { ) {
alias, err := roomserverAPI.NewRoomserverAliasAPIHTTP(b.Cfg.RoomServerURL(), b.httpClient) alias, err := roomserverAPI.NewRoomserverAliasAPIHTTP(b.Cfg.RoomServerURL(), b.httpClient)
if err != nil { if err != nil {
logrus.WithError(err).Panic("NewRoomserverAliasAPIHTTP failed") logrus.WithError(err).Panic("NewRoomserverAliasAPIHTTP failed")
@ -125,7 +127,7 @@ func (b *BaseDendrite) CreateHTTPRoomserverAPIs() (
if err != nil { if err != nil {
logrus.WithError(err).Panic("NewRoomserverInputAPIHTTP failed", b.httpClient) logrus.WithError(err).Panic("NewRoomserverInputAPIHTTP failed", b.httpClient)
} }
query, err := roomserverAPI.NewRoomserverQueryAPIHTTP(b.Cfg.RoomServerURL(), b.httpClient) query, err := roomserverAPI.NewRoomserverQueryAPIHTTP(b.Cfg.RoomServerURL(), b.httpClient, b.Cache)
if err != nil { if err != nil {
logrus.WithError(err).Panic("NewRoomserverQueryAPIHTTP failed", b.httpClient) logrus.WithError(err).Panic("NewRoomserverQueryAPIHTTP failed", b.httpClient)
} }

View file

@ -1,7 +1,6 @@
package caching package caching
import ( import (
"fmt"
"sync" "sync"
"github.com/golang/groupcache/lru" "github.com/golang/groupcache/lru"
@ -20,7 +19,6 @@ func NewInMemoryLRUCache() *InMemoryLRUCache {
} }
func (c *InMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.RoomVersion, bool) { func (c *InMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.RoomVersion, bool) {
fmt.Println("Cache hit for", roomID)
if c == nil { if c == nil {
return "", false return "", false
} }
@ -36,7 +34,6 @@ func (c *InMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.Room
} }
func (c *InMemoryLRUCache) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) { func (c *InMemoryLRUCache) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) {
fmt.Println("Cache store for", roomID)
if c == nil { if c == nil {
return return
} }

View file

@ -21,6 +21,7 @@ import (
"errors" "errors"
"net/http" "net/http"
"github.com/matrix-org/dendrite/common/caching"
commonHTTP "github.com/matrix-org/dendrite/common/http" commonHTTP "github.com/matrix-org/dendrite/common/http"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
opentracing "github.com/opentracing/opentracing-go" opentracing "github.com/opentracing/opentracing-go"
@ -411,16 +412,17 @@ const RoomserverQueryRoomVersionForRoomPath = "/api/roomserver/queryRoomVersionF
// NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API. // NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API.
// If httpClient is nil an error is returned // If httpClient is nil an error is returned
func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) (RoomserverQueryAPI, error) { func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client, cache caching.Cache) (RoomserverQueryAPI, error) {
if httpClient == nil { if httpClient == nil {
return nil, errors.New("NewRoomserverQueryAPIHTTP: httpClient is <nil>") return nil, errors.New("NewRoomserverQueryAPIHTTP: httpClient is <nil>")
} }
return &httpRoomserverQueryAPI{roomserverURL, httpClient}, nil return &httpRoomserverQueryAPI{roomserverURL, httpClient, cache}, nil
} }
type httpRoomserverQueryAPI struct { type httpRoomserverQueryAPI struct {
roomserverURL string roomserverURL string
httpClient *http.Client httpClient *http.Client
cache caching.Cache
} }
// QueryLatestEventsAndState implements RoomserverQueryAPI // QueryLatestEventsAndState implements RoomserverQueryAPI
@ -585,9 +587,18 @@ func (h *httpRoomserverQueryAPI) QueryRoomVersionForRoom(
request *QueryRoomVersionForRoomRequest, request *QueryRoomVersionForRoomRequest,
response *QueryRoomVersionForRoomResponse, response *QueryRoomVersionForRoomResponse,
) error { ) error {
if roomVersion, ok := h.cache.GetRoomVersion(request.RoomID); ok {
response.RoomVersion = roomVersion
return nil
}
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionForRoom") span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionForRoom")
defer span.Finish() defer span.Finish()
apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) err := commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
if err == nil {
h.cache.StoreRoomVersion(request.RoomID, response.RoomVersion)
}
return err
} }

View file

@ -902,6 +902,7 @@ func (r *RoomserverQueryAPI) QueryRoomVersionForRoom(
response.RoomVersion = roomVersion response.RoomVersion = roomVersion
return nil return nil
} }
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID) roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
if err != nil { if err != nil {
return err return err

View file

@ -21,7 +21,6 @@ import (
asQuery "github.com/matrix-org/dendrite/appservice/query" asQuery "github.com/matrix-org/dendrite/appservice/query"
"github.com/matrix-org/dendrite/common/basecomponent" "github.com/matrix-org/dendrite/common/basecomponent"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/dendrite/roomserver/alias" "github.com/matrix-org/dendrite/roomserver/alias"
"github.com/matrix-org/dendrite/roomserver/input" "github.com/matrix-org/dendrite/roomserver/input"
"github.com/matrix-org/dendrite/roomserver/query" "github.com/matrix-org/dendrite/roomserver/query"
@ -51,7 +50,7 @@ func SetupRoomServerComponent(
queryAPI := query.RoomserverQueryAPI{ queryAPI := query.RoomserverQueryAPI{
DB: roomserverDB, DB: roomserverDB,
Cache: caching.NewInMemoryLRUCache(), Cache: base.Cache,
} }
queryAPI.SetupHTTP(http.DefaultServeMux) queryAPI.SetupHTTP(http.DefaultServeMux)