|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +suite("test_audit_log_queue_time", "nonConcurrent") { |
| 19 | + // Check admin privilege |
| 20 | + try { |
| 21 | + sql "set global enable_audit_plugin = true" |
| 22 | + } catch (Exception e) { |
| 23 | + log.warn("skip this case, because " + e.getMessage()) |
| 24 | + assertTrue(e.getMessage().toUpperCase().contains("ADMIN")) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + def tableName = "audit_queue_time_test" |
| 29 | + def wgName = "test_queue_time_wg" |
| 30 | + def testMarker = UUID.randomUUID().toString().substring(0, 8) |
| 31 | + |
| 32 | + // Cleanup environment |
| 33 | + sql "drop table if exists ${tableName}" |
| 34 | + sql "drop workload group if exists ${wgName}" |
| 35 | + |
| 36 | + // Create test table |
| 37 | + sql """ |
| 38 | + CREATE TABLE `${tableName}` ( |
| 39 | + `id` bigint, |
| 40 | + `name` varchar(32) |
| 41 | + ) ENGINE=OLAP |
| 42 | + DUPLICATE KEY(`id`) |
| 43 | + DISTRIBUTED BY HASH(`id`) BUCKETS 1 |
| 44 | + PROPERTIES ("replication_allocation" = "tag.location.default: 1") |
| 45 | + """ |
| 46 | + |
| 47 | + sql "insert into ${tableName} values (1, 'test')" |
| 48 | + |
| 49 | + def maxConcurrency = 1 |
| 50 | + // Create workload group: max_concurrency=1 ensures queries queue up |
| 51 | + sql """ |
| 52 | + create workload group ${wgName} |
| 53 | + properties ( |
| 54 | + 'max_concurrency' = '${maxConcurrency}', |
| 55 | + 'max_queue_size' = '10', |
| 56 | + 'queue_timeout' = '30000' |
| 57 | + ) |
| 58 | + """ |
| 59 | + |
| 60 | + // Wait for workload group to take effect |
| 61 | + Thread.sleep(5000) |
| 62 | + |
| 63 | + // Truncate audit_log for easier testing |
| 64 | + sql "truncate table __internal_schema.audit_log" |
| 65 | + |
| 66 | + // Submit concurrent queries with marker for later lookup |
| 67 | + def sqlSleepTime = 5 |
| 68 | + def queuedSqlCnt = 1 |
| 69 | + def threads = [] |
| 70 | + for (int i = 0; i < maxConcurrency + queuedSqlCnt; i++) { |
| 71 | + def idx = i |
| 72 | + threads << Thread.start { |
| 73 | + try { |
| 74 | + sql "set workload_group=${wgName}" |
| 75 | + // Use sleep function to simulate long query, ensuring subsequent queries need to queue |
| 76 | + sql """ |
| 77 | + select sleep(${sqlSleepTime}), '${testMarker}_${idx}' as marker |
| 78 | + from ${tableName} limit 1 |
| 79 | + """ |
| 80 | + } catch (Exception e) { |
| 81 | + log.warn("Query ${idx} failed: ${e.getMessage()}") |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Wait for all queries to complete |
| 87 | + threads.each { it.join() } |
| 88 | + |
| 89 | + // Wait for audit log to flush |
| 90 | + Thread.sleep(5000) |
| 91 | + sql "call flush_audit_log()" |
| 92 | + Thread.sleep(5000) |
| 93 | + |
| 94 | + // Verify queue_time_ms column exists |
| 95 | + def schemaResult = sql "desc internal.__internal_schema.audit_log" |
| 96 | + def hasQueueTimeMs = schemaResult.any { it[0] == "queue_time_ms" } |
| 97 | + assertTrue(hasQueueTimeMs) |
| 98 | + |
| 99 | + // check result |
| 100 | + def retry = 10 |
| 101 | + def query = """ |
| 102 | + select query_id, queue_time_ms, stmt |
| 103 | + from __internal_schema.audit_log |
| 104 | + where stmt like '%${testMarker}%' |
| 105 | + and queue_time_ms > 0 |
| 106 | + order by time |
| 107 | + """ |
| 108 | + def auditResult = sql "${query}" |
| 109 | + |
| 110 | + while (auditResult.isEmpty()) { |
| 111 | + if (retry-- < 0) { |
| 112 | + throw new RuntimeException("It has retried a few but still failed, you need to check it") |
| 113 | + } |
| 114 | + sql "call flush_audit_log()" |
| 115 | + sleep(3000) |
| 116 | + auditResult = sql "${query}" |
| 117 | + } |
| 118 | + |
| 119 | + auditResult.each { row -> |
| 120 | + assertTrue(row[1] >= sqlSleepTime * 1000) |
| 121 | + } |
| 122 | + |
| 123 | + assertTrue(auditResult.size() >= queuedSqlCnt) |
| 124 | + |
| 125 | + // Cleanup |
| 126 | + sql "drop table if exists ${tableName}" |
| 127 | + sql "drop workload group if exists ${wgName}" |
| 128 | + sql "set global enable_audit_plugin = false" |
| 129 | +} |
0 commit comments