2011-02-10 01:05:55 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package blobserver
|
|
|
|
|
|
|
|
import (
|
2011-02-26 20:22:26 +00:00
|
|
|
"sync"
|
2013-08-21 20:57:28 +00:00
|
|
|
"time"
|
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
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-11-25 01:12:56 +00:00
|
|
|
"camlistore.org/pkg/syncutil"
|
2011-02-10 01:05:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlobHub interface {
|
2013-11-23 19:09:06 +00:00
|
|
|
// TODO(bradfitz): no need for this to be an interface anymore. make GetHub return
|
|
|
|
// a concrete type instead? also, maybe remove all the async listener stuff, or
|
|
|
|
// audit its users and remove anything that is now unnecessary.
|
|
|
|
|
|
|
|
// NotifyBlobReceived notes that a storage target has successfully received
|
|
|
|
// a blob and asynchronously notifies registered listeners.
|
|
|
|
//
|
|
|
|
// If any synchronous receive hooks are registered, they're run before
|
|
|
|
// NotifyBlobReceived returns and their error is returned.
|
2013-11-25 00:20:11 +00:00
|
|
|
NotifyBlobReceived(blob.SizedRef) error
|
2013-11-23 19:09:06 +00:00
|
|
|
|
|
|
|
// AddReceiveHook adds a hook that is synchronously run
|
|
|
|
// whenever blobs are received. All registered hooks are run
|
|
|
|
// on each blob upload but if more than one returns an error,
|
|
|
|
// NotifyBlobReceived will only return one of the errors.
|
2013-11-25 00:20:11 +00:00
|
|
|
AddReceiveHook(func(blob.SizedRef) error)
|
2011-02-10 01:05:55 +00:00
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
RegisterListener(ch chan<- blob.Ref)
|
|
|
|
UnregisterListener(ch chan<- blob.Ref)
|
2011-02-26 20:22:26 +00:00
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
RegisterBlobListener(blob blob.Ref, ch chan<- blob.Ref)
|
|
|
|
UnregisterBlobListener(blob blob.Ref, ch chan<- blob.Ref)
|
2011-02-10 01:05:55 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
var (
|
2013-11-23 19:09:06 +00:00
|
|
|
hubmu sync.RWMutex
|
2013-08-21 20:57:28 +00:00
|
|
|
stohub = map[interface{}]BlobHub{} // Storage -> hub
|
|
|
|
)
|
|
|
|
|
2013-11-23 19:09:06 +00:00
|
|
|
// GetHub return a BlobHub for the given storage implementation.
|
2013-08-21 20:57:28 +00:00
|
|
|
func GetHub(storage interface{}) BlobHub {
|
2013-11-23 19:09:06 +00:00
|
|
|
if h, ok := getHub(storage); ok {
|
|
|
|
return h
|
|
|
|
}
|
2013-08-21 20:57:28 +00:00
|
|
|
hubmu.Lock()
|
|
|
|
defer hubmu.Unlock()
|
|
|
|
h, ok := stohub[storage]
|
|
|
|
if ok {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
h = new(memHub)
|
|
|
|
stohub[storage] = h
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2013-11-23 19:09:06 +00:00
|
|
|
func getHub(storage interface{}) (BlobHub, bool) {
|
|
|
|
hubmu.RLock()
|
|
|
|
defer hubmu.RUnlock()
|
|
|
|
h, ok := stohub[storage]
|
|
|
|
return h, ok
|
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
// canLongPoll is set to false on App Engine. (multi-process environment...)
|
|
|
|
var canLongPoll = true
|
|
|
|
|
|
|
|
// WaitForBlob waits until deadline for blobs to arrive. If blobs is empty, any
|
|
|
|
// blobs are waited on. Otherwise, those specific blobs are waited on.
|
|
|
|
// When WaitForBlob returns, nothing may have happened.
|
|
|
|
func WaitForBlob(storage interface{}, deadline time.Time, blobs []blob.Ref) {
|
|
|
|
hub := GetHub(storage)
|
|
|
|
ch := make(chan blob.Ref, 1)
|
|
|
|
if len(blobs) == 0 {
|
|
|
|
hub.RegisterListener(ch)
|
|
|
|
defer hub.UnregisterListener(ch)
|
|
|
|
}
|
|
|
|
for _, br := range blobs {
|
|
|
|
hub.RegisterBlobListener(br, ch)
|
|
|
|
defer hub.UnregisterBlobListener(br, ch)
|
|
|
|
}
|
|
|
|
var tc <-chan time.Time
|
|
|
|
if !canLongPoll {
|
|
|
|
tc = time.After(2 * time.Second)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-tc:
|
|
|
|
case <-time.After(deadline.Sub(time.Now())):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type memHub struct {
|
2013-11-23 19:09:06 +00:00
|
|
|
mu sync.RWMutex
|
2011-02-26 20:22:26 +00:00
|
|
|
|
2013-11-25 00:20:11 +00:00
|
|
|
hooks []func(blob.SizedRef) error
|
2013-08-21 20:57:28 +00:00
|
|
|
listeners map[chan<- blob.Ref]bool
|
2013-11-23 19:09:06 +00:00
|
|
|
blobListeners map[blob.Ref]map[chan<- blob.Ref]bool
|
2011-02-26 20:22:26 +00:00
|
|
|
}
|
2011-02-10 01:05:55 +00:00
|
|
|
|
2013-11-25 00:20:11 +00:00
|
|
|
func (h *memHub) NotifyBlobReceived(sb blob.SizedRef) error {
|
2013-11-23 19:09:06 +00:00
|
|
|
h.mu.RLock()
|
|
|
|
defer h.mu.RUnlock()
|
2011-02-26 20:22:26 +00:00
|
|
|
|
2013-11-25 00:20:11 +00:00
|
|
|
br := sb.Ref
|
|
|
|
|
2011-02-26 20:22:26 +00:00
|
|
|
// Callback channels to notify, nil until non-empty
|
2013-08-21 20:57:28 +00:00
|
|
|
var notify []chan<- blob.Ref
|
2011-02-26 20:22:26 +00:00
|
|
|
|
|
|
|
// Append global listeners
|
2013-11-23 19:09:06 +00:00
|
|
|
for ch := range h.listeners {
|
2011-02-26 20:22:26 +00:00
|
|
|
notify = append(notify, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append blob-specific listeners
|
|
|
|
if h.blobListeners != nil {
|
2013-11-23 19:09:06 +00:00
|
|
|
if set, ok := h.blobListeners[br]; ok {
|
2011-02-26 20:22:26 +00:00
|
|
|
for ch, _ := range set {
|
|
|
|
notify = append(notify, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
if len(notify) > 0 {
|
|
|
|
// Run in a separate Goroutine so NotifyBlobReceived doesn't block
|
|
|
|
// callers if callbacks are slow.
|
|
|
|
go func() {
|
|
|
|
for _, ch := range notify {
|
|
|
|
ch <- br
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2013-11-23 19:09:06 +00:00
|
|
|
|
2013-11-25 01:12:56 +00:00
|
|
|
var grp syncutil.Group
|
|
|
|
for i := range h.hooks {
|
|
|
|
hook := h.hooks[i]
|
|
|
|
grp.Go(func() error { return hook(sb) })
|
2013-11-23 19:09:06 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 01:12:56 +00:00
|
|
|
return grp.Err()
|
2011-02-26 20:22:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
func (h *memHub) RegisterListener(ch chan<- blob.Ref) {
|
2013-11-23 19:09:06 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2011-02-26 20:22:26 +00:00
|
|
|
if h.listeners == nil {
|
2013-08-21 20:57:28 +00:00
|
|
|
h.listeners = make(map[chan<- blob.Ref]bool)
|
2011-02-26 20:22:26 +00:00
|
|
|
}
|
|
|
|
h.listeners[ch] = true
|
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
func (h *memHub) UnregisterListener(ch chan<- blob.Ref) {
|
2013-11-23 19:09:06 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2011-02-26 20:22:26 +00:00
|
|
|
if h.listeners == nil {
|
|
|
|
panic("blobhub: UnregisterListener called without RegisterListener")
|
|
|
|
}
|
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
|
|
|
delete(h.listeners, ch)
|
2011-02-10 01:05:55 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
func (h *memHub) RegisterBlobListener(br blob.Ref, ch chan<- blob.Ref) {
|
2013-11-23 19:09:06 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2011-02-26 20:22:26 +00:00
|
|
|
if h.blobListeners == nil {
|
2013-11-23 19:09:06 +00:00
|
|
|
h.blobListeners = make(map[blob.Ref]map[chan<- blob.Ref]bool)
|
2011-02-26 20:22:26 +00:00
|
|
|
}
|
2013-11-23 19:09:06 +00:00
|
|
|
_, ok := h.blobListeners[br]
|
2011-02-26 20:22:26 +00:00
|
|
|
if !ok {
|
2013-11-23 19:09:06 +00:00
|
|
|
h.blobListeners[br] = make(map[chan<- blob.Ref]bool)
|
2011-02-26 20:22:26 +00:00
|
|
|
}
|
2013-11-23 19:09:06 +00:00
|
|
|
h.blobListeners[br][ch] = true
|
2011-02-10 01:05:55 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 19:09:06 +00:00
|
|
|
func (h *memHub) UnregisterBlobListener(br blob.Ref, ch chan<- blob.Ref) {
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
2011-02-26 20:22:26 +00:00
|
|
|
if h.blobListeners == nil {
|
|
|
|
panic("blobhub: UnregisterBlobListener called without RegisterBlobListener")
|
|
|
|
}
|
2013-11-23 19:09:06 +00:00
|
|
|
set, ok := h.blobListeners[br]
|
2011-02-26 20:22:26 +00:00
|
|
|
if !ok {
|
2013-11-23 19:09:06 +00:00
|
|
|
panic("blobhub: UnregisterBlobListener called without RegisterBlobListener for " + br.String())
|
2011-02-26 20:22:26 +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
|
|
|
delete(set, ch)
|
2011-02-26 20:22:26 +00:00
|
|
|
if len(set) == 0 {
|
2013-11-23 19:09:06 +00:00
|
|
|
delete(h.blobListeners, br)
|
2011-02-26 20:22:26 +00:00
|
|
|
}
|
2011-02-10 01:05:55 +00:00
|
|
|
}
|
2011-03-28 05:06:29 +00:00
|
|
|
|
2013-11-25 00:20:11 +00:00
|
|
|
func (h *memHub) AddReceiveHook(hook func(blob.SizedRef) error) {
|
2013-11-23 19:09:06 +00:00
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
h.hooks = append(h.hooks, hook)
|
2011-03-28 05:06:29 +00:00
|
|
|
}
|