1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include "check.hh"
#include "chunk_storage.hh"
#include "debug.hh"
#include "dir.hh"
#include "hex.hh"
#include "random.hh"
namespace ChunkStorage {
Writer::Writer( StorageInfo const & storageInfo,
EncryptionKey const & encryptionKey,
TmpMgr & tmpMgr, ChunkIndex & index, string const & bundlesDir,
string const & indexDir, size_t maxCompressorsToRun ):
storageInfo( storageInfo ), encryptionKey( encryptionKey ),
tmpMgr( tmpMgr ), index( index ), bundlesDir( bundlesDir ),
indexDir( indexDir ), hasCurrentBundleId( false ),
maxCompressorsToRun( maxCompressorsToRun ), runningCompressors( 0 )
{
verbosePrintf( "Using up to %zu thread(s) for compression\n",
maxCompressorsToRun );
}
Writer::~Writer()
{
waitForAllCompressorsToFinish();
}
bool Writer::add( ChunkId const & id, void const * data, size_t size )
{
if ( index.addChunk( id, getCurrentBundleId() ) )
{
// Added to the index? Emit to the bundle then
if ( getCurrentBundle().getPayloadSize() + size >
storageInfo.bundle_max_payload_size() )
finishCurrentBundle();
getCurrentBundle().addChunk( id.toBlob(), data, size );
return true;
}
else
return false;
}
void Writer::addBundle( BundleInfo const & bundleInfo, Bundle::Id const & bundleId )
{
if ( !indexFile.get() )
{
// Create a new index file
indexTempFile = tmpMgr.makeTemporaryFile();
indexFile = new IndexFile::Writer( encryptionKey,
indexTempFile->getFileName() );
}
indexFile->add( bundleInfo, bundleId );
}
void Writer::commit()
{
finishCurrentBundle();
waitForAllCompressorsToFinish();
// Move all bundles
for ( size_t x = pendingBundleRenames.size(); x--; )
{
PendingBundleRename & r = pendingBundleRenames[ x ];
r.first->moveOverTo( Bundle::generateFileName( r.second, bundlesDir,
true ) );
}
pendingBundleRenames.clear();
// Move the index file
if ( indexFile.get() )
{
indexFile.reset();
// Generate a random filename
unsigned char buf[ 24 ]; // Same comments as for Bundle::IdSize
Random::genaratePseudo( buf, sizeof( buf ) );
indexTempFile->moveOverTo( Dir::addPath( indexDir,
toHex( buf, sizeof( buf ) ) ) );
indexTempFile.reset();
}
}
void Writer::reset()
{
finishCurrentBundle();
waitForAllCompressorsToFinish();
pendingBundleRenames.clear();
if ( indexFile.get() )
{
indexFile.reset();
}
}
Bundle::Creator & Writer::getCurrentBundle()
{
if ( !currentBundle.get() )
currentBundle = new Bundle::Creator;
return *currentBundle;
}
void Writer::finishCurrentBundle()
{
if ( !currentBundle.get() )
return;
Bundle::Id const & bundleId = getCurrentBundleId();
addBundle( currentBundle->getCurrentBundleInfo(), bundleId );
sptr< TemporaryFile > file = tmpMgr.makeTemporaryFile();
pendingBundleRenames.push_back( PendingBundleRename( file, bundleId ) );
// Create a new compressor
// Wait for some compressors to finish if there are too many of them
Lock _( runningCompressorsMutex );
while ( runningCompressors >= maxCompressorsToRun )
runningCompressorsCondition.wait( runningCompressorsMutex );
Compressor * compressor = new Compressor( *this, currentBundle,
file->getFileName() );
currentBundle.reset();
hasCurrentBundleId = false;
compressor->start();
++runningCompressors;
}
void Writer::waitForAllCompressorsToFinish()
{
Lock _( runningCompressorsMutex );
while ( runningCompressors )
runningCompressorsCondition.wait( runningCompressorsMutex );
}
Bundle::Id const & Writer::getCurrentBundleId()
{
if ( !hasCurrentBundleId )
{
// Generate a new one
Random::genaratePseudo( ¤tBundleId, sizeof( currentBundleId ) );
hasCurrentBundleId = true;
}
return currentBundleId;
}
Writer::Compressor::Compressor( Writer & writer,
sptr< Bundle::Creator > const & bundleCreator,
string const & fileName ):
writer( writer ), bundleCreator( bundleCreator ), fileName( fileName )
{
}
void * Writer::Compressor::Compressor::threadFunction() throw()
{
try
{
bundleCreator->write( fileName, writer.encryptionKey );
}
catch( std::exception & e )
{
FAIL( "Bunding writing failed: %s", e.what() );
}
{
Lock _( writer.runningCompressorsMutex );
CHECK( writer.runningCompressors, "no running compressors" );
--writer.runningCompressors;
writer.runningCompressorsCondition.signal();
}
detach();
// We're in detached thread, so no further cleanup is necessary
delete this;
return NULL;
}
Reader::Reader( StorageInfo const & storageInfo,
EncryptionKey const & encryptionKey,
ChunkIndex & index, string const & bundlesDir,
size_t maxCacheSizeBytes ):
storageInfo( storageInfo ), encryptionKey( encryptionKey ),
index( index ), bundlesDir( bundlesDir ),
// We need to have at least one cached reader, otherwise we would have to
// unpack a bundle each time a chunk is read, even for consecutive chunks
// in the same bundle
cachedReaders( maxCacheSizeBytes < storageInfo.bundle_max_payload_size() ?
1 : maxCacheSizeBytes / storageInfo.bundle_max_payload_size() )
{
verbosePrintf( "Using up to %zu MB of RAM as cache\n",
maxCacheSizeBytes / 1048576 );
}
void Reader::get( ChunkId const & chunkId, string & data, size_t & size )
{
if ( Bundle::Id const * bundleId = index.findChunk( chunkId ) )
{
Bundle::Reader & reader = getReaderFor( *bundleId );
reader.get( chunkId.toBlob(), data, size );
}
else
{
string blob = chunkId.toBlob();
throw exNoSuchChunk( toHex( ( unsigned char const * ) blob.data(),
blob.size() ) );
}
}
Bundle::Reader & Reader::getReaderFor( Bundle::Id const & id )
{
sptr< Bundle::Reader > & reader = cachedReaders.entry< Bundle::Reader >(
string( ( char const * ) &id, sizeof( id ) ) );
if ( !reader.get() )
{
// Load the bundle
reader =
new Bundle::Reader( Bundle::generateFileName( id, bundlesDir, false ),
encryptionKey );
}
return *reader;
}
}
|