Skip to content

Commit

Permalink
Add set-placement-plugin and unset-placement-plugin to annotation bas…
Browse files Browse the repository at this point in the history
…ed /api/cluster API
  • Loading branch information
murblanc committed Sep 16, 2020
1 parent 6f64c42 commit 607f164
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions solr/core/src/java/org/apache/solr/handler/ClusterAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package org.apache.solr.handler;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.solr.api.Command;
import org.apache.solr.api.EndPoint;
import org.apache.solr.api.PayloadObj;
import org.apache.solr.client.solrj.request.beans.ClusterPropInfo;
import org.apache.solr.cluster.placement.impl.PlacementPluginConfigImpl;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.annotation.JsonProperty;
import org.apache.solr.common.cloud.ClusterProperties;
Expand Down Expand Up @@ -81,9 +83,8 @@ public void deleteCommandStatus(SolrQueryRequest req, SolrQueryResponse rsp) thr
CollectionsHandler.CollectionOperation.DELETESTATUS_OP.execute(req, rsp, coreContainer.getCollectionsHandler());
}

@SuppressWarnings({"rawtypes"})
public static SolrQueryRequest wrapParams(SolrQueryRequest req, Object... def) {
Map m = Utils.makeMap(def);
Map<String, Object> m = Utils.makeMap(def);
return wrapParams(req, m);
}

Expand Down Expand Up @@ -117,8 +118,6 @@ public void getNodes(SolrQueryRequest req, SolrQueryResponse rsp) {
path = "/cluster",
permission = COLL_EDIT_PERM)
public class Commands {


@Command(name = "add-role")
@SuppressWarnings({"rawtypes", "unchecked"})
public void addRole(PayloadObj<RoleInfo> obj) throws Exception {
Expand All @@ -134,8 +133,7 @@ public void removeRole(PayloadObj<RoleInfo> obj) throws Exception {
RoleInfo info = obj.get();
Map m = info.toMap(new HashMap<>());
m.put("action", REMOVEROLE.toString());
collectionsHandler.handleRequestBody(wrapParams(obj.getRequest(),m), obj.getResponse());

collectionsHandler.handleRequestBody(wrapParams(obj.getRequest(), m), obj.getResponse());
}

@Command(name = "set-obj-property")
Expand All @@ -149,7 +147,6 @@ public void setObjProperty(PayloadObj<ClusterPropInfo> obj) {
} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error in API", e);
}

}

@Command(name = "set-property")
Expand All @@ -158,18 +155,44 @@ public void setProperty(PayloadObj<Map<String,String>> obj) throws Exception {
Map m = obj.get();
m.put("action", CLUSTERPROP.toString());
collectionsHandler.handleRequestBody(wrapParams(obj.getRequest(),m ), obj.getResponse());
}

@Command(name = "set-placement-plugin")
public void setPlacementPlugin(PayloadObj<Map<String, Object>> obj) {
Map<String, Object> placementPluginConfig = obj.getDataMap();
ClusterProperties clusterProperties = new ClusterProperties(coreContainer.getZkController().getZkClient());
// Very basic sanity check (not checking class actually exists)
if (!placementPluginConfig.containsKey(PlacementPluginConfigImpl.CONFIG_CLASS)) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Must contain " + PlacementPluginConfigImpl.CONFIG_CLASS + " attribute");
}
try {
// Need to reset to null first otherwise the mappings in placementPluginConfig are added to existing ones
// in /clusterprops.json rather than replace them
clusterProperties.setClusterProperties(
Collections.singletonMap(PlacementPluginConfigImpl.PLACEMENT_PLUGIN_CONFIG_KEY, null));
clusterProperties.setClusterProperties(
Collections.singletonMap(PlacementPluginConfigImpl.PLACEMENT_PLUGIN_CONFIG_KEY, placementPluginConfig));
} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error in API", e);
}
}

@Command(name = "unset-placement-plugin")
public void unsetPlacementPlugin(PayloadObj<Object> obj) {
ClusterProperties clusterProperties = new ClusterProperties(coreContainer.getZkController().getZkClient());
try {
clusterProperties.setClusterProperties(
Collections.singletonMap(PlacementPluginConfigImpl.PLACEMENT_PLUGIN_CONFIG_KEY, null));
} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error in API", e);
}
}
}

public static class RoleInfo implements ReflectMapWriter {
@JsonProperty(required = true)
public String node;
@JsonProperty(required = true)
public String role;

}


}

0 comments on commit 607f164

Please sign in to comment.