Apache Nutch is an extensible and scalable web crawler
Fixes for NUTCH-1480: Changes: - Internal cache. - Fixed the unit test of the Elastic index writer.
r0ann3l041927ae943d
diff --git a/src/java/org/apache/nutch/indexer/CleaningJob.java b/src/java/org/apache/nutch/indexer/CleaningJob.javaindex fd5dff913..e8dab4031 100644--- a/src/java/org/apache/nutch/indexer/CleaningJob.java+++ b/src/java/org/apache/nutch/indexer/CleaningJob.java@@ -93,7 +93,7 @@ public class CleaningJob implements Tool {public void setup(Reducer<ByteWritable, Text, Text, ByteWritable>.Context context) {Configuration conf = context.getConfiguration();- writers = new IndexWriters(conf);+ writers = IndexWriters.get(conf);try {writers.open(conf, "Deletion");} catch (IOException e) {@@ -186,7 +186,7 @@ public class CleaningJob implements Tool {String usage = "Usage: CleaningJob <crawldb> [-noCommit]";LOG.error("Missing crawldb. " + usage);System.err.println(usage);- IndexWriters writers = new IndexWriters(getConf());+ IndexWriters writers = IndexWriters.get(getConf());System.err.println(writers.describe());return 1;}diff --git a/src/java/org/apache/nutch/indexer/IndexWriterParams.java b/src/java/org/apache/nutch/indexer/IndexWriterParams.javaindex b6c25c870..9c9ee72ef 100644--- a/src/java/org/apache/nutch/indexer/IndexWriterParams.java+++ b/src/java/org/apache/nutch/indexer/IndexWriterParams.java@@ -16,7 +16,7 @@ public class IndexWriterParams extends HashMap<String, String> {* @param m the map whose mappings are to be placed in this map* @throws NullPointerException if the specified map is null*/- IndexWriterParams(Map<? extends String, ? extends String> m) {+ public IndexWriterParams(Map<? extends String, ? extends String> m) {super(m);}diff --git a/src/java/org/apache/nutch/indexer/IndexWriters.java b/src/java/org/apache/nutch/indexer/IndexWriters.javaindex 4841b5a55..085a01fa4 100644--- a/src/java/org/apache/nutch/indexer/IndexWriters.java+++ b/src/java/org/apache/nutch/indexer/IndexWriters.java@@ -17,13 +17,11 @@package org.apache.nutch.indexer;import org.apache.hadoop.conf.Configuration;-import org.apache.hadoop.mapred.JobConf;-import org.apache.nutch.indexer.NutchDocument;import org.apache.nutch.plugin.Extension;import org.apache.nutch.plugin.ExtensionPoint;import org.apache.nutch.plugin.PluginRepository;import org.apache.nutch.plugin.PluginRuntimeException;-import org.apache.nutch.util.ObjectCache;+import org.apache.nutch.util.NutchConfiguration;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.w3c.dom.Document;@@ -41,6 +39,7 @@ import java.lang.invoke.MethodHandles;import java.util.HashMap;import java.util.List;import java.util.Map;+import java.util.WeakHashMap;/*** Creates and caches {@link IndexWriter} implementing plugins.@@ -50,59 +49,56 @@ public class IndexWriters {private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- private HashMap<String, IndexWriterWrapper> indexWriters;+ private static final WeakHashMap<String, IndexWriters> CACHE = new WeakHashMap<>();- public IndexWriters(Configuration conf) {- ObjectCache objectCache = ObjectCache.get(conf);+ public static synchronized IndexWriters get(Configuration conf) {+ String uuid = NutchConfiguration.getUUID(conf);+ if (uuid == null) {+ uuid = "nonNutchConf@" + conf.hashCode(); // fallback+ }+ return CACHE.computeIfAbsent(uuid, k -> new IndexWriters(conf));+ }- synchronized (objectCache) {- this.indexWriters = (HashMap<String, IndexWriterWrapper>) objectCache- .getObject(IndexWriterWrapper.class.getName());+ private HashMap<String, IndexWriterWrapper> indexWriters;- //It's not cached yet- if (this.indexWriters == null) {- try {- ExtensionPoint point = PluginRepository.get(conf)- .getExtensionPoint(IndexWriter.X_POINT_ID);+ private IndexWriters(Configuration conf) {+ //It's not cached yet+ if (this.indexWriters == null) {+ try {+ ExtensionPoint point = PluginRepository.get(conf)+ .getExtensionPoint(IndexWriter.X_POINT_ID);- if (point == null) {- throw new RuntimeException(IndexWriter.X_POINT_ID + " not found.");- }+ if (point == null) {+ throw new RuntimeException(IndexWriter.X_POINT_ID + " not found.");+ }- Extension[] extensions = point.getExtensions();+ Extension[] extensions = point.getExtensions();- HashMap<String, Extension> extensionMap = new HashMap<>();- for (Extension extension : extensions) {- LOG.info("Index writer {} identified.", extension.getClazz());- extensionMap.putIfAbsent(extension.getClazz(), extension);- }+ HashMap<String, Extension> extensionMap = new HashMap<>();+ for (Extension extension : extensions) {+ LOG.info("Index writer {} identified.", extension.getClazz());+ extensionMap.putIfAbsent(extension.getClazz(), extension);+ }- IndexWriterConfig[] indexWriterConfigs = loadWritersConfiguration(- conf);- HashMap<String, IndexWriterWrapper> indexWriters = new HashMap<>();+ IndexWriterConfig[] indexWriterConfigs = loadWritersConfiguration(+ conf);+ this.indexWriters = new HashMap<>();- for (IndexWriterConfig indexWriterConfig : indexWriterConfigs) {- final String clazz = indexWriterConfig.getClazz();+ for (IndexWriterConfig indexWriterConfig : indexWriterConfigs) {+ final String clazz = indexWriterConfig.getClazz();- //If was enabled in plugin.includes property- if (extensionMap.containsKey(clazz)) {- IndexWriterWrapper writerWrapper = new IndexWriterWrapper();- writerWrapper.setIndexWriterConfig(indexWriterConfig);- writerWrapper.setIndexWriter(- (IndexWriter) extensionMap.get(clazz).getExtensionInstance());+ //If was enabled in plugin.includes property+ if (extensionMap.containsKey(clazz)) {+ IndexWriterWrapper writerWrapper = new IndexWriterWrapper();+ writerWrapper.setIndexWriterConfig(indexWriterConfig);+ writerWrapper.setIndexWriter(+ (IndexWriter) extensionMap.get(clazz).getExtensionInstance());- indexWriters.put(indexWriterConfig.getId(), writerWrapper);- }+ indexWriters.put(indexWriterConfig.getId(), writerWrapper);}-- objectCache- .setObject(IndexWriterWrapper.class.getName(), indexWriters);- } catch (PluginRuntimeException e) {- throw new RuntimeException(e);}-- this.indexWriters = (HashMap<String, IndexWriterWrapper>) objectCache- .getObject(IndexWriterWrapper.class.getName());+ } catch (PluginRuntimeException e) {+ throw new RuntimeException(e);}}}diff --git a/src/java/org/apache/nutch/indexer/IndexerOutputFormat.java b/src/java/org/apache/nutch/indexer/IndexerOutputFormat.javaindex 7d0ed5ad1..3ce4f8061 100644--- a/src/java/org/apache/nutch/indexer/IndexerOutputFormat.java+++ b/src/java/org/apache/nutch/indexer/IndexerOutputFormat.java@@ -17,7 +17,6 @@package org.apache.nutch.indexer;import java.io.IOException;-import java.lang.invoke.MethodHandles;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;@@ -25,15 +24,15 @@ import org.apache.hadoop.mapreduce.RecordWriter;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.mapreduce.TaskAttemptContext;-public class IndexerOutputFormat extends- FileOutputFormat<Text, NutchIndexAction> {+public class IndexerOutputFormat+ extends FileOutputFormat<Text, NutchIndexAction> {@Overridepublic RecordWriter<Text, NutchIndexAction> getRecordWriter(TaskAttemptContext context) throws IOException {- Configuration conf = context.getConfiguration();- final IndexWriters writers = new IndexWriters(conf);+ Configuration conf = context.getConfiguration();+ final IndexWriters writers = IndexWriters.get(conf);String name = getUniqueFile(context, "part", "");writers.open(conf, name);@@ -41,6 +40,12 @@ public class IndexerOutputFormat extendsreturn new RecordWriter<Text, NutchIndexAction>() {public void close(TaskAttemptContext context) throws IOException {+ // do the commits once and for all the reducers in one go+ boolean noCommit = conf+ .getBoolean(IndexerMapReduce.INDEXER_NO_COMMIT, false);+ if (!noCommit) {+ writers.commit();+ }writers.close();}diff --git a/src/java/org/apache/nutch/indexer/IndexingFiltersChecker.java b/src/java/org/apache/nutch/indexer/IndexingFiltersChecker.javaindex 284d4adc7..afc6aaf3c 100644--- a/src/java/org/apache/nutch/indexer/IndexingFiltersChecker.java+++ b/src/java/org/apache/nutch/indexer/IndexingFiltersChecker.java@@ -269,7 +269,7 @@ public class IndexingFiltersChecker extends AbstractChecker {output.append("\n"); // For readability if keepClientCnxOpenif (getConf().getBoolean("doIndex", false) && doc != null) {- IndexWriters writers = new IndexWriters(getConf());+ IndexWriters writers = IndexWriters.get(getConf());writers.open(getConf(), "IndexingFilterChecker");writers.write(doc);writers.close();diff --git a/src/java/org/apache/nutch/indexer/IndexingJob.java b/src/java/org/apache/nutch/indexer/IndexingJob.javaindex a839955c0..ae77da4a0 100644--- a/src/java/org/apache/nutch/indexer/IndexingJob.java+++ b/src/java/org/apache/nutch/indexer/IndexingJob.java@@ -120,7 +120,7 @@ public class IndexingJob extends NutchTool implements Tool {LOG.info("Indexer: adding binary content");}}- IndexWriters writers = new IndexWriters(getConf());+ IndexWriters writers = IndexWriters.get(conf);LOG.info(writers.describe());IndexerMapReduce.initMRJob(crawlDb, linkDb, segments, job, addBinaryContent);@@ -132,6 +132,7 @@ public class IndexingJob extends NutchTool implements Tool {conf.setBoolean(IndexerMapReduce.URL_FILTERING, filter);conf.setBoolean(IndexerMapReduce.URL_NORMALIZING, normalize);conf.setBoolean(IndexerMapReduce.INDEXER_BINARY_AS_BASE64, base64);+ conf.setBoolean(IndexerMapReduce.INDEXER_NO_COMMIT, noCommit);if (params != null) {conf.set(IndexerMapReduce.INDEXER_PARAMS, params);@@ -157,11 +158,6 @@ public class IndexingJob extends NutchTool implements Tool {LOG.error(StringUtils.stringifyException(e));throw e;}- // do the commits once and for all the reducers in one go- if (!noCommit) {- writers.open(conf, "commit");- writers.commit();- }LOG.info("Indexer: number of documents indexed, deleted, or skipped:");for (Counter counter : job.getCounters().getGroup("IndexerStatus")) {LOG.info("Indexer: {} {}",@@ -181,7 +177,7 @@ public class IndexingJob extends NutchTool implements Tool {System.err//.println("Usage: Indexer <crawldb> [-linkdb <linkdb>] [-params k1=v1&k2=v2...] (<segment> ... | -dir <segments>) [-noCommit] [-deleteGone] [-filter] [-normalize]");.println("Usage: Indexer <crawldb> [-linkdb <linkdb>] [-params k1=v1&k2=v2...] (<segment> ... | -dir <segments>) [-noCommit] [-deleteGone] [-filter] [-normalize] [-addBinaryContent] [-base64]");- IndexWriters writers = new IndexWriters(getConf());+ IndexWriters writers = IndexWriters.get(getConf());System.err.println(writers.describe());return -1;}diff --git a/src/java/org/apache/nutch/util/ObjectCache.java b/src/java/org/apache/nutch/util/ObjectCache.javaindex 4e93d9d5f..4ed3fd0b9 100644--- a/src/java/org/apache/nutch/util/ObjectCache.java+++ b/src/java/org/apache/nutch/util/ObjectCache.java@@ -29,7 +29,7 @@ public class ObjectCache {private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());- private static final WeakHashMap<String, ObjectCache> CACHE = new WeakHashMap<>();+ private static final WeakHashMap<Configuration, ObjectCache> CACHE = new WeakHashMap<>();private final HashMap<String, Object> objectMap;@@ -38,12 +38,12 @@ public class ObjectCache {}public synchronized static ObjectCache get(Configuration conf) {- ObjectCache objectCache = CACHE.get(NutchConfiguration.getUUID(conf));+ ObjectCache objectCache = CACHE.get(conf);if (objectCache == null) {LOG.debug("No object cache found for conf=" + conf+ ", instantiating a new object cache");objectCache = new ObjectCache();- CACHE.put(NutchConfiguration.getUUID(conf), objectCache);+ CACHE.put(conf, objectCache);}return objectCache;}diff --git a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.javaindex 21db9701d..1540241b0 100644--- a/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java+++ b/src/plugin/indexer-elastic/src/java/org/apache/nutch/indexwriter/elastic/ElasticIndexWriter.java@@ -112,7 +112,7 @@ public class ElasticIndexWriter implements IndexWriter {.getInt(ElasticConstants.EXPONENTIAL_BACKOFF_RETRIES,DEFAULT_EXP_BACKOFF_RETRIES);- makeClient(parameters);+ client = makeClient(parameters);LOG.debug("Creating BulkProcessor with maxBulkDocs={}, maxBulkLength={}",maxBulkDocs, maxBulkLength);@@ -127,7 +127,7 @@ public class ElasticIndexWriter implements IndexWriter {/*** Generates a TransportClient or NodeClient*/- protected void makeClient(IndexWriterParams parameters) throws IOException {+ protected Client makeClient(IndexWriterParams parameters) throws IOException {String clusterName = parameters.get(ElasticConstants.CLUSTER);String[] hosts = parameters.getStrings(ElasticConstants.HOSTS);int port = parameters.getInt(ElasticConstants.PORT, DEFAULT_PORT);@@ -155,6 +155,8 @@ public class ElasticIndexWriter implements IndexWriter {Settings settings = settingsBuilder.build();+ Client client = null;+// Prefer TransportClientif (hosts != null && port > 1) {TransportClient transportClient = new PreBuiltTransportClient(settings);@@ -167,6 +169,8 @@ public class ElasticIndexWriter implements IndexWriter {node = new Node(settings);client = node.client();}++ return client;}/**diff --git a/src/plugin/indexer-elastic/src/test/org/apache/nutch/indexwriter/elastic/TestElasticIndexWriter.java b/src/plugin/indexer-elastic/src/test/org/apache/nutch/indexwriter/elastic/TestElasticIndexWriter.javaindex 58ac8b08e..dc59cd46f 100644--- a/src/plugin/indexer-elastic/src/test/org/apache/nutch/indexwriter/elastic/TestElasticIndexWriter.java+++ b/src/plugin/indexer-elastic/src/test/org/apache/nutch/indexwriter/elastic/TestElasticIndexWriter.java@@ -21,6 +21,7 @@ import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.mapreduce.Job;+import org.apache.nutch.indexer.IndexWriterParams;import org.apache.nutch.indexer.NutchDocument;import org.apache.nutch.util.NutchConfiguration;import org.elasticsearch.action.Action;@@ -43,6 +44,9 @@ import org.elasticsearch.threadpool.ThreadPool;import org.junit.Assert;import org.junit.Before;import org.junit.Test;++import java.util.HashMap;+import java.util.Map;import java.util.UUID;public class TestElasticIndexWriter {@@ -103,7 +107,7 @@ public class TestElasticIndexWriter {// customize the plugin to signal successful bulk operationstestIndexWriter = new ElasticIndexWriter() {@Override- protected Client makeClient(Configuration conf) {+ protected Client makeClient(IndexWriterParams parameters) {return client;}@@ -134,8 +138,12 @@ public class TestElasticIndexWriter {conf.setInt(ElasticConstants.MAX_BULK_DOCS, numDocs);Job job = Job.getInstance(conf);+ Map<String, String> parameters = new HashMap<>();+ parameters.put(ElasticConstants.CLUSTER, "nutch");+ parameters.put(ElasticConstants.MAX_BULK_DOCS, String.valueOf(numDocs));+testIndexWriter.setConf(conf);- testIndexWriter.open(conf, "name");+ testIndexWriter.open(new IndexWriterParams(parameters));NutchDocument doc = new NutchDocument();doc.add("id", "http://www.example.com");@@ -169,8 +177,12 @@ public class TestElasticIndexWriter {conf.setInt(ElasticConstants.MAX_BULK_LENGTH, testMaxBulkLength);Job job = Job.getInstance(conf);+ Map<String, String> parameters = new HashMap<>();+ parameters.put(ElasticConstants.CLUSTER, "nutch");+ parameters.put(ElasticConstants.MAX_BULK_LENGTH, String.valueOf(testMaxBulkLength));+testIndexWriter.setConf(conf);- testIndexWriter.open(conf, "name");+ testIndexWriter.open(new IndexWriterParams(parameters));NutchDocument doc = new NutchDocument();doc.add(key, value);@@ -197,8 +209,13 @@ public class TestElasticIndexWriter {Job job = Job.getInstance(conf);+ Map<String, String> parameters = new HashMap<>();+ parameters.put(ElasticConstants.CLUSTER, "nutch");+ parameters.put(ElasticConstants.EXPONENTIAL_BACKOFF_RETRIES, String.valueOf(maxNumFailures));+ parameters.put(ElasticConstants.MAX_BULK_DOCS, String.valueOf(numDocs));+testIndexWriter.setConf(conf);- testIndexWriter.open(conf, "name");+ testIndexWriter.open(new IndexWriterParams(parameters));NutchDocument doc = new NutchDocument();doc.add("id", "http://www.example.com");