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

Update Registry Manager to support multi-tenancy and OpenSearch Serverless #73

Merged
merged 15 commits into from
Jul 5, 2024
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ POSSIBILITY OF SUCH DAMAGE.
<dependency>
<groupId>gov.nasa.pds</groupId>
<artifactId>registry-common</artifactId>
<version>1.5.1</version>
<version>1.6.0-SNAPSHOT</version>
</dependency>
<!-- JSON parser -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import gov.nasa.pds.registry.mgr.cmd.dd.UpgradeDDCmd;
import gov.nasa.pds.registry.mgr.cmd.reg.CreateRegistryCmd;
import gov.nasa.pds.registry.mgr.cmd.reg.DeleteRegistryCmd;
import gov.nasa.pds.registry.mgr.cmd.reg.FetchRegistryCmd;
import gov.nasa.pds.registry.mgr.cmd.reg.KnownRegistryCmd;
import gov.nasa.pds.registry.mgr.util.log.Log4jConfigurator;


Expand Down Expand Up @@ -73,7 +75,8 @@ public static void printHelp()
System.out.println("Registry:");
System.out.println(" create-registry Create registry and data dictionary indices");
System.out.println(" delete-registry Delete registry and data dictionary indices and all its data");

System.out.println(" fetch-registry Fetch a registry connection URL and output it to stdout");
System.out.println(" known-registries List all known registry XML connections contained in this artifact");
System.out.println();
System.out.println("Data Dictionary:");
System.out.println(" list-dd List data dictionaries");
Expand Down Expand Up @@ -239,6 +242,8 @@ private void initCommands()
// Registry
commands.put("create-registry", new CreateRegistryCmd());
commands.put("delete-registry", new DeleteRegistryCmd());
commands.put("known-registries", new KnownRegistryCmd());
commands.put("fetch-registry", new FetchRegistryCmd());

// Data dictionary
commands.put("list-dd", new ListDDCmd());
Expand Down Expand Up @@ -314,6 +319,9 @@ private void initOptions()
bld = Option.builder("packageId").hasArg().argName("id");
options.addOption(bld.build());

bld = Option.builder("rc").hasArg().argName("rc");
options.addOption(bld.build());

bld = Option.builder("status").hasArg().argName("status");
options.addOption(bld.build());

Expand Down
109 changes: 28 additions & 81 deletions src/main/java/gov/nasa/pds/registry/mgr/cmd/data/DeleteDataCmd.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package gov.nasa.pds.registry.mgr.cmd.data;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;

import org.apache.commons.cli.CommandLine;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;

import com.google.gson.Gson;

import gov.nasa.pds.registry.common.es.client.EsClientFactory;
import gov.nasa.pds.registry.common.es.client.EsUtils;
import gov.nasa.pds.registry.common.util.CloseUtils;
import gov.nasa.pds.registry.common.ConnectionFactory;
import gov.nasa.pds.registry.common.EstablishConnectionFactory;
import gov.nasa.pds.registry.common.Request;
import gov.nasa.pds.registry.common.ResponseException;
import gov.nasa.pds.registry.common.RestClient;
import gov.nasa.pds.registry.mgr.Constants;
import gov.nasa.pds.registry.mgr.cmd.CliCommand;
import gov.nasa.pds.registry.mgr.dao.RegistryRequestBuilder;


/**
Expand All @@ -30,8 +20,6 @@
public class DeleteDataCmd implements CliCommand
{
private String filterMessage;
private String regQuery;
private String refsQuery;


/**
Expand All @@ -51,120 +39,79 @@ public void run(CommandLine cmdLine) throws Exception
return;
}

String esUrl = cmdLine.getOptionValue("es", "http://localhost:9200");
String esUrl = cmdLine.getOptionValue("es", "app:/connections/direct/localhost.xml");
String indexName = cmdLine.getOptionValue("index", Constants.DEFAULT_REGISTRY_INDEX);
String authPath = cmdLine.getOptionValue("auth");

buildEsQuery(cmdLine);

System.out.println("Elasticsearch URL: " + esUrl);
System.out.println(" Index: " + indexName);
System.out.println(filterMessage);
System.out.println();

RestClient client = null;

try
{
client = EsClientFactory.createRestClient(esUrl, authPath);
ConnectionFactory conFact = EstablishConnectionFactory.from(esUrl, authPath);
try (RestClient client = conFact.createRestClient()) {
Request.DeleteByQuery regQuery = client.createDeleteByQuery().setIndex(indexName),
refQuery = client.createDeleteByQuery().setIndex(indexName + "-refs");
buildEsQuery(cmdLine, regQuery, refQuery);
// Delete from registry index
deleteByQuery(client, indexName, regQuery);
deleteByQuery(indexName, client.performRequest(regQuery));
// Delete from product references index
deleteByQuery(client, indexName + "-refs", refsQuery);
deleteByQuery(indexName + "-refs", client.performRequest(refQuery));
}
catch(ResponseException ex)
{
throw new Exception(EsUtils.extractErrorMessage(ex));
}
finally
{
CloseUtils.close(client);
throw new Exception(ex.extractErrorMessage());
}
}


private static void deleteByQuery(RestClient client, String indexName, String query) throws Exception
{
Request req = new Request("POST", "/" + indexName + "/_delete_by_query");
req.setJsonEntity(query);

Response resp = client.performRequest(req);
double numDeleted = extractNumDeleted(resp);

System.out.format("Deleted %.0f document(s) from %s index\n", numDeleted, indexName);
}


@SuppressWarnings("rawtypes")
private static double extractNumDeleted(Response resp)
private static void deleteByQuery(String indexName, long numDeleted) throws Exception
{
try
{
InputStream is = resp.getEntity().getContent();
Reader rd = new InputStreamReader(is);

Gson gson = new Gson();
Object obj = gson.fromJson(rd, Object.class);
rd.close();

obj = ((Map)obj).get("deleted");
return (Double)obj;
}
catch(Exception ex)
{
return 0;
}
System.out.format("Deleted %d document(s) from %s index\n", numDeleted, indexName);
}


/**
* Build Elasticsearch query to delete records.
* Records can be deleted by LIDVID, LID, PackageID. All records can also be deleted.
* @param cmdLine
* @throws Exception
*/
private void buildEsQuery(CommandLine cmdLine) throws Exception
{
// Registry index
RegistryRequestBuilder regBld = new RegistryRequestBuilder();
// Product references index
RegistryRequestBuilder refsBld = new RegistryRequestBuilder();

private void buildEsQuery(CommandLine cmdLine, Request.DeleteByQuery regQuery, Request.DeleteByQuery refsQuery) throws Exception
{
String id = cmdLine.getOptionValue("lidvid");
if(id != null)
{
this.filterMessage = " LIDVID: " + id;
this.regQuery = regBld.createFilterQuery("lidvid", id);
this.refsQuery = refsBld.createFilterQuery("collection_lidvid", id);

regQuery.createFilterQuery("lidvid", id);
refsQuery.createFilterQuery("collection_lidvid", id);
return;
}

id = cmdLine.getOptionValue("lid");
if(id != null)
{
this.filterMessage = " LID: " + id;
this.regQuery = regBld.createFilterQuery("lid", id);
this.refsQuery = refsBld.createFilterQuery("collection_lid", id);

regQuery.createFilterQuery("lid", id);
refsQuery.createFilterQuery("collection_lid", id);
return;
}

id = cmdLine.getOptionValue("packageId");
if(id != null)
{
this.filterMessage = " Package ID: " + id;
this.regQuery = regBld.createFilterQuery("_package_id", id);
this.refsQuery = refsBld.createFilterQuery("_package_id", id);
regQuery.createFilterQuery("_package_id", id);
refsQuery.createFilterQuery("_package_id", id);

return;
}

if(cmdLine.hasOption("all"))
{
this.filterMessage = "Delete all documents ";
this.regQuery = regBld.createMatchAllQuery();
this.refsQuery = refsBld.createMatchAllQuery();
regQuery.createMatchAllQuery();
refsQuery.createMatchAllQuery();

return;
}
Expand All @@ -187,7 +134,7 @@ public void printHelp()
System.out.println(" -all Delete all data");
System.out.println("Optional parameters:");
System.out.println(" -auth <file> Authentication config file");
System.out.println(" -es <url> Elasticsearch URL. Default is http://localhost:9200");
System.out.println(" -es <url> Elasticsearch URL. Default is app:/connections/direct/localhost.xml");
System.out.println(" -index <name> Elasticsearch index name. Default is 'registry'");
System.out.println();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void run(CommandLine cmdLine) throws Exception
throw new Exception("Missing required parameter '-file'");
}

String esUrl = cmdLine.getOptionValue("es", "http://localhost:9200");
String esUrl = cmdLine.getOptionValue("es", "app:/connections/direct/localhost.xml");
String indexName = cmdLine.getOptionValue("index", Constants.DEFAULT_REGISTRY_INDEX);
String authPath = cmdLine.getOptionValue("auth");

Expand Down Expand Up @@ -138,7 +138,7 @@ public void printHelp()
System.out.println(" -all Export all data");
System.out.println("Optional parameters:");
System.out.println(" -auth <file> Authentication config file");
System.out.println(" -es <url> Elasticsearch URL. Default is http://localhost:9200");
System.out.println(" -es <url> Elasticsearch URL. Default is app:/connections/direct/localhost.xml");
System.out.println(" -index <name> Elasticsearch index name. Default is 'registry'");
System.out.println();
}
Expand Down
Loading
Loading