GithubHelp home page GithubHelp logo

pipashan's Introduction

Pipashan

Pipashan is a C++20 header-only decentralized consensus library that facilitates the distributed computing. Pipashan implements a QueuedPaxos which is a Paxos protocol variant that implements effectively automatic conflict resolution.

Purpose

Features:

  • Fault-tolerance
  • High performance
  • Low-resource consumption
  • Effectively automatic conflict resolution
  • High availability and scalability

Getting Started

Installation

Prepare the dependencies:

Copy the folder pipashan to the include folder, then use #include<pipashan/node.hpp> in your source code.

Concepts

Consensus is a about the process of coming to a complete agreement across several nodes in a cluster. Pipashan hides the complicated tasks of consensus and provides intuitive methods to implement consensus for distributed computing.

//cons is a pointer to a consensus instance of a node.
if(cons->propose("Pipashan"))
{
	//Reaching a consensus in the cluster.
	//The nodes which have same consensus instances in the cluster have accepted the text "Pipashan".
}
else
{
	//No nodes have accepted the text "Pipashan".
	//Some nodes maybe not received, others maybe rollback the text.
}

The pointer cons points to a consensus instance which is maintained by pipashan.

Consensus Factory

The consensus factory is assigned to the pipashan node for creating the consensus instance. The key task of consensus factory is to define an adapter.

class consensus_factory : public pipashan::paxos_factory_interface
{
private:
	bool make(pipashan::paxos& paxos) noexcept override
	{
		pipashan::adapter_type adapter;

		//Implement the adapter
		//adapter.grouping_key = ...
		//adapter.accept = ...
		//adapter.rollback = ...
		//adapter.serialize = ...
		//adapter.deserialize = ...

		//Assign the adapter and start the consensus instance
		paxos.start(adapter);
		return true;
	}

	void destroy() noexcept override
	{
		//The consensus instance is destorying, there is no resurce we should explicitly
		//delete in this example
	}
};

//Assigns the factory with a given name "consensus"
node.paxos_factory("consensus", std::make_shared<consensus_factory>());

//Create the consensus instance with the specified name "consensus" and the number of consensus
//nodes is 3.
auto cons = node.create_paxos("consensus", 3);

The pipashan node can create multiple consensus instances, but it creates only one consensus instance for a given name.

Adapter

The adapter is a bridge between the pipashan and your consensus object. For example, let's define an adapter to make a value for consensus in the cluster.

class consensus_factory : public pipashan::paxos_factory_interface
{
private:
	bool make(pipashan::paxos& paxos) noexcept override
	{
		pipashan::adapter_type adapter;

		//Implement the adapter

		//grouping_key is used for grouping proposals, the proposals in a same group are executed
		//in order. In this example, we give a named "x" group for all proposals.
		//When pipashan receives a unknown grouping_key, it automatically creates the associated
		//data for the key.
		adapter.grouping_key = [](std::string_view) {
			static std::string x{ "x" };
			return x;
		};

		//adapter.accept executes the proposal. It returns paxos_ack::done if the proposal is executed
		//successfully, it returns paxos_ack::failed otherwise.
		adapter.accept = [this](std::string_view data) {
			if ('+' == data[0]) 
				++value_;
			else
				--value_;

			return pipashan::proto::paxos_ack::done;
		};

		adapter.rollback = [](std::string_view data) {
			if('+' == data[0])
				--value_;
			else
				++value_;
			return pipashan::proto::paxos_ack::done;
		};

		//adapter.serialize is used for serializing the current state to a given file
		adapter.serialize = [this](std::filesystem::path p) {
			std::ofstream os{ p };
			os << value_;
		};

		//adapter.deserialize is used for deserializing the state from a given file, with the method,
		//the consensus instance can restore the states when fatal error occurs.
		adapter.deserialize = [this](std::filesystem::path p) {
			std::ifstream is{ p };
			is >> value_;
		};

		//Assign the adapter and start the consensus instance
		paxos.start(adapter);
		return true;
	}

	void destroy() noexcept override
	{
		//The consensus instance is destorying, there is no resurce we should explicitly
		//delete in this example
	}
private:
	std::size_t value_{ 0 }; //The object of consensus.
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.