Collection<TezEvent> outOfBandEvents)
throws TezException, IOException {
TezEvent updateEvent = null;
int eventCounter = 0;
int eventsRange = 0;
TezTaskAttemptID taskAttemptID = null;
List<TezEvent> events = new ArrayList<TezEvent>();
try {
taskLock.readLock().lock();
if (currentTask != null) {
eventsToSend.drainTo(events);
taskAttemptID = currentTaskAttemptID;
eventCounter = currentTask.getEventCounter();
eventsRange = maxEventsToGet;
if (!currentTask.isTaskDone() && !currentTask.hadFatalError()) {
updateEvent = new TezEvent(new TaskStatusUpdateEvent(
currentTask.getCounters(), currentTask.getProgress()),
new EventMetaData(EventProducerConsumerType.SYSTEM,
currentTask.getVertexName(), "", taskAttemptID));
events.add(updateEvent);
} else if (outOfBandEvents == null && events.isEmpty()) {
LOG.info("Setting TaskAttemptID to null as the task has already"
+ " completed. Caused by race-condition between the normal"
+ " heartbeat and out-of-band heartbeats");
taskAttemptID = null;
} else {
if (outOfBandEvents != null && !outOfBandEvents.isEmpty()) {
events.addAll(outOfBandEvents);
}
}
}
} finally {
taskLock.readLock().unlock();
}
long reqId = requestCounter.incrementAndGet();
TezHeartbeatRequest request = new TezHeartbeatRequest(reqId, events,
containerIdStr, taskAttemptID, eventCounter, eventsRange);
if (LOG.isDebugEnabled()) {
LOG.debug("Sending heartbeat to AM"
+ ", request=" + request.toString());
}
TezHeartbeatResponse response = umbilical.heartbeat(request);
if (LOG.isDebugEnabled()) {
LOG.debug("Received heartbeat response from AM"
+ ", response=" + response);
}
if(response.shouldDie()) {
LOG.info("Received should die response from AM");
return false;
}
if (response.getLastRequestId() != reqId) {
throw new TezException("AM and Task out of sync"
+ ", responseReqId=" + response.getLastRequestId()
+ ", expectedReqId=" + reqId);
}
try {
taskLock.readLock().lock();
if (taskAttemptID == null
|| !taskAttemptID.equals(currentTaskAttemptID)) {
if (response.getEvents() != null
&& !response.getEvents().isEmpty()) {
LOG.warn("No current assigned task, ignoring all events in"
+ " heartbeat response, eventCount="
+ response.getEvents().size());