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-16391: Convert create-core, core-status, /luke to JAX-RS #3054

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.client.api.endpoint;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import org.apache.solr.client.api.model.CoreStatusResponse;
import org.apache.solr.client.api.model.CreateCoreParams;
import org.apache.solr.client.api.model.CreateCoreResponse;

public interface CoreApis {

/** V2 API definition for creating a core on the receiving Solr node */
@Path("/cores")
interface Create {
@POST
@Operation(summary = "Create a new core on the receiving Solr node.", tags = "cores")
CreateCoreResponse createCore(CreateCoreParams requestBody) throws Exception;
}

@Path("/cores")
interface GetStatus {

@GET
@Operation(summary = "Fetch status info for all cores hosted on this node.", tags = "cores")
CoreStatusResponse getAllCoreStatus(@QueryParam("indexInfo") Boolean indexInfo)
throws Exception;

@GET
@Operation(
summary = "Fetch status info for the core hosted on this node with the specified name.",
tags = "cores")
@Path("/{coreName}")
CoreStatusResponse getCoreStatus(
@PathParam("coreName") String coreName, @QueryParam("indexInfo") Boolean indexInfo)
throws Exception;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.client.api.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import java.util.Map;

public class CoreStatusResponse extends SolrJerseyResponse {

// NOCOMMIT In the v1 code this is a Map of Exception instances by core name. How are exceptions
// serialized out by things though, that's what I'd have to mirror on the v2 side.
@JsonProperty public Map<String, Object> initFailures;

@JsonProperty public Map<String, SingleCoreData> status;

public static class SingleCoreData {
@JsonProperty public String name;

@JsonProperty public Boolean isLoaded;
@JsonProperty public Boolean isLoading;

@JsonProperty public String instanceDir;
@JsonProperty public String dataDir;
@JsonProperty public String config;
@JsonProperty public String schema;
@JsonProperty public Date startTime;
@JsonProperty public Long uptime;
@JsonProperty public String lastPublished;
@JsonProperty public Integer configVersion;
@JsonProperty public CloudDetails cloud;
@JsonProperty public IndexDetails index;
}

public static class CloudDetails {
@JsonProperty public String collection;
@JsonProperty public String shard;
@JsonProperty public String replica;
@JsonProperty public String replicaType; // TODO enum?
}

public static class IndexDetails {
@JsonProperty public Integer numDocs;
@JsonProperty public Integer maxDoc;
@JsonProperty public Integer deletedDocs;
@JsonProperty public Long version;
@JsonProperty public Integer segmentCount;
@JsonProperty public Boolean current;
@JsonProperty public Boolean hasDeletions;
@JsonProperty public String directory;
@JsonProperty public String segmentsFile;
@JsonProperty public Long segmentsFileSizeInBytes;
@JsonProperty public Map<String, String> userData;
@JsonProperty public Date lastModified;
@JsonProperty public Long sizeInBytes;
@JsonProperty public String size; // Human readable representation of 'sizeInBytes'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.client.api.model;

package org.apache.solr.client.solrj.request.beans;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.Map;
import org.apache.solr.common.annotation.JsonProperty;
import org.apache.solr.common.util.ReflectMapWriter;

public class CreateCorePayload implements ReflectMapWriter {
public class CreateCoreParams {
@JsonProperty(required = true)
public String name;

Expand All @@ -40,21 +39,21 @@ public class CreateCorePayload implements ReflectMapWriter {

@JsonProperty public Boolean loadOnStartup;

// If our JsonProperty clone was more feature-rich here we could specify the property be called
// 'transient', but without that support it needs to be named something else to avoid conflicting
// with the 'transient' keyword in Java
@JsonProperty public Boolean isTransient;
@Schema(name = "isTransient")
@JsonProperty("transient")
public Boolean isTransient;

@JsonProperty public String shard;

@JsonProperty public String collection;

// TODO - what type is 'roles' expected to be?
@JsonProperty public List<String> roles;

@JsonProperty public String replicaType;

@JsonProperty public Map<String, Object> properties;
@JsonProperty public Map<String, String> properties;

@JsonProperty public Map<String, String> collectionProperties;

@JsonProperty public String coreNodeName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.client.api.model;

package org.apache.solr.client.solrj.request;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;
import org.apache.solr.common.util.NamedList;

public class CoreStatus {

private final NamedList<Object> response;

public CoreStatus(NamedList<Object> response) {
this.response = response;
}

public String getDataDirectory() {
return (String) response.get("dataDir");
}

public String getInstanceDirectory() {
return (String) response.findRecursive("instanceDir");
}

@Override
public String toString() {
return response.toString();
}

public Date getCoreStartTime() {
return (Date) response.get("startTime");
}
/** Response for {@link org.apache.solr.client.api.endpoint.CoreApis} 'create' API */
public class CreateCoreResponse extends SolrJerseyResponse {
@JsonProperty public String core;
}
22 changes: 10 additions & 12 deletions solr/core/src/java/org/apache/solr/cli/CLIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static org.apache.solr.common.SolrException.ErrorCode.FORBIDDEN;
import static org.apache.solr.common.SolrException.ErrorCode.UNAUTHORIZED;
import static org.apache.solr.common.params.CommonParams.NAME;
import static org.apache.solr.common.params.CommonParams.SYSTEM_INFO_PATH;

import java.io.IOException;
Expand All @@ -45,7 +44,7 @@
import org.apache.solr.client.solrj.impl.Http2SolrClient;
import org.apache.solr.client.solrj.impl.SolrZkClientTimeout;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.request.CoresApi;
import org.apache.solr.client.solrj.request.GenericSolrRequest;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.SolrZkClient;
Expand Down Expand Up @@ -321,16 +320,15 @@ public static boolean safeCheckCoreExists(String solrUrl, String coreName, Strin
final int clamPeriodForStatusPollMs = 1000;
Thread.sleep(clamPeriodForStatusPollMs);
}
NamedList<Object> existsCheckResult =
CoreAdminRequest.getStatus(coreName, solrClient).getResponse();
NamedList<Object> status = (NamedList<Object>) existsCheckResult.get("status");
NamedList<Object> coreStatus = (NamedList<Object>) status.get(coreName);
Map<String, Object> failureStatus =
(Map<String, Object>) existsCheckResult.get("initFailures");
String errorMsg = (String) failureStatus.get(coreName);
final boolean hasName = coreStatus != null && coreStatus.get(NAME) != null;
exists = hasName || errorMsg != null;
wait = hasName && errorMsg == null && "true".equals(coreStatus.get("isLoading"));
final var coreStatusReq = new CoresApi.GetCoreStatus(coreName);
final var coreStatusRsp = coreStatusReq.process(solrClient).getParsed();
final var coreStatusByName = coreStatusRsp.status;
final var coreStatus = coreStatusByName.get(coreName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these var are a lot more readable...

final var failureStatus = coreStatusRsp.initFailures;
final var initFailureForCore = failureStatus.get(coreName);
final boolean hasName = coreStatus != null && coreStatus.name != null;
exists = hasName || initFailureForCore != null;
wait = hasName && initFailureForCore == null && "true".equals(coreStatus.isLoading);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boo that we are string comparing versus using a boolean...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I see boolean true used elsewhere for isLoading?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} while (wait && System.nanoTime() - startWaitAt < MAX_WAIT_FOR_CORE_LOAD_NANOS);
} catch (Exception exc) {
// just ignore it since we're only interested in a positive result here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -41,25 +40,22 @@
import org.apache.solr.api.Api;
import org.apache.solr.api.JerseyResource;
import org.apache.solr.cloud.CloudDescriptor;
import org.apache.solr.cloud.ZkController;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CommonAdminParams;
import org.apache.solr.common.params.CoreAdminParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.EnvUtils;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SolrNamedThreadFactory;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.CoreDescriptor;
import org.apache.solr.handler.RequestHandlerBase;
import org.apache.solr.handler.admin.api.AllCoresStatusAPI;
import org.apache.solr.handler.admin.api.CoreSnapshot;
import org.apache.solr.handler.admin.api.CreateCoreAPI;
import org.apache.solr.handler.admin.api.CoreStatus;
import org.apache.solr.handler.admin.api.CreateCore;
import org.apache.solr.handler.admin.api.CreateCoreBackup;
import org.apache.solr.handler.admin.api.GetNodeCommandStatus;
import org.apache.solr.handler.admin.api.InstallCoreData;
Expand All @@ -74,7 +70,6 @@
import org.apache.solr.handler.admin.api.RequestCoreRecoveryAPI;
import org.apache.solr.handler.admin.api.RequestSyncShardAPI;
import org.apache.solr.handler.admin.api.RestoreCore;
import org.apache.solr.handler.admin.api.SingleCoreStatusAPI;
import org.apache.solr.handler.admin.api.SplitCoreAPI;
import org.apache.solr.handler.admin.api.SwapCores;
import org.apache.solr.handler.admin.api.UnloadCore;
Expand Down Expand Up @@ -289,35 +284,6 @@ private static Map<String, CoreAdminOp> initializeOpMap() {
return opMap;
}

protected static Map<String, String> buildCoreParams(SolrParams params) {

Map<String, String> coreParams = new HashMap<>();

// standard core create parameters
for (Map.Entry<String, String> entry : paramToProp.entrySet()) {
String value = params.get(entry.getKey(), null);
if (StrUtils.isNotNullOrEmpty(value)) {
coreParams.put(entry.getValue(), value);
}
}

// extra properties
Iterator<String> paramsIt = params.getParameterNamesIterator();
while (paramsIt.hasNext()) {
String param = paramsIt.next();
if (param.startsWith(CoreAdminParams.PROPERTY_PREFIX)) {
String propName = param.substring(CoreAdminParams.PROPERTY_PREFIX.length());
String propValue = params.get(param);
coreParams.put(propName, propValue);
}
if (param.startsWith(ZkController.COLLECTION_PARAM_PREFIX)) {
coreParams.put(param, params.get(param));
}
}

return coreParams;
}

public static ModifiableSolrParams params(String... params) {
ModifiableSolrParams msp = new ModifiableSolrParams();
for (int i = 0; i < params.length; i += 2) {
Expand Down Expand Up @@ -374,9 +340,6 @@ void call() throws Exception {
@Override
public Collection<Api> getApis() {
final List<Api> apis = new ArrayList<>();
apis.addAll(AnnotatedApi.getApis(new AllCoresStatusAPI(this)));
apis.addAll(AnnotatedApi.getApis(new SingleCoreStatusAPI(this)));
apis.addAll(AnnotatedApi.getApis(new CreateCoreAPI(this)));
apis.addAll(AnnotatedApi.getApis(new RejoinLeaderElectionAPI(this)));
apis.addAll(AnnotatedApi.getApis(new OverseerOperationAPI(this)));
apis.addAll(AnnotatedApi.getApis(new SplitCoreAPI(this)));
Expand All @@ -394,7 +357,9 @@ public Collection<Api> getApis() {
public Collection<Class<? extends JerseyResource>> getJerseyResources() {
return List.of(
CoreSnapshot.class,
CoreStatus.class,
InstallCoreData.class,
CreateCore.class,
CreateCoreBackup.class,
RestoreCore.class,
ReloadCore.class,
Expand Down
Loading
Loading