/* * Copyright 2012-2015 Aerospike, Inc. * * Portions may be licensed to Aerospike, Inc. under one or more contributor * license agreements. * * Licensed 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. */ package com.aerospike.core.model; import java.util.HashMap; import java.util.concurrent.TimeUnit; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.viewers.Viewer; import org.eclipse.ui.IViewPart; import org.eclipse.ui.progress.UIJob; import com.aerospike.client.AerospikeClient; import com.aerospike.client.AerospikeException; import com.aerospike.client.Info; import com.aerospike.client.cluster.Node; import com.aerospike.client.policy.InfoPolicy; import com.aerospike.core.CoreActivator; import com.aerospike.core.preferences.PreferenceConstants; public class ClusterRefreshJob extends Job{ AsCluster cluster; IViewPart viewPart; private Viewer viewer; IProject project; public ClusterRefreshJob(IProject project){ this(CoreActivator.getCluster(project)); this.project = project; } public ClusterRefreshJob(AsCluster cluster) { super("Cluster refresh"); this.cluster = cluster; this.viewer = cluster.getViewer(); this.project = cluster.getProject(); super.setName("Cluster refresh for " + project.getName()); } @Override protected IStatus run(IProgressMonitor monitor) { String jobName = "Refreshing Aerospike cluster for " + project.getName(); monitor.beginTask(jobName, 30); //System.out.println("****** " + jobName); InfoPolicy infoPolicy = CoreActivator.getInfoPolicy(cluster.getProject()); try { TimeUnit.SECONDS.sleep(1); // refresh nodes if (monitor.isCanceled()) return Status.CANCEL_STATUS; AerospikeClient client = CoreActivator.getClient(cluster.getProject()); if (client == null) return Status.CANCEL_STATUS; monitor.subTask("Fetching nodes"); Node[] nodes = client.getNodes(); for (Node node : nodes){ AsNode newNode = this.cluster.addNode(node); HashMap<String, String> info = (HashMap<String, String>) Info.request(infoPolicy, node); newNode.setDetails(info); String throughputInfo = Info.request(infoPolicy, node, "throughput:"); newNode.setLatency(throughputInfo); if (monitor.isCanceled()) return Status.CANCEL_STATUS; } monitor.worked(10); monitor.subTask("Fetching name spaces"); this.cluster.namespaces.clearSetData(); // refresh Namespace list for each node for (Object kid : this.cluster.nodes.getChildren()){ if (monitor.isCanceled()) return Status.CANCEL_STATUS; if (kid instanceof AsNode){ AsNode node = (AsNode)kid; String namespacesString = Info.request(infoPolicy, node.getNode(), "namespaces"); if (!namespacesString.isEmpty()){ String[] nameSpaces = namespacesString.split(";"); for (String nameSpace : nameSpaces){ if (monitor.isCanceled()) return Status.CANCEL_STATUS; AsNameSpace nodeNamespace = node.fetchNameSpace(nameSpace); AsNameSpace clusterNameSpace = this.cluster.namespaces.fetchNameSpace(nameSpace); String nameSpaceString = Info.request(infoPolicy, node.getNode(), "namespace/"+nameSpace); nodeNamespace.setNamespaceInfo(nameSpaceString); clusterNameSpace.mergeNamespaceInfo(nameSpaceString); String setsString = Info.request(infoPolicy, node.getNode(), "sets/"+nameSpace); if (!setsString.isEmpty()){ String[] sets = setsString.split(";"); for (String setData : sets) { if (monitor.isCanceled()) return Status.CANCEL_STATUS; nodeNamespace.addSet(setData); clusterNameSpace.mergeSet(setData); } } } } } } monitor.worked(10); // refresh packages; if (monitor.isCanceled()) return Status.CANCEL_STATUS; monitor.subTask("Fetching packages"); String packagesString = Info.request(infoPolicy, nodes[0], "udf-list"); if (!packagesString.isEmpty()){ this.cluster.packages.clear(); String[] packagesList = packagesString.split(";"); for (String pkgString : packagesList){ if (monitor.isCanceled()) return Status.CANCEL_STATUS; Module pkg = this.cluster.packages.fetchPackage(pkgString); String udfString = Info.request(infoPolicy, nodes[0], "udf-get:filename=" + pkg.getName()); pkg.setDetailInfo(udfString);//gen=qgmyp0d8hQNvJdnR42X3BXgUGPE=;type=L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CBleHBpcmF0aW9uIG9mIHJlY29yZAotLSBmdW5jdGlvbiBleHBpcmUocix0dGwpCi0tICAgIGlmIHJlY29yZC50dGwocikgPT0gZ2VuIHRoZW4KLS0gICAgICAgIHJbbmFtZV0gPSB2YWx1ZQotLSAgICAgICAgYWVyb3NwaWtlOnVwZGF0ZShyKQotLSAgICBlbmQKLS0gZW5kCg==; } } monitor.worked(10); //refresh Indexes if (monitor.isCanceled()) return Status.CANCEL_STATUS; monitor.subTask("Fetching Indexes"); String indexString = Info.request(infoPolicy, nodes[0], "sindex"); if (!indexString.isEmpty()){ String[] indexList = indexString.split(";"); for (String index : indexList){ if (monitor.isCanceled()) return Status.CANCEL_STATUS; this.cluster.indexes.add(index); } } monitor.worked(10); } catch (InterruptedException e) { CoreActivator.showError(e, "Could not refresh cluster"); return Status.CANCEL_STATUS; } catch (AerospikeException e) { CoreActivator.log(IStatus.ERROR, "Could not refresh cluster", e); return Status.CANCEL_STATUS; } // refresh the view UIJob job = new UIJob("Refresh Aerospike nodes") { public IStatus runInUIThread(IProgressMonitor arg0) { if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed()) { viewer.refresh(); } return Status.OK_STATUS; } }; // ISchedulingRule rule = op.getRule(); // if (rule != null) { // job.setRule(rule); // } job.setUser(true); job.schedule(); try { String autoRefreshString = project.getPersistentProperty(CoreActivator.AUTO_REFRESH); boolean autoRefresh = Boolean.parseBoolean((autoRefreshString==null)?"false":autoRefreshString); if (autoRefresh){ String refreshPeriodString = project.getPersistentProperty(CoreActivator.REFRESH_PERIOD); long period = 30000l; if (refreshPeriodString != null) period = 1000 * Long.parseLong(refreshPeriodString); schedule(period); } } catch (CoreException e) { CoreActivator.log(IStatus.ERROR, "Error scheduling cluster refresh", e); } return Status.OK_STATUS; } }