Skip to content

Commit 1bb9a66

Browse files
authored
Merge pull request #464 from kuba--/cache-440
One configurable cache per `repository pool`
2 parents 71c5197 + 1d8e0c3 commit 1bb9a66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1065
-328
lines changed

Gopkg.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
[[constraint]]
1818
name = "gopkg.in/src-d/go-git.v4"
1919
source = "github.com/src-d/go-git"
20-
revision = "d3cec13ac0b195bfb897ed038a08b5130ab9969e"
20+
revision = "2fb32d2a8601213b6db109d3e9028c6b64af1874"
2121

2222
[[constraint]]
2323
name = "gopkg.in/src-d/go-git-fixtures.v3"

cmd/gitbase/command/server.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
gopilosa "github.com/pilosa/go-pilosa"
1717
"github.com/sirupsen/logrus"
1818
"github.com/uber/jaeger-client-go/config"
19+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1920
sqle "gopkg.in/src-d/go-mysql-server.v0"
2021
"gopkg.in/src-d/go-mysql-server.v0/server"
2122
"gopkg.in/src-d/go-mysql-server.v0/sql"
@@ -40,25 +41,25 @@ type Server struct {
4041
pool *gitbase.RepositoryPool
4142
name string
4243

43-
Version string // Version of the application.
44-
Directories []string `short:"d" long:"directories" description:"Path where the git repositories are located (standard and siva), multiple directories can be defined. Accepts globs."`
45-
Depth int `long:"depth" default:"1000" description:"load repositories looking at less than <depth> nested subdirectories."`
46-
Host string `long:"host" default:"localhost" description:"Host where the server is going to listen"`
47-
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
48-
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
49-
Password string `short:"P" long:"password" default:"" description:"Password used for connection"`
50-
PilosaURL string `long:"pilosa" default:"http://localhost:10101" description:"URL to your pilosa server" env:"PILOSA_ENDPOINT"`
51-
IndexDir string `short:"i" long:"index" default:"/var/lib/gitbase/index" description:"Directory where the gitbase indexes information will be persisted." env:"GITBASE_INDEX_DIR"`
52-
DisableSquash bool `long:"no-squash" description:"Disables the table squashing."`
53-
TraceEnabled bool `long:"trace" env:"GITBASE_TRACE" description:"Enables jaeger tracing"`
54-
ReadOnly bool `short:"r" long:"readonly" description:"Only allow read queries. This disables creating and deleting indexes as well." env:"GITBASE_READONLY"`
55-
Parallelism uint `long:"parallelism" description:"Maximum number of parallel threads per table. By default, it's the number of CPU cores. 0 means default, 1 means disabled."`
56-
57-
SkipGitErrors bool // SkipGitErrors disables failing when Git errors are found.
58-
DisableGit bool `long:"no-git" description:"disable the load of git standard repositories."`
59-
DisableSiva bool `long:"no-siva" description:"disable the load of siva files."`
60-
Verbose bool `short:"v" description:"Activates the verbose mode"`
61-
OldUast bool `long:"old-uast-serialization" description:"serialize uast in the old format" env:"GITBASE_UAST_SERIALIZATION"`
44+
Version string // Version of the application.
45+
Directories []string `short:"d" long:"directories" description:"Path where the git repositories are located (standard and siva), multiple directories can be defined. Accepts globs."`
46+
Depth int `long:"depth" default:"1000" description:"load repositories looking at less than <depth> nested subdirectories."`
47+
Host string `long:"host" default:"localhost" description:"Host where the server is going to listen"`
48+
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
49+
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
50+
Password string `short:"P" long:"password" default:"" description:"Password used for connection"`
51+
PilosaURL string `long:"pilosa" default:"http://localhost:10101" description:"URL to your pilosa server" env:"PILOSA_ENDPOINT"`
52+
IndexDir string `short:"i" long:"index" default:"/var/lib/gitbase/index" description:"Directory where the gitbase indexes information will be persisted." env:"GITBASE_INDEX_DIR"`
53+
CacheSize cache.FileSize `long:"cache" default:"512" description:"Object cache size in megabytes" env:"GITBASE_CACHESIZE_MB"`
54+
Parallelism uint `long:"parallelism" description:"Maximum number of parallel threads per table. By default, it's the number of CPU cores. 0 means default, 1 means disabled."`
55+
DisableSquash bool `long:"no-squash" description:"Disables the table squashing."`
56+
TraceEnabled bool `long:"trace" env:"GITBASE_TRACE" description:"Enables jaeger tracing"`
57+
ReadOnly bool `short:"r" long:"readonly" description:"Only allow read queries. This disables creating and deleting indexes as well." env:"GITBASE_READONLY"`
58+
SkipGitErrors bool // SkipGitErrors disables failing when Git errors are found.
59+
DisableGit bool `long:"no-git" description:"disable the load of git standard repositories."`
60+
DisableSiva bool `long:"no-siva" description:"disable the load of siva files."`
61+
Verbose bool `short:"v" description:"Activates the verbose mode"`
62+
OldUast bool `long:"old-uast-serialization" description:"serialize uast in the old format" env:"GITBASE_UAST_SERIALIZATION"`
6263
}
6364

6465
type jaegerLogrus struct {
@@ -184,7 +185,7 @@ func (c *Server) buildDatabase() error {
184185
)
185186
}
186187

187-
c.pool = gitbase.NewRepositoryPool()
188+
c.pool = gitbase.NewRepositoryPool(c.CacheSize * cache.MiByte)
188189

189190
if err := c.addDirectories(); err != nil {
190191
return err

common_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/stretchr/testify/require"
88
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
9+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
910
"gopkg.in/src-d/go-mysql-server.v0/sql"
1011
"gopkg.in/src-d/go-mysql-server.v0/sql/plan"
1112
)
@@ -36,7 +37,7 @@ func buildSession(t *testing.T, repos fixtures.Fixtures,
3637

3738
require.NoError(fixtures.Init())
3839

39-
pool := NewRepositoryPool()
40+
pool := NewRepositoryPool(cache.DefaultMaxSize)
4041
for _, fixture := range repos {
4142
path := fixture.Worktree().Root()
4243
ok, err := IsGitRepo(path)

fs_error_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
billy "gopkg.in/src-d/go-billy.v4"
1414
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
1515
git "gopkg.in/src-d/go-git.v4"
16+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1617
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1718
"gopkg.in/src-d/go-mysql-server.v0/sql"
1819
)
@@ -56,7 +57,7 @@ func setupErrorRepos(t *testing.T) (*sql.Context, CleanupFunc) {
5657
fixture := fixtures.ByTag("worktree").One()
5758
baseFS := fixture.Worktree()
5859

59-
pool := NewRepositoryPool()
60+
pool := NewRepositoryPool(cache.DefaultMaxSize)
6061

6162
fs, err := brokenFS(brokenPackfile, baseFS)
6263
require.NoError(err)
@@ -118,23 +119,21 @@ func testTable(t *testing.T, tableName string, number int) {
118119
}
119120

120121
type billyRepository struct {
121-
id string
122-
fs billy.Filesystem
122+
id string
123+
fs billy.Filesystem
124+
cache cache.Object
123125
}
124126

125127
func billyRepo(id string, fs billy.Filesystem) repository {
126-
return &billyRepository{id, fs}
128+
return &billyRepository{id, fs, cache.NewObjectLRUDefault()}
127129
}
128130

129131
func (r *billyRepository) ID() string {
130132
return r.id
131133
}
132134

133135
func (r *billyRepository) Repo() (*Repository, error) {
134-
storage, err := filesystem.NewStorage(r.fs)
135-
if err != nil {
136-
return nil, err
137-
}
136+
storage := filesystem.NewStorage(r.fs, r.cache)
138137

139138
repo, err := git.Open(storage, r.fs)
140139
if err != nil {
@@ -152,6 +151,10 @@ func (r *billyRepository) Path() string {
152151
return r.id
153152
}
154153

154+
func (r *billyRepository) Cache() cache.Object {
155+
return r.cache
156+
}
157+
155158
type brokenType uint64
156159

157160
const (

integration_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/src-d/gitbase/internal/function"
1717
"github.com/stretchr/testify/require"
1818
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
19+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1920
sqle "gopkg.in/src-d/go-mysql-server.v0"
2021
"gopkg.in/src-d/go-mysql-server.v0/sql"
2122
"gopkg.in/src-d/go-mysql-server.v0/sql/analyzer"
@@ -33,7 +34,7 @@ func TestIntegration(t *testing.T) {
3334

3435
path := fixtures.ByTag("worktree").One().Worktree().Root()
3536

36-
pool := gitbase.NewRepositoryPool()
37+
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize)
3738
require.NoError(t, pool.AddGitWithID("worktree", path))
3839

3940
testCases := []struct {
@@ -443,7 +444,7 @@ func TestMissingHeadRefs(t *testing.T) {
443444
"_testdata",
444445
)
445446

446-
pool := gitbase.NewRepositoryPool()
447+
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize)
447448
require.NoError(
448449
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
449450
if err != nil {
@@ -830,7 +831,7 @@ func setup(t testing.TB) (*sqle.Engine, *gitbase.RepositoryPool, func()) {
830831
require.NoError(t, fixtures.Clean())
831832
}
832833

833-
pool := gitbase.NewRepositoryPool()
834+
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize)
834835
for _, f := range fixtures.ByTag("worktree") {
835836
pool.AddGitWithID("worktree", f.Worktree().Root())
836837
}

internal/function/uast_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gopkg.in/bblfsh/sdk.v1/protocol"
1212
"gopkg.in/bblfsh/sdk.v1/uast"
1313
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
14+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1415
"gopkg.in/src-d/go-mysql-server.v0/sql"
1516
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
1617
)
@@ -421,7 +422,7 @@ func setup(t *testing.T) (*sql.Context, func()) {
421422
t.Helper()
422423
require.NoError(t, fixtures.Init())
423424

424-
pool := gitbase.NewRepositoryPool()
425+
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize)
425426
for _, f := range fixtures.ByTag("worktree") {
426427
pool.AddGit(f.Worktree().Root())
427428
}

packfiles.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ func getUnpackedObject(repo repository, hash plumbing.Hash) (o object.Object, er
166166

167167
defer ioutil.CheckClose(f, &err)
168168

169-
storage, err := filesystem.NewStorage(fs)
170-
if err != nil {
171-
return nil, err
172-
}
169+
storage := filesystem.NewStorage(fs, repo.Cache())
173170

174171
obj := storage.NewEncodedObject()
175172
r, err := objfile.NewReader(f)
@@ -224,11 +221,7 @@ func newRepoObjectDecoder(
224221
return nil, err
225222
}
226223

227-
storage, err := filesystem.NewStorage(fs)
228-
if err != nil {
229-
_ = packf.Close()
230-
return nil, err
231-
}
224+
storage := filesystem.NewStorage(fs, repo.Cache())
232225

233226
idx, err := openPackfileIndex(dot, hash)
234227
if err != nil {

packfiles_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66

77
"github.com/stretchr/testify/require"
88
"gopkg.in/src-d/go-git.v4/plumbing"
9+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
910
)
1011

1112
var testSivaFilePath = filepath.Join("_testdata", "fff7062de8474d10a67d417ccea87ba6f58ca81d.siva")
1213

1314
func TestRepositoryPackfiles(t *testing.T) {
1415
require := require.New(t)
1516

16-
fs, packfiles, err := repositoryPackfiles(sivaRepo("siva", testSivaFilePath))
17+
fs, packfiles, err := repositoryPackfiles(sivaRepo("siva", testSivaFilePath, cache.NewObjectLRUDefault()))
1718

1819
require.NoError(err)
1920
require.Equal([]plumbing.Hash{
@@ -24,7 +25,7 @@ func TestRepositoryPackfiles(t *testing.T) {
2425
}
2526

2627
func TestRepositoryIndex(t *testing.T) {
27-
idx, err := newRepositoryIndex(sivaRepo("siva", testSivaFilePath))
28+
idx, err := newRepositoryIndex(sivaRepo("siva", testSivaFilePath, cache.NewObjectLRUDefault()))
2829
require.NoError(t, err)
2930

3031
testCases := []struct {

repositories_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/require"
8+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
89
"gopkg.in/src-d/go-mysql-server.v0/sql"
910
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
1011
)
@@ -17,10 +18,10 @@ func TestRepositoriesTable(t *testing.T) {
1718
"seven", "eight", "nine",
1819
}
1920

20-
pool := NewRepositoryPool()
21+
pool := NewRepositoryPool(cache.DefaultMaxSize)
2122

2223
for _, id := range repoIDs {
23-
pool.Add(gitRepo(id, ""))
24+
pool.Add(gitRepo(id, "", pool.cache))
2425
}
2526

2627
session := NewSession(pool)

0 commit comments

Comments
 (0)