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 (
|
|
|
|
"camli/blobref"
|
|
|
|
"camli/client"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Things that can be uploaded. (at most one of these)
|
2011-03-05 17:53:51 +00:00
|
|
|
var flagLoop = flag.Bool("loop", false, "sync in a loop once done; requires --removesrc")
|
2011-03-02 02:02:01 +00:00
|
|
|
var flagVerbose = flag.Bool("verbose", false, "be verbose")
|
|
|
|
|
2011-03-05 17:53:51 +00:00
|
|
|
var flagSrc = flag.String("src", "", "Source blobserver prefix (generally a mirrored queue partition)")
|
2011-03-03 04:03:09 +00:00
|
|
|
var flagSrcPass = flag.String("srcpassword", "", "Source password")
|
|
|
|
var flagDest = flag.String("dest", "", "Destination blobserver, or 'stdout' to just enumerate the --src blobs to stdout")
|
|
|
|
var flagDestPass = flag.String("destpassword", "", "Destination password")
|
2011-03-02 02:02:01 +00:00
|
|
|
|
2011-03-05 17:53:51 +00:00
|
|
|
var flagRemoveSource = flag.Bool("removesrc", false,
|
|
|
|
"remove each blob from the source after syncing to the destination; for queue processing")
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2011-03-03 04:03:09 +00:00
|
|
|
if *flagSrc == "" {
|
2011-03-05 17:53:51 +00:00
|
|
|
usage("No --src specified.")
|
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.")
|
|
|
|
}
|
|
|
|
if *flagLoop && !*flagRemoveSource {
|
|
|
|
usage("Can't use --loop without --removesrc")
|
2011-03-02 02:02:01 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 04:03:09 +00:00
|
|
|
sc := client.New(*flagSrc, *flagSrcPass)
|
|
|
|
dc := client.New(*flagDest, *flagDestPass)
|
2011-03-02 02:02:01 +00:00
|
|
|
|
|
|
|
var logger *log.Logger = nil
|
|
|
|
if *flagVerbose {
|
|
|
|
logger = log.New(os.Stderr, "", 0)
|
|
|
|
}
|
|
|
|
sc.SetLogger(logger)
|
|
|
|
dc.SetLogger(logger)
|
|
|
|
|
2011-03-05 17:53:51 +00:00
|
|
|
passNum := 0
|
|
|
|
for {
|
|
|
|
passNum++
|
|
|
|
if err := doPass(sc, dc, passNum); err != nil {
|
|
|
|
log.Fatalf("sync failed: %v", err)
|
|
|
|
}
|
|
|
|
if !*flagLoop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doPass(sc, dc *client.Client, passNum int) (retErr os.Error) {
|
2011-03-03 04:03:09 +00:00
|
|
|
srcBlobs := make(chan *blobref.SizedBlobRef, 100)
|
|
|
|
destBlobs := make(chan *blobref.SizedBlobRef, 100)
|
|
|
|
srcErr := make(chan os.Error)
|
|
|
|
destErr := make(chan os.Error)
|
2011-03-05 03:02:26 +00:00
|
|
|
errorCount := 0
|
2011-03-05 17:53:51 +00:00
|
|
|
|
2011-03-02 02:02:01 +00:00
|
|
|
go func() {
|
2011-03-03 04:03:09 +00:00
|
|
|
srcErr <- sc.EnumerateBlobs(srcBlobs)
|
2011-03-02 02:02:01 +00:00
|
|
|
}()
|
2011-03-05 17:53:51 +00:00
|
|
|
checkSourceError := func() {
|
|
|
|
if err := <-srcErr; err != nil {
|
|
|
|
retErr = os.NewError(fmt.Sprintf("Enumerate error from source: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
destErr <- dc.EnumerateBlobs(destBlobs)
|
|
|
|
}()
|
|
|
|
checkDestError := func() {
|
2011-03-03 04:03:09 +00:00
|
|
|
if err := <-destErr; err != nil {
|
2011-03-05 17:53:51 +00:00
|
|
|
retErr = os.NewError(fmt.Sprintf("Enumerate error from destination: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesCopied := int64(0)
|
|
|
|
blobsCopied := 0
|
|
|
|
|
|
|
|
destNotHaveBlobs := make(chan *blobref.SizedBlobRef, 100)
|
|
|
|
go client.ListMissingDestinationBlobs(destNotHaveBlobs, srcBlobs, destBlobs)
|
|
|
|
for sb := range destNotHaveBlobs {
|
|
|
|
fmt.Printf("Destination needs blob: %s\n", sb)
|
|
|
|
|
|
|
|
blobReader, size, err := sc.Fetch(sb.BlobRef)
|
|
|
|
if err != nil {
|
|
|
|
errorCount++
|
|
|
|
log.Printf("Error fetching %s: %v", sb.BlobRef, err)
|
|
|
|
continue
|
2011-03-03 04:03:09 +00:00
|
|
|
}
|
2011-03-05 17:53:51 +00:00
|
|
|
if size != sb.Size {
|
|
|
|
errorCount++
|
|
|
|
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 {
|
|
|
|
errorCount++
|
|
|
|
log.Printf("Upload of %s to destination blobserver failed: %v", sb.BlobRef, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !pr.Skipped {
|
|
|
|
blobsCopied++
|
|
|
|
bytesCopied += pr.Size
|
|
|
|
}
|
|
|
|
if *flagRemoveSource {
|
2011-03-05 20:46:28 +00:00
|
|
|
if err = sc.RemoveBlob(sb.BlobRef); err != nil {
|
2011-03-05 19:34:12 +00:00
|
|
|
errorCount++
|
|
|
|
log.Printf("Failed to delete %s from source: %v", sb.BlobRef, err)
|
|
|
|
}
|
2011-03-05 17:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: return blobsCopied & bytesCopied
|
|
|
|
checkSourceError()
|
|
|
|
checkDestError()
|
|
|
|
if retErr == nil && errorCount > 0 {
|
|
|
|
retErr = os.NewError(fmt.Sprintf("%d errors during sync", errorCount))
|
2011-03-03 04:03:09 +00:00
|
|
|
}
|
2011-03-05 17:53:51 +00:00
|
|
|
return
|
2011-03-02 02:02:01 +00:00
|
|
|
}
|