Skip to content

Commit

Permalink
STAR-453 ignore DSE index creation commands (apache#117)
Browse files Browse the repository at this point in the history
The commands end without an error with applied = true
(in the same way as "IF EXIST" commands when the entity exists).
A warning is returned to the caller. The index is not created.

(cherry picked from commit b89547e)
(cherry picked from commit ef28215)
(cherry picked from commit 1b5bf82)
(cherry picked from commit 2dacdef)
  • Loading branch information
jtgrabowski authored and jacek-lewandowski committed Mar 9, 2022
1 parent 1ad9079 commit 14e31e5
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.*;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

Expand Down Expand Up @@ -52,6 +53,19 @@ public final class CreateIndexStatement extends AlterSchemaStatement
private final IndexAttributes attrs;
private final boolean ifNotExists;

private static final String DSE_INDEX_WARNING = "Index %s was not created. DSE custom index (%s) is not " +
"supported. Consult the docs on alternatives (SAI indexes, " +
"Secondary Indexes).";

@VisibleForTesting
public static final Set<String> DSE_INDEXES = ImmutableSet.of(
"com.datastax.bdp.cassandra.index.solr.SolrSecondaryIndex",
"com.datastax.bdp.cassandra.index.solr.ThriftSolrSecondaryIndex",
"com.datastax.bdp.cassandra.index.solr.Cql3SolrSecondaryIndex",
"com.datastax.bdp.search.solr.ThriftSolrSecondaryIndex",
"com.datastax.bdp.search.solr.Cql3SolrSecondaryIndex"
);

public CreateIndexStatement(String keyspaceName,
String tableName,
String indexName,
Expand All @@ -69,6 +83,13 @@ public CreateIndexStatement(String keyspaceName,

public Keyspaces apply(Keyspaces schema)
{
if (isDseIndexCreateStatement())
{
// DSE indexes are not supported. The index is not created, the attempt is ignored (doesn't cause error),
// a meaningfull warning is returned instead.
return schema;
}

attrs.validate();

if (attrs.isCustom && attrs.customClass.equals(SASIIndex.class.getName()) && !DatabaseDescriptor.getEnableSASIIndexes())
Expand Down Expand Up @@ -147,9 +168,17 @@ Set<String> clientWarnings(KeyspacesDiff diff)
if (attrs.isCustom && attrs.customClass.equals(SASIIndex.class.getName()))
return ImmutableSet.of(SASIIndex.USAGE_WARNING);

if (isDseIndexCreateStatement())
return ImmutableSet.of(String.format(DSE_INDEX_WARNING, indexName, attrs.customClass));

return ImmutableSet.of();
}

private boolean isDseIndexCreateStatement()
{
return DSE_INDEXES.contains(attrs.customClass);
}

private void validateIndexTarget(TableMetadata table, IndexMetadata.Kind kind, IndexTarget target)
{
ColumnMetadata column = table.getColumn(target.column);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.cassandra.cql3.statements;

import java.util.Set;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.exceptions.InvalidQueryException;
import org.apache.cassandra.SchemaLoader;
import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.cql3.QueryProcessor;
import org.apache.cassandra.cql3.statements.schema.CreateIndexStatement;
import org.apache.cassandra.schema.KeyspaceParams;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@RunWith(Parameterized.class)
public class CreateIndexStatementTest extends CQLTester
{
@Parameterized.Parameters(name = "index = {0}")
public static Set<String> dseIndexes()
{
return CreateIndexStatement.DSE_INDEXES;
}

@Parameterized.Parameter()
public String indexClass;

@BeforeClass
public static void setup() throws Exception
{
SchemaLoader.createKeyspace("ks", KeyspaceParams.simple(1));
QueryProcessor.executeOnceInternal("CREATE TABLE ks.tbl (k int, c int, v int, primary key (k, c))");
}

private void assertNoIndex(String indexName) throws Throwable
{
try
{
executeNet("DESCRIBE INDEX ks." + indexName);
fail("Expected InvalidQueryException caused by a missing index");
}
catch (InvalidQueryException e)
{
assertTrue(e.getMessage().contains(indexName + "' not found"));
}
}

@Test
public void dseIndexCreationShouldBeIgonerWithWarning() throws Throwable
{
// should not throw
ResultSet rows = executeNet(String.format("CREATE CUSTOM INDEX index_name ON ks.tbl (v) USING '%s'", indexClass));

assertTrue(rows.wasApplied()); // the command is ignored

String warning = rows.getAllExecutionInfo().get(0).getWarnings().get(0);
assertTrue("Custom DSE index creation should cause a warning", warning.contains("DSE custom index"));

assertNoIndex("index_name");
}
}

0 comments on commit 14e31e5

Please sign in to comment.