Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSD class method execution #989

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/api-status.json
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,26 @@
"became_stable_version": "v0.19.0"
}
],
"preview_api": []
"preview_api": [
{
"name": "ReadOpExecStep.Bytes",
"comment": "Bytes returns the result of the executed command as a byte slice.\n",
"added_in_version": "$NEXT_RELEASE",
"expected_stable_version": "$NEXT_RELEASE_STABLE"
},
{
"name": "ReadOp.Exec",
"comment": "Exec executes an OSD class method on an object.\nSee rados_exec() in the RADOS C api documentation for a general description.\n\nImplements:\n\n\tvoid rados_read_op_exec(rados_read_op_t read_op,\n\t const char *cls,\n\t const char *method,\n\t const char *in_buf,\n\t size_t in_len,\n\t char **out_buf,\n\t size_t *out_len,\n\t int *prval);\n",
"added_in_version": "$NEXT_RELEASE",
"expected_stable_version": "$NEXT_RELEASE_STABLE"
},
{
"name": "WriteOp.Exec",
"comment": "Exec executes an OSD class method on an object.\nSee rados_exec() in the RADOS C api documentation for a general description.\n\nImplements:\n\n\tvoid rados_write_op_exec(rados_write_op_t write_op,\n\t const char *cls,\n\t const char *method,\n\t const char *in_buf,\n\t size_t in_len,\n\t int *prval)\n",
"added_in_version": "$NEXT_RELEASE",
"expected_stable_version": "$NEXT_RELEASE_STABLE"
}
]
},
"rbd": {
"deprecated_api": [
Expand Down
8 changes: 7 additions & 1 deletion docs/api-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ FSAdmin.FSQuiesce | v0.27.0 | v0.29.0 |

## Package: rados

No Preview/Deprecated APIs found. All APIs are considered stable.
### Preview APIs

Name | Added in Version | Expected Stable Version |
---- | ---------------- | ----------------------- |
ReadOpExecStep.Bytes | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |
ReadOp.Exec | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |
WriteOp.Exec | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |

## Package: rbd

Expand Down
111 changes: 111 additions & 0 deletions rados/read_op_exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//go:build ceph_preview
// +build ceph_preview

package rados

// #cgo LDFLAGS: -lrados
// #include <stdlib.h>
// #include <rados/librados.h>
//
import "C"

import (
"runtime"
"unsafe"
)

// ReadOpExecStep - step for exec operation code.
type ReadOpExecStep struct {
withoutFree

inBuffPtr *C.char
inBuffLen C.size_t
outBuffPtr *C.char
outBuffLen C.size_t
prval C.int
canReadOutput bool
}

// newExecStepOp - init new *execStepOp.
func newReadOpExecStep(in []byte) *ReadOpExecStep {
es := &ReadOpExecStep{
outBuffPtr: nil,
outBuffLen: 0,
prval: 0,
}

if len(in) > 0 {
es.inBuffPtr = (*C.char)(unsafe.Pointer(&in[0]))
es.inBuffLen = C.size_t(len(in))
}

runtime.SetFinalizer(es, func(es *ReadOpExecStep) {
if es != nil {
es.freeBuffer()
es = nil
}
})
return es
}

// freeBuffer - releases C allocated buffer. It is separated from es.free() because lifespan of C allocated buffer is
// longer than lifespan of read operation.
func (es *ReadOpExecStep) freeBuffer() {
if es.outBuffPtr != nil {
C.free(unsafe.Pointer(es.outBuffPtr))
es.outBuffPtr = nil
es.canReadOutput = false
}
}

// update - update state operation.
func (es *ReadOpExecStep) update() error {
err := getError(es.prval)
es.canReadOutput = err == nil
return err
}

// Bytes returns the result of the executed command as a byte slice.
func (es *ReadOpExecStep) Bytes() ([]byte, error) {
if !es.canReadOutput {
return nil, ErrOperationIncomplete
}

return C.GoBytes(unsafe.Pointer(es.outBuffPtr), C.int(es.outBuffLen)), nil
}

// Exec executes an OSD class method on an object.
// See rados_exec() in the RADOS C api documentation for a general description.
//
// Implements:
//
// void rados_read_op_exec(rados_read_op_t read_op,
// const char *cls,
// const char *method,
// const char *in_buf,
// size_t in_len,
// char **out_buf,
// size_t *out_len,
// int *prval);
func (r *ReadOp) Exec(clsName, method string, in []byte) *ReadOpExecStep {
cClsName := C.CString(clsName)
defer C.free(unsafe.Pointer(cClsName))

cMethod := C.CString(method)
defer C.free(unsafe.Pointer(cMethod))

es := newReadOpExecStep(in)
r.steps = append(r.steps, es)
C.rados_read_op_exec(
r.op,
cClsName,
cMethod,
es.inBuffPtr,
es.inBuffLen,
&es.outBuffPtr,
&es.outBuffLen,
&es.prval,
)

return es
}
38 changes: 38 additions & 0 deletions rados/rw_op_exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package rados

import (
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func (suite *RadosTestSuite) TestReadWriteOpExec() {
suite.SetupConnection()

oid, err := uuid.NewV4()
assert.NoError(suite.T(), err)

data := "cls data"

wrOp := CreateWriteOp()
defer wrOp.Release()
wrOp.Exec("hello", "record_hello", []byte(data))
assert.NoError(suite.T(), err)
err = wrOp.Operate(suite.ioctx, oid.String(), OperationNoFlag)
assert.NoError(suite.T(), err)

rdOp := CreateReadOp()
defer rdOp.Release()

es := rdOp.Exec("hello", "replay", nil)
result, err := es.Bytes()
require.ErrorIs(suite.T(), err, ErrOperationIncomplete)
require.Nil(suite.T(), result)

err = rdOp.Operate(suite.ioctx, oid.String(), OperationNoFlag)
assert.NoError(suite.T(), err)

result, err = es.Bytes()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), []byte("Hello, "+data+"!"), result)
}
71 changes: 71 additions & 0 deletions rados/write_op_exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build ceph_preview
// +build ceph_preview

package rados

// #cgo LDFLAGS: -lrados
// #include <stdlib.h>
// #include <rados/librados.h>
//
import "C"

import (
"unsafe"
)

// writeOpExecStep - exec step in write operation.
type writeOpExecStep struct {
withoutFree

inBuffPtr *C.char
inBuffLen C.size_t
prval C.int
}

// newWriteOpExecStep - init new *writeOpExecStep.
func newWriteOpExecStep(in []byte) *writeOpExecStep {
es := &writeOpExecStep{
prval: 0,
}
if len(in) > 0 {
es.inBuffPtr = (*C.char)(unsafe.Pointer(&in[0]))
es.inBuffLen = C.size_t(len(in))
}

return es
}

// update - update state operation.
func (es *writeOpExecStep) update() error {
return getError(es.prval)
}

// Exec executes an OSD class method on an object.
// See rados_exec() in the RADOS C api documentation for a general description.
//
// Implements:
//
// void rados_write_op_exec(rados_write_op_t write_op,
// const char *cls,
// const char *method,
// const char *in_buf,
// size_t in_len,
// int *prval)
func (w *WriteOp) Exec(clsName, method string, in []byte) {
cClsName := C.CString(clsName)
defer C.free(unsafe.Pointer(cClsName))

cMethod := C.CString(method)
defer C.free(unsafe.Pointer(cMethod))

es := newWriteOpExecStep(in)
w.steps = append(w.steps, es)
C.rados_write_op_exec(
w.op,
cClsName,
cMethod,
es.inBuffPtr,
es.inBuffLen,
&es.prval,
)
}