Bug at - Producer-Consumer Example
-----------------------------------

                 Key: ZOOKEEPER-887
                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-887
             Project: Zookeeper
          Issue Type: Bug
          Components: java client
            Reporter: sanjivsingh
            Priority: Minor


I tried to test  Producer-Consumer Example published at ...
http://hadoop.apache.org/zookeeper/docs/r3.0.0/zookeeperTutorial.html

Queue.produce( int p)   working correctly,,,

there is problem in Queue.consume( )  method.

 int consume() throws KeeperException, InterruptedException{
            int retvalue = -1;
            Stat stat = null;

            // Get the first element available
            while (true) {
                synchronized (mutex) {
                    List<String> list = zk.getChildren(root, true);
                    if (list.size() == 0) {
                        System.out.println("Going to wait");
                        mutex.wait();
                    } else {
                        Integer min = new
Integer(list.get(0).substring(7));
                        for(String s : list){
                            Integer tempValue = new
Integer(s.substring(7));
                            //System.out.println("Temporary value: " +
tempValue);
                            if(tempValue < min) min = tempValue;
                        }
                        System.out.println("Temporary value: " + root
+ "/element" + min);
                        byte[] b = zk.getData(root + "/element" + min,
                                    false, stat);
                        zk.delete(root + "/element" + min, 0);
                        ByteBuffer buffer = ByteBuffer.wrap(b);
                        retvalue = buffer.getInt();

                        return retvalue;
                    }
                }
            }
        }

    wat exactly produce( )  doing   is that add child under root  like
element000000001,
   element000000002 ,element000000003 etc....

   but
  In consume( ) method ,
          1.  Integer min = new Integer(list.get(0).substring(7));
          2.             for(String s : list){
          3.                 Integer tempValue = new
Integer(s.substring(7));
          4.                  if(tempValue < min) min = tempValue;
          5.               }
          6.       byte[] b = zk.getData(root + "/element" + min,
false, stat);
          7.        zk.delete(root + "/element" + min, 0);

   bcuz of..
  line 1 & 3 .. converting  like  String  000000001   --------->
Interger  1
  and bcuz of this , in line 6 & 7

  It is tring to access znode like   at  root + "/element1" rather
than  root + "/element000000001"
  that is definelty no-existing one..........

 I m putting forward  a solution....

    int consume() throws KeeperException, InterruptedException{
            int retvalue = -1;
            Stat stat = null;

            // Get the first element available
            while (true) {
                synchronized (mutex) {

                    List<String> list = zk.getChildren(root, true);
                    if (list.size() == 0) {
                        System.out.println("Going to wait");
                        mutex.wait();
                    } else {
                        Integer min = new
Integer(list.get(0).substring(7));

                        int i=0 ,p=0;
                        for(String s : list){
                            Integer tempValue = new
Integer(s.substring(7));
                            if(tempValue < min)
                                    p=i;
                            i++;
                        }

                        byte[] b = zk.getData(root + "/element" +
list.get(p).substring(7), false, stat);
                        zk.delete(root + "/element" +
list.get(p).substring(7), 0);
                        ByteBuffer buffer = ByteBuffer.wrap(b);
                        retvalue = buffer.getInt();

                        return retvalue;
                    }
                }
            }
        }
    }

 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to