24use Wikimedia\Timestamp\ConvertibleTimestamp;
63 private const CHECK_SIZE = 1;
65 private const CHECK_TIME = 2;
67 private const CHECK_SHA1 = 4;
98 parent::__construct( $config );
99 $this->syncChecks = $config[
'syncChecks'] ?? self::CHECK_SIZE;
100 $this->autoResync = $config[
'autoResync'] ??
false;
101 $this->asyncWrites = isset( $config[
'replication'] ) && $config[
'replication'] ===
'async';
105 foreach ( $config[
'backends'] as $index => $beConfig ) {
106 $name = $beConfig[
'name'];
107 if ( isset( $namesUsed[
$name] ) ) {
108 throw new LogicException(
"Two or more backends defined with the name $name." );
110 $namesUsed[
$name] = 1;
112 unset( $beConfig[
'readOnly'] );
113 unset( $beConfig[
'lockManager'] );
116 if ( !empty( $beConfig[
'isMultiMaster'] ) ) {
117 if ( $this->masterIndex >= 0 ) {
118 throw new LogicException(
'More than one master backend defined.' );
120 $this->masterIndex = $index;
122 if ( !empty( $beConfig[
'readAffinity'] ) ) {
123 $this->readIndex = $index;
126 if ( !isset( $beConfig[
'class'] ) ) {
127 throw new InvalidArgumentException(
'No class given for a backend config.' );
129 $class = $beConfig[
'class'];
130 $this->backends[$index] =
new $class( $beConfig );
132 if ( $this->masterIndex < 0 ) {
133 throw new LogicException(
'No master backend defined.' );
135 if ( $this->readIndex < 0 ) {
148 if ( empty( $opts[
'nonLocking'] ) ) {
150 if ( !$status->isOK() ) {
158 $opts[
'preserveCache'] =
true;
161 if ( !$status->isOK() ) {
166 if ( !$syncStatus->isOK() ) {
167 $this->logger->error(
168 "$fname: failed sync check: " . FormatJson::encode( $relevantPaths )
172 $this->autoResync ===
false ||
173 !$this->
resyncFiles( $relevantPaths, $this->autoResync )->isOK()
175 $status->merge( $syncStatus );
182 $masterStatus = $mbe->doOperations( $realOps, $opts );
183 $status->merge( $masterStatus );
187 if ( $masterStatus->isOK() && $masterStatus->successCount > 0 ) {
188 foreach ( $this->backends as $index => $backend ) {
189 if ( $index === $this->masterIndex ) {
196 DeferredUpdates::addCallableUpdate(
198 $backend, $realOps, $opts, $scopeLock, $relevantPaths, $fname
200 $this->logger->debug(
201 "$fname: '{$backend->getName()}' async replication; paths: " .
202 FormatJson::encode( $relevantPaths )
204 $backend->doOperations( $realOps, $opts );
208 $this->logger->debug(
209 "$fname: '{$backend->getName()}' sync replication; paths: " .
210 FormatJson::encode( $relevantPaths )
212 $status->merge( $backend->doOperations( $realOps, $opts ) );
219 $status->success = $masterStatus->success;
220 $status->successCount = $masterStatus->successCount;
221 $status->failCount = $masterStatus->failCount;
237 if ( $this->syncChecks == 0 || count( $this->backends ) <= 1 ) {
242 foreach ( $this->backends as $backend ) {
243 $realPaths = $this->
substPaths( $paths, $backend );
244 $backend->preloadFileStat( [
'srcs' => $realPaths,
'latest' =>
true ] );
247 foreach ( $paths as
$path ) {
248 $params = [
'src' =>
$path,
'latest' =>
true ];
251 $masterParams = $this->
substOpPaths( $params, $masterBackend );
252 $masterStat = $masterBackend->getFileStat( $masterParams );
253 if ( $masterStat === self::STAT_ERROR ) {
254 $status->fatal(
'backend-fail-stat',
$path );
257 if ( $this->syncChecks & self::CHECK_SHA1 ) {
258 $masterSha1 = $masterBackend->getFileSha1Base36( $masterParams );
259 if ( ( $masterSha1 !==
false ) !== (
bool)$masterStat ) {
260 $status->fatal(
'backend-fail-hash',
$path );
268 foreach ( $this->backends as $index => $cloneBackend ) {
269 if ( $index === $this->masterIndex ) {
274 $cloneParams = $this->
substOpPaths( $params, $cloneBackend );
275 $cloneStat = $cloneBackend->getFileStat( $cloneParams );
281 $status->fatal(
'backend-fail-synced',
$path );
283 ( $this->syncChecks & self::CHECK_SIZE ) &&
284 $cloneStat[
'size'] !== $masterStat[
'size']
287 $status->fatal(
'backend-fail-synced',
$path );
289 ( $this->syncChecks & self::CHECK_TIME ) &&
291 (
int)ConvertibleTimestamp::convert( TS_UNIX, $masterStat[
'mtime'] ) -
292 (
int)ConvertibleTimestamp::convert( TS_UNIX, $cloneStat[
'mtime'] )
296 $status->fatal(
'backend-fail-synced',
$path );
298 ( $this->syncChecks & self::CHECK_SHA1 ) &&
299 $cloneBackend->getFileSha1Base36( $cloneParams ) !== $masterSha1
302 $status->fatal(
'backend-fail-synced',
$path );
308 $status->fatal(
'backend-fail-synced',
$path );
325 if ( count( $this->backends ) <= 1 ) {
329 foreach ( $paths as
$path ) {
330 foreach ( $this->backends as $backend ) {
331 $realPath = $this->
substPaths( $path, $backend );
332 if ( !$backend->isPathUsableInternal( $realPath ) ) {
333 $status->fatal(
'backend-fail-usable',
$path );
355 foreach ( $paths as
$path ) {
356 $params = [
'src' =>
$path,
'latest' =>
true ];
359 $masterParams = $this->
substOpPaths( $params, $masterBackend );
360 $masterPath = $masterParams[
'src'];
361 $masterStat = $masterBackend->getFileStat( $masterParams );
362 if ( $masterStat === self::STAT_ERROR ) {
363 $status->fatal(
'backend-fail-stat',
$path );
364 $this->logger->error(
"$fname: file '$masterPath' is not available" );
367 $masterSha1 = $masterBackend->getFileSha1Base36( $masterParams );
368 if ( ( $masterSha1 !==
false ) !== (
bool)$masterStat ) {
369 $status->fatal(
'backend-fail-hash',
$path );
370 $this->logger->error(
"$fname: file '$masterPath' hash does not match stat" );
375 foreach ( $this->backends as $index => $cloneBackend ) {
376 if ( $index === $this->masterIndex ) {
381 $cloneParams = $this->
substOpPaths( $params, $cloneBackend );
382 $clonePath = $cloneParams[
'src'];
383 $cloneStat = $cloneBackend->getFileStat( $cloneParams );
384 if ( $cloneStat === self::STAT_ERROR ) {
385 $status->fatal(
'backend-fail-stat',
$path );
386 $this->logger->error(
"$fname: file '$clonePath' is not available" );
389 $cloneSha1 = $cloneBackend->getFileSha1Base36( $cloneParams );
390 if ( ( $cloneSha1 !==
false ) !== (
bool)$cloneStat ) {
391 $status->fatal(
'backend-fail-hash',
$path );
392 $this->logger->error(
"$fname: file '$clonePath' hash does not match stat" );
396 if ( $masterSha1 === $cloneSha1 ) {
398 $this->logger->debug(
"$fname: file '$clonePath' matches '$masterPath'" );
399 } elseif ( $masterSha1 !==
false ) {
402 $resyncMode ===
'conservative' &&
405 $cloneStat[
'mtime'] > $masterStat[
'mtime']
408 $status->fatal(
'backend-fail-synced',
$path );
411 $fsFile = $masterBackend->getLocalReference( $masterParams );
412 $status->merge( $cloneBackend->quickStore( [
417 } elseif ( $masterStat ===
false ) {
419 if ( $resyncMode ===
'conservative' ) {
421 $status->fatal(
'backend-fail-synced',
$path );
422 $this->logger->error(
"$fname: not allowed to delete file '$clonePath'" );
425 $status->merge( $cloneBackend->quickDelete( [
'src' => $clonePath ] ) );
431 if ( !$status->isOK() ) {
432 $this->logger->error(
"$fname: failed to resync: " . FormatJson::encode( $paths ) );
446 foreach ( $ops as $op ) {
447 if ( isset( $op[
'src'] ) ) {
450 if ( empty( $op[
'ignoreMissingSource'] )
451 || $this->
fileExists( [
'src' => $op[
'src'] ] )
453 $paths[] = $op[
'src'];
456 if ( isset( $op[
'srcs'] ) ) {
457 $paths = array_merge( $paths, $op[
'srcs'] );
459 if ( isset( $op[
'dst'] ) ) {
460 $paths[] = $op[
'dst'];
464 return array_values( array_unique( array_filter( $paths, [ FileBackend::class,
'isStoragePath' ] ) ) );
477 foreach ( $ops as $op ) {
479 foreach ( [
'src',
'srcs',
'dst',
'dir' ] as $par ) {
480 if ( isset( $newOp[$par] ) ) {
481 $newOp[$par] = $this->
substPaths( $newOp[$par], $backend );
512 '!^mwstore://' . preg_quote( $this->name,
'!' ) .
'/!',
513 StringUtils::escapeRegexReplacement(
"mwstore://{$backend->getName()}/" ),
527 '!^mwstore://' . preg_quote( $backend->
getName(),
'!' ) .
'/!',
528 StringUtils::escapeRegexReplacement(
"mwstore://{$this->name}/" ),
538 foreach ( $ops as $op ) {
539 if ( $op[
'op'] ===
'store' && !isset( $op[
'srcRef'] ) ) {
550 $realOps = $this->
substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
552 $status->merge( $masterStatus );
554 foreach ( $this->backends as $index => $backend ) {
555 if ( $index === $this->masterIndex ) {
561 DeferredUpdates::addCallableUpdate(
562 static function () use ( $backend, $realOps ) {
563 $backend->doQuickOperations( $realOps );
567 $status->merge( $backend->doQuickOperations( $realOps ) );
573 $status->success = $masterStatus->success;
574 $status->successCount = $masterStatus->successCount;
575 $status->failCount = $masterStatus->failCount;
604 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
606 $status->merge( $masterStatus );
608 foreach ( $this->backends as $index => $backend ) {
609 if ( $index === $this->masterIndex ) {
614 if ( $this->asyncWrites ) {
615 DeferredUpdates::addCallableUpdate(
616 static function () use ( $backend, $method, $realParams ) {
617 $backend->$method( $realParams );
621 $status->merge( $backend->$method( $realParams ) );
632 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
634 $status->merge( $this->backends[$index]->
concatenate( $realParams ) );
641 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
643 return $this->backends[$index]->fileExists( $realParams );
648 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
650 return $this->backends[$index]->getFileTimestamp( $realParams );
655 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
657 return $this->backends[$index]->getFileSize( $realParams );
662 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
664 return $this->backends[$index]->getFileStat( $realParams );
669 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
671 return $this->backends[$index]->getFileXAttributes( $realParams );
676 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
678 $contentsM = $this->backends[$index]->getFileContentsMulti( $realParams );
681 foreach ( $contentsM as
$path => $data ) {
690 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
692 return $this->backends[$index]->getFileSha1Base36( $realParams );
697 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
699 return $this->backends[$index]->getFileProps( $realParams );
704 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
706 return $this->backends[$index]->streamFile( $realParams );
711 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
713 $fsFilesM = $this->backends[$index]->getLocalReferenceMulti( $realParams );
716 foreach ( $fsFilesM as
$path => $fsFile ) {
725 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
727 $tempFilesM = $this->backends[$index]->getLocalCopyMulti( $realParams );
730 foreach ( $tempFilesM as
$path => $tempFile ) {
739 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
741 return $this->backends[$index]->getFileHttpUrl( $realParams );
745 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
751 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
757 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
767 foreach ( $this->backends as $backend ) {
768 $realPaths = is_array( $paths ) ? $this->
substPaths( $paths, $backend ) :
null;
769 $backend->clearCache( $realPaths );
774 $realPaths = $this->
substPaths( $paths, $this->backends[$this->readIndex] );
780 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
782 return $this->backends[$index]->preloadFileStat( $realParams );
786 $realOps = $this->
substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
789 $paths = $this->backends[
$this->masterIndex]->getPathsToLockForOpsInternal( $fileOps );
793 $paths[LockManager::LOCK_UW],
794 $this->backends[$this->masterIndex]
797 $paths[LockManager::LOCK_EX],
798 $this->backends[$this->masterIndex]
Proxy backend that mirrors writes to several internal backends.
FileBackendStore[] $backends
Prioritized list of FileBackendStore objects.
fileExists(array $params)
Check if a file exists at a storage path in the backend.
getLocalReferenceMulti(array $params)
Like getLocalReference() except it takes an array of storage paths and yields an order-preserved map ...
getLocalCopyMulti(array $params)
Like getLocalCopy() except it takes an array of storage paths and yields an order preserved-map of st...
consistencyCheck(array $paths)
Check that a set of files are consistent across all internal backends.
unsubstPaths( $paths, FileBackendStore $backend)
Substitute the backend of internal storage paths with the proxy backend's name.
getFileSha1Base36(array $params)
Get a SHA-1 hash of the content of the file at a storage path in the backend.
int $readIndex
Index of read affinity backend.
clearCache(array $paths=null)
Invalidate any in-process file stat and property cache.
doQuickOperationsInternal(array $ops, array $opts)
getFileStat(array $params)
Get quick information about a file at a storage path in the backend.
getFileContentsMulti(array $params)
Like getFileContents() except it takes an array of storage paths and returns an order preserved map o...
preloadCache(array $paths)
Preload persistent file stat cache and property cache into in-process cache.
resyncFiles(array $paths, $resyncMode=true)
Check that a set of files are consistent across all internal backends and re-synchronize those files ...
getDirectoryList(array $params)
Get an iterator to list all directories under a storage directory.
__construct(array $config)
Construct a proxy backend that consists of several internal backends.
getFileList(array $params)
Get an iterator to list all stored files under a storage directory.
getReadIndexFromParams(array $params)
doOperationsInternal(array $ops, array $opts)
streamFile(array $params)
Stream the content of the file at a storage path in the backend.
int $masterIndex
Index of master backend.
accessibilityCheck(array $paths)
Check that a set of file paths are usable across all internal backends.
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
hasVolatileSources(array $ops)
fileStoragePathsForOps(array $ops)
Get a list of file storage paths to read or write for a list of operations.
concatenate(array $params)
Concatenate a list of storage files into a single file system file.
substOpPaths(array $ops, FileBackendStore $backend)
Same as substOpBatchPaths() but for a single operation.
substOpBatchPaths(array $ops, FileBackendStore $backend)
Substitute the backend name in storage path parameters for a set of operations with that of a given i...
getFileProps(array $params)
Get the properties of the content of the file at a storage path in the backend.
doDirectoryOp( $method, array $params)
getScopedLocksForOps(array $ops, StatusValue $status)
Get an array of scoped locks needed for a batch of file operations.
getFileHttpUrl(array $params)
Return an HTTP URL to a given file that requires no authentication to use.
preloadFileStat(array $params)
Preload file stat information (concurrently if possible) into in-process cache.
getFileSize(array $params)
Get the size (bytes) of a file at a storage path in the backend.
getFileXAttributes(array $params)
Get metadata about a file at a storage path in the backend.
substPaths( $paths, FileBackendStore $backend)
Substitute the backend of storage paths with an internal backend's name.
directoryExists(array $params)
Check if a directory exists at a given storage path.
getFileTimestamp(array $params)
Get the last-modified timestamp of the file at a storage path.
Base class for all backends using particular storage medium.
Base class for all file backend classes (including multi-write backends).
string $name
Unique backend name.
string $domainId
Unique domain name.
getScopedFileLocks(array $paths, $type, StatusValue $status, $timeout=0)
Lock the files at the given storage paths in the backend.
newStatus(... $args)
Yields the result of the status wrapper callback on either:
getName()
Get the unique backend name.
Generic operation result class Has warning/error list, boolean status and arbitrary value.