シャーディングされたクラスターでのインデックスビルドの作成
このタスクについて
ローリング処理によるインデックスビルドは、デフォルトのインデックスビルドの代替となります。
配置が次のいずれかのケースに一致する場合にのみ、ローリングインデックス構築を使用してください。
平均 CPU 使用率が (N-1)/N-10% を超える場合 (N はmongodで使用可能な CPU スレッド数)
WiredTigerキャッシュ入力率が定期的に 90% を超える場合
Tip
Atlas を使用すると、クラスターを一時的に増やす、従来のインデックス構築の要件を満たすことができます。ただし、Atlas ではクラスターを増やすには料金が請求されます。詳細については、「 クラスター構成のコスト 」を参照してください。
Considerations
Unique Indexes
以下の手順を使用して一意なインデックスを作成するには、以下の手順を使用して、この手順の実行中にコレクションへのすべての書き込みを停止する必要があります。
If you cannot stop all writes to the collection during this procedure,
do not use the procedure on this page. Instead, build your unique index
on the collection by issuing db.collection.createIndex()
on
the mongos
for a sharded cluster.
Oplog サイズ
インデックス作成または再インデックス作成操作が遅れることなく完了できるように、oplog が十分に大きいことを確認してください。詳細については、oplog のサイズ設定に関するその他の情報を参照してください。
始める前に
- 一意なインデックスの構築
To create unique indexes using the following procedure, you must stop all writes to the collection during the index build. Otherwise, you may end up with inconsistent data across the replica set members. If you cannot stop all writes to the collection, do not use the following procedure to create unique indexes.
警告
コレクションへのすべての書き込みを停止できない場合は、一意なインデックスを作成する以下の手順を使用しないでください。
Before creating the index, validate that no documents in the collection violate the index constraints. If a collection is distributed across shards and a shard contains a chunk with duplicate documents, the create index operation may succeed on the shards without duplicates but not on the shard with duplicates. To avoid leaving inconsistent indexes across shards, you can issue the
db.collection.dropIndex()
from amongos
to drop the index from the collection.
MongoDB 8.0以降では、 directShardOperations
ロールを使用して、シャードに対してコマンドを直接実行する必要があるメンテナンス操作を実行できます。
警告
directShardOperations
ロールを使用して コマンドを実行すると、クラスターが正しく動作しなくなり、データが破損する可能性があります。 directShardOperations
ロールは、メンテナンス目的で、または MongoDB サポートのガイダンスに必ず従う必要があります。 メンテナンス操作を実行したら、 directShardOperations
ロールの使用を停止します。
手順
重要
The following procedure to build indexes in a rolling fashion applies to sharded clusters deployments, and not replica set deployments. For the procedure for replica sets, see レプリカセットでのローリングインデックスビルドの作成 instead.
A. Stop the Balancer
接続 mongosh
to a mongos
instance in the sharded cluster, and run sh.stopBalancer()
to
disable the balancer: [1]
sh.stopBalancer()
注意
If a migration is in progress, the system will complete the in-progress migration before stopping the balancer.
To verify that the balancer is disabled, run
sh.getBalancerState()
, which returns false if the balancer
is disabled:
sh.getBalancerState()
[1] | MongoDB 6.0.3以降、 自動チャンク分割は実行されません。 これはバランシング ポリシーの改善によるものです。 自動分割コマンドは引き続き存在しますが、操作は実行されません6.0.3より前のバージョンの MongoDB では、 sh.stopBalancer() は、シャーディングされたクラスターの自動分割も無効にします。 |
B. Determine the Distribution of the Collection
From mongosh
connected to the
mongos
, refresh the cached routing table for that
mongos
to avoid returning stale distribution information
for the collection. Once refreshed, run
db.collection.getShardDistribution()
for the collection you
wish to build the index.
For example, if you want to create an ascending index
on the records
collection in the test
database:
db.adminCommand( { flushRouterConfig: "test.records" } ); db.records.getShardDistribution();
The method outputs the shard distribution. For example, consider a
sharded cluster with 3 shards shardA
, shardB
, and shardC
and the db.collection.getShardDistribution()
returns the
following:
Shard shardA at shardA/s1-mongo1.example.net:27018,s1-mongo2.example.net:27018,s1-mongo3.example.net:27018 data : 1KiB docs : 50 chunks : 1 estimated data per chunk : 1KiB estimated docs per chunk : 50 Shard shardC at shardC/s3-mongo1.example.net:27018,s3-mongo2.example.net:27018,s3-mongo3.example.net:27018 data : 1KiB docs : 50 chunks : 1 estimated data per chunk : 1KiB estimated docs per chunk : 50 Totals data : 3KiB docs : 100 chunks : 2 Shard shardA contains 50% data, 50% docs in cluster, avg obj size on shard : 40B Shard shardC contains 50% data, 50% docs in cluster, avg obj size on shard : 40B
From the output, you only build the indexes for test.records
on
shardA
and shardC
.
C. Build Indexes on the Shards That Contain Collection Chunks
For each shard that contains chunks for the collection, follow the procedure to build the index on the shard.
C1. Stop One Secondary and Restart as a Standalone
For an affected shard, stop the mongod
process
associated with one of its secondary. Restart after making the following
configuration updates:
構成ファイルを使用している場合は、次の構成を更新します。
Change the
net.port
to a different port. [2] Make a note of the original port setting as a comment.replication.replSetName
オプションをコメントアウトします。sharding.clusterRole
オプションをコメントアウトします。Set parameter
skipShardingConfigurationChecks
totrue
in thesetParameter
section.setParameter
セクションでパラメータ
disableLogicalSessionCacheRefresh
をtrue
に設定します。
For example, for a shard replica set member, the updated configuration file will include content like the following example:
net: bindIp: localhost,<hostname(s)|ip address(es)> port: 27218 # port: 27018 #replication: # replSetName: shardA #sharding: # clusterRole: shardsvr setParameter: skipShardingConfigurationChecks: true disableLogicalSessionCacheRefresh: true
そして再起動します。
mongod --config <path/To/ConfigFile>
その他の設定(例: storage.dbPath
など)は同じままです。
コマンドライン オプションを使用する場合は、次の構成更新を行います。
削除
--shardsvr
if a shard member and--configsvr
if a config server member.Set parameter
skipShardingConfigurationChecks
totrue
in the--setParameter
option.--setParameter
オプションでパラメータ
disableLogicalSessionCacheRefresh
をtrue
に設定します。
For example, restart your shard replica set member
without the --replSet
and
--shardsvr
options.
Specify a new port number and set both the
skipShardingConfigurationChecks
and
disableLogicalSessionCacheRefresh
parameters to
true:
mongod --port 27218 --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true
その他の設定(例: --dbpath
など)は同じままです。
[2] | (1、2)mongod を別のポートで実行することで、インデックスの構築中にレプリカセットの他のノードと全クライアントがそのノードにコンタクトしないようにします。 |
C2. Build the Index
新しいポートでスタンドアロンとして実行されている mongod
インスタンスに直接接続し、このインスタンスの新しいインデックスを作成します。
For example, connect mongosh
to the instance,
and use the db.collection.createIndex()
method to create
an ascending index on the username
field of the records
collection:
db.records.createIndex( { username: 1 } )
C3. Restart the Program mongod
as a Replica Set Member
When the index build completes, shutdown the mongod
instance. Undo the configuration changes made when starting as a
standalone to return to its original configuration and restart.
重要
Be sure to remove the
skipShardingConfigurationChecks
parameter and
disableLogicalSessionCacheRefresh
parameter.
For example, to restart your replica set shard member:
構成ファイルを使用している場合:
元のポート番号に戻します。
replication.replSetName
のコメントアウトを外します。
sharding.clusterRole
のコメントアウトを外します。
Remove parameter
skipShardingConfigurationChecks
in thesetParameter
section.setParameter
セクションのパラメータ
disableLogicalSessionCacheRefresh
を削除します。
net: bindIp: localhost,<hostname(s)|ip address(es)> port: 27018 replication: replSetName: shardA sharding: clusterRole: shardsvr
その他の設定(例: storage.dbPath
など)は同じままです。
そして再起動します。
mongod --config <path/To/ConfigFile>
If you are using command-line options:
元のポート番号に戻します。
Include
--replSet
.Include
--shardsvr
if a shard member or--configsvr
if a config server member.Remove parameter
skipShardingConfigurationChecks
.Remove parameter
disableLogicalSessionCacheRefresh
.
以下に例を挙げます。
mongod --port 27018 --replSet shardA --shardsvr
その他の設定(例: --dbpath
など)は同じままです。
Allow replication to catch up on this member.
C4. Repeat the Procedure for the Remaining Secondaries for the Shard
Once the member catches up with the other members of the set, repeat the procedure one member at a time for the remaining secondary members for the shard:
C5. Build the Index on the Primary
When all the secondaries for the shard have the new index, step down the primary for the shard, restart it as a standalone using the procedure described above, and build the index on the former primary:
rs.stepDown()
mongosh
プライマリを降格するには、 で メソッドを使用します。ステップダウンが成功すると、現在のプライマリがセカンダリになり、レプリカセット ノードによって新しいプライマリが選択されます。
D. Repeat for the Other Affected Shards
Once you finish building the index for a shard, repeat C. Build Indexes on the Shards That Contain Collection Chunks for the other affected shards.
E. Restart the Balancer
Once you finish the rolling index build for the affected shards, restart the balancer.
接続 mongosh
to a mongos
instance in the sharded cluster, and run sh.startBalancer()
: [3]
sh.startBalancer()
[3] | MongoDB 6.0.3以降、 自動チャンク分割は実行されません。 これはバランシング ポリシーの改善によるものです。 自動分割コマンドは引き続き存在しますが、操作は実行されません6.0.3より前のバージョンの MongoDB では、 sh.startBalancer() は、シャーディングされたクラスターの自動分割も有効にします。 |
詳細情報
コレクションのチャンクを含む各シャードにまったく同じインデックス(インデックスオプションを含む)がない場合、シャーディングされたコレクションのインデックスには一貫性がありません。通常の操作では一貫性のないインデックスは発生しないはずですが、次のような一貫性のないインデックスが発生する可能性があります。
ユーザーが
unique
キー制約を使用してインデックスを作成していて、1 つのシャードに重複ドキュメントを含むチャンクが含まれている場合。このような場合、インデックスの作成操作は、重複のないシャードでは成功するものの、重複のあるシャードでは成功しない可能性があります。When a user is creating an index across the shards in a rolling manner but either fails to build the index for an associated shard or incorrectly builds an index with different specification.
コンフィギュレーションサーバーのプライマリは、シャーディングされたコレクションのシャード間でのインデックスの不整合を定期的にチェックします。 これらの定期的チェックを構成するには、 enableShardedIndexConsistencyCheck
とshardedIndexConsistencyCheckIntervalMS
を参照してください。
コマンドserverStatus
は、構成サーバーのプライマリで実行された場合にインデックスの不整合を報告するフィールド shardedIndexConsistency
を返します。
シャーディングされたコレクションに一貫性のないインデックスがないかどうかを確認するには、「シャード間で一貫性のないインデックスを検索する」を参照してください。