auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public void onPartitionBecomeDroppedFromOffline(String partitionName) { // remove old replica from StorageManager and delete store directory (this also includes recover from decommission // failure and remove old replica from replication/stats manager) PartitionStateChangeListener storageManagerListener = partitionStateChangeListeners.get(StateModelListenerType.StorageManagerListener); if (storageManagerListener != null) { try { storageManagerListener.onPartitionBecomeDroppedFromOffline(partitionName); } catch (Exception e) { participantMetrics.offlineCount.addAndGet(-1); participantMetrics.errorStateCount.addAndGet(1); throw e; } } participantMetrics.offlineCount.addAndGet(-1); participantMetrics.partitionDroppedCount.inc(); }

Target:
see above about finally.
Source:
boolean awaitBlobCreations() throws InterruptedException { return creationHelper.await(11, TimeUnit.SECONDS); }

Target:
I am curious how much difference it has with this minor change.
Source:
public void setException(Exception e) { if (e == null) { this.e = null; } else { this.e = new RuntimeException(e); } }

Target:
nit : `this.e = e == null ? e : new RuntimeException(e);`
Source:
public PutMessageFormatInputStream(StoreKey key, ByteBuffer blobEncryptionKey, BlobProperties blobProperties, ByteBuffer userMetadata, InputStream blobStream, long streamSize) throws MessageFormatException { this(key, blobEncryptionKey, blobProperties, userMetadata, blobStream, streamSize, BlobType.DataBlob); }

Target:
you can change this to: ``` this(key, blobEncryptionKey, blobProperties, userMetadata, blobStream, streamSize, BlobType.DataBlob, (short) 0); ``` avoiding an extra method call.
Source:
public CloudBlobMetadata getOldestDeletedBlob(String partitionPath) throws CloudStorageException { List<CloudBlobMetadata> deadBlobs = requestAgent.doWithRetries( () -> cloudDestination.getDeletedBlobs(partitionPath, System.currentTimeMillis(), queryLimit), "GetDeadBlobs", partitionPath); return deadBlobs.isEmpty() ? null : deadBlobs.get(0); }

Target:
please add java docs to these methods
Source:
String getLiveness() { String liveness = UP; if (resourceStatePolicy.isHardDown()) { liveness = NODE_DOWN; } else if (resourceStatePolicy.isDown()) { liveness = SOFT_DOWN; } return liveness; }

Target:
Can simplify to one line, no? Or if you're going for readability, you can eliminate the local variable and just do "return ..." everywhere.
Source:
public CloudServiceClusterChangeHandler(String dcName, ClusterMapConfig clusterMapConfig, ClusterChangeHandlerCallback changeHandlerCallback) throws Exception { this.dcName = dcName; this.clusterMapConfig = clusterMapConfig; this.changeHandlerCallback = changeHandlerCallback; cloudServiceDataNode = new CloudServiceDataNode(dcName, clusterMapConfig); cloudServiceInstanceName = ClusterMapUtils.getInstanceName(cloudServiceDataNode.getDatacenterName(), cloudServiceDataNode.getPort()); }

Target:
Since in `CloudServiceDataNode` hostname will be datacenter name, I think we can use `cloudServiceDataNode.getHostname()` to generate instance name.
Source:
public List<PartitionId> getAllPartitionIds(String partitionClass) { List<PartitionId> staticPartitionIds = staticClusterManager.getAllPartitionIds(partitionClass); return staticPartitionIds; }

Target:
combine two lines
Source:
public void registerClusterMapListener(ClusterMapChangeListener clusterMapChangeListener) { if (helixClusterManager != null) { helixClusterManager.registerClusterMapListener(clusterMapChangeListener); } }

Target:
Can it be null? You assume it's not in other places.
Source:
public BoundedNettyByteBufReceive replace(ByteBuf content) { BoundedNettyByteBufReceive receive = new BoundedNettyByteBufReceive(content, sizeToRead); return receive; }

Target:
variable `receive` is redundant.
Source:
public ResponseInfo replace(ByteBuf content) { ResponseInfo info = new ResponseInfo(requestInfo, error, content, dataNode); return info; }

Target:
`info` is redundant
Source:
public void writeTo(AsyncWritableChannel channel, Callback<Long> callback) throws IOException { prepareBuffer(); channel.write(bufferToSend, callback); }

Target:
I would actually recommend removing the `throws IOException` from the interface. Any checked exception can just be dealt with by calling `callback.onComplete(null, ex)`. I think that this helps unify the failure handling paths that the caller needs to deal with.
Source:
public NettySslHttp2Factory(SSLConfig sslConfig) throws GeneralSecurityException, IOException { nettyServerSslContext = getServerSslContext(sslConfig); nettyClientSslContext = getClientSslContext(sslConfig); // Netty's OpenSsl based implementation does not use the JDK SSLContext so we have to fall back to the JDK based // factory to support this method. jdkSslContext = sslConfig.sslHttp2SelfSign ? null : new JdkSslFactory(sslConfig).getSSLContext(); this.endpointIdentification = sslConfig.sslEndpointIdentificationAlgorithm.isEmpty() ? null : sslConfig.sslEndpointIdentificationAlgorithm; }

Target:
I think there may not be a valid case for supporting the `getSSLContext()` method for HTTP2, since it was mostly present so that `BlockingChannelConnectionPool` could use blocking `SSLSocket`s instead of `SSLEngine`s, but there won't be that need with HTTP2. Instead, `getSSLContext()` could just throw `UnsupportedOperationException`.
Source:
public boolean isBootstrapInProgress() { throw new UnsupportedOperationException("Method not supported"); }

Target:
Does this need to be implemented for live-serving mode?
Source:
boolean isBootstrapInProgress() { File bootstrapFile = new File(dataDir, BOOTSTRAP_FILE_NAME); return bootstrapFile.exists(); }

Target:
can combine two lines
Source:
public Future<Long> write(ByteBuffer src, Callback<Long> callback) { if (src == null) { throw new IllegalArgumentException("Source buffer cannot be null"); } ChunkData chunkData = new ChunkData(Unpooled.wrappedBuffer(src), callback); chunks.add(chunkData); if (channelEventListener != null) { channelEventListener.onEvent(EventType.Write); } if (!isOpen()) { resolveAllRemainingChunks(new ClosedChannelException()); } return chunkData.future; }

Target:
could you replace this impl with a call to `write(Unpooled.wrappedBuffer(src), callback)`?
Source:
public void uploadFile(String containerName, String fileName, InputStream inputStream) throws BlobStorageException, IOException { try { BlobContainerClient containerClient = getContainer(containerName, true); BlockBlobClient blobClient = containerClient.getBlobClient(fileName).getBlockBlobClient(); blobClient.uploadWithResponse(inputStream, inputStream.available(), null, null, null, null, null, null, Context.NONE); } catch (UncheckedIOException e) { // error processing input stream throw e.getCause(); } }

Target:
Could the method `getBlockBlobClient(String containerName, String blobName, boolean autoCreateContainer)` be used here?
Source:
String getAzureContainerName(String partitionPath) { // Include Ambry cluster name in case the same storage account is used to backup multiple clusters. // Azure requires container names to be all lower case String rawContainerName = clusterName + SEPARATOR + partitionPath; return rawContainerName.toLowerCase(); }

Target:
minor: can be a private method.
Source:
String getAzureBlobName(BlobId blobId) { // Use the last four chars as prefix to assist in Azure sharding, since beginning of blobId has little variation. String blobIdStr = blobId.getID(); return blobIdStr.substring(blobIdStr.length() - 4) + SEPARATOR + blobIdStr; }

Target:
minor: can be private
Source:
public void testEarlyReadableStreamChannelClose() throws Exception { for (int numChunksInBlob = 0; numChunksInBlob <= 4; numChunksInBlob++) { for (int numChunksToRead = 0; numChunksToRead < numChunksInBlob; numChunksToRead++) { System.out.println("" + numChunksInBlob + " chunks but only read " + numChunksToRead); testEarlyReadableStreamChannelClose(numChunksInBlob, numChunksToRead); } } }

Target:
Could you remove this print line? If this is for debugging if the test fails, you can consider adding `numChunksInBlob` and `numChunksToRead` to the assert messages.
Source:
public void stop() { if (started.compareAndSet(true, false)) { if (scheduler != null) { Utils.shutDownExecutorService(scheduler, config.nettyMetricsStopWaitTimeoutSeconds, TimeUnit.SECONDS); logger.trace("De-scheduled for collecting netty metrics"); } } }

Target:
INFO level
Source:
public void connect() throws IOException { synchronized (lock) { if (!connected) { socket = createSocket(); writeChannel = Channels.newChannel(socket.getOutputStream()); readChannel = socket.getInputStream(); connected = true; logger.debug( "Connection established to {}({}):{}", host, socket.getRemoteSocketAddress(), socket.getPort() ); } } }

Target:
Could you format the code in intellij?
Source:
public static List<CloudBlobMetadata> capMetadataListBySize(List<CloudBlobMetadata> originalList, long size) { long totalSize = 0; List<CloudBlobMetadata> cappedList = new ArrayList<>(); for (CloudBlobMetadata metadata : originalList) { // Cap results at max size if (totalSize + metadata.getSize() > size) { if (cappedList.size() == 0) { // We must add at least one regardless of size cappedList.add(metadata); } break; } cappedList.add(metadata); totalSize += metadata.getSize(); } return cappedList; }

Target:
I'd prefer to keep CloudBlobMetadata a pojo class and move this method to a utility class that both prod and test code can call.
Source:
public ByteBuffer getAndRelease() { return buffer; }

Target:
if this is supposed to "release" the buffer, should the `buffer` field be set to null?
Source:
public ByteBuf getAndRelease() { if (released.compareAndSet(false, true)) { try { return buffer.retainedDuplicate(); } finally { buffer.release(); } } else { throw new IllegalStateException("Payload already released"); } }

Target:
Should this method return null if the buffer was not yet created or the buffer was released because of an error? Right now, it may throw an exception upon calling `retainedDuplicate()`
Source:
public void clearReceive() { if (networkReceive != null) { ReferenceCountUtil.release(networkReceive.getReceivedBytes().getAndRelease()); } networkReceive = null; }

Target:
would this cause exceptions upon calling `getAndRelease` in `NetworkClient`? I think clearReceive is called from within the selector and then the receive is unboxed again in`NetworkClient:sendAndPoll`
Source:
public boolean isReadComplete() { return !(buffer == null || sizeRead < sizeToRead); }

Target:
Maybe clearer to use AND and no NOT.
Source:
private ConcurrentHashMap<String, PartitionId> mapPartitionNameToPartition(ClusterMap clusterMap, DataNodeId localNode) { List<? extends ReplicaId> localReplicas = clusterMap.getReplicaIds(localNode); return localReplicas.stream() .collect(Collectors.toMap(replicaId -> replicaId.getPartitionId().toPathString(), ReplicaId::getPartitionId, (e1, e2) -> e2, ConcurrentHashMap::new)); }

Target:
Can fold this line into next one.
Source:
public void onPartitionStateChangeToStandbyFromLeader(String partitionName) { logger.debug("Partition state change notification from Leader to Standby received for partition {}", partitionName); synchronized (notificationLock) { try { removeCloudReplica(partitionName); } catch (ReplicationException rex) { // Helix will run into error state if exception throws in Helix context. logger.error("Exception on removing Partition from {}: ", partitionName, dataNodeId, rex); } } }

Target:
Do you know in which case, it may throw exception?
Source:
public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) { try { logger.trace("Live instance change triggered from with: {}", dcName, liveInstances); updateInstanceLiveness(liveInstances); if (!liveStateInitialized.get()) { logger.info("Received initial notification for live instance change from {}", dcName); liveStateInitialized.set(true); } helixClusterManagerMetrics.liveInstanceChangeTriggerCount.inc(); } catch (Throwable t) { errorCount.incrementAndGet(); throw t; } }

Target:
trace seems too low, how often will this happen?
Source:
public void onRoutingTableChange(RoutingTableSnapshot routingTableSnapshot, Object context) { logger.info("Routing table change triggered from {}", dcName); synchronized (notificationLock) { dcToRoutingTableSnapshotRef.get(dcName).getAndSet(routingTableSnapshot); helixClusterManagerMetrics.routingTableChangeTriggerCount.inc(); } }

Target:
Yeah, I'm also not sure why this needs extra sync.
Source:
public DeleteSubRecord getDeleteSubRecord() { if (subRecord.getType().equals(SubRecord.Type.DELETE)) return (DeleteSubRecord) subRecord; return null; }

Target:
nit: put around if statement.
Source:
public MessageInfo(StoreKey key, long size, boolean deleted, boolean ttlUpdated, long expirationTimeInMs, short accountId, short containerId, long operationTimeMs) { this(key, size, deleted, ttlUpdated, expirationTimeInMs, null, accountId, containerId, operationTimeMs, (short) -1); }

Target:
What is the thought to use -1 as default value? I feel it's more natural to use 0 as default value so that existing Put/Delete/TtlUpdate all get updateVersion 0, which is expected. With default 0, no need to introduce `hasUpdateVersion`. 0 makes everything auto backwards compatible.
Source:
public boolean shutdownBlobStore(PartitionId id) { CloudBlobStore blobStore = partitionToStore.getOrDefault(id, null); if (blobStore == null) { return false; } blobStore.shutdown(); return true; }

Target:
just `partitionToStore.get()` should return null if the element is not present.
Source:
public boolean addBlobStore(ReplicaId replica) { CloudBlobStore cloudBlobStore = partitionToStore.computeIfAbsent(replica.getPartitionId(), store -> new CloudBlobStore(properties, replica.getPartitionId(), cloudDestination, clusterMap, vcrMetrics)); partitionToStore.put(replica.getPartitionId(), cloudBlobStore); return startBlobStore(replica.getPartitionId()); }

Target:
I think that computeIfAbsent will already insert the entry into the map after computing so this call might not be needed
Source:
private void ensureUnequal(CloudFindToken token1, CloudFindToken token2) { assertFalse("Tokens should match", token1.equals(token2)); assertFalse("Tokens should match", token1.hashCode() == token2.hashCode()); }

Target:
Can remove hashcode check since conflicts are possible.
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) { logger.debug("The ZNRecord to read does not exist on path={}", znRecordPath); return null; } return fetchAccountMetadataFromZNRecord(znRecord); }

Target:
minor: I think here can be `warn`
Source:
public ResourceResponse<Document> upsertMetadata(CloudBlobMetadata blobMetadata) throws DocumentClientException { RequestOptions options = getRequestOptions(blobMetadata.getPartitionId()); return retryOperationWithThrottling( () -> documentClient.upsertDocument(cosmosCollectionLink, blobMetadata, null, true), azureMetrics.documentCreateTime); }

Target:
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?
Source:
public CosmosDataAccessor(DocumentClient documentClient, AzureCloudConfig azureCloudConfig, AzureMetrics azureMetrics) { this.documentClient = documentClient; this.cosmosCollectionLink = azureCloudConfig.cosmosCollectionLink; this.azureMetrics = azureMetrics; this.maxRetries = azureCloudConfig.cosmosMaxRetries; }

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

Target:
`logger.error` ?
Source:
void onErrorResponse(ReplicaId replicaId, RouterException exception) { 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(); }

Target:
This is method could be private.
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() .equals(this.originatingDcName)) { originatingDcNotFoundCount++; } } }

Target:
`this` can be removed
Source:
void setOperationException(RouterException exception) { RouterUtils.replaceOperationException(operationException, (RouterException) exception, this::getPrecedenceLevel); }

Target:
You don't need to cast here since you're accepting a `RouterException`
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: group = frontendMetrics.getSegmentMetricsGroup; break; } } return group; }

Target:
discussed offline. probably better to use getBlobMetricsGroup here to keep metrics grouping consistent with range requests.
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; } fail(); }

Target:
minor: move `fail()` to line 144 and remove `return` at line 147
Source:
private CompositeBlobInfo deserializeMetadataContentV3(ByteBuffer metadataContent, StoreKeyFactory storeKeyFactory) throws MessageFormatException, 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); }

Target:
no `MessageFormatException` thrown from this method
Source:
public void participate(InstanceType role) throws Exception { manager = HelixManagerFactory.getZKHelixManager(vcrClusterName, vcrInstanceName, InstanceType.PARTICIPANT, cloudConfig.vcrClusterZkConnectString); manager.getStateMachineEngine() .registerStateModelFactory(LeaderStandbySMD.name, new HelixVcrStateModelFactory(this)); manager.connect(); helixAdmin = manager.getClusterManagmentTool(); logger.info("Participate HelixVcrCluster as successfully.", role); }

Target:
use role
Source:
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; }

Target:
I would make it <= (allow equal case to meet requirement). Is there a specific reason to be strict about it?
Source:
public VirtualReplicatorCluster getVirtualReplicatorCluster() { if (virtualReplicatorCluster == null) { virtualReplicatorCluster = new StaticVcrCluster(cloudConfig, clusterMapConfig, clusterMap); } return virtualReplicatorCluster; }

Target:
Singleton isn't threadsafe. We're not worried about multiple threads calling this method?
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"); minTtlMillis = cloudConfig.vcrMinTtlDays * TimeUnit.DAYS.toMillis(1); }

Target:
`TimeUnit.DAYS.toMillis(cloudConfig.vcrMinTtlDays)`
Source:
public String toString() { return "CloudReplica[" + dataNodeId.getHostname() + ":" + getDataNodeId().getPort() + ":" + getReplicaPath() + "]"; }

Target:
Still calling getDataNodeId() for the port.
Source:
private BlobId addBlobToSet(MockMessageWriteSet messageWriteSet, long size, long expiresAtMs, short accountId, 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; }

Target:
`throws StoreException` is not needed.
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); finalCallback.onCompletion(null, null); }, uri, LOGGER, finalCallback); }

Target:
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`.
Source:
private static void assertObjectsAreDistinct(GetBlobOptions a, GetBlobOptions b) { assertThat("GetBlobOptions should not be equal.", a, not(b)); assertThat("GetBlobOptions hashcodes should not be equal", a.hashCode(), not(b.hashCode())); assertThat("GetBlobOptions toString should not be equal", a.toString(), not(b.toString())); }

Target:
hashcode can be equal even if the objects aren't - this test is not correct and may lead to spurious failures
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(); routerMetrics.onPutBlobError(routerException, blobProperties != null && blobProperties.isEncrypted(), stitchOperation); completeOperation(futureResult, callback, null, routerException); // Close so that any existing operations are also disposed off. close(); }

Target:
`blobProperties != null` could be removed, because it has been checked in all callers.
Source:
public static List<Object[]> data() { return Arrays.asList( new Object[][]{{SimpleOperationTracker.class.getSimpleName(), false}, {SimpleOperationTracker.class.getSimpleName(), true}, {AdaptiveOperationTracker.class.getSimpleName(), false}, {AdaptiveOperationTracker.class.getSimpleName(), true}}); }

Target:
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?
Source:
public JSONObject getSnapshot() { return new JSONObject(); }

Target:
just curious why here returns new `JSONObject`, however, in previous test, `null` is directly returned?
Source:
public void testConstructAccountFromJson() throws Exception { System.out.println(refAccountJson); assertAccountAgainstReference(Account.fromJson(refAccountJson), true, true); }

Target:
need to remove this
Source:
public DiskManager getDiskManager(PartitionId id) { return partitionToDiskManager.get(id); }

Target:
why is this public?
Source:
public void after() throws InterruptedException { if (storageManager != null) { storageManager.shutdown(); } }

Target:
If `storageManager` gets a value in constructor, then we don't need to check `null`.
Source:
static Long getXid(InstanceConfig instanceConfig) { String xid = instanceConfig.getRecord().getSimpleField(XID_STR); return xid == null ? Long.MIN_VALUE : Long.valueOf(xid); }

Target:
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`)
Source:
public HelixAccountServiceFactory(VerifiableProperties verifiableProperties, MetricRegistry metricRegistry) { storeConfig = new HelixPropertyStoreConfig(verifiableProperties); accountServiceConfig = new HelixAccountServiceConfig(verifiableProperties); accountServiceMetrics = new AccountServiceMetrics(metricRegistry); this.notifier = accountServiceConfig.zkClientConnectString.equals(HelixAccountServiceConfig.INVALID_ZK_CLIENT_CONNECT_STRING) ? null : new HelixNotifier(accountServiceConfig.zkClientConnectString, storeConfig); }

Target:
minor: don't need `this.`
Source:
void checkExpectedRouterErrorCode(RouterErrorCode routerErrorCode) throws Exception { 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()); } }

Target:
it seems the `Exception` is unnecessary, we can remove it.
Source:
public int appendFromDirectly(byte[] byteArray, int offset, int length) throws IOException { rollOverIfRequired(length); return activeSegment.appendFromDirectly(byteArray, offset, length); }

Target:
this doesn't need to be `public` right?
Source:
void readIntoDirectly(byte[] byteArray, long position, int length) throws IOException { validateReadRange(position, length); if (file.length() < position + length) { 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); } }

Target:
isn't this check already done in `validateReadRange()`?
Source:
private BlobId createBlobId(String hexBlobId) throws IOException { return new BlobId(hexBlobId, clusterMap); }

Target:
minor: don't think a function is required
Source:
public HelixClusterManager getClusterMap() throws IOException { if (helixClusterManager == null) { ZkClient zkClient = 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; }

Target:
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`.
Source:
public static long getTtlInSecsFromExpiryMs(long expiresAtMs, long creationTimeMs) { if (expiresAtMs == Utils.Infinite_Time) { return Utils.Infinite_Time; } return Math.max(0, TimeUnit.MILLISECONDS.toSeconds(expiresAtMs - creationTimeMs)); }

Target:
How about `return expiresAtMs == Utils.Infinite_Time ? Utils.Infinite_Time : Math.max(0, TimeUnit.MILLISECONDS.toSeconds(expiresAtMs - creationTimeMs));` ?
Source:
private Integer 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; } }

Target:
return `int` instead of `Integer`
Source:
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) 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(); }

Target:
this change needed? `null` won't work anymore?
Source:
public String getDatacenterName(byte id) { int idx = (int) id; if (idx < 0 || idx >= dataCentersInClusterMap.size()) { return null; } return dataCentersInClusterMap.get(id); }

Target:
prefer to use `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) { throw new IllegalArgumentException("Invalid blob ID: " + blobId); } objectTracker.computeIfAbsent(blobId, k -> new Tracker(numberOfReplicas)).trackCreation(sourceHost, port); }

Target:
pass in the cause exception too.
Source:
public String toString() { return "[id: " + this.id + " entryType: " + this.entryType + " size: " + this.size + "]"; }

Target:
minor: don't need `this.` qualifiers
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 + "\""); } 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); }

Target:
you don't need the `Arrays.toString(sealedReplicas.toArray())`
Source:
static SSLFactory getNewInstance(SSLConfig sslConfig) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { return Utils.getObj(sslConfig.sslFactory, sslConfig); }

Target:
`throws Exception`
Source:
public void testBuildBlobId() throws Exception { BlobId blobId = new BlobId(version, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted); System.out.println(blobId.getID()); assertEquals("Wrong blobId version", version, getVersionFromBlobString(blobId.getID())); assertBlobIdFieldValues(version, blobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted); }

Target:
Remove?
Source:
private boolean setSealedReplicas(List<String> sealedReplicas) { synchronized (participateLock) { 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); } }

Target:
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.
Source:
boolean startBlobStore(PartitionId id) { BlobStore store = stores.get(id); if (store == null || store.isStarted() || !running) { return false; } try { store.start(); } catch (Exception e) { logger.error("Exception while starting store on disk {}", id, disk, e); return false; } return true; }

Target:
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)
Source:
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")); }

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

Target:
this function can be `private`
Source:
public void setDiskState(HardwareState state, boolean allowChangesThroughClusterMap) { this.allowChangesThroughClustermap = allowChangesThroughClusterMap; this.state = state; }

Target:
ah I think this might actually need to be synchronized with the other two methods. Add the keyword `synchronized` to `setDiskState()`, `onDiskError()` and `onDiskOk()`
Source:
void shutdownBlobStore(PartitionId id) { 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); } } }

Target:
shouldn't we return `boolean` here? so that we can return an error if the stop store failed?
Source:
boolean disableCompactionForBlobStore(BlobStore store) { if (compactionExecutor == null) { return true; } return compactionExecutor.disableCompactionForBlobStore(store); }

Target:
minor: you can simplify this by `return compactionExecutor == null || compactionExecutor.disableCompactionForBlobStore(store)`
Source:
public static 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; }

Target:
`private boolean`
Source:
void onJobProcessingStart() { cryptoJobMetrics.operationRate.mark(); jobProcessingStartTimeMs = System.currentTimeMillis(); cryptoJobMetrics.jobQueuingTimeInMs.update(jobProcessingStartTimeMs - jobSubmissionTimeMs); }

Target:
should this be fired on job submission?
Source:
private void maybePersistOldState(Map<String, String> oldState) { if (backupDirPath != null) { try { if (backupPrefixAndPath == null) { backupPrefixAndPath = reserveBackupFile(); } writeBackup(backupPrefixAndPath.getSecond(), oldState); } catch (Exception e) { logger.error("Could not write previous state backup file", e); accountServiceMetrics.backupErrorCount.inc(); } } }

Target:
minor: why not do this in the constructor?
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; this.encryptionKey = encryptionKeyBuffer != null ? encryptionKeyBuffer.array() : null; }

Target:
Do you need to do extract the array? You can exploit `ByteBuffer.equals()` in the comparison.
Source:
public int hashCode() { return Utils.hashcode(new Object[]{uuid}); }

Target:
Are you sure this change is safe? Shouldn't this be based on versions too?
Source:
void poll(RequestRegistrationCallback<GetOperation> requestRegistrationCallback) { //First, check if any of the existing requests have timed out. if (!maybeProcessDecryptCallbackAndComplete()) { cleanupExpiredInFlightRequests(); checkAndMaybeComplete(true); if (!isOperationComplete()) { fetchRequests(requestRegistrationCallback); } } }

Target:
this doesn't need to be inside the `if`
Source:
public RouterServerSSLTest(boolean testEncryption) { if (testEncryption) { testFramework.setTestEncryption(); } }

Target:
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.
Source:
public void closeJob(GeneralSecurityException gse) { callback.onCompletion(new DecryptJobResult(blobId, null, null), gse); }

Target:
same here - result should be `null`
Source:
public void testSimpleBlobPutSuccess() throws Exception { requestAndResultsList.clear(); requestAndResultsList.add(new RequestAndResult(chunkSize)); submitPutsAndAssertSuccess(true); for (int i = 0; i < 1; i++) { // size in [1, chunkSize] requestAndResultsList.clear(); requestAndResultsList.add(new RequestAndResult(random.nextInt(chunkSize) + 1)); submitPutsAndAssertSuccess(true); } }

Target:
is this a debugging change that hasn't been reverted?
Source:
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(); } }

Target:
is this used in some other class? Can't be private?
Source:
public void after() { if (router != null) { router.close(); } if (networkClient != null) { networkClient.close(); } Assert.assertEquals("All operations should have completed", 0, router.getOperationsCount()); if (cryptoJobHandler != null) { cryptoJobHandler.close(); } }

Target:
can you check why this is necessary now? This sort of invalidates the assert below.
Source:
void clearStates() { exceptionOnEncryption = new AtomicReference<>(); exceptionOnDecryption = new AtomicReference<>(); }

Target:
minor: you could set them to null
Source:
public 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); } } }

Target:
why is this `public` now?
Source:
public static Container fromJson(JSONObject json, short parentAccountId) throws JSONException { return new Container(json, parentAccountId); }

Target:
does this need to be `public`?
Source:
MessageInfoAndMetadataListSerde(List<MessageInfo> messageInfoList, List<MessageMetadata> messageMetadataList, short version) { if (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; }

Target:
why can't we check for size match only if messageMetadataList is not Empty. This also avoids NCopies in the next constructor
Source:
public MessageHeader_Format_V2(ByteBuffer input) { this.buffer = input; }

Target:
minor: `this` not required
Source:
private static MessageInfo getMessageInfo(StoreKey id, List<MessageInfo> messageInfos, boolean deleteMsg) { MessageInfo toRet = null; for (MessageInfo messageInfo : messageInfos) { if (messageInfo.getStoreKey().equals(id)) { if ((deleteMsg && messageInfo.isDeleted()) || (!deleteMsg && !messageInfo.isDeleted())) { toRet = messageInfo; break; } } } return toRet; }

Target:
`if(deleteMsg == messageInfo.isDeleted())`?
Source:
public boolean addAccountUpdateConsumer(Consumer<Collection<Account>> accountUpdateConsumer) { checkOpen(); Objects.requireNonNull(accountUpdateConsumer, "accountUpdateConsumer to subscribe cannot be null"); return accountUpdateConsumers.add(accountUpdateConsumer); }

Target:
why not make these no-ops? Adding and removing doesn't seem to make sense here
Source:
private int numberOfEntries(ByteBuffer mmap) { return (mmap.capacity() - indexSizeExcludingEntries) / (persistedEntrySize); }

Target:
minor: parentheses on denominator not required
Source:
private StoreKey getKeyAt(ByteBuffer mmap, int index) throws IOException { mmap.position(firstKeyRelativeOffset + (index * (persistedEntrySize))); return factory.getStoreKey(new DataInputStream(new ByteBufferInputStream(mmap))); }

Target:
minor: parentheses around `persistedEntrySize` not required.
Source:
public Object setArg(String key, Object value) { Object oldVal = allArgs.put(key, value); allArgsReadOnly = Collections.unmodifiableMap(allArgs); return oldVal; }

Target:
why is this step needed? changing the underlying map should have changed this too.
Source:
public Object setArg(String key, Object value) { Object oldVal = allArgs.put(key, value); return oldVal; }

Target:
`return allArgs.put(key,value)`
Source:
public AccountService getAccountService() { logger.info("Starting an InMemoryUnknownAccountService."); long startTime = System.currentTimeMillis(); AccountService res = new InMemoryUnknownAccountService(); logger.info("InMemoryUnknownAccountService started, took ms", System.currentTimeMillis() - startTime); return res; }

Target:
call this variable accountService
Source:
public Collection<Account> getAllAccounts() { return Collections.unmodifiableCollection(accounts); }

Target:
missed this in the first iteration, sorry - but can you make _this_ the static class variable (the unmodifiable collection).
Source:
public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) { logger.trace("Live instance change triggered from with: {}", dcName, liveInstances); updateInstanceLiveness(liveInstances); if (!liveStateInitialized) { logger.info("Received initial notification for live instance change from {}", dcName); liveStateInitialized = true; } helixClusterManagerMetrics.liveInstanceChangeTriggerCount.inc(); }

Target:
minor: since this one is not under a lock, could you use `AtomicBoolean` and `compareAndSet()`?
Source:
public void setup() throws Exception { storeKeyToMockStoreMap.clear(); helixNotifier = new HelixNotifier(getMockHelixStore(storeConfig)); helixNotifier2 = new HelixNotifier(getMockHelixStore(storeConfig)); resetReferenceTopicsAndMessages(); resetListeners(); deleteStoreIfExists(); }

Target:
could you move this to the top?
Source:
public void close() { try { isOpen = false; cache.close(); helixStore.stop(); } catch (Exception e) { logger.error("Exception occurred when closing HelixAccountService.", e); } }

Target:
Does nuage require metrics on the update path?
Source:
public void close() { if (isOpen) { try { isOpen = false; accountInfoMapRef.set(null); helixStore.stop(); } catch (Exception e) { logger.error("Exception occurred when closing HelixAccountService.", e); } } }

Target:
why is this required?
Source:
public static void serializeBlobPropertiesV2Record(ByteBuffer outputBuffer, BlobProperties properties) { int startOffset = outputBuffer.position(); outputBuffer.putShort(BlobProperties_Version_V1); BlobPropertiesSerDe.putBlobPropertiesToBufferV2(outputBuffer, properties); Crc32 crc = new Crc32(); crc.update(outputBuffer.array(), startOffset, getBlobPropertiesV2RecordSize(properties) - Crc_Size); outputBuffer.putLong(crc.getValue()); }

Target:
private for both functions?
Source:
public DiskSpaceRequirements(long segmentSizeInBytes, long segmentsNeeded, long swapSegmentsInUse) { if (segmentSizeInBytes < 0 || segmentsNeeded < 0 || swapSegmentsInUse < 0) { throw new IllegalArgumentException( "Arguments cannot be negative. segmentSizeInBytes: " + segmentSizeInBytes + ", segmentsNeeded: " + segmentsNeeded + ", swapSegmentsInUse: " + swapSegmentsInUse); } this.segmentSizeInBytes = segmentSizeInBytes; this.segmentsNeeded = segmentsNeeded; this.swapSegmentsInUse = swapSegmentsInUse; }

Target:
`segmentSizeInBytes <= 0`?
Source:
Set<Long> getFileSizeSet() { rwLock.readLock().lock(); try { return internalMap.keySet(); } finally { rwLock.readLock().unlock(); } }

Target:
would we need the lock if we used a `ConcurrentHashMap` and `LinkedinBlockingQueue`?
Source:
private File allocate(String filename, long size) throws IOException { File segmentFile = new File(dataDir, filename); if (!segmentFile.exists()) { if (diskSpaceAllocator != null) { diskSpaceAllocator.allocate(segmentFile, size); } else { Utils.preAllocateFileIfNeeded(segmentFile, size); } } return segmentFile; }

Target:
as discussed, let's do this inside the allocator
Source:
private static Long getFileSizeForDirName(String fileSizeDirName) { Long sizeInBytes = null; if (fileSizeDirName.startsWith(FILE_SIZE_DIR_PREFIX)) { String sizeString = fileSizeDirName.substring(FILE_SIZE_DIR_PREFIX.length()); try { sizeInBytes = Long.parseLong(sizeString); } catch (NumberFormatException ignored) { } } return sizeInBytes; }

Target:
why do we ignore this?
Source:
public StoreKey getStoreKey(DataInputStream value) throws IOException { return BlobId.fromDataInputStream(value, clusterMap); }

Target:
same here - why is this changed from a constructor to this?
Source:
public short sizeInBytes() { short sizeForBlobIdV1 = (short) (VERSION_FIELD_LENGTH_IN_BYTES + partitionId.getBytes().length + UUID_SIZE_FIELD_LENGTH_IN_BYTES + uuid.length()); switch (version) { case BLOB_ID_V1: return sizeForBlobIdV1; case BLOB_ID_V2: return (short) (FLAG_FIELD_LENGTH_IN_BYTES + DATACENTER_ID_FIELD_LENGTH_IN_BYTES + ACCOUNT_ID_FIELD_LENGTH_IN_BYTES + CONTAINER_ID_FIELD_LENGTH_IN_BYTES + sizeForBlobIdV1); default: throw new IllegalArgumentException("blobId version=" + version + " not supported"); } }

Target:
shouldn't this be `uuid.getBytes().length`?
Source:
public short getDatacenterId() { return datacenterId; }

Target:
why isn't the return value a `byte`?
Source:
public boolean remove(String path, int options) { if (path.equals("/")) { notifyListeners("/", HelixPropertyStoreUtils.StoreOperationType.DELETE); pathToRecords.clear(); return true; } else { throw new IllegalStateException("Not implemented"); } }

Target:
do we have test cases that remove a particular path that is not "/"?
Source:
public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Account)) { return false; } Account account = (Account) o; if (id != account.id) { return false; } if (!name.equals(account.name)) { return false; } return status.equals(account.status); }

Target:
was this auto generated ?