mynameborat commented on a change in pull request #938: SAMZA-1531: Support run.id in standalone for batch processing. URL: https://github.com/apache/samza/pull/938#discussion_r264938632
########## File path: samza-core/src/main/java/org/apache/samza/runtime/LocalApplicationRunner.java ########## @@ -77,16 +90,70 @@ */ public LocalApplicationRunner(SamzaApplication app, Config config) { this.appDesc = ApplicationDescriptorUtil.getAppDescriptor(app, config); - this.planner = new LocalJobPlanner(appDesc); + Boolean isAppModeBatch = new ApplicationConfig(appDesc.getConfig()).getAppMode() == ApplicationConfig.ApplicationMode.BATCH; + if(isAppModeBatch) { + initializeCoordinationUtils(config); + initializeRunId(); + } + this.planner = new LocalJobPlanner(appDesc, coordinationUtils, uid, runId); } /** * Constructor only used in unit test to allow injection of {@link LocalJobPlanner} */ @VisibleForTesting - LocalApplicationRunner(ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc, LocalJobPlanner planner) { + LocalApplicationRunner(ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc, LocalJobPlanner planner, CoordinationUtils coordinationUtils) { this.appDesc = appDesc; this.planner = planner; + this.coordinationUtils = coordinationUtils; + Boolean isAppModeBatch = new ApplicationConfig(appDesc.getConfig()).getAppMode() == ApplicationConfig.ApplicationMode.BATCH; + if(isAppModeBatch) { + initializeRunId(); + } + } + + private void initializeCoordinationUtils(Config config) { + JobCoordinatorConfig jcConfig = new JobCoordinatorConfig(config); + String coordinationId = new ApplicationConfig(config).getGlobalAppId() + CoordinationConstants.APPLICATION_RUNNER_PATH_SUFFIX; + this.coordinationUtils = jcConfig.getCoordinationUtilsFactory().getCoordinationUtils(coordinationId, uid, config); + } + + private void initializeRunId(){ + LOG.info("Manasa: LocalApplicationRunner: getRunId() : entered "); + Boolean isAppModeBatch = new ApplicationConfig(appDesc.getConfig()).getAppMode() == ApplicationConfig.ApplicationMode.BATCH; + if(coordinationUtils == null || !isAppModeBatch) { + return; + } + + runIdLock = coordinationUtils.getReadWriteLock(CoordinationConstants.RUNID_LOCK_ID); + runIdAccess = coordinationUtils.getDataAccess(); + + if(runIdAccess == null || runIdLock == null) { + LOG.warn("Processor {} failed to create utils for run.id generation", uid); + return; + } + + try { + // acquire lock to write or read run.id + DistributedReadWriteLock.AccessType lockAccess = runIdLock.lock(CoordinationConstants.LOCK_TIMEOUT_MS, TimeUnit.MILLISECONDS); + if(lockAccess == DistributedReadWriteLock.AccessType.WRITE) { + LOG.info("write lock acquired for run.id generation by Processor " + uid); + runId = String.valueOf(System.currentTimeMillis()) + "-" + UUID.randomUUID().toString().substring(0, 8); + LOG.info("The run id for this run is {}", runId); + runIdAccess.writeData(CoordinationConstants.RUNID_PATH, runId, new LocalDistributedDataWatcher()); + runIdLock.unlock(); + } else if(lockAccess == DistributedReadWriteLock.AccessType.READ) { + LOG.info("read lock acquired for run.id by Processor " + uid); + runId = (String) runIdAccess.readData(CoordinationConstants.RUNID_PATH, new LocalDistributedDataWatcher()); + runIdLock.unlock(); + } else { + String msg = String.format("Processor {} failed to get the lock for run.id", uid); + throw new SamzaException(msg); + } + } catch (TimeoutException e) { + String msg = String.format("Processor {} timed out waiting to acquire lock for run.id generation", uid); + throw new SamzaException(msg, e); + } Review comment: can we invoke `runIdLock.unlock` in a finally block instead to make sure we unlock the lock in the event of exceptions? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services