auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
public void stop(@Nonnull Throwable cause) throws Exception { if (body!=null) START body.cancel(cause); END } can you enclose this in `{ }` ;

Target:
public void stop(@Nonnull Throwable cause) throws Exception { if (body!=null) { body.cancel(cause); } }
Source:
public SequenceReader(List<? extends Reader> readers) { int size = readers.size(); START fReaders = new Reader[size]; END for (int i = 0; i < size; i++) { fReaders[i] = readers.get(i); } } @RobberPhex If you make the input a `Collection`, then you can take a `Set` as input. You should also consider using a List internally instead of an array. It might make the above simpler.

Target:
public SequenceReader(List<? extends Reader> readers) { final int size = readers.size(); fReaders = new Reader[size]; for (int i = 0; i < size; i++) { fReaders[i] = readers.get(i); } }
Source:
public void copyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory() throws IOException { START File srcDir = temporaryFolder.newFile("notadirectory"); END File destDir = temporaryFolder.newFolder("destinationDirectory"); String expectedMessage = String.format("Source '%s' is not a directory", srcDir); assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage); } Use `final` where possible.

Target:
public void copyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory() throws IOException { final File srcDir = temporaryFolder.newFile("notadirectory"); final File destDir = temporaryFolder.newFolder("destinationDirectory"); final String expectedMessage = String.format("Source '%s' is not a directory", srcDir); assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage); }
Source:
private static boolean isValidHostName(String name) { return isIPv4Address(name) || isIPv6Address(name) || isRFC3986HostName(name); START } END isIPv4Address can return false for input that looks like an IP address but fails a subsequent check, e.g. 999.9.9.9. In which case it will be subject to two further checks (which must fail if the code is correct).

Target:
private static boolean isValidHostName(String name) { return isIPv6Address(name) || isRFC3986HostName(name); }
Source:
protected ExpectedBehaviour getExpectedBehaviour() { START return excNumberDefined ? ExpectedBehaviour.SPECIFIC_EXCEPTION : ExpectedBehaviour.ANY_EXCEPTION; END } Yes, it looks good now. What do you mean by min number of references? (In general I think we have a Checkstyle will raise an error if we have unused imports).

Target:
protected ExpectedBehaviour getExpectedBehaviour() { return excNumberDefined ? SPECIFIC_EXCEPTION : ANY_EXCEPTION; }
Source:
abstract protected ResultSet getColumns() throws SQLException; public Map<String, DbParameterAccessor> getParams() throws SQLException { START Map<String, DbParameterAccessor> allParams = new HashMap<String, DbParameterAccessor>(); END try (ResultSet rs = getColumns()) { while (rs.next()) { DbParameterAccessor accessor = createColumnAccessor(rs); allParams.put(normaliseName(accessor.getName()), accessor); } } return allParams; } Should be fine to call `new HashMap<>()` here, no need to list types on the right side (I think we already did that somewhere).

Target:
abstract protected ResultSet getColumns() throws SQLException; public Map<String, DbParameterAccessor> getParams() throws SQLException { Map<String, DbParameterAccessor> allParams = new HashMap<>(); try (ResultSet rs = getColumns()) { while (rs.next()) { DbParameterAccessor accessor = createColumnAccessor(rs); allParams.put(normaliseName(accessor.getName()), accessor); } } return allParams; }
Source:
public void connect(String connectionString, Properties info) throws SQLException { registerDriver(); START if (currentConnection != null) { END closeConnection(); } currentConnection = DriverManager.getConnection(connectionString, info); afterConnectionEstablished(); } No need of that `null` check here - `closeConnection()` already takes care about that.

Target:
public void connect(String connectionString, Properties info) throws SQLException { registerDriver(); closeConnection(); currentConnection = DriverManager.getConnection(connectionString, info); afterConnectionEstablished(); }
Source:
public static String normaliseName(final String name) { if (name == null) { return ""; } if (isEscaped(name)) { START return name.substring(1,name.length() - 1).replace("\"\"", "\""); END } return dbfit.util.NameNormaliser.normaliseName(name); } Missing space after `1`.

Target:
public static String normaliseName(final String name) { if (name == null || name.isEmpty()) { return ""; } if (isEscaped(name)) { return name.substring(1, name.length() - 1).replace("\"\"", "\""); } return dbfit.util.NameNormaliser.normaliseName(name); }
Source:
public static Object normaliseValue(Object currVal) throws SQLException { if (currVal==null) return null; TypeTransformer tn=TypeNormaliserFactory.getNormaliser(currVal.getClass()); START if (tn!=null) currVal=tn.transform(currVal); END return currVal; } As we're anyway touching these lines, let's reformat and add space around `=` and `!=`.

Target:
public static Object normaliseValue(Object currVal) throws SQLException { if (currVal == null) { return null; } TypeTransformer tn = TypeNormaliserFactory.getNormaliser(currVal.getClass()); if (tn != null) { currVal = tn.transform(currVal); } return currVal; }
Source:
public void run() throws SQLException { ResultSet rs = statement.executeQuery(); try { rs.next(); returnValue = rs.getObject(1); START } finally { END rs.close(); } } In other places we've been using [try with resources syntax](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). Might be appropriate here too?

Target:
public void run() throws SQLException { try (ResultSet rs = statement.executeQuery()) { rs.next(); returnValue = rs.getObject(1); } }
Source:
public void afterConnectionEstablished() throws SQLException { START TypeAdapter.registerParseDelegate(java.math.BigDecimal.class, new dbfit.util.BigDecimalParseDelegate()); END if (currentConnection.getMetaData().supportsTransactions()) { Options.setOption(Options.OPTION_AUTO_COMMIT, "false"); currentConnection.setAutoCommit(false); } else { Options.setOption(Options.OPTION_AUTO_COMMIT, "true"); currentConnection.setAutoCommit(true); } } Looks like this `registerParseDelegate` call is already present in `FitNesseTestHost`. Do we need it here again?

Target:
public void afterConnectionEstablished() throws SQLException { if (currentConnection.getMetaData().supportsTransactions()) { Options.setOption(Options.OPTION_AUTO_COMMIT, "false"); currentConnection.setAutoCommit(false); } else { Options.setOption(Options.OPTION_AUTO_COMMIT, "true"); currentConnection.setAutoCommit(true); } }
Source:
public StatementExecution toStatementExecution() throws SQLException { START System.out.println("DbStoredProcedureCall: toStatementExecution"); END StatementExecution cs = new StatementExecution( this.environment.getConnection().prepareCall(toSqlString()), isFunction() ? this.environment.functionReturnValueViaResultSet() : false, isFunction() ? this.environment.discountFunctionReturnValueParameter() : false); bindParametersTo(cs); return cs; } Do we need that `System.out.println`?

Target:
public StatementExecution toStatementExecution() throws SQLException { StatementExecution cs = new StatementExecution( this.environment.getConnection().prepareCall(toSqlString()), isFunction() ? this.environment.functionReturnValueViaResultSet() : false, isFunction() ? this.environment.discountFunctionReturnValueParameter() : false); bindParametersTo(cs); return cs; }
Source:
public void setObject(int index, Object value, int sqlType) throws SQLException { START if (value == null) END statement.setNull(index, sqlType); else // Don't use the variant that takes sqlType. // Derby (at least) assumes no decimal places for Types.DECIMAL and truncates the source data. statement.setObject(index, value); } The general convention is to use `{}` braces in if/else statements.

Target:
public void setObject(int index, Object value, int sqlType) throws SQLException { if (value == null) { statement.setNull(index, sqlType); } else { // Don't use the variant that takes sqlType. // Derby (at least) assumes no decimal places for Types.DECIMAL and truncates the source data. statement.setObject(index, value); } }
Source:
public StatementExecution buildPreparedStatement() throws SQLException { START return new StatementExecution(environment.createStatementWithBoundFixtureSymbols(testHost, statementText), false, environment.supportsSavepoints()); END } These SavePoints are bringing some noise here. There was a discussion in #211 about removing them: if we do that, we could simplify the changes here.

Target:
public StatementExecution buildPreparedStatement() throws SQLException { return new StatementExecution(environment.createStatementWithBoundFixtureSymbols(testHost, statementText), false); }
Source:
public void doTable(Parse table) { super.doTable(table); START if (counts.wrong == 0 && counts.exceptions == 0) { END table.parts.last().more = getSummary(); } } what's the thinking behind only showing this when everything's OK? Is there a particular reason why we don't want to show the summary when matching fails?

Target:
public void doTable(Parse table) { super.doTable(table); table.parts.last().more = getSummary(); }
Source:
private IProject getProject(final IJavaElementDelta delta) { final IJavaProject javaProject = delta.getElement().getJavaProject(); if (javaProject != null) { return javaProject.getProject(); } // carry on with children elements. for (IJavaElementDelta affectedChild : delta.getAffectedChildren()) { START return getProject(affectedChild); END } // if no child return null; } so you're returning something on the very first iteration. Not sure of the consequences of that.

Target:
private IProject getProject(final IJavaElementDelta delta) { final IJavaProject javaProject = delta.getElement().getJavaProject(); if (javaProject != null) { return javaProject.getProject(); } // carry on with children elements. for (IJavaElementDelta affectedChild : delta.getAffectedChildren()) { final IProject project = getProject(affectedChild); if(project != null) { return project; } } // if no child return null; }
Source:
public void indexMarker(final IMarker marker) { try { Logger.traceIndexing("Indexing after addition...", marker); final Document doc = LuceneDocumentFactory.createDocument(marker); Logger.debugIndexing("Adding Marker into index with following fields: {}", doc.getFields()); indexWriter.addDocument(doc); indexWriter.commit(); START } catch (Exception e) { END Logger.error("Failed to index the JAX-RS Endpoint " + marker, e); } finally { Logger.traceIndexing(" Done indexing {}.", marker); } } Could specific exception be used here?

Target:
public void indexMarker(final IMarker marker) { try { Logger.traceIndexing("Indexing after addition...", marker); final Document doc = LuceneDocumentFactory.createDocument(marker); Logger.debugIndexing("Adding Marker into index with following fields: {}", doc.getFields()); indexWriter.addDocument(doc); indexWriter.commit(); } catch (IOException e) { Logger.error("Failed to index the JAX-RS Endpoint " + marker, e); } finally { Logger.traceIndexing(" Done indexing {}.", marker); } }
Source:
private static boolean isMessageContinue(@NotNull String messagePart) { START return Character.isWhitespace(messagePart.charAt(0)); END } This is dangerous. You're not 100% sure you're not given an empty message here, yet you call charAt(0).

Target:
private static boolean isMessageContinue(@NotNull String messagePart) { return messagePart.isEmpty() || Character.isWhitespace(messagePart.charAt(0)); }
Source:
private Boolean calcNoAutoImportAll() { START return containsCompileDirectiveWithOption("no_auto_import"); END } Please check how it works with 'quoted' names.

Target:
private boolean calcNoAutoImportAll() { return containsCompileDirectiveWithOption("no_auto_import"); }
Source:
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { ErlangFunctionCallExpression callExpr = PsiTreeUtil.getParentOfType( descriptor.getPsiElement(), ErlangFunctionCallExpression.class); if (callExpr == null) return; START callExpr.replace(ErlangElementFactory.createFunctionWithModuleCallExpression(project, myModuleName, callExpr.getText())); END } Invert if statement please.

Target:
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { ErlangFunctionCallExpression callExpr = PsiTreeUtil.getParentOfType( descriptor.getPsiElement(), ErlangFunctionCallExpression.class); if (callExpr != null) { callExpr.replace(ErlangElementFactory.createFunctionWithModuleCallExpression(project, myModuleName, callExpr.getText())); } }
Source:
START public void testCommon() throws Throwable { END doTest("Remove from import"); } throws Throwable?

Target:
public void testCommon() { doTest(); }
Source:
private static List<String> replaceElementsSuffix(List<String> bcp, String originalSuffix, String newSuffix) { for (int i=0; i<bcp.size(); i++) { String entry = bcp.get(i); if (entry.endsWith(originalSuffix)) { START bcp.set(i, entry.substring(0, entry.length() - 4) + newSuffix); END } } return bcp; } entry.length() - originalSuffix.length()

Target:
private static List<String> replaceElementsSuffix(List<String> bcp, String originalSuffix, String newSuffix) { for (int i=0; i<bcp.size(); i++) { String entry = bcp.get(i); if (entry.endsWith(originalSuffix)) { bcp.set(i, entry.substring(0, entry.length() - originalSuffix.length()) + newSuffix); } } return bcp; }
Source:
START public Collection<LabelItem> getLabels() { END return new ArrayList<LabelItem>(labels); } if Getter returns a Collection, why this creates ArrayList first?

Target:
public List<LabelItem<?>> getLabels() { return new ArrayList<LabelItem<?>>(labels); }
Source:
protected void log(final Level level, final String msg) { START this.logger.log(level, this.simpleName + " " + msg); END } warning: The static field AbstractProcessor.logger should be accessed in a static way.

Target:
protected void log(final Level level, final String msg) { this.LOGGER.log(level, this.simpleName + " " + msg); }
Source:
START public Map<StoreKey, MessageInfo> findKeys(List<? extends StoreKey> storeKeys) throws StoreException { END Map<StoreKey, MessageInfo> messageInfoMap = new HashMap<>(); for (StoreKey storeKey : storeKeys) { // TODO: Need to look into the possibility if its more efficient to not call findKey() multiple times in a loop, // and instead search the index once for all the blobs in the list. try { messageInfoMap.put(storeKey, findKey(storeKey)); } catch (StoreException e) { logger.error("findKey failed for key with reason {}", storeKey.getID(), e.getErrorCode().toString()); } } return messageInfoMap; } This method doesn't throw out StoreException.

Target:
public Map<StoreKey, MessageInfo> findKeys(List<? extends StoreKey> storeKeys) throws StoreException { Map<StoreKey, MessageInfo> messageInfoMap = new HashMap<>(); for (StoreKey storeKey : storeKeys) { // TODO: Need to look into the possibility if its more efficient to not call findKey() multiple times in a loop, // and instead search the index once for all the blobs in the list. try { messageInfoMap.put(storeKey, findKey(storeKey)); } catch (StoreException e) { logger.error("findKey failed for key with reason {}", storeKey.getID(), e.getErrorCode().toString()); throw e; } } return messageInfoMap; }
Source:
public boolean hasPrevious() { START return cursor != 0; END } why not `cursor > 0` ?

Target:
public boolean hasPrevious() { return cursor > 0; }
Source:
CompactionDetails getCompactionDetails(BlobStore blobStore) throws StoreException { ReplicaId replicaId = blobStore.getReplicaId(); START if (compactionPolicyFactory != null && compactionPolicyFactory instanceof HybridCompactionPolicyFactory) { CompactionPolicyCounter compactionPolicyCounter = replicaToCounterMap.getOrDefault(replicaId, new CompactionPolicyCounter(storeConfig)); compactionPolicyCounter.increment(); replicaToCounterMap.put(replicaId, compactionPolicyCounter); } END return blobStore.getCompactionDetails(compactionPolicy, replicaToCounterMap.get(replicaId)); } we actually don't have to differentiate the compaction policy factory when creating and incrementing the couter. We can just increment it for every policy.

Target:
CompactionDetails getCompactionDetails(BlobStore blobStore) throws StoreException { return blobStore.getCompactionDetails(compactionPolicy); }
Source:
public void sendResponse(Send payloadToSend, NetworkRequest originalRequest, ServerNetworkResponseMetrics metrics) throws InterruptedException { if (!(originalRequest instanceof NettyServerRequest)) { throw new IllegalArgumentException("NetworkRequest should be NettyRequest"); } ChannelHandlerContext ctx = ((NettyServerRequest) originalRequest).getCtx(); http2ServerMetrics.requestTotalProcessingTime.update( System.currentTimeMillis() - originalRequest.getStartTimeInMs()); START ctx.pipeline().writeAndFlush(payloadToSend); END } ctx.writeAndFlush would do the work. By the way, maybe we can do `ctx.isOpen` check before we write, it, the connection might be closed before server got a chance to write.

Target:
public void sendResponse(Send payloadToSend, NetworkRequest originalRequest, ServerNetworkResponseMetrics metrics) throws InterruptedException { if (!(originalRequest instanceof NettyServerRequest)) { throw new IllegalArgumentException("NetworkRequest should be NettyRequest"); } ChannelHandlerContext ctx = ((NettyServerRequest) originalRequest).getCtx(); http2ServerMetrics.requestTotalProcessingTime.update( System.currentTimeMillis() - originalRequest.getStartTimeInMs()); ctx.channel().writeAndFlush(payloadToSend); }
Source:
START public ChannelHandlerContext getCtx() { END return ctx; } package-private?

Target:
ChannelHandlerContext getCtx() { return ctx; }
Source:
public Http2MultiplexedChannelPool(InetSocketAddress inetSocketAddress, SSLFactory sslFactory, EventLoopGroup eventLoopGroup, Http2ClientConfig http2ClientConfig, Http2ClientMetrics http2ClientMetrics, ChannelInitializer streamChannelInitializer) { this(new SimpleChannelPool(createBootStrap(eventLoopGroup, http2ClientConfig, inetSocketAddress), new Http2ChannelPoolHandler(sslFactory, inetSocketAddress.getHostName(), inetSocketAddress.getPort(), http2ClientConfig)), eventLoopGroup, ConcurrentHashMap.newKeySet(), inetSocketAddress, START new Http2ClientConfig(new VerifiableProperties(new Properties())), http2ClientMetrics, END streamChannelInitializer); } why is the http2ClientConfig passed in from the constructor not used here?

Target:
public Http2MultiplexedChannelPool(InetSocketAddress inetSocketAddress, SSLFactory sslFactory, EventLoopGroup eventLoopGroup, Http2ClientConfig http2ClientConfig, Http2ClientMetrics http2ClientMetrics, ChannelInitializer streamChannelInitializer) { this(new SimpleChannelPool(createBootStrap(eventLoopGroup, http2ClientConfig, inetSocketAddress), new Http2ChannelPoolHandler(sslFactory, inetSocketAddress.getHostName(), inetSocketAddress.getPort(), http2ClientConfig)), eventLoopGroup, ConcurrentHashMap.newKeySet(), inetSocketAddress, http2ClientConfig, http2ClientMetrics, streamChannelInitializer); }
Source:
START public InMemoryStore getStore(PartitionId partitionId) { END return storesByPartition.get(partitionId); } same here, can be package private

Target:
InMemoryStore getStore(PartitionId partitionId) { return storesByPartition.get(partitionId); }
Source:
public void validateRequest(RestRequest restRequest, Callback<Void> callback) { Exception exception = null; serverMetrics.securityServiceValidateRequestRate.mark(); long startTimeMs = System.currentTimeMillis(); if (!isOpen) { exception = new RestServiceException("ServerSecurityService is closed", RestServiceErrorCode.ServiceUnavailable); } else if (restRequest == null) { throw new IllegalArgumentException("restRequest is null"); } START serverMetrics.securityServiceValidateRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs); END callback.onCompletion(null, exception); } Remove timer.

Target:
public void validateRequest(RestRequest restRequest, Callback<Void> callback) { Exception exception = null; serverMetrics.securityServiceValidateRequestRate.mark(); if (!isOpen) { exception = new RestServiceException("ServerSecurityService is closed", RestServiceErrorCode.ServiceUnavailable); } else if (restRequest == null) { throw new IllegalArgumentException("restRequest is null"); } callback.onCompletion(null, exception); }
Source:
public PartitionLeaderInfo(StoreManager storeManager) { START peerLeaderReplicasByPartition = new ConcurrentHashMap<>(); END this.storeManager = storeManager; } minor: can remove this and directly initialize it at line 41

Target:
public PartitionLeaderInfo(StoreManager storeManager) { this.storeManager = storeManager; // We can't initialize the peerLeaderReplicasByPartition here because we don't know the leader partitions on local node (server) until it has finished participating with Helix. // peerLeaderReplicasByPartition map will be updated after server participates with Helix and state of replicas transition to LEADER (via onPartitionBecomeLeaderFromStandby()) }
Source:
void addToCache(String blobKey, short lifeVersion, BlobState blobState) { if (isVcr) { if (blobState == BlobState.TTL_UPDATED) { // In case of ttl update we update the ttl without taking into account the life version. // So make sure that we do not decrease the lifeVersion in cache due to an incoming ttl update. if (recentBlobCache.containsKey(blobKey)) { lifeVersion = (lifeVersion > recentBlobCache.get(blobKey).getLifeVersion()) ? lifeVersion START : recentBlobCache.get(blobKey).getLifeVersion(); END } } recentBlobCache.put(blobKey, new BlobLifeState(blobState, lifeVersion, recentBlobCache.get(blobKey))); } } Can this just be Math.max(lifeVersion, recentBlobCache.get(blobKey).getLifeVersion()) ?

Target:
void addToCache(String blobKey, short lifeVersion, BlobState blobState) { if (isVcr) { if (blobState == BlobState.TTL_UPDATED) { // In case of ttl update we update the ttl without taking into account the life version. // So make sure that we do not decrease the lifeVersion in cache due to an incoming ttl update. if (recentBlobCache.containsKey(blobKey)) { lifeVersion = (short) Math.max(lifeVersion, recentBlobCache.get(blobKey).getLifeVersion()); } } recentBlobCache.put(blobKey, new BlobLifeState(blobState, lifeVersion, recentBlobCache.get(blobKey))); } }
Source:
private static boolean hasReachedAnyLocalQuorum(Map<String, Integer> totalNumberInDcs, Map<String, Integer> currentNumberInDcs) { for (String dcName : totalNumberInDcs.keySet()) { int totalNum = totalNumberInDcs.get(dcName); if (currentNumberInDcs.containsKey(dcName)) { int currentNum = currentNumberInDcs.get(dcName); START if (totalNum != 1 && currentNum > totalNum / 2) { END return true; } } } return false; } same question here, do we account for the colo with single replica?

Target:
static boolean hasReachedAnyLocalQuorum(Map<String, Integer> totalNumberInDcs, Map<String, Integer> currentNumberInDcs) { for (String dcName : totalNumberInDcs.keySet()) { int totalNum = totalNumberInDcs.get(dcName); if (currentNumberInDcs.containsKey(dcName)) { int currentNum = currentNumberInDcs.get(dcName); if (currentNum > totalNum / 2) { return true; } } } return false; }
Source:
public static boolean isPossibleClientTermination(Throwable cause) { START if (cause instanceof SSLException) { return SSL_ENGINE_CLOSED_EXCEPTION_MSG.equals(cause.getMessage()); END } else if (cause instanceof IOException) { return CLIENT_RESET_EXCEPTION_MSG.equals(cause.getMessage()) || CLIENT_BROKEN_PIPE_EXCEPTION_MSG.equals( cause.getMessage()); } else { return false; } } Actually SSLException is an IOException, so we don't have to do this if-else statement.

Target:
public static boolean isPossibleClientTermination(Throwable cause) { return cause instanceof IOException && (CLIENT_RESET_EXCEPTION_MSG.equals(cause.getMessage()) || CLIENT_BROKEN_PIPE_EXCEPTION_MSG.equals(cause.getMessage()) || SSL_ENGINE_CLOSED_EXCEPTION_MSG.equals( cause.getMessage())); }
Source:
public IndexEntry next() { try { StoreKey key = getKeyAt(mmap, currentIdx); mmap.get(valueBuf); return new IndexEntry(key, new IndexValue(startOffset.getName(), ByteBuffer.wrap(valueBuf), getVersion())); } catch (Exception e) { logger.error("Failed to read index entry at " + currentIdx, e); } finally { currentIdx++; } START return null; END } This will cause NullPointException if it failed to read index. Is this intended?

Target:
public IndexEntry next() { try { StoreKey key = getKeyAt(mmap, currentIdx); mmap.get(valueBuf); return new IndexEntry(key, new IndexValue(startOffset.getName(), ByteBuffer.wrap(valueBuf), getVersion())); } catch (Exception e) { String message = "Failed to read index entry at " + currentIdx; logger.error(message, e); throw new IllegalStateException(message, e); } finally { currentIdx++; } }
Source:
START public Map<String, List<ReplicaId>> getPeerLeaderReplicasByPartition() { END return Collections.unmodifiableMap(peerLeaderReplicasByPartition); } This can be package private. Also add java doc to this method please.

Target:
Map<String, List<ReplicaId>> getPeerLeaderReplicasByPartition() { return Collections.unmodifiableMap(peerLeaderReplicasByPartition); }
Source:
public void onPartitionBecomeLeaderFromStandby(String partitionName) { try { PartitionStateChangeListener cloudToStoreReplicationListener = partitionStateChangeListeners.get(StateModelListenerType.CloudToStoreReplicationManagerListener); if (cloudToStoreReplicationListener != null) { cloudToStoreReplicationListener.onPartitionBecomeLeaderFromStandby(partitionName); } } catch (Exception e) { START participantMetrics.standbyCount.addAndGet(-1); END participantMetrics.errorStateCount.addAndGet(1); throw e; } participantMetrics.standbyCount.addAndGet(-1); participantMetrics.leaderCount.addAndGet(1); } see above about finally.

Target:
public void onPartitionBecomeLeaderFromStandby(String partitionName) { try { PartitionStateChangeListener cloudToStoreReplicationListener = partitionStateChangeListeners.get(StateModelListenerType.CloudToStoreReplicationManagerListener); if (cloudToStoreReplicationListener != null) { cloudToStoreReplicationListener.onPartitionBecomeLeaderFromStandby(partitionName); } } catch (Exception e) { participantMetrics.errorStateCount.addAndGet(1); throw e; } finally { participantMetrics.standbyCount.addAndGet(-1); } participantMetrics.leaderCount.addAndGet(1); }
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; } } START participantMetrics.offlineCount.addAndGet(-1); END participantMetrics.partitionDroppedCount.inc(); } see above about finally.

Target:
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); try { if (storageManagerListener != null) { storageManagerListener.onPartitionBecomeDroppedFromOffline(partitionName); } } catch (Exception e) { participantMetrics.errorStateCount.addAndGet(1); throw e; } finally { participantMetrics.offlineCount.addAndGet(-1); } participantMetrics.partitionDroppedCount.inc(); }
Source:
boolean awaitBlobCreations() throws InterruptedException { START return creationHelper.await(11, TimeUnit.SECONDS); END } I am curious how much difference it has with this minor change.

Target:
boolean awaitBlobCreations() throws InterruptedException { return creationHelper.await(10, TimeUnit.SECONDS); }
Source:
public void setException(Exception e) { START if (e == null) { END this.e = null; } else { this.e = new RuntimeException(e); } } nit : `this.e = e == null ? e : new RuntimeException(e);`

Target:
public void setException(Exception e) { this.e = e == null ? null : new RuntimeException(e); }
Source:
public PutMessageFormatInputStream(StoreKey key, ByteBuffer blobEncryptionKey, BlobProperties blobProperties, ByteBuffer userMetadata, InputStream blobStream, long streamSize) throws MessageFormatException { START this(key, blobEncryptionKey, blobProperties, userMetadata, blobStream, streamSize, BlobType.DataBlob); END } you can change this to: ``` this(key, blobEncryptionKey, blobProperties, userMetadata, blobStream, streamSize, BlobType.DataBlob, (short) 0); ``` avoiding an extra method call.

Target:
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, (short) 0); }
Source:
START public CloudBlobMetadata getOldestDeletedBlob(String partitionPath) throws CloudStorageException { END List<CloudBlobMetadata> deadBlobs = requestAgent.doWithRetries( () -> cloudDestination.getDeletedBlobs(partitionPath, System.currentTimeMillis(), queryLimit), "GetDeadBlobs", partitionPath); return deadBlobs.isEmpty() ? null : deadBlobs.get(0); } please add java docs to these methods

Target:
public CloudBlobMetadata getOldestDeletedBlob(String partitionPath) throws CloudStorageException { List<CloudBlobMetadata> deadBlobs = requestAgent.doWithRetries( () -> cloudDestination.getDeletedBlobs(partitionPath, 1, System.currentTimeMillis(), 1), "GetDeadBlobs", partitionPath); return deadBlobs.isEmpty() ? null : deadBlobs.get(0); }
Source:
String getLiveness() { String liveness = UP; if (resourceStatePolicy.isHardDown()) { liveness = NODE_DOWN; } else if (resourceStatePolicy.isDown()) { liveness = SOFT_DOWN; } START return liveness; END } Can simplify to one line, no? Or if you're going for readability, you can eliminate the local variable and just do "return ..." everywhere.

Target:
String getLiveness() { return resourceStatePolicy.isHardDown() ? NODE_DOWN : resourceStatePolicy.isDown() ? SOFT_DOWN : UP; }
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 = START ClusterMapUtils.getInstanceName(cloudServiceDataNode.getDatacenterName(), cloudServiceDataNode.getPort()); END } Since in `CloudServiceDataNode` hostname will be datacenter name, I think we can use `cloudServiceDataNode.getHostname()` to generate instance name.

Target:
public CloudServiceClusterChangeHandler(String dcName, ClusterMapConfig clusterMapConfig, ClusterChangeHandlerCallback changeHandlerCallback) throws Exception { this.dcName = dcName; this.clusterMapConfig = clusterMapConfig; this.changeHandlerCallback = changeHandlerCallback; cloudServiceDataNode = new CloudServiceDataNode(dcName, clusterMapConfig); }
Source:
public List<PartitionId> getAllPartitionIds(String partitionClass) { List<PartitionId> staticPartitionIds = staticClusterManager.getAllPartitionIds(partitionClass); return staticPartitionIds; START } END combine two lines

Target:
public List<PartitionId> getAllPartitionIds(String partitionClass) { return staticClusterManager.getAllPartitionIds(partitionClass); }
Source:
public void registerClusterMapListener(ClusterMapChangeListener clusterMapChangeListener) { START if (helixClusterManager != null) { END helixClusterManager.registerClusterMapListener(clusterMapChangeListener); } } Can it be null? You assume it's not in other places.

Target:
public void registerClusterMapListener(ClusterMapChangeListener clusterMapChangeListener) { helixClusterManager.registerClusterMapListener(clusterMapChangeListener); }
Source:
public BoundedNettyByteBufReceive replace(ByteBuf content) { BoundedNettyByteBufReceive receive = new BoundedNettyByteBufReceive(content, sizeToRead); START return receive; END } variable `receive` is redundant.

Target:
public BoundedNettyByteBufReceive replace(ByteBuf content) { return new BoundedNettyByteBufReceive(content, sizeToRead); }
Source:
public ResponseInfo replace(ByteBuf content) { ResponseInfo info = new ResponseInfo(requestInfo, error, content, dataNode); START return info; END } `info` is redundant

Target:
public ResponseInfo replace(ByteBuf content) { return new ResponseInfo(requestInfo, error, content, dataNode); }
Source:
START public void writeTo(AsyncWritableChannel channel, Callback<Long> callback) throws IOException { END prepareBuffer(); channel.write(bufferToSend, callback); } 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.

Target:
public void writeTo(AsyncWritableChannel channel, Callback<Long> callback) { prepareBuffer(); channel.write(bufferToSend, callback); }
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. START jdkSslContext = sslConfig.sslHttp2SelfSign ? null : new JdkSslFactory(sslConfig).getSSLContext(); END this.endpointIdentification = sslConfig.sslEndpointIdentificationAlgorithm.isEmpty() ? null : sslConfig.sslEndpointIdentificationAlgorithm; } 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`.

Target:
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. this.endpointIdentification = sslConfig.sslEndpointIdentificationAlgorithm.isEmpty() ? null : sslConfig.sslEndpointIdentificationAlgorithm; }
Source:
public boolean isBootstrapInProgress() { START throw new UnsupportedOperationException("Method not supported"); END } Does this need to be implemented for live-serving mode?

Target:
public boolean isBootstrapInProgress() { return false; }
Source:
boolean isBootstrapInProgress() { START File bootstrapFile = new File(dataDir, BOOTSTRAP_FILE_NAME); END return bootstrapFile.exists(); } can combine two lines

Target:
boolean isBootstrapInProgress() { return (new File(dataDir, BOOTSTRAP_FILE_NAME)).exists(); }
Source:
public Future<Long> write(ByteBuffer src, Callback<Long> callback) { START if (src == null) { END 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; } could you replace this impl with a call to `write(Unpooled.wrappedBuffer(src), callback)`?

Target:
public Future<Long> write(ByteBuffer src, Callback<Long> callback) { if (src == null) { throw new IllegalArgumentException("Source buffer cannot be null"); } return write(Unpooled.wrappedBuffer(src), callback); }
Source:
public void uploadFile(String containerName, String fileName, InputStream inputStream) throws BlobStorageException, IOException { try { BlobContainerClient containerClient = getContainer(containerName, true); START BlockBlobClient blobClient = containerClient.getBlobClient(fileName).getBlockBlobClient(); END blobClient.uploadWithResponse(inputStream, inputStream.available(), null, null, null, null, null, null, Context.NONE); } catch (UncheckedIOException e) { // error processing input stream throw e.getCause(); } } Could the method `getBlockBlobClient(String containerName, String blobName, boolean autoCreateContainer)` be used here?

Target:
public void uploadFile(String containerName, String fileName, InputStream inputStream) throws BlobStorageException, IOException { try { BlockBlobClient blobClient = getBlockBlobClient(containerName, fileName, true); blobClient.uploadWithResponse(inputStream, inputStream.available(), null, null, null, null, null, null, Context.NONE); } catch (UncheckedIOException e) { // error processing input stream throw e.getCause(); } }
Source:
START String getAzureContainerName(String partitionPath) { END // 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(); } minor: can be a private method.

Target:
private 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(); }
Source:
START String getAzureBlobName(BlobId blobId) { END // 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; } minor: can be private

Target:
private 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; }
Source:
public void testEarlyReadableStreamChannelClose() throws Exception { for (int numChunksInBlob = 0; numChunksInBlob <= 4; numChunksInBlob++) { for (int numChunksToRead = 0; numChunksToRead < numChunksInBlob; numChunksToRead++) { START System.out.println("" + numChunksInBlob + " chunks but only read " + numChunksToRead); END testEarlyReadableStreamChannelClose(numChunksInBlob, numChunksToRead); } } } 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.

Target:
public void testEarlyReadableStreamChannelClose() throws Exception { for (int numChunksInBlob = 0; numChunksInBlob <= 4; numChunksInBlob++) { for (int numChunksToRead = 0; numChunksToRead < numChunksInBlob; numChunksToRead++) { testEarlyReadableStreamChannelClose(numChunksInBlob, numChunksToRead); } } }
Source:
public void stop() { if (started.compareAndSet(true, false)) { if (scheduler != null) { Utils.shutDownExecutorService(scheduler, config.nettyMetricsStopWaitTimeoutSeconds, TimeUnit.SECONDS); START logger.trace("De-scheduled for collecting netty metrics"); END } } } INFO level

Target:
public void stop() { if (started.compareAndSet(true, false)) { if (scheduler != null) { Utils.shutDownExecutorService(scheduler, config.nettyMetricsStopWaitTimeoutSeconds, TimeUnit.SECONDS); logger.info("De-scheduled for collecting netty metrics"); } } }
Source:
public void connect() throws IOException { synchronized (lock) { if (!connected) { socket = createSocket(); writeChannel = Channels.newChannel(socket.getOutputStream()); readChannel = socket.getInputStream(); connected = true; logger.debug( START "Connection established to {}({}):{}", END host, socket.getRemoteSocketAddress(), socket.getPort() ); } } } Could you format the code in intellij?

Target:
public void connect() throws IOException { synchronized (this) { if (!connected) { socket = createSocket(); writeChannel = Channels.newChannel(socket.getOutputStream()); readChannel = socket.getInputStream(); connected = true; logger.debug("Connection established to {}({}):{}", host, socket.getRemoteSocketAddress(), socket.getPort()); } } }
Source:
START public static List<CloudBlobMetadata> capMetadataListBySize(List<CloudBlobMetadata> originalList, long size) { END 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; } 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.

Target:
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.isEmpty()) { // We must add at least one regardless of size cappedList.add(metadata); } break; } cappedList.add(metadata); totalSize += metadata.getSize(); } return cappedList; }
Source:
public ByteBuffer getAndRelease() { START return buffer; END } if this is supposed to "release" the buffer, should the `buffer` field be set to null?

Target:
public ByteBuffer getAndRelease() { try { return buffer; } finally { buffer = null; } }
Source:
START public ByteBuf getAndRelease() { END if (released.compareAndSet(false, true)) { try { return buffer.retainedDuplicate(); } finally { buffer.release(); } } else { throw new IllegalStateException("Payload already released"); } } 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()`

Target:
public ByteBuf getAndRelease() { if (buffer == null) { return null; } else { try { return buffer.retainedDuplicate(); } finally { buffer.release(); buffer = null; } } }
Source:
public void clearReceive() { if (networkReceive != null) { START ReferenceCountUtil.release(networkReceive.getReceivedBytes().getAndRelease()); END } networkReceive = null; } 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`

Target:
public void clearReceive() { networkReceive = null; }
Source:
public boolean isReadComplete() { START return !(buffer == null || sizeRead < sizeToRead); END } Maybe clearer to use AND and no NOT.

Target:
public boolean isReadComplete() { return buffer != null && sizeRead >= sizeToRead; }
Source:
private ConcurrentHashMap<String, PartitionId> mapPartitionNameToPartition(ClusterMap clusterMap, DataNodeId localNode) { START List<? extends ReplicaId> localReplicas = clusterMap.getReplicaIds(localNode); END return localReplicas.stream() .collect(Collectors.toMap(replicaId -> replicaId.getPartitionId().toPathString(), ReplicaId::getPartitionId, (e1, e2) -> e2, ConcurrentHashMap::new)); } Can fold this line into next one.

Target:
private ConcurrentHashMap<String, PartitionId> mapPartitionNameToPartition(ClusterMap clusterMap, DataNodeId localNode) { return clusterMap.getReplicaIds(localNode) .stream() .collect(Collectors.toMap(replicaId -> replicaId.getPartitionId().toPathString(), ReplicaId::getPartitionId, (e1, e2) -> e2, ConcurrentHashMap::new)); }
Source:
public void onPartitionStateChangeToStandbyFromLeader(String partitionName) { START logger.debug("Partition state change notification from Leader to Standby received for partition {}", END 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); } } } Do you know in which case, it may throw exception?

Target:
public void onPartitionStateChangeToStandbyFromLeader(String partitionName) { logger.info("Partition state change notification from Leader to Standby received for partition {}", partitionName); synchronized (notificationLock) { removeCloudReplica(partitionName); } }
Source:
public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) { try { START logger.trace("Live instance change triggered from with: {}", dcName, liveInstances); END 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; } } trace seems too low, how often will this happen?

Target:
public void onLiveInstanceChange(List<LiveInstance> liveInstances, NotificationContext changeContext) { try { logger.debug("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; } }
Source:
public void onRoutingTableChange(RoutingTableSnapshot routingTableSnapshot, Object context) { logger.info("Routing table change triggered from {}", dcName); START synchronized (notificationLock) { END dcToRoutingTableSnapshotRef.get(dcName).getAndSet(routingTableSnapshot); helixClusterManagerMetrics.routingTableChangeTriggerCount.inc(); } } Yeah, I'm also not sure why this needs extra sync.

Target:
public void onRoutingTableChange(RoutingTableSnapshot routingTableSnapshot, Object context) { logger.info("Routing table change triggered from {}", dcName); dcToRoutingTableSnapshotRef.get(dcName).getAndSet(routingTableSnapshot); helixClusterManagerMetrics.routingTableChangeTriggerCount.inc(); }
Source:
public DeleteSubRecord getDeleteSubRecord() { START if (subRecord.getType().equals(SubRecord.Type.DELETE)) return (DeleteSubRecord) subRecord; return null; END } nit: put around if statement.

Target:
public DeleteSubRecord getDeleteSubRecord() { if (subRecord.getType().equals(SubRecord.Type.DELETE)) { return (DeleteSubRecord) subRecord; } return null; }
Source:
public MessageInfo(StoreKey key, long size, boolean deleted, boolean ttlUpdated, long expirationTimeInMs, short accountId, short containerId, long operationTimeMs) { START this(key, size, deleted, ttlUpdated, expirationTimeInMs, null, accountId, containerId, operationTimeMs, (short) -1); END } 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.

Target:
public MessageInfo(StoreKey key, long size, boolean deleted, boolean ttlUpdated, long expirationTimeInMs, short accountId, short containerId, long operationTimeMs) { this(key, size, deleted, ttlUpdated, false, expirationTimeInMs, null, accountId, containerId, operationTimeMs, (short) 0); }