Package org.apache.hama.bsp.sync

Examples of org.apache.hama.bsp.sync.ZooKeeperSyncServerImpl


  }

  @Test
  public void testClearZKNodes() throws IOException, KeeperException,
      InterruptedException {
    final ZooKeeperSyncServerImpl server = new ZooKeeperSyncServerImpl();
    boolean done = false;
    try {
      server.init(configuration);
      ExecutorService executorService = Executors.newCachedThreadPool();
      executorService.submit(new Runnable() {
        @Override
        public void run() {
            try {
                server.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      });

      executorService.awaitTermination(10, TimeUnit.SECONDS);

      String bspRoot = "/bsp";

      ZooKeeperSyncClientImpl peerClient = (ZooKeeperSyncClientImpl) SyncServiceFactory
          .getPeerSyncClient(configuration);

      ZKSyncBSPMasterClient masterClient = (ZKSyncBSPMasterClient) SyncServiceFactory
          .getMasterSyncClient(configuration);

      masterClient.init(configuration);

      Thread.sleep(100);

      Log.info("Created master and client sync clients");

      assertTrue(masterClient.hasKey(bspRoot));

      Log.info("BSP root exists");

      BSPJobID jobID = new BSPJobID("test1", 1);
      masterClient.registerJob(jobID.toString());
      TaskID taskId1 = new TaskID(jobID, 1);
      TaskID taskId2 = new TaskID(jobID, 2);

      TaskAttemptID task1 = new TaskAttemptID(taskId1, 1);
      TaskAttemptID task2 = new TaskAttemptID(taskId2, 1);

      int zkPort = BSPNetUtils.getFreePort(21815);
      configuration.setInt(Constants.PEER_PORT, zkPort);
      peerClient.init(configuration, jobID, task1);

      peerClient.registerTask(jobID, "hamanode1", 5000L, task1);
      peerClient.registerTask(jobID, "hamanode2", 5000L, task2);

      peerClient.storeInformation(
          peerClient.constructKey(jobID, "info", "level2"), new IntWritable(5),
          true, null);

      String[] names = peerClient.getAllPeerNames(task1);

      Log.info("Found child count = " + names.length);

      assertEquals(2, names.length);

      Log.info("Passed the child count test");

      masterClient.addKey(masterClient.constructKey(jobID, "peer", "1"),
          true, null);
      masterClient.addKey(masterClient.constructKey(jobID, "peer", "2"),
          true, null);

      String[] peerChild = masterClient.getChildKeySet(
          masterClient.constructKey(jobID, "peer"), null);
      Log.info("Found child count = " + peerChild.length);

      assertEquals(2, peerChild.length);

      Log.info(" Peer name " + peerChild[0]);
      Log.info(" Peer name " + peerChild[1]);

      Log.info("Passed the child key set test");

      masterClient.deregisterJob(jobID.toString());
      Log.info(masterClient.constructKey(jobID));

      Thread.sleep(200);

      assertEquals(false, masterClient.hasKey(masterClient.constructKey(jobID)));

      Log.info("Passed the key presence test");

      boolean result = masterClient
          .getInformation(masterClient.constructKey(jobID, "info", "level3"),
              new IntWritable());

      assertEquals(false, result);
     
      Writable[] writableArr = new Writable[2];
      writableArr[0] = new LongWritable(3L);
      writableArr[1] = new LongWritable(5L);
      ArrayWritable arrWritable = new ArrayWritable(LongWritable.class);
      arrWritable.set(writableArr);
      masterClient.storeInformation(
          masterClient.constructKey(jobID, "info", "level3"),
          arrWritable, true, null);
     
      ArrayWritable valueHolder = new ArrayWritable(LongWritable.class);
     
      boolean getResult = masterClient.getInformation(
          masterClient.constructKey(jobID, "info", "level3"), valueHolder);
     
      assertTrue(getResult);
     
      assertEquals(arrWritable.get()[0], valueHolder.get()[0]);
      assertEquals(arrWritable.get()[1], valueHolder.get()[1]);
     
      Log.info("Passed array writable test");
      done = true;

    } catch (Exception e) {
      e.printStackTrace();

    } finally {
      server.stopServer();
    }
    assertEquals(true, done);
  }
View Full Code Here


  }

  @Test
  public void testClearZKNodes() throws IOException, KeeperException,
      InterruptedException {
    final ZooKeeperSyncServerImpl server = new ZooKeeperSyncServerImpl();
    boolean done = false;
    try {
      server.init(configuration);
      ExecutorService executorService = Executors.newCachedThreadPool();
      executorService.submit(new Runnable() {
        @Override
        public void run() {
          try {
            server.start();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      });

      executorService.awaitTermination(10, TimeUnit.SECONDS);

      String bspRoot = "/bsp";

      ZooKeeperSyncClientImpl peerClient = (ZooKeeperSyncClientImpl) SyncServiceFactory
          .getPeerSyncClient(configuration);

      ZKSyncBSPMasterClient masterClient = (ZKSyncBSPMasterClient) SyncServiceFactory
          .getMasterSyncClient(configuration);

      masterClient.init(configuration);

      Thread.sleep(100);

      Log.info("Created master and client sync clients");

      assertTrue(masterClient.hasKey(bspRoot));

      Log.info("BSP root exists");

      BSPJobID jobID = new BSPJobID("test1", 1);
      masterClient.registerJob(jobID.toString());
      TaskID taskId1 = new TaskID(jobID, 1);
      TaskID taskId2 = new TaskID(jobID, 2);

      TaskAttemptID task1 = new TaskAttemptID(taskId1, 1);
      TaskAttemptID task2 = new TaskAttemptID(taskId2, 1);

      int zkPort = BSPNetUtils.getFreePort(21815);
      configuration.setInt(Constants.PEER_PORT, zkPort);
      peerClient.init(configuration, jobID, task1);

      peerClient.registerTask(jobID, "hamanode1", 5000L, task1);
      peerClient.registerTask(jobID, "hamanode2", 5000L, task2);

      peerClient.storeInformation(
          peerClient.constructKey(jobID, "info", "level2"), new IntWritable(5),
          true, null);

      String[] names = peerClient.getAllPeerNames(task1);

      Log.info("Found child count = " + names.length);

      assertEquals(2, names.length);

      Log.info("Passed the child count test");

      masterClient.addKey(masterClient.constructKey(jobID, "peer", "1"), true,
          null);
      masterClient.addKey(masterClient.constructKey(jobID, "peer", "2"), true,
          null);

      String[] peerChild = masterClient.getChildKeySet(
          masterClient.constructKey(jobID, "peer"), null);
      Log.info("Found child count = " + peerChild.length);

      assertEquals(2, peerChild.length);

      Log.info(" Peer name " + peerChild[0]);
      Log.info(" Peer name " + peerChild[1]);

      Log.info("Passed the child key set test");

      masterClient.deregisterJob(jobID.toString());
      Log.info(masterClient.constructKey(jobID));

      Thread.sleep(200);

      assertEquals(false, masterClient.hasKey(masterClient.constructKey(jobID)));

      Log.info("Passed the key presence test");

      boolean result = masterClient
          .getInformation(masterClient.constructKey(jobID, "info", "level3"),
              new IntWritable());

      assertEquals(false, result);

      Writable[] writableArr = new Writable[2];
      writableArr[0] = new LongWritable(3L);
      writableArr[1] = new LongWritable(5L);
      ArrayWritable arrWritable = new ArrayWritable(LongWritable.class);
      arrWritable.set(writableArr);
      masterClient.storeInformation(
          masterClient.constructKey(jobID, "info", "level3"), arrWritable,
          true, null);

      ArrayWritable valueHolder = new ArrayWritable(LongWritable.class);

      boolean getResult = masterClient.getInformation(
          masterClient.constructKey(jobID, "info", "level3"), valueHolder);

      assertTrue(getResult);

      assertEquals(arrWritable.get()[0], valueHolder.get()[0]);
      assertEquals(arrWritable.get()[1], valueHolder.get()[1]);

      Log.info("Passed array writable test");
      done = true;

    } catch (Exception e) {
      e.printStackTrace();

    } finally {
      server.stopServer();
    }
    assertEquals(true, done);
  }
View Full Code Here

  }

  @Test
  public void testClearZKNodes() throws IOException, KeeperException,
      InterruptedException {
    final ZooKeeperSyncServerImpl server = new ZooKeeperSyncServerImpl();
    boolean done = false;
    try {
      server.init(configuration);
      ExecutorService executorService = Executors.newCachedThreadPool();
      executorService.submit(new Runnable() {
        @Override
        public void run() {
          try {
            server.start();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      });

      executorService.awaitTermination(10, TimeUnit.SECONDS);

      String bspRoot = "/bsp";

      ZooKeeperSyncClientImpl peerClient = (ZooKeeperSyncClientImpl) SyncServiceFactory
          .getPeerSyncClient(configuration);

      ZKSyncBSPMasterClient masterClient = (ZKSyncBSPMasterClient) SyncServiceFactory
          .getMasterSyncClient(configuration);

      masterClient.init(configuration);

      Thread.sleep(100);

      Log.info("Created master and client sync clients");

      assertTrue(masterClient.hasKey(bspRoot));

      Log.info("BSP root exists");

      BSPJobID jobID = new BSPJobID("test1", 1);
      masterClient.registerJob(jobID.toString());
      TaskID taskId1 = new TaskID(jobID, 1);
      TaskID taskId2 = new TaskID(jobID, 2);

      TaskAttemptID task1 = new TaskAttemptID(taskId1, 1);
      TaskAttemptID task2 = new TaskAttemptID(taskId2, 1);

      int zkPort = BSPNetUtils.getFreePort(21815);
      configuration.setInt(Constants.PEER_PORT, zkPort);
      peerClient.init(configuration, jobID, task1);

      peerClient.registerTask(jobID, "hamanode1", 5000L, task1);
      peerClient.registerTask(jobID, "hamanode2", 5000L, task2);

      peerClient.storeInformation(
          peerClient.constructKey(jobID, "info", "level2"), new IntWritable(5),
          true, null);

      String[] names = peerClient.getAllPeerNames(task1.getJobID());

      Log.info("Found child count = " + names.length);

      assertEquals(2, names.length);

      Log.info("Passed the child count test");

      masterClient.addKey(masterClient.constructKey(jobID, "peer", "1"), true,
          null);
      masterClient.addKey(masterClient.constructKey(jobID, "peer", "2"), true,
          null);

      String[] peerChild = masterClient.getChildKeySet(
          masterClient.constructKey(jobID, "peer"), null);
      Log.info("Found child count = " + peerChild.length);

      assertEquals(2, peerChild.length);

      Log.info(" Peer name " + peerChild[0]);
      Log.info(" Peer name " + peerChild[1]);

      Log.info("Passed the child key set test");

      masterClient.deregisterJob(jobID.toString());
      Log.info(masterClient.constructKey(jobID));

      Thread.sleep(200);

      assertEquals(false, masterClient.hasKey(masterClient.constructKey(jobID)));

      Log.info("Passed the key presence test");

      boolean result = masterClient
          .getInformation(masterClient.constructKey(jobID, "info", "level3"),
              new IntWritable());

      assertEquals(false, result);

      Writable[] writableArr = new Writable[2];
      writableArr[0] = new LongWritable(3L);
      writableArr[1] = new LongWritable(5L);
      ArrayWritable arrWritable = new ArrayWritable(LongWritable.class);
      arrWritable.set(writableArr);
      masterClient.storeInformation(
          masterClient.constructKey(jobID, "info", "level3"), arrWritable,
          true, null);

      ArrayWritable valueHolder = new ArrayWritable(LongWritable.class);

      boolean getResult = masterClient.getInformation(
          masterClient.constructKey(jobID, "info", "level3"), valueHolder);

      assertTrue(getResult);

      assertEquals(arrWritable.get()[0], valueHolder.get()[0]);
      assertEquals(arrWritable.get()[1], valueHolder.get()[1]);

      Log.info("Passed array writable test");
      done = true;

    } catch (Exception e) {
      e.printStackTrace();

    } finally {
      server.stopServer();
    }
    assertEquals(true, done);
  }
View Full Code Here

            .getCanonicalName());
  }

  public void testClearZKNodes() throws IOException, KeeperException,
      InterruptedException {
    final ZooKeeperSyncServerImpl server = new ZooKeeperSyncServerImpl();
    try {
      server.init(configuration);
      new Thread(new Runnable() {

        @Override
        public void run() {
          try {
            server.start();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }).start();

      int timeout = configuration.getInt(Constants.ZOOKEEPER_SESSION_TIMEOUT,
          6000);
      String connectStr = QuorumPeer.getZKQuorumServersString(configuration);
      String bspRoot = "/";
      // Establishing a zk session.
      ZooKeeper zk = new ZooKeeper(connectStr, timeout, null);

      // Creating dummy bspRoot if it doesn't already exist.
      Stat s = zk.exists(bspRoot, false);
      if (s == null) {
        zk.create(bspRoot, new byte[0], Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
      }

      // Creating dummy child nodes at depth 1.
      String node1 = bspRoot + "task1";
      String node2 = bspRoot + "task2";
      zk.create(node1, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      zk.create(node2, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

      // Creating dummy child node at depth 2.
      String node11 = node1 + "superstep1";
      zk.create(node11, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

      ArrayList<String> list = (ArrayList<String>) zk.getChildren(bspRoot,
          false);
      assertEquals(2, list.size());
      System.out.println(list.size());

      // clear it
      BSPMaster.clearZKNodes(zk, "/");

      list = (ArrayList<String>) zk.getChildren(bspRoot, false);
      System.out.println(list.size());
      assertEquals(0, list.size());

      try {
        zk.getData(node11, false, null);
        fail();
      } catch (KeeperException.NoNodeException e) {
        System.out.println("Node has been removed correctly!");
      } finally {
        zk.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      server.stopServer();
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.hama.bsp.sync.ZooKeeperSyncServerImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.