This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
forked from voldemort/voldemort
/
VoldemortRollbackJob.java
135 lines (117 loc) · 5.8 KB
/
VoldemortRollbackJob.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2008-2013 LinkedIn, Inc
*
* 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 voldemort.store.readonly.mr.azkaban;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import voldemort.VoldemortException;
import voldemort.client.ClientConfig;
import voldemort.client.protocol.admin.AdminClient;
import voldemort.client.protocol.admin.AdminClientConfig;
import voldemort.cluster.Cluster;
import voldemort.cluster.Node;
import voldemort.store.readonly.mr.utils.VoldemortUtils;
import voldemort.store.readonly.swapper.AdminStoreSwapper;
import azkaban.common.jobs.AbstractJob;
import azkaban.common.utils.Props;
import com.google.common.collect.Maps;
/**
* This job rolls back to [ current version - 1 ]. This may not always work
* since some folks may start using the version feature to put their own version
* number. But till that doesn't happen, this will back.
*
*/
public class VoldemortRollbackJob extends AbstractJob {
private final Logger log;
private final Props props;
private List<String> storeNames;
private List<String> clusterUrls;
public VoldemortRollbackJob(String name, Props props) throws IOException {
super(name);
this.props = props;
this.log = Logger.getLogger(name);
this.storeNames = VoldemortUtils.getCommaSeparatedStringValues(props.getString("store.name"),
"store names");
this.clusterUrls = VoldemortUtils.getCommaSeparatedStringValues(props.getString("push.cluster"),
"cluster urls");
}
@Override
public void run() throws Exception {
// Go over every cluster and rollback one store at a time
for(String clusterUrl: clusterUrls) {
AdminClient adminClient = null;
ExecutorService service = null;
try {
service = Executors.newCachedThreadPool();
adminClient = new AdminClient(clusterUrl,
new AdminClientConfig(),
new ClientConfig());
Cluster cluster = adminClient.getAdminClientCluster();
AdminStoreSwapper swapper = new AdminStoreSwapper(cluster,
service,
adminClient,
1000 * props.getInt("timeout.seconds",
24 * 60 * 60),
true,
true);
// Get the current version for all stores on all nodes
Map<Integer, Map<String, Long>> previousVersions = Maps.newHashMap();
for(Node node: cluster.getNodes()) {
Map<String, Long> currentVersion = adminClient.readonlyOps.getROCurrentVersion(node.getId(),
storeNames);
log.info("Retrieving current version information on node " + node.getId());
Map<String, Long> previousVersion = Maps.newHashMap();
for(Entry<String, Long> entry: currentVersion.entrySet()) {
previousVersion.put(entry.getKey(), entry.getValue() - 1);
if(entry.getValue() == 0) {
throw new VoldemortException("Store '" + entry.getKey() + "' on node "
+ node.getId()
+ " does not have version to rollback to");
}
}
previousVersions.put(node.getId(), previousVersion);
}
// Swap one store at a time
for(String storeName: storeNames) {
for(Node node: cluster.getNodes()) {
log.info("Rolling back data on node " + node.getId() + " and for store "
+ storeName + " to version "
+ previousVersions.get(node.getId()).get(storeName));
swapper.invokeRollback(storeName,
previousVersions.get(node.getId()).get(storeName));
log.info("Successfully rolled back data on node " + node.getId()
+ " and for store " + storeName);
}
}
} finally {
if(service != null) {
service.shutdownNow();
service.awaitTermination(10, TimeUnit.SECONDS);
service = null;
}
if(adminClient != null) {
adminClient.close();
adminClient = null;
}
}
}
}
}