Search in sources :

Example 1 with Aliases

use of org.apache.solr.common.cloud.Aliases in project lucene-solr by apache.

the class JdbcTest method testJDBCMethods.

private void testJDBCMethods(String collection, String connectionString, Properties properties, String sql) throws Exception {
    try (Connection con = DriverManager.getConnection(connectionString, properties)) {
        assertTrue(con.isValid(DEFAULT_CONNECTION_TIMEOUT));
        assertEquals(zkHost, con.getCatalog());
        con.setCatalog(zkHost);
        assertEquals(zkHost, con.getCatalog());
        assertEquals(null, con.getSchema());
        con.setSchema("myschema");
        assertEquals(null, con.getSchema());
        DatabaseMetaData databaseMetaData = con.getMetaData();
        assertNotNull(databaseMetaData);
        assertEquals(con, databaseMetaData.getConnection());
        assertEquals(connectionString, databaseMetaData.getURL());
        assertEquals(4, databaseMetaData.getJDBCMajorVersion());
        assertEquals(0, databaseMetaData.getJDBCMinorVersion());
        assertEquals("Apache Solr", databaseMetaData.getDatabaseProductName());
        // The following tests require package information that is not available when running via Maven
        //      assertEquals(this.getClass().getPackage().getSpecificationVersion(), databaseMetaData.getDatabaseProductVersion());
        //      assertEquals(0, databaseMetaData.getDatabaseMajorVersion());
        //      assertEquals(0, databaseMetaData.getDatabaseMinorVersion());
        //      assertEquals(this.getClass().getPackage().getSpecificationTitle(), databaseMetaData.getDriverName());
        //      assertEquals(this.getClass().getPackage().getSpecificationVersion(), databaseMetaData.getDriverVersion());
        //      assertEquals(0, databaseMetaData.getDriverMajorVersion());
        //      assertEquals(0, databaseMetaData.getDriverMinorVersion());
        List<String> tableSchemas = new ArrayList<>(Arrays.asList(zkHost, "metadata"));
        try (ResultSet rs = databaseMetaData.getSchemas()) {
            assertTrue(rs.next());
            assertTrue(tableSchemas.contains(rs.getString("tableSchem")));
            tableSchemas.remove(rs.getString("tableSchem"));
            assertNull(rs.getString("tableCat"));
            assertTrue(rs.next());
            assertTrue(tableSchemas.contains(rs.getString("tableSchem")));
            tableSchemas.remove(rs.getString("tableSchem"));
            assertNull(rs.getString("tableCat"));
            assertFalse(rs.next());
            assertTrue(tableSchemas.isEmpty());
        }
        try (ResultSet rs = databaseMetaData.getCatalogs()) {
            assertTrue(rs.next());
            assertNull(rs.getString("tableCat"));
            assertFalse(rs.next());
        }
        CloudSolrClient solrClient = cluster.getSolrClient();
        solrClient.connect();
        ZkStateReader zkStateReader = solrClient.getZkStateReader();
        SortedSet<String> tables = new TreeSet<>();
        Set<String> collectionsSet = zkStateReader.getClusterState().getCollectionsMap().keySet();
        tables.addAll(collectionsSet);
        Aliases aliases = zkStateReader.getAliases();
        if (aliases != null) {
            Map<String, String> collectionAliasMap = aliases.getCollectionAliasMap();
            if (collectionAliasMap != null) {
                Set<String> aliasesSet = collectionAliasMap.keySet();
                tables.addAll(aliasesSet);
            }
        }
        try (ResultSet rs = databaseMetaData.getTables(null, zkHost, "%", null)) {
            for (String table : tables) {
                assertTrue(rs.next());
                assertNull(rs.getString("tableCat"));
                assertEquals(zkHost, rs.getString("tableSchem"));
                assertEquals(table, rs.getString("tableName"));
                assertEquals("TABLE", rs.getString("tableType"));
                assertNull(rs.getString("remarks"));
            }
            assertFalse(rs.next());
        }
        assertTrue(con.isReadOnly());
        con.setReadOnly(true);
        assertTrue(con.isReadOnly());
        assertNull(con.getWarnings());
        con.clearWarnings();
        assertNull(con.getWarnings());
        try (Statement statement = con.createStatement()) {
            checkStatement(con, statement);
            try (ResultSet rs = statement.executeQuery(sql)) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertTrue(statement.execute(sql));
            assertEquals(-1, statement.getUpdateCount());
            try (ResultSet rs = statement.getResultSet()) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertFalse(statement.getMoreResults());
        }
        try (PreparedStatement statement = con.prepareStatement(sql)) {
            checkStatement(con, statement);
            try (ResultSet rs = statement.executeQuery()) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertTrue(statement.execute());
            assertEquals(-1, statement.getUpdateCount());
            try (ResultSet rs = statement.getResultSet()) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertFalse(statement.getMoreResults());
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Aliases(org.apache.solr.common.cloud.Aliases) DatabaseMetaData(java.sql.DatabaseMetaData) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) TreeSet(java.util.TreeSet) ResultSet(java.sql.ResultSet)

Example 2 with Aliases

use of org.apache.solr.common.cloud.Aliases in project lucene-solr by apache.

the class TupleStream method getSlices.

public static Collection<Slice> getSlices(String collectionName, ZkStateReader zkStateReader, boolean checkAlias) throws IOException {
    ClusterState clusterState = zkStateReader.getClusterState();
    Map<String, DocCollection> collectionsMap = clusterState.getCollectionsMap();
    // Check collection case sensitive
    if (collectionsMap.containsKey(collectionName)) {
        return collectionsMap.get(collectionName).getActiveSlices();
    }
    // Check collection case insensitive
    for (String collectionMapKey : collectionsMap.keySet()) {
        if (collectionMapKey.equalsIgnoreCase(collectionName)) {
            return collectionsMap.get(collectionMapKey).getActiveSlices();
        }
    }
    if (checkAlias) {
        // check for collection alias
        Aliases aliases = zkStateReader.getAliases();
        String alias = aliases.getCollectionAlias(collectionName);
        if (alias != null) {
            Collection<Slice> slices = new ArrayList<>();
            List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
            for (String aliasCollectionName : aliasList) {
                // Add all active slices for this alias collection
                slices.addAll(collectionsMap.get(aliasCollectionName).getActiveSlices());
            }
            return slices;
        }
    }
    throw new IOException("Slices not found for " + collectionName);
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) ArrayList(java.util.ArrayList) DocCollection(org.apache.solr.common.cloud.DocCollection) Aliases(org.apache.solr.common.cloud.Aliases) IOException(java.io.IOException)

Example 3 with Aliases

use of org.apache.solr.common.cloud.Aliases in project lucene-solr by apache.

the class CloudSolrStream method getSlices.

public static Collection<Slice> getSlices(String collectionName, ZkStateReader zkStateReader, boolean checkAlias) throws IOException {
    ClusterState clusterState = zkStateReader.getClusterState();
    Map<String, DocCollection> collectionsMap = clusterState.getCollectionsMap();
    // Check collection case sensitive
    if (collectionsMap.containsKey(collectionName)) {
        return collectionsMap.get(collectionName).getActiveSlices();
    }
    // Check collection case insensitive
    for (String collectionMapKey : collectionsMap.keySet()) {
        if (collectionMapKey.equalsIgnoreCase(collectionName)) {
            return collectionsMap.get(collectionMapKey).getActiveSlices();
        }
    }
    if (checkAlias) {
        // check for collection alias
        Aliases aliases = zkStateReader.getAliases();
        String alias = aliases.getCollectionAlias(collectionName);
        if (alias != null) {
            Collection<Slice> slices = new ArrayList<>();
            List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
            for (String aliasCollectionName : aliasList) {
                // Add all active slices for this alias collection
                slices.addAll(collectionsMap.get(aliasCollectionName).getActiveSlices());
            }
            return slices;
        }
    }
    throw new IOException("Slices not found for " + collectionName);
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Slice(org.apache.solr.common.cloud.Slice) ArrayList(java.util.ArrayList) DocCollection(org.apache.solr.common.cloud.DocCollection) Aliases(org.apache.solr.common.cloud.Aliases) IOException(java.io.IOException)

Example 4 with Aliases

use of org.apache.solr.common.cloud.Aliases in project lucene-solr by apache.

the class DeleteAliasCmd method checkForAliasAbsence.

private void checkForAliasAbsence(String name) {
    TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS);
    boolean success = false;
    Aliases aliases = null;
    while (!timeout.hasTimedOut()) {
        aliases = ocmh.zkStateReader.getAliases();
        String collections = aliases.getCollectionAlias(name);
        if (collections == null) {
            success = true;
            break;
        }
    }
    if (!success) {
        log.warn("Timeout waiting to be notified of Alias change...");
    }
}
Also used : TimeOut(org.apache.solr.util.TimeOut) Aliases(org.apache.solr.common.cloud.Aliases)

Example 5 with Aliases

use of org.apache.solr.common.cloud.Aliases in project lucene-solr by apache.

the class DeleteAliasCmd method call.

@Override
public void call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {
    String aliasName = message.getStr(NAME);
    Map<String, Map<String, String>> newAliasesMap = new HashMap<>();
    Map<String, String> newCollectionAliasesMap = new HashMap<>();
    ZkStateReader zkStateReader = ocmh.zkStateReader;
    newCollectionAliasesMap.putAll(zkStateReader.getAliases().getCollectionAliasMap());
    newCollectionAliasesMap.remove(aliasName);
    newAliasesMap.put("collection", newCollectionAliasesMap);
    Aliases newAliases = new Aliases(newAliasesMap);
    byte[] jsonBytes = null;
    if (newAliases.collectionAliasSize() > 0) {
        // only sub map right now
        jsonBytes = Utils.toJSON(newAliases.getAliasMap());
    }
    try {
        zkStateReader.getZkClient().setData(ZkStateReader.ALIASES, jsonBytes, true);
        checkForAliasAbsence(aliasName);
        // some fudge for other nodes
        Thread.sleep(100);
    } catch (KeeperException e) {
        log.error("", e);
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
    } catch (InterruptedException e) {
        log.warn("", e);
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) HashMap(java.util.HashMap) Aliases(org.apache.solr.common.cloud.Aliases) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException) SolrException(org.apache.solr.common.SolrException)

Aggregations

Aliases (org.apache.solr.common.cloud.Aliases)10 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 SolrException (org.apache.solr.common.SolrException)4 ClusterState (org.apache.solr.common.cloud.ClusterState)4 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 DocCollection (org.apache.solr.common.cloud.DocCollection)3 Slice (org.apache.solr.common.cloud.Slice)3 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)2 TimeOut (org.apache.solr.util.TimeOut)2 KeeperException (org.apache.zookeeper.KeeperException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 HashSet (java.util.HashSet)1