diff --git a/docs/api-status.json b/docs/api-status.json index 9d2e48a..22ef7f9 100644 --- a/docs/api-status.json +++ b/docs/api-status.json @@ -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": [ diff --git a/docs/api-status.md b/docs/api-status.md index 21b4307..39d64ff 100644 --- a/docs/api-status.md +++ b/docs/api-status.md @@ -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 diff --git a/rados/read_op_exec.go b/rados/read_op_exec.go new file mode 100644 index 0000000..44d6d18 --- /dev/null +++ b/rados/read_op_exec.go @@ -0,0 +1,111 @@ +//go:build ceph_preview +// +build ceph_preview + +package rados + +// #cgo LDFLAGS: -lrados +// #include +// #include +// +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 +} diff --git a/rados/rw_op_exec_test.go b/rados/rw_op_exec_test.go new file mode 100644 index 0000000..47b5704 --- /dev/null +++ b/rados/rw_op_exec_test.go @@ -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) +} diff --git a/rados/write_op_exec.go b/rados/write_op_exec.go new file mode 100644 index 0000000..8f7fc07 --- /dev/null +++ b/rados/write_op_exec.go @@ -0,0 +1,71 @@ +//go:build ceph_preview +// +build ceph_preview + +package rados + +// #cgo LDFLAGS: -lrados +// #include +// #include +// +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, + ) +}