@Override public HiveServer2Instance getLeader() { for (HiveServer2Instance hs2Instance : getAll()) { if (hs2Instance.isLeader()) { return hs2Instance; } } return null; }
static HS2ActivePassiveHARegistry create(Configuration conf, boolean isClient) { String zkNameSpace = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE); Preconditions.checkArgument(!StringUtils.isBlank(zkNameSpace), HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE.varname + " cannot be null or empty"); String principal = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_KERBEROS_PRINCIPAL); String keytab = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_KERBEROS_KEYTAB); String zkNameSpacePrefix = zkNameSpace + "-"; return new HS2ActivePassiveHARegistry(null, zkNameSpacePrefix, LEADER_LATCH_PATH, principal, keytab, isClient ? null : SASL_LOGIN_CONTEXT_NAME, conf, isClient); }
private void addActiveEndpointToServiceRecord() throws IOException { addEndpointToServiceRecord(getNewServiceRecord(), ACTIVE_ENDPOINT); }
private void populateCache() throws IOException { PathChildrenCache pcc = ensureInstancesCache(0); populateCache(pcc, false); }
/** * Helper method to get a HiveServer2HARegistry instance to read from the registry. Only used by clients (JDBC), * service discovery to connect to active HS2 instance in Active/Passive HA configuration. * * @param conf {@link Configuration} instance which contains service registry information. * @return HiveServer2HARegistry */ public static synchronized HS2ActivePassiveHARegistry getClient(Configuration conf) throws IOException { String namespace = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE); Preconditions.checkArgument(!StringUtils.isBlank(namespace), HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE.varname + " cannot be null or empty"); String nsKey = ZkRegistryBase.getRootNamespace(null, namespace + "-"); HS2ActivePassiveHARegistry registry = hs2Registries.get(nsKey); if (registry == null) { registry = HS2ActivePassiveHARegistry.create(conf, true); registry.start(); hs2Registries.put(nsKey, registry); LOG.info("Added registry client to cache with namespace: {}", nsKey); } else { LOG.info("Returning cached registry client for namespace: {}", nsKey); } return registry; } }
@Override public void failover() throws Exception { if (hasLeadership()) { LOG.info("Failover request received for HS2 instance: {}. Restarting leader latch..", uniqueId); leaderLatch.close(LeaderLatch.CloseMode.NOTIFY_LEADER); leaderLatch = getNewLeaderLatchPath(); // re-attach all registered listeners for (Map.Entry<LeaderLatchListener, ExecutorService> registeredListener : registeredListeners.entrySet()) { if (registeredListener.getValue() == null) { leaderLatch.addListener(registeredListener.getKey()); } else { leaderLatch.addListener(registeredListener.getKey(), registeredListener.getValue()); } } leaderLatch.start(); LOG.info("Failover complete. Leader latch restarted successfully. New leader: {}", leaderLatch.getLeader().getId()); } else { LOG.warn("Failover request received for HS2 instance: {} that is not leader. Skipping..", uniqueId); } }
@Override public void failover() throws Exception { hs2HARegistry.failover(); } }
private ServiceRecord getNewServiceRecord() { ServiceRecord srv = new ServiceRecord(); final Map<String, String> confsToPublish = getConfsToPublish(); for (Map.Entry<String, String> entry : confsToPublish.entrySet()) { srv.set(entry.getKey(), entry.getValue()); } return srv; } }
@Override public Collection<HiveServer2Instance> getAll() { return getAllInternal(); }
@Override public Set<HiveServer2Instance> getByHost(final String host) { return getByHostInternal(host); }
private HS2ActivePassiveHARegistry(final String instanceName, final String zkNamespacePrefix, final String leaderLatchPath, final String krbPrincipal, final String krbKeytab, final String saslContextName, final Configuration conf, final boolean isClient) { super(instanceName, conf, null, zkNamespacePrefix, null, INSTANCE_PREFIX, INSTANCE_GROUP, saslContextName, krbPrincipal, krbKeytab, null); this.isClient = isClient; if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_IN_TEST) && conf.get(ZkRegistryBase.UNIQUE_IDENTIFIER) != null) { this.uniqueId = conf.get(ZkRegistryBase.UNIQUE_IDENTIFIER); } else { this.uniqueId = UNIQUE_ID.toString(); } this.latchPath = leaderLatchPath; this.leaderLatch = getNewLeaderLatchPath(); }
leaderActionsExecutorService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(true) .setNameFormat("Leader Actions Handler Thread").build()); hs2HARegistry = HS2ActivePassiveHARegistry.create(hiveConf, false);
private void populateCache() throws IOException { PathChildrenCache pcc = ensureInstancesCache(0); populateCache(pcc, false); }
@Override public void failover() throws Exception { if (hasLeadership()) { LOG.info("Failover request received for HS2 instance: {}. Restarting leader latch..", uniqueId); leaderLatch.close(LeaderLatch.CloseMode.NOTIFY_LEADER); leaderLatch = getNewLeaderLatchPath(); // re-attach all registered listeners for (Map.Entry<LeaderLatchListener, ExecutorService> registeredListener : registeredListeners.entrySet()) { if (registeredListener.getValue() == null) { leaderLatch.addListener(registeredListener.getKey()); } else { leaderLatch.addListener(registeredListener.getKey(), registeredListener.getValue()); } } leaderLatch.start(); LOG.info("Failover complete. Leader latch restarted successfully. New leader: {}", leaderLatch.getLeader().getId()); } else { LOG.warn("Failover request received for HS2 instance: {} that is not leader. Skipping..", uniqueId); } }
/** * Helper method to get a HiveServer2HARegistry instance to read from the registry. Only used by clients (JDBC), * service discovery to connect to active HS2 instance in Active/Passive HA configuration. * * @param conf {@link Configuration} instance which contains service registry information. * @return HiveServer2HARegistry */ public static synchronized HS2ActivePassiveHARegistry getClient(Configuration conf) throws IOException { String namespace = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE); Preconditions.checkArgument(!StringUtils.isBlank(namespace), HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE.varname + " cannot be null or empty"); String nsKey = ZkRegistryBase.getRootNamespace(null, namespace + "-"); HS2ActivePassiveHARegistry registry = hs2Registries.get(nsKey); if (registry == null) { registry = HS2ActivePassiveHARegistry.create(conf, true); registry.start(); hs2Registries.put(nsKey, registry); LOG.info("Added registry client to cache with namespace: {}", nsKey); } else { LOG.info("Returning cached registry client for namespace: {}", nsKey); } return registry; } }
@Override public void failover() throws Exception { hs2HARegistry.failover(); } }
private ServiceRecord getNewServiceRecord() { ServiceRecord srv = new ServiceRecord(); final Map<String, String> confsToPublish = getConfsToPublish(); for (Map.Entry<String, String> entry : confsToPublish.entrySet()) { srv.set(entry.getKey(), entry.getValue()); } return srv; } }
@Override public Collection<HiveServer2Instance> getAll() { return getAllInternal(); }
@Override public Set<HiveServer2Instance> getByHost(final String host) { return getByHostInternal(host); }
private HS2ActivePassiveHARegistry(final String instanceName, final String zkNamespacePrefix, final String leaderLatchPath, final String krbPrincipal, final String krbKeytab, final String saslContextName, final Configuration conf, final boolean isClient) { super(instanceName, conf, null, zkNamespacePrefix, null, INSTANCE_PREFIX, INSTANCE_GROUP, saslContextName, krbPrincipal, krbKeytab, null); this.isClient = isClient; if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_IN_TEST) && conf.get(ZkRegistryBase.UNIQUE_IDENTIFIER) != null) { this.uniqueId = conf.get(ZkRegistryBase.UNIQUE_IDENTIFIER); } else { this.uniqueId = UNIQUE_ID.toString(); } this.latchPath = leaderLatchPath; this.leaderLatch = getNewLeaderLatchPath(); }