auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
RawBsonDocument mark(final String databaseName, final RawBsonDocument command) { spawnIfNecesary(); try { try { START System.out.println(command); END return executeCommand(databaseName, command); } catch (MongoTimeoutException e) { spawnIfNecesary(); return executeCommand(databaseName, command); } } catch (MongoException e) { throw wrapInClientException(e); } } Ditch the debugging :)

Target:
RawBsonDocument mark(final String databaseName, final RawBsonDocument command) { spawnIfNecesary(); try { try { return executeCommand(databaseName, command); } catch (MongoTimeoutException e) { spawnIfNecesary(); return executeCommand(databaseName, command); } } catch (MongoException e) { throw wrapInClientException(e); } }
Source:
public ChangeStreamIterable<TResult> showMigrationEvents(final boolean showMigrationEvents) { START this.showMigrationEvents = notNull("showMigrationEvents", showMigrationEvents); END return this; } Remove this line. Doesn't make sense for a primitive value. What's happening here is that the primitive boolean is being auto-boxed into a `Boolean`.

Target:
public ChangeStreamIterable<TResult> showMigrationEvents(final boolean showMigrationEvents) { this.showMigrationEvents = showMigrationEvents; return this; }
Source:
private String readStringFromExtendedJson() { START JsonToken patternToken = popToken(); END if (patternToken.getType() != JsonTokenType.STRING && patternToken.getType() != JsonTokenType.UNQUOTED_STRING) { throw new JsonParseException("JSON reader expected a string but found '%s'.", patternToken.getValue()); } return patternToken.getValue(String.class); } This change casts too wide a net, as this method is called from both methods implementing "Extended JSON" and for methods implementing "Shell" mode, e.g. JsonReader#visitObjectIdConstructor, and it's not correct in the shell to do something like: { _id : ObjectId(5aaff8caa33024673db4cddf) } It's also called to read values in some places, not just keys. To address this, I think you will need to leave the current method alone and add a new method that is used only to read extended JSON keys, e.g. in JsonReader#visitTimestampExtendedJson

Target:
private String readStringFromExtendedJson() { JsonToken patternToken = popToken(); if (patternToken.getType() != JsonTokenType.STRING) { throw new JsonParseException("JSON reader expected a string but found '%s'.", patternToken.getValue()); } return patternToken.getValue(String.class); }
Source:
private static TypeData<?> getTypeData(final Method method) { if (isGetter(method.getName())) { return getTypeData(method.getGenericReturnType(), method.getReturnType()); } else { START if (method.getGenericParameterTypes().length != 1) { END throw new CodecConfigurationException(format("Invalid count of arguments for setter method: %s", method.getName())); } return getTypeData(method.getGenericParameterTypes()[0], method.getParameterTypes()[0]); } } A method starting with "set" that has more than one parameter does not define a property, so should just be ignored rather than thrown an exception.

Target:
private static TypeData<?> getTypeData(final Method method) { if (isGetter(method)) { return getTypeData(method.getGenericReturnType(), method.getReturnType()); } else { return getTypeData(method.getGenericParameterTypes()[0], method.getParameterTypes()[0]); } }
Source:
public void close() { try { if (channel != null) { channel.close(); } START } catch (final IOException e) { // NOPMD END // ignore } finally { channel = null; isClosed = true; } } There are not parameters though. I really do not want to go through and make these all final, since we have not configured checkstyle to do that. I also don't see a big benefit, as I don't think I've ever accidentally modified an exception reference in a catch clause.

Target:
public void close() { try { if (channel != null) { channel.close(); } } catch (IOException e) { // NOPMD // ignore } finally { channel = null; isClosed = true; } }
Source:
public void stateChanged(final ChangeEvent<ServerDescription> event) { description = event.getNewValue(); START for (final ChangeListener<ServerDescription> listener : changeListeners) { END listener.stateChanged(event); } } Local variable: no final needed. Is your IDE configured differently that checkstyle?

Target:
public void stateChanged(final ChangeEvent<ServerDescription> event) { description = event.getNewValue(); for (ChangeListener<ServerDescription> listener : changeListeners) { listener.stateChanged(event); } }
Source:
public MeasurementKey(StudyOutcomeMeasure<? extends Variable> som, Arm a, WhenTaken wt) { START if (som.getValue() == null) { END throw new NullPointerException("Variable may not be null"); } if (som.getValue() instanceof OutcomeMeasure && a == null) { throw new NullPointerException( "Arm may not be null for Endpoints/ADEs"); } if (wt == null) { throw new NullPointerException("Moment of measurement may not be null"); } d_som = som; d_arm = a; d_wt = wt; } I guess we _will_ allow the variable to be null, or are you planning to use a placeholder of some kind?

Target:
public MeasurementKey(StudyOutcomeMeasure<? extends Variable> som, Arm a, WhenTaken wt) { if (som.getValue() instanceof OutcomeMeasure && a == null) throw new NullPointerException("Arm may not be null for Endpoints/ADEs"); if (wt == null) throw new NullPointerException("Moment of measurement may not be null"); d_som = som; d_arm = a; d_wt = wt; }
Source:
public static JButton createExtendSimulationButton(final MCMCWrapper model) { JButton button = new JButton(Main.IMAGELOADER.getIcon(FileNames.ICON_RESTART)); START button.setEnabled(false); END button.setText("No, extend"); button.setToolTipText("Extend the simulation"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ((MixedTreatmentComparison) getMTCModel(model.getModel())).setExtendSimulation(ExtendSimulation.EXTEND); } }); return button; } What if the simulation is in the "decision phase" when you build the panel?

Target:
public static JButton createExtendSimulationButton(final MCMCWrapper model) { JButton button = new JButton(Main.IMAGELOADER.getIcon(FileNames.ICON_RESTART)); button.setText("No, extend"); button.setToolTipText("Extend the simulation"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ((MixedTreatmentComparison) ((MixedTreatmentComparison)model.getModel())).setExtendSimulation(ExtendSimulation.EXTEND); } }); return button; }
Source:
private synchronized void createResults() { if (d_results.getNumberOfSamples() < 2) return; d_quantiles = new double[3]; d_quantiles[1] = RANDOM.nextDouble() * 4 - 2; d_quantiles[2] = d_quantiles[1] + RANDOM.nextDouble() * 1.5; START d_quantiles[0] = d_quantiles[2] - d_quantiles[1]; END d_defined = true; firePropertyChange(PROPERTY_DEFINED, null, d_defined); } I think this should be d_quantiles[1] - (d_quantiles[2] - d_quantiles[1]);

Target:
private synchronized void createResults() { if (d_results.getNumberOfSamples() < 2) return; d_quantiles = new double[3]; d_quantiles[1] = RANDOM.nextDouble() * 4 - 2; d_quantiles[2] = d_quantiles[1] + RANDOM.nextDouble() * 1.5; d_quantiles[0] = d_quantiles[1] - (d_quantiles[2] - d_quantiles[1]); d_defined = true; firePropertyChange(PROPERTY_DEFINED, null, d_defined); }
Source:
Collection<LocationProvider> getProviders() { START return this.providers; END } nit: I´d like the ´this.´ usages to be only whenever are necessary, to be consistent with the style used in the rest of the library code.

Target:
Collection<LocationProvider> getProviders() { return providers; }
Source:
public int registerCallback(CordovaPlugin receiver, int requestCode) { START int mappedId = this.currentCallbackId++; END callbacks.put(mappedId, new Pair<CordovaPlugin, Integer>(receiver, requestCode)); return mappedId; } Can this be called concurrently? If so, consider using java.util.concurrent.atomic.AtomicInteger.

Target:
public synchronized int registerCallback(CordovaPlugin receiver, int requestCode) { int mappedId = this.currentCallbackId++; callbacks.put(mappedId, new Pair<CordovaPlugin, Integer>(receiver, requestCode)); return mappedId; }
Source:
public Boolean shouldAllowRequest(String url) { START return true; END } Aye; I missed it when I changed that one from a _b_ oolean.

Target:
public Boolean shouldAllowRequest(String url) { return null; }
Source:
private Set<String> parseFileNames(String input) { Set<String> names = new HashSet<>(); START names.addAll(Stream.of(input.split(",")).map(String::trim).collect(Collectors.toList())); END return names; } will this skip empty strings?

Target:
private static Set<String> parseFileNames(String input) { Set<String> names = new HashSet<>(); names.addAll(Stream.of(input.split(",")).map(String::trim).collect(Collectors.toList())); return names; }
Source:
public void createDirectory(CreateDirectoryPRequest request, StreamObserver<CreateDirectoryPResponse> responseObserver) { CreateDirectoryPOptions options = request.getOptions(); RpcUtils.call(LOG, () -> { AlluxioURI pathUri = getAlluxioURI(request.getPath()); mFileSystemMaster.createDirectory(pathUri, (CreateDirectoryContext) CreateDirectoryContext START .create(options.toBuilder()).withTracker(new GrpcCallTracker(responseObserver))); END return CreateDirectoryPResponse.newBuilder().build(); }, "CreateDirectory", "request=%s", responseObserver, request); } if `withTracker` returns the generic type of the `OperationContext` (`C` in your implementation), then is it necessary to include the cast to `CreateDirectoryContext`? It just doesn't seem clean. edit - I looked into it and figured out the issue. Poor use of generics. Change the class definition of `CreatePathContext` to remove the need to cast old: ```java public abstract class CreatePathContext<T extends GeneratedMessageV3.Builder<?>, K> extends OperationContext<T, CreatePathContext> { ``` new: ```java public abstract class CreatePathContext<T extends GeneratedMessageV3.Builder<?>, K extends OperationContext> extends OperationContext<T, K> { ```

Target:
public void createDirectory(CreateDirectoryPRequest request, StreamObserver<CreateDirectoryPResponse> responseObserver) { CreateDirectoryPOptions options = request.getOptions(); RpcUtils.call(LOG, () -> { AlluxioURI pathUri = getAlluxioURI(request.getPath()); mFileSystemMaster.createDirectory(pathUri, CreateDirectoryContext.create(options.toBuilder()) .withTracker(new GrpcCallTracker(responseObserver))); return CreateDirectoryPResponse.newBuilder().build(); }, "CreateDirectory", "request=%s", responseObserver, request); }
Source:
private JobAttempt newJob(AlluxioURI filePath, int replication) { START final ClientContext clientContext = ClientContext.create(mFsContext.getPathConf(filePath)); END if (mClient == null) { mClient = JobMasterClient.Factory.create( JobMasterClientContext.newBuilder(clientContext).build()); } JobAttempt jobAttempt = new JobAttempt(new LoadConfig(filePath.getPath(), replication), new CountingRetry(3)); jobAttempt.run(); return jobAttempt; } Do you need the path-conf here?

Target:
private JobAttempt newJob(AlluxioURI filePath, int replication) { JobAttempt jobAttempt = new JobAttempt(new LoadConfig(filePath.getPath(), replication), new CountingRetry(3)); jobAttempt.run(); return jobAttempt; }
Source:
public static synchronized CacheManager get(AlluxioConfiguration conf) throws IOException { // TODO(feng): support multiple cache managers if (sCacheManager == null) { try { sCacheManager = Optional.of(create(conf)); } catch (IOException e) { sCacheManager = Optional.empty(); START throw e; END } } return sCacheManager.orElse(null); } Do we need to throw the exception here, I thought we are using null as the signal of failed to create?

Target:
public static synchronized CacheManager get(AlluxioConfiguration conf) throws IOException { // TODO(feng): support multiple cache managers if (sCacheManager == null) { sCacheManager = create(conf); } return sCacheManager; }
Source:
public BackupLeaderRole(CoreMasterContext masterContext) { super(masterContext); LOG.info("Creating backup-leader role."); // Store state lock manager pausing state change when necessary. mStateLockManager = masterContext.getStateLockManager(); // Read properties. START mStateLockTimeout = ServerConfiguration.getMs(PropertyKey.MASTER_SHELL_BACKUP_STATE_LOCK_TIMEOUT); END mBackupAbandonTimeout = ServerConfiguration.getMs(PropertyKey.MASTER_BACKUP_ABANDON_TIMEOUT); } Is this supposed to be shell?

Target:
public BackupLeaderRole(CoreMasterContext masterContext) { super(masterContext); LOG.info("Creating backup-leader role."); // Store state lock manager pausing state change when necessary. mStateLockManager = masterContext.getStateLockManager(); // Read properties. mBackupAbandonTimeout = ServerConfiguration.getMs(PropertyKey.MASTER_BACKUP_ABANDON_TIMEOUT); }
Source:
public BlockManagementTaskResult() { START mBlockOpResults = new Hashtable<>(); END } Any reason we want to use `HashTable` here?

Target:
public BlockManagementTaskResult() { mBlockOpResults = new HashMap<>(); }
Source:
public void seek(long position) throws IOException { START if (Math.abs(position - mPos) > MOVEMENT_LIMIT) { END mSequentialReadCount = 0; } else { ((FSDataInputStream) in).seek(position); } mPos = position; } If we seek backward should sequential count always be set to 0?

Target:
public void seek(long position) throws IOException { if (position < mPos || position - mPos > MOVEMENT_LIMIT) { mSequentialReadCount = 0; } mPos = position; }
Source:
public long skip(long n) throws IOException { if (n > MOVEMENT_LIMIT) { mSequentialReadCount = 0; mPos += n; return n; } else { START long skipped = ((FSDataInputStream) in).skip(n); END mPos += skipped; return skipped; } } Is it guaranteed that `in` is positioned correctly?

Target:
public long skip(long n) throws IOException { if (n <= 0) { return 0; } if (n > MOVEMENT_LIMIT) { mSequentialReadCount = 0; } mPos += n; return n; }
Source:
public void seek(long position) throws IOException { if (position < mPos || position - mPos > MOVEMENT_LIMIT) { mSequentialReadCount = 0; } else { START ((Seekable) in).seek(position); END } mPos = position; } what if 1. stream is in random read mode and `mPos=0` 2. `HdfsPositionedUnderFileInputeStream.seek(MOVEMENT_LIMIT-1)` 3. we end up calling seek on `FSDataInputStream` and that is undesired? instead can we refactor this by creating an auxiliary method `isSequentialReadMode=mSequentialReadCount> SEQUENTIAL_READ_LIMIT` ``` if (position < mPos || position - mPos > MOVEMENT_LIMIT) { mSequentialReadCount = 0; } if (isSequentialReadMode()) { ((Seekable) in).seek(position); ```

Target:
public void seek(long position) throws IOException { if (position < mPos || position - mPos > MOVEMENT_LIMIT) { mSequentialReadCount = 0; } mPos = position; }
Source:
public void addStatus(AlluxioURI path, UfsStatus status) { START UfsStatus prev = mStatuses.putIfAbsent(path, status); END if (!path.getName().equals(status.getName())) { throw new IllegalArgumentException( String.format("path name %s does not match ufs status name %s", path.getName(), status.getName())); } } what is `prev` for, and if it was already there, what should we do?

Target:
public UfsStatus addStatus(AlluxioURI path, UfsStatus status) { if (!path.getName().equals(status.getName())) { throw new IllegalArgumentException( String.format("path name %s does not match ufs status name %s", path.getName(), status.getName())); } return mStatuses.put(path, status); }
Source:
public BlockSortedField updateSortedField(long blockId, LRUSortedField oldValue) { long clockValue = 0; START if (oldValue != null) { END clockValue = mLRUClock.incrementAndGet(); } if (LOG.isDebugEnabled()) { LOG.debug("LRU update for Block: {}. Clock: {}", blockId, clockValue); } return new LRUSortedField(clockValue); } @ggezer For the LRU evictor: ``` @Override public void onCommitBlock(long sessionId, long blockId, BlockStoreLocation location) { // Since the temp block has been committed, update Evictor about the new added blocks mLRUCache.put(blockId, UNUSED_MAP_VALUE); } ``` Doesn't this mean newly created blocks are added to the tail of the LRU cache (most recently used)?

Target:
public BlockSortedField updateSortedField(long blockId, LRUSortedField oldValue) { long clockValue = mLRUClock.incrementAndGet(); if (LOG.isDebugEnabled()) { LOG.debug("LRU update for Block: {}. Clock: {}", blockId, clockValue); } return new LRUSortedField(clockValue); }
Source:
START protected synchronized long getRemoteServiceVersion() throws AlluxioStatusException { END return retryRPC(() -> mVersionService.getServiceVersion( GetServiceVersionPRequest.newBuilder().setServiceType(getRemoteServiceType()).build()) .getVersion(), LOG, "getRemoteServiceVersion", ""); } Why did we add synchronization here?

Target:
protected long getRemoteServiceVersion() throws AlluxioStatusException { return retryRPC(() -> mVersionService.getServiceVersion( GetServiceVersionPRequest.newBuilder().setServiceType(getRemoteServiceType()).build()) .getVersion(), LOG, "getRemoteServiceVersion", ""); }
Source:
public String getMetricsJson(String url) throws IOException { START String responseJson; END responseJson = HttpUtils.get(url, COLLECT_METRICS_TIMEOUT); return String.format("Url: %s%nResponse: %s", url, responseJson); } Why is this two lines?

Target:
public String getMetricsJson(String url) throws IOException { String responseJson = HttpUtils.get(url, COLLECT_METRICS_TIMEOUT); return String.format("Url: %s%nResponse: %s", url, responseJson); }
Source:
public void validateArgs(CommandLine cl) throws InvalidArgumentException { START CommandUtils.checkNumOfArgsEquals(this, cl, 2); END } `-h` is optional so I think the num of args will stay 1. Can you please build Alluxio and give it a manual test?

Target:
public void validateArgs(CommandLine cl) throws InvalidArgumentException { CommandUtils.checkNumOfArgsEquals(this, cl, 1); }
Source:
public synchronized void close() { if (mSaslClient != null) { try { mSaslClient.dispose(); START mSaslClient = null; END } catch (SaslException exc) { LOG.debug("Failed to close SaslClient.", exc); } } } do we want to keep this non-null if there is an issue with `dispose`? Basically, should we set it to null in finally or here?

Target:
public synchronized void close() { if (mSaslClient != null) { try { mSaslClient.dispose(); } catch (SaslException exc) { LOG.debug("Failed to close SaslClient.", exc); } finally { mSaslClient = null; } } }
Source:
public String toString() { START return MoreObjects.toStringHelper("") END .add("HostName", mHostName) .add("SocketAddress", mSocketAddress) .toString(); } I am confused when this is supposed to be "" or `this`?

Target:
public String toString() { return MoreObjects.toStringHelper(this) .add("HostName", mHostName) .add("SocketAddress", mSocketAddress) .toString(); }
Source:
public Iterator<Journal.JournalEntry> getJournalEntryIterator() { List<Iterator<Journal.JournalEntry>> componentIters = StreamUtils START .map(JournalEntryIterable::getJournalEntryIterator, mDBs.values()); END return Iterators.concat(getDbIterator(), getDbInfoIterator(), Iterators.concat(componentIters.iterator())); } why can't the `mDBs.values()` also add the `DBInfo` entries?

Target:
public Iterator<Journal.JournalEntry> getJournalEntryIterator() { List<Iterator<Journal.JournalEntry>> componentIters = StreamUtils .map(JournalEntryIterable::getJournalEntryIterator, mDBs.values()); return Iterators.concat(getDbIterator(), Iterators.concat(componentIters.iterator())); }
Source:
public int available() throws IOException { START int available = in.available() + (int) (((FSDataInputStream) in).getPos() - mPos); END return available > 0 ? available : 0; } Could you explain the logic here?

Target:
public int available() throws IOException { if (mPos != ((FSDataInputStream) in).getPos()) { return 0; } return in.available(); }
Source:
public int read() throws IOException { byte[] buffer = new byte[1]; int bytesRead = ((FSDataInputStream) in).read(mPos, buffer, 0, buffer.length); if (bytesRead > 0) { mPos += bytesRead; return BufferUtils.byteToInt(buffer[0]); } Preconditions.checkArgument(bytesRead != 0, START "Expected a non-zero value if end of stream has not been reached"); END return bytesRead; } Is it valid to hit EOS here and return `-1`?

Target:
public int read() throws IOException { byte[] buffer = new byte[1]; int bytesRead = read(buffer); if (bytesRead > 0) { return BufferUtils.byteToInt(buffer[0]); } Preconditions.checkArgument(bytesRead != 0, "Expected a non-zero value if end of stream has not been reached"); return bytesRead; }
Source:
START protected BaseFileSystem(FileSystemContext fsContext) { END mFsContext = fsContext; mBlockStore = AlluxioBlockStore.create(fsContext); mCloser.register(mFsContext); } @apc999 This needs to be public or reflection will not work

Target:
public BaseFileSystem(FileSystemContext fsContext) { mFsContext = fsContext; mBlockStore = AlluxioBlockStore.create(fsContext); mCloser.register(mFsContext); }
Source:
static CacheEvictor create(AlluxioConfiguration conf) { return CommonUtils.createNewClassInstance( START conf.getClass(PropertyKey.USER_CLIENT_CACHE_EVICTOR_CLASS), new Class[0], new Object[0]); END } I think you want `null` instead of empty arrays?

Target:
static CacheEvictor create(AlluxioConfiguration conf) { return CommonUtils.createNewClassInstance( conf.getClass(PropertyKey.USER_CLIENT_CACHE_EVICTOR_CLASS), null, null); }
Source:
public void onPlanStatusChange(PlanInfo planInfo) { Status status = planInfo.getStatus(); START if (status.equals(Status.COMPLETED)) { END done(planInfo.getId()); } else if (status.equals(Status.CANCELED) || status.equals(Status.FAILED)) { stop(planInfo.getId(), status, planInfo.getErrorMessage()); } } should this be `switch` statement?

Target:
public void onPlanStatusChange(PlanInfo planInfo) { Status status = planInfo.getStatus(); switch (status) { case COMPLETED: done(planInfo.getId()); break; case CANCELED: case FAILED: stop(planInfo.getId(), status, planInfo.getErrorMessage()); break; default: break; } }
Source:
public SshCommand(String hostname, String[] execString) { super(execString); mHostName = hostname; START mCommand = new String[]{"bash", "-c", END String.format("ssh %s %s %s", ShellUtils.COMMON_SSH_OPTS, hostname, String.join(" ", execString))}; } Can we just use this for the `super` constructor? (and hopefully keep `mCommand` `final`)

Target:
public SshCommand(String hostname, String[] execString) { super(new String[]{"bash", "-c", String.format("ssh %s %s %s", ShellUtils.COMMON_SSH_OPTS, hostname, String.join(" ", execString))}); mHostName = hostname; }
Source:
public ScpCommand(String remoteHost, String fromFile, String toFile, boolean isDir) { super(new String[]{}); String template = "scp %s %s:%s localhost:%s"; if (isDir) { template = "scp -r %s %s:%s localhost:%s"; } // copy from hostname to local START mCommand = new String[]{"bash", "-c", END String.format(template, ShellUtils.COMMON_SSH_OPTS, remoteHost, fromFile, toFile) }; mHostName = remoteHost; } Can we use this when calling the `super` constructor?

Target:
public ScpCommand(String remoteHost, String fromFile, String toFile, boolean isDir) { super(new String[]{"bash", "-c", String.format(isDir ? "scp -r %s %s:%s localhost:%s" : "scp %s %s:%s localhost:%s", ShellUtils.COMMON_SSH_OPTS, remoteHost, fromFile, toFile) }); mHostName = remoteHost; }
Source:
START private HiveMetaStoreClient getHive() throws IOException { END if (mHive != null) { return mHive; } mHive = newHiveClient(); return mHive; } I think we are going to have to make this thread safe, by using a lock to protect changes to the cached client. Probably a RW lock, read to get the client, write to update it.

Target:
private HiveMetaStoreClient getHive() throws IOException { if (mHive != null) { return mHive; } newHiveClient(); return mHive; }
Source:
private void submit(long workerId, JobCommand.Builder command) { if (!mWorkerIdToPendingCommands.containsKey(workerId)) { START mWorkerIdToPendingCommands.put(workerId, Lists.newArrayList()); END } mWorkerIdToPendingCommands.get(workerId).add(command.build()); } Is this already protected by a lock?

Target:
private synchronized void submit(long workerId, JobCommand.Builder command) { if (!mWorkerIdToPendingCommands.containsKey(workerId)) { mWorkerIdToPendingCommands.put(workerId, Lists.newArrayList()); } mWorkerIdToPendingCommands.get(workerId).add(command.build()); }
Source:
protected void beforeExecute(Thread t, Runnable r) { super.beforeExecute(t, r); mPauseLock.lock(); try { mNumPaused++; while (mIsPaused) { mUnpaused.await(); } START mNumPaused--; END } catch (InterruptedException e) { t.interrupt(); } finally { mPauseLock.unlock(); } } does this not have to be in the `finally` block?

Target:
protected void beforeExecute(Thread t, Runnable r) { super.beforeExecute(t, r); mPauseLock.lock(); try { mNumPaused++; while (mIsPaused) { mUnpaused.await(); } } catch (InterruptedException e) { t.interrupt(); } finally { mNumPaused--; mPauseLock.unlock(); } }
Source:
public boolean shutdown() { // Stop accepting new connections. mServer.shutdown(); // Close resources that potentially owns active streams. try { mCloser.close(); } catch (IOException e) { LOG.warn("Failed to close resources during shutdown.", e); // Do nothing. } // Force shutdown remaining calls. START mServer.shutdownNow(); END // Wait until server terminates. try { return mServer.awaitTermination(mServerShutdownTimeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); return false; } } This looks like a change of behavior - we used to give some time to the server threads to finish RPC calls before we interrupt them.

Target:
public boolean shutdown() { // Stop accepting new connections. mServer.shutdown(); // Close resources that potentially owns active streams. try { mCloser.close(); } catch (IOException e) { LOG.error("Failed to close resources during shutdown.", e); // Do nothing. } // Force shutdown remaining calls. mServer.shutdownNow(); // Wait until server terminates. try { return mServer.awaitTermination(mServerShutdownTimeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); return false; } }
Source:
public JobWorkerHealth(alluxio.grpc.JobWorkerHealth jobWorkerHealth) { mWorkerId = jobWorkerHealth.getWorkerId(); START mLoadAverage = Lists.newArrayList(jobWorkerHealth.getLoadAverageList()); END mLastUpdated = jobWorkerHealth.getLastUpdated(); mHostname = jobWorkerHealth.getHostname(); } What's the reason to copy the gRPC value into a new list?

Target:
public JobWorkerHealth(alluxio.grpc.JobWorkerHealth jobWorkerHealth) { mWorkerId = jobWorkerHealth.getWorkerId(); mLoadAverage = jobWorkerHealth.getLoadAverageList(); mLastUpdated = jobWorkerHealth.getLastUpdated(); mHostname = jobWorkerHealth.getHostname(); }
Source:
public static void beforeClass() throws Exception { sLocalAlluxioJobCluster = new LocalAlluxioJobCluster(); sLocalAlluxioJobCluster.setProperty(PropertyKey.JOB_MASTER_WORKER_HEARTBEAT_INTERVAL, "25ms"); sLocalAlluxioJobCluster.start(); START sFileSystem = sLocalAlluxioClusterResource.get().getClient(); END } we already do this in `before()`. Can we just keep it non-static and remove it from here?

Target:
public static void beforeClass() throws Exception { sLocalAlluxioJobCluster = new LocalAlluxioJobCluster(); sLocalAlluxioJobCluster.setProperty(PropertyKey.JOB_MASTER_WORKER_HEARTBEAT_INTERVAL, "25ms"); sLocalAlluxioJobCluster.start(); }
Source:
public void asyncWriteWithPersistWaitTime() throws Exception { AlluxioURI filePath = new AlluxioURI(PathUtils.uniqPath()); createTwoBytesFile(mFileSystem, filePath, 2000); START CommonUtils.sleepMs(500); END IntegrationTestUtils .checkPersistStateAndWaitForPersist(sLocalAlluxioClusterResource, mFileSystem, filePath, 2); } if we shorten all the intervals for the cluster, can we shorten this? Or, does the last line do the wait anyways? If so, does this line do anything?

Target:
public void asyncWriteWithPersistWaitTime() throws Exception { AlluxioURI filePath = new AlluxioURI(PathUtils.uniqPath()); createTwoBytesFile(mFileSystem, filePath, 2000); IntegrationTestUtils .checkPersistStateAndWaitForPersist(sLocalAlluxioClusterResource, mFileSystem, filePath, 2); }
Source:
public static CsvReader create(AlluxioURI uri) throws IOException { Path inputPath = new Path(uri.getScheme(), uri.getAuthority().toString(), uri.getPath()); Configuration conf = ReadWriterUtils.readNoCacheConf(); FileSystem fs = inputPath.getFileSystem(conf); START return new CsvReader(fs, inputPath); END } Can we do `new CsvReader(inputPath)` and do the conf/fs creation in the constructor?

Target:
public static CsvReader create(AlluxioURI uri) throws IOException { return new CsvReader(new Path(uri.getScheme(), uri.getAuthority().toString(), uri.getPath())); }
Source:
private void enforceResumeJournals() { try { mJournalSystem.resume(); } catch (IOException e) { ProcessUtils.fatalError(LOG, e, "Failed to resume journals."); } START } END catch throwable instead?

Target:
private void enforceResumeJournals() { try { mJournalSystem.resume(); } catch (Throwable e) { ProcessUtils.fatalError(LOG, e, "Failed to resume journals."); } }
Source:
public void suspend() throws IOException { try (LockResource stateLock = new LockResource(mStateLock)) { Preconditions.checkState(!mSuspended, "Already suspended"); LOG.info("Suspending state machine."); mSuspended = true; START LOG.info("Suspended state machine at sequence: {}", mLastAppliedSequence); END } } The previous log message can be removed since there is trivial logic in between.

Target:
public void suspend() throws IOException { try (LockResource stateLock = new LockResource(mStateLock)) { Preconditions.checkState(!mSuspended, "Already suspended"); mSuspended = true; LOG.info("Suspended state machine at sequence: {}", mLastAppliedSequence); } }
Source:
public void start(Boolean isLeader) throws IOException { super.start(isLeader); START mTransformManager.startHeartbeat(getExecutorService(), mMasterContext.getUserState()); END } does this heartbeat need to be stopped somewhere?

Target:
public void start(Boolean isLeader) throws IOException { super.start(isLeader); if (isLeader) { mTransformManager.start(getExecutorService(), mMasterContext.getUserState()); } }
Source:
public boolean processJournalEntry(Journal.JournalEntry entry) { if (entry.hasAttachDb()) { apply(entry.getAttachDb()); return true; } else if (entry.hasAddTable()) { Database db = mDBs.get(entry.getAddTable().getDbName()); return db.processJournalEntry(entry); } else if (entry.hasDetachDb()) { START apply(entry.getDetachDb()); END } return false; } this method needs to return `true` if this journal entry has been handled.

Target:
public boolean processJournalEntry(Journal.JournalEntry entry) { if (entry.hasAttachDb()) { apply(entry.getAttachDb()); return true; } else if (entry.hasAddTable()) { Database db = mDBs.get(entry.getAddTable().getDbName()); return db.processJournalEntry(entry); } else if (entry.hasDetachDb()) { apply(entry.getDetachDb()); return true; } return false; }
Source:
public int run(CommandLine cl) throws AlluxioStatusException { String[] args = cl.getArgs(); String dbName = args[0]; String dbType = args[1]; Properties p = cl.getOptionProperties(OPTION_OPTION.getOpt()); mClient.attachDatabase(dbName, dbType, Maps.fromProperties(p)); START System.out.println(String.format("Attached database %s (type: %s)", dbName, dbType)); END return 0; } I feel like this output isn't very helpful since we are just reprinting the user-specified options

Target:
public int run(CommandLine cl) throws AlluxioStatusException { String[] args = cl.getArgs(); String dbName = args[0]; String dbType = args[1]; Properties p = cl.getOptionProperties(OPTION_OPTION.getOpt()); mClient.attachDatabase(dbName, dbType, Maps.fromProperties(p)); return 0; }
Source:
public AlluxioURI reverseResolve(AlluxioURI ufspath) throws IOException, AlluxioException { START checkUri(ufspath); END return rpc(client -> { AlluxioURI path = client.reverseResolve(ufspath); LOG.debug("Reverse resolved to {}", ufspath.getPath(), path.getPath()); return path; }); } I think you can remove this check. That checks for the alluxio scheme and authority.

Target:
public AlluxioURI reverseResolve(AlluxioURI ufspath) throws IOException, AlluxioException { return rpc(client -> { AlluxioURI path = client.reverseResolve(ufspath); LOG.debug("Reverse resolved to {}", ufspath.getPath(), path.getPath()); return path; }); }
Source:
public List<alluxio.grpc.catalog.Partition> readTable(String dbName, String tableName, Constraint constraint) throws IOException { Table table = getTable(dbName, tableName); List<Partition> partitions = table.getPartitions(); // TODO(david): implement partition pruning List<alluxio.grpc.catalog.Partition> returnList = START partitions.stream().map(Partition::toProto).collect(Collectors.toList()); END return returnList; } Can we return this directly?

Target:
public List<alluxio.grpc.catalog.Partition> readTable(String dbName, String tableName, Constraint constraint) throws IOException { Table table = getTable(dbName, tableName); List<Partition> partitions = table.getPartitions(); // TODO(david): implement partition pruning return partitions.stream().map(Partition::toProto).collect(Collectors.toList()); }
Source:
public boolean isRootListable() { try { return getObjectListingChunkForPath("", true) != null; } catch (Exception e) { START LOG.debug("Unable to list root of bucket {}:", super.mUri.toString(), e.getMessage()); END return false; } } Is `e.getMessage()` used in the message? And, do we want the full stack trace of the exception, or do we just want the message?

Target:
public boolean isRootListable() { try { return getObjectListingChunkForPath("", true) != null; } catch (Exception e) { LOG.debug("Unable to list root of bucket {}:", super.mUri.toString(), e); return false; } }
Source:
public synchronized void updateTasks(List<TaskInfo> taskInfoList) { START synchronized (mJobInfo) { END for (TaskInfo taskInfo : taskInfoList) { mJobInfo.setTaskInfo(taskInfo.getTaskId(), taskInfo); } updateStatus(); } } this synchronized block is redundant? remove the method synchronization then?

Target:
public void updateTasks(List<TaskInfo> taskInfoList) { synchronized (mJobInfo) { for (TaskInfo taskInfo : taskInfoList) { mJobInfo.setTaskInfo(taskInfo.getTaskId(), taskInfo); } updateStatus(); } }
Source:
public synchronized void failTasksForWorker(long workerId) { Integer taskId = mWorkerIdToTaskId.get(workerId); if (taskId == null) { return; } TaskInfo taskInfo = mJobInfo.getTaskInfo(taskId); if (taskInfo.getStatus().isFinished()) { return; } START synchronized (mJobInfo) { END if (!mJobInfo.getStatus().isFinished()) { taskInfo.setStatus(Status.FAILED); taskInfo.setErrorMessage("Job worker was lost before the task could complete"); updateStatus(); } } } same here. redundant synchronization?

Target:
public void failTasksForWorker(long workerId) { synchronized (mJobInfo) { Integer taskId = mWorkerIdToTaskId.get(workerId); if (taskId == null) { return; } TaskInfo taskInfo = mJobInfo.getTaskInfo(taskId); if (taskInfo.getStatus().isFinished()) { return; } if (!mJobInfo.getStatus().isFinished()) { taskInfo.setStatus(Status.FAILED); taskInfo.setErrorMessage("Job worker was lost before the task could complete"); updateStatus(); } } }
Source:
private void shutdownManagedChannel(ManagedChannel managedChannel, long shutdownTimeoutMs) { managedChannel.shutdown(); try { managedChannel.awaitTermination(shutdownTimeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // Allow thread to exit. } finally { managedChannel.shutdownNow(); } START Verify.verify(managedChannel.isShutdown()); END } Is this necessary?

Target:
private void shutdownManagedChannel(ManagedChannel managedChannel, long shutdownTimeoutMs) { managedChannel.shutdown(); try { managedChannel.awaitTermination(shutdownTimeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // Allow thread to exit. } finally { managedChannel.shutdownNow(); } }
Source:
START private String getTransportPath(AlluxioURI uri) { END if (uri.hasScheme()) { return uri.toString(); } else { return uri.getPath(); } } It looks useful. How about make this a member method of `AlluxioURI` rather than a util method here (and if it stays here, I would even make it static method).

Target:
private static String getTransportPath(AlluxioURI uri) { if (uri.hasScheme()) { return uri.toString(); } else { // Scheme-less URIs are assumed to be Alluxio paths // and getPath() is used to avoid string conversion. return uri.getPath(); } }
Source:
public Options getOptions() { return new Options() .addOption(FORCE_OPTION) .addOption(LIST_DIR_AS_FILE_OPTION) .addOption(LIST_HUMAN_READABLE_OPTION) .addOption(LIST_PINNED_FILES_OPTION) START .addOption(TIMESTAMP_OPTION) END .addOption(RECURSIVE_OPTION) .addOption(REVERSE_SORT_OPTION) .addOption(SORT_OPTION); } NIT: Keep the options sorted alphabetically.

Target:
public Options getOptions() { return new Options() .addOption(FORCE_OPTION) .addOption(LIST_DIR_AS_FILE_OPTION) .addOption(LIST_HUMAN_READABLE_OPTION) .addOption(LIST_PINNED_FILES_OPTION) .addOption(RECURSIVE_OPTION) .addOption(REVERSE_SORT_OPTION) .addOption(SORT_OPTION) .addOption(TIMESTAMP_OPTION); }
Source:
public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof IdentityObject)) { // If it isn't an instance of an identity object, compare our internal pointer with the arg // A form of "auto unboxing" when performing comparisons return mObj == o; START } END try { IdentityObject<T> other = (IdentityObject<T>) o; return mObj == other.mObj; } catch (ClassCastException e) { return false; } } This looks dangerous. It may hide some issues in the future if we change the implementation. Do we really need unboxing?

Target:
public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof IdentityObject)) { return false; } try { IdentityObject<T> other = (IdentityObject<T>) o; return mObj == other.mObj; } catch (ClassCastException e) { return false; } }
Source:
public synchronized JournalContext createJournalContext() throws UnavailableException { if (mState != State.PRIMARY) { // We throw UnavailableException here so that clients will retry with the next primary master. throw new UnavailableException("Failed to write to journal: journal is in state " + mState); } AsyncJournalWriter writer = mAsyncWriter; START if (writer == null) { END throw new UnavailableException("Failed to write to journal: journal is shutdown."); } return new MasterJournalContext(writer); } Was this a bug? When could this happen?

Target:
public synchronized JournalContext createJournalContext() throws UnavailableException { if (mState != State.PRIMARY) { // We throw UnavailableException here so that clients will retry with the next primary master. throw new UnavailableException("Failed to write to journal: journal is in state " + mState); } return new MasterJournalContext(mAsyncWriter); }
Source:
public void createWithLengthLimit() throws Exception { String c256 = String.join("", Collections.nCopies(16, "0123456789ABCDEF")); START assertEquals(256, c256.length()); END mFileInfo.flags.set(O_WRONLY.intValue()); assertEquals(-ErrorCodes.ENAMETOOLONG(), mFuseFs.create("/foo/" + c256, 0, mFileInfo)); } we don't need to assert `c256` as it is not a part of Alluxio logic :)

Target:
public void createWithLengthLimit() throws Exception { String c256 = String.join("", Collections.nCopies(16, "0123456789ABCDEF")); mFileInfo.flags.set(O_WRONLY.intValue()); assertEquals(-ErrorCodes.ENAMETOOLONG(), mFuseFs.create("/foo/" + c256, 0, mFileInfo)); }
Source:
public static <T> void call(Logger logger, RpcCallableThrowsIOException<T> callable, String methodName, boolean failureOk, String description, StreamObserver<T> responseObserver, Object... args) { try { responseObserver .onNext(callAndReturn(logger, callable, methodName, failureOk, description, args)); START responseObserver.onCompleted(); END } catch (StatusException e) { responseObserver.onError(e); } } Move this line out of the try-catch to avoid error due to calling onError on closed call?

Target:
public static <T> void call(Logger logger, RpcCallableThrowsIOException<T> callable, String methodName, boolean failureOk, String description, StreamObserver<T> responseObserver, Object... args) { T response; try { response = callAndReturn(logger, callable, methodName, failureOk, description, args); } catch (StatusException e) { responseObserver.onError(e); return; } responseObserver.onNext(response); responseObserver.onCompleted(); }
Source:
protected String getRootKey() { START if (mUri.getScheme() != null && mUri.getScheme().equals("s3a")) { END return Constants.HEADER_S3A + mBucketName; } else { return Constants.HEADER_S3 + mBucketName; } } `"s3a".equals(mUri.getScheme())`? Should we also check for `s3`, is there any risk of the uri scheme not being `s3a` or `s3`?

Target:
protected String getRootKey() { if ("s3a".equals(mUri.getScheme())) { return Constants.HEADER_S3A + mBucketName; } else { return Constants.HEADER_S3 + mBucketName; } }
Source:
public boolean remove(T element) { START return mQueue.removeIf(x -> x.mValue == element); END } NIT: use `getValue()` instead of accessing private member?

Target:
public boolean remove(T element) { return mQueue.removeIf(x -> x.getValue() == element); }
Source:
public GrpcChannel(GrpcManagedChannelPool.ChannelKey channelKey, Channel channel, long shutdownTimeoutMs) { mChannelKey = channelKey; mChannelHealthState = channel instanceof AuthenticatedChannel ? () -> (((AuthenticatedChannel) channel).isAuthenticated() && mChannelHealthy) : () -> mChannelHealthy; START if (channel instanceof AuthenticatedChannel) { END // Store {@link AuthenticatedChannel::#close) for signaling end of // authenticated session during shutdown. mAuthCloseCallback = ((AuthenticatedChannel) channel)::close; } mChannel = ClientInterceptors.intercept(channel, new ChannelResponseTracker((this))); mChannelReleased = false; mShutdownTimeoutMs = shutdownTimeoutMs; } we do various things for the instance of `AuthenticatedChannel`. Can we just have a bool for that? Also, what other types of channels (not AuthenticatedChannel) are possible? All this checking looks like it can be abstracted away.

Target:
public GrpcChannel(GrpcManagedChannelPool.ChannelKey channelKey, Channel channel, long shutdownTimeoutMs) { mChannelKey = channelKey; mChannelHealthState = () -> mChannelHealthy; mChannel = ClientInterceptors.intercept(channel, new ChannelResponseTracker((this))); mChannelReleased = false; mShutdownTimeoutMs = shutdownTimeoutMs; }
Source:
START public Map<String, String> getXAttr() { END return mXAttr; } Can we use `byte[]` for attribute values? Given that we want to store encoded values, using `byte[]` is more compact and platform agnostic.

Target:
public Map<String, byte[]> getXAttr() { return mXAttr; }
Source:
private CreateOptions(String authUmask) { mAcl = null; mCreateParent = false; mEnsureAtomic = false; // default owner and group are null (unset) mOwner = null; mGroup = null; mMode = ModeUtils.applyFileUMask(Mode.defaults(), authUmask); START mXAttr = new HashMap<>(); END } This will be expensive(CPU, mem) to create repeatedly, especially if xattrs is never used. For example, maps have a default size (looks like 16) which can add up. Should we consider not creating the map at all if unused?

Target:
private CreateOptions(String authUmask) { mAcl = null; mCreateParent = false; mEnsureAtomic = false; // default owner and group are null (unset) mOwner = null; mGroup = null; mMode = ModeUtils.applyFileUMask(Mode.defaults(), authUmask); }
Source:
public synchronized void loadConfIfNotLoaded(InetSocketAddress address) throws AlluxioStatusException { boolean clusterConfLoaded = mClusterConf.clusterDefaultsLoaded(); boolean pathConfLoaded = mIsPathConfLoaded; START loadConf(address, !clusterConfLoaded, !pathConfLoaded); END } nit: inline booleans

Target:
public synchronized void loadConfIfNotLoaded(InetSocketAddress address) throws AlluxioStatusException { loadConf(address, !mClusterConf.clusterDefaultsLoaded(), !mIsPathConfLoaded); }
Source:
START default String hash() { END return null; } how about an empty string instead?

Target:
default String hash() { return ""; }
Source:
public Version(Supplier<Stream<byte[]>> properties) { try { mMD5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } mProperties = properties; mShouldUpdate = new AtomicBoolean(true); START mVersion = ""; END } why do we set this?

Target:
public Version(Supplier<Stream<byte[]>> properties) { try { mMD5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } mProperties = properties; mShouldUpdate = new AtomicBoolean(true); }
Source:
public int hashCode() { return Objects.hashCode( START mCommonOptions, END mAcl, mBlockSizeBytes, mGroup, mLocationPolicy, mMode, mMountId, mOwner, mReplicationDurable, mReplicationMax, mReplicationMin, mUfsPath, mWriteTier, mWriteType ); } Can we use alphabetical order? :)

Target:
public int hashCode() { return Objects.hashCode( mAcl, mBlockSizeBytes, mCommonOptions, mGroup, mLocationPolicy, mMode, mMountId, mOwner, mReplicationDurable, mReplicationMax, mReplicationMin, mUfsPath, mWriteTier, mWriteType ); }
Source:
public void record(long timeNano, int numEvents) { long leftEndPoint = bucket(timeNano); START mSeries.put(leftEndPoint, (mSeries.getOrDefault(leftEndPoint, 0)) + numEvents); END } You can remove the parentheses around `mSeries.getOrDefault(leftEndPoint, 0)` - they are no longer needed ```java mSeries.put(leftEndPoint, mSeries.getOrDefault(leftEndPoint, 0) + numEvents); ```

Target:
public void record(long timeNano, int numEvents) { long leftEndPoint = bucket(timeNano); mSeries.put(leftEndPoint, mSeries.getOrDefault(leftEndPoint, 0) + numEvents); }
Source:
public Map<String, List<String>> getLostStorage() { START return Collections.unmodifiableMap(mLostStorage); END } safer to create a copy here. Otherwise there could be a `ConcurrentModificationException` if one RPC tries to read the returned map while the map is being updated by another RPC.

Target:
public Map<String, List<String>> getLostStorage() { return new HashMap<>(mLostStorage); }
Source:
public List<String> getLostStorage() { START return Collections.unmodifiableList(mLostStorage); END } seems susceptible to concurrent modification exceptions. Could we return a copy instead?

Target:
public List<String> getLostStorage() { return new ArrayList<>(mLostStorage); }
Source:
public Map<String, List<String>> getDirectoryPathsOnTiers() { Map<String, List<String>> pathsOnTiers = new HashMap<>(); for (Pair<String, String> tierPath : mCapacityBytesOnDirs.keySet()) { String tier = tierPath.getFirst(); START pathsOnTiers.computeIfAbsent(tier, k -> new ArrayList<>()); END pathsOnTiers.get(tier).add(tierPath.getSecond()); } return pathsOnTiers; } why not `xxx.computeIfAbsent(xxx).add(xxx)`, no reason to `get(tier)` again in the line below.

Target:
public Map<String, List<String>> getDirectoryPathsOnTiers() { Map<String, List<String>> pathsOnTiers = new HashMap<>(); for (Pair<String, String> tierPath : mCapacityBytesOnDirs.keySet()) { String tier = tierPath.getFirst(); pathsOnTiers.computeIfAbsent(tier, k -> new ArrayList<>()).add(tierPath.getSecond()); } return pathsOnTiers; }
Source:
private void applyPathProperties(PathPropertiesEntry entry) { final String path = entry.getPath(); final Map<String, String> properties = entry.getPropertiesMap(); if (mProperties.containsKey(path)) { mProperties.get(path).clear(); START mProperties.get(path).putAll(properties); END } else { mProperties.put(path, new HashMap<>(properties)); } } Is it necessary to keep the previous hashmap, could we just use put which will override?

Target:
private void applyPathProperties(PathPropertiesEntry entry) { String path = entry.getPath(); Map<String, String> properties = entry.getPropertiesMap(); if (mProperties.containsKey(path)) { mProperties.get(path).clear(); mProperties.get(path).putAll(properties); } else { mProperties.put(path, new HashMap<>(properties)); } }
Source:
private void applyRemovePathProperties(RemovePathPropertiesEntry entry) { START final String path = entry.getPath(); END mProperties.remove(path); } nit: unnecessary final

Target:
private void applyRemovePathProperties(RemovePathPropertiesEntry entry) { String path = entry.getPath(); mProperties.remove(path); }