// Lock tries to set a temporary lock in the database. // If a lock already exists with the given name/owner, then the lock is renewed // // Lock does not block, instead, it returns true and its expiration time // is the lock has been successfully acquired or false otherwise func Lock(name string, duration time.Duration, owner string) (bool, time.Time) { pruneLocks() until := time.Now().Add(duration) untilString := strconv.FormatInt(until.Unix(), 10) // Try to get the expiration time of a lock with the same name/owner currentExpiration, err := toValue(cayley.StartPath(store, name).Has("locked_by", owner).Out("locked_until")) if err == nil && currentExpiration != "" { // Renew our lock if currentExpiration == untilString { return true, until } t := cayley.NewTransaction() t.RemoveQuad(cayley.Quad(name, "locked_until", currentExpiration, "")) t.AddQuad(cayley.Quad(name, "locked_until", untilString, "")) // It is not necessary to verify if the lock is ours again in the transaction // because if someone took it, the lock's current expiration probably changed and the transaction will fail return store.ApplyTransaction(t) == nil, until } t := cayley.NewTransaction() t.AddQuad(cayley.Quad(name, "locked", "locked", "")) // Necessary to make the transaction fails if the lock already exists (and has not been pruned) t.AddQuad(cayley.Quad(name, "locked_until", untilString, "")) t.AddQuad(cayley.Quad(name, "locked_by", owner, "")) glog.SetStderrThreshold("FATAL") success := store.ApplyTransaction(t) == nil glog.SetStderrThreshold("ERROR") return success, until }
// Healthcheck simply adds and then remove a quad in Cayley to ensure it is working // It returns true when everything is ok func Healthcheck() health.Status { var err error if store != nil { t := cayley.NewTransaction() q := cayley.Triple("cayley", "is", "healthy") t.AddQuad(q) t.RemoveQuad(q) glog.SetStderrThreshold("FATAL") // TODO REMOVE ME err = store.ApplyTransaction(t) glog.SetStderrThreshold("ERROR") // TODO REMOVE ME } return health.Status{IsEssential: true, IsHealthy: err == nil, Details: nil} }
func benchmarkVulnerabilities(b *testing.B, sublayersCount, packagesCount, packagesPerBranchesCount int) { glog.SetVerbosity(0) glog.SetAlsoToStderr(false) glog.SetStderrThreshold("FATAL") reset() err := database.Open("sql", dbInfo) if err != nil { panic(err) } defer database.Close() defer reset() layerID := generateLayersData(sublayersCount, packagesCount, packagesPerBranchesCount) var v []*database.Vulnerability for n := 0; n < b.N; n++ { // store result to prevent the compiler eliminating the function call. v = getVulnerabilities(layerID) } // store result to prevent the compiler eliminating the Benchmark itself. vulnerabilities = v }