|
@@ -11,10 +11,14 @@ package org.elasticsearch.ingest.geoip;
|
|
|
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
|
|
import org.elasticsearch.ElasticsearchParseException;
|
|
|
import org.elasticsearch.common.Randomness;
|
|
|
+import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.index.VersionType;
|
|
|
import org.elasticsearch.ingest.IngestDocument;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.test.StreamsUtils;
|
|
|
+import org.elasticsearch.threadpool.TestThreadPool;
|
|
|
+import org.elasticsearch.threadpool.ThreadPool;
|
|
|
+import org.elasticsearch.watcher.ResourceWatcherService;
|
|
|
import org.junit.AfterClass;
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
@@ -31,14 +35,12 @@ import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
-import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
-import static org.hamcrest.Matchers.hasToString;
|
|
|
import static org.hamcrest.Matchers.sameInstance;
|
|
|
|
|
|
public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
|
|
|
- private static Map<String, DatabaseReaderLazyLoader> databaseReaders;
|
|
|
+ private static LocalDatabases localDatabases;
|
|
|
|
|
|
@BeforeClass
|
|
|
public static void loadDatabaseReaders() throws IOException {
|
|
@@ -47,20 +49,17 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
final Path geoIpConfigDir = configDir.resolve("ingest-geoip");
|
|
|
Files.createDirectories(geoIpConfigDir);
|
|
|
copyDatabaseFiles(geoIpDir);
|
|
|
-
|
|
|
- databaseReaders = IngestGeoIpPlugin.loadDatabaseReaders(new GeoIpCache(1000), geoIpDir, geoIpConfigDir);
|
|
|
+ localDatabases = new LocalDatabases(geoIpDir, geoIpConfigDir, new GeoIpCache(1000));
|
|
|
}
|
|
|
|
|
|
@AfterClass
|
|
|
public static void closeDatabaseReaders() throws IOException {
|
|
|
- for (DatabaseReaderLazyLoader reader : databaseReaders.values()) {
|
|
|
- reader.close();
|
|
|
- }
|
|
|
- databaseReaders = null;
|
|
|
+ localDatabases.close();
|
|
|
+ localDatabases = null;
|
|
|
}
|
|
|
|
|
|
public void testBuildDefaults() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
@@ -76,7 +75,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testSetIgnoreMissing() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
@@ -93,7 +92,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testCountryBuildDefaults() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
@@ -111,7 +110,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testAsnBuildDefaults() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
@@ -129,7 +128,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testBuildTargetField() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
|
config.put("target_field", "_field");
|
|
@@ -140,7 +139,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testBuildDbFile() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
|
config.put("database_file", "GeoLite2-Country.mmdb");
|
|
@@ -153,7 +152,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testBuildWithCountryDbAndAsnFields() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
|
config.put("database_file", "GeoLite2-Country.mmdb");
|
|
@@ -167,7 +166,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testBuildWithAsnDbAndCityFields() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
|
config.put("database_file", "GeoLite2-ASN.mmdb");
|
|
@@ -181,7 +180,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testBuildNonExistingDbFile() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
|
|
|
Map<String, Object> config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
@@ -191,7 +190,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testBuildFields() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
|
|
|
Set<GeoIpProcessor.Property> properties = EnumSet.noneOf(GeoIpProcessor.Property.class);
|
|
|
List<String> fieldNames = new ArrayList<>();
|
|
@@ -215,7 +214,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testBuildIllegalFieldOption() throws Exception {
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
|
|
|
Map<String, Object> config1 = new HashMap<>();
|
|
|
config1.put("field", "_field");
|
|
@@ -241,10 +240,9 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
// Loading another database reader instances, because otherwise we can't test lazy loading as the
|
|
|
// database readers used at class level are reused between tests. (we want to keep that otherwise running this
|
|
|
// test will take roughly 4 times more time)
|
|
|
- Map<String, DatabaseReaderLazyLoader> databaseReaders =
|
|
|
- IngestGeoIpPlugin.loadDatabaseReaders(new GeoIpCache(1000), geoIpDir, geoIpConfigDir);
|
|
|
- GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
- for (DatabaseReaderLazyLoader lazyLoader : databaseReaders.values()) {
|
|
|
+ LocalDatabases localDatabases = new LocalDatabases(geoIpDir, geoIpConfigDir, new GeoIpCache(1000));
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
+ for (DatabaseReaderLazyLoader lazyLoader : localDatabases.getAllDatabases()) {
|
|
|
assertNull(lazyLoader.databaseReader.get());
|
|
|
}
|
|
|
|
|
@@ -257,10 +255,10 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
final GeoIpProcessor city = factory.create(null, "_tag", null, config);
|
|
|
|
|
|
// these are lazy loaded until first use so we expect null here
|
|
|
- assertNull(databaseReaders.get("GeoLite2-City.mmdb").databaseReader.get());
|
|
|
+ assertNull(localDatabases.getDatabase("GeoLite2-City.mmdb").databaseReader.get());
|
|
|
city.execute(document);
|
|
|
// the first ingest should trigger a database load
|
|
|
- assertNotNull(databaseReaders.get("GeoLite2-City.mmdb").databaseReader.get());
|
|
|
+ assertNotNull(localDatabases.getDatabase("GeoLite2-City.mmdb").databaseReader.get());
|
|
|
|
|
|
config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
@@ -268,10 +266,10 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
final GeoIpProcessor country = factory.create(null, "_tag", null, config);
|
|
|
|
|
|
// these are lazy loaded until first use so we expect null here
|
|
|
- assertNull(databaseReaders.get("GeoLite2-Country.mmdb").databaseReader.get());
|
|
|
+ assertNull(localDatabases.getDatabase("GeoLite2-Country.mmdb").databaseReader.get());
|
|
|
country.execute(document);
|
|
|
// the first ingest should trigger a database load
|
|
|
- assertNotNull(databaseReaders.get("GeoLite2-Country.mmdb").databaseReader.get());
|
|
|
+ assertNotNull(localDatabases.getDatabase("GeoLite2-Country.mmdb").databaseReader.get());
|
|
|
|
|
|
config = new HashMap<>();
|
|
|
config.put("field", "_field");
|
|
@@ -279,10 +277,10 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
final GeoIpProcessor asn = factory.create(null, "_tag", null, config);
|
|
|
|
|
|
// these are lazy loaded until first use so we expect null here
|
|
|
- assertNull(databaseReaders.get("GeoLite2-ASN.mmdb").databaseReader.get());
|
|
|
+ assertNull(localDatabases.getDatabase("GeoLite2-ASN.mmdb").databaseReader.get());
|
|
|
asn.execute(document);
|
|
|
// the first ingest should trigger a database load
|
|
|
- assertNotNull(databaseReaders.get("GeoLite2-ASN.mmdb").databaseReader.get());
|
|
|
+ assertNotNull(localDatabases.getDatabase("GeoLite2-ASN.mmdb").databaseReader.get());
|
|
|
}
|
|
|
|
|
|
public void testLoadingCustomDatabase() throws IOException {
|
|
@@ -299,10 +297,12 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
* Loading another database reader instances, because otherwise we can't test lazy loading as the database readers used at class
|
|
|
* level are reused between tests. (we want to keep that otherwise running this test will take roughly 4 times more time).
|
|
|
*/
|
|
|
- final Map<String, DatabaseReaderLazyLoader> databaseReaders =
|
|
|
- IngestGeoIpPlugin.loadDatabaseReaders(new GeoIpCache(1000), geoIpDir, geoIpConfigDir);
|
|
|
- final GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders);
|
|
|
- for (DatabaseReaderLazyLoader lazyLoader : databaseReaders.values()) {
|
|
|
+ ThreadPool threadPool = new TestThreadPool("test");
|
|
|
+ ResourceWatcherService resourceWatcherService = new ResourceWatcherService(Settings.EMPTY, threadPool);
|
|
|
+ LocalDatabases localDatabases = new LocalDatabases(geoIpDir, geoIpConfigDir, new GeoIpCache(1000));
|
|
|
+ localDatabases.initialize(resourceWatcherService);
|
|
|
+ GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(localDatabases);
|
|
|
+ for (DatabaseReaderLazyLoader lazyLoader : localDatabases.getAllDatabases()) {
|
|
|
assertNull(lazyLoader.databaseReader.get());
|
|
|
}
|
|
|
|
|
@@ -315,38 +315,12 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
final GeoIpProcessor city = factory.create(null, "_tag", null, config);
|
|
|
|
|
|
// these are lazy loaded until first use so we expect null here
|
|
|
- assertNull(databaseReaders.get("GeoIP2-City.mmdb").databaseReader.get());
|
|
|
+ assertNull(localDatabases.getDatabase("GeoIP2-City.mmdb").databaseReader.get());
|
|
|
city.execute(document);
|
|
|
// the first ingest should trigger a database load
|
|
|
- assertNotNull(databaseReaders.get("GeoIP2-City.mmdb").databaseReader.get());
|
|
|
- }
|
|
|
-
|
|
|
- public void testDatabaseNotExistsInDir() throws IOException {
|
|
|
- final Path geoIpDir = createTempDir();
|
|
|
- final Path configDir = createTempDir();
|
|
|
- final Path geoIpConfigDir = configDir.resolve("ingest-geoip");
|
|
|
- if (randomBoolean()) {
|
|
|
- Files.createDirectories(geoIpConfigDir);
|
|
|
- }
|
|
|
- copyDatabaseFiles(geoIpDir);
|
|
|
- final String databaseFilename = randomFrom(IngestGeoIpPlugin.DEFAULT_DATABASE_FILENAMES);
|
|
|
- Files.delete(geoIpDir.resolve(databaseFilename));
|
|
|
- final IOException e = expectThrows(IOException.class,
|
|
|
- () -> IngestGeoIpPlugin.loadDatabaseReaders(new GeoIpCache(1000), geoIpDir, geoIpConfigDir));
|
|
|
- assertThat(e, hasToString(containsString("expected database [" + databaseFilename + "] to exist in [" + geoIpDir + "]")));
|
|
|
- }
|
|
|
-
|
|
|
- public void testDatabaseExistsInConfigDir() throws IOException {
|
|
|
- final Path geoIpDir = createTempDir();
|
|
|
- final Path configDir = createTempDir();
|
|
|
- final Path geoIpConfigDir = configDir.resolve("ingest-geoip");
|
|
|
- Files.createDirectories(geoIpConfigDir);
|
|
|
- copyDatabaseFiles(geoIpDir);
|
|
|
- final String databaseFilename = randomFrom(IngestGeoIpPlugin.DEFAULT_DATABASE_FILENAMES);
|
|
|
- copyDatabaseFile(geoIpConfigDir, databaseFilename);
|
|
|
- final IOException e = expectThrows(IOException.class,
|
|
|
- () -> IngestGeoIpPlugin.loadDatabaseReaders(new GeoIpCache(1000), geoIpDir, geoIpConfigDir));
|
|
|
- assertThat(e, hasToString(containsString("expected database [" + databaseFilename + "] to not exist in [" + geoIpConfigDir + "]")));
|
|
|
+ assertNotNull(localDatabases.getDatabase("GeoIP2-City.mmdb").databaseReader.get());
|
|
|
+ resourceWatcherService.close();
|
|
|
+ threadPool.shutdown();
|
|
|
}
|
|
|
|
|
|
private static void copyDatabaseFile(final Path path, final String databaseFilename) throws IOException {
|
|
@@ -355,7 +329,7 @@ public class GeoIpProcessorFactoryTests extends ESTestCase {
|
|
|
path.resolve(databaseFilename));
|
|
|
}
|
|
|
|
|
|
- private static void copyDatabaseFiles(final Path path) throws IOException {
|
|
|
+ static void copyDatabaseFiles(final Path path) throws IOException {
|
|
|
for (final String databaseFilename : IngestGeoIpPlugin.DEFAULT_DATABASE_FILENAMES) {
|
|
|
copyDatabaseFile(path, databaseFilename);
|
|
|
}
|