当前位置: 首页>>代码示例>>Java>>正文


Java ZkClient.close方法代码示例

本文整理汇总了Java中org.apache.helix.manager.zk.ZkClient.close方法的典型用法代码示例。如果您正苦于以下问题:Java ZkClient.close方法的具体用法?Java ZkClient.close怎么用?Java ZkClient.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.helix.manager.zk.ZkClient的用法示例。


在下文中一共展示了ZkClient.close方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setUpHelixCluster

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
private void setUpHelixCluster(String zookeeperQuorum, String clusterName) {
  ZkClient zkClient = ZKClientPool.getZkClient(zookeeperQuorum);
  HelixAdmin helixAdmin = new ZKHelixAdmin(zkClient);
  try {
    if(!ImmutableSet.copyOf(helixAdmin.getClusters()).contains(clusterName)) {
      ClusterSetup helixClusterSetUp = new ClusterSetup(zkClient);
      helixClusterSetUp.addCluster(clusterName, false);
      helixClusterSetUp.setConfig(HelixConfigScope.ConfigScopeProperty.CLUSTER, clusterName,
          "allowParticipantAutoJoin=true");
    }
  } finally {
    zkClient.close();
  }
}
 
开发者ID:pinterest-attic,项目名称:terrapin,代码行数:15,代码来源:TerrapinControllerHandler.java

示例2: testWatchRenew

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
/**
 * test zk watchers are renewed automatically after session expiry
 * zookeeper-client side keeps all registered watchers see ZooKeeper.WatchRegistration.register()
 * after session expiry, all watchers are renewed
 * if a path that has watches on it has been removed during session expiry,
 * the watchers on that path will still get callbacks after session renewal, especially:
 * a data-watch will get data-deleted callback
 * a child-watch will get a child-change callback with current-child-list = null
 * this can be used for cleanup watchers on the zookeeper-client side
 */
@Test
public void testWatchRenew() throws Exception {

  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String testName = className + "_" + methodName;

  final ZkClient client =
      new ZkClient(ZK_ADDR, ZkClient.DEFAULT_SESSION_TIMEOUT,
          ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
  // make sure "/testName/test" doesn't exist
  final String path = "/" + testName + "/test";
  client.delete(path);

  ZkListener listener = new ZkListener();
  client.subscribeDataChanges(path, listener);
  client.subscribeChildChanges(path, listener);

  ZkTestHelper.expireSession(client);

  boolean succeed = listener._childChangeCountDown.await(10, TimeUnit.SECONDS);
  Assert.assertTrue(succeed,
      "fail to wait on child-change count-down in 10 seconds after session-expiry");
  Assert.assertEquals(listener._parentPath, path,
      "fail to get child-change callback after session-expiry");
  Assert.assertNull(listener._currentChilds,
      "fail to get child-change callback with currentChilds=null after session expiry");

  succeed = listener._dataDeleteCountDown.await(10, TimeUnit.SECONDS);
  Assert.assertTrue(succeed,
      "fail to wait on data-delete count-down in 10 seconds after session-expiry");
  Assert.assertEquals(listener._dataDeletePath, path,
      "fail to get data-delete callback after session-expiry");

  client.close();
}
 
开发者ID:apache,项目名称:helix,代码行数:47,代码来源:TestZkBasis.java

示例3: verifyEmptyCurStateAndExtView

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
public static boolean verifyEmptyCurStateAndExtView(String clusterName, String resourceName,
    Set<String> instanceNames, String zkAddr) {
  ZkClient zkClient = new ZkClient(zkAddr);
  zkClient.setZkSerializer(new ZNRecordSerializer());

  try {
    ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
    Builder keyBuilder = accessor.keyBuilder();

    for (String instanceName : instanceNames) {
      List<String> sessionIds = accessor.getChildNames(keyBuilder.sessions(instanceName));

      for (String sessionId : sessionIds) {
        CurrentState curState =
            accessor.getProperty(keyBuilder.currentState(instanceName, sessionId, resourceName));

        if (curState != null && curState.getRecord().getMapFields().size() != 0) {
          return false;
        }
      }

      ExternalView extView = accessor.getProperty(keyBuilder.externalView(resourceName));

      if (extView != null && extView.getRecord().getMapFields().size() != 0) {
        return false;
      }

    }

    return true;
  } finally {
    zkClient.close();
  }
}
 
开发者ID:apache,项目名称:helix,代码行数:36,代码来源:TestHelper.java

示例4: setupCluster

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
public static void setupCluster(String clusterName, String ZkAddr, int startPort,
    String participantNamePrefix, String resourceNamePrefix, int resourceNb, int partitionNb,
    int nodesNb, int replica, String stateModelDef, RebalanceMode mode, boolean doRebalance)
    throws Exception {
  ZkClient zkClient = new ZkClient(ZkAddr);
  if (zkClient.exists("/" + clusterName)) {
    LOG.warn("Cluster already exists:" + clusterName + ". Deleting it");
    zkClient.deleteRecursive("/" + clusterName);
  }

  ClusterSetup setupTool = new ClusterSetup(ZkAddr);
  setupTool.addCluster(clusterName, true);

  for (int i = 0; i < nodesNb; i++) {
    int port = startPort + i;
    setupTool.addInstanceToCluster(clusterName, participantNamePrefix + "_" + port);
  }

  for (int i = 0; i < resourceNb; i++) {
    String resourceName = resourceNamePrefix + i;
    setupTool.addResourceToCluster(clusterName, resourceName, partitionNb, stateModelDef,
        mode.toString());
    if (doRebalance) {
      setupTool.rebalanceStorageCluster(clusterName, resourceName, replica);
    }
  }
  zkClient.close();
}
 
开发者ID:apache,项目名称:helix,代码行数:29,代码来源:TestHelper.java

示例5: verifyState

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
/**
 * @param stateMap
 *          : "ResourceName/partitionKey" -> setOf(instances)
 * @param state
 *          : MASTER|SLAVE|ERROR...
 */
public static void verifyState(String clusterName, String zkAddr,
    Map<String, Set<String>> stateMap, String state) {
  ZkClient zkClient = new ZkClient(zkAddr);
  zkClient.setZkSerializer(new ZNRecordSerializer());

  try {
    ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
    Builder keyBuilder = accessor.keyBuilder();

    for (String resGroupPartitionKey : stateMap.keySet()) {
      Map<String, String> retMap = getResourceAndPartitionKey(resGroupPartitionKey);
      String resGroup = retMap.get("RESOURCE");
      String partitionKey = retMap.get("PARTITION");

      ExternalView extView = accessor.getProperty(keyBuilder.externalView(resGroup));
      for (String instance : stateMap.get(resGroupPartitionKey)) {
        String actualState = extView.getStateMap(partitionKey).get(instance);
        Assert.assertNotNull(actualState, "externalView doesn't contain state for " + resGroup
            + "/" + partitionKey + " on " + instance + " (expect " + state + ")");

        Assert
            .assertEquals(actualState, state, "externalView for " + resGroup + "/" + partitionKey
                + " on " + instance + " is " + actualState + " (expect " + state + ")");
      }
    }
  } finally {
    zkClient.close();
  }
}
 
开发者ID:apache,项目名称:helix,代码行数:37,代码来源:TestHelper.java

示例6: main

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
  if (args.length < 2) {
    System.err
        .println("USAGE: java FileStore zookeeperAddress(e.g. localhost:2181) serverId(host_port)");
    System.exit(1);
  }

  final String zkAddr = args[0];
  final String clusterName = SetupCluster.DEFAULT_CLUSTER_NAME;
  final String serverId = args[1];

  ZkClient zkclient = null;
  try {
    // start consumer
    final FileStore store = new FileStore(zkAddr, clusterName, serverId);

    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        System.out.println("Shutting down server:" + serverId);
        store.disconnect();
      }
    });
    store.connect();
  } finally {
    if (zkclient != null) {
      zkclient.close();
    }
  }
}
 
开发者ID:apache,项目名称:helix,代码行数:31,代码来源:FileStore.java

示例7: setupCluster

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
public static List<InstanceConfig> setupCluster(String zkAddr, String clusterName, int numNodes) {
  ZkClient zkclient = null;
  List<InstanceConfig> instanceConfigs = new ArrayList<InstanceConfig>();
  List<String> instanceNames = new ArrayList<String>();
  try {
    HelixAdmin admin = new ZKHelixAdmin(zkAddr);
    ClusterAdmin clusterAdmin = new ClusterAdmin(admin);
    // add cluster, always recreate
    admin.dropCluster(clusterName);
    clusterAdmin.createCluster(clusterName);

    // addNodes
    for (int i = 0; i < numNodes; i++) {
      String port = String.valueOf(MYSQL_PORTS[i]);
      String host = MYSQL_HOSTS[i];
      String serverId = host + "_" + port;
      instanceNames.add(serverId);
      InstanceConfig config = new InstanceConfig(serverId);
      config.setHostName(host);
      config.setPort(port);
      config.setInstanceEnabled(true);
      config.getRecord().setSimpleField(MySQLConstants.MYSQL_PORT, port);
      config.getRecord().setSimpleField(MySQLConstants.MYSQL_SUPER_USER, "monty");
      config.getRecord().setSimpleField(MySQLConstants.MYSQL_SUPER_PASSWORD, "some_pass");
      admin.addInstance(clusterName, config);
      instanceConfigs.add(config);
    }
    // add resource "MasterSlaveAssignment" which maintains the mapping master and slave mappings
    clusterAdmin.doInitialAssignment(clusterName, instanceNames, 2);

    // Create database
    clusterAdmin.createDatabase(clusterName, "MyDB", 6, "");

    // Add a table to the database
    clusterAdmin.createTable(clusterName, "MyDB", "MyTable", " ( col1 INT, col2 INT ) ");

  } finally {
    if (zkclient != null) {
      zkclient.close();
    }
  }
  return instanceConfigs;
}
 
开发者ID:kishoreg,项目名称:fullmatix,代码行数:44,代码来源:MysqlIntegrationTest.java

示例8: testWatchRemove

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
/**
 * after calling zkclient#unsubscribeXXXListener()
 * an already registered watch will not be removed from ZooKeeper#watchManager#XXXWatches
 * immediately.
 * the watch will get removed on the following conditions:
 * 1) there is a set/delete on the listening path via the zkclient
 * 2) session expiry on the zkclient (i.e. the watch will not be renewed after session expiry)
 * @throws Exception
 */
@Test
public void testWatchRemove() throws Exception {
  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String testName = className + "_" + methodName;

  final ZkClient client =
      new ZkClient(ZK_ADDR, ZkClient.DEFAULT_SESSION_TIMEOUT,
          ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
  // make sure "/testName/test" doesn't exist
  final String path = "/" + testName + "/test";
  client.createPersistent(path, true);

  ZkListener listener = new ZkListener();
  client.subscribeDataChanges(path, listener);
  client.subscribeChildChanges(path, listener);

  // listener should be in both ZkClient#_dataListener and ZkClient#_childListener set
  Map<String, Set<IZkDataListener>> dataListenerMap = ZkTestHelper.getZkDataListener(client);
  Assert.assertEquals(dataListenerMap.size(), 1, "ZkClient#_dataListener should have 1 listener");
  Set<IZkDataListener> dataListenerSet = dataListenerMap.get(path);
  Assert.assertNotNull(dataListenerSet, "ZkClient#_dataListener should have 1 listener on path: "
      + path);
  Assert.assertEquals(dataListenerSet.size(), 1,
      "ZkClient#_dataListener should have 1 listener on path: " + path);

  Map<String, Set<IZkChildListener>> childListenerMap = ZkTestHelper.getZkChildListener(client);
  Assert.assertEquals(childListenerMap.size(), 1,
      "ZkClient#_childListener should have 1 listener");
  Set<IZkChildListener> childListenerSet = childListenerMap.get(path);
  Assert.assertNotNull(childListenerSet,
      "ZkClient#_childListener should have 1 listener on path: " + path);
  Assert.assertEquals(childListenerSet.size(), 1,
      "ZkClient#_childListener should have 1 listener on path: " + path);

  // watch should be in ZooKeeper#watchManager#XXXWatches
  Map<String, List<String>> watchMap = ZkTestHelper.getZkWatch(client);
  // System.out.println("watchMap1: " + watchMap);
  List<String> dataWatch = watchMap.get("dataWatches");
  Assert.assertNotNull(dataWatch,
      "ZooKeeper#watchManager#dataWatches should have 1 data watch on path: " + path);
  Assert.assertEquals(dataWatch.size(), 1,
      "ZooKeeper#watchManager#dataWatches should have 1 data watch on path: " + path);
  Assert.assertEquals(dataWatch.get(0), path,
      "ZooKeeper#watchManager#dataWatches should have 1 data watch on path: " + path);

  List<String> childWatch = watchMap.get("childWatches");
  Assert.assertNotNull(childWatch,
      "ZooKeeper#watchManager#childWatches should have 1 child watch on path: " + path);
  Assert.assertEquals(childWatch.size(), 1,
      "ZooKeeper#watchManager#childWatches should have 1 child watch on path: " + path);
  Assert.assertEquals(childWatch.get(0), path,
      "ZooKeeper#watchManager#childWatches should have 1 child watch on path: " + path);

  client.unsubscribeDataChanges(path, listener);
  client.unsubscribeChildChanges(path, listener);
  // System.out.println("watchMap2: " + watchMap);
  ZkTestHelper.expireSession(client);

  // after session expiry, those watches should be removed
  watchMap = ZkTestHelper.getZkWatch(client);
  // System.out.println("watchMap3: " + watchMap);
  dataWatch = watchMap.get("dataWatches");
  Assert.assertTrue(dataWatch.isEmpty(), "ZooKeeper#watchManager#dataWatches should be empty");
  childWatch = watchMap.get("childWatches");
  Assert.assertTrue(childWatch.isEmpty(), "ZooKeeper#watchManager#childWatches should be empty");

  client.close();
}
 
开发者ID:apache,项目名称:helix,代码行数:79,代码来源:TestZkBasis.java

示例9: createHelixClusterIfNeeded

import org.apache.helix.manager.zk.ZkClient; //导入方法依赖的package包/类
public static void createHelixClusterIfNeeded(String helixClusterName, String zkPath, boolean isUpdateStateModel) {
  final HelixAdmin admin = new ZKHelixAdmin(zkPath);
  final String segmentStateModelName = PinotHelixSegmentOnlineOfflineStateModelGenerator.PINOT_SEGMENT_ONLINE_OFFLINE_STATE_MODEL;

  if (admin.getClusters().contains(helixClusterName)) {
    LOGGER.info("cluster already exists ********************************************* ");
    if (isUpdateStateModel) {
      final StateModelDefinition curStateModelDef = admin.getStateModelDef(helixClusterName, segmentStateModelName);
      List<String> states = curStateModelDef.getStatesPriorityList();
      if (states.contains(PinotHelixSegmentOnlineOfflineStateModelGenerator.CONSUMING_STATE)) {
        LOGGER.info("State model {} already updated to contain CONSUMING state", segmentStateModelName);
        return;
      } else {
        LOGGER.info("Updating {} to add states for low level kafka consumers", segmentStateModelName);
        StateModelDefinition newStateModelDef = PinotHelixSegmentOnlineOfflineStateModelGenerator.generatePinotStateModelDefinition();
        ZkClient zkClient = new ZkClient(zkPath);
        zkClient.waitUntilConnected(20, TimeUnit.SECONDS);
        zkClient.setZkSerializer(new ZNRecordSerializer());
        HelixDataAccessor accessor = new ZKHelixDataAccessor(helixClusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
        PropertyKey.Builder keyBuilder = accessor.keyBuilder();
        accessor.setProperty(keyBuilder.stateModelDef(segmentStateModelName), newStateModelDef);
        LOGGER.info("Completed updating statemodel {}", segmentStateModelName);
        zkClient.close();
      }
    }
    return;
  }

  LOGGER.info("Creating a new cluster, as the helix cluster : " + helixClusterName
      + " was not found ********************************************* ");
  admin.addCluster(helixClusterName, false);

  LOGGER.info("Enable auto join.");
  final HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(helixClusterName).build();

  final Map<String, String> props = new HashMap<String, String>();
  props.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, String.valueOf(true));
  //we need only one segment to be loaded at a time
  props.put(MessageType.STATE_TRANSITION + "." + HelixTaskExecutor.MAX_THREADS, String.valueOf(1));

  admin.setConfig(scope, props);

  LOGGER.info("Adding state model {} (with CONSUMED state) generated using {} **********************************************",
      segmentStateModelName , PinotHelixSegmentOnlineOfflineStateModelGenerator.class.toString());

  // If this is a fresh cluster we are creating, then the cluster will see the CONSUMING state in the
  // state model. But then the servers will never be asked to go to that STATE (whether they have the code
  // to handle it or not) unil we complete the feature using low-level kafka consumers and turn the feature on.
  admin.addStateModelDef(helixClusterName, segmentStateModelName,
      PinotHelixSegmentOnlineOfflineStateModelGenerator.generatePinotStateModelDefinition());

  LOGGER.info("Adding state model definition named : "
      + PinotHelixBrokerResourceOnlineOfflineStateModelGenerator.PINOT_BROKER_RESOURCE_ONLINE_OFFLINE_STATE_MODEL
      + " generated using : " + PinotHelixBrokerResourceOnlineOfflineStateModelGenerator.class.toString()
      + " ********************************************** ");

  admin.addStateModelDef(helixClusterName,
      PinotHelixBrokerResourceOnlineOfflineStateModelGenerator.PINOT_BROKER_RESOURCE_ONLINE_OFFLINE_STATE_MODEL,
      PinotHelixBrokerResourceOnlineOfflineStateModelGenerator.generatePinotStateModelDefinition());

  LOGGER.info("Adding empty ideal state for Broker!");
  HelixHelper.updateResourceConfigsFor(new HashMap<String, String>(), CommonConstants.Helix.BROKER_RESOURCE_INSTANCE,
      helixClusterName, admin);
  IdealState idealState =
      PinotTableIdealStateBuilder.buildEmptyIdealStateForBrokerResource(admin, helixClusterName);
  admin.setResourceIdealState(helixClusterName, CommonConstants.Helix.BROKER_RESOURCE_INSTANCE, idealState);
  initPropertyStorePath(helixClusterName, zkPath);
  LOGGER.info("New Cluster setup completed... ********************************************** ");
}
 
开发者ID:linkedin,项目名称:pinot,代码行数:71,代码来源:HelixSetupUtils.java


注:本文中的org.apache.helix.manager.zk.ZkClient.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。