Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): in-memory cache support for nlslayer #962

Open
wants to merge 19 commits into
base: feat/in-memory-cache-system-and-uptrace-for-style
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules
/mongo
/data
/redis
/.env
/.env.*
/.idea
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: "3"
services:
reearth:
image: reearth/reearth:latest
Expand All @@ -20,3 +20,9 @@ services:
- 27017:27017
volumes:
- ./mongo:/data/db
reearth-redis:
image: redis:latest
ports:
- 6379:6379
volumes:
- ./redis:/data
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,11 @@ github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwc
github.com/apache/arrow/go/v12 v12.0.0 h1:xtZE63VWl7qLdB0JObIXvvhGjoVNrQ9ciIHG2OK5cmc=
github.com/apache/thrift v0.16.0 h1:qEy6UW60iVOlUy+b9ZR0d5WzUWYGOo4HfopoyBaNmoY=
github.com/aws/aws-sdk-go v1.35.5 h1:doSEOxC0UkirPcle20Rc+1kAhJ4Ip+GSEeZ3nKl7Qlk=
github.com/aws/smithy-go v1.20.1 h1:4SZlSlMr36UEqC7XOyRVb27XMeZubNcBNN+9IgEPIQw=
github.com/bradfitz/gomemcache v0.0.0-20170208213004-1952afaa557d h1:7IjN4QP3c38xhg6wz8R3YjoU+6S9e7xBc0DAVLLIpHE=
github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk=
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chrispappas/golang-generics-set v1.0.1 h1:91l8cInAWTxCPwZ8UNg7qkkPsdFdkYS9hytsd8UJsIU=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
Expand Down Expand Up @@ -258,6 +256,7 @@ github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpsp
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 h1:+n/aFZefKZp7spd8DFdX7uMikMLXX4oubIzJF4kv/wI=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI=
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
github.com/pierrre/gotestcover v0.0.0-20160517101806-924dca7d15f0 h1:i5VIxp6QB8oWZ8IkK8zrDgeT6ORGIUeiN+61iETwJbI=
Expand Down Expand Up @@ -354,6 +353,7 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
Expand Down
8 changes: 8 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ REEARTH_HOST_WEB=https://localhost:3000
REEARTH_ASSETBASEURL=https://localhost:8080/assets
REEARTH_DEV=false

# Redis
REDIS_HOST=localhost:6379
REDIS_PASSWORD=
REDIS_DB=0

# Uptrace
UPTRACE_DSN=https://[email protected]/project_id

# GCP
GOOGLE_CLOUD_PROJECT=
GCS_BUCKETNAME=
Expand Down
5 changes: 4 additions & 1 deletion server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ run-app:
run-db:
docker compose -f ../docker-compose.yml up -d reearth-mongo

run-redis:
docker compose -f ../docker-compose.yml up -d reearth-redis

gql:
go generate ./internal/adapter/gql

.PHONY: lint test e2e build run-app run-db gql
.PHONY: lint test e2e build run-app run-db run-redis gql
17 changes: 17 additions & 0 deletions server/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"testing"

"github.com/gavv/httpexpect/v2"
"github.com/go-redis/redis/v8"
"github.com/reearth/reearth/server/internal/app"
"github.com/reearth/reearth/server/internal/app/config"
"github.com/reearth/reearth/server/internal/infrastructure/fs"
"github.com/reearth/reearth/server/internal/infrastructure/memory"
"github.com/reearth/reearth/server/internal/infrastructure/mongo"
infraRedis "github.com/reearth/reearth/server/internal/infrastructure/redis"
"github.com/reearth/reearth/server/internal/usecase/gateway"
"github.com/reearth/reearth/server/internal/usecase/repo"
"github.com/reearth/reearthx/account/accountinfrastructure/accountmongo"
Expand Down Expand Up @@ -80,12 +82,27 @@ func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Containe
t.Fatalf("server failed to listen: %v", err)
}

var redisAdapter *infraRedis.RedisAdapter
if cfg.RedisHost != "" {
redisClient := redis.NewClient(&redis.Options{
Addr: cfg.RedisHost,
Password: "",
DB: 0,
})
_, err = redisClient.Ping(ctx).Result()
if err != nil {
t.Fatalf("Failed to connect to Redis: %+v\n", err)
}
redisAdapter = infraRedis.NewRedisAdapter(redisClient)
}

srv := app.NewServer(ctx, &app.ServerConfig{
Config: cfg,
Repos: repos,
Gateways: gateways,
Debug: true,
AccountRepos: repos.AccountRepos(),
RedisAdapter: redisAdapter,
})

ch := make(chan error)
Expand Down
22 changes: 21 additions & 1 deletion server/e2e/gql_featureCollection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"testing"

"github.com/alicebob/miniredis/v2"
"github.com/gavv/httpexpect/v2"
"github.com/reearth/reearth/server/internal/app/config"
)
Expand Down Expand Up @@ -196,12 +197,23 @@ func deleteGeoJSONFeature(
return requestBody, res, fId
}

func TestFeatureCollectionCRUD(t *testing.T) {
func featureCollectionCRUD(t *testing.T, isUseRedis bool) {
redisAddress := ""
if isUseRedis {
mr, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mr.Close()
redisAddress = mr.Addr()
}

e := StartServer(t, &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
RedisHost: redisAddress,
}, true, baseSeeder)

pId := createProject(e)
Expand Down Expand Up @@ -506,3 +518,11 @@ func TestFeatureCollectionCRUD(t *testing.T) {
Value("features").Array().
Length().Equal(0)
}

func TestFeatureCollectionCRUD(t *testing.T) {
featureCollectionCRUD(t, false)
}

func TestFeatureCollectionCRUDWithRedis(t *testing.T) {
featureCollectionCRUD(t, true)
}
35 changes: 34 additions & 1 deletion server/e2e/gql_nlslayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"testing"

"github.com/alicebob/miniredis/v2"
"github.com/gavv/httpexpect/v2"
"github.com/reearth/reearth/server/internal/app/config"
"github.com/samber/lo"
Expand Down Expand Up @@ -299,12 +300,23 @@ func fetchSceneForNewLayers(e *httpexpect.Expect, sID string) (GraphQLRequest, *
return fetchSceneRequestBody, res
}

func TestNLSLayerCRUD(t *testing.T) {
func nlsLayerCRUD(t *testing.T, isUseRedis bool) {
redisAddress := ""
if isUseRedis {
mr, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mr.Close()
redisAddress = mr.Addr()
}

e := StartServer(t, &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
RedisHost: redisAddress,
}, true, baseSeeder)

pId := createProject(e)
Expand Down Expand Up @@ -419,6 +431,14 @@ func TestNLSLayerCRUD(t *testing.T) {
Length().Equal(0)
}

func TestNLSLayerCRUD(t *testing.T) {
nlsLayerCRUD(t, false)
}

func TestNLSLayerCRUDWithRedis(t *testing.T) {
nlsLayerCRUD(t, true)
}

func createInfobox(e *httpexpect.Expect, layerId string) (GraphQLRequest, *httpexpect.Value, string) {
requestBody := GraphQLRequest{
OperationName: "CreateNLSInfobox",
Expand Down Expand Up @@ -653,12 +673,18 @@ func moveInfoboxBlock(e *httpexpect.Expect, layerId, infoboxBlockId string, inde
}

func TestInfoboxBlocksCRUD(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mr.Close()

e := StartServer(t, &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
RedisHost: mr.Addr(),
}, true, baseSeeder)

pId := createProject(e)
Expand Down Expand Up @@ -755,11 +781,18 @@ func addCustomProperties(
}

func TestCustomProperties(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mr.Close()

e := StartServer(t, &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
RedisHost: mr.Addr(),
}, true, baseSeeder)

pId := createProject(e)
Expand Down
22 changes: 21 additions & 1 deletion server/e2e/gql_style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"testing"

"github.com/alicebob/miniredis/v2"
"github.com/gavv/httpexpect/v2"
"github.com/reearth/reearth/server/internal/app/config"
)
Expand Down Expand Up @@ -174,12 +175,23 @@ func fetchSceneForStyles(e *httpexpect.Expect, sID string) (GraphQLRequest, *htt
return fetchSceneRequestBody, res
}

func TestStyleCRUD(t *testing.T) {
func styleCRUD(t *testing.T, isUseRedis bool) {
redisAddress := ""
if isUseRedis {
mr, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mr.Close()
redisAddress = mr.Addr()
}

e := StartServer(t, &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
RedisHost: redisAddress,
}, true, baseSeeder)

pId := createProject(e)
Expand Down Expand Up @@ -249,3 +261,11 @@ func TestStyleCRUD(t *testing.T) {
Value("styles").Array().
Length().Equal(0)
}

func TestStyleCRUD(t *testing.T) {
styleCRUD(t, false)
}

func TestStyleCRUDWithRedis(t *testing.T) {
styleCRUD(t, true)
}
8 changes: 8 additions & 0 deletions server/e2e/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import (
"net/http"
"testing"

"github.com/alicebob/miniredis/v2"
"github.com/reearth/reearth/server/internal/app/config"
)

func TestPingAPI(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mr.Close()

e := StartServer(t, &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
RedisHost: mr.Addr(),
}, false, nil)

e.OPTIONS("/api/ping").
Expand Down
Loading
Loading