downgrade ceph

This commit is contained in:
chessman
2019-06-12 20:03:19 +03:00
parent 48da8d6281
commit f339c5e70c
6 changed files with 129 additions and 286 deletions

View File

@@ -7,7 +7,6 @@ import "C"
import "unsafe"
import "bytes"
import "fmt"
// ClusterStat represents Ceph cluster statistics.
type ClusterStat struct {
@@ -19,8 +18,7 @@ type ClusterStat struct {
// Conn is a connection handle to a Ceph cluster.
type Conn struct {
cluster C.rados_t
connected bool
cluster C.rados_t
}
// PingMonitor sends a ping to a monitor and returns the reply.
@@ -46,18 +44,15 @@ func (c *Conn) PingMonitor(id string) (string, error) {
// if any.
func (c *Conn) Connect() error {
ret := C.rados_connect(c.cluster)
if ret != 0 {
if ret == 0 {
return nil
} else {
return RadosError(int(ret))
}
c.connected = true
return nil
}
// Shutdown disconnects from the cluster.
func (c *Conn) Shutdown() {
if err := c.ensure_connected(); err != nil {
return
}
C.rados_shutdown(c.cluster)
}
@@ -167,20 +162,9 @@ func (c *Conn) WaitForLatestOSDMap() error {
}
}
func (c *Conn) ensure_connected() error {
if c.connected {
return nil
} else {
return RadosError(1)
}
}
// GetClusterStat returns statistics about the cluster associated with the
// connection.
func (c *Conn) GetClusterStats() (stat ClusterStat, err error) {
if err := c.ensure_connected(); err != nil {
return ClusterStat{}, err
}
c_stat := C.struct_rados_cluster_stat_t{}
ret := C.rados_cluster_stat(c.cluster, &c_stat)
if ret < 0 {
@@ -266,10 +250,6 @@ func (c *Conn) MakePool(name string) error {
// DeletePool deletes a pool and all the data inside the pool.
func (c *Conn) DeletePool(name string) error {
if err := c.ensure_connected(); err != nil {
fmt.Println("NOT CONNECTED WHOOPS")
return err
}
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
ret := int(C.rados_pool_delete(c.cluster, c_name))

View File

@@ -99,13 +99,8 @@ func (ioctx *IOContext) Write(oid string, data []byte, offset uint64) error {
c_oid := C.CString(oid)
defer C.free(unsafe.Pointer(c_oid))
dataPointer := unsafe.Pointer(nil)
if len(data) > 0 {
dataPointer = unsafe.Pointer(&data[0])
}
ret := C.rados_write(ioctx.ioctx, c_oid,
(*C.char)(dataPointer),
(*C.char)(unsafe.Pointer(&data[0])),
(C.size_t)(len(data)),
(C.uint64_t)(offset))
@@ -141,18 +136,17 @@ func (ioctx *IOContext) Append(oid string, data []byte) error {
// Read reads up to len(data) bytes from the object with key oid starting at byte
// offset offset. It returns the number of bytes read and an error, if any.
func (ioctx *IOContext) Read(oid string, data []byte, offset uint64) (int, error) {
if len(data) == 0 {
return 0, nil
}
c_oid := C.CString(oid)
defer C.free(unsafe.Pointer(c_oid))
var buf *C.char
if len(data) > 0 {
buf = (*C.char)(unsafe.Pointer(&data[0]))
}
ret := C.rados_read(
ioctx.ioctx,
c_oid,
buf,
(*C.char)(unsafe.Pointer(&data[0])),
(C.size_t)(len(data)),
(C.uint64_t)(offset))
@@ -220,7 +214,7 @@ func (ioctx *IOContext) GetPoolName() (name string, err error) {
for {
ret := C.rados_ioctx_get_pool_name(ioctx.ioctx,
(*C.char)(unsafe.Pointer(&buf[0])), C.unsigned(len(buf)))
if ret == -C.ERANGE {
if ret == -34 { // FIXME
buf = make([]byte, len(buf)*2)
continue
} else if ret < 0 {
@@ -237,9 +231,7 @@ type ObjectListFunc func(oid string)
// ListObjects lists all of the objects in the pool associated with the I/O
// context, and called the provided listFn function for each object, passing
// to the function the name of the object. Call SetNamespace with
// RadosAllNamespaces before calling this function to return objects from all
// namespaces
// to the function the name of the object.
func (ioctx *IOContext) ListObjects(listFn ObjectListFunc) error {
var ctx C.rados_list_ctx_t
ret := C.rados_nobjects_list_open(ioctx.ioctx, &ctx)
@@ -251,13 +243,15 @@ func (ioctx *IOContext) ListObjects(listFn ObjectListFunc) error {
for {
var c_entry *C.char
ret := C.rados_nobjects_list_next(ctx, &c_entry, nil, nil)
if ret == -C.ENOENT {
if ret == -2 { // FIXME
return nil
} else if ret < 0 {
return GetRadosError(int(ret))
}
listFn(C.GoString(c_entry))
}
panic("invalid state")
}
// Stat returns the size of the object and its last modification time
@@ -450,13 +444,12 @@ func (ioctx *IOContext) ListOmapValues(oid string, startAfter string, filterPref
var c_iter C.rados_omap_iter_t
var c_prval C.int
C.rados_read_op_omap_get_vals2(
C.rados_read_op_omap_get_vals(
op,
c_start_after,
c_filter_prefix,
c_max_return,
&c_iter,
nil,
&c_prval,
)
@@ -587,10 +580,9 @@ func (ioctx *IOContext) CleanOmap(oid string) error {
}
type Iter struct {
ctx C.rados_list_ctx_t
err error
entry string
namespace string
ctx C.rados_list_ctx_t
err error
entry string
}
type IterToken uint32
@@ -629,13 +621,11 @@ func (iter *Iter) Seek(token IterToken) {
//
func (iter *Iter) Next() bool {
var c_entry *C.char
var c_namespace *C.char
if cerr := C.rados_nobjects_list_next(iter.ctx, &c_entry, nil, &c_namespace); cerr < 0 {
if cerr := C.rados_nobjects_list_next(iter.ctx, &c_entry, nil, nil); cerr < 0 {
iter.err = GetRadosError(int(cerr))
return false
}
iter.entry = C.GoString(c_entry)
iter.namespace = C.GoString(c_namespace)
return true
}
@@ -647,14 +637,6 @@ func (iter *Iter) Value() string {
return iter.entry
}
// Returns the namespace associated with the current value of the iterator (object name), after a successful call to Next.
func (iter *Iter) Namespace() string {
if iter.err != nil {
return ""
}
return iter.namespace
}
// Checks whether the iterator has encountered an error.
func (iter *Iter) Err() error {
if iter.err == RadosErrorNotFound {
@@ -708,9 +690,9 @@ func (ioctx *IOContext) LockExclusive(oid, name, cookie, desc string, duration t
switch ret {
case 0:
return int(ret), nil
case -C.EBUSY:
case -16: // EBUSY
return int(ret), nil
case -C.EEXIST:
case -17: // EEXIST
return int(ret), nil
default:
return int(ret), RadosError(int(ret))
@@ -759,9 +741,9 @@ func (ioctx *IOContext) LockShared(oid, name, cookie, tag, desc string, duration
switch ret {
case 0:
return int(ret), nil
case -C.EBUSY:
case -16: // EBUSY
return int(ret), nil
case -C.EEXIST:
case -17: // EEXIST
return int(ret), nil
default:
return int(ret), RadosError(int(ret))
@@ -790,7 +772,7 @@ func (ioctx *IOContext) Unlock(oid, name, cookie string) (int, error) {
switch ret {
case 0:
return int(ret), nil
case -C.ENOENT:
case -2: // -ENOENT
return int(ret), nil
default:
return int(ret), RadosError(int(ret))
@@ -880,9 +862,9 @@ func (ioctx *IOContext) BreakLock(oid, name, client, cookie string) (int, error)
switch ret {
case 0:
return int(ret), nil
case -C.ENOENT:
case -2: // -ENOENT
return int(ret), nil
case -C.EINVAL: // -EINVAL
case -22: // -EINVAL
return int(ret), nil
default:
return int(ret), RadosError(int(ret))

View File

@@ -17,8 +17,6 @@ func (e RadosError) Error() string {
return fmt.Sprintf("rados: %s", C.GoString(C.strerror(C.int(-e))))
}
var RadosAllNamespaces = C.LIBRADOS_ALL_NSPACES
var RadosErrorNotFound = RadosError(-C.ENOENT)
var RadosErrorPermissionDenied = RadosError(-C.EPERM)
@@ -37,13 +35,11 @@ func Version() (int, int, int) {
return int(c_major), int(c_minor), int(c_patch)
}
func makeConn() *Conn {
return &Conn{connected: false}
}
func newConn(user *C.char) (*Conn, error) {
conn := makeConn()
ret := C.rados_create(&conn.cluster, user)
// NewConn creates a new connection object. It returns the connection and an
// error, if any.
func NewConn() (*Conn, error) {
conn := &Conn{}
ret := C.rados_create(&conn.cluster, nil)
if ret == 0 {
return conn, nil
@@ -52,18 +48,20 @@ func newConn(user *C.char) (*Conn, error) {
}
}
// NewConn creates a new connection object. It returns the connection and an
// error, if any.
func NewConn() (*Conn, error) {
return newConn(nil)
}
// NewConnWithUser creates a new connection object with a custom username.
// It returns the connection and an error, if any.
func NewConnWithUser(user string) (*Conn, error) {
c_user := C.CString(user)
defer C.free(unsafe.Pointer(c_user))
return newConn(c_user)
conn := &Conn{}
ret := C.rados_create(&conn.cluster, c_user)
if ret == 0 {
return conn, nil
} else {
return nil, RadosError(int(ret))
}
}
// NewConnWithClusterAndUser creates a new connection object for a specific cluster and username.
@@ -75,7 +73,7 @@ func NewConnWithClusterAndUser(clusterName string, userName string) (*Conn, erro
c_name := C.CString(userName)
defer C.free(unsafe.Pointer(c_name))
conn := makeConn()
conn := &Conn{}
ret := C.rados_create2(&conn.cluster, c_cluster_name, c_name, 0)
if ret == 0 {
return conn, nil