auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public static Collection<? extends PropertyKey> defaultKeys() { START return Sets.newHashSet(DEFAULT_KEYS_MAP.values() END .parallelStream().filter(key -> !PropertyKey.isRemoved(key)) .collect(Collectors.toSet())); } should we instead construct `DEFAULT_KEYS_MAP` so that it doesn't contain removed keys?

Target:
public static Collection<? extends PropertyKey> defaultKeys() { return Sets.newHashSet(DEFAULT_KEYS_MAP.values()); }
Source:
public LsCommand() { START mExtensionsDir = ServerConfiguration.get(PropertyKey.EXTENSIONS_DIR); END } Because we can just call `ServerConfiguration.get` why don't we make an empty constructor here and just make the `ServerConfiguration` call when the command is actually executing. I think it just simplifies the code since the `mExtensionsDir` isn't used anywhere else

Target:
public LsCommand() { }
Source:
private ListObjectsV2Result getObjectListingChunk(ListObjectsV2Request request) throws IOException { START AmazonClientOperation<ListObjectsV2Result> op = () -> { END // Query S3 for the next batch of objects. ListObjectsV2Result result = mClient.listObjectsV2(request); // Advance the request continuation token to the next set of objects. request.setContinuationToken(result.getNextContinuationToken()); return result; }; try { return retry(op, "get next chunk of listing result"); } catch (AmazonClientException e) { throw new IOException(e); } } can we inline the operations, similar to how we use `RpcUtils.call`?

Target:
private ListObjectsV2Result getObjectListingChunk(ListObjectsV2Request request) throws IOException { try { return retry(() -> { // Query S3 for the next batch of objects. ListObjectsV2Result result = mClient.listObjectsV2(request); // Advance the request continuation token to the next set of objects. request.setContinuationToken(result.getNextContinuationToken()); return result; }, "get next chunk of listing result"); } catch (AmazonClientException e) { throw new IOException(e); } }
Source:
public Response createDirectory(@PathParam("path") final String path, final CreateDirectoryPOptions options) { return RestUtils.call((RestUtils.RestCallable<Void>) () -> { if (options == null) { mFileSystem.createDirectory(new AlluxioURI(path), START FileSystemOptions.createDirectoryDefaults(ServerConfiguration.global())); END } else { mFileSystem.createDirectory(new AlluxioURI(path), options); } return null; }, ServerConfiguration.global()); } This goes to FileSystem. Why do you need to supply the defaults here?

Target:
public Response createDirectory(@PathParam("path") final String path, final CreateDirectoryPOptions options) { return RestUtils.call((RestUtils.RestCallable<Void>) () -> { if (options == null) { mFileSystem.createDirectory(new AlluxioURI(path)); } else { mFileSystem.createDirectory(new AlluxioURI(path), options); } return null; }, ServerConfiguration.global()); }
Source:
public void getStartTimeMs() throws Exception { START Assert.assertTrue(getInfo(NO_PARAMS).getStartTimeMs() > 0); END } we prefer `assertX` over `Assert.assertX`

Target:
public void getStartTimeMs() throws Exception { assertTrue(getInfo(NO_PARAMS).getStartTimeMs() > 0); }
Source:
public synchronized void close() throws IOException { if (mWriter != null) { mWriter.close(); mWriter = null; START mAsyncWriter.close(); END mAsyncWriter = null; } if (mTailerThread != null) { mTailerThread.awaitTermination(false); mTailerThread = null; } mState = State.CLOSED; } Do we need to check if `mAsyncWriter != null`?

Target:
public synchronized void close() throws IOException { if (mWriter != null) { mWriter.close(); mWriter = null; } if (mAsyncWriter != null) { mAsyncWriter.close(); mAsyncWriter = null; } if (mTailerThread != null) { mTailerThread.awaitTermination(false); mTailerThread = null; } mState = State.CLOSED; }
Source:
public void release(int port) { Reservation r = mReserved.remove(port); if (r != null) { START r.mFile.delete(); END try { r.mLock.release(); r.mChannel.close(); } catch (IOException e) { throw new RuntimeException(e); } } } this doesn't throw anything? And, we can delete a file, and then close its channel?

Target:
public void release(int port) { Reservation r = mReserved.remove(port); if (r != null) { // If delete fails, we may leave a file behind. However, the file will be unlocked, so another // process can still take the port. r.mFile.delete(); try { r.mLock.release(); r.mLock.channel().close(); } catch (IOException e) { throw new RuntimeException(e); } } }
Source:
START void startServing() { END startServing("", ""); } `private` if it's meant to be used within the class, `protected` if it's meant to be used by subclasses

Target:
protected void startServing() { startServing("", ""); }
Source:
public static BlockStore.Factory getBlockStoreFactory() { MetastoreType type = Configuration.getEnum(PropertyKey.MASTER_METASTORE, MetastoreType.class); switch (type) { case HEAP: START return lockManger -> new HeapBlockStore(); END case ROCKS: return args -> new RocksBlockStore(Configuration.global()); default: throw new IllegalStateException("Unknown metastore type: " + type); } } NIT: "lockManger" -> "lockManager"

Target:
public static BlockStore.Factory getBlockStoreFactory() { MetastoreType type = Configuration.getEnum(PropertyKey.MASTER_METASTORE, MetastoreType.class); switch (type) { case HEAP: return args -> new HeapBlockStore(args); case ROCKS: return args -> new RocksBlockStore(args); default: throw new IllegalStateException("Unknown metastore type: " + type); } }
Source:
public void remove(Long inodeId) { START System.out.printf("Remove %s from rocks%n", inodeId); END try { byte[] id = Longs.toByteArray(inodeId); mDb.delete(mInodesColumn, mDisableWAL, id); } catch (RocksDBException e) { throw new RuntimeException(e); } } old debugging output?

Target:
public void remove(Long inodeId) { try { byte[] id = Longs.toByteArray(inodeId); mDb.delete(mInodesColumn, mDisableWAL, id); } catch (RocksDBException e) { throw new RuntimeException(e); } }
Source:
public void removeChild(String name) { START if (mChildren.remove(name) != null) { END mWeight.decrementAndGet(); } } `mChildren` could be `null`

Target:
public void removeChild(String name) { if (mChildren != null && mChildren.remove(name) != null) { mWeight.decrementAndGet(); } }
Source:
public void close() throws IOException { START if (mClient.isShutdown()) { END return; } mStream.close(); try { mStream.waitForComplete(CLOSE_TIMEOUT_MS); } finally { mContext.releaseBlockWorkerClient(mAddress, mClient); } } If the client is shutdown, do we still need to release it?

Target:
public void close() throws IOException { try { if (mClient.isShutdown()) { return; } mStream.close(); mStream.waitForComplete(CLOSE_TIMEOUT_MS); } finally { mContext.releaseBlockWorkerClient(mAddress, mClient); } }
Source:
private LocalFileDataReader(LocalFileBlockReader reader, long offset, long len, long chunkSize) START throws IOException { END mReader = reader; Preconditions.checkArgument(chunkSize > 0); mPos = offset; mEnd = Math.min(mReader.getLength(), offset + len); mChunkSize = chunkSize; } Does this method throw IOException?

Target:
private LocalFileDataReader(LocalFileBlockReader reader, long offset, long len, long chunkSize) { mReader = reader; Preconditions.checkArgument(chunkSize > 0); mPos = offset; mEnd = Math.min(mReader.getLength(), offset + len); mChunkSize = chunkSize; }
Source:
START public static FileSystemContext create(Subject subject, MasterInquireClient masterInquireClient, END AlluxioConfiguration alluxioConf) { AlluxioProperties props = null; if (alluxioConf != null) { props = alluxioConf.getProperties(); } FileSystemContext context = new FileSystemContext(subject, props); context.init(masterInquireClient); return context; } Would it make sense to include `MasterInquireClient` in the `ClientContext` since the inquire client (how to find the alluxio master) is pretty central to any client. Then, I feel like the FileSystemContext really only needs 1 constructor that takes a `ClientContext`.

Target:
public static FileSystemContext create(Subject subject, MasterInquireClient masterInquireClient, AlluxioConfiguration alluxioConf) { FileSystemContext context = new FileSystemContext(subject, alluxioConf); context.init(masterInquireClient); return context; }
Source:
START public static AlluxioConfiguration merge(AlluxioConfiguration conf, Map<?, ?> properties, END Source source) { AlluxioProperties props = conf.getProperties(); props.merge(properties, source); return new InstancedConfiguration(props); } does this modify the input `conf`?

Target:
public static void merge(AlluxioConfiguration conf, Map<?, ?> properties, Source source) { AlluxioProperties props = conf.getProperties(); props.merge(properties, source); }
Source:
public void close() throws IOException { mChannel.shutdown(); try { long timeout = Configuration.getMs(PropertyKey.WORKER_NETWORK_NETTY_SHUTDOWN_TIMEOUT); if (!mChannel.awaitTermination(timeout, TimeUnit.MILLISECONDS)) { START LOGGER.warn("Timed out after {}ms to close gRPC channel to block worker.", timeout); END } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } } Add worker host to log message

Target:
public void close() throws IOException { mChannel.shutdown(); }
Source:
public void stop() throws IOException { if (mAsyncAuditLogWriter != null) { mAsyncAuditLogWriter.stop(); mAsyncAuditLogWriter = null; } super.stop(); START mSyncManager.stop(); END } does this have to be stopped before `super.stop()`?

Target:
public void stop() throws IOException { if (mAsyncAuditLogWriter != null) { mAsyncAuditLogWriter.stop(); mAsyncAuditLogWriter = null; } mSyncManager.stop(); super.stop(); }
Source:
START public List<alluxio.wire.SyncPointInfo> getSyncPathList() throws IOException, AlluxioException { END FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient(); try { return masterClient.getSyncPathList(); } catch (UnavailableException e) { throw e; } catch (AlluxioStatusException e) { throw e.toAlluxioException(); } finally { mFileSystemContext.releaseMasterClient(masterClient); } } nit: why not import?

Target:
public List<SyncPointInfo> getSyncPathList() throws IOException, AlluxioException { FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient(); try { return masterClient.getSyncPathList(); } catch (UnavailableException e) { throw e; } catch (AlluxioStatusException e) { throw e.toAlluxioException(); } finally { mFileSystemContext.releaseMasterClient(masterClient); } }
Source:
private static String newDbPath(String baseDir) { // We use a unique subdirectory to avoid conflicts with the previous RocksDB instance when we // re-create the db in initDB(). return PathUtils.concatPath(baseDir, START "blocks-" + System.nanoTime() + "-" + CommonUtils.randomAlphaNumString(3)); END } Should we use `currentTimeMillis()` to have some ordering sense in the dir names?

Target:
private static String newDbPath(String baseDir) { // We use a unique subdirectory to avoid conflicts with the previous RocksDB instance when we // re-create the db in initDB(). return PathUtils.concatPath(baseDir, "blocks-" + System.currentTimeMillis() + "-" + CommonUtils.randomAlphaNumString(3)); }
Source:
START public void createFileOutsideOfAlluxio(AlluxioURI uri) throws Exception { END try (FileOutStream os = mFileSystem.createFile(uri, CreateFileOptions.defaults() .setWriteType(WriteType.THROUGH) .setBlockSizeBytes(TEST_BLOCK_SIZE))) { os.write(BufferUtils.getIncreasingByteArray(TEST_BLOCK_SIZE + 1)); } } nit: should be private

Target:
private void createFileOutsideOfAlluxio(AlluxioURI uri) throws Exception { try (FileOutStream os = mFileSystem.createFile(uri, CreateFileOptions.defaults() .setWriteType(WriteType.THROUGH) .setBlockSizeBytes(TEST_BLOCK_SIZE))) { os.write(BufferUtils.getIncreasingByteArray(TEST_BLOCK_SIZE + 1)); } }
Source:
private void assertBadAuthority(String authority, String failureMessage) throws Exception { try { START useUriWithAuthority("zk@a:0,b:0,c:1"); END fail(failureMessage); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("does not match")); } } should the `zk@a:0,b:0,c:1` be change to `authority`?

Target:
private void assertBadAuthority(String authority, String failureMessage) throws Exception { try { useUriWithAuthority(authority); fail(failureMessage); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("does not match")); } }
Source:
public int getInt(PropertyKey key) { String rawValue = get(key); try { START return Integer.parseInt(lookup(rawValue)); END } catch (NumberFormatException e) { throw new RuntimeException(ExceptionMessage.KEY_NOT_INTEGER.getMessage(key)); } catch (UnresolvablePropertyException e) { throw new RuntimeException(e); } } I think the `lookup` calls in the `getX` series of methods are unnecessary. `get(key)` will already call `lookup`, so the second `lookup` does nothing

Target:
public int getInt(PropertyKey key) { String rawValue = get(key); try { return Integer.parseInt(rawValue); } catch (NumberFormatException e) { throw new RuntimeException(ExceptionMessage.KEY_NOT_INTEGER.getMessage(key)); } }
Source:
START public void noAuthorityNoWarning() throws IOException { END URI unknown = URI.create("alluxio:///"); FileSystem.get(unknown, new org.apache.hadoop.conf.Configuration()); assertFalse(mTestLogger.mAppender.wasLogged("Authority \"\" is unknown")); } for this case I think it's better not to log a warning. This is the recommended way to write an alluxio uri that picks up its authority from configuration.

Target:
public void noAuthorityNoWarning() throws IOException { URI unknown = URI.create("alluxio:///"); FileSystem.get(unknown, new org.apache.hadoop.conf.Configuration()); assertFalse(mTestLogger.wasLogged("Authority \"\" is unknown")); }
Source:
public void run() { mLastHeartbeatThread.start(); try { // Shutdown hooks should run quickly, so we limit the wait time to 500ms. It isn't the end // of the world if the final heartbeat fails. mLastHeartbeatThread.join(500); if (mLastHeartbeatThread.isAlive()) { START LOG.warn("Failed to heartbeat to the metrics master before exit"); END } } catch (InterruptedException e) { return; } } Should we put this in a finally?

Target:
public void run() { mLastHeartbeatThread.start(); try { // Shutdown hooks should run quickly, so we limit the wait time to 500ms. It isn't the end // of the world if the final heartbeat fails. mLastHeartbeatThread.join(500); } catch (InterruptedException e) { return; } finally { if (mLastHeartbeatThread.isAlive()) { LOG.warn("Failed to heartbeat to the metrics master before exit"); } } }
Source:
public void close() throws IOException { // super.close should be called first before releasing the resources in this instance, as the // super class may invoke other methods in this class. For example, // org.apache.hadoop.fs.FileSystem.close may check the existence of certain temp files before // closing super.close(); synchronized (CACHED_CONTEXTS_LOCK) { Pair<FileSystemContext, AtomicInteger> contextAndRefCount = CACHED_CONTEXTS.get(mContext.getParentSubject()); if (contextAndRefCount == null) { LOG.warn("Failed to find context to close, have you called close multiple times?"); return; } START if (contextAndRefCount.getSecond().decrementAndGet() == 0) { END contextAndRefCount.getFirst().close(); } } } can we push the reference counting and close implementation into `FileSystemContext`? It reads better than `getFirst()/getSecond()`.

Target:
public void close() throws IOException { // super.close should be called first before releasing the resources in this instance, as the // super class may invoke other methods in this class. For example, // org.apache.hadoop.fs.FileSystem.close may check the existence of certain temp files before // closing super.close(); if (mContext != null) { mContext.close(); } }
Source:
START public void mergeEmptyHadoopConfiguration() throws URISyntaxException { END org.apache.hadoop.conf.Configuration hadoopConfig = new org.apache.hadoop.conf.Configuration(); long beforeSize = Configuration.toMap().size(); HadoopConfigurationUtils.mergeHadoopConfiguration(hadoopConfig, Configuration.global()); long afterSize = Configuration.toMap().size(); Assert.assertEquals(beforeSize, afterSize); Assert.assertFalse(Configuration.getBoolean(PropertyKey.ZOOKEEPER_ENABLED)); } why throw this?

Target:
public void mergeEmptyHadoopConfiguration() { org.apache.hadoop.conf.Configuration hadoopConfig = new org.apache.hadoop.conf.Configuration(); long beforeSize = Configuration.toMap().size(); HadoopConfigurationUtils.mergeHadoopConfiguration(hadoopConfig, Configuration.global()); long afterSize = Configuration.toMap().size(); Assert.assertEquals(beforeSize, afterSize); Assert.assertFalse(Configuration.getBoolean(PropertyKey.ZOOKEEPER_ENABLED)); }
Source:
public String getHost() { START if (getAuthorityType() == AuthorityType.SINGLE_MASTER) { END SingleMasterAuthority authority = (SingleMasterAuthority) mUri.getAuthority(); return authority.getHost(); } return null; } do we need the `getAuthorityType()` method? `instanceof` can be used in a similar way, and it makes it clearer that it will be safe to cast to the authority subclass

Target:
public String getHost() { if (mUri.getAuthority() instanceof SingleMasterAuthority) { SingleMasterAuthority authority = (SingleMasterAuthority) mUri.getAuthority(); return authority.getHost(); } return null; }
Source:
private TtlBucket getBucketContaining(Inode<?> inode) { START if (inode.getTtl() <= Constants.NO_TTL) { END // no bucket will contain a inode with NO_TTL. return null; } long ttlEndTimeMs = inode.getCreationTimeMs() + inode.getTtl(); // Gets the last bucket with interval start time less than or equal to the inode's life end // time. TtlBucket bucket = mBucketList.floor(new TtlBucket(ttlEndTimeMs)); if (bucket == null || bucket.getTtlIntervalEndTimeMs() < ttlEndTimeMs || (bucket.getTtlIntervalEndTimeMs() == ttlEndTimeMs && TtlBucket.getTtlIntervalMs() != 0)) { // 1. There is no bucket in the list, or // 2. All buckets' interval start time is larger than the inode's life end time, or // 3. No bucket actually contains ttlEndTimeMs in its interval. return null; } return bucket; } Prefer the original or `inode.getTtl() < 0`.

Target:
private TtlBucket getBucketContaining(Inode<?> inode) { if (inode.getTtl() == Constants.NO_TTL) { // no bucket will contain a inode with NO_TTL. return null; } long ttlEndTimeMs = inode.getCreationTimeMs() + inode.getTtl(); // Gets the last bucket with interval start time less than or equal to the inode's life end // time. TtlBucket bucket = mBucketList.floor(new TtlBucket(ttlEndTimeMs)); if (bucket == null || bucket.getTtlIntervalEndTimeMs() < ttlEndTimeMs || (bucket.getTtlIntervalEndTimeMs() == ttlEndTimeMs && TtlBucket.getTtlIntervalMs() != 0)) { // 1. There is no bucket in the list, or // 2. All buckets' interval start time is larger than the inode's life end time, or // 3. No bucket actually contains ttlEndTimeMs in its interval. return null; } return bucket; }
Source:
public MetricsCommand(MetaMasterClient metaMasterClient, PrintStream printStream) throws IOException { mMetaMasterClient = metaMasterClient; mPrintStream = printStream; START mMetricsMap = new TreeMap<>(mMetaMasterClient.getMetrics()); END } not good to perform expensive/blocking operations in the constructor - could we call this from `run()` instead?

Target:
public MetricsCommand(MetaMasterClient metaMasterClient, PrintStream printStream) throws IOException { mMetaMasterClient = metaMasterClient; mPrintStream = printStream; }
Source:
START public void setAcl(String path, AccessControlList acl) { END // Noop here by default } This might still need the `throws IOException` signature.

Target:
public void setAcl(String path, AccessControlList acl) throws IOException{ // Noop here by default }
Source:
public synchronized ConfigCheckReport getConfigCheckReport() { START if (mConfigDirty == true) { END mConfigDirty = false; regenerateReport(); } return mConfigCheckReport; } `if(mConfigDirty)`?

Target:
public synchronized ConfigCheckReport getConfigCheckReport() { if (mConfigDirty) { // Reset mConfigDirty *before* calling regenerateReport so that if the configuration changes // during report regeneration, the change will be reflected in the next call to // getConfigCheckReport. mConfigDirty = false; regenerateReport(); } return mConfigCheckReport; }
Source:
public T removeAcl(List<AclEntry> entries) throws IOException { for (AclEntry entry : entries) { if (entry.isDefault()) { AccessControlList defaultAcl = getDefaultACL(); START if (defaultAcl != null) { END defaultAcl.removeEntry(entry); } } else { mAcl.removeEntry(entry); } } return getThis(); } Can `defaultAcl` be `null`?

Target:
public T removeAcl(List<AclEntry> entries) throws IOException { for (AclEntry entry : entries) { if (entry.isDefault()) { AccessControlList defaultAcl = getDefaultACL(); defaultAcl.removeEntry(entry); } else { mAcl.removeEntry(entry); } } return getThis(); }
Source:
START public String stripPrefixIfPresent(String path) { END String stripedKey = CommonUtils.stripPrefixIfPresent(path, PathUtils.normalizePath(getRootKey(), PATH_SEPARATOR)); if (!stripedKey.equals(path)) { return stripedKey; } return CommonUtils.stripPrefixIfPresent(path, PATH_SEPARATOR); } Can we limit the scope?

Target:
protected String stripPrefixIfPresent(String path) { String stripedKey = CommonUtils.stripPrefixIfPresent(path, PathUtils.normalizePath(getRootKey(), PATH_SEPARATOR)); if (!stripedKey.equals(path)) { return stripedKey; } return CommonUtils.stripPrefixIfPresent(path, PATH_SEPARATOR); }
Source:
LockedInodePath(AlluxioURI uri, InodeLockList lockList, InodeTree.LockMode lockMode) throws InvalidPathException { START Preconditions.checkArgument(!lockList.getInodes().isEmpty()); END mUri = uri; mPathComponents = PathUtils.getPathComponents(mUri.getPath()); mLockList = lockList; mLockMode = lockMode; } `getInodes` is expensive, right? (new array, copying entries)

Target:
LockedInodePath(AlluxioURI uri, InodeLockList lockList, InodeTree.LockMode lockMode) throws InvalidPathException { Preconditions.checkArgument(!lockList.isEmpty()); mUri = uri; mPathComponents = PathUtils.getPathComponents(mUri.getPath()); mLockList = lockList; mLockMode = lockMode; }
Source:
public boolean isOpen() { START return mTransport != null && mTransport.isOpen(); END } we can add a precondition to the constructor that `mTransport` is non-null, then since it's final we don't need to check any more. At that point it might be worth inlining the `isOpen` calls

Target:
public boolean isOpen() { return mTransport.isOpen(); }
Source:
public ServerConfigurationChecker(ServerConfigurationStore masterStore, ServerConfigurationStore workerStore) { mMasterStore = masterStore; mWorkerStore = workerStore; mConfigCheckReport = new ConfigCheckReport(); START mLastLogConfigReportTimeMs = System.currentTimeMillis(); END mMasterStore.registerChangeListener(this::regenerateReport); } initialize to -1 since we haven't logged a report yet?

Target:
public ServerConfigurationChecker(ServerConfigurationStore masterStore, ServerConfigurationStore workerStore) { mMasterStore = masterStore; mWorkerStore = workerStore; mConfigCheckReport = new ConfigCheckReport(); mMasterStore.registerChangeListener(this::regenerateReport); }
Source:
public Address setHost(String host) { Preconditions.checkNotNull(host, "host"); START mHost = host; END return this; } `checkNotNull` is designed to be chained like `mHost = Preconditions.checkNotNull(host, "host")`

Target:
public Address setHost(String host) { mHost = Preconditions.checkNotNull(host, "host"); return this; }
Source:
public void before() throws Exception { START GROUPS.put(IMPERSONATION_USER, IMPERSONATION_GROUP1 + "," + IMPERSONATION_GROUP2); END GROUPS.put(HDFS_USER, HDFS_GROUP1 + "," + HDFS_GROUP2); // Give the root dir 777, to write files as different users. FileSystem.Factory.get().setAttribute(new AlluxioURI("/"), SetAttributeOptions.defaults().setMode(new Mode((short) 0777))); } should these two go to the static `beforeClass`?

Target:
public void before() throws Exception { // Give the root dir 777, to write files as different users. This must be run as the user // that starts the master process FileSystem.Factory.get().setAttribute(new AlluxioURI("/"), SetAttributeOptions.defaults().setMode(new Mode((short) 0777))); }
Source:
public String getId() { START return mId; END } Is there any way to determine the ID of a process, how will users be able to map an ID to a process after seeing the metrics be abnormal?

Target:
public long getId() { return mId; }
Source:
public synchronized void heartbeat(final List<Metric> metrics) throws IOException { START retryRPC(new RpcCallable<Void>() { END @Override public Void call() throws TException { mClient.metricsHeartbeat(FileSystemContext.INSTANCE.getId(), NetworkAddressUtils.getClientHostName(), new MetricsHeartbeatTOptions(metrics)); return null; } }); } The nested retries (`retryRPC` and `heartbeatThread`) may cause unneeded complexity?

Target:
public synchronized void heartbeat(final List<Metric> metrics) throws IOException { retryRPC(new RpcCallable<Void>() { @Override public Void call() throws TException { mClient.metricsHeartbeat(Long.toString(FileSystemContext.get().getId()), NetworkAddressUtils.getClientHostName(), new MetricsHeartbeatTOptions(metrics)); return null; } }); }
Source:
public void clientHeartbeat(String clientId, String hostname, List<Metric> metrics) { if (metrics.isEmpty()) { return; START } END mMetricsStore.putClientMetrics(hostname, clientId, metrics); } Could we do this logic in `putClientMetrics`, since it should be able to handle an empty set?

Target:
public void clientHeartbeat(String clientId, String hostname, List<Metric> metrics) { mMetricsStore.putClientMetrics(hostname, clientId, metrics); }
Source:
public static FileSystemContext get() { if (sInstance == null) { synchronized (FileSystemContext.class) { sInstance = create(); } } START return sInstance; END } This is not safe, `sInstance` can be set multiple times?

Target:
public static FileSystemContext get() { if (sInstance == null) { synchronized (FileSystemContext.class) { if (sInstance == null) { sInstance = create(); } } } return sInstance; }
Source:
private static void initializeGauges() { MetricsSystem.registerGaugeIfAbsent(MetricsSystem.getClientMetricName("NettyConnectionsOpen"), new Gauge<Long>() { @Override public Long getValue() { long ret = 0; START for (NettyChannelPool pool : sInstance.mNettyChannelPools.values()) { END ret += pool.size(); } return ret; } }); } We might not always be using `sInstance` right? Based on the current implementation, each context object has its own metrics heartbeat, so this gauge should be based on the context?

Target:
private static void initializeGauges() { MetricsSystem.registerGaugeIfAbsent(MetricsSystem.getClientMetricName("NettyConnectionsOpen"), new Gauge<Long>() { @Override public Long getValue() { long ret = 0; for (NettyChannelPool pool : get().mNettyChannelPools.values()) { ret += pool.size(); } return ret; } }); }
Source:
public MetaCommand masterHeartbeat(long masterId) { MasterInfo master = mMasters.getFirstByField(ID_INDEX, masterId); if (master == null) { LOG.warn("Could not find master id: for heartbeat.", masterId); return MetaCommand.Register; } START synchronized (master) { END master.updateLastUpdatedTimeMs(); } return MetaCommand.Nothing; } is this `synchronized` required?

Target:
public MetaCommand masterHeartbeat(long masterId) { MasterInfo master = mMasters.getFirstByField(ID_INDEX, masterId); if (master == null) { LOG.warn("Could not find master id: for heartbeat.", masterId); return MetaCommand.Register; } master.updateLastUpdatedTimeMs(); return MetaCommand.Nothing; }
Source:
protected void stopMasters() { try { START mWorkerConfigChecker.reset(); END mRegistry.stop(); } catch (IOException e) { throw Throwables.propagate(e); } } Should this be moved to `startMasters`? @aaudiber

Target:
protected void stopMasters() { try { mRegistry.stop(); } catch (IOException e) { throw Throwables.propagate(e); } }
Source:
public alluxio.thrift.Metric toThrift() { alluxio.thrift.Metric metric = new alluxio.thrift.Metric(); metric.setInstance(mInstance); metric.setHostname(mHostname); metric.setName(mName); START if (!(mValue instanceof Integer) && !(mValue instanceof Long)) { END LOG.error("The value of metric %s is neither integer nor long", this); } metric.setValue(((Number) mValue).longValue()); return metric; } can we make `mValue` type `Long`?

Target:
public alluxio.thrift.Metric toThrift() { alluxio.thrift.Metric metric = new alluxio.thrift.Metric(); metric.setInstance(mInstance.toString()); metric.setHostname(mHostname); metric.setName(mName); metric.setValue(mValue); return metric; }
Source:
START protected DefaultMetricsMaster(MasterContext masterContext) { END this(masterContext, new SystemClock(), ExecutorServiceFactories .fixedThreadPoolExecutorServiceFactory(Constants.METRICS_MASTER_NAME, 2)); } why protected?

Target:
DefaultMetricsMaster(MasterContext masterContext) { this(masterContext, new SystemClock(), ExecutorServiceFactories .fixedThreadPoolExecutorServiceFactory(Constants.METRICS_MASTER_NAME, 2)); }
Source:
public long getValue(Map<MetricsFilter, Set<Metric>> map) { long value = 0; for (Metric metric : map.get(mFilter)) { START value += (long) metric.getValue(); END } return value; } is this cast necessary? I thought metrics should always be longs

Target:
public long getValue(Map<MetricsFilter, Set<Metric>> map) { long value = 0; for (Metric metric : map.get(mFilter)) { value += metric.getValue(); } return value; }
Source:
public static Map<String, String> toMap() { START Map<String, String> map = toRawMap(); END // Resolve values with ${VALUE} format String nestedPropIdentifier = "$"; for (Map.Entry<String, String> entry : map.entrySet()) { String value = entry.getValue(); if (value != null && value.contains(nestedPropIdentifier)) { map.put(entry.getKey(), lookup(value)); } } return map; } maybe a simpler approach here and better code reuse? ```java for (PropertyKey key : PropertyKey.defaultKeys()) { map.add(key.getName, get(key)); } return map; ```

Target:
public static Map<String, String> toMap() { Map<String, String> map = toRawMap(); for (Map.Entry<String, String> entry : map.entrySet()) { String value = entry.getValue(); if (value != null) { map.put(entry.getKey(), lookup(value)); } } return map; }
Source:
public AccessControlList getAcl(String path) throws UnimplementedException, IOException { START throw new UnimplementedException("ACL is not supported"); END } Instead of using an exception, could we return some default value?

Target:
public AccessControlList getAcl(String path) throws IOException { return null; }
Source:
public void testMetricsCapacity() { when(mBlockMaster.getCapacityBytes()).thenReturn(1000L); START Assert.assertEquals(1000L, getGauge(Metrics.CAPACITY_TOTAL)); END when(mBlockMaster.getUsedBytes()).thenReturn(200L); Assert.assertEquals(200L, getGauge(Metrics.CAPACITY_USED)); Assert.assertEquals(800L, getGauge(Metrics.CAPACITY_FREE)); } nit: can just use `assertEquals`, similar to using `when` instead of `Mockito.when`

Target:
public void testMetricsCapacity() { when(mBlockMaster.getCapacityBytes()).thenReturn(1000L); assertEquals(1000L, getGauge(Metrics.CAPACITY_TOTAL)); when(mBlockMaster.getUsedBytes()).thenReturn(200L); assertEquals(200L, getGauge(Metrics.CAPACITY_USED)); assertEquals(800L, getGauge(Metrics.CAPACITY_FREE)); }
Source:
public CapacityCommand(BlockMasterClient blockMasterClient, PrintStream printStream) { mBlockMasterClient = blockMasterClient; mPrintStream = printStream; START initValues(); END } what's the reasoning behind using this helper vs initializing in the constructor vs initializing during declaration?

Target:
public CapacityCommand(BlockMasterClient blockMasterClient, PrintStream printStream) { mBlockMasterClient = blockMasterClient; mPrintStream = printStream; }
Source:
public synchronized List<WorkerInfo> getWorkerReport( final GetWorkerReportOptions options) throws IOException { START return retryRPC(new RpcCallable<List<WorkerInfo>>() { END @Override public List<WorkerInfo> call() throws TException { List<WorkerInfo> result = new ArrayList<>(); for (alluxio.thrift.WorkerInfo workerInfo : mClient .getWorkerReport(options.toThrift()).getWorkerInfoList()) { result.add(ThriftUtils.fromThrift(workerInfo)); } return result; } }); } NIT: can you use Java 8 lambda function to simplify the code? https://stackoverflow.com/questions/25270467/java-8-lambda-replace-anonymous-inner-class-by-lambda

Target:
public synchronized List<WorkerInfo> getWorkerReport( final GetWorkerReportOptions options) throws IOException { return retryRPC(() -> { List<WorkerInfo> result = new ArrayList<>(); for (alluxio.thrift.WorkerInfo workerInfo : mClient .getWorkerReport(options.toThrift()).getWorkerInfoList()) { result.add(ThriftUtils.fromThrift(workerInfo)); } return result; }); }
Source:
public GetWorkerInfoListTResponse getWorkerInfoList(GetWorkerInfoListTOptions options) throws AlluxioTException { START return RpcUtils.call(LOG, new RpcCallableThrowsIOException<GetWorkerInfoListTResponse>() { END @Override public GetWorkerInfoListTResponse call() throws AlluxioException, AlluxioStatusException { List<WorkerInfo> workerInfos = new ArrayList<>(); for (alluxio.wire.WorkerInfo workerInfo : mBlockMaster.getWorkerInfoList()) { workerInfos.add(ThriftUtils.toThrift(workerInfo)); } return new GetWorkerInfoListTResponse(workerInfos); } }); } nit: Java 8 lambda function

Target:
public GetWorkerInfoListTResponse getWorkerInfoList(GetWorkerInfoListTOptions options) throws AlluxioTException { return RpcUtils.call(LOG, (RpcCallableThrowsIOException<GetWorkerInfoListTResponse>) () -> { List<WorkerInfo> workerInfos = new ArrayList<>(); for (alluxio.wire.WorkerInfo workerInfo : mBlockMaster.getWorkerInfoList()) { workerInfos.add(ThriftUtils.toThrift(workerInfo)); } return new GetWorkerInfoListTResponse(workerInfos); }); }
Source:
public Options getOptions() { return new Options() START .addOption(LIVE_OPTION) END .addOption(LOST_OPTION) .addOption(SPECIFIED_OPTION) .addOption(HELP_OPTION); } sort

Target:
public Options getOptions() { return new Options() .addOption(HELP_OPTION) .addOption(LIVE_OPTION) .addOption(LOST_OPTION) .addOption(SPECIFIED_OPTION); }
Source:
public String getDescription() { return "Report Alluxio running cluster information.\n" + "Where [category] is an optional argument. If no arguments are passed in, " + "summary information will be printed out.\n" + "[category] can be one of the following:\n" START + " summary cluster summary\n" END + " capacity worker capacity information\n"; } I would sort the lines of these subcommands here

Target:
public String getDescription() { return "Report Alluxio running cluster information.\n" + "Where [category] is an optional argument. If no arguments are passed in, " + "summary information will be printed out.\n" + "[category] can be one of the following:\n" + " capacity worker capacity information\n" + " summary cluster summary\n"; }
Source:
public BlockMasterInfo setCapacityBytesOnTiers(Map<String, Long> capacityBytesOnTiers) { mCapacityBytesOnTiers = new HashMap<>(); for (Map.Entry<String, Long> entry : capacityBytesOnTiers.entrySet()) { long capacity = entry.getValue(); START if (capacity > 0) { END mCapacityBytesOnTiers.put(entry.getKey(), capacity); } } return this; } wire types are basic objects that store and retrieve data using simple getters and setters. There shouldn't be any non-trivial code like this. If we need to filter out negative capacities, it should be done outside of this class. To make a defensive copy, you can use `new HashMap<>(capacityBytesOnTiers)`

Target:
public BlockMasterInfo setCapacityBytesOnTiers(Map<String, Long> capacityBytesOnTiers) { mCapacityBytesOnTiers = new HashMap<>(capacityBytesOnTiers); return this; }
Source:
public int hashCode() { START return Objects.hashCode(mLiveWorkerNum, mLostWorkerNum, mCapacityBytes, mUsedBytes, END mFreeBytes, mCapacityBytesOnTiers, mUsedBytesOnTiers); } nit: can you sort them by member var names? So in the future, adding a new member will have a deterministic location.

Target:
public int hashCode() { return Objects.hashCode(mCapacityBytes, mCapacityBytesOnTiers, mFreeBytes, mLiveWorkerNum, mLostWorkerNum, mUsedBytes, mUsedBytesOnTiers); }
Source:
public RetryHandlingBlockMasterClient(MasterClientConfig conf) { super(conf); START mClient = null; END } class member variable is `null` by default, typically we don't init null value.

Target:
public RetryHandlingBlockMasterClient(MasterClientConfig conf) { super(conf); }
Source:
public synchronized BlockMasterInfo getBlockMasterInfo(final Set<BlockMasterInfoField> fields) throws IOException { return retryRPC(() -> { START Set<alluxio.thrift.BlockMasterInfoField> thriftFields = new HashSet<>(); END if (fields == null) { thriftFields = null; } else { for (BlockMasterInfoField field : fields) { thriftFields.add(field.toThrift()); } } return ThriftUtils.fromThrift( mClient.getBlockMasterInfo(new GetBlockMasterInfoTOptions(thriftFields)) .getBlockMasterInfo()); }); } nit: to simplify the code and a minor optimization (avoid creating unused `HashSet` instance): ```java Set<alluxio.thrift.BlockMasterInfoField> thriftFields = null; if (fields != null) { thriftFields = new HashSet<>(); for (BlockMasterInfoField field : fields) { ... } } ```

Target:
public synchronized BlockMasterInfo getBlockMasterInfo(final Set<BlockMasterInfoField> fields) throws IOException { return retryRPC(() -> { Set<alluxio.thrift.BlockMasterInfoField> thriftFields = null; if (fields != null) { thriftFields = new HashSet<>(); for (BlockMasterInfoField field : fields) { thriftFields.add(field.toThrift()); } } return ThriftUtils.fromThrift( mClient.getBlockMasterInfo(new GetBlockMasterInfoTOptions(thriftFields)) .getBlockMasterInfo()); }); }
Source:
public void validateArgs(String... args) throws InvalidArgumentException { START if (args.length > 2) { END throw new InvalidArgumentException( ExceptionMessage.INVALID_ARGS_GENERIC.getMessage(getCommandName())); } } isn't there only one arg, summary?

Target:
public void validateArgs(String... args) throws InvalidArgumentException { if (args.length > 1) { throw new InvalidArgumentException( ExceptionMessage.INVALID_ARGS_GENERIC.getMessage(getCommandName())); } }
Source:
public ReportCommand() { mMetaMasterClient = new RetryHandlingMetaMasterClient(MasterClientConfig.defaults()); mBlockMasterClient = new RetryHandlingBlockMasterClient(MasterClientConfig.defaults()); START mPrintStream = new PrintStream(System.out); END } why do we wrap `System.out` inside another `PrintStream`?

Target:
public ReportCommand() { mMetaMasterClient = new RetryHandlingMetaMasterClient(MasterClientConfig.defaults()); mBlockMasterClient = new RetryHandlingBlockMasterClient(MasterClientConfig.defaults()); mPrintStream = System.out; }
Source:
public boolean remove(@Nullable Object object) { if (object == null) { return false; } START assert object != null : "@AssumeAssertion(nullness)"; END // Locking this object protects against removing the exact object that might be in the // process of being added, but does not protect against removing a distinct, but equivalent // object. synchronized (object) { if (mPrimaryIndex != null && mPrimaryIndex.containsObject((T) object)) { // This isn't technically typesafe. However, given that success is true, it's very unlikely // that the object passed to remove is not of type <T>. @SuppressWarnings("unchecked") T tObj = (T) object; removeFromIndices(tObj); return true; } else { return false; } } } is the above null check not enough to convince the checker framework that `object` is non-null at this point?

Target:
public boolean remove(@Nullable Object object) { if (object == null) { return false; } // Locking this object protects against removing the exact object that might be in the // process of being added, but does not protect against removing a distinct, but equivalent // object. synchronized (object) { if (mPrimaryIndex.containsObject((T) object)) { // This isn't technically typesafe. However, given that success is true, it's very unlikely // that the object passed to remove is not of type <T>. @SuppressWarnings("unchecked") T tObj = (T) object; removeFromIndices(tObj); return true; } else { return false; } } }
Source:
public Options getOptions() { return new Options() .addOption(RECURSIVE_OPTION) .addOption(FORCE_OPTION) .addOption(LIST_DIR_AS_FILE_OPTION) .addOption(LIST_PINNED_FILES_OPTION) .addOption(LIST_HUMAN_READABLE_OPTION) START .addOption(SORT_OPTION) END .addOption(REVERSE_SORT_OPTION); } this wasn't alphabetized before, but it would be nice to now

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); }
Source:
public boolean isInSafeMode() { // bails out early before expensive clock checks if (!mWorkerConnectWaitStartTimeMs.isMarked()) { return false; } Long startTime = mWorkerConnectWaitStartTimeMs.getReference(); if (startTime == null) { // master has not started waiting for workers yet return true; } // lazily updates safe mode state upon inquiry long waitTime = Configuration.getMs(PropertyKey.MASTER_WORKER_CONNECT_WAIT_TIME); if (mClock.millis() - startTime < waitTime) { return true; } START return !mWorkerConnectWaitStartTimeMs.compareAndSet(startTime, null, true, false); END } is this return value correct? If another thread comes in and brings us out of safe mode first, this will say we're still in safe mode.

Target:
public boolean isInSafeMode() { // bails out early before expensive clock checks if (!mWorkerConnectWaitStartTimeMs.isMarked()) { return false; } Long startTime = mWorkerConnectWaitStartTimeMs.getReference(); if (startTime == null) { // master has not started waiting for workers yet return true; } // lazily updates safe mode state upon inquiry long waitTime = Configuration.getMs(PropertyKey.MASTER_WORKER_CONNECT_WAIT_TIME); if (mClock.millis() - startTime < waitTime) { return true; } mWorkerConnectWaitStartTimeMs.compareAndSet(startTime, null, true, false); return mWorkerConnectWaitStartTimeMs.isMarked(); }
Source:
public boolean validate(Map<String, String> optionsMap) { START if (shouldSkip()) { END return true; } if (!validateHdfsSettingParity(optionsMap)) { return false; } return true; } Since this method is defined on the interface, it would make more sense for the ValidateEnv class to call this method and skip the task instead of doing it in each task.

Target:
public boolean validate(Map<String, String> optionsMap) { if (!validateHdfsSettingParity(optionsMap)) { return false; } return true; }
Source:
protected void startServing(String startMessage, String stopMessage) { MetricsSystem.startSinks(); START mSafeModeManager.notifyRpcServerStarted(); END startServingWebServer(); startJvmMonitorProcess(); LOG.info("Alluxio master version started{}. " + "bindHost={}, connectHost={}, rpcPort={}, webPort={}", RuntimeConstants.VERSION, startMessage, NetworkAddressUtils.getBindAddress(ServiceType.MASTER_RPC), NetworkAddressUtils.getConnectAddress(ServiceType.MASTER_RPC), NetworkAddressUtils.getPort(ServiceType.MASTER_RPC), NetworkAddressUtils.getPort(ServiceType.MASTER_WEB)); startServingRPCServer(); LOG.info("Alluxio master ended{}", stopMessage); } we should do this immediately before calling `mThriftServer.serve()` in `startServingRPCServer`

Target:
protected void startServing(String startMessage, String stopMessage) { MetricsSystem.startSinks(); startServingWebServer(); startJvmMonitorProcess(); LOG.info("Alluxio master version started{}. " + "bindHost={}, connectHost={}, rpcPort={}, webPort={}", RuntimeConstants.VERSION, startMessage, NetworkAddressUtils.getBindAddress(ServiceType.MASTER_RPC), NetworkAddressUtils.getConnectAddress(ServiceType.MASTER_RPC), NetworkAddressUtils.getPort(ServiceType.MASTER_RPC), NetworkAddressUtils.getPort(ServiceType.MASTER_WEB)); startServingRPCServer(); LOG.info("Alluxio master ended{}", stopMessage); }
Source:
private void init(long offset) throws BlockDoesNotExistException, IOException { UnderFileSystem ufs = mUfsManager.get(mBlockMeta.getMountId()).getUfs(); ufs.connectFromWorker( NetworkAddressUtils.getConnectHost(NetworkAddressUtils.ServiceType.WORKER_RPC)); try { updateUnderFileSystemInputStream(offset); } catch (IOException e) { throw new BlockDoesNotExistException( ExceptionMessage.UFS_PATH_DOES_NOT_EXIST.getMessage(mBlockMeta.getUnderFileSystemPath()), START e); END } updateBlockWriter(offset); } Is it guaranteed that the `IOException` will be because of the path not existing?

Target:
private void init(long offset) throws IOException { UnderFileSystem ufs = mUfsManager.get(mBlockMeta.getMountId()).getUfs(); ufs.connectFromWorker( NetworkAddressUtils.getConnectHost(NetworkAddressUtils.ServiceType.WORKER_RPC)); updateUnderFileSystemInputStream(offset); updateBlockWriter(offset); }
Source:
protected ObjectUnderFileSystem(AlluxioURI uri, UnderFileSystemConfiguration ufsConf) { super(uri, ufsConf); int numThreads = Configuration.getInt(PropertyKey.UNDERFS_OBJECT_STORE_SERVICE_THREADS); mExecutorService = ExecutorServiceFactories.fixedThreadPoolExecutorServiceFactory( "alluxio-underfs-object-service-worker", numThreads).create(); mOpenRetryBaseSleepMs = (int) Configuration.getMs(PropertyKey.UNDERFS_OBJECT_STORE_READ_RETRY_BASE_SLEEP_MS); mOpenRetryMaxSleepMs = (int) Configuration.getMs(PropertyKey.UNDERFS_OBJECT_STORE_READ_RETRY_MAX_SLEEP_MS); mOpenRetryMaxNum = START Configuration.getInt(PropertyKey.UNDERFS_OBJECT_STORE_READ_RETRY_MAX_NUM); END } Is the configuration call expensive? Otherwise I suggest to just do this lookup in the open call since the values will not be used anywhere else.

Target:
protected ObjectUnderFileSystem(AlluxioURI uri, UnderFileSystemConfiguration ufsConf) { super(uri, ufsConf); int numThreads = Configuration.getInt(PropertyKey.UNDERFS_OBJECT_STORE_SERVICE_THREADS); mExecutorService = ExecutorServiceFactories.fixedThreadPoolExecutorServiceFactory( "alluxio-underfs-object-service-worker", numThreads).create(); }
Source:
public long getPos() { try { return ((FSDataInputStream) in).getPos(); } catch (IOException e) { START throw new RuntimeException("Failed to get position " + e); END } } Is this a good idea, this will kill the thread arbitrarily?

Target:
public long getPos() throws IOException { return ((FSDataInputStream) in).getPos(); }
Source:
public static LocalAlluxioMaster create(final String workDirectory) throws IOException { if (!Files.isDirectory(Paths.get(workDirectory))) { Files.createDirectory(Paths.get(workDirectory)); } START Configuration.set(PropertyKey.MASTER_SAFEMODE_WAIT, "0ms"); END return new LocalAlluxioMaster(); } Move this to ConfigurationTestUtils#testConfigurationDefaults

Target:
public static LocalAlluxioMaster create(final String workDirectory) throws IOException { if (!Files.isDirectory(Paths.get(workDirectory))) { Files.createDirectory(Paths.get(workDirectory)); } return new LocalAlluxioMaster(); }
Source:
public void addInodeDirectoryFromJournal(InodeDirectoryEntry entry) throws AccessControlException { InodeDirectory directory = InodeDirectory.fromJournalEntry(entry); if (directory.getName().equals(ROOT_INODE_NAME)) { // This is the root inode. Clear all the state, and set the root. reset(); setRoot(directory); // If journal entry has no security enabled, change the replayed inode permission to be 0777 // for backwards-compatibility. START if (SecurityUtils.isSecurityEnabled() && entry.hasMode()) { END mRoot.setMode(Constants.DEFAULT_FILE_SYSTEM_MODE); } } else { addInodeFromJournalInternal(directory, entry.hasMode()); } } should this be !entry.hasMode()?

Target:
public void addInodeDirectoryFromJournal(InodeDirectoryEntry entry) throws AccessControlException { InodeDirectory directory = InodeDirectory.fromJournalEntry(entry); if (directory.getName().equals(ROOT_INODE_NAME)) { // This is the root inode. Clear all the state, and set the root. reset(); setRoot(directory); } else { addInodeFromJournalInternal(directory); } }