4e7b794156fec7f85951a67cd1a3fd1525a7a59c
[fluo-recipes.git] / modules / accumulo / src / main / java / io / fluo / recipes / accumulo / ops / TableOperations.java
1 /*
2 * Copyright 2015 Fluo authors (see AUTHORS)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15 package io.fluo.recipes.accumulo.ops;
16
17 import java.util.List;
18 import java.util.TreeSet;
19
20 import io.fluo.api.client.FluoClient;
21 import io.fluo.api.client.FluoFactory;
22 import io.fluo.api.config.FluoConfiguration;
23 import io.fluo.api.data.Bytes;
24 import io.fluo.recipes.common.Pirtos;
25 import io.fluo.recipes.common.RowRange;
26 import io.fluo.recipes.common.TransientRegistry;
27 import org.apache.accumulo.core.client.AccumuloException;
28 import org.apache.accumulo.core.client.ClientConfiguration;
29 import org.apache.accumulo.core.client.Connector;
30 import org.apache.accumulo.core.client.ZooKeeperInstance;
31 import org.apache.accumulo.core.client.security.tokens.PasswordToken;
32 import org.apache.commons.configuration.Configuration;
33 import org.apache.hadoop.io.Text;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38 * Utility methods for operating on the Fluo table used by recipes.
39 */
40
41 public class TableOperations {
42
43 private static final String RGB_CLASS =
44 "org.apache.accumulo.server.master.balancer.RegexGroupBalancer";
45 private static final String RGB_PATTERN_PROP = "table.custom.balancer.group.regex.pattern";
46 private static final String RGB_DEFAULT_PROP = "table.custom.balancer.group.regex.default";
47 private static final String TABLE_BALANCER_PROP = "table.balancer";
48
49 private static final Logger logger = LoggerFactory.getLogger(TableOperations.class);
50
51 private static Connector getConnector(FluoConfiguration fluoConfig) throws Exception {
52
53 ZooKeeperInstance zki =
54 new ZooKeeperInstance(new ClientConfiguration().withInstance(
55 fluoConfig.getAccumuloInstance()).withZkHosts(fluoConfig.getAccumuloZookeepers()));
56
57 Connector conn =
58 zki.getConnector(fluoConfig.getAccumuloUser(),
59 new PasswordToken(fluoConfig.getAccumuloPassword()));
60 return conn;
61 }
62
63 /**
64 * This method will perform all post initialization recommended actions.
65 */
66 public static void optimizeTable(FluoConfiguration fluoConfig, Pirtos pirtos) throws Exception {
67
68 Connector conn = getConnector(fluoConfig);
69
70 TreeSet<Text> splits = new TreeSet<>();
71
72 for (Bytes split : pirtos.getSplits()) {
73 splits.add(new Text(split.toArray()));
74 }
75
76 String table = fluoConfig.getAccumuloTable();
77 conn.tableOperations().addSplits(table, splits);
78
79 if (pirtos.getTabletGroupingRegex() != null && !pirtos.getTabletGroupingRegex().isEmpty()) {
80 // was going to call :
81 // conn.instanceOperations().testClassLoad(RGB_CLASS, TABLET_BALANCER_CLASS)
82 // but that failed. See ACCUMULO-4068
83
84 try {
85 // setting this prop first intentionally because it should fail in 1.6
86 conn.tableOperations()
87 .setProperty(table, RGB_PATTERN_PROP, pirtos.getTabletGroupingRegex());
88 conn.tableOperations().setProperty(table, RGB_DEFAULT_PROP, "none");
89 conn.tableOperations().setProperty(table, TABLE_BALANCER_PROP, RGB_CLASS);
90 } catch (AccumuloException e) {
91 logger
92 .warn("Unable to setup regex balancer (this is expected to fail in Accumulo 1.6.X) : "
93 + e.getMessage());
94 logger.debug("Unable to setup regex balancer (this is expected to fail in Accumulo 1.6.X)",
95 e);
96 }
97 }
98 }
99
100 /**
101 * Compact all transient regions that were registered using {@link TransientRegistry}
102 */
103 public static void compactTransient(FluoConfiguration fluoConfig) throws Exception {
104 Connector conn = getConnector(fluoConfig);
105
106 try (FluoClient client = FluoFactory.newClient(fluoConfig)) {
107 Configuration appConfig = client.getAppConfiguration();
108
109 TransientRegistry transientRegistry = new TransientRegistry(appConfig);
110 List<RowRange> ranges = transientRegistry.getTransientRanges();
111
112 for (RowRange r : ranges) {
113 logger.debug("Compacting {} {}", r.getStart(), r.getEnd());
114 conn.tableOperations().compact(fluoConfig.getAccumuloTable(),
115 new Text(r.getStart().toArray()), new Text(r.getEnd().toArray()), true, true);
116 }
117 }
118 }
119 }