Skip to content

Commit

Permalink
Resolving merge conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Feb 26, 2024
2 parents f2ae66e + 16262e0 commit f1c6adb
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Start the containers:

`docker compose up`

Initialize the `awesome` search relevance index:
Initialize the `awesome` UBL store:

```
curl -X PUT http://localhost:9200/_plugins/ubi/awesome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
import org.apache.logging.log4j.Logger;
import org.opensearch.client.node.NodeClient;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.ubi.backends.Backend;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.ubi.backends.Backend;

import java.util.List;
import java.util.Set;

import static org.opensearch.rest.RestRequest.Method.*;

Expand Down Expand Up @@ -55,38 +56,66 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient nod

final String storeName = request.param("store");

LOGGER.info("Creating search relevance index {}", storeName);
backend.initialize(storeName);
return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, "created"));
// Validate the store name.
if(!backend.validateStoreName(storeName)) {
return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, "missing store name"));
}

LOGGER.info("Creating UBL store {}", storeName);

return (channel) -> {
/*if(backend.exists(storeName)) {
channel.sendResponse(new BytesRestResponse(RestStatus.CONFLICT, "already exists"));
} else {*/
backend.initialize(storeName);
channel.sendResponse(new BytesRestResponse(RestStatus.OK, "created"));
//}
};

} else if (request.method() == DELETE) {

final String storeName = request.param("store");

// TODO: Make sure the store actually exists first.
// Validate the store name.
if(!backend.validateStoreName(storeName)) {
return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, "missing store name"));
}

LOGGER.info("Deleting UBL store {}", storeName);

LOGGER.info("Deleting search relevance index {}", storeName);
backend.delete(storeName);
return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, "deleted"));
return (channel) -> {
backend.delete(storeName);
channel.sendResponse(new BytesRestResponse(RestStatus.OK, "created"));
};

} else if (request.method() == POST) {

if (request.hasContent()) {

final String storeName = request.param("store");

// TODO: Make sure the store actually exists first.
// Make sure the store exists.
/*if(!backend.exists(storeName)) {
return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.NOT_FOUND, "store not found"));
}*/

LOGGER.info("Persisting event into {}", storeName);
LOGGER.info("Queuing event for storage into UBL store {}", storeName);
final String eventJson = request.content().utf8ToString();
backend.persistEvent(storeName, eventJson);

return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, "Event received"));
return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, "event received"));

} else {
throw new IllegalArgumentException("Missing event content");
}

} else if (request.method() == GET) {

final Set<String> stores = backend.get();
final String s = String.join(",", stores);

return (channel) -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, s));

}

// TODO: Return a list names of all search_relevance stores.
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/opensearch/ubi/backends/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

package org.opensearch.ubi.backends;

import org.opensearch.ubi.model.QueryResponse;
import org.opensearch.ubi.model.QueryRequest;
import org.opensearch.ubi.model.QueryResponse;

import java.util.List;
import java.util.Set;

public interface Backend {

Expand All @@ -23,6 +23,10 @@ public interface Backend {

void persistQuery(final String storeName, QueryRequest queryRequest, QueryResponse queryResponse) throws Exception;

List<String> get();
Set<String> get();

boolean exists(final String storeName);

boolean validateStoreName(final String storeName);

}
52 changes: 44 additions & 8 deletions src/main/java/org/opensearch/ubi/backends/OpenSearchBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.apache.logging.log4j.Logger;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest;
import org.opensearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexMetadata;
Expand All @@ -28,10 +30,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class OpenSearchBackend implements Backend {

Expand All @@ -51,9 +53,6 @@ public OpenSearchBackend(final Client client) {
@Override
public void initialize(final String storeName) {

// TODO: Determine if already initialized with this index name first.
// TODO: Also need some error handling around this in case one or both of these index creations fail.

LOGGER.info("Creating search relevance store {}", storeName);

// Create the events index.
Expand Down Expand Up @@ -130,9 +129,46 @@ public void persistQuery(final String storeName, final QueryRequest queryRequest
}

@Override
public List<String> get() {
// TODO: Get the list of initialized stores for the plugin.
return new ArrayList<>();
public Set<String> get() {

/*final GetIndexRequest getIndexRequest = new GetIndexRequest();
final GetIndexResponse getIndexResponse = client.admin().indices().getIndex(getIndexRequest).actionGet();
final String[] indexes = getIndexResponse.indices();
final Set<String> stores = new HashSet<>();
for(final String index : indexes) {
LOGGER.info("Index name: " + index);
if(index.startsWith(".") && index.endsWith("_queries")) {
stores.add(index);
}
}
return stores;*/

return Collections.emptySet();

}

@Override
public boolean exists(final String storeName) {

final String indexName = getEventsIndexName(storeName);

// TODO: This has to run on a non-blocking thread.
final IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(indexName);
final IndicesExistsResponse indicesExistsResponse = client.admin().indices().exists(indicesExistsRequest).actionGet();

return indicesExistsResponse.isExists();

}

@Override
public boolean validateStoreName(final String storeName) {

// Validate the store name.
return storeName != null && !storeName.isEmpty();

}

private String getEventsIndexName(final String storeName) {
Expand Down
23 changes: 23 additions & 0 deletions src/yamlRestTest/resources/rest-api-spec/api/ubl.create_store.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"ubl.create_store": {
"stability": "stable",
"url": {
"paths": [
{
"path": "/_plugins/ubl/{store}",
"parts": {
"store": {
"required": false,
"type": "string",
"description": "The name of the store"
}
},
"methods": [
"PUT"
]
}
]
},
"body": null
}
}
29 changes: 29 additions & 0 deletions src/yamlRestTest/resources/rest-api-spec/api/ubl.delete_store.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"ubl.delete_store": {
"stability": "stable",
"url": {
"paths": [
{
"path": "/_plugins/ubl",
"methods": [
"DELETE"
]
},
{
"path": "/_plugins/ubl/{store}",
"parts": {
"store": {
"required": false,
"type": "string",
"description": "The store name"
}
},
"methods": [
"DELETE"
]
}
]
},
"body": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"Create and delete a store":
- do:
ubl.create_store:
store: mystore

- do:
indices.exists:
index: .mystore_events

- match: { created }

- do:
ubl.delete_store:
store: mystore

- do:
indices.exists:
index: .mystore_events

- is_false: ''

0 comments on commit f1c6adb

Please sign in to comment.