コード例 #1
0
int main ()
{
   std::atomic <uint16_t> response_count (0);

   paxos::server::callback_type callback =
      [& response_count](int64_t, std::string const & workload) -> std::string
      {
         ++response_count;

         return "bar";
      };

   paxos::configuration configuration;
   configuration.set_strategy_factory (new test_strategy_factory (configuration.durable_storage ()));

   paxos::server server1 ("127.0.0.1", 1337, callback);
   paxos::server server2 ("127.0.0.1", 1338, callback);
   paxos::server server3 ("127.0.0.1", 1339, callback, configuration);
   paxos::client client;

   server1.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
   server2.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
   server3.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
   client.add  ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});

   /*!
     This would be retried and shouldn't cause any troubles.
    */
   PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
   PAXOS_ASSERT_EQ (response_count, 2);

   PAXOS_INFO ("test succeeded");   
}
コード例 #2
0
ファイル: heap.cpp プロジェクト: HanumathRao/libpaxos-cpp
void
heap::remove (
   int64_t              proposal_id)
{
   PAXOS_ASSERT (proposal_id >= 0);

   if (data_.find (proposal_id) == data_.end ())
   {
      /*!
        This is weird, likely caused by some manual copying / fiddling with the data
        backend. Only sensible thing to do is to ignore this remove command.
       */
      PAXOS_WARN ("proposal_id " << proposal_id << " not found in history, ignoring remove!");
      return;
   }

   PAXOS_DEBUG (this << " deleting all data since " << proposal_id);

   data_.erase (data_.begin (),
                data_.find (proposal_id));
   data_.erase (proposal_id);

   PAXOS_ASSERT_EQ (lowest_proposal_id (), proposal_id + 1);

}
コード例 #3
0
ファイル: heap.cpp プロジェクト: HanumathRao/libpaxos-cpp
/*! virtual */ void
heap::store (
   int64_t                   proposal_id,
   std::string const &       byte_array)
{
   PAXOS_DEBUG (this << " proposal_id = " << proposal_id << ", highest_proposal_id = " << highest_proposal_id ());

   PAXOS_ASSERT_EQ (
      proposal_id, highest_proposal_id () + 1);

   data_[proposal_id] = byte_array;
}
コード例 #4
0
int main ()
{
   paxos::configuration configuration1;
   paxos::configuration configuration2;
   paxos::configuration configuration3;

   configuration1.durable_storage ().set_history_size (5);
   configuration2.durable_storage ().set_history_size (5);
   configuration3.durable_storage ().set_history_size (5);

   paxos::server::callback_type callback = 
      [](
         int64_t                promise_id,
         std::string const &    workload) -> std::string
      {
         return "bar";
      };

   paxos::client client;
   client.add  ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});

   paxos::server server1 ("127.0.0.1", 1337, callback, configuration1);
   paxos::server server2 ("127.0.0.1", 1338, callback, configuration2);
   
   server1.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
   server2.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});

   {
      paxos::server server3 ("127.0.0.1", 1339, callback, configuration3);
      server3.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
    
      /*!
        Issue 10 calls, so we can verify that the lowest proposal id is in fact 10.
       */
      for (size_t i = 0; i < 15; ++i)
      {
         PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
      }

      PAXOS_ASSERT_EQ (configuration1.durable_storage ().lowest_proposal_id (), 11);
      PAXOS_ASSERT_EQ (configuration2.durable_storage ().lowest_proposal_id (), 11);
      PAXOS_ASSERT_EQ (configuration3.durable_storage ().lowest_proposal_id (), 11);
   }
   
   /*!
     Now, since one host in our quorum is currently down, logs should *not* be cleaned
     up further than the proposal that has been previously accepted by that host.
    */

   for (size_t i = 0; i < 30; ++i)
   {
      PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
   }

   PAXOS_ASSERT_GE (configuration1.durable_storage ().lowest_proposal_id (), 15);
   PAXOS_ASSERT_GE (configuration2.durable_storage ().lowest_proposal_id (), 15);
   PAXOS_ASSERT_EQ (configuration3.durable_storage ().lowest_proposal_id (), 11);

   boost::this_thread::sleep (
      boost::posix_time::milliseconds (
         paxos::configuration ().timeout ()));

   {
      /*!
        And after our server is back online, the history can be cleared again.
       */

      paxos::server server3 ("127.0.0.1", 1339, callback, configuration3);
      server3.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
    
      for (size_t i = 0; i < 15; ++i)
      {
         PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
      }

      PAXOS_ASSERT_EQ (configuration1.durable_storage ().lowest_proposal_id (), 56);
      PAXOS_ASSERT_EQ (configuration2.durable_storage ().lowest_proposal_id (), 56);
      PAXOS_ASSERT_EQ (configuration3.durable_storage ().lowest_proposal_id (), 56);
   }


   
   PAXOS_INFO ("test succeeded");
}
コード例 #5
0
ファイル: durability2.cpp プロジェクト: xjump/c_cxx_repo
int main ()
{
   std::map <int64_t, uint16_t> responses;

   /*!
     Synchronizes access to responses
    */
   boost::mutex mutex;

   paxos::configuration configuration1;
   paxos::configuration configuration2;
   paxos::configuration configuration3;

   /*!
     Note that the configuration objects below outlive the server objects declared later in
     the test, thus providing semi-durable storage.
    */
   configuration1.set_durable_storage (
      new paxos::durable::heap ());
   configuration2.set_durable_storage (
      new paxos::durable::heap ());
   configuration3.set_durable_storage (
      new paxos::durable::heap ());

   paxos::server::callback_type callback = 
      [& responses,
       & mutex](
         int64_t                promise_id,
         std::string const &    workload) -> std::string
      {
         boost::mutex::scoped_lock lock (mutex);

         if (responses.find (promise_id) == responses.end ())
         {
            responses[promise_id] = 1;
         }
         else
         {
            responses[promise_id]++;
         }

         PAXOS_ASSERT (responses[promise_id] <= 3);

         return "bar";
      };

   paxos::client client;
   client.add  ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});

   {
      paxos::server server1 ("127.0.0.1", 1337, callback, configuration1);

      server1.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});

      {
         paxos::server server2 ("127.0.0.1", 1338, callback, configuration2);

         server2.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});

         {
            paxos::server server3 ("127.0.0.1", 1339, callback, configuration3);

            server3.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});

            PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
            PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
            PAXOS_ASSERT_EQ (all_responses_equal (responses, 3), true);
         }



         PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
         PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
         PAXOS_ASSERT_EQ (all_responses_equal (responses, 3), false);
      }

      PAXOS_ASSERT_THROW (client.send ("foo").get (), paxos::exception::no_majority);
   }

   /*!
     Note that we're re-adding servers in reverse order here; this is to ensure that
     server3 doesn't become our leader while it's lagging behind.
    */
   paxos::server server3 ("127.0.0.1", 1339, callback, configuration3);
   server3.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
   
   PAXOS_ASSERT_THROW (client.send ("foo").get (), paxos::exception::no_majority);

   paxos::server server2 ("127.0.0.1", 1338, callback, configuration2);
   server2.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});
   boost::this_thread::sleep (
      boost::posix_time::milliseconds (
         paxos::configuration ().timeout ()));

   PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");
   PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");

   paxos::server server1 ("127.0.0.1", 1337, callback, configuration1);
   server1.add ({{"127.0.0.1", 1337}, {"127.0.0.1", 1338}, {"127.0.0.1", 1339}});


   boost::this_thread::sleep (
      boost::posix_time::milliseconds (
         paxos::configuration ().timeout ()));

   do
   {
      PAXOS_ASSERT_EQ (client.send ("foo").get (), "bar");   
      
   } while (all_responses_equal (responses, 3) == false);

   PAXOS_INFO ("test succeeded");
}