2011-03-02 02:02:01 +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 main
import (
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
"errors"
2011-03-02 02:02:01 +00:00
"flag"
"fmt"
"log"
"os"
2012-12-22 23:22:22 +00:00
"time"
2012-12-22 00:41:15 +00:00
"camlistore.org/pkg/blobref"
"camlistore.org/pkg/client"
2011-03-02 02:02:01 +00:00
)
2012-12-22 00:41:15 +00:00
var (
flagLoop = flag . Bool ( "loop" , false , "sync in a loop once done; requires --removesrc" )
flagVerbose = flag . Bool ( "verbose" , false , "be verbose" )
2013-01-15 13:53:25 +00:00
flagAll = flag . Bool ( "all" , false , "Discover all sync destinations configured on the source server and run them." )
2011-03-02 02:02:01 +00:00
2013-01-15 13:53:25 +00:00
flagSrc = flag . String ( "src" , "" , "Source blobserver is either a URL prefix (with optional path), a host[:port], or blank to use the Camlistore client config's default host." )
flagDest = flag . String ( "dest" , "" , "Destination blobserver, or 'stdout' to just enumerate the --src blobs to stdout" )
2011-03-02 02:02:01 +00:00
2012-12-22 00:41:15 +00:00
flagRemoveSource = flag . Bool ( "removesrc" , false ,
"remove each blob from the source after syncing to the destination; for queue processing" )
)
2011-03-05 17:53:51 +00:00
2013-01-17 00:24:42 +00:00
var logger * log . Logger = nil
2011-06-14 15:09:15 +00:00
type SyncStats struct {
2011-07-02 16:09:50 +00:00
BlobsCopied int
BytesCopied int64
ErrorCount int
2011-06-14 15:09:15 +00:00
}
2011-03-02 02:02:01 +00:00
func usage ( err string ) {
if err != "" {
2011-03-05 17:53:51 +00:00
fmt . Fprintf ( os . Stderr , "Error: %s\n\nUsage:\n" , err )
2011-03-02 02:02:01 +00:00
}
flag . PrintDefaults ( )
os . Exit ( 2 )
}
2013-01-15 13:53:25 +00:00
func syncAll ( ) error {
if * flagLoop {
usage ( "--all can not be used with --loop" )
}
dc := discoClient ( )
2013-01-17 00:24:42 +00:00
dc . SetLogger ( logger )
2013-01-15 13:53:25 +00:00
syncHandlers , err := dc . SyncHandlers ( )
if err != nil {
log . Fatalf ( "sync handlers discovery failed: %v" , err )
}
for _ , sh := range syncHandlers {
from := client . New ( sh . From )
2013-01-17 00:24:42 +00:00
from . SetLogger ( logger )
2013-01-15 13:53:25 +00:00
from . SetupAuth ( )
to := client . New ( sh . To )
2013-01-17 00:24:42 +00:00
to . SetLogger ( logger )
2013-01-15 13:53:25 +00:00
to . SetupAuth ( )
stats , err := doPass ( from , to )
if * flagVerbose {
log . Printf ( "sync stats, blobs: %d, bytes %d\n" , stats . BlobsCopied , stats . BytesCopied )
}
if err != nil {
return err
}
}
return nil
}
// discoClient returns a client initialized with a server
// based from --src or from the configuration file if --src
// is blank. The returned client can then be used to discover
// the blobRoot and syncHandlers.
func discoClient ( ) * client . Client {
var cl * client . Client
if * flagSrc == "" {
cl = client . NewOrFail ( )
} else {
cl = client . New ( * flagSrc )
}
cl . SetupAuth ( )
return cl
}
2011-03-02 02:02:01 +00:00
func main ( ) {
flag . Parse ( )
2013-01-15 13:53:25 +00:00
if * flagLoop && ! * flagRemoveSource {
usage ( "Can't use --loop without --removesrc" )
}
2013-01-17 00:24:42 +00:00
if * flagVerbose {
logger = log . New ( os . Stderr , "" , 0 )
}
2013-01-15 13:53:25 +00:00
if * flagAll {
err := syncAll ( )
if err != nil {
log . Fatalf ( "sync all failed: %v" , err )
}
return
2011-03-02 02:02:01 +00:00
}
2011-03-03 04:03:09 +00:00
if * flagDest == "" {
2011-03-05 17:53:51 +00:00
usage ( "No --dest specified." )
}
2013-01-15 13:53:25 +00:00
2013-01-17 00:24:42 +00:00
discl := discoClient ( )
discl . SetLogger ( logger )
src , err := discl . BlobRoot ( )
2013-01-15 13:53:25 +00:00
if err != nil {
log . Fatalf ( "Failed to get blob source: %v" , err )
2011-03-02 02:02:01 +00:00
}
2013-01-15 13:53:25 +00:00
sc := client . New ( src )
2011-11-16 10:41:38 +00:00
sc . SetupAuth ( )
dc := client . New ( * flagDest )
dc . SetupAuth ( )
2011-03-02 02:02:01 +00:00
sc . SetLogger ( logger )
dc . SetLogger ( logger )
2011-03-05 17:53:51 +00:00
passNum := 0
for {
passNum ++
2013-01-15 13:53:25 +00:00
stats , err := doPass ( sc , dc )
2011-06-14 15:09:15 +00:00
if * flagVerbose {
log . Printf ( "sync stats - pass: %d, blobs: %d, bytes %d\n" , passNum , stats . BlobsCopied , stats . BytesCopied )
}
2012-12-23 17:00:51 +00:00
if err != nil {
log . Fatalf ( "sync failed: %v" , err )
}
2011-03-05 17:53:51 +00:00
if ! * flagLoop {
break
}
}
}
2013-01-15 13:53:25 +00:00
func doPass ( sc , dc * client . Client ) ( stats SyncStats , retErr error ) {
2011-05-10 23:13:37 +00:00
srcBlobs := make ( chan blobref . SizedBlobRef , 100 )
destBlobs := make ( chan blobref . SizedBlobRef , 100 )
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
srcErr := make ( chan error )
destErr := make ( chan error )
2011-03-05 17:53:51 +00:00
2011-03-02 02:02:01 +00:00
go func ( ) {
2012-10-09 14:47:43 +00:00
srcErr <- sc . SimpleEnumerateBlobs ( srcBlobs )
2011-03-02 02:02:01 +00:00
} ( )
2011-03-05 17:53:51 +00:00
checkSourceError := func ( ) {
if err := <- srcErr ; err != nil {
2012-12-22 23:22:22 +00:00
retErr = fmt . Errorf ( "Enumerate error from source: %v" , err )
2011-03-05 17:53:51 +00:00
}
}
2011-03-03 04:03:09 +00:00
if * flagDest == "stdout" {
for sb := range srcBlobs {
fmt . Printf ( "%s %d\n" , sb . BlobRef , sb . Size )
}
2011-03-05 17:53:51 +00:00
checkSourceError ( )
return
2011-03-02 02:02:01 +00:00
}
2011-03-05 17:53:51 +00:00
go func ( ) {
2012-10-09 14:47:43 +00:00
destErr <- dc . SimpleEnumerateBlobs ( destBlobs )
2011-03-05 17:53:51 +00:00
} ( )
checkDestError := func ( ) {
2011-03-03 04:03:09 +00:00
if err := <- destErr ; err != nil {
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
retErr = errors . New ( fmt . Sprintf ( "Enumerate error from destination: %v" , err ) )
2011-03-05 17:53:51 +00:00
}
}
2012-12-23 17:22:25 +00:00
destNotHaveBlobs := make ( chan blobref . SizedBlobRef )
sizeMismatch := make ( chan * blobref . BlobRef )
2012-12-22 23:22:22 +00:00
readSrcBlobs := srcBlobs
if * flagVerbose {
readSrcBlobs = loggingBlobRefChannel ( srcBlobs )
}
2012-12-23 17:22:25 +00:00
mismatches := [ ] * blobref . BlobRef { }
go client . ListMissingDestinationBlobs ( destNotHaveBlobs , sizeMismatch , readSrcBlobs , destBlobs )
For :
for {
select {
case br := <- sizeMismatch :
// TODO(bradfitz): check both sides and repair, carefully. For now, fail.
log . Printf ( "WARNING: blobref %v has differing sizes on source and est" , br )
2011-06-14 15:09:15 +00:00
stats . ErrorCount ++
2012-12-23 17:22:25 +00:00
mismatches = append ( mismatches , br )
case sb , ok := <- destNotHaveBlobs :
if ! ok {
break For
}
fmt . Printf ( "Destination needs blob: %s\n" , sb )
blobReader , size , err := sc . FetchStreaming ( sb . BlobRef )
if err != nil {
stats . ErrorCount ++
log . Printf ( "Error fetching %s: %v" , sb . BlobRef , err )
continue
}
if size != sb . Size {
2011-06-14 15:09:15 +00:00
stats . ErrorCount ++
2012-12-23 17:22:25 +00:00
log . Printf ( "Source blobserver's enumerate size of %d for blob %s doesn't match its Get size of %d" ,
sb . Size , sb . BlobRef , size )
continue
}
uh := & client . UploadHandle { BlobRef : sb . BlobRef , Size : size , Contents : blobReader }
pr , err := dc . Upload ( uh )
if err != nil {
stats . ErrorCount ++
log . Printf ( "Upload of %s to destination blobserver failed: %v" , sb . BlobRef , err )
continue
}
if ! pr . Skipped {
stats . BlobsCopied ++
stats . BytesCopied += pr . Size
}
if * flagRemoveSource {
if err = sc . RemoveBlob ( sb . BlobRef ) ; err != nil {
stats . ErrorCount ++
log . Printf ( "Failed to delete %s from source: %v" , sb . BlobRef , err )
}
2011-03-05 19:34:12 +00:00
}
2011-03-05 17:53:51 +00:00
}
}
checkSourceError ( )
checkDestError ( )
2011-06-14 15:09:15 +00:00
if retErr == nil && stats . ErrorCount > 0 {
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
retErr = errors . New ( fmt . Sprintf ( "%d errors during sync" , stats . ErrorCount ) )
2011-03-03 04:03:09 +00:00
}
2011-06-14 15:09:15 +00:00
return stats , retErr
2011-03-02 02:02:01 +00:00
}
2012-12-22 23:22:22 +00:00
func loggingBlobRefChannel ( ch <- chan blobref . SizedBlobRef ) chan blobref . SizedBlobRef {
ch2 := make ( chan blobref . SizedBlobRef )
go func ( ) {
defer close ( ch2 )
var last time . Time
2012-12-23 17:00:51 +00:00
var nblob , nbyte int64
2012-12-22 23:22:22 +00:00
for v := range ch {
ch2 <- v
2012-12-23 17:00:51 +00:00
nblob ++
nbyte += v . Size
2012-12-22 23:22:22 +00:00
now := time . Now ( )
if last . IsZero ( ) || now . After ( last . Add ( 1 * time . Second ) ) {
last = now
2012-12-23 17:00:51 +00:00
log . Printf ( "At source blob %v (%d blobs, %d bytes)" , v . BlobRef , nblob , nbyte )
2012-12-22 23:22:22 +00:00
}
}
2012-12-23 17:00:51 +00:00
log . Printf ( "Total blobs: %d, %d bytes" , nblob , nbyte )
2012-12-22 23:22:22 +00:00
} ( )
return ch2
}