Skip to content

Commit

Permalink
wait for active shards on index creation
Browse files Browse the repository at this point in the history
  • Loading branch information
n1v0lg committed Sep 17, 2024
1 parent e5d0213 commit 345a4ec
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public abstract class NativeRealmIntegTestCase extends SecurityIntegTestCase {

@Before
public void ensureNativeStoresStarted() throws Exception {
createSecurityIndex();
assertSecurityIndexActive();
createSecurityIndexWithWaitForActiveShards();
if (shouldSetReservedUserPasswords()) {
setupReservedPasswords();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ protected boolean addMockHttpTransport() {

@Before
public void waitForSecurityIndexWritable() throws Exception {
createSecurityIndex();
assertSecurityIndexActive();
createSecurityIndexWithWaitForActiveShards();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,7 @@ public void testAuthenticateWithWrongToken() throws Exception {

@Before
public void waitForSecurityIndexWritable() throws Exception {
createSecurityIndex();
assertSecurityIndexActive();
createSecurityIndexWithWaitForActiveShards();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
public class SecurityScrollTests extends SecurityIntegTestCase {

public void testScrollIsPerUser() throws Exception {
createSecurityIndex();
assertSecurityIndexActive();
createSecurityIndexWithWaitForActiveShards();
new PutRoleRequestBuilder(client()).name("scrollable")
.addIndices(new String[] { randomAlphaOfLengthBetween(4, 12) }, new String[] { "read" }, null, null, null, randomBoolean())
.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testOnIndexAvailableForSearchIndexCompletesWithinTimeout() throws Ex
// pick longer wait than in the assertBusy that waits for below to ensure index has had enough time to initialize
securityIndexManager.onIndexAvailableForSearch((ActionListener<Void>) future, TimeValue.timeValueSeconds(40));

createSecurityIndex();
createSecurityIndexWithWaitForActiveShards();

assertBusy(
() -> assertThat(securityIndexManager.isAvailable(SecurityIndexManager.Availability.SEARCH_SHARDS), is(true)),
Expand All @@ -125,7 +125,7 @@ public void testOnIndexAvailableForSearchIndexCompletesWithinTimeout() throws Ex

@SuppressWarnings("unchecked")
public void testOnIndexAvailableForSearchIndexAlreadyAvailable() throws Exception {
createSecurityIndex();
createSecurityIndexWithWaitForActiveShards();

final SecurityIndexManager securityIndexManager = internalCluster().getInstances(NativePrivilegeStore.class)
.iterator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.internal.Client;
Expand Down Expand Up @@ -383,7 +384,9 @@ protected Function<Client, Client> getClientWrapper() {

/**
* Waits for security index to become available. Note that you must ensure index creation was triggered before calling this method,
* either by calling one of the resource creation APIs (e.g., creating a user), or by calling {@link #createSecurityIndex()}.
* by calling one of the resource creation APIs (e.g., creating a user).
* If you use {@link #createSecurityIndexWithWaitForActiveShards()} to create the index it's not necessary call
* {@link #assertSecurityIndexActive} since the create method ensures the index is active.
*/
public void assertSecurityIndexActive() throws Exception {
assertSecurityIndexActive(cluster());
Expand Down Expand Up @@ -424,7 +427,7 @@ protected void deleteSecurityIndex() {
}
}

protected void createSecurityIndex() {
protected void createSecurityIndexWithWaitForActiveShards() {
final Client client = client().filterWithHeader(
Collections.singletonMap(
"Authorization",
Expand All @@ -434,7 +437,8 @@ protected void createSecurityIndex() {
)
)
);
CreateIndexRequest createIndexRequest = new CreateIndexRequest(SECURITY_MAIN_ALIAS);
CreateIndexRequest createIndexRequest = new CreateIndexRequest(SECURITY_MAIN_ALIAS).waitForActiveShards(ActiveShardCount.ALL)
.masterNodeTimeout(TEST_REQUEST_TIMEOUT);
client.admin().indices().create(createIndexRequest).actionGet();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ protected Settings buildRealmSettings(

@Before
public void setupRoleMappings() throws Exception {
createSecurityIndex();
assertSecurityIndexActive();
createSecurityIndexWithWaitForActiveShards();

List<String> content = getRoleMappingContent(RoleMappingEntry::nativeContent);
if (content.isEmpty()) {
Expand Down

0 comments on commit 345a4ec

Please sign in to comment.