func TestSS_SaveRestore(t *testing.T) {
	loc := datastore.NewINodeDBSSLocator(authtu.TestDSConfig(testRootKey()))
	if _, err := loc.DeleteAll(); err != nil {
		t.Errorf("Failed to loc.DeleteAll: %v", err)
	}

	bs := tu.TestFileBlobStore()
	sio := blobstoredbstatesnapshotio.New(bs, tu.TestCipher(), loc)

	db, err := inodedb.NewEmptyDB(sio, inodedb.NewSimpleDBTransactionLogIO())
	if err != nil {
		t.Errorf("Failed to NewEmptyDB: %v", err)
		return
	}
	if err := db.Sync(); err != nil {
		t.Errorf("Failed to Sync DB: %v", err)
		return
	}

	_, err = inodedb.NewDB(sio, inodedb.NewSimpleDBTransactionLogIO())
	if err != nil {
		t.Errorf("Failed to NewDB: %v", err)
	}
}
Esempio n. 2
0
func TestINodeDBSSLocator_PutLocate(t *testing.T) {
	loc := datastore.NewINodeDBSSLocator(authtu.TestDSConfig(authtu.TestBucketName()))

	if _, err := loc.DeleteAll(); err != nil {
		t.Errorf("DeleteAll failed unexpectedly: %v", err)
		return
	}

	bp, err := loc.Locate(0)
	if err != datastore.EEMPTY {
		t.Errorf("Locate() when no entry should fail, but succeeded.")
	}

	if err := loc.Put("META-snapshot123", 123); err != nil {
		t.Errorf("Put failed unexpectedly: %v", err)
		return
	}
	if err := loc.Put("META-snapshot231", 231); err != nil {
		t.Errorf("Put failed unexpectedly: %v", err)
		return
	}

	bp, err = loc.Locate(0)
	if err != nil {
		t.Errorf("Locate failed unexpectedly: %v", err)
		return
	}
	if bp != "META-snapshot231" {
		t.Errorf("Locate returned unexpected bp: %v", bp)
		return
	}

	bp, err = loc.Locate(1)
	if err != nil {
		t.Errorf("Locate failed unexpectedly: %v", err)
		return
	}
	if bp != "META-snapshot123" {
		t.Errorf("Locate returned unexpected bp: %v", bp)
		return
	}

	if err := loc.Put("META-snapshot345", 345); err != nil {
		t.Errorf("Put failed unexpectedly: %v", err)
		return
	}

	bp, err = loc.Locate(0)
	if err != nil {
		t.Errorf("Locate failed unexpectedly: %v", err)
		return
	}
	if bp != "META-snapshot345" {
		t.Errorf("Locate returned unexpected bp: %v", bp)
		return
	}

	bps, err := loc.DeleteAll()
	if err != nil {
		t.Errorf("DeleteAll failed unexpectedly: %v", err)
		return
	}
	sort.Strings(bps)
	if !reflect.DeepEqual([]string{
		"META-snapshot123",
		"META-snapshot231",
		"META-snapshot345",
	}, bps) {
		t.Errorf("DeleteAll returned unexpected blobpaths: %v", bps)
		return
	}
}
Esempio n. 3
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	flag.Usage = Usage
	flag.Parse()
	if flag.NArg() != 1 {
		Usage()
		os.Exit(1)
	}
	switch flag.Arg(0) {
	case "list", "purge":
		break
	default:
		log.Printf("Unknown cmd: %v", flag.Arg(0))
		Usage()
		os.Exit(2)
	}

	cfg, err := facade.NewConfig(*flagConfigDir)
	if err != nil {
		log.Printf("%v", err)
		Usage()
		os.Exit(1)
	}

	tsrc, err := auth.GetGCloudTokenSource(context.Background(), cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
	if err != nil {
		log.Fatalf("Failed to init GCloudTokenSource: %v", err)
	}
	key := btncrypt.KeyFromPassword(cfg.Password)
	c, err := btncrypt.NewCipher(key)
	if err != nil {
		log.Fatalf("Failed to init btncrypt.Cipher: %v", err)
	}

	dscfg := datastore.NewConfig(cfg.ProjectName, cfg.BucketName, c, tsrc)
	ssloc := datastore.NewINodeDBSSLocator(dscfg)
	switch flag.Arg(0) {
	case "purge":
		fmt.Printf("Do you really want to proceed with deleting all inodedbsslocator entry for %s?\n", cfg.BucketName)
		fmt.Printf("Type \"deleteall\" to proceed: ")
		sc := bufio.NewScanner(os.Stdin)
		if !sc.Scan() {
			return
		}
		if sc.Text() != "deleteall" {
			log.Printf("Cancelled.\n")
			os.Exit(1)
		}

		es, err := ssloc.DeleteAll()
		if err != nil {
			log.Printf("DeleteAll failed: %v", err)
		}
		log.Printf("DeleteAll deleted entries for blobpath: %v", es)
		// FIXME: delete the entries from blobpath too

	case "list":
		history := 0
	histloop:
		for {
			bp, err := ssloc.Locate(history)
			if err != nil {
				if err == datastore.EEMPTY {
					log.Printf("Locate(%d): no entry", history)
				} else {
					log.Printf("Locate(%d) err: %v", history, err)
				}
				break histloop
			}
			log.Printf("Locate(%d): %v", history, bp)

			history++
		}
	default:
		panic("NOT REACHED")
	}
}
func TestSS_AutoAvoidCorruptedSnapshot(t *testing.T) {
	loc := datastore.NewINodeDBSSLocator(authtu.TestDSConfig(testRootKey()))
	if _, err := loc.DeleteAll(); err != nil {
		t.Errorf("Failed to loc.DeleteAll: %v", err)
	}

	bs := tu.TestFileBlobStore()
	sio := blobstoredbstatesnapshotio.New(bs, tu.TestCipher(), loc)
	txlogio := inodedb.NewSimpleDBTransactionLogIO()

	{
		db, err := inodedb.NewEmptyDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewEmptyDB: %v", err)
			return
		}

		// create 1st snapshot
		if err := db.Sync(); err != nil {
			t.Errorf("Failed to Sync DB: %v", err)
			return
		}

		// apply some mod to inodedb
		nlock, err := db.LockNode(inodedb.AllocateNewNodeID)
		if err != nil {
			t.Errorf("Failed to LockNode: %v", err)
			return
		}

		tx := inodedb.DBTransaction{Ops: []inodedb.DBOperation{
			&inodedb.CreateNodeOp{NodeLock: nlock, OrigPath: "/hoge.txt", Type: inodedb.FileNodeT},
			&inodedb.HardLinkOp{NodeLock: inodedb.NodeLock{1, inodedb.NoTicket}, Name: "hoge.txt", TargetID: nlock.ID},
		}}
		if _, err := db.ApplyTransaction(tx); err != nil {
			t.Errorf("Failed to apply tx: %v", err)
			return
		}

		if err := db.UnlockNode(nlock); err != nil {
			t.Errorf("Failed to UnlockNode: %v", err)
			return
		}

		// create 2nd snapshot
		if err := db.Sync(); err != nil {
			t.Errorf("Failed to Sync DB (2): %v", err)
			return
		}
	}

	if _, err := inodedb.NewDB(sio, txlogio); err != nil {
		t.Errorf("Failed to NewDB (uncorrupted): %v", err)
		return
	}

	// destroy latest snapshot (corrupt data)
	ssbp, err := loc.Locate(0)
	if err != nil {
		t.Errorf("Failed to locate latest ssbp: %v", err)
		return
	}
	{
		wc, err := bs.OpenWriter(ssbp)
		if err != nil {
			t.Errorf("Failed to OpenWriter: %v", err)
			return
		}
		if _, err := wc.Write([]byte("hoge")); err != nil {
			t.Errorf("Failed to Write: %v", err)
		}
		wc.Close()
	}

	{
		_, err = inodedb.NewDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewDB (corrupted): %v", err)
			return
		}
	}

	// destroy latest snapshot (remove ss blob)
	if err := bs.RemoveBlob(ssbp); err != nil {
		t.Errorf("Failed to RemoveBlob: %v", err)
	}

	{
		_, err = inodedb.NewDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewDB (ss blob removed): %v", err)
			return
		}
	}
}
Esempio n. 5
0
func NewOtaru(cfg *Config, oneshotcfg *OneshotConfig) (*Otaru, error) {
	o := &Otaru{}

	var err error

	key := btncrypt.KeyFromPassword(cfg.Password)
	o.C, err = btncrypt.NewCipher(key)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init Cipher: %v", err)
	}

	o.S = scheduler.NewScheduler()

	if !cfg.LocalDebug {
		o.Tsrc, err = auth.GetGCloudTokenSource(context.TODO(), cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("Failed to init GCloudClientSource: %v", err)
		}
		o.DSCfg = datastore.NewConfig(cfg.ProjectName, cfg.BucketName, o.C, o.Tsrc)
		o.GL = datastore.NewGlobalLocker(o.DSCfg, GenHostName(), "FIXME: fill info")
		if err := o.GL.Lock(); err != nil {
			return nil, err
		}
	}

	o.CacheTgtBS, err = blobstore.NewFileBlobStore(cfg.CacheDir, oflags.O_RDWRCREATE)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init FileBlobStore: %v", err)
	}

	if !cfg.LocalDebug {
		o.DefaultBS, err = gcs.NewGCSBlobStore(cfg.ProjectName, cfg.BucketName, o.Tsrc, oflags.O_RDWRCREATE)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("Failed to init GCSBlobStore: %v", err)
		}
		if !cfg.UseSeparateBucketForMetadata {
			o.BackendBS = o.DefaultBS
		} else {
			metabucketname := fmt.Sprintf("%s-meta", cfg.BucketName)
			o.MetadataBS, err = gcs.NewGCSBlobStore(cfg.ProjectName, metabucketname, o.Tsrc, oflags.O_RDWRCREATE)
			if err != nil {
				o.Close()
				return nil, fmt.Errorf("Failed to init GCSBlobStore (metadata): %v", err)
			}

			o.BackendBS = blobstore.Mux{
				blobstore.MuxEntry{metadata.IsMetadataBlobpath, o.MetadataBS},
				blobstore.MuxEntry{nil, o.DefaultBS},
			}
		}
	} else {
		o.BackendBS, err = blobstore.NewFileBlobStore(path.Join(os.Getenv("HOME"), ".otaru", "bbs"), oflags.O_RDWRCREATE)
	}

	queryFn := chunkstore.NewQueryChunkVersion(o.C)
	o.CBS, err = cachedblobstore.New(o.BackendBS, o.CacheTgtBS, o.S, oflags.O_RDWRCREATE /* FIXME */, queryFn)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init CachedBlobStore: %v", err)
	}
	if err := o.CBS.RestoreState(o.C); err != nil {
		logger.Warningf(mylog, "Attempted to restore cachedblobstore state but failed: %v", err)
	}
	o.CSS = cachedblobstore.NewCacheSyncScheduler(o.CBS)

	if !cfg.LocalDebug {
		o.SSLoc = datastore.NewINodeDBSSLocator(o.DSCfg)
	} else {
		logger.Panicf(mylog, "Implement mock sslocator that doesn't depend on gcloud/datastore")
	}
	o.SIO = blobstoredbstatesnapshotio.New(o.CBS, o.C, o.SSLoc)

	if !cfg.LocalDebug {
		txio := datastore.NewDBTransactionLogIO(o.DSCfg)
		o.TxIO = txio
		o.TxIOSS = util.NewSyncScheduler(txio, 300*time.Millisecond)
	} else {
		o.TxIO = inodedb.NewSimpleDBTransactionLogIO()
	}
	o.CTxIO = inodedb.NewCachedDBTransactionLogIO(o.TxIO)

	if oneshotcfg.Mkfs {
		o.IDBBE, err = inodedb.NewEmptyDB(o.SIO, o.CTxIO)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("NewEmptyDB failed: %v", err)
		}
	} else {
		o.IDBBE, err = inodedb.NewDB(o.SIO, o.CTxIO)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("NewDB failed: %v", err)
		}
	}

	o.IDBS = inodedb.NewDBService(o.IDBBE)
	o.IDBSS = util.NewSyncScheduler(o.IDBS, 30*time.Second)

	o.FS = otaru.NewFileSystem(o.IDBS, o.CBS, o.C)
	o.MGMT = mgmt.NewServer()
	if err := o.runMgmtServer(); err != nil {
		o.Close()
		return nil, fmt.Errorf("Mgmt server run failed: %v", err)
	}

	return o, nil
}