auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public AccountBuilder(Account origin) { if (origin == null) { throw new IllegalArgumentException("origin cannot be null."); } this.id = origin.getId(); this.name = origin.getName(); this.status = origin.getStatus(); for (Container container : origin.getAllContainers()) { idToContainerMetadataMap.put(container.getId(), container); } this.version = ACCOUNT_METADATA_VERSION_1; }

Target:
something to think about. Incase we introduce a new version for Account sometime in future, how will this code look like. Some fields in V2 may not be available in V1. Do we have dummy values for it ?
Source:
public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Partition partition = (Partition) o; if (id == null ^ partition.id == null){ // one of them is null (XOR, so not both) return false; } return id.equals(partition.id); }

Target:
I think this check is unnecessary and can be omitted. `id` is supposed to be non-null, the construction of `Partition` will fail otherwise.
Source:
CompactAllPolicy(StoreConfig storeConfig, Time time) { this.storeConfig = storeConfig; this.time = time; this.messageRetentionTimeInMs = (long) storeConfig.storeDeletedMessageRetentionDays * Time.SecsPerDay * Time.MsPerSec; }

Target:
This is being fixed in a different way (using the preferred `TimeUnit` utilities) as part of [this](https://github.com/linkedin/ambry/pull/617/files#diff-fad74bb47f2e54088777e34754ff84d6L52) patch.
Source:
void setBelongsToRecentIndexSegment(boolean belongsToRecentIndexSegment) { this.belongsToRecentIndexSegment.compareAndSet(false, belongsToRecentIndexSegment); }

Target:
could be just ``` this. belongsToRecentIndexSegment = this. belongsToRecentIndexSegment || belongsToRecentIndexSegment ``` don't see why `compareAndSet()` (which is used in multithreading scenarios) is needed.
Source:
public void setValue(long value) { this.value = value; }

Target:
are these setters used anywhere?
Source:
void enable() { if (compactionExecutor != null) { logger.info("Compaction thread started for {}", mountPath); compactionThread = Utils.newThread(THREAD_NAME_PREFIX + mountPath, compactionExecutor, true); compactionThread.start(); try { if (!compactionExecutor.startLatch.await(1, TimeUnit.SECONDS)) { throw new IllegalStateException("Compaction thread did not start in 1 second"); } } catch (InterruptedException e) { throw new IllegalStateException(e); } } }

Target:
why do you care about this? This is essentially checking if the JVM started the thread we asked it to start, isn't it? That seems like an arbitrary check to do. If this is being done for tests, perhaps you can use `TestUtils.getThreadByThisName()`?
Source:
public void cleanup() throws InterruptedException, IOException, StoreException { state.destroy(); assertTrue(tempDir.getAbsolutePath() + " could not be deleted", StoreTestUtils.cleanDirectory(tempDir, true)); indexScannerScheduler.shutdown(); if (!indexScannerScheduler.awaitTermination(1000, TimeUnit.MILLISECONDS)) { fail("Could not terminate index scanner task after BlobStoreStats shutdown"); } queueProcessorScheduler.shutdown(); if (!queueProcessorScheduler.awaitTermination(1000, TimeUnit.MILLISECONDS)) { fail("Could not terminate queue processor task after BlobStoreStats shutdown"); } }

Target:
shouldn't you shutdown the schedulers and then cleanup the directory
Source:
public CompactAllPolicyTest() throws InterruptedException { Pair<MockBlobStore, StoreConfig> initState = CompactionPolicyTest.initializeBlobStore(properties, time, -1, -1, -1); this.config = initState.getSecond(); this.messageRetentionTimeInMs = config.storeDeletedMessageRetentionDays * Time.HoursPerDay * Time.SecsPerHour * Time.MsPerSec; this.blobStore = initState.getFirst(); this.compactionPolicy = new CompactAllPolicy(config, time); }

Target:
minor: `this` qualifiers not required.
Source:
CompactionDetails getCompactionDetails(BlobStore blobStore) throws StoreException { return blobStore.getCompactionDetails(compactionPolicy); }

Target:
can we make this a `private` function now? It will be tested along with the `CompactionExecutor` anyway. The actual policy tests are in the respective tests.
Source:
public void shutdown() throws InterruptedException { if (statsAggregator != null) { statsAggregator.cancel(true); } scheduler.shutdown(); if (!scheduler.awaitTermination(30, TimeUnit.SECONDS)) { logger.error("Could not terminate aggregator tasks after StatsManager shutdown"); } }

Target:
wouldn't it be better to safely shutdown the actual stats job rather than cancelling it and terminating it in the middle? If this was being pushed to a central location, it would cut off the data abruptly.
Source:
public void requestHandleWithGoodInputTest() throws Exception { doRequestHandleTest(HttpMethod.POST, "POST", false, false); doRequestHandleTest(HttpMethod.GET, "GET", false, false); doRequestHandleTest(HttpMethod.DELETE, "DELETE", false, false); // SSL enabled doRequestHandleTest(HttpMethod.GET, "GET", false, true); }

Target:
why just for GET and not for all request types?
Source:
public void getLogSegmentsNotInJournalTest() throws InterruptedException, IOException, StoreException { if (isLogSegmented) { testGetLogSegmentsNotInJournal(2, 2 * MAX_IN_MEM_ELEMENTS, false); testGetLogSegmentsNotInJournal(3, 2 * MAX_IN_MEM_ELEMENTS, false); } else { assertEquals("LogSegments mismatch for non segmented log ", null, state.index.getLogSegmentsNotInJournal()); } }

Target:
what is the intent of this function call? `testGetLogSegmentsNotInJournal()` seems to reduce this to the case tested in the previous function call.
Source:
public ClusterParticipant getClusterParticipant() throws IOException { if (clusterMapConfig == null) { throw new IllegalArgumentException( "Cluster participant creation requires the factory to be initialized with a non-null ClusterMapConfig"); } if (clusterParticipant == null) { clusterParticipant = new ClusterParticipant() { @Override public void initialize(String hostname, int port) throws Exception { } @Override public void terminate() { } }; } return clusterParticipant; }

Target:
any reason to not do this in the constructor? Is it `null` in the normal flow?
Source:
public HelixClusterAgentsFactory(ClusterMapConfig clusterMapConfig, MetricRegistry metricRegistry) { this.clusterMapConfig = clusterMapConfig; this.instanceName = ClusterMapUtils.getInstanceName(clusterMapConfig.clusterMapHostName, clusterMapConfig.clusterMapPort); helixFactory = new HelixFactory(); this.metricRegistry = metricRegistry; }

Target:
why are there two public constructors?
Source:
private boolean alreadyExistsInTgt(StoreKey key, IndexValue srcValue) throws StoreException { IndexValue tgtValue = tgtIndex.findKey(key); return tgtValue != null && srcValue.isFlagSet(IndexValue.Flags.Delete_Index) == tgtValue.isFlagSet( IndexValue.Flags.Delete_Index); }

Target:
minor: exists in the targetIndex
Source:
public void basicTest() throws Exception { refreshState(false, true); List<String> segmentsUnderCompaction = getLogSegments(0, 2); reduceValidDataSizeInLogSegments(segmentsUnderCompaction, state.log.getSegmentCapacity() - LogSegment.HEADER_SIZE); long deleteReferenceTimeMs = state.time.milliseconds(); compactAndVerify(segmentsUnderCompaction, deleteReferenceTimeMs, true); }

Target:
minor: I do know that we don't increment the time here. But just to mimic the actual flow, can you make reduceValidDataSizeInLogSegments return the deleteRefTime and use the same in next line.
Source:
void notifyForFirstChunkCreation() { if (firstChunkIdAndProperties != null) { String chunkId = firstChunkIdAndProperties.getFirst().getID(); BlobProperties chunkProperties = firstChunkIdAndProperties.getSecond(); notificationSystem.onBlobCreated(chunkId, chunkProperties, userMetadata, NotificationBlobType.DataChunk); } }

Target:
Maybe omit this check since this method will now only be called if it is known for sure that there is a first chunk that needs notification (which is also why it is no longer prefixed with "maybe").
Source:
SortedMap<String, Long> getUsedCapacityBySegment() { SortedMap<String, Long> usedCapacityBySegments = new TreeMap<>(); LogSegment logSegment = log.getFirstSegment(); while (logSegment != null) { usedCapacityBySegments.put(logSegment.getName(), logSegment.getEndOffset()); logSegment = log.getNextSegment(logSegment); } return usedCapacityBySegments; }

Target:
Why the map needs to be sorted?
Source:
Pair<StoreKey, PersistentIndex.IndexEntryType> getResetKey() { if (version == (short) 1 && resetKey != null) { return new Pair<>(resetKey.getFirst(), PersistentIndex.IndexEntryType.values()[resetKey.getSecond()]); } else { return null; } }

Target:
Should for ease of test be the reason to change the main code?
Source:
short getVersion() { return this.version; }

Target:
minor: this not required.
Source:
private void initializeAdminsAndAddCluster() { for (Map.Entry<String, String> entry : dataCenterToZkAddress.entrySet()) { ZKHelixAdmin admin = new ZKHelixAdmin(entry.getValue()); adminForDc.put(entry.getKey(), admin); // Add a cluster entry in every DC if (!admin.getClusters().contains(clusterName)) { admin.addCluster(clusterName); admin.addStateModelDef(clusterName, "LeaderStandby", LeaderStandbySMD.build()); } } }

Target:
replace the string "LeaderStandby" with LeaderStandbySMD.name
Source:
StoreFindToken() { this(Type.Uninitialized, null, null, null, null); }

Target:
Can/Will this constructor be gotten rid of once Version_0 and Version_1 are phased out?
Source:
boolean getInclusive() { return inclusive == 1 ? true : false; }

Target:
1. In general, avoid `expr ? true : false` pattern and simply substitute that with `expr`. 2. Ideally, this check should be `... == (byte) 1` or `(... != 0)`.
Source:
private void deleteBlob(final String blobId, FutureResult<Void> futureResult, final Callback<Void> callback) { deleteManager.submitDeleteBlobOperation(blobId, futureResult, new Callback<Void>() { @Override public void onCompletion(Void result, Exception exception) { if (exception == null) { backgroundDeleter.initiateChunkDeletesIfAny(blobId); } callback.onCompletion(result, exception); } }); readyForPollCallback.onPollReady(); }

Target:
`callback` can be `null`.
Source:
BackgroundDeleter() throws IOException { super("backgroundDeleter"); super.putManager.close(); }

Target:
minor: do you need the `super` qualifier here?
Source:
static long getGeneration(String name) { if (name.isEmpty()) { throw new IllegalArgumentException("Name provided cannot be empty"); } return Long.parseLong(name.substring(name.indexOf(BlobStore.SEPARATOR) + 1, name.length())); }

Target:
we don't need the 2nd param(name.length()) in subString.
Source:
DiskIOScheduler(Map<String, Throttler> throttlers) { if (throttlers != null) { this.throttlers.putAll(throttlers); } }

Target:
minor: currently, i don't think the concurrent map instance in this class is required (might be in the future if we allow injecting of throttlers). For now, won't `this.throttlers = throttlers` do?
Source:
Store getStore(PartitionId id) { BlobStore store = stores.get(id); return store.isStarted() ? store : null; }

Target:
if store is null, NullPoinerException ?
Source:
public IndexRecord(String msg, boolean isDeleted, boolean isExpired) { this.message = msg; this.isDeleted = isDeleted; this.isExpired = isExpired; }

Target:
Remove `public`
Source:
private void reportBytesRead(int count) { if (bytesAvailable - count >= 0) { bytesAvailable -= count; } }

Target:
`if (bytesAvailable >= count)` looks simpler?
Source:
private boolean waitForThreadState(Thread thread, Thread.State state) { long MAX_WAIT_MS = 2000; long checkStartTimeMs = SystemTime.getInstance().milliseconds(); while (thread.getState() != state) { if (SystemTime.getInstance().milliseconds() - checkStartTimeMs > MAX_WAIT_MS) { return false; } else { Thread.yield(); } } return true; }

Target:
docs
Source:
private void onDequeue() { router.metrics.totalOperationsInExecution.incrementAndGet(); if (operationQueueStartTime != null) { long queueTime = System.currentTimeMillis() - operationQueueStartTime; router.metrics.operationQueuingTimeInMs.update(queueTime); logger.trace("Operation spent ms in execution queue", queueTime); router.metrics.operationDequeuingRate.mark(); } else { router.metrics.queueStartTimeNotRecordedError.inc(); } }

Target:
perhaps this can go in where `onDequeue()` is called, so that the increment and decrement are within the same method?
Source:
void onFillComplete() { buf.flip(); prepareForSending(); chunkReadyAtMs = time.milliseconds(); routerMetrics.chunkFillTimeMs.update(chunkReadyAtMs - chunkFreeAtMs); }

Target:
Are metadata chunk and data chunk treated in the same way, so it is a single metric that reports `chunkFillTime` for both chunk types?
Source:
private PutChunk getChunkToFill() { if (chunkToFill != null && chunkToFill.isBuilding()) { return chunkToFill; } chunkToFill = null; for (PutChunk chunk : putChunks) { if (chunk.isFree()) { chunkCounter++; chunk.prepareForBuilding(chunkCounter, getSizeOfChunkAt(chunkCounter)); chunkToFill = chunk; routerMetrics.chunkFillerWaitingTrunkTimeMs.update(time.milliseconds() - timeFilledPreviousChunkMs); timeStartFillingCurrentChunkMs = time.milliseconds(); break; } } return chunkToFill; }

Target:
I don't think this is correct. You are updating `timeFilledPreviousChunkMs` when the fill is complete and use that as the threshold for how much we wait. But once the fill is complete, we can't reuse the PutChunk until the chunk it holds is sent out, and we seem to end up calculating that in the "wait time"
Source:
void poll(RequestRegistrationCallback<DeleteOperation> requestRegistrationCallback) { try { cleanupExpiredInflightRequests(); checkAndMaybeComplete(); if (!isOperationComplete()) { fetchRequests(requestRegistrationCallback); } } catch (Exception e) { logger.error("Unexpected error in DeleteBlobOperation poll; operation cancelled.", e); routerMetrics.unexpectedErrorCount.inc(); setOperationException(new RouterException("DeleteOperation poll encountered unexpected error", e, RouterErrorCode.UnexpectedInternalError)); } }

Target:
a general rule of thumb to follow i think would be to not log unless the exception is being swallowed or it is the very last destination of the exception. In this case, the exception is being set and will be passed to someone else who will log it eventually. If you log when the exception is not being swallowed, there might be double logging and pollution of the log.
Source:
void poll(RequestRegistrationCallback<DeleteOperation> requestRegistrationCallback) { try { cleanupExpiredInflightRequests(); checkAndMaybeComplete(); if (!isOperationComplete()) { fetchRequests(requestRegistrationCallback); } } catch (Exception e) { routerMetrics.unexpectedErrorCount.inc(); setOperationException(new RouterException("DeleteOperation poll encountered unexpected error", e, RouterErrorCode.UnexpectedInternalError)); } }

Target:
did you consider pros and cons of doing this at the operation level v/s doing this try-catch at a higher level i.e. at the `RequestResponseHandler` thread itself?
Source:
NodeLevelMetrics getDataNodeBasedMetrics(DataNodeId dataNodeId) { return dataNodeToMetrics.containsKey(dataNodeId) ? dataNodeToMetrics.get(dataNodeId) : dataNodeToMetrics.get(null); }

Target:
why would we have a default case? what is the point in updating metrics for an "unknown" datanode?
Source:
public void setDigestAlgorithm(String digestAlgorithm) throws NoSuchAlgorithmException { throw new IllegalStateException("Not implemented"); }

Target:
how did you decide between throwing vs. silently ignoring (which is what you do in `ReadableStreamChannelInputStreamTest`)?
Source:
public synchronized ResponseStatus getStatus() { ResponseStatus status = null; try { if (responseMetadata.has(RESPONSE_STATUS_KEY)) { status = ResponseStatus.valueOf(responseMetadata.getString(RESPONSE_STATUS_KEY)); } } catch (Exception e) { throw new IllegalStateException(e); } return status; }

Target:
For curiosity, will this method be called by multiple threads?
Source:
public void testSSLConnect() throws IOException { String connectionId = selector.connect(new InetSocketAddress("localhost", server.port), BUFFER_SIZE, BUFFER_SIZE, PortType.SSL); Assert.assertFalse("Channel should not be ready by now (until handshake completes)", selector.isChannelReady(connectionId)); while (!selector.connected().contains(connectionId)) { selector.poll(10000L); } Assert.assertTrue("Channel should have been ready by now ", selector.isChannelReady(connectionId)); }

Target:
Is it possible that a handshake completes very fast and becomes ready before the connectionId is added to connected?
Source:
public void initializeSelectorMetrics(final AtomicLong activeConnections) { numActiveConnections = new Gauge<Long>() { @Override public Long getValue() { return activeConnections.get(); } }; }

Target:
does this need to be `public`?
Source:
private void maybeWriteToChannel() { // if there are chunks available to be written out, do now. if (firstChunk.isComplete() && readCalled) { while (operationException.get() == null && chunkIndexToBuffer.containsKey(indexOfNextChunkToWriteOut)) { ByteBuffer chunkBuf = chunkIndexToBuffer.remove(indexOfNextChunkToWriteOut); asyncWritableChannel.write(chunkBuf, chunkAsyncWriteCallback); indexOfNextChunkToWriteOut++; } if (operationException.get() != null || numChunksWrittenOut.get() == numChunksTotal) { completeRead(); } } }

Target:
Might be an orthogonal question, but if this (or any other statement really) were to throw a `RuntimeException`, who would catch and what is the behavior? And is that the right behavior?
Source:
void handleResponse(ResponseInfo responseInfo) { GetRequest getRequest = (GetRequest) responseInfo.getRequest(); GetOperation getOperation = correlationIdToGetOperation.remove(getRequest.getCorrelationId()); if (getOperations.contains(getOperation)) { getOperation.handleResponse(responseInfo); if (getOperation.isOperationComplete()) { getOperations.remove(getOperation); routerMetrics.operationDequeuingRate.mark(); } } }

Target:
is it possible you will double count if `close()` has removed the operation already?
Source:
public void testCompositeBlobNotChunkSizeMultipleGetSuccess() throws Exception { maxChunkSize = random.nextInt(1024 * 1024) + 1; for (int i = 0; i < 10; i++) { blobSize = maxChunkSize * i + random.nextInt(maxChunkSize - 1) + 1; doPut(); getAndAssertSuccess(); } }

Target:
why extra line
Source:
public void testSimpleBlobGetSuccess() throws Exception { maxChunkSize = random.nextInt(1024 * 1024) + 1; for (int i = 0; i < 10; i++) { // blobSize in the range [1, maxChunkSize] blobSize = random.nextInt(maxChunkSize) + 1; doPut(); getAndAssertSuccess(); } }

Target:
once again, personal pref - but i would find the tests _much_ more readable if `doPut()` just took the arguments it needed instead of us setting the arguments via class variables. One of the reasons I say that is because if I want to reuse the function and want to know what its knobs are, I _have_ to read the whole function. If it took arguments, I would simply read the description of the arguments and know what I need. Any new arguments will also need to be added as class variables and that might make it more confusing.
Source:
boolean remove(GetOperation op) { if (getOperations.remove(op)) { routerMetrics.operationDequeuingRate.mark(); return true; } else { return false; } }

Target:
why is this not `private`?
Source:
public void initializeTests() throws Exception { coordinatorProps = new Properties(); notificationSystem = new MockNotificationSystem(9); plaintextCluster = new MockCluster(notificationSystem, SystemTime.getInstance()); plaintextCluster.startServers(); }

Target:
these could even go into the constructor. Test classes get instantiated for each individual test.
Source:
protected GetRequest createGetRequest(BlobId blobId, MessageFormatFlags flag) { List<BlobId> blobIds = Collections.singletonList(blobId); List<PartitionRequestInfo> partitionRequestInfoList = new ArrayList<PartitionRequestInfo>(); PartitionRequestInfo partitionRequestInfo = new PartitionRequestInfo(blobId.getPartition(), blobIds); partitionRequestInfoList.add(partitionRequestInfo); return new GetRequest(NonBlockingRouter.correlationIdGenerator.incrementAndGet(), routerConfig.routerHostname, flag, partitionRequestInfoList, GetOptions.None); }

Target:
A singletonList() can be used here as well.
Source:
private void markOperationStart() { logger.trace("{} started for {}", operationType, blobId); operationStartTime = System.currentTimeMillis(); }

Target:
Why don't we have rate metrics
Source:
protected void markStartTime() { callbackTracker.markOperationStart(); }

Target:
We can make these methods as private I guess.
Source:
void submitDeleteBlobOperation(String blobIdString, FutureResult<Void> futureResult, Callback<Void> callback) { try { BlobId blobId = RouterUtils.getBlobIdFromString(blobIdString, clusterMap); DeleteOperation deleteOperation = new DeleteOperation(routerConfig, blobId, clusterMap, responseHandler, futureResult, callback, new DeleteRequestRegistrationCallbackImpl(), time); deleteOperations.add(deleteOperation); } catch (RouterException e) { NonBlockingRouter.completeOperation(futureResult, callback, null, e); } }

Target:
I hope you plan to work on logging and metrics in a different patch. We need lot of trace logging in general.
Source:
private void prepareForSending() { try { // if this is part of a retry, make sure no previously attempted partitions are retried. if (partitionId != null) { attemptedPartitionIds.add(partitionId); } partitionId = getPartitionForPut(attemptedPartitionIds); chunkBlobId = new BlobId(partitionId); operationTracker = new SimpleOperationTracker(routerConfig.routerDatacenterName, partitionId, false, routerConfig.routerPutSuccessTarget, routerConfig.routerPutRequestParallelism, false); correlationIdToChunkPutRequestInfo.clear(); state = ChunkState.Ready; } catch (RouterException e) { setOperationException(e); } }

Target:
why you dont want to shuffle replicas for put
Source:
public DeleteManager(ClusterMap clusterMap, ResponseHandler responseHandler, NotificationSystem notificationSystem, RouterConfig routerConfig, NonBlockingRouterMetrics routerMetrics, Time time) { this.clusterMap = clusterMap; this.responseHandler = responseHandler; this.notificationSystem = notificationSystem; this.routerConfig = routerConfig; this.routerMetrics = routerMetrics; this.time = time; deleteOperations = Collections.newSetFromMap(new ConcurrentHashMap<DeleteOperation, Boolean>()); correlationIdToDeleteOperation = new HashMap<Integer, DeleteOperation>(); }

Target:
why `public`?
Source:
public void testGoodCase() throws Exception { initialize(); BlobId convertedBlobId = RouterUtils.getBlobIdFromString(blobIdStr, clusterMap); assertTrue("The converted BlobId should be the same as the original.", convertedBlobId.equals(originalBlobId)); }

Target:
use assertEquals
Source:
MockSelector(MockServerLayout serverLayout, AtomicReference<MockSelectorState> state, Time time) throws IOException { super(new NetworkMetrics(new MetricRegistry()), time, null); // we don't need the actual selector, close it. super.close(); this.serverLayout = serverLayout; this.state = state == null ? new AtomicReference<MockSelectorState>(MockSelectorState.Good) : state; this.time = time; }

Target:
Why should the argument be an `AtomicReference`? If you really do need an `AtomicReference`, shouldn't the class take the input and turn it into an atomic reference?
Source:
private PutChunk getChunkToFill() { if (chunkToFill != null && chunkToFill.isBuilding()) { return chunkToFill; } else { for (PutChunk chunk : putChunks) { if (chunk.isFree()) { chunkCounter++; chunk.prepareForBuilding(chunkCounter, getSizeOfChunkAt(chunkCounter)); chunkToFill = chunk; return chunk; } } } // No free or building chunks. return null; }

Target:
Can we have return only once at the end
Source:
private void ensureCallbackOrder(ChannelWriter channelWriter, int expectedCallbackId) { for (int i = expectedCallbackId; i < channelWriter.writes.size(); i++) { WriteCallback writeCallback = channelWriter.writes.get(i).writeCallback; if (expectedCallbackId == i) { assertTrue("Callback for the expected oldest chunk not invoked", writeCallback.callbackInvoked.get()); } else { assertFalse("Callback for a chunk younger than the expected is invoked", writeCallback.callbackInvoked.get()); } } }

Target:
this might be more readable if you simply took the expected case outside of the loop, and then had the loop run from `expectedCallbackId + 1` to the end.
Source:
void checkInConnection(String connectionId) { availableConnections.add(connectionId); availableCount++; }

Target:
Should we check if the `connectionId` actually belongs to this <host, port> pair? There could be possibly error cases that connections to <host1, port1> are checked into <host2, port2>.
Source:
NetworkClient getNetworkClient() throws IOException { Selector selector = new Selector(networkMetrics, time, sslFactory); ConnectionTracker connectionTracker = new ConnectionTracker(maxConnectionsPerPortPlainText, maxConnectionsPerPortSsl, time); return new NetworkClient(selector, connectionTracker, networkConfig, networkRequestMetrics, connectionCheckoutTimeoutMs, time); }

Target:
How do you plan to embed MockSelector for testing purposes?
Source:
public void testClose() throws IOException { initialize(); List<RequestInfo> requestInfoList = new ArrayList<RequestInfo>(); { networkClient.close(); try { networkClient.sendAndPoll(requestInfoList); requestInfoList.clear(); Assert.fail("Polling after close should throw"); } catch (IllegalStateException e) { } } }

Target:
braces not needed?
Source:
public void addContent(HttpContent httpContent) throws RestServiceException { if (!isOpen()) { nettyMetrics.multipartRequestAlreadyClosedError.inc(); throw new RestServiceException("The request has been closed and is not accepting content", RestServiceErrorCode.RequestChannelClosed); } else { ReferenceCountUtil.retain(httpContent); rawRequestContents.add(httpContent); } }

Target:
Do you intend to make the other change we discussed in a separate patch?
Source:
public void disconnect(ChannelHandlerContext ctx, ChannelPromise future) throws Exception { if (requestInProgress) { requestInProgress = false; logDurations(); logMessage.append(" : Channel disconnected while request in progress."); publicAccessLogger.logError(logMessage.toString()); } super.disconnect(ctx, future); }

Target:
write a function for this. Looks like this code is not repeated in 3 places.
Source:
RouterNetworkComponentsFactory(NetworkMetrics networkMetrics, NetworkConfig networkConfig, SSLFactory sslFactory, int maxConnectionsPerPortPlainText, int maxConnectionsPerPortSsl, Time time) { if (networkMetrics == null || networkConfig == null) { throw new IllegalArgumentException("Invalid inputs passed in, NetworkMetrics: " + networkMetrics + " " + "NetworkConfig: " + networkConfig); } this.networkMetrics = networkMetrics; this.networkConfig = networkConfig; this.sslFactory = sslFactory; this.maxConnectionsPerPortPlainText = maxConnectionsPerPortPlainText; this.maxConnectionsPerPortSsl = maxConnectionsPerPortSsl; this.time = time; }

Target:
Perhaps we can skip this check? Since this a package private class?
Source:
RouterNetworkComponents getRouterNetworkComponents() throws Exception { Selector selector = new Selector(networkMetrics, time, sslFactory); ConnectionManager connectionManager = new NonBlockingConnectionManager(selector, networkConfig, maxConnectionsPerPortPlainText, maxConnectionsPerPortSsl, time); return new RouterNetworkComponents(selector, connectionManager); }

Target:
Where is the `Exception` here? I know the `SSLFactory` constructor throws `Exception` but I don't see anything other than `IOException` from this function.
Source:
private void completeRequest(boolean closeNetworkChannel) { if (closeNetworkChannel && ctx.channel().isOpen()) { writeFuture.addListener(ChannelFutureListener.CLOSE); logger.trace("Requested closing of channel {}", ctx.channel()); } if (request != null && request.isOpen()) { closeRequest(); } responseComplete = true; }

Target:
why duplicating the checks. Already closeRequest() does this check
Source:
public static void serializeMetadataContentRecord(ByteBuffer outputBuffer, List<StoreKey> keys) { int keySize = keys.get(0).sizeInBytes(); for (StoreKey storeKey : keys) { if (storeKey.sizeInBytes() != keySize) { throw new IllegalArgumentException("Keys are not of same size"); } } outputBuffer.putShort(Metadata_Content_Version_V1); outputBuffer.putInt(keys.size()); for (StoreKey storeKey : keys) { outputBuffer.put(storeKey.toBytes()); } }

Target:
Ah I see. You intend to put the number of keys itself. But why? Isn't that information redundant?
Source:
private List<StoreKey> deserializeMetadataContent(ByteBuffer metadataContent, StoreKeyFactory storeKeyFactory) throws MessageFormatException, IOException { ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(metadataContent); DataInputStream inputStream = new DataInputStream(byteBufferInputStream); short metdataContentVersion = inputStream.readShort(); Assert.assertEquals("Metadata Content Version mismatch ", metdataContentVersion, MessageFormatRecord.Metadata_Content_Version_V1); return MessageFormatRecord.Metadata_Content_Format_V1.deserializeMetadataContentRecord(inputStream, storeKeyFactory); }

Target:
flip
Source:
private static AsyncRequestResponseHandler buildInstance(RestServerMetrics restServerMetrics) { lock.lock(); try { if (instance == null) { AsyncRequestResponseHandlerFactory.restServerMetrics = restServerMetrics; instance = new AsyncRequestResponseHandler(restServerMetrics); } // check if same instance of RestServerMetrics - otherwise it is a problem. if (AsyncRequestResponseHandlerFactory.restServerMetrics != restServerMetrics) { throw new IllegalStateException("RestServerMetrics instance provided during construction of " + "AsyncRequestResponseHandler differs from the one currently received"); } } finally { lock.unlock(); } return instance; }

Target:
Since it's a "build" instead of "get", you can return null instead
Source:
public void handleResponse(RestRequest restRequest, RestResponseChannel restResponseChannel, ReadableStreamChannel response, Exception exception) throws RestServiceException { if (isRunning() && asyncResponseHandler != null && asyncResponseHandler.isRunning()) { asyncResponseHandler.submitResponse(restRequest, restResponseChannel, response, exception); } else { restServerMetrics.requestResponseHandlerUnavailableError.inc(); throw new RestServiceException( "Requests cannot be handled because the AsyncRequestResponseHandler is not available", RestServiceErrorCode.ServiceUnavailable); } }

Target:
This should just be `if (isRunning())`
Source:
protected int getWorkersAlive() { int count = 0; for (int i = 0; i < requestWorkerThreads.size(); i++) { if (asyncRequestWorkers.get(i).isRunning() && requestWorkerThreads.get(i).isAlive()) { count++; } } return count; }

Target:
We should ensure that `isRunning` is in sync with `isAlive()` and never have to check for both. Ideally, the `isAlive()` check should be dropped.
Source:
private OperationController getOperationController() { ArrayList<OperationController> ocList = ocListRef.get(); if (ocList.size() == 0) { return null; } else { return ocList.get(ThreadLocalRandom.current().nextInt(ocList.size())); } }

Target:
how sure are we that ThreadLocalRandom will not generate the same numbers for a given seed? For example, if each local random was generating the same series, you will not have a balanced scheduling.
Source:
void completeOperation(FutureResult futureResult, Callback callback, Object operationResult, Exception exception) { try { futureResult.done(operationResult, exception); if (callback != null) { callback.onCompletion(operationResult, exception); } } catch (Exception e) { //@todo add metric. logger.error("Exception caught during future and callback completion", e); } finally { //@todo add metric. } }

Target:
private
Source:
public String connect(InetSocketAddress address, int sendBufferSize, int receiveBufferSize, PortType portType) throws IOException { String hostPortString = address.getHostString() + address.getPort(); Integer count = connectionIdToCount.get(hostPortString); if (count != null) { count++; } else { count = 1; } connectionIdToCount.put(hostPortString, count); String connectionId = hostPortString + index++; connections.add(connectionId); return connectionId; }

Target:
why can't u use count instead of index?
Source:
public DataNodeRemoteReplicaInfos(String datacenter, RemoteReplicaInfo remoteReplicaInfo) { this.datacenter = datacenter; this.dataNodeToReplicaLists = new HashMap<DataNodeId, List<RemoteReplicaInfo>>(); this.dataNodeToReplicaLists.put(remoteReplicaInfo.getReplicaId().getDataNodeId(), Arrays.asList(remoteReplicaInfo)); }

Target:
This constructor is a little weird right? You have a `addRemoteReplica()` - why do you need to send one `remoteReplicaInfo` here?
Source:
public int read() { int data = -1; if (loadData()) { data = currentChunk.get() & 0xFF; bytesAvailable--; } return data; }

Target:
ByteBuffer get() will return 1 byte, why "&0xFF" then?
Source:
private void maybeCloseNetworkChannel(boolean forceClose) { lastWriteFuture.addListener(ChannelFutureListener.CLOSE); logger.trace("Requested closing of channel {}", ctx.channel()); }

Target:
you haven't used the forceClose within the method
Source:
public void run() { while (isRunning()) { try { processResponses(); processRequests(); } catch (Exception e) { logger.error("Swallowing unexpected exception during processing of responses and requests", e); restServerMetrics.dequeuedRequestHandlerUnexpectedExceptionError.inc(); } } discardRequestsResponses(); logger.trace("AsyncHandlerWorker stopped"); shutdownLatch.countDown(); }

Target:
how do you ensure that the caller dont start the thread w/o setting the blob storage service?
Source:
private void closeResponseChannel() { if (isOpen()) { try { channelWriteLock.lock(); responseChannelOpen.set(false); logger.trace("NettyResponseChannel for network channel closed", ctx.channel()); } finally { channelWriteLock.unlock(); } } }

Target:
what is this `try-finally` for?
Source:
public long writeTo(WritableByteChannel channel) throws IOException { long written = 0; if (!isSendComplete()) { written = channel.write(buffer); } return written; }

Target:
``` java return isSendComplete() ? 0 : channel.write(buffer); ``` if you want to make it more elegant.
Source:
public GetBlobOperation(String datacenterName, ConnectionPool connectionPool, ExecutorService requesterPool, OperationContext oc, BlobId blobId, long operationTimeoutMs, ClusterMap clusterMap) throws CoordinatorException { super(datacenterName, connectionPool, requesterPool, oc, blobId, operationTimeoutMs, clusterMap, MessageFormatFlags.Blob); logger.trace( "GetBlob: Datacenter name " + datacenterName + " Connection pool " + connectionPool + ", requester pool " + ( requesterPool != null) + ", Operation context " + oc + ", blobId " + blobId + ", timeout " + operationTimeoutMs + ", clustermap " + (clusterMap != null)); this.blobOutput = null; }

Target:
If you want to check if `connectionPool` is null or not, you should probably do what you did for `requesterPool`
Source:
void prepare() throws IOException { }

Target:
empty function? also no qualifier (public,private)?
Source:
public int available() { return readableStreamChannel.getSize() - totalBytesRead.get() < Integer.MAX_VALUE ? (int) readableStreamChannel.getSize() - totalBytesRead.get() : Integer.MAX_VALUE; }

Target:
And you have to be careful when casting long to int - basically you have to make sure you never cast a long > Integer.MAX_VALUE to int. Here you are casting too early, you have to cast after finding the difference when it is guaranteed to be less than Integer.MAX_VALUE. This should be `return readableStreamChannel.getSize() - totalBytesRead.get() < Integer.MAX_VALUE ? (int) (readableStreamChannel.getSize() - totalBytesRead.get()) : Integer.MAX_VALUE;`
Source:
public int read() throws IOException { ByteBuffer buffer = singleByteBufferChannel.getBuffer(); buffer.clear(); int data = -1; if (read(singleByteBufferChannel) != -1) { buffer.flip(); data = buffer.get() & 0xFF; totalBytesRead.incrementAndGet(); } return data; }

Target:
byte to int is always safe, so why can't you simply do `data = buffer.get()`?
Source:
HardDeleteThread() { this.throttler = new Throttler(config.storeHardDeleteBytesPerSec, throttlerCheckIntervalMs, true, SystemTime.getInstance()); }

Target:
not related to this patch. Why hardDeleterSleepTimeWhenCaughtUpMs is not configurable and hardcoded?
Source:
private Transmissions transmissions(SelectionKey key) { if (key == null) { throw new IllegalStateException("Attempt to get attachment from a null key"); } return (Transmissions) key.attachment(); }

Target:
do we need this check? You can argue such statements in all other methods.
Source:
@Override public String toString () { return "DataNode[" + getHostname() + ":" + getPort() + "]"; }

Target:
what formatting is this? @override should be above the method
Source:
@Override public boolean equals (Object o){ if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } DataNode dataNode = (DataNode) o; if (port != dataNode.port) { return false; } return hostname.equals(dataNode.hostname); }

Target:
do you need to check the other fields? two objects cannot be equal otherwise right?
Source:
@Override public int compareTo (DataNodeId o){ if (o == null) { throw new NullPointerException("input argument null"); } DataNode other = (DataNode) o; int compare = (port < other.port) ? -1 : ((port == other.port) ? 0 : 1); if (compare == 0) { compare = hostname.compareTo(other.hostname); } return compare; }

Target:
what does compare and hascode mean for datanode if they have different ssl ports?
Source:
protected OperationRequest makeOperationRequest(ReplicaId replicaId) { DeleteRequest deleteRequest = new DeleteRequest(context.getCorrelationId(), context.getClientId(), blobId); if (sslEnabledColos.contains(replicaId.getDataNodeId().getDatacenterName())) { return new DeleteOperationRequest(connectionPool, responseQueue, context, blobId, replicaId, deleteRequest, true); } else { return new DeleteOperationRequest(connectionPool, responseQueue, context, blobId, replicaId, deleteRequest, false); } }

Target:
this check is not required if the colo info is in context.
Source:
public static void startAdminBlobStorageService() throws InstantiationException, IOException { clusterMap = new MockClusterMap(false); adminBlobStorageService = getAdminBlobStorageService(clusterMap); adminBlobStorageService.start(); }

Target:
can we just keep the default constructor that sets this to false?
Source:
public static JSONArray getJsonArrayDuplicateDataNodes(int dataNodeCount, String hostname, int basePort, HardwareState hardwareState, JSONArray disks) throws JSONException { JSONArray jsonArray = new JSONArray(); for (int i = 0; i < dataNodeCount; ++i) { jsonArray.put(getJsonDataNode(hostname, basePort + i, hardwareState, disks)); } return jsonArray; }

Target:
why change it to basePort + i here?
Source:
public String toString() { return "Request metadata: " + (restRequestMetadata != null ? restRequestMetadata.toString() : "null") + " Request content: " + (restRequestContent != null ? restRequestContent.toString() : "null") + " Response handler: " + (restResponseHandler != null ? restResponseHandler.toString() : "null"); }

Target:
Actually using StringBuilder is better and faster here. The compiler will optimize string concatenation to use StringBuffer, not StringBuilder. StringBuilder is asynchronous and runs faster.
Source:
public void onComplete(Exception e) { logger.trace("Firing onComplete() for listeners"); synchronized (listeners) { exception = e; if (operationComplete.compareAndSet(false, true)) { for (RestRequestInfoEventListener listener : listeners) { try { listener.onHandlingComplete(this, e); } catch (Exception ee) { logger.error("Swallowing onComplete listener exception", ee); } } } else { logger.error("onComplete for RestRequestInfo has already been fired. Ignoring current invocation"); } } }

Target:
Yes, I meant 'if'.
Source:
public AdminMetrics(MetricRegistry metricRegistry) { //errors unknownActionErrorCount = metricRegistry.counter(MetricRegistry.name(AdminBlobStorageService.class, "unknownActionErrorCount")); }

Target:
is this the only metric you need?
Source:
public void channelInactive(ChannelHandlerContext ctx) { // TODO: once we support keepalive, we need to do this at the end of a request too. try { if (requestHandler != null) { requestHandler.onRequestComplete(request); } if (responseHandler != null) { responseHandler.onRequestComplete(request); } } catch (Exception e) { logger.error("Unable to perform cleanup tasks. Swallowing exception..", e); nettyMetrics.channelInactiveTasksFailureCount.inc(); } }

Target:
we would need to move this logic elsewhere and not rely on channelInactive for keep alive. Why not just do that now?
Source:
public void run() { try { while (running) { if (!hardDelete()) { try { Thread.sleep(hardDeleterSleepTimeWhenCaughtUpMs); } catch (InterruptedException e) { logger.info("Caught interrupted exception"); } } } } finally { shutdownLatch.countDown(); } }

Target:
I meant we need to know if the thread is running independent of the progress. you could be caught up but the thread can die and we will not know.
Source:
public long appendFrom(ReadableByteChannel channel, long size) throws IOException { int tempIndex = index; index++; int sizeRead = 0; while(sizeRead < size) { sizeRead += channel.read(buflist.get(tempIndex)); tempIndex = index++; } if(sizeRead != size) { throw new IOException("Not able to read entire size. Expected " + size+" Actual " + sizeRead); } return sizeRead; }

Target:
Do you really need a separate tempIndex variable?
Source:
public void onNodeTimeout() { String dataNodeStr; try { dataNodeStr = toJSONObject().toString(); } catch (JSONException e) { dataNodeStr = null; } logger.info("Node [" + dataNodeStr + "] timed out, informing resource state"); if (dataNodeStatePolicy.onError()) { logger.info("Node [" + dataNodeStr + "] was determined to be down"); } }

Target:
It will be good to avoid converting the node to a JSON and then to a String which is expensive. We should just define a toString if it is not already defined and log(this).
Source:
private PartitionId getPartitionForPut() throws CoordinatorException { List<? extends PartitionId> partitions = clusterMap.getWritablePartitionIds(); if (partitions.isEmpty()) { throw new CoordinatorException("No writable partitions available.", CoordinatorError.AmbryUnavailable); } return partitions.get((int) getRandomLong(randomForPartitionSelection, partitions.size())); }

Target:
List<PartitionId>?
Source:
public RemoteReplicaInfo(ReplicaId replicaId, Store localStore, FindToken token, long tokenPersistIntervalInMs) { this.replicaId = replicaId; this.currentToken = token; if (tokenToPersist == null) { this.tokenToPersist = token; timeTokenSetInMs = SystemTime.getInstance().milliseconds(); } this.tokenPersistIntervalInMs = tokenPersistIntervalInMs; this.totalBytesReadFromLocalStore = new Long(0); this.localStore = localStore; }

Target:
"= 0" is sufficient to initialize 'long totalBytesReadFromLocalStore;'
Source:
public MockStore(List<MessageInfo> messageInfo, List<ByteBuffer> buffers) { if (messageInfo.size() != buffers.size()) { throw new IllegalArgumentException("message info size and buffer size does not match"); } messageInfoList = messageInfo; log = new DummyLog(buffers); bytesWrittenSoFar = new Long(0); }

Target:
"= 0" seems sufficient?
Source:
public long getAllocatedRawCapacityInBytes() { long allocatedRawCapacityInBytes = 0; for (Partition partition : partitionMap.values()) { allocatedRawCapacityInBytes += partition.getAllocatedRawCapacityInBytes(); } return allocatedRawCapacityInBytes; }

Target:
It might be better off to calculate all this on initialization of the clustermap. these are never going to change and recalculating them every minute is not useful.
Source:
public BlobProperties getBlobProperties(String blobIdString) throws CoordinatorException { OperationContext oc = getOperationContext(); BlobId blobId = getBlobIdFromString(blobIdString); GetBlobPropertiesOperation gbpo = new GetBlobPropertiesOperation(datacenterName, connectionPool, requesterPool, oc, blobId, operationTimeoutMs, clusterMap); gbpo.execute(); return gbpo.getBlobProperties(); }

Target:
this is lot more clear
Source:
public long sizeInBytes() { // header + blobId return super.sizeInBytes() + Blob_Id_Size_In_Bytes + blobId.sizeInBytes(); }

Target:
This is a bug. blobId.sizeInBytes already returns the serialized form size.
Source:
public StoreMetrics(String storeName, ReadableMetricsRegistry registry) { super(registry); writes = newCounter(storeName + "-Writes"); reads = newCounter(storeName + "-Reads"); deletes = newCounter(storeName + "-Deletes"); overflowWriteError = new Counter(storeName + "-OverflowWriteError"); }

Target:
I cannot tell if this is correct or not. Why not "newCounter" instead of "new Counter". Also, missing cancelTTLs = newCounter(storeName + "-Deletes"); On last project, we ended up with inconsistent metrics names ("a-b" mixed with "a_b"). We can either watch this via code reviews or try something like Metric.NameSeparator="-". I think code reviews are fine but we need to be vigilant because as soon as someone has a dashboard or alert set to metric name, its permanent.
Source:
private void validateParameter(String name, String value) { Class<? extends IParameterValidator> validators[] = wrappedParameter.validateWith(); if (validators != null && validators.length>0) { for(final Class<? extends IParameterValidator> validator: validators) { validateParameter(this, validator, name, value); } } }

Target:
Spaces: `length > 0`.
Source:
public static <T> T readBy(Session session, Class<T> clazz, String id) { LOG.trace("GenericDBUtil.readBy"); T entity = null; try { entity = session.get(clazz, id); LOG.trace("GenericDBUtil.readBy - entity = session.get()"); } catch (HibernateException | NullPointerException e) { LOG.error("Exception during read caused by : {}", e.getMessage(), e); throw e; } finally { closeSession(session); } return entity; }

Target:
Either we log or rethrow exception but not both. See this https://sonarcloud.io/organizations/default/rules?open=squid%3AS2139&rule_key=squid%3AS2139
Source:
public int pingEndpoint() { String sName = selectedEndpoint.getName(); int pingResponse = 0; for (ConnectionEndpoint connEndpoint : endpoints) { String eName = connEndpoint.getName(); if (sName.equals(eName)) { return exchangeService.pingService(connEndpoint); } } return pingResponse; }

Target:
we never set this anywhere in the code. Either remove it and hardcode 0 as a return value or set pingResponse to 0 and break from the loop on line 348.
Source:
public LoadTestDataSimpleResponseMessageType deleteAddress(DeleteAddressRequestMessageType request) { Address record = new Address(request.getAddress(), loadTestDataService.getPatientBy(request.getAddress().getPatientId())); if (null == record) { return buildSimpleResponse(Boolean.FALSE, ACT_FAIL); } loadTestDataService.deleteAddress(record); return buildSimpleResponse(Boolean.TRUE, ACT_SUCCESSFUL); }

Target:
What are all these null checks for? These can never be null - You're creating the object in the lines directly above?
Source:
public boolean checkDistinguishedName(String userName) { Name name = null; try { name = new LdapName(userName); } catch (InvalidNameException ex) { LOG.error("Invalid distinguished name {}", userName); LOG.debug("Invalid distinguished name {}", userName, ex); } return name != null; }

Target:
change debug to warn or error. No need to include both inside here
Source:
public static String getExchangeAlias(String exchangeName) { String alias = null; if (StringUtils.isNotBlank(exchangeName)) { List<ExchangeType> exchanges = ExchangeManager.getInstance().getAllExchanges(); if (CollectionUtils.isNotEmpty(exchanges)) { for (ExchangeType ex : exchanges) { if (exchangeName.equalsIgnoreCase(ex.getName())) { alias = ex.getCertificateAlias(); break; } } } } return alias; }

Target:
exchangeManager.getGatewayAlias however the internalExchangeManager.getGatewayAlias will return an null and will default:CIENT_KEY_ALIAS