Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-11617 #310

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.solr.cloud.api.collections;

import java.lang.invoke.MethodHandles;
import java.util.Locale;
import java.util.Map;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.util.NamedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.solr.cloud.api.collections.OverseerCollectionMessageHandler.*;
import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST;
import static org.apache.solr.common.params.CommonParams.NAME;

public class ModifyAliasCmd implements Cmd {

public static final String META_DATA = "metadata";

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final OverseerCollectionMessageHandler messageHandler;

ModifyAliasCmd(OverseerCollectionMessageHandler messageHandler) {
this.messageHandler = messageHandler;
}

@Override
public void call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {
String aliasName = message.getStr(NAME);


ZkStateReader zkStateReader = messageHandler.zkStateReader;
if (zkStateReader.getAliases().getCollectionAliasMap().get(aliasName) == null) {
// nicer than letting aliases object throw later on...
throw new SolrException(BAD_REQUEST,
String.format(Locale.ROOT, "Can't modify non-existent alias %s", aliasName));
}

@SuppressWarnings("unchecked")
Map<String, String> metadata = (Map<String, String>) message.get(META_DATA);

zkStateReader.aliasesHolder.applyModificationAndExportToZk(aliases1 -> {
for (String key : metadata.keySet()) {
if ("".equals(key.trim())) {
throw new SolrException(BAD_REQUEST, "metadata keys must not be pure whitespace");
}
if (!key.equals(key.trim())) {
throw new SolrException(BAD_REQUEST, "metadata keys should not begin or end with whitespace");
}
String value = metadata.get(key);
if ("".equals(value)) {
value = null;
}
aliases1 = aliases1.cloneWithCollectionAliasMetadata(aliasName, key, value);
}
return aliases1;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public OverseerCollectionMessageHandler(ZkStateReader zkStateReader, String myId
.put(CREATEALIAS, new CreateAliasCmd(this))
.put(CREATEROUTEDALIAS, new CreateAliasCmd(this))
.put(DELETEALIAS, new DeleteAliasCmd(this))
.put(MODIFYALIAS, new ModifyAliasCmd(this))
.put(ROUTEDALIAS_CREATECOLL, new RoutedAliasCreateCollectionCmd(this))
.put(OVERSEERSTATUS, new OverseerStatusCmd(this))
.put(DELETESHARD, new DeleteShardCmd(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,34 @@ public enum CollectionOperation implements CollectionOp {
DELETEALIAS_OP(DELETEALIAS, (req, rsp, h) -> req.getParams().required().getAll(null, NAME)),

/**
* Handle cluster status request.
* Can return status per specific collection/shard or per all collections.
* Change metadata for an alias (use CREATEALIAS_OP to change the actual value of the alias)
*/
MODIFYALIAS_OP(MODIFYALIAS, (req, rsp, h) -> {
Map<String, Object> params = req.getParams().required().getAll(null, NAME);

// Note: success/no-op in the event of no metadata supplied is intentional. Keeps code simple and one less case
// for api-callers to check for.
return convertPrefixToMap(req.getParams(), params, "metadata");
}),

/**
* List the aliases and associated metadata.
*/
LISTALIASES_OP(LISTALIASES, (req, rsp, h) -> {
ZkStateReader zkStateReader = h.coreContainer.getZkController().getZkStateReader();
Aliases aliases = zkStateReader.getAliases();
if (aliases != null) {
// the aliases themselves...
rsp.getValues().add("aliases", aliases.getCollectionAliasMap());
// Any metadata for the above aliases.
Map<String,Map<String,String>> meta = new LinkedHashMap<>();
for (String alias : aliases.getCollectionAliasListMap().keySet()) {
Map<String, String> collectionAliasMetadata = aliases.getCollectionAliasMetadata(alias);
if (collectionAliasMetadata != null) {
meta.put(alias, collectionAliasMetadata);
}
}
rsp.getValues().add("metadata", meta);
}
return null;
}),
Expand Down Expand Up @@ -990,6 +1010,31 @@ public Map<String, Object> execute(SolrQueryRequest req, SolrQueryResponse rsp,
}),
DELETENODE_OP(DELETENODE, (req, rsp, h) -> req.getParams().required().getAll(null, "node"));

/**
* Places all prefixed properties in the sink map (or a new map) using the prefix as the key and a map of
* all prefixed properties as the value. The sub-map keys have the prefix removed.
*
* @param params The solr params from which to extract prefixed properties.
* @param sink The map to add the properties too.
* @param prefix The prefix to identify properties to be extracted
* @return The sink map, or a new map if the sink map was null
*/
private static Map<String, Object> convertPrefixToMap(SolrParams params, Map<String, Object> sink, String prefix) {
Map<String,Object> result = new LinkedHashMap<>();
Iterator<String> iter = params.getParameterNamesIterator();
while (iter.hasNext()) {
String param = iter.next();
if (param.startsWith(prefix)) {
result.put(param.substring(prefix.length()+1), params.get(param));
}
}
if (sink == null) {
sink = new LinkedHashMap<>();
}
sink.put(prefix, result);
return sink;
}

public final CollectionOp fun;
CollectionAction action;
long timeOut;
Expand Down
161 changes: 149 additions & 12 deletions solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.IOUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
Expand All @@ -40,20 +49,53 @@
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.Utils;
import org.apache.zookeeper.KeeperException;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.apache.solr.common.cloud.ZkStateReader.ALIASES;

public class AliasIntegrationTest extends SolrCloudTestCase {

private CloseableHttpClient httpClient;
private CloudSolrClient solrClient;

@BeforeClass
public static void setupCluster() throws Exception {
configureCluster(2)
.addConfig("conf", configset("cloud-minimal"))
.configure();
}

@Before
@Override
public void setUp() throws Exception {
super.setUp();
solrClient = getCloudSolrClient(cluster);
httpClient = (CloseableHttpClient) solrClient.getHttpClient();
}

@After
@Override
public void tearDown() throws Exception {
super.tearDown();
IOUtils.close(solrClient, httpClient);

// make sure all aliases created are removed for the next test method
Map<String, String> aliases = new CollectionAdminRequest.ListAliases().process(cluster.getSolrClient()).getAliases();
for (String alias : aliases.keySet()) {
CollectionAdminRequest.deleteAlias(alias).processAsync(cluster.getSolrClient());
}

// make sure all collections are removed for the next test method
List<String> collections = CollectionAdminRequest.listCollections(cluster.getSolrClient());
for (String collection : collections) {
CollectionAdminRequest.deleteCollection(collection).process(cluster.getSolrClient());
}
}

@Test
public void testMetadata() throws Exception {
CollectionAdminRequest.createCollection("collection1meta", "conf", 2, 1).process(cluster.getSolrClient());
Expand All @@ -75,6 +117,7 @@ public void testMetadata() throws Exception {
assertEquals("collection2meta", aliases.get(1));
//ensure we have the back-compat format in ZK:
final byte[] rawBytes = zkStateReader.getZkClient().getData(ALIASES, null, null, true);
//noinspection unchecked
assertTrue(((Map<String,Map<String,?>>)Utils.fromJSON(rawBytes)).get("collection").get("meta1") instanceof String);

// set metadata
Expand Down Expand Up @@ -178,6 +221,104 @@ public void testMetadata() throws Exception {
}
}

public void testModifyMetadataV2() throws Exception {
final String aliasName = getTestName();
ZkStateReader zkStateReader = createColectionsAndAlias(aliasName);
final String baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
//TODO fix Solr test infra so that this /____v2/ becomes /api/
HttpPost post = new HttpPost(baseUrl + "/____v2/c");
post.setEntity(new StringEntity("{\n" +
"\"modify-alias\" : {\n" +
" \"name\": \"" + aliasName + "\",\n" +
" \"metadata\" : {\n" +
" \"foo\": \"baz\",\n" +
" \"bar\": \"bam\"\n" +
" }\n" +
//TODO should we use "NOW=" param? Won't work with v2 and is kinda a hack any way since intended for distrib
" }\n" +
"}", ContentType.APPLICATION_JSON));
assertSuccess(post);
checkFooAndBarMeta(aliasName, zkStateReader);
}

public void testModifyMetadataV1() throws Exception {
// note we don't use TZ in this test, thus it's UTC
final String aliasName = getTestName();
ZkStateReader zkStateReader = createColectionsAndAlias(aliasName);
final String baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
HttpGet get = new HttpGet(baseUrl + "/admin/collections?action=MODIFYALIAS" +
"&wt=xml" +
"&name=" + aliasName +
"&metadata.foo=baz" +
"&metadata.bar=bam");
assertSuccess(get);
checkFooAndBarMeta(aliasName, zkStateReader);
}

public void testModifyMetadataCAR() throws Exception {
// note we don't use TZ in this test, thus it's UTC
final String aliasName = getTestName();
ZkStateReader zkStateReader = createColectionsAndAlias(aliasName);
CollectionAdminRequest.ModifyAlias modifyAlias = CollectionAdminRequest.modifyAlias(aliasName);
modifyAlias.addMetadata("foo","baz");
modifyAlias.addMetadata("bar","bam");
modifyAlias.process(cluster.getSolrClient());
checkFooAndBarMeta(aliasName, zkStateReader);

// now verify we can delete
modifyAlias = CollectionAdminRequest.modifyAlias(aliasName);
modifyAlias.addMetadata("foo","");
modifyAlias.process(cluster.getSolrClient());
modifyAlias = CollectionAdminRequest.modifyAlias(aliasName);
modifyAlias.addMetadata("bar",null);
modifyAlias.process(cluster.getSolrClient());
modifyAlias = CollectionAdminRequest.modifyAlias(aliasName);

// whitespace value
modifyAlias.addMetadata("foo"," ");
modifyAlias.process(cluster.getSolrClient());


}

private void checkFooAndBarMeta(String aliasName, ZkStateReader zkStateReader) {
Map<String, String> meta = zkStateReader.getAliases().getCollectionAliasMetadata(aliasName);
assertNotNull(meta);
assertTrue(meta.containsKey("foo"));
assertEquals("baz", meta.get("foo"));
assertTrue(meta.containsKey("bar"));
assertEquals("bam", meta.get("bar"));
}

private ZkStateReader createColectionsAndAlias(String aliasName) throws SolrServerException, IOException, KeeperException, InterruptedException {
CollectionAdminRequest.createCollection("collection1meta", "conf", 2, 1).process(cluster.getSolrClient());
CollectionAdminRequest.createCollection("collection2meta", "conf", 1, 1).process(cluster.getSolrClient());
waitForState("Expected collection1 to be created with 2 shards and 1 replica", "collection1meta", clusterShape(2, 1));
waitForState("Expected collection2 to be created with 1 shard and 1 replica", "collection2meta", clusterShape(1, 1));
ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
zkStateReader.createClusterStateWatchersAndUpdate();
List<String> aliases = zkStateReader.getAliases().resolveAliases(aliasName);
assertEquals(1, aliases.size());
assertEquals(aliasName, aliases.get(0));
UnaryOperator<Aliases> op6 = a -> a.cloneWithCollectionAlias(aliasName, "collection1meta,collection2meta");
final ZkStateReader.AliasesManager aliasesHolder = zkStateReader.aliasesHolder;

aliasesHolder.applyModificationAndExportToZk(op6);
aliases = zkStateReader.getAliases().resolveAliases(aliasName);
assertEquals(2, aliases.size());
assertEquals("collection1meta", aliases.get(0));
assertEquals("collection2meta", aliases.get(1));
return zkStateReader;
}

private void assertSuccess(HttpUriRequest msg) throws IOException {
try (CloseableHttpResponse response = httpClient.execute(msg)) {
if (200 != response.getStatusLine().getStatusCode()) {
System.err.println(EntityUtils.toString(response.getEntity()));
fail("Unexpected status: " + response.getStatusLine());
}
}
}
// Rather a long title, but it's common to recommend when people need to re-index for any reason that they:
// 1> create a new collection
// 2> index the corpus to the new collection and verify it
Expand Down Expand Up @@ -497,22 +638,19 @@ public void testErrorChecks() throws Exception {
ignoreException(".");

// Invalid Alias name
SolrException e = expectThrows(SolrException.class, () -> {
CollectionAdminRequest.createAlias("test:alias", "testErrorChecks-collection").process(cluster.getSolrClient());
});
SolrException e = expectThrows(SolrException.class, () ->
CollectionAdminRequest.createAlias("test:alias", "testErrorChecks-collection").process(cluster.getSolrClient()));
assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));

// Target collection doesn't exists
e = expectThrows(SolrException.class, () -> {
CollectionAdminRequest.createAlias("testalias", "doesnotexist").process(cluster.getSolrClient());
});
e = expectThrows(SolrException.class, () ->
CollectionAdminRequest.createAlias("testalias", "doesnotexist").process(cluster.getSolrClient()));
assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));
assertTrue(e.getMessage().contains("Can't create collection alias for collections='doesnotexist', 'doesnotexist' is not an existing collection or alias"));

// One of the target collections doesn't exist
e = expectThrows(SolrException.class, () -> {
CollectionAdminRequest.createAlias("testalias", "testErrorChecks-collection,doesnotexist").process(cluster.getSolrClient());
});
e = expectThrows(SolrException.class, () ->
CollectionAdminRequest.createAlias("testalias", "testErrorChecks-collection,doesnotexist").process(cluster.getSolrClient()));
assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));
assertTrue(e.getMessage().contains("Can't create collection alias for collections='testErrorChecks-collection,doesnotexist', 'doesnotexist' is not an existing collection or alias"));

Expand All @@ -522,9 +660,8 @@ public void testErrorChecks() throws Exception {
CollectionAdminRequest.createAlias("testalias2", "testalias").process(cluster.getSolrClient());

// Alias + invalid
e = expectThrows(SolrException.class, () -> {
CollectionAdminRequest.createAlias("testalias3", "testalias2,doesnotexist").process(cluster.getSolrClient());
});
e = expectThrows(SolrException.class, () ->
CollectionAdminRequest.createAlias("testalias3", "testalias2,doesnotexist").process(cluster.getSolrClient()));
assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));
unIgnoreException(".");

Expand Down