Skip to content

Commit

Permalink
fix: use reftracker pkg name instead of rt; merged v1 ops into single…
Browse files Browse the repository at this point in the history
… file

Plus some other minor changes like typos, rephrasing sentences, etc.
  • Loading branch information
gman0 committed Feb 22, 2022
1 parent 683f3df commit ed32611
Show file tree
Hide file tree
Showing 23 changed files with 812 additions and 953 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// ErrObjectOutOfDate is an error returned by RADOS read/write ops whose
// rados_*_op_assert_version failed.
var ErrObjectOutOfDate = goerrors.New("RADOS object is out of date since the last time it was read, try again later")
var ErrObjectOutOfDate = goerrors.New("object is out of date since the last time it was read, try again later")

// UnexpectedReadSize formats an error message for a failure due to bad read
// size.
Expand All @@ -36,25 +36,25 @@ func UnexpectedReadSize(expectedBytes, actualBytes int) error {
expectedBytes, actualBytes)
}

// UnknownRTObjectVersion formats an error message for a failure due to unknown
// RT object version.
func UnknownRTObjectVersion(unknownVersion uint32) error {
return fmt.Errorf("unknown RT object version %d", unknownVersion)
// UnknownObjectVersion formats an error message for a failure due to unknown
// reftracker object version.
func UnknownObjectVersion(unknownVersion uint32) error {
return fmt.Errorf("unknown reftracker version %d", unknownVersion)
}

// FailedRTObjectRead formats an error message for a failed RADOS read op.
func FailedRTObjectRead(cause error) error {
// FailedObjectRead formats an error message for a failed RADOS read op.
func FailedObjectRead(cause error) error {
if cause != nil {
return fmt.Errorf("failed to read RT object: %w", TryRADOSAborted(cause))
return fmt.Errorf("failed to read object: %w", TryRADOSAborted(cause))
}

return nil
}

// FailedRTObjectRead formats an error message for a failed RADOS read op.
func FailedRTObjectWrite(cause error) error {
// FailedObjectRead formats an error message for a failed RADOS read op.
func FailedObjectWrite(cause error) error {
if cause != nil {
return fmt.Errorf("failed to read RT object: %w", TryRADOSAborted(cause))
return fmt.Errorf("failed to write object: %w", TryRADOSAborted(cause))
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ func (w *FakeWriteOp) Operate(oid string) error {
w.IoCtx.LastObjVersion = obj.Ver
}

fmt.Printf("WRITE %+v\n", w.IoCtx.Rados.Objs)

return nil
}

Expand Down Expand Up @@ -221,8 +219,6 @@ func (r *FakeReadOp) Operate(oid string) error {
r.IoCtx.LastObjVersion = obj.Ver
}

fmt.Printf("READ %+v\n", r.IoCtx.Rados.Objs)

return nil
}

Expand Down
File renamed without changes.
64 changes: 35 additions & 29 deletions internal/util/rt/rt.go → internal/util/reftracker/reftracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,33 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package rt
package reftracker

import (
goerrors "errors"
"fmt"

"github.com/ceph/go-ceph/rados"

"github.com/ceph/ceph-csi/internal/util/rt/errors"
"github.com/ceph/ceph-csi/internal/util/rt/radoswrapper"
"github.com/ceph/ceph-csi/internal/util/rt/reftype"
v1 "github.com/ceph/ceph-csi/internal/util/rt/v1"
"github.com/ceph/ceph-csi/internal/util/rt/version"
"github.com/ceph/ceph-csi/internal/util/reftracker/errors"
"github.com/ceph/ceph-csi/internal/util/reftracker/radoswrapper"
"github.com/ceph/ceph-csi/internal/util/reftracker/reftype"
v1 "github.com/ceph/ceph-csi/internal/util/reftracker/v1"
"github.com/ceph/ceph-csi/internal/util/reftracker/version"
)

// RT, reference tracker, is key-based implementation of a reference counter.
// Unlike an integer-based counter, RT counts references by tracking unique
// keys. This allows accounting in situations where idempotency must be
// reftracker is key-based implementation of a reference counter.
//
// Unlike integer-based counter, reftracker counts references by tracking
// unique keys. This allows accounting in situations where idempotency must be
// preserved. It guarantees there will be no duplicit increments or decrements
// of the counter.
//
// It is persistently stored as a RADOS object, and is safe to use with
// It is stored persistently as a RADOS object, and is safe to use with
// multiple concurrent writers, and across different nodes of a cluster.

// Add atomically adds refs to reference tracker named `rtName`.
// If the RT object doesn't exist yet, it is created. This is also
// If the reftracker object doesn't exist yet, it is created. This is also
// reflected in the return value.
func Add(
ioctx radoswrapper.IOContextW,
Expand All @@ -50,23 +51,23 @@ func Add(
return false, err
}

// Read RT object version.
// Read reftracker version.

rtVer, err := version.Read(ioctx, rtName)
if err != nil {
if goerrors.Is(err, rados.ErrNotFound) {
// This is a new RT. Initialize it with `refs`.
// This is a new reftracker. Initialize it with `refs`.
if err = v1.Init(ioctx, rtName, refs); err != nil {
return false, fmt.Errorf("failed to initialize RT object: %w", err)
return false, fmt.Errorf("failed to initialize reftracker: %w", err)
}

return true, nil
}

return false, fmt.Errorf("failed to read RT object version: %w", err)
return false, fmt.Errorf("failed to read reftracker version: %w", err)
}

// Add references to the RT object.
// Add references to reftracker object.

gen, err := ioctx.GetLastVersion()
if err != nil {
Expand All @@ -80,16 +81,16 @@ func Add(
err = fmt.Errorf("failed to add refs: %w", err)
}
default:
err = errors.UnknownRTObjectVersion(rtVer)
err = errors.UnknownObjectVersion(rtVer)
}

return false, err
}

// Remove atomically removes refs from reference tracker named `rtName`.
// If the RT holds no references after this removal, the whole object is
// deleted too. This is also reflected in the return value. If the RT object
// doesn't exist, (true, nil) is returned.
// If the reftracker object holds no references after this removal, the whole
// object is deleted too. This is also reflected in the return value.
// If the reftracker object doesn't exist, (true, nil) is returned.
func Remove(
ioctx radoswrapper.IOContextW,
rtName string,
Expand All @@ -99,19 +100,19 @@ func Remove(
return false, err
}

// Read RT object version.
// Read reftracker version.

rtVer, err := version.Read(ioctx, rtName)
if err != nil {
if goerrors.Is(err, rados.ErrNotFound) {
// This RT object doesn't exist. Assume it was already deleted.
// This reftracker doesn't exist. Assume it was already deleted.
return true, nil
}

return false, fmt.Errorf("failed to read RT object version: %w", err)
return false, fmt.Errorf("failed to read reftracker version: %w", err)
}

// Remove references from the RT object.
// Remove references from reftracker.

gen, err := ioctx.GetLastVersion()
if err != nil {
Expand All @@ -125,31 +126,36 @@ func Remove(
err = fmt.Errorf("failed to remove refs: %w", err)
}
default:
err = errors.UnknownRTObjectVersion(rtVer)
err = errors.UnknownObjectVersion(rtVer)
}

return deleted, err
}

var (
errNoName = goerrors.New("missing reftracker name")
errNoRefs = goerrors.New("missing refs")
)

func validateAddInput(rtName string, refs map[string]struct{}) error {
if rtName == "" {
return goerrors.New("RT name is empty")
return errNoName
}

if len(refs) == 0 {
return goerrors.New("no RT refs given")
return errNoRefs
}

return nil
}

func validateRemoveInput(rtName string, refs map[string]reftype.RefType) error {
if rtName == "" {
return goerrors.New("RT name is empty")
return errNoName
}

if len(refs) == 0 {
return goerrors.New("no RT refs given")
return errNoRefs
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package rt
package reftracker

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/ceph/ceph-csi/internal/util/rt/radoswrapper"
"github.com/ceph/ceph-csi/internal/util/rt/reftype"
"github.com/ceph/ceph-csi/internal/util/reftracker/radoswrapper"
"github.com/ceph/ceph-csi/internal/util/reftracker/reftype"
)

const rtName = "hello-rt"

func TestRTAdd(t *testing.T) {
t.Parallel()

// Verify input validation for RT name.
// Verify input validation for reftracker name.
t.Run("AddNoName", func(ts *testing.T) {
ts.Parallel()

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestRTRemove(t *testing.T) {
}
})

// Attempt to remove refs in a non-existent RT object should result
// Attempt to remove refs in a non-existent reftracker object should result
// in success, with deleted=true,err=nil.
t.Run("RemoveNotExists", func(ts *testing.T) {
ts.Parallel()
Expand All @@ -128,7 +128,8 @@ func TestRTRemove(t *testing.T) {
assert.True(ts, deleted)
})

// Removing only non-existent refs should not result in RT object deletion.
// Removing only non-existent refs should not result in reftracker object
// deletion.
t.Run("RemoveNonExistentRefs", func(ts *testing.T) {
ts.Parallel()

Expand All @@ -151,7 +152,8 @@ func TestRTRemove(t *testing.T) {
assert.False(ts, deleted)
})

// Removing all refs plus some surplus should result in RT object deletion.
// Removing all refs plus some surplus should result in reftracker object
// deletion.
t.Run("RemoveNonExistentRefs", func(ts *testing.T) {
ts.Parallel()

Expand All @@ -173,7 +175,7 @@ func TestRTRemove(t *testing.T) {
assert.True(ts, deleted)
})

// Bulk removal of all refs should result in RT object deletion.
// Bulk removal of all refs should result in reftracker object deletion.
t.Run("RemoveBulk", func(ts *testing.T) {
ts.Parallel()

Expand All @@ -195,7 +197,7 @@ func TestRTRemove(t *testing.T) {
assert.True(ts, deleted)
})

// Removal of all refs one-by-one should result in RT object deletion
// Removal of all refs one-by-one should result in reftracker object deletion
// in the last Remove() call.
t.Run("RemoveSingle", func(ts *testing.T) {
ts.Parallel()
Expand All @@ -218,15 +220,15 @@ func TestRTRemove(t *testing.T) {
assert.False(ts, deleted)
}

// Remove the last reference. It should remove the whole RT object too.
// Remove the last reference. It should remove the whole reftracker object too.
deleted, err := Remove(ioctx, rtName, map[string]reftype.RefType{
"ref1": reftype.Normal,
})
assert.NoError(ts, err)
assert.True(ts, deleted)
})

// Cycle through RT object twice.
// Cycle through reftracker object twice.
t.Run("AddRemoveAddRemove", func(ts *testing.T) {
ts.Parallel()

Expand Down Expand Up @@ -291,7 +293,7 @@ func TestRTRemove(t *testing.T) {
func TestRTMask(t *testing.T) {
t.Parallel()

// Bulk masking all refs should result in RT object deletion.
// Bulk masking all refs should result in reftracker object deletion.
t.Run("MaskAllBulk", func(ts *testing.T) {
ts.Parallel()

Expand All @@ -313,8 +315,8 @@ func TestRTMask(t *testing.T) {
assert.True(ts, deleted)
})

// Masking all refs one-by-one should result in RT object deletion in the last
// Remove() call.
// Masking all refs one-by-one should result in reftracker object deletion in
// the last Remove() call.
t.Run("RemoveSingle", func(ts *testing.T) {
ts.Parallel()

Expand All @@ -336,7 +338,8 @@ func TestRTMask(t *testing.T) {
assert.False(ts, deleted)
}

// Remove the last reference. It should delete the whole RT object too.
// Remove the last reference. It should delete the whole reftracker object
// too.
deleted, err := Remove(ioctx, rtName, map[string]reftype.RefType{
"ref1": reftype.Mask,
})
Expand All @@ -345,7 +348,7 @@ func TestRTMask(t *testing.T) {
})

// Bulk removing two (out of 3) refs and then masking the ref that's left
// should result in RT object deletion in the last Remove() call.
// should result in reftracker object deletion in the last Remove() call.
t.Run("RemoveBulkMaskSingle", func(ts *testing.T) {
ts.Parallel()

Expand Down Expand Up @@ -374,7 +377,7 @@ func TestRTMask(t *testing.T) {
})

// Bulk masking two (out of 3) refs and then removing the ref that's left
// should result in RT object deletion in the last Remove() call.
// should result in reftracker object deletion in the last Remove() call.
t.Run("MaskSingleRemoveBulk", func(ts *testing.T) {
ts.Parallel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"encoding/binary"
"fmt"

"github.com/ceph/ceph-csi/internal/util/rt/errors"
"github.com/ceph/ceph-csi/internal/util/reftracker/errors"
)

// RefType describes type of the RT reference.
// RefType describes type of the reftracker reference.
type RefType int32

const (
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package v1
import (
"encoding/binary"

"github.com/ceph/ceph-csi/internal/util/rt/errors"
"github.com/ceph/ceph-csi/internal/util/reftracker/errors"
)

// Represents the number of references an RT object holds.
// Represents the number of references a reftracker object holds.
type refCount uint32

const (
Expand Down
File renamed without changes.
Loading

0 comments on commit ed32611

Please sign in to comment.