Limit to only 1 ttl database write when storing data on storage nodes (#348)

* Limit to only 1 database write

* Check file system rather than database

* Move check to storefile. We need to figure out how to fix this mess

* piecestore should not overwrite data, it should fail when trying to write to a file that already exists

* Format errors, delete unused function in psdb for checking if TTL exists

* Combine errors better
This commit is contained in:
Alexander Leitner 2018-09-13 10:30:45 -04:00 committed by GitHub
parent a7d34bea4b
commit 3399376eb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 21 deletions

View File

@ -56,7 +56,7 @@ func StoreWriter(id string, dir string) (io.WriteCloser, error) {
}
// Create File on file system
return os.OpenFile(dataPath, os.O_RDWR|os.O_CREATE, 0755)
return os.OpenFile(dataPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0755)
}
// RetrieveReader retrieves data from pstore directory

View File

@ -201,20 +201,12 @@ func (db *DB) GetBandwidthAllocationBySignature(signature []byte) ([][]byte, err
return agreements, nil
}
// AddTTLToDB adds TTL into database by id
func (db *DB) AddTTLToDB(id string, expiration int64) error {
// AddTTL adds TTL into database by id
func (db *DB) AddTTL(id string, expiration, size int64) error {
defer db.locked()()
created := time.Now().Unix()
_, err := db.DB.Exec("INSERT or REPLACE INTO ttl (id, created, expires) VALUES (?, ?, ?)", id, created, expiration)
return err
}
// UpdateTTLSize adds stored data size into database by id
func (db *DB) UpdateTTLSize(id string, size int64) error {
defer db.locked()()
_, err := db.DB.Exec("UPDATE ttl SET size=? WHERE id=?", size, id)
_, err := db.DB.Exec("INSERT OR REPLACE INTO ttl (id, created, expires, size) VALUES (?, ?, ?, ?)", id, created, expiration, size)
return err
}

View File

@ -67,7 +67,7 @@ func TestHappyPath(t *testing.T) {
t.Run("#"+strconv.Itoa(P), func(t *testing.T) {
t.Parallel()
for _, ttl := range tests {
err := db.AddTTLToDB(ttl.ID, ttl.Expiration)
err := db.AddTTL(ttl.ID, ttl.Expiration, 0)
if err != nil {
t.Fatal(err)
}

View File

@ -44,19 +44,14 @@ func (s *Server) Store(reqStream pb.PieceStoreRoutes_StoreServer) (err error) {
return StoreError.New("Piece ID not specified")
}
// If we put in the database first then that checks if the data already exists
if err = s.DB.AddTTLToDB(pd.GetId(), pd.GetExpirationUnixSec()); err != nil {
return StoreError.New("Failed to write expiration data to database")
}
total, err := s.storeData(ctx, reqStream, pd.GetId())
if err != nil {
return err
}
// If we put in the database first then that checks if the data already exists
if err = s.DB.UpdateTTLSize(pd.GetId(), total); err != nil {
return StoreError.New("Failed to write total data to ttl database")
if err = s.DB.AddTTL(pd.GetId(), pd.GetExpirationUnixSec(), total); err != nil {
deleteErr := s.deleteByID(pd.GetId())
return StoreError.New("failed to write piece meta data to database: %v", utils.CombineErrors(err, deleteErr))
}
log.Printf("Successfully stored %s.", pd.GetId())