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

fix: replace new/delete with smart pointers #1493

Merged
merged 28 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fb3d3f9
[feat] change storage new/del to smartp
iiiuwioajdks May 10, 2023
c80ecb5
Eliminated new/delete using smart ptr within src/net/.
cheniujh May 11, 2023
8eb737e
[feat] replace new/delete with smart pointers
May 12, 2023
fa652de
fix build
iiiuwioajdks May 12, 2023
c4edc27
change shared_ptr to unique_ptr
iiiuwioajdks May 12, 2023
2f3d1be
change to auto
iiiuwioajdks May 12, 2023
f2d2980
feat
iiiuwioajdks May 13, 2023
f8e637e
removed an unnecessary shared_ptr and use unique_ptr instead.
cheniujh May 14, 2023
8fbc638
Merge branch 'unstable' of https:/pika-remove-new/pika in…
cheniujh May 14, 2023
9be3a5e
change pstd/
iiiuwioajdks May 14, 2023
f097f44
Merge branch 'unstable' of github.com:pika-remove-new/pika into unstable
iiiuwioajdks May 14, 2023
4c75a1a
revised a comment
cheniujh May 14, 2023
6b4bc88
Merge remote-tracking branch 'origin/unstable' into unstable
cheniujh May 14, 2023
d3c227c
change pstd implement
iiiuwioajdks May 14, 2023
ea5f745
Merge branch 'unstable' of github.com:pika-remove-new/pika into unstable
iiiuwioajdks May 14, 2023
1b72139
remove code of jh
iiiuwioajdks May 14, 2023
cffdba0
Merge branch 'OpenAtomFoundation:unstable' into unstable
iiiuwioajdks May 14, 2023
6a5add3
fix tools build
iiiuwioajdks May 14, 2023
2eef208
Merge branch 'OpenAtomFoundation:unstable' into unstable
iiiuwioajdks May 15, 2023
3b06d6c
fix cmdtable
iiiuwioajdks May 16, 2023
0abe10a
fix
iiiuwioajdks May 16, 2023
2df4bb2
Merge branch 'OpenAtomFoundation:unstable' into unstable
iiiuwioajdks May 17, 2023
8a1ebe2
Merge branch 'OpenAtomFoundation:unstable' into unstable
iiiuwioajdks May 19, 2023
6afda63
feature
iiiuwioajdks May 19, 2023
81c3043
merge & feat
iiiuwioajdks May 19, 2023
8fc4892
fix
iiiuwioajdks May 20, 2023
74dea31
fix
iiiuwioajdks May 20, 2023
50aa21e
Merge branch 'unstable' into unstable
iiiuwioajdks May 22, 2023
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
6 changes: 3 additions & 3 deletions include/pika_binlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class Binlog {

std::atomic<bool> opened_;

Version* version_ = nullptr;
pstd::WritableFile* queue_ = nullptr;
pstd::RWFile* versionfile_ = nullptr;
std::unique_ptr<Version> version_;
std::unique_ptr<pstd::WritableFile> queue_;
std::unique_ptr<pstd::RWFile> versionfile_;

pstd::Mutex mutex_;

Expand Down
2 changes: 1 addition & 1 deletion include/pika_binlog_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PikaBinlogReader {
uint64_t last_record_offset_ = 0;

std::shared_ptr<Binlog> logger_;
pstd::SequentialFile* queue_ = nullptr;
std::unique_ptr<pstd::SequentialFile> queue_;

char* const backing_store_;
Slice buffer_;
Expand Down
2 changes: 1 addition & 1 deletion include/pika_consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Context {

private:
std::string path_;
pstd::RWFile* save_ = nullptr;
std::unique_ptr<pstd::RWFile> save_;
// No copying allowed;
Context(const Context&);
void operator=(const Context&);
Expand Down
7 changes: 4 additions & 3 deletions include/pika_partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Partition : public std::enable_shared_from_this<Partition> {
void DbRWLockReader();
void DbRWUnLock();

pstd::lock::LockMgr* LockMgr();
std::shared_ptr<pstd::lock::LockMgr> LockMgr();

void PrepareRsync();
bool TryUpdateMasterOffset();
Expand Down Expand Up @@ -97,7 +97,7 @@ class Partition : public std::enable_shared_from_this<Partition> {
bool opened_ = false;

std::shared_mutex db_rwlock_;
pstd::lock::LockMgr* lock_mgr_ = nullptr;
std::shared_ptr<pstd::lock::LockMgr> lock_mgr_;
std::shared_ptr<storage::Storage> db_;

bool full_sync_ = false;
Expand All @@ -116,7 +116,7 @@ class Partition : public std::enable_shared_from_this<Partition> {
void FinishBgsave();
BgSaveInfo bgsave_info_;
pstd::Mutex bgsave_protector_;
storage::BackupEngine* bgsave_engine_;
std::unique_ptr<storage::BackupEngine> bgsave_engine_;

// key scan info use
void InitKeyScan();
Expand All @@ -129,3 +129,4 @@ class Partition : public std::enable_shared_from_this<Partition> {
};

#endif

34 changes: 15 additions & 19 deletions src/pika_binlog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,24 @@ Binlog::Binlog(const std::string& binlog_path, const int file_size)
LOG(INFO) << "Binlog: Manifest file not exist, we create a new one.";

profile = NewFileName(filename_, pro_num_);
s = pstd::NewWritableFile(profile, &queue_);
s = pstd::NewWritableFile(profile, queue_);
if (!s.ok()) {
LOG(FATAL) << "Binlog: new " << filename_ << " " << s.ToString();
}

s = pstd::NewRWFile(manifest, &versionfile_);
s = pstd::NewRWFile(manifest, versionfile_);
if (!s.ok()) {
LOG(FATAL) << "Binlog: new versionfile error " << s.ToString();
}

version_ = new Version(versionfile_);
version_ = std::unique_ptr<Version>(new Version(versionfile_.get()));
version_->StableSave();
} else {
LOG(INFO) << "Binlog: Find the exist file.";

s = pstd::NewRWFile(manifest, &versionfile_);
s = pstd::NewRWFile(manifest, versionfile_);
if (s.ok()) {
version_ = new Version(versionfile_);
version_ = std::unique_ptr<Version>(new Version(versionfile_.get()));
iiiuwioajdks marked this conversation as resolved.
Show resolved Hide resolved
version_->Init();
pro_num_ = version_->pro_num_;

Expand All @@ -110,7 +110,7 @@ Binlog::Binlog(const std::string& binlog_path, const int file_size)

profile = NewFileName(filename_, pro_num_);
DLOG(INFO) << "Binlog: open profile " << profile;
s = pstd::AppendWritableFile(profile, &queue_, version_->pro_offset_);
s = pstd::AppendWritableFile(profile, queue_, version_->pro_offset_);
if (!s.ok()) {
LOG(FATAL) << "Binlog: Open file " << profile << " error " << s.ToString();
}
Expand All @@ -125,10 +125,6 @@ Binlog::Binlog(const std::string& binlog_path, const int file_size)
Binlog::~Binlog() {
std::lock_guard l(mutex_);
Close();
delete version_;
delete versionfile_;

delete queue_;
}

void Binlog::Close() {
Expand Down Expand Up @@ -185,15 +181,15 @@ Status Binlog::Put(const char* item, int len) {
/* Check to roll log file */
uint64_t filesize = queue_->Filesize();
if (filesize > file_size_) {
pstd::WritableFile* queue = nullptr;
std::unique_ptr<pstd::WritableFile> queue;
std::string profile = NewFileName(filename_, pro_num_ + 1);
s = pstd::NewWritableFile(profile, &queue);
s = pstd::NewWritableFile(profile, queue);
if (!s.ok()) {
LOG(ERROR) << "Binlog: new " << filename_ << " " << s.ToString();
return s;
}
delete queue_;
queue_ = queue;
queue_.reset();
queue_ = std::move(queue);
pro_num_++;

{
Expand Down Expand Up @@ -352,7 +348,7 @@ Status Binlog::SetProducerStatus(uint32_t pro_num, uint64_t pro_offset, uint32_t
pro_offset = 0;
}

delete queue_;
queue_.reset();

std::string init_profile = NewFileName(filename_, 0);
if (pstd::FileExists(init_profile)) {
Expand All @@ -364,8 +360,8 @@ Status Binlog::SetProducerStatus(uint32_t pro_num, uint64_t pro_offset, uint32_t
pstd::DeleteFile(profile);
}

pstd::NewWritableFile(profile, &queue_);
Binlog::AppendPadding(queue_, &pro_offset);
pstd::NewWritableFile(profile, queue_);
Binlog::AppendPadding(queue_.get(), &pro_offset);

pro_num_ = pro_num;

Expand All @@ -383,7 +379,7 @@ Status Binlog::SetProducerStatus(uint32_t pro_num, uint64_t pro_offset, uint32_t
}

Status Binlog::Truncate(uint32_t pro_num, uint64_t pro_offset, uint64_t index) {
delete queue_;
queue_.reset();
std::string profile = NewFileName(filename_, pro_num);
const int fd = open(profile.c_str(), O_RDWR | O_CLOEXEC, 0644);
if (fd < 0) {
Expand All @@ -403,7 +399,7 @@ Status Binlog::Truncate(uint32_t pro_num, uint64_t pro_offset, uint64_t index) {
version_->StableSave();
}

Status s = pstd::AppendWritableFile(profile, &queue_, version_->pro_offset_);
Status s = pstd::AppendWritableFile(profile, queue_, version_->pro_offset_);
if (!s.ok()) {
return s;
}
Expand Down
13 changes: 6 additions & 7 deletions src/pika_binlog_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ PikaBinlogReader::PikaBinlogReader()

PikaBinlogReader::~PikaBinlogReader() {
delete[] backing_store_;
delete queue_;
}

void PikaBinlogReader::GetReaderStatus(uint32_t* cur_filenum, uint64_t* cur_offset) {
Expand All @@ -52,15 +51,15 @@ int PikaBinlogReader::Seek(std::shared_ptr<Binlog> logger, uint32_t filenum, uin
LOG(WARNING) << confile << " not exits";
return -1;
}
pstd::SequentialFile* readfile;
if (!pstd::NewSequentialFile(confile, &readfile).ok()) {
std::unique_ptr<pstd::SequentialFile> readfile;
if (!pstd::NewSequentialFile(confile, readfile).ok()) {
LOG(WARNING) << "New swquential " << confile << " failed";
return -1;
}
if (queue_) {
delete queue_;
queue_.reset();
}
queue_ = readfile;
queue_ = std::move(readfile);
logger_ = logger;

std::lock_guard l(rwlock_);
Expand Down Expand Up @@ -251,10 +250,10 @@ Status PikaBinlogReader::Get(std::string* scratch, uint32_t* filenum, uint64_t*
// Roll to next file need retry;
if (pstd::FileExists(confile)) {
DLOG(INFO) << "BinlogSender roll to new binlog" << confile;
delete queue_;
queue_.reset();
queue_ = nullptr;

pstd::NewSequentialFile(confile, &(queue_));
pstd::NewSequentialFile(confile, queue_);
{
std::lock_guard l(rwlock_);
cur_filenum_++;
Expand Down
6 changes: 3 additions & 3 deletions src/pika_consensus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern PikaCmdTableManager* g_pika_cmd_table_manager;

Context::Context(const std::string path) : applied_index_(), path_(path), save_(nullptr) {}

Context::~Context() { delete save_; }
Context::~Context() {}
iiiuwioajdks marked this conversation as resolved.
Show resolved Hide resolved

Status Context::StableSave() {
char* p = save_->GetData();
Expand All @@ -36,13 +36,13 @@ Status Context::StableSave() {

Status Context::Init() {
if (!pstd::FileExists(path_)) {
Status s = pstd::NewRWFile(path_, &save_);
Status s = pstd::NewRWFile(path_, save_);
if (!s.ok()) {
LOG(FATAL) << "Context new file failed " << s.ToString();
}
StableSave();
} else {
Status s = pstd::NewRWFile(path_, &save_);
Status s = pstd::NewRWFile(path_, save_);
if (!s.ok()) {
LOG(FATAL) << "Context new file failed " << s.ToString();
}
Expand Down
20 changes: 6 additions & 14 deletions src/pika_meta.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ Status PikaMeta::StableSave(const std::vector<TableStruct>& table_structs) {
std::string tmp_file = local_meta_file;
tmp_file.append("_tmp");

pstd::RWFile* saver = nullptr;
std::unique_ptr<pstd::RWFile> saver;
pstd::CreatePath(local_meta_path_);
Status s = pstd::NewRWFile(tmp_file, &saver);
Status s = pstd::NewRWFile(tmp_file, saver);
if (!s.ok()) {
delete saver;
LOG(WARNING) << "Open local meta file failed";
return Status::Corruption("open local meta file failed");
}
Expand All @@ -50,7 +49,6 @@ Status PikaMeta::StableSave(const std::vector<TableStruct>& table_structs) {

std::string meta_str;
if (!meta.SerializeToString(&meta_str)) {
delete saver;
LOG(WARNING) << "Serialize meta string failed";
return Status::Corruption("serialize meta string failed");
}
Expand All @@ -62,7 +60,6 @@ Status PikaMeta::StableSave(const std::vector<TableStruct>& table_structs) {
memcpy(p, &meta_str_size, sizeof(uint32_t));
p += sizeof(uint32_t);
memcpy(p, meta_str.data(), meta_str.size());
delete saver;

pstd::DeleteFile(local_meta_file);
if (pstd::RenameFile(tmp_file, local_meta_file)) {
Expand All @@ -80,16 +77,14 @@ Status PikaMeta::ParseMeta(std::vector<TableStruct>* const table_structs) {
return Status::Corruption("meta file not found");
}

pstd::RWFile* reader = nullptr;
Status s = pstd::NewRWFile(local_meta_file, &reader);
std::unique_ptr<pstd::RWFile> reader;
Status s = pstd::NewRWFile(local_meta_file, reader);
if (!s.ok()) {
delete reader;
LOG(WARNING) << "Open local meta file failed";
return Status::Corruption("open local meta file failed");
}

if (reader->GetData() == nullptr) {
delete reader;
LOG(WARNING) << "Meta file init error";
return Status::Corruption("meta file init error");
}
Expand All @@ -98,18 +93,15 @@ Status PikaMeta::ParseMeta(std::vector<TableStruct>* const table_structs) {
uint32_t meta_size = 0;
memcpy((char*)(&version), reader->GetData(), sizeof(uint32_t));
memcpy((char*)(&meta_size), reader->GetData() + sizeof(uint32_t), sizeof(uint32_t));
char* const buf = new char[meta_size];
auto const buf_ptr = std::make_unique<char[]>(meta_size);
char* const buf = buf_ptr.get();
memcpy(buf, reader->GetData() + 2 * sizeof(uint32_t), meta_size);

InnerMessage::PikaMeta meta;
if (!meta.ParseFromArray(buf, meta_size)) {
delete[] buf;
delete reader;
LOG(WARNING) << "Parse meta string failed";
return Status::Corruption("parse meta string failed");
}
delete[] buf;
delete reader;

table_structs->clear();
for (int idx = 0; idx < meta.table_infos_size(); ++idx) {
Expand Down
11 changes: 5 additions & 6 deletions src/pika_partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Partition::Partition(const std::string& table_name, uint32_t partition_id, const
db_ = std::shared_ptr<storage::Storage>(new storage::Storage());
rocksdb::Status s = db_->Open(g_pika_server->storage_options(), db_path_);

lock_mgr_ = new pstd::lock::LockMgr(1000, 0, std::make_shared<pstd::lock::MutexFactoryImpl>());
lock_mgr_ = std::make_shared<pstd::lock::LockMgr>(1000, 0, std::make_shared<pstd::lock::MutexFactoryImpl>());

opened_ = s.ok() ? true : false;
assert(db_);
Expand All @@ -63,8 +63,6 @@ Partition::Partition(const std::string& table_name, uint32_t partition_id, const

Partition::~Partition() {
Close();
delete bgsave_engine_;
delete lock_mgr_;
}

void Partition::Leave() {
Expand Down Expand Up @@ -121,7 +119,7 @@ void Partition::DbRWLockReader() { db_rwlock_.lock_shared(); }

void Partition::DbRWUnLock() { db_rwlock_.unlock(); }

pstd::lock::LockMgr* Partition::LockMgr() { return lock_mgr_; }
std::shared_ptr<pstd::lock::LockMgr> Partition::LockMgr() { return lock_mgr_; }

void Partition::PrepareRsync() {
pstd::DeleteDirIfExist(dbsync_path_);
Expand Down Expand Up @@ -366,8 +364,8 @@ bool Partition::InitBgsaveEnv() {

// Prepare bgsave env, need bgsave_protector protect
bool Partition::InitBgsaveEngine() {
delete bgsave_engine_;
rocksdb::Status s = storage::BackupEngine::Open(db().get(), &bgsave_engine_);
bgsave_engine_ = nullptr;
rocksdb::Status s = storage::BackupEngine::Open(db().get(), bgsave_engine_);
if (!s.ok()) {
LOG(WARNING) << partition_name_ << " open backup engine failed " << s.ToString();
return false;
Expand Down Expand Up @@ -498,3 +496,4 @@ Status Partition::GetKeyNum(std::vector<storage::KeyInfo>* key_info) {
key_scan_info_.duration = time(nullptr) - key_scan_info_.start_time;
return Status::OK();
}

4 changes: 2 additions & 2 deletions src/pstd/include/base_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <stdio.h>
#include <stdlib.h>

#include <memory>
#include <string>
#include <vector>

#include "pstd/include/pstd_define.h"

namespace pstd {
Expand Down Expand Up @@ -73,7 +73,7 @@ class BaseConf {
void PushConfItem(const Rep::ConfItem& item);

private:
Rep* rep_ = nullptr;
std::unique_ptr<Rep> rep_;

/*
* No copy && no assign operator
Expand Down
Loading