2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 01:52:14 +00:00
|
|
|
/*
|
|
|
|
Package localdisk registers the "filesystem" blobserver storage type,
|
|
|
|
storing blobs in a forest of sharded directories at the specified root.
|
|
|
|
|
|
|
|
Example low-level config:
|
|
|
|
|
|
|
|
"/storage/": {
|
|
|
|
"handler": "storage-filesystem",
|
|
|
|
"handlerArgs": {
|
|
|
|
"path": "/var/camlistore/blobs"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
*/
|
2011-02-04 22:31:23 +00:00
|
|
|
package localdisk
|
2010-07-11 04:18:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2011-04-09 06:20:24 +00:00
|
|
|
"io"
|
2010-12-14 02:20:31 +00:00
|
|
|
"os"
|
2014-02-07 18:45:43 +00:00
|
|
|
"path/filepath"
|
2013-09-08 21:55:41 +00:00
|
|
|
"sync"
|
2011-04-03 15:07:40 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2013-10-09 19:20:28 +00:00
|
|
|
"camlistore.org/pkg/blobserver/local"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2014-02-07 18:45:43 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
2013-07-29 03:08:55 +00:00
|
|
|
"camlistore.org/pkg/types"
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
|
|
|
|
2013-07-08 01:52:14 +00:00
|
|
|
// DiskStorage implements the blobserver.Storage interface using the
|
|
|
|
// local filesystem.
|
2011-05-09 16:11:18 +00:00
|
|
|
type DiskStorage struct {
|
2011-02-04 22:31:23 +00:00
|
|
|
root string
|
|
|
|
|
2013-09-08 21:55:41 +00:00
|
|
|
// dirLockMu must be held for writing when deleting an empty directory
|
|
|
|
// and for read when receiving blobs.
|
2013-09-10 20:57:36 +00:00
|
|
|
dirLockMu *sync.RWMutex
|
2013-10-09 19:20:28 +00:00
|
|
|
|
|
|
|
// gen will be nil if partition != ""
|
|
|
|
gen *local.Generationer
|
2011-04-09 06:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 16:23:07 +00:00
|
|
|
func (ds *DiskStorage) String() string {
|
|
|
|
return fmt.Sprintf("\"filesystem\" file-per-blob at %s", ds.root)
|
|
|
|
}
|
|
|
|
|
2014-02-07 18:45:43 +00:00
|
|
|
// IsDir reports whether root is a localdisk (file-per-blob) storage directory.
|
|
|
|
func IsDir(root string) (bool, error) {
|
|
|
|
if osutil.DirExists(filepath.Join(root, blob.RefFromString("").HashName())) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2013-07-08 01:52:14 +00:00
|
|
|
// New returns a new local disk storage implementation at the provided
|
|
|
|
// root directory, which must already exist.
|
2012-11-07 19:55:37 +00:00
|
|
|
func New(root string) (*DiskStorage, error) {
|
2011-02-04 22:31:23 +00:00
|
|
|
// Local disk.
|
2012-11-07 19:55:37 +00:00
|
|
|
fi, err := os.Stat(root)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("Storage root %q doesn't exist", root)
|
2011-02-04 22:31:23 +00:00
|
|
|
}
|
2012-11-07 19:55:37 +00:00
|
|
|
if err != nil {
|
2013-01-12 00:48:44 +00:00
|
|
|
return nil, fmt.Errorf("Failed to stat directory %q: %v", root, err)
|
2012-11-07 19:55:37 +00:00
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, fmt.Errorf("Storage root %q exists but is not a directory.", root)
|
|
|
|
}
|
|
|
|
ds := &DiskStorage{
|
2013-09-10 20:57:36 +00:00
|
|
|
root: root,
|
|
|
|
dirLockMu: new(sync.RWMutex),
|
2013-10-09 19:20:28 +00:00
|
|
|
gen: local.NewGenerationer(root),
|
2011-05-09 16:11:18 +00:00
|
|
|
}
|
2013-11-29 00:33:01 +00:00
|
|
|
if err := ds.migrate3to2(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error updating localdisk format: %v", err)
|
|
|
|
}
|
2012-11-07 19:55:37 +00:00
|
|
|
if _, _, err := ds.StorageGeneration(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error initialization generation for %q: %v", root, err)
|
|
|
|
}
|
|
|
|
return ds, nil
|
2010-12-14 02:20:31 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (storage blobserver.Storage, err error) {
|
2013-01-12 00:48:44 +00:00
|
|
|
path := config.RequiredString("path")
|
2011-04-03 15:07:40 +00:00
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-01-12 00:48:44 +00:00
|
|
|
return New(path)
|
2011-04-02 01:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
blobserver.RegisterStorageConstructor("filesystem", blobserver.StorageConstructor(newFromConfig))
|
|
|
|
}
|
|
|
|
|
2013-09-08 21:55:41 +00:00
|
|
|
func (ds *DiskStorage) tryRemoveDir(dir string) {
|
|
|
|
ds.dirLockMu.Lock()
|
|
|
|
defer ds.dirLockMu.Unlock()
|
|
|
|
os.Remove(dir) // ignore error
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func (ds *DiskStorage) Fetch(blob blob.Ref) (io.ReadCloser, uint32, error) {
|
Get rid of QueueCreator and all its associated complexity.
Previous TODO entry was:
-- Get rid of QueueCreator entirely. Plan:
-- sync handler still has a source and dest (one pair) but
instead of calling CreateQueue on the source, it instead
has an index.Storage (configured via a RequiredObject
so it can be a kvfile, leveldb, mysql, postgres etc)
-- make all the index.Storage types be instantiable
from a jsonconfig Object, perhaps with constructors keyed
on a "type" field.
-- make sync handler support blobserver.Receiver (or StatReceiver)
like indexes, so it can receive blobs. but all it needs to
do to acknowledge the ReceiveBlob is write and flush to its
index.Storage. the syncing is async by default. (otherwise callers
could just use "replica" if they wanted sync replication).
But maybe for ease of configuration switching, we could also
support a sync mode. when it needs to replicate a blob,
it uses the source.
-- future option: sync mirror to an alternate path on ReceiveBlob
that can delete. e.g. you're uploading to s3 and google,
but don't want to upload to both at once, so you use the localdisk
as a buffer to spread out your upstream bandwidth.
-- end result: no more hardlinks or queue creator.
Change-Id: I6244fc4f3a655f08470ae3160502659399f468ed
2013-11-22 22:33:31 +00:00
|
|
|
fileName := ds.blobPath(blob)
|
2010-12-14 02:20:31 +00:00
|
|
|
stat, err := os.Stat(fileName)
|
2012-11-07 19:02:34 +00:00
|
|
|
if os.IsNotExist(err) {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return nil, 0, os.ErrNotExist
|
2010-12-14 02:20:31 +00:00
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
size := types.U32(stat.Size())
|
2011-04-07 17:58:29 +00:00
|
|
|
file, err := os.Open(fileName)
|
2010-12-14 02:20:31 +00:00
|
|
|
if err != nil {
|
2012-11-07 19:02:34 +00:00
|
|
|
if os.IsNotExist(err) {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
err = os.ErrNotExist
|
2011-03-05 08:03:53 +00:00
|
|
|
}
|
2010-12-14 02:20:31 +00:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
return file, size, nil
|
2010-12-14 02:20:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (ds *DiskStorage) RemoveBlobs(blobs []blob.Ref) error {
|
2011-02-03 06:42:31 +00:00
|
|
|
for _, blob := range blobs {
|
Get rid of QueueCreator and all its associated complexity.
Previous TODO entry was:
-- Get rid of QueueCreator entirely. Plan:
-- sync handler still has a source and dest (one pair) but
instead of calling CreateQueue on the source, it instead
has an index.Storage (configured via a RequiredObject
so it can be a kvfile, leveldb, mysql, postgres etc)
-- make all the index.Storage types be instantiable
from a jsonconfig Object, perhaps with constructors keyed
on a "type" field.
-- make sync handler support blobserver.Receiver (or StatReceiver)
like indexes, so it can receive blobs. but all it needs to
do to acknowledge the ReceiveBlob is write and flush to its
index.Storage. the syncing is async by default. (otherwise callers
could just use "replica" if they wanted sync replication).
But maybe for ease of configuration switching, we could also
support a sync mode. when it needs to replicate a blob,
it uses the source.
-- future option: sync mirror to an alternate path on ReceiveBlob
that can delete. e.g. you're uploading to s3 and google,
but don't want to upload to both at once, so you use the localdisk
as a buffer to spread out your upstream bandwidth.
-- end result: no more hardlinks or queue creator.
Change-Id: I6244fc4f3a655f08470ae3160502659399f468ed
2013-11-22 22:33:31 +00:00
|
|
|
fileName := ds.blobPath(blob)
|
2011-02-03 06:42:31 +00:00
|
|
|
err := os.Remove(fileName)
|
2011-02-03 16:28:47 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
continue
|
2012-11-07 19:02:34 +00:00
|
|
|
case os.IsNotExist(err):
|
2012-11-07 21:40:17 +00:00
|
|
|
// deleting already-deleted file; harmless.
|
2011-02-03 06:42:31 +00:00
|
|
|
continue
|
2011-02-03 16:28:47 +00:00
|
|
|
default:
|
|
|
|
return err
|
2011-02-03 06:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|