[ 
https://issues.apache.org/jira/browse/ZOOKEEPER-887?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15472910#comment-15472910
 ] 

Prabhunath Yadav commented on ZOOKEEPER-887:
--------------------------------------------

Even the solution provided by sanjivsingh is not optimal , the node should be 
deleted in sequence that is the lowest one should be deleted first , as it is  
following queue property , i have made the optimal and accurate solution for 
this and would like to attach the solution.

>  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
>         Attachments: zookeeper-887.patch
>
>
> 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 was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to