mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-23 14:53:10 -06:00
RoomInfo caching
This commit is contained in:
parent
a06c18bb56
commit
c2f4a78587
32
internal/caching/cache_roominfo.go
Normal file
32
internal/caching/cache_roominfo.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -24,7 +24,7 @@ const (
|
||||||
|
|
||||||
type RoomServerCaches interface {
|
type RoomServerCaches interface {
|
||||||
RoomServerNIDsCache
|
RoomServerNIDsCache
|
||||||
RoomVersionCache
|
RoomInfoCache
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoomServerNIDsCache contains the subset of functions needed for
|
// RoomServerNIDsCache contains the subset of functions needed for
|
||||||
|
|
@ -35,12 +35,6 @@ type RoomServerNIDsCache interface {
|
||||||
|
|
||||||
GetRoomServerEventTypeNID(eventType string) (types.EventTypeNID, bool)
|
GetRoomServerEventTypeNID(eventType string) (types.EventTypeNID, bool)
|
||||||
StoreRoomServerEventTypeNID(eventType string, nid types.EventTypeNID)
|
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) {
|
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) {
|
func (c Caches) StoreRoomServerEventTypeNID(eventType string, nid types.EventTypeNID) {
|
||||||
c.RoomServerEventTypeNIDs.Set(eventType, nid)
|
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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
@ -4,12 +4,10 @@ package caching
|
||||||
// different implementations as long as they satisfy the Cache
|
// different implementations as long as they satisfy the Cache
|
||||||
// interface.
|
// interface.
|
||||||
type Caches struct {
|
type Caches struct {
|
||||||
RoomVersions Cache // RoomVersionCache
|
|
||||||
ServerKeys Cache // ServerKeyCache
|
ServerKeys Cache // ServerKeyCache
|
||||||
RoomServerStateKeyNIDs Cache // RoomServerNIDsCache
|
RoomServerStateKeyNIDs Cache // RoomServerNIDsCache
|
||||||
RoomServerEventTypeNIDs Cache // RoomServerNIDsCache
|
RoomServerEventTypeNIDs Cache // RoomServerNIDsCache
|
||||||
RoomServerRoomNIDs Cache // RoomServerNIDsCache
|
RoomServerRoomInfo Cache // RoomServerInfoCache
|
||||||
RoomServerRoomIDs Cache // RoomServerNIDsCache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache is the interface that an implementation must satisfy.
|
// Cache is the interface that an implementation must satisfy.
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
||||||
roomVersions, err := NewInMemoryLRUCachePartition(
|
roomInfos, err := NewInMemoryLRUCachePartition(
|
||||||
RoomVersionCacheName,
|
RoomInfoCacheName,
|
||||||
RoomVersionCacheMutable,
|
RoomInfoCacheMutable,
|
||||||
RoomVersionCacheMaxEntries,
|
RoomInfoCacheMaxEntries,
|
||||||
enablePrometheus,
|
enablePrometheus,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -45,31 +45,11 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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{
|
return &Caches{
|
||||||
RoomVersions: roomVersions,
|
RoomServerRoomInfo: roomInfos,
|
||||||
ServerKeys: serverKeys,
|
ServerKeys: serverKeys,
|
||||||
RoomServerStateKeyNIDs: roomServerStateKeyNIDs,
|
RoomServerStateKeyNIDs: roomServerStateKeyNIDs,
|
||||||
RoomServerEventTypeNIDs: roomServerEventTypeNIDs,
|
RoomServerEventTypeNIDs: roomServerEventTypeNIDs,
|
||||||
RoomServerRoomNIDs: roomServerRoomNIDs,
|
|
||||||
RoomServerRoomIDs: roomServerRoomIDs,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -473,11 +473,6 @@ func (r *Queryer) QueryRoomVersionForRoom(
|
||||||
request *api.QueryRoomVersionForRoomRequest,
|
request *api.QueryRoomVersionForRoomRequest,
|
||||||
response *api.QueryRoomVersionForRoomResponse,
|
response *api.QueryRoomVersionForRoomResponse,
|
||||||
) error {
|
) error {
|
||||||
if roomVersion, ok := r.Cache.GetRoomVersion(request.RoomID); ok {
|
|
||||||
response.RoomVersion = roomVersion
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -486,7 +481,6 @@ func (r *Queryer) QueryRoomVersionForRoom(
|
||||||
return fmt.Errorf("QueryRoomVersionForRoom: missing room info for room %s", request.RoomID)
|
return fmt.Errorf("QueryRoomVersionForRoom: missing room info for room %s", request.RoomID)
|
||||||
}
|
}
|
||||||
response.RoomVersion = info.RoomVersion
|
response.RoomVersion = info.RoomVersion
|
||||||
r.Cache.StoreRoomVersion(request.RoomID, response.RoomVersion)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ const (
|
||||||
type httpRoomserverInternalAPI struct {
|
type httpRoomserverInternalAPI struct {
|
||||||
roomserverURL string
|
roomserverURL string
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
cache caching.RoomVersionCache
|
cache caching.RoomInfoCache
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRoomserverClient creates a RoomserverInputAPI implemented by talking to a HTTP POST API.
|
// NewRoomserverClient creates a RoomserverInputAPI implemented by talking to a HTTP POST API.
|
||||||
|
|
@ -63,7 +63,7 @@ type httpRoomserverInternalAPI struct {
|
||||||
func NewRoomserverClient(
|
func NewRoomserverClient(
|
||||||
roomserverURL string,
|
roomserverURL string,
|
||||||
httpClient *http.Client,
|
httpClient *http.Client,
|
||||||
cache caching.RoomVersionCache,
|
cache caching.RoomInfoCache,
|
||||||
) (api.RoomserverInternalAPI, error) {
|
) (api.RoomserverInternalAPI, error) {
|
||||||
if httpClient == nil {
|
if httpClient == nil {
|
||||||
return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is <nil>")
|
return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is <nil>")
|
||||||
|
|
@ -383,8 +383,8 @@ func (h *httpRoomserverInternalAPI) QueryRoomVersionForRoom(
|
||||||
request *api.QueryRoomVersionForRoomRequest,
|
request *api.QueryRoomVersionForRoomRequest,
|
||||||
response *api.QueryRoomVersionForRoomResponse,
|
response *api.QueryRoomVersionForRoomResponse,
|
||||||
) error {
|
) error {
|
||||||
if roomVersion, ok := h.cache.GetRoomVersion(request.RoomID); ok {
|
if roomInfo, ok := h.cache.GetRoomInfo(request.RoomID); ok {
|
||||||
response.RoomVersion = roomVersion
|
response.RoomVersion = roomInfo.RoomVersion
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -392,11 +392,7 @@ func (h *httpRoomserverInternalAPI) QueryRoomVersionForRoom(
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath
|
apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomPath
|
||||||
err := httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
return httputil.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
if err == nil {
|
|
||||||
h.cache.StoreRoomVersion(request.RoomID, response.RoomVersion)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *httpRoomserverInternalAPI) QueryCurrentState(
|
func (h *httpRoomserverInternalAPI) QueryCurrentState(
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,14 @@ func (d *Database) StateEntriesForTuples(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) {
|
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(
|
func (d *Database) AddState(
|
||||||
|
|
@ -495,8 +502,8 @@ func (d *Database) assignRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx,
|
ctx context.Context, txn *sql.Tx,
|
||||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||||
) (types.RoomNID, error) {
|
) (types.RoomNID, error) {
|
||||||
if roomNID, ok := d.Cache.GetRoomServerRoomNID(roomID); ok {
|
if roomInfo, ok := d.Cache.GetRoomInfo(roomID); ok && roomInfo.RoomNID != 0 {
|
||||||
return roomNID, nil
|
return roomInfo.RoomNID, nil
|
||||||
}
|
}
|
||||||
// Check if we already have a numeric ID in the database.
|
// Check if we already have a numeric ID in the database.
|
||||||
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||||
|
|
@ -508,9 +515,6 @@ func (d *Database) assignRoomNID(
|
||||||
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == nil {
|
|
||||||
d.Cache.StoreRoomServerRoomNID(roomID, roomNID)
|
|
||||||
}
|
|
||||||
return roomNID, err
|
return roomNID, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue