Ejemplo n.º 1
0
// Create a view on the given GCS object generation. If rl is non-nil, it must
// contain a lease for the contents of the object and will be used when
// possible instead of re-reading the object.
//
// If the object is larger than the given chunk size, we will only read
// and cache portions of it at a time.
func NewReadProxy(
	o *gcs.Object,
	rl lease.ReadLease,
	chunkSize uint64,
	leaser lease.FileLeaser,
	bucket gcs.Bucket) (rp lease.ReadProxy) {
	// Sanity check: the read lease's size should match the object's size if it
	// is present.
	if rl != nil && uint64(rl.Size()) != o.Size {
		panic(fmt.Sprintf(
			"Read lease size %d doesn't match object size %d",
			rl.Size(),
			o.Size))
	}

	// Special case: don't bring in the complication of a multi-read proxy if we
	// have only one refresher.
	refreshers := makeRefreshers(chunkSize, o, bucket)
	if len(refreshers) == 1 {
		rp = lease.NewReadProxy(leaser, refreshers[0], rl)
	} else {
		rp = lease.NewMultiReadProxy(leaser, refreshers, rl)
	}

	return
}
Ejemplo n.º 2
0
func (t *ReadProxyTest) InitialReadLease_Valid() {
	var err error

	// Set up an initial lease.
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	t.proxy = lease.NewReadProxy(
		t.leaser,
		t.makeRefresher(),
		rl)

	// ReadAt
	ExpectCall(rl, "ReadAt")(Any(), 11).
		WillOnce(Return(0, errors.New("taco"))).
		WillOnce(Return(17, nil))

	_, err = t.proxy.ReadAt(context.Background(), []byte{}, 11)
	ExpectThat(err, Error(HasSubstr("taco")))

	n, err := t.proxy.ReadAt(context.Background(), []byte{}, 11)
	ExpectEq(17, n)

	// Upgrade
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")

	ExpectCall(rl, "Revoke")()
	ExpectCall(rl, "Upgrade")().
		WillOnce(Return(nil, errors.New("taco"))).
		WillOnce(Return(rwl, nil))

	_, err = t.proxy.Upgrade(context.Background())
	ExpectThat(err, Error(HasSubstr("taco")))

	tmp, _ := t.proxy.Upgrade(context.Background())
	ExpectEq(rwl, tmp)
}
Ejemplo n.º 3
0
func (t *ReadProxyTest) SetUp(ti *TestInfo) {
	t.mockController = ti.MockController

	// Set up the leaser.
	t.leaser = mock_lease.NewMockFileLeaser(ti.MockController, "leaser")

	// Set up the lease.
	t.proxy = lease.NewReadProxy(
		t.leaser,
		t.makeRefresher(),
		nil)
}
Ejemplo n.º 4
0
func (t *ReadProxyTest) InitialReadLease_Revoked() {
	// Set up an initial lease.
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	t.proxy = lease.NewReadProxy(
		t.leaser,
		t.makeRefresher(),
		rl)

	// Simulate it being revoked for all methods.
	ExpectCall(rl, "ReadAt")(Any(), Any()).
		WillOnce(Return(0, &lease.RevokedError{}))

	ExpectCall(rl, "Upgrade")().
		WillOnce(Return(nil, &lease.RevokedError{}))

	ExpectCall(t.leaser, "NewFile")().
		Times(2).
		WillRepeatedly(Return(nil, errors.New("")))

	t.proxy.ReadAt(context.Background(), []byte{}, 0)
	t.proxy.Upgrade(context.Background())
}