Search in sources :

Example 11 with ScheduledQueryKey

use of org.apache.hadoop.hive.metastore.api.ScheduledQueryKey in project hive by apache.

the class TestScheduledQueryStatements method testAlter.

@Test
public void testAlter() throws ParseException, Exception {
    IDriver driver = createDriver();
    driver.run("set role admin");
    driver.run("create scheduled query alter1 cron '0 0 7 * * ? *' as select 1 from tu");
    driver.run("alter scheduled query alter1 executed as 'user3'");
    driver.run("alter scheduled query alter1 defined as select 22 from tu");
    try (CloseableObjectStore os = new CloseableObjectStore(env_setup.getTestCtx().hiveConf)) {
        Optional<MScheduledQuery> sq = os.getMScheduledQuery(new ScheduledQueryKey("alter1", "hive"));
        assertTrue(sq.isPresent());
        assertEquals("user3", sq.get().toThrift().getUser());
        assertThat(sq.get().getNextExecution(), Matchers.greaterThan((int) (System.currentTimeMillis() / 1000)));
    }
}
Also used : IDriver(org.apache.hadoop.hive.ql.IDriver) ScheduledQueryKey(org.apache.hadoop.hive.metastore.api.ScheduledQueryKey) MScheduledQuery(org.apache.hadoop.hive.metastore.model.MScheduledQuery) Test(org.junit.Test)

Example 12 with ScheduledQueryKey

use of org.apache.hadoop.hive.metastore.api.ScheduledQueryKey in project hive by apache.

the class ScheduledQueryAnalyzer method checkAuthorization.

private void checkAuthorization(ScheduledQueryMaintenanceRequestType type, ScheduledQuery schq) throws SemanticException {
    boolean schqAuthorization = (SessionState.get().getAuthorizerV2() != null) && conf.getBoolVar(ConfVars.HIVE_SECURITY_AUTHORIZATION_SCHEDULED_QUERIES_SUPPORTED);
    try {
        if (!schqAuthorization) {
            String currentUser = getUserName();
            if (!Objects.equal(currentUser, schq.getUser())) {
                throw new HiveAccessControlException("Authorization of scheduled queries is not enabled - only owners may change scheduled queries (currentUser: " + currentUser + ", owner: " + schq.getUser() + ")");
            }
        } else {
            HiveOperationType opType = toHiveOpType(type);
            List<HivePrivilegeObject> privObjects = new ArrayList<HivePrivilegeObject>();
            ScheduledQueryKey key = schq.getScheduleKey();
            privObjects.add(HivePrivilegeObject.forScheduledQuery(schq.getUser(), key.getClusterNamespace(), key.getScheduleName()));
            SessionState.get().getAuthorizerV2().checkPrivileges(opType, privObjects, privObjects, new HiveAuthzContext.Builder().build());
        }
    } catch (Exception e) {
        throw new SemanticException(e.getMessage(), e);
    }
}
Also used : HiveAccessControlException(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException) CronBuilder(com.cronutils.builder.CronBuilder) CronDefinitionBuilder(com.cronutils.model.definition.CronDefinitionBuilder) ArrayList(java.util.ArrayList) HiveOperationType(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType) HivePrivilegeObject(org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject) ScheduledQueryKey(org.apache.hadoop.hive.metastore.api.ScheduledQueryKey) HiveAccessControlException(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException) TException(org.apache.thrift.TException)

Example 13 with ScheduledQueryKey

use of org.apache.hadoop.hive.metastore.api.ScheduledQueryKey in project hive by apache.

the class TestMetastoreScheduledQueries method testPoll.

@Test
public void testPoll() throws Exception {
    ScheduledQuery schq = createScheduledQuery(new ScheduledQueryKey("q1", "polltest"));
    ScheduledQueryMaintenanceRequest r = new ScheduledQueryMaintenanceRequest();
    r.setType(ScheduledQueryMaintenanceRequestType.CREATE);
    r.setScheduledQuery(schq);
    client.scheduledQueryMaintenance(r);
    schq.setScheduleKey(new ScheduledQueryKey("q1", "polltestOther"));
    client.scheduledQueryMaintenance(r);
    // disabled queries are not considered
    schq.setScheduleKey(new ScheduledQueryKey("q2disabled", "polltest"));
    schq.setEnabled(false);
    client.scheduledQueryMaintenance(r);
    // do some poll requests; and wait for q1's execution
    ScheduledQueryPollRequest request = new ScheduledQueryPollRequest();
    request.setClusterNamespace("polltest");
    ScheduledQueryPollResponse pollResult = null;
    // wait for poll to hit
    for (int i = 0; i < 30; i++) {
        pollResult = client.scheduledQueryPoll(request);
        if (pollResult.isSetQuery()) {
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(pollResult.isSetQuery());
    assertTrue(pollResult.isSetScheduleKey());
    assertTrue(pollResult.isSetExecutionId());
    // after reading the only scheduled query; there are no more queries to run (for 1 sec)
    ScheduledQueryPollResponse pollResult2 = client.scheduledQueryPoll(request);
    assertTrue(!pollResult2.isSetQuery());
    try (PersistenceManager pm = PersistenceManagerProvider.getPersistenceManager()) {
        MScheduledExecution q = pm.getObjectById(MScheduledExecution.class, pollResult.getExecutionId());
        assertNotNull(q);
        assertEquals(QueryState.INITED, q.getState());
        assertTrue(q.getStartTime() <= getEpochSeconds());
        assertTrue(q.getStartTime() >= getEpochSeconds() - 1);
        assertTrue(q.getEndTime() == null);
        assertTrue(q.getLastUpdateTime() <= getEpochSeconds());
        assertTrue(q.getLastUpdateTime() >= getEpochSeconds() - 1);
    }
    // wait 1 sec
    Thread.sleep(1000);
    ScheduledQueryProgressInfo info;
    info = new ScheduledQueryProgressInfo(pollResult.getExecutionId(), QueryState.EXECUTING, "executor-query-id");
    client.scheduledQueryProgress(info);
    try (PersistenceManager pm = PersistenceManagerProvider.getPersistenceManager()) {
        MScheduledExecution q = pm.getObjectById(MScheduledExecution.class, pollResult.getExecutionId());
        assertEquals(QueryState.EXECUTING, q.getState());
        assertEquals("executor-query-id", q.getExecutorQueryId());
        assertTrue(q.getLastUpdateTime() <= getEpochSeconds());
        assertTrue(q.getLastUpdateTime() >= getEpochSeconds() - 1);
    }
    // wait 1 sec
    Thread.sleep(1000);
    info = new ScheduledQueryProgressInfo(pollResult.getExecutionId(), QueryState.FAILED, "executor-query-id");
    info.setErrorMessage(generateLongErrorMessage());
    // info.set
    client.scheduledQueryProgress(info);
    try (PersistenceManager pm = PersistenceManagerProvider.getPersistenceManager()) {
        MScheduledExecution q = pm.getObjectById(MScheduledExecution.class, pollResult.getExecutionId());
        assertEquals(QueryState.FAILED, q.getState());
        assertEquals("executor-query-id", q.getExecutorQueryId());
        assertNull(q.getLastUpdateTime());
        assertTrue(q.getEndTime() <= getEpochSeconds());
        assertTrue(q.getEndTime() >= getEpochSeconds() - 1);
        assertTrue(q.getErrorMessage().length() < 2000);
        assertFalse(q.getErrorMessage().contains("x"));
    }
    // clustername is taken into account; this should be empty
    request.setClusterNamespace("polltestSomethingElse");
    pollResult = client.scheduledQueryPoll(request);
    assertFalse(pollResult.isSetQuery());
}
Also used : ScheduledQueryPollRequest(org.apache.hadoop.hive.metastore.api.ScheduledQueryPollRequest) MScheduledExecution(org.apache.hadoop.hive.metastore.model.MScheduledExecution) PersistenceManager(javax.jdo.PersistenceManager) ScheduledQueryPollResponse(org.apache.hadoop.hive.metastore.api.ScheduledQueryPollResponse) ScheduledQueryProgressInfo(org.apache.hadoop.hive.metastore.api.ScheduledQueryProgressInfo) ScheduledQueryMaintenanceRequest(org.apache.hadoop.hive.metastore.api.ScheduledQueryMaintenanceRequest) ScheduledQueryKey(org.apache.hadoop.hive.metastore.api.ScheduledQueryKey) ScheduledQuery(org.apache.hadoop.hive.metastore.api.ScheduledQuery) MetastoreUnitTest(org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest) Test(org.junit.Test)

Example 14 with ScheduledQueryKey

use of org.apache.hadoop.hive.metastore.api.ScheduledQueryKey in project hive by apache.

the class TestMetastoreScheduledQueries method testDisableInternal.

/**
 * Simulates some schq failure scenario.
 *
 * Q1 is executed correctly 1 time; but every further executions are end with failures
 * Q2 should be executed without without issues even thru Q1 will be disabled at some point
 */
public void testDisableInternal(int q1NumberOfExecutions, int seconds, String testNamespace) throws Exception {
    int q2NumberOfExecutions = seconds - 1;
    ScheduledQueryKey schqKey1 = new ScheduledQueryKey("q1", testNamespace);
    ScheduledQueryKey schqKey2 = new ScheduledQueryKey("q2", testNamespace);
    createEverySecondSchq(schqKey1);
    createEverySecondSchq(schqKey2);
    // do some polls and report failure each time
    ScheduledQueryPollRequest request = new ScheduledQueryPollRequest();
    request.setClusterNamespace(testNamespace);
    ScheduledQueryPollResponse pollResult = null;
    int idx1 = 0;
    int idx2 = 0;
    for (int i = 0; i < seconds * 10 + 9; i++) {
        pollResult = client.scheduledQueryPoll(request);
        if (pollResult.isSetQuery()) {
            if (pollResult.getScheduleKey().equals(schqKey1)) {
                idx1++;
                if (idx1 == 1) {
                    ScheduledQueryProgressInfo info = new ScheduledQueryProgressInfo(pollResult.getExecutionId(), QueryState.FINISHED, "executor-query-id");
                    client.scheduledQueryProgress(info);
                } else {
                    ScheduledQueryProgressInfo info = new ScheduledQueryProgressInfo(pollResult.getExecutionId(), QueryState.FAILED, "executor-query-id");
                    info.setErrorMessage("some issue happened");
                    client.scheduledQueryProgress(info);
                }
                ScheduledQuery schq = client.getScheduledQuery(pollResult.getScheduleKey());
                if (idx1 > q1NumberOfExecutions) {
                    fail("unexpected execution of q1 happened");
                }
                if (idx1 == q1NumberOfExecutions) {
                    assertFalse("Scheduled query q1 must be disabled at this point", schq.isEnabled());
                } else {
                    assertTrue("Scheduled query q1 must be enabled at this point", schq.isEnabled());
                }
            }
            if (pollResult.getScheduleKey().equals(schqKey2)) {
                idx2++;
                ScheduledQueryProgressInfo info = new ScheduledQueryProgressInfo(pollResult.getExecutionId(), QueryState.FINISHED, "executor-query-id");
                client.scheduledQueryProgress(info);
                ScheduledQuery schq = client.getScheduledQuery(pollResult.getScheduleKey());
                assertTrue("Scheduled query q2 must be enabled", schq.isEnabled());
            }
        }
        Thread.sleep(100);
    }
    if (idx1 != q1NumberOfExecutions) {
        fail("expected " + q1NumberOfExecutions + " execution of q1; only " + idx1 + " happened");
    }
    if (idx2 < q2NumberOfExecutions) {
        fail("at least " + q2NumberOfExecutions + " expected for q2");
    }
}
Also used : ScheduledQueryPollRequest(org.apache.hadoop.hive.metastore.api.ScheduledQueryPollRequest) ScheduledQueryPollResponse(org.apache.hadoop.hive.metastore.api.ScheduledQueryPollResponse) ScheduledQueryProgressInfo(org.apache.hadoop.hive.metastore.api.ScheduledQueryProgressInfo) ScheduledQueryKey(org.apache.hadoop.hive.metastore.api.ScheduledQueryKey) ScheduledQuery(org.apache.hadoop.hive.metastore.api.ScheduledQuery)

Example 15 with ScheduledQueryKey

use of org.apache.hadoop.hive.metastore.api.ScheduledQueryKey in project hive by apache.

the class TestMetastoreScheduledQueries method testSkip2.

@Test
public void testSkip2() throws Exception {
    metaStore.getConf().set(MetastoreConf.ConfVars.SCHEDULED_QUERIES_AUTODISABLE_COUNT.getVarname(), "4");
    metaStore.getConf().set(MetastoreConf.ConfVars.SCHEDULED_QUERIES_SKIP_OPPORTUNITIES_AFTER_FAILURES.getVarname(), "2");
    client.close();
    client = metaStore.getClient();
    testDisableInternal(5, 6, "skip2");
    try (PersistenceManager pm = PersistenceManagerProvider.getPersistenceManager()) {
        ScheduledQueryKey key = new ScheduledQueryKey("q1", "skip2");
        Query query = pm.newQuery(MScheduledExecution.class);
        query.setOrdering("scheduledExecutionId descending");
        query.setRange(0, 20);
        List<MScheduledExecution> list = (List<MScheduledExecution>) query.execute();
        List<MScheduledExecution> q1list = new ArrayList<MScheduledExecution>();
        List<Integer> tList = new ArrayList<Integer>();
        for (MScheduledExecution schqExec : list) {
            if (schqExec.getScheduledQuery().getScheduleKey().equals(key)) {
                q1list.add(schqExec);
                tList.add(schqExec.getStartTime());
            }
        }
        tList = Lists.reverse(tList);
        Integer startTime = tList.get(0);
        tList = tList.stream().map(e -> e - startTime).collect(Collectors.toList());
        assertArrayEquals(new Integer[] { 0, 1, 2, 4, 6, 6 }, tList.toArray());
    }
}
Also used : ScheduledQuery(org.apache.hadoop.hive.metastore.api.ScheduledQuery) Query(javax.jdo.Query) MScheduledExecution(org.apache.hadoop.hive.metastore.model.MScheduledExecution) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ScheduledQueryKey(org.apache.hadoop.hive.metastore.api.ScheduledQueryKey) MetastoreUnitTest(org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest) Test(org.junit.Test)

Aggregations

ScheduledQueryKey (org.apache.hadoop.hive.metastore.api.ScheduledQueryKey)17 ScheduledQuery (org.apache.hadoop.hive.metastore.api.ScheduledQuery)11 Test (org.junit.Test)10 MetastoreUnitTest (org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest)7 ScheduledQueryMaintenanceRequest (org.apache.hadoop.hive.metastore.api.ScheduledQueryMaintenanceRequest)7 ScheduledQueryPollResponse (org.apache.hadoop.hive.metastore.api.ScheduledQueryPollResponse)5 PersistenceManager (javax.jdo.PersistenceManager)4 ScheduledQueryPollRequest (org.apache.hadoop.hive.metastore.api.ScheduledQueryPollRequest)4 MScheduledExecution (org.apache.hadoop.hive.metastore.model.MScheduledExecution)4 MScheduledQuery (org.apache.hadoop.hive.metastore.model.MScheduledQuery)4 IDriver (org.apache.hadoop.hive.ql.IDriver)4 ArrayList (java.util.ArrayList)2 ObjectStore (org.apache.hadoop.hive.metastore.ObjectStore)2 ScheduledQueryProgressInfo (org.apache.hadoop.hive.metastore.api.ScheduledQueryProgressInfo)2 CronBuilder (com.cronutils.builder.CronBuilder)1 CronDefinitionBuilder (com.cronutils.model.definition.CronDefinitionBuilder)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 Query (javax.jdo.Query)1