View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.client.admin;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.accumulo.core.Constants;
25  import org.apache.accumulo.core.client.AccumuloException;
26  import org.apache.accumulo.core.client.AccumuloSecurityException;
27  import org.apache.accumulo.core.client.Instance;
28  import org.apache.accumulo.core.client.TableNotFoundException;
29  import org.apache.accumulo.core.client.impl.ClientExec;
30  import org.apache.accumulo.core.client.impl.ClientExecReturn;
31  import org.apache.accumulo.core.client.impl.MasterClient;
32  import org.apache.accumulo.core.client.impl.ServerClient;
33  import org.apache.accumulo.core.client.impl.thrift.ClientService;
34  import org.apache.accumulo.core.client.impl.thrift.ConfigurationType;
35  import org.apache.accumulo.core.master.thrift.MasterClientService;
36  import org.apache.accumulo.core.security.thrift.Credential;
37  import org.apache.accumulo.core.security.thrift.ThriftSecurityException;
38  import org.apache.accumulo.core.tabletserver.thrift.TabletClientService.Client;
39  import org.apache.accumulo.core.util.ArgumentChecker;
40  import org.apache.accumulo.core.util.ThriftUtil;
41  import org.apache.accumulo.core.zookeeper.ZooUtil;
42  import org.apache.accumulo.fate.zookeeper.ZooCache;
43  import org.apache.accumulo.trace.instrument.Tracer;
44  import org.apache.thrift.TException;
45  import org.apache.thrift.transport.TTransportException;
46  
47  /**
48   * Provides a class for administering the accumulo instance
49   */
50  public class InstanceOperationsImpl implements InstanceOperations {
51    private Instance instance;
52    private Credential credentials;
53    
54    /**
55     * @param instance
56     *          the connection information for this instance
57     * @param credentials
58     *          the username/password for this connection
59     */
60    public InstanceOperationsImpl(Instance instance, Credential credentials) {
61      ArgumentChecker.notNull(instance, credentials);
62      this.instance = instance;
63      this.credentials = credentials;
64    }
65    
66    /* (non-Javadoc)
67     * @see org.apache.accumulo.core.client.admin.InstanceOperations#setProperty(java.lang.String, java.lang.String)
68     */
69    @Override
70    public void setProperty(final String property, final String value) throws AccumuloException, AccumuloSecurityException {
71      ArgumentChecker.notNull(property, value);
72      MasterClient.execute(instance, new ClientExec<MasterClientService.Client>() {
73        @Override
74        public void execute(MasterClientService.Client client) throws Exception {
75          client.setSystemProperty(Tracer.traceInfo(), credentials, property, value);
76        }
77      });
78    }
79    
80    /* (non-Javadoc)
81     * @see org.apache.accumulo.core.client.admin.InstanceOperations#removeProperty(java.lang.String)
82     */
83    @Override
84    public void removeProperty(final String property) throws AccumuloException, AccumuloSecurityException {
85      ArgumentChecker.notNull(property);
86      MasterClient.execute(instance, new ClientExec<MasterClientService.Client>() {
87        @Override
88        public void execute(MasterClientService.Client client) throws Exception {
89          client.removeSystemProperty(Tracer.traceInfo(), credentials, property);
90        }
91      });
92    }
93    
94    /* (non-Javadoc)
95     * @see org.apache.accumulo.core.client.admin.InstanceOperations#getSystemConfiguration()
96     */
97    @Override
98    public Map<String,String> getSystemConfiguration() throws AccumuloException, AccumuloSecurityException {
99      return ServerClient.execute(instance, new ClientExecReturn<Map<String,String>,ClientService.Client>() {
100       @Override
101       public Map<String,String> execute(ClientService.Client client) throws Exception {
102         return client.getConfiguration(ConfigurationType.CURRENT);
103       }
104     });
105   }
106   
107   /* (non-Javadoc)
108    * @see org.apache.accumulo.core.client.admin.InstanceOperations#getSiteConfiguration()
109    */
110   @Override
111   public Map<String,String> getSiteConfiguration() throws AccumuloException, AccumuloSecurityException {
112     return ServerClient.execute(instance, new ClientExecReturn<Map<String,String>,ClientService.Client>() {
113       @Override
114       public Map<String,String> execute(ClientService.Client client) throws Exception {
115         return client.getConfiguration(ConfigurationType.SITE);
116       }
117     });
118   }
119   
120   /* (non-Javadoc)
121    * @see org.apache.accumulo.core.client.admin.InstanceOperations#getTabletServers()
122    */
123   
124   @Override
125   public List<String> getTabletServers() {
126     ZooCache cache = ZooCache.getInstance(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut());
127     String path = ZooUtil.getRoot(instance) + Constants.ZTSERVERS;
128     List<String> results = new ArrayList<String>();
129     for (String candidate : cache.getChildren(path)) {
130       List<String> children = cache.getChildren(path + "/" + candidate);
131       if (children != null && children.size() > 0) {
132         List<String> copy = new ArrayList<String>(children);
133         Collections.sort(copy);
134         byte[] data = cache.get(path + "/" + candidate + "/" + copy.get(0));
135         if (data != null && !"master".equals(new String(data))) {
136           results.add(candidate);
137         }
138       }
139     }
140     return results;
141   }
142   
143   /* (non-Javadoc)
144    * @see org.apache.accumulo.core.client.admin.InstanceOperations#getActiveScans(java.lang.String)
145    */
146   
147   @Override
148   public List<ActiveScan> getActiveScans(String tserver) throws AccumuloException, AccumuloSecurityException {
149     Client client = null;
150     try {
151       client = ThriftUtil.getTServerClient(tserver, instance.getConfiguration());
152       
153       List<ActiveScan> as = new ArrayList<ActiveScan>();
154       for (org.apache.accumulo.core.tabletserver.thrift.ActiveScan activeScan : client.getActiveScans(Tracer.traceInfo(), credentials)) {
155         try {
156           as.add(new ActiveScan(instance, activeScan));
157         } catch (TableNotFoundException e) {
158           throw new AccumuloException(e);
159         }
160       }
161       return as;
162     } catch (TTransportException e) {
163       throw new AccumuloException(e);
164     } catch (ThriftSecurityException e) {
165       throw new AccumuloSecurityException(e.user, e.code, e);
166     } catch (TException e) {
167       throw new AccumuloException(e);
168     } finally {
169       if (client != null)
170         ThriftUtil.returnClient(client);
171     }
172   }
173   
174   /* (non-Javadoc)
175    * @see org.apache.accumulo.core.client.admin.InstanceOperations#testClassLoad(java.lang.String, java.lang.String)
176    */
177   @Override
178   public boolean testClassLoad(final String className, final String asTypeName) throws AccumuloException, AccumuloSecurityException {
179     return ServerClient.execute(instance, new ClientExecReturn<Boolean,ClientService.Client>() {
180       @Override
181       public Boolean execute(ClientService.Client client) throws Exception {
182         return client.checkClass(Tracer.traceInfo(), className, asTypeName);
183       }
184     });
185   }
186   
187   /*
188    * (non-Javadoc)
189    * 
190    * @see org.apache.accumulo.core.client.admin.InstanceOperations#getActiveCompactions(java.lang.String)
191    */
192   @Override
193   public List<ActiveCompaction> getActiveCompactions(String tserver) throws AccumuloException, AccumuloSecurityException {
194     Client client = null;
195     try {
196       client = ThriftUtil.getTServerClient(tserver, instance.getConfiguration());
197       
198       List<ActiveCompaction> as = new ArrayList<ActiveCompaction>();
199       for (org.apache.accumulo.core.tabletserver.thrift.ActiveCompaction activeCompaction : client.getActiveCompactions(Tracer.traceInfo(), credentials)) {
200         as.add(new ActiveCompaction(instance, activeCompaction));
201       }
202       return as;
203     } catch (TTransportException e) {
204       throw new AccumuloException(e);
205     } catch (ThriftSecurityException e) {
206       throw new AccumuloSecurityException(e.user, e.code, e);
207     } catch (TException e) {
208       throw new AccumuloException(e);
209     } finally {
210       if (client != null)
211         ThriftUtil.returnClient(client);
212     }
213   }
214   
215   /*
216    * (non-Javadoc)
217    * 
218    * @see org.apache.accumulo.core.client.admin.InstanceOperations#ping(java.lang.String)
219    */
220   @Override
221   public void ping(String tserver) throws AccumuloException {
222     Client client = null;
223     try {
224       client = ThriftUtil.getTServerClient(tserver, instance.getConfiguration());
225       client.getTabletServerStatus(Tracer.traceInfo(), credentials);
226     } catch (TTransportException e) {
227       throw new AccumuloException(e);
228     } catch (ThriftSecurityException e) {
229       throw new AccumuloException(e);
230     } catch (TException e) {
231       throw new AccumuloException(e);
232     } finally {
233       if (client != null) {
234         ThriftUtil.returnClient(client);
235       }
236     }
237   }
238 }