From c2f4a785875c83da1594b3434eefdf1be47f9dce Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 21 Sep 2020 15:29:26 +0100 Subject: [PATCH] RoomInfo caching --- internal/caching/cache_roominfo.go | 32 ++++++++++++++++++++ internal/caching/cache_roomservernids.go | 37 +----------------------- internal/caching/cache_roomversions.go | 30 ------------------- internal/caching/caches.go | 4 +-- internal/caching/impl_inmemorylru.go | 30 ++++--------------- roomserver/internal/query/query.go | 6 ---- roomserver/inthttp/client.go | 14 ++++----- roomserver/storage/shared/storage.go | 16 ++++++---- 8 files changed, 54 insertions(+), 115 deletions(-) create mode 100644 internal/caching/cache_roominfo.go delete mode 100644 internal/caching/cache_roomversions.go diff --git a/internal/caching/cache_roominfo.go b/internal/caching/cache_roominfo.go new file mode 100644 index 000000000..42fa94199 --- /dev/null +++ b/internal/caching/cache_roominfo.go @@ -0,0 +1,32 @@ +package caching + +import ( + "github.com/matrix-org/dendrite/roomserver/types" +) + +const ( + RoomInfoCacheName = "room_info" + RoomInfoCacheMaxEntries = 1024 + RoomInfoCacheMutable = false +) + +// RoomVersionsCache contains the subset of functions needed for +// a room version cache. +type RoomInfoCache interface { + GetRoomInfo(roomID string) (roomVersion types.RoomInfo, ok bool) + StoreRoomInfo(roomID string, roomVersion types.RoomInfo) +} + +func (c Caches) GetRoomInfo(roomID string) (types.RoomInfo, bool) { + val, found := c.RoomServerRoomInfo.Get(roomID) + if found && val != nil { + if roomInfo, ok := val.(types.RoomInfo); ok { + return roomInfo, true + } + } + return types.RoomInfo{}, false +} + +func (c Caches) StoreRoomInfo(roomID string, roomInfo types.RoomInfo) { + c.RoomServerRoomInfo.Set(roomID, roomInfo) +} diff --git a/internal/caching/cache_roomservernids.go b/internal/caching/cache_roomservernids.go index 7cb312c95..bbe859615 100644 --- a/internal/caching/cache_roomservernids.go +++ b/internal/caching/cache_roomservernids.go @@ -24,7 +24,7 @@ const ( type RoomServerCaches interface { RoomServerNIDsCache - RoomVersionCache + RoomInfoCache } // RoomServerNIDsCache contains the subset of functions needed for @@ -35,12 +35,6 @@ type RoomServerNIDsCache interface { GetRoomServerEventTypeNID(eventType string) (types.EventTypeNID, bool) StoreRoomServerEventTypeNID(eventType string, nid types.EventTypeNID) - - GetRoomServerRoomNID(roomID string) (types.RoomNID, bool) - StoreRoomServerRoomNID(roomID string, nid types.RoomNID) - - GetRoomServerRoomID(roomNID types.RoomNID) (string, bool) - StoreRoomServerRoomID(roomNID types.RoomNID, roomID string) } func (c Caches) GetRoomServerStateKeyNID(stateKey string) (types.EventStateKeyNID, bool) { @@ -70,32 +64,3 @@ func (c Caches) GetRoomServerEventTypeNID(eventType string) (types.EventTypeNID, func (c Caches) StoreRoomServerEventTypeNID(eventType string, nid types.EventTypeNID) { c.RoomServerEventTypeNIDs.Set(eventType, nid) } - -func (c Caches) GetRoomServerRoomNID(roomID string) (types.RoomNID, bool) { - val, found := c.RoomServerRoomNIDs.Get(roomID) - if found && val != nil { - if roomNID, ok := val.(types.RoomNID); ok { - return roomNID, true - } - } - return 0, false -} - -func (c Caches) StoreRoomServerRoomNID(roomID string, roomNID types.RoomNID) { - c.RoomServerRoomNIDs.Set(roomID, roomNID) - c.RoomServerRoomIDs.Set(string(roomNID), roomID) -} - -func (c Caches) GetRoomServerRoomID(roomNID types.RoomNID) (string, bool) { - val, found := c.RoomServerRoomIDs.Get(string(roomNID)) - if found && val != nil { - if roomID, ok := val.(string); ok { - return roomID, true - } - } - return "", false -} - -func (c Caches) StoreRoomServerRoomID(roomNID types.RoomNID, roomID string) { - c.StoreRoomServerRoomNID(roomID, roomNID) -} diff --git a/internal/caching/cache_roomversions.go b/internal/caching/cache_roomversions.go deleted file mode 100644 index 0b46d3d4b..000000000 --- a/internal/caching/cache_roomversions.go +++ /dev/null @@ -1,30 +0,0 @@ -package caching - -import "github.com/matrix-org/gomatrixserverlib" - -const ( - RoomVersionCacheName = "room_versions" - RoomVersionCacheMaxEntries = 1024 - RoomVersionCacheMutable = false -) - -// RoomVersionsCache contains the subset of functions needed for -// a room version cache. -type RoomVersionCache interface { - GetRoomVersion(roomID string) (roomVersion gomatrixserverlib.RoomVersion, ok bool) - StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) -} - -func (c Caches) GetRoomVersion(roomID string) (gomatrixserverlib.RoomVersion, bool) { - val, found := c.RoomVersions.Get(roomID) - if found && val != nil { - if roomVersion, ok := val.(gomatrixserverlib.RoomVersion); ok { - return roomVersion, true - } - } - return "", false -} - -func (c Caches) StoreRoomVersion(roomID string, roomVersion gomatrixserverlib.RoomVersion) { - c.RoomVersions.Set(roomID, roomVersion) -} diff --git a/internal/caching/caches.go b/internal/caching/caches.go index 655cc037c..ccb69780c 100644 --- a/internal/caching/caches.go +++ b/internal/caching/caches.go @@ -4,12 +4,10 @@ package caching // different implementations as long as they satisfy the Cache // interface. type Caches struct { - RoomVersions Cache // RoomVersionCache ServerKeys Cache // ServerKeyCache RoomServerStateKeyNIDs Cache // RoomServerNIDsCache RoomServerEventTypeNIDs Cache // RoomServerNIDsCache - RoomServerRoomNIDs Cache // RoomServerNIDsCache - RoomServerRoomIDs Cache // RoomServerNIDsCache + RoomServerRoomInfo Cache // RoomServerInfoCache } // Cache is the interface that an implementation must satisfy. diff --git a/internal/caching/impl_inmemorylru.go b/internal/caching/impl_inmemorylru.go index e99c18d74..7b1ef6570 100644 --- a/internal/caching/impl_inmemorylru.go +++ b/internal/caching/impl_inmemorylru.go @@ -9,10 +9,10 @@ import ( ) func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) { - roomVersions, err := NewInMemoryLRUCachePartition( - RoomVersionCacheName, - RoomVersionCacheMutable, - RoomVersionCacheMaxEntries, + roomInfos, err := NewInMemoryLRUCachePartition( + RoomInfoCacheName, + RoomInfoCacheMutable, + RoomInfoCacheMaxEntries, enablePrometheus, ) if err != nil { @@ -45,31 +45,11 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) { if err != nil { return nil, err } - roomServerRoomNIDs, err := NewInMemoryLRUCachePartition( - RoomServerRoomNIDsCacheName, - RoomServerRoomNIDsCacheMutable, - RoomServerRoomNIDsCacheMaxEntries, - enablePrometheus, - ) - if err != nil { - return nil, err - } - roomServerRoomIDs, err := NewInMemoryLRUCachePartition( - RoomServerRoomIDsCacheName, - RoomServerRoomIDsCacheMutable, - RoomServerRoomIDsCacheMaxEntries, - enablePrometheus, - ) - if err != nil { - return nil, err - } return &Caches{ - RoomVersions: roomVersions, + RoomServerRoomInfo: roomInfos, ServerKeys: serverKeys, RoomServerStateKeyNIDs: roomServerStateKeyNIDs, RoomServerEventTypeNIDs: roomServerEventTypeNIDs, - RoomServerRoomNIDs: roomServerRoomNIDs, - RoomServerRoomIDs: roomServerRoomIDs, }, nil } diff --git a/roomserver/internal/query/query.go b/roomserver/internal/query/query.go index fb981447f..c7f29bfa5 100644 --- a/roomserver/internal/query/query.go +++ b/roomserver/internal/query/query.go @@ -473,11 +473,6 @@ func (r *Queryer) QueryRoomVersionForRoom( request *api.QueryRoomVersionForRoomRequest, response *api.QueryRoomVersionForRoomResponse, ) error { - if roomVersion, ok := r.Cache.GetRoomVersion(request.RoomID); ok { - response.RoomVersion = roomVersion - return nil - } - info, err := r.DB.RoomInfo(ctx, request.RoomID) if err != nil { return err @@ -486,7 +481,6 @@ func (r *Queryer) QueryRoomVersionForRoom( return fmt.Errorf("QueryRoomVersionForRoom: missing room info for room %s", request.RoomID) } response.RoomVersion = info.RoomVersion - r.Cache.StoreRoomVersion(request.RoomID, response.RoomVersion) return nil } diff --git a/roomserver/inthttp/client.go b/roomserver/inthttp/client.go index f2510c753..8de4c60de 100644 --- a/roomserver/inthttp/client.go +++ b/roomserver/inthttp/client.go @@ -55,7 +55,7 @@ const ( type httpRoomserverInternalAPI struct { roomserverURL string httpClient *http.Client - cache caching.RoomVersionCache + cache caching.RoomInfoCache } // NewRoomserverClient creates a RoomserverInputAPI implemented by talking to a HTTP POST API. @@ -63,7 +63,7 @@ type httpRoomserverInternalAPI struct { func NewRoomserverClient( roomserverURL string, httpClient *http.Client, - cache caching.RoomVersionCache, + cache caching.RoomInfoCache, ) (api.RoomserverInternalAPI, error) { if httpClient == nil { return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is ") @@ -383,8 +383,8 @@ func (h *httpRoomserverInternalAPI) QueryRoomVersionForRoom( request *api.QueryRoomVersionForRoomRequest, response *api.QueryRoomVersionForRoomResponse, ) error { - if roomVersion, ok := h.cache.GetRoomVersion(request.RoomID); ok { - response.RoomVersion = roomVersion + if roomInfo, ok := h.cache.GetRoomInfo(request.RoomID); ok { + response.RoomVersion = roomInfo.RoomVersion return nil } @@ -392,11 +392,7 @@ func (h *httpRoomserverInternalAPI) QueryRoomVersionForRoom( defer span.Finish() apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath - err := httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response) - if err == nil { - h.cache.StoreRoomVersion(request.RoomID, response.RoomVersion) - } - return err + return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response) } func (h *httpRoomserverInternalAPI) QueryCurrentState( diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index e710b99b7..ae6bf0108 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -123,7 +123,14 @@ func (d *Database) StateEntriesForTuples( } func (d *Database) RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) { - return d.RoomsTable.SelectRoomInfo(ctx, roomID) + if info, ok := d.Cache.GetRoomInfo(roomID); ok { + return &info, nil + } + info, err := d.RoomsTable.SelectRoomInfo(ctx, roomID) + if err == nil { + d.Cache.StoreRoomInfo(roomID, *info) + } + return info, err } func (d *Database) AddState( @@ -495,8 +502,8 @@ func (d *Database) assignRoomNID( ctx context.Context, txn *sql.Tx, roomID string, roomVersion gomatrixserverlib.RoomVersion, ) (types.RoomNID, error) { - if roomNID, ok := d.Cache.GetRoomServerRoomNID(roomID); ok { - return roomNID, nil + if roomInfo, ok := d.Cache.GetRoomInfo(roomID); ok && roomInfo.RoomNID != 0 { + return roomInfo.RoomNID, nil } // Check if we already have a numeric ID in the database. roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID) @@ -508,9 +515,6 @@ func (d *Database) assignRoomNID( roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID) } } - if err == nil { - d.Cache.StoreRoomServerRoomNID(roomID, roomNID) - } return roomNID, err }