Search in sources :

Example 11 with ResourceGroupId

use of io.trino.spi.resourcegroups.ResourceGroupId in project trino by trinodb.

the class TestStaticSelector method testSourceRegex.

@Test
public void testSourceRegex() {
    ResourceGroupId resourceGroupId = new ResourceGroupId(new ResourceGroupId("global"), "foo");
    StaticSelector selector = new StaticSelector(Optional.empty(), Optional.empty(), Optional.of(Pattern.compile(".*source.*")), Optional.empty(), Optional.empty(), Optional.empty(), new ResourceGroupIdTemplate("global.foo"));
    assertEquals(selector.match(newSelectionCriteria("userA", null, ImmutableSet.of("tag1"), EMPTY_RESOURCE_ESTIMATES)), Optional.empty());
    assertEquals(selector.match(newSelectionCriteria("userB", "source", ImmutableSet.of(), EMPTY_RESOURCE_ESTIMATES)).map(SelectionContext::getResourceGroupId), Optional.of(resourceGroupId));
    assertEquals(selector.match(newSelectionCriteria("A.user", "a source b", ImmutableSet.of("tag1"), EMPTY_RESOURCE_ESTIMATES)).map(SelectionContext::getResourceGroupId), Optional.of(resourceGroupId));
}
Also used : ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) Test(org.testng.annotations.Test)

Example 12 with ResourceGroupId

use of io.trino.spi.resourcegroups.ResourceGroupId in project trino by trinodb.

the class AbstractResourceConfigurationManager method parentGroupContext.

@Override
public SelectionContext<ResourceGroupIdTemplate> parentGroupContext(SelectionContext<ResourceGroupIdTemplate> context) {
    ResourceGroupId parentGroupId = context.getResourceGroupId().getParent().orElseThrow(() -> new IllegalArgumentException("Group has no parent group: " + context.getResourceGroupId()));
    List<ResourceGroupNameTemplate> parentGroupIdTemplate = new ArrayList<>(context.getContext().getSegments());
    parentGroupIdTemplate.remove(parentGroupIdTemplate.size() - 1);
    return new SelectionContext<>(parentGroupId, ResourceGroupIdTemplate.fromSegments(parentGroupIdTemplate));
}
Also used : ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) ArrayList(java.util.ArrayList) SelectionContext(io.trino.spi.resourcegroups.SelectionContext)

Example 13 with ResourceGroupId

use of io.trino.spi.resourcegroups.ResourceGroupId in project trino by trinodb.

the class StaticSelector method match.

@Override
public Optional<SelectionContext<ResourceGroupIdTemplate>> match(SelectionCriteria criteria) {
    Map<String, String> variables = new HashMap<>();
    if (userRegex.isPresent()) {
        Matcher userMatcher = userRegex.get().matcher(criteria.getUser());
        if (!userMatcher.matches()) {
            return Optional.empty();
        }
        addVariableValues(userRegex.get(), criteria.getUser(), variables);
    }
    if (userGroupRegex.isPresent() && criteria.getUserGroups().stream().noneMatch(group -> userGroupRegex.get().matcher(group).matches())) {
        return Optional.empty();
    }
    if (sourceRegex.isPresent()) {
        String source = criteria.getSource().orElse("");
        if (!sourceRegex.get().matcher(source).matches()) {
            return Optional.empty();
        }
        addVariableValues(sourceRegex.get(), source, variables);
    }
    if (!clientTags.isEmpty() && !criteria.getTags().containsAll(clientTags)) {
        return Optional.empty();
    }
    if (selectorResourceEstimate.isPresent() && !selectorResourceEstimate.get().match(criteria.getResourceEstimates())) {
        return Optional.empty();
    }
    if (queryType.isPresent()) {
        String contextQueryType = criteria.getQueryType().orElse("");
        if (!queryType.get().equalsIgnoreCase(contextQueryType)) {
            return Optional.empty();
        }
    }
    variables.putIfAbsent(USER_VARIABLE, criteria.getUser());
    // Special handling for source, which is an optional field that is part of the standard variables
    variables.putIfAbsent(SOURCE_VARIABLE, criteria.getSource().orElse(""));
    ResourceGroupId id = group.expandTemplate(new VariableMap(variables));
    return Optional.of(new SelectionContext<>(id, group));
}
Also used : ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) ImmutableSet(com.google.common.collect.ImmutableSet) SelectionContext(io.trino.spi.resourcegroups.SelectionContext) Set(java.util.Set) HashMap(java.util.HashMap) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) SelectionCriteria(io.trino.spi.resourcegroups.SelectionCriteria) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher)

Example 14 with ResourceGroupId

use of io.trino.spi.resourcegroups.ResourceGroupId in project trino by trinodb.

the class TestResourceGroupsDao method testExactMatchSelector.

@Test
public void testExactMatchSelector() {
    H2ResourceGroupsDao dao = setup("exact_match_selector");
    dao.createExactMatchSelectorsTable();
    ResourceGroupId resourceGroupId1 = new ResourceGroupId(ImmutableList.of("global", "test", "user", "insert"));
    ResourceGroupId resourceGroupId2 = new ResourceGroupId(ImmutableList.of("global", "test", "user", "select"));
    JsonCodec<ResourceGroupId> codec = JsonCodec.jsonCodec(ResourceGroupId.class);
    dao.insertExactMatchSelector("test", "@test@test_pipeline", INSERT.name(), codec.toJson(resourceGroupId1));
    dao.insertExactMatchSelector("test", "@test@test_pipeline", SELECT.name(), codec.toJson(resourceGroupId2));
    assertNull(dao.getExactMatchResourceGroup("test", "@test@test_pipeline", null));
    assertEquals(dao.getExactMatchResourceGroup("test", "@test@test_pipeline", INSERT.name()), codec.toJson(resourceGroupId1));
    assertEquals(dao.getExactMatchResourceGroup("test", "@test@test_pipeline", SELECT.name()), codec.toJson(resourceGroupId2));
    assertNull(dao.getExactMatchResourceGroup("test", "@test@test_pipeline", DELETE.name()));
    assertNull(dao.getExactMatchResourceGroup("test", "abc", INSERT.name()));
    assertNull(dao.getExactMatchResourceGroup("prod", "@test@test_pipeline", INSERT.name()));
}
Also used : ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) Test(org.testng.annotations.Test)

Example 15 with ResourceGroupId

use of io.trino.spi.resourcegroups.ResourceGroupId in project trino by trinodb.

the class TestQueues method assertResourceGroup.

private void assertResourceGroup(DistributedQueryRunner queryRunner, Session session, String query, ResourceGroupId expectedResourceGroup) throws InterruptedException {
    QueryId queryId = createQuery(queryRunner, session, query);
    waitForQueryState(queryRunner, queryId, ImmutableSet.of(RUNNING, FINISHED));
    Optional<ResourceGroupId> resourceGroupId = queryRunner.getCoordinator().getQueryManager().getFullQueryInfo(queryId).getResourceGroupId();
    assertTrue(resourceGroupId.isPresent(), "Query should have a resource group");
    assertEquals(resourceGroupId.get(), expectedResourceGroup, format("Expected: '%s' resource group, found: %s", expectedResourceGroup, resourceGroupId.get()));
}
Also used : ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) QueryId(io.trino.spi.QueryId)

Aggregations

ResourceGroupId (io.trino.spi.resourcegroups.ResourceGroupId)30 Test (org.testng.annotations.Test)17 SelectionCriteria (io.trino.spi.resourcegroups.SelectionCriteria)5 TestQueues.createResourceGroupId (io.trino.execution.TestQueues.createResourceGroupId)4 QueryId (io.trino.spi.QueryId)4 Pattern (java.util.regex.Pattern)4 Session (io.trino.Session)3 AccessControlConfig (io.trino.security.AccessControlConfig)3 AccessControlManager (io.trino.security.AccessControlManager)3 ResourceGroupInfo (io.trino.server.ResourceGroupInfo)3 SelectionContext (io.trino.spi.resourcegroups.SelectionContext)3 InMemoryTransactionManager.createTestTransactionManager (io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager)3 TransactionManager (io.trino.transaction.TransactionManager)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 H2TestUtil.adhocSession (io.trino.execution.resourcegroups.db.H2TestUtil.adhocSession)2 H2TestUtil.dashboardSession (io.trino.execution.resourcegroups.db.H2TestUtil.dashboardSession)2 H2TestUtil.rejectingSession (io.trino.execution.resourcegroups.db.H2TestUtil.rejectingSession)2 ResourceGroup (io.trino.spi.resourcegroups.ResourceGroup)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2