Skip to content

Commit

Permalink
Merge pull request #3 from cedric-pessan/master
Browse files Browse the repository at this point in the history
Pzq cluster feature with replications
  • Loading branch information
mkoppanen committed Jul 15, 2014
2 parents 96830e9 + d854323 commit 39aa9f6
Show file tree
Hide file tree
Showing 16 changed files with 1,067 additions and 133 deletions.
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ ELSEIF (WIN32)
SET(Boost_USE_STATIC_RUNTIME OFF)
ENDIF ()

FIND_PACKAGE(Boost COMPONENTS program_options thread REQUIRED)
FIND_PACKAGE(Boost COMPONENTS program_options thread system REQUIRED)
FIND_PACKAGE(ZeroMQ REQUIRED)
FIND_PACKAGE(kyotocabinet REQUIRED)
FIND_PACKAGE(libuuid REQUIRED)
FIND_PACKAGE(Libuuid REQUIRED)

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${ZeroMQ_INCLUDE_DIRS})
Expand All @@ -32,13 +32,20 @@ LINK_DIRECTORIES(${ZeroMQ_LIBRARY_DIRS})
LINK_DIRECTORIES(${kyotocabinet_LIBRARY_DIRS})
LINK_DIRECTORIES(${libuuid_LIBRARY_DIRS})

ADD_EXECUTABLE(${MODULE_NAME} src/main.cpp src/manager.cpp src/store.cpp src/visitor.cpp src/reaper.cpp)
ADD_EXECUTABLE(${MODULE_NAME} src/main.cpp
src/manager.cpp
src/store.cpp
src/visitor.cpp
src/reaper.cpp
src/cluster.cpp
src/ackcache.cpp)
TARGET_LINK_LIBRARIES(${MODULE_NAME} ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(${MODULE_NAME} ${ZeroMQ_LIBRARIES})
TARGET_LINK_LIBRARIES(${MODULE_NAME} ${kyotocabinet_LIBRARIES})
TARGET_LINK_LIBRARIES(${MODULE_NAME} ${libuuid_LIBRARIES})

IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
TARGET_LINK_LIBRARIES(${MODULE_NAME} uuid)
TARGET_LINK_LIBRARIES(${MODULE_NAME} pthread)
ENDIF()

106 changes: 69 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pzq
===

A very simple store and forward device using ZeroMQ.
A very simple store and forward device using ZeroMQ. It has some
optional cluster capabilities in order to provide high availability of
all accepted messages.

This version is only compatible with zeromq version 3.2 and above.

Building
========
Expand Down Expand Up @@ -31,6 +35,21 @@ Options
communication socket
--monitor-dsn arg (=ipc:///tmp/pzq-monitor)
The DSN for the monitoring socket
--replicas arg (=0) Number of replicas that should
created before acknowledging message to
producer
--nodes arg List of DSN for other cluster nodes
separated by a ','
--timeout-nodes arg (=10000000) How long to wait before considering a
node down (microseconds)
--broadcast-dsn arg DSN used by other cluster nodes to
broadcast msessages.
Broardcast port
should be the same for all nodes
--timeout_replication arg (=100000) How long to wait for replication before
acknowledging producer with a
replication error message


Consistency
Expand Down Expand Up @@ -68,54 +87,67 @@ be added to the messages.

- Producing a message

+--------------------+
| message id |
+--------------------+
| 0 size part |
+--------------------+
| 1..N message parts |
+--------------------+
```
+--------------------+
| message id |
+--------------------+
| 0 size part |
+--------------------+
| 1..N message parts |
+--------------------+
```

- Producer ACK message

+---------------------+
| message id |
+---------------------+
| status code 0/1 |
+---------------------+
| 0 size part |
+---------------------+
| 0..1 status message |
+---------------------+
```
+---------------------+
| message id |
+---------------------+
| status code 0/1 |
+---------------------+
| 0 size part |
+---------------------+
| 0..1 status message |
+---------------------+
```

*Note*: Status code 1 for success and 0 for failure.
Status message is set in case of failure.

*Note*: In the case of a message successully stored in queue but all
replications could not be created before timeout_replication,
the status will be 1 but the status message will contain
REPLICATION_FAILED

- Consumer message

+---------------------+
| peer id |
+---------------------+
| message id |
+---------------------+
| sent time |
+---------------------+
| ack timeout |
+---------------------+
| 0 size part |
+---------------------+
| 1..N message parts |
+---------------------+
```
+---------------------+
| peer id |
+---------------------+
| message id |
+---------------------+
| sent time |
+---------------------+
| ack timeout |
+---------------------+
| 0 size part |
+---------------------+
| 1..N message parts |
+---------------------+
```

- Consumer ACK message

+---------------------+
| peer id |
+---------------------+
| message id |
+---------------------+
| status code 0/1 |
+---------------------+
```
+---------------------+
| peer id |
+---------------------+
| message id |
+---------------------+
| status code 0/1 |
+---------------------+
```

*Note*: Status code 1 for success and 0 for failure.

Expand Down
109 changes: 109 additions & 0 deletions src/ackcache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2014 Cédric Pessan <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ackcache.hpp"
#include "time.hpp"

using std::string;

namespace pzq
{
ackcache_t::ackcache_t( uint64_t timeoutReplication )
{
pzq::log( "Initializing ack cache" );
m_timeoutReplication = timeoutReplication;
}

ackcache_t::~ackcache_t()
{
}

void ackcache_t::push( const std::string& idMsg, pzq::message_t ack, int replicas )
{
m_cache.insert( ack_t( idMsg, ack, microsecond_timestamp() + m_timeoutReplication, replicas ) );
}

pzq::message_t ackcache_t::getAndRemoveById( string id )
{
pzq::message_t msg;

cache_t::nth_index< 0 >::type& index = m_cache.get< 0 >();
cache_t::nth_index< 0 >::type::iterator it = index.find( id );
if( it != index.end() )
{
it->decrementReplicas();
if(it->getReplicas() == 0 )
{
msg = it->getAck();
index.erase( it );
}
}

return msg;
}

int ackcache_t::getDelayUntilNextAck() const
{
if( m_cache.size() == 0) return std::numeric_limits< int >::max();

const cache_t::nth_index< 1 >::type& index = m_cache.get< 1 >();
return ( (int64_t)microsecond_timestamp() - (int64_t)index.begin()->m_ts ) / 1000;
}

pzq::message_t ackcache_t::pop()
{
if( m_cache.size() == 0) return pzq::message_t();

cache_t::nth_index< 1 >::type& index = m_cache.get< 1 >();
pzq::message_t msg = index.begin()->getAck();
index.erase( index.begin() );
return msg;
}

ackcache_t::ack_t::ack_t( const string& idmsg, pzq::message_t ack, uint64_t ts, int replicas )
{
m_idmsg = idmsg;
m_ts= ts;
m_ack = ack;
m_replicas = shared_ptr< int >( new int );
*m_replicas = replicas;
}

ackcache_t::ack_t::~ack_t()
{
}

ackcache_t::ack_t::ack_t( const ack_t& ack )
{
m_idmsg = ack.m_idmsg;
m_ts = ack.m_ts;
}

pzq::message_t ackcache_t::ack_t::getAck() const
{
return m_ack;
}

void ackcache_t::ack_t::decrementReplicas() const
{
(*m_replicas)--;
}

int ackcache_t::ack_t::getReplicas() const
{
return *m_replicas;
}
}
85 changes: 85 additions & 0 deletions src/ackcache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2014 Cédric Pessan <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef PZQ_ACKCACHE_HPP
#define PZQ_ACKCACHE_HPP

#include "pzq.hpp"

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>

using namespace ::boost;
using namespace ::boost::multi_index;

namespace pzq
{
class ackcache_t
{
public:
ackcache_t( uint64_t timeoutReplication );
~ackcache_t();

void push( const std::string& idMsg, pzq::message_t ack, int replicas );

pzq::message_t getAndRemoveById( std::string id );

int getDelayUntilNextAck() const;

pzq::message_t pop();

class ack_t
{
public:
ack_t( const std::string& idMsg, pzq::message_t ack, uint64_t ts, int replicas );
ack_t( const ack_t& );
~ack_t();

pzq::message_t getAck() const;
void decrementReplicas() const;
int getReplicas() const;

private:
ack_t();
ack_t& operator=( const ack_t& );

pzq::message_t m_ack;

public:
std::string m_idmsg;
uint64_t m_ts;
shared_ptr< int > m_replicas;
};

typedef multi_index_container<
ack_t,
indexed_by<
hashed_unique< member< ack_t, std::string, &ack_t::m_idmsg > >,
ordered_unique< member< ack_t, uint64_t, &ack_t::m_ts > > > > cache_t;

private:
ackcache_t();
ackcache_t( const ackcache_t& );
ackcache_t& operator=( const ackcache_t& );

cache_t m_cache;
uint64_t m_timeoutReplication;
};
}

#endif // PZQ_ACKCACHE_HPP
Loading

0 comments on commit 39aa9f6

Please sign in to comment.