Skip to content

Commit

Permalink
Improve error message when resource group selector cannot be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
wenleix committed Aug 29, 2019
1 parent 95b2b33 commit d960aef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
import static com.google.common.base.Preconditions.checkState;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.airlift.units.Duration.succinctNanos;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

public class DbResourceGroupConfigurationManager
extends AbstractResourceConfigurationManager
Expand Down Expand Up @@ -116,9 +118,8 @@ protected Optional<Duration> getCpuQuotaPeriod()
@Override
protected List<ResourceGroupSpec> getRootGroups()
{
if (lastRefresh.get() == 0) {
throw new PrestoException(CONFIGURATION_UNAVAILABLE, "Root groups cannot be fetched from database");
}
checkMaxRefreshInterval();

if (this.selectors.get().isEmpty()) {
throw new PrestoException(CONFIGURATION_INVALID, "No root groups are configured");
}
Expand Down Expand Up @@ -156,9 +157,8 @@ public void configure(ResourceGroup group, SelectionContext<VariableMap> criteri
@Override
public Optional<SelectionContext<VariableMap>> match(SelectionCriteria criteria)
{
if (lastRefresh.get() == 0) {
throw new PrestoException(CONFIGURATION_UNAVAILABLE, "Selectors cannot be fetched from database");
}
checkMaxRefreshInterval();

if (selectors.get().isEmpty()) {
throw new PrestoException(CONFIGURATION_INVALID, "No selectors are configured");
}
Expand All @@ -173,9 +173,8 @@ public Optional<SelectionContext<VariableMap>> match(SelectionCriteria criteria)
@VisibleForTesting
public List<ResourceGroupSelector> getSelectors()
{
if (lastRefresh.get() == 0) {
throw new PrestoException(CONFIGURATION_UNAVAILABLE, "Selectors cannot be fetched from database");
}
checkMaxRefreshInterval();

if (selectors.get().isEmpty()) {
throw new PrestoException(CONFIGURATION_INVALID, "No selectors are configured");
}
Expand Down Expand Up @@ -237,11 +236,11 @@ public synchronized void load()
lastRefresh.set(System.nanoTime());
}
catch (Throwable e) {
if (succinctNanos(System.nanoTime() - lastRefresh.get()).compareTo(maxRefreshInterval) > 0) {
lastRefresh.set(0);
}
refreshFailures.update(1);
log.error(e, "Error loading configuration from db");
if (lastRefresh.get() != 0) {
log.debug("Last successful configuration loading was %s ago", succinctNanos(System.nanoTime() - lastRefresh.get()).toString());
}
}
}

Expand Down Expand Up @@ -367,6 +366,18 @@ private ResourceGroup getRootGroup(ResourceGroupId groupId)
return groups.get(groupId);
}

private void checkMaxRefreshInterval()
{
if (System.nanoTime() - lastRefresh.get() > maxRefreshInterval.toMillis() * MILLISECONDS.toNanos(1)) {
String message = "Resource group configuration cannot be fetched from database.";
if (lastRefresh.get() != 0) {
message += format(" Current resource group configuration is loaded %s ago", succinctNanos(System.nanoTime() - lastRefresh.get()).toString());
}

throw new PrestoException(CONFIGURATION_UNAVAILABLE, message);
}
}

@Managed
@Nested
public CounterStat getRefreshFailures()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ public void testRefreshInterval()
fail("Expected unavailable configuration exception");
}
catch (Exception e) {
assertEquals(e.getMessage(), "Selectors cannot be fetched from database");
assertTrue(e.getMessage().startsWith("Resource group configuration cannot be fetched from database."));
}

try {
manager.getRootGroups();
fail("Expected unavailable configuration exception");
}
catch (Exception e) {
assertEquals(e.getMessage(), "Root groups cannot be fetched from database");
assertTrue(e.getMessage().startsWith("Resource group configuration cannot be fetched from database."));
}

manager.destroy();
Expand Down

0 comments on commit d960aef

Please sign in to comment.