repo_cache.go

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