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 2020/03/25 10:53:33 UTC

[GitHub] [ignite] tledkov-gridgain opened a new pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

tledkov-gridgain opened a new pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569
 
 
   

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r400015937
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2PooledConnection.java
 ##########
 @@ -0,0 +1,130 @@
+/*
+ * 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.h2;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Pooled connection wrapper to use close semantic to recycle connection (return to the pool).
+ */
+public class H2PooledConnection implements AutoCloseable {
+    /** */
+    private volatile H2Connection delegate;
+
+    /** Connection manager. */
+    private final ConnectionManager connMgr;
+
+    /** Closed (recycled) flag. */
+    private final AtomicBoolean closed = new AtomicBoolean();
+
+    /**
+     * @param conn Connection to use.
+     * @param connMgr Connection manager is use to recycle connection
+     *      (connection is closed or returned to connection pool).
+     */
+    H2PooledConnection(H2Connection conn, ConnectionManager connMgr) {
+        this.delegate = conn;
+        this.connMgr = connMgr;
+    }
+
+    /**
+     * @return Schema name if schema is set, null otherwise.
+     */
+    public String schema() {
+        return delegate.schema();
+    }
+
+    /**
+     * @param schema Schema name set on this connection.
+     */
+    public void schema(@Nullable String schema) {
+        delegate.schema(schema);
+    }
+
+    /**
+     * @return Connection.
+     */
+    public Connection connection() {
+        return delegate.connection();
+    }
+
+    /**
+     * @return Statement cache size.
+     */
+    public int statementCacheSize() {
+        return delegate.statementCacheSize();
+    }
+
+    /**
+     * Prepare statement caching it if needed.
+     *
+     * @param sql SQL.
+     * @return Prepared statement.
+     */
+    public PreparedStatement prepareStatement(String sql, byte qryFlags) throws IgniteCheckedException {
+        return delegate.prepareStatement(sql, qryFlags);
+    }
+
+    /**
+     * Get prepared statement without caching.
+     *
+     * @param sql SQL.
+     * @return Prepared statement.
+     */
+    public PreparedStatement prepareStatementNoCache(String sql) throws IgniteCheckedException {
+        boolean insertHack = GridH2Table.insertHackRequired(sql);
+
+        if (insertHack) {
+            GridH2Table.insertHack(true);
+
+            try {
+                return delegate.prepareStatementNoCache(sql);
+            }
+            finally {
+                GridH2Table.insertHack(false);
+            }
+        }
+        else
+            return delegate.prepareStatementNoCache(sql);
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(H2PooledConnection.class, this);
+    }
+
+    /** Closes wrapped connection (return to pool or close). */
+    @Override public void close() {
+        assert delegate != null;
+
+        if (closed.compareAndSet(false, true)) {
+            // clear thread local
+            // H2Utils.resetSession(this);
 
 Review comment:
   Is it still actual?

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r402168901
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2PooledConnection.java
 ##########
 @@ -0,0 +1,129 @@
+/*
+ * 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.h2;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Pooled connection wrapper to use close semantic to recycle connection (return to the pool).
+ */
+public class H2PooledConnection implements AutoCloseable {
+    /** */
+    private volatile H2Connection delegate;
+
+    /** Connection manager. */
+    private final ConnectionManager connMgr;
+
+    /** Closed (recycled) flag. */
+    private final AtomicBoolean closed = new AtomicBoolean();
+
+    /**
+     * @param conn Connection to use.
+     * @param connMgr Connection manager is use to recycle connection
+     *      (connection is closed or returned to connection pool).
+     */
+    H2PooledConnection(H2Connection conn, ConnectionManager connMgr) {
+        this.delegate = conn;
+        this.connMgr = connMgr;
+    }
+
+    /**
+     * @return Schema name if schema is set, null otherwise.
+     */
+    public String schema() {
+        return delegate.schema();
+    }
+
+    /**
+     * @param schema Schema name set on this connection.
+     */
+    public void schema(@Nullable String schema) {
+        delegate.schema(schema);
+    }
+
+    /**
+     * @return Connection.
+     */
+    public Connection connection() {
+        return delegate.connection();
+    }
+
+    /**
+     * @return Statement cache size.
+     */
+    public int statementCacheSize() {
+        return delegate.statementCacheSize();
+    }
+
+    /**
+     * Prepare statement caching it if needed.
+     *
+     * @param sql SQL.
+     * @return Prepared statement.
+     */
+    public PreparedStatement prepareStatement(String sql, byte qryFlags) throws IgniteCheckedException {
+        return delegate.prepareStatement(sql, qryFlags);
+    }
+
+    /**
+     * Get prepared statement without caching.
+     *
+     * @param sql SQL.
+     * @return Prepared statement.
+     */
+    public PreparedStatement prepareStatementNoCache(String sql) throws IgniteCheckedException {
+        boolean insertHack = GridH2Table.insertHackRequired(sql);
+
+        if (insertHack) {
+            GridH2Table.insertHack(true);
+
+            try {
+                return delegate.prepareStatementNoCache(sql);
+            }
+            finally {
+                GridH2Table.insertHack(false);
+            }
+        }
+        else
+            return delegate.prepareStatementNoCache(sql);
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(H2PooledConnection.class, this);
+    }
+
+    /** Closes wrapped connection (return to pool or close). */
+    @Override public void close() {
+        assert delegate != null;
+
+        if (closed.compareAndSet(false, true)) {
+             H2Utils.resetSession(this);
+
+            connMgr.recycle(delegate);
+
+            delegate = null;
+        }
+    }
+}
 
 Review comment:
   Empty line.

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r400032441
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/opt/QueryContext.java
 ##########
 @@ -28,6 +29,8 @@
  * Thread local SQL query context which is intended to be accessible from everywhere.
  */
 public class QueryContext {
+    private static final ThreadLocal<QueryContext> qctxThreaded = new ThreadLocal<>();
 
 Review comment:
   Javadoc?

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov merged pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov merged pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569
 
 
   

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r402169284
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/opt/QueryContext.java
 ##########
 @@ -28,6 +29,11 @@
  * Thread local SQL query context which is intended to be accessible from everywhere.
  */
 public class QueryContext {
+    /** Thread local query context is used for API that doesn't support h2 Session:
 
 Review comment:
   New line.

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r400037534
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java
 ##########
 @@ -437,6 +458,45 @@ public static void setupConnection(
         s.setForceJoinOrder(enforceJoinOrder);
         s.setJoinBatchEnabled(distributedJoins);
         s.setLazyQueryExecution(lazy);
+
+        QueryContext oldCtx = (QueryContext)s.getVariable(QCTX_VARIABLE_NAME).getObject();
+
+        assert oldCtx == null || oldCtx == qctx : oldCtx;
+
+        s.setVariable("IGNITE_QUERY_CONTEXT", new ValueRuntimeSimpleObject<>(qctx));
 
 Review comment:
   Do you mean QCTX_VARIABLE_NAME here instead of "IGNITE_QUERY_CONTEXT" ?
   BTW 'QCTX_VARIABLE_NAME' constant is not equals to "IGNITE_QUERY_CONTEXT"
   Is it a typo, I've missed smth?

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r400037959
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java
 ##########
 @@ -437,6 +458,45 @@ public static void setupConnection(
         s.setForceJoinOrder(enforceJoinOrder);
         s.setJoinBatchEnabled(distributedJoins);
         s.setLazyQueryExecution(lazy);
+
+        QueryContext oldCtx = (QueryContext)s.getVariable(QCTX_VARIABLE_NAME).getObject();
+
+        assert oldCtx == null || oldCtx == qctx : oldCtx;
+
+        s.setVariable("IGNITE_QUERY_CONTEXT", new ValueRuntimeSimpleObject<>(qctx));
+
+        // Hack with thread local context is used only for H2 methods that is called without Session object.
+        // e.g. GridH2Table.getRowCountApproximation (used only on optimization phase, after parse).
+        QueryContext.threadLocal(qctx);
+    }
+
+    /**
+     * Clean up session for further reuse.
+     *
+     * @param conn Connection to use.
+     */
+    public static void resetSession(H2PooledConnection conn) {
+        Session s = session(conn);
+
+        s.setVariable("IGNITE_QUERY_CONTEXT", ValueNull.INSTANCE);
+    }
+
+    /**
+     * @param conn Connection to use.
+     * @return Query context.
+     */
+    public static QueryContext context(H2PooledConnection conn) {
+        Session s = session(conn);
+
+        return context(s);
+    }
+
+    /**
+     * @param ses Session.
+     * @return Query context.
+     */
+    public static QueryContext context(Session ses) {
+        return (QueryContext)ses.getVariable("IGNITE_QUERY_CONTEXT").getObject();
 
 Review comment:
   QCTX_VARIABLE_NAME ?

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r400037896
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java
 ##########
 @@ -437,6 +458,45 @@ public static void setupConnection(
         s.setForceJoinOrder(enforceJoinOrder);
         s.setJoinBatchEnabled(distributedJoins);
         s.setLazyQueryExecution(lazy);
+
+        QueryContext oldCtx = (QueryContext)s.getVariable(QCTX_VARIABLE_NAME).getObject();
+
+        assert oldCtx == null || oldCtx == qctx : oldCtx;
+
+        s.setVariable("IGNITE_QUERY_CONTEXT", new ValueRuntimeSimpleObject<>(qctx));
+
+        // Hack with thread local context is used only for H2 methods that is called without Session object.
+        // e.g. GridH2Table.getRowCountApproximation (used only on optimization phase, after parse).
+        QueryContext.threadLocal(qctx);
+    }
+
+    /**
+     * Clean up session for further reuse.
+     *
+     * @param conn Connection to use.
+     */
+    public static void resetSession(H2PooledConnection conn) {
+        Session s = session(conn);
+
+        s.setVariable("IGNITE_QUERY_CONTEXT", ValueNull.INSTANCE);
 
 Review comment:
   QCTX_VARIABLE_NAME ?

----------------------------------------------------------------
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


With regards,
Apache Git Services

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

Posted by GitBox <gi...@apache.org>.
AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r402169132
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/ConcurrentStripedPool.java
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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.h2;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Concurrent pool of object based on ConcurrentLinkedDeque.
+ */
+public class ConcurrentStripedPool<E> implements Iterable<E> {
+    /** Stripe pools. */
+    private final ConcurrentLinkedQueue<E>[] stripePools;
+
+    /** Stripes count. */
+    private final int stripes;
+
+    /** Stripe pools size (calculates fast, optimistic and approximate). */
+    private AtomicInteger[] stripeSize;
+
+    /** Max pool size. */
+    private int maxPoolSize;
+
+    /**
+     * Constructor.
+     *
+     * @param stripes Count of stripes.
+     * @param maxPoolSize Max pool size.
+     */
+    @SuppressWarnings("unchecked")
+    public ConcurrentStripedPool(int stripes, int maxPoolSize) {
+        this.stripes = stripes;
+        this.maxPoolSize = maxPoolSize;
+
+        stripePools = new ConcurrentLinkedQueue[stripes];
+        stripeSize = new AtomicInteger[stripes];
+
+        for (int i = 0; i < stripes; ++i) {
+            stripePools[i] = new ConcurrentLinkedQueue<>();
+            stripeSize[i] = new AtomicInteger();
+        }
+    }
+
+    /**
+     * Pushes an element onto the pool.
+     *
+     * @param e the element to push
+     * @throws NullPointerException if the specified element is null and this deque does not permit null elements
+     * @return {@code true} if the element is returned to the pool, {@code false} if the is no space at the pool.
+     */
+    public boolean recycle(E e) {
+        int idx = (int)(Thread.currentThread().getId() % stripes);
+
+        if (stripeSize[idx].get() > maxPoolSize)
+            return false;
+
+        stripePools[idx].add(e);
+
+        stripeSize[idx].incrementAndGet();
+
+        return true;
+    }
+
+    /**
+     * Retrieves element from pool, or returns {@code null} if the pool is empty.
+     *
+     * @return the  element of the pool, or {@code null} if the pool is empty.
+     */
+    public E borrow() {
+        int idx = (int)(Thread.currentThread().getId() % stripes);
+
+        E r = stripePools[idx].poll();
+
+        if (r != null)
+            stripeSize[idx].decrementAndGet();
+
+        return r;
+    }
+
+    /**
+     * Performs the given action for each element of the pool until all elements have been processed or the action
+     * throws an exception. Exceptions thrown by the action are relayed to the caller.
+     *
+     * @param action The action to be performed for each element
+     * @throws NullPointerException if the specified action is null
+     */
+    @Override public void forEach(Consumer<? super E> action) {
+        Objects.requireNonNull(action);
+
+        for (int i = 0; i < stripes; ++i)
+            stripePools[i].forEach(action);
+    }
+
+    /**
+     * Removes all of the elements from the pool..
+     */
+    public void clear() {
+        for (int i = 0; i < stripes; ++i)
+            stripePools[i].clear();
+    }
+
+    /** {@inheritDoc} */
+    @Override public @NotNull Iterator<E> iterator() {
+        return new Iterator<E>() {
+            int idx = 0;
+            Iterator<E> it = stripePools[idx].iterator();
+
+            @Override public boolean hasNext() {
+                if (it.hasNext())
+                    return true;
+
+                idx++;
+
+                if (idx < stripes) {
+                    it = stripePools[idx].iterator();
+
+                    return it.hasNext();
+                }
+                else
+                    return false;
+            }
+
+            @Override public E next() {
+                if (it.hasNext())
+                    return it.next();
+
+                idx++;
+
+                if (idx < stripes) {
+                    it = stripePools[idx].iterator();
+
+                    return it.next();
+                }
+                else
+                    throw new NoSuchElementException();
+            }
+        };
+    }
+
+    /**
+     * Returns a sequential {@code Stream} of the pool.
+     *
+     * @return a sequential {@code Stream} over the elements iof the pool.
+     */
+    public Stream<E> stream() {
+        return StreamSupport.stream(spliterator(), false);
+    }
+
+    /**
+     * @param size New max pool size.
+     */
+    public void resize(int size) {
+        maxPoolSize = size;
+    }
+}
 
 Review comment:
   Empty line.

----------------------------------------------------------------
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


With regards,
Apache Git Services