repo_cache.go

  1package cache
  2
  3import (
  4	"fmt"
  5	"io"
  6	"io/ioutil"
  7	"os"
  8	"strconv"
  9	"sync"
 10
 11	"github.com/MichaelMure/git-bug/entity"
 12	"github.com/MichaelMure/git-bug/repository"
 13	"github.com/MichaelMure/git-bug/util/multierr"
 14	"github.com/MichaelMure/git-bug/util/process"
 15)
 16
 17// 1: original format
 18// 2: added cache for identities with a reference in the bug cache
 19// 3: no more legacy identity
 20// 4: entities make their IDs from data, not git commit
 21const formatVersion = 4
 22
 23// The maximum number of bugs loaded in memory. After that, eviction will be done.
 24const defaultMaxLoadedBugs = 1000
 25
 26var _ repository.RepoCommon = &RepoCache{}
 27var _ repository.RepoConfig = &RepoCache{}
 28var _ repository.RepoKeyring = &RepoCache{}
 29
 30// cacheMgmt is the expected interface for a sub-cache.
 31type cacheMgmt interface {
 32	Typename() string
 33	Load() error
 34	Build() error
 35	SetCacheSize(size int)
 36	MergeAll(remote string) <-chan entity.MergeResult
 37	GetNamespace() string
 38	Close() error
 39}
 40
 41// RepoCache is a cache for a Repository. This cache has multiple functions:
 42//
 43// 1. After being loaded, a Bug is kept in memory in the cache, allowing for fast
 44// 		access later.
 45// 2. The cache maintain in memory and on disk a pre-digested excerpt for each bug,
 46// 		allowing for fast querying the whole set of bugs without having to load
 47//		them individually.
 48// 3. The cache guarantee that a single instance of a Bug is loaded at once, avoiding
 49// 		loss of data that we could have with multiple copies in the same process.
 50// 4. The same way, the cache maintain in memory a single copy of the loaded identities.
 51//
 52// The cache also protect the on-disk data by locking the git repository for its
 53// own usage, by writing a lock file. Of course, normal git operations are not
 54// affected, only git-bug related one.
 55type RepoCache struct {
 56	// the underlying repo
 57	repo repository.ClockedRepo
 58
 59	// the name of the repository, as defined in the MultiRepoCache
 60	name string
 61
 62	// resolvers for all known entities and excerpts
 63	resolvers entity.Resolvers
 64
 65	bugs       *RepoCacheBug
 66	identities *RepoCacheIdentity
 67
 68	subcaches []cacheMgmt
 69
 70	// the user identity's id, if known
 71	muUserIdentity sync.RWMutex
 72	userIdentityId entity.Id
 73}
 74
 75// NewRepoCache create or open an unnamed (aka default) cache on top of a raw repository.
 76// If the returned BuildEvent channel is not nil, the caller is expected to read all events before the cache is considered
 77// ready to use.
 78func NewRepoCache(r repository.ClockedRepo) (*RepoCache, chan BuildEvent, error) {
 79	return NewNamedRepoCache(r, "")
 80}
 81
 82// NewNamedRepoCache create or open a named cache on top of a raw repository.
 83// If the returned BuildEvent channel is not nil, the caller is expected to read all events before the cache is considered
 84// ready to use.
 85func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent, error) {
 86	c := &RepoCache{
 87		repo: r,
 88		name: name,
 89	}
 90
 91	c.identities = NewRepoCacheIdentity(r, c.getResolvers, c.GetUserIdentity)
 92	c.subcaches = append(c.subcaches, c.identities)
 93
 94	c.bugs = NewRepoCacheBug(r, c.getResolvers, c.GetUserIdentity)
 95	c.subcaches = append(c.subcaches, c.bugs)
 96
 97	c.resolvers = entity.Resolvers{
 98		&IdentityCache{}:   entity.ResolverFunc[*IdentityCache](c.identities.Resolve),
 99		&IdentityExcerpt{}: entity.ResolverFunc[*IdentityExcerpt](c.identities.ResolveExcerpt),
100		&BugCache{}:        entity.ResolverFunc[*BugCache](c.bugs.Resolve),
101		&BugExcerpt{}:      entity.ResolverFunc[*BugExcerpt](c.bugs.ResolveExcerpt),
102	}
103
104	err := c.lock()
105	if err != nil {
106		return &RepoCache{}, nil, err
107	}
108
109	err = c.load()
110	if err == nil {
111		return c, nil, nil
112	}
113
114	// Cache is either missing, broken or outdated. Rebuilding.
115	events := c.buildCache()
116
117	return c, events, nil
118}
119
120func NewRepoCacheNoEvents(r repository.ClockedRepo) (*RepoCache, error) {
121	cache, events, err := NewRepoCache(r)
122	if err != nil {
123		return nil, err
124	}
125	if events != nil {
126		for event := range events {
127			if event.Err != nil {
128				for range events {
129				}
130				return nil, err
131			}
132		}
133	}
134	return cache, nil
135}
136
137// Bugs gives access to the Bug entities
138func (c *RepoCache) Bugs() *RepoCacheBug {
139	return c.bugs
140}
141
142// Identities gives access to the Identity entities
143func (c *RepoCache) Identities() *RepoCacheIdentity {
144	return c.identities
145}
146
147func (c *RepoCache) getResolvers() entity.Resolvers {
148	return c.resolvers
149}
150
151// setCacheSize change the maximum number of loaded bugs
152func (c *RepoCache) setCacheSize(size int) {
153	for _, subcache := range c.subcaches {
154		subcache.SetCacheSize(size)
155	}
156}
157
158// load will try to read from the disk all the cache files
159func (c *RepoCache) load() error {
160	var errWait multierr.ErrWaitGroup
161	for _, mgmt := range c.subcaches {
162		errWait.Go(mgmt.Load)
163	}
164	return errWait.Wait()
165}
166
167func (c *RepoCache) lock() error {
168	err := repoIsAvailable(c.repo)
169	if err != nil {
170		return err
171	}
172
173	f, err := c.repo.LocalStorage().Create(lockfile)
174	if err != nil {
175		return err
176	}
177
178	pid := fmt.Sprintf("%d", os.Getpid())
179	_, err = f.Write([]byte(pid))
180	if err != nil {
181		return err
182	}
183
184	return f.Close()
185}
186
187func (c *RepoCache) Close() error {
188	var errWait multierr.ErrWaitGroup
189	for _, mgmt := range c.subcaches {
190		errWait.Go(mgmt.Close)
191	}
192	err := errWait.Wait()
193	if err != nil {
194		return err
195	}
196
197	err = c.repo.Close()
198	if err != nil {
199		return err
200	}
201
202	return c.repo.LocalStorage().Remove(lockfile)
203}
204
205type BuildEventType int
206
207const (
208	_ BuildEventType = iota
209	BuildEventStarted
210	BuildEventFinished
211)
212
213// BuildEvent carry an event happening during the cache build process.
214type BuildEvent struct {
215	// Err carry an error if the build process failed. If set, no other field matter.
216	Err error
217	// Typename is the name of the entity of which the event relate to.
218	Typename string
219	// Event is the type of the event.
220	Event BuildEventType
221}
222
223func (c *RepoCache) buildCache() chan BuildEvent {
224	out := make(chan BuildEvent)
225
226	go func() {
227		defer close(out)
228
229		var wg sync.WaitGroup
230		for _, subcache := range c.subcaches {
231			wg.Add(1)
232			go func(subcache cacheMgmt) {
233				defer wg.Done()
234				out <- BuildEvent{
235					Typename: subcache.Typename(),
236					Event:    BuildEventStarted,
237				}
238
239				err := subcache.Build()
240				if err != nil {
241					out <- BuildEvent{
242						Typename: subcache.Typename(),
243						Err:      err,
244					}
245					return
246				}
247
248				out <- BuildEvent{
249					Typename: subcache.Typename(),
250					Event:    BuildEventFinished,
251				}
252			}(subcache)
253		}
254		wg.Wait()
255	}()
256
257	return out
258}
259
260// repoIsAvailable check is the given repository is locked by a Cache.
261// Note: this is a smart function that will clean the lock file if the
262// corresponding process is not there anymore.
263// If no error is returned, the repo is free to edit.
264func repoIsAvailable(repo repository.RepoStorage) error {
265	// Todo: this leave way for a racey access to the repo between the test
266	// if the file exist and the actual write. It's probably not a problem in
267	// practice because using a repository will be done from user interaction
268	// or in a context where a single instance of git-bug is already guaranteed
269	// (say, a server with the web UI running). But still, that might be nice to
270	// have a mutex or something to guard that.
271
272	// Todo: this will fail if somehow the filesystem is shared with another
273	// computer. Should add a configuration that prevent the cleaning of the
274	// lock file
275
276	f, err := repo.LocalStorage().Open(lockfile)
277	if err != nil && !os.IsNotExist(err) {
278		return err
279	}
280
281	if err == nil {
282		// lock file already exist
283		buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
284		if err != nil {
285			return err
286		}
287		if len(buf) == 10 {
288			return fmt.Errorf("the lock file should be < 10 bytes")
289		}
290
291		pid, err := strconv.Atoi(string(buf))
292		if err != nil {
293			return err
294		}
295
296		if process.IsRunning(pid) {
297			return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
298		}
299
300		// The lock file is just laying there after a crash, clean it
301
302		fmt.Println("A lock file is present but the corresponding process is not, removing it.")
303		err = f.Close()
304		if err != nil {
305			return err
306		}
307
308		err = repo.LocalStorage().Remove(lockfile)
309		if err != nil {
310			return err
311		}
312	}
313
314	return nil
315}