Skip to content

Commit

Permalink
1, Changed user data transport protocol from dataman to thrift. 2, Re…
Browse files Browse the repository at this point in the history
…moved user property email and phone. 3, Added new user property lock status
  • Loading branch information
boshengchen committed Jun 3, 2019
1 parent e7e0424 commit d95be2f
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 188 deletions.
1 change: 0 additions & 1 deletion src/daemons/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ add_executable(
$<TARGET_OBJECTS:stats_obj>
$<TARGET_OBJECTS:process_obj>
$<TARGET_OBJECTS:ws_obj>
$<TARGET_OBJECTS:dataman_obj>
)
nebula_link_libraries(
nebula-metad
Expand Down
20 changes: 14 additions & 6 deletions src/interface/meta.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ struct UserItem {
1: string account,
2: string first_name,
3: string last_name,
4: string email,
5: string phone,
4: bool is_lock,
}

struct RoleItem {
1: string account,
2: string space,
3: RoleType RoleType,
3: RoleType role_type,
}

struct ExecResp {
Expand Down Expand Up @@ -356,22 +355,31 @@ struct GetUserReq {
}

struct GetUserResp {
1: UserItem user_item,
1: ErrorCode code,
// Valid if ret equals E_LEADER_CHANGED.
2: common.HostAddr leader,
3: UserItem user_item,
}

struct ListUsersReq {
}

struct ListUsersResp {
1: list<UserItem> users,
1: ErrorCode code,
// Valid if ret equals E_LEADER_CHANGED.
2: common.HostAddr leader,
3: list<UserItem> users,
}

struct ListRolesReq {
1: string space,
}

struct ListRolesResp {
1: list<RoleItem> roles,
1: ErrorCode code,
// Valid if ret equals E_LEADER_CHANGED.
2: common.HostAddr leader,
3: list<RoleItem> roles,
}

struct ChangePasswordReq {
Expand Down
1 change: 0 additions & 1 deletion src/meta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ add_dependencies(
kvstore_obj
thread_obj
stats_obj
dataman_obj
)

add_library(
Expand Down
71 changes: 24 additions & 47 deletions src/meta/MetaServiceUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
*/

#include "meta/MetaServiceUtils.h"
#include "dataman/RowWriter.h"
#include "dataman/RowUpdater.h"
#include "dataman/RowReader.h"
#include <thrift/lib/cpp2/protocol/Serializer.h>
#include <thrift/lib/cpp2/protocol/CompactProtocol.h>

Expand All @@ -20,8 +17,8 @@ const std::string kHostsTable = "__hosts__"; // NOLINT
const std::string kTagsTable = "__tags__"; // NOLINT
const std::string kEdgesTable = "__edges__"; // NOLINT
const std::string kIndexTable = "__index__"; // NOLINT
const std::string kUsersTable = "__user__"; // NOLINT
const std::string kRolesTable = "__role__"; // NOLINT
const std::string kUsersTable = "__users__"; // NOLINT
const std::string kRolesTable = "__roles__"; // NOLINT

std::string MetaServiceUtils::spaceKey(GraphSpaceID spaceId) {
std::string key;
Expand Down Expand Up @@ -326,21 +323,14 @@ std::string MetaServiceUtils::userKey(UserID userId) {
}

std::string MetaServiceUtils::userVal(const std::string& password,
const cpp2::UserItem& userItem,
std::shared_ptr<const SchemaProviderIf> schema) {
const cpp2::UserItem& userItem) {
auto len = password.size();
std::string val;
RowWriter writer(std::move(schema));
writer << userItem.get_account()
<< userItem.get_first_name()
<< userItem.get_last_name()
<< userItem.get_email()
<< userItem.get_phone();
std::string encoded(writer.encode());
val.reserve(sizeof(int32_t) + len + encoded.size());
std::string val, userVal;
apache::thrift::CompactSerializer::serialize(userItem, &userVal);
val.reserve(sizeof(int32_t) + len + userVal.size());
val.append(reinterpret_cast<const char*>(&len), sizeof(int32_t));
val.append(password);
val.append(encoded);
val.append(userVal);
return val;
}

Expand All @@ -349,30 +339,25 @@ folly::StringPiece MetaServiceUtils::userItemVal(folly::StringPiece rawVal) {
return rawVal.subpiece(offset, rawVal.size() - offset);
}

std::string MetaServiceUtils::replaceUserVal(const cpp2::UserItem& user, folly::StringPiece val,
std::shared_ptr<SchemaProviderIf> schema) {
auto reader = RowReader::getRowReader(MetaServiceUtils::userItemVal(val), schema);
RowUpdater updater(move(reader), schema);

std::string MetaServiceUtils::replaceUserVal(const cpp2::UserItem& user, folly::StringPiece val) {
cpp2:: UserItem oldUser;
apache::thrift::CompactSerializer::deserialize(userItemVal(val), oldUser);
if (user.__isset.first_name) {
updater.setString(GLOBAL_USER_ITEM_FIRSTNAME, user.first_name);
oldUser.set_first_name(user.get_first_name());
}
if (user.__isset.last_name) {
updater.setString(GLOBAL_USER_ITEM_LASTNAME, user.get_last_name());
oldUser.set_last_name(user.get_last_name());
}
if (user.__isset.email) {
updater.setString(GLOBAL_USER_ITEM_EMAIL, user.get_email());
if (user.__isset.is_lock) {
oldUser.set_is_lock(user.get_is_lock());
}
if (user.__isset.phone) {
updater.setString(GLOBAL_USER_ITEM_PHONE, user.get_phone());
}
auto newVal = updater.encode();
std::string newVal, userVal;
apache::thrift::CompactSerializer::serialize(oldUser, &userVal);
auto len = sizeof(int32_t) + *reinterpret_cast<const int32_t *>(val.begin());
std::string userVal;
userVal.reserve(len + newVal.size());
userVal.append(val.subpiece(0, len).str());
userVal.append(newVal);
return userVal;
newVal.reserve(len + userVal.size());
newVal.append(val.subpiece(0, len).str());
newVal.append(userVal);
return newVal;
}

std::string MetaServiceUtils::roleKey(GraphSpaceID spaceId, UserID userId) {
Expand Down Expand Up @@ -403,18 +388,10 @@ std::string MetaServiceUtils::changePassword(folly::StringPiece val, folly::Stri
return newVal;
}

cpp2::UserItem MetaServiceUtils::parseUserItem(folly::StringPiece val,
std::shared_ptr<SchemaProviderIf> schema) {
auto reader = RowReader::getRowReader(userItemVal(val), schema);
folly::StringPiece accont, first, last, email, phone;
reader->getString(GLOBAL_USER_ITEM_ACCOUNT, accont);
reader->getString(GLOBAL_USER_ITEM_FIRSTNAME, first);
reader->getString(GLOBAL_USER_ITEM_LASTNAME, last);
reader->getString(GLOBAL_USER_ITEM_EMAIL, email);
reader->getString(GLOBAL_USER_ITEM_PHONE, phone);
cpp2::UserItem userItem(apache::thrift::FragileConstructor::FRAGILE,
accont.str(), first.str(), last.str(), email.str(), phone.str());
return userItem;
cpp2::UserItem MetaServiceUtils::parseUserItem(folly::StringPiece val) {
cpp2:: UserItem user;
apache::thrift::CompactSerializer::deserialize(userItemVal(val), user);
return user;
}

std::string MetaServiceUtils::roleSpacePrefix(GraphSpaceID spaceId) {
Expand Down
17 changes: 3 additions & 14 deletions src/meta/MetaServiceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "base/Base.h"
#include "interface/gen-cpp2/meta_types.h"
#include "SchemaProviderIf.h"

namespace nebula {
namespace meta {
Expand All @@ -21,13 +20,6 @@ enum class EntryType : int8_t {
USER = 0x04,
};

#define GLOBAL_USER_SCHEMA_TAG "global_user_schema_tag"
#define GLOBAL_USER_ITEM_ACCOUNT "account"
#define GLOBAL_USER_ITEM_FIRSTNAME "first-name"
#define GLOBAL_USER_ITEM_LASTNAME "last-name"
#define GLOBAL_USER_ITEM_EMAIL "email-addr"
#define GLOBAL_USER_ITEM_PHONE "phone-num"

class MetaServiceUtils final {
public:
MetaServiceUtils() = delete;
Expand Down Expand Up @@ -101,22 +93,19 @@ class MetaServiceUtils final {
static std::string userKey(UserID userId);

static std::string userVal(const std::string& password,
const cpp2::UserItem& userItem,
std::shared_ptr<const SchemaProviderIf> schema);
const cpp2::UserItem& userItem);

static folly::StringPiece userItemVal(folly::StringPiece rawVal);

static std::string replaceUserVal(const cpp2::UserItem& user, folly::StringPiece rawVal,
std::shared_ptr<SchemaProviderIf> schema);
static std::string replaceUserVal(const cpp2::UserItem& user, folly::StringPiece rawVal);

static std::string roleKey(GraphSpaceID spaceId, UserID userId);

static std::string roleVal(cpp2::RoleType roleType);

static std::string changePassword(folly::StringPiece val, folly::StringPiece newPwd);

static cpp2::UserItem parseUserItem(folly::StringPiece val,
std::shared_ptr<SchemaProviderIf> schema);
static cpp2::UserItem parseUserItem(folly::StringPiece val);

static std::string roleSpacePrefix(GraphSpaceID spaceId);

Expand Down
4 changes: 1 addition & 3 deletions src/meta/processors/BaseProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "kvstore/KVStore.h"
#include "meta/MetaServiceUtils.h"
#include "meta/common/MetaCommon.h"
#include "meta/NebulaSchemaProvider.h"
#include "network/NetworkUtils.h"

namespace nebula {
Expand Down Expand Up @@ -98,6 +97,7 @@ class BaseProcessor {
case Status::kSpaceNotFound:
case Status::kHostNotFound:
case Status::kTagNotFound:
case Status::kUserNotFound:
return cpp2::ErrorCode::E_NOT_FOUND;
default:
return cpp2::ErrorCode::E_UNKNOWN;
Expand Down Expand Up @@ -207,8 +207,6 @@ class BaseProcessor {

StatusOr<UserID> getUserId(const std::string& account);

StatusOr<std::shared_ptr<NebulaSchemaProvider>> getUserSchema();

bool checkPassword(UserID userId, const std::string& password);

StatusOr<std::string> getUserAccount(UserID userId);
Expand Down
36 changes: 1 addition & 35 deletions src/meta/processors/BaseProcessor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -215,35 +215,6 @@ StatusOr<UserID> BaseProcessor<RESP>::getUserId(const std::string& account) {
return Status::UserNotFound(folly::stringPrintf("User %s not found", account.c_str()));
}

template<typename RESP>
StatusOr<std::shared_ptr<NebulaSchemaProvider>> BaseProcessor<RESP>::getUserSchema() {
auto ret = getTagId(0, GLOBAL_USER_SCHEMA_TAG);
if (!ret.ok()) {
return Status::TagNotFound(folly::stringPrintf("Tag %s not found", GLOBAL_USER_SCHEMA_TAG));
}

std::unique_ptr<kvstore::KVIterator> iter;
auto tagPrefix = MetaServiceUtils::schemaTagPrefix(0, ret.value());
auto code = kvstore_->prefix(kDefaultSpaceId_, kDefaultPartId_, tagPrefix, &iter);
if (code != kvstore::ResultCode::SUCCEEDED || !iter->valid()) {
return Status:: TagNotFound(folly::stringPrintf("Tag %s not found",
GLOBAL_USER_SCHEMA_TAG));
}

// Get last version of tag
auto version = MetaServiceUtils::parseTagVersion(iter->key());
auto schema = MetaServiceUtils::parseSchema(iter->val());
auto& columns = schema.get_columns();

std::shared_ptr<NebulaSchemaProvider> schemaProv(new NebulaSchemaProvider(version));
for (auto& column : columns) {
schemaProv->addField(column.get_name(),
const_cast<nebula::cpp2::ValueType &&>(column.get_type()));
}

return schemaProv;
}

template<typename RESP>
bool BaseProcessor<RESP>::checkPassword(UserID userId, const std::string& password) {
auto userKey = MetaServiceUtils::userKey(userId);
Expand All @@ -266,12 +237,7 @@ StatusOr<std::string> BaseProcessor<RESP>::getUserAccount(UserID userId) {
return Status::UserNotFound(folly::stringPrintf("User not found by id %d", userId));
}

auto schema = getUserSchema();
if (!schema.ok()) {
return Status::TagNotFound("Global user tag not found.");
}

auto user = MetaServiceUtils::parseUserItem(value, schema.value());
auto user = MetaServiceUtils::parseUserItem(value);
return user.get_account();
}

Expand Down
Loading

0 comments on commit d95be2f

Please sign in to comment.