Skip to content

Commit

Permalink
WIP [HTM-1076][HTM-1077] TMFeatureType event handler to propagate cha…
Browse files Browse the repository at this point in the history
…nges to solr indexes

fix PMD style

fixup after API changes in #876

move testcases and remove now useless test, use solr service inject

fix rebase issues
  • Loading branch information
mprins committed Sep 11, 2024
1 parent b39aa26 commit 4436ac2
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.apache.solr.client.solrj.impl.ConcurrentUpdateHttp2SolrClient;
import org.apache.solr.client.solrj.impl.Http2SolrClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -77,6 +74,7 @@
import org.tailormap.api.repository.UserRepository;
import org.tailormap.api.security.InternalAdminAuthentication;
import org.tailormap.api.solr.SolrHelper;
import org.tailormap.api.solr.SolrService;
import org.tailormap.api.viewer.model.AppStyling;
import org.tailormap.api.viewer.model.Component;
import org.tailormap.api.viewer.model.ComponentConfig;
Expand Down Expand Up @@ -107,16 +105,13 @@ public class PopulateTestData {
@Value("${MAP5_URL:#{null}}")
private String map5url;

@Value("${tailormap-api.solr-core-name:tailormap}")
private String solrCoreName;

private final ApplicationContext appContext;
private final UserRepository userRepository;
private final GroupRepository groupRepository;
private final CatalogRepository catalogRepository;
private final GeoServiceRepository geoServiceRepository;
private final GeoServiceHelper geoServiceHelper;

private final SolrService solrService;
private final FeatureSourceRepository featureSourceRepository;
private final ApplicationRepository applicationRepository;
private final ConfigurationRepository configurationRepository;
Expand All @@ -136,6 +131,7 @@ public PopulateTestData(
ConfigurationRepository configurationRepository,
FeatureSourceFactoryHelper featureSourceFactoryHelper,
SearchIndexRepository searchIndexRepository,
SolrService solrService,
UploadRepository uploadRepository) {
this.appContext = appContext;
this.userRepository = userRepository;
Expand All @@ -148,6 +144,7 @@ public PopulateTestData(
this.configurationRepository = configurationRepository;
this.featureSourceFactoryHelper = featureSourceFactoryHelper;
this.searchIndexRepository = searchIndexRepository;
this.solrService = solrService;

Check warning on line 147 in src/main/java/org/tailormap/api/configuration/dev/PopulateTestData.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/configuration/dev/PopulateTestData.java#L147

Added line #L147 was not covered by tests
this.uploadRepository = uploadRepository;
}

Expand All @@ -169,7 +166,7 @@ public void populate() throws Exception {
InternalAdminAuthentication.clearSecurityContextAuthentication();
}
if (exit) {
// Exit after transaction is completed - for 'mvn verify' to populate testdata before
// Exit after transaction is completed for 'mvn verify' to populate testdata before
// integration tests
new Thread(
() -> {
Expand Down Expand Up @@ -1344,23 +1341,9 @@ public void createSolrIndex() throws Exception {
logger.info("Creating Solr index");
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
final String solrUrl =
"http://"
+ (connectToSpatialDbsAtLocalhost ? "127.0.0.1" : "solr")
+ ":8983/solr/"
+ solrCoreName;
SolrHelper solrHelper =
new SolrHelper(
new ConcurrentUpdateHttp2SolrClient.Builder(
solrUrl,
new Http2SolrClient.Builder()
.useHttp1_1(true)
.withFollowRedirects(true)
.withConnectionTimeout(10000, TimeUnit.MILLISECONDS)
.withRequestTimeout(60000, TimeUnit.MILLISECONDS)
.build())
.withQueueSize(SolrHelper.SOLR_BATCH_SIZE * 2)
.withThreadCount(10)
.build());
"http://" + (connectToSpatialDbsAtLocalhost ? "127.0.0.1" : "solr") + ":8983/solr/";
this.solrService.setSolrUrl(solrUrl);
SolrHelper solrHelper = new SolrHelper(this.solrService.getSolrClientForIndexing());

Check warning on line 1346 in src/main/java/org/tailormap/api/configuration/dev/PopulateTestData.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/configuration/dev/PopulateTestData.java#L1345-L1346

Added lines #L1345 - L1346 were not covered by tests

GeoService geoService = geoServiceRepository.findById("snapshot-geoserver").orElseThrow();
Application defaultApp = applicationRepository.findByName("default");
Expand Down
23 changes: 6 additions & 17 deletions src/main/java/org/tailormap/api/controller/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import java.io.IOException;
import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.util.concurrent.TimeUnit;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.Http2SolrClient;
import org.apache.solr.common.SolrException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,6 +35,7 @@
import org.tailormap.api.persistence.json.AppTreeLayerNode;
import org.tailormap.api.repository.SearchIndexRepository;
import org.tailormap.api.solr.SolrHelper;
import org.tailormap.api.solr.SolrService;
import org.tailormap.api.viewer.model.SearchResponse;

@AppRestController
Expand All @@ -50,17 +49,14 @@ public class SearchController {

private final SearchIndexRepository searchIndexRepository;

@Value("${tailormap-api.solr-url}")
private String solrUrl;

@Value("${tailormap-api.solr-core-name:tailormap}")
private String solrCoreName;

@Value("${tailormap-api.pageSize:100}")
private int numResultsToReturn;

public SearchController(SearchIndexRepository searchIndexRepository) {
private final SolrService solrService;

public SearchController(SearchIndexRepository searchIndexRepository, SolrService solrService) {

Check warning on line 57 in src/main/java/org/tailormap/api/controller/SearchController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/SearchController.java#L57

Added line #L57 was not covered by tests
this.searchIndexRepository = searchIndexRepository;
this.solrService = solrService;

Check warning on line 59 in src/main/java/org/tailormap/api/controller/SearchController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/SearchController.java#L59

Added line #L59 was not covered by tests
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -92,7 +88,7 @@ public ResponseEntity<Serializable> search(
"Layer '%s' does not have a search index"
.formatted(appTreeLayerNode.getLayerName())));

try (SolrClient solrClient = getSolrClient();
try (SolrClient solrClient = solrService.getSolrClientForSearching();

Check warning on line 91 in src/main/java/org/tailormap/api/controller/SearchController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/SearchController.java#L91

Added line #L91 was not covered by tests
SolrHelper solrHelper = new SolrHelper(solrClient)) {
final SearchResponse searchResponse =
solrHelper.findInIndex(searchIndex, solrQuery, start, numResultsToReturn);
Expand All @@ -109,11 +105,4 @@ public ResponseEntity<Serializable> search(
HttpStatus.BAD_REQUEST, "Error while searching with given query", e);
}
}

private SolrClient getSolrClient() {
return new Http2SolrClient.Builder(solrUrl + solrCoreName)
.withConnectionTimeout(10, TimeUnit.SECONDS)
.withFollowRedirects(true)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.ConcurrentUpdateHttp2SolrClient;
import org.apache.solr.client.solrj.impl.Http2SolrClient;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.common.SolrException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -41,37 +37,34 @@
import org.tailormap.api.repository.FeatureTypeRepository;
import org.tailormap.api.repository.SearchIndexRepository;
import org.tailormap.api.solr.SolrHelper;
import org.tailormap.api.solr.SolrService;
import org.tailormap.api.viewer.model.ErrorResponse;

/** Admin controller for Solr. */
@RestController
public class SolrAdminController {
@Value("${tailormap-api.solr-url}")
private String solrUrl;

@Value("${tailormap-api.solr-core-name:tailormap}")
private String solrCoreName;

private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final FeatureSourceFactoryHelper featureSourceFactoryHelper;

private final FeatureTypeRepository featureTypeRepository;
private final SearchIndexRepository searchIndexRepository;
private final SolrService solrService;

public SolrAdminController(
FeatureSourceFactoryHelper featureSourceFactoryHelper,
FeatureTypeRepository featureTypeRepository,
SearchIndexRepository searchIndexRepository) {
SearchIndexRepository searchIndexRepository,
SolrService solrService) {

Check warning on line 57 in src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java#L57

Added line #L57 was not covered by tests
this.featureSourceFactoryHelper = featureSourceFactoryHelper;
this.featureTypeRepository = featureTypeRepository;
this.searchIndexRepository = searchIndexRepository;
this.solrService = solrService;

Check warning on line 61 in src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java#L61

Added line #L61 was not covered by tests
}

/**
* Ping solr.
*
* @return the response entity (ok or an error response)
* @return the response entity (OK or an error response)
*/
@Operation(summary = "Ping Solr", description = "Ping Solr to check if it is available")
@ApiResponse(
Expand All @@ -92,7 +85,7 @@ public SolrAdminController(
path = "${tailormap-api.admin.base-path}/index/ping",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> pingSolr() {
try (SolrClient solrClient = getSolrClient()) {
try (SolrClient solrClient = solrService.getSolrClientForIndexing()) {

Check warning on line 88 in src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java#L88

Added line #L88 was not covered by tests
final SolrPingResponse ping = solrClient.ping();
logger.info("Solr ping status {}", ping.getResponse().get("status"));
return ResponseEntity.ok(
Expand All @@ -111,24 +104,6 @@ public ResponseEntity<?> pingSolr() {
}
}

/**
* Get a concurrent update Solr client for bulk operations.
*
* @return the Solr client
*/
private SolrClient getSolrClient() {
return new ConcurrentUpdateHttp2SolrClient.Builder(
solrUrl + solrCoreName,
new Http2SolrClient.Builder()
.withFollowRedirects(true)
.withConnectionTimeout(10000, TimeUnit.MILLISECONDS)
.withRequestTimeout(60000, TimeUnit.MILLISECONDS)
.build())
.withQueueSize(SolrHelper.SOLR_BATCH_SIZE * 2)
.withThreadCount(10)
.build();
}

/**
* (re-) Index a layer.
*
Expand Down Expand Up @@ -195,7 +170,7 @@ public ResponseEntity<?> index(@PathVariable Long searchIndexId) {
boolean createNewIndex =
(null == searchIndex.getLastIndexed()
|| searchIndex.getStatus() == SearchIndex.Status.INITIAL);
try (SolrClient solrClient = getSolrClient();
try (SolrClient solrClient = solrService.getSolrClientForIndexing();

Check warning on line 173 in src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java#L173

Added line #L173 was not covered by tests
SolrHelper solrHelper = new SolrHelper(solrClient)) {
solrHelper.addFeatureTypeIndex(searchIndex, indexingFT, featureSourceFactoryHelper);
searchIndexRepository.save(searchIndex);
Expand Down Expand Up @@ -244,7 +219,7 @@ public ResponseEntity<?> index(@PathVariable Long searchIndexId) {
produces = MediaType.APPLICATION_JSON_VALUE)
@Transactional
public ResponseEntity<?> clearIndex(@PathVariable Long searchIndexId) {
try (SolrClient solrClient = getSolrClient();
try (SolrClient solrClient = solrService.getSolrClientForIndexing();

Check warning on line 222 in src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/tailormap/api/controller/admin/SolrAdminController.java#L222

Added line #L222 was not covered by tests
SolrHelper solrHelper = new SolrHelper(solrClient)) {
solrHelper.clearIndexForLayer(searchIndexId);
// do not delete the SearchIndex metadata object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
*/
package org.tailormap.api.repository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.lang.NonNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.tailormap.api.persistence.Application;
Expand All @@ -21,4 +24,17 @@ public interface ApplicationRepository extends JpaRepository<Application, Long>
@Override
@NonNull
Optional<Application> findById(@NonNull Long aLong);

/**
* Find all applications that have a layer that is linked to a specific (Solr) index.
*
* @param indexId The index id to search for
*/
@NonNull
@PreAuthorize("permitAll()")
@Query(
value =
"select * from application app, lateral jsonb_path_query(app.settings, ('$.layerSettings.**{1}.searchIndexId ? (@ == '||:indexId||')')::jsonpath)",
nativeQuery = true)
List<Application> findByIndexId(@Param("indexId") @NonNull Long indexId);
}
Loading

0 comments on commit 4436ac2

Please sign in to comment.