remove struct to ensure 64bit alignment for atomics (#2578)

Change-Id: Id2a4b740b2486a844673f69ce2e54c8c1e3187e2
This commit is contained in:
Jeff Wendling 2019-07-16 14:53:58 -06:00 committed by GitHub
parent 0b827f1375
commit 89afe3ee37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 38 deletions

View File

@ -18,16 +18,14 @@ import (
)
type bandwidthdb struct {
*InfoDB
bandwidth bandwidthUsed
loop *sync2.Cycle
}
type bandwidthUsed struct {
// Moved to top of struct to resolve alignment issue with atomic operations on ARM
used int64
mu sync.RWMutex
usedSpace int64
usedMu sync.RWMutex
usedSince time.Time
*InfoDB
loop *sync2.Cycle
}
// Bandwidth returns table for storing bandwidth usage.
@ -56,19 +54,19 @@ func (db *bandwidthdb) Add(ctx context.Context, satelliteID storj.NodeID, action
bandwidth_usage(satellite_id, action, amount, created_at)
VALUES(?, ?, ?, ?)`, satelliteID, action, amount, created.UTC())
if err == nil {
db.bandwidth.mu.Lock()
defer db.bandwidth.mu.Unlock()
db.usedMu.Lock()
defer db.usedMu.Unlock()
beginningOfMonth := getBeginningOfMonth(created.UTC())
if beginningOfMonth.Equal(db.bandwidth.usedSince) {
db.bandwidth.used += amount
} else if beginningOfMonth.After(db.bandwidth.usedSince) {
if beginningOfMonth.Equal(db.usedSince) {
db.usedSpace += amount
} else if beginningOfMonth.After(db.usedSince) {
usage, err := db.Summary(ctx, beginningOfMonth, time.Now().UTC())
if err != nil {
return err
}
db.bandwidth.usedSince = beginningOfMonth
db.bandwidth.used = usage.Total()
db.usedSince = beginningOfMonth
db.usedSpace = usage.Total()
}
}
return ErrInfo.Wrap(err)
@ -77,13 +75,13 @@ func (db *bandwidthdb) Add(ctx context.Context, satelliteID storj.NodeID, action
// MonthSummary returns summary of the current months bandwidth usages
func (db *bandwidthdb) MonthSummary(ctx context.Context) (_ int64, err error) {
defer mon.Task()(&ctx)(&err)
db.bandwidth.mu.RLock()
db.usedMu.RLock()
beginningOfMonth := getBeginningOfMonth(time.Now().UTC())
if beginningOfMonth.Equal(db.bandwidth.usedSince) {
defer db.bandwidth.mu.RUnlock()
return db.bandwidth.used, nil
if beginningOfMonth.Equal(db.usedSince) {
defer db.usedMu.RUnlock()
return db.usedSpace, nil
}
db.bandwidth.mu.RUnlock()
db.usedMu.RUnlock()
usage, err := db.Summary(ctx, beginningOfMonth, time.Now())
if err != nil {
@ -199,7 +197,7 @@ func (db *bandwidthdb) Rollup(ctx context.Context) (err error) {
SELECT datetime(strftime('%Y-%m-%dT%H:00:00', created_at)) created_hr, satellite_id, action, SUM(amount)
FROM bandwidth_usage
WHERE datetime(created_at) < datetime(?)
GROUP BY created_hr, satellite_id, action;
GROUP BY created_hr, satellite_id, action;
DELETE FROM bandwidth_usage WHERE datetime(created_at) < datetime(?);
`, hour, hour)

View File

@ -9,7 +9,6 @@ import (
"math/rand"
"os"
"path/filepath"
"sync"
"time"
"github.com/zeebo/errs"
@ -46,8 +45,8 @@ func newInfo(path string) (*InfoDB, error) {
dbutil.Configure(db, mon)
infoDb := &InfoDB{db: utcDB{db}}
infoDb.pieceinfo = pieceinfo{InfoDB: infoDb, space: spaceUsed{used: 0, once: sync.Once{}}}
infoDb.bandwidthdb = bandwidthdb{InfoDB: infoDb, bandwidth: bandwidthUsed{used: 0, mu: sync.RWMutex{}, usedSince: time.Time{}}, loop: sync2.NewCycle(time.Hour)}
infoDb.pieceinfo = pieceinfo{InfoDB: infoDb}
infoDb.bandwidthdb = bandwidthdb{InfoDB: infoDb, loop: sync2.NewCycle(time.Hour)}
infoDb.location = path
return infoDb, nil
@ -73,8 +72,8 @@ func NewInfoInMemory() (*InfoDB, error) {
}))
infoDb := &InfoDB{db: utcDB{db}}
infoDb.pieceinfo = pieceinfo{InfoDB: infoDb, space: spaceUsed{used: 0, once: sync.Once{}}}
infoDb.bandwidthdb = bandwidthdb{InfoDB: infoDb, bandwidth: bandwidthUsed{used: 0, mu: sync.RWMutex{}, usedSince: time.Time{}}, loop: sync2.NewCycle(time.Hour)}
infoDb.pieceinfo = pieceinfo{InfoDB: infoDb}
infoDb.bandwidthdb = bandwidthdb{InfoDB: infoDb, loop: sync2.NewCycle(time.Hour)}
return infoDb, nil
}

View File

@ -19,14 +19,11 @@ import (
)
type pieceinfo struct {
*InfoDB
space spaceUsed
}
type spaceUsed struct {
// Moved to top of struct to resolve alignment issue with atomic operations on ARM
used int64
once sync.Once
usedSpace int64
loadSpaceOnce sync.Once
*InfoDB
}
// PieceInfo returns database for storing piece information
@ -64,7 +61,7 @@ func (db *pieceinfo) Add(ctx context.Context, info *pieces.Info) (err error) {
if err == nil {
db.loadSpaceUsed(ctx)
atomic.AddInt64(&db.space.used, info.PieceSize)
atomic.AddInt64(&db.usedSpace, info.PieceSize)
}
return ErrInfo.Wrap(err)
}
@ -152,7 +149,7 @@ func (db *pieceinfo) Delete(ctx context.Context, satelliteID storj.NodeID, piece
if pieceSize != 0 && err == nil {
db.loadSpaceUsed(ctx)
atomic.AddInt64(&db.space.used, -pieceSize)
atomic.AddInt64(&db.usedSpace, -pieceSize)
}
return ErrInfo.Wrap(err)
@ -205,14 +202,14 @@ func (db *pieceinfo) SpaceUsed(ctx context.Context) (_ int64, err error) {
defer mon.Task()(&ctx)(&err)
db.loadSpaceUsed(ctx)
return atomic.LoadInt64(&db.space.used), nil
return atomic.LoadInt64(&db.usedSpace), nil
}
func (db *pieceinfo) loadSpaceUsed(ctx context.Context) {
defer mon.Task()(&ctx)(nil)
db.space.once.Do(func() {
db.loadSpaceOnce.Do(func() {
usedSpace, _ := db.CalculatedSpaceUsed(ctx)
atomic.AddInt64(&db.space.used, usedSpace)
atomic.AddInt64(&db.usedSpace, usedSpace)
})
}