Skip to content

Commit

Permalink
support multi-client with python interface
Browse files Browse the repository at this point in the history
Change-Id: I468a7b4c6d1503afc56532ab36677b9604de8d27
  • Loading branch information
wu-hanqing committed Jun 1, 2020
1 parent 250f3c4 commit 2653d69
Show file tree
Hide file tree
Showing 17 changed files with 1,537 additions and 145 deletions.
5 changes: 3 additions & 2 deletions curvefs_python/BUILD_bak
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ COPTS = [

cc_library(
name = "curvefs",
srcs = glob(["*.cxx"]),
srcs = glob(["*.cxx", "*.cpp"]),
hdrs = glob(["*h"]),
deps = [
"//external:gflags",
Expand All @@ -37,7 +37,7 @@ cc_library(
"//proto:nameserver2_cc_proto",
"//proto:topology_cc_proto",
"//proto:chunkserver-cc-protos",
"//src/client:curve_client"
"//src/client:curve_client"
],
copts = COPTS,
linkopts = [
Expand Down Expand Up @@ -75,6 +75,7 @@ cc_library(
"-lunwind",
"-lstdc++",
"-lm",
"-Wl,-rpath=$$ORIGIN",
],
visibility = ["//visibility:public"],
)
3 changes: 3 additions & 0 deletions curvefs_python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# coding: utf-8

from curvefs import *
143 changes: 143 additions & 0 deletions curvefs_python/cbd_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Project: curve
* Date: Fri May 15 15:46:59 CST 2020
* Author: wuhanqing
* Copyright (c) 2020 NetEase
*/

#include "curvefs_python/cbd_client.h"

#include <vector>

#include "src/client/libcurve_file.h"

inline curve::client::UserInfo ToCurveClientUserInfo(UserInfo_t* userInfo) {
return curve::client::UserInfo(userInfo->owner, userInfo->password);
}

CBDClient::CBDClient() : client_(new curve::client::FileClient()) {}

CBDClient::~CBDClient() {}

int CBDClient::Init(const char* configPath) {
return client_->Init(configPath);
}

void CBDClient::UnInit() {
return client_->UnInit();
}

int CBDClient::Open(const char* filename, UserInfo_t* userInfo) {
return client_->Open(filename, ToCurveClientUserInfo(userInfo));
}

int CBDClient::Close(int fd) {
return client_->Close(fd);
}

int CBDClient::Create(const char* filename, UserInfo_t* userInfo, size_t size) {
return client_->Create(filename, ToCurveClientUserInfo(userInfo), size);
}

int CBDClient::Unlink(const char* filename, UserInfo_t* userInfo) {
return client_->Unlink(filename, ToCurveClientUserInfo(userInfo));
}

int CBDClient::DeleteForce(const char* filename, UserInfo_t* userInfo) {
return client_->Unlink(filename, ToCurveClientUserInfo(userInfo), true);
}

int CBDClient::Rename(UserInfo_t* userInfo, const char* oldPath,
const char* newPath) {
return client_->Rename(ToCurveClientUserInfo(userInfo), oldPath, newPath);
}

int CBDClient::Extend(const char* filename, UserInfo_t* userInfo,
uint64_t size) {
return client_->Extend(filename, ToCurveClientUserInfo(userInfo), size);
}

int CBDClient::Read(int fd, char* buf, unsigned long offset, unsigned long length) { // NOLINT
return client_->Read(fd, buf, offset, length);
}

int CBDClient::Write(int fd, const char* buf, unsigned long offset, unsigned long length) { // NOLINT
return client_->Write(fd, buf, offset, length);
}

int CBDClient::AioRead(int fd, AioContext* aioctx) {
return client_->AioRead(fd, reinterpret_cast<CurveAioContext*>(aioctx));
}

int CBDClient::AioWrite(int fd, AioContext* aioctx) {
return client_->AioWrite(fd, reinterpret_cast<CurveAioContext*>(aioctx));
}

int CBDClient::StatFile(const char* filename, UserInfo_t* userInfo,
FileInfo_t* finfo) {
return client_->StatFile(filename, ToCurveClientUserInfo(userInfo),
reinterpret_cast<FileStatInfo*>(finfo));
}

int CBDClient::ChangeOwner(const char* filename, const char* newOwner,
UserInfo_t* userInfo) {
return client_->ChangeOwner(filename, newOwner,
ToCurveClientUserInfo(userInfo));
}

DirInfos_t* CBDClient::OpenDir(const char* dirpath, UserInfo_t* userInfo) {
DirInfos_t* dirinfo = new (std::nothrow) DirInfos_t();
dirinfo->dirpath = const_cast<char*>(dirpath);
dirinfo->userinfo = userInfo;
dirinfo->fileinfo = nullptr;

return dirinfo;
}

int CBDClient::Listdir(DirInfos_t* dirinfo) {
std::vector<FileStatInfo> fileInfos;
int ret = client_->Listdir(
dirinfo->dirpath, ToCurveClientUserInfo(dirinfo->userinfo), &fileInfos);

if (ret != LIBCURVE_ERROR::OK) {
return ret;
}

dirinfo->dirsize = fileInfos.size();
dirinfo->fileinfo = new FileInfo[dirinfo->dirsize];

for (uint64_t i = 0; i < dirinfo->dirsize; ++i) {
dirinfo->fileinfo[i].id = fileInfos[i].id;
dirinfo->fileinfo[i].parentid = fileInfos[i].parentid;
dirinfo->fileinfo[i].filetype = fileInfos[i].filetype;
dirinfo->fileinfo[i].length = fileInfos[i].length;
dirinfo->fileinfo[i].ctime = fileInfos[i].ctime;

memcpy(dirinfo->fileinfo[i].owner, fileInfos[i].owner, NAME_MAX_SIZE);
memcpy(dirinfo->fileinfo[i].filename, fileInfos[i].filename,
NAME_MAX_SIZE);
}

return ret;
}

void CBDClient::CloseDir(DirInfos_t* dirinfo) {
if (dirinfo->fileinfo != nullptr) {
delete[] dirinfo->fileinfo;
dirinfo->fileinfo = nullptr;
}

delete dirinfo;
}

int CBDClient::Mkdir(const char* dirpath, UserInfo_t* userInfo) {
return client_->Mkdir(dirpath, ToCurveClientUserInfo(userInfo));
}

int CBDClient::Rmdir(const char* dirpath, UserInfo_t* userInfo) {
return client_->Rmdir(dirpath, ToCurveClientUserInfo(userInfo));
}

std::string CBDClient::GetClusterId() {
return client_->GetClusterId();
}
65 changes: 65 additions & 0 deletions curvefs_python/cbd_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Project: curve
* Date: Fri May 15 15:46:59 CST 2020
* Author: wuhanqing
* Copyright (c) 2020 NetEase
*/

#ifndef CURVEFS_PYTHON_CBD_CLIENT_H_
#define CURVEFS_PYTHON_CBD_CLIENT_H_

#include <memory>
#include <string>

#include "curvefs_python/curve_type.h"

namespace curve {
namespace client {

class FileClient;

} // namespace client
} // namespace curve

class CBDClient {
public:
CBDClient();
~CBDClient();

int Init(const char* configPath);
void UnInit();

int Open(const char* filename, UserInfo_t* userInfo);
int Close(int fd);

int Create(const char* filename, UserInfo_t* userInfo, size_t size);
int Unlink(const char* filename, UserInfo_t* info);
int DeleteForce(const char* filename, UserInfo_t* info);
int Rename(UserInfo_t* info, const char* oldpath, const char* newpath);
int Extend(const char* filename, UserInfo_t* info, uint64_t size);

// 同步读写
int Read(int fd, char* buf, unsigned long offset, unsigned long length); // NOLINT
int Write(int fd, const char* buf, unsigned long offset, unsigned long length); // NOLINT

// 异步读写
int AioRead(int fd, AioContext* aioctx);
int AioWrite(int fd, AioContext* aioctx);

// 获取文件的基本信息
int StatFile(const char* filename, UserInfo_t* info, FileInfo_t* finfo);
int ChangeOwner(const char* filename, const char* owner, UserInfo_t* info);

DirInfos_t* OpenDir(const char* dirpath, UserInfo_t* userinfo);
int Listdir(DirInfos_t* dirinfo);
void CloseDir(DirInfos_t* dirinfo);
int Mkdir(const char* dirpath, UserInfo_t* info);
int Rmdir(const char* dirpath, UserInfo_t* info);

std::string GetClusterId();

private:
std::unique_ptr<curve::client::FileClient> client_;
};

#endif // CURVEFS_PYTHON_CBD_CLIENT_H_
120 changes: 120 additions & 0 deletions curvefs_python/curve_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* Project: curve
* Date: Fri May 15 15:46:59 CST 2020
* Author: wuhanqing
* Copyright (c) 2020 NetEase
*/

#ifndef CURVEFS_PYTHON_CURVE_TYPE_H_
#define CURVEFS_PYTHON_CURVE_TYPE_H_

#include <stddef.h>
#include <stdint.h>
#include <unistd.h>

#define CURVE_INODE_DIRECTORY 0
#define CURVE_INODE_PAGEFILE 1
#define CURVEINODE_APPENDFILE 2
#define CURVE_INODE_APPENDECFILE 3

#define CURVE_ERROR_OK 0
// 文件或者目录已存在
#define CURVE_ERROR_EXISTS 1
// 操作失败
#define CURVE_ERROR_FAILED 2
// 禁止IO
#define CURVE_ERROR_DISABLEIO 3
// 认证失败
#define CURVE_ERROR_AUTHFAIL 4
// 正在删除
#define CURVE_ERROR_DELETING 5
// 文件不存在
#define CURVE_ERROR_NOTEXIST 6
// 快照中
#define CURVE_ERROR_UNDER_SNAPSHOT 7
// 非快照期间
#define CURVE_ERROR_NOT_UNDERSNAPSHOT 8
// 删除错误
#define CURVE_ERROR_DELETE_ERROR 9
// segment未分配
#define CURVE_ERROR_NOT_ALLOCATE 10
// 操作不支持
#define CURVE_ERROR_NOT_SUPPORT 11
// 目录非空
#define CURVE_ERROR_NOT_EMPTY 12
// 禁止缩容
#define CURVE_ERROR_NO_SHRINK_BIGGER_FILE 13
// session不存在
#define CURVE_ERROR_SESSION_NOTEXISTS 14
// 文件被占用
#define CURVE_ERROR_FILE_OCCUPIED 15
// 参数错误
#define CURVE_ERROR_PARAM_ERROR 16
// MDS一侧存储错误
#define CURVE_ERROR_INTERNAL_ERROR 17
// crc检查错误
#define CURVE_ERROR_CRC_ERROR 18
// request参数存在问题
#define CURVE_ERROR_INVALID_REQUEST 19
// 磁盘存在问题
#define CURVE_ERROR_DISK_FAIL 20
// 空间不足
#define CURVE_ERROR_NO_SPACE 21
// IO未对齐
#define CURVE_ERROR_NOT_ALIGNED 22
// 文件被关闭,fd不可用
#define CURVE_ERROR_BAD_FD 23
// 文件长度不支持
#define CURVE_ERROR_LENGTH_NOT_SUPPORT 24

// 文件状态
#define CURVE_FILE_CREATED 0
#define CURVE_FILE_DELETING 1
#define CURVE_FILE_CLONING 2
#define CURVE_FILE_CLONEMETAINSTALLED 3
#define CURVE_FILE_CLONED 4
#define CURVE_FILE_BEINGCLONED 5

// 未知错误
#define CURVE_ERROR_UNKNOWN 100

#define CURVE_OP_READ 0
#define CURVE_OP_WRITE 1

#define CLUSTERIDMAX 256


typedef void (*AioCallBack)(struct AioContext* context);
typedef struct AioContext {
unsigned long offset; //NOLINT
unsigned long length; //NOLINT
int ret;
int op;
AioCallBack cb;
void* buf;
} AioContext_t;

typedef struct UserInfo {
char owner[256];
char password[256];
} UserInfo_t;

typedef struct FileInfo {
uint64_t id;
uint64_t parentid;
int filetype;
uint64_t length;
uint64_t ctime;
char filename[256];
char owner[256];
int fileStatus;
} FileInfo_t;

typedef struct DirInfos {
char* dirpath;
UserInfo_t* userinfo;
uint64_t dirsize;
FileInfo_t* fileinfo;
} DirInfos_t;

#endif // CURVEFS_PYTHON_CURVE_TYPE_H_
5 changes: 5 additions & 0 deletions curvefs_python/curvefs.i
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
%module curvefs
%{
#include <stdint.h>
#include "curve_type.h"
#include "libcurvefs.h"
#include "cbd_client.h"
%}

%include <stdint.i>
%include <std_string.i>
%include "curve_type.h"
%include "libcurvefs.h"
%include "cbd_client.h"
Loading

0 comments on commit 2653d69

Please sign in to comment.