forked from apache/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplLoadWork.java
429 lines (383 loc) · 16.5 KB
/
ReplLoadWork.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.hadoop.hive.ql.exec.repl;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.common.repl.ReplScope;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.exec.TaskFactory;
import org.apache.hadoop.hive.ql.exec.repl.bootstrap.events.DatabaseEvent;
import org.apache.hadoop.hive.ql.exec.repl.bootstrap.events.filesystem.BootstrapEventsIterator;
import org.apache.hadoop.hive.ql.exec.repl.bootstrap.events.filesystem.ConstraintEventsIterator;
import org.apache.hadoop.hive.ql.exec.repl.incremental.IncrementalLoadEventsIterator;
import org.apache.hadoop.hive.ql.exec.repl.incremental.IncrementalLoadTasksBuilder;
import org.apache.hadoop.hive.ql.exec.repl.util.ReplUtils;
import org.apache.hadoop.hive.ql.exec.repl.util.TaskTracker;
import org.apache.hadoop.hive.ql.exec.util.Retryable;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.parse.EximUtil;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.parse.repl.metric.ReplicationMetricCollector;
import org.apache.hadoop.hive.ql.plan.Explain;
import org.apache.hadoop.hive.ql.session.LineageState;
import org.apache.hadoop.hive.ql.exec.Task;
import org.apache.hadoop.metrics2.util.MBeans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.ObjectName;
import java.io.IOException;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.apache.hadoop.hive.conf.Constants.SCHEDULED_QUERY_EXECUTIONID;
import static org.apache.hadoop.hive.conf.Constants.SCHEDULED_QUERY_SCHEDULENAME;
import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.REPL_STATS_TOP_EVENTS_COUNTS;
import static org.apache.hadoop.hive.ql.exec.repl.OptimisedBootstrapUtils.BOOTSTRAP_TABLES_LIST;
import static org.apache.hadoop.hive.ql.exec.repl.OptimisedBootstrapUtils.EVENT_ACK_FILE;
import static org.apache.hadoop.hive.ql.exec.repl.OptimisedBootstrapUtils.checkFileExists;
import static org.apache.hadoop.hive.ql.exec.repl.OptimisedBootstrapUtils.getBootstrapTableList;
@Explain(displayName = "Replication Load Operator", explainLevels = { Explain.Level.USER,
Explain.Level.DEFAULT,
Explain.Level.EXTENDED })
public class ReplLoadWork implements Serializable, ReplLoadWorkMBean {
private static final Logger LOG = LoggerFactory.getLogger(ReplLoadWork.class);
private static boolean enableMBeansRegistrationForTests = false;
public static boolean disableMbeanUnregistrationForTests = false;
final String dbNameToLoadIn;
final ReplScope currentReplScope;
final String dumpDirectory;
private boolean lastReplIDUpdated;
private String sourceDbName;
private Long dumpExecutionId;
private final transient ReplicationMetricCollector metricCollector;
final boolean replScopeModified;
private final ConstraintEventsIterator constraintsIterator;
private int loadTaskRunCount = 0;
private DatabaseEvent.State state = null;
private final transient BootstrapEventsIterator bootstrapIterator;
private transient IncrementalLoadTasksBuilder incrementalLoadTasksBuilder;
private transient Task<?> rootTask;
private Iterator<String> externalTableDataCopyItr;
private ReplStatsTracker replStatsTracker;
private String scheduledQueryName;
private String executionId;
private boolean shouldFailover;
public boolean isFirstFailover;
public boolean isSecondFailover;
public List<String> tablesToBootstrap = new LinkedList<>();
public List<String> tablesToDrop = new LinkedList<>();
/*
these are sessionState objects that are copied over to work to allow for parallel execution.
based on the current use case the methods are selectively synchronized, which might need to be
taken care when using other methods.
*/
final LineageState sessionStateLineageState;
public ReplLoadWork(HiveConf hiveConf, String dumpDirectory,
String sourceDbName, String dbNameToLoadIn, ReplScope currentReplScope,
LineageState lineageState, boolean isIncrementalDump, Long eventTo,
Long dumpExecutionId,
ReplicationMetricCollector metricCollector,
boolean replScopeModified) throws IOException, SemanticException {
sessionStateLineageState = lineageState;
this.dumpDirectory = dumpDirectory;
this.dbNameToLoadIn = dbNameToLoadIn;
this.currentReplScope = currentReplScope;
this.sourceDbName = sourceDbName;
this.dumpExecutionId = dumpExecutionId;
this.metricCollector = metricCollector;
this.replScopeModified = replScopeModified;
// If DB name is changed during REPL LOAD, then set it instead of referring to source DB name.
if ((currentReplScope != null) && StringUtils.isNotBlank(dbNameToLoadIn)) {
currentReplScope.setDbName(dbNameToLoadIn);
}
rootTask = null;
if (isIncrementalDump) {
ObjectName name = initializeMetricsMBeans(hiveConf, dbNameToLoadIn);
if (replStatsTracker == null) {
int numEvents = hiveConf.getIntVar(REPL_STATS_TOP_EVENTS_COUNTS);
if (numEvents < 0) {
LOG.warn("Invalid value configured for {}, Using default of {}", REPL_STATS_TOP_EVENTS_COUNTS,
REPL_STATS_TOP_EVENTS_COUNTS.defaultIntVal);
numEvents = REPL_STATS_TOP_EVENTS_COUNTS.defaultIntVal;
}
replStatsTracker = new ReplStatsTracker(numEvents);
}
if (metricCollector != null) {
metricCollector.setMetricsMBean(name);
}
Path failoverReadyMarker = new Path(dumpDirectory, ReplAck.FAILOVER_READY_MARKER.toString());
FileSystem fs = failoverReadyMarker.getFileSystem(hiveConf);
shouldFailover = hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_REPL_FAILOVER_START)
&& fs.exists(failoverReadyMarker);
Path dumpDirParent = new Path(dumpDirectory).getParent();
isFirstFailover = checkFileExists(dumpDirParent, hiveConf, EVENT_ACK_FILE);
isSecondFailover =
!isFirstFailover && checkFileExists(dumpDirParent, hiveConf, BOOTSTRAP_TABLES_LIST);
/*
* If the current incremental dump also includes bootstrap for some tables, then create iterator
* for the same.
*/
Path incBootstrapDir = new Path(dumpDirectory, ReplUtils.INC_BOOTSTRAP_ROOT_DIR_NAME);
if (isSecondFailover) {
String[] bootstrappedTables = getBootstrapTableList(new Path(dumpDirectory).getParent(), hiveConf);
LOG.info("Optimised bootstrap load for database {} with initial bootstrapped table list as {}",
dbNameToLoadIn, tablesToBootstrap);
// Get list of tables bootstrapped.
if (fs.exists(incBootstrapDir)) {
Path tableMetaPath = new Path(incBootstrapDir, EximUtil.METADATA_PATH_NAME + "/" + sourceDbName);
tablesToBootstrap =
Stream.of(fs.listStatus(tableMetaPath)).map(st -> st.getPath().getName()).collect(Collectors.toList());
}
else {
tablesToBootstrap = Collections.emptyList();
}
List<String> tableList = Arrays.asList(bootstrappedTables);
tablesToDrop = ListUtils.subtract(tableList, tablesToBootstrap);
LOG.info("Optimised bootstrap for database {} with drop table list as {} and bootstrap table list as {}",
dbNameToLoadIn, tablesToDrop, tablesToBootstrap);
}
if (fs.exists(incBootstrapDir)) {
this.bootstrapIterator = new BootstrapEventsIterator(
new Path(incBootstrapDir, EximUtil.METADATA_PATH_NAME).toString(), dbNameToLoadIn, true,
hiveConf, metricCollector);
this.constraintsIterator = new ConstraintEventsIterator(dumpDirectory, hiveConf);
} else {
this.bootstrapIterator = null;
this.constraintsIterator = null;
}
try {
incrementalLoadTasksBuilder = new IncrementalLoadTasksBuilder(dbNameToLoadIn, dumpDirectory,
new IncrementalLoadEventsIterator(dumpDirectory, hiveConf), hiveConf, eventTo, metricCollector,
replStatsTracker, shouldFailover, tablesToBootstrap.size());
} catch (HiveException e) {
throw new SemanticException(e.getMessage(), e);
}
} else {
this.bootstrapIterator = new BootstrapEventsIterator(new Path(dumpDirectory, EximUtil.METADATA_PATH_NAME)
.toString(), dbNameToLoadIn, true, hiveConf, metricCollector);
this.constraintsIterator = new ConstraintEventsIterator(
new Path(dumpDirectory, EximUtil.METADATA_PATH_NAME).toString(), hiveConf);
incrementalLoadTasksBuilder = null;
}
}
private ObjectName initializeMetricsMBeans(HiveConf hiveConf, String dbNameToLoadIn) {
try {
scheduledQueryName = hiveConf.get(SCHEDULED_QUERY_SCHEDULENAME, "");
// If the scheduled query name isn't available we don't enable JMX.
if (!StringUtils.isEmpty(scheduledQueryName) || enableMBeansRegistrationForTests) {
executionId = hiveConf.get(SCHEDULED_QUERY_EXECUTIONID, "N/A");
String metricsName = "Database-" + dbNameToLoadIn + " Policy-" + scheduledQueryName;
// Clean-up any MBean registered previously, which couldn't be cleaned up due to some previous error.
unRegisterMBeanIfRegistered("HiveServer2", metricsName, Collections.emptyMap());
ObjectName name = MBeans.register("HiveServer2", metricsName, this);
return name;
}
} catch (Exception e) {
LOG.error("Failed to initialise Metrics MBean, Status won't be updated in the JMX", e);
}
return null;
}
// Unregisters MBeans by forming the Metrics same as how the Hadoop code forms during MBean registration.
private void unRegisterMBeanIfRegistered(String serviceName, String nameName,
Map<String, String> additionalParameters) {
String additionalKeys =
additionalParameters.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining(","));
String nameStr = "Hadoop:" + "service=" + serviceName + "," + "name=" + nameName + (additionalKeys.isEmpty() ? "" :
"," + additionalKeys);
try {
ObjectName name = ObjectName.getInstance(nameStr);
MBeans.unregister(name);
LOG.debug("Successfully attempted to unregistered the MBean {}", name);
} catch (Exception e) {
LOG.debug("Unable to unregister MBean {}", nameStr, e);
}
}
BootstrapEventsIterator bootstrapIterator() {
return bootstrapIterator;
}
boolean shouldFailover() {
return this.shouldFailover;
}
ConstraintEventsIterator constraintsIterator() {
return constraintsIterator;
}
int executedLoadTask() {
return ++loadTaskRunCount;
}
void updateDbEventState(DatabaseEvent.State state) {
this.state = state;
}
DatabaseEvent databaseEvent(HiveConf hiveConf) {
return state.toEvent(hiveConf);
}
boolean hasDbState() {
return state != null;
}
boolean isIncrementalLoad() {
return incrementalLoadTasksBuilder != null;
}
boolean hasBootstrapLoadTasks() {
return (((bootstrapIterator != null) && bootstrapIterator.hasNext())
|| ((constraintsIterator != null) && constraintsIterator.hasNext()));
}
IncrementalLoadTasksBuilder incrementalLoadTasksBuilder() {
return incrementalLoadTasksBuilder;
}
public Task<?> getRootTask() {
return rootTask;
}
@Override
public String getDumpDirectory() {return dumpDirectory;}
public void setRootTask(Task<?> rootTask) {
this.rootTask = rootTask;
}
public boolean isLastReplIDUpdated() {
return lastReplIDUpdated;
}
public void setLastReplIDUpdated(boolean lastReplIDUpdated) {
this.lastReplIDUpdated = lastReplIDUpdated;
}
public String getSourceDbName() {
return sourceDbName;
}
public ReplicationMetricCollector getMetricCollector() {
return metricCollector;
}
public Long getDumpExecutionId() {
return dumpExecutionId;
}
public List<Task<?>> externalTableCopyTasks(TaskTracker tracker, HiveConf conf) throws IOException {
if (conf.getBoolVar(HiveConf.ConfVars.REPL_DUMP_SKIP_IMMUTABLE_DATA_COPY)) {
return Collections.emptyList();
}
List<Task<?>> tasks = new ArrayList<>();
Retryable retryable = Retryable.builder()
.withHiveConf(conf)
.withRetryOnException(UncheckedIOException.class).build();
try {
retryable.executeCallable((Callable<Void>) ()-> {
try{
int numEntriesToSkip = tasks == null ? 0 : tasks.size();
while (externalTableDataCopyItr.hasNext() && tracker.canAddMoreTasks()) {
if(numEntriesToSkip > 0) {
//skip entries added in the previous attempts of this retryable block
externalTableDataCopyItr.next();
numEntriesToSkip--;
continue;
}
DirCopyWork dirCopyWork = new DirCopyWork(metricCollector, (new Path(dumpDirectory).getParent()).toString());
dirCopyWork.loadFromString(externalTableDataCopyItr.next());
Task<DirCopyWork> task = TaskFactory.get(dirCopyWork, conf);
tasks.add(task);
tracker.addTask(task);
LOG.debug("Added task for {}", dirCopyWork);
}
} catch (UncheckedIOException e) {
LOG.error("Reading entry for data copy failed for external tables, attempting retry.", e);
throw e;
}
return null;
});
} catch (Exception e) {
throw new IOException(ErrorMsg.REPL_RETRY_EXHAUSTED.format(e.getMessage()));
}
LOG.info("Added total {} tasks for external table locations copy.", tasks.size());
return tasks;
}
public Iterator<String> getExternalTableDataCopyItr() {
return externalTableDataCopyItr;
}
public void setExternalTableDataCopyItr(Iterator<String> externalTableDataCopyItr) {
this.externalTableDataCopyItr = externalTableDataCopyItr;
}
@Override
public String getSourceDatabase() {
return sourceDbName;
}
@Override
public String getTargetDatabase() {
return dbNameToLoadIn;
}
@Override
public String getReplicationType() {
return isIncrementalLoad() ? "INCREMENTAL" : "BOOTSTRAP";
}
@Override
public String getScheduledQueryName() {
return scheduledQueryName;
}
@Override
public String getExecutionId() {
return executionId;
}
@Override
public String getReplStats() {
try {
if (replStatsTracker != null) {
return replStatsTracker.toString();
} else {
return "N/A";
}
} catch (Exception e) {
return "Got Error" + e.getMessage();
}
}
@Override
public String getCurrentEventId() {
try {
if (replStatsTracker != null) {
return replStatsTracker.getLastEventId();
} else {
return "";
}
} catch (Exception e) {
return "Got Error" + e.getMessage();
}
}
@Override
public Long getLastEventId() {
if (incrementalLoadTasksBuilder != null) {
return incrementalLoadTasksBuilder.eventTo();
}
return -1L;
}
/**
* Enable JMX tracking for testing.
* @param enableRegistration enable registering MBeans.
* @param disableUnregistration disable unregistering MBeans, so that value can be used by tests to validate.
*/
@VisibleForTesting
public static void setMbeansParamsForTesting(boolean enableRegistration, boolean disableUnregistration) {
enableMBeansRegistrationForTests = enableRegistration;
disableMbeanUnregistrationForTests = disableUnregistration;
}
}