Skip to content

Commit

Permalink
add interface for paxosStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
JMHOO committed Nov 20, 2016
1 parent feb5d9c commit 657ca1e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 8 deletions.
Binary file not shown.
15 changes: 13 additions & 2 deletions ServerCPP/src/paxosAcceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@
#include "paxosAcceptor.h"
#include "GLog.h"
#include "jsonPaxos.h"
#include "paxosStorage.h"

namespace Paxos
{
Acceptor::Acceptor(Paxos::Instance * instance, ILog* ptrLog) : m_pInstance(instance), logger(ptrLog)
Acceptor::Acceptor(Paxos::Instance * instance, ILog* ptrLog) : m_pInstance(instance), logger(ptrLog), m_storage(ptrLog)
{

m_storage.Init();
}

bool Acceptor::Initialize()
{
uint64_t lMaxInstanceID = m_storage.GetMaxInstanceID();
if( lMaxInstanceID == -1 )
{// no records exist in file
lMaxInstanceID = 0;
}

return true;
}
void Acceptor::NewTransaction()
{

Expand Down
6 changes: 5 additions & 1 deletion ServerCPP/src/paxosAcceptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef _ADCS__paxosAcceptor_H_
#define _ADCS__paxosAcceptor_H_

#include "paxosStorage.h"

class ILog;
class IPacket;

Expand All @@ -19,7 +21,8 @@ namespace Paxos
{
public:
Acceptor(Paxos::Instance * instance, ILog* ptrLog);

bool Initialize();

void ProcessMessage(IPacket* p);

void NewTransaction();
Expand All @@ -28,6 +31,7 @@ namespace Paxos
Paxos::Instance * m_pInstance;
ILog* logger;

Storage m_storage;
};
}

Expand Down
24 changes: 24 additions & 0 deletions ServerCPP/src/paxosInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ namespace Paxos
_paxos_instance = new Instance(new GlobalLog("Paxos", LL_DEBUG));
if( _paxos_instance == nullptr )
return false;
if( !_paxos_instance->Initialize() )
{
delete _paxos_instance;
_paxos_instance = nullptr;
return false;
}
}
return true;
}
Expand Down Expand Up @@ -56,6 +62,24 @@ namespace Paxos
delete logger;
}

bool Instance::Initialize()
{
if( !acceptor.Initialize() )
{
logger->Error("Instance::Initialize, initialize acceptor failed.");
return false;
}

logger->Info("Paxos Instance, acceptor OK, now instance ID:%lu", m_ID64);

// set proposal id from acceptor state


loop.Start(true);

return true;
}

uint64_t Instance::GetInstanceID()
{
return m_ID64;
Expand Down
1 change: 1 addition & 0 deletions ServerCPP/src/paxosInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace Paxos
Instance(ILog* ptrLog);
~Instance();

bool Initialize();

uint64_t GetInstanceID();
void SetInstanceID(const uint64_t id);
Expand Down
10 changes: 9 additions & 1 deletion ServerCPP/src/paxosProposal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ namespace Paxos
void Proposal::ProcessMessage(IPacket* p)
{
jsonPaxos* pm = dynamic_cast<jsonPaxos*>(p);

PaxosType type = pm->GetMessageType();
if( type == PaxosType::PrepareResponse )
{
OnPrepareResponse(p);
}
else if( type == PaxosType::AcceptResponse)
{
OnAcceptResponse(p);
}
}

void Proposal::Prepare(bool bUseNewID)
Expand Down
19 changes: 17 additions & 2 deletions ServerCPP/src/paxosStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Paxos
return (bool)ifile;
}

std::string Storage::Seek(const int64_t instanceId)
std::string Storage::Seek(const uint64_t instanceId)
{
if(IsFileExist() == true) {
std::ifstream storage_file(file_path);
Expand All @@ -66,7 +66,7 @@ namespace Paxos
}
}

void Storage::write(const int64_t instanceId, const int64_t proposalId, const std::string value)
void Storage::write(const uint64_t instanceId, const uint64_t proposalId, const std::string value)
{
if(IsFileExist() == true) {
std::string data = std::to_string(instanceId) + " " + std::to_string(proposalId) + " " + value;
Expand All @@ -79,6 +79,21 @@ namespace Paxos
}
}

uint64_t Storage::GetMaxInstanceID()
{
return -1;
}

bool Storage::WriteState(const uint64_t promisedProposalID, const int promisedNodeID, const uint64_t acceptedProposalID, const int acceptedNodeID, const std::string value)
{
return true;
}

bool Storage::ReadState(uint64_t promisedProposalID, int& promisedNodeID, uint64_t& acceptedProposalID, int& acceptedNodeID, std::string& value)
{
return true;
}

std::string Storage::getFilePath() {
return this->file_path;
}
Expand Down
8 changes: 6 additions & 2 deletions ServerCPP/src/paxosStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ namespace Paxos
Storage(ILog* ptrLog);

void Init(); //Init() check if default storage file is exist. Create file if not exist
std::string Seek(const int64_t instanceId);
void write(const int64_t instanceId, const int64_t proposalId, const std::string value);
std::string Seek(const uint64_t instanceId);
void write(const uint64_t instanceId, const uint64_t proposalId, const std::string value);
std::string getFilePath();
uint64_t GetMaxInstanceID();

bool WriteState(const uint64_t promisedProposalID, const int promisedNodeID, const uint64_t acceptedProposalID, const int acceptedNodeID, const std::string value);
bool ReadState(uint64_t promisedProposalID, int& promisedNodeID, uint64_t& acceptedProposalID, int& acceptedNodeID, std::string& value);

private:
ILog* logger;
Expand Down

0 comments on commit 657ca1e

Please sign in to comment.