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 => $config ) {
106 $name = $config[
'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( $config[
'readOnly'] );
113 unset( $config[
'fileJournal'] );
114 unset( $config[
'lockManager'] );
116 if ( !empty( $config[
'isMultiMaster'] ) ) {
117 if ( $this->masterIndex >= 0 ) {
118 throw new LogicException(
'More than one master backend defined.' );
120 $this->masterIndex = $index;
123 if ( !empty( $config[
'readAffinity'] ) ) {
124 $this->readIndex = $index;
127 if ( !isset( $config[
'class'] ) ) {
128 throw new InvalidArgumentException(
'No class given for a backend config.' );
130 $class = $config[
'class'];
131 $this->backends[$index] =
new $class( $config );
133 if ( $this->masterIndex < 0 ) {
134 throw new LogicException(
'No master backend defined.' );
136 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 __METHOD__ .
": 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(
197 function () use ( $backend, $realOps, $opts, $scopeLock, $relevantPaths ) {
198 $this->logger->error(
199 "'{$backend->getName()}' async replication; paths: " .
200 FormatJson::encode( $relevantPaths )
202 $backend->doOperations( $realOps, $opts );
206 $this->logger->error(
207 "'{$backend->getName()}' sync replication; paths: " .
208 FormatJson::encode( $relevantPaths )
210 $status->merge( $backend->doOperations( $realOps, $opts ) );
217 $status->success = $masterStatus->success;
218 $status->successCount = $masterStatus->successCount;
219 $status->failCount = $masterStatus->failCount;
235 if ( $this->syncChecks == 0 || count( $this->backends ) <= 1 ) {
240 foreach ( $this->backends as $backend ) {
241 $realPaths = $this->
substPaths( $paths, $backend );
242 $backend->preloadFileStat( [
'srcs' => $realPaths,
'latest' =>
true ] );
245 foreach ( $paths as
$path ) {
246 $params = [
'src' =>
$path,
'latest' =>
true ];
249 $masterParams = $this->
substOpPaths( $params, $masterBackend );
250 $masterStat = $masterBackend->getFileStat( $masterParams );
251 if ( $masterStat === self::STAT_ERROR ) {
252 $status->fatal(
'backend-fail-stat',
$path );
255 if ( $this->syncChecks & self::CHECK_SHA1 ) {
256 $masterSha1 = $masterBackend->getFileSha1Base36( $masterParams );
257 if ( ( $masterSha1 !==
false ) !== (
bool)$masterStat ) {
258 $status->fatal(
'backend-fail-hash',
$path );
266 foreach ( $this->backends as $index => $cloneBackend ) {
267 if ( $index === $this->masterIndex ) {
272 $cloneParams = $this->
substOpPaths( $params, $cloneBackend );
273 $cloneStat = $cloneBackend->getFileStat( $cloneParams );
279 $status->fatal(
'backend-fail-synced',
$path );
281 ( $this->syncChecks & self::CHECK_SIZE ) &&
282 $cloneStat[
'size'] !== $masterStat[
'size']
285 $status->fatal(
'backend-fail-synced',
$path );
287 ( $this->syncChecks & self::CHECK_TIME ) &&
289 ConvertibleTimestamp::convert( TS_UNIX, $masterStat[
'mtime'] ) -
290 ConvertibleTimestamp::convert( TS_UNIX, $cloneStat[
'mtime'] )
294 $status->fatal(
'backend-fail-synced',
$path );
296 ( $this->syncChecks & self::CHECK_SHA1 ) &&
297 $cloneBackend->getFileSha1Base36( $cloneParams ) !== $masterSha1
300 $status->fatal(
'backend-fail-synced',
$path );
306 $status->fatal(
'backend-fail-synced',
$path );
323 if ( count( $this->backends ) <= 1 ) {
327 foreach ( $paths as
$path ) {
328 foreach ( $this->backends as $backend ) {
329 $realPath = $this->
substPaths( $path, $backend );
330 if ( !$backend->isPathUsableInternal( $realPath ) ) {
331 $status->fatal(
'backend-fail-usable',
$path );
353 foreach ( $paths as
$path ) {
354 $params = [
'src' =>
$path,
'latest' =>
true ];
357 $masterParams = $this->
substOpPaths( $params, $masterBackend );
358 $masterPath = $masterParams[
'src'];
359 $masterStat = $masterBackend->getFileStat( $masterParams );
360 if ( $masterStat === self::STAT_ERROR ) {
361 $status->fatal(
'backend-fail-stat',
$path );
362 $this->logger->error(
"$fname: file '$masterPath' is not available" );
365 $masterSha1 = $masterBackend->getFileSha1Base36( $masterParams );
366 if ( ( $masterSha1 !==
false ) !== (
bool)$masterStat ) {
367 $status->fatal(
'backend-fail-hash',
$path );
368 $this->logger->error(
"$fname: file '$masterPath' hash does not match stat" );
373 foreach ( $this->backends as $index => $cloneBackend ) {
374 if ( $index === $this->masterIndex ) {
379 $cloneParams = $this->
substOpPaths( $params, $cloneBackend );
380 $clonePath = $cloneParams[
'src'];
381 $cloneStat = $cloneBackend->getFileStat( $cloneParams );
382 if ( $cloneStat === self::STAT_ERROR ) {
383 $status->fatal(
'backend-fail-stat',
$path );
384 $this->logger->error(
"$fname: file '$clonePath' is not available" );
387 $cloneSha1 = $cloneBackend->getFileSha1Base36( $cloneParams );
388 if ( ( $cloneSha1 !==
false ) !== (
bool)$cloneStat ) {
389 $status->fatal(
'backend-fail-hash',
$path );
390 $this->logger->error(
"$fname: file '$clonePath' hash does not match stat" );
394 if ( $masterSha1 === $cloneSha1 ) {
396 $this->logger->debug(
"$fname: file '$clonePath' matches '$masterPath'" );
397 } elseif ( $masterSha1 !==
false ) {
400 $resyncMode ===
'conservative' &&
403 $cloneStat[
'mtime'] > $masterStat[
'mtime']
406 $status->fatal(
'backend-fail-synced',
$path );
409 $fsFile = $masterBackend->getLocalReference( $masterParams );
410 $status->merge( $cloneBackend->quickStore( [
415 } elseif ( $masterStat ===
false ) {
417 if ( $resyncMode ===
'conservative' ) {
419 $status->fatal(
'backend-fail-synced',
$path );
422 $status->merge( $cloneBackend->quickDelete( [
'src' => $clonePath ] ) );
428 if ( !$status->isOK() ) {
429 $this->logger->error(
"$fname: failed to resync: " . FormatJson::encode( $paths ) );
443 foreach ( $ops as $op ) {
444 if ( isset( $op[
'src'] ) ) {
447 if ( empty( $op[
'ignoreMissingSource'] )
448 || $this->
fileExists( [
'src' => $op[
'src'] ] )
450 $paths[] = $op[
'src'];
453 if ( isset( $op[
'srcs'] ) ) {
454 $paths = array_merge( $paths, $op[
'srcs'] );
456 if ( isset( $op[
'dst'] ) ) {
457 $paths[] = $op[
'dst'];
461 return array_values( array_unique( array_filter( $paths,
'FileBackend::isStoragePath' ) ) );
474 foreach ( $ops as $op ) {
476 foreach ( [
'src',
'srcs',
'dst',
'dir' ] as $par ) {
477 if ( isset( $newOp[$par] ) ) {
478 $newOp[$par] = $this->
substPaths( $newOp[$par], $backend );
509 '!^mwstore://' . preg_quote( $this->name,
'!' ) .
'/!',
524 '!^mwstore://' . preg_quote( $backend->
getName(),
'!' ) .
'/!',
535 foreach ( $ops as $op ) {
536 if ( $op[
'op'] ===
'store' && !isset( $op[
'srcRef'] ) ) {
547 $realOps = $this->
substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
549 $status->merge( $masterStatus );
551 foreach ( $this->backends as $index => $backend ) {
552 if ( $index === $this->masterIndex ) {
558 DeferredUpdates::addCallableUpdate(
559 function () use ( $backend, $realOps ) {
560 $backend->doQuickOperations( $realOps );
564 $status->merge( $backend->doQuickOperations( $realOps ) );
570 $status->success = $masterStatus->success;
571 $status->successCount = $masterStatus->successCount;
572 $status->failCount = $masterStatus->failCount;
601 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
603 $status->merge( $masterStatus );
605 foreach ( $this->backends as $index => $backend ) {
606 if ( $index === $this->masterIndex ) {
611 if ( $this->asyncWrites ) {
612 DeferredUpdates::addCallableUpdate(
613 function () use ( $backend, $method, $realParams ) {
614 $backend->$method( $realParams );
618 $status->merge( $backend->$method( $realParams ) );
629 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
631 $status->merge( $this->backends[$index]->
concatenate( $realParams ) );
638 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
640 return $this->backends[$index]->fileExists( $realParams );
645 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
647 return $this->backends[$index]->getFileTimestamp( $realParams );
652 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
654 return $this->backends[$index]->getFileSize( $realParams );
659 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
661 return $this->backends[$index]->getFileStat( $realParams );
666 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
668 return $this->backends[$index]->getFileXAttributes( $realParams );
673 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
675 $contentsM = $this->backends[$index]->getFileContentsMulti( $realParams );
678 foreach ( $contentsM as
$path => $data ) {
687 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
689 return $this->backends[$index]->getFileSha1Base36( $realParams );
694 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
696 return $this->backends[$index]->getFileProps( $realParams );
701 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
703 return $this->backends[$index]->streamFile( $realParams );
708 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
710 $fsFilesM = $this->backends[$index]->getLocalReferenceMulti( $realParams );
713 foreach ( $fsFilesM as
$path => $fsFile ) {
722 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
724 $tempFilesM = $this->backends[$index]->getLocalCopyMulti( $realParams );
727 foreach ( $tempFilesM as
$path => $tempFile ) {
736 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
738 return $this->backends[$index]->getFileHttpUrl( $realParams );
742 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
748 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
754 $realParams = $this->
substOpPaths( $params, $this->backends[$this->masterIndex] );
764 foreach ( $this->backends as $backend ) {
765 $realPaths = is_array( $paths ) ? $this->
substPaths( $paths, $backend ) :
null;
766 $backend->clearCache( $realPaths );
771 $realPaths = $this->
substPaths( $paths, $this->backends[$this->readIndex] );
777 $realParams = $this->
substOpPaths( $params, $this->backends[$index] );
779 return $this->backends[$index]->preloadFileStat( $realParams );
783 $realOps = $this->
substOpBatchPaths( $ops, $this->backends[$this->masterIndex] );
786 $paths = $this->backends[
$this->masterIndex]->getPathsToLockForOpsInternal( $fileOps );
790 $paths[LockManager::LOCK_UW],
791 $this->backends[$this->masterIndex]
794 $paths[LockManager::LOCK_EX],
795 $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 Stable to override.
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.
static escapeRegexReplacement( $string)
Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter.