From fc4e82c24644122adff416815e00c4a8234e6919 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 22 Apr 2020 10:22:28 +0100 Subject: [PATCH] Also reduce hits on query API --- cmd/roomserver-integration-tests/main.go | 2 +- common/basecomponent/base.go | 6 ++++-- common/caching/inmemorylru.go | 3 --- roomserver/api/query.go | 17 ++++++++++++++--- roomserver/query/query.go | 1 + roomserver/roomserver.go | 3 +-- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cmd/roomserver-integration-tests/main.go b/cmd/roomserver-integration-tests/main.go index df5607bcb..ea4732a6b 100644 --- a/cmd/roomserver-integration-tests/main.go +++ b/cmd/roomserver-integration-tests/main.go @@ -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)} 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) }) if err != nil { diff --git a/common/basecomponent/base.go b/common/basecomponent/base.go index 78894289e..c3df6c50a 100644 --- a/common/basecomponent/base.go +++ b/common/basecomponent/base.go @@ -23,6 +23,7 @@ import ( "golang.org/x/crypto/ed25519" + "github.com/matrix-org/dendrite/common/caching" "github.com/matrix-org/dendrite/common/keydb" "github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/gomatrixserverlib" @@ -56,6 +57,7 @@ type BaseDendrite struct { APIMux *mux.Router httpClient *http.Client Cfg *config.Dendrite + Cache caching.Cache KafkaConsumer sarama.Consumer KafkaProducer sarama.SyncProducer } @@ -87,6 +89,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string) *BaseDendrite { componentName: componentName, tracerCloser: closer, Cfg: cfg, + Cache: caching.NewInMemoryLRUCache(), // TODO: make configurable APIMux: mux.NewRouter().UseEncodedPath(), httpClient: &http.Client{Timeout: HTTPClientTimeout}, KafkaConsumer: kafkaConsumer, @@ -116,7 +119,6 @@ func (b *BaseDendrite) CreateHTTPRoomserverAPIs() ( roomserverAPI.RoomserverInputAPI, roomserverAPI.RoomserverQueryAPI, ) { - alias, err := roomserverAPI.NewRoomserverAliasAPIHTTP(b.Cfg.RoomServerURL(), b.httpClient) if err != nil { logrus.WithError(err).Panic("NewRoomserverAliasAPIHTTP failed") @@ -125,7 +127,7 @@ func (b *BaseDendrite) CreateHTTPRoomserverAPIs() ( if err != nil { 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 { logrus.WithError(err).Panic("NewRoomserverQueryAPIHTTP failed", b.httpClient) } diff --git a/common/caching/inmemorylru.go b/common/caching/inmemorylru.go index 5e5b03f83..774b70b2c 100644 --- a/common/caching/inmemorylru.go +++ b/common/caching/inmemorylru.go @@ -1,7 +1,6 @@ package caching import ( - "fmt" "sync" "github.com/golang/groupcache/lru" @@ -20,7 +19,6 @@ func NewInMemoryLRUCache() *InMemoryLRUCache { } func (c *InMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.RoomVersion, bool) { - fmt.Println("Cache hit for", roomID) if c == nil { return "", false } @@ -36,7 +34,6 @@ func (c *InMemoryLRUCache) GetRoomVersion(roomID string) (gomatrixserverlib.Room } func (c *InMemoryLRUCache) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) { - fmt.Println("Cache store for", roomID) if c == nil { return } diff --git a/roomserver/api/query.go b/roomserver/api/query.go index 5f024d266..b78bc4a67 100644 --- a/roomserver/api/query.go +++ b/roomserver/api/query.go @@ -21,6 +21,7 @@ import ( "errors" "net/http" + "github.com/matrix-org/dendrite/common/caching" commonHTTP "github.com/matrix-org/dendrite/common/http" "github.com/matrix-org/gomatrixserverlib" 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. // 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 { return nil, errors.New("NewRoomserverQueryAPIHTTP: httpClient is ") } - return &httpRoomserverQueryAPI{roomserverURL, httpClient}, nil + return &httpRoomserverQueryAPI{roomserverURL, httpClient, cache}, nil } type httpRoomserverQueryAPI struct { roomserverURL string httpClient *http.Client + cache caching.Cache } // QueryLatestEventsAndState implements RoomserverQueryAPI @@ -585,9 +587,18 @@ func (h *httpRoomserverQueryAPI) QueryRoomVersionForRoom( request *QueryRoomVersionForRoomRequest, response *QueryRoomVersionForRoomResponse, ) error { + if roomVersion, ok := h.cache.GetRoomVersion(request.RoomID); ok { + response.RoomVersion = roomVersion + return nil + } + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionForRoom") defer span.Finish() 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 } diff --git a/roomserver/query/query.go b/roomserver/query/query.go index db89766c0..73fc37e98 100644 --- a/roomserver/query/query.go +++ b/roomserver/query/query.go @@ -902,6 +902,7 @@ func (r *RoomserverQueryAPI) QueryRoomVersionForRoom( response.RoomVersion = roomVersion return nil } + roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID) if err != nil { return err diff --git a/roomserver/roomserver.go b/roomserver/roomserver.go index 3af40777e..f5b658968 100644 --- a/roomserver/roomserver.go +++ b/roomserver/roomserver.go @@ -21,7 +21,6 @@ import ( asQuery "github.com/matrix-org/dendrite/appservice/query" "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/input" "github.com/matrix-org/dendrite/roomserver/query" @@ -51,7 +50,7 @@ func SetupRoomServerComponent( queryAPI := query.RoomserverQueryAPI{ DB: roomserverDB, - Cache: caching.NewInMemoryLRUCache(), + Cache: base.Cache, } queryAPI.SetupHTTP(http.DefaultServeMux)