@Override public synchronized void onLogResponse(LogSyncResponse logSyncResponse) throws IOException { if (logSyncResponse.getDeliveryStatuses() != null) { boolean isAlreadyScheduled = false; for (LogDeliveryStatus response : logSyncResponse.getDeliveryStatuses()) { if (response.getResult() == SyncResponseResultType.SUCCESS) { storage.removeRecordBlock(response.getRequestId()); } else { storage.notifyUploadFailed(response.getRequestId()); final LogDeliveryErrorCode errorCode = response.getErrorCode(); final LogFailoverCommand controller = this.controller; executorContext .getCallbackExecutor() .execute( new Runnable() { @Override public void run() { strategy.onFailure(controller, errorCode); } }); isAlreadyScheduled = true; } LOG.info("Removing bucket id from timeouts: {}", response.getRequestId()); timeouts.remove(response.getRequestId()); } if (!isAlreadyScheduled) { processUploadDecision(strategy.isUploadNeeded(storage.getStatus())); } } }
@Override public void fillSyncRequest(LogSyncRequest request) { LogBlock group = null; if (storage.getStatus().getRecordCount() == 0) { LOG.debug("Log storage is empty"); return; } group = storage.getRecordBlock(strategy.getBatchSize(), strategy.getBatchCount()); if (group != null) { List<LogRecord> recordList = group.getRecords(); if (!recordList.isEmpty()) { LOG.trace("Sending {} log records", recordList.size()); List<LogEntry> logs = new LinkedList<>(); for (LogRecord record : recordList) { logs.add(new LogEntry(ByteBuffer.wrap(record.getData()))); } request.setRequestId(group.getBlockId()); request.setLogEntries(logs); LOG.info("Adding following bucket id [{}] for timeout tracking", group.getBlockId()); timeouts.add(group.getBlockId()); final LogBlock timeoutGroup = group; executorContext .getScheduledExecutor() .schedule( new Runnable() { @Override public void run() { checkDeliveryTimeout(timeoutGroup.getBlockId()); } }, strategy.getTimeout(), TimeUnit.SECONDS); } } else { LOG.warn("Log group is null: log group size is too small"); } }
private void processUploadDecision(LogUploadStrategyDecision decision) { switch (decision) { case UPLOAD: transport.sync(); break; case NOOP: if (strategy.getUploadCheckPeriod() > 0 && storage.getStatus().getRecordCount() > 0) { scheduleUploadCheck(); } break; default: break; } }
private boolean checkDeliveryTimeout(int bucketId) { LOG.debug("Checking for a delivery timeout of the bucket with id: [{}] ", bucketId); boolean isTimeout = timeouts.remove(bucketId); if (isTimeout) { LOG.info("Log delivery timeout detected for the bucket with id: [{}]", bucketId); storage.notifyUploadFailed(bucketId); final LogFailoverCommand controller = this.controller; executorContext .getCallbackExecutor() .execute( new Runnable() { @Override public void run() { strategy.onTimeout(controller); } }); } else { LOG.trace("No log delivery timeout for the bucket with id [{}] was detected", bucketId); } return isTimeout; }
protected void uploadIfNeeded() { processUploadDecision(strategy.isUploadNeeded(storage.getStatus())); }
@Override public void stop() { storage.close(); }