You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/04/08 15:37:04 UTC

[GitHub] [ignite] tledkov-gridgain opened a new pull request #8991: IGNITE-14508 Calcite integration: introduce SQL logical test runner

tledkov-gridgain opened a new pull request #8991:
URL: https://github.com/apache/ignite/pull/8991


   Thank you for submitting the pull request to the Apache Ignite.
   
   In order to streamline the review of the contribution 
   we ask you to ensure the following steps have been taken:
   
   ### The Contribution Checklist
   - [ ] There is a single JIRA ticket related to the pull request. 
   - [ ] The web-link to the pull request is attached to the JIRA ticket.
   - [ ] The JIRA ticket has the _Patch Available_ state.
   - [ ] The pull request body describes changes that have been made. 
   The description explains _WHAT_ and _WHY_ was made instead of _HOW_.
   - [ ] The pull request title is treated as the final commit message. 
   The following pattern must be used: `IGNITE-XXXX Change summary` where `XXXX` - number of JIRA issue.
   - [ ] A reviewer has been mentioned through the JIRA comments 
   (see [the Maintainers list](https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute#HowtoContribute-ReviewProcessandMaintainers)) 
   - [ ] The pull request has been checked by the Teamcity Bot and 
   the `green visa` attached to the JIRA ticket (see [TC.Bot: Check PR](https://mtcga.gridgain.com/prs.html))
   
   ### Notes
   - [How to Contribute](https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute)
   - [Coding abbreviation rules](https://cwiki.apache.org/confluence/display/IGNITE/Abbreviation+Rules)
   - [Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Coding+Guidelines)
   - [Apache Ignite Teamcity Bot](https://cwiki.apache.org/confluence/display/IGNITE/Apache+Ignite+Teamcity+Bot)
   
   If you need any help, please email dev@ignite.apache.org or ask anу advice on http://asf.slack.com _#ignite_ channel.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] korlov42 commented on a change in pull request #8991: IGNITE-14508 Calcite integration: introduce SQL logical test runner

Posted by GitBox <gi...@apache.org>.
korlov42 commented on a change in pull request #8991:
URL: https://github.com/apache/ignite/pull/8991#discussion_r614717403



##########
File path: modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/LogicalTestEnvironment.java
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.logical;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ *
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface LogicalTestEnvironment {

Review comment:
       Let's rename this to ScriptRunnerTestsEnvironment

##########
File path: modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/LogicalTestRunner.java
##########
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.logical;
+
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteKernal;
+import org.apache.ignite.internal.IgnitionEx;
+import org.apache.ignite.internal.processors.query.QueryEngine;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger;
+import org.apache.ignite.thread.IgniteThread;
+import org.junit.runner.Description;
+import org.junit.runner.Runner;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunNotifier;
+
+/**
+ *
+ */
+public class LogicalTestRunner extends Runner {
+    /** Filesystem. */
+    private static final FileSystem FS = FileSystems.getDefault();
+
+    /** Shared finder. */
+    private static final TcpDiscoveryVmIpFinder sharedFinder = new TcpDiscoveryVmIpFinder().setShared(true);
+
+    /** */
+    private static IgniteLogger log;
+
+    static {
+        try {
+            log = new GridTestLog4jLogger("src/test/config/log4j-test.xml");
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+
+            log = null;
+
+            assert false : "Cannot init logger";
+        }
+    }
+
+    /** */
+    private final Class<?> testCls;
+
+    /** Scripts root directory. */
+    private final Path scriptsRoot;
+
+    /** Single script to execute. */
+    private final Path script;
+
+    /** Nodes count. */
+    private final int nodes;
+
+    /** Restart cluster for each test group. */
+    private final boolean restartCluster;
+
+    /** Test script timeout. */
+    private final long timeout;
+
+    /** */
+    public LogicalTestRunner(Class<?> testCls) {
+        this.testCls = testCls;
+        LogicalTestEnvironment env = testCls.getAnnotation(LogicalTestEnvironment.class);
+
+        assert !F.isEmpty(env.scriptsRoot());
+
+        nodes = env.nodes();
+        scriptsRoot = FS.getPath(env.scriptsRoot());
+        script = F.isEmpty(env.script()) ? null : FS.getPath(env.script());
+        restartCluster = env.restart();
+        timeout = env.timeout();
+    }
+
+    /** {@inheritDoc} */
+    @Override public Description getDescription() {
+        return Description.createSuiteDescription(testCls.getName(), "scripts");
+    }
+
+    /** {@inheritDoc} */
+    @Override public void run(RunNotifier notifier) {
+        try {
+            if (script == null) {
+                Files.walk(scriptsRoot).sorted().forEach((p) -> {
+                    if (p.equals(scriptsRoot))
+                        return;
+
+                    if (Files.isDirectory(p)) {
+                        if (!F.isEmpty(Ignition.allGrids()) && restartCluster) {
+                            log.info(">>> Restart cluster");
+
+                            Ignition.stopAll(false);
+                        }
+
+                        if (F.isEmpty(Ignition.allGrids()))
+                            startCluster();
+
+                        return;
+                    }
+
+                    String dirName = p.getParent().toString().substring(scriptsRoot.toString().length() + 1);

Review comment:
       a code started from here should be replaced with `runTest`

##########
File path: modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/LogicalTestRunner.java
##########
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.logical;
+
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteKernal;
+import org.apache.ignite.internal.IgnitionEx;
+import org.apache.ignite.internal.processors.query.QueryEngine;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger;
+import org.apache.ignite.thread.IgniteThread;
+import org.junit.runner.Description;
+import org.junit.runner.Runner;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunNotifier;
+
+/**
+ *
+ */
+public class LogicalTestRunner extends Runner {
+    /** Filesystem. */
+    private static final FileSystem FS = FileSystems.getDefault();
+
+    /** Shared finder. */
+    private static final TcpDiscoveryVmIpFinder sharedFinder = new TcpDiscoveryVmIpFinder().setShared(true);
+
+    /** */
+    private static IgniteLogger log;
+
+    static {
+        try {
+            log = new GridTestLog4jLogger("src/test/config/log4j-test.xml");
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+
+            log = null;
+
+            assert false : "Cannot init logger";
+        }
+    }
+
+    /** */
+    private final Class<?> testCls;
+
+    /** Scripts root directory. */
+    private final Path scriptsRoot;
+
+    /** Single script to execute. */
+    private final Path script;
+
+    /** Nodes count. */
+    private final int nodes;
+
+    /** Restart cluster for each test group. */
+    private final boolean restartCluster;
+
+    /** Test script timeout. */
+    private final long timeout;
+
+    /** */
+    public LogicalTestRunner(Class<?> testCls) {
+        this.testCls = testCls;
+        LogicalTestEnvironment env = testCls.getAnnotation(LogicalTestEnvironment.class);
+
+        assert !F.isEmpty(env.scriptsRoot());
+
+        nodes = env.nodes();
+        scriptsRoot = FS.getPath(env.scriptsRoot());
+        script = F.isEmpty(env.script()) ? null : FS.getPath(env.script());
+        restartCluster = env.restart();
+        timeout = env.timeout();
+    }
+
+    /** {@inheritDoc} */
+    @Override public Description getDescription() {
+        return Description.createSuiteDescription(testCls.getName(), "scripts");
+    }
+
+    /** {@inheritDoc} */
+    @Override public void run(RunNotifier notifier) {
+        try {
+            if (script == null) {
+                Files.walk(scriptsRoot).sorted().forEach((p) -> {
+                    if (p.equals(scriptsRoot))
+                        return;
+
+                    if (Files.isDirectory(p)) {
+                        if (!F.isEmpty(Ignition.allGrids()) && restartCluster) {
+                            log.info(">>> Restart cluster");
+
+                            Ignition.stopAll(false);
+                        }
+
+                        if (F.isEmpty(Ignition.allGrids()))
+                            startCluster();
+
+                        return;
+                    }
+
+                    String dirName = p.getParent().toString().substring(scriptsRoot.toString().length() + 1);
+                    String fileName = p.getFileName().toString();
+
+                    if (!fileName.endsWith("test") && !fileName.endsWith("test_slow"))
+                        return;
+
+                    Ignite ign = F.first(Ignition.allGrids());
+
+                    for (String cacheName : ign.cacheNames())
+                        ign.destroyCache(cacheName);
+
+                    Description desc = Description.createTestDescription(dirName, fileName);
+
+                    notifier.fireTestStarted(desc);
+
+                    try {
+                        QueryEngine engine = Commons.lookupComponent(
+                            ((IgniteEx)ign).context(),
+                            QueryEngine.class
+                        );
+
+                        ScriptTestRunner scriptTestRunner = new ScriptTestRunner(p, engine, log);
+
+                        log.info(">>> Start: " + dirName + "/" + fileName);
+
+                        runScript(scriptTestRunner);
+                    }
+                    catch (Throwable e) {
+                        notifier.fireTestFailure(new Failure(desc, e));
+                    }
+                    finally {
+                        log.info(">>> Finish: " + dirName + "/" + fileName);
+                        notifier.fireTestFinished(desc);
+                    }
+                });
+            }
+            else {
+                startCluster();
+
+                runTest(script, notifier);
+            }
+        }
+        catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        finally {
+            Ignition.stopAll(false);
+        }
+    }
+
+    /** */
+    private void runTest(Path test, RunNotifier notifier) {
+        String dirName = test.getParent().toString().substring(scriptsRoot.toString().length() + 1);

Review comment:
       ```suggestion
           String dirName = test.subpath(scriptsRoot.getNameCount(), p.getNameCount() - 1).toString();
   ```
   
   But please be aware that here should be additional handling for the case when test.parent == scriptsRoot

##########
File path: modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/LogicalTestEnvironment.java
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.logical;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ *
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface LogicalTestEnvironment {
+    /**
+     * @return Test scripts root directory.
+     */
+    String scriptsRoot();
+
+    /**
+     * @return Single script to execute.
+     */
+    String script() default "";

Review comment:
       it's quite confusing that script root is ignored when script name is set. Probably it would be better to specify regex here to apply to all scripts found under sriptsRoot. WDYT?

##########
File path: modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/ScriptTestRunner.java
##########
@@ -0,0 +1,575 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.logical;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cache.query.FieldsQueryCursor;
+import org.apache.ignite.cache.query.QueryCursor;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.QueryEngine;
+import org.apache.ignite.internal.util.tostring.GridToStringInclude;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+
+/**
+ *
+ */
+public class ScriptTestRunner {
+    /** Hashing label. */
+    private static final Pattern HASHING_PTRN = Pattern.compile("([0-9]+) values hashing to ([0-9a-fA-F]+)");
+
+    /** Hashing label. */
+    private static final Set<String> ignoredStmts = ImmutableSet.of("PRAGMA");
+
+    /** NULL label. */
+    private static final String NULL = "NULL";
+
+    /** NULL label. */
+    private static final String schemaPublic = "PUBLIC";
+
+    /** Test script path. */
+    private final Path test;
+
+    /** Query engine. */
+    private final QueryEngine engine;
+
+    /** Logger. */
+    private final IgniteLogger log;
+
+    /** Script. */
+    private Script script;
+
+    /** Loop variables. */
+    private HashMap<String, Integer> loopVars = new HashMap<>();
+
+    /** */
+    public ScriptTestRunner(Path test, QueryEngine engine, IgniteLogger log) {
+        this.test = test;
+        this.engine = engine;
+        this.log = log;
+    }
+
+    /** */
+    public void run() throws Exception {
+        script = new Script(test);
+
+        try {
+            while (script.ready()) {
+                try {
+                    Command cmd = script.nextCommand();
+
+                    if (cmd != null)
+                        cmd.execute();
+                }
+                finally {
+                    loopVars.clear();
+                }
+            }
+        }
+        finally {
+            script.close();
+        }
+    }
+
+    /** */
+    private List<List<?>> sql(String sql) {
+        if (!loopVars.isEmpty()) {
+            for (Map.Entry<String, Integer> loopVar : loopVars.entrySet())
+                sql = sql.replaceAll("\\$\\{" + loopVar.getKey() + "\\}", loopVar.getValue().toString());
+        }
+
+        log.info("Execute: " + sql);
+
+        List<FieldsQueryCursor<List<?>>> curs = engine.query(null, schemaPublic, sql);
+
+        assert curs.size() == 1 : "Unexpected results [cursorsCount=" + curs.size() + ']';
+
+        try (QueryCursor<List<?>> cur = curs.get(0)) {
+            return cur.getAll();
+        }
+    }
+
+    /** */
+    private class Script implements AutoCloseable {

Review comment:
       May be it would be better to make `Script implements Iterable<Command>`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org