|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.router.scheduler; |
| 15 | + |
| 16 | +import com.facebook.airlift.json.JsonCodec; |
| 17 | +import com.facebook.airlift.log.Logging; |
| 18 | +import com.facebook.presto.client.QueryError; |
| 19 | +import com.facebook.presto.client.QueryResults; |
| 20 | +import com.facebook.presto.common.ErrorType; |
| 21 | +import com.facebook.presto.nativeworker.PrestoNativeQueryRunnerUtils; |
| 22 | +import com.facebook.presto.server.MockHttpServletRequest; |
| 23 | +import com.facebook.presto.spi.PrestoException; |
| 24 | +import com.facebook.presto.spi.router.RouterRequestInfo; |
| 25 | +import com.facebook.presto.spi.router.Scheduler; |
| 26 | +import com.facebook.presto.testing.QueryRunner; |
| 27 | +import com.facebook.presto.tests.AbstractTestQueryFramework; |
| 28 | +import com.facebook.presto.tests.DistributedQueryRunner; |
| 29 | +import com.google.common.collect.ImmutableListMultimap; |
| 30 | +import com.google.common.collect.ListMultimap; |
| 31 | +import org.testng.annotations.BeforeClass; |
| 32 | +import org.testng.annotations.Test; |
| 33 | + |
| 34 | +import java.net.URI; |
| 35 | +import java.util.Optional; |
| 36 | + |
| 37 | +import static com.facebook.airlift.json.JsonCodec.jsonCodec; |
| 38 | +import static com.facebook.presto.client.PrestoHeaders.PRESTO_CATALOG; |
| 39 | +import static com.facebook.presto.client.PrestoHeaders.PRESTO_SCHEMA; |
| 40 | +import static com.facebook.presto.client.PrestoHeaders.PRESTO_TIME_ZONE; |
| 41 | +import static com.facebook.presto.client.PrestoHeaders.PRESTO_USER; |
| 42 | +import static com.facebook.presto.common.ErrorType.USER_ERROR; |
| 43 | +import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createLineitem; |
| 44 | +import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createRegion; |
| 45 | +import static com.facebook.presto.sidecar.NativeSidecarPluginQueryRunnerUtils.setupNativeSidecarPlugin; |
| 46 | +import static java.util.Collections.emptyList; |
| 47 | +import static org.testng.Assert.assertEquals; |
| 48 | +import static org.testng.Assert.assertNotNull; |
| 49 | +import static org.testng.Assert.assertSame; |
| 50 | +import static org.testng.Assert.assertTrue; |
| 51 | + |
| 52 | +public class TestPlanCheckerRouterPlugin |
| 53 | + extends AbstractTestQueryFramework |
| 54 | +{ |
| 55 | + private static final JsonCodec<QueryResults> QUERY_RESULTS_CODEC = jsonCodec(QueryResults.class); |
| 56 | + private PlanCheckerRouterPluginConfig planCheckerRouterConfig; |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public void setup() |
| 60 | + throws Exception |
| 61 | + { |
| 62 | + Logging.initialize(); |
| 63 | + |
| 64 | + URI javaRouterUri = new URI("192.168.0.1"); |
| 65 | + URI nativeRouterUri = new URI("192.168.0.2"); |
| 66 | + |
| 67 | + URI planCheckerClusters = ((DistributedQueryRunner) getQueryRunner()).getCoordinator().getBaseUrl(); |
| 68 | + planCheckerRouterConfig = |
| 69 | + new PlanCheckerRouterPluginConfig() |
| 70 | + .setJavaRouterURI(javaRouterUri) |
| 71 | + .setNativeRouterURI(nativeRouterUri) |
| 72 | + .setPlanCheckClustersURIs(planCheckerClusters.toString()); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void createTables() |
| 77 | + { |
| 78 | + // TODO : We create a Java query runner and use it create tables, this fails in CI with the native query runner. Need to fix this |
| 79 | + QueryRunner queryRunner = (QueryRunner) getExpectedQueryRunner(); |
| 80 | + createLineitem(queryRunner); |
| 81 | + createRegion(queryRunner); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected QueryRunner createQueryRunner() |
| 86 | + throws Exception |
| 87 | + { |
| 88 | + DistributedQueryRunner queryRunner = (DistributedQueryRunner) PrestoNativeQueryRunnerUtils.nativeHiveQueryRunnerBuilder() |
| 89 | + .setAddStorageFormatToPath(true) |
| 90 | + .setFailOnNestedLoopJoin(true) |
| 91 | + .setCoordinatorSidecarEnabled(true) |
| 92 | + .build(); |
| 93 | + setupNativeSidecarPlugin(queryRunner); |
| 94 | + return queryRunner; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected QueryRunner createExpectedQueryRunner() |
| 99 | + throws Exception |
| 100 | + { |
| 101 | + return PrestoNativeQueryRunnerUtils.javaHiveQueryRunnerBuilder() |
| 102 | + .setAddStorageFormatToPath(true) |
| 103 | + .build(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testPlanCheckerPluginWithNativeCompatibleQueries() |
| 108 | + { |
| 109 | + Scheduler scheduler = new PlanCheckerRouterPluginScheduler(planCheckerRouterConfig); |
| 110 | + scheduler.setCandidates(planCheckerRouterConfig.getPlanCheckClustersURIs()); |
| 111 | + |
| 112 | + // native compatible query |
| 113 | + Optional<URI> target = scheduler.getDestination( |
| 114 | + getMockRouterRequestInfo( |
| 115 | + ImmutableListMultimap.of( |
| 116 | + PRESTO_USER, "test", |
| 117 | + PRESTO_TIME_ZONE, "America/Bahia_Banderas", |
| 118 | + PRESTO_CATALOG, "tpch", |
| 119 | + PRESTO_SCHEMA, "tiny"), |
| 120 | + "SELECT lower(comment) from region")); |
| 121 | + assertTrue(target.isPresent()); |
| 122 | + assertEquals(target.get(), planCheckerRouterConfig.getNativeRouterURI()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testPlanCheckerPluginWithNativeIncompatibleQueries() |
| 127 | + { |
| 128 | + Scheduler scheduler = new PlanCheckerRouterPluginScheduler(planCheckerRouterConfig); |
| 129 | + scheduler.setCandidates(planCheckerRouterConfig.getPlanCheckClustersURIs()); |
| 130 | + |
| 131 | + // native incompatible query |
| 132 | + Optional<URI> target = scheduler.getDestination( |
| 133 | + getMockRouterRequestInfo( |
| 134 | + ImmutableListMultimap.of( |
| 135 | + PRESTO_USER, "test", |
| 136 | + PRESTO_TIME_ZONE, "America/Bahia_Banderas", |
| 137 | + PRESTO_CATALOG, "tpch", |
| 138 | + PRESTO_SCHEMA, "tiny"), |
| 139 | + "SELECT EXISTS(SELECT 1 WHERE l.orderkey > 0 OR l.orderkey != 3) FROM lineitem l LIMIT 1")); |
| 140 | + assertTrue(target.isPresent()); |
| 141 | + assertEquals(target.get(), planCheckerRouterConfig.getJavaRouterURI()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testPlanCheckerPluginWithUserErrors() |
| 146 | + { |
| 147 | + Scheduler scheduler = new PlanCheckerRouterPluginScheduler(planCheckerRouterConfig); |
| 148 | + scheduler.setCandidates(planCheckerRouterConfig.getPlanCheckClustersURIs()); |
| 149 | + |
| 150 | + // queries with user error, the below query does not have a defined catalog and schema |
| 151 | + try { |
| 152 | + scheduler.getDestination( |
| 153 | + getMockRouterRequestInfo( |
| 154 | + ImmutableListMultimap.of( |
| 155 | + PRESTO_USER, "test", |
| 156 | + PRESTO_TIME_ZONE, "America/Bahia_Banderas"), |
| 157 | + "SELECT lower(comment) from region")); |
| 158 | + } |
| 159 | + catch (PrestoException e) { |
| 160 | + verifyQueryError(e, "line 1:55: Schema must be specified when session schema is not set"); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static RouterRequestInfo getMockRouterRequestInfo(ListMultimap<String, String> headers, String query) |
| 165 | + { |
| 166 | + return new RouterRequestInfo("test", Optional.empty(), emptyList(), query, new MockHttpServletRequest(headers)); |
| 167 | + } |
| 168 | + |
| 169 | + private static void verifyQueryError(PrestoException e, String message) |
| 170 | + { |
| 171 | + QueryError error = QUERY_RESULTS_CODEC.fromJson(e.getMessage()).getError(); |
| 172 | + assertNotNull(error); |
| 173 | + assertTrue(error.getMessage().equalsIgnoreCase(message)); |
| 174 | + assertSame(ErrorType.valueOf(error.getErrorType()), USER_ERROR); |
| 175 | + } |
| 176 | +} |
0 commit comments