Class IndexShardTestCase

    • Field Detail

      • primaryTerm

        protected long primaryTerm
      • currentClusterStateVersion

        protected static java.util.concurrent.atomic.AtomicLong currentClusterStateVersion
    • Constructor Detail

      • IndexShardTestCase

        public IndexShardTestCase()
    • Method Detail

      • setUp

        public void setUp()
                   throws java.lang.Exception
        Overrides:
        setUp in class LuceneTestCase
        Throws:
        java.lang.Exception
      • setUpThreadPool

        protected ThreadPool setUpThreadPool()
      • tearDown

        public void tearDown()
                      throws java.lang.Exception
        Overrides:
        tearDown in class LuceneTestCase
        Throws:
        java.lang.Exception
      • tearDownThreadPool

        protected void tearDownThreadPool()
      • allowShardFailures

        protected void allowShardFailures()
        by default, tests will fail if any shard created by this class fails. Tests that cause failures by design can call this method to ignore those failures
      • failOnShardFailures

        protected void failOnShardFailures()
      • threadPoolSettings

        public Settings threadPoolSettings()
      • createStore

        protected Store createStore​(IndexSettings indexSettings,
                                    ShardPath shardPath)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • createStore

        protected Store createStore​(ShardId shardId,
                                    IndexSettings indexSettings,
                                    org.apache.lucene.store.Directory directory)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(boolean primary)
                               throws java.io.IOException
        Creates a new initializing shard. The shard will have its own unique data path.
        Parameters:
        primary - indicates whether to a primary shard (ready to recover from an empty store) or a replica (ready to recover from another shard)
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(boolean primary,
                                      Settings settings)
                               throws java.io.IOException
        Creates a new initializing shard. The shard will have its own unique data path.
        Parameters:
        primary - indicates whether to a primary shard (ready to recover from an empty store) or a replica (ready to recover from another shard)
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(boolean primary,
                                      Settings settings,
                                      EngineFactory engineFactory)
                               throws java.io.IOException
        Creates a new initializing shard. The shard will have its own unique data path.
        Parameters:
        primary - indicates whether to a primary shard (ready to recover from an empty store) or a replica (ready to recover from another shard)
        settings - the settings to use for this shard
        engineFactory - the engine factory to use for this shard
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(ShardRouting shardRouting,
                                      Settings settings,
                                      EngineFactory engineFactory,
                                      IndexingOperationListener... listeners)
                               throws java.io.IOException
        Creates a new initializing shard. The shard will have its own unique data path.
        Parameters:
        shardRouting - the ShardRouting to use for this shard
        settings - the settings to use for this shard
        engineFactory - the engine factory to use for this shard
        listeners - an optional set of listeners to add to the shard
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(ShardId shardId,
                                      boolean primary,
                                      IndexingOperationListener... listeners)
                               throws java.io.IOException
        creates a new initializing shard. The shard will have its own unique data path.
        Parameters:
        shardId - the shard id to use
        primary - indicates whether to a primary shard (ready to recover from an empty store) or a replica (ready to recover from another shard)
        listeners - an optional set of listeners to add to the shard
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(ShardId shardId,
                                      boolean primary,
                                      java.lang.String nodeId,
                                      IndexMetaData indexMetaData,
                                      @Nullable
                                      CheckedFunction<DirectoryReader,​DirectoryReader,​java.io.IOException> readerWrapper)
                               throws java.io.IOException
        creates a new initializing shard. The shard will will be put in its proper path under the supplied node id.
        Parameters:
        shardId - the shard id to use
        primary - indicates whether to a primary shard (ready to recover from an empty store) or a replica (ready to recover from another shard)
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(ShardId shardId,
                                      boolean primary,
                                      java.lang.String nodeId,
                                      IndexMetaData indexMetaData,
                                      @Nullable
                                      CheckedFunction<DirectoryReader,​DirectoryReader,​java.io.IOException> readerWrapper,
                                      java.lang.Runnable globalCheckpointSyncer)
                               throws java.io.IOException
        creates a new initializing shard. The shard will will be put in its proper path under the supplied node id.
        Parameters:
        shardId - the shard id to use
        primary - indicates whether to a primary shard (ready to recover from an empty store) or a replica (ready to recover from another shard)
        Throws:
        java.io.IOException
      • newShard

        protected IndexShard newShard​(ShardRouting routing,
                                      IndexMetaData indexMetaData,
                                      @Nullable
                                      CheckedFunction<DirectoryReader,​DirectoryReader,​java.io.IOException> indexReaderWrapper,
                                      @Nullable
                                      EngineFactory engineFactory,
                                      java.lang.Runnable globalCheckpointSyncer,
                                      RetentionLeaseSyncer retentionLeaseSyncer,
                                      IndexingOperationListener... listeners)
                               throws java.io.IOException
        creates a new initializing shard. The shard will will be put in its proper path under the current node id the shard is assigned to.
        Parameters:
        routing - shard routing to use
        indexMetaData - indexMetaData for the shard, including any mapping
        indexReaderWrapper - an optional wrapper to be used during search
        globalCheckpointSyncer - callback for syncing global checkpoints
        listeners - an optional set of listeners to add to the shard
        Throws:
        java.io.IOException
      • reinitShard

        protected IndexShard reinitShard​(IndexShard current,
                                         IndexingOperationListener... listeners)
                                  throws java.io.IOException
        Takes an existing shard, closes it and starts a new initialing shard at the same location
        Parameters:
        listeners - new listerns to use for the newly created shard
        Throws:
        java.io.IOException
      • reinitShard

        protected IndexShard reinitShard​(IndexShard current,
                                         ShardRouting routing,
                                         IndexingOperationListener... listeners)
                                  throws java.io.IOException
        Takes an existing shard, closes it and starts a new initialing shard at the same location
        Parameters:
        routing - the shard routing to use for the newly created shard.
        listeners - new listerns to use for the newly created shard
        Throws:
        java.io.IOException
      • reinitShard

        protected IndexShard reinitShard​(IndexShard current,
                                         ShardRouting routing,
                                         EngineFactory engineFactory,
                                         IndexingOperationListener... listeners)
                                  throws java.io.IOException
        Takes an existing shard, closes it and starts a new initialing shard at the same location
        Parameters:
        routing - the shard routing to use for the newly created shard.
        listeners - new listerns to use for the newly created shard
        engineFactory - the engine factory for the new shard
        Throws:
        java.io.IOException
      • newStartedShard

        protected IndexShard newStartedShard()
                                      throws java.io.IOException
        Creates a new empty shard and starts it. The shard will randomly be a replica or a primary.
        Throws:
        java.io.IOException
      • newStartedShard

        protected IndexShard newStartedShard​(Settings settings)
                                      throws java.io.IOException
        Creates a new empty shard and starts it
        Parameters:
        settings - the settings to use for this shard
        Throws:
        java.io.IOException
      • newStartedShard

        protected IndexShard newStartedShard​(boolean primary)
                                      throws java.io.IOException
        Creates a new empty shard and starts it.
        Parameters:
        primary - controls whether the shard will be a primary or a replica.
        Throws:
        java.io.IOException
      • newStartedShard

        protected IndexShard newStartedShard​(boolean primary,
                                             Settings settings)
                                      throws java.io.IOException
        Creates a new empty shard and starts it.
        Parameters:
        primary - controls whether the shard will be a primary or a replica.
        settings - the settings to use for this shard
        Throws:
        java.io.IOException
      • newStartedShard

        protected IndexShard newStartedShard​(boolean primary,
                                             Settings settings,
                                             EngineFactory engineFactory)
                                      throws java.io.IOException
        Creates a new empty shard with the specified settings and engine factory and starts it.
        Parameters:
        primary - controls whether the shard will be a primary or a replica.
        settings - the settings to use for this shard
        engineFactory - the engine factory to use for this shard
        Throws:
        java.io.IOException
      • newStartedShard

        protected IndexShard newStartedShard​(CheckedFunction<java.lang.Boolean,​IndexShard,​java.io.IOException> shardFunction,
                                             boolean primary)
                                      throws java.io.IOException
        creates a new empty shard and starts it.
        Parameters:
        shardFunction - shard factory function
        primary - controls whether the shard will be a primary or a replica.
        Throws:
        java.io.IOException
      • closeShards

        protected void closeShards​(IndexShard... shards)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • closeShard

        protected void closeShard​(IndexShard shard,
                                  boolean assertConsistencyBetweenTranslogAndLucene)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • closeShards

        protected void closeShards​(java.lang.Iterable<IndexShard> shards)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • recoverShardFromStore

        protected void recoverShardFromStore​(IndexShard primary)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • updateRoutingEntry

        public static void updateRoutingEntry​(IndexShard shard,
                                              ShardRouting shardRouting)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • recoveryEmptyReplica

        protected void recoveryEmptyReplica​(IndexShard replica,
                                            boolean startReplica)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • getFakeDiscoNode

        protected DiscoveryNode getFakeDiscoNode​(java.lang.String id)
      • recoverReplica

        protected void recoverReplica​(IndexShard replica,
                                      IndexShard primary,
                                      boolean startReplica)
                               throws java.io.IOException
        recovers a replica from the given primary
        Throws:
        java.io.IOException
      • recoverReplica

        protected void recoverReplica​(IndexShard replica,
                                      IndexShard primary,
                                      java.util.function.BiFunction<IndexShard,​DiscoveryNode,​RecoveryTarget> targetSupplier,
                                      boolean markAsRecovering,
                                      boolean markAsStarted)
                               throws java.io.IOException
        recovers a replica from the given primary
        Throws:
        java.io.IOException
      • recoverUnstartedReplica

        protected final void recoverUnstartedReplica​(IndexShard replica,
                                                     IndexShard primary,
                                                     java.util.function.BiFunction<IndexShard,​DiscoveryNode,​RecoveryTarget> targetSupplier,
                                                     boolean markAsRecovering,
                                                     java.util.Set<java.lang.String> inSyncIds,
                                                     IndexShardRoutingTable routingTable)
                                              throws java.io.IOException
        Recovers a replica from the give primary, allow the user to supply a custom recovery target. A typical usage of a custom recovery target is to assert things in the various stages of recovery. Note: this method keeps the shard in IndexShardState.POST_RECOVERY and doesn't start it.
        Parameters:
        replica - the recovery target shard
        primary - the recovery source shard
        targetSupplier - supplies an instance of RecoveryTarget
        markAsRecovering - set to false if the replica is marked as recovering
        Throws:
        java.io.IOException
      • startReplicaAfterRecovery

        protected void startReplicaAfterRecovery​(IndexShard replica,
                                                 IndexShard primary,
                                                 java.util.Set<java.lang.String> inSyncIds,
                                                 IndexShardRoutingTable routingTable)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • promoteReplica

        protected void promoteReplica​(IndexShard replica,
                                      java.util.Set<java.lang.String> inSyncIds,
                                      IndexShardRoutingTable routingTable)
                               throws java.io.IOException
        promotes a replica to primary, incrementing it's term and starting it if needed
        Throws:
        java.io.IOException
      • getShardDocUIDs

        public static java.util.Set<java.lang.String> getShardDocUIDs​(IndexShard shard)
                                                               throws java.io.IOException
        Throws:
        java.io.IOException
      • getDocIdAndSeqNos

        public static java.util.List<DocIdSeqNoAndSource> getDocIdAndSeqNos​(IndexShard shard)
                                                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • assertDocCount

        protected void assertDocCount​(IndexShard shard,
                                      int docDount)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • assertDocs

        protected void assertDocs​(IndexShard shard,
                                  java.lang.String... ids)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • assertConsistentHistoryBetweenTranslogAndLucene

        public static void assertConsistentHistoryBetweenTranslogAndLucene​(IndexShard shard)
                                                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • indexDoc

        protected Engine.IndexResult indexDoc​(IndexShard shard,
                                              java.lang.String type,
                                              java.lang.String id)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • indexDoc

        protected Engine.IndexResult indexDoc​(IndexShard shard,
                                              java.lang.String type,
                                              java.lang.String id,
                                              java.lang.String source)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • indexDoc

        protected Engine.IndexResult indexDoc​(IndexShard shard,
                                              java.lang.String type,
                                              java.lang.String id,
                                              java.lang.String source,
                                              XContentType xContentType,
                                              java.lang.String routing)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • deleteDoc

        protected Engine.DeleteResult deleteDoc​(IndexShard shard,
                                                java.lang.String type,
                                                java.lang.String id)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • flushShard

        protected void flushShard​(IndexShard shard)
      • flushShard

        protected void flushShard​(IndexShard shard,
                                  boolean force)
      • recoverShardFromSnapshot

        protected void recoverShardFromSnapshot​(IndexShard shard,
                                                Snapshot snapshot,
                                                Repository repository)
        Recover a shard from a snapshot using a given repository
      • snapshotShard

        protected void snapshotShard​(IndexShard shard,
                                     Snapshot snapshot,
                                     Repository repository)
                              throws java.io.IOException
        Snapshot a shard using a given repository
        Throws:
        java.io.IOException
      • getEngine

        public static Engine getEngine​(IndexShard indexShard)
        Helper method to access (package-protected) engine from tests