auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public boolean shutdownBlobStore(PartitionId id) { START CloudBlobStore blobStore = partitionToStore.getOrDefault(id, null); END if (blobStore == null) { return false; } blobStore.shutdown(); return true; } just `partitionToStore.get()` should return null if the element is not present.

Target:
public boolean shutdownBlobStore(PartitionId id) { CloudBlobStore blobStore = partitionToStore.get(id); if (blobStore == null) { return false; } blobStore.shutdown(); return true; }
Source:
public boolean addBlobStore(ReplicaId replica) { CloudBlobStore cloudBlobStore = partitionToStore.computeIfAbsent(replica.getPartitionId(), store -> new CloudBlobStore(properties, replica.getPartitionId(), cloudDestination, clusterMap, vcrMetrics)); START partitionToStore.put(replica.getPartitionId(), cloudBlobStore); END return startBlobStore(replica.getPartitionId()); } I think that computeIfAbsent will already insert the entry into the map after computing so this call might not be needed

Target:
public boolean addBlobStore(ReplicaId replica) { CloudBlobStore cloudBlobStore = partitionToStore.computeIfAbsent(replica.getPartitionId(), store -> new CloudBlobStore(properties, replica.getPartitionId(), cloudDestination, clusterMap, vcrMetrics)); return startBlobStore(replica.getPartitionId()); }
Source:
private void ensureUnequal(CloudFindToken token1, CloudFindToken token2) { assertFalse("Tokens should match", token1.equals(token2)); START assertFalse("Tokens should match", token1.hashCode() == token2.hashCode()); END } Can remove hashcode check since conflicts are possible.

Target:
private void ensureUnequal(CloudFindToken token1, CloudFindToken token2) { assertFalse("Tokens should match", token1.equals(token2)); }
Source:
Map<String, String> fetchAccountMetadata() { long startTimeMs = System.currentTimeMillis(); logger.trace("Start reading ZNRecord from path={}", znRecordPath); ZNRecord znRecord = helixStore.get(znRecordPath, null, AccessOption.PERSISTENT); logger.trace("Fetched ZNRecord from path={}, took time={} ms", znRecordPath, startTimeMs); if (znRecord == null) { START logger.debug("The ZNRecord to read does not exist on path={}", znRecordPath); END return null; } return fetchAccountMetadataFromZNRecord(znRecord); } minor: I think here can be `warn`

Target:
Map<String, String> fetchAccountMetadata() { long startTimeMs = System.currentTimeMillis(); logger.trace("Start reading ZNRecord from path={}", znRecordPath); ZNRecord znRecord = helixStore.get(znRecordPath, null, AccessOption.PERSISTENT); logger.trace("Fetched ZNRecord from path={}, took time={} ms", znRecordPath, startTimeMs); if (znRecord == null) { logger.info("The ZNRecord to read does not exist on path={}", znRecordPath); return null; } return fetchAccountMetadataFromZNRecord(znRecord); }
Source:
public ResourceResponse<Document> upsertMetadata(CloudBlobMetadata blobMetadata) throws DocumentClientException { RequestOptions options = getRequestOptions(blobMetadata.getPartitionId()); return retryOperationWithThrottling( START () -> documentClient.upsertDocument(cosmosCollectionLink, blobMetadata, null, true), END azureMetrics.documentCreateTime); } We do need the options to contain the partition key. We will need to debug the error cause and see what the two different values are. Can you bring up in debugger and set a breakpoint there?

Target:
public ResourceResponse<Document> upsertMetadata(CloudBlobMetadata blobMetadata) throws DocumentClientException { RequestOptions options = getRequestOptions(blobMetadata.getPartitionId()); return retryOperationWithThrottling( () -> documentClient.upsertDocument(cosmosCollectionLink, blobMetadata, options, true), azureMetrics.documentCreateTime); }
Source:
public CosmosDataAccessor(DocumentClient documentClient, AzureCloudConfig azureCloudConfig, AzureMetrics azureMetrics) { START this.documentClient = documentClient; END this.cosmosCollectionLink = azureCloudConfig.cosmosCollectionLink; this.azureMetrics = azureMetrics; this.maxRetries = azureCloudConfig.cosmosMaxRetries; } ```CosmosDataAccessor(documentClient,azureCloudConfig.cosmosCollectionLink, azureMetrics, azureCloudConfig.cosmosMaxRetries)``` ?

Target:
public CosmosDataAccessor(DocumentClient documentClient, AzureCloudConfig azureCloudConfig, AzureMetrics azureMetrics) { this(documentClient, azureCloudConfig.cosmosCollectionLink, azureCloudConfig.cosmosMaxRetries, azureMetrics); }
Source:
void onErrorResponse(ReplicaId replicaId, RouterException exception) { START logger.info("DC: " + replicaId.getDataNodeId().getDatacenterName() + " exception: " + exception); END operationTracker.onResponse(replicaId, TrackedRequestFinalState.fromRouterErrorCodeToFinalState(exception.getErrorCode())); setOperationException(exception); routerMetrics.routerRequestErrorCount.inc(); routerMetrics.getDataNodeBasedMetrics(replicaId.getDataNodeId()).getBlobInfoRequestErrorCount.inc(); } `logger.error` ?

Target:
private void onErrorResponse(ReplicaId replicaId, RouterException exception) { operationTracker.onResponse(replicaId, TrackedRequestFinalState.fromRouterErrorCodeToFinalState(exception.getErrorCode())); setOperationException(exception); routerMetrics.routerRequestErrorCount.inc(); routerMetrics.getDataNodeBasedMetrics(replicaId.getDataNodeId()).getBlobInfoRequestErrorCount.inc(); }
Source:
START void onErrorResponse(ReplicaId replicaId, RouterException exception) { END logger.info("Get exception: " + exception); operationTracker.onResponse(replicaId, TrackedRequestFinalState.fromRouterErrorCodeToFinalState(exception.getErrorCode())); setOperationException(exception); if (exception.getErrorCode() != RouterErrorCode.BlobDeleted && exception.getErrorCode() != RouterErrorCode.BlobExpired) { routerMetrics.routerRequestErrorCount.inc(); } routerMetrics.getDataNodeBasedMetrics(replicaId.getDataNodeId()).deleteRequestErrorCount.inc(); } This is method could be private.

Target:
private void onErrorResponse(ReplicaId replicaId, RouterException exception) { operationTracker.onResponse(replicaId, TrackedRequestFinalState.fromRouterErrorCodeToFinalState(exception.getErrorCode())); setOperationException(exception); routerMetrics.routerRequestErrorCount.inc(); routerMetrics.getDataNodeBasedMetrics(replicaId.getDataNodeId()).deleteRequestErrorCount.inc(); }
Source:
public void onResponse(ReplicaId replicaId, TrackedRequestFinalState trackedRequestFinalState) { if (respondedReplicas.contains(replicaId)) { return; } respondedReplicas.add(replicaId); inflightCount--; if (trackedRequestFinalState == TrackedRequestFinalState.SUCCESS) { succeededCount++; } else { failedCount++; // NOT_FOUND is a special error. When tracker sees more than 2 NOT_FOUND from the originating DC, we can // be sure the operation will ends up with a NOT_FOUND error. if (trackedRequestFinalState == TrackedRequestFinalState.NOT_FOUND && replicaId.getDataNodeId() .getDatacenterName() START .equals(this.originatingDcName)) { END originatingDcNotFoundCount++; } } } `this` can be removed

Target:
public void onResponse(ReplicaId replicaId, TrackedRequestFinalState trackedRequestFinalState) { inflightCount--; if (trackedRequestFinalState == TrackedRequestFinalState.SUCCESS) { succeededCount++; } else { failedCount++; // NOT_FOUND is a special error. When tracker sees more than 2 NOT_FOUND from the originating DC, we can // be sure the operation will end up with a NOT_FOUND error. if (trackedRequestFinalState == TrackedRequestFinalState.NOT_FOUND && replicaId.getDataNodeId() .getDatacenterName() .equals(originatingDcName)) { originatingDcNotFoundCount++; } } }
Source:
void setOperationException(RouterException exception) { START RouterUtils.replaceOperationException(operationException, (RouterException) exception, this::getPrecedenceLevel); END } You don't need to cast here since you're accepting a `RouterException`

Target:
void setOperationException(RouterException exception) { RouterUtils.replaceOperationException(operationException, exception, this::getPrecedenceLevel); }
Source:
private static RestRequestMetricsGroup getMetricsGroupForGet(FrontendMetrics frontendMetrics, SubResource subResource) { RestRequestMetricsGroup group = null; if (subResource == null) { group = frontendMetrics.getBlobMetricsGroup; } else { switch (subResource) { case BlobInfo: group = frontendMetrics.getBlobInfoMetricsGroup; break; case UserMetadata: group = frontendMetrics.getUserMetadataMetricsGroup; break; case Replicas: group = frontendMetrics.getReplicasMetricsGroup; break; case Segment: START group = frontendMetrics.getSegmentMetricsGroup; END break; } } return group; } discussed offline. probably better to use getBlobMetricsGroup here to keep metrics grouping consistent with range requests.

Target:
private static RestRequestMetricsGroup getMetricsGroupForGet(FrontendMetrics frontendMetrics, SubResource subResource) { RestRequestMetricsGroup group = null; if (subResource == null || subResource.equals(SubResource.Segment)) { group = frontendMetrics.getBlobMetricsGroup; } else { switch (subResource) { case BlobInfo: group = frontendMetrics.getBlobInfoMetricsGroup; break; case UserMetadata: group = frontendMetrics.getUserMetadataMetricsGroup; break; case Replicas: group = frontendMetrics.getReplicasMetricsGroup; break; } } return group; }
Source:
public void testBadSegmentSubResourceNumber() throws UnsupportedEncodingException, URISyntaxException { String requestPath = "/mediaexpectedOpOrId/Segment/notAnInteger"; List<String> prefixesToRemove = new ArrayList<>(); RestRequest restRequest = RestUtilsTest.createRestRequest(RestMethod.GET, requestPath, null); try { RequestPath.parse(restRequest, prefixesToRemove, CLUSTER_NAME); } catch (RestServiceException e) { assertTrue(e.getErrorCode().equals(RestServiceErrorCode.BadRequest)); //expected return; } START fail(); END } minor: move `fail()` to line 144 and remove `return` at line 147

Target:
public void testBadSegmentSubResourceNumber() throws UnsupportedEncodingException, URISyntaxException { String requestPath = "/mediaexpectedOpOrId/Segment/notAnInteger"; List<String> prefixesToRemove = new ArrayList<>(); RestRequest restRequest = RestUtilsTest.createRestRequest(RestMethod.GET, requestPath, null); try { RequestPath.parse(restRequest, prefixesToRemove, CLUSTER_NAME); fail(); } catch (RestServiceException e) { assertTrue(e.getErrorCode().equals(RestServiceErrorCode.BadRequest)); } }
Source:
private CompositeBlobInfo deserializeMetadataContentV3(ByteBuffer metadataContent, StoreKeyFactory storeKeyFactory) START throws MessageFormatException, IOException { END ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(metadataContent); DataInputStream inputStream = new DataInputStream(byteBufferInputStream); short metadataContentVersion = inputStream.readShort(); Assert.assertEquals("Metadata Content Version mismatch ", MessageFormatRecord.Metadata_Content_Version_V3, metadataContentVersion); return MessageFormatRecord.Metadata_Content_Format_V3.deserializeMetadataContentRecord(inputStream, storeKeyFactory); } no `MessageFormatException` thrown from this method

Target:
private CompositeBlobInfo deserializeMetadataContentV3(ByteBuffer metadataContent, StoreKeyFactory storeKeyFactory) throws IOException { ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(metadataContent); DataInputStream inputStream = new DataInputStream(byteBufferInputStream); short metadataContentVersion = inputStream.readShort(); Assert.assertEquals("Metadata Content Version mismatch ", MessageFormatRecord.Metadata_Content_Version_V3, metadataContentVersion); return MessageFormatRecord.Metadata_Content_Format_V3.deserializeMetadataContentRecord(inputStream, storeKeyFactory); }
Source:
public void participate(InstanceType role) throws Exception { START manager = HelixManagerFactory.getZKHelixManager(vcrClusterName, vcrInstanceName, InstanceType.PARTICIPANT, END cloudConfig.vcrClusterZkConnectString); manager.getStateMachineEngine() .registerStateModelFactory(LeaderStandbySMD.name, new HelixVcrStateModelFactory(this)); manager.connect(); helixAdmin = manager.getClusterManagmentTool(); logger.info("Participate HelixVcrCluster as successfully.", role); } use role

Target:
public void participate(InstanceType role) throws Exception { manager = HelixManagerFactory.getZKHelixManager(vcrClusterName, vcrInstanceName, role, cloudConfig.vcrClusterZkConnectString); manager.getStateMachineEngine() .registerStateModelFactory(LeaderStandbySMD.name, new HelixVcrStateModelFactory(this)); manager.connect(); helixAdmin = manager.getClusterManagmentTool(); logger.info("Participate HelixVcrCluster as successfully.", role); }
Source:
boolean meetRequirement(Long value) throws NullPointerException { Objects.requireNonNull(value, "Input value is empty which cannot be applied to threshold check."); START return boundType == BoundType.UpperBound ? value < threshold : value > threshold; END } I would make it <= (allow equal case to meet requirement). Is there a specific reason to be strict about it?

Target:
boolean meetRequirement(Long value) throws NullPointerException { Objects.requireNonNull(value, "Input value is empty which cannot be applied to threshold check."); return boundType == BoundType.UpperBound ? value <= threshold : value >= threshold; }
Source:
START public VirtualReplicatorCluster getVirtualReplicatorCluster() { END if (virtualReplicatorCluster == null) { virtualReplicatorCluster = new StaticVcrCluster(cloudConfig, clusterMapConfig, clusterMap); } return virtualReplicatorCluster; } Singleton isn't threadsafe. We're not worried about multiple threads calling this method?

Target:
synchronized public VirtualReplicatorCluster getVirtualReplicatorCluster() { if (virtualReplicatorCluster == null) { virtualReplicatorCluster = new StaticVcrCluster(cloudConfig, clusterMapConfig, clusterMap); } return virtualReplicatorCluster; }
Source:
CloudBlobStore(PartitionId partitionId, CloudConfig cloudConfig, CloudDestination cloudDestination) { this.partitionId = Objects.requireNonNull(partitionId, "partitionId is required"); this.cloudConfig = Objects.requireNonNull(cloudConfig, "cloudConfig is required"); this.cloudDestination = Objects.requireNonNull(cloudDestination, "cloudDestination is required"); START minTtlMillis = cloudConfig.vcrMinTtlDays * TimeUnit.DAYS.toMillis(1); END } `TimeUnit.DAYS.toMillis(cloudConfig.vcrMinTtlDays)`

Target:
CloudBlobStore(PartitionId partitionId, CloudConfig cloudConfig, CloudDestination cloudDestination) { this.partitionId = Objects.requireNonNull(partitionId, "partitionId is required"); this.cloudConfig = Objects.requireNonNull(cloudConfig, "cloudConfig is required"); this.cloudDestination = Objects.requireNonNull(cloudDestination, "cloudDestination is required"); minTtlMillis = TimeUnit.DAYS.toMillis(cloudConfig.vcrMinTtlDays); }
Source:
public String toString() { START return "CloudReplica[" + dataNodeId.getHostname() + ":" + getDataNodeId().getPort() + ":" + getReplicaPath() + "]"; END } Still calling getDataNodeId() for the port.

Target:
public String toString() { return "CloudReplica[" + dataNodeId.getHostname() + ":" + dataNodeId.getPort() + ":" + getReplicaPath() + "]"; }
Source:
START private BlobId addBlobToSet(MockMessageWriteSet messageWriteSet, long size, long expiresAtMs, short accountId, END short containerId) throws StoreException { BlobId id = getUniqueId(accountId, containerId); long crc = random.nextLong(); MessageInfo info = new MessageInfo(id, size, false, false, expiresAtMs, crc, accountId, containerId, Utils.Infinite_Time); ByteBuffer buffer = ByteBuffer.wrap(TestUtils.getRandomBytes((int) size)); messageWriteSet.add(info, buffer); return id; } `throws StoreException` is not needed.

Target:
private BlobId addBlobToSet(MockMessageWriteSet messageWriteSet, long size, long expiresAtMs, short accountId, short containerId) { BlobId id = getUniqueId(accountId, containerId); long crc = random.nextLong(); MessageInfo info = new MessageInfo(id, size, false, false, expiresAtMs, crc, accountId, containerId, Utils.Infinite_Time); ByteBuffer buffer = ByteBuffer.wrap(TestUtils.getRandomBytes((int) size)); messageWriteSet.add(info, buffer); return id; }
Source:
private Callback<Long> fetchAccountUpdateBodyCallback(CopyingAsyncWritableChannel channel) { return buildCallback(frontendMetrics.postAccountsReadRequestMetrics, bytesRead -> { updateAccounts(FrontendUtils.readJsonFromChannel(channel)); restResponseChannel.setHeader(RestUtils.Headers.DATE, new GregorianCalendar().getTime()); restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, 0); START finalCallback.onCompletion(null, null); END }, uri, LOGGER, finalCallback); } Correct me if I am wrong. If `updateAccounts` throws exception, where it would be handled? And the exception parameter is still null in `onCompletion` ? Same question in `GetAccountsHandler`.

Target:
private Callback<Long> fetchAccountUpdateBodyCallback(CopyingAsyncWritableChannel channel) { return buildCallback(frontendMetrics.postAccountsReadRequestMetrics, bytesRead -> { updateAccounts(readJsonFromChannel(channel)); restResponseChannel.setHeader(RestUtils.Headers.DATE, new GregorianCalendar().getTime()); restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, 0); finalCallback.onCompletion(null, null); }, uri, LOGGER, finalCallback); }
Source:
private static void assertObjectsAreDistinct(GetBlobOptions a, GetBlobOptions b) { assertThat("GetBlobOptions should not be equal.", a, not(b)); START assertThat("GetBlobOptions hashcodes should not be equal", a.hashCode(), not(b.hashCode())); END assertThat("GetBlobOptions toString should not be equal", a.toString(), not(b.toString())); } hashcode can be equal even if the objects aren't - this test is not correct and may lead to spurious failures

Target:
private static void assertObjectsAreDistinct(GetBlobOptions a, GetBlobOptions b) { assertThat("GetBlobOptions should not be equal.", a, not(b)); assertThat("GetBlobOptions toString should not be equal", a.toString(), not(b.toString())); // Note: don't compare hashcodes since collisions are possible. }
Source:
private void handlePutManagerClosed(BlobProperties blobProperties, boolean stitchOperation, FutureResult<String> futureResult, Callback<String> callback) { RouterException routerException = new RouterException("Cannot accept operation because Router is closed", RouterErrorCode.RouterClosed); routerMetrics.operationDequeuingRate.mark(); START routerMetrics.onPutBlobError(routerException, blobProperties != null && blobProperties.isEncrypted(), END stitchOperation); completeOperation(futureResult, callback, null, routerException); // Close so that any existing operations are also disposed off. close(); } `blobProperties != null` could be removed, because it has been checked in all callers.

Target:
private void handlePutManagerClosed(BlobProperties blobProperties, boolean stitchOperation, FutureResult<String> futureResult, Callback<String> callback) { RouterException routerException = new RouterException("Cannot accept operation because Router is closed", RouterErrorCode.RouterClosed); routerMetrics.operationDequeuingRate.mark(); routerMetrics.onPutBlobError(routerException, blobProperties.isEncrypted(), stitchOperation); completeOperation(futureResult, callback, null, routerException); // Close so that any existing operations are also disposed off. close(); }
Source:
public static List<Object[]> data() { return Arrays.asList( START new Object[][]{{SimpleOperationTracker.class.getSimpleName(), false}, {SimpleOperationTracker.class.getSimpleName(), true}, {AdaptiveOperationTracker.class.getSimpleName(), false}, {AdaptiveOperationTracker.class.getSimpleName(), true}}); END } need only one of the two cases for encrypted = true (either simple or adaptive). I don't believe both are useful - what do you think?

Target:
public static List<Object[]> data() { return Arrays.asList( new Object[][]{{SimpleOperationTracker.class.getSimpleName(), false}, {AdaptiveOperationTracker.class.getSimpleName(), false}, {AdaptiveOperationTracker.class.getSimpleName(), true}}); }
Source:
public JSONObject getSnapshot() { START return new JSONObject(); END } just curious why here returns new `JSONObject`, however, in previous test, `null` is directly returned?

Target:
public JSONObject getSnapshot() { return null; }
Source:
public void testConstructAccountFromJson() throws Exception { START System.out.println(refAccountJson); END assertAccountAgainstReference(Account.fromJson(refAccountJson), true, true); } need to remove this

Target:
public void testConstructAccountFromJson() throws Exception { assertAccountAgainstReference(Account.fromJson(refAccountJson), true, true); }
Source:
START public DiskManager getDiskManager(PartitionId id) { END return partitionToDiskManager.get(id); } why is this public?

Target:
DiskManager getDiskManager(PartitionId id) { return partitionToDiskManager.get(id); }
Source:
public void after() throws InterruptedException { START if (storageManager != null) { END storageManager.shutdown(); } } If `storageManager` gets a value in constructor, then we don't need to check `null`.

Target:
public void after() throws InterruptedException { storageManager.shutdown(); }
Source:
START static Long getXid(InstanceConfig instanceConfig) { END String xid = instanceConfig.getRecord().getSimpleField(XID_STR); return xid == null ? Long.MIN_VALUE : Long.valueOf(xid); } minor: some of the variables that use this are `long` and some are `Long`. I think `long` will be good everywhere? (since this cannot be `null`)

Target:
static long getXid(InstanceConfig instanceConfig) { String xid = instanceConfig.getRecord().getSimpleField(XID_STR); return xid == null ? Long.MIN_VALUE : Long.valueOf(xid); }
Source:
public HelixAccountServiceFactory(VerifiableProperties verifiableProperties, MetricRegistry metricRegistry) { storeConfig = new HelixPropertyStoreConfig(verifiableProperties); accountServiceConfig = new HelixAccountServiceConfig(verifiableProperties); accountServiceMetrics = new AccountServiceMetrics(metricRegistry); START this.notifier = END accountServiceConfig.zkClientConnectString.equals(HelixAccountServiceConfig.INVALID_ZK_CLIENT_CONNECT_STRING) ? null : new HelixNotifier(accountServiceConfig.zkClientConnectString, storeConfig); } minor: don't need `this.`

Target:
public HelixAccountServiceFactory(VerifiableProperties verifiableProperties, MetricRegistry metricRegistry) { this(new HelixPropertyStoreConfig(verifiableProperties), new HelixAccountServiceConfig(verifiableProperties), new AccountServiceMetrics(metricRegistry), new HelixNotifier(new HelixAccountServiceConfig(verifiableProperties).zkClientConnectString, new HelixPropertyStoreConfig(verifiableProperties))); }
Source:
START void checkExpectedRouterErrorCode(RouterErrorCode routerErrorCode) throws Exception { END try { future.get(AWAIT_TIMEOUT, TimeUnit.SECONDS); Assert.fail("Blob should have failed in operation: " + getOperationName()); } catch (Exception e) { Assert.assertTrue("Expect RouterException", e.getCause() instanceof RouterException); Assert.assertEquals("RouterErrorCode doesn't match.", routerErrorCode, ((RouterException) e.getCause()).getErrorCode()); } } it seems the `Exception` is unnecessary, we can remove it.

Target:
void checkExpectedRouterErrorCode(RouterErrorCode routerErrorCode) { try { future.get(AWAIT_TIMEOUT, TimeUnit.SECONDS); Assert.fail("Blob should have failed in operation: " + getOperationName()); } catch (Exception e) { Assert.assertTrue("Expect RouterException", e.getCause() instanceof RouterException); Assert.assertEquals("RouterErrorCode doesn't match.", routerErrorCode, ((RouterException) e.getCause()).getErrorCode()); } }
Source:
START public int appendFromDirectly(byte[] byteArray, int offset, int length) throws IOException { END rollOverIfRequired(length); return activeSegment.appendFromDirectly(byteArray, offset, length); } this doesn't need to be `public` right?

Target:
int appendFromDirectly(byte[] byteArray, int offset, int length) throws IOException { rollOverIfRequired(length); return activeSegment.appendFromDirectly(byteArray, offset, length); }
Source:
void readIntoDirectly(byte[] byteArray, long position, int length) throws IOException { validateReadRange(position, length); START if (file.length() < position + length) { END throw new IOException( "Requested range exceed file length. position = " + position + " length = " + length + "fileLength = " + file.length()); } // TODO: Try to avoid opening file on every opeartion. try (DirectRandomAccessFile directFile = new DirectRandomAccessFile(file, "r", 2 * 1024 * 1024)) { directFile.seek(position); directFile.read(byteArray, 0, length); } } isn't this check already done in `validateReadRange()`?

Target:
void readIntoDirectly(byte[] byteArray, long position, int length) throws IOException { validateReadRange(position, length); // TODO: Try to avoid opening file on every opeartion. try (DirectRandomAccessFile directFile = new DirectRandomAccessFile(file, "r", 2 * 1024 * 1024)) { directFile.seek(position); directFile.read(byteArray, 0, length); } }
Source:
START private BlobId createBlobId(String hexBlobId) throws IOException { END return new BlobId(hexBlobId, clusterMap); } minor: don't think a function is required

Target:
private BlobId createBlobId(String hexBlobId) throws IOException { if (hexBlobId == null) { return null; } return new BlobId(hexBlobId, clusterMap); }
Source:
public HelixClusterManager getClusterMap() throws IOException { if (helixClusterManager == null) { START ZkClient zkClient = END new ZkClient(helixPropertyStoreConfig.zkClientConnectString, helixPropertyStoreConfig.zkClientSessionTimeoutMs, helixPropertyStoreConfig.zkClientConnectionTimeoutMs, new ZNRecordSerializer()); List<String> subscribedPaths = Collections.singletonList(helixPropertyStoreConfig.rootPath); helixPropertyStore = new ZkHelixPropertyStore<>(new ZkBaseDataAccessor<>(zkClient), helixPropertyStoreConfig.rootPath, subscribedPaths); helixClusterManager = new HelixClusterManager(clusterMapConfig, helixPropertyStore, instanceName, helixFactory, metricRegistry); } return helixClusterManager; } I don't think that HelixClusterManager needs to be passed in a `HelixPropertyStore` instance. Also, the property store path in `HelixPropertyStoreConfig` on the frontend will refer to the wrong path since that config is also used by `HelixAccountService`.

Target:
public HelixClusterManager getClusterMap() throws IOException { if (helixClusterManager == null) { helixClusterManager = new HelixClusterManager(clusterMapConfig, instanceName, helixFactory, metricRegistry); } return helixClusterManager; }
Source:
public static long getTtlInSecsFromExpiryMs(long expiresAtMs, long creationTimeMs) { if (expiresAtMs == Utils.Infinite_Time) { return Utils.Infinite_Time; } START return Math.max(0, TimeUnit.MILLISECONDS.toSeconds(expiresAtMs - creationTimeMs)); END } How about `return expiresAtMs == Utils.Infinite_Time ? Utils.Infinite_Time : Math.max(0, TimeUnit.MILLISECONDS.toSeconds(expiresAtMs - creationTimeMs));` ?

Target:
public static long getTtlInSecsFromExpiryMs(long expiresAtMs, long creationTimeMs) { return expiresAtMs == Utils.Infinite_Time ? Utils.Infinite_Time : Math.max(0, TimeUnit.MILLISECONDS.toSeconds(expiresAtMs - creationTimeMs)); }
Source:
START private Integer getPrecedenceLevel(RouterErrorCode routerErrorCode) { END switch (routerErrorCode) { case BlobDeleted: return 0; case BlobExpired: return 1; case AmbryUnavailable: return 2; case UnexpectedInternalError: return 3; case OperationTimedOut: return 4; case BlobDoesNotExist: return 5; default: return Integer.MIN_VALUE; } } return `int` instead of `Integer`

Target:
private int getPrecedenceLevel(RouterErrorCode routerErrorCode) { switch (routerErrorCode) { case BlobDeleted: return 0; case BlobExpired: return 1; case AmbryUnavailable: return 2; case UnexpectedInternalError: return 3; case OperationTimedOut: return 4; case BlobDoesNotExist: return 5; default: return Integer.MIN_VALUE; } }
Source:
MockReplicationManager(ReplicationConfig replicationConfig, ClusterMapConfig clusterMapConfig, StoreConfig storeConfig, StorageManager storageManager, ClusterMap clusterMap, DataNodeId dataNodeId) throws ReplicationException { START super(replicationConfig, clusterMapConfig, storeConfig, storageManager, new StoreKeyFactory() { END @Override public StoreKey getStoreKey(DataInputStream stream) throws IOException { return null; } @Override public StoreKey getStoreKey(String base64String) throws IOException { return null; } }, clusterMap, null, dataNodeId, null, clusterMap.getMetricRegistry(), null); this.dataNodeId = dataNodeId; reset(); } this change needed? `null` won't work anymore?

Target:
MockReplicationManager(ReplicationConfig replicationConfig, ClusterMapConfig clusterMapConfig, StoreConfig storeConfig, StorageManager storageManager, ClusterMap clusterMap, DataNodeId dataNodeId) throws ReplicationException { super(replicationConfig, clusterMapConfig, storeConfig, storageManager, new StoreKeyFactory() { @Override public StoreKey getStoreKey(DataInputStream stream) { return null; } @Override public StoreKey getStoreKey(String input) { return null; } }, clusterMap, null, dataNodeId, null, clusterMap.getMetricRegistry(), null); this.dataNodeId = dataNodeId; reset(); }
Source:
public String getDatacenterName(byte id) { int idx = (int) id; if (idx < 0 || idx >= dataCentersInClusterMap.size()) { return null; } return dataCentersInClusterMap.get(id); START } END prefer to use `dataCentersInClusterMap.get(idx)`.

Target:
public String getDatacenterName(byte id) { int idx = (int) id; if (idx < 0 || idx >= dataCentersInClusterMap.size()) { return null; } return dataCentersInClusterMap.get(idx); }
Source:
public synchronized void onBlobReplicaCreated(String sourceHost, int port, String blobId, BlobReplicaSourceType sourceType) { int numberOfReplicas; try { BlobId blobIdObj = new BlobId(blobId, clusterMap); PartitionId partitionId = blobIdObj.getPartition(); numberOfReplicas = partitionId.getReplicaIds().size(); } catch (Exception e) { START throw new IllegalArgumentException("Invalid blob ID: " + blobId); END } objectTracker.computeIfAbsent(blobId, k -> new Tracker(numberOfReplicas)).trackCreation(sourceHost, port); } pass in the cause exception too.

Target:
public synchronized void onBlobReplicaCreated(String sourceHost, int port, String blobId, BlobReplicaSourceType sourceType) { int numberOfReplicas; try { BlobId blobIdObj = new BlobId(blobId, clusterMap); PartitionId partitionId = blobIdObj.getPartition(); numberOfReplicas = partitionId.getReplicaIds().size(); } catch (Exception e) { throw new IllegalArgumentException("Invalid blob ID: " + blobId, e); } objectTracker.computeIfAbsent(blobId, k -> new Tracker(numberOfReplicas)).trackCreation(sourceHost, port); }
Source:
public String toString() { START return "[id: " + this.id + " entryType: " + this.entryType + " size: " + this.size + "]"; END } minor: don't need `this.` qualifiers

Target:
public String toString() { return "[id: " + id + " entryType: " + entryType + " size: " + size + "]"; }
Source:
private boolean setSealedReplicas(List<String> sealedReplicas) { InstanceConfig instanceConfig = helixAdmin.getInstanceConfig(clusterName, instanceName); if (instanceConfig == null) { throw new IllegalStateException( "No instance config found for cluster: \"" + clusterName + "\", instance: \"" + instanceName + "\""); } START logger.trace("Setting InstanceConfig with list of sealed replicas: {}", Arrays.toString(sealedReplicas.toArray())); END instanceConfig.getRecord().setListField(ClusterMapUtils.SEALED_STR, sealedReplicas); return helixAdmin.setInstanceConfig(clusterName, instanceName, instanceConfig); } you don't need the `Arrays.toString(sealedReplicas.toArray())`

Target:
private boolean setSealedReplicas(List<String> sealedReplicas) { InstanceConfig instanceConfig = helixAdmin.getInstanceConfig(clusterName, instanceName); if (instanceConfig == null) { throw new IllegalStateException( "No instance config found for cluster: \"" + clusterName + "\", instance: \"" + instanceName + "\""); } logger.trace("Setting InstanceConfig with list of sealed replicas: {}", sealedReplicas.toArray()); instanceConfig.getRecord().setListField(ClusterMapUtils.SEALED_STR, sealedReplicas); return helixAdmin.setInstanceConfig(clusterName, instanceName, instanceConfig); }
Source:
START static SSLFactory getNewInstance(SSLConfig sslConfig) END throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { return Utils.getObj(sslConfig.sslFactory, sslConfig); } `throws Exception`

Target:
static SSLFactory getNewInstance(SSLConfig sslConfig) throws Exception { return Utils.getObj(sslConfig.sslFactory, sslConfig); }
Source:
public void testBuildBlobId() throws Exception { BlobId blobId = new BlobId(version, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted); START System.out.println(blobId.getID()); END assertEquals("Wrong blobId version", version, getVersionFromBlobString(blobId.getID())); assertBlobIdFieldValues(version, blobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted); } Remove?

Target:
public void testBuildBlobId() throws Exception { BlobId blobId = new BlobId(version, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted); assertEquals("Wrong blobId version", version, getVersionFromBlobString(blobId.getID())); assertBlobIdFieldValues(version, blobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted); }
Source:
private boolean setSealedReplicas(List<String> sealedReplicas) { START synchronized (participateLock) { END InstanceConfig instanceConfig = helixAdmin.getInstanceConfig(clusterName, instanceName); if (instanceConfig == null) { throw new IllegalStateException( "No instance config found for cluster: \"" + clusterName + "\", instance: \"" + instanceName + "\""); } logger.trace("Setting InstanceConfig with list of sealed replicas: {}", Arrays.toString(sealedReplicas.toArray())); instanceConfig.getRecord().setListField(ClusterMapUtils.SEALED_STR, sealedReplicas); return helixAdmin.setInstanceConfig(clusterName, instanceName, instanceConfig); } } Actually, we will have to do this differently. Notice that the method that calls this, `setReplicaSealedState()`, is synchronized. That is the method that does the get and set, so synchronization should happen within it. So we need to make sure that that method synchronizes on the same object as the `participate()` call.

Target:
private boolean setSealedReplicas(List<String> sealedReplicas) { InstanceConfig instanceConfig = helixAdmin.getInstanceConfig(clusterName, instanceName); if (instanceConfig == null) { throw new IllegalStateException( "No instance config found for cluster: \"" + clusterName + "\", instance: \"" + instanceName + "\""); } logger.trace("Setting InstanceConfig with list of sealed replicas: {}", Arrays.toString(sealedReplicas.toArray())); instanceConfig.getRecord().setListField(ClusterMapUtils.SEALED_STR, sealedReplicas); return helixAdmin.setInstanceConfig(clusterName, instanceName, instanceConfig); }
Source:
boolean startBlobStore(PartitionId id) { BlobStore store = stores.get(id); START if (store == null || store.isStarted() || !running) { END return false; } try { store.start(); } catch (Exception e) { logger.error("Exception while starting store on disk {}", id, disk, e); return false; } return true; } If the store is already started, I think we should return `true`. Same for stop (return `true` if the store is already stopped and don't try to stop again)

Target:
boolean startBlobStore(PartitionId id) { BlobStore store = stores.get(id); if (store == null || !running) { return false; } else if (store.isStarted()) { return true; } else { try { store.start(); } catch (Exception e) { logger.error("Exception while starting store on disk {}", id, disk, e); return false; } return true; } }
Source:
START boolean deregisterIndexGauges(String storeId) { END String prefix = storeId + SEPERATOR; return registry.remove(MetricRegistry.name(Log.class, prefix + "CurrentCapacityUsed")) && registry.remove( MetricRegistry.name(Log.class, prefix + "PercentageUsedCapacity")) && registry.remove( MetricRegistry.name(Log.class, prefix + "CurrentSegmentCount")); } this function can be `private`

Target:
private boolean deregisterIndexGauges(String storeId) { String prefix = storeId + SEPERATOR; return registry.remove(MetricRegistry.name(Log.class, prefix + "CurrentCapacityUsed")) && registry.remove( MetricRegistry.name(Log.class, prefix + "PercentageUsedCapacity")) && registry.remove( MetricRegistry.name(Log.class, prefix + "CurrentSegmentCount")); }
Source:
START boolean deregisterCompactorGauges(String storeId) { END String prefix = storeId + SEPERATOR; return registry.remove(MetricRegistry.name(BlobStoreCompactor.class, prefix + "CompactionInProgress")); } this function can be `private`

Target:
private boolean deregisterCompactorGauges(String storeId) { String prefix = storeId + SEPERATOR; return registry.remove(MetricRegistry.name(BlobStoreCompactor.class, prefix + "CompactionInProgress")); }
Source:
public void setDiskState(HardwareState state, boolean allowChangesThroughClusterMap) { START this.allowChangesThroughClustermap = allowChangesThroughClusterMap; END this.state = state; } ah I think this might actually need to be synchronized with the other two methods. Add the keyword `synchronized` to `setDiskState()`, `onDiskError()` and `onDiskOk()`

Target:
public synchronized void setDiskState(HardwareState state, boolean allowChangesThroughClusterMap) { this.allowChangesThroughClustermap = allowChangesThroughClusterMap; this.state = state; }
Source:
START void shutdownBlobStore(PartitionId id) { END BlobStore store = (BlobStore) getStore(id); if (store != null) { try { store.shutdown(); } catch (Exception e) { logger.error("Exception while shutting down store on disk {}", id, disk, e); } } } shouldn't we return `boolean` here? so that we can return an error if the stop store failed?

Target:
boolean shutdownBlobStore(PartitionId id) { BlobStore store = (BlobStore) getStore(id); if (store == null) { return false; } try { store.shutdown(); } catch (Exception e) { logger.error("Exception while shutting down store on disk {}", id, disk, e); return false; } return true; }
Source:
boolean disableCompactionForBlobStore(BlobStore store) { if (compactionExecutor == null) { return true; } START return compactionExecutor.disableCompactionForBlobStore(store); END } minor: you can simplify this by `return compactionExecutor == null || compactionExecutor.disableCompactionForBlobStore(store)`

Target:
boolean disableCompactionForBlobStore(BlobStore store) { return compactionExecutor == null || compactionExecutor.disableCompactionForBlobStore(store); }
Source:
START public static boolean validateAuthorization(short storeAccountId, short storeContainerId, short requestAccountId, END short requestContainerId) { if (storeAccountId != Account.UNKNOWN_ACCOUNT_ID && storeContainerId != Container.UNKNOWN_CONTAINER_ID) { return (storeAccountId == requestAccountId) && (storeContainerId == requestContainerId); } return true; } `private boolean`

Target:
private boolean validateAuthorization(short storeAccountId, short storeContainerId, short requestAccountId, short requestContainerId) { if (storeAccountId != Account.UNKNOWN_ACCOUNT_ID && storeContainerId != Container.UNKNOWN_CONTAINER_ID) { return (storeAccountId == requestAccountId) && (storeContainerId == requestContainerId); } return true; }
Source:
void onJobProcessingStart() { START cryptoJobMetrics.operationRate.mark(); END jobProcessingStartTimeMs = System.currentTimeMillis(); cryptoJobMetrics.jobQueuingTimeInMs.update(jobProcessingStartTimeMs - jobSubmissionTimeMs); } should this be fired on job submission?

Target:
void onJobProcessingStart() { jobProcessingStartTimeMs = System.currentTimeMillis(); cryptoJobMetrics.jobQueuingTimeInMs.update(jobProcessingStartTimeMs - jobSubmissionTimeMs); }
Source:
private void maybePersistOldState(Map<String, String> oldState) { if (backupDirPath != null) { try { if (backupPrefixAndPath == null) { START backupPrefixAndPath = reserveBackupFile(); END } writeBackup(backupPrefixAndPath.getSecond(), oldState); } catch (Exception e) { logger.error("Could not write previous state backup file", e); accountServiceMetrics.backupErrorCount.inc(); } } } minor: why not do this in the constructor?

Target:
private void maybePersistOldState(Map<String, String> oldState) { if (backupPrefixAndPath != null) { try { writeBackup(backupPrefixAndPath.getSecond(), oldState); } catch (Exception e) { logger.error("Could not write previous state backup file", e); accountServiceMetrics.backupErrorCount.inc(); } } }
Source:
ServerResponse(ServerErrorCode serverErrorCode, StoreKey storeKey, BlobProperties blobProperties, byte[] userMetadata, byte[] blobData, ByteBuffer encryptionKeyBuffer) { this.serverErrorCode = serverErrorCode; this.storeKey = storeKey; this.blobProperties = blobProperties; this.userMetadata = userMetadata; this.blobData = blobData; START this.encryptionKey = encryptionKeyBuffer != null ? encryptionKeyBuffer.array() : null; END } Do you need to do extract the array? You can exploit `ByteBuffer.equals()` in the comparison.

Target:
ServerResponse(ServerErrorCode serverErrorCode, StoreKey storeKey, BlobProperties blobProperties, byte[] userMetadata, byte[] blobData, ByteBuffer encryptionKeyBuffer) { this.serverErrorCode = serverErrorCode; this.storeKey = storeKey; this.blobProperties = blobProperties; this.userMetadata = userMetadata; this.blobData = blobData; this.encryptionKey = encryptionKeyBuffer; }
Source:
public int hashCode() { START return Utils.hashcode(new Object[]{uuid}); END } Are you sure this change is safe? Shouldn't this be based on versions too?

Target:
public int hashCode() { return uuid.hashCode(); }
Source:
void poll(RequestRegistrationCallback<GetOperation> requestRegistrationCallback) { //First, check if any of the existing requests have timed out. if (!maybeProcessDecryptCallbackAndComplete()) { START cleanupExpiredInFlightRequests(); END checkAndMaybeComplete(true); if (!isOperationComplete()) { fetchRequests(requestRegistrationCallback); } } } this doesn't need to be inside the `if`

Target:
void poll(RequestRegistrationCallback<GetOperation> requestRegistrationCallback) { //First, check if any of the existing requests have timed out. cleanupExpiredInFlightRequests(); checkAndMaybeComplete(); if (!isOperationComplete()) { fetchRequests(requestRegistrationCallback); } }
Source:
public RouterServerSSLTest(boolean testEncryption) { if (testEncryption) { START testFramework.setTestEncryption(); END } } This method could take in a boolean. This would allow the tests to be executed in any order, instead of encryption staying on once it was enabled.

Target:
public RouterServerSSLTest(boolean testEncryption) { testFramework.setTestEncryption(testEncryption); }
Source:
public void closeJob(GeneralSecurityException gse) { START callback.onCompletion(new DecryptJobResult(blobId, null, null), gse); END } same here - result should be `null`

Target:
public void closeJob(GeneralSecurityException gse) { callback.onCompletion(null, gse); }
Source:
public void testSimpleBlobPutSuccess() throws Exception { requestAndResultsList.clear(); requestAndResultsList.add(new RequestAndResult(chunkSize)); submitPutsAndAssertSuccess(true); START for (int i = 0; i < 1; i++) { END // size in [1, chunkSize] requestAndResultsList.clear(); requestAndResultsList.add(new RequestAndResult(random.nextInt(chunkSize) + 1)); submitPutsAndAssertSuccess(true); } } is this a debugging change that hasn't been reverted?

Target:
public void testSimpleBlobPutSuccess() throws Exception { requestAndResultsList.clear(); requestAndResultsList.add(new RequestAndResult(chunkSize)); submitPutsAndAssertSuccess(true); for (int i = 0; i < 10; i++) { // size in [1, chunkSize] requestAndResultsList.clear(); requestAndResultsList.add(new RequestAndResult(random.nextInt(chunkSize) + 1)); submitPutsAndAssertSuccess(true); } }
Source:
START void awaitOpCompletionOrTimeOut(FutureResult futureResult) throws InterruptedException { END int timer = 0; List<RequestInfo> allRequests = new ArrayList<>(); while (timer < AWAIT_TIMEOUT_MS / 2 && !futureResult.completed()) { pollOpManager(allRequests); Thread.sleep(50); timer += 50; allRequests.clear(); } } is this used in some other class? Can't be private?

Target:
private void awaitOpCompletionOrTimeOut(FutureResult futureResult) throws InterruptedException { int timer = 0; List<RequestInfo> allRequests = new ArrayList<>(); while (timer < AWAIT_TIMEOUT_MS / 2 && !futureResult.completed()) { pollOpManager(allRequests); Thread.sleep(50); timer += 50; allRequests.clear(); } }
Source:
public void after() { START if (router != null) { END router.close(); } if (networkClient != null) { networkClient.close(); } Assert.assertEquals("All operations should have completed", 0, router.getOperationsCount()); if (cryptoJobHandler != null) { cryptoJobHandler.close(); } } can you check why this is necessary now? This sort of invalidates the assert below.

Target:
public void after() { if (networkClient != null) { networkClient.close(); } Assert.assertEquals("All operations should have completed", 0, router.getOperationsCount()); if (cryptoJobHandler != null) { cryptoJobHandler.close(); } }
Source:
void clearStates() { START exceptionOnEncryption = new AtomicReference<>(); END exceptionOnDecryption = new AtomicReference<>(); } minor: you could set them to null

Target:
void clearStates() { exceptionOnEncryption.set(null); exceptionOnDecryption.set(null); }
Source:
START public Account(short id, String name, AccountStatus status, Collection<Container> containers) { END this.id = id; this.name = name; this.status = status; checkRequiredFieldsForBuild(); if (containers != null) { for (Container container : containers) { checkParentAccountIdInContainers(container); checkDuplicateContainerNameOrId(container); updateContainerMap(container); } } } why is this `public` now?

Target:
Account(short id, String name, AccountStatus status, Collection<Container> containers) { this.id = id; this.name = name; this.status = status; checkRequiredFieldsForBuild(); if (containers != null) { for (Container container : containers) { checkParentAccountIdInContainers(container); checkDuplicateContainerNameOrId(container); updateContainerMap(container); } } }
Source:
START public static Container fromJson(JSONObject json, short parentAccountId) throws JSONException { END return new Container(json, parentAccountId); } does this need to be `public`?

Target:
static Container fromJson(JSONObject json, short parentAccountId) throws JSONException { return new Container(json, parentAccountId); }
Source:
MessageInfoAndMetadataListSerde(List<MessageInfo> messageInfoList, List<MessageMetadata> messageMetadataList, short version) { START if (messageMetadataList != null && messageInfoList.size() != messageMetadataList.size()) { END throw new IllegalArgumentException( "Mismatch in the number of messages in message Info list: " + messageInfoList.size() + " and message metadata list: " + messageMetadataList.size()); } this.messageInfoList = messageInfoList; this.messageMetadataList = messageMetadataList; this.version = version; } why can't we check for size match only if messageMetadataList is not Empty. This also avoids NCopies in the next constructor

Target:
MessageInfoAndMetadataListSerde(List<MessageInfo> messageInfoList, List<MessageMetadata> messageMetadataList, short version) { if (messageInfoList != null && messageMetadataList != null && messageInfoList.size() != messageMetadataList.size()) { throw new IllegalArgumentException( "Mismatch in the number of messages in message Info list: " + messageInfoList.size() + " and message metadata list: " + messageMetadataList.size()); } this.messageInfoList = messageInfoList; this.messageMetadataList = messageMetadataList; this.version = version; }
Source:
public MessageHeader_Format_V2(ByteBuffer input) { START this.buffer = input; END } minor: `this` not required

Target:
public MessageHeader_Format_V2(ByteBuffer input) { buffer = input; }