-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from cedric-pessan/master
Pzq cluster feature with replications
- Loading branch information
Showing
16 changed files
with
1,067 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.