Skip to content

Commit

Permalink
Change readMap to readGenericMap; deal with response.decRef()
Browse files Browse the repository at this point in the history
  • Loading branch information
renekrie committed Aug 28, 2024
1 parent 20231a3 commit 35e05b9
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 72 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<jts.version>1.19.0</jts.version>
<json-path.version>2.7.0</json-path.version>

<querqy.lucene.version>5.7.lucene942.0</querqy.lucene.version>
<querqy.lucene.version>5.7.lucene961.0</querqy.lucene.version>

<elasticsearch.version>8.14.2</elasticsearch.version>
<jacoco.version>0.8.12</jacoco.version>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/querqy/elasticsearch/query/Rewriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Rewriter(final StreamInput in) throws IOException {
name = in.readString();
final boolean hasParams = in.readBoolean();
if (hasParams) {
params = in.readMap(StreamInput::readString, StreamInput::readGenericValue);
params = in.readGenericMap();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class PutRewriterRequest extends ActionRequest {
public PutRewriterRequest(final StreamInput in) throws IOException {
super(in);
rewriterId = in.readString();
content = in.readMap(StreamInput::readString, StreamInput::readGenericValue);
content = in.readGenericMap();
}

public PutRewriterRequest(final String rewriterId, final Map<String, Object> content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void testThatQueryAnalyzerIsApplied() throws Exception {

SearchResponse response = client().search(searchRequestBuilder.request()).get();
assertEquals(1L, response.getHits().getTotalHits().value);
response.decRef();

final QuerqyQueryBuilder queryAnalyzerMismatch = new QuerqyQueryBuilder(
getInstanceFromNode(QuerqyProcessor.class));
Expand All @@ -49,6 +50,7 @@ public void testThatQueryAnalyzerIsApplied() throws Exception {

response = client().search(searchRequestBuilder.request()).get();
assertEquals(0L, response.getHits().getTotalHits().value);
response.decRef();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ public void testUpdate1To3() throws Exception {
assertEquals(false, config_v_003_mapping.get("doc_values"));

}


}
23 changes: 14 additions & 9 deletions src/test/java/querqy/elasticsearch/RewriterIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testSearchWithConfig() throws Exception {

final PutRewriterRequest request = new PutRewriterRequest("common_rules", content);

client().execute(PutRewriterAction.INSTANCE, request).get();
client().execute(PutRewriterAction.INSTANCE, request).get().decRef();

QuerqyQueryBuilder querqyQuery = new QuerqyQueryBuilder(getInstanceFromNode(QuerqyProcessor.class));
querqyQuery.setRewriters(Collections.singletonList(new Rewriter("common_rules")));
Expand All @@ -65,6 +65,7 @@ public void testSearchWithConfig() throws Exception {

SearchResponse response = client().search(searchRequestBuilder.request()).get();
assertEquals(2L, response.getHits().getTotalHits().value);
response.decRef();

}

Expand All @@ -82,7 +83,7 @@ public void testRawQuery() throws Exception {

final PutRewriterRequest request = new PutRewriterRequest("common_rules", content);

client().execute(PutRewriterAction.INSTANCE, request).get();
client().execute(PutRewriterAction.INSTANCE, request).get().decRef();

QuerqyQueryBuilder querqyQuery = new QuerqyQueryBuilder(getInstanceFromNode(QuerqyProcessor.class));
querqyQuery.setRewriters(Collections.singletonList(new Rewriter("common_rules")));
Expand All @@ -97,6 +98,7 @@ public void testRawQuery() throws Exception {
final SearchHits hits = response.getHits();
assertEquals(1L, hits.getTotalHits().value);
assertEquals("a c", hits.getHits()[0].getSourceAsMap().get("field2"));
response.decRef();
}

@Test
Expand Down Expand Up @@ -124,6 +126,7 @@ public void testLargeConfig() throws Exception {
final PutRewriterResponse response = client().execute(PutRewriterAction.INSTANCE, request).get();

assertEquals(RestStatus.CREATED, response.status());
response.decRef();

}

Expand All @@ -142,7 +145,7 @@ public void testSearchWithUpdatedConfig() throws Exception {

final PutRewriterRequest request = new PutRewriterRequest("common_rules", content);

client().execute(PutRewriterAction.INSTANCE, request).get();
client().execute(PutRewriterAction.INSTANCE, request).get().decRef();

QuerqyQueryBuilder querqyQuery = new QuerqyQueryBuilder(getInstanceFromNode(QuerqyProcessor.class));
querqyQuery.setRewriters(Collections.singletonList(new Rewriter("common_rules")));
Expand All @@ -155,6 +158,7 @@ public void testSearchWithUpdatedConfig() throws Exception {

SearchResponse response = client().search(searchRequestBuilder.request()).get();
assertEquals(1L, response.getHits().getTotalHits().value);
response.decRef();


final Map<String, Object> content2 = new HashMap<>();
Expand All @@ -167,7 +171,7 @@ public void testSearchWithUpdatedConfig() throws Exception {
content2.put("config", config2);

final PutRewriterRequest request2 = new PutRewriterRequest("common_rules", content2);
client().execute(PutRewriterAction.INSTANCE, request2).get();
client().execute(PutRewriterAction.INSTANCE, request2).get().decRef();

QuerqyQueryBuilder querqyQuery2 = new QuerqyQueryBuilder(getInstanceFromNode(QuerqyProcessor.class));
querqyQuery2.setRewriters(Collections.singletonList(new Rewriter("common_rules")));
Expand All @@ -180,6 +184,7 @@ public void testSearchWithUpdatedConfig() throws Exception {

SearchResponse response2 = client().search(searchRequestBuilder2.request()).get();
assertEquals(2L, response2.getHits().getTotalHits().value);
response2.decRef();

}

Expand All @@ -199,7 +204,7 @@ public void testThatRewriterIsDeleted() throws Exception {

final PutRewriterRequest request = new PutRewriterRequest("common_rules", content);

client().execute(PutRewriterAction.INSTANCE, request).get();
client().execute(PutRewriterAction.INSTANCE, request).get().decRef();

QuerqyQueryBuilder querqyQuery = new QuerqyQueryBuilder(getInstanceFromNode(QuerqyProcessor.class));
querqyQuery.setRewriters(Collections.singletonList(new Rewriter("common_rules")));
Expand All @@ -212,10 +217,10 @@ public void testThatRewriterIsDeleted() throws Exception {

SearchResponse response = client().search(searchRequestBuilder.request()).get();
assertEquals(2L, response.getHits().getTotalHits().value);

response.decRef();

final DeleteRewriterRequest delRequest = new DeleteRewriterRequest("common_rules");
client().execute(DeleteRewriterAction.INSTANCE, delRequest).get();
client().execute(DeleteRewriterAction.INSTANCE, delRequest).get().decRef();

QuerqyQueryBuilder querqyQuery2 = new QuerqyQueryBuilder(getInstanceFromNode(QuerqyProcessor.class));
querqyQuery2.setRewriters(Collections.singletonList(new Rewriter("common_rules")));
Expand All @@ -227,7 +232,7 @@ public void testThatRewriterIsDeleted() throws Exception {
searchRequestBuilder2.setQuery(querqyQuery2);

try {
client().search(searchRequestBuilder.request()).get();
client().search(searchRequestBuilder.request()).get().decRef();
fail("Could use deleted rewriter in request");
} catch (final ExecutionException e) {
assertTrue(e.getMessage().contains("Rewriter not found: common_rules"));
Expand All @@ -239,7 +244,7 @@ public void testThatRewriterIsDeleted() throws Exception {

@After
public void deleteRewriterIndex() {
client().admin().indices().prepareDelete(".querqy").get();
client().admin().indices().prepareDelete(".querqy").get().decRef();
}


Expand Down
18 changes: 12 additions & 6 deletions src/test/java/querqy/elasticsearch/RewriterShardContextsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void testClearRewritersFromCache() throws Exception {
config1.put("p1", 1L);
payload1.put("config", config1);

queryClient().execute(PutRewriterAction.INSTANCE, new PutRewriterRequest("r2", payload1)).get();
queryClient().execute(PutRewriterAction.INSTANCE, new PutRewriterRequest("r2", payload1)).get()
.decRef();

// assure we can use the rewriter in the query
QuerqyQueryBuilder query = new QuerqyQueryBuilder();
Expand All @@ -75,11 +76,13 @@ public void testClearRewritersFromCache() throws Exception {
final SearchResponse response1 = queryClient().prepareSearch("idx").setPreference("_only_nodes:node_s1,node_s2")
.setQuery(query).setRequestCache(false).execute().get();
assertEquals(0, response1.getFailedShards());
response1.decRef();

// clear loaded rewriters
final NodesClearRewriterCacheResponse clearRewriterCacheResponse1 = client()
.execute(NodesClearRewriterCacheAction.INSTANCE, new NodesClearRewriterCacheRequest()).get();
assertFalse(clearRewriterCacheResponse1.hasFailures());
clearRewriterCacheResponse1.decRef();

QuerqyQueryBuilder query3 = new QuerqyQueryBuilder();

Expand All @@ -91,11 +94,12 @@ public void testClearRewritersFromCache() throws Exception {
final SearchResponse response2 = queryClient().prepareSearch("idx").setPreference("_only_nodes:node_s1,node_s2")
.setQuery(query3).setRequestCache(false).execute().get();
assertEquals(0, response2.getFailedShards()); // rewriter probably reloaded
response2.decRef();

// delete rewriter config from .query index - this should never be done directly (use a delete rewriter action)
final DeleteResponse deleteResponse = client().prepareDelete(".querqy", "r2").execute().get();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());

deleteResponse.decRef();

QuerqyQueryBuilder query2 = new QuerqyQueryBuilder();

Expand All @@ -108,18 +112,20 @@ public void testClearRewritersFromCache() throws Exception {
final SearchResponse response3 = queryClient().prepareSearch("idx")
.setPreference("_only_nodes:node_s1,node_s2").setQuery(query2).execute().get();
assertEquals(0, response3.getFailedShards());
response3.decRef();

// clear loaded rewriters
final NodesClearRewriterCacheResponse clearRewriterCacheResponse2 = client().
execute(NodesClearRewriterCacheAction.INSTANCE, new NodesClearRewriterCacheRequest()).get();
assertFalse(clearRewriterCacheResponse2.hasFailures());
clearRewriterCacheResponse2.decRef();

// now we should crash: rewriters are neither loaded nor will there be a config in the .querqy index

try {

queryClient().prepareSearch("idx").setPreference("_only_nodes:node_s1,node_s2")
.setQuery(query).execute().get();
.setQuery(query).execute().get().decRef();
fail("Rewriter must not exist");

} catch (final ExecutionException e) {
Expand All @@ -140,15 +146,15 @@ public void index() {
client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder()
.put("index.shard.check_on_startup", false)
.put("index.number_of_shards", 2)
.put("index.number_of_replicas", 1)).get();
.put("index.number_of_replicas", 1)).get().decRef();
client().prepareIndex(indexName)
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
.setSource("field1", "a b", "field2", "a c")
.get();
.get().decRef();
client().prepareIndex(indexName)
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
.setSource("field1", "b c")
.get();
.get().decRef();
}

/**
Expand Down
Loading

0 comments on commit 35e05b9

Please sign in to comment.