lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markrmil...@apache.org
Subject svn commit: r901211 - in /lucene/solr/branches/cloud/src: java/org/apache/solr/cloud/ZkController.java java/org/apache/solr/cloud/ZkNodeProps.java test/org/apache/solr/cloud/ZkControllerTest.java test/org/apache/solr/cloud/ZkNodePropsTest.java
Date Wed, 20 Jan 2010 14:19:49 GMT
Author: markrmiller
Date: Wed Jan 20 14:19:49 2010
New Revision: 901211

URL: http://svn.apache.org/viewvc?rev=901211&view=rev
Log:
improve ZkNodeProps and tests

Modified:
    lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkController.java
    lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkNodeProps.java
    lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkControllerTest.java
    lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkNodePropsTest.java

Modified: lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkController.java
URL: http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkController.java?rev=901211&r1=901210&r2=901211&view=diff
==============================================================================
--- lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkController.java (original)
+++ lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkController.java Wed Jan 20
14:19:49 2010
@@ -577,7 +577,7 @@
     
     props.put(NODE_NAME, getNodeName());
 
-    props.store(new DataOutputStream(baos));
+    props.store(baos);
 
     String shardZkNodeName = hostName + ":" + localHostPort + "_"+ localHostContext + (coreName.length()
== 0 ? "" : "_" + coreName);
     try {

Modified: lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkNodeProps.java
URL: http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkNodeProps.java?rev=901211&r1=901210&r2=901211&view=diff
==============================================================================
--- lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkNodeProps.java (original)
+++ lucene/solr/branches/cloud/src/java/org/apache/solr/cloud/ZkNodeProps.java Wed Jan 20
14:19:49 2010
@@ -3,31 +3,54 @@
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.Set;
+import java.util.Map.Entry;
 
 public class ZkNodeProps extends HashMap<String,String> {
 
   private static final long serialVersionUID = 1L;
 
-  public void load(DataInputStream in) throws IOException {
-    String stringRep = in.readUTF();
+  public void load(InputStream in) throws IOException {
+    DataInputStream dis = new DataInputStream(in);
+    String stringRep = null;
+    try {
+      stringRep = dis.readUTF();
+    } finally {
+      dis.close();
+    }
     String[] lines = stringRep.split("\n");
-    for(String line : lines) {
+    for (String line : lines) {
       int sepIndex = line.indexOf('=');
       String key = line.substring(0, sepIndex);
       String value = line.substring(sepIndex + 1, line.length());
       put(key, value);
     }
   }
+
+  public void store(OutputStream out) throws IOException {
+    StringBuilder sb = new StringBuilder();
+    Set<Entry<String,String>> entries = entrySet();
+    for(Entry<String,String> entry : entries) {
+      sb.append(entry.getKey() + "=" + entry.getValue() + "\n");
+    }
+    DataOutputStream dos = new DataOutputStream(out);
+    try {
+      dos.writeUTF(sb.toString());
+    } finally {
+      dos.close();
+    }
+  }
   
-  public void store(DataOutputStream out) throws IOException {
+  public String toString() {
     StringBuilder sb = new StringBuilder();
-    Set<String> keys = keySet();
-    for(String key : keys) {
-      String value = get(key);
-      sb.append(key + "=" + value + "\n");
+    Set<Entry<String,String>> entries = entrySet();
+    for(Entry<String,String> entry : entries) {
+      sb.append(entry.getKey() + "=" + entry.getValue() + "\n");
     }
-    out.writeUTF(sb.toString());
+    return sb.toString();
   }
+
 }

Modified: lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkControllerTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkControllerTest.java?rev=901211&r1=901210&r2=901211&view=diff
==============================================================================
--- lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkControllerTest.java (original)
+++ lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkControllerTest.java Wed Jan
20 14:19:49 2010
@@ -26,10 +26,23 @@
 
 import junit.framework.TestCase;
 
+import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
 
 public class ZkControllerTest extends TestCase {
 
+  private static final String TEST_NODE_NAME = "test_node_name";
+
+  private static final String URL3 = "http://localhost:3133/solr/core1";
+
+  private static final String URL2 = "http://localhost:3123/solr/core1";
+
+  private static final String SHARD3 = "localhost:3123_solr_core3";
+
+  private static final String SHARD2 = "localhost:3123_solr_core2";
+
+  private static final String SHARD1 = "localhost:3123_solr_core1";
+
   private static final String COLLECTION_NAME = "collection1";
 
   static final String ZOO_KEEPER_ADDRESS = "localhost:2181/solr";
@@ -64,34 +77,53 @@
 
       zkClient.makePath("collections/collection1/config=collection1");
 
-      addShardToZk(zkClient, shardsPath, URL1, "slave");
-      addShardToZk(zkClient, shardsPath, "http://localhost:3123/solr/core1",
-          "master");
-      addShardToZk(zkClient, shardsPath, "http://localhost:3133/solr/core1",
-          "slave");
+      addShardToZk(zkClient, shardsPath, SHARD1, URL1, "slave");
+      addShardToZk(zkClient, shardsPath, SHARD2, URL2, "master");
+      addShardToZk(zkClient, shardsPath, SHARD3, URL3, "slave");
 
       if (DEBUG) {
         zkClient.printLayoutToStdOut();
       }
 
-      zkController = new ZkController(ZOO_KEEPER_ADDRESS, TIMEOUT,
-          "localhost", "8983", "/solr");
+      zkController = new ZkController(ZOO_KEEPER_ADDRESS, TIMEOUT, "localhost",
+          "8983", "/solr");
       zkController.updateCloudState();
       CloudState cloudInfo = zkController.getCloudState();
       Map<String,Slice> slices = cloudInfo.getSlices("collection1");
       assertNotNull(slices);
 
-
-      if (DEBUG) {
-        for(Slice slice : slices.values()) {
-          for (String shard : slice.getShards().keySet()) {
-            System.out.println("shard:" + shard);
+      for (Slice slice : slices.values()) {
+        Map<String,ZkNodeProps> shards = slice.getShards();
+        if (DEBUG) {
+          for (String shardName : shards.keySet()) {
+            ZkNodeProps props = shards.get(shardName);
+            System.out.println("shard:" + shardName);
+            System.out.println("props:" + props.toString());
           }
         }
+        assertNotNull(shards.get(SHARD1));
+        assertNotNull(shards.get(SHARD2));
+        assertNotNull(shards.get(SHARD3));
+
+        ZkNodeProps props = shards.get(SHARD1);
+        assertEquals("slave", props.get(ZkController.ROLE_PROP));
+        assertEquals(URL1, props.get(ZkController.URL_PROP));
+        assertEquals(TEST_NODE_NAME, props.get(ZkController.NODE_NAME));
+
+        props = shards.get(SHARD2);
+        assertEquals("master", props.get(ZkController.ROLE_PROP));
+        assertEquals(URL2, props.get(ZkController.URL_PROP));
+        assertEquals(TEST_NODE_NAME, props.get(ZkController.NODE_NAME));
+        
+        props = shards.get(SHARD3);
+        assertEquals("slave", props.get(ZkController.ROLE_PROP));
+        assertEquals(URL3, props.get(ZkController.URL_PROP));
+        assertEquals(TEST_NODE_NAME, props.get(ZkController.NODE_NAME));
+
       }
 
-      // nocommit : check properties 
-      
+      // nocommit : check properties
+
     } finally {
       if (zkClient != null) {
         zkClient.close();
@@ -99,7 +131,7 @@
       if (server != null) {
         server.shutdown();
       }
-      if(zkController != null) {
+      if (zkController != null) {
         zkController.close();
       }
     }
@@ -130,14 +162,13 @@
     String configName = zkController.readConfigName(COLLECTION_NAME);
     assertEquals(configName, actualConfigName);
 
-
     // nocommit : close in finally
     zkController.close();
     zkClient.close();
     server.shutdown();
 
   }
-  
+
   public void testUploadToCloud() throws Exception {
     String zkDir = tmpDir.getAbsolutePath() + File.separator
         + "zookeeper/server1/data";
@@ -150,13 +181,13 @@
     ZkController zkController = new ZkController(ZOO_KEEPER_ADDRESS, TIMEOUT,
         "localhost", "8983", "/solr");
 
+    zkController.uploadDirToCloud(new File("solr/conf"),
+        ZkController.CONFIGS_ZKNODE + "/config1");
 
-    zkController.uploadDirToCloud(new File("solr/conf"), ZkController.CONFIGS_ZKNODE + "/config1");
-    
     if (DEBUG) {
       zkController.printLayoutToStdOut();
     }
-    
+
     // nocommit close in finally
     zkController.close();
     server.shutdown();
@@ -164,19 +195,19 @@
   }
 
   private void addShardToZk(SolrZkClient zkClient, String shardsPath,
-      String url, String role) throws IOException, KeeperException,
-      InterruptedException {
+      String zkNodeName, String url, String role) throws IOException,
+      KeeperException, InterruptedException {
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
     ZkNodeProps props = new ZkNodeProps();
     props.put(ZkController.URL_PROP, url);
     props.put(ZkController.ROLE_PROP, role);
-    props.store(new DataOutputStream(baos));
+    props.put(ZkController.NODE_NAME, TEST_NODE_NAME);
+    props.store(baos);
 
-    //nocommit : fix
-//    zkClient.create(shardsPath + ZkController.CORE_ZKPREFIX,
-//        baos.toByteArray(), CreateMode.EPHEMERAL_SEQUENTIAL);
+    System.out.println("shards path:" + shardsPath);
+    zkClient.create(shardsPath + "/" + zkNodeName, baos.toByteArray(),
+        CreateMode.PERSISTENT);
   }
 
-
 }

Modified: lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkNodePropsTest.java?rev=901211&r1=901210&r2=901211&view=diff
==============================================================================
--- lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkNodePropsTest.java (original)
+++ lucene/solr/branches/cloud/src/test/org/apache/solr/cloud/ZkNodePropsTest.java Wed Jan
20 14:19:49 2010
@@ -19,8 +19,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
 import java.io.IOException;
 
 import junit.framework.TestCase;
@@ -35,12 +33,18 @@
     props.put("prop1", "value1");
     props.put("prop2", "value2");
     props.put("prop3", "value3");
-    props.store(new DataOutputStream(baos));
+    props.put("prop4", "value4");
+    props.put("prop5", "value5");
+    props.put("prop6", "value6");
+    props.store(baos);
     
     ZkNodeProps props2 = new ZkNodeProps();
-    props2.load(new DataInputStream(new ByteArrayInputStream(baos.toByteArray())));
+    props2.load(new ByteArrayInputStream(baos.toByteArray()));
     assertEquals("value1", props2.get("prop1"));
     assertEquals("value2", props2.get("prop2"));
     assertEquals("value3", props2.get("prop3"));
+    assertEquals("value4", props2.get("prop4"));
+    assertEquals("value5", props2.get("prop5"));
+    assertEquals("value6", props2.get("prop6"));
   }
 }



Mime
View raw message