This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
AdminClient.java
310 lines (249 loc) · 8.46 KB
/
AdminClient.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
package com.yahoo.gondola.container;
import com.google.common.collect.Range;
import com.yahoo.gondola.Config;
import com.yahoo.gondola.container.ShardManagerProtocol.ShardManagerException;
import com.yahoo.gondola.container.client.ShardManagerClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.List;
import java.util.Map;
import static com.yahoo.gondola.container.ShardManagerProtocol.ShardManagerException.CODE.SLAVE_NOT_SYNC;
/**
* The type Admin client.
*/
public class AdminClient {
// TODO: move to config
public static final int RETRY_COUNT = 3;
public static final int TIMEOUT_MS = 3000;
private String serviceName;
private Config config;
private ShardManagerClient shardManagerClient;
private ConfigWriter configWriter;
private static Logger logger = LoggerFactory.getLogger(AdminClient.class);
private boolean tracing = false;
/**
* Instantiates a new Admin client.
*
* @param serviceName the service name
* @param shardManagerClient the shard manager client
* @param config the config
*/
public AdminClient(String serviceName, ShardManagerClient shardManagerClient, Config config,
ConfigWriter configWriter) {
this.serviceName = serviceName;
this.shardManagerClient = shardManagerClient;
this.config = config;
this.configWriter = configWriter;
this.config.registerForUpdates(config1 -> {
tracing = config1.getBoolean("tracing.adminCli");
});
}
/**
* Sets service name.
*
* @param serviceName the service name
* @throws AdminException the admin exception
*/
public void setServiceName(String serviceName) throws AdminException {
this.serviceName = serviceName;
}
/**
* Gets service name.
*
* @return the service name
* @throws AdminException the admin exception
*/
public String getServiceName() throws AdminException {
return serviceName;
}
/**
* Gets config.
*
* @return the config
* @throws AdminException the admin exception
*/
public Config getConfig() throws AdminException {
return config;
}
/**
* Sets config.
*
* @param configFile the config file.
* @throws AdminException the admin exception
*/
public void setConfig(File configFile) throws AdminException {
}
/**
* Split shard.
*
* @param fromShardId the from shard id
* @param toShardId the to shard id
* @throws AdminException the admin exception
*/
public void splitShard(String fromShardId, String toShardId) throws AdminException, InterruptedException {
Range<Integer> range = lookupSplitRange(fromShardId, toShardId);
assignBuckets(range, fromShardId, toShardId);
}
/**
* Assign buckets.
*
* @param fromShardId the from shard id
* @param toShardId the to shard id
* @param range the range
*/
public void assignBuckets(Range<Integer> range, String fromShardId, String toShardId)
throws InterruptedException, AdminException {
trace("[admin] Executing assign buckets={} from {} to {}", range, fromShardId, toShardId);
for (int i = 1; i <= RETRY_COUNT; i++) {
try {
trace("[admin] Initializing slaves on {} ...", toShardId);
shardManagerClient.startObserving(toShardId, fromShardId, TIMEOUT_MS);
trace(
"[admin] All nodes in {} are in slave mode, "
+ "waiting for slave logs approaching to leader's log position.",
toShardId);
if (!shardManagerClient.waitSlavesApproaching(toShardId, -1)) {
throw new ShardManagerException(SLAVE_NOT_SYNC);
}
trace("[admin] All nodes in {} logs approached to leader's log position, assigning buckets={} ...",
toShardId, range);
// migrateBuckets is a atomic operation executing on leader at fromShard,
// after operation is success, it will stop observing mode of toShard.
shardManagerClient.migrateBuckets(range, fromShardId, toShardId, TIMEOUT_MS);
trace("[admin] success!");
trace("[admin] Writing latest config to config storage!");
saveConfig(fromShardId, toShardId);
break;
} catch (ShardManagerException e) {
try {
shardManagerClient.stopObserving(toShardId, fromShardId, TIMEOUT_MS);
} catch (ShardManagerException e1) {
logger.info("Rollback, Stop observing failed, ignoring the error. msg={}", e1.getMessage());
}
if (i != RETRY_COUNT) {
logger.warn("Error occurred during assign buckets.. retrying {} / {}, errorMsg={}",
i, RETRY_COUNT, e.getMessage());
} else {
logger.error("Assign bucket failed, lastError={}", e.getMessage());
throw new AdminException(e);
}
}
}
}
private void saveConfig(String fromShardId, String toShardId) throws AdminException {
configWriter.setBucketMap(fromShardId, getBucketMapString(fromShardId));
configWriter.setBucketMap(fromShardId, getBucketMapString(toShardId));
setConfig(configWriter.save());
}
private String getBucketMapString(String fromShardId) {
// TODO: implement
return "";
}
private void trace(String format, Object... args) {
if (tracing) {
logger.info(format, args);
}
}
private Range<Integer> lookupSplitRange(String fromShardId, String toShardId) {
// TODO: implement
return Range.closed(0, 5);
}
/**
* Merge shard.
*
* @param fromShardId the from shard id
* @param toShardId the to shard id
* @throws AdminException the admin exception
*/
public void mergeShard(String fromShardId, String toShardId) throws AdminException, InterruptedException {
Range<Integer> range = lookupMergeRange(fromShardId, toShardId);
assignBuckets(range, fromShardId, toShardId);
}
private Range<Integer> lookupMergeRange(String fromShardId, String toShardId) {
// TODO: implement
return Range.closed(1, 5);
}
/**
* Enable.
*
* @param target the target
* @param targetId the target id
* @throws AdminException the admin exception
*/
public void enable(Target target, String targetId) throws AdminException {
}
/**
* Disable.
*
* @param target the target
* @param targetId the target id
* @throws AdminException the admin exception
*/
public void disable(Target target, String targetId) throws AdminException {
}
/**
* Gets stats.
*
* @param target the target
* @return the stats
* @throws AdminException the admin exception
*/
public Map<Target, List<Stat>> getStats(Target target)
throws AdminException {
return null;
}
/**
* Enable trace.
*
* @param target the target
* @param targetId the target id
* @throws AdminException the admin exception
*/
public void enableTracing(Target target, String targetId) throws AdminException {
}
/**
* Disable trace.
*
* @param target the target
* @param targetId the target id
* @throws AdminException the admin exception
*/
public void disableTracing(Target target, String targetId) throws AdminException {
}
enum Target {
HOST,
SHARD,
SITE,
STORAGE,
ALL
}
class Stat {
}
class HostStat extends Stat {
}
class StorageStat extends Stat {
}
class ShardStat extends Stat {
}
class AdminException extends Exception {
ErrorCode errorCode;
public AdminException(ShardManagerException e) {
super(e);
}
public AdminException() {
}
}
enum ErrorCode {
CONFIG_NOT_FOUND(10000);
private int code;
ErrorCode(int code) {
this.code = code;
}
}
}