http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/task/ClearClientCache.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/task/ClearClientCache.java b/helix-ui/src/main/java/org/apache/helix/ui/task/ClearClientCache.java index d30a479..87afba0 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/task/ClearClientCache.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/task/ClearClientCache.java @@ -1,5 +1,24 @@ package org.apache.helix.ui.task; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import com.google.common.collect.ImmutableMultimap; import io.dropwizard.servlets.tasks.Task; import org.apache.helix.ui.util.ClientCache; @@ -7,19 +26,19 @@ import org.apache.helix.ui.util.ClientCache; import java.io.PrintWriter; public class ClearClientCache extends Task { - private final ClientCache clientCache; + private final ClientCache clientCache; - public ClearClientCache(ClientCache clientCache) { - super("clearClientCache"); - this.clientCache = clientCache; - } + public ClearClientCache(ClientCache clientCache) { + super("clearClientCache"); + this.clientCache = clientCache; + } - @Override - public void execute(ImmutableMultimap<String, String> params, PrintWriter printWriter) throws Exception { - printWriter.println("Clearing ZK connections ..."); - printWriter.flush(); - clientCache.invalidateAll(); - printWriter.println("Done!"); - printWriter.flush(); - } + @Override + public void execute(ImmutableMultimap<String, String> params, PrintWriter printWriter) throws Exception { + printWriter.println("Clearing ZK connections ..."); + printWriter.flush(); + clientCache.invalidateAll(); + printWriter.println("Done!"); + printWriter.flush(); + } }
http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/task/ClearDataCacheTask.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/task/ClearDataCacheTask.java b/helix-ui/src/main/java/org/apache/helix/ui/task/ClearDataCacheTask.java index fd5661f..af524d3 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/task/ClearDataCacheTask.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/task/ClearDataCacheTask.java @@ -1,5 +1,24 @@ package org.apache.helix.ui.task; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import com.google.common.collect.ImmutableMultimap; import io.dropwizard.servlets.tasks.Task; import org.apache.helix.ui.util.DataCache; @@ -7,19 +26,19 @@ import org.apache.helix.ui.util.DataCache; import java.io.PrintWriter; public class ClearDataCacheTask extends Task { - private final DataCache dataCache; + private final DataCache dataCache; - public ClearDataCacheTask(DataCache dataCache) { - super("clearDataCache"); - this.dataCache = dataCache; - } + public ClearDataCacheTask(DataCache dataCache) { + super("clearDataCache"); + this.dataCache = dataCache; + } - @Override - public void execute(ImmutableMultimap<String, String> params, PrintWriter printWriter) throws Exception { - printWriter.println("Clearing data caches ..."); - printWriter.flush(); - dataCache.invalidate(); - printWriter.println("Done!"); - printWriter.flush(); - } + @Override + public void execute(ImmutableMultimap<String, String> params, PrintWriter printWriter) throws Exception { + printWriter.println("Clearing data caches ..."); + printWriter.flush(); + dataCache.invalidate(); + printWriter.println("Done!"); + printWriter.flush(); + } } http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/util/ClientCache.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/util/ClientCache.java b/helix-ui/src/main/java/org/apache/helix/ui/util/ClientCache.java index 968e26c..f461277 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/util/ClientCache.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/util/ClientCache.java @@ -1,7 +1,25 @@ package org.apache.helix.ui.util; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import com.google.common.cache.*; -import org.I0Itec.zkclient.exception.ZkTimeoutException; import org.apache.helix.manager.zk.ZNRecordSerializer; import org.apache.helix.manager.zk.ZkClient; import org.apache.helix.ui.api.ClusterConnection; @@ -18,83 +36,83 @@ import java.util.Set; import java.util.concurrent.TimeUnit; public class ClientCache { - private static final Logger LOG = LoggerFactory.getLogger(ClientCache.class); - private static final int DEFAULT_SESSION_TIMEOUT_MILLIS = 5000; - private static final int DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000; + private static final Logger LOG = LoggerFactory.getLogger(ClientCache.class); + private static final int DEFAULT_SESSION_TIMEOUT_MILLIS = 5000; + private static final int DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000; - private final ZkAddressValidator zkAddressValidator; + private final ZkAddressValidator zkAddressValidator; - public ClientCache(ZkAddressValidator zkAddressValidator) { - this.zkAddressValidator = zkAddressValidator; - } + public ClientCache(ZkAddressValidator zkAddressValidator) { + this.zkAddressValidator = zkAddressValidator; + } - // Manages and caches lifecycle of connections to ZK - final LoadingCache<String, ClusterConnection> clientCache = CacheBuilder.newBuilder() - .maximumSize(3) - .expireAfterAccess(5, TimeUnit.MINUTES) - .removalListener(new RemovalListener<String, ClusterConnection>() { - @Override - public void onRemoval(RemovalNotification<String, ClusterConnection> removalNotification) { - if (removalNotification.getValue() != null) { - ZkClient zkClient = removalNotification.getValue().getZkClient(); - if (zkClient != null) { - zkClient.close(); - LOG.info("Disconnected from {}", removalNotification.getKey()); - } - } - } - }) - .build(new CacheLoader<String, ClusterConnection>() { - @Override - public ClusterConnection load(String zkAddress) throws Exception { - ZkClient zkClient = new ZkClient( - zkAddress, - DEFAULT_SESSION_TIMEOUT_MILLIS, - DEFAULT_CONNECTION_TIMEOUT_MILLIS, - new ZNRecordSerializer()); - zkClient.waitUntilConnected(); - LOG.info("Connected to {}", zkAddress); - return new ClusterConnection(zkClient); + // Manages and caches lifecycle of connections to ZK + final LoadingCache<String, ClusterConnection> clientCache = CacheBuilder.newBuilder() + .maximumSize(3) + .expireAfterAccess(5, TimeUnit.MINUTES) + .removalListener(new RemovalListener<String, ClusterConnection>() { + @Override + public void onRemoval(RemovalNotification<String, ClusterConnection> removalNotification) { + if (removalNotification.getValue() != null) { + ZkClient zkClient = removalNotification.getValue().getZkClient(); + if (zkClient != null) { + zkClient.close(); + LOG.info("Disconnected from {}", removalNotification.getKey()); } - }); - - public ClusterConnection get(String zkAddress) { - try { - zkAddress = URLDecoder.decode(zkAddress, "UTF-8"); - } catch (Exception e) { - throw new IllegalArgumentException(e); - } - - if (!zkAddressValidator.validate(zkAddress)) { - throw new WebApplicationException("Cannot access " + zkAddress, Response.Status.UNAUTHORIZED); - } + } + } + }) + .build(new CacheLoader<String, ClusterConnection>() { + @Override + public ClusterConnection load(String zkAddress) throws Exception { + ZkClient zkClient = new ZkClient( + zkAddress, + DEFAULT_SESSION_TIMEOUT_MILLIS, + DEFAULT_CONNECTION_TIMEOUT_MILLIS, + new ZNRecordSerializer()); + zkClient.waitUntilConnected(); + LOG.info("Connected to {}", zkAddress); + return new ClusterConnection(zkClient); + } + }); - ClusterConnection clusterConnection; - try { - clusterConnection = clientCache.get(zkAddress); - } catch (Exception e) { - throw new WebApplicationException(e, Response.Status.GATEWAY_TIMEOUT); - } + public ClusterConnection get(String zkAddress) { + try { + zkAddress = URLDecoder.decode(zkAddress, "UTF-8"); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } - if (!clusterConnection.getZkClient().getConnection().getZookeeperState().equals(ZooKeeper.States.CONNECTED)) { - clientCache.invalidate(zkAddress); - throw new WebApplicationException("ZooKeeper connection was dead", Response.Status.GATEWAY_TIMEOUT); - } + if (!zkAddressValidator.validate(zkAddress)) { + throw new WebApplicationException("Cannot access " + zkAddress, Response.Status.UNAUTHORIZED); + } - return clusterConnection; + ClusterConnection clusterConnection; + try { + clusterConnection = clientCache.get(zkAddress); + } catch (Exception e) { + throw new WebApplicationException(e, Response.Status.GATEWAY_TIMEOUT); } - public void invalidateAll() { - clientCache.invalidateAll(); + if (!clusterConnection.getZkClient().getConnection().getZookeeperState().equals(ZooKeeper.States.CONNECTED)) { + clientCache.invalidate(zkAddress); + throw new WebApplicationException("ZooKeeper connection was dead", Response.Status.GATEWAY_TIMEOUT); } - public Set<String> getDeadConnections() { - Set<String> deadConnections = new HashSet<String>(); - for (Map.Entry<String, ClusterConnection> entry : clientCache.asMap().entrySet()) { - if (!entry.getValue().getZkClient().getConnection().getZookeeperState().isAlive()) { - deadConnections.add(entry.getKey()); - } - } - return deadConnections; + return clusterConnection; + } + + public void invalidateAll() { + clientCache.invalidateAll(); + } + + public Set<String> getDeadConnections() { + Set<String> deadConnections = new HashSet<String>(); + for (Map.Entry<String, ClusterConnection> entry : clientCache.asMap().entrySet()) { + if (!entry.getValue().getZkClient().getConnection().getZookeeperState().isAlive()) { + deadConnections.add(entry.getKey()); + } } + return deadConnections; + } } http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/util/DataCache.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/util/DataCache.java b/helix-ui/src/main/java/org/apache/helix/ui/util/DataCache.java index c96a718..1441006 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/util/DataCache.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/util/DataCache.java @@ -1,5 +1,24 @@ package org.apache.helix.ui.util; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -16,182 +35,179 @@ import java.util.*; import java.util.concurrent.TimeUnit; public class DataCache { - private static final Logger LOG = LoggerFactory.getLogger(DataCache.class); - private static final int CACHE_EXPIRY_TIME = 30; - private static final TimeUnit CACHE_EXPIRY_UNIT = TimeUnit.SECONDS; - - private final LoadingCache<String, List<String>> clusterCache; - private final LoadingCache<ClusterSpec, List<String>> resourceCache; - private final LoadingCache<ClusterSpec, List<ConfigTableRow>> configCache; - private final LoadingCache<ResourceSpec, List<ConfigTableRow>> resourceConfigCache; - private final LoadingCache<ClusterSpec, List<InstanceSpec>> instanceCache; - - public DataCache(final ClientCache clientCache) { - this.clusterCache = CacheBuilder.newBuilder() - .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) - .build(new CacheLoader<String, List<String>>() { - @Override - public List<String> load(String zkAddress) throws Exception { - ZkClient zkClient = clientCache.get(zkAddress).getZkClient(); - List<String> clusters = zkClient.getChildren("/"); - Collections.sort(clusters); - return clusters; - } - }); - - this.resourceCache = CacheBuilder.newBuilder() - .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) - .build(new CacheLoader<ClusterSpec, List<String>>() { - @Override - public List<String> load(ClusterSpec clusterSpec) throws Exception { - ClusterSetup clusterSetup = clientCache.get(clusterSpec.getZkAddress()).getClusterSetup(); - List<String> resources = new ArrayList<String>(clusterSetup.getClusterManagementTool().getResourcesInCluster(clusterSpec.getClusterName())); - Collections.sort(resources); - return resources; - } - }); - - this.configCache = CacheBuilder.newBuilder() - .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) - .build(new CacheLoader<ClusterSpec, List<ConfigTableRow>>() { - @Override - public List<ConfigTableRow> load(ClusterSpec clusterSpec) throws Exception { - ClusterSetup clusterSetup = clientCache.get(clusterSpec.getZkAddress()).getClusterSetup(); - List<ConfigTableRow> configTable = new ArrayList<ConfigTableRow>(); - - // Cluster config - HelixConfigScope configScope - = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.CLUSTER) - .forCluster(clusterSpec.getClusterName()).build(); - List<String> clusterConfigKeys - = clusterSetup.getClusterManagementTool().getConfigKeys(configScope); - Map<String, String> config - = clusterSetup.getClusterManagementTool().getConfig(configScope, clusterConfigKeys); - for (Map.Entry<String, String> entry : config.entrySet()) { - configTable.add(new ConfigTableRow( - HelixConfigScope.ConfigScopeProperty.CLUSTER.toString(), - clusterSpec.getClusterName(), - entry.getKey(), - entry.getValue())); - } - - Collections.sort(configTable); - - return configTable; - } - }); - - this.resourceConfigCache = CacheBuilder.newBuilder() - .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) - .build(new CacheLoader<ResourceSpec, List<ConfigTableRow>>() { - @Override - public List<ConfigTableRow> load(ResourceSpec resourceSpec) throws Exception { - ClusterSetup clusterSetup = clientCache.get(resourceSpec.getZkAddress()).getClusterSetup(); - List<ConfigTableRow> configTable = new ArrayList<ConfigTableRow>(); - - HelixConfigScope configScope = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.RESOURCE) - .forCluster(resourceSpec.getClusterName()) - .forResource(resourceSpec.getResourceName()) - .build(); - try - { - List<String> clusterConfigKeys = clusterSetup.getClusterManagementTool().getConfigKeys(configScope); - - Map<String, String> config = clusterSetup.getClusterManagementTool().getConfig(configScope, clusterConfigKeys); - - if (config != null) { - for (Map.Entry<String, String> entry : config.entrySet()) { - configTable.add(new ConfigTableRow( - HelixConfigScope.ConfigScopeProperty.RESOURCE.toString(), - resourceSpec.getClusterName(), - entry.getKey(), - entry.getValue())); - } - } - } - catch (Exception e) - { - LOG.warn("Could not get resource config for {}", resourceSpec.getResourceName(), e); - } - - return configTable; + private static final Logger LOG = LoggerFactory.getLogger(DataCache.class); + private static final int CACHE_EXPIRY_TIME = 30; + private static final TimeUnit CACHE_EXPIRY_UNIT = TimeUnit.SECONDS; + + private final LoadingCache<String, List<String>> clusterCache; + private final LoadingCache<ClusterSpec, List<String>> resourceCache; + private final LoadingCache<ClusterSpec, List<ConfigTableRow>> configCache; + private final LoadingCache<ResourceSpec, List<ConfigTableRow>> resourceConfigCache; + private final LoadingCache<ClusterSpec, List<InstanceSpec>> instanceCache; + + public DataCache(final ClientCache clientCache) { + this.clusterCache = CacheBuilder.newBuilder() + .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) + .build(new CacheLoader<String, List<String>>() { + @Override + public List<String> load(String zkAddress) throws Exception { + ZkClient zkClient = clientCache.get(zkAddress).getZkClient(); + List<String> clusters = zkClient.getChildren("/"); + Collections.sort(clusters); + return clusters; + } + }); + + this.resourceCache = CacheBuilder.newBuilder() + .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) + .build(new CacheLoader<ClusterSpec, List<String>>() { + @Override + public List<String> load(ClusterSpec clusterSpec) throws Exception { + ClusterSetup clusterSetup = clientCache.get(clusterSpec.getZkAddress()).getClusterSetup(); + List<String> resources = new ArrayList<String>(clusterSetup.getClusterManagementTool().getResourcesInCluster(clusterSpec.getClusterName())); + Collections.sort(resources); + return resources; + } + }); + + this.configCache = CacheBuilder.newBuilder() + .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) + .build(new CacheLoader<ClusterSpec, List<ConfigTableRow>>() { + @Override + public List<ConfigTableRow> load(ClusterSpec clusterSpec) throws Exception { + ClusterSetup clusterSetup = clientCache.get(clusterSpec.getZkAddress()).getClusterSetup(); + List<ConfigTableRow> configTable = new ArrayList<ConfigTableRow>(); + + // Cluster config + HelixConfigScope configScope + = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.CLUSTER) + .forCluster(clusterSpec.getClusterName()).build(); + List<String> clusterConfigKeys + = clusterSetup.getClusterManagementTool().getConfigKeys(configScope); + Map<String, String> config + = clusterSetup.getClusterManagementTool().getConfig(configScope, clusterConfigKeys); + for (Map.Entry<String, String> entry : config.entrySet()) { + configTable.add(new ConfigTableRow( + HelixConfigScope.ConfigScopeProperty.CLUSTER.toString(), + clusterSpec.getClusterName(), + entry.getKey(), + entry.getValue())); + } + + Collections.sort(configTable); + + return configTable; + } + }); + + this.resourceConfigCache = CacheBuilder.newBuilder() + .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) + .build(new CacheLoader<ResourceSpec, List<ConfigTableRow>>() { + @Override + public List<ConfigTableRow> load(ResourceSpec resourceSpec) throws Exception { + ClusterSetup clusterSetup = clientCache.get(resourceSpec.getZkAddress()).getClusterSetup(); + List<ConfigTableRow> configTable = new ArrayList<ConfigTableRow>(); + + HelixConfigScope configScope = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.RESOURCE) + .forCluster(resourceSpec.getClusterName()) + .forResource(resourceSpec.getResourceName()) + .build(); + try { + List<String> clusterConfigKeys = clusterSetup.getClusterManagementTool().getConfigKeys(configScope); + + Map<String, String> config = clusterSetup.getClusterManagementTool().getConfig(configScope, clusterConfigKeys); + + if (config != null) { + for (Map.Entry<String, String> entry : config.entrySet()) { + configTable.add(new ConfigTableRow( + HelixConfigScope.ConfigScopeProperty.RESOURCE.toString(), + resourceSpec.getClusterName(), + entry.getKey(), + entry.getValue())); } - }); - - this.instanceCache = CacheBuilder.newBuilder() - .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) - .build(new CacheLoader<ClusterSpec, List<InstanceSpec>>() { - @Override - public List<InstanceSpec> load(ClusterSpec clusterSpec) throws Exception { - ClusterConnection clusterConnection = clientCache.get(clusterSpec.getZkAddress()); - - // Instances in the cluster - List<String> instances = - clusterConnection.getClusterSetup().getClusterManagementTool().getInstancesInCluster(clusterSpec.getClusterName()); - - // Live instances in the cluster - // TODO: should be able to use clusterSetup for this, but no method available - List<String> liveInstances - = clusterConnection.getZkClient().getChildren(String.format("/%s/LIVEINSTANCES", clusterSpec.getClusterName())); - Set<String> liveInstanceSet = new HashSet<String>(); - if (liveInstances != null) { - liveInstanceSet.addAll(liveInstances); - } - - // Enabled instances - Set<String> enabledInstances = new HashSet<String>(); - if (instances != null) { - for (String instance : instances) { - InstanceConfig instanceConfig = clusterConnection.getClusterSetup() - .getClusterManagementTool() - .getInstanceConfig(clusterSpec.getClusterName(), instance); - if (instanceConfig.getInstanceEnabled()) { - enabledInstances.add(instance); - } - } - } - - // Rows - List<InstanceSpec> instanceSpecs = new ArrayList<InstanceSpec>(); - if (instances != null) { - for (String instance : instances) { - instanceSpecs.add(new InstanceSpec( - instance, - enabledInstances.contains(instance), - liveInstanceSet.contains(instance))); - } - } - - return instanceSpecs; + } + } catch (Exception e) { + LOG.warn("Could not get resource config for {}", resourceSpec.getResourceName(), e); + } + + return configTable; + } + }); + + this.instanceCache = CacheBuilder.newBuilder() + .expireAfterWrite(CACHE_EXPIRY_TIME, CACHE_EXPIRY_UNIT) + .build(new CacheLoader<ClusterSpec, List<InstanceSpec>>() { + @Override + public List<InstanceSpec> load(ClusterSpec clusterSpec) throws Exception { + ClusterConnection clusterConnection = clientCache.get(clusterSpec.getZkAddress()); + + // Instances in the cluster + List<String> instances = + clusterConnection.getClusterSetup().getClusterManagementTool().getInstancesInCluster(clusterSpec.getClusterName()); + + // Live instances in the cluster + // TODO: should be able to use clusterSetup for this, but no method available + List<String> liveInstances + = clusterConnection.getZkClient().getChildren(String.format("/%s/LIVEINSTANCES", clusterSpec.getClusterName())); + Set<String> liveInstanceSet = new HashSet<String>(); + if (liveInstances != null) { + liveInstanceSet.addAll(liveInstances); + } + + // Enabled instances + Set<String> enabledInstances = new HashSet<String>(); + if (instances != null) { + for (String instance : instances) { + InstanceConfig instanceConfig = clusterConnection.getClusterSetup() + .getClusterManagementTool() + .getInstanceConfig(clusterSpec.getClusterName(), instance); + if (instanceConfig.getInstanceEnabled()) { + enabledInstances.add(instance); } - }); - } - - public void invalidate() { - clusterCache.invalidateAll(); - resourceCache.invalidateAll(); - configCache.invalidateAll(); - resourceConfigCache.invalidateAll(); - instanceCache.invalidateAll(); - } - - public LoadingCache<String, List<String>> getClusterCache() { - return clusterCache; - } - - public LoadingCache<ClusterSpec, List<String>> getResourceCache() { - return resourceCache; - } - - public LoadingCache<ClusterSpec, List<ConfigTableRow>> getConfigCache() { - return configCache; - } - - public LoadingCache<ResourceSpec, List<ConfigTableRow>> getResourceConfigCache() { - return resourceConfigCache; - } - - public LoadingCache<ClusterSpec, List<InstanceSpec>> getInstanceCache() { - return instanceCache; - } + } + } + + // Rows + List<InstanceSpec> instanceSpecs = new ArrayList<InstanceSpec>(); + if (instances != null) { + for (String instance : instances) { + instanceSpecs.add(new InstanceSpec( + instance, + enabledInstances.contains(instance), + liveInstanceSet.contains(instance))); + } + } + + return instanceSpecs; + } + }); + } + + public void invalidate() { + clusterCache.invalidateAll(); + resourceCache.invalidateAll(); + configCache.invalidateAll(); + resourceConfigCache.invalidateAll(); + instanceCache.invalidateAll(); + } + + public LoadingCache<String, List<String>> getClusterCache() { + return clusterCache; + } + + public LoadingCache<ClusterSpec, List<String>> getResourceCache() { + return resourceCache; + } + + public LoadingCache<ClusterSpec, List<ConfigTableRow>> getConfigCache() { + return configCache; + } + + public LoadingCache<ResourceSpec, List<ConfigTableRow>> getResourceConfigCache() { + return resourceConfigCache; + } + + public LoadingCache<ClusterSpec, List<InstanceSpec>> getInstanceCache() { + return instanceCache; + } } http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/util/DropWizardApplicationRunner.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/util/DropWizardApplicationRunner.java b/helix-ui/src/main/java/org/apache/helix/ui/util/DropWizardApplicationRunner.java index fb27130..24de1b8 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/util/DropWizardApplicationRunner.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/util/DropWizardApplicationRunner.java @@ -1,12 +1,30 @@ package org.apache.helix.ui.util; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import io.dropwizard.Application; import io.dropwizard.Configuration; import io.dropwizard.cli.ServerCommand; import io.dropwizard.configuration.ConfigurationFactory; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; - import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.component.AbstractLifeCycle; import org.eclipse.jetty.util.component.LifeCycle; @@ -17,70 +35,70 @@ import java.io.File; * A utility to run DropWizard (http://dropwizard.io/) applications in-process. */ public class DropWizardApplicationRunner { - /** - * Creates a Jetty server for an application that can be started / stopped in-process - * - * @param config An application configuration instance (with properties set) - * @param applicationClass The {@link io.dropwizard.Application} implementation class - * @param <T> The configuration class - * @return A Jetty server - */ - @SuppressWarnings("unchecked") - public static <T extends Configuration> - Server createServer(T config, Class<? extends Application<T>> applicationClass) throws Exception { - // Create application - final Application<T> application = applicationClass.getConstructor().newInstance(); + /** + * Creates a Jetty server for an application that can be started / stopped in-process + * + * @param config An application configuration instance (with properties set) + * @param applicationClass The {@link io.dropwizard.Application} implementation class + * @param <T> The configuration class + * @return A Jetty server + */ + @SuppressWarnings("unchecked") + public static <T extends Configuration> + Server createServer(T config, Class<? extends Application<T>> applicationClass) throws Exception { + // Create application + final Application<T> application = applicationClass.getConstructor().newInstance(); - // Create bootstrap - final ServerCommand<T> serverCommand = new ServerCommand<T>(application); - final Bootstrap<T> bootstrap = new Bootstrap<T>(application); - bootstrap.addCommand(serverCommand); - application.initialize(bootstrap); + // Create bootstrap + final ServerCommand<T> serverCommand = new ServerCommand<T>(application); + final Bootstrap<T> bootstrap = new Bootstrap<T>(application); + bootstrap.addCommand(serverCommand); + application.initialize(bootstrap); - // Write a temporary config file - File tmpConfigFile = new File( - System.getProperty("java.io.tmpdir"), - config.getClass().getCanonicalName() + "_" + System.currentTimeMillis()); - tmpConfigFile.deleteOnExit(); - bootstrap.getObjectMapper().writeValue(tmpConfigFile, config); + // Write a temporary config file + File tmpConfigFile = new File( + System.getProperty("java.io.tmpdir"), + config.getClass().getCanonicalName() + "_" + System.currentTimeMillis()); + tmpConfigFile.deleteOnExit(); + bootstrap.getObjectMapper().writeValue(tmpConfigFile, config); - // Parse configuration - ConfigurationFactory<T> configurationFactory - = bootstrap.getConfigurationFactoryFactory() - .create((Class<T>) config.getClass(), - bootstrap.getValidatorFactory().getValidator(), - bootstrap.getObjectMapper(), - "dw"); - final T builtConfig = configurationFactory.build( - bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath()); + // Parse configuration + ConfigurationFactory<T> configurationFactory + = bootstrap.getConfigurationFactoryFactory() + .create((Class<T>) config.getClass(), + bootstrap.getValidatorFactory().getValidator(), + bootstrap.getObjectMapper(), + "dw"); + final T builtConfig = configurationFactory.build( + bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath()); - // Configure logging - builtConfig.getLoggingFactory() - .configure(bootstrap.getMetricRegistry(), - bootstrap.getApplication().getName()); + // Configure logging + builtConfig.getLoggingFactory() + .configure(bootstrap.getMetricRegistry(), + bootstrap.getApplication().getName()); - // Environment - final Environment environment = new Environment(bootstrap.getApplication().getName(), - bootstrap.getObjectMapper(), - bootstrap.getValidatorFactory().getValidator(), - bootstrap.getMetricRegistry(), - bootstrap.getClassLoader()); + // Environment + final Environment environment = new Environment(bootstrap.getApplication().getName(), + bootstrap.getObjectMapper(), + bootstrap.getValidatorFactory().getValidator(), + bootstrap.getMetricRegistry(), + bootstrap.getClassLoader()); - // Initialize environment - builtConfig.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry()); - bootstrap.run(builtConfig, environment); - application.run(builtConfig, environment); + // Initialize environment + builtConfig.getMetricsFactory().configure(environment.lifecycle(), bootstrap.getMetricRegistry()); + bootstrap.run(builtConfig, environment); + application.run(builtConfig, environment); - // Server - final Server server = builtConfig.getServerFactory().build(environment); - server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() { - @Override - public void lifeCycleStopped(LifeCycle event) { - builtConfig.getLoggingFactory().stop(); - } - }); + // Server + final Server server = builtConfig.getServerFactory().build(environment); + server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() { + @Override + public void lifeCycleStopped(LifeCycle event) { + builtConfig.getLoggingFactory().stop(); + } + }); - return server; - } + return server; + } } http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/util/ZkAddressValidator.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/util/ZkAddressValidator.java b/helix-ui/src/main/java/org/apache/helix/ui/util/ZkAddressValidator.java index a77ee3b..6b97535 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/util/ZkAddressValidator.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/util/ZkAddressValidator.java @@ -1,37 +1,56 @@ package org.apache.helix.ui.util; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import java.util.HashSet; import java.util.Set; public class ZkAddressValidator { - private final Set<String> zkMachines; - - public ZkAddressValidator(Set<String> zkAddresses) { - if (zkAddresses == null) { - this.zkMachines = null; - } else { - this.zkMachines = new HashSet<String>(); - for (String zkAddress : zkAddresses) { - for (String machine : zkAddress.split(",")) { - this.zkMachines.add(machine); - } - } - } - } + private final Set<String> zkMachines; - public boolean validate(String zkAddress) { - if (zkMachines == null) { - return true; + public ZkAddressValidator(Set<String> zkAddresses) { + if (zkAddresses == null) { + this.zkMachines = null; + } else { + this.zkMachines = new HashSet<String>(); + for (String zkAddress : zkAddresses) { + for (String machine : zkAddress.split(",")) { + this.zkMachines.add(machine); } + } + } + } - String[] machines = zkAddress.split(","); - for (String machine : machines) { - if (!zkMachines.contains(machine)) { - return false; - } - } + public boolean validate(String zkAddress) { + if (zkMachines == null) { + return true; + } - return true; + String[] machines = zkAddress.split(","); + for (String machine : machines) { + if (!zkMachines.contains(machine)) { + return false; + } } + + return true; + } } http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/view/ClusterView.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/view/ClusterView.java b/helix-ui/src/main/java/org/apache/helix/ui/view/ClusterView.java index b9fc0a6..27e1050 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/view/ClusterView.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/view/ClusterView.java @@ -1,5 +1,24 @@ package org.apache.helix.ui.view; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import io.dropwizard.views.View; import org.apache.helix.ui.api.ConfigTableRow; import org.apache.helix.ui.api.InstanceSpec; @@ -9,77 +28,77 @@ import java.net.URLEncoder; import java.util.List; public class ClusterView extends View { - private final boolean adminMode; - private final String zkAddress; - private final List<String> clusters; - private final boolean activeValid; - private final String activeCluster; - private final List<String> activeClusterResources; - private final List<InstanceSpec> instanceSpecs; - private final List<ConfigTableRow> configTable; - private final List<String> stateModels; - private final List<String> rebalanceModes; + private final boolean adminMode; + private final String zkAddress; + private final List<String> clusters; + private final boolean activeValid; + private final String activeCluster; + private final List<String> activeClusterResources; + private final List<InstanceSpec> instanceSpecs; + private final List<ConfigTableRow> configTable; + private final List<String> stateModels; + private final List<String> rebalanceModes; - public ClusterView(boolean adminMode, - String zkAddress, - List<String> clusters, - boolean activeValid, - String activeCluster, - List<String> activeClusterResources, - List<InstanceSpec> instanceSpecs, - List<ConfigTableRow> configTable, - List<String> stateModels, - List<String> rebalanceModes) { - super("cluster-view.ftl"); - this.adminMode = adminMode; - this.zkAddress = zkAddress; - this.clusters = clusters; - this.activeValid = activeValid; - this.activeCluster = activeCluster; - this.activeClusterResources = activeClusterResources; - this.instanceSpecs = instanceSpecs; - this.configTable = configTable; - this.stateModels = stateModels; - this.rebalanceModes = rebalanceModes; - } + public ClusterView(boolean adminMode, + String zkAddress, + List<String> clusters, + boolean activeValid, + String activeCluster, + List<String> activeClusterResources, + List<InstanceSpec> instanceSpecs, + List<ConfigTableRow> configTable, + List<String> stateModels, + List<String> rebalanceModes) { + super("cluster-view.ftl"); + this.adminMode = adminMode; + this.zkAddress = zkAddress; + this.clusters = clusters; + this.activeValid = activeValid; + this.activeCluster = activeCluster; + this.activeClusterResources = activeClusterResources; + this.instanceSpecs = instanceSpecs; + this.configTable = configTable; + this.stateModels = stateModels; + this.rebalanceModes = rebalanceModes; + } - public boolean isAdminMode() { - return adminMode; - } + public boolean isAdminMode() { + return adminMode; + } - public String getZkAddress() throws IOException { - return URLEncoder.encode(zkAddress, "UTF-8"); - } + public String getZkAddress() throws IOException { + return URLEncoder.encode(zkAddress, "UTF-8"); + } - public List<String> getClusters() { - return clusters; - } + public List<String> getClusters() { + return clusters; + } - public boolean isActiveValid() { - return activeValid; - } + public boolean isActiveValid() { + return activeValid; + } - public String getActiveCluster() { - return activeCluster; - } + public String getActiveCluster() { + return activeCluster; + } - public List<String> getActiveClusterResources() { - return activeClusterResources; - } + public List<String> getActiveClusterResources() { + return activeClusterResources; + } - public List<InstanceSpec> getInstanceSpecs() { - return instanceSpecs; - } + public List<InstanceSpec> getInstanceSpecs() { + return instanceSpecs; + } - public List<ConfigTableRow> getConfigTable() { - return configTable; - } + public List<ConfigTableRow> getConfigTable() { + return configTable; + } - public List<String> getStateModels() { - return stateModels; - } + public List<String> getStateModels() { + return stateModels; + } - public List<String> getRebalanceModes() { - return rebalanceModes; - } + public List<String> getRebalanceModes() { + return rebalanceModes; + } } http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/view/LandingView.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/view/LandingView.java b/helix-ui/src/main/java/org/apache/helix/ui/view/LandingView.java index 330fa4b..96d69e0 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/view/LandingView.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/view/LandingView.java @@ -1,9 +1,28 @@ package org.apache.helix.ui.view; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import io.dropwizard.views.View; public class LandingView extends View { - public LandingView() { - super("landing-view.ftl"); - } + public LandingView() { + super("landing-view.ftl"); + } } http://git-wip-us.apache.org/repos/asf/helix/blob/46407b31/helix-ui/src/main/java/org/apache/helix/ui/view/ResourceView.java ---------------------------------------------------------------------- diff --git a/helix-ui/src/main/java/org/apache/helix/ui/view/ResourceView.java b/helix-ui/src/main/java/org/apache/helix/ui/view/ResourceView.java index f8367a0..a57d7c3 100644 --- a/helix-ui/src/main/java/org/apache/helix/ui/view/ResourceView.java +++ b/helix-ui/src/main/java/org/apache/helix/ui/view/ResourceView.java @@ -1,5 +1,24 @@ package org.apache.helix.ui.view; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + import io.dropwizard.views.View; import org.apache.helix.ui.api.ConfigTableRow; import org.apache.helix.ui.api.IdealStateSpec; @@ -13,91 +32,91 @@ import java.util.List; import java.util.Set; public class ResourceView extends View { - private final boolean adminMode; - private final String zkAddress; - private final List<String> clusters; - private final boolean activeValid; - private final String activeCluster; - private final List<String> activeClusterResources; - private final String activeResource; - private final List<ResourceStateTableRow> resourceStateTable; - private final List<ConfigTableRow> configTable; - private final IdealStateSpec idealStateSpec; - private final List<InstanceSpec> instanceSpecs; - - public ResourceView(boolean adminMode, - String zkAddress, - List<String> clusters, - boolean activeValid, - String activeCluster, - List<String> activeClusterResources, - String activeResource, - List<ResourceStateTableRow> resourceStateTable, - Set<String> resourceInstances, - List<ConfigTableRow> configTable, - IdealStateSpec idealStateSpec, - List<InstanceSpec> instanceSpecs) { - super("resource-view.ftl"); - this.adminMode = adminMode; - this.zkAddress = zkAddress; - this.clusters = clusters; - this.activeValid = activeValid; - this.activeCluster = activeCluster; - this.activeClusterResources = activeClusterResources; - this.activeResource = activeResource; - this.resourceStateTable = resourceStateTable; - this.configTable = configTable; - this.idealStateSpec = idealStateSpec; - this.instanceSpecs = new ArrayList<InstanceSpec>(); - - for (InstanceSpec instanceSpec : instanceSpecs) { - if (resourceInstances.contains(instanceSpec.getInstanceName())) { - this.instanceSpecs.add(instanceSpec); - } - } + private final boolean adminMode; + private final String zkAddress; + private final List<String> clusters; + private final boolean activeValid; + private final String activeCluster; + private final List<String> activeClusterResources; + private final String activeResource; + private final List<ResourceStateTableRow> resourceStateTable; + private final List<ConfigTableRow> configTable; + private final IdealStateSpec idealStateSpec; + private final List<InstanceSpec> instanceSpecs; + + public ResourceView(boolean adminMode, + String zkAddress, + List<String> clusters, + boolean activeValid, + String activeCluster, + List<String> activeClusterResources, + String activeResource, + List<ResourceStateTableRow> resourceStateTable, + Set<String> resourceInstances, + List<ConfigTableRow> configTable, + IdealStateSpec idealStateSpec, + List<InstanceSpec> instanceSpecs) { + super("resource-view.ftl"); + this.adminMode = adminMode; + this.zkAddress = zkAddress; + this.clusters = clusters; + this.activeValid = activeValid; + this.activeCluster = activeCluster; + this.activeClusterResources = activeClusterResources; + this.activeResource = activeResource; + this.resourceStateTable = resourceStateTable; + this.configTable = configTable; + this.idealStateSpec = idealStateSpec; + this.instanceSpecs = new ArrayList<InstanceSpec>(); + + for (InstanceSpec instanceSpec : instanceSpecs) { + if (resourceInstances.contains(instanceSpec.getInstanceName())) { + this.instanceSpecs.add(instanceSpec); + } } + } - public boolean isAdminMode() { - return adminMode; - } + public boolean isAdminMode() { + return adminMode; + } - public String getZkAddress() throws IOException { - return URLEncoder.encode(zkAddress, "UTF-8"); - } + public String getZkAddress() throws IOException { + return URLEncoder.encode(zkAddress, "UTF-8"); + } - public List<String> getClusters() { - return clusters; - } + public List<String> getClusters() { + return clusters; + } - public boolean isActiveValid() { - return activeValid; - } + public boolean isActiveValid() { + return activeValid; + } - public String getActiveCluster() { - return activeCluster; - } + public String getActiveCluster() { + return activeCluster; + } - public List<String> getActiveClusterResources() { - return activeClusterResources; - } + public List<String> getActiveClusterResources() { + return activeClusterResources; + } - public List<InstanceSpec> getInstanceSpecs() { - return instanceSpecs; - } + public List<InstanceSpec> getInstanceSpecs() { + return instanceSpecs; + } - public String getActiveResource() { - return activeResource; - } + public String getActiveResource() { + return activeResource; + } - public List<ResourceStateTableRow> getResourceStateTable() { - return resourceStateTable; - } + public List<ResourceStateTableRow> getResourceStateTable() { + return resourceStateTable; + } - public List<ConfigTableRow> getConfigTable() { - return configTable; - } + public List<ConfigTableRow> getConfigTable() { + return configTable; + } - public IdealStateSpec getIdealStateSpec() { - return idealStateSpec; - } + public IdealStateSpec getIdealStateSpec() { + return idealStateSpec; + } }
