Apache Nutch is an extensible and scalable web crawler
NUTCH-581 - DistributedSearch does not update search servers added to search-servers.txt on the fly. This allows search servers to be added and removed on the fly. Thanks Rohan.
Dennis Kubese55f9b64e51b
diff --git a/CHANGES.txt b/CHANGES.txtindex e6eb58628..e1a79979b 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -173,6 +173,9 @@ Unreleased changes (1.0-dev)index-basic plugin. For backwards compatibility, add index-anchor plugin tonutch-site.xml plugin.includes. (kubes)+60. NUTCH-581 - DistributedSearch does not update search servers added to+ search-servers.txt on the fly. (Rohan Mehta via kubes)+Release 0.9 - 2007-04-021. Changed log4j confiquration to log to stdout on commandlinediff --git a/src/java/org/apache/nutch/searcher/DistributedSearch.java b/src/java/org/apache/nutch/searcher/DistributedSearch.javaindex 0ed6a4e9f..0d69fc3d1 100644--- a/src/java/org/apache/nutch/searcher/DistributedSearch.java+++ b/src/java/org/apache/nutch/searcher/DistributedSearch.java@@ -17,24 +17,27 @@package org.apache.nutch.searcher;-import java.net.InetSocketAddress;-import java.io.*;-import java.util.*;+import java.io.BufferedReader;+import java.io.IOException;+import java.io.InputStreamReader;import java.lang.reflect.Method;+import java.net.InetSocketAddress;+import java.util.ArrayList;+import java.util.Comparator;+import java.util.HashMap;+import java.util.StringTokenizer;+import java.util.TreeSet;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;--import org.apache.nutch.parse.ParseData;-import org.apache.nutch.parse.ParseText;-import org.apache.nutch.crawl.Inlinks;-import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.fs.FileSystem;+import org.apache.hadoop.fs.Path;import org.apache.hadoop.ipc.RPC;import org.apache.hadoop.ipc.VersionedProtocol;-import org.apache.hadoop.fs.Path;-import org.apache.hadoop.fs.FileSystem;-+import org.apache.nutch.crawl.Inlinks;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.parse.ParseText;import org.apache.nutch.util.NutchConfiguration;/** Implements the search API over IPC connnections. */@@ -94,12 +97,19 @@ public class DistributedSearch {private boolean running = true;private Configuration conf;+ private Path file;+ private long timestamp;+ private FileSystem fs;+/** Construct a client talking to servers listed in the named file.* Each line in the file lists a server hostname and port, separated by* whitespace.*/- public Client(Path file, Configuration conf) throws IOException {+ public Client(Path file, Configuration conf)+ throws IOException {this(readConfig(file, conf), conf);+ this.file = file;+ this.timestamp = fs.getFileStatus(file).getModificationTime();}private static InetSocketAddress[] readConfig(Path path, Configuration conf)@@ -135,6 +145,7 @@ public class DistributedSearch {this.conf = conf;this.defaultAddresses = addresses;this.liveServer = new boolean[addresses.length];+ this.fs = FileSystem.get(conf);updateSegments();setDaemon(true);start();@@ -160,6 +171,24 @@ public class DistributedSearch {}}+ /**+ * Check to see if search-servers file has been modified+ *+ * @throws IOException+ */+ public boolean isFileModified()+ throws IOException {++ if (file != null) {+ long modTime = fs.getFileStatus(file).getModificationTime();+ if (timestamp < modTime) {+ this.timestamp = fs.getFileStatus(file).getModificationTime();+ return true;+ }+ }++ return false;+ }/** Updates segment names.*@@ -167,8 +196,12 @@ public class DistributedSearch {*/public void updateSegments() throws IOException {- int liveServers=0;- int liveSegments=0;+ int liveServers = 0;+ int liveSegments = 0;++ if (isFileModified()) {+ defaultAddresses = readConfig(file, conf);+ }// Create new array of flags so they can all be updated at once.boolean[] updatedLiveServer = new boolean[defaultAddresses.length];@@ -188,15 +221,17 @@ public class DistributedSearch {}continue;}+for (int j = 0; j < segments.length; j++) {if (LOG.isTraceEnabled()) {LOG.trace("Client: segment "+segments[j]+" at "+addr);}segmentToAddress.put(segments[j], addr);}+updatedLiveServer[i] = true;liveServers++;- liveSegments+=segments.length;+ liveSegments += segments.length;}// Now update live server flags.@@ -413,5 +448,9 @@ public class DistributedSearch {running = false;interrupt();}++ public boolean[] getLiveServer() {+ return liveServer;+ }}-}+}\ No newline at end of filediff --git a/src/test/org/apache/nutch/searcher/DistributedSearchTest.java b/src/test/org/apache/nutch/searcher/DistributedSearchTest.javadeleted file mode 100644index 3fbd48951..000000000--- a/src/test/org/apache/nutch/searcher/DistributedSearchTest.java+++ /dev/null@@ -1,68 +0,0 @@-/*-* Licensed to the Apache Software Foundation (ASF) under one or more-* contributor license agreements. See the NOTICE file distributed with-* this work for additional information regarding copyright ownership.-* The ASF licenses this file to You under the Apache License, Version 2.0-* (the "License"); you may not use this file except in compliance with-* the License. You may obtain a copy of the License at-*-* http://www.apache.org/licenses/LICENSE-2.0-*-* Unless required by applicable law or agreed to in writing, software-* distributed under the License is distributed on an "AS IS" BASIS,-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-* See the License for the specific language governing permissions and-* limitations under the License.-*/-package org.apache.nutch.searcher;--import java.io.IOException;-import java.net.InetSocketAddress;--import org.apache.hadoop.conf.Configuration;-import org.apache.hadoop.fs.Path;-import org.apache.hadoop.ipc.Server;-import org.apache.nutch.searcher.DistributedSearch.Client;-import org.apache.nutch.util.NutchConfiguration;--import junit.framework.TestCase;--public class DistributedSearchTest extends TestCase {-- private static final int DEFAULT_PORT = 60000;- private static final String DISTRIBUTED_SEARCH_TEST_PORT = "distributed.search.test.port";- Configuration conf;- Path searchdir=new Path("build/test/data/testcrawl/");- Server server;-- protected void setUp() throws Exception {- super.setUp();- conf=NutchConfiguration.create();- //set up server & start it- server=DistributedSearch.Server.getServer(conf, searchdir, conf.getInt(DISTRIBUTED_SEARCH_TEST_PORT, DEFAULT_PORT));- server.start();- }-- protected void tearDown() throws Exception {- super.tearDown();- if(server!=null){- //stop server- //server.stop();- }- }-- public void testDistibutedSearch() throws IOException{-- int port=conf.getInt(DISTRIBUTED_SEARCH_TEST_PORT, DEFAULT_PORT);-- InetSocketAddress[] addresses=new InetSocketAddress[1];- addresses[0]=new InetSocketAddress("localhost", port);-- Client c=new DistributedSearch.Client(addresses, conf);-- Query query=Query.parse("apache", conf);- Hits hits=c.search(query, 5, null, null, false);- c.getDetails(hits.getHit(0));- assertTrue(hits.getTotal()>0);- }-}diff --git a/src/test/org/apache/nutch/searcher/TestDistributedSearch.java b/src/test/org/apache/nutch/searcher/TestDistributedSearch.javanew file mode 100644index 000000000..150bf8a75--- /dev/null+++ b/src/test/org/apache/nutch/searcher/TestDistributedSearch.java@@ -0,0 +1,144 @@+/*+* Licensed to the Apache Software Foundation (ASF) under one or more+* contributor license agreements. See the NOTICE file distributed with+* this work for additional information regarding copyright ownership.+* The ASF licenses this file to You under the Apache License, Version 2.0+* (the "License"); you may not use this file except in compliance with+* the License. You may obtain a copy of the License at+*+* http://www.apache.org/licenses/LICENSE-2.0+*+* Unless required by applicable law or agreed to in writing, software+* distributed under the License is distributed on an "AS IS" BASIS,+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+* See the License for the specific language governing permissions and+* limitations under the License.+*/+package org.apache.nutch.searcher;++import java.io.BufferedWriter;+import java.io.IOException;+import java.io.OutputStreamWriter;+import java.net.InetSocketAddress;++import junit.framework.TestCase;++import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.fs.FileSystem;+import org.apache.hadoop.fs.Path;+import org.apache.hadoop.ipc.Server;+import org.apache.nutch.searcher.DistributedSearch.Client;+import org.apache.nutch.util.NutchConfiguration;++public class TestDistributedSearch+ extends TestCase {++ private static final int DEFAULT_PORT = 60000;+ private static final String DISTRIBUTED_SEARCH_TEST_PORT = "distributed.search.test.port";++ private static final int DEFAULT_PORT1 = 60001;+ private static final String DISTRIBUTED_SEARCH_TEST_PORT1 = "distributed.search.test.port1";++ private static final int DEFAULT_PORT2 = 60002;+ private static final String DISTRIBUTED_SEARCH_TEST_PORT2 = "distributed.search.test.port2";++ Path searchdir = new Path("build/test/data/testcrawl/");++ public void testDistibutedSearch()+ throws IOException {++ Configuration conf = NutchConfiguration.create();++ //set up server & start it+ Server server = DistributedSearch.Server.getServer(conf, searchdir,+ conf.getInt(DISTRIBUTED_SEARCH_TEST_PORT, DEFAULT_PORT));+ server.start();++ int port = conf.getInt(DISTRIBUTED_SEARCH_TEST_PORT, DEFAULT_PORT);++ InetSocketAddress[] addresses = new InetSocketAddress[1];+ addresses[0] = new InetSocketAddress("localhost", port);++ Client c = new DistributedSearch.Client(addresses, conf);++ Query query = Query.parse("apache", conf);+ Hits hits = c.search(query, 5, null, null, false);+ c.getDetails(hits.getHit(0));+ assertTrue(hits.getTotal() > 0);++ if(server != null){+ server.stop();+ }+ }++ public void testUpdateSegments()+ throws IOException {++ // Startup 2 search servers. One was already started in setup, start another+ // one at a different port++ Configuration conf = NutchConfiguration.create();++ Server server1 = DistributedSearch.Server.getServer(conf, searchdir,+ conf.getInt(DISTRIBUTED_SEARCH_TEST_PORT1, DEFAULT_PORT1));++ Server server2 = DistributedSearch.Server.getServer(conf, searchdir,+ conf.getInt(DISTRIBUTED_SEARCH_TEST_PORT2, DEFAULT_PORT2));++ server1.start();+ server2.start();++ /* create a new file search-servers.txt+ * with 1 server at port 60000+ */+ FileSystem fs = FileSystem.get(conf);+ Path testServersPath = new Path(searchdir, "search-server.txt");+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fs.create(+ testServersPath, true)));+ bw.write("localhost " + DEFAULT_PORT1 + "\n");+ bw.flush();+ bw.close();++ /*+ * Check if it found the server+ */+ Client c = new DistributedSearch.Client(testServersPath, conf);+ boolean[] liveServers = c.getLiveServer();+ assertEquals(liveServers.length, 1);++ /* Add both the servers at ports 60000 & 60005+ * to the search-server.txt file+ */++ // give the servers a little time to wait for file modification+ try {+ Thread.sleep(1000);+ } catch (InterruptedException e) {+ // TODO Auto-generated catch block+ e.printStackTrace();+ }++ bw = new BufferedWriter(new OutputStreamWriter(fs.create(testServersPath,+ true)));+ bw.write("localhost " + DEFAULT_PORT1 + "\n");+ bw.write("localhost " + DEFAULT_PORT2 + "\n");+ bw.flush();+ bw.close();++ // Check if it found both the servers+ c.updateSegments();+++ liveServers = c.getLiveServer();+ assertEquals(liveServers.length, 2);++ if (server1 != null) {+ server1.stop();+ }+ if (server2 != null) {+ server2.stop();+ }++ fs.delete(testServersPath);+ }+}