Search in sources :

Example 11 with Driver

use of org.h2.Driver in project neo4j by neo4j.

the class BoltCausalClusteringIT method sessionShouldExpireOnFailingReadQuery.

@Test
public void sessionShouldExpireOnFailingReadQuery() throws Exception {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(1).startCluster();
    CoreClusterMember coreServer = cluster.getCoreMemberById(0);
    Driver driver = GraphDatabase.driver(coreServer.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
    inExpirableSession(driver, Driver::session, (session) -> {
        session.run("CREATE (p:Person {name: {name} })", Values.parameters("name", "Jim"));
        return null;
    });
    try (Session readSession = driver.session(AccessMode.READ)) {
        // when
        connectedServer(readSession).shutdown();
        // then
        readSession.run("MATCH (n) RETURN n LIMIT 1").consume();
        fail("Should have thrown an exception as the read replica went away mid query");
    } catch (SessionExpiredException sep) {
        // then
        assertThat(sep.getMessage(), containsString("is no longer available"));
    } finally {
        driver.close();
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 12 with Driver

use of org.h2.Driver in project neo4j by neo4j.

the class BoltCausalClusteringIT method shouldNotBeAbleToWriteOnAReadSession.

@Test
public void shouldNotBeAbleToWriteOnAReadSession() throws Exception {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(0).startCluster();
    assertEventually("Failed to execute write query on read server", () -> {
        switchLeader(cluster.awaitLeader());
        CoreClusterMember leader = cluster.awaitLeader();
        Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"));
        try (Session session = driver.session(AccessMode.READ)) {
            // when
            session.run("CREATE (n:Person {name: 'Jim'})").consume();
            return false;
        } catch (ClientException ex) {
            assertEquals("Write queries cannot be performed in READ access mode.", ex.getMessage());
            return true;
        } finally {
            driver.close();
        }
    }, is(true), 30, SECONDS);
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Driver(org.neo4j.driver.v1.Driver) ClientException(org.neo4j.driver.v1.exceptions.ClientException) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Example 13 with Driver

use of org.h2.Driver in project ignite by apache.

the class IgniteH2Indexing method queryDistributedSqlFields.

/** {@inheritDoc} */
@Override
public FieldsQueryCursor<List<?>> queryDistributedSqlFields(String schemaName, SqlFieldsQuery qry, boolean keepBinary, GridQueryCancel cancel, @Nullable Integer mainCacheId) {
    final String sqlQry = qry.getSql();
    Connection c = connectionForSchema(schemaName);
    final boolean enforceJoinOrder = qry.isEnforceJoinOrder();
    final boolean distributedJoins = qry.isDistributedJoins();
    final boolean grpByCollocated = qry.isCollocated();
    final DistributedJoinMode distributedJoinMode = distributedJoinMode(qry.isLocal(), distributedJoins);
    GridCacheTwoStepQuery twoStepQry = null;
    List<GridQueryFieldMetadata> meta;
    final H2TwoStepCachedQueryKey cachedQryKey = new H2TwoStepCachedQueryKey(schemaName, sqlQry, grpByCollocated, distributedJoins, enforceJoinOrder, qry.isLocal());
    H2TwoStepCachedQuery cachedQry = twoStepCache.get(cachedQryKey);
    if (cachedQry != null) {
        twoStepQry = cachedQry.query().copy();
        meta = cachedQry.meta();
    } else {
        final UUID locNodeId = ctx.localNodeId();
        // Here we will just parse the statement, no need to optimize it at all.
        H2Utils.setupConnection(c, /*distributedJoins*/
        false, /*enforceJoinOrder*/
        true);
        GridH2QueryContext.set(new GridH2QueryContext(locNodeId, locNodeId, 0, PREPARE).distributedJoinMode(distributedJoinMode));
        PreparedStatement stmt = null;
        Prepared prepared;
        boolean cachesCreated = false;
        try {
            try {
                while (true) {
                    try {
                        // Do not cache this statement because the whole query object will be cached later on.
                        stmt = prepareStatement(c, sqlQry, false);
                        break;
                    } catch (SQLException e) {
                        if (!cachesCreated && (e.getErrorCode() == ErrorCode.SCHEMA_NOT_FOUND_1 || e.getErrorCode() == ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1 || e.getErrorCode() == ErrorCode.INDEX_NOT_FOUND_1)) {
                            try {
                                ctx.cache().createMissingQueryCaches();
                            } catch (IgniteCheckedException ignored) {
                                throw new CacheException("Failed to create missing caches.", e);
                            }
                            cachesCreated = true;
                        } else
                            throw new IgniteSQLException("Failed to parse query: " + sqlQry, IgniteQueryErrorCode.PARSING, e);
                    }
                }
                prepared = GridSqlQueryParser.prepared(stmt);
                if (qry instanceof JdbcSqlFieldsQuery && ((JdbcSqlFieldsQuery) qry).isQuery() != prepared.isQuery())
                    throw new IgniteSQLException("Given statement type does not match that declared by JDBC driver", IgniteQueryErrorCode.STMT_TYPE_MISMATCH);
                if (prepared.isQuery()) {
                    bindParameters(stmt, F.asList(qry.getArgs()));
                    twoStepQry = GridSqlQuerySplitter.split((JdbcPreparedStatement) stmt, qry.getArgs(), grpByCollocated, distributedJoins, enforceJoinOrder, this);
                    assert twoStepQry != null;
                }
            } finally {
                GridH2QueryContext.clearThreadLocal();
            }
            // It is a DML statement if we did not create a twoStepQuery.
            if (twoStepQry == null) {
                if (DmlStatementsProcessor.isDmlStatement(prepared)) {
                    try {
                        return dmlProc.updateSqlFieldsDistributed(schemaName, stmt, qry, cancel);
                    } catch (IgniteCheckedException e) {
                        throw new IgniteSQLException("Failed to execute DML statement [stmt=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
                    }
                }
                if (DdlStatementsProcessor.isDdlStatement(prepared)) {
                    try {
                        return ddlProc.runDdlStatement(sqlQry, stmt);
                    } catch (IgniteCheckedException e) {
                        throw new IgniteSQLException("Failed to execute DDL statement [stmt=" + sqlQry + ']', e);
                    }
                }
            }
            LinkedHashSet<Integer> caches0 = new LinkedHashSet<>();
            assert twoStepQry != null;
            int tblCnt = twoStepQry.tablesCount();
            if (mainCacheId != null)
                caches0.add(mainCacheId);
            if (tblCnt > 0) {
                for (QueryTable tblKey : twoStepQry.tables()) {
                    GridH2Table tbl = dataTable(tblKey);
                    int cacheId = CU.cacheId(tbl.cacheName());
                    caches0.add(cacheId);
                }
            }
            if (caches0.isEmpty())
                twoStepQry.local(true);
            else {
                //Prohibit usage indices with different numbers of segments in same query.
                List<Integer> cacheIds = new ArrayList<>(caches0);
                checkCacheIndexSegmentation(cacheIds);
                twoStepQry.cacheIds(cacheIds);
                twoStepQry.local(qry.isLocal());
            }
            meta = H2Utils.meta(stmt.getMetaData());
        } catch (IgniteCheckedException e) {
            throw new CacheException("Failed to bind parameters: [qry=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
        } catch (SQLException e) {
            throw new IgniteSQLException(e);
        } finally {
            U.close(stmt, log);
        }
    }
    if (log.isDebugEnabled())
        log.debug("Parsed query: `" + sqlQry + "` into two step query: " + twoStepQry);
    twoStepQry.pageSize(qry.getPageSize());
    if (cancel == null)
        cancel = new GridQueryCancel();
    int[] partitions = qry.getPartitions();
    if (partitions == null && twoStepQry.derivedPartitions() != null) {
        try {
            partitions = calculateQueryPartitions(twoStepQry.derivedPartitions(), qry.getArgs());
        } catch (IgniteCheckedException e) {
            throw new CacheException("Failed to calculate derived partitions: [qry=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
        }
    }
    QueryCursorImpl<List<?>> cursor = new QueryCursorImpl<>(runQueryTwoStep(schemaName, twoStepQry, keepBinary, enforceJoinOrder, qry.getTimeout(), cancel, qry.getArgs(), partitions), cancel);
    cursor.fieldsMeta(meta);
    if (cachedQry == null && !twoStepQry.explain()) {
        cachedQry = new H2TwoStepCachedQuery(meta, twoStepQry.copy());
        twoStepCache.putIfAbsent(cachedQryKey, cachedQry);
    }
    return cursor;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) CacheException(javax.cache.CacheException) Prepared(org.h2.command.Prepared) ArrayList(java.util.ArrayList) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) QueryTable(org.apache.ignite.internal.processors.cache.query.QueryTable) IgniteSystemProperties.getInteger(org.apache.ignite.IgniteSystemProperties.getInteger) DistributedJoinMode(org.apache.ignite.internal.processors.query.h2.opt.DistributedJoinMode) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) JdbcSqlFieldsQuery(org.apache.ignite.internal.jdbc2.JdbcSqlFieldsQuery)

Example 14 with Driver

use of org.h2.Driver in project neo4j by neo4j.

the class BoltCausalClusteringIT method executeWriteAndReadThroughBolt.

private int executeWriteAndReadThroughBolt(CoreClusterMember core) throws TimeoutException, InterruptedException {
    try (Driver driver = GraphDatabase.driver(core.routingURI(), AuthTokens.basic("neo4j", "neo4j"))) {
        return inExpirableSession(driver, (d) -> d.session(AccessMode.WRITE), (session) -> {
            // when
            session.run("MERGE (n:Person {name: 'Jim'})").consume();
            Record record = session.run("MATCH (n:Person) RETURN COUNT(*) AS count").next();
            return record.get("count").asInt();
        });
    }
}
Also used : Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record)

Example 15 with Driver

use of org.h2.Driver in project neo4j by neo4j.

the class BoltCausalClusteringIT method shouldPickANewServerToWriteToOnLeaderSwitch.

@Test
public void shouldPickANewServerToWriteToOnLeaderSwitch() throws Throwable {
    // given
    cluster = clusterRule.withNumberOfReadReplicas(0).startCluster();
    CoreClusterMember leader = cluster.awaitLeader();
    LeaderSwitcher leaderSwitcher = new LeaderSwitcher(cluster);
    Config config = Config.build().withLogging(new JULogging(Level.OFF)).toConfig();
    Set<BoltServerAddress> seenAddresses = new HashSet<>();
    try (Driver driver = GraphDatabase.driver(leader.routingURI(), AuthTokens.basic("neo4j", "neo4j"), config)) {
        boolean success = false;
        long deadline = System.currentTimeMillis() + (30 * 1000);
        while (!success) {
            if (System.currentTimeMillis() > deadline) {
                fail("Failed to write to the new leader in time. Addresses seen: " + seenAddresses);
            }
            try (Session session = driver.session(AccessMode.WRITE)) {
                BoltServerAddress boltServerAddress = ((RoutingNetworkSession) session).address();
                session.run("CREATE (p:Person)");
                seenAddresses.add(boltServerAddress);
                success = seenAddresses.size() >= 2;
            } catch (Exception e) {
                Thread.sleep(100);
            }
            /*
                 * Having the latch release here ensures that we've done at least one pass through the loop, which means
                 * we've completed a connection before the forced master switch.
                 */
            if (seenAddresses.size() >= 1) {
                leaderSwitcher.start();
            }
        }
    } finally {
        leaderSwitcher.stop();
        assertTrue(leaderSwitcher.hadLeaderSwitch());
        assertThat(seenAddresses.size(), greaterThanOrEqualTo(2));
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Config(org.neo4j.driver.v1.Config) Driver(org.neo4j.driver.v1.Driver) TimeoutException(java.util.concurrent.TimeoutException) SessionExpiredException(org.neo4j.driver.v1.exceptions.SessionExpiredException) ServiceUnavailableException(org.neo4j.driver.v1.exceptions.ServiceUnavailableException) IOException(java.io.IOException) ClientException(org.neo4j.driver.v1.exceptions.ClientException) BoltServerAddress(org.neo4j.driver.internal.net.BoltServerAddress) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) JULogging(org.neo4j.driver.internal.logging.JULogging) HashSet(java.util.HashSet) Session(org.neo4j.driver.v1.Session) RoutingNetworkSession(org.neo4j.driver.internal.RoutingNetworkSession) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 Driver (org.neo4j.driver.v1.Driver)16 Session (org.neo4j.driver.v1.Session)14 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)10 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)9 Record (org.neo4j.driver.v1.Record)6 Driver (org.h2.Driver)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 Transaction (org.neo4j.driver.v1.Transaction)5 SessionExpiredException (org.neo4j.driver.v1.exceptions.SessionExpiredException)5 IOException (java.io.IOException)4 Connection (java.sql.Connection)4 RemoteException (java.rmi.RemoteException)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 HashSet (java.util.HashSet)2 ReadReplica (org.neo4j.causalclustering.discovery.ReadReplica)2 BoltServerAddress (org.neo4j.driver.internal.net.BoltServerAddress)2 GuardTimeoutException (org.neo4j.kernel.guard.GuardTimeoutException)2 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)2