From 15a3e05144785bfcaddcf2d11b6c549edd9f62ee Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 12 Feb 2012 12:29:56 +0000 Subject: Add experimental "TCP Nice" mode, disabled by default. --- bin/bbackupd/BackupClientContext.cpp | 118 +++++++++++++-------------- bin/bbackupd/BackupClientContext.h | 18 +++- bin/bbackupd/BackupClientDirectoryRecord.cpp | 7 ++ bin/bbackupd/BackupDaemon.cpp | 4 +- 4 files changed, 82 insertions(+), 65 deletions(-) (limited to 'bin') diff --git a/bin/bbackupd/BackupClientContext.cpp b/bin/bbackupd/BackupClientContext.cpp index 7602ed29..e1c4349f 100644 --- a/bin/bbackupd/BackupClientContext.cpp +++ b/bin/bbackupd/BackupClientContext.cpp @@ -28,6 +28,7 @@ #include "autogen_BackupProtocol.h" #include "BackupStoreFile.h" #include "Logging.h" +#include "TcpNice.h" #include "MemLeakFindOn.h" @@ -49,15 +50,14 @@ BackupClientContext::BackupClientContext bool ExtendedLogging, bool ExtendedLogToFile, std::string ExtendedLogFile, - ProgressNotifier& rProgressNotifier + ProgressNotifier& rProgressNotifier, + bool TcpNiceMode ) : mrResolver(rResolver), mrTLSContext(rTLSContext), mHostname(rHostname), mPort(Port), mAccountNumber(AccountNumber), - mpSocket(0), - mpConnection(0), mExtendedLogging(ExtendedLogging), mExtendedLogToFile(ExtendedLogToFile), mExtendedLogFile(ExtendedLogFile), @@ -71,7 +71,8 @@ BackupClientContext::BackupClientContext mpExcludeDirs(0), mKeepAliveTimer(0, "KeepAliveTime"), mbIsManaged(false), - mrProgressNotifier(rProgressNotifier) + mrProgressNotifier(rProgressNotifier), + mTcpNiceMode(TcpNiceMode) { } @@ -107,40 +108,42 @@ BackupClientContext::~BackupClientContext() BackupProtocolClient &BackupClientContext::GetConnection() { // Already got it? Just return it. - if(mpConnection != 0) + if(mapConnection.get()) { - return *mpConnection; + return *mapConnection; } - // Get a socket connection - if(mpSocket == 0) - { - mpSocket = new SocketStreamTLS; - ASSERT(mpSocket != 0); // will have exceptioned if this was a problem - } + // there shouldn't be a connection open + ASSERT(mapSocket.get() == 0); + + SocketStreamTLS *pSocket = new SocketStreamTLS; try { // Defensive. - if(mpConnection != 0) - { - delete mpConnection; - mpConnection = 0; - } + mapConnection.reset(); // Log intention BOX_INFO("Opening connection to server '" << mHostname << "'..."); // Connect! - mpSocket->Open(mrTLSContext, Socket::TypeINET, - mHostname.c_str(), mPort); + pSocket->Open(mrTLSContext, Socket::TypeINET, mHostname.c_str(), mPort); + + if(mTcpNiceMode) + { + mapNice.reset(new NiceSocketStream(std::auto_ptr(pSocket))); + mapConnection.reset(new BackupProtocolClient(*mapNice)); + } + else + { + mapConnection.reset(new BackupProtocolClient(*pSocket)); + } - // And create a procotol object - mpConnection = new BackupProtocolClient(*mpSocket); + pSocket = NULL; // Set logging option - mpConnection->SetLogToSysLog(mExtendedLogging); + mapConnection->SetLogToSysLog(mExtendedLogging); if (mExtendedLogToFile) { @@ -156,16 +159,17 @@ BackupProtocolClient &BackupClientContext::GetConnection() } else { - mpConnection->SetLogToFile(mpExtendedLogFileHandle); + mapConnection->SetLogToFile(mpExtendedLogFileHandle); } } // Handshake - mpConnection->Handshake(); + mapConnection->Handshake(); // Check the version of the server { - std::auto_ptr serverVersion(mpConnection->QueryVersion(BACKUP_STORE_SERVER_VERSION)); + std::auto_ptr serverVersion( + mapConnection->QueryVersion(BACKUP_STORE_SERVER_VERSION)); if(serverVersion->GetVersion() != BACKUP_STORE_SERVER_VERSION) { THROW_EXCEPTION(BackupStoreException, WrongServerVersion) @@ -173,7 +177,8 @@ BackupProtocolClient &BackupClientContext::GetConnection() } // Login -- if this fails, the Protocol will exception - std::auto_ptr loginConf(mpConnection->QueryLogin(mAccountNumber, 0 /* read/write */)); + std::auto_ptr loginConf( + mapConnection->QueryLogin(mAccountNumber, 0 /* read/write */)); // Check that the client store marker is the one we expect if(mClientStoreMarker != ClientStoreMarker_NotKnown) @@ -183,9 +188,9 @@ BackupProtocolClient &BackupClientContext::GetConnection() // Not good... finish the connection, abort, etc, ignoring errors try { - mpConnection->QueryFinished(); - mpSocket->Shutdown(); - mpSocket->Close(); + mapConnection->QueryFinished(); + mapSocket.reset(); + mapNice.reset(); } catch(...) { @@ -213,14 +218,13 @@ BackupProtocolClient &BackupClientContext::GetConnection() catch(...) { // Clean up. - delete mpConnection; - mpConnection = 0; - delete mpSocket; - mpSocket = 0; + mapConnection.reset(); + mapSocket.reset(); + mapNice.reset(); throw; } - return *mpConnection; + return *mapConnection; } // -------------------------------------------------------------------------- @@ -233,7 +237,7 @@ BackupProtocolClient &BackupClientContext::GetConnection() // -------------------------------------------------------------------------- void BackupClientContext::CloseAnyOpenConnection() { - if(mpConnection) + if(mapConnection.get()) { try { @@ -244,14 +248,14 @@ void BackupClientContext::CloseAnyOpenConnection() box_time_t marker = GetCurrentBoxTime(); // Set it on the store - mpConnection->QuerySetClientStoreMarker(marker); + mapConnection->QuerySetClientStoreMarker(marker); // Record it so that it can be picked up later. mClientStoreMarker = marker; } // Quit nicely - mpConnection->QueryFinished(); + mapConnection->QueryFinished(); } catch(...) { @@ -259,26 +263,18 @@ void BackupClientContext::CloseAnyOpenConnection() } // Delete it anyway. - delete mpConnection; - mpConnection = 0; + mapConnection.reset(); } - - if(mpSocket) + + try { - try - { - // Be nice about closing the socket - mpSocket->Shutdown(); - mpSocket->Close(); - } - catch(...) - { - // Ignore errors - } - - // Delete object - delete mpSocket; - mpSocket = 0; + // Be nice about closing the socket + mapSocket.reset(); + mapNice.reset(); + } + catch(...) + { + // Ignore errors } // Delete any pending list @@ -307,9 +303,9 @@ void BackupClientContext::CloseAnyOpenConnection() // -------------------------------------------------------------------------- int BackupClientContext::GetTimeout() const { - if(mpConnection) + if(mapConnection.get()) { - return mpConnection->GetTimeout(); + return mapConnection->GetTimeout(); } return (15*60*1000); @@ -509,7 +505,7 @@ void BackupClientContext::SetKeepAliveTime(int iSeconds) { mKeepAliveTime = iSeconds < 0 ? 0 : iSeconds; BOX_TRACE("Set keep-alive time to " << mKeepAliveTime << " seconds"); - mKeepAliveTimer = Timer(mKeepAliveTime, "KeepAliveTime"); + mKeepAliveTimer = Timer(mKeepAliveTime * 1000, "KeepAliveTime"); } // -------------------------------------------------------------------------- @@ -551,7 +547,7 @@ void BackupClientContext::UnManageDiffProcess() // -------------------------------------------------------------------------- void BackupClientContext::DoKeepAlive() { - if (!mpConnection) + if (!mapConnection.get()) { return; } @@ -567,9 +563,9 @@ void BackupClientContext::DoKeepAlive() } BOX_TRACE("KeepAliveTime reached, sending keep-alive message"); - mpConnection->QueryGetIsAlive(); + mapConnection->QueryGetIsAlive(); - mKeepAliveTimer = Timer(mKeepAliveTime, "KeepAliveTime"); + mKeepAliveTimer = Timer(mKeepAliveTime * 1000, "KeepAliveTime"); } int BackupClientContext::GetMaximumDiffingTime() diff --git a/bin/bbackupd/BackupClientContext.h b/bin/bbackupd/BackupClientContext.h index 404d2d77..41d90ff0 100644 --- a/bin/bbackupd/BackupClientContext.h +++ b/bin/bbackupd/BackupClientContext.h @@ -16,6 +16,7 @@ #include "BackupDaemonInterface.h" #include "BackupStoreFile.h" #include "ExcludeList.h" +#include "TcpNice.h" #include "Timer.h" class TLSContext; @@ -49,7 +50,8 @@ public: bool ExtendedLogging, bool ExtendedLogToFile, std::string ExtendedLogFile, - ProgressNotifier &rProgressNotifier + ProgressNotifier &rProgressNotifier, + bool TcpNiceMode ); virtual ~BackupClientContext(); private: @@ -207,6 +209,14 @@ public: { return mrProgressNotifier; } + + void SetNiceMode(bool enabled) + { + if(mTcpNiceMode) + { + mapNice->SetEnabled(enabled); + } + } private: LocationResolver &mrResolver; @@ -214,8 +224,9 @@ private: std::string mHostname; int mPort; uint32_t mAccountNumber; - SocketStreamTLS *mpSocket; - BackupProtocolClient *mpConnection; + std::auto_ptr mapSocket; + std::auto_ptr mapNice; + std::auto_ptr mapConnection; bool mExtendedLogging; bool mExtendedLogToFile; std::string mExtendedLogFile; @@ -232,6 +243,7 @@ private: int mKeepAliveTime; int mMaximumDiffingTime; ProgressNotifier &mrProgressNotifier; + bool mTcpNiceMode; }; #endif // BACKUPCLIENTCONTEXT__H diff --git a/bin/bbackupd/BackupClientDirectoryRecord.cpp b/bin/bbackupd/BackupClientDirectoryRecord.cpp index 86c9688f..3a0ed08b 100644 --- a/bin/bbackupd/BackupClientDirectoryRecord.cpp +++ b/bin/bbackupd/BackupClientDirectoryRecord.cpp @@ -1671,6 +1671,7 @@ int64_t BackupClientDirectoryRecord::UploadFile( &isCompletelyDifferent)); rContext.UnManageDiffProcess(); + rContext.SetNiceMode(true); RateLimitingStream rateLimit(*patchStream, rParams.mMaxUploadRate); @@ -1690,6 +1691,8 @@ int64_t BackupClientDirectoryRecord::UploadFile( // std::auto_ptr stored(connection.QueryStoreFile(mObjectID, ModificationTime, AttributesHash, isCompletelyDifferent?(0):(diffFromID), rStoreFilename, *pStreamToUpload)); + + rContext.SetNiceMode(false); // Get object ID from the result objID = stored->GetObjectID(); @@ -1715,6 +1718,8 @@ int64_t BackupClientDirectoryRecord::UploadFile( &rParams, &(rParams.mrRunStatusProvider))); + rContext.SetNiceMode(true); + RateLimitingStream rateLimit(*upload, rParams.mMaxUploadRate); IOStream* pStreamToUpload; @@ -1735,6 +1740,8 @@ int64_t BackupClientDirectoryRecord::UploadFile( AttributesHash, 0 /* no diff from file ID */, rStoreFilename, *pStreamToUpload)); + + rContext.SetNiceMode(false); // Get object ID from the result objID = stored->GetObjectID(); diff --git a/bin/bbackupd/BackupDaemon.cpp b/bin/bbackupd/BackupDaemon.cpp index 3d0c9533..bca045d4 100644 --- a/bin/bbackupd/BackupDaemon.cpp +++ b/bin/bbackupd/BackupDaemon.cpp @@ -844,7 +844,9 @@ void BackupDaemon::RunSyncNow() conf.GetKeyValueUint32("AccountNumber"), conf.GetKeyValueBool("ExtendedLogging"), conf.KeyExists("ExtendedLogFile"), - extendedLogFile, *mpProgressNotifier + extendedLogFile, + *mpProgressNotifier, + conf.GetKeyValueBool("TcpNice") ); // The minimum age a file needs to be before it will be -- cgit v1.2.3