Shield: Give native stores possibility to exit poller loop

Similar to the lifecycle services, stopping the shield lifecycle should
also ensure that the poller threads are stopped, which is tricky, in case
they run through huge user/role lists.

Original commit: elastic/x-pack-elasticsearch@7a48f19853
This commit is contained in:
Alexander Reelsen 2016-01-28 18:30:27 +01:00
parent 7331f8b179
commit 8635d264ae
2 changed files with 28 additions and 0 deletions

View File

@ -532,6 +532,9 @@ public class ESNativeUsersStore extends AbstractComponent implements ClusterStat
@Override @Override
public void doRun() { public void doRun() {
if (isStopped()) {
return;
}
if (shieldIndexExists == false) { if (shieldIndexExists == false) {
logger.trace("cannot poll for user changes since shield admin index [{}] does not exist", ShieldTemplateService.SHIELD_ADMIN_INDEX_NAME); logger.trace("cannot poll for user changes since shield admin index [{}] does not exist", ShieldTemplateService.SHIELD_ADMIN_INDEX_NAME);
return; return;
@ -568,6 +571,11 @@ public class ESNativeUsersStore extends AbstractComponent implements ClusterStat
} }
} }
// exit before comparing with known users
if (isStopped()) {
return;
}
// we now have a list of users that were in our version map and have been deleted // we now have a list of users that were in our version map and have been deleted
Iterator<ObjectCursor<String>> userIter = knownUsers.iterator(); Iterator<ObjectCursor<String>> userIter = knownUsers.iterator();
while (userIter.hasNext()) { while (userIter.hasNext()) {
@ -619,6 +627,10 @@ public class ESNativeUsersStore extends AbstractComponent implements ClusterStat
boolean keepScrolling = response.getHits().getHits().length > 0; boolean keepScrolling = response.getHits().getHits().length > 0;
while (keepScrolling) { while (keepScrolling) {
if (isStopped()) {
// instead of throwing an exception we return an empty map so nothing is processed and we exit early
return new ObjectLongHashMap<>();
}
for (SearchHit hit : response.getHits().getHits()) { for (SearchHit hit : response.getHits().getHits()) {
String username = hit.id(); String username = hit.id();
long version = hit.version(); long version = hit.version();
@ -638,6 +650,11 @@ public class ESNativeUsersStore extends AbstractComponent implements ClusterStat
} }
return map; return map;
} }
private boolean isStopped() {
State state = state();
return state == State.STOPPED || state == State.STOPPING;
}
} }
public static class Fields { public static class Fields {

View File

@ -460,6 +460,9 @@ public class ESNativeRolesStore extends AbstractComponent implements RolesStore,
@Override @Override
protected void doRun() throws Exception { protected void doRun() throws Exception {
if (isStopped()) {
return;
}
if (shieldIndexExists == false) { if (shieldIndexExists == false) {
logger.trace("cannot poll for role changes since shield admin index [{}] does not exist", ShieldTemplateService.SHIELD_ADMIN_INDEX_NAME); logger.trace("cannot poll for role changes since shield admin index [{}] does not exist", ShieldTemplateService.SHIELD_ADMIN_INDEX_NAME);
return; return;
@ -485,6 +488,9 @@ public class ESNativeRolesStore extends AbstractComponent implements RolesStore,
boolean keepScrolling = response.getHits().getHits().length > 0; boolean keepScrolling = response.getHits().getHits().length > 0;
while (keepScrolling) { while (keepScrolling) {
if (isStopped()) {
return;
}
for (final SearchHit hit : response.getHits().getHits()) { for (final SearchHit hit : response.getHits().getHits()) {
final String roleName = hit.getId(); final String roleName = hit.getId();
final long version = hit.version(); final long version = hit.version();
@ -530,6 +536,11 @@ public class ESNativeRolesStore extends AbstractComponent implements RolesStore,
public void onFailure(Throwable t) { public void onFailure(Throwable t) {
logger.error("error occurred while checking the native roles for changes", t); logger.error("error occurred while checking the native roles for changes", t);
} }
private boolean isStopped() {
State state = state();
return state == State.STOPPED || state == State.STOPPING;
}
} }
private static class RoleAndVersion { private static class RoleAndVersion {