This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from o19s/queue-queries
Writing queries to a queue prior to indexing
- Loading branch information
Showing
13 changed files
with
238 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
#!/bin/bash -e | ||
|
||
# Delete the store if it exists. | ||
curl -X DELETE http://localhost:9200/_plugins/ubi/mystore | ||
curl -X DELETE "http://localhost:9200/_plugins/ubi/mystore" | ||
|
||
# Create the store | ||
curl -X PUT http://localhost:9200/_plugins/ubi/mystore | ||
# Create the store. | ||
curl -X PUT "http://localhost:9200/_plugins/ubi/mystore?index=ecommerce" | ||
|
||
# Insert events | ||
locust -f load-test.py --headless -u 1 -r 1 --run-time 10s --host http://localhost:9200 | ||
# Index subset of Chorus data. | ||
../index-chorus-data.sh `realpath ../../chorus-opensearch-edition` | ||
|
||
# Insert events and queries. | ||
locust -f load-test.py --headless -u 1 -r 1 --run-time 30s --host http://localhost:9200 | ||
|
||
# Let events index. | ||
sleep 2 | ||
sleep 10 | ||
|
||
# Get count of indexed events. | ||
EVENTS=`curl -s http://localhost:9200/.mystore_events/_count | jq .count` | ||
echo "Found $EVENTS indexed" | ||
echo "Found $EVENTS events" | ||
|
||
# Get count of indexed queries. | ||
QUERIES=`curl -s http://localhost:9200/.mystore_queries/_count | jq .count` | ||
echo "Found $QUERIES queries" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package com.o19s.ubi; | ||
|
||
import com.o19s.ubi.events.Event; | ||
import com.o19s.ubi.events.EventManager; | ||
import com.o19s.ubi.model.QueryRequest; | ||
import com.o19s.ubi.utils.UbiUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.action.bulk.BulkRequest; | ||
import org.opensearch.action.index.IndexRequest; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.common.xcontent.XContentType; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* An event manager that inserts events into an OpenSearch index. | ||
*/ | ||
public class OpenSearchEventManager extends EventManager { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(OpenSearchEventManager.class); | ||
|
||
private final Client client; | ||
private static OpenSearchEventManager openSearchEventManager; | ||
|
||
private OpenSearchEventManager(Client client) { | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public void process() { | ||
|
||
if(eventsQueue.size() > 0) { | ||
|
||
final BulkRequest eventsBulkRequest = new BulkRequest(); | ||
LOGGER.info("Bulk inserting " + eventsQueue.size() + " UBI events"); | ||
|
||
for (final Event event : eventsQueue.get()) { | ||
|
||
final IndexRequest indexRequest = new IndexRequest(event.getIndexName()) | ||
.source(event.getEvent(), XContentType.JSON); | ||
|
||
eventsBulkRequest.add(indexRequest); | ||
|
||
} | ||
|
||
eventsQueue.clear(); | ||
client.bulk(eventsBulkRequest); | ||
|
||
} | ||
|
||
if(queryRequestsQueue.size() > 0) { | ||
|
||
final BulkRequest queryRequestsBulkRequest = new BulkRequest(); | ||
LOGGER.info("Bulk inserting " + queryRequestsQueue.size() + " UBI queries"); | ||
|
||
for(final QueryRequest queryRequest : queryRequestsQueue.get()) { | ||
|
||
LOGGER.info("Writing query ID {} with response ID {}", | ||
queryRequest.getQueryId(), queryRequest.getQueryResponse().getQueryResponseId()); | ||
|
||
// What will be indexed - adheres to the queries-mapping.json | ||
final Map<String, Object> source = new HashMap<>(); | ||
source.put("timestamp", queryRequest.getTimestamp()); | ||
source.put("query_id", queryRequest.getQueryId()); | ||
source.put("query", queryRequest.getQuery()); | ||
source.put("query_response_id", queryRequest.getQueryResponse().getQueryResponseId()); | ||
source.put("query_response_hit_ids", queryRequest.getQueryResponse().getQueryResponseHitIds()); | ||
source.put("user_id", queryRequest.getUserId()); | ||
source.put("session_id", queryRequest.getSessionId()); | ||
|
||
// Get the name of the queries. | ||
final String queriesIndexName = UbiUtils.getQueriesIndexName(queryRequest.getStoreName()); | ||
|
||
// Build the index request. | ||
final IndexRequest indexRequest = new IndexRequest(queriesIndexName) | ||
.source(source, XContentType.JSON); | ||
|
||
queryRequestsBulkRequest.add(indexRequest); | ||
|
||
} | ||
|
||
queryRequestsQueue.clear(); | ||
client.bulk(queryRequestsBulkRequest); | ||
|
||
} | ||
|
||
} | ||
|
||
@Override | ||
public void add(final Event event) { | ||
eventsQueue.add(event); | ||
} | ||
|
||
@Override | ||
public void add(final QueryRequest queryRequest) { | ||
queryRequestsQueue.add(queryRequest); | ||
} | ||
|
||
/** | ||
* Gets a singleton instance of the manager. | ||
* @param client An OpenSearch {@link Client}. | ||
* @return An instance of {@link OpenSearchEventManager}. | ||
*/ | ||
public static OpenSearchEventManager getInstance(Client client) { | ||
if(openSearchEventManager == null) { | ||
openSearchEventManager = new OpenSearchEventManager(client); | ||
} | ||
return openSearchEventManager; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.