Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2950 Improve performance of UpdateHostDb - be lazy creating HostDatum metaData objects: - do not create MapWritable object unless needed - use clear() instead of constructing new object when reading metadata from sequence file - use statically serialization of empty metaData MapWritable as empty HostDatum metadata is the most common case (stay back-ward compatible by keeping metadata mandatory)

Sebastian Nagel5a6ac3bb1ccb
diff --git a/src/java/org/apache/nutch/crawl/Generator.java b/src/java/org/apache/nutch/crawl/Generator.java
index 7eee5e7c8..15fedbf54 100644
--- a/src/java/org/apache/nutch/crawl/Generator.java
+++ b/src/java/org/apache/nutch/crawl/Generator.java
@@ -347,26 +347,28 @@ public class Generator extends NutchTool implements Tool {
context.set("conf", conf);
// Set metadata variables
- for (Map.Entry<Writable, Writable> entry : datum.getMetaData()
- .entrySet()) {
- Object value = entry.getValue();
-
- if (value instanceof FloatWritable) {
- FloatWritable fvalue = (FloatWritable) value;
- Text tkey = (Text) entry.getKey();
- context.set(tkey.toString(), fvalue.get());
- }
+ if (datum.hasMetaData()) {
+ for (Map.Entry<Writable, Writable> entry : datum.getMetaData()
+ .entrySet()) {
+ Object value = entry.getValue();
+
+ if (value instanceof FloatWritable) {
+ FloatWritable fvalue = (FloatWritable) value;
+ Text tkey = (Text) entry.getKey();
+ context.set(tkey.toString(), fvalue.get());
+ }
- if (value instanceof IntWritable) {
- IntWritable ivalue = (IntWritable) value;
- Text tkey = (Text) entry.getKey();
- context.set(tkey.toString(), ivalue.get());
- }
+ if (value instanceof IntWritable) {
+ IntWritable ivalue = (IntWritable) value;
+ Text tkey = (Text) entry.getKey();
+ context.set(tkey.toString(), ivalue.get());
+ }
- if (value instanceof Text) {
- Text tvalue = (Text) value;
- Text tkey = (Text) entry.getKey();
- context.set(tkey.toString().replace("-", "_"), tvalue.toString());
+ if (value instanceof Text) {
+ Text tvalue = (Text) value;
+ Text tkey = (Text) entry.getKey();
+ context.set(tkey.toString().replace("-", "_"), tvalue.toString());
+ }
}
}
diff --git a/src/java/org/apache/nutch/hostdb/HostDatum.java b/src/java/org/apache/nutch/hostdb/HostDatum.java
index 3698be047..fb9e78bda 100644
--- a/src/java/org/apache/nutch/hostdb/HostDatum.java
+++ b/src/java/org/apache/nutch/hostdb/HostDatum.java
@@ -16,8 +16,10 @@
*/
package org.apache.nutch.hostdb;
+import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Map.Entry;
@@ -35,12 +37,23 @@ public class HostDatum implements Writable, Cloneable {
protected Date lastCheck = new Date(0);
protected String homepageUrl = new String();
- protected MapWritable metaData = new MapWritable();
+ protected MapWritable metaData = null;
+ protected static final byte[] emptyMetaDataWritableSerialized;
+ static {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ DataOutputStream outStream = new DataOutputStream(buffer);
+ MapWritable emptyMetaData = new MapWritable();
+ try {
+ emptyMetaData.write(outStream);
+ outStream.close();
+ } catch (IOException e) {}
+ emptyMetaDataWritableSerialized = buffer.toByteArray();
+ }
// Records the number of times DNS look-up failed, may indicate host no longer exists
protected long dnsFailures = 0;
- // Records the number of connection failures, may indicate our netwerk being blocked by firewall
+ // Records the number of connection failures, may indicate our network being blocked by firewall
protected long connectionFailures = 0;
protected long unfetched = 0;
@@ -129,7 +142,7 @@ public class HostDatum implements Writable, Cloneable {
}
public boolean hasHomepageUrl() {
- return homepageUrl.length() > 0;
+ return homepageUrl.isEmpty();
}
public String getHomepageUrl() {
@@ -197,18 +210,20 @@ public class HostDatum implements Writable, Cloneable {
setNotModified(0);
}
- public void setMetaData(org.apache.hadoop.io.MapWritable mapWritable) {
- this.metaData = new org.apache.hadoop.io.MapWritable(mapWritable);
+ public void setMetaData(MapWritable mapWritable) {
+ this.metaData = new MapWritable(mapWritable);
}
/**
- * Add all metadata from other CrawlDatum to this CrawlDatum.
+ * Add all metadata from other HostDatum to this HostDatum.
*
* @param other HostDatum
*/
public void putAllMetaData(HostDatum other) {
- for (Entry<Writable, Writable> e : other.getMetaData().entrySet()) {
- getMetaData().put(e.getKey(), e.getValue());
+ if (other.hasMetaData()) {
+ for (Entry<Writable, Writable> e : other.getMetaData().entrySet()) {
+ getMetaData().put(e.getKey(), e.getValue());
+ }
}
}
@@ -217,11 +232,17 @@ public class HostDatum implements Writable, Cloneable {
* @return a {@link MapWritable} if it was set or read in {@link #readFields(DataInput)},
* OR returns empty map in case {@link HostDatum} was freshly created (lazily instantiated).
*/
- public org.apache.hadoop.io.MapWritable getMetaData() {
- if (this.metaData == null) this.metaData = new org.apache.hadoop.io.MapWritable();
+ public MapWritable getMetaData() {
+ if (this.metaData == null)
+ this.metaData = new MapWritable();
return this.metaData;
}
+ /** @return true if host has (non-empty) metadata */
+ public boolean hasMetaData() {
+ return this.metaData != null && !this.metaData.isEmpty();
+ }
+
@Override
public Object clone() throws CloneNotSupportedException {
HostDatum result = (HostDatum)super.clone();
@@ -260,7 +281,11 @@ public class HostDatum implements Writable, Cloneable {
redirPerm = in.readLong();
gone = in.readLong();
- metaData = new org.apache.hadoop.io.MapWritable();
+ if (metaData == null) {
+ metaData = new MapWritable();
+ } else {
+ metaData.clear();
+ }
metaData.readFields(in);
}
@@ -280,7 +305,11 @@ public class HostDatum implements Writable, Cloneable {
out.writeLong(redirPerm);
out.writeLong(gone);
- metaData.write(out);
+ if (hasMetaData()) {
+ metaData.write(out);
+ } else {
+ out.write(emptyMetaDataWritableSerialized);
+ }
}
@Override
@@ -312,12 +341,15 @@ public class HostDatum implements Writable, Cloneable {
buf.append("\t");
buf.append(homepageUrl);
buf.append("\t");
- for (Entry<Writable, Writable> e : getMetaData().entrySet()) {
- buf.append(e.getKey().toString());
- buf.append(':');
- buf.append(e.getValue().toString());
- buf.append("|||");
+ if (hasMetaData()) {
+ for (Entry<Writable, Writable> e : getMetaData().entrySet()) {
+ buf.append(e.getKey().toString());
+ buf.append(':');
+ buf.append(e.getValue().toString());
+ buf.append("|||");
+ }
}
return buf.toString();
}
+
}
diff --git a/src/java/org/apache/nutch/hostdb/ReadHostDb.java b/src/java/org/apache/nutch/hostdb/ReadHostDb.java
index 930e62cd2..ffddb1889 100644
--- a/src/java/org/apache/nutch/hostdb/ReadHostDb.java
+++ b/src/java/org/apache/nutch/hostdb/ReadHostDb.java
@@ -111,19 +111,22 @@ public class ReadHostDb extends Configured implements Tool {
jcontext.set("connectionFailures", datum.getConnectionFailures());
// Set metadata variables
- for (Map.Entry<Writable, Writable> entry : datum.getMetaData().entrySet()) {
- Object value = entry.getValue();
-
- if (value instanceof FloatWritable) {
- FloatWritable fvalue = (FloatWritable)value;
- Text tkey = (Text)entry.getKey();
- jcontext.set(tkey.toString(), fvalue.get());
- }
-
- if (value instanceof IntWritable) {
- IntWritable ivalue = (IntWritable)value;
- Text tkey = (Text)entry.getKey();
- jcontext.set(tkey.toString(), ivalue.get());
+ if (datum.hasMetaData()) {
+ for (Map.Entry<Writable, Writable> entry : datum.getMetaData()
+ .entrySet()) {
+ Object value = entry.getValue();
+
+ if (value instanceof FloatWritable) {
+ FloatWritable fvalue = (FloatWritable) value;
+ Text tkey = (Text) entry.getKey();
+ jcontext.set(tkey.toString(), fvalue.get());
+ }
+
+ if (value instanceof IntWritable) {
+ IntWritable ivalue = (IntWritable) value;
+ Text tkey = (Text) entry.getKey();
+ jcontext.set(tkey.toString(), ivalue.get());
+ }
}
}
diff --git a/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java b/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java
index d9d41ea45..d7156edb4 100644
--- a/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java
+++ b/src/java/org/apache/nutch/hostdb/UpdateHostDbReducer.java
@@ -324,7 +324,7 @@ public class UpdateHostDbReducer
}
// Check metadata
- if (!buffer.getMetaData().isEmpty()) {
+ if (buffer.hasMetaData()) {
hostDatum.setMetaData(buffer.getMetaData());
}