Beispiel #1
0
func Test_ChunkIOWrite_OverflowUpdate(t *testing.T) {
	testbh := &TestBlobHandle{}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())
	if err := cio.PWrite(0, HelloWorld); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	if err := cio.PWrite(7, HogeFugaPiyo); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}

	cr, err := chunkstore.NewChunkReader(bytes.NewBuffer(testbh.Buf), TestCipher())
	if err != nil {
		t.Errorf("failed to create chunk reader: %v", err)
		return
	}
	exp := []byte("Hello, hogefugapiyo")
	if cr.Length() != len(exp) {
		t.Errorf("failed to recover payload len")
	}
	readtgt := make([]byte, len(exp))
	if _, err := io.ReadFull(cr, readtgt); err != nil {
		t.Errorf("failed to Read from ChunkReader: %v", err)
		return
	}
	if !bytes.Equal(readtgt, exp) {
		t.Errorf("Read content invalid")
		return
	}
}
Beispiel #2
0
func Test_ChunkIOWrite_ZeroFillPadding(t *testing.T) {
	testbh := &TestBlobHandle{}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())

	// [ zero ][ hello ]
	//    10      12
	if err := cio.PWrite(10, HelloWorld); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	readtgt := make([]byte, len(HelloWorld))
	if err := cio.PRead(10, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, HelloWorld) {
		t.Errorf("Read content invalid")
		return
	}
	readtgt2 := make([]byte, 10+len(HelloWorld))
	if err := cio.PRead(0, readtgt2); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	checkZero(t, readtgt2, 0, 10)
	if !bytes.Equal(readtgt2[10:10+12], HelloWorld) {
		t.Errorf("Read content invalid: hello1 %v != %v", readtgt2[10:10+12], HelloWorld)
		return
	}

	// [ zero ][ hello ][ zero ][ hello ]
	//    10      12      512k      12
	if err := cio.PWrite(10+12+512*1024, HelloWorld); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	readtgt3 := make([]byte, 10+12+512*1024+12)
	if err := cio.PRead(0, readtgt3); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	checkZero(t, readtgt3, 0, 10)
	checkZero(t, readtgt3, 10+12, 512*1024)
	if !bytes.Equal(readtgt3[10:10+12], HelloWorld) {
		t.Errorf("Read content invalid: hello1")
		return
	}
	if !bytes.Equal(readtgt3[10+12+512*1024:10+12+512*1024+12], HelloWorld) {
		t.Errorf("Read content invalid: hello2")
		return
	}

	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}
}
Beispiel #3
0
func TestChunkIO_Write_Update1MB(t *testing.T) {
	td := genTestData(1024*1024 + 123)
	b := genFrameByChunkWriter(t, td)
	if b == nil {
		return
	}
	testbh := &TestBlobHandle{b}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())
	origver := cio.Header().PayloadVersion

	// Full update
	td2 := negateBits(td)
	if err := cio.PWrite(0, td2); err != nil {
		t.Errorf("failed to PWrite into ChunkIO: %v", err)
		return
	}
	if cio.Header().PayloadVersion <= origver {
		t.Errorf("PayloadVersion after PWrite < origver: %d < %d", cio.Header().PayloadVersion, origver)
	}
	origver = cio.Header().PayloadVersion
	readtgt := make([]byte, len(td))
	if err := cio.PRead(0, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, td2) {
		t.Errorf("Read content invalid")
		return
	}

	// Partial update
	if err := cio.PWrite(1012345, td[1012345:1012345+321]); err != nil {
		t.Errorf("failed to PWrite into ChunkIO: %v", err)
		return
	}
	if cio.Header().PayloadVersion <= origver {
		t.Errorf("PayloadVersion after PWrite < origver: %d < %d", cio.Header().PayloadVersion, origver)
	}
	origver = cio.Header().PayloadVersion
	td3 := make([]byte, len(td2))
	copy(td3, td2)
	copy(td3[1012345:1012345+321], td[1012345:1012345+321])
	if err := cio.PRead(0, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, td3) {
		t.Errorf("Read content invalid")
		return
	}
	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}
}
Beispiel #4
0
func TestChunkIO_Write_UpdateHello(t *testing.T) {
	b := genFrameByChunkWriter(t, HelloWorld)
	if b == nil {
		return
	}
	testbh := &TestBlobHandle{b}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())

	if cio.Header().PayloadVersion != 1 {
		t.Errorf("Initial PayloadVersion != 1")
	}

	upd := []byte("testin write")
	if err := cio.PWrite(0, upd); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}

	if cio.Header().PayloadVersion != 2 {
		t.Errorf("PayloadVersion after PWrite != 2")
	}

	readtgt := make([]byte, len(upd))
	if err := cio.PRead(0, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, upd) {
		t.Errorf("Read content invalid")
		return
	}

	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}

	queryFn := chunkstore.NewQueryChunkVersion(TestCipher())
	ver, err := queryFn(&blobstore.OffsetReader{testbh, 0})
	if err != nil {
		t.Errorf("PayloadVersion query failed")
	}
	if ver != 2 {
		t.Errorf("PayloadVersion read back after cio.Close != 2")
	}
}
Beispiel #5
0
func Test_ChunkIOWrite_NewHello_ChunkReaderRead(t *testing.T) {
	testbh := &TestBlobHandle{}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())
	if err := cio.PWrite(0, HelloWorld); err != nil {
		t.Errorf("failed to PWrite to ChunkIO: %v", err)
		return
	}
	readtgt := make([]byte, len(HelloWorld))
	if err := cio.PRead(0, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, HelloWorld) {
		t.Errorf("Read content invalid")
		return
	}
	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}

	cr, err := chunkstore.NewChunkReader(bytes.NewBuffer(testbh.Buf), TestCipher())
	if err != nil {
		t.Errorf("failed to create chunk reader: %v", err)
		return
	}
	if cr.Length() != len(HelloWorld) {
		t.Errorf("failed to recover payload len")
	}
	readtgt2 := make([]byte, len(HelloWorld))
	if _, err := io.ReadFull(cr, readtgt2); err != nil {
		t.Errorf("failed to Read from ChunkReader: %v", err)
		return
	}
	if !bytes.Equal(readtgt2, HelloWorld) {
		t.Errorf("Read content invalid")
		return
	}
}
Beispiel #6
0
func TestChunkIO_Read_HelloWorld(t *testing.T) {
	b := genFrameByChunkWriter(t, HelloWorld)
	if b == nil {
		return
	}
	testbh := &TestBlobHandle{b}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())

	readtgt := make([]byte, len(HelloWorld))
	if err := cio.PRead(0, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, HelloWorld) {
		t.Errorf("Read content invalid")
		return
	}

	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}
}
func (sio *BlobStoreDBStateSnapshotIO) RestoreSnapshot() (*inodedb.DBState, error) {
	raw, err := sio.bs.Open(metadata.INodeDBSnapshotBlobpath, fl.O_RDONLY)
	if err != nil {
		return nil, err
	}

	cio := chunkstore.NewChunkIO(raw, sio.c)
	log.Printf("serialized blob size: %d", cio.Size())
	zr, err := zlib.NewReader(&io.LimitedReader{&blobstore.OffsetReader{cio, 0}, cio.Size()})
	if err != nil {
		return nil, err
	}
	log.Printf("LoadINodeDBFromBlobStore: zlib init success!")
	dec := gob.NewDecoder(zr)

	es := []error{}
	state, err := inodedb.DecodeDBStateFromGob(dec)
	if err != nil {
		es = append(es, fmt.Errorf("Failed to decode dbstate: %v", err))
	}
	if err := zr.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close zlib Reader: %v", err))
	}
	if err := cio.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close ChunkIO: %v", err))
	}
	if err := raw.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close BlobHandle: %v", err))
	}

	if err := util.ToErrors(es); err != nil {
		return nil, err
	}
	sio.snapshotVer = state.Version()
	return state, nil
}
Beispiel #8
0
func TestChunkIO_Read_1MB(t *testing.T) {
	td := genTestData(1024*1024 + 123)
	b := genFrameByChunkWriter(t, td)
	if b == nil {
		return
	}
	testbh := &TestBlobHandle{b}
	cio := chunkstore.NewChunkIO(testbh, TestCipher())

	// Full read
	readtgt := make([]byte, len(td))
	if err := cio.PRead(0, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, td) {
		t.Errorf("Read content invalid")
		return
	}

	// Partial read
	readtgt = readtgt[:321]
	if err := cio.PRead(1012345, readtgt); err != nil {
		t.Errorf("failed to PRead from ChunkIO: %v", err)
		return
	}
	if !bytes.Equal(readtgt, td[1012345:1012345+321]) {
		t.Errorf("Read content invalid")
		return
	}

	if err := cio.Close(); err != nil {
		t.Errorf("failed to Close ChunkIO: %v", err)
		return
	}
}